blob: 96d55b8204e3fcd9b6f189cdf33c8a97c7942ee3 [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
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000087/* #define WXS_ELEM_DECL_CONS_ENABLED */
88
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000089#ifdef DEBUG_IDC
90 #ifndef DEBUG_IDC_NODE_TABLE
91 #define DEBUG_IDC_NODE_TABLE
92 #endif
Daniel Veillarddee23482008-04-11 12:58:43 +000093#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000094
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000095/* #define ENABLE_PARTICLE_RESTRICTION 1 */
96
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000097#define ENABLE_REDEFINE
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000098
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000099/* #define ENABLE_NAMED_LOCALS */
100
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +0000101/* #define ENABLE_IDC_NODE_TABLES_TEST */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000102
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000103#define DUMP_CONTENT_MODEL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000104
105#ifdef LIBXML_READER_ENABLED
106/* #define XML_SCHEMA_READER_ENABLED */
107#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +0000108
Daniel Veillard4255d502002-04-16 15:50:10 +0000109#define UNBOUNDED (1 << 30)
110#define TODO \
111 xmlGenericError(xmlGenericErrorContext, \
112 "Unimplemented block at %s:%d\n", \
113 __FILE__, __LINE__);
114
William M. Brack2f2a6632004-08-20 23:09:47 +0000115#define XML_SCHEMAS_NO_NAMESPACE (const xmlChar *) "##"
Daniel Veillardc0826a72004-08-10 14:17:33 +0000116
Daniel Veillard4255d502002-04-16 15:50:10 +0000117/*
118 * The XML Schemas namespaces
119 */
120static const xmlChar *xmlSchemaNs = (const xmlChar *)
121 "http://www.w3.org/2001/XMLSchema";
122
123static const xmlChar *xmlSchemaInstanceNs = (const xmlChar *)
124 "http://www.w3.org/2001/XMLSchema-instance";
125
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000126static const xmlChar *xmlNamespaceNs = (const xmlChar *)
127 "http://www.w3.org/2000/xmlns/";
128
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000129/*
130* Come casting macros.
131*/
132#define ACTXT_CAST (xmlSchemaAbstractCtxtPtr)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +0000133#define PCTXT_CAST (xmlSchemaParserCtxtPtr)
134#define VCTXT_CAST (xmlSchemaValidCtxtPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000135#define WXS_BASIC_CAST (xmlSchemaBasicItemPtr)
136#define WXS_TREE_CAST (xmlSchemaTreeItemPtr)
137#define WXS_PTC_CAST (xmlSchemaParticlePtr)
138#define WXS_TYPE_CAST (xmlSchemaTypePtr)
139#define WXS_ELEM_CAST (xmlSchemaElementPtr)
140#define WXS_ATTR_GROUP_CAST (xmlSchemaAttributeGroupPtr)
141#define WXS_ATTR_CAST (xmlSchemaAttributePtr)
142#define WXS_ATTR_USE_CAST (xmlSchemaAttributeUsePtr)
143#define WXS_ATTR_PROHIB_CAST (xmlSchemaAttributeUseProhibPtr)
144#define WXS_MODEL_GROUPDEF_CAST (xmlSchemaModelGroupDefPtr)
145#define WXS_MODEL_GROUP_CAST (xmlSchemaModelGroupPtr)
146#define WXS_IDC_CAST (xmlSchemaIDCPtr)
147#define WXS_QNAME_CAST (xmlSchemaQNameRefPtr)
148#define WXS_LIST_CAST (xmlSchemaItemListPtr)
Daniel Veillardc0826a72004-08-10 14:17:33 +0000149
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000150/*
151* Macros to query common properties of components.
152*/
153#define WXS_ITEM_NODE(i) xmlSchemaGetComponentNode(WXS_BASIC_CAST (i))
Daniel Veillard4255d502002-04-16 15:50:10 +0000154
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000155#define WXS_ITEM_TYPE_NAME(i) xmlSchemaGetComponentTypeStr(WXS_BASIC_CAST (i))
156/*
157* Macros for element declarations.
158*/
159#define WXS_ELEM_TYPEDEF(e) (e)->subtypes
Daniel Veillardc0826a72004-08-10 14:17:33 +0000160
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000161#define WXS_SUBST_HEAD(item) (item)->refDecl
162/*
163* Macros for attribute declarations.
164*/
165#define WXS_ATTR_TYPEDEF(a) (a)->subtypes
166/*
167* Macros for attribute uses.
168*/
169#define WXS_ATTRUSE_DECL(au) WXS_ATTR_CAST (WXS_ATTR_USE_CAST (au))->attrDecl
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000170
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000171#define WXS_ATTRUSE_TYPEDEF(au) WXS_ATTR_TYPEDEF(WXS_ATTRUSE_DECL( WXS_ATTR_USE_CAST au))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000172
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000173#define WXS_ATTRUSE_DECL_NAME(au) (WXS_ATTRUSE_DECL(au))->name
174
175#define WXS_ATTRUSE_DECL_TNS(au) (WXS_ATTRUSE_DECL(au))->targetNamespace
176/*
177* Macros for attribute groups.
178*/
179#define WXS_ATTR_GROUP_HAS_REFS(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS)
180#define WXS_ATTR_GROUP_EXPANDED(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED)
181/*
182* Macros for particles.
183*/
184#define WXS_PARTICLE(p) WXS_PTC_CAST (p)
185
186#define WXS_PARTICLE_TERM(p) (WXS_PARTICLE(p))->children
187
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +0000188#define WXS_PARTICLE_TERM_AS_ELEM(p) (WXS_ELEM_CAST WXS_PARTICLE_TERM(p))
189
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000190#define WXS_PARTICLE_MODEL(p) WXS_MODEL_GROUP_CAST WXS_PARTICLE(p)->children
191/*
192* Macros for model groups definitions.
193*/
194#define WXS_MODELGROUPDEF_MODEL(mgd) (WXS_MODEL_GROUP_CAST (mgd))->children
195/*
196* Macros for model groups.
197*/
198#define WXS_IS_MODEL_GROUP(i) \
199 (((i)->type == XML_SCHEMA_TYPE_SEQUENCE) || \
200 ((i)->type == XML_SCHEMA_TYPE_CHOICE) || \
201 ((i)->type == XML_SCHEMA_TYPE_ALL))
202
203#define WXS_MODELGROUP_PARTICLE(mg) WXS_PTC_CAST (mg)->children
204/*
205* Macros for schema buckets.
206*/
207#define WXS_IS_BUCKET_INCREDEF(t) (((t) == XML_SCHEMA_SCHEMA_INCLUDE) || \
208 ((t) == XML_SCHEMA_SCHEMA_REDEFINE))
209
210#define WXS_IS_BUCKET_IMPMAIN(t) (((t) == XML_SCHEMA_SCHEMA_MAIN) || \
211 ((t) == XML_SCHEMA_SCHEMA_IMPORT))
212
213#define WXS_IMPBUCKET(b) ((xmlSchemaImportPtr) (b))
214
215#define WXS_INCBUCKET(b) ((xmlSchemaIncludePtr) (b))
216/*
217* Macros for complex/simple types.
218*/
219#define WXS_IS_ANYTYPE(i) \
220 (( (i)->type == XML_SCHEMA_TYPE_BASIC) && \
221 ( (WXS_TYPE_CAST (i))->builtInType == XML_SCHEMAS_ANYTYPE))
222
223#define WXS_IS_COMPLEX(i) \
224 (((i)->type == XML_SCHEMA_TYPE_COMPLEX) || \
225 ((i)->builtInType == XML_SCHEMAS_ANYTYPE))
226
227#define WXS_IS_SIMPLE(item) \
228 ((item->type == XML_SCHEMA_TYPE_SIMPLE) || \
229 ((item->type == XML_SCHEMA_TYPE_BASIC) && \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000230 (item->builtInType != XML_SCHEMAS_ANYTYPE)))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000231
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000232#define WXS_IS_ANY_SIMPLE_TYPE(i) \
233 (((i)->type == XML_SCHEMA_TYPE_BASIC) && \
234 ((i)->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000235
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000236#define WXS_IS_RESTRICTION(t) \
237 ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000238
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000239#define WXS_IS_EXTENSION(t) \
240 ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION)
241
242#define WXS_IS_TYPE_NOT_FIXED(i) \
243 (((i)->type != XML_SCHEMA_TYPE_BASIC) && \
244 (((i)->flags & XML_SCHEMAS_TYPE_INTERNAL_RESOLVED) == 0))
245
246#define WXS_IS_TYPE_NOT_FIXED_1(item) \
247 (((item)->type != XML_SCHEMA_TYPE_BASIC) && \
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000248 (((item)->flags & XML_SCHEMAS_TYPE_FIXUP_1) == 0))
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +0000249
250#define WXS_TYPE_IS_GLOBAL(t) ((t)->flags & XML_SCHEMAS_TYPE_GLOBAL)
251
252#define WXS_TYPE_IS_LOCAL(t) (((t)->flags & XML_SCHEMAS_TYPE_GLOBAL) == 0)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000253/*
254* Macros for exclusively for complex types.
255*/
256#define WXS_HAS_COMPLEX_CONTENT(item) \
257 ((item->contentType == XML_SCHEMA_CONTENT_MIXED) || \
258 (item->contentType == XML_SCHEMA_CONTENT_EMPTY) || \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000259 (item->contentType == XML_SCHEMA_CONTENT_ELEMENTS))
260
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000261#define WXS_HAS_SIMPLE_CONTENT(item) \
262 ((item->contentType == XML_SCHEMA_CONTENT_SIMPLE) || \
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000263 (item->contentType == XML_SCHEMA_CONTENT_BASIC))
264
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000265#define WXS_HAS_MIXED_CONTENT(item) \
266 (item->contentType == XML_SCHEMA_CONTENT_MIXED)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000267
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000268#define WXS_EMPTIABLE(t) \
269 (xmlSchemaIsParticleEmptiable(WXS_PTC_CAST (t)->subtypes))
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000270
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000271#define WXS_TYPE_CONTENTTYPE(t) (t)->subtypes
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000272
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000273#define WXS_TYPE_PARTICLE(t) WXS_PTC_CAST (t)->subtypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000274
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000275#define WXS_TYPE_PARTICLE_TERM(t) WXS_PARTICLE_TERM(WXS_TYPE_PARTICLE(t))
276/*
277* Macros for exclusively for simple types.
278*/
279#define WXS_LIST_ITEMTYPE(t) (t)->subtypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000280
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000281#define WXS_IS_ATOMIC(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_ATOMIC)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000282
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000283#define WXS_IS_LIST(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_LIST)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000284
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000285#define WXS_IS_UNION(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_UNION)
286/*
287* Misc parser context macros.
288*/
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000289#define WXS_CONSTRUCTOR(ctx) (ctx)->constructor
290
291#define WXS_HAS_BUCKETS(ctx) \
292( (WXS_CONSTRUCTOR((ctx))->buckets != NULL) && \
293(WXS_CONSTRUCTOR((ctx))->buckets->nbItems > 0) )
294
295#define WXS_SUBST_GROUPS(ctx) WXS_CONSTRUCTOR((ctx))->substGroups
296
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000297#define WXS_BUCKET(ctx) WXS_CONSTRUCTOR((ctx))->bucket
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000298
299#define WXS_SCHEMA(ctx) (ctx)->schema
300
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000301#define WXS_ADD_LOCAL(ctx, item) \
302 xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->locals), 10, item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000303
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000304#define WXS_ADD_GLOBAL(ctx, item) \
305 xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->globals), 5, item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000306
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000307#define WXS_ADD_PENDING(ctx, item) \
308 xmlSchemaAddItemSize(&((ctx)->constructor->pending), 10, item)
309/*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000310* xmlSchemaItemList macros.
311*/
312#define WXS_ILIST_IS_EMPTY(l) ((l == NULL) || ((l)->nbItems == 0))
313/*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000314* Misc macros.
315*/
316#define IS_SCHEMA(node, type) \
317 ((node != NULL) && (node->ns != NULL) && \
318 (xmlStrEqual(node->name, (const xmlChar *) type)) && \
319 (xmlStrEqual(node->ns->href, xmlSchemaNs)))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000320
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000321#define FREE_AND_NULL(str) if ((str) != NULL) { xmlFree((xmlChar *) (str)); str = NULL; }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000322
323/*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000324* Since we put the default/fixed values into the dict, we can
325* use pointer comparison for those values.
326* REMOVED: (xmlStrEqual((v1), (v2)))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000327*/
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000328#define WXS_ARE_DEFAULT_STR_EQUAL(v1, v2) ((v1) == (v2))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000329
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000330#define INODE_NILLED(item) (item->flags & XML_SCHEMA_ELEM_INFO_NILLED)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000331
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000332#define CAN_PARSE_SCHEMA(b) (((b)->doc != NULL) && ((b)->parsed == 0))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000333
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000334#define HFAILURE if (res == -1) goto exit_failure;
335
336#define HERROR if (res != 0) goto exit_error;
337
338#define HSTOP(ctx) if ((ctx)->stop) goto exit;
339/*
340* Some flags used for various schema constraints.
341*/
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +0000342#define SUBSET_RESTRICTION 1<<0
343#define SUBSET_EXTENSION 1<<1
344#define SUBSET_SUBSTITUTION 1<<2
345#define SUBSET_LIST 1<<3
346#define SUBSET_UNION 1<<4
347
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000348typedef struct _xmlSchemaNodeInfo xmlSchemaNodeInfo;
349typedef xmlSchemaNodeInfo *xmlSchemaNodeInfoPtr;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000350
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000351typedef struct _xmlSchemaItemList xmlSchemaItemList;
352typedef xmlSchemaItemList *xmlSchemaItemListPtr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000353struct _xmlSchemaItemList {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000354 void **items; /* used for dynamic addition of schemata */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000355 int nbItems; /* used for dynamic addition of schemata */
356 int sizeItems; /* used for dynamic addition of schemata */
357};
358
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000359#define XML_SCHEMA_CTXT_PARSER 1
360#define XML_SCHEMA_CTXT_VALIDATOR 2
361
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000362typedef struct _xmlSchemaAbstractCtxt xmlSchemaAbstractCtxt;
363typedef xmlSchemaAbstractCtxt *xmlSchemaAbstractCtxtPtr;
364struct _xmlSchemaAbstractCtxt {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000365 int type; /* E.g. XML_SCHEMA_CTXT_VALIDATOR */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000366};
367
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000368typedef struct _xmlSchemaBucket xmlSchemaBucket;
369typedef xmlSchemaBucket *xmlSchemaBucketPtr;
370
371#define XML_SCHEMA_SCHEMA_MAIN 0
372#define XML_SCHEMA_SCHEMA_IMPORT 1
373#define XML_SCHEMA_SCHEMA_INCLUDE 2
374#define XML_SCHEMA_SCHEMA_REDEFINE 3
375
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000376/**
Daniel Veillarddee23482008-04-11 12:58:43 +0000377 * xmlSchemaSchemaRelation:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000378 *
379 * Used to create a graph of schema relationships.
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000380 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000381typedef struct _xmlSchemaSchemaRelation xmlSchemaSchemaRelation;
382typedef xmlSchemaSchemaRelation *xmlSchemaSchemaRelationPtr;
383struct _xmlSchemaSchemaRelation {
384 xmlSchemaSchemaRelationPtr next;
385 int type; /* E.g. XML_SCHEMA_SCHEMA_IMPORT */
386 const xmlChar *importNamespace;
387 xmlSchemaBucketPtr bucket;
388};
389
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000390#define XML_SCHEMA_BUCKET_MARKED 1<<0
391#define XML_SCHEMA_BUCKET_COMPS_ADDED 1<<1
392
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000393struct _xmlSchemaBucket {
394 int type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000395 int flags;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000396 const xmlChar *schemaLocation;
397 const xmlChar *origTargetNamespace;
398 const xmlChar *targetNamespace;
399 xmlDocPtr doc;
400 xmlSchemaSchemaRelationPtr relations;
401 int located;
402 int parsed;
403 int imported;
404 int preserveDoc;
Daniel Veillarddee23482008-04-11 12:58:43 +0000405 xmlSchemaItemListPtr globals; /* Global components. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000406 xmlSchemaItemListPtr locals; /* Local components. */
407};
408
409/**
Daniel Veillarddee23482008-04-11 12:58:43 +0000410 * xmlSchemaImport:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000411 * (extends xmlSchemaBucket)
412 *
413 * Reflects a schema. Holds some information
414 * about the schema and its toplevel components. Duplicate
415 * toplevel components are not checked at this level.
416 */
417typedef struct _xmlSchemaImport xmlSchemaImport;
418typedef xmlSchemaImport *xmlSchemaImportPtr;
419struct _xmlSchemaImport {
420 int type; /* Main OR import OR include. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000421 int flags;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000422 const xmlChar *schemaLocation; /* The URI of the schema document. */
423 /* For chameleon includes, @origTargetNamespace will be NULL */
424 const xmlChar *origTargetNamespace;
Daniel Veillarddee23482008-04-11 12:58:43 +0000425 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000426 * For chameleon includes, @targetNamespace will be the
Daniel Veillarddee23482008-04-11 12:58:43 +0000427 * targetNamespace of the including schema.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000428 */
429 const xmlChar *targetNamespace;
430 xmlDocPtr doc; /* The schema node-tree. */
431 /* @relations will hold any included/imported/redefined schemas. */
432 xmlSchemaSchemaRelationPtr relations;
433 int located;
434 int parsed;
435 int imported;
436 int preserveDoc;
437 xmlSchemaItemListPtr globals;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000438 xmlSchemaItemListPtr locals;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000439 /* The imported schema. */
440 xmlSchemaPtr schema;
441};
442
443/*
444* (extends xmlSchemaBucket)
445*/
446typedef struct _xmlSchemaInclude xmlSchemaInclude;
447typedef xmlSchemaInclude *xmlSchemaIncludePtr;
448struct _xmlSchemaInclude {
449 int type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000450 int flags;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000451 const xmlChar *schemaLocation;
452 const xmlChar *origTargetNamespace;
453 const xmlChar *targetNamespace;
454 xmlDocPtr doc;
455 xmlSchemaSchemaRelationPtr relations;
456 int located;
457 int parsed;
458 int imported;
459 int preserveDoc;
Daniel Veillarddee23482008-04-11 12:58:43 +0000460 xmlSchemaItemListPtr globals; /* Global components. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000461 xmlSchemaItemListPtr locals; /* Local components. */
462
463 /* The owning main or import schema bucket. */
464 xmlSchemaImportPtr ownerImport;
465};
466
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000467/**
468 * xmlSchemaBasicItem:
469 *
470 * The abstract base type for schema components.
471 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000472typedef struct _xmlSchemaBasicItem xmlSchemaBasicItem;
473typedef xmlSchemaBasicItem *xmlSchemaBasicItemPtr;
474struct _xmlSchemaBasicItem {
475 xmlSchemaTypeType type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000476};
477
478/**
479 * xmlSchemaAnnotItem:
480 *
481 * The abstract base type for annotated schema components.
482 * (Extends xmlSchemaBasicItem)
483 */
484typedef struct _xmlSchemaAnnotItem xmlSchemaAnnotItem;
485typedef xmlSchemaAnnotItem *xmlSchemaAnnotItemPtr;
486struct _xmlSchemaAnnotItem {
487 xmlSchemaTypeType type;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000488 xmlSchemaAnnotPtr annot;
489};
490
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000491/**
492 * xmlSchemaTreeItem:
493 *
494 * The abstract base type for tree-like structured schema components.
495 * (Extends xmlSchemaAnnotItem)
496 */
497typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem;
498typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr;
499struct _xmlSchemaTreeItem {
500 xmlSchemaTypeType type;
501 xmlSchemaAnnotPtr annot;
502 xmlSchemaTreeItemPtr next;
503 xmlSchemaTreeItemPtr children;
504};
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000505
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000506
507#define XML_SCHEMA_ATTR_USE_FIXED 1<<0
508/**
509 * xmlSchemaAttributeUsePtr:
510 *
511 * The abstract base type for tree-like structured schema components.
512 * (Extends xmlSchemaTreeItem)
513 */
514typedef struct _xmlSchemaAttributeUse xmlSchemaAttributeUse;
515typedef xmlSchemaAttributeUse *xmlSchemaAttributeUsePtr;
516struct _xmlSchemaAttributeUse {
517 xmlSchemaTypeType type;
518 xmlSchemaAnnotPtr annot;
519 xmlSchemaAttributeUsePtr next; /* The next attr. use. */
Daniel Veillarddee23482008-04-11 12:58:43 +0000520 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000521 * The attr. decl. OR a QName-ref. to an attr. decl. OR
522 * a QName-ref. to an attribute group definition.
523 */
524 xmlSchemaAttributePtr attrDecl;
525
526 int flags;
527 xmlNodePtr node;
528 int occurs; /* required, optional */
529 const xmlChar * defValue;
530 xmlSchemaValPtr defVal;
531};
532
533/**
534 * xmlSchemaAttributeUseProhibPtr:
535 *
536 * A helper component to reflect attribute prohibitions.
537 * (Extends xmlSchemaBasicItem)
538 */
539typedef struct _xmlSchemaAttributeUseProhib xmlSchemaAttributeUseProhib;
540typedef xmlSchemaAttributeUseProhib *xmlSchemaAttributeUseProhibPtr;
541struct _xmlSchemaAttributeUseProhib {
542 xmlSchemaTypeType type; /* == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB */
543 xmlNodePtr node;
544 const xmlChar *name;
545 const xmlChar *targetNamespace;
546 int isRef;
547};
548
549/**
550 * xmlSchemaRedef:
551 */
552typedef struct _xmlSchemaRedef xmlSchemaRedef;
553typedef xmlSchemaRedef *xmlSchemaRedefPtr;
554struct _xmlSchemaRedef {
555 xmlSchemaRedefPtr next;
556 xmlSchemaBasicItemPtr item; /* The redefining component. */
557 xmlSchemaBasicItemPtr reference; /* The referencing component. */
558 xmlSchemaBasicItemPtr target; /* The to-be-redefined component. */
559 const xmlChar *refName; /* The name of the to-be-redefined component. */
560 const xmlChar *refTargetNs; /* The target namespace of the
561 to-be-redefined comp. */
562 xmlSchemaBucketPtr targetBucket; /* The redefined schema. */
563};
564
565/**
566 * xmlSchemaConstructionCtxt:
567 */
568typedef struct _xmlSchemaConstructionCtxt xmlSchemaConstructionCtxt;
569typedef xmlSchemaConstructionCtxt *xmlSchemaConstructionCtxtPtr;
570struct _xmlSchemaConstructionCtxt {
571 xmlSchemaPtr mainSchema; /* The main schema. */
572 xmlSchemaBucketPtr mainBucket; /* The main schema bucket */
573 xmlDictPtr dict;
574 xmlSchemaItemListPtr buckets; /* List of schema buckets. */
575 /* xmlSchemaItemListPtr relations; */ /* List of schema relations. */
Daniel Veillarddee23482008-04-11 12:58:43 +0000576 xmlSchemaBucketPtr bucket; /* The current schema bucket */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000577 xmlSchemaItemListPtr pending; /* All Components of all schemas that
578 need to be fixed. */
579 xmlHashTablePtr substGroups;
580 xmlSchemaRedefPtr redefs;
581 xmlSchemaRedefPtr lastRedef;
582};
583
584#define XML_SCHEMAS_PARSE_ERROR 1
585#define SCHEMAS_PARSE_OPTIONS XML_PARSE_NOENT
586
587struct _xmlSchemaParserCtxt {
588 int type;
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +0000589 void *errCtxt; /* user specific error context */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000590 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
591 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
Kasimier T. Buchcikdfbc33a2006-01-03 10:51:59 +0000592 int err;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000593 int nberrors;
594 xmlStructuredErrorFunc serror;
595
596 xmlSchemaConstructionCtxtPtr constructor;
597 int ownsConstructor; /* TODO: Move this to parser *flags*. */
598
599 /* xmlSchemaPtr topschema; */
600 /* xmlHashTablePtr namespaces; */
601
602 xmlSchemaPtr schema; /* The main schema in use */
603 int counter;
604
605 const xmlChar *URL;
606 xmlDocPtr doc;
607 int preserve; /* Whether the doc should be freed */
608
609 const char *buffer;
610 int size;
611
612 /*
613 * Used to build complex element content models
614 */
615 xmlAutomataPtr am;
616 xmlAutomataStatePtr start;
617 xmlAutomataStatePtr end;
618 xmlAutomataStatePtr state;
619
620 xmlDictPtr dict; /* dictionnary for interned string names */
621 xmlSchemaTypePtr ctxtType; /* The current context simple/complex type */
622 int options;
623 xmlSchemaValidCtxtPtr vctxt;
624 int isS4S;
625 int isRedefine;
626 int xsiAssemble;
627 int stop; /* If the parser should stop; i.e. a critical error. */
628 const xmlChar *targetNamespace;
629 xmlSchemaBucketPtr redefined; /* The schema to be redefined. */
630
631 xmlSchemaRedefPtr redef; /* Used for redefinitions. */
Daniel Veillarddee23482008-04-11 12:58:43 +0000632 int redefCounter; /* Used for redefinitions. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000633 xmlSchemaItemListPtr attrProhibs;
634};
635
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000636/**
637 * xmlSchemaQNameRef:
638 *
639 * A component reference item (not a schema component)
640 * (Extends xmlSchemaBasicItem)
641 */
642typedef struct _xmlSchemaQNameRef xmlSchemaQNameRef;
643typedef xmlSchemaQNameRef *xmlSchemaQNameRefPtr;
644struct _xmlSchemaQNameRef {
645 xmlSchemaTypeType type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000646 xmlSchemaBasicItemPtr item; /* The resolved referenced item. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000647 xmlSchemaTypeType itemType;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000648 const xmlChar *name;
649 const xmlChar *targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000650 xmlNodePtr node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000651};
652
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000653/**
654 * xmlSchemaParticle:
655 *
656 * A particle component.
657 * (Extends xmlSchemaTreeItem)
658 */
659typedef struct _xmlSchemaParticle xmlSchemaParticle;
660typedef xmlSchemaParticle *xmlSchemaParticlePtr;
661struct _xmlSchemaParticle {
662 xmlSchemaTypeType type;
663 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000664 xmlSchemaTreeItemPtr next; /* next particle */
665 xmlSchemaTreeItemPtr children; /* the "term" (e.g. a model group,
666 a group definition, a XML_SCHEMA_EXTRA_QNAMEREF (if a reference),
667 etc.) */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000668 int minOccurs;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000669 int maxOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000670 xmlNodePtr node;
671};
672
673/**
674 * xmlSchemaModelGroup:
675 *
676 * A model group component.
677 * (Extends xmlSchemaTreeItem)
678 */
679typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup;
680typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr;
681struct _xmlSchemaModelGroup {
682 xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_SEQUENCE, XML_SCHEMA_TYPE_CHOICE, XML_SCHEMA_TYPE_ALL */
683 xmlSchemaAnnotPtr annot;
684 xmlSchemaTreeItemPtr next; /* not used */
685 xmlSchemaTreeItemPtr children; /* first particle (OR "element decl" OR "wildcard") */
686 xmlNodePtr node;
687};
688
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000689#define XML_SCHEMA_MODEL_GROUP_DEF_MARKED 1<<0
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000690#define XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED 1<<1
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000691/**
692 * xmlSchemaModelGroupDef:
693 *
694 * A model group definition component.
695 * (Extends xmlSchemaTreeItem)
696 */
697typedef struct _xmlSchemaModelGroupDef xmlSchemaModelGroupDef;
698typedef xmlSchemaModelGroupDef *xmlSchemaModelGroupDefPtr;
699struct _xmlSchemaModelGroupDef {
700 xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_GROUP */
701 xmlSchemaAnnotPtr annot;
702 xmlSchemaTreeItemPtr next; /* not used */
703 xmlSchemaTreeItemPtr children; /* the "model group" */
704 const xmlChar *name;
705 const xmlChar *targetNamespace;
706 xmlNodePtr node;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000707 int flags;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000708};
709
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000710typedef struct _xmlSchemaIDC xmlSchemaIDC;
711typedef xmlSchemaIDC *xmlSchemaIDCPtr;
712
713/**
714 * xmlSchemaIDCSelect:
715 *
716 * The identity-constraint "field" and "selector" item, holding the
717 * XPath expression.
718 */
719typedef struct _xmlSchemaIDCSelect xmlSchemaIDCSelect;
720typedef xmlSchemaIDCSelect *xmlSchemaIDCSelectPtr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000721struct _xmlSchemaIDCSelect {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000722 xmlSchemaIDCSelectPtr next;
723 xmlSchemaIDCPtr idc;
724 int index; /* an index position if significant for IDC key-sequences */
725 const xmlChar *xpath; /* the XPath expression */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000726 void *xpathComp; /* the compiled XPath expression */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000727};
728
729/**
730 * xmlSchemaIDC:
731 *
732 * The identity-constraint definition component.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000733 * (Extends xmlSchemaAnnotItem)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000734 */
735
736struct _xmlSchemaIDC {
737 xmlSchemaTypeType type;
738 xmlSchemaAnnotPtr annot;
739 xmlSchemaIDCPtr next;
740 xmlNodePtr node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000741 const xmlChar *name;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000742 const xmlChar *targetNamespace;
743 xmlSchemaIDCSelectPtr selector;
744 xmlSchemaIDCSelectPtr fields;
745 int nbFields;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000746 xmlSchemaQNameRefPtr ref;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000747};
748
749/**
750 * xmlSchemaIDCAug:
751 *
752 * The augmented IDC information used for validation.
753 */
754typedef struct _xmlSchemaIDCAug xmlSchemaIDCAug;
755typedef xmlSchemaIDCAug *xmlSchemaIDCAugPtr;
756struct _xmlSchemaIDCAug {
757 xmlSchemaIDCAugPtr next; /* next in a list */
758 xmlSchemaIDCPtr def; /* the IDC definition */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000759 int keyrefDepth; /* the lowest tree level to which IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000760 tables need to be bubbled upwards */
761};
762
763/**
764 * xmlSchemaPSVIIDCKeySequence:
765 *
766 * The key sequence of a node table item.
767 */
768typedef struct _xmlSchemaPSVIIDCKey xmlSchemaPSVIIDCKey;
769typedef xmlSchemaPSVIIDCKey *xmlSchemaPSVIIDCKeyPtr;
770struct _xmlSchemaPSVIIDCKey {
771 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000772 xmlSchemaValPtr val;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000773};
774
775/**
776 * xmlSchemaPSVIIDCNode:
777 *
778 * The node table item of a node table.
779 */
780typedef struct _xmlSchemaPSVIIDCNode xmlSchemaPSVIIDCNode;
781typedef xmlSchemaPSVIIDCNode *xmlSchemaPSVIIDCNodePtr;
782struct _xmlSchemaPSVIIDCNode {
783 xmlNodePtr node;
784 xmlSchemaPSVIIDCKeyPtr *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000785 int nodeLine;
Daniel Veillarddee23482008-04-11 12:58:43 +0000786 int nodeQNameID;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000787
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000788};
789
790/**
791 * xmlSchemaPSVIIDCBinding:
792 *
793 * The identity-constraint binding item of the [identity-constraint table].
794 */
795typedef struct _xmlSchemaPSVIIDCBinding xmlSchemaPSVIIDCBinding;
796typedef xmlSchemaPSVIIDCBinding *xmlSchemaPSVIIDCBindingPtr;
797struct _xmlSchemaPSVIIDCBinding {
798 xmlSchemaPSVIIDCBindingPtr next; /* next binding of a specific node */
799 xmlSchemaIDCPtr definition; /* the IDC definition */
800 xmlSchemaPSVIIDCNodePtr *nodeTable; /* array of key-sequences */
801 int nbNodes; /* number of entries in the node table */
802 int sizeNodes; /* size of the node table */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000803 xmlSchemaItemListPtr dupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000804};
805
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000806
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000807#define XPATH_STATE_OBJ_TYPE_IDC_SELECTOR 1
808#define XPATH_STATE_OBJ_TYPE_IDC_FIELD 2
809
810#define XPATH_STATE_OBJ_MATCHES -2
811#define XPATH_STATE_OBJ_BLOCKED -3
812
813typedef struct _xmlSchemaIDCMatcher xmlSchemaIDCMatcher;
814typedef xmlSchemaIDCMatcher *xmlSchemaIDCMatcherPtr;
815
816/**
817 * xmlSchemaIDCStateObj:
818 *
819 * The state object used to evaluate XPath expressions.
820 */
821typedef struct _xmlSchemaIDCStateObj xmlSchemaIDCStateObj;
822typedef xmlSchemaIDCStateObj *xmlSchemaIDCStateObjPtr;
823struct _xmlSchemaIDCStateObj {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000824 int type;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000825 xmlSchemaIDCStateObjPtr next; /* next if in a list */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000826 int depth; /* depth of creation */
827 int *history; /* list of (depth, state-id) tuples */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000828 int nbHistory;
829 int sizeHistory;
830 xmlSchemaIDCMatcherPtr matcher; /* the correspondent field/selector
831 matcher */
832 xmlSchemaIDCSelectPtr sel;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000833 void *xpathCtxt;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000834};
835
836#define IDC_MATCHER 0
837
838/**
839 * xmlSchemaIDCMatcher:
840 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000841 * Used to evaluate IDC selectors (and fields).
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000842 */
843struct _xmlSchemaIDCMatcher {
844 int type;
845 int depth; /* the tree depth at creation time */
846 xmlSchemaIDCMatcherPtr next; /* next in the list */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +0000847 xmlSchemaIDCMatcherPtr nextCached; /* next in the cache list */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000848 xmlSchemaIDCAugPtr aidc; /* the augmented IDC item */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000849 int idcType;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000850 xmlSchemaPSVIIDCKeyPtr **keySeqs; /* the key-sequences of the target
851 elements */
852 int sizeKeySeqs;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000853 xmlSchemaItemListPtr targets; /* list of target-node
854 (xmlSchemaPSVIIDCNodePtr) entries */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000855};
856
857/*
858* Element info flags.
859*/
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000860#define XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES 1<<0
861#define XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES 1<<1
862#define XML_SCHEMA_ELEM_INFO_NILLED 1<<2
863#define XML_SCHEMA_ELEM_INFO_LOCAL_TYPE 1<<3
864
865#define XML_SCHEMA_NODE_INFO_VALUE_NEEDED 1<<4
866#define XML_SCHEMA_ELEM_INFO_EMPTY 1<<5
867#define XML_SCHEMA_ELEM_INFO_HAS_CONTENT 1<<6
868
869#define XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT 1<<7
870#define XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT 1<<8
871#define XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED 1<<9
872#define XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE 1<<10
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000873
874/**
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000875 * xmlSchemaNodeInfo:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000876 *
877 * Holds information of an element node.
878 */
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000879struct _xmlSchemaNodeInfo {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000880 int nodeType;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000881 xmlNodePtr node;
Daniel Veillarddee23482008-04-11 12:58:43 +0000882 int nodeLine;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000883 const xmlChar *localName;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000884 const xmlChar *nsName;
885 const xmlChar *value;
886 xmlSchemaValPtr val; /* the pre-computed value if any */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000887 xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000888
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000889 int flags; /* combination of node info flags */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000890
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000891 int valNeeded;
892 int normVal;
893
894 xmlSchemaElementPtr decl; /* the element/attribute declaration */
895 int depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000896 xmlSchemaPSVIIDCBindingPtr idcTable; /* the table of PSVI IDC bindings
897 for the scope element*/
898 xmlSchemaIDCMatcherPtr idcMatchers; /* the IDC matchers for the scope
899 element */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000900 xmlRegExecCtxtPtr regexCtxt;
901
902 const xmlChar **nsBindings; /* Namespace bindings on this element */
903 int nbNsBindings;
Daniel Veillarddee23482008-04-11 12:58:43 +0000904 int sizeNsBindings;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000905
906 int hasKeyrefs;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +0000907 int appliedXPath; /* Indicates that an XPath has been applied. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000908};
909
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000910#define XML_SCHEMAS_ATTR_UNKNOWN 1
911#define XML_SCHEMAS_ATTR_ASSESSED 2
912#define XML_SCHEMAS_ATTR_PROHIBITED 3
913#define XML_SCHEMAS_ATTR_ERR_MISSING 4
914#define XML_SCHEMAS_ATTR_INVALID_VALUE 5
915#define XML_SCHEMAS_ATTR_ERR_NO_TYPE 6
916#define XML_SCHEMAS_ATTR_ERR_FIXED_VALUE 7
917#define XML_SCHEMAS_ATTR_DEFAULT 8
918#define XML_SCHEMAS_ATTR_VALIDATE_VALUE 9
919#define XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL 10
920#define XML_SCHEMAS_ATTR_HAS_ATTR_USE 11
921#define XML_SCHEMAS_ATTR_HAS_ATTR_DECL 12
922#define XML_SCHEMAS_ATTR_WILD_SKIP 13
923#define XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL 14
924#define XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID 15
925#define XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID 16
926#define XML_SCHEMAS_ATTR_META 17
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000927/*
928* @metaType values of xmlSchemaAttrInfo.
929*/
930#define XML_SCHEMA_ATTR_INFO_META_XSI_TYPE 1
931#define XML_SCHEMA_ATTR_INFO_META_XSI_NIL 2
932#define XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC 3
933#define XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC 4
934#define XML_SCHEMA_ATTR_INFO_META_XMLNS 5
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000935
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000936typedef struct _xmlSchemaAttrInfo xmlSchemaAttrInfo;
937typedef xmlSchemaAttrInfo *xmlSchemaAttrInfoPtr;
938struct _xmlSchemaAttrInfo {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000939 int nodeType;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000940 xmlNodePtr node;
Daniel Veillarddee23482008-04-11 12:58:43 +0000941 int nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000942 const xmlChar *localName;
943 const xmlChar *nsName;
944 const xmlChar *value;
945 xmlSchemaValPtr val; /* the pre-computed value if any */
946 xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
947 int flags; /* combination of node info flags */
948
949 xmlSchemaAttributePtr decl; /* the attribute declaration */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000950 xmlSchemaAttributeUsePtr use; /* the attribute use */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000951 int state;
952 int metaType;
953 const xmlChar *vcValue; /* the value constraint value */
954 xmlSchemaNodeInfoPtr parent;
955};
956
957
958#define XML_SCHEMA_VALID_CTXT_FLAG_STREAM 1
Daniel Veillard4255d502002-04-16 15:50:10 +0000959/**
960 * xmlSchemaValidCtxt:
961 *
962 * A Schemas validation context
963 */
Daniel Veillard4255d502002-04-16 15:50:10 +0000964struct _xmlSchemaValidCtxt {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000965 int type;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +0000966 void *errCtxt; /* user specific data block */
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000967 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000968 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000969 xmlStructuredErrorFunc serror;
Daniel Veillard4255d502002-04-16 15:50:10 +0000970
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000971 xmlSchemaPtr schema; /* The schema in use */
972 xmlDocPtr doc;
Daniel Veillard4255d502002-04-16 15:50:10 +0000973 xmlParserInputBufferPtr input;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000974 xmlCharEncoding enc;
975 xmlSAXHandlerPtr sax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000976 xmlParserCtxtPtr parserCtxt;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000977 void *user_data; /* TODO: What is this for? */
Daniel Veillard4255d502002-04-16 15:50:10 +0000978
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000979 int err;
980 int nberrors;
Daniel Veillard4255d502002-04-16 15:50:10 +0000981
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000982 xmlNodePtr node;
983 xmlNodePtr cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000984 /* xmlSchemaTypePtr type; */
Daniel Veillard4255d502002-04-16 15:50:10 +0000985
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000986 xmlRegExecCtxtPtr regexp;
987 xmlSchemaValPtr value;
Daniel Veillard4255d502002-04-16 15:50:10 +0000988
Daniel Veillardc0826a72004-08-10 14:17:33 +0000989 int valueWS;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000990 int options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000991 xmlNodePtr validationRoot;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000992 xmlSchemaParserCtxtPtr pctxt;
993 int xsiAssemble;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000994
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000995 int depth;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000996 xmlSchemaNodeInfoPtr *elemInfos; /* array of element informations */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000997 int sizeElemInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000998 xmlSchemaNodeInfoPtr inode; /* the current element information */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000999
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00001000 xmlSchemaIDCAugPtr aidcs; /* a list of augmented IDC informations */
1001
1002 xmlSchemaIDCStateObjPtr xpathStates; /* first active state object. */
1003 xmlSchemaIDCStateObjPtr xpathStatePool; /* first stored state object. */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +00001004 xmlSchemaIDCMatcherPtr idcMatcherCache; /* Cache for IDC matcher objects. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001005
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00001006 xmlSchemaPSVIIDCNodePtr *idcNodes; /* list of all IDC node-table entries*/
1007 int nbIdcNodes;
1008 int sizeIdcNodes;
1009
1010 xmlSchemaPSVIIDCKeyPtr *idcKeys; /* list of all IDC node-table entries */
1011 int nbIdcKeys;
1012 int sizeIdcKeys;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001013
1014 int flags;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001015
1016 xmlDictPtr dict;
1017
Daniel Veillard39e5c892005-07-03 22:48:50 +00001018#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001019 xmlTextReaderPtr reader;
Daniel Veillard39e5c892005-07-03 22:48:50 +00001020#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001021
1022 xmlSchemaAttrInfoPtr *attrInfos;
1023 int nbAttrInfos;
1024 int sizeAttrInfos;
1025
1026 int skipDepth;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00001027 xmlSchemaItemListPtr nodeQNames;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00001028 int hasKeyrefs;
1029 int createIDCNodeTables;
1030 int psviExposeIDCNodeTables;
Daniel Veillard4255d502002-04-16 15:50:10 +00001031};
1032
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00001033/**
1034 * xmlSchemaSubstGroup:
1035 *
1036 *
1037 */
1038typedef struct _xmlSchemaSubstGroup xmlSchemaSubstGroup;
1039typedef xmlSchemaSubstGroup *xmlSchemaSubstGroupPtr;
1040struct _xmlSchemaSubstGroup {
1041 xmlSchemaElementPtr head;
1042 xmlSchemaItemListPtr members;
1043};
1044
Daniel Veillard4255d502002-04-16 15:50:10 +00001045/************************************************************************
1046 * *
1047 * Some predeclarations *
1048 * *
1049 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001050
Daniel Veillardbd2904b2003-11-25 15:38:59 +00001051static int xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt,
1052 xmlSchemaPtr schema,
1053 xmlNodePtr node);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00001054static int xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr ctxt,
1055 xmlSchemaPtr schema,
1056 xmlNodePtr node);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001057static int
1058xmlSchemaTypeFixup(xmlSchemaTypePtr type,
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +00001059 xmlSchemaAbstractCtxtPtr ctxt);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00001060static const xmlChar *
Daniel Veillard01fa6152004-06-29 17:04:39 +00001061xmlSchemaFacetTypeToString(xmlSchemaTypeType type);
1062static int
William M. Brack2f2a6632004-08-20 23:09:47 +00001063xmlSchemaParseImport(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1064 xmlNodePtr node);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001065static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001066xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
1067 xmlSchemaParserCtxtPtr ctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +00001068static void
1069xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001070static xmlSchemaWhitespaceValueType
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001071xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001072static xmlSchemaTreeItemPtr
1073xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1074 xmlNodePtr node, xmlSchemaTypeType type,
1075 int withParticle);
1076static const xmlChar *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001077xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001078static xmlSchemaTypeLinkPtr
1079xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001080static void
1081xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
1082 const char *funcName,
1083 const char *message);
1084static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +00001085xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001086 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001087 xmlSchemaTypePtr baseType,
1088 int subset);
1089static void
1090xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001091 xmlSchemaParserCtxtPtr ctxt);
1092static void
1093xmlSchemaComponentListFree(xmlSchemaItemListPtr list);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001094static xmlSchemaQNameRefPtr
1095xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
1096 xmlSchemaPtr schema,
1097 xmlNodePtr node);
William M. Brack87640d52004-04-17 14:58:15 +00001098
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001099/************************************************************************
1100 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001101 * Helper functions *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001102 * *
1103 ************************************************************************/
1104
1105/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001106 * xmlSchemaItemTypeToStr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001107 * @type: the type of the schema item
1108 *
1109 * Returns the component name of a schema item.
1110 */
1111static const xmlChar *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001112xmlSchemaItemTypeToStr(xmlSchemaTypeType type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001113{
1114 switch (type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001115 case XML_SCHEMA_TYPE_BASIC:
1116 return(BAD_CAST "simple type definition");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001117 case XML_SCHEMA_TYPE_SIMPLE:
1118 return(BAD_CAST "simple type definition");
1119 case XML_SCHEMA_TYPE_COMPLEX:
1120 return(BAD_CAST "complex type definition");
1121 case XML_SCHEMA_TYPE_ELEMENT:
1122 return(BAD_CAST "element declaration");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001123 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1124 return(BAD_CAST "attribute use");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001125 case XML_SCHEMA_TYPE_ATTRIBUTE:
1126 return(BAD_CAST "attribute declaration");
1127 case XML_SCHEMA_TYPE_GROUP:
1128 return(BAD_CAST "model group definition");
1129 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1130 return(BAD_CAST "attribute group definition");
1131 case XML_SCHEMA_TYPE_NOTATION:
1132 return(BAD_CAST "notation declaration");
1133 case XML_SCHEMA_TYPE_SEQUENCE:
1134 return(BAD_CAST "model group (sequence)");
1135 case XML_SCHEMA_TYPE_CHOICE:
1136 return(BAD_CAST "model group (choice)");
1137 case XML_SCHEMA_TYPE_ALL:
1138 return(BAD_CAST "model group (all)");
1139 case XML_SCHEMA_TYPE_PARTICLE:
1140 return(BAD_CAST "particle");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001141 case XML_SCHEMA_TYPE_IDC_UNIQUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001142 return(BAD_CAST "unique identity-constraint");
1143 /* return(BAD_CAST "IDC (unique)"); */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001144 case XML_SCHEMA_TYPE_IDC_KEY:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001145 return(BAD_CAST "key identity-constraint");
1146 /* return(BAD_CAST "IDC (key)"); */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001147 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001148 return(BAD_CAST "keyref identity-constraint");
1149 /* return(BAD_CAST "IDC (keyref)"); */
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00001150 case XML_SCHEMA_TYPE_ANY:
1151 return(BAD_CAST "wildcard (any)");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001152 case XML_SCHEMA_EXTRA_QNAMEREF:
1153 return(BAD_CAST "[helper component] QName reference");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001154 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
1155 return(BAD_CAST "[helper component] attribute use prohibition");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001156 default:
1157 return(BAD_CAST "Not a schema component");
1158 }
1159}
1160
1161/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001162 * xmlSchemaGetComponentTypeStr:
1163 * @type: the type of the schema item
1164 *
1165 * Returns the component name of a schema item.
1166 */
1167static const xmlChar *
1168xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item)
1169{
1170 switch (item->type) {
1171 case XML_SCHEMA_TYPE_BASIC:
1172 if (WXS_IS_COMPLEX(WXS_TYPE_CAST item))
1173 return(BAD_CAST "complex type definition");
1174 else
Daniel Veillarddee23482008-04-11 12:58:43 +00001175 return(BAD_CAST "simple type definition");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001176 default:
1177 return(xmlSchemaItemTypeToStr(item->type));
1178 }
1179}
1180
1181/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001182 * xmlSchemaGetComponentNode:
1183 * @item: a schema component
1184 *
1185 * Returns node associated with the schema component.
1186 * NOTE that such a node need not be available; plus, a component's
1187 * node need not to reflect the component directly, since there is no
1188 * one-to-one relationship between the XML Schema representation and
1189 * the component representation.
1190 */
1191static xmlNodePtr
1192xmlSchemaGetComponentNode(xmlSchemaBasicItemPtr item)
1193{
1194 switch (item->type) {
1195 case XML_SCHEMA_TYPE_ELEMENT:
1196 return (((xmlSchemaElementPtr) item)->node);
1197 case XML_SCHEMA_TYPE_ATTRIBUTE:
1198 return (((xmlSchemaAttributePtr) item)->node);
1199 case XML_SCHEMA_TYPE_COMPLEX:
1200 case XML_SCHEMA_TYPE_SIMPLE:
1201 return (((xmlSchemaTypePtr) item)->node);
1202 case XML_SCHEMA_TYPE_ANY:
1203 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1204 return (((xmlSchemaWildcardPtr) item)->node);
1205 case XML_SCHEMA_TYPE_PARTICLE:
1206 return (((xmlSchemaParticlePtr) item)->node);
1207 case XML_SCHEMA_TYPE_SEQUENCE:
1208 case XML_SCHEMA_TYPE_CHOICE:
1209 case XML_SCHEMA_TYPE_ALL:
1210 return (((xmlSchemaModelGroupPtr) item)->node);
1211 case XML_SCHEMA_TYPE_GROUP:
1212 return (((xmlSchemaModelGroupDefPtr) item)->node);
1213 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1214 return (((xmlSchemaAttributeGroupPtr) item)->node);
1215 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1216 case XML_SCHEMA_TYPE_IDC_KEY:
1217 case XML_SCHEMA_TYPE_IDC_KEYREF:
1218 return (((xmlSchemaIDCPtr) item)->node);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001219 case XML_SCHEMA_EXTRA_QNAMEREF:
1220 return(((xmlSchemaQNameRefPtr) item)->node);
1221 /* TODO: What to do with NOTATIONs?
1222 case XML_SCHEMA_TYPE_NOTATION:
1223 return (((xmlSchemaNotationPtr) item)->node);
1224 */
1225 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1226 return (((xmlSchemaAttributeUsePtr) item)->node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001227 default:
1228 return (NULL);
1229 }
1230}
1231
1232#if 0
1233/**
1234 * xmlSchemaGetNextComponent:
1235 * @item: a schema component
1236 *
1237 * Returns the next sibling of the schema component.
1238 */
1239static xmlSchemaBasicItemPtr
1240xmlSchemaGetNextComponent(xmlSchemaBasicItemPtr item)
1241{
1242 switch (item->type) {
1243 case XML_SCHEMA_TYPE_ELEMENT:
1244 return ((xmlSchemaBasicItemPtr) ((xmlSchemaElementPtr) item)->next);
1245 case XML_SCHEMA_TYPE_ATTRIBUTE:
1246 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributePtr) item)->next);
1247 case XML_SCHEMA_TYPE_COMPLEX:
1248 case XML_SCHEMA_TYPE_SIMPLE:
1249 return ((xmlSchemaBasicItemPtr) ((xmlSchemaTypePtr) item)->next);
1250 case XML_SCHEMA_TYPE_ANY:
1251 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1252 return (NULL);
1253 case XML_SCHEMA_TYPE_PARTICLE:
1254 return ((xmlSchemaBasicItemPtr) ((xmlSchemaParticlePtr) item)->next);
1255 case XML_SCHEMA_TYPE_SEQUENCE:
1256 case XML_SCHEMA_TYPE_CHOICE:
1257 case XML_SCHEMA_TYPE_ALL:
1258 return (NULL);
1259 case XML_SCHEMA_TYPE_GROUP:
1260 return (NULL);
1261 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1262 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributeGroupPtr) item)->next);
1263 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1264 case XML_SCHEMA_TYPE_IDC_KEY:
1265 case XML_SCHEMA_TYPE_IDC_KEYREF:
1266 return ((xmlSchemaBasicItemPtr) ((xmlSchemaIDCPtr) item)->next);
1267 default:
1268 return (NULL);
1269 }
1270}
1271#endif
1272
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001273
1274/**
1275 * xmlSchemaFormatQName:
1276 * @buf: the string buffer
1277 * @namespaceName: the namespace name
1278 * @localName: the local name
1279 *
1280 * Returns the given QName in the format "{namespaceName}localName" or
1281 * just "localName" if @namespaceName is NULL.
1282 *
1283 * Returns the localName if @namespaceName is NULL, a formatted
1284 * string otherwise.
1285 */
1286static const xmlChar*
1287xmlSchemaFormatQName(xmlChar **buf,
1288 const xmlChar *namespaceName,
1289 const xmlChar *localName)
1290{
1291 FREE_AND_NULL(*buf)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001292 if (namespaceName != NULL) {
1293 *buf = xmlStrdup(BAD_CAST "{");
1294 *buf = xmlStrcat(*buf, namespaceName);
1295 *buf = xmlStrcat(*buf, BAD_CAST "}");
1296 }
1297 if (localName != NULL) {
1298 if (namespaceName == NULL)
1299 return(localName);
1300 *buf = xmlStrcat(*buf, localName);
1301 } else {
1302 *buf = xmlStrcat(*buf, BAD_CAST "(NULL)");
Daniel Veillarddee23482008-04-11 12:58:43 +00001303 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001304 return ((const xmlChar *) *buf);
1305}
1306
Daniel Veillarddee23482008-04-11 12:58:43 +00001307static const xmlChar*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001308xmlSchemaFormatQNameNs(xmlChar **buf, xmlNsPtr ns, const xmlChar *localName)
1309{
1310 if (ns != NULL)
1311 return (xmlSchemaFormatQName(buf, ns->href, localName));
1312 else
1313 return (xmlSchemaFormatQName(buf, NULL, localName));
1314}
1315
1316static const xmlChar *
1317xmlSchemaGetComponentName(xmlSchemaBasicItemPtr item)
1318{
1319 switch (item->type) {
1320 case XML_SCHEMA_TYPE_ELEMENT:
1321 return (((xmlSchemaElementPtr) item)->name);
1322 case XML_SCHEMA_TYPE_ATTRIBUTE:
1323 return (((xmlSchemaAttributePtr) item)->name);
1324 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1325 return (((xmlSchemaAttributeGroupPtr) item)->name);
1326 case XML_SCHEMA_TYPE_BASIC:
1327 case XML_SCHEMA_TYPE_SIMPLE:
1328 case XML_SCHEMA_TYPE_COMPLEX:
1329 return (((xmlSchemaTypePtr) item)->name);
1330 case XML_SCHEMA_TYPE_GROUP:
1331 return (((xmlSchemaModelGroupDefPtr) item)->name);
1332 case XML_SCHEMA_TYPE_IDC_KEY:
1333 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1334 case XML_SCHEMA_TYPE_IDC_KEYREF:
1335 return (((xmlSchemaIDCPtr) item)->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001336 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1337 if (WXS_ATTRUSE_DECL(item) != NULL) {
1338 return(xmlSchemaGetComponentName(
1339 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1340 } else
1341 return(NULL);
1342 case XML_SCHEMA_EXTRA_QNAMEREF:
1343 return (((xmlSchemaQNameRefPtr) item)->name);
1344 case XML_SCHEMA_TYPE_NOTATION:
1345 return (((xmlSchemaNotationPtr) item)->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001346 default:
1347 /*
1348 * Other components cannot have names.
1349 */
1350 break;
1351 }
1352 return (NULL);
1353}
1354
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001355#define xmlSchemaGetQNameRefName(r) (WXS_QNAME_CAST (r))->name
1356#define xmlSchemaGetQNameRefTargetNs(r) (WXS_QNAME_CAST (r))->targetNamespace
1357/*
1358static const xmlChar *
1359xmlSchemaGetQNameRefName(void *ref)
1360{
1361 return(((xmlSchemaQNameRefPtr) ref)->name);
1362}
1363
1364static const xmlChar *
1365xmlSchemaGetQNameRefTargetNs(void *ref)
1366{
1367 return(((xmlSchemaQNameRefPtr) ref)->targetNamespace);
1368}
1369*/
1370
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001371static const xmlChar *
1372xmlSchemaGetComponentTargetNs(xmlSchemaBasicItemPtr item)
1373{
1374 switch (item->type) {
1375 case XML_SCHEMA_TYPE_ELEMENT:
1376 return (((xmlSchemaElementPtr) item)->targetNamespace);
1377 case XML_SCHEMA_TYPE_ATTRIBUTE:
1378 return (((xmlSchemaAttributePtr) item)->targetNamespace);
1379 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1380 return (((xmlSchemaAttributeGroupPtr) item)->targetNamespace);
1381 case XML_SCHEMA_TYPE_BASIC:
1382 return (BAD_CAST "http://www.w3.org/2001/XMLSchema");
1383 case XML_SCHEMA_TYPE_SIMPLE:
1384 case XML_SCHEMA_TYPE_COMPLEX:
1385 return (((xmlSchemaTypePtr) item)->targetNamespace);
1386 case XML_SCHEMA_TYPE_GROUP:
1387 return (((xmlSchemaModelGroupDefPtr) item)->targetNamespace);
1388 case XML_SCHEMA_TYPE_IDC_KEY:
1389 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1390 case XML_SCHEMA_TYPE_IDC_KEYREF:
1391 return (((xmlSchemaIDCPtr) item)->targetNamespace);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001392 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1393 if (WXS_ATTRUSE_DECL(item) != NULL) {
1394 return(xmlSchemaGetComponentTargetNs(
1395 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1396 }
1397 /* TODO: Will returning NULL break something? */
1398 break;
1399 case XML_SCHEMA_EXTRA_QNAMEREF:
1400 return (((xmlSchemaQNameRefPtr) item)->targetNamespace);
1401 case XML_SCHEMA_TYPE_NOTATION:
1402 return (((xmlSchemaNotationPtr) item)->targetNamespace);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001403 default:
1404 /*
1405 * Other components cannot have names.
1406 */
1407 break;
1408 }
1409 return (NULL);
1410}
1411
1412static const xmlChar*
1413xmlSchemaGetComponentQName(xmlChar **buf,
1414 void *item)
1415{
1416 return (xmlSchemaFormatQName(buf,
1417 xmlSchemaGetComponentTargetNs((xmlSchemaBasicItemPtr) item),
1418 xmlSchemaGetComponentName((xmlSchemaBasicItemPtr) item)));
1419}
1420
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001421static const xmlChar*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001422xmlSchemaGetComponentDesignation(xmlChar **buf, void *item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001423{
1424 xmlChar *str = NULL;
1425
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001426 *buf = xmlStrcat(*buf, WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001427 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001428 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str,
1429 (xmlSchemaBasicItemPtr) item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001430 *buf = xmlStrcat(*buf, BAD_CAST "'");
1431 FREE_AND_NULL(str);
1432 return(*buf);
1433}
1434
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001435static const xmlChar*
1436xmlSchemaGetIDCDesignation(xmlChar **buf, xmlSchemaIDCPtr idc)
1437{
1438 return(xmlSchemaGetComponentDesignation(buf, idc));
1439}
1440
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001441/**
1442 * xmlSchemaWildcardPCToString:
1443 * @pc: the type of processContents
1444 *
1445 * Returns a string representation of the type of
1446 * processContents.
1447 */
1448static const xmlChar *
1449xmlSchemaWildcardPCToString(int pc)
1450{
1451 switch (pc) {
1452 case XML_SCHEMAS_ANY_SKIP:
1453 return (BAD_CAST "skip");
1454 case XML_SCHEMAS_ANY_LAX:
1455 return (BAD_CAST "lax");
1456 case XML_SCHEMAS_ANY_STRICT:
1457 return (BAD_CAST "strict");
1458 default:
1459 return (BAD_CAST "invalid process contents");
1460 }
1461}
1462
1463/**
1464 * xmlSchemaGetCanonValueWhtspExt:
1465 * @val: the precomputed value
1466 * @retValue: the returned value
1467 * @ws: the whitespace type of the value
1468 *
1469 * Get a the cononical representation of the value.
1470 * The caller has to free the returned retValue.
1471 *
1472 * Returns 0 if the value could be built and -1 in case of
1473 * API errors or if the value type is not supported yet.
1474 */
1475static int
1476xmlSchemaGetCanonValueWhtspExt(xmlSchemaValPtr val,
1477 xmlSchemaWhitespaceValueType ws,
1478 xmlChar **retValue)
1479{
1480 int list;
1481 xmlSchemaValType valType;
1482 const xmlChar *value, *value2 = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00001483
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001484
1485 if ((retValue == NULL) || (val == NULL))
1486 return (-1);
1487 list = xmlSchemaValueGetNext(val) ? 1 : 0;
1488 *retValue = NULL;
1489 do {
Daniel Veillarddee23482008-04-11 12:58:43 +00001490 value = NULL;
1491 valType = xmlSchemaGetValType(val);
1492 switch (valType) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001493 case XML_SCHEMAS_STRING:
1494 case XML_SCHEMAS_NORMSTRING:
1495 case XML_SCHEMAS_ANYSIMPLETYPE:
1496 value = xmlSchemaValueGetAsString(val);
1497 if (value != NULL) {
1498 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
1499 value2 = xmlSchemaCollapseString(value);
1500 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
1501 value2 = xmlSchemaWhiteSpaceReplace(value);
1502 if (value2 != NULL)
1503 value = value2;
1504 }
Daniel Veillarddee23482008-04-11 12:58:43 +00001505 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001506 default:
1507 if (xmlSchemaGetCanonValue(val, &value2) == -1) {
1508 if (value2 != NULL)
1509 xmlFree((xmlChar *) value2);
1510 goto internal_error;
1511 }
1512 value = value2;
1513 }
1514 if (*retValue == NULL)
1515 if (value == NULL) {
1516 if (! list)
1517 *retValue = xmlStrdup(BAD_CAST "");
1518 } else
1519 *retValue = xmlStrdup(value);
1520 else if (value != NULL) {
1521 /* List. */
1522 *retValue = xmlStrcat((xmlChar *) *retValue, BAD_CAST " ");
1523 *retValue = xmlStrcat((xmlChar *) *retValue, value);
1524 }
1525 FREE_AND_NULL(value2)
1526 val = xmlSchemaValueGetNext(val);
1527 } while (val != NULL);
1528
1529 return (0);
1530internal_error:
1531 if (*retValue != NULL)
1532 xmlFree((xmlChar *) (*retValue));
1533 if (value2 != NULL)
1534 xmlFree((xmlChar *) value2);
1535 return (-1);
1536}
1537
1538/**
1539 * xmlSchemaFormatItemForReport:
1540 * @buf: the string buffer
1541 * @itemDes: the designation of the item
1542 * @itemName: the name of the item
Daniel Veillarddee23482008-04-11 12:58:43 +00001543 * @item: the item as an object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001544 * @itemNode: the node of the item
1545 * @local: the local name
1546 * @parsing: if the function is used during the parse
1547 *
1548 * Returns a representation of the given item used
Daniel Veillarddee23482008-04-11 12:58:43 +00001549 * for error reports.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001550 *
Daniel Veillarddee23482008-04-11 12:58:43 +00001551 * The following order is used to build the resulting
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001552 * designation if the arguments are not NULL:
1553 * 1a. If itemDes not NULL -> itemDes
1554 * 1b. If (itemDes not NULL) and (itemName not NULL)
1555 * -> itemDes + itemName
1556 * 2. If the preceding was NULL and (item not NULL) -> item
1557 * 3. If the preceding was NULL and (itemNode not NULL) -> itemNode
Daniel Veillarddee23482008-04-11 12:58:43 +00001558 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001559 * If the itemNode is an attribute node, the name of the attribute
1560 * will be appended to the result.
1561 *
1562 * Returns the formatted string and sets @buf to the resulting value.
Daniel Veillarddee23482008-04-11 12:58:43 +00001563 */
1564static xmlChar*
1565xmlSchemaFormatItemForReport(xmlChar **buf,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001566 const xmlChar *itemDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001567 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001568 xmlNodePtr itemNode)
1569{
1570 xmlChar *str = NULL;
1571 int named = 1;
1572
1573 if (*buf != NULL) {
1574 xmlFree(*buf);
1575 *buf = NULL;
1576 }
Daniel Veillarddee23482008-04-11 12:58:43 +00001577
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001578 if (itemDes != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +00001579 *buf = xmlStrdup(itemDes);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001580 } else if (item != NULL) {
1581 switch (item->type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001582 case XML_SCHEMA_TYPE_BASIC: {
1583 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1584
1585 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001586 *buf = xmlStrdup(BAD_CAST "atomic type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001587 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001588 *buf = xmlStrdup(BAD_CAST "list type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001589 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001590 *buf = xmlStrdup(BAD_CAST "union type 'xs:");
1591 else
1592 *buf = xmlStrdup(BAD_CAST "simple type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001593 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001594 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001595 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001596 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001597 case XML_SCHEMA_TYPE_SIMPLE: {
1598 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1599
1600 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001601 *buf = xmlStrdup(BAD_CAST"");
1602 } else {
1603 *buf = xmlStrdup(BAD_CAST "local ");
1604 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001605 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001606 *buf = xmlStrcat(*buf, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001607 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001608 *buf = xmlStrcat(*buf, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001609 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001610 *buf = xmlStrcat(*buf, BAD_CAST "union type");
1611 else
1612 *buf = xmlStrcat(*buf, BAD_CAST "simple type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001613 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001614 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001615 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001616 *buf = xmlStrcat(*buf, BAD_CAST "'");
1617 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001618 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001619 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001620 case XML_SCHEMA_TYPE_COMPLEX: {
1621 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1622
1623 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001624 *buf = xmlStrdup(BAD_CAST "");
1625 else
1626 *buf = xmlStrdup(BAD_CAST "local ");
1627 *buf = xmlStrcat(*buf, BAD_CAST "complex type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001628 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001629 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001630 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001631 *buf = xmlStrcat(*buf, BAD_CAST "'");
1632 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001633 }
1634 break;
1635 case XML_SCHEMA_TYPE_ATTRIBUTE_USE: {
1636 xmlSchemaAttributeUsePtr ause;
Daniel Veillarddee23482008-04-11 12:58:43 +00001637
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001638 ause = WXS_ATTR_USE_CAST item;
1639 *buf = xmlStrdup(BAD_CAST "attribute use ");
1640 if (WXS_ATTRUSE_DECL(ause) != NULL) {
1641 *buf = xmlStrcat(*buf, BAD_CAST "'");
1642 *buf = xmlStrcat(*buf,
1643 xmlSchemaGetComponentQName(&str, WXS_ATTRUSE_DECL(ause)));
1644 FREE_AND_NULL(str)
1645 *buf = xmlStrcat(*buf, BAD_CAST "'");
1646 } else {
1647 *buf = xmlStrcat(*buf, BAD_CAST "(unknown)");
1648 }
1649 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001650 break;
1651 case XML_SCHEMA_TYPE_ATTRIBUTE: {
1652 xmlSchemaAttributePtr attr;
Daniel Veillarddee23482008-04-11 12:58:43 +00001653
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001654 attr = (xmlSchemaAttributePtr) item;
1655 *buf = xmlStrdup(BAD_CAST "attribute decl.");
1656 *buf = xmlStrcat(*buf, BAD_CAST " '");
1657 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1658 attr->targetNamespace, attr->name));
1659 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001660 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001661 }
1662 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001663 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1664 xmlSchemaGetComponentDesignation(buf, item);
1665 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001666 case XML_SCHEMA_TYPE_ELEMENT: {
1667 xmlSchemaElementPtr elem;
1668
Daniel Veillarddee23482008-04-11 12:58:43 +00001669 elem = (xmlSchemaElementPtr) item;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001670 *buf = xmlStrdup(BAD_CAST "element decl.");
1671 *buf = xmlStrcat(*buf, BAD_CAST " '");
1672 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1673 elem->targetNamespace, elem->name));
1674 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001675 }
1676 break;
1677 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1678 case XML_SCHEMA_TYPE_IDC_KEY:
Daniel Veillarddee23482008-04-11 12:58:43 +00001679 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001680 if (item->type == XML_SCHEMA_TYPE_IDC_UNIQUE)
1681 *buf = xmlStrdup(BAD_CAST "unique '");
1682 else if (item->type == XML_SCHEMA_TYPE_IDC_KEY)
1683 *buf = xmlStrdup(BAD_CAST "key '");
1684 else
1685 *buf = xmlStrdup(BAD_CAST "keyRef '");
1686 *buf = xmlStrcat(*buf, ((xmlSchemaIDCPtr) item)->name);
1687 *buf = xmlStrcat(*buf, BAD_CAST "'");
1688 break;
1689 case XML_SCHEMA_TYPE_ANY:
1690 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1691 *buf = xmlStrdup(xmlSchemaWildcardPCToString(
1692 ((xmlSchemaWildcardPtr) item)->processContents));
1693 *buf = xmlStrcat(*buf, BAD_CAST " wildcard");
1694 break;
1695 case XML_SCHEMA_FACET_MININCLUSIVE:
1696 case XML_SCHEMA_FACET_MINEXCLUSIVE:
1697 case XML_SCHEMA_FACET_MAXINCLUSIVE:
1698 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
1699 case XML_SCHEMA_FACET_TOTALDIGITS:
1700 case XML_SCHEMA_FACET_FRACTIONDIGITS:
1701 case XML_SCHEMA_FACET_PATTERN:
1702 case XML_SCHEMA_FACET_ENUMERATION:
1703 case XML_SCHEMA_FACET_WHITESPACE:
1704 case XML_SCHEMA_FACET_LENGTH:
1705 case XML_SCHEMA_FACET_MAXLENGTH:
1706 case XML_SCHEMA_FACET_MINLENGTH:
1707 *buf = xmlStrdup(BAD_CAST "facet '");
1708 *buf = xmlStrcat(*buf, xmlSchemaFacetTypeToString(item->type));
1709 *buf = xmlStrcat(*buf, BAD_CAST "'");
1710 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001711 case XML_SCHEMA_TYPE_GROUP: {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001712 *buf = xmlStrdup(BAD_CAST "model group def.");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001713 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001714 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001715 *buf = xmlStrcat(*buf, BAD_CAST "'");
1716 FREE_AND_NULL(str)
1717 }
1718 break;
1719 case XML_SCHEMA_TYPE_SEQUENCE:
1720 case XML_SCHEMA_TYPE_CHOICE:
1721 case XML_SCHEMA_TYPE_ALL:
1722 case XML_SCHEMA_TYPE_PARTICLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001723 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1724 break;
1725 case XML_SCHEMA_TYPE_NOTATION: {
1726 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1727 *buf = xmlStrcat(*buf, BAD_CAST " '");
1728 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
1729 *buf = xmlStrcat(*buf, BAD_CAST "'");
1730 FREE_AND_NULL(str);
1731 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001732 default:
1733 named = 0;
1734 }
Daniel Veillarddee23482008-04-11 12:58:43 +00001735 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001736 named = 0;
1737
1738 if ((named == 0) && (itemNode != NULL)) {
1739 xmlNodePtr elem;
1740
1741 if (itemNode->type == XML_ATTRIBUTE_NODE)
1742 elem = itemNode->parent;
Daniel Veillarddee23482008-04-11 12:58:43 +00001743 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001744 elem = itemNode;
1745 *buf = xmlStrdup(BAD_CAST "Element '");
1746 if (elem->ns != NULL) {
1747 *buf = xmlStrcat(*buf,
1748 xmlSchemaFormatQName(&str, elem->ns->href, elem->name));
1749 FREE_AND_NULL(str)
1750 } else
1751 *buf = xmlStrcat(*buf, elem->name);
1752 *buf = xmlStrcat(*buf, BAD_CAST "'");
Daniel Veillarddee23482008-04-11 12:58:43 +00001753
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001754 }
1755 if ((itemNode != NULL) && (itemNode->type == XML_ATTRIBUTE_NODE)) {
1756 *buf = xmlStrcat(*buf, BAD_CAST ", attribute '");
1757 if (itemNode->ns != NULL) {
1758 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1759 itemNode->ns->href, itemNode->name));
1760 FREE_AND_NULL(str)
1761 } else
1762 *buf = xmlStrcat(*buf, itemNode->name);
1763 *buf = xmlStrcat(*buf, BAD_CAST "'");
1764 }
1765 FREE_AND_NULL(str)
Daniel Veillarddee23482008-04-11 12:58:43 +00001766
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001767 return (*buf);
1768}
1769
1770/**
1771 * xmlSchemaFormatFacetEnumSet:
1772 * @buf: the string buffer
1773 * @type: the type holding the enumeration facets
1774 *
1775 * Builds a string consisting of all enumeration elements.
1776 *
1777 * Returns a string of all enumeration elements.
1778 */
1779static const xmlChar *
1780xmlSchemaFormatFacetEnumSet(xmlSchemaAbstractCtxtPtr actxt,
1781 xmlChar **buf, xmlSchemaTypePtr type)
1782{
1783 xmlSchemaFacetPtr facet;
1784 xmlSchemaWhitespaceValueType ws;
1785 xmlChar *value = NULL;
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +00001786 int res, found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001787
1788 if (*buf != NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +00001789 xmlFree(*buf);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001790 *buf = NULL;
1791
1792 do {
1793 /*
1794 * Use the whitespace type of the base type.
Daniel Veillarddee23482008-04-11 12:58:43 +00001795 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001796 ws = xmlSchemaGetWhiteSpaceFacetValue(type->baseType);
1797 for (facet = type->facets; facet != NULL; facet = facet->next) {
1798 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
1799 continue;
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +00001800 found = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001801 res = xmlSchemaGetCanonValueWhtspExt(facet->val,
1802 ws, &value);
1803 if (res == -1) {
1804 xmlSchemaInternalErr(actxt,
1805 "xmlSchemaFormatFacetEnumSet",
1806 "compute the canonical lexical representation");
1807 if (*buf != NULL)
1808 xmlFree(*buf);
1809 *buf = NULL;
1810 return (NULL);
1811 }
1812 if (*buf == NULL)
1813 *buf = xmlStrdup(BAD_CAST "'");
1814 else
1815 *buf = xmlStrcat(*buf, BAD_CAST ", '");
1816 *buf = xmlStrcat(*buf, BAD_CAST value);
1817 *buf = xmlStrcat(*buf, BAD_CAST "'");
1818 if (value != NULL) {
1819 xmlFree((xmlChar *)value);
1820 value = NULL;
1821 }
1822 }
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +00001823 /*
1824 * The enumeration facet of a type restricts the enumeration
1825 * facet of the ancestor type; i.e., such restricted enumerations
1826 * do not belong to the set of the given type. Thus we break
1827 * on the first found enumeration.
1828 */
1829 if (found)
1830 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001831 type = type->baseType;
1832 } while ((type != NULL) && (type->type != XML_SCHEMA_TYPE_BASIC));
1833
1834 return ((const xmlChar *) *buf);
1835}
1836
1837/************************************************************************
1838 * *
1839 * Error functions *
1840 * *
1841 ************************************************************************/
1842
1843#if 0
1844static void
1845xmlSchemaErrMemory(const char *msg)
1846{
1847 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1848 msg);
1849}
1850#endif
1851
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001852static void
1853xmlSchemaPSimpleErr(const char *msg)
1854{
1855 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1856 msg);
1857}
1858
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001859/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001860 * xmlSchemaPErrMemory:
1861 * @node: a context node
1862 * @extra: extra informations
1863 *
1864 * Handle an out of memory condition
1865 */
1866static void
1867xmlSchemaPErrMemory(xmlSchemaParserCtxtPtr ctxt,
1868 const char *extra, xmlNodePtr node)
1869{
1870 if (ctxt != NULL)
1871 ctxt->nberrors++;
1872 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL,
1873 extra);
1874}
1875
1876/**
1877 * xmlSchemaPErr:
1878 * @ctxt: the parsing context
1879 * @node: the context node
1880 * @error: the error code
1881 * @msg: the error message
1882 * @str1: extra data
1883 * @str2: extra data
Daniel Veillarddee23482008-04-11 12:58:43 +00001884 *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001885 * Handle a parser error
1886 */
1887static void
1888xmlSchemaPErr(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1889 const char *msg, const xmlChar * str1, const xmlChar * str2)
1890{
1891 xmlGenericErrorFunc channel = NULL;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001892 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001893 void *data = NULL;
1894
1895 if (ctxt != NULL) {
1896 ctxt->nberrors++;
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00001897 ctxt->err = error;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001898 channel = ctxt->error;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00001899 data = ctxt->errCtxt;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001900 schannel = ctxt->serror;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001901 }
Daniel Veillard659e71e2003-10-10 14:10:40 +00001902 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001903 error, XML_ERR_ERROR, NULL, 0,
1904 (const char *) str1, (const char *) str2, NULL, 0, 0,
1905 msg, str1, str2);
1906}
1907
1908/**
1909 * xmlSchemaPErr2:
1910 * @ctxt: the parsing context
1911 * @node: the context node
1912 * @node: the current child
1913 * @error: the error code
1914 * @msg: the error message
1915 * @str1: extra data
1916 * @str2: extra data
Daniel Veillarddee23482008-04-11 12:58:43 +00001917 *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001918 * Handle a parser error
1919 */
1920static void
1921xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
1922 xmlNodePtr child, int error,
1923 const char *msg, const xmlChar * str1, const xmlChar * str2)
1924{
1925 if (child != NULL)
1926 xmlSchemaPErr(ctxt, child, error, msg, str1, str2);
1927 else
1928 xmlSchemaPErr(ctxt, node, error, msg, str1, str2);
1929}
1930
Daniel Veillard01fa6152004-06-29 17:04:39 +00001931
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001932/**
Daniel Veillard3646d642004-06-02 19:19:14 +00001933 * xmlSchemaPErrExt:
1934 * @ctxt: the parsing context
1935 * @node: the context node
Daniel Veillarddee23482008-04-11 12:58:43 +00001936 * @error: the error code
Daniel Veillard3646d642004-06-02 19:19:14 +00001937 * @strData1: extra data
1938 * @strData2: extra data
1939 * @strData3: extra data
1940 * @msg: the message
1941 * @str1: extra parameter for the message display
1942 * @str2: extra parameter for the message display
1943 * @str3: extra parameter for the message display
1944 * @str4: extra parameter for the message display
1945 * @str5: extra parameter for the message display
Daniel Veillarddee23482008-04-11 12:58:43 +00001946 *
Daniel Veillard3646d642004-06-02 19:19:14 +00001947 * Handle a parser error
1948 */
1949static void
1950xmlSchemaPErrExt(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
Daniel Veillarddee23482008-04-11 12:58:43 +00001951 const xmlChar * strData1, const xmlChar * strData2,
1952 const xmlChar * strData3, const char *msg, const xmlChar * str1,
Daniel Veillard3646d642004-06-02 19:19:14 +00001953 const xmlChar * str2, const xmlChar * str3, const xmlChar * str4,
1954 const xmlChar * str5)
1955{
1956
1957 xmlGenericErrorFunc channel = NULL;
1958 xmlStructuredErrorFunc schannel = NULL;
1959 void *data = NULL;
1960
1961 if (ctxt != NULL) {
1962 ctxt->nberrors++;
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00001963 ctxt->err = error;
Daniel Veillard3646d642004-06-02 19:19:14 +00001964 channel = ctxt->error;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00001965 data = ctxt->errCtxt;
Daniel Veillard3646d642004-06-02 19:19:14 +00001966 schannel = ctxt->serror;
1967 }
1968 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
1969 error, XML_ERR_ERROR, NULL, 0,
Daniel Veillarddee23482008-04-11 12:58:43 +00001970 (const char *) strData1, (const char *) strData2,
1971 (const char *) strData3, 0, 0, msg, str1, str2,
William M. Brack803812b2004-06-03 02:11:24 +00001972 str3, str4, str5);
Daniel Veillard3646d642004-06-02 19:19:14 +00001973}
Daniel Veillard01fa6152004-06-29 17:04:39 +00001974
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001975/************************************************************************
1976 * *
1977 * Allround error functions *
1978 * *
1979 ************************************************************************/
Daniel Veillard3646d642004-06-02 19:19:14 +00001980
1981/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001982 * xmlSchemaVTypeErrMemory:
1983 * @node: a context node
1984 * @extra: extra informations
1985 *
1986 * Handle an out of memory condition
1987 */
1988static void
1989xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt,
1990 const char *extra, xmlNodePtr node)
1991{
1992 if (ctxt != NULL) {
1993 ctxt->nberrors++;
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00001994 ctxt->err = XML_SCHEMAV_INTERNAL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001995 }
1996 __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL,
1997 extra);
1998}
1999
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002000static void
2001xmlSchemaPSimpleInternalErr(xmlNodePtr node,
2002 const char *msg, const xmlChar *str)
2003{
2004 __xmlSimpleError(XML_FROM_SCHEMASP, XML_SCHEMAP_INTERNAL, node,
2005 msg, (const char *) str);
2006}
2007
2008#define WXS_ERROR_TYPE_ERROR 1
2009#define WXS_ERROR_TYPE_WARNING 2
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002010/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002011 * xmlSchemaErr3:
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002012 * @ctxt: the validation context
2013 * @node: the context node
2014 * @error: the error code
2015 * @msg: the error message
2016 * @str1: extra data
2017 * @str2: extra data
2018 * @str3: extra data
Daniel Veillarddee23482008-04-11 12:58:43 +00002019 *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002020 * Handle a validation error
2021 */
2022static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002023xmlSchemaErr4Line(xmlSchemaAbstractCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002024 xmlErrorLevel errorLevel,
2025 int error, xmlNodePtr node, int line, const char *msg,
2026 const xmlChar *str1, const xmlChar *str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002027 const xmlChar *str3, const xmlChar *str4)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002028{
Daniel Veillard659e71e2003-10-10 14:10:40 +00002029 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002030 xmlGenericErrorFunc channel = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002031 void *data = NULL;
2032
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002033 if (ctxt != NULL) {
2034 if (ctxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2035 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctxt;
Daniel Veillard971771e2005-07-09 17:32:57 +00002036 const char *file = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002037 if (errorLevel != XML_ERR_WARNING) {
2038 vctxt->nberrors++;
2039 vctxt->err = error;
Daniel Veillarddee23482008-04-11 12:58:43 +00002040 channel = vctxt->error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002041 } else {
2042 channel = vctxt->warning;
2043 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002044 schannel = vctxt->serror;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00002045 data = vctxt->errCtxt;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002046
2047 /*
2048 * Error node. If we specify a line number, then
2049 * do not channel any node to the error function.
2050 */
2051 if (line == 0) {
2052 if ((node == NULL) &&
2053 (vctxt->depth >= 0) &&
2054 (vctxt->inode != NULL)) {
2055 node = vctxt->inode->node;
2056 }
2057 /*
2058 * Get filename and line if no node-tree.
2059 */
2060 if ((node == NULL) &&
2061 (vctxt->parserCtxt != NULL) &&
2062 (vctxt->parserCtxt->input != NULL)) {
2063 file = vctxt->parserCtxt->input->filename;
2064 line = vctxt->parserCtxt->input->line;
2065 }
2066 } else {
2067 /*
2068 * Override the given node's (if any) position
2069 * and channel only the given line number.
2070 */
2071 node = NULL;
2072 /*
2073 * Get filename.
2074 */
2075 if (vctxt->doc != NULL)
2076 file = (const char *) vctxt->doc->URL;
2077 else if ((vctxt->parserCtxt != NULL) &&
2078 (vctxt->parserCtxt->input != NULL))
2079 file = vctxt->parserCtxt->input->filename;
Daniel Veillarddee23482008-04-11 12:58:43 +00002080 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002081 __xmlRaiseError(schannel, channel, data, ctxt,
2082 node, XML_FROM_SCHEMASV,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002083 error, errorLevel, file, line,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002084 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002085 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002086
2087 } else if (ctxt->type == XML_SCHEMA_CTXT_PARSER) {
2088 xmlSchemaParserCtxtPtr pctxt = (xmlSchemaParserCtxtPtr) ctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002089 if (errorLevel != XML_ERR_WARNING) {
2090 pctxt->nberrors++;
2091 pctxt->err = error;
Daniel Veillarddee23482008-04-11 12:58:43 +00002092 channel = pctxt->error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002093 } else {
2094 channel = pctxt->warning;
2095 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002096 schannel = pctxt->serror;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00002097 data = pctxt->errCtxt;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002098 __xmlRaiseError(schannel, channel, data, ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002099 node, XML_FROM_SCHEMASP, error,
2100 errorLevel, NULL, 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002101 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002102 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002103 } else {
2104 TODO
2105 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002106 }
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002107}
2108
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002109/**
2110 * xmlSchemaErr3:
2111 * @ctxt: the validation context
2112 * @node: the context node
2113 * @error: the error code
2114 * @msg: the error message
2115 * @str1: extra data
2116 * @str2: extra data
2117 * @str3: extra data
Daniel Veillarddee23482008-04-11 12:58:43 +00002118 *
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002119 * Handle a validation error
2120 */
2121static void
Daniel Veillarddee23482008-04-11 12:58:43 +00002122xmlSchemaErr3(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002123 int error, xmlNodePtr node, const char *msg,
2124 const xmlChar *str1, const xmlChar *str2, const xmlChar *str3)
2125{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002126 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2127 msg, str1, str2, str3, NULL);
2128}
2129
2130static void
Daniel Veillarddee23482008-04-11 12:58:43 +00002131xmlSchemaErr4(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002132 int error, xmlNodePtr node, const char *msg,
2133 const xmlChar *str1, const xmlChar *str2,
2134 const xmlChar *str3, const xmlChar *str4)
2135{
2136 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2137 msg, str1, str2, str3, str4);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002138}
2139
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002140static void
2141xmlSchemaErr(xmlSchemaAbstractCtxtPtr actxt,
2142 int error, xmlNodePtr node, const char *msg,
2143 const xmlChar *str1, const xmlChar *str2)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002144{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002145 xmlSchemaErr4(actxt, error, node, msg, str1, str2, NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002146}
2147
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002148static xmlChar *
2149xmlSchemaFormatNodeForError(xmlChar ** msg,
2150 xmlSchemaAbstractCtxtPtr actxt,
2151 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002152{
2153 xmlChar *str = NULL;
2154
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002155 *msg = NULL;
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00002156 if ((node != NULL) &&
2157 (node->type != XML_ELEMENT_NODE) &&
2158 (node->type != XML_ATTRIBUTE_NODE))
2159 {
Daniel Veillarddee23482008-04-11 12:58:43 +00002160 /*
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00002161 * Don't try to format other nodes than element and
2162 * attribute nodes.
2163 * Play save and return an empty string.
2164 */
2165 *msg = xmlStrdup(BAD_CAST "");
2166 return(*msg);
2167 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002168 if (node != NULL) {
2169 /*
2170 * Work on tree nodes.
2171 */
2172 if (node->type == XML_ATTRIBUTE_NODE) {
2173 xmlNodePtr elem = node->parent;
Daniel Veillarddee23482008-04-11 12:58:43 +00002174
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002175 *msg = xmlStrdup(BAD_CAST "Element '");
2176 if (elem->ns != NULL)
2177 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2178 elem->ns->href, elem->name));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002179 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002180 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2181 NULL, elem->name));
2182 FREE_AND_NULL(str);
2183 *msg = xmlStrcat(*msg, BAD_CAST "', ");
Daniel Veillarddee23482008-04-11 12:58:43 +00002184 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002185 } else {
2186 *msg = xmlStrdup(BAD_CAST "Element '");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002187 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002188 if (node->ns != NULL)
2189 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2190 node->ns->href, node->name));
Daniel Veillardc0826a72004-08-10 14:17:33 +00002191 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002192 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2193 NULL, node->name));
2194 FREE_AND_NULL(str);
2195 *msg = xmlStrcat(*msg, BAD_CAST "': ");
2196 } else if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2197 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) actxt;
2198 /*
2199 * Work on node infos.
Daniel Veillarddee23482008-04-11 12:58:43 +00002200 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002201 if (vctxt->inode->nodeType == XML_ATTRIBUTE_NODE) {
2202 xmlSchemaNodeInfoPtr ielem =
2203 vctxt->elemInfos[vctxt->depth];
2204
2205 *msg = xmlStrdup(BAD_CAST "Element '");
2206 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2207 ielem->nsName, ielem->localName));
2208 FREE_AND_NULL(str);
2209 *msg = xmlStrcat(*msg, BAD_CAST "', ");
Daniel Veillarddee23482008-04-11 12:58:43 +00002210 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002211 } else {
2212 *msg = xmlStrdup(BAD_CAST "Element '");
2213 }
2214 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2215 vctxt->inode->nsName, vctxt->inode->localName));
2216 FREE_AND_NULL(str);
2217 *msg = xmlStrcat(*msg, BAD_CAST "': ");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002218 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
Daniel Veillarddee23482008-04-11 12:58:43 +00002219 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002220 * Hmm, no node while parsing?
2221 * Return an empty string, in case NULL will break something.
2222 */
2223 *msg = xmlStrdup(BAD_CAST "");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002224 } else {
2225 TODO
2226 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002227 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002228 /*
2229 * VAL TODO: The output of the given schema component is currently
2230 * disabled.
2231 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002232#if 0
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002233 if ((type != NULL) && (xmlSchemaIsGlobalItem(type))) {
2234 *msg = xmlStrcat(*msg, BAD_CAST " [");
2235 *msg = xmlStrcat(*msg, xmlSchemaFormatItemForReport(&str,
2236 NULL, type, NULL, 0));
2237 FREE_AND_NULL(str)
2238 *msg = xmlStrcat(*msg, BAD_CAST "]");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002239 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002240#endif
2241 return (*msg);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002242}
2243
Daniel Veillardc0826a72004-08-10 14:17:33 +00002244static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002245xmlSchemaInternalErr2(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002246 const char *funcName,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002247 const char *message,
2248 const xmlChar *str1,
2249 const xmlChar *str2)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002250{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002251 xmlChar *msg = NULL;
2252
Daniel Veillard14b56432006-03-09 18:41:40 +00002253 if (actxt == NULL)
2254 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002255 msg = xmlStrdup(BAD_CAST "Internal error: ");
2256 msg = xmlStrcat(msg, BAD_CAST funcName);
Daniel Veillarddee23482008-04-11 12:58:43 +00002257 msg = xmlStrcat(msg, BAD_CAST ", ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002258 msg = xmlStrcat(msg, BAD_CAST message);
2259 msg = xmlStrcat(msg, BAD_CAST ".\n");
2260
2261 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR)
2262 xmlSchemaErr(actxt, XML_SCHEMAV_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002263 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002264
Daniel Veillarddee23482008-04-11 12:58:43 +00002265 else if (actxt->type == XML_SCHEMA_CTXT_PARSER)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002266 xmlSchemaErr(actxt, XML_SCHEMAP_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002267 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002268
2269 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002270}
2271
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002272static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002273xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
2274 const char *funcName,
2275 const char *message)
2276{
2277 xmlSchemaInternalErr2(actxt, funcName, message, NULL, NULL);
2278}
2279
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002280#if 0
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002281static void
2282xmlSchemaPInternalErr(xmlSchemaParserCtxtPtr pctxt,
2283 const char *funcName,
2284 const char *message,
2285 const xmlChar *str1,
2286 const xmlChar *str2)
2287{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002288 xmlSchemaInternalErr2(ACTXT_CAST pctxt, funcName, message,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002289 str1, str2);
2290}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002291#endif
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002292
2293static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002294xmlSchemaCustomErr4(xmlSchemaAbstractCtxtPtr actxt,
2295 xmlParserErrors error,
2296 xmlNodePtr node,
2297 xmlSchemaBasicItemPtr item,
2298 const char *message,
2299 const xmlChar *str1, const xmlChar *str2,
2300 const xmlChar *str3, const xmlChar *str4)
2301{
2302 xmlChar *msg = NULL;
2303
2304 if ((node == NULL) && (item != NULL) &&
2305 (actxt->type == XML_SCHEMA_CTXT_PARSER)) {
2306 node = WXS_ITEM_NODE(item);
2307 xmlSchemaFormatItemForReport(&msg, NULL, item, NULL);
2308 msg = xmlStrcat(msg, BAD_CAST ": ");
2309 } else
2310 xmlSchemaFormatNodeForError(&msg, actxt, node);
2311 msg = xmlStrcat(msg, (const xmlChar *) message);
Daniel Veillarddee23482008-04-11 12:58:43 +00002312 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002313 xmlSchemaErr4(actxt, error, node,
2314 (const char *) msg, str1, str2, str3, str4);
2315 FREE_AND_NULL(msg)
2316}
2317
2318static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002319xmlSchemaCustomErr(xmlSchemaAbstractCtxtPtr actxt,
2320 xmlParserErrors error,
2321 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002322 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002323 const char *message,
2324 const xmlChar *str1,
2325 const xmlChar *str2)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002326{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002327 xmlSchemaCustomErr4(actxt, error, node, item,
Daniel Veillarddee23482008-04-11 12:58:43 +00002328 message, str1, str2, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002329}
2330
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002331
2332
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002333static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002334xmlSchemaCustomWarning(xmlSchemaAbstractCtxtPtr actxt,
2335 xmlParserErrors error,
2336 xmlNodePtr node,
2337 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2338 const char *message,
2339 const xmlChar *str1,
2340 const xmlChar *str2,
2341 const xmlChar *str3)
2342{
2343 xmlChar *msg = NULL;
2344
2345 xmlSchemaFormatNodeForError(&msg, actxt, node);
2346 msg = xmlStrcat(msg, (const xmlChar *) message);
Daniel Veillarddee23482008-04-11 12:58:43 +00002347 msg = xmlStrcat(msg, BAD_CAST ".\n");
2348
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002349 /* URGENT TODO: Set the error code to something sane. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002350 xmlSchemaErr4Line(actxt, XML_ERR_WARNING, error, node, 0,
2351 (const char *) msg, str1, str2, str3, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002352
2353 FREE_AND_NULL(msg)
2354}
2355
2356
2357
2358static void
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002359xmlSchemaKeyrefErr(xmlSchemaValidCtxtPtr vctxt,
2360 xmlParserErrors error,
2361 xmlSchemaPSVIIDCNodePtr idcNode,
2362 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2363 const char *message,
2364 const xmlChar *str1,
2365 const xmlChar *str2)
2366{
2367 xmlChar *msg = NULL, *qname = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002368
2369 msg = xmlStrdup(BAD_CAST "Element '%s': ");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002370 msg = xmlStrcat(msg, (const xmlChar *) message);
2371 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002372 xmlSchemaErr4Line(ACTXT_CAST vctxt, XML_ERR_ERROR,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002373 error, NULL, idcNode->nodeLine, (const char *) msg,
2374 xmlSchemaFormatQName(&qname,
2375 vctxt->nodeQNames->items[idcNode->nodeQNameID +1],
Daniel Veillarddee23482008-04-11 12:58:43 +00002376 vctxt->nodeQNames->items[idcNode->nodeQNameID]),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002377 str1, str2, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002378 FREE_AND_NULL(qname);
2379 FREE_AND_NULL(msg);
2380}
2381
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002382static int
2383xmlSchemaEvalErrorNodeType(xmlSchemaAbstractCtxtPtr actxt,
2384 xmlNodePtr node)
2385{
2386 if (node != NULL)
2387 return (node->type);
2388 if ((actxt->type == XML_SCHEMA_CTXT_VALIDATOR) &&
2389 (((xmlSchemaValidCtxtPtr) actxt)->inode != NULL))
2390 return ( ((xmlSchemaValidCtxtPtr) actxt)->inode->nodeType);
2391 return (-1);
2392}
2393
2394static int
2395xmlSchemaIsGlobalItem(xmlSchemaTypePtr item)
2396{
2397 switch (item->type) {
2398 case XML_SCHEMA_TYPE_COMPLEX:
2399 case XML_SCHEMA_TYPE_SIMPLE:
2400 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL)
2401 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002402 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002403 case XML_SCHEMA_TYPE_GROUP:
2404 return (1);
2405 case XML_SCHEMA_TYPE_ELEMENT:
2406 if ( ((xmlSchemaElementPtr) item)->flags &
2407 XML_SCHEMAS_ELEM_GLOBAL)
2408 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002409 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002410 case XML_SCHEMA_TYPE_ATTRIBUTE:
2411 if ( ((xmlSchemaAttributePtr) item)->flags &
2412 XML_SCHEMAS_ATTR_GLOBAL)
2413 return(1);
2414 break;
2415 /* Note that attribute groups are always global. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002416 default:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002417 return(1);
2418 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002419 return (0);
2420}
2421
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002422static void
2423xmlSchemaSimpleTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2424 xmlParserErrors error,
2425 xmlNodePtr node,
2426 const xmlChar *value,
2427 xmlSchemaTypePtr type,
2428 int displayValue)
2429{
2430 xmlChar *msg = NULL;
2431
2432 xmlSchemaFormatNodeForError(&msg, actxt, node);
2433
2434 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2435 XML_ATTRIBUTE_NODE))
2436 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
2437 else
2438 msg = xmlStrcat(msg, BAD_CAST "The character content is not a valid "
2439 "value of ");
2440
2441 if (! xmlSchemaIsGlobalItem(type))
2442 msg = xmlStrcat(msg, BAD_CAST "the local ");
2443 else
2444 msg = xmlStrcat(msg, BAD_CAST "the ");
2445
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002446 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002447 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002448 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002449 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002450 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002451 msg = xmlStrcat(msg, BAD_CAST "union type");
2452
2453 if (xmlSchemaIsGlobalItem(type)) {
2454 xmlChar *str = NULL;
2455 msg = xmlStrcat(msg, BAD_CAST " '");
2456 if (type->builtInType != 0) {
2457 msg = xmlStrcat(msg, BAD_CAST "xs:");
2458 msg = xmlStrcat(msg, type->name);
Daniel Veillarddee23482008-04-11 12:58:43 +00002459 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002460 msg = xmlStrcat(msg,
2461 xmlSchemaFormatQName(&str,
2462 type->targetNamespace, type->name));
2463 msg = xmlStrcat(msg, BAD_CAST "'");
2464 FREE_AND_NULL(str);
2465 }
2466 msg = xmlStrcat(msg, BAD_CAST ".\n");
2467 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2468 XML_ATTRIBUTE_NODE))
2469 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2470 else
2471 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2472 FREE_AND_NULL(msg)
2473}
2474
Daniel Veillardc0826a72004-08-10 14:17:33 +00002475static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002476xmlSchemaFormatErrorNodeQName(xmlChar ** str,
2477 xmlSchemaNodeInfoPtr ni,
2478 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002479{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002480 if (node != NULL) {
2481 if (node->ns != NULL)
2482 return (xmlSchemaFormatQName(str, node->ns->href, node->name));
2483 else
2484 return (xmlSchemaFormatQName(str, NULL, node->name));
2485 } else if (ni != NULL)
2486 return (xmlSchemaFormatQName(str, ni->nsName, ni->localName));
2487 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002488}
2489
Daniel Veillardc0826a72004-08-10 14:17:33 +00002490static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002491xmlSchemaIllegalAttrErr(xmlSchemaAbstractCtxtPtr actxt,
2492 xmlParserErrors error,
2493 xmlSchemaAttrInfoPtr ni,
2494 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002495{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002496 xmlChar *msg = NULL, *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002497
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002498 xmlSchemaFormatNodeForError(&msg, actxt, node);
2499 msg = xmlStrcat(msg, BAD_CAST "The attribute '%s' is not allowed.\n");
2500 xmlSchemaErr(actxt, error, node, (const char *) msg,
2501 xmlSchemaFormatErrorNodeQName(&str, (xmlSchemaNodeInfoPtr) ni, node),
Daniel Veillarddee23482008-04-11 12:58:43 +00002502 NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002503 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002504 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002505}
2506
Daniel Veillardc0826a72004-08-10 14:17:33 +00002507static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002508xmlSchemaComplexTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2509 xmlParserErrors error,
2510 xmlNodePtr node,
2511 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002512 const char *message,
2513 int nbval,
2514 int nbneg,
2515 xmlChar **values)
2516{
2517 xmlChar *str = NULL, *msg = NULL;
2518 xmlChar *localName, *nsName;
2519 const xmlChar *cur, *end;
Daniel Veillard6a0baa02005-12-10 11:11:12 +00002520 int i;
Daniel Veillarddee23482008-04-11 12:58:43 +00002521
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002522 xmlSchemaFormatNodeForError(&msg, actxt, node);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002523 msg = xmlStrcat(msg, (const xmlChar *) message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002524 msg = xmlStrcat(msg, BAD_CAST ".");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002525 /*
2526 * Note that is does not make sense to report that we have a
2527 * wildcard here, since the wildcard might be unfolded into
2528 * multiple transitions.
2529 */
2530 if (nbval + nbneg > 0) {
2531 if (nbval + nbneg > 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002532 str = xmlStrdup(BAD_CAST " Expected is one of ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002533 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002534 str = xmlStrdup(BAD_CAST " Expected is ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002535 nsName = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002536
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002537 for (i = 0; i < nbval + nbneg; i++) {
2538 cur = values[i];
Daniel Veillard77005e62005-07-19 16:26:18 +00002539 if (cur == NULL)
2540 continue;
2541 if ((cur[0] == 'n') && (cur[1] == 'o') && (cur[2] == 't') &&
2542 (cur[3] == ' ')) {
Daniel Veillard77005e62005-07-19 16:26:18 +00002543 cur += 4;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002544 str = xmlStrcat(str, BAD_CAST "##other");
Daniel Veillard77005e62005-07-19 16:26:18 +00002545 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002546 /*
2547 * Get the local name.
2548 */
2549 localName = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002550
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002551 end = cur;
2552 if (*end == '*') {
2553 localName = xmlStrdup(BAD_CAST "*");
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002554 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002555 } else {
2556 while ((*end != 0) && (*end != '|'))
2557 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002558 localName = xmlStrncat(localName, BAD_CAST cur, end - cur);
Daniel Veillarddee23482008-04-11 12:58:43 +00002559 }
2560 if (*end != 0) {
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002561 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002562 /*
2563 * Skip "*|*" if they come with negated expressions, since
2564 * they represent the same negated wildcard.
2565 */
2566 if ((nbneg == 0) || (*end != '*') || (*localName != '*')) {
2567 /*
2568 * Get the namespace name.
2569 */
2570 cur = end;
2571 if (*end == '*') {
2572 nsName = xmlStrdup(BAD_CAST "{*}");
2573 } else {
2574 while (*end != 0)
2575 end++;
Daniel Veillarddee23482008-04-11 12:58:43 +00002576
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002577 if (i >= nbval)
2578 nsName = xmlStrdup(BAD_CAST "{##other:");
2579 else
2580 nsName = xmlStrdup(BAD_CAST "{");
Daniel Veillarddee23482008-04-11 12:58:43 +00002581
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002582 nsName = xmlStrncat(nsName, BAD_CAST cur, end - cur);
2583 nsName = xmlStrcat(nsName, BAD_CAST "}");
2584 }
2585 str = xmlStrcat(str, BAD_CAST nsName);
2586 FREE_AND_NULL(nsName)
2587 } else {
2588 FREE_AND_NULL(localName);
2589 continue;
2590 }
Daniel Veillarddee23482008-04-11 12:58:43 +00002591 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002592 str = xmlStrcat(str, BAD_CAST localName);
2593 FREE_AND_NULL(localName);
Daniel Veillarddee23482008-04-11 12:58:43 +00002594
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002595 if (i < nbval + nbneg -1)
2596 str = xmlStrcat(str, BAD_CAST ", ");
Daniel Veillarddee23482008-04-11 12:58:43 +00002597 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002598 str = xmlStrcat(str, BAD_CAST " ).\n");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002599 msg = xmlStrcat(msg, BAD_CAST str);
2600 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002601 } else
2602 msg = xmlStrcat(msg, BAD_CAST "\n");
2603 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002604 xmlFree(msg);
2605}
2606
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002607static void
2608xmlSchemaFacetErr(xmlSchemaAbstractCtxtPtr actxt,
2609 xmlParserErrors error,
2610 xmlNodePtr node,
2611 const xmlChar *value,
2612 unsigned long length,
2613 xmlSchemaTypePtr type,
2614 xmlSchemaFacetPtr facet,
2615 const char *message,
2616 const xmlChar *str1,
2617 const xmlChar *str2)
2618{
2619 xmlChar *str = NULL, *msg = NULL;
2620 xmlSchemaTypeType facetType;
2621 int nodeType = xmlSchemaEvalErrorNodeType(actxt, node);
2622
2623 xmlSchemaFormatNodeForError(&msg, actxt, node);
2624 if (error == XML_SCHEMAV_CVC_ENUMERATION_VALID) {
2625 facetType = XML_SCHEMA_FACET_ENUMERATION;
2626 /*
2627 * If enumerations are validated, one must not expect the
2628 * facet to be given.
Daniel Veillarddee23482008-04-11 12:58:43 +00002629 */
2630 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002631 facetType = facet->type;
2632 msg = xmlStrcat(msg, BAD_CAST "[");
2633 msg = xmlStrcat(msg, BAD_CAST "facet '");
2634 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facetType));
2635 msg = xmlStrcat(msg, BAD_CAST "'] ");
2636 if (message == NULL) {
2637 /*
2638 * Use a default message.
2639 */
2640 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
2641 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
2642 (facetType == XML_SCHEMA_FACET_MAXLENGTH)) {
2643
2644 char len[25], actLen[25];
2645
2646 /* FIXME, TODO: What is the max expected string length of the
2647 * this value?
2648 */
2649 if (nodeType == XML_ATTRIBUTE_NODE)
2650 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has a length of '%s'; ");
2651 else
2652 msg = xmlStrcat(msg, BAD_CAST "The value has a length of '%s'; ");
2653
2654 snprintf(len, 24, "%lu", xmlSchemaGetFacetValueAsULong(facet));
2655 snprintf(actLen, 24, "%lu", length);
2656
2657 if (facetType == XML_SCHEMA_FACET_LENGTH)
Daniel Veillarddee23482008-04-11 12:58:43 +00002658 msg = xmlStrcat(msg,
2659 BAD_CAST "this differs from the allowed length of '%s'.\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002660 else if (facetType == XML_SCHEMA_FACET_MAXLENGTH)
Daniel Veillarddee23482008-04-11 12:58:43 +00002661 msg = xmlStrcat(msg,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002662 BAD_CAST "this exceeds the allowed maximum length of '%s'.\n");
2663 else if (facetType == XML_SCHEMA_FACET_MINLENGTH)
Daniel Veillarddee23482008-04-11 12:58:43 +00002664 msg = xmlStrcat(msg,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002665 BAD_CAST "this underruns the allowed minimum length of '%s'.\n");
Daniel Veillarddee23482008-04-11 12:58:43 +00002666
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002667 if (nodeType == XML_ATTRIBUTE_NODE)
2668 xmlSchemaErr3(actxt, error, node, (const char *) msg,
2669 value, (const xmlChar *) actLen, (const xmlChar *) len);
Daniel Veillarddee23482008-04-11 12:58:43 +00002670 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002671 xmlSchemaErr(actxt, error, node, (const char *) msg,
2672 (const xmlChar *) actLen, (const xmlChar *) len);
Daniel Veillarddee23482008-04-11 12:58:43 +00002673
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002674 } else if (facetType == XML_SCHEMA_FACET_ENUMERATION) {
2675 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not an element "
2676 "of the set {%s}.\n");
Daniel Veillarddee23482008-04-11 12:58:43 +00002677 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002678 xmlSchemaFormatFacetEnumSet(actxt, &str, type));
2679 } else if (facetType == XML_SCHEMA_FACET_PATTERN) {
2680 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not accepted "
2681 "by the pattern '%s'.\n");
Daniel Veillarddee23482008-04-11 12:58:43 +00002682 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002683 facet->value);
2684 } else if (facetType == XML_SCHEMA_FACET_MININCLUSIVE) {
2685 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is less than the "
2686 "minimum value allowed ('%s').\n");
2687 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2688 facet->value);
2689 } else if (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) {
2690 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is greater than the "
2691 "maximum value allowed ('%s').\n");
2692 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2693 facet->value);
2694 } else if (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00002695 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be greater than "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002696 "'%s'.\n");
2697 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2698 facet->value);
2699 } else if (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00002700 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be less than "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002701 "'%s'.\n");
2702 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2703 facet->value);
2704 } else if (facetType == XML_SCHEMA_FACET_TOTALDIGITS) {
2705 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more "
2706 "digits than are allowed ('%s').\n");
2707 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2708 facet->value);
2709 } else if (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) {
2710 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more fractional "
2711 "digits than are allowed ('%s').\n");
2712 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2713 facet->value);
Daniel Veillarddee23482008-04-11 12:58:43 +00002714 } else if (nodeType == XML_ATTRIBUTE_NODE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002715 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not facet-valid.\n");
Daniel Veillarddee23482008-04-11 12:58:43 +00002716 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2717 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002718 msg = xmlStrcat(msg, BAD_CAST "The value is not facet-valid.\n");
2719 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2720 }
2721 } else {
2722 msg = xmlStrcat(msg, (const xmlChar *) message);
2723 msg = xmlStrcat(msg, BAD_CAST ".\n");
2724 xmlSchemaErr(actxt, error, node, (const char *) msg, str1, str2);
Daniel Veillarddee23482008-04-11 12:58:43 +00002725 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002726 FREE_AND_NULL(str)
2727 xmlFree(msg);
2728}
2729
2730#define VERROR(err, type, msg) \
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002731 xmlSchemaCustomErr(ACTXT_CAST vctxt, err, NULL, type, msg, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002732
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002733#define VERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST vctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002734
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002735#define PERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST pctxt, func, msg);
2736#define PERROR_INT2(func, msg) xmlSchemaInternalErr(ACTXT_CAST ctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002737
2738#define AERROR_INT(func, msg) xmlSchemaInternalErr(actxt, func, msg);
2739
2740
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002741/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00002742 * xmlSchemaPMissingAttrErr:
2743 * @ctxt: the schema validation context
2744 * @ownerDes: the designation of the owner
2745 * @ownerName: the name of the owner
2746 * @ownerItem: the owner as a schema object
2747 * @ownerElem: the owner as an element node
2748 * @node: the parent element node of the missing attribute node
2749 * @type: the corresponding type of the attribute node
2750 *
2751 * Reports an illegal attribute.
2752 */
2753static void
2754xmlSchemaPMissingAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002755 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002756 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002757 xmlNodePtr ownerElem,
2758 const char *name,
2759 const char *message)
2760{
2761 xmlChar *des = NULL;
2762
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002763 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
2764
Daniel Veillardc0826a72004-08-10 14:17:33 +00002765 if (message != NULL)
2766 xmlSchemaPErr(ctxt, ownerElem, error, "%s: %s.\n", BAD_CAST des, BAD_CAST message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002767 else
2768 xmlSchemaPErr(ctxt, ownerElem, error,
2769 "%s: The attribute '%s' is required but missing.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002770 BAD_CAST des, BAD_CAST name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002771 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002772}
2773
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002774
Daniel Veillardc0826a72004-08-10 14:17:33 +00002775/**
2776 * xmlSchemaPResCompAttrErr:
2777 * @ctxt: the schema validation context
2778 * @error: the error code
2779 * @ownerDes: the designation of the owner
2780 * @ownerItem: the owner as a schema object
2781 * @ownerElem: the owner as an element node
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002782 * @name: the name of the attribute holding the QName
Daniel Veillardc0826a72004-08-10 14:17:33 +00002783 * @refName: the referenced local name
2784 * @refURI: the referenced namespace URI
2785 * @message: optional message
2786 *
2787 * Used to report QName attribute values that failed to resolve
2788 * to schema components.
2789 */
2790static void
2791xmlSchemaPResCompAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002792 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002793 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002794 xmlNodePtr ownerElem,
2795 const char *name,
2796 const xmlChar *refName,
2797 const xmlChar *refURI,
2798 xmlSchemaTypeType refType,
2799 const char *refTypeStr)
2800{
2801 xmlChar *des = NULL, *strA = NULL;
2802
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002803 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002804 if (refTypeStr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002805 refTypeStr = (const char *) xmlSchemaItemTypeToStr(refType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002806 xmlSchemaPErrExt(ctxt, ownerElem, error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002807 NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002808 "%s, attribute '%s': The QName value '%s' does not resolve to a(n) "
2809 "%s.\n", BAD_CAST des, BAD_CAST name,
2810 xmlSchemaFormatQName(&strA, refURI, refName),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002811 BAD_CAST refTypeStr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002812 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002813 FREE_AND_NULL(strA)
2814}
2815
William M. Brack2f2a6632004-08-20 23:09:47 +00002816/**
2817 * xmlSchemaPCustomAttrErr:
2818 * @ctxt: the schema parser context
2819 * @error: the error code
2820 * @ownerDes: the designation of the owner
2821 * @ownerItem: the owner as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002822 * @attr: the illegal attribute node
William M. Brack2f2a6632004-08-20 23:09:47 +00002823 *
2824 * Reports an illegal attribute during the parse.
2825 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002826static void
2827xmlSchemaPCustomAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002828 xmlParserErrors error,
William M. Brack2f2a6632004-08-20 23:09:47 +00002829 xmlChar **ownerDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002830 xmlSchemaBasicItemPtr ownerItem,
William M. Brack2f2a6632004-08-20 23:09:47 +00002831 xmlAttrPtr attr,
2832 const char *msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002833{
2834 xmlChar *des = NULL;
2835
2836 if (ownerDes == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002837 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002838 else if (*ownerDes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002839 xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002840 des = *ownerDes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002841 } else
2842 des = *ownerDes;
Daniel Veillard14b56432006-03-09 18:41:40 +00002843 if (attr == NULL) {
2844 xmlSchemaPErrExt(ctxt, NULL, error, NULL, NULL, NULL,
2845 "%s, attribute '%s': %s.\n",
Daniel Veillardaac7c682006-03-10 13:40:16 +00002846 BAD_CAST des, (const xmlChar *) "Unknown",
2847 (const xmlChar *) msg, NULL, NULL);
Daniel Veillard14b56432006-03-09 18:41:40 +00002848 } else {
2849 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
2850 "%s, attribute '%s': %s.\n",
2851 BAD_CAST des, attr->name, (const xmlChar *) msg, NULL, NULL);
2852 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00002853 if (ownerDes == NULL)
2854 FREE_AND_NULL(des);
2855}
2856
2857/**
2858 * xmlSchemaPIllegalAttrErr:
William M. Brack2f2a6632004-08-20 23:09:47 +00002859 * @ctxt: the schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00002860 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00002861 * @ownerDes: the designation of the attribute's owner
2862 * @ownerItem: the attribute's owner item
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002863 * @attr: the illegal attribute node
Daniel Veillardc0826a72004-08-10 14:17:33 +00002864 *
William M. Brack2f2a6632004-08-20 23:09:47 +00002865 * Reports an illegal attribute during the parse.
Daniel Veillardc0826a72004-08-10 14:17:33 +00002866 */
2867static void
2868xmlSchemaPIllegalAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002869 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002870 xmlSchemaBasicItemPtr ownerComp ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002871 xmlAttrPtr attr)
2872{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002873 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002874
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002875 xmlSchemaFormatNodeForError(&strA, ACTXT_CAST ctxt, attr->parent);
2876 xmlSchemaErr4(ACTXT_CAST ctxt, error, (xmlNodePtr) attr,
2877 "%sThe attribute '%s' is not allowed.\n", BAD_CAST strA,
2878 xmlSchemaFormatQNameNs(&strB, attr->ns, attr->name),
2879 NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002880 FREE_AND_NULL(strA);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002881 FREE_AND_NULL(strB);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002882}
2883
William M. Brack2f2a6632004-08-20 23:09:47 +00002884/**
2885 * xmlSchemaPCustomErr:
2886 * @ctxt: the schema parser context
2887 * @error: the error code
2888 * @itemDes: the designation of the schema item
2889 * @item: the schema item
2890 * @itemElem: the node of the schema item
2891 * @message: the error message
2892 * @str1: an optional param for the error message
2893 * @str2: an optional param for the error message
2894 * @str3: an optional param for the error message
2895 *
2896 * Reports an error during parsing.
2897 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002898static void
2899xmlSchemaPCustomErrExt(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002900 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002901 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002902 xmlNodePtr itemElem,
2903 const char *message,
2904 const xmlChar *str1,
2905 const xmlChar *str2,
2906 const xmlChar *str3)
2907{
2908 xmlChar *des = NULL, *msg = NULL;
2909
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002910 xmlSchemaFormatItemForReport(&des, NULL, item, itemElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002911 msg = xmlStrdup(BAD_CAST "%s: ");
2912 msg = xmlStrcat(msg, (const xmlChar *) message);
2913 msg = xmlStrcat(msg, BAD_CAST ".\n");
2914 if ((itemElem == NULL) && (item != NULL))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002915 itemElem = WXS_ITEM_NODE(item);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002916 xmlSchemaPErrExt(ctxt, itemElem, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002917 (const char *) msg, BAD_CAST des, str1, str2, str3, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002918 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002919 FREE_AND_NULL(msg);
2920}
2921
William M. Brack2f2a6632004-08-20 23:09:47 +00002922/**
2923 * xmlSchemaPCustomErr:
2924 * @ctxt: the schema parser context
2925 * @error: the error code
2926 * @itemDes: the designation of the schema item
2927 * @item: the schema item
2928 * @itemElem: the node of the schema item
2929 * @message: the error message
2930 * @str1: the optional param for the error message
2931 *
2932 * Reports an error during parsing.
2933 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002934static void
2935xmlSchemaPCustomErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002936 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002937 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002938 xmlNodePtr itemElem,
2939 const char *message,
2940 const xmlChar *str1)
2941{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002942 xmlSchemaPCustomErrExt(ctxt, error, item, itemElem, message,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002943 str1, NULL, NULL);
2944}
2945
William M. Brack2f2a6632004-08-20 23:09:47 +00002946/**
2947 * xmlSchemaPAttrUseErr:
2948 * @ctxt: the schema parser context
2949 * @error: the error code
2950 * @itemDes: the designation of the schema type
2951 * @item: the schema type
2952 * @itemElem: the node of the schema type
2953 * @attr: the invalid schema attribute
2954 * @message: the error message
2955 * @str1: the optional param for the error message
2956 *
2957 * Reports an attribute use error during parsing.
2958 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002959static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002960xmlSchemaPAttrUseErr4(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002961 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002962 xmlNodePtr node,
2963 xmlSchemaBasicItemPtr ownerItem,
2964 const xmlSchemaAttributeUsePtr attruse,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002965 const char *message,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002966 const xmlChar *str1, const xmlChar *str2,
2967 const xmlChar *str3,const xmlChar *str4)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002968{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002969 xmlChar *str = NULL, *msg = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002970
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002971 xmlSchemaFormatItemForReport(&msg, NULL, ownerItem, NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00002972 msg = xmlStrcat(msg, BAD_CAST ", ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002973 msg = xmlStrcat(msg,
2974 BAD_CAST xmlSchemaFormatItemForReport(&str, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002975 WXS_BASIC_CAST attruse, NULL));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002976 FREE_AND_NULL(str);
2977 msg = xmlStrcat(msg, BAD_CAST ": ");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002978 msg = xmlStrcat(msg, (const xmlChar *) message);
2979 msg = xmlStrcat(msg, BAD_CAST ".\n");
Daniel Veillarddee23482008-04-11 12:58:43 +00002980 xmlSchemaErr4(ACTXT_CAST ctxt, error, node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002981 (const char *) msg, str1, str2, str3, str4);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002982 xmlFree(msg);
2983}
2984
William M. Brack2f2a6632004-08-20 23:09:47 +00002985/**
2986 * xmlSchemaPIllegalFacetAtomicErr:
2987 * @ctxt: the schema parser context
2988 * @error: the error code
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002989 * @type: the schema type
2990 * @baseType: the base type of type
William M. Brack2f2a6632004-08-20 23:09:47 +00002991 * @facet: the illegal facet
2992 *
2993 * Reports an illegal facet for atomic simple types.
2994 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002995static void
2996xmlSchemaPIllegalFacetAtomicErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002997 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002998 xmlSchemaTypePtr type,
2999 xmlSchemaTypePtr baseType,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003000 xmlSchemaFacetPtr facet)
3001{
3002 xmlChar *des = NULL, *strT = NULL;
3003
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003004 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type, type->node);
3005 xmlSchemaPErrExt(ctxt, type->node, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003006 "%s: The facet '%s' is not allowed on types derived from the "
3007 "type %s.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00003008 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003009 xmlSchemaFormatItemForReport(&strT, NULL, WXS_BASIC_CAST baseType, NULL),
Daniel Veillardc0826a72004-08-10 14:17:33 +00003010 NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003011 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003012 FREE_AND_NULL(strT);
3013}
3014
William M. Brack2f2a6632004-08-20 23:09:47 +00003015/**
3016 * xmlSchemaPIllegalFacetListUnionErr:
3017 * @ctxt: the schema parser context
3018 * @error: the error code
3019 * @itemDes: the designation of the schema item involved
3020 * @item: the schema item involved
3021 * @facet: the illegal facet
3022 *
3023 * Reports an illegal facet for <list> and <union>.
3024 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003025static void
3026xmlSchemaPIllegalFacetListUnionErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003027 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003028 xmlSchemaTypePtr type,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003029 xmlSchemaFacetPtr facet)
3030{
Daniel Veillardb2947172006-03-27 09:45:01 +00003031 xmlChar *des = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003032
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003033 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type,
3034 type->node);
3035 xmlSchemaPErr(ctxt, type->node, error,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003036 "%s: The facet '%s' is not allowed.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00003037 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003038 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003039}
3040
3041/**
3042 * xmlSchemaPMutualExclAttrErr:
3043 * @ctxt: the schema validation context
3044 * @error: the error code
3045 * @elemDes: the designation of the parent element node
3046 * @attr: the bad attribute node
3047 * @type: the corresponding type of the attribute node
3048 *
3049 * Reports an illegal attribute.
3050 */
3051static void
3052xmlSchemaPMutualExclAttrErr(xmlSchemaParserCtxtPtr ctxt,
3053 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003054 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003055 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003056 const char *name1,
3057 const char *name2)
3058{
3059 xmlChar *des = NULL;
3060
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003061 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003062 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003063 "%s: The attributes '%s' and '%s' are mutually exclusive.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003064 BAD_CAST des, BAD_CAST name1, BAD_CAST name2, NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003065 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003066}
3067
3068/**
3069 * xmlSchemaPSimpleTypeErr:
3070 * @ctxt: the schema validation context
3071 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00003072 * @type: the type specifier
Daniel Veillardc0826a72004-08-10 14:17:33 +00003073 * @ownerDes: the designation of the owner
Daniel Veillarddee23482008-04-11 12:58:43 +00003074 * @ownerItem: the schema object if existent
Daniel Veillardc0826a72004-08-10 14:17:33 +00003075 * @node: the validated node
3076 * @value: the validated value
3077 *
3078 * Reports a simple type validation error.
3079 * TODO: Should this report the value of an element as well?
3080 */
3081static void
Daniel Veillarddee23482008-04-11 12:58:43 +00003082xmlSchemaPSimpleTypeErr(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003083 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003084 xmlSchemaBasicItemPtr ownerItem ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003085 xmlNodePtr node,
William M. Brack2f2a6632004-08-20 23:09:47 +00003086 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003087 const char *expected,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003088 const xmlChar *value,
3089 const char *message,
3090 const xmlChar *str1,
3091 const xmlChar *str2)
3092{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003093 xmlChar *msg = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00003094
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003095 xmlSchemaFormatNodeForError(&msg, ACTXT_CAST ctxt, node);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003096 if (message == NULL) {
3097 /*
3098 * Use default messages.
Daniel Veillarddee23482008-04-11 12:58:43 +00003099 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003100 if (type != NULL) {
3101 if (node->type == XML_ATTRIBUTE_NODE)
3102 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
3103 else
3104 msg = xmlStrcat(msg, BAD_CAST "The character content is not a "
Daniel Veillarddee23482008-04-11 12:58:43 +00003105 "valid value of ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003106 if (! xmlSchemaIsGlobalItem(type))
3107 msg = xmlStrcat(msg, BAD_CAST "the local ");
3108 else
3109 msg = xmlStrcat(msg, BAD_CAST "the ");
Daniel Veillarddee23482008-04-11 12:58:43 +00003110
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003111 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003112 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003113 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003114 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003115 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003116 msg = xmlStrcat(msg, BAD_CAST "union type");
Daniel Veillarddee23482008-04-11 12:58:43 +00003117
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003118 if (xmlSchemaIsGlobalItem(type)) {
3119 xmlChar *str = NULL;
3120 msg = xmlStrcat(msg, BAD_CAST " '");
3121 if (type->builtInType != 0) {
3122 msg = xmlStrcat(msg, BAD_CAST "xs:");
3123 msg = xmlStrcat(msg, type->name);
Daniel Veillarddee23482008-04-11 12:58:43 +00003124 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003125 msg = xmlStrcat(msg,
3126 xmlSchemaFormatQName(&str,
3127 type->targetNamespace, type->name));
3128 msg = xmlStrcat(msg, BAD_CAST "'.");
3129 FREE_AND_NULL(str);
3130 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003131 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003132 if (node->type == XML_ATTRIBUTE_NODE)
3133 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not valid.");
3134 else
3135 msg = xmlStrcat(msg, BAD_CAST "The character content is not "
3136 "valid.");
Daniel Veillarddee23482008-04-11 12:58:43 +00003137 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003138 if (expected) {
3139 msg = xmlStrcat(msg, BAD_CAST " Expected is '");
3140 msg = xmlStrcat(msg, BAD_CAST expected);
3141 msg = xmlStrcat(msg, BAD_CAST "'.\n");
3142 } else
3143 msg = xmlStrcat(msg, BAD_CAST "\n");
Daniel Veillardc0826a72004-08-10 14:17:33 +00003144 if (node->type == XML_ATTRIBUTE_NODE)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003145 xmlSchemaPErr(ctxt, node, error, (const char *) msg, value, NULL);
3146 else
3147 xmlSchemaPErr(ctxt, node, error, (const char *) msg, NULL, NULL);
3148 } else {
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003149 msg = xmlStrcat(msg, BAD_CAST message);
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00003150 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003151 xmlSchemaPErrExt(ctxt, node, error, NULL, NULL, NULL,
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003152 (const char*) msg, str1, str2, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003153 }
Daniel Veillarddee23482008-04-11 12:58:43 +00003154 /* Cleanup. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003155 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003156}
3157
William M. Brack2f2a6632004-08-20 23:09:47 +00003158/**
3159 * xmlSchemaPContentErr:
3160 * @ctxt: the schema parser context
3161 * @error: the error code
3162 * @onwerDes: the designation of the holder of the content
3163 * @ownerItem: the owner item of the holder of the content
3164 * @ownerElem: the node of the holder of the content
3165 * @child: the invalid child node
3166 * @message: the optional error message
3167 * @content: the optional string describing the correct content
3168 *
3169 * Reports an error concerning the content of a schema element.
3170 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003171static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003172xmlSchemaPContentErr(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003173 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003174 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003175 xmlNodePtr ownerElem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003176 xmlNodePtr child,
3177 const char *message,
3178 const char *content)
3179{
3180 xmlChar *des = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003181
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003182 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003183 if (message != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003184 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3185 "%s: %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003186 BAD_CAST des, BAD_CAST message);
3187 else {
3188 if (content != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003189 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3190 "%s: The content is not valid. Expected is %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003191 BAD_CAST des, BAD_CAST content);
3192 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003193 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3194 "%s: The content is not valid.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003195 BAD_CAST des, NULL);
3196 }
3197 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003198 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003199}
3200
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003201/************************************************************************
3202 * *
3203 * Streamable error functions *
3204 * *
3205 ************************************************************************/
Kasimier T. Buchcik8b418172004-11-17 13:14:27 +00003206
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003207
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003208
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003209
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003210/************************************************************************
3211 * *
3212 * Validation helper functions *
3213 * *
3214 ************************************************************************/
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003215
Daniel Veillardc0826a72004-08-10 14:17:33 +00003216
Daniel Veillard4255d502002-04-16 15:50:10 +00003217/************************************************************************
3218 * *
3219 * Allocation functions *
3220 * *
3221 ************************************************************************/
3222
3223/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003224 * xmlSchemaNewSchemaForParserCtxt:
William M. Brack08171912003-12-29 02:52:11 +00003225 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003226 *
3227 * Allocate a new Schema structure.
3228 *
3229 * Returns the newly allocated structure or NULL in case or error
3230 */
3231static xmlSchemaPtr
3232xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt)
3233{
3234 xmlSchemaPtr ret;
3235
3236 ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema));
3237 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003238 xmlSchemaPErrMemory(ctxt, "allocating schema", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003239 return (NULL);
3240 }
3241 memset(ret, 0, sizeof(xmlSchema));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003242 ret->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +00003243 xmlDictReference(ret->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00003244
3245 return (ret);
3246}
3247
3248/**
3249 * xmlSchemaNewFacet:
Daniel Veillard4255d502002-04-16 15:50:10 +00003250 *
3251 * Allocate a new Facet structure.
3252 *
3253 * Returns the newly allocated structure or NULL in case or error
3254 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003255xmlSchemaFacetPtr
3256xmlSchemaNewFacet(void)
Daniel Veillard4255d502002-04-16 15:50:10 +00003257{
3258 xmlSchemaFacetPtr ret;
3259
3260 ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet));
3261 if (ret == NULL) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003262 return (NULL);
3263 }
3264 memset(ret, 0, sizeof(xmlSchemaFacet));
3265
3266 return (ret);
3267}
3268
3269/**
3270 * xmlSchemaNewAnnot:
William M. Brack08171912003-12-29 02:52:11 +00003271 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003272 * @node: a node
3273 *
3274 * Allocate a new annotation structure.
3275 *
3276 * Returns the newly allocated structure or NULL in case or error
3277 */
3278static xmlSchemaAnnotPtr
3279xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
3280{
3281 xmlSchemaAnnotPtr ret;
3282
3283 ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot));
3284 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003285 xmlSchemaPErrMemory(ctxt, "allocating annotation", node);
Daniel Veillard4255d502002-04-16 15:50:10 +00003286 return (NULL);
3287 }
3288 memset(ret, 0, sizeof(xmlSchemaAnnot));
3289 ret->content = node;
3290 return (ret);
3291}
3292
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003293static xmlSchemaItemListPtr
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003294xmlSchemaItemListCreate(void)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003295{
3296 xmlSchemaItemListPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003297
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003298 ret = xmlMalloc(sizeof(xmlSchemaItemList));
3299 if (ret == NULL) {
3300 xmlSchemaPErrMemory(NULL,
3301 "allocating an item list structure", NULL);
3302 return (NULL);
3303 }
3304 memset(ret, 0, sizeof(xmlSchemaItemList));
3305 return (ret);
3306}
3307
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003308static void
3309xmlSchemaItemListClear(xmlSchemaItemListPtr list)
3310{
3311 if (list->items != NULL) {
3312 xmlFree(list->items);
3313 list->items = NULL;
3314 }
3315 list->nbItems = 0;
3316 list->sizeItems = 0;
3317}
3318
3319static int
3320xmlSchemaItemListAdd(xmlSchemaItemListPtr list, void *item)
3321{
3322 if (list->items == NULL) {
3323 list->items = (void **) xmlMalloc(
3324 20 * sizeof(void *));
3325 if (list->items == NULL) {
3326 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3327 return(-1);
3328 }
3329 list->sizeItems = 20;
3330 } else if (list->sizeItems <= list->nbItems) {
3331 list->sizeItems *= 2;
3332 list->items = (void **) xmlRealloc(list->items,
3333 list->sizeItems * sizeof(void *));
3334 if (list->items == NULL) {
3335 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3336 list->sizeItems = 0;
3337 return(-1);
3338 }
3339 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003340 list->items[list->nbItems++] = item;
3341 return(0);
3342}
3343
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003344static int
3345xmlSchemaItemListAddSize(xmlSchemaItemListPtr list,
3346 int initialSize,
3347 void *item)
3348{
3349 if (list->items == NULL) {
3350 if (initialSize <= 0)
3351 initialSize = 1;
3352 list->items = (void **) xmlMalloc(
3353 initialSize * sizeof(void *));
3354 if (list->items == NULL) {
3355 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3356 return(-1);
3357 }
3358 list->sizeItems = initialSize;
3359 } else if (list->sizeItems <= list->nbItems) {
3360 list->sizeItems *= 2;
3361 list->items = (void **) xmlRealloc(list->items,
3362 list->sizeItems * sizeof(void *));
3363 if (list->items == NULL) {
3364 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3365 list->sizeItems = 0;
3366 return(-1);
3367 }
3368 }
3369 list->items[list->nbItems++] = item;
3370 return(0);
3371}
3372
3373static int
3374xmlSchemaItemListInsert(xmlSchemaItemListPtr list, void *item, int idx)
Daniel Veillarddee23482008-04-11 12:58:43 +00003375{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003376 if (list->items == NULL) {
3377 list->items = (void **) xmlMalloc(
3378 20 * sizeof(void *));
3379 if (list->items == NULL) {
3380 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3381 return(-1);
3382 }
3383 list->sizeItems = 20;
3384 } else if (list->sizeItems <= list->nbItems) {
3385 list->sizeItems *= 2;
3386 list->items = (void **) xmlRealloc(list->items,
3387 list->sizeItems * sizeof(void *));
3388 if (list->items == NULL) {
3389 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3390 list->sizeItems = 0;
3391 return(-1);
3392 }
3393 }
3394 /*
3395 * Just append if the index is greater/equal than the item count.
3396 */
3397 if (idx >= list->nbItems) {
3398 list->items[list->nbItems++] = item;
3399 } else {
3400 int i;
3401 for (i = list->nbItems; i > idx; i--)
3402 list->items[i] = list->items[i-1];
3403 list->items[idx] = item;
3404 list->nbItems++;
3405 }
3406 return(0);
3407}
3408
3409#if 0 /* enable if ever needed */
3410static int
3411xmlSchemaItemListInsertSize(xmlSchemaItemListPtr list,
3412 int initialSize,
3413 void *item,
3414 int idx)
Daniel Veillarddee23482008-04-11 12:58:43 +00003415{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003416 if (list->items == NULL) {
3417 if (initialSize <= 0)
3418 initialSize = 1;
3419 list->items = (void **) xmlMalloc(
3420 initialSize * sizeof(void *));
3421 if (list->items == NULL) {
3422 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3423 return(-1);
3424 }
3425 list->sizeItems = initialSize;
3426 } else if (list->sizeItems <= list->nbItems) {
3427 list->sizeItems *= 2;
3428 list->items = (void **) xmlRealloc(list->items,
3429 list->sizeItems * sizeof(void *));
3430 if (list->items == NULL) {
3431 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3432 list->sizeItems = 0;
3433 return(-1);
3434 }
3435 }
3436 /*
3437 * Just append if the index is greater/equal than the item count.
3438 */
3439 if (idx >= list->nbItems) {
3440 list->items[list->nbItems++] = item;
3441 } else {
3442 int i;
3443 for (i = list->nbItems; i > idx; i--)
3444 list->items[i] = list->items[i-1];
3445 list->items[idx] = item;
3446 list->nbItems++;
3447 }
3448 return(0);
3449}
3450#endif
3451
3452static int
3453xmlSchemaItemListRemove(xmlSchemaItemListPtr list, int idx)
3454{
3455 int i;
3456 if ((list->items == NULL) || (idx >= list->nbItems)) {
3457 xmlSchemaPSimpleErr("Internal error: xmlSchemaItemListRemove, "
3458 "index error.\n");
3459 return(-1);
3460 }
3461
3462 if (list->nbItems == 1) {
3463 /* TODO: Really free the list? */
3464 xmlFree(list->items);
3465 list->items = NULL;
3466 list->nbItems = 0;
3467 list->sizeItems = 0;
3468 } else if (list->nbItems -1 == idx) {
3469 list->nbItems--;
Daniel Veillarddee23482008-04-11 12:58:43 +00003470 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003471 for (i = idx; i < list->nbItems -1; i++)
3472 list->items[i] = list->items[i+1];
3473 list->nbItems--;
3474 }
3475 return(0);
3476}
3477
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003478/**
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003479 * xmlSchemaItemListFree:
3480 * @annot: a schema type structure
3481 *
3482 * Deallocate a annotation structure
3483 */
3484static void
3485xmlSchemaItemListFree(xmlSchemaItemListPtr list)
3486{
3487 if (list == NULL)
3488 return;
3489 if (list->items != NULL)
3490 xmlFree(list->items);
3491 xmlFree(list);
3492}
3493
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003494static void
3495xmlSchemaBucketFree(xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003496{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003497 if (bucket == NULL)
3498 return;
3499 if (bucket->globals != NULL) {
3500 xmlSchemaComponentListFree(bucket->globals);
3501 xmlSchemaItemListFree(bucket->globals);
3502 }
3503 if (bucket->locals != NULL) {
3504 xmlSchemaComponentListFree(bucket->locals);
Daniel Veillarddee23482008-04-11 12:58:43 +00003505 xmlSchemaItemListFree(bucket->locals);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003506 }
3507 if (bucket->relations != NULL) {
3508 xmlSchemaSchemaRelationPtr prev, cur = bucket->relations;
3509 do {
Daniel Veillarddee23482008-04-11 12:58:43 +00003510 prev = cur;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003511 cur = cur->next;
3512 xmlFree(prev);
3513 } while (cur != NULL);
3514 }
3515 if ((! bucket->preserveDoc) && (bucket->doc != NULL)) {
3516 xmlFreeDoc(bucket->doc);
Daniel Veillarddee23482008-04-11 12:58:43 +00003517 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003518 if (bucket->type == XML_SCHEMA_SCHEMA_IMPORT) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003519 if (WXS_IMPBUCKET(bucket)->schema != NULL)
3520 xmlSchemaFree(WXS_IMPBUCKET(bucket)->schema);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003521 }
3522 xmlFree(bucket);
3523}
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003524
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003525static xmlSchemaBucketPtr
3526xmlSchemaBucketCreate(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003527 int type, const xmlChar *targetNamespace)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003528{
3529 xmlSchemaBucketPtr ret;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003530 int size;
3531 xmlSchemaPtr mainSchema;
3532
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003533 if (WXS_CONSTRUCTOR(pctxt)->mainSchema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003534 PERROR_INT("xmlSchemaBucketCreate",
3535 "no main schema on constructor");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003536 return(NULL);
3537 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003538 mainSchema = WXS_CONSTRUCTOR(pctxt)->mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003539 /* Create the schema bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003540 if (WXS_IS_BUCKET_INCREDEF(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003541 size = sizeof(xmlSchemaInclude);
3542 else
3543 size = sizeof(xmlSchemaImport);
3544 ret = (xmlSchemaBucketPtr) xmlMalloc(size);
3545 if (ret == NULL) {
3546 xmlSchemaPErrMemory(NULL, "allocating schema bucket", NULL);
3547 return(NULL);
3548 }
3549 memset(ret, 0, size);
3550 ret->targetNamespace = targetNamespace;
3551 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003552 ret->globals = xmlSchemaItemListCreate();
3553 if (ret->globals == NULL) {
3554 xmlFree(ret);
3555 return(NULL);
3556 }
3557 ret->locals = xmlSchemaItemListCreate();
3558 if (ret->locals == NULL) {
3559 xmlFree(ret);
3560 return(NULL);
3561 }
Daniel Veillarddee23482008-04-11 12:58:43 +00003562 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003563 * The following will assure that only the first bucket is marked as
3564 * XML_SCHEMA_SCHEMA_MAIN and it points to the *main* schema.
3565 * For each following import buckets an xmlSchema will be created.
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003566 * An xmlSchema will be created for every distinct targetNamespace.
3567 * We assign the targetNamespace to the schemata here.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003568 */
3569 if (! WXS_HAS_BUCKETS(pctxt)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003570 if (WXS_IS_BUCKET_INCREDEF(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003571 PERROR_INT("xmlSchemaBucketCreate",
3572 "first bucket but it's an include or redefine");
3573 xmlSchemaBucketFree(ret);
3574 return(NULL);
3575 }
Daniel Veillarddee23482008-04-11 12:58:43 +00003576 /* Force the type to be XML_SCHEMA_SCHEMA_MAIN. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003577 ret->type = XML_SCHEMA_SCHEMA_MAIN;
3578 /* Point to the *main* schema. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003579 WXS_CONSTRUCTOR(pctxt)->mainBucket = ret;
3580 WXS_IMPBUCKET(ret)->schema = mainSchema;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003581 /*
3582 * Ensure that the main schema gets a targetNamespace.
3583 */
3584 mainSchema->targetNamespace = targetNamespace;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003585 } else {
Daniel Veillarddee23482008-04-11 12:58:43 +00003586 if (type == XML_SCHEMA_SCHEMA_MAIN) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003587 PERROR_INT("xmlSchemaBucketCreate",
3588 "main bucket but it's not the first one");
3589 xmlSchemaBucketFree(ret);
3590 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00003591 } else if (type == XML_SCHEMA_SCHEMA_IMPORT) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003592 /*
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003593 * Create a schema for imports and assign the
3594 * targetNamespace.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003595 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003596 WXS_IMPBUCKET(ret)->schema = xmlSchemaNewSchema(pctxt);
3597 if (WXS_IMPBUCKET(ret)->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003598 xmlSchemaBucketFree(ret);
3599 return(NULL);
3600 }
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003601 WXS_IMPBUCKET(ret)->schema->targetNamespace = targetNamespace;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003602 }
Daniel Veillarddee23482008-04-11 12:58:43 +00003603 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003604 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003605 int res;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003606 /*
3607 * Imports go into the "schemasImports" slot of the main *schema*.
3608 * Note that we create an import entry for the main schema as well; i.e.,
3609 * even if there's only one schema, we'll get an import.
3610 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003611 if (mainSchema->schemasImports == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003612 mainSchema->schemasImports = xmlHashCreateDict(5,
3613 WXS_CONSTRUCTOR(pctxt)->dict);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003614 if (mainSchema->schemasImports == NULL) {
3615 xmlSchemaBucketFree(ret);
3616 return(NULL);
3617 }
3618 }
3619 if (targetNamespace == NULL)
3620 res = xmlHashAddEntry(mainSchema->schemasImports,
3621 XML_SCHEMAS_NO_NAMESPACE, ret);
3622 else
3623 res = xmlHashAddEntry(mainSchema->schemasImports,
3624 targetNamespace, ret);
3625 if (res != 0) {
3626 PERROR_INT("xmlSchemaBucketCreate",
3627 "failed to add the schema bucket to the hash");
3628 xmlSchemaBucketFree(ret);
3629 return(NULL);
3630 }
3631 } else {
3632 /* Set the @ownerImport of an include bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003633 if (WXS_IS_BUCKET_IMPMAIN(WXS_CONSTRUCTOR(pctxt)->bucket->type))
3634 WXS_INCBUCKET(ret)->ownerImport =
3635 WXS_IMPBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003636 else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003637 WXS_INCBUCKET(ret)->ownerImport =
3638 WXS_INCBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket)->ownerImport;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003639
3640 /* Includes got into the "includes" slot of the *main* schema. */
3641 if (mainSchema->includes == NULL) {
3642 mainSchema->includes = xmlSchemaItemListCreate();
3643 if (mainSchema->includes == NULL) {
3644 xmlSchemaBucketFree(ret);
3645 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00003646 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003647 }
3648 xmlSchemaItemListAdd(mainSchema->includes, ret);
3649 }
Daniel Veillarddee23482008-04-11 12:58:43 +00003650 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003651 * Add to list of all buckets; this is used for lookup
3652 * during schema construction time only.
3653 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003654 if (xmlSchemaItemListAdd(WXS_CONSTRUCTOR(pctxt)->buckets, ret) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003655 return(NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003656 return(ret);
3657}
3658
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003659static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003660xmlSchemaAddItemSize(xmlSchemaItemListPtr *list, int initialSize, void *item)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003661{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003662 if (*list == NULL) {
3663 *list = xmlSchemaItemListCreate();
3664 if (*list == NULL)
3665 return(-1);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003666 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003667 xmlSchemaItemListAddSize(*list, initialSize, item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003668 return(0);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003669}
3670
3671/**
Daniel Veillardfdc91562002-07-01 21:52:03 +00003672 * xmlSchemaFreeAnnot:
3673 * @annot: a schema type structure
3674 *
3675 * Deallocate a annotation structure
3676 */
3677static void
3678xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot)
3679{
3680 if (annot == NULL)
3681 return;
Kasimier T. Buchcik004b5462005-08-08 12:43:09 +00003682 if (annot->next == NULL) {
3683 xmlFree(annot);
3684 } else {
3685 xmlSchemaAnnotPtr prev;
3686
3687 do {
3688 prev = annot;
3689 annot = annot->next;
3690 xmlFree(prev);
3691 } while (annot != NULL);
3692 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00003693}
3694
3695/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003696 * xmlSchemaFreeNotation:
3697 * @schema: a schema notation structure
3698 *
3699 * Deallocate a Schema Notation structure.
3700 */
3701static void
3702xmlSchemaFreeNotation(xmlSchemaNotationPtr nota)
3703{
3704 if (nota == NULL)
3705 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003706 xmlFree(nota);
3707}
3708
3709/**
3710 * xmlSchemaFreeAttribute:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003711 * @attr: an attribute declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00003712 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003713 * Deallocates an attribute declaration structure.
Daniel Veillard4255d502002-04-16 15:50:10 +00003714 */
3715static void
3716xmlSchemaFreeAttribute(xmlSchemaAttributePtr attr)
3717{
3718 if (attr == NULL)
3719 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003720 if (attr->annot != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003721 xmlSchemaFreeAnnot(attr->annot);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003722 if (attr->defVal != NULL)
3723 xmlSchemaFreeValue(attr->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003724 xmlFree(attr);
3725}
3726
3727/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003728 * xmlSchemaFreeAttributeUse:
3729 * @use: an attribute use
3730 *
3731 * Deallocates an attribute use structure.
3732 */
3733static void
3734xmlSchemaFreeAttributeUse(xmlSchemaAttributeUsePtr use)
3735{
3736 if (use == NULL)
3737 return;
3738 if (use->annot != NULL)
3739 xmlSchemaFreeAnnot(use->annot);
3740 if (use->defVal != NULL)
3741 xmlSchemaFreeValue(use->defVal);
3742 xmlFree(use);
3743}
3744
3745/**
3746 * xmlSchemaFreeAttributeUseProhib:
3747 * @prohib: an attribute use prohibition
3748 *
3749 * Deallocates an attribute use structure.
3750 */
3751static void
3752xmlSchemaFreeAttributeUseProhib(xmlSchemaAttributeUseProhibPtr prohib)
3753{
3754 if (prohib == NULL)
3755 return;
3756 xmlFree(prohib);
3757}
3758
3759/**
Daniel Veillard3646d642004-06-02 19:19:14 +00003760 * xmlSchemaFreeWildcardNsSet:
3761 * set: a schema wildcard namespace
3762 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003763 * Deallocates a list of wildcard constraint structures.
Daniel Veillard3646d642004-06-02 19:19:14 +00003764 */
3765static void
3766xmlSchemaFreeWildcardNsSet(xmlSchemaWildcardNsPtr set)
3767{
3768 xmlSchemaWildcardNsPtr next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003769
Daniel Veillard3646d642004-06-02 19:19:14 +00003770 while (set != NULL) {
3771 next = set->next;
3772 xmlFree(set);
3773 set = next;
3774 }
3775}
3776
3777/**
3778 * xmlSchemaFreeWildcard:
Daniel Veillard01fa6152004-06-29 17:04:39 +00003779 * @wildcard: a wildcard structure
Daniel Veillard3646d642004-06-02 19:19:14 +00003780 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003781 * Deallocates a wildcard structure.
Daniel Veillard3646d642004-06-02 19:19:14 +00003782 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00003783void
Daniel Veillard3646d642004-06-02 19:19:14 +00003784xmlSchemaFreeWildcard(xmlSchemaWildcardPtr wildcard)
3785{
3786 if (wildcard == NULL)
3787 return;
3788 if (wildcard->annot != NULL)
3789 xmlSchemaFreeAnnot(wildcard->annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003790 if (wildcard->nsSet != NULL)
3791 xmlSchemaFreeWildcardNsSet(wildcard->nsSet);
3792 if (wildcard->negNsSet != NULL)
3793 xmlFree(wildcard->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +00003794 xmlFree(wildcard);
3795}
3796
3797/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003798 * xmlSchemaFreeAttributeGroup:
3799 * @schema: a schema attribute group structure
3800 *
3801 * Deallocate a Schema Attribute Group structure.
3802 */
3803static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003804xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillard4255d502002-04-16 15:50:10 +00003805{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003806 if (attrGr == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00003807 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003808 if (attrGr->annot != NULL)
3809 xmlSchemaFreeAnnot(attrGr->annot);
3810 if (attrGr->attrUses != NULL)
3811 xmlSchemaItemListFree(WXS_LIST_CAST attrGr->attrUses);
3812 xmlFree(attrGr);
Daniel Veillard3646d642004-06-02 19:19:14 +00003813}
3814
3815/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003816 * xmlSchemaFreeQNameRef:
3817 * @item: a QName reference structure
3818 *
3819 * Deallocatea a QName reference structure.
3820 */
3821static void
3822xmlSchemaFreeQNameRef(xmlSchemaQNameRefPtr item)
3823{
3824 xmlFree(item);
3825}
3826
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003827/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00003828 * xmlSchemaFreeTypeLinkList:
3829 * @alink: a type link
3830 *
3831 * Deallocate a list of types.
3832 */
3833static void
3834xmlSchemaFreeTypeLinkList(xmlSchemaTypeLinkPtr link)
3835{
3836 xmlSchemaTypeLinkPtr next;
3837
3838 while (link != NULL) {
3839 next = link->next;
3840 xmlFree(link);
3841 link = next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003842 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003843}
3844
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003845static void
3846xmlSchemaFreeIDCStateObjList(xmlSchemaIDCStateObjPtr sto)
3847{
3848 xmlSchemaIDCStateObjPtr next;
3849 while (sto != NULL) {
3850 next = sto->next;
3851 if (sto->history != NULL)
3852 xmlFree(sto->history);
3853 if (sto->xpathCtxt != NULL)
3854 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
3855 xmlFree(sto);
3856 sto = next;
3857 }
3858}
3859
3860/**
3861 * xmlSchemaFreeIDC:
3862 * @idc: a identity-constraint definition
3863 *
3864 * Deallocates an identity-constraint definition.
3865 */
3866static void
3867xmlSchemaFreeIDC(xmlSchemaIDCPtr idcDef)
3868{
3869 xmlSchemaIDCSelectPtr cur, prev;
3870
3871 if (idcDef == NULL)
3872 return;
3873 if (idcDef->annot != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003874 xmlSchemaFreeAnnot(idcDef->annot);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003875 /* Selector */
3876 if (idcDef->selector != NULL) {
3877 if (idcDef->selector->xpathComp != NULL)
3878 xmlFreePattern((xmlPatternPtr) idcDef->selector->xpathComp);
3879 xmlFree(idcDef->selector);
3880 }
3881 /* Fields */
3882 if (idcDef->fields != NULL) {
3883 cur = idcDef->fields;
3884 do {
3885 prev = cur;
3886 cur = cur->next;
3887 if (prev->xpathComp != NULL)
3888 xmlFreePattern((xmlPatternPtr) prev->xpathComp);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003889 xmlFree(prev);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003890 } while (cur != NULL);
3891 }
3892 xmlFree(idcDef);
3893}
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003894
Daniel Veillard01fa6152004-06-29 17:04:39 +00003895/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003896 * xmlSchemaFreeElement:
3897 * @schema: a schema element structure
3898 *
3899 * Deallocate a Schema Element structure.
3900 */
3901static void
3902xmlSchemaFreeElement(xmlSchemaElementPtr elem)
3903{
3904 if (elem == NULL)
3905 return;
Daniel Veillard32370232002-10-16 14:08:14 +00003906 if (elem->annot != NULL)
3907 xmlSchemaFreeAnnot(elem->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003908 if (elem->contModel != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003909 xmlRegFreeRegexp(elem->contModel);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003910 if (elem->defVal != NULL)
3911 xmlSchemaFreeValue(elem->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003912 xmlFree(elem);
3913}
3914
3915/**
3916 * xmlSchemaFreeFacet:
3917 * @facet: a schema facet structure
3918 *
3919 * Deallocate a Schema Facet structure.
3920 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003921void
Daniel Veillard4255d502002-04-16 15:50:10 +00003922xmlSchemaFreeFacet(xmlSchemaFacetPtr facet)
3923{
3924 if (facet == NULL)
3925 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003926 if (facet->val != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003927 xmlSchemaFreeValue(facet->val);
Daniel Veillard4255d502002-04-16 15:50:10 +00003928 if (facet->regexp != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003929 xmlRegFreeRegexp(facet->regexp);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003930 if (facet->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003931 xmlSchemaFreeAnnot(facet->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003932 xmlFree(facet);
3933}
3934
3935/**
3936 * xmlSchemaFreeType:
3937 * @type: a schema type structure
3938 *
3939 * Deallocate a Schema Type structure.
3940 */
3941void
3942xmlSchemaFreeType(xmlSchemaTypePtr type)
3943{
3944 if (type == NULL)
3945 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003946 if (type->annot != NULL)
Daniel Veillard32370232002-10-16 14:08:14 +00003947 xmlSchemaFreeAnnot(type->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003948 if (type->facets != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003949 xmlSchemaFacetPtr facet, next;
Daniel Veillard4255d502002-04-16 15:50:10 +00003950
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003951 facet = type->facets;
3952 while (facet != NULL) {
3953 next = facet->next;
3954 xmlSchemaFreeFacet(facet);
3955 facet = next;
3956 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003957 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003958 if (type->attrUses != NULL)
3959 xmlSchemaItemListFree((xmlSchemaItemListPtr) type->attrUses);
Daniel Veillard01fa6152004-06-29 17:04:39 +00003960 if (type->memberTypes != NULL)
3961 xmlSchemaFreeTypeLinkList(type->memberTypes);
3962 if (type->facetSet != NULL) {
3963 xmlSchemaFacetLinkPtr next, link;
3964
3965 link = type->facetSet;
3966 do {
3967 next = link->next;
3968 xmlFree(link);
3969 link = next;
3970 } while (link != NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003971 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00003972 if (type->contModel != NULL)
3973 xmlRegFreeRegexp(type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +00003974 xmlFree(type);
3975}
3976
3977/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003978 * xmlSchemaFreeModelGroupDef:
3979 * @item: a schema model group definition
3980 *
3981 * Deallocates a schema model group definition.
3982 */
3983static void
3984xmlSchemaFreeModelGroupDef(xmlSchemaModelGroupDefPtr item)
3985{
3986 if (item->annot != NULL)
3987 xmlSchemaFreeAnnot(item->annot);
3988 xmlFree(item);
3989}
3990
3991/**
3992 * xmlSchemaFreeModelGroup:
3993 * @item: a schema model group
3994 *
3995 * Deallocates a schema model group structure.
3996 */
3997static void
3998xmlSchemaFreeModelGroup(xmlSchemaModelGroupPtr item)
3999{
4000 if (item->annot != NULL)
4001 xmlSchemaFreeAnnot(item->annot);
4002 xmlFree(item);
4003}
4004
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004005static void
4006xmlSchemaComponentListFree(xmlSchemaItemListPtr list)
4007{
4008 if ((list == NULL) || (list->nbItems == 0))
4009 return;
4010 {
4011 xmlSchemaTreeItemPtr item;
4012 xmlSchemaTreeItemPtr *items = (xmlSchemaTreeItemPtr *) list->items;
4013 int i;
4014
4015 for (i = 0; i < list->nbItems; i++) {
4016 item = items[i];
4017 if (item == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +00004018 continue;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004019 switch (item->type) {
4020 case XML_SCHEMA_TYPE_SIMPLE:
4021 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004022 xmlSchemaFreeType((xmlSchemaTypePtr) item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004023 break;
4024 case XML_SCHEMA_TYPE_ATTRIBUTE:
4025 xmlSchemaFreeAttribute((xmlSchemaAttributePtr) item);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004026 break;
4027 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
4028 xmlSchemaFreeAttributeUse((xmlSchemaAttributeUsePtr) item);
4029 break;
4030 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
4031 xmlSchemaFreeAttributeUseProhib(
4032 (xmlSchemaAttributeUseProhibPtr) item);
4033 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004034 case XML_SCHEMA_TYPE_ELEMENT:
4035 xmlSchemaFreeElement((xmlSchemaElementPtr) item);
4036 break;
4037 case XML_SCHEMA_TYPE_PARTICLE:
4038 if (item->annot != NULL)
4039 xmlSchemaFreeAnnot(item->annot);
4040 xmlFree(item);
4041 break;
4042 case XML_SCHEMA_TYPE_SEQUENCE:
4043 case XML_SCHEMA_TYPE_CHOICE:
4044 case XML_SCHEMA_TYPE_ALL:
4045 xmlSchemaFreeModelGroup((xmlSchemaModelGroupPtr) item);
4046 break;
4047 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
4048 xmlSchemaFreeAttributeGroup(
4049 (xmlSchemaAttributeGroupPtr) item);
4050 break;
4051 case XML_SCHEMA_TYPE_GROUP:
4052 xmlSchemaFreeModelGroupDef(
4053 (xmlSchemaModelGroupDefPtr) item);
4054 break;
4055 case XML_SCHEMA_TYPE_ANY:
4056 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
4057 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr) item);
4058 break;
4059 case XML_SCHEMA_TYPE_IDC_KEY:
4060 case XML_SCHEMA_TYPE_IDC_UNIQUE:
4061 case XML_SCHEMA_TYPE_IDC_KEYREF:
4062 xmlSchemaFreeIDC((xmlSchemaIDCPtr) item);
4063 break;
4064 case XML_SCHEMA_TYPE_NOTATION:
4065 xmlSchemaFreeNotation((xmlSchemaNotationPtr) item);
4066 break;
4067 case XML_SCHEMA_EXTRA_QNAMEREF:
4068 xmlSchemaFreeQNameRef((xmlSchemaQNameRefPtr) item);
4069 break;
4070 default: {
4071 /* TODO: This should never be hit. */
4072 xmlSchemaPSimpleInternalErr(NULL,
4073 "Internal error: xmlSchemaComponentListFree, "
4074 "unexpected component type '%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004075 (const xmlChar *) WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004076 }
4077 break;
4078 }
4079 }
4080 list->nbItems = 0;
4081 }
4082}
4083
Daniel Veillardb0f397e2003-12-23 23:30:53 +00004084/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004085 * xmlSchemaFree:
4086 * @schema: a schema structure
4087 *
4088 * Deallocate a Schema structure.
4089 */
4090void
4091xmlSchemaFree(xmlSchemaPtr schema)
4092{
4093 if (schema == NULL)
4094 return;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004095 /* @volatiles is not used anymore :-/ */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004096 if (schema->volatiles != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004097 TODO
4098 /*
4099 * Note that those slots are not responsible for freeing
4100 * schema components anymore; this will now be done by
4101 * the schema buckets.
4102 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004103 if (schema->notaDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004104 xmlHashFree(schema->notaDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004105 if (schema->attrDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004106 xmlHashFree(schema->attrDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004107 if (schema->attrgrpDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004108 xmlHashFree(schema->attrgrpDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004109 if (schema->elemDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004110 xmlHashFree(schema->elemDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004111 if (schema->typeDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004112 xmlHashFree(schema->typeDecl, NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004113 if (schema->groupDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004114 xmlHashFree(schema->groupDecl, NULL);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004115 if (schema->idcDef != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004116 xmlHashFree(schema->idcDef, NULL);
4117
Daniel Veillard1d913862003-11-21 00:28:39 +00004118 if (schema->schemasImports != NULL)
4119 xmlHashFree(schema->schemasImports,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004120 (xmlHashDeallocator) xmlSchemaBucketFree);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004121 if (schema->includes != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004122 xmlSchemaItemListPtr list = (xmlSchemaItemListPtr) schema->includes;
4123 int i;
4124 for (i = 0; i < list->nbItems; i++) {
Daniel Veillarddee23482008-04-11 12:58:43 +00004125 xmlSchemaBucketFree((xmlSchemaBucketPtr) list->items[i]);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004126 }
4127 xmlSchemaItemListFree(list);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004128 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004129 if (schema->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004130 xmlSchemaFreeAnnot(schema->annot);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004131 /* Never free the doc here, since this will be done by the buckets. */
4132
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004133 xmlDictFree(schema->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00004134 xmlFree(schema);
4135}
4136
4137/************************************************************************
4138 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00004139 * Debug functions *
4140 * *
4141 ************************************************************************/
4142
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004143#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004144
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004145static void
4146xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output); /* forward */
4147
Daniel Veillard4255d502002-04-16 15:50:10 +00004148/**
4149 * xmlSchemaElementDump:
4150 * @elem: an element
4151 * @output: the file output
4152 *
4153 * Dump the element
4154 */
4155static void
4156xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004157 const xmlChar * name ATTRIBUTE_UNUSED,
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004158 const xmlChar * namespace ATTRIBUTE_UNUSED,
4159 const xmlChar * context ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00004160{
4161 if (elem == NULL)
4162 return;
4163
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004164
4165 fprintf(output, "Element");
4166 if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL)
4167 fprintf(output, " (global)");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004168 fprintf(output, ": '%s' ", elem->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004169 if (namespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004170 fprintf(output, "ns '%s'", namespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004171 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004172#if 0
Daniel Veillard4255d502002-04-16 15:50:10 +00004173 if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004174 fprintf(output, " min %d ", elem->minOccurs);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004175 if (elem->maxOccurs >= UNBOUNDED)
4176 fprintf(output, "max: unbounded\n");
4177 else if (elem->maxOccurs != 1)
4178 fprintf(output, "max: %d\n", elem->maxOccurs);
4179 else
4180 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004181 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004182#endif
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004183 /*
4184 * Misc other properties.
4185 */
4186 if ((elem->flags & XML_SCHEMAS_ELEM_NILLABLE) ||
4187 (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) ||
4188 (elem->flags & XML_SCHEMAS_ELEM_FIXED) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004189 (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004190 fprintf(output, " props: ");
4191 if (elem->flags & XML_SCHEMAS_ELEM_FIXED)
4192 fprintf(output, "[fixed] ");
4193 if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)
4194 fprintf(output, "[default] ");
4195 if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT)
4196 fprintf(output, "[abstract] ");
4197 if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE)
4198 fprintf(output, "[nillable] ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004199 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004200 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004201 /*
4202 * Default/fixed value.
4203 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004204 if (elem->value != NULL)
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004205 fprintf(output, " value: '%s'\n", elem->value);
4206 /*
4207 * Type.
4208 */
4209 if (elem->namedType != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004210 fprintf(output, " type: '%s' ", elem->namedType);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004211 if (elem->namedTypeNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004212 fprintf(output, "ns '%s'\n", elem->namedTypeNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004213 else
4214 fprintf(output, "\n");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004215 } else if (elem->subtypes != NULL) {
4216 /*
4217 * Dump local types.
4218 */
4219 xmlSchemaTypeDump(elem->subtypes, output);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004220 }
4221 /*
4222 * Substitution group.
4223 */
4224 if (elem->substGroup != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004225 fprintf(output, " substitutionGroup: '%s' ", elem->substGroup);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004226 if (elem->substGroupNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004227 fprintf(output, "ns '%s'\n", elem->substGroupNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004228 else
4229 fprintf(output, "\n");
4230 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004231}
4232
4233/**
4234 * xmlSchemaAnnotDump:
4235 * @output: the file output
4236 * @annot: a annotation
4237 *
4238 * Dump the annotation
4239 */
4240static void
4241xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot)
4242{
4243 xmlChar *content;
4244
4245 if (annot == NULL)
4246 return;
4247
4248 content = xmlNodeGetContent(annot->content);
4249 if (content != NULL) {
4250 fprintf(output, " Annot: %s\n", content);
4251 xmlFree(content);
4252 } else
4253 fprintf(output, " Annot: empty\n");
4254}
4255
4256/**
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004257 * xmlSchemaContentModelDump:
4258 * @particle: the schema particle
4259 * @output: the file output
4260 * @depth: the depth used for intentation
Daniel Veillard4255d502002-04-16 15:50:10 +00004261 *
4262 * Dump a SchemaType structure
4263 */
4264static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004265xmlSchemaContentModelDump(xmlSchemaParticlePtr particle, FILE * output, int depth)
4266{
4267 xmlChar *str = NULL;
4268 xmlSchemaTreeItemPtr term;
4269 char shift[100];
4270 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004271
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004272 if (particle == NULL)
4273 return;
4274 for (i = 0;((i < depth) && (i < 25));i++)
4275 shift[2 * i] = shift[2 * i + 1] = ' ';
4276 shift[2 * i] = shift[2 * i + 1] = 0;
Daniel Veillardbccae2d2009-06-04 11:22:45 +02004277 fprintf(output, "%s", shift);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004278 if (particle->children == NULL) {
4279 fprintf(output, "MISSING particle term\n");
4280 return;
4281 }
4282 term = particle->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004283 if (term == NULL) {
4284 fprintf(output, "(NULL)");
4285 } else {
4286 switch (term->type) {
4287 case XML_SCHEMA_TYPE_ELEMENT:
4288 fprintf(output, "ELEM '%s'", xmlSchemaFormatQName(&str,
4289 ((xmlSchemaElementPtr)term)->targetNamespace,
4290 ((xmlSchemaElementPtr)term)->name));
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004291 FREE_AND_NULL(str);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004292 break;
4293 case XML_SCHEMA_TYPE_SEQUENCE:
4294 fprintf(output, "SEQUENCE");
4295 break;
4296 case XML_SCHEMA_TYPE_CHOICE:
4297 fprintf(output, "CHOICE");
4298 break;
4299 case XML_SCHEMA_TYPE_ALL:
4300 fprintf(output, "ALL");
4301 break;
4302 case XML_SCHEMA_TYPE_ANY:
4303 fprintf(output, "ANY");
4304 break;
4305 default:
4306 fprintf(output, "UNKNOWN\n");
4307 return;
4308 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004309 }
4310 if (particle->minOccurs != 1)
4311 fprintf(output, " min: %d", particle->minOccurs);
4312 if (particle->maxOccurs >= UNBOUNDED)
4313 fprintf(output, " max: unbounded");
4314 else if (particle->maxOccurs != 1)
4315 fprintf(output, " max: %d", particle->maxOccurs);
4316 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004317 if (term &&
4318 ((term->type == XML_SCHEMA_TYPE_SEQUENCE) ||
4319 (term->type == XML_SCHEMA_TYPE_CHOICE) ||
4320 (term->type == XML_SCHEMA_TYPE_ALL)) &&
4321 (term->children != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004322 xmlSchemaContentModelDump((xmlSchemaParticlePtr) term->children,
4323 output, depth +1);
4324 }
4325 if (particle->next != NULL)
4326 xmlSchemaContentModelDump((xmlSchemaParticlePtr) particle->next,
4327 output, depth);
4328}
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004329
4330/**
4331 * xmlSchemaAttrUsesDump:
4332 * @uses: attribute uses list
Daniel Veillarddee23482008-04-11 12:58:43 +00004333 * @output: the file output
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004334 *
4335 * Dumps a list of attribute use components.
4336 */
4337static void
4338xmlSchemaAttrUsesDump(xmlSchemaItemListPtr uses, FILE * output)
4339{
4340 xmlSchemaAttributeUsePtr use;
4341 xmlSchemaAttributeUseProhibPtr prohib;
4342 xmlSchemaQNameRefPtr ref;
4343 const xmlChar *name, *tns;
4344 xmlChar *str = NULL;
4345 int i;
4346
4347 if ((uses == NULL) || (uses->nbItems == 0))
4348 return;
4349
Daniel Veillarddee23482008-04-11 12:58:43 +00004350 fprintf(output, " attributes:\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004351 for (i = 0; i < uses->nbItems; i++) {
4352 use = uses->items[i];
4353 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
4354 fprintf(output, " [prohibition] ");
4355 prohib = (xmlSchemaAttributeUseProhibPtr) use;
4356 name = prohib->name;
4357 tns = prohib->targetNamespace;
4358 } else if (use->type == XML_SCHEMA_EXTRA_QNAMEREF) {
4359 fprintf(output, " [reference] ");
4360 ref = (xmlSchemaQNameRefPtr) use;
4361 name = ref->name;
4362 tns = ref->targetNamespace;
4363 } else {
4364 fprintf(output, " [use] ");
4365 name = WXS_ATTRUSE_DECL_NAME(use);
4366 tns = WXS_ATTRUSE_DECL_TNS(use);
4367 }
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004368 fprintf(output, "'%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004369 (const char *) xmlSchemaFormatQName(&str, tns, name));
4370 FREE_AND_NULL(str);
4371 }
4372}
4373
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004374/**
4375 * xmlSchemaTypeDump:
4376 * @output: the file output
4377 * @type: a type structure
4378 *
4379 * Dump a SchemaType structure
4380 */
4381static void
Daniel Veillard4255d502002-04-16 15:50:10 +00004382xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output)
4383{
4384 if (type == NULL) {
4385 fprintf(output, "Type: NULL\n");
4386 return;
4387 }
4388 fprintf(output, "Type: ");
4389 if (type->name != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004390 fprintf(output, "'%s' ", type->name);
Daniel Veillard4255d502002-04-16 15:50:10 +00004391 else
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004392 fprintf(output, "(no name) ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004393 if (type->targetNamespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004394 fprintf(output, "ns '%s' ", type->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004395 switch (type->type) {
4396 case XML_SCHEMA_TYPE_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004397 fprintf(output, "[basic] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004398 break;
4399 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004400 fprintf(output, "[simple] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004401 break;
4402 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004403 fprintf(output, "[complex] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004404 break;
4405 case XML_SCHEMA_TYPE_SEQUENCE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004406 fprintf(output, "[sequence] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004407 break;
4408 case XML_SCHEMA_TYPE_CHOICE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004409 fprintf(output, "[choice] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004410 break;
4411 case XML_SCHEMA_TYPE_ALL:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004412 fprintf(output, "[all] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004413 break;
4414 case XML_SCHEMA_TYPE_UR:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004415 fprintf(output, "[ur] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004416 break;
4417 case XML_SCHEMA_TYPE_RESTRICTION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004418 fprintf(output, "[restriction] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004419 break;
4420 case XML_SCHEMA_TYPE_EXTENSION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004421 fprintf(output, "[extension] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004422 break;
4423 default:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004424 fprintf(output, "[unknown type %d] ", type->type);
Daniel Veillard4255d502002-04-16 15:50:10 +00004425 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004426 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004427 fprintf(output, "content: ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004428 switch (type->contentType) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004429 case XML_SCHEMA_CONTENT_UNKNOWN:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004430 fprintf(output, "[unknown] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004431 break;
4432 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004433 fprintf(output, "[empty] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004434 break;
4435 case XML_SCHEMA_CONTENT_ELEMENTS:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004436 fprintf(output, "[element] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004437 break;
4438 case XML_SCHEMA_CONTENT_MIXED:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004439 fprintf(output, "[mixed] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004440 break;
4441 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004442 /* not used. */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004443 break;
4444 case XML_SCHEMA_CONTENT_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004445 fprintf(output, "[basic] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004446 break;
4447 case XML_SCHEMA_CONTENT_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004448 fprintf(output, "[simple] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004449 break;
4450 case XML_SCHEMA_CONTENT_ANY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004451 fprintf(output, "[any] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004452 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004453 }
4454 fprintf(output, "\n");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004455 if (type->base != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004456 fprintf(output, " base type: '%s'", type->base);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004457 if (type->baseNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004458 fprintf(output, " ns '%s'\n", type->baseNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004459 else
4460 fprintf(output, "\n");
4461 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004462 if (type->attrUses != NULL)
4463 xmlSchemaAttrUsesDump(type->attrUses, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004464 if (type->annot != NULL)
4465 xmlSchemaAnnotDump(output, type->annot);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004466#ifdef DUMP_CONTENT_MODEL
4467 if ((type->type == XML_SCHEMA_TYPE_COMPLEX) &&
4468 (type->subtypes != NULL)) {
4469 xmlSchemaContentModelDump((xmlSchemaParticlePtr) type->subtypes,
4470 output, 1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004471 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004472#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004473}
4474
4475/**
4476 * xmlSchemaDump:
4477 * @output: the file output
4478 * @schema: a schema structure
4479 *
4480 * Dump a Schema structure.
4481 */
4482void
4483xmlSchemaDump(FILE * output, xmlSchemaPtr schema)
4484{
Daniel Veillardce682bc2004-11-05 17:22:25 +00004485 if (output == NULL)
4486 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004487 if (schema == NULL) {
4488 fprintf(output, "Schemas: NULL\n");
4489 return;
4490 }
4491 fprintf(output, "Schemas: ");
4492 if (schema->name != NULL)
4493 fprintf(output, "%s, ", schema->name);
4494 else
4495 fprintf(output, "no name, ");
4496 if (schema->targetNamespace != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00004497 fprintf(output, "%s", (const char *) schema->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004498 else
4499 fprintf(output, "no target namespace");
4500 fprintf(output, "\n");
4501 if (schema->annot != NULL)
4502 xmlSchemaAnnotDump(output, schema->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00004503 xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump,
4504 output);
4505 xmlHashScanFull(schema->elemDecl,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004506 (xmlHashScannerFull) xmlSchemaElementDump, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004507}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004508
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004509#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004510/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004511 * xmlSchemaDebugDumpIDCTable:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004512 * @vctxt: the WXS validation context
4513 *
4514 * Displays the current IDC table for debug purposes.
4515 */
4516static void
4517xmlSchemaDebugDumpIDCTable(FILE * output,
4518 const xmlChar *namespaceName,
4519 const xmlChar *localName,
4520 xmlSchemaPSVIIDCBindingPtr bind)
4521{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004522 xmlChar *str = NULL;
4523 const xmlChar *value;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004524 xmlSchemaPSVIIDCNodePtr tab;
4525 xmlSchemaPSVIIDCKeyPtr key;
4526 int i, j, res;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004527
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004528 fprintf(output, "IDC: TABLES on '%s'\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004529 xmlSchemaFormatQName(&str, namespaceName, localName));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004530 FREE_AND_NULL(str)
4531
4532 if (bind == NULL)
4533 return;
4534 do {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004535 fprintf(output, "IDC: BINDING '%s' (%d)\n",
Daniel Veillarddee23482008-04-11 12:58:43 +00004536 xmlSchemaGetComponentQName(&str,
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004537 bind->definition), bind->nbNodes);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004538 FREE_AND_NULL(str)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004539 for (i = 0; i < bind->nbNodes; i++) {
4540 tab = bind->nodeTable[i];
4541 fprintf(output, " ( ");
4542 for (j = 0; j < bind->definition->nbFields; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004543 key = tab->keys[j];
4544 if ((key != NULL) && (key->val != NULL)) {
4545 res = xmlSchemaGetCanonValue(key->val, &value);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004546 if (res >= 0)
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004547 fprintf(output, "'%s' ", value);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004548 else
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004549 fprintf(output, "CANON-VALUE-FAILED ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004550 if (res == 0)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004551 FREE_AND_NULL(value)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004552 } else if (key != NULL)
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004553 fprintf(output, "(no val), ");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004554 else
4555 fprintf(output, "(key missing), ");
4556 }
4557 fprintf(output, ")\n");
4558 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004559 if (bind->dupls && bind->dupls->nbItems) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004560 fprintf(output, "IDC: dupls (%d):\n", bind->dupls->nbItems);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004561 for (i = 0; i < bind->dupls->nbItems; i++) {
4562 tab = bind->dupls->items[i];
4563 fprintf(output, " ( ");
4564 for (j = 0; j < bind->definition->nbFields; j++) {
4565 key = tab->keys[j];
4566 if ((key != NULL) && (key->val != NULL)) {
4567 res = xmlSchemaGetCanonValue(key->val, &value);
4568 if (res >= 0)
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004569 fprintf(output, "'%s' ", value);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004570 else
4571 fprintf(output, "CANON-VALUE-FAILED ");
4572 if (res == 0)
4573 FREE_AND_NULL(value)
4574 } else if (key != NULL)
4575 fprintf(output, "(no val), ");
4576 else
4577 fprintf(output, "(key missing), ");
4578 }
4579 fprintf(output, ")\n");
4580 }
4581 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004582 bind = bind->next;
4583 } while (bind != NULL);
4584}
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00004585#endif /* DEBUG_IDC */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004586#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4255d502002-04-16 15:50:10 +00004587
4588/************************************************************************
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004589 * *
4590 * Utilities *
4591 * *
4592 ************************************************************************/
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004593
Daniel Veillardc0826a72004-08-10 14:17:33 +00004594/**
4595 * xmlSchemaGetPropNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004596 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004597 * @name: the name of the attribute
4598 *
4599 * Seeks an attribute with a name of @name in
4600 * no namespace.
4601 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004602 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004603 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004604static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004605xmlSchemaGetPropNode(xmlNodePtr node, const char *name)
Daniel Veillard01fa6152004-06-29 17:04:39 +00004606{
4607 xmlAttrPtr prop;
4608
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004609 if ((node == NULL) || (name == NULL))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004610 return(NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004611 prop = node->properties;
4612 while (prop != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004613 if ((prop->ns == NULL) && xmlStrEqual(prop->name, BAD_CAST name))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004614 return(prop);
4615 prop = prop->next;
4616 }
4617 return (NULL);
4618}
4619
4620/**
4621 * xmlSchemaGetPropNodeNs:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004622 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004623 * @uri: the uri
4624 * @name: the name of the attribute
4625 *
4626 * Seeks an attribute with a local name of @name and
4627 * a namespace URI of @uri.
4628 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004629 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004630 */
4631static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004632xmlSchemaGetPropNodeNs(xmlNodePtr node, const char *uri, const char *name)
Daniel Veillardc0826a72004-08-10 14:17:33 +00004633{
4634 xmlAttrPtr prop;
4635
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004636 if ((node == NULL) || (name == NULL))
Daniel Veillarddee23482008-04-11 12:58:43 +00004637 return(NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004638 prop = node->properties;
4639 while (prop != NULL) {
4640 if ((prop->ns != NULL) &&
4641 xmlStrEqual(prop->name, BAD_CAST name) &&
4642 xmlStrEqual(prop->ns->href, BAD_CAST uri))
Daniel Veillard01fa6152004-06-29 17:04:39 +00004643 return(prop);
4644 prop = prop->next;
4645 }
4646 return (NULL);
4647}
4648
4649static const xmlChar *
4650xmlSchemaGetNodeContent(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
4651{
4652 xmlChar *val;
4653 const xmlChar *ret;
4654
4655 val = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004656 if (val == NULL)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00004657 val = xmlStrdup((xmlChar *)"");
Daniel Veillard01fa6152004-06-29 17:04:39 +00004658 ret = xmlDictLookup(ctxt->dict, val, -1);
4659 xmlFree(val);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004660 return(ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004661}
4662
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004663static const xmlChar *
4664xmlSchemaGetNodeContentNoDict(xmlNodePtr node)
4665{
4666 return((const xmlChar*) xmlNodeGetContent(node));
4667}
4668
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004669/**
4670 * xmlSchemaGetProp:
4671 * @ctxt: the parser context
4672 * @node: the node
4673 * @name: the property name
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004674 *
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004675 * Read a attribute value and internalize the string
4676 *
4677 * Returns the string or NULL if not present.
4678 */
4679static const xmlChar *
4680xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
4681 const char *name)
4682{
4683 xmlChar *val;
4684 const xmlChar *ret;
4685
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004686 val = xmlGetNoNsProp(node, BAD_CAST name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004687 if (val == NULL)
4688 return(NULL);
4689 ret = xmlDictLookup(ctxt->dict, val, -1);
4690 xmlFree(val);
4691 return(ret);
4692}
4693
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004694/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +00004695 * *
4696 * Parsing functions *
4697 * *
4698 ************************************************************************/
4699
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004700#define WXS_FIND_GLOBAL_ITEM(slot) \
4701 if (xmlStrEqual(nsName, schema->targetNamespace)) { \
4702 ret = xmlHashLookup(schema->slot, name); \
4703 if (ret != NULL) goto exit; \
4704 } \
4705 if (xmlHashSize(schema->schemasImports) > 1) { \
4706 xmlSchemaImportPtr import; \
4707 if (nsName == NULL) \
4708 import = xmlHashLookup(schema->schemasImports, \
4709 XML_SCHEMAS_NO_NAMESPACE); \
4710 else \
4711 import = xmlHashLookup(schema->schemasImports, nsName); \
4712 if (import == NULL) \
4713 goto exit; \
4714 ret = xmlHashLookup(import->schema->slot, name); \
4715 }
4716
Daniel Veillard4255d502002-04-16 15:50:10 +00004717/**
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004718 * xmlSchemaGetElem:
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004719 * @schema: the schema context
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004720 * @name: the element name
4721 * @ns: the element namespace
4722 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004723 * Lookup a global element declaration in the schema.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004724 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004725 * Returns the element declaration or NULL if not found.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004726 */
4727static xmlSchemaElementPtr
4728xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004729 const xmlChar * nsName)
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004730{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004731 xmlSchemaElementPtr ret = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00004732
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004733 if ((name == NULL) || (schema == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004734 return(NULL);
4735 if (schema != NULL) {
4736 WXS_FIND_GLOBAL_ITEM(elemDecl)
Daniel Veillarddee23482008-04-11 12:58:43 +00004737 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004738exit:
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004739#ifdef DEBUG
4740 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004741 if (nsName == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004742 fprintf(stderr, "Unable to lookup element decl. %s", name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004743 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004744 fprintf(stderr, "Unable to lookup element decl. %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004745 nsName);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004746 }
4747#endif
4748 return (ret);
4749}
4750
4751/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004752 * xmlSchemaGetType:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004753 * @schema: the main schema
4754 * @name: the type's name
4755 * nsName: the type's namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00004756 *
4757 * Lookup a type in the schemas or the predefined types
4758 *
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004759 * Returns the group definition or NULL if not found.
Daniel Veillard4255d502002-04-16 15:50:10 +00004760 */
4761static xmlSchemaTypePtr
4762xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004763 const xmlChar * nsName)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004764{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004765 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00004766
4767 if (name == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +00004768 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004769 /* First try the built-in types. */
Daniel Veillarddee23482008-04-11 12:58:43 +00004770 if ((nsName != NULL) && xmlStrEqual(nsName, xmlSchemaNs)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004771 ret = xmlSchemaGetPredefinedType(name, nsName);
4772 if (ret != NULL)
4773 goto exit;
4774 /*
4775 * Note that we try the parsed schemas as well here
4776 * since one might have parsed the S4S, which contain more
4777 * than the built-in types.
4778 * TODO: Can we optimize this?
4779 */
4780 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004781 if (schema != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004782 WXS_FIND_GLOBAL_ITEM(typeDecl)
Daniel Veillarddee23482008-04-11 12:58:43 +00004783 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004784exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004785
Daniel Veillard4255d502002-04-16 15:50:10 +00004786#ifdef DEBUG
4787 if (ret == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004788 if (nsName == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004789 fprintf(stderr, "Unable to lookup type %s", name);
4790 else
4791 fprintf(stderr, "Unable to lookup type %s:%s", name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004792 nsName);
Daniel Veillard4255d502002-04-16 15:50:10 +00004793 }
4794#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004795 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004796}
4797
Daniel Veillard3646d642004-06-02 19:19:14 +00004798/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004799 * xmlSchemaGetAttributeDecl:
4800 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004801 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004802 * @ns: the target namespace of the attribute
Daniel Veillard3646d642004-06-02 19:19:14 +00004803 *
4804 * Lookup a an attribute in the schema or imported schemas
4805 *
4806 * Returns the attribute declaration or NULL if not found.
4807 */
4808static xmlSchemaAttributePtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004809xmlSchemaGetAttributeDecl(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004810 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004811{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004812 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004813
4814 if ((name == NULL) || (schema == NULL))
4815 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004816 if (schema != NULL) {
4817 WXS_FIND_GLOBAL_ITEM(attrDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004818 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004819exit:
Daniel Veillard3646d642004-06-02 19:19:14 +00004820#ifdef DEBUG
4821 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004822 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004823 fprintf(stderr, "Unable to lookup attribute %s", name);
4824 else
4825 fprintf(stderr, "Unable to lookup attribute %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004826 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004827 }
4828#endif
4829 return (ret);
4830}
4831
4832/**
4833 * xmlSchemaGetAttributeGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004834 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004835 * @name: the name of the attribute group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004836 * @ns: the target namespace of the attribute group
Daniel Veillard3646d642004-06-02 19:19:14 +00004837 *
4838 * Lookup a an attribute group in the schema or imported schemas
4839 *
4840 * Returns the attribute group definition or NULL if not found.
4841 */
4842static xmlSchemaAttributeGroupPtr
4843xmlSchemaGetAttributeGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004844 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004845{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004846 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004847
4848 if ((name == NULL) || (schema == NULL))
4849 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004850 if (schema != NULL) {
4851 WXS_FIND_GLOBAL_ITEM(attrgrpDecl)
4852 }
4853exit:
4854 /* TODO:
4855 if ((ret != NULL) && (ret->redef != NULL)) {
4856 * Return the last redefinition. *
4857 ret = ret->redef;
Daniel Veillard3646d642004-06-02 19:19:14 +00004858 }
William M. Brack2f2a6632004-08-20 23:09:47 +00004859 */
Daniel Veillard3646d642004-06-02 19:19:14 +00004860#ifdef DEBUG
4861 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004862 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004863 fprintf(stderr, "Unable to lookup attribute group %s", name);
4864 else
4865 fprintf(stderr, "Unable to lookup attribute group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004866 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004867 }
4868#endif
4869 return (ret);
4870}
4871
4872/**
4873 * xmlSchemaGetGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004874 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004875 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004876 * @ns: the target namespace of the group
Daniel Veillard3646d642004-06-02 19:19:14 +00004877 *
4878 * Lookup a group in the schema or imported schemas
4879 *
4880 * Returns the group definition or NULL if not found.
4881 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004882static xmlSchemaModelGroupDefPtr
Daniel Veillard3646d642004-06-02 19:19:14 +00004883xmlSchemaGetGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004884 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004885{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004886 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004887
4888 if ((name == NULL) || (schema == NULL))
4889 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004890 if (schema != NULL) {
4891 WXS_FIND_GLOBAL_ITEM(groupDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004892 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004893exit:
Daniel Veillarddee23482008-04-11 12:58:43 +00004894
Daniel Veillard3646d642004-06-02 19:19:14 +00004895#ifdef DEBUG
4896 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004897 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004898 fprintf(stderr, "Unable to lookup group %s", name);
4899 else
4900 fprintf(stderr, "Unable to lookup group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004901 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004902 }
4903#endif
4904 return (ret);
4905}
4906
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004907static xmlSchemaNotationPtr
4908xmlSchemaGetNotation(xmlSchemaPtr schema,
4909 const xmlChar *name,
4910 const xmlChar *nsName)
4911{
4912 xmlSchemaNotationPtr ret = NULL;
4913
4914 if ((name == NULL) || (schema == NULL))
4915 return (NULL);
4916 if (schema != NULL) {
4917 WXS_FIND_GLOBAL_ITEM(notaDecl)
4918 }
4919exit:
4920 return (ret);
4921}
4922
4923static xmlSchemaIDCPtr
4924xmlSchemaGetIDC(xmlSchemaPtr schema,
4925 const xmlChar *name,
4926 const xmlChar *nsName)
4927{
4928 xmlSchemaIDCPtr ret = NULL;
4929
4930 if ((name == NULL) || (schema == NULL))
4931 return (NULL);
4932 if (schema != NULL) {
4933 WXS_FIND_GLOBAL_ITEM(idcDef)
4934 }
4935exit:
4936 return (ret);
4937}
4938
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004939/**
4940 * xmlSchemaGetNamedComponent:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004941 * @schema: the schema
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004942 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004943 * @ns: the target namespace of the group
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004944 *
4945 * Lookup a group in the schema or imported schemas
4946 *
4947 * Returns the group definition or NULL if not found.
4948 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004949static xmlSchemaBasicItemPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004950xmlSchemaGetNamedComponent(xmlSchemaPtr schema,
4951 xmlSchemaTypeType itemType,
4952 const xmlChar *name,
4953 const xmlChar *targetNs)
4954{
4955 switch (itemType) {
4956 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004957 return ((xmlSchemaBasicItemPtr) xmlSchemaGetGroup(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004958 name, targetNs));
4959 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004960 return ((xmlSchemaBasicItemPtr) xmlSchemaGetElem(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004961 name, targetNs));
4962 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004963 TODO
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004964 return (NULL);
4965 }
4966}
4967
Daniel Veillard4255d502002-04-16 15:50:10 +00004968/************************************************************************
4969 * *
4970 * Parsing functions *
4971 * *
4972 ************************************************************************/
4973
4974#define IS_BLANK_NODE(n) \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004975 (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content, -1)))
Daniel Veillard4255d502002-04-16 15:50:10 +00004976
4977/**
4978 * xmlSchemaIsBlank:
4979 * @str: a string
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004980 * @len: the length of the string or -1
Daniel Veillard4255d502002-04-16 15:50:10 +00004981 *
4982 * Check if a string is ignorable
4983 *
4984 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
4985 */
4986static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004987xmlSchemaIsBlank(xmlChar * str, int len)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004988{
Daniel Veillard4255d502002-04-16 15:50:10 +00004989 if (str == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004990 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004991 if (len < 0) {
4992 while (*str != 0) {
4993 if (!(IS_BLANK_CH(*str)))
4994 return (0);
4995 str++;
4996 }
4997 } else while ((*str != 0) && (len != 0)) {
4998 if (!(IS_BLANK_CH(*str)))
4999 return (0);
5000 str++;
5001 len--;
Daniel Veillard4255d502002-04-16 15:50:10 +00005002 }
Daniel Veillarddee23482008-04-11 12:58:43 +00005003
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005004 return (1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005005}
5006
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005007#define WXS_COMP_NAME(c, t) ((t) (c))->name
5008#define WXS_COMP_TNS(c, t) ((t) (c))->targetNamespace
5009/*
5010* xmlSchemaFindRedefCompInGraph:
5011* ATTENTION TODO: This uses pointer comp. for strings.
5012*/
5013static xmlSchemaBasicItemPtr
5014xmlSchemaFindRedefCompInGraph(xmlSchemaBucketPtr bucket,
5015 xmlSchemaTypeType type,
5016 const xmlChar *name,
5017 const xmlChar *nsName)
5018{
5019 xmlSchemaBasicItemPtr ret;
5020 int i;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005021
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005022 if ((bucket == NULL) || (name == NULL))
5023 return(NULL);
5024 if ((bucket->globals == NULL) ||
5025 (bucket->globals->nbItems == 0))
5026 goto subschemas;
5027 /*
5028 * Search in global components.
5029 */
5030 for (i = 0; i < bucket->globals->nbItems; i++) {
5031 ret = bucket->globals->items[i];
5032 if (ret->type == type) {
5033 switch (type) {
5034 case XML_SCHEMA_TYPE_COMPLEX:
5035 case XML_SCHEMA_TYPE_SIMPLE:
5036 if ((WXS_COMP_NAME(ret, xmlSchemaTypePtr) == name) &&
5037 (WXS_COMP_TNS(ret, xmlSchemaTypePtr) ==
5038 nsName))
5039 {
5040 return(ret);
5041 }
5042 break;
5043 case XML_SCHEMA_TYPE_GROUP:
5044 if ((WXS_COMP_NAME(ret,
5045 xmlSchemaModelGroupDefPtr) == name) &&
5046 (WXS_COMP_TNS(ret,
5047 xmlSchemaModelGroupDefPtr) == nsName))
5048 {
5049 return(ret);
5050 }
5051 break;
5052 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
5053 if ((WXS_COMP_NAME(ret,
5054 xmlSchemaAttributeGroupPtr) == name) &&
5055 (WXS_COMP_TNS(ret,
5056 xmlSchemaAttributeGroupPtr) == nsName))
5057 {
5058 return(ret);
5059 }
Kasimier T. Buchcik5d2998b2005-11-22 17:36:01 +00005060 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005061 default:
5062 /* Should not be hit. */
5063 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00005064 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005065 }
5066 }
5067subschemas:
5068 /*
5069 * Process imported/included schemas.
5070 */
5071 if (bucket->relations != NULL) {
5072 xmlSchemaSchemaRelationPtr rel = bucket->relations;
5073
5074 /*
5075 * TODO: Marking the bucket will not avoid multiple searches
5076 * in the same schema, but avoids at least circularity.
5077 */
5078 bucket->flags |= XML_SCHEMA_BUCKET_MARKED;
5079 do {
5080 if ((rel->bucket != NULL) &&
5081 ((rel->bucket->flags & XML_SCHEMA_BUCKET_MARKED) == 0)) {
5082 ret = xmlSchemaFindRedefCompInGraph(rel->bucket,
5083 type, name, nsName);
5084 if (ret != NULL)
5085 return(ret);
5086 }
5087 rel = rel->next;
5088 } while (rel != NULL);
5089 bucket->flags ^= XML_SCHEMA_BUCKET_MARKED;
5090 }
5091 return(NULL);
5092}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005093
5094/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005095 * xmlSchemaAddNotation:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005096 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005097 * @schema: the schema being built
5098 * @name: the item name
5099 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00005100 * Add an XML schema annotation declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00005101 * *WARNING* this interface is highly subject to change
5102 *
5103 * Returns the new struture or NULL in case of error
5104 */
5105static xmlSchemaNotationPtr
5106xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005107 const xmlChar *name, const xmlChar *nsName,
5108 xmlNodePtr node ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00005109{
5110 xmlSchemaNotationPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005111
5112 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5113 return (NULL);
5114
Daniel Veillard4255d502002-04-16 15:50:10 +00005115 ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation));
5116 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005117 xmlSchemaPErrMemory(ctxt, "add annotation", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005118 return (NULL);
5119 }
5120 memset(ret, 0, sizeof(xmlSchemaNotation));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005121 ret->type = XML_SCHEMA_TYPE_NOTATION;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005122 ret->name = name;
5123 ret->targetNamespace = nsName;
5124 /* TODO: do we need the node to be set?
5125 * ret->node = node;*/
5126 WXS_ADD_GLOBAL(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005127 return (ret);
5128}
5129
Daniel Veillard4255d502002-04-16 15:50:10 +00005130/**
5131 * xmlSchemaAddAttribute:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005132 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005133 * @schema: the schema being built
5134 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005135 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005136 *
5137 * Add an XML schema Attrribute declaration
5138 * *WARNING* this interface is highly subject to change
5139 *
5140 * Returns the new struture or NULL in case of error
5141 */
5142static xmlSchemaAttributePtr
5143xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005144 const xmlChar * name, const xmlChar * nsName,
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00005145 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005146{
5147 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005148
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005149 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005150 return (NULL);
5151
5152 ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute));
5153 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005154 xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005155 return (NULL);
5156 }
5157 memset(ret, 0, sizeof(xmlSchemaAttribute));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005158 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE;
5159 ret->node = node;
5160 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005161 ret->targetNamespace = nsName;
5162
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005163 if (topLevel)
5164 WXS_ADD_GLOBAL(ctxt, ret);
5165 else
5166 WXS_ADD_LOCAL(ctxt, ret);
5167 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005168 return (ret);
5169}
5170
5171/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005172 * xmlSchemaAddAttributeUse:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005173 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005174 * @schema: the schema being built
5175 * @name: the item name
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005176 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005177 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005178 * Add an XML schema Attrribute declaration
5179 * *WARNING* this interface is highly subject to change
5180 *
5181 * Returns the new struture or NULL in case of error
5182 */
5183static xmlSchemaAttributeUsePtr
5184xmlSchemaAddAttributeUse(xmlSchemaParserCtxtPtr pctxt,
5185 xmlNodePtr node)
5186{
5187 xmlSchemaAttributeUsePtr ret = NULL;
5188
5189 if (pctxt == NULL)
5190 return (NULL);
5191
5192 ret = (xmlSchemaAttributeUsePtr) xmlMalloc(sizeof(xmlSchemaAttributeUse));
5193 if (ret == NULL) {
5194 xmlSchemaPErrMemory(pctxt, "allocating attribute", NULL);
5195 return (NULL);
5196 }
5197 memset(ret, 0, sizeof(xmlSchemaAttributeUse));
5198 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE_USE;
5199 ret->node = node;
5200
5201 WXS_ADD_LOCAL(pctxt, ret);
5202 return (ret);
5203}
5204
5205/*
5206* xmlSchemaAddRedef:
5207*
5208* Adds a redefinition information. This is used at a later stage to:
5209* resolve references to the redefined components and to check constraints.
5210*/
5211static xmlSchemaRedefPtr
Daniel Veillarddee23482008-04-11 12:58:43 +00005212xmlSchemaAddRedef(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005213 xmlSchemaBucketPtr targetBucket,
5214 void *item,
5215 const xmlChar *refName,
5216 const xmlChar *refTargetNs)
5217{
5218 xmlSchemaRedefPtr ret;
5219
5220 ret = (xmlSchemaRedefPtr)
5221 xmlMalloc(sizeof(xmlSchemaRedef));
5222 if (ret == NULL) {
5223 xmlSchemaPErrMemory(pctxt,
5224 "allocating redefinition info", NULL);
5225 return (NULL);
5226 }
5227 memset(ret, 0, sizeof(xmlSchemaRedef));
5228 ret->item = item;
5229 ret->targetBucket = targetBucket;
5230 ret->refName = refName;
5231 ret->refTargetNs = refTargetNs;
5232 if (WXS_CONSTRUCTOR(pctxt)->redefs == NULL)
5233 WXS_CONSTRUCTOR(pctxt)->redefs = ret;
5234 else
5235 WXS_CONSTRUCTOR(pctxt)->lastRedef->next = ret;
5236 WXS_CONSTRUCTOR(pctxt)->lastRedef = ret;
5237
5238 return (ret);
5239}
5240
5241/**
5242 * xmlSchemaAddAttributeGroupDefinition:
5243 * @ctxt: a schema parser context
5244 * @schema: the schema being built
5245 * @name: the item name
5246 * @nsName: the target namespace
5247 * @node: the corresponding node
5248 *
5249 * Add an XML schema Attrribute Group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00005250 *
5251 * Returns the new struture or NULL in case of error
5252 */
5253static xmlSchemaAttributeGroupPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005254xmlSchemaAddAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
5255 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
5256 const xmlChar *name,
5257 const xmlChar *nsName,
5258 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00005259{
5260 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005261
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005262 if ((pctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005263 return (NULL);
5264
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005265 ret = (xmlSchemaAttributeGroupPtr)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005266 xmlMalloc(sizeof(xmlSchemaAttributeGroup));
Daniel Veillard4255d502002-04-16 15:50:10 +00005267 if (ret == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005268 xmlSchemaPErrMemory(pctxt, "allocating attribute group", NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005269 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005270 }
5271 memset(ret, 0, sizeof(xmlSchemaAttributeGroup));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005272 ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005273 ret->name = name;
5274 ret->targetNamespace = nsName;
Daniel Veillarddee23482008-04-11 12:58:43 +00005275 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005276
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005277 /* TODO: Remove the flag. */
5278 ret->flags |= XML_SCHEMAS_ATTRGROUP_GLOBAL;
5279 if (pctxt->isRedefine) {
5280 pctxt->redef = xmlSchemaAddRedef(pctxt, pctxt->redefined,
5281 ret, name, nsName);
5282 if (pctxt->redef == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005283 xmlFree(ret);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005284 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00005285 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005286 pctxt->redefCounter = 0;
5287 }
5288 WXS_ADD_GLOBAL(pctxt, ret);
5289 WXS_ADD_PENDING(pctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005290 return (ret);
5291}
5292
5293/**
5294 * xmlSchemaAddElement:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005295 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005296 * @schema: the schema being built
5297 * @name: the type name
5298 * @namespace: the type namespace
5299 *
5300 * Add an XML schema Element declaration
5301 * *WARNING* this interface is highly subject to change
5302 *
5303 * Returns the new struture or NULL in case of error
5304 */
5305static xmlSchemaElementPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005306xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005307 const xmlChar * name, const xmlChar * nsName,
William M. Brack2f2a6632004-08-20 23:09:47 +00005308 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005309{
5310 xmlSchemaElementPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005311
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005312 if ((ctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005313 return (NULL);
5314
Daniel Veillard4255d502002-04-16 15:50:10 +00005315 ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement));
5316 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005317 xmlSchemaPErrMemory(ctxt, "allocating element", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005318 return (NULL);
5319 }
5320 memset(ret, 0, sizeof(xmlSchemaElement));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005321 ret->type = XML_SCHEMA_TYPE_ELEMENT;
5322 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005323 ret->targetNamespace = nsName;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005324 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005325
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005326 if (topLevel)
5327 WXS_ADD_GLOBAL(ctxt, ret);
5328 else
5329 WXS_ADD_LOCAL(ctxt, ret);
5330 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005331 return (ret);
5332}
5333
5334/**
5335 * xmlSchemaAddType:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005336 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005337 * @schema: the schema being built
5338 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005339 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005340 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00005341 * Add an XML schema item
Daniel Veillard4255d502002-04-16 15:50:10 +00005342 * *WARNING* this interface is highly subject to change
5343 *
5344 * Returns the new struture or NULL in case of error
5345 */
5346static xmlSchemaTypePtr
5347xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005348 xmlSchemaTypeType type,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00005349 const xmlChar * name, const xmlChar * nsName,
5350 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005351{
5352 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005353
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005354 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005355 return (NULL);
5356
5357 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
5358 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005359 xmlSchemaPErrMemory(ctxt, "allocating type", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005360 return (NULL);
5361 }
5362 memset(ret, 0, sizeof(xmlSchemaType));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005363 ret->type = type;
5364 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005365 ret->targetNamespace = nsName;
5366 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005367 if (topLevel) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005368 if (ctxt->isRedefine) {
5369 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5370 ret, name, nsName);
5371 if (ctxt->redef == NULL) {
5372 xmlFree(ret);
5373 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00005374 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005375 ctxt->redefCounter = 0;
5376 }
5377 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005378 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005379 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillarddee23482008-04-11 12:58:43 +00005380 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005381 return (ret);
5382}
5383
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005384static xmlSchemaQNameRefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005385xmlSchemaNewQNameRef(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005386 xmlSchemaTypeType refType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005387 const xmlChar *refName,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005388 const xmlChar *refNs)
5389{
5390 xmlSchemaQNameRefPtr ret;
5391
5392 ret = (xmlSchemaQNameRefPtr)
5393 xmlMalloc(sizeof(xmlSchemaQNameRef));
5394 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005395 xmlSchemaPErrMemory(pctxt,
5396 "allocating QName reference item", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005397 return (NULL);
5398 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005399 ret->node = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005400 ret->type = XML_SCHEMA_EXTRA_QNAMEREF;
5401 ret->name = refName;
5402 ret->targetNamespace = refNs;
5403 ret->item = NULL;
5404 ret->itemType = refType;
5405 /*
5406 * Store the reference item in the schema.
5407 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005408 WXS_ADD_LOCAL(pctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005409 return (ret);
5410}
5411
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005412static xmlSchemaAttributeUseProhibPtr
5413xmlSchemaAddAttributeUseProhib(xmlSchemaParserCtxtPtr pctxt)
5414{
5415 xmlSchemaAttributeUseProhibPtr ret;
5416
5417 ret = (xmlSchemaAttributeUseProhibPtr)
5418 xmlMalloc(sizeof(xmlSchemaAttributeUseProhib));
5419 if (ret == NULL) {
5420 xmlSchemaPErrMemory(pctxt,
5421 "allocating attribute use prohibition", NULL);
5422 return (NULL);
5423 }
5424 memset(ret, 0, sizeof(xmlSchemaAttributeUseProhib));
5425 ret->type = XML_SCHEMA_EXTRA_ATTR_USE_PROHIB;
5426 WXS_ADD_LOCAL(pctxt, ret);
5427 return (ret);
5428}
5429
5430
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005431/**
5432 * xmlSchemaAddModelGroup:
5433 * @ctxt: a schema parser context
5434 * @schema: the schema being built
5435 * @type: the "compositor" type of the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005436 * @node: the node in the schema doc
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005437 *
5438 * Adds a schema model group
5439 * *WARNING* this interface is highly subject to change
5440 *
5441 * Returns the new struture or NULL in case of error
5442 */
5443static xmlSchemaModelGroupPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005444xmlSchemaAddModelGroup(xmlSchemaParserCtxtPtr ctxt,
5445 xmlSchemaPtr schema,
5446 xmlSchemaTypeType type,
5447 xmlNodePtr node)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005448{
5449 xmlSchemaModelGroupPtr ret = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005450
5451 if ((ctxt == NULL) || (schema == NULL))
5452 return (NULL);
5453
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005454 ret = (xmlSchemaModelGroupPtr)
5455 xmlMalloc(sizeof(xmlSchemaModelGroup));
5456 if (ret == NULL) {
5457 xmlSchemaPErrMemory(ctxt, "allocating model group component",
5458 NULL);
5459 return (NULL);
5460 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005461 memset(ret, 0, sizeof(xmlSchemaModelGroup));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005462 ret->type = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005463 ret->node = node;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005464 WXS_ADD_LOCAL(ctxt, ret);
5465 if ((type == XML_SCHEMA_TYPE_SEQUENCE) ||
5466 (type == XML_SCHEMA_TYPE_CHOICE))
5467 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005468 return (ret);
5469}
5470
5471
5472/**
5473 * xmlSchemaAddParticle:
5474 * @ctxt: a schema parser context
5475 * @schema: the schema being built
5476 * @node: the corresponding node in the schema doc
5477 * @min: the minOccurs
5478 * @max: the maxOccurs
5479 *
5480 * Adds an XML schema particle component.
5481 * *WARNING* this interface is highly subject to change
5482 *
5483 * Returns the new struture or NULL in case of error
5484 */
5485static xmlSchemaParticlePtr
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00005486xmlSchemaAddParticle(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005487 xmlNodePtr node, int min, int max)
5488{
5489 xmlSchemaParticlePtr ret = NULL;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00005490 if (ctxt == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005491 return (NULL);
5492
5493#ifdef DEBUG
5494 fprintf(stderr, "Adding particle component\n");
5495#endif
5496 ret = (xmlSchemaParticlePtr)
5497 xmlMalloc(sizeof(xmlSchemaParticle));
5498 if (ret == NULL) {
5499 xmlSchemaPErrMemory(ctxt, "allocating particle component",
5500 NULL);
5501 return (NULL);
5502 }
5503 ret->type = XML_SCHEMA_TYPE_PARTICLE;
5504 ret->annot = NULL;
5505 ret->node = node;
5506 ret->minOccurs = min;
5507 ret->maxOccurs = max;
5508 ret->next = NULL;
5509 ret->children = NULL;
5510
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005511 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillarddee23482008-04-11 12:58:43 +00005512 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005513 * Note that addition to pending components will be done locally
5514 * to the specific parsing function, since the most particles
5515 * need not to be fixed up (i.e. the reference to be resolved).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005516 * REMOVED: WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005517 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005518 return (ret);
5519}
5520
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005521/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005522 * xmlSchemaAddModelGroupDefinition:
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005523 * @ctxt: a schema validation context
5524 * @schema: the schema being built
5525 * @name: the group name
5526 *
5527 * Add an XML schema Group definition
5528 *
5529 * Returns the new struture or NULL in case of error
5530 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005531static xmlSchemaModelGroupDefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005532xmlSchemaAddModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
5533 xmlSchemaPtr schema,
5534 const xmlChar *name,
5535 const xmlChar *nsName,
5536 xmlNodePtr node)
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005537{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005538 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005539
5540 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5541 return (NULL);
5542
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005543 ret = (xmlSchemaModelGroupDefPtr)
5544 xmlMalloc(sizeof(xmlSchemaModelGroupDef));
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005545 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005546 xmlSchemaPErrMemory(ctxt, "adding group", NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005547 return (NULL);
5548 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005549 memset(ret, 0, sizeof(xmlSchemaModelGroupDef));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005550 ret->name = name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005551 ret->type = XML_SCHEMA_TYPE_GROUP;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005552 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005553 ret->targetNamespace = nsName;
5554
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005555 if (ctxt->isRedefine) {
5556 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5557 ret, name, nsName);
5558 if (ctxt->redef == NULL) {
5559 xmlFree(ret);
5560 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00005561 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005562 ctxt->redefCounter = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005563 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005564 WXS_ADD_GLOBAL(ctxt, ret);
5565 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005566 return (ret);
5567}
5568
Daniel Veillard3646d642004-06-02 19:19:14 +00005569/**
5570 * xmlSchemaNewWildcardNs:
5571 * @ctxt: a schema validation context
5572 *
5573 * Creates a new wildcard namespace constraint.
5574 *
5575 * Returns the new struture or NULL in case of error
5576 */
5577static xmlSchemaWildcardNsPtr
5578xmlSchemaNewWildcardNsConstraint(xmlSchemaParserCtxtPtr ctxt)
5579{
5580 xmlSchemaWildcardNsPtr ret;
5581
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005582 ret = (xmlSchemaWildcardNsPtr)
Daniel Veillard3646d642004-06-02 19:19:14 +00005583 xmlMalloc(sizeof(xmlSchemaWildcardNs));
5584 if (ret == NULL) {
5585 xmlSchemaPErrMemory(ctxt, "creating wildcard namespace constraint", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005586 return (NULL);
Daniel Veillard3646d642004-06-02 19:19:14 +00005587 }
5588 ret->value = NULL;
5589 ret->next = NULL;
5590 return (ret);
5591}
5592
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005593static xmlSchemaIDCPtr
5594xmlSchemaAddIDC(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5595 const xmlChar *name, const xmlChar *nsName,
5596 int category, xmlNodePtr node)
5597{
Daniel Veillarddee23482008-04-11 12:58:43 +00005598 xmlSchemaIDCPtr ret = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005599
5600 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5601 return (NULL);
5602
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005603 ret = (xmlSchemaIDCPtr) xmlMalloc(sizeof(xmlSchemaIDC));
5604 if (ret == NULL) {
5605 xmlSchemaPErrMemory(ctxt,
5606 "allocating an identity-constraint definition", NULL);
5607 return (NULL);
5608 }
5609 memset(ret, 0, sizeof(xmlSchemaIDC));
5610 /* The target namespace of the parent element declaration. */
5611 ret->targetNamespace = nsName;
5612 ret->name = name;
5613 ret->type = category;
Daniel Veillarddee23482008-04-11 12:58:43 +00005614 ret->node = node;
5615
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005616 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005617 /*
5618 * Only keyrefs need to be fixup up.
5619 */
5620 if (category == XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005621 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005622 return (ret);
5623}
5624
Daniel Veillard3646d642004-06-02 19:19:14 +00005625/**
5626 * xmlSchemaAddWildcard:
5627 * @ctxt: a schema validation context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005628 * @schema: a schema
5629 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005630 * Adds a wildcard.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005631 * It corresponds to a xsd:anyAttribute and xsd:any.
Daniel Veillard3646d642004-06-02 19:19:14 +00005632 *
5633 * Returns the new struture or NULL in case of error
5634 */
5635static xmlSchemaWildcardPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005636xmlSchemaAddWildcard(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5637 xmlSchemaTypeType type, xmlNodePtr node)
Daniel Veillard3646d642004-06-02 19:19:14 +00005638{
5639 xmlSchemaWildcardPtr ret = NULL;
5640
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005641 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard3646d642004-06-02 19:19:14 +00005642 return (NULL);
5643
5644 ret = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
5645 if (ret == NULL) {
5646 xmlSchemaPErrMemory(ctxt, "adding wildcard", NULL);
5647 return (NULL);
5648 }
5649 memset(ret, 0, sizeof(xmlSchemaWildcard));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005650 ret->type = type;
Daniel Veillarddee23482008-04-11 12:58:43 +00005651 ret->node = node;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005652 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillard3646d642004-06-02 19:19:14 +00005653 return (ret);
5654}
5655
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005656static void
5657xmlSchemaSubstGroupFree(xmlSchemaSubstGroupPtr group)
5658{
5659 if (group == NULL)
5660 return;
5661 if (group->members != NULL)
5662 xmlSchemaItemListFree(group->members);
5663 xmlFree(group);
5664}
5665
5666static xmlSchemaSubstGroupPtr
5667xmlSchemaSubstGroupAdd(xmlSchemaParserCtxtPtr pctxt,
5668 xmlSchemaElementPtr head)
5669{
5670 xmlSchemaSubstGroupPtr ret;
5671
5672 /* Init subst group hash. */
5673 if (WXS_SUBST_GROUPS(pctxt) == NULL) {
5674 WXS_SUBST_GROUPS(pctxt) = xmlHashCreateDict(10, pctxt->dict);
5675 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5676 return(NULL);
5677 }
5678 /* Create a new substitution group. */
5679 ret = (xmlSchemaSubstGroupPtr) xmlMalloc(sizeof(xmlSchemaSubstGroup));
5680 if (ret == NULL) {
5681 xmlSchemaPErrMemory(NULL,
5682 "allocating a substitution group container", NULL);
5683 return(NULL);
5684 }
5685 memset(ret, 0, sizeof(xmlSchemaSubstGroup));
5686 ret->head = head;
5687 /* Create list of members. */
5688 ret->members = xmlSchemaItemListCreate();
5689 if (ret->members == NULL) {
5690 xmlSchemaSubstGroupFree(ret);
5691 return(NULL);
5692 }
5693 /* Add subst group to hash. */
5694 if (xmlHashAddEntry2(WXS_SUBST_GROUPS(pctxt),
5695 head->name, head->targetNamespace, ret) != 0) {
5696 PERROR_INT("xmlSchemaSubstGroupAdd",
5697 "failed to add a new substitution container");
5698 xmlSchemaSubstGroupFree(ret);
5699 return(NULL);
5700 }
5701 return(ret);
5702}
5703
5704static xmlSchemaSubstGroupPtr
5705xmlSchemaSubstGroupGet(xmlSchemaParserCtxtPtr pctxt,
5706 xmlSchemaElementPtr head)
Daniel Veillarddee23482008-04-11 12:58:43 +00005707{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005708 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5709 return(NULL);
5710 return(xmlHashLookup2(WXS_SUBST_GROUPS(pctxt),
5711 head->name, head->targetNamespace));
5712
5713}
5714
5715/**
5716 * xmlSchemaAddElementSubstitutionMember:
5717 * @pctxt: a schema parser context
5718 * @head: the head of the substitution group
5719 * @member: the new member of the substitution group
5720 *
5721 * Allocate a new annotation structure.
5722 *
5723 * Returns the newly allocated structure or NULL in case or error
5724 */
5725static int
5726xmlSchemaAddElementSubstitutionMember(xmlSchemaParserCtxtPtr pctxt,
5727 xmlSchemaElementPtr head,
5728 xmlSchemaElementPtr member)
5729{
5730 xmlSchemaSubstGroupPtr substGroup = NULL;
5731
5732 if ((pctxt == NULL) || (head == NULL) || (member == NULL))
5733 return (-1);
5734
5735 substGroup = xmlSchemaSubstGroupGet(pctxt, head);
5736 if (substGroup == NULL)
5737 substGroup = xmlSchemaSubstGroupAdd(pctxt, head);
5738 if (substGroup == NULL)
5739 return(-1);
5740 if (xmlSchemaItemListAdd(substGroup->members, member) == -1)
5741 return(-1);
5742 return(0);
5743}
5744
Daniel Veillard4255d502002-04-16 15:50:10 +00005745/************************************************************************
5746 * *
5747 * Utilities for parsing *
5748 * *
5749 ************************************************************************/
5750
Daniel Veillard4255d502002-04-16 15:50:10 +00005751/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00005752 * xmlSchemaPValAttrNodeQNameValue:
5753 * @ctxt: a schema parser context
5754 * @schema: the schema context
5755 * @ownerDes: the designation of the parent element
5756 * @ownerItem: the parent as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005757 * @value: the QName value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005758 * @local: the resulting local part if found, the attribute value otherwise
5759 * @uri: the resulting namespace URI if found
5760 *
5761 * Extracts the local name and the URI of a QName value and validates it.
5762 * This one is intended to be used on attribute values that
5763 * should resolve to schema components.
5764 *
5765 * Returns 0, in case the QName is valid, a positive error code
5766 * if not valid and -1 if an internal error occurs.
5767 */
5768static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005769xmlSchemaPValAttrNodeQNameValue(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005770 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005771 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005772 xmlAttrPtr attr,
5773 const xmlChar *value,
5774 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005775 const xmlChar **local)
5776{
5777 const xmlChar *pref;
5778 xmlNsPtr ns;
5779 int len, ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005780
Daniel Veillardc0826a72004-08-10 14:17:33 +00005781 *uri = NULL;
5782 *local = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005783 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005784 if (ret > 0) {
5785 xmlSchemaPSimpleTypeErr(ctxt,
5786 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5787 ownerItem, (xmlNodePtr) attr,
5788 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
5789 NULL, value, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005790 *local = value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005791 return (ctxt->err);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005792 } else if (ret < 0)
5793 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005794
5795 if (!strchr((char *) value, ':')) {
Daniel Veillard24505b02005-07-28 23:49:35 +00005796 ns = xmlSearchNs(attr->doc, attr->parent, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005797 if (ns)
5798 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
5799 else if (schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005800 /* TODO: move XML_SCHEMAS_INCLUDING_CONVERT_NS to the
5801 * parser context. */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005802 /*
5803 * This one takes care of included schemas with no
5804 * target namespace.
5805 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005806 *uri = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005807 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005808 *local = xmlDictLookup(ctxt->dict, value, -1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005809 return (0);
5810 }
5811 /*
5812 * At this point xmlSplitQName3 has to return a local name.
5813 */
5814 *local = xmlSplitQName3(value, &len);
5815 *local = xmlDictLookup(ctxt->dict, *local, -1);
5816 pref = xmlDictLookup(ctxt->dict, value, len);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005817 ns = xmlSearchNs(attr->doc, attr->parent, pref);
5818 if (ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005819 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005820 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005821 ownerItem, (xmlNodePtr) attr,
5822 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), NULL, value,
5823 "The value '%s' of simple type 'xs:QName' has no "
5824 "corresponding namespace declaration in scope", value, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005825 return (ctxt->err);
5826 } else {
5827 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005828 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00005829 return (0);
5830}
5831
5832/**
5833 * xmlSchemaPValAttrNodeQName:
5834 * @ctxt: a schema parser context
5835 * @schema: the schema context
5836 * @ownerDes: the designation of the owner element
5837 * @ownerItem: the owner as a schema object
5838 * @attr: the attribute node
5839 * @local: the resulting local part if found, the attribute value otherwise
5840 * @uri: the resulting namespace URI if found
5841 *
5842 * Extracts and validates the QName of an attribute value.
5843 * This one is intended to be used on attribute values that
5844 * should resolve to schema components.
5845 *
5846 * Returns 0, in case the QName is valid, a positive error code
5847 * if not valid and -1 if an internal error occurs.
5848 */
5849static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005850xmlSchemaPValAttrNodeQName(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005851 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005852 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005853 xmlAttrPtr attr,
5854 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005855 const xmlChar **local)
5856{
5857 const xmlChar *value;
5858
5859 value = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005860 return (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005861 ownerItem, attr, value, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005862}
5863
5864/**
5865 * xmlSchemaPValAttrQName:
5866 * @ctxt: a schema parser context
5867 * @schema: the schema context
5868 * @ownerDes: the designation of the parent element
5869 * @ownerItem: the owner as a schema object
5870 * @ownerElem: the parent node of the attribute
5871 * @name: the name of the attribute
5872 * @local: the resulting local part if found, the attribute value otherwise
5873 * @uri: the resulting namespace URI if found
5874 *
5875 * Extracts and validates the QName of an attribute value.
5876 *
5877 * Returns 0, in case the QName is valid, a positive error code
5878 * if not valid and -1 if an internal error occurs.
5879 */
5880static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005881xmlSchemaPValAttrQName(xmlSchemaParserCtxtPtr ctxt,
5882 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005883 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005884 xmlNodePtr ownerElem,
5885 const char *name,
5886 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005887 const xmlChar **local)
5888{
5889 xmlAttrPtr attr;
5890
5891 attr = xmlSchemaGetPropNode(ownerElem, name);
5892 if (attr == NULL) {
5893 *local = NULL;
5894 *uri = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005895 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005896 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005897 return (xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005898 ownerItem, attr, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005899}
5900
5901/**
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005902 * xmlSchemaPValAttrID:
5903 * @ctxt: a schema parser context
5904 * @schema: the schema context
5905 * @ownerDes: the designation of the parent element
5906 * @ownerItem: the owner as a schema object
5907 * @ownerElem: the parent node of the attribute
5908 * @name: the name of the attribute
5909 *
5910 * Extracts and validates the ID of an attribute value.
5911 *
5912 * Returns 0, in case the ID is valid, a positive error code
5913 * if not valid and -1 if an internal error occurs.
5914 */
5915static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005916xmlSchemaPValAttrNodeID(xmlSchemaParserCtxtPtr ctxt, xmlAttrPtr attr)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005917{
5918 int ret;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005919 const xmlChar *value;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005920
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005921 if (attr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005922 return(0);
5923 value = xmlSchemaGetNodeContentNoDict((xmlNodePtr) attr);
5924 ret = xmlValidateNCName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005925 if (ret == 0) {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005926 /*
5927 * NOTE: the IDness might have already be declared in the DTD
5928 */
5929 if (attr->atype != XML_ATTRIBUTE_ID) {
5930 xmlIDPtr res;
5931 xmlChar *strip;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005932
5933 /*
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005934 * TODO: Use xmlSchemaStrip here; it's not exported at this
5935 * moment.
5936 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005937 strip = xmlSchemaCollapseString(value);
5938 if (strip != NULL) {
5939 xmlFree((xmlChar *) value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005940 value = strip;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005941 }
5942 res = xmlAddID(NULL, attr->doc, value, attr);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005943 if (res == NULL) {
5944 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005945 xmlSchemaPSimpleTypeErr(ctxt,
5946 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005947 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005948 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5949 NULL, NULL, "Duplicate value '%s' of simple "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005950 "type 'xs:ID'", value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005951 } else
5952 attr->atype = XML_ATTRIBUTE_ID;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005953 }
5954 } else if (ret > 0) {
5955 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005956 xmlSchemaPSimpleTypeErr(ctxt,
5957 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005958 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005959 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5960 NULL, NULL, "The value '%s' of simple type 'xs:ID' is "
5961 "not a valid 'xs:NCName'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005962 value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005963 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005964 if (value != NULL)
5965 xmlFree((xmlChar *)value);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005966
5967 return (ret);
5968}
5969
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005970static int
5971xmlSchemaPValAttrID(xmlSchemaParserCtxtPtr ctxt,
5972 xmlNodePtr ownerElem,
5973 const xmlChar *name)
5974{
5975 xmlAttrPtr attr;
5976
5977 attr = xmlSchemaGetPropNode(ownerElem, (const char *) name);
5978 if (attr == NULL)
5979 return(0);
5980 return(xmlSchemaPValAttrNodeID(ctxt, attr));
5981
5982}
5983
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005984/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005985 * xmlGetMaxOccurs:
5986 * @ctxt: a schema validation context
5987 * @node: a subtree containing XML Schema informations
5988 *
5989 * Get the maxOccurs property
5990 *
5991 * Returns the default if not found, or the value
5992 */
5993static int
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005994xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
5995 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005996{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005997 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00005998 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005999 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006000
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006001 attr = xmlSchemaGetPropNode(node, "maxOccurs");
6002 if (attr == NULL)
6003 return (def);
6004 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00006005
6006 if (xmlStrEqual(val, (const xmlChar *) "unbounded")) {
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006007 if (max != UNBOUNDED) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006008 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006009 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6010 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006011 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006012 val, NULL, NULL, NULL);
6013 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006014 } else
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006015 return (UNBOUNDED); /* encoding it with -1 might be another option */
Daniel Veillard4255d502002-04-16 15:50:10 +00006016 }
6017
6018 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006019 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006020 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006021 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006022 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006023 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6024 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006025 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006026 val, NULL, NULL, NULL);
6027 return (def);
6028 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006029 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006030 ret = ret * 10 + (*cur - '0');
6031 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006032 }
William M. Brack76e95df2003-10-18 16:20:14 +00006033 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006034 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006035 /*
6036 * TODO: Restrict the maximal value to Integer.
6037 */
6038 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006039 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006040 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6041 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006042 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006043 val, NULL, NULL, NULL);
6044 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006045 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006046 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006047}
6048
6049/**
6050 * xmlGetMinOccurs:
6051 * @ctxt: a schema validation context
6052 * @node: a subtree containing XML Schema informations
6053 *
6054 * Get the minOccurs property
6055 *
6056 * Returns the default if not found, or the value
6057 */
6058static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006059xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006060 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006061{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006062 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00006063 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006064 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006065
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006066 attr = xmlSchemaGetPropNode(node, "minOccurs");
6067 if (attr == NULL)
6068 return (def);
6069 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00006070 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006071 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006072 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006073 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006074 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006075 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6076 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006077 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006078 val, NULL, NULL, NULL);
6079 return (def);
6080 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006081 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006082 ret = ret * 10 + (*cur - '0');
6083 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006084 }
William M. Brack76e95df2003-10-18 16:20:14 +00006085 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006086 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006087 /*
6088 * TODO: Restrict the maximal value to Integer.
6089 */
6090 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006091 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006092 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6093 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006094 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006095 val, NULL, NULL, NULL);
6096 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006097 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006098 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006099}
6100
6101/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006102 * xmlSchemaPGetBoolNodeValue:
6103 * @ctxt: a schema validation context
6104 * @ownerDes: owner designation
6105 * @ownerItem: the owner as a schema item
6106 * @node: the node holding the value
6107 *
6108 * Converts a boolean string value into 1 or 0.
6109 *
6110 * Returns 0 or 1.
6111 */
6112static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006113xmlSchemaPGetBoolNodeValue(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006114 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006115 xmlNodePtr node)
6116{
6117 xmlChar *value = NULL;
6118 int res = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006119
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006120 value = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006121 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006122 * 3.2.2.1 Lexical representation
Rob Richardsc6947bb2008-06-29 15:04:41 +00006123 * An instance of a datatype that is defined as �boolean�
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006124 * can have the following legal literals {true, false, 1, 0}.
6125 */
6126 if (xmlStrEqual(BAD_CAST value, BAD_CAST "true"))
6127 res = 1;
6128 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "false"))
6129 res = 0;
6130 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "1"))
6131 res = 1;
6132 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006133 res = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006134 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006135 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006136 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006137 ownerItem, node,
6138 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6139 NULL, BAD_CAST value,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006140 NULL, NULL, NULL);
6141 }
6142 if (value != NULL)
6143 xmlFree(value);
6144 return (res);
6145}
6146
6147/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006148 * xmlGetBooleanProp:
6149 * @ctxt: a schema validation context
6150 * @node: a subtree containing XML Schema informations
6151 * @name: the attribute name
6152 * @def: the default value
6153 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006154 * Evaluate if a boolean property is set
Daniel Veillard4255d502002-04-16 15:50:10 +00006155 *
6156 * Returns the default if not found, 0 if found to be false,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006157 * 1 if found to be true
Daniel Veillard4255d502002-04-16 15:50:10 +00006158 */
6159static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006160xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006161 xmlNodePtr node,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006162 const char *name, int def)
6163{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006164 const xmlChar *val;
Daniel Veillard4255d502002-04-16 15:50:10 +00006165
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006166 val = xmlSchemaGetProp(ctxt, node, name);
Daniel Veillard4255d502002-04-16 15:50:10 +00006167 if (val == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006168 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006169 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006170 * 3.2.2.1 Lexical representation
Rob Richardsc6947bb2008-06-29 15:04:41 +00006171 * An instance of a datatype that is defined as �boolean�
Daniel Veillardc0826a72004-08-10 14:17:33 +00006172 * can have the following legal literals {true, false, 1, 0}.
6173 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006174 if (xmlStrEqual(val, BAD_CAST "true"))
6175 def = 1;
6176 else if (xmlStrEqual(val, BAD_CAST "false"))
6177 def = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006178 else if (xmlStrEqual(val, BAD_CAST "1"))
6179 def = 1;
6180 else if (xmlStrEqual(val, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006181 def = 0;
6182 else {
6183 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006184 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006185 NULL,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00006186 (xmlNodePtr) xmlSchemaGetPropNode(node, name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006187 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6188 NULL, val, NULL, NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006189 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006190 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006191}
6192
6193/************************************************************************
6194 * *
6195 * Shema extraction from an Infoset *
6196 * *
6197 ************************************************************************/
6198static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr
6199 ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00006200 xmlNodePtr node,
6201 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006202static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr
6203 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006204 xmlSchemaPtr schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006205 xmlNodePtr node,
Daniel Veillard3646d642004-06-02 19:19:14 +00006206 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006207static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr
6208 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006209 xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006210 xmlNodePtr node,
6211 xmlSchemaTypeType parentType);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006212static xmlSchemaBasicItemPtr
6213xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
6214 xmlSchemaPtr schema,
6215 xmlNodePtr node,
6216 xmlSchemaItemListPtr uses,
6217 int parentType);
Daniel Veillard4255d502002-04-16 15:50:10 +00006218static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt,
6219 xmlSchemaPtr schema,
6220 xmlNodePtr node);
Daniel Veillard3646d642004-06-02 19:19:14 +00006221static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006222xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6223 xmlSchemaPtr schema, xmlNodePtr node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006224
6225/**
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006226 * xmlSchemaPValAttrNodeValue:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006227 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00006228 * @ctxt: a schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00006229 * @ownerDes: the designation of the parent element
6230 * @ownerItem: the schema object owner if existent
6231 * @attr: the schema attribute node being validated
6232 * @value: the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006233 * @type: the built-in type to be validated against
Daniel Veillard01fa6152004-06-29 17:04:39 +00006234 *
6235 * Validates a value against the given built-in type.
6236 * This one is intended to be used internally for validation
6237 * of schema attribute values during parsing of the schema.
6238 *
6239 * Returns 0 if the value is valid, a positive error code
6240 * number otherwise and -1 in case of an internal or API error.
6241 */
6242static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006243xmlSchemaPValAttrNodeValue(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006244 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006245 xmlAttrPtr attr,
6246 const xmlChar *value,
6247 xmlSchemaTypePtr type)
Daniel Veillard01fa6152004-06-29 17:04:39 +00006248{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006249
6250 int ret = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006251
6252 /*
6253 * NOTE: Should we move this to xmlschematypes.c? Hmm, but this
6254 * one is really meant to be used internally, so better not.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006255 */
6256 if ((pctxt == NULL) || (type == NULL) || (attr == NULL))
Daniel Veillard01fa6152004-06-29 17:04:39 +00006257 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006258 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6259 PERROR_INT("xmlSchemaPValAttrNodeValue",
6260 "the given type is not a built-in type");
6261 return (-1);
6262 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006263 switch (type->builtInType) {
6264 case XML_SCHEMAS_NCNAME:
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006265 case XML_SCHEMAS_QNAME:
6266 case XML_SCHEMAS_ANYURI:
6267 case XML_SCHEMAS_TOKEN:
6268 case XML_SCHEMAS_LANGUAGE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006269 ret = xmlSchemaValPredefTypeNode(type, value, NULL,
6270 (xmlNodePtr) attr);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006271 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006272 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006273 PERROR_INT("xmlSchemaPValAttrNodeValue",
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00006274 "validation using the given type is not supported while "
6275 "parsing a schema");
Daniel Veillard01fa6152004-06-29 17:04:39 +00006276 return (-1);
6277 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006278 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006279 /*
6280 * TODO: Should we use the S4S error codes instead?
6281 */
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006282 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006283 PERROR_INT("xmlSchemaPValAttrNodeValue",
6284 "failed to validate a schema attribute value");
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006285 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006286 } else if (ret > 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006287 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006288 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
6289 else
6290 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Daniel Veillarddee23482008-04-11 12:58:43 +00006291 xmlSchemaPSimpleTypeErr(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006292 ret, ownerItem, (xmlNodePtr) attr,
6293 type, NULL, value, NULL, NULL, NULL);
6294 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006295 return (ret);
6296}
6297
6298/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006299 * xmlSchemaPValAttrNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006300 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006301 * @ctxt: a schema parser context
6302 * @ownerDes: the designation of the parent element
6303 * @ownerItem: the schema object owner if existent
6304 * @attr: the schema attribute node being validated
6305 * @type: the built-in type to be validated against
6306 * @value: the resulting value if any
6307 *
6308 * Extracts and validates a value against the given built-in type.
6309 * This one is intended to be used internally for validation
6310 * of schema attribute values during parsing of the schema.
6311 *
6312 * Returns 0 if the value is valid, a positive error code
6313 * number otherwise and -1 in case of an internal or API error.
6314 */
6315static int
6316xmlSchemaPValAttrNode(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006317 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006318 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006319 xmlSchemaTypePtr type,
6320 const xmlChar **value)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006321{
Daniel Veillardc0826a72004-08-10 14:17:33 +00006322 const xmlChar *val;
6323
6324 if ((ctxt == NULL) || (type == NULL) || (attr == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006325 return (-1);
6326
Daniel Veillardc0826a72004-08-10 14:17:33 +00006327 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
6328 if (value != NULL)
6329 *value = val;
6330
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006331 return (xmlSchemaPValAttrNodeValue(ctxt, ownerItem, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006332 val, type));
Daniel Veillardc0826a72004-08-10 14:17:33 +00006333}
6334
6335/**
6336 * xmlSchemaPValAttr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006337 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006338 * @ctxt: a schema parser context
6339 * @node: the element node of the attribute
6340 * @ownerDes: the designation of the parent element
6341 * @ownerItem: the schema object owner if existent
6342 * @ownerElem: the owner element node
6343 * @name: the name of the schema attribute node
6344 * @type: the built-in type to be validated against
6345 * @value: the resulting value if any
6346 *
6347 * Extracts and validates a value against the given built-in type.
6348 * This one is intended to be used internally for validation
6349 * of schema attribute values during parsing of the schema.
6350 *
6351 * Returns 0 if the value is valid, a positive error code
6352 * number otherwise and -1 in case of an internal or API error.
6353 */
6354static int
Daniel Veillarddee23482008-04-11 12:58:43 +00006355xmlSchemaPValAttr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006356 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006357 xmlNodePtr ownerElem,
6358 const char *name,
6359 xmlSchemaTypePtr type,
6360 const xmlChar **value)
6361{
6362 xmlAttrPtr attr;
6363
6364 if ((ctxt == NULL) || (type == NULL)) {
6365 if (value != NULL)
6366 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006367 return (-1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006368 }
6369 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6370 if (value != NULL)
6371 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006372 xmlSchemaPErr(ctxt, ownerElem,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00006373 XML_SCHEMAP_INTERNAL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006374 "Internal error: xmlSchemaPValAttr, the given "
6375 "type '%s' is not a built-in type.\n",
6376 type->name, NULL);
6377 return (-1);
6378 }
6379 attr = xmlSchemaGetPropNode(ownerElem, name);
6380 if (attr == NULL) {
6381 if (value != NULL)
6382 *value = NULL;
6383 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006384 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006385 return (xmlSchemaPValAttrNode(ctxt, ownerItem, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006386 type, value));
6387}
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006388
6389static int
6390xmlSchemaCheckReference(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006391 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006392 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006393 xmlAttrPtr attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006394 const xmlChar *namespaceName)
6395{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006396 /* TODO: Pointer comparison instead? */
6397 if (xmlStrEqual(pctxt->targetNamespace, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006398 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006399 if (xmlStrEqual(xmlSchemaNs, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006400 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006401 /*
6402 * Check if the referenced namespace was <import>ed.
6403 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006404 if (WXS_BUCKET(pctxt)->relations != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006405 xmlSchemaSchemaRelationPtr rel;
6406
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006407 rel = WXS_BUCKET(pctxt)->relations;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006408 do {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006409 if (WXS_IS_BUCKET_IMPMAIN(rel->type) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006410 xmlStrEqual(namespaceName, rel->importNamespace))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006411 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006412 rel = rel->next;
6413 } while (rel != NULL);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006414 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006415 /*
6416 * No matching <import>ed namespace found.
6417 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006418 {
6419 xmlNodePtr n = (attr != NULL) ? (xmlNodePtr) attr : node;
6420
6421 if (namespaceName == NULL)
6422 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6423 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6424 "References from this schema to components in no "
6425 "namespace are not allowed, since not indicated by an "
6426 "import statement", NULL, NULL);
6427 else
6428 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6429 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6430 "References from this schema to components in the "
6431 "namespace '%s' are not allowed, since not indicated by an "
6432 "import statement", namespaceName, NULL);
6433 }
6434 return (XML_SCHEMAP_SRC_RESOLVE);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006435}
6436
Daniel Veillardc0826a72004-08-10 14:17:33 +00006437/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006438 * xmlSchemaParseLocalAttributes:
Daniel Veillard4255d502002-04-16 15:50:10 +00006439 * @ctxt: a schema validation context
6440 * @schema: the schema being built
6441 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006442 * @type: the hosting type where the attributes will be anchored
Daniel Veillard4255d502002-04-16 15:50:10 +00006443 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006444 * Parses attribute uses and attribute declarations and
Daniel Veillarddee23482008-04-11 12:58:43 +00006445 * attribute group references.
Daniel Veillard4255d502002-04-16 15:50:10 +00006446 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006447static int
6448xmlSchemaParseLocalAttributes(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6449 xmlNodePtr *child, xmlSchemaItemListPtr *list,
6450 int parentType, int *hasRefs)
Daniel Veillard4255d502002-04-16 15:50:10 +00006451{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006452 void *item;
Daniel Veillard4255d502002-04-16 15:50:10 +00006453
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006454 while ((IS_SCHEMA((*child), "attribute")) ||
6455 (IS_SCHEMA((*child), "attributeGroup"))) {
6456 if (IS_SCHEMA((*child), "attribute")) {
6457 item = xmlSchemaParseLocalAttribute(ctxt, schema, *child,
6458 *list, parentType);
6459 } else {
6460 item = xmlSchemaParseAttributeGroupRef(ctxt, schema, *child);
6461 if ((item != NULL) && (hasRefs != NULL))
6462 *hasRefs = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006463 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006464 if (item != NULL) {
6465 if (*list == NULL) {
6466 /* TODO: Customize grow factor. */
6467 *list = xmlSchemaItemListCreate();
6468 if (*list == NULL)
6469 return(-1);
6470 }
6471 if (xmlSchemaItemListAddSize(*list, 2, item) == -1)
6472 return(-1);
6473 }
6474 *child = (*child)->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006475 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006476 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00006477}
6478
6479/**
6480 * xmlSchemaParseAnnotation:
6481 * @ctxt: a schema validation context
6482 * @schema: the schema being built
6483 * @node: a subtree containing XML Schema informations
6484 *
6485 * parse a XML schema Attrribute declaration
6486 * *WARNING* this interface is highly subject to change
6487 *
William M. Bracke7091952004-05-11 15:09:58 +00006488 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00006489 * 1 in case of success.
6490 */
6491static xmlSchemaAnnotPtr
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006492xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int needed)
Daniel Veillard4255d502002-04-16 15:50:10 +00006493{
6494 xmlSchemaAnnotPtr ret;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006495 xmlNodePtr child = NULL;
6496 xmlAttrPtr attr;
6497 int barked = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00006498
Daniel Veillardc0826a72004-08-10 14:17:33 +00006499 /*
6500 * INFO: S4S completed.
6501 */
6502 /*
6503 * id = ID
6504 * {any attributes with non-schema namespace . . .}>
6505 * Content: (appinfo | documentation)*
6506 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006507 if ((ctxt == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00006508 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006509 if (needed)
6510 ret = xmlSchemaNewAnnot(ctxt, node);
6511 else
6512 ret = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006513 attr = node->properties;
6514 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006515 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006516 (!xmlStrEqual(attr->name, BAD_CAST "id"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006517 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006518 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006519
6520 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006521 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006522 }
6523 attr = attr->next;
6524 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006525 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006526 /*
6527 * And now for the children...
6528 */
6529 child = node->children;
6530 while (child != NULL) {
6531 if (IS_SCHEMA(child, "appinfo")) {
6532 /* TODO: make available the content of "appinfo". */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006533 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006534 * source = anyURI
6535 * {any attributes with non-schema namespace . . .}>
6536 * Content: ({any})*
6537 */
6538 attr = child->properties;
6539 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006540 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006541 (!xmlStrEqual(attr->name, BAD_CAST "source"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006542 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006543 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006544
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006545 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006546 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006547 }
6548 attr = attr->next;
6549 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006550 xmlSchemaPValAttr(ctxt, NULL, child, "source",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006551 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006552 child = child->next;
6553 } else if (IS_SCHEMA(child, "documentation")) {
6554 /* TODO: make available the content of "documentation". */
6555 /*
6556 * source = anyURI
6557 * {any attributes with non-schema namespace . . .}>
6558 * Content: ({any})*
6559 */
6560 attr = child->properties;
6561 while (attr != NULL) {
6562 if (attr->ns == NULL) {
6563 if (!xmlStrEqual(attr->name, BAD_CAST "source")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006564 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006565 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006566 }
6567 } else {
6568 if (xmlStrEqual(attr->ns->href, xmlSchemaNs) ||
6569 (xmlStrEqual(attr->name, BAD_CAST "lang") &&
6570 (!xmlStrEqual(attr->ns->href, XML_XML_NAMESPACE)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006571
6572 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006573 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006574 }
6575 }
6576 attr = attr->next;
6577 }
6578 /*
6579 * Attribute "xml:lang".
6580 */
6581 attr = xmlSchemaGetPropNodeNs(child, (const char *) XML_XML_NAMESPACE, "lang");
6582 if (attr != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006583 xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006584 xmlSchemaGetBuiltInType(XML_SCHEMAS_LANGUAGE), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006585 child = child->next;
6586 } else {
6587 if (!barked)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006588 xmlSchemaPContentErr(ctxt,
6589 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006590 NULL, node, child, NULL, "(appinfo | documentation)*");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006591 barked = 1;
6592 child = child->next;
6593 }
6594 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006595
Daniel Veillard4255d502002-04-16 15:50:10 +00006596 return (ret);
6597}
6598
6599/**
6600 * xmlSchemaParseFacet:
6601 * @ctxt: a schema validation context
6602 * @schema: the schema being built
6603 * @node: a subtree containing XML Schema informations
6604 *
6605 * parse a XML schema Facet declaration
6606 * *WARNING* this interface is highly subject to change
6607 *
6608 * Returns the new type structure or NULL in case of error
6609 */
6610static xmlSchemaFacetPtr
6611xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006612 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006613{
6614 xmlSchemaFacetPtr facet;
6615 xmlNodePtr child = NULL;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006616 const xmlChar *value;
Daniel Veillard4255d502002-04-16 15:50:10 +00006617
6618 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6619 return (NULL);
6620
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00006621 facet = xmlSchemaNewFacet();
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006622 if (facet == NULL) {
6623 xmlSchemaPErrMemory(ctxt, "allocating facet", node);
6624 return (NULL);
6625 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006626 facet->node = node;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006627 value = xmlSchemaGetProp(ctxt, node, "value");
Daniel Veillard4255d502002-04-16 15:50:10 +00006628 if (value == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006629 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE,
6630 "Facet %s has no value\n", node->name, NULL);
6631 xmlSchemaFreeFacet(facet);
Daniel Veillard4255d502002-04-16 15:50:10 +00006632 return (NULL);
6633 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006634 if (IS_SCHEMA(node, "minInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006635 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006636 } else if (IS_SCHEMA(node, "minExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006637 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006638 } else if (IS_SCHEMA(node, "maxInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006639 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006640 } else if (IS_SCHEMA(node, "maxExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006641 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006642 } else if (IS_SCHEMA(node, "totalDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006643 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006644 } else if (IS_SCHEMA(node, "fractionDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006645 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006646 } else if (IS_SCHEMA(node, "pattern")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006647 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006648 } else if (IS_SCHEMA(node, "enumeration")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006649 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006650 } else if (IS_SCHEMA(node, "whiteSpace")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006651 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006652 } else if (IS_SCHEMA(node, "length")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006653 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006654 } else if (IS_SCHEMA(node, "maxLength")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006655 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
6656 } else if (IS_SCHEMA(node, "minLength")) {
6657 facet->type = XML_SCHEMA_FACET_MINLENGTH;
6658 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006659 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE,
6660 "Unknown facet type %s\n", node->name, NULL);
6661 xmlSchemaFreeFacet(facet);
6662 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006663 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006664 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +00006665 facet->value = value;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006666 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
6667 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
6668 const xmlChar *fixed;
6669
6670 fixed = xmlSchemaGetProp(ctxt, node, "fixed");
6671 if (fixed != NULL) {
6672 if (xmlStrEqual(fixed, BAD_CAST "true"))
6673 facet->fixed = 1;
6674 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006675 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006676 child = node->children;
6677
6678 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006679 facet->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006680 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006681 }
6682 if (child != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006683 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD,
6684 "Facet %s has unexpected child content\n",
6685 node->name, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006686 }
6687 return (facet);
6688}
6689
6690/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006691 * xmlSchemaParseWildcardNs:
6692 * @ctxt: a schema parser context
6693 * @wildc: the wildcard, already created
6694 * @node: a subtree containing XML Schema informations
6695 *
6696 * Parses the attribute "processContents" and "namespace"
6697 * of a xsd:anyAttribute and xsd:any.
6698 * *WARNING* this interface is highly subject to change
6699 *
6700 * Returns 0 if everything goes fine, a positive error code
6701 * if something is not valid and -1 if an internal error occurs.
6702 */
6703static int
6704xmlSchemaParseWildcardNs(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006705 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006706 xmlSchemaWildcardPtr wildc,
6707 xmlNodePtr node)
6708{
6709 const xmlChar *pc, *ns, *dictnsItem;
6710 int ret = 0;
6711 xmlChar *nsItem;
6712 xmlSchemaWildcardNsPtr tmp, lastNs = NULL;
6713 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006714
Daniel Veillardc0826a72004-08-10 14:17:33 +00006715 pc = xmlSchemaGetProp(ctxt, node, "processContents");
6716 if ((pc == NULL)
6717 || (xmlStrEqual(pc, (const xmlChar *) "strict"))) {
6718 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
6719 } else if (xmlStrEqual(pc, (const xmlChar *) "skip")) {
6720 wildc->processContents = XML_SCHEMAS_ANY_SKIP;
6721 } else if (xmlStrEqual(pc, (const xmlChar *) "lax")) {
6722 wildc->processContents = XML_SCHEMAS_ANY_LAX;
6723 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006724 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006725 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006726 NULL, node,
6727 NULL, "(strict | skip | lax)", pc,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006728 NULL, NULL, NULL);
6729 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006730 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006731 }
6732 /*
6733 * Build the namespace constraints.
6734 */
6735 attr = xmlSchemaGetPropNode(node, "namespace");
6736 ns = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00006737 if ((attr == NULL) || (xmlStrEqual(ns, BAD_CAST "##any")))
Daniel Veillardc0826a72004-08-10 14:17:33 +00006738 wildc->any = 1;
6739 else if (xmlStrEqual(ns, BAD_CAST "##other")) {
6740 wildc->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006741 if (wildc->negNsSet == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006742 return (-1);
6743 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006744 wildc->negNsSet->value = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006745 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006746 const xmlChar *end, *cur;
6747
6748 cur = ns;
6749 do {
6750 while (IS_BLANK_CH(*cur))
6751 cur++;
6752 end = cur;
6753 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
6754 end++;
6755 if (end == cur)
6756 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006757 nsItem = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006758 if ((xmlStrEqual(nsItem, BAD_CAST "##other")) ||
6759 (xmlStrEqual(nsItem, BAD_CAST "##any"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006760 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006761 XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006762 NULL, (xmlNodePtr) attr,
6763 NULL,
6764 "((##any | ##other) | List of (xs:anyURI | "
6765 "(##targetNamespace | ##local)))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00006766 nsItem, NULL, NULL, NULL);
6767 ret = XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER;
6768 } else {
6769 if (xmlStrEqual(nsItem, BAD_CAST "##targetNamespace")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006770 dictnsItem = ctxt->targetNamespace;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006771 } else if (xmlStrEqual(nsItem, BAD_CAST "##local")) {
6772 dictnsItem = NULL;
6773 } else {
6774 /*
6775 * Validate the item (anyURI).
6776 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006777 xmlSchemaPValAttrNodeValue(ctxt, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006778 nsItem, xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI));
6779 dictnsItem = xmlDictLookup(ctxt->dict, nsItem, -1);
6780 }
6781 /*
6782 * Avoid dublicate namespaces.
6783 */
6784 tmp = wildc->nsSet;
6785 while (tmp != NULL) {
6786 if (dictnsItem == tmp->value)
6787 break;
6788 tmp = tmp->next;
6789 }
6790 if (tmp == NULL) {
6791 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
6792 if (tmp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006793 xmlFree(nsItem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006794 return (-1);
6795 }
6796 tmp->value = dictnsItem;
6797 tmp->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006798 if (wildc->nsSet == NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006799 wildc->nsSet = tmp;
6800 else
6801 lastNs->next = tmp;
6802 lastNs = tmp;
6803 }
6804
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006805 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006806 xmlFree(nsItem);
6807 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006808 } while (*cur != 0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006809 }
6810 return (ret);
6811}
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006812
6813static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006814xmlSchemaPCheckParticleCorrect_2(xmlSchemaParserCtxtPtr ctxt,
6815 xmlSchemaParticlePtr item ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006816 xmlNodePtr node,
6817 int minOccurs,
6818 int maxOccurs) {
6819
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006820 if ((maxOccurs == 0) && ( minOccurs == 0))
6821 return (0);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006822 if (maxOccurs != UNBOUNDED) {
6823 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006824 * TODO: Maybe we should better not create the particle,
6825 * if min/max is invalid, since it could confuse the build of the
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006826 * content model.
6827 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006828 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006829 * 3.9.6 Schema Component Constraint: Particle Correct
6830 *
6831 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006832 if (maxOccurs < 1) {
6833 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006834 * 2.2 {max occurs} must be greater than or equal to 1.
6835 */
6836 xmlSchemaPCustomAttrErr(ctxt,
6837 XML_SCHEMAP_P_PROPS_CORRECT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006838 NULL, NULL,
6839 xmlSchemaGetPropNode(node, "maxOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006840 "The value must be greater than or equal to 1");
6841 return (XML_SCHEMAP_P_PROPS_CORRECT_2_2);
6842 } else if (minOccurs > maxOccurs) {
6843 /*
6844 * 2.1 {min occurs} must not be greater than {max occurs}.
6845 */
6846 xmlSchemaPCustomAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006847 XML_SCHEMAP_P_PROPS_CORRECT_2_1,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006848 NULL, NULL,
6849 xmlSchemaGetPropNode(node, "minOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006850 "The value must not be greater than the value of 'maxOccurs'");
6851 return (XML_SCHEMAP_P_PROPS_CORRECT_2_1);
6852 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006853 }
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006854 return (0);
6855}
6856
Daniel Veillardc0826a72004-08-10 14:17:33 +00006857/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006858 * xmlSchemaParseAny:
6859 * @ctxt: a schema validation context
6860 * @schema: the schema being built
6861 * @node: a subtree containing XML Schema informations
6862 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006863 * Parsea a XML schema <any> element. A particle and wildcard
6864 * will be created (except if minOccurs==maxOccurs==0, in this case
6865 * nothing will be created).
Daniel Veillard4255d502002-04-16 15:50:10 +00006866 * *WARNING* this interface is highly subject to change
6867 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006868 * Returns the particle or NULL in case of error or if minOccurs==maxOccurs==0
Daniel Veillard4255d502002-04-16 15:50:10 +00006869 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006870static xmlSchemaParticlePtr
Daniel Veillard4255d502002-04-16 15:50:10 +00006871xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6872 xmlNodePtr node)
6873{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006874 xmlSchemaParticlePtr particle;
Daniel Veillard4255d502002-04-16 15:50:10 +00006875 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006876 xmlSchemaWildcardPtr wild;
6877 int min, max;
6878 xmlAttrPtr attr;
6879 xmlSchemaAnnotPtr annot = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00006880
6881 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6882 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006883 /*
6884 * Check for illegal attributes.
6885 */
6886 attr = node->properties;
6887 while (attr != NULL) {
6888 if (attr->ns == NULL) {
6889 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6890 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
6891 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
6892 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6893 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006894 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006895 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006896 }
6897 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006898 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006899 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006900 }
6901 attr = attr->next;
6902 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006903 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006904 /*
6905 * minOccurs/maxOccurs.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006906 */
6907 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
6908 "(xs:nonNegativeInteger | unbounded)");
6909 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1,
6910 "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006911 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
6912 /*
6913 * Create & parse the wildcard.
6914 */
6915 wild = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY, node);
6916 if (wild == NULL)
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006917 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006918 xmlSchemaParseWildcardNs(ctxt, schema, wild, node);
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006919 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006920 * And now for the children...
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006921 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006922 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00006923 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006924 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006925 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006926 }
6927 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006928 xmlSchemaPContentErr(ctxt,
6929 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006930 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006931 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006932 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006933 /*
6934 * No component if minOccurs==maxOccurs==0.
6935 */
6936 if ((min == 0) && (max == 0)) {
6937 /* Don't free the wildcard, since it's already on the list. */
6938 return (NULL);
6939 }
6940 /*
6941 * Create the particle.
6942 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00006943 particle = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006944 if (particle == NULL)
6945 return (NULL);
6946 particle->annot = annot;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006947 particle->children = (xmlSchemaTreeItemPtr) wild;
Daniel Veillard4255d502002-04-16 15:50:10 +00006948
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006949 return (particle);
Daniel Veillard4255d502002-04-16 15:50:10 +00006950}
6951
6952/**
6953 * xmlSchemaParseNotation:
6954 * @ctxt: a schema validation context
6955 * @schema: the schema being built
6956 * @node: a subtree containing XML Schema informations
6957 *
6958 * parse a XML schema Notation declaration
6959 *
6960 * Returns the new structure or NULL in case of error
6961 */
6962static xmlSchemaNotationPtr
6963xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006964 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006965{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006966 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00006967 xmlSchemaNotationPtr ret;
6968 xmlNodePtr child = NULL;
6969
6970 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6971 return (NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006972 name = xmlSchemaGetProp(ctxt, node, "name");
Daniel Veillard4255d502002-04-16 15:50:10 +00006973 if (name == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006974 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME,
6975 "Notation has no name\n", NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006976 return (NULL);
6977 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006978 ret = xmlSchemaAddNotation(ctxt, schema, name,
6979 ctxt->targetNamespace, node);
6980 if (ret == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00006981 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006982 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006983
Daniel Veillard4255d502002-04-16 15:50:10 +00006984 child = node->children;
6985 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006986 ret->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006987 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006988 }
6989 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006990 xmlSchemaPContentErr(ctxt,
6991 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006992 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006993 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006994 }
6995
6996 return (ret);
6997}
6998
6999/**
7000 * xmlSchemaParseAnyAttribute:
7001 * @ctxt: a schema validation context
7002 * @schema: the schema being built
7003 * @node: a subtree containing XML Schema informations
7004 *
7005 * parse a XML schema AnyAttrribute declaration
7006 * *WARNING* this interface is highly subject to change
7007 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007008 * Returns a wildcard or NULL.
Daniel Veillard4255d502002-04-16 15:50:10 +00007009 */
Daniel Veillard3646d642004-06-02 19:19:14 +00007010static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007011xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
7012 xmlSchemaPtr schema, xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007013{
Daniel Veillard3646d642004-06-02 19:19:14 +00007014 xmlSchemaWildcardPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007015 xmlNodePtr child = NULL;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007016 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00007017
7018 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
7019 return (NULL);
7020
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007021 ret = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
7022 node);
Daniel Veillard4255d502002-04-16 15:50:10 +00007023 if (ret == NULL) {
7024 return (NULL);
7025 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007026 /*
7027 * Check for illegal attributes.
7028 */
7029 attr = node->properties;
7030 while (attr != NULL) {
7031 if (attr->ns == NULL) {
7032 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7033 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
7034 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007035 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007036 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007037 }
7038 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007039 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007040 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007041 }
7042 attr = attr->next;
7043 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007044 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007045 /*
7046 * Parse the namespace list.
7047 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007048 if (xmlSchemaParseWildcardNs(ctxt, schema, ret, node) != 0)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007049 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007050 /*
7051 * And now for the children...
7052 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007053 child = node->children;
7054 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007055 ret->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007056 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007057 }
7058 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00007059 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007060 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007061 NULL, node, child,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007062 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00007063 }
7064
7065 return (ret);
7066}
7067
7068
7069/**
7070 * xmlSchemaParseAttribute:
7071 * @ctxt: a schema validation context
7072 * @schema: the schema being built
7073 * @node: a subtree containing XML Schema informations
7074 *
7075 * parse a XML schema Attrribute declaration
7076 * *WARNING* this interface is highly subject to change
7077 *
William M. Bracke7091952004-05-11 15:09:58 +00007078 * Returns the attribute declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00007079 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007080static xmlSchemaBasicItemPtr
7081xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
7082 xmlSchemaPtr schema,
7083 xmlNodePtr node,
7084 xmlSchemaItemListPtr uses,
7085 int parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +00007086{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007087 const xmlChar *attrValue, *name = NULL, *ns = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00007088 xmlSchemaAttributeUsePtr use = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007089 xmlNodePtr child = NULL;
7090 xmlAttrPtr attr;
7091 const xmlChar *tmpNs = NULL, *tmpName = NULL, *defValue = NULL;
7092 int isRef = 0, occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7093 int nberrors, hasForm = 0, defValueType = 0;
7094
7095#define WXS_ATTR_DEF_VAL_DEFAULT 1
7096#define WXS_ATTR_DEF_VAL_FIXED 2
7097
7098 /*
7099 * 3.2.3 Constraints on XML Representations of Attribute Declarations
7100 */
7101
7102 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7103 return (NULL);
7104 attr = xmlSchemaGetPropNode(node, "ref");
7105 if (attr != NULL) {
7106 if (xmlSchemaPValAttrNodeQName(pctxt, schema,
7107 NULL, attr, &tmpNs, &tmpName) != 0) {
7108 return (NULL);
7109 }
7110 if (xmlSchemaCheckReference(pctxt, schema, node, attr, tmpNs) != 0)
7111 return(NULL);
7112 isRef = 1;
7113 }
7114 nberrors = pctxt->nberrors;
7115 /*
7116 * Check for illegal attributes.
7117 */
7118 attr = node->properties;
7119 while (attr != NULL) {
7120 if (attr->ns == NULL) {
7121 if (isRef) {
7122 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7123 xmlSchemaPValAttrNodeID(pctxt, attr);
7124 goto attr_next;
7125 } else if (xmlStrEqual(attr->name, BAD_CAST "ref")) {
7126 goto attr_next;
7127 }
7128 } else {
7129 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
7130 goto attr_next;
7131 } else if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7132 xmlSchemaPValAttrNodeID(pctxt, attr);
7133 goto attr_next;
7134 } else if (xmlStrEqual(attr->name, BAD_CAST "type")) {
7135 xmlSchemaPValAttrNodeQName(pctxt, schema, NULL,
7136 attr, &tmpNs, &tmpName);
7137 goto attr_next;
7138 } else if (xmlStrEqual(attr->name, BAD_CAST "form")) {
7139 /*
7140 * Evaluate the target namespace
7141 */
Daniel Veillarddee23482008-04-11 12:58:43 +00007142 hasForm = 1;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007143 attrValue = xmlSchemaGetNodeContent(pctxt,
7144 (xmlNodePtr) attr);
7145 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
7146 ns = pctxt->targetNamespace;
7147 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified"))
7148 {
7149 xmlSchemaPSimpleTypeErr(pctxt,
7150 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
7151 NULL, (xmlNodePtr) attr,
7152 NULL, "(qualified | unqualified)",
7153 attrValue, NULL, NULL, NULL);
7154 }
7155 goto attr_next;
7156 }
7157 }
7158 if (xmlStrEqual(attr->name, BAD_CAST "use")) {
7159
7160 attrValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7161 /* TODO: Maybe we need to normalize the value beforehand. */
7162 if (xmlStrEqual(attrValue, BAD_CAST "optional"))
7163 occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7164 else if (xmlStrEqual(attrValue, BAD_CAST "prohibited"))
7165 occurs = XML_SCHEMAS_ATTR_USE_PROHIBITED;
7166 else if (xmlStrEqual(attrValue, BAD_CAST "required"))
7167 occurs = XML_SCHEMAS_ATTR_USE_REQUIRED;
7168 else {
7169 xmlSchemaPSimpleTypeErr(pctxt,
7170 XML_SCHEMAP_INVALID_ATTR_USE,
7171 NULL, (xmlNodePtr) attr,
7172 NULL, "(optional | prohibited | required)",
7173 attrValue, NULL, NULL, NULL);
7174 }
7175 goto attr_next;
7176 } else if (xmlStrEqual(attr->name, BAD_CAST "default")) {
7177 /*
7178 * 3.2.3 : 1
7179 * default and fixed must not both be present.
7180 */
7181 if (defValue) {
7182 xmlSchemaPMutualExclAttrErr(pctxt,
7183 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7184 NULL, attr, "default", "fixed");
7185 } else {
7186 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7187 defValueType = WXS_ATTR_DEF_VAL_DEFAULT;
7188 }
7189 goto attr_next;
7190 } else if (xmlStrEqual(attr->name, BAD_CAST "fixed")) {
7191 /*
7192 * 3.2.3 : 1
7193 * default and fixed must not both be present.
7194 */
7195 if (defValue) {
7196 xmlSchemaPMutualExclAttrErr(pctxt,
7197 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7198 NULL, attr, "default", "fixed");
7199 } else {
7200 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7201 defValueType = WXS_ATTR_DEF_VAL_FIXED;
7202 }
7203 goto attr_next;
7204 }
7205 } else if (! xmlStrEqual(attr->ns->href, xmlSchemaNs))
7206 goto attr_next;
7207
7208 xmlSchemaPIllegalAttrErr(pctxt,
7209 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7210
7211attr_next:
7212 attr = attr->next;
Daniel Veillarddee23482008-04-11 12:58:43 +00007213 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007214 /*
7215 * 3.2.3 : 2
7216 * If default and use are both present, use must have
7217 * the actual value optional.
7218 */
7219 if ((defValueType == WXS_ATTR_DEF_VAL_DEFAULT) &&
7220 (occurs != XML_SCHEMAS_ATTR_USE_OPTIONAL)) {
7221 xmlSchemaPSimpleTypeErr(pctxt,
7222 XML_SCHEMAP_SRC_ATTRIBUTE_2,
7223 NULL, node, NULL,
7224 "(optional | prohibited | required)", NULL,
7225 "The value of the attribute 'use' must be 'optional' "
7226 "if the attribute 'default' is present",
7227 NULL, NULL);
7228 }
7229 /*
7230 * We want correct attributes.
7231 */
7232 if (nberrors != pctxt->nberrors)
7233 return(NULL);
7234 if (! isRef) {
7235 xmlSchemaAttributePtr attrDecl;
7236
7237 /* TODO: move XML_SCHEMAS_QUALIF_ATTR to the parser. */
7238 if ((! hasForm) && (schema->flags & XML_SCHEMAS_QUALIF_ATTR))
Daniel Veillarddee23482008-04-11 12:58:43 +00007239 ns = pctxt->targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007240 /*
7241 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7242 * TODO: Move this to the component layer.
7243 */
7244 if (xmlStrEqual(ns, xmlSchemaInstanceNs)) {
7245 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7246 XML_SCHEMAP_NO_XSI,
7247 node, NULL,
7248 "The target namespace must not match '%s'",
7249 xmlSchemaInstanceNs, NULL);
7250 }
7251 attr = xmlSchemaGetPropNode(node, "name");
7252 if (attr == NULL) {
7253 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7254 NULL, node, "name", NULL);
7255 return (NULL);
7256 }
7257 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7258 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7259 return (NULL);
7260 }
7261 /*
7262 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7263 * TODO: Move this to the component layer.
7264 */
7265 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
7266 xmlSchemaPSimpleTypeErr(pctxt,
7267 XML_SCHEMAP_NO_XMLNS,
7268 NULL, (xmlNodePtr) attr,
7269 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7270 "The value of the attribute must not match 'xmlns'",
7271 NULL, NULL);
7272 return (NULL);
7273 }
7274 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED)
7275 goto check_children;
7276 /*
7277 * Create the attribute use component.
7278 */
7279 use = xmlSchemaAddAttributeUse(pctxt, node);
7280 if (use == NULL)
7281 return(NULL);
7282 use->occurs = occurs;
7283 /*
7284 * Create the attribute declaration.
7285 */
7286 attrDecl = xmlSchemaAddAttribute(pctxt, schema, name, ns, node, 0);
7287 if (attrDecl == NULL)
7288 return (NULL);
7289 if (tmpName != NULL) {
7290 attrDecl->typeName = tmpName;
7291 attrDecl->typeNs = tmpNs;
7292 }
7293 use->attrDecl = attrDecl;
7294 /*
7295 * Value constraint.
Daniel Veillarddee23482008-04-11 12:58:43 +00007296 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007297 if (defValue != NULL) {
7298 attrDecl->defValue = defValue;
7299 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7300 attrDecl->flags |= XML_SCHEMAS_ATTR_FIXED;
7301 }
7302 } else if (occurs != XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7303 xmlSchemaQNameRefPtr ref;
7304
7305 /*
7306 * Create the attribute use component.
7307 */
7308 use = xmlSchemaAddAttributeUse(pctxt, node);
7309 if (use == NULL)
7310 return(NULL);
7311 /*
7312 * We need to resolve the reference at later stage.
7313 */
7314 WXS_ADD_PENDING(pctxt, use);
7315 use->occurs = occurs;
7316 /*
7317 * Create a QName reference to the attribute declaration.
7318 */
7319 ref = xmlSchemaNewQNameRef(pctxt, XML_SCHEMA_TYPE_ATTRIBUTE,
7320 tmpName, tmpNs);
7321 if (ref == NULL)
7322 return(NULL);
7323 /*
7324 * Assign the reference. This will be substituted for the
7325 * referenced attribute declaration when the QName is resolved.
7326 */
7327 use->attrDecl = WXS_ATTR_CAST ref;
7328 /*
7329 * Value constraint.
7330 */
7331 if (defValue != NULL)
7332 use->defValue = defValue;
7333 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7334 use->flags |= XML_SCHEMA_ATTR_USE_FIXED;
7335 }
Daniel Veillarddee23482008-04-11 12:58:43 +00007336
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007337check_children:
7338 /*
7339 * And now for the children...
7340 */
7341 child = node->children;
7342 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED) {
Daniel Veillarddee23482008-04-11 12:58:43 +00007343 xmlSchemaAttributeUseProhibPtr prohib;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007344
7345 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007346 xmlSchemaParseAnnotation(pctxt, child, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007347 child = child->next;
7348 }
7349 if (child != NULL) {
7350 xmlSchemaPContentErr(pctxt,
7351 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7352 NULL, node, child, NULL,
7353 "(annotation?)");
7354 }
7355 /*
7356 * Check for pointlessness of attribute prohibitions.
7357 */
Daniel Veillarddee23482008-04-11 12:58:43 +00007358 if (parentType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007359 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7360 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7361 node, NULL,
7362 "Skipping attribute use prohibition, since it is "
7363 "pointless inside an <attributeGroup>",
7364 NULL, NULL, NULL);
7365 return(NULL);
7366 } else if (parentType == XML_SCHEMA_TYPE_EXTENSION) {
7367 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7368 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7369 node, NULL,
7370 "Skipping attribute use prohibition, since it is "
7371 "pointless when extending a type",
7372 NULL, NULL, NULL);
7373 return(NULL);
7374 }
7375 if (! isRef) {
Daniel Veillarddee23482008-04-11 12:58:43 +00007376 tmpName = name;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007377 tmpNs = ns;
7378 }
7379 /*
7380 * Check for duplicate attribute prohibitions.
7381 */
7382 if (uses) {
7383 int i;
Daniel Veillarddee23482008-04-11 12:58:43 +00007384
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007385 for (i = 0; i < uses->nbItems; i++) {
7386 use = uses->items[i];
7387 if ((use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) &&
7388 (tmpName == (WXS_ATTR_PROHIB_CAST use)->name) &&
7389 (tmpNs == (WXS_ATTR_PROHIB_CAST use)->targetNamespace))
7390 {
7391 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00007392
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007393 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7394 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7395 node, NULL,
7396 "Skipping duplicate attribute use prohibition '%s'",
7397 xmlSchemaFormatQName(&str, tmpNs, tmpName),
7398 NULL, NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00007399 FREE_AND_NULL(str)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007400 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00007401 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007402 }
7403 }
7404 /*
7405 * Create the attribute prohibition helper component.
7406 */
7407 prohib = xmlSchemaAddAttributeUseProhib(pctxt);
7408 if (prohib == NULL)
7409 return(NULL);
7410 prohib->node = node;
7411 prohib->name = tmpName;
7412 prohib->targetNamespace = tmpNs;
7413 if (isRef) {
7414 /*
7415 * We need at least to resolve to the attribute declaration.
7416 */
7417 WXS_ADD_PENDING(pctxt, prohib);
7418 }
7419 return(WXS_BASIC_CAST prohib);
Daniel Veillarddee23482008-04-11 12:58:43 +00007420 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007421 if (IS_SCHEMA(child, "annotation")) {
7422 /*
7423 * TODO: Should this go into the attr decl?
7424 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007425 use->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007426 child = child->next;
7427 }
7428 if (isRef) {
7429 if (child != NULL) {
7430 if (IS_SCHEMA(child, "simpleType"))
7431 /*
7432 * 3.2.3 : 3.2
7433 * If ref is present, then all of <simpleType>,
7434 * form and type must be absent.
7435 */
7436 xmlSchemaPContentErr(pctxt,
7437 XML_SCHEMAP_SRC_ATTRIBUTE_3_2,
7438 NULL, node, child, NULL,
7439 "(annotation?)");
7440 else
7441 xmlSchemaPContentErr(pctxt,
7442 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7443 NULL, node, child, NULL,
7444 "(annotation?)");
7445 }
7446 } else {
7447 if (IS_SCHEMA(child, "simpleType")) {
7448 if (WXS_ATTRUSE_DECL(use)->typeName != NULL) {
7449 /*
7450 * 3.2.3 : 4
7451 * type and <simpleType> must not both be present.
7452 */
7453 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7454 NULL, node, child,
7455 "The attribute 'type' and the <simpleType> child "
7456 "are mutually exclusive", NULL);
7457 } else
7458 WXS_ATTRUSE_TYPEDEF(use) =
7459 xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7460 child = child->next;
7461 }
7462 if (child != NULL)
7463 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7464 NULL, node, child, NULL,
7465 "(annotation?, simpleType?)");
7466 }
Daniel Veillarddee23482008-04-11 12:58:43 +00007467 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007468 return (WXS_BASIC_CAST use);
7469}
7470
7471
7472static xmlSchemaAttributePtr
7473xmlSchemaParseGlobalAttribute(xmlSchemaParserCtxtPtr pctxt,
7474 xmlSchemaPtr schema,
7475 xmlNodePtr node)
7476{
7477 const xmlChar *attrValue;
Daniel Veillard4255d502002-04-16 15:50:10 +00007478 xmlSchemaAttributePtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007479 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007480 xmlAttrPtr attr;
William M. Bracke7091952004-05-11 15:09:58 +00007481
7482 /*
7483 * Note that the w3c spec assumes the schema to be validated with schema
7484 * for schemas beforehand.
7485 *
7486 * 3.2.3 Constraints on XML Representations of Attribute Declarations
William M. Bracke7091952004-05-11 15:09:58 +00007487 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007488 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
Daniel Veillarddee23482008-04-11 12:58:43 +00007489 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007490 /*
7491 * 3.2.3 : 3.1
7492 * One of ref or name must be present, but not both
7493 */
7494 attr = xmlSchemaGetPropNode(node, "name");
7495 if (attr == NULL) {
7496 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7497 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007498 return (NULL);
7499 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007500 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7501 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0) {
7502 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007503 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007504 /*
7505 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7506 * TODO: Move this to the component layer.
7507 */
7508 if (xmlStrEqual(attrValue, BAD_CAST "xmlns")) {
7509 xmlSchemaPSimpleTypeErr(pctxt,
7510 XML_SCHEMAP_NO_XMLNS,
7511 NULL, (xmlNodePtr) attr,
7512 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7513 "The value of the attribute must not match 'xmlns'",
7514 NULL, NULL);
7515 return (NULL);
7516 }
7517 /*
7518 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7519 * TODO: Move this to the component layer.
7520 * Or better leave it here and add it to the component layer
7521 * if we have a schema construction API.
7522 */
7523 if (xmlStrEqual(pctxt->targetNamespace, xmlSchemaInstanceNs)) {
7524 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7525 XML_SCHEMAP_NO_XSI, node, NULL,
7526 "The target namespace must not match '%s'",
7527 xmlSchemaInstanceNs, NULL);
7528 }
7529
7530 ret = xmlSchemaAddAttribute(pctxt, schema, attrValue,
7531 pctxt->targetNamespace, node, 1);
7532 if (ret == NULL)
7533 return (NULL);
7534 ret->flags |= XML_SCHEMAS_ATTR_GLOBAL;
Daniel Veillarddee23482008-04-11 12:58:43 +00007535
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007536 /*
7537 * Check for illegal attributes.
7538 */
7539 attr = node->properties;
7540 while (attr != NULL) {
7541 if (attr->ns == NULL) {
7542 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7543 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
7544 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
7545 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7546 (!xmlStrEqual(attr->name, BAD_CAST "type")))
7547 {
7548 xmlSchemaPIllegalAttrErr(pctxt,
Daniel Veillarddee23482008-04-11 12:58:43 +00007549 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007550 }
7551 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7552 xmlSchemaPIllegalAttrErr(pctxt,
7553 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7554 }
7555 attr = attr->next;
7556 }
7557 xmlSchemaPValAttrQName(pctxt, schema, NULL,
7558 node, "type", &ret->typeNs, &ret->typeName);
Daniel Veillarddee23482008-04-11 12:58:43 +00007559
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007560 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
William M. Bracke7091952004-05-11 15:09:58 +00007561 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007562 * Attribute "fixed".
William M. Bracke7091952004-05-11 15:09:58 +00007563 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007564 ret->defValue = xmlSchemaGetProp(pctxt, node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007565 if (ret->defValue != NULL)
7566 ret->flags |= XML_SCHEMAS_ATTR_FIXED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007567 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007568 * Attribute "default".
7569 */
7570 attr = xmlSchemaGetPropNode(node, "default");
7571 if (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007572 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007573 * 3.2.3 : 1
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007574 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00007575 */
7576 if (ret->flags & XML_SCHEMAS_ATTR_FIXED) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007577 xmlSchemaPMutualExclAttrErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_1,
7578 WXS_BASIC_CAST ret, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007579 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007580 ret->defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007581 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00007582 /*
7583 * And now for the children...
7584 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007585 child = node->children;
7586 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007587 ret->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007588 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007589 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007590 if (IS_SCHEMA(child, "simpleType")) {
7591 if (ret->typeName != NULL) {
7592 /*
7593 * 3.2.3 : 4
7594 * type and <simpleType> must not both be present.
7595 */
7596 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7597 NULL, node, child,
7598 "The attribute 'type' and the <simpleType> child "
7599 "are mutually exclusive", NULL);
7600 } else
7601 ret->subtypes = xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7602 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007603 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007604 if (child != NULL)
7605 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7606 NULL, node, child, NULL,
7607 "(annotation?, simpleType?)");
7608
Daniel Veillard4255d502002-04-16 15:50:10 +00007609 return (ret);
7610}
7611
7612/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007613 * xmlSchemaParseAttributeGroupRef:
Daniel Veillard4255d502002-04-16 15:50:10 +00007614 * @ctxt: a schema validation context
7615 * @schema: the schema being built
7616 * @node: a subtree containing XML Schema informations
7617 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007618 * Parse an attribute group definition reference.
7619 * Note that a reference to an attribute group does not
7620 * correspond to any component at all.
Daniel Veillard4255d502002-04-16 15:50:10 +00007621 * *WARNING* this interface is highly subject to change
7622 *
7623 * Returns the attribute group or NULL in case of error.
7624 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007625static xmlSchemaQNameRefPtr
7626xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
7627 xmlSchemaPtr schema,
7628 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007629{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007630 xmlSchemaQNameRefPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007631 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007632 xmlAttrPtr attr;
7633 const xmlChar *refNs = NULL, *ref = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00007634
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007635 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00007636 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007637
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007638 attr = xmlSchemaGetPropNode(node, "ref");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007639 if (attr == NULL) {
7640 xmlSchemaPMissingAttrErr(pctxt,
7641 XML_SCHEMAP_S4S_ATTR_MISSING,
7642 NULL, node, "ref", NULL);
7643 return (NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00007644 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007645 xmlSchemaPValAttrNodeQName(pctxt, schema,
7646 NULL, attr, &refNs, &ref);
7647 if (xmlSchemaCheckReference(pctxt, schema, node, attr, refNs) != 0)
7648 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00007649
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007650 /*
7651 * Check for illegal attributes.
7652 */
7653 attr = node->properties;
7654 while (attr != NULL) {
7655 if (attr->ns == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007656 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007657 (!xmlStrEqual(attr->name, BAD_CAST "id")))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007658 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007659 xmlSchemaPIllegalAttrErr(pctxt,
7660 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007661 }
7662 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007663 xmlSchemaPIllegalAttrErr(pctxt,
7664 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007665 }
7666 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007667 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00007668 /* Attribute ID */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007669 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7670
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007671 /*
7672 * And now for the children...
7673 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007674 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00007675 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007676 /*
7677 * TODO: We do not have a place to store the annotation, do we?
7678 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007679 xmlSchemaParseAnnotation(pctxt, child, 0);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007680 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007681 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007682 if (child != NULL) {
7683 xmlSchemaPContentErr(pctxt,
7684 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7685 NULL, node, child, NULL,
7686 "(annotation?)");
7687 }
7688
7689 /*
7690 * Handle attribute group redefinitions.
7691 */
7692 if (pctxt->isRedefine && pctxt->redef &&
7693 (pctxt->redef->item->type ==
7694 XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
7695 (ref == pctxt->redef->refName) &&
7696 (refNs == pctxt->redef->refTargetNs))
7697 {
7698 /*
7699 * SPEC src-redefine:
7700 * (7.1) "If it has an <attributeGroup> among its contents
Rob Richardsc6947bb2008-06-29 15:04:41 +00007701 * the �actual value� of whose ref [attribute] is the same
7702 * as the �actual value� of its own name attribute plus
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007703 * target namespace, then it must have exactly one such group."
7704 */
7705 if (pctxt->redefCounter != 0) {
7706 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00007707
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007708 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7709 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
7710 "The redefining attribute group definition "
7711 "'%s' must not contain more than one "
7712 "reference to the redefined definition",
7713 xmlSchemaFormatQName(&str, refNs, ref), NULL);
7714 FREE_AND_NULL(str);
7715 return(NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007716 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007717 pctxt->redefCounter++;
7718 /*
7719 * URGENT TODO: How to ensure that the reference will not be
7720 * handled by the normal component resolution mechanism?
7721 */
7722 ret = xmlSchemaNewQNameRef(pctxt,
7723 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7724 if (ret == NULL)
7725 return(NULL);
7726 ret->node = node;
7727 pctxt->redef->reference = WXS_BASIC_CAST ret;
7728 } else {
7729 /*
7730 * Create a QName-reference helper component. We will substitute this
7731 * component for the attribute uses of the referenced attribute group
7732 * definition.
7733 */
7734 ret = xmlSchemaNewQNameRef(pctxt,
7735 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7736 if (ret == NULL)
7737 return(NULL);
7738 ret->node = node;
7739 /* Add to pending items, to be able to resolve the reference. */
7740 WXS_ADD_PENDING(pctxt, ret);
Daniel Veillarddee23482008-04-11 12:58:43 +00007741 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007742 return (ret);
7743}
7744
7745/**
7746 * xmlSchemaParseAttributeGroupDefinition:
7747 * @pctxt: a schema validation context
7748 * @schema: the schema being built
7749 * @node: a subtree containing XML Schema informations
7750 *
7751 * parse a XML schema Attribute Group declaration
7752 * *WARNING* this interface is highly subject to change
7753 *
7754 * Returns the attribute group definition or NULL in case of error.
7755 */
7756static xmlSchemaAttributeGroupPtr
7757xmlSchemaParseAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
7758 xmlSchemaPtr schema,
7759 xmlNodePtr node)
7760{
7761 const xmlChar *name;
7762 xmlSchemaAttributeGroupPtr ret;
7763 xmlNodePtr child = NULL;
7764 xmlAttrPtr attr;
7765 int hasRefs = 0;
7766
7767 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7768 return (NULL);
7769
7770 attr = xmlSchemaGetPropNode(node, "name");
7771 if (attr == NULL) {
7772 xmlSchemaPMissingAttrErr(pctxt,
7773 XML_SCHEMAP_S4S_ATTR_MISSING,
7774 NULL, node, "name", NULL);
7775 return (NULL);
7776 }
7777 /*
7778 * The name is crucial, exit if invalid.
7779 */
7780 if (xmlSchemaPValAttrNode(pctxt,
7781 NULL, attr,
7782 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7783 return (NULL);
7784 }
7785 ret = xmlSchemaAddAttributeGroupDefinition(pctxt, schema,
7786 name, pctxt->targetNamespace, node);
7787 if (ret == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +00007788 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007789 /*
7790 * Check for illegal attributes.
7791 */
7792 attr = node->properties;
7793 while (attr != NULL) {
7794 if (attr->ns == NULL) {
7795 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7796 (!xmlStrEqual(attr->name, BAD_CAST "id")))
7797 {
7798 xmlSchemaPIllegalAttrErr(pctxt,
7799 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7800 }
7801 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7802 xmlSchemaPIllegalAttrErr(pctxt,
7803 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7804 }
7805 attr = attr->next;
7806 }
7807 /* Attribute ID */
7808 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7809 /*
7810 * And now for the children...
7811 */
7812 child = node->children;
7813 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007814 ret->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007815 child = child->next;
7816 }
7817 /*
7818 * Parse contained attribute decls/refs.
7819 */
7820 if (xmlSchemaParseLocalAttributes(pctxt, schema, &child,
7821 (xmlSchemaItemListPtr *) &(ret->attrUses),
7822 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, &hasRefs) == -1)
7823 return(NULL);
7824 if (hasRefs)
7825 ret->flags |= XML_SCHEMAS_ATTRGROUP_HAS_REFS;
7826 /*
7827 * Parse the attribute wildcard.
7828 */
7829 if (IS_SCHEMA(child, "anyAttribute")) {
7830 ret->attributeWildcard = xmlSchemaParseAnyAttribute(pctxt,
7831 schema, child);
7832 child = child->next;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007833 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007834 if (child != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007835 xmlSchemaPContentErr(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007836 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007837 NULL, node, child, NULL,
7838 "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))");
Daniel Veillard4255d502002-04-16 15:50:10 +00007839 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007840 return (ret);
7841}
7842
7843/**
William M. Brack2f2a6632004-08-20 23:09:47 +00007844 * xmlSchemaPValAttrFormDefault:
7845 * @value: the value
7846 * @flags: the flags to be modified
7847 * @flagQualified: the specific flag for "qualified"
7848 *
7849 * Returns 0 if the value is valid, 1 otherwise.
7850 */
7851static int
7852xmlSchemaPValAttrFormDefault(const xmlChar *value,
7853 int *flags,
7854 int flagQualified)
7855{
7856 if (xmlStrEqual(value, BAD_CAST "qualified")) {
7857 if ((*flags & flagQualified) == 0)
7858 *flags |= flagQualified;
7859 } else if (!xmlStrEqual(value, BAD_CAST "unqualified"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007860 return (1);
7861
William M. Brack2f2a6632004-08-20 23:09:47 +00007862 return (0);
7863}
7864
7865/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00007866 * xmlSchemaPValAttrBlockFinal:
7867 * @value: the value
7868 * @flags: the flags to be modified
7869 * @flagAll: the specific flag for "#all"
7870 * @flagExtension: the specific flag for "extension"
7871 * @flagRestriction: the specific flag for "restriction"
7872 * @flagSubstitution: the specific flag for "substitution"
7873 * @flagList: the specific flag for "list"
7874 * @flagUnion: the specific flag for "union"
7875 *
7876 * Validates the value of the attribute "final" and "block". The value
7877 * is converted into the specified flag values and returned in @flags.
7878 *
7879 * Returns 0 if the value is valid, 1 otherwise.
7880 */
7881
7882static int
7883xmlSchemaPValAttrBlockFinal(const xmlChar *value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007884 int *flags,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007885 int flagAll,
7886 int flagExtension,
7887 int flagRestriction,
7888 int flagSubstitution,
7889 int flagList,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007890 int flagUnion)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007891{
7892 int ret = 0;
7893
7894 /*
7895 * TODO: This does not check for dublicate entries.
7896 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00007897 if ((flags == NULL) || (value == NULL))
7898 return (-1);
7899 if (value[0] == 0)
7900 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007901 if (xmlStrEqual(value, BAD_CAST "#all")) {
7902 if (flagAll != -1)
7903 *flags |= flagAll;
7904 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007905 if (flagExtension != -1)
7906 *flags |= flagExtension;
7907 if (flagRestriction != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007908 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007909 if (flagSubstitution != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007910 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007911 if (flagList != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007912 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007913 if (flagUnion != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007914 *flags |= flagUnion;
7915 }
7916 } else {
7917 const xmlChar *end, *cur = value;
7918 xmlChar *item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007919
Daniel Veillardc0826a72004-08-10 14:17:33 +00007920 do {
7921 while (IS_BLANK_CH(*cur))
7922 cur++;
7923 end = cur;
7924 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
7925 end++;
7926 if (end == cur)
7927 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007928 item = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007929 if (xmlStrEqual(item, BAD_CAST "extension")) {
7930 if (flagExtension != -1) {
7931 if ((*flags & flagExtension) == 0)
7932 *flags |= flagExtension;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007933 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007934 ret = 1;
7935 } else if (xmlStrEqual(item, BAD_CAST "restriction")) {
7936 if (flagRestriction != -1) {
7937 if ((*flags & flagRestriction) == 0)
7938 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007939 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007940 ret = 1;
7941 } else if (xmlStrEqual(item, BAD_CAST "substitution")) {
7942 if (flagSubstitution != -1) {
7943 if ((*flags & flagSubstitution) == 0)
7944 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007945 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007946 ret = 1;
7947 } else if (xmlStrEqual(item, BAD_CAST "list")) {
7948 if (flagList != -1) {
7949 if ((*flags & flagList) == 0)
7950 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007951 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007952 ret = 1;
7953 } else if (xmlStrEqual(item, BAD_CAST "union")) {
7954 if (flagUnion != -1) {
7955 if ((*flags & flagUnion) == 0)
7956 *flags |= flagUnion;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007957 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007958 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007959 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007960 ret = 1;
7961 if (item != NULL)
7962 xmlFree(item);
7963 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007964 } while ((ret == 0) && (*cur != 0));
7965 }
7966
Daniel Veillardc0826a72004-08-10 14:17:33 +00007967 return (ret);
7968}
7969
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007970static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007971xmlSchemaCheckCSelectorXPath(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007972 xmlSchemaIDCPtr idc,
7973 xmlSchemaIDCSelectPtr selector,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007974 xmlAttrPtr attr,
7975 int isField)
7976{
7977 xmlNodePtr node;
7978
7979 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007980 * c-selector-xpath:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007981 * Schema Component Constraint: Selector Value OK
7982 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007983 * TODO: 1 The {selector} must be a valid XPath expression, as defined
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007984 * in [XPath].
7985 */
7986 if (selector == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007987 xmlSchemaPErr(ctxt, idc->node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007988 XML_SCHEMAP_INTERNAL,
7989 "Internal error: xmlSchemaCheckCSelectorXPath, "
7990 "the selector is not specified.\n", NULL, NULL);
7991 return (-1);
7992 }
7993 if (attr == NULL)
7994 node = idc->node;
7995 else
7996 node = (xmlNodePtr) attr;
7997 if (selector->xpath == NULL) {
7998 xmlSchemaPCustomErr(ctxt,
7999 /* TODO: Adjust error code. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008000 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008001 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008002 "The XPath expression of the selector is not valid", NULL);
8003 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
8004 } else {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008005 const xmlChar **nsArray = NULL;
8006 xmlNsPtr *nsList = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008007 /*
8008 * Compile the XPath expression.
8009 */
8010 /*
8011 * TODO: We need the array of in-scope namespaces for compilation.
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008012 * TODO: Call xmlPatterncompile with different options for selector/
8013 * field.
8014 */
Daniel Veillard14b56432006-03-09 18:41:40 +00008015 if (attr == NULL)
Daniel Veillardaac7c682006-03-10 13:40:16 +00008016 nsList = NULL;
Daniel Veillard14b56432006-03-09 18:41:40 +00008017 else
8018 nsList = xmlGetNsList(attr->doc, attr->parent);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008019 /*
8020 * Build an array of prefixes and namespaces.
8021 */
8022 if (nsList != NULL) {
8023 int i, count = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008024
8025 for (i = 0; nsList[i] != NULL; i++)
8026 count++;
8027
8028 nsArray = (const xmlChar **) xmlMalloc(
8029 (count * 2 + 1) * sizeof(const xmlChar *));
8030 if (nsArray == NULL) {
8031 xmlSchemaPErrMemory(ctxt, "allocating a namespace array",
8032 NULL);
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00008033 xmlFree(nsList);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008034 return (-1);
8035 }
8036 for (i = 0; i < count; i++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008037 nsArray[2 * i] = nsList[i]->href;
8038 nsArray[2 * i + 1] = nsList[i]->prefix;
8039 }
8040 nsArray[count * 2] = NULL;
8041 xmlFree(nsList);
8042 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008043 /*
8044 * TODO: Differentiate between "selector" and "field".
8045 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008046 if (isField)
8047 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008048 NULL, XML_PATTERN_XSFIELD, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008049 else
8050 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008051 NULL, XML_PATTERN_XSSEL, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008052 if (nsArray != NULL)
8053 xmlFree((xmlChar **) nsArray);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008054
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00008055 if (selector->xpathComp == NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008056 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00008057 /* TODO: Adjust error code? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008058 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008059 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008060 "The XPath expression '%s' could not be "
8061 "compiled", selector->xpath);
8062 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008063 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008064 }
8065 return (0);
8066}
8067
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008068#define ADD_ANNOTATION(annot) \
8069 xmlSchemaAnnotPtr cur = item->annot; \
8070 if (item->annot == NULL) { \
8071 item->annot = annot; \
8072 return (annot); \
8073 } \
8074 cur = item->annot; \
8075 if (cur->next != NULL) { \
8076 cur = cur->next; \
8077 } \
8078 cur->next = annot;
8079
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008080/**
8081 * xmlSchemaAssignAnnotation:
8082 * @item: the schema component
8083 * @annot: the annotation
8084 *
8085 * Adds the annotation to the given schema component.
8086 *
8087 * Returns the given annotaion.
8088 */
8089static xmlSchemaAnnotPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008090xmlSchemaAddAnnotation(xmlSchemaAnnotItemPtr annItem,
8091 xmlSchemaAnnotPtr annot)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008092{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008093 if ((annItem == NULL) || (annot == NULL))
8094 return (NULL);
8095 switch (annItem->type) {
8096 case XML_SCHEMA_TYPE_ELEMENT: {
8097 xmlSchemaElementPtr item = (xmlSchemaElementPtr) annItem;
8098 ADD_ANNOTATION(annot)
8099 }
8100 break;
8101 case XML_SCHEMA_TYPE_ATTRIBUTE: {
8102 xmlSchemaAttributePtr item = (xmlSchemaAttributePtr) annItem;
8103 ADD_ANNOTATION(annot)
8104 }
8105 break;
8106 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
8107 case XML_SCHEMA_TYPE_ANY: {
8108 xmlSchemaWildcardPtr item = (xmlSchemaWildcardPtr) annItem;
8109 ADD_ANNOTATION(annot)
8110 }
8111 break;
8112 case XML_SCHEMA_TYPE_PARTICLE:
8113 case XML_SCHEMA_TYPE_IDC_KEY:
8114 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008115 case XML_SCHEMA_TYPE_IDC_UNIQUE: {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008116 xmlSchemaAnnotItemPtr item = (xmlSchemaAnnotItemPtr) annItem;
8117 ADD_ANNOTATION(annot)
8118 }
8119 break;
8120 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: {
8121 xmlSchemaAttributeGroupPtr item =
8122 (xmlSchemaAttributeGroupPtr) annItem;
8123 ADD_ANNOTATION(annot)
8124 }
8125 break;
8126 case XML_SCHEMA_TYPE_NOTATION: {
8127 xmlSchemaNotationPtr item = (xmlSchemaNotationPtr) annItem;
8128 ADD_ANNOTATION(annot)
8129 }
8130 break;
8131 case XML_SCHEMA_FACET_MININCLUSIVE:
8132 case XML_SCHEMA_FACET_MINEXCLUSIVE:
8133 case XML_SCHEMA_FACET_MAXINCLUSIVE:
8134 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
8135 case XML_SCHEMA_FACET_TOTALDIGITS:
8136 case XML_SCHEMA_FACET_FRACTIONDIGITS:
8137 case XML_SCHEMA_FACET_PATTERN:
8138 case XML_SCHEMA_FACET_ENUMERATION:
8139 case XML_SCHEMA_FACET_WHITESPACE:
8140 case XML_SCHEMA_FACET_LENGTH:
8141 case XML_SCHEMA_FACET_MAXLENGTH:
8142 case XML_SCHEMA_FACET_MINLENGTH: {
8143 xmlSchemaFacetPtr item = (xmlSchemaFacetPtr) annItem;
8144 ADD_ANNOTATION(annot)
8145 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008146 break;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008147 case XML_SCHEMA_TYPE_SIMPLE:
8148 case XML_SCHEMA_TYPE_COMPLEX: {
8149 xmlSchemaTypePtr item = (xmlSchemaTypePtr) annItem;
8150 ADD_ANNOTATION(annot)
8151 }
8152 break;
8153 case XML_SCHEMA_TYPE_GROUP: {
8154 xmlSchemaModelGroupDefPtr item = (xmlSchemaModelGroupDefPtr) annItem;
8155 ADD_ANNOTATION(annot)
8156 }
8157 break;
8158 case XML_SCHEMA_TYPE_SEQUENCE:
8159 case XML_SCHEMA_TYPE_CHOICE:
8160 case XML_SCHEMA_TYPE_ALL: {
8161 xmlSchemaModelGroupPtr item = (xmlSchemaModelGroupPtr) annItem;
8162 ADD_ANNOTATION(annot)
8163 }
8164 break;
8165 default:
8166 xmlSchemaPCustomErr(NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008167 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008168 NULL, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008169 "Internal error: xmlSchemaAddAnnotation, "
8170 "The item is not a annotated schema component", NULL);
8171 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008172 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008173 return (annot);
8174}
8175
8176/**
8177 * xmlSchemaParseIDCSelectorAndField:
8178 * @ctxt: a schema validation context
8179 * @schema: the schema being built
8180 * @node: a subtree containing XML Schema informations
8181 *
8182 * Parses a XML Schema identity-contraint definition's
8183 * <selector> and <field> elements.
8184 *
8185 * Returns the parsed identity-constraint definition.
8186 */
8187static xmlSchemaIDCSelectPtr
Daniel Veillarddee23482008-04-11 12:58:43 +00008188xmlSchemaParseIDCSelectorAndField(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008189 xmlSchemaIDCPtr idc,
8190 xmlNodePtr node,
8191 int isField)
8192{
8193 xmlSchemaIDCSelectPtr item;
8194 xmlNodePtr child = NULL;
8195 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008196
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008197 /*
8198 * Check for illegal attributes.
8199 */
8200 attr = node->properties;
8201 while (attr != NULL) {
8202 if (attr->ns == NULL) {
8203 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8204 (!xmlStrEqual(attr->name, BAD_CAST "xpath"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008205 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008206 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008207 }
8208 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008209 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008210 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008211 }
8212 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008213 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008214 /*
8215 * Create the item.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008216 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008217 item = (xmlSchemaIDCSelectPtr) xmlMalloc(sizeof(xmlSchemaIDCSelect));
8218 if (item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008219 xmlSchemaPErrMemory(ctxt,
8220 "allocating a 'selector' of an identity-constraint definition",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008221 NULL);
8222 return (NULL);
8223 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008224 memset(item, 0, sizeof(xmlSchemaIDCSelect));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008225 /*
8226 * Attribute "xpath" (mandatory).
8227 */
8228 attr = xmlSchemaGetPropNode(node, "xpath");
8229 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008230 xmlSchemaPMissingAttrErr(ctxt,
8231 XML_SCHEMAP_S4S_ATTR_MISSING,
8232 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008233 "name", NULL);
8234 } else {
8235 item->xpath = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8236 /*
8237 * URGENT TODO: "field"s have an other syntax than "selector"s.
8238 */
8239
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008240 if (xmlSchemaCheckCSelectorXPath(ctxt, idc, item, attr,
8241 isField) == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008242 xmlSchemaPErr(ctxt,
8243 (xmlNodePtr) attr,
8244 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008245 "Internal error: xmlSchemaParseIDCSelectorAndField, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008246 "validating the XPath expression of a IDC selector.\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008247 NULL, NULL);
8248 }
8249
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008250 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008251 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008252 /*
8253 * And now for the children...
8254 */
8255 child = node->children;
8256 if (IS_SCHEMA(child, "annotation")) {
8257 /*
8258 * Add the annotation to the parent IDC.
8259 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008260 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) idc,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008261 xmlSchemaParseAnnotation(ctxt, child, 1));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008262 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008263 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008264 if (child != NULL) {
8265 xmlSchemaPContentErr(ctxt,
8266 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008267 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008268 NULL, "(annotation?)");
8269 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008270
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008271 return (item);
8272}
8273
8274/**
8275 * xmlSchemaParseIDC:
8276 * @ctxt: a schema validation context
8277 * @schema: the schema being built
8278 * @node: a subtree containing XML Schema informations
8279 *
8280 * Parses a XML Schema identity-contraint definition.
8281 *
8282 * Returns the parsed identity-constraint definition.
8283 */
8284static xmlSchemaIDCPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008285xmlSchemaParseIDC(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008286 xmlSchemaPtr schema,
8287 xmlNodePtr node,
8288 xmlSchemaTypeType idcCategory,
8289 const xmlChar *targetNamespace)
8290{
8291 xmlSchemaIDCPtr item = NULL;
8292 xmlNodePtr child = NULL;
8293 xmlAttrPtr attr;
8294 const xmlChar *name = NULL;
8295 xmlSchemaIDCSelectPtr field = NULL, lastField = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008296
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008297 /*
8298 * Check for illegal attributes.
8299 */
8300 attr = node->properties;
8301 while (attr != NULL) {
8302 if (attr->ns == NULL) {
8303 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8304 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8305 ((idcCategory != XML_SCHEMA_TYPE_IDC_KEYREF) ||
8306 (!xmlStrEqual(attr->name, BAD_CAST "refer")))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008307 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008308 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008309 }
8310 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008311 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008312 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008313 }
8314 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008315 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008316 /*
8317 * Attribute "name" (mandatory).
8318 */
8319 attr = xmlSchemaGetPropNode(node, "name");
8320 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008321 xmlSchemaPMissingAttrErr(ctxt,
8322 XML_SCHEMAP_S4S_ATTR_MISSING,
8323 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008324 "name", NULL);
8325 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008326 } else if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008327 NULL, attr,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008328 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
8329 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008330 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008331 /* Create the component. */
8332 item = xmlSchemaAddIDC(ctxt, schema, name, targetNamespace,
8333 idcCategory, node);
8334 if (item == NULL)
8335 return(NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008336
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008337 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008338 if (idcCategory == XML_SCHEMA_TYPE_IDC_KEYREF) {
8339 /*
8340 * Attribute "refer" (mandatory).
8341 */
8342 attr = xmlSchemaGetPropNode(node, "refer");
8343 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008344 xmlSchemaPMissingAttrErr(ctxt,
8345 XML_SCHEMAP_S4S_ATTR_MISSING,
8346 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008347 "refer", NULL);
8348 } else {
8349 /*
8350 * Create a reference item.
8351 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008352 item->ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_IDC_KEY,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008353 NULL, NULL);
8354 if (item->ref == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008355 return (NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008356 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008357 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008358 &(item->ref->targetNamespace),
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008359 &(item->ref->name));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008360 xmlSchemaCheckReference(ctxt, schema, node, attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008361 item->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008362 }
8363 }
8364 /*
8365 * And now for the children...
8366 */
8367 child = node->children;
8368 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008369 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008370 child = child->next;
8371 }
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008372 if (child == NULL) {
8373 xmlSchemaPContentErr(ctxt,
8374 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008375 NULL, node, child,
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008376 "A child element is missing",
8377 "(annotation?, (selector, field+))");
8378 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008379 /*
8380 * Child element <selector>.
8381 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008382 if (IS_SCHEMA(child, "selector")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008383 item->selector = xmlSchemaParseIDCSelectorAndField(ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008384 item, child, 0);
8385 child = child->next;
8386 /*
8387 * Child elements <field>.
8388 */
8389 if (IS_SCHEMA(child, "field")) {
8390 do {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008391 field = xmlSchemaParseIDCSelectorAndField(ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008392 item, child, 1);
8393 if (field != NULL) {
8394 field->index = item->nbFields;
8395 item->nbFields++;
8396 if (lastField != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008397 lastField->next = field;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008398 else
8399 item->fields = field;
8400 lastField = field;
8401 }
8402 child = child->next;
8403 } while (IS_SCHEMA(child, "field"));
8404 } else {
8405 xmlSchemaPContentErr(ctxt,
8406 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008407 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008408 NULL, "(annotation?, (selector, field+))");
8409 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008410 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008411 if (child != NULL) {
8412 xmlSchemaPContentErr(ctxt,
8413 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008414 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008415 NULL, "(annotation?, (selector, field+))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008416 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008417
8418 return (item);
8419}
8420
Daniel Veillardc0826a72004-08-10 14:17:33 +00008421/**
Daniel Veillard4255d502002-04-16 15:50:10 +00008422 * xmlSchemaParseElement:
8423 * @ctxt: a schema validation context
8424 * @schema: the schema being built
8425 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008426 * @topLevel: indicates if this is global declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00008427 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008428 * Parses a XML schema element declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00008429 * *WARNING* this interface is highly subject to change
8430 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008431 * Returns the element declaration or a particle; NULL in case
8432 * of an error or if the particle has minOccurs==maxOccurs==0.
Daniel Veillard4255d502002-04-16 15:50:10 +00008433 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008434static xmlSchemaBasicItemPtr
Daniel Veillard4255d502002-04-16 15:50:10 +00008435xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008436 xmlNodePtr node, int *isElemRef, int topLevel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008437{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008438 xmlSchemaElementPtr decl = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008439 xmlSchemaParticlePtr particle = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008440 xmlSchemaAnnotPtr annot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008441 xmlNodePtr child = NULL;
8442 xmlAttrPtr attr, nameAttr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008443 int min, max, isRef = 0;
8444 xmlChar *des = NULL;
William M. Bracke7091952004-05-11 15:09:58 +00008445
8446 /* 3.3.3 Constraints on XML Representations of Element Declarations */
8447 /* TODO: Complete implementation of 3.3.6 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008448
Daniel Veillard4255d502002-04-16 15:50:10 +00008449 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008450 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008451
8452 if (isElemRef != NULL)
8453 *isElemRef = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008454 /*
8455 * If we get a "ref" attribute on a local <element> we will assume it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008456 * a reference - even if there's a "name" attribute; this seems to be more
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008457 * robust.
8458 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008459 nameAttr = xmlSchemaGetPropNode(node, "name");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008460 attr = xmlSchemaGetPropNode(node, "ref");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008461 if ((topLevel) || (attr == NULL)) {
8462 if (nameAttr == NULL) {
8463 xmlSchemaPMissingAttrErr(ctxt,
8464 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008465 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008466 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008467 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008468 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00008469 isRef = 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008470
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008471 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008472 child = node->children;
8473 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008474 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008475 child = child->next;
8476 }
8477 /*
8478 * Skip particle part if a global declaration.
8479 */
8480 if (topLevel)
8481 goto declaration_part;
8482 /*
8483 * The particle part ==================================================
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008484 */
8485 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
8486 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1, "(xs:nonNegativeInteger | unbounded)");
8487 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00008488 particle = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008489 if (particle == NULL)
8490 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008491
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008492 /* ret->flags |= XML_SCHEMAS_ELEM_REF; */
8493
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008494 if (isRef) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008495 const xmlChar *refNs = NULL, *ref = NULL;
8496 xmlSchemaQNameRefPtr refer = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008497 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008498 * The reference part =============================================
Daniel Veillardc0826a72004-08-10 14:17:33 +00008499 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008500 if (isElemRef != NULL)
8501 *isElemRef = 1;
8502
Daniel Veillardc0826a72004-08-10 14:17:33 +00008503 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008504 NULL, attr, &refNs, &ref);
8505 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008506 /*
8507 * SPEC (3.3.3 : 2.1) "One of ref or name must be present, but not both"
Daniel Veillardc0826a72004-08-10 14:17:33 +00008508 */
8509 if (nameAttr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008510 xmlSchemaPMutualExclAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008511 XML_SCHEMAP_SRC_ELEMENT_2_1, NULL, nameAttr, "ref", "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008512 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008513 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008514 * Check for illegal attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008515 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008516 attr = node->properties;
8517 while (attr != NULL) {
8518 if (attr->ns == NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008519 if (xmlStrEqual(attr->name, BAD_CAST "ref") ||
8520 xmlStrEqual(attr->name, BAD_CAST "name") ||
8521 xmlStrEqual(attr->name, BAD_CAST "id") ||
8522 xmlStrEqual(attr->name, BAD_CAST "maxOccurs") ||
8523 xmlStrEqual(attr->name, BAD_CAST "minOccurs"))
8524 {
8525 attr = attr->next;
8526 continue;
8527 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008528 /* SPEC (3.3.3 : 2.2) */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008529 xmlSchemaPCustomAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008530 XML_SCHEMAP_SRC_ELEMENT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008531 NULL, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008532 "Only the attributes 'minOccurs', 'maxOccurs' and "
8533 "'id' are allowed in addition to 'ref'");
8534 break;
8535 }
8536 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
8537 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008538 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008539 }
8540 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008541 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008542 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008543 * No children except <annotation> expected.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008544 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008545 if (child != NULL) {
8546 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008547 NULL, node, child, NULL, "(annotation?)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008548 }
8549 if ((min == 0) && (max == 0))
8550 goto return_null;
8551 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008552 * Create the reference item and attach it to the particle.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008553 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008554 refer = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_ELEMENT,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008555 ref, refNs);
8556 if (refer == NULL)
8557 goto return_null;
8558 particle->children = (xmlSchemaTreeItemPtr) refer;
8559 particle->annot = annot;
8560 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008561 * Add the particle to pending components, since the reference
8562 * need to be resolved.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008563 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008564 WXS_ADD_PENDING(ctxt, particle);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008565 return ((xmlSchemaBasicItemPtr) particle);
8566 }
8567 /*
8568 * The declaration part ===============================================
8569 */
8570declaration_part:
8571 {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008572 const xmlChar *ns = NULL, *fixed, *name, *attrValue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008573 xmlSchemaIDCPtr curIDC = NULL, lastIDC = NULL;
8574
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008575 if (xmlSchemaPValAttrNode(ctxt, NULL, nameAttr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008576 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008577 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008578 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008579 * Evaluate the target namespace.
8580 */
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008581 if (topLevel) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008582 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008583 } else {
8584 attr = xmlSchemaGetPropNode(node, "form");
8585 if (attr != NULL) {
8586 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8587 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008588 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008589 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008590 xmlSchemaPSimpleTypeErr(ctxt,
8591 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
8592 NULL, (xmlNodePtr) attr,
8593 NULL, "(qualified | unqualified)",
8594 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008595 }
8596 } else if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008597 ns = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008598 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008599 decl = xmlSchemaAddElement(ctxt, name, ns, node, topLevel);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008600 if (decl == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008601 goto return_null;
William M. Bracke7091952004-05-11 15:09:58 +00008602 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008603 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008604 * Check for illegal attributes.
8605 */
William M. Bracke7091952004-05-11 15:09:58 +00008606 attr = node->properties;
8607 while (attr != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008608 if (attr->ns == NULL) {
8609 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8610 (!xmlStrEqual(attr->name, BAD_CAST "type")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008611 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008612 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008613 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008614 (!xmlStrEqual(attr->name, BAD_CAST "block")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008615 (!xmlStrEqual(attr->name, BAD_CAST "nillable")))
8616 {
8617 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008618 if ((!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008619 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008620 (!xmlStrEqual(attr->name, BAD_CAST "form")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008621 {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008622 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008623 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008624 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008625 } else if ((!xmlStrEqual(attr->name, BAD_CAST "final")) &&
8626 (!xmlStrEqual(attr->name, BAD_CAST "abstract")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008627 (!xmlStrEqual(attr->name, BAD_CAST "substitutionGroup"))) {
8628
8629 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008630 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008631 }
8632 }
8633 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008634
Daniel Veillardc0826a72004-08-10 14:17:33 +00008635 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008636 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Bracke7091952004-05-11 15:09:58 +00008637 }
8638 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008639 }
William M. Bracke7091952004-05-11 15:09:58 +00008640 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008641 * Extract/validate attributes.
8642 */
8643 if (topLevel) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008644 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008645 * Process top attributes of global element declarations here.
8646 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008647 decl->flags |= XML_SCHEMAS_ELEM_GLOBAL;
8648 decl->flags |= XML_SCHEMAS_ELEM_TOPLEVEL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008649 xmlSchemaPValAttrQName(ctxt, schema,
8650 NULL, node, "substitutionGroup",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008651 &(decl->substGroupNs), &(decl->substGroup));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008652 if (xmlGetBooleanProp(ctxt, node, "abstract", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008653 decl->flags |= XML_SCHEMAS_ELEM_ABSTRACT;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008654 /*
8655 * Attribute "final".
8656 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008657 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008658 if (attr == NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008659 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
8660 decl->flags |= XML_SCHEMAS_ELEM_FINAL_EXTENSION;
8661 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
8662 decl->flags |= XML_SCHEMAS_ELEM_FINAL_RESTRICTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008663 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008664 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8665 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008666 -1,
8667 XML_SCHEMAS_ELEM_FINAL_EXTENSION,
8668 XML_SCHEMAS_ELEM_FINAL_RESTRICTION, -1, -1, -1) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008669 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008670 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008671 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008672 NULL, "(#all | List of (extension | restriction))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00008673 attrValue, NULL, NULL, NULL);
8674 }
8675 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008676 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008677 /*
8678 * Attribute "block".
8679 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008680 attr = xmlSchemaGetPropNode(node, "block");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008681 if (attr == NULL) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00008682 /*
8683 * Apply default "block" values.
8684 */
8685 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
8686 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_RESTRICTION;
8687 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
8688 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_EXTENSION;
8689 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
8690 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008691 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008692 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8693 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008694 -1,
8695 XML_SCHEMAS_ELEM_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008696 XML_SCHEMAS_ELEM_BLOCK_RESTRICTION,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008697 XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION, -1, -1) != 0) {
8698 xmlSchemaPSimpleTypeErr(ctxt,
8699 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008700 NULL, (xmlNodePtr) attr,
William M. Brack2f2a6632004-08-20 23:09:47 +00008701 NULL, "(#all | List of (extension | "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008702 "restriction | substitution))", attrValue,
8703 NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008704 }
8705 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008706 if (xmlGetBooleanProp(ctxt, node, "nillable", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008707 decl->flags |= XML_SCHEMAS_ELEM_NILLABLE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008708
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008709 attr = xmlSchemaGetPropNode(node, "type");
8710 if (attr != NULL) {
8711 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008712 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008713 &(decl->namedTypeNs), &(decl->namedType));
8714 xmlSchemaCheckReference(ctxt, schema, node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008715 attr, decl->namedTypeNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008716 }
8717 decl->value = xmlSchemaGetProp(ctxt, node, "default");
8718 attr = xmlSchemaGetPropNode(node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008719 if (attr != NULL) {
8720 fixed = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008721 if (decl->value != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008722 /*
8723 * 3.3.3 : 1
8724 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008725 */
8726 xmlSchemaPMutualExclAttrErr(ctxt,
8727 XML_SCHEMAP_SRC_ELEMENT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008728 NULL, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008729 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008730 decl->flags |= XML_SCHEMAS_ELEM_FIXED;
8731 decl->value = fixed;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008732 }
William M. Bracke7091952004-05-11 15:09:58 +00008733 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008734 /*
8735 * And now for the children...
8736 */
William M. Bracke7091952004-05-11 15:09:58 +00008737 if (IS_SCHEMA(child, "complexType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008738 /*
8739 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008740 * "type" and either <simpleType> or <complexType> are mutually
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008741 * exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008742 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008743 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008744 xmlSchemaPContentErr(ctxt,
8745 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008746 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008747 "The attribute 'type' and the <complexType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008748 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008749 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008750 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseComplexType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008751 child = child->next;
8752 } else if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008753 /*
8754 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008755 * "type" and either <simpleType> or <complexType> are
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008756 * mutually exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008757 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008758 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008759 xmlSchemaPContentErr(ctxt,
8760 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008761 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008762 "The attribute 'type' and the <simpleType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008763 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008764 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008765 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008766 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008767 }
William M. Bracke7091952004-05-11 15:09:58 +00008768 while ((IS_SCHEMA(child, "unique")) ||
Daniel Veillardc0826a72004-08-10 14:17:33 +00008769 (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008770 if (IS_SCHEMA(child, "unique")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008771 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008772 XML_SCHEMA_TYPE_IDC_UNIQUE, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008773 } else if (IS_SCHEMA(child, "key")) {
8774 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008775 XML_SCHEMA_TYPE_IDC_KEY, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008776 } else if (IS_SCHEMA(child, "keyref")) {
8777 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008778 XML_SCHEMA_TYPE_IDC_KEYREF, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008779 }
8780 if (lastIDC != NULL)
8781 lastIDC->next = curIDC;
8782 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008783 decl->idcs = (void *) curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008784 lastIDC = curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008785 child = child->next;
William M. Bracke7091952004-05-11 15:09:58 +00008786 }
8787 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008788 xmlSchemaPContentErr(ctxt,
8789 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008790 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008791 NULL, "(annotation?, ((simpleType | complexType)?, "
8792 "(unique | key | keyref)*))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008793 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008794 decl->annot = annot;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008795 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008796 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008797 * NOTE: Element Declaration Representation OK 4. will be checked at a
Daniel Veillardc0826a72004-08-10 14:17:33 +00008798 * different layer.
8799 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008800 FREE_AND_NULL(des)
8801 if (topLevel)
8802 return ((xmlSchemaBasicItemPtr) decl);
8803 else {
8804 particle->children = (xmlSchemaTreeItemPtr) decl;
8805 return ((xmlSchemaBasicItemPtr) particle);
8806 }
8807
8808return_null:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008809 FREE_AND_NULL(des);
8810 if (annot != NULL) {
8811 if (particle != NULL)
8812 particle->annot = NULL;
8813 if (decl != NULL)
8814 decl->annot = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008815 xmlSchemaFreeAnnot(annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008816 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008817 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00008818}
8819
8820/**
8821 * xmlSchemaParseUnion:
8822 * @ctxt: a schema validation context
8823 * @schema: the schema being built
8824 * @node: a subtree containing XML Schema informations
8825 *
8826 * parse a XML schema Union definition
8827 * *WARNING* this interface is highly subject to change
8828 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008829 * Returns -1 in case of internal error, 0 in case of success and a positive
8830 * error code otherwise.
Daniel Veillard4255d502002-04-16 15:50:10 +00008831 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008832static int
Daniel Veillard4255d502002-04-16 15:50:10 +00008833xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008834 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008835{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008836 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008837 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008838 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008839 const xmlChar *cur = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00008840
8841 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008842 return (-1);
8843 /* Not a component, don't create it. */
8844 type = ctxt->ctxtType;
8845 /*
8846 * Mark the simple type as being of variety "union".
8847 */
8848 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008849 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008850 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
Rob Richardsc6947bb2008-06-29 15:04:41 +00008851 * then the �simple ur-type definition�."
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008852 */
8853 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008854 /*
8855 * Check for illegal attributes.
8856 */
8857 attr = node->properties;
8858 while (attr != NULL) {
8859 if (attr->ns == NULL) {
8860 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8861 (!xmlStrEqual(attr->name, BAD_CAST "memberTypes"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008862 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008863 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008864 }
8865 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008866 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008867 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008868 }
8869 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008870 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008871 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +00008872 /*
8873 * Attribute "memberTypes". This is a list of QNames.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008874 * TODO: Check the value to contain anything.
William M. Brack2f2a6632004-08-20 23:09:47 +00008875 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008876 attr = xmlSchemaGetPropNode(node, "memberTypes");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008877 if (attr != NULL) {
8878 const xmlChar *end;
8879 xmlChar *tmp;
8880 const xmlChar *localName, *nsName;
8881 xmlSchemaTypeLinkPtr link, lastLink = NULL;
8882 xmlSchemaQNameRefPtr ref;
8883
8884 cur = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008885 type->base = cur;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008886 do {
8887 while (IS_BLANK_CH(*cur))
8888 cur++;
8889 end = cur;
8890 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
8891 end++;
8892 if (end == cur)
8893 break;
8894 tmp = xmlStrndup(cur, end - cur);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008895 if (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008896 NULL, attr, BAD_CAST tmp, &nsName, &localName) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008897 /*
8898 * Create the member type link.
8899 */
8900 link = (xmlSchemaTypeLinkPtr)
8901 xmlMalloc(sizeof(xmlSchemaTypeLink));
8902 if (link == NULL) {
8903 xmlSchemaPErrMemory(ctxt, "xmlSchemaParseUnion, "
8904 "allocating a type link", NULL);
8905 return (-1);
8906 }
8907 link->type = NULL;
8908 link->next = NULL;
8909 if (lastLink == NULL)
8910 type->memberTypes = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008911 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008912 lastLink->next = link;
8913 lastLink = link;
8914 /*
8915 * Create a reference item.
8916 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008917 ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_SIMPLE,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008918 localName, nsName);
8919 if (ref == NULL) {
8920 FREE_AND_NULL(tmp)
8921 return (-1);
8922 }
8923 /*
8924 * Assign the reference to the link, it will be resolved
8925 * later during fixup of the union simple type.
8926 */
8927 link->type = (xmlSchemaTypePtr) ref;
8928 }
8929 FREE_AND_NULL(tmp)
8930 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008931 } while (*cur != 0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008932
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008933 }
William M. Brack2f2a6632004-08-20 23:09:47 +00008934 /*
8935 * And now for the children...
8936 */
Daniel Veillard4255d502002-04-16 15:50:10 +00008937 child = node->children;
8938 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008939 /*
8940 * Add the annotation to the simple type ancestor.
8941 */
8942 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008943 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008944 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00008945 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008946 if (IS_SCHEMA(child, "simpleType")) {
8947 xmlSchemaTypePtr subtype, last = NULL;
8948
8949 /*
8950 * Anchor the member types in the "subtypes" field of the
8951 * simple type.
8952 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008953 while (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008954 subtype = (xmlSchemaTypePtr)
8955 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
8956 if (subtype != NULL) {
8957 if (last == NULL) {
8958 type->subtypes = subtype;
8959 last = subtype;
8960 } else {
8961 last->next = subtype;
8962 last = subtype;
8963 }
8964 last->next = NULL;
8965 }
8966 child = child->next;
8967 }
Daniel Veillard4255d502002-04-16 15:50:10 +00008968 }
8969 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00008970 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008971 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008972 NULL, node, child, NULL, "(annotation?, simpleType*)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008973 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008974 if ((attr == NULL) && (type->subtypes == NULL)) {
8975 /*
8976 * src-union-memberTypes-or-simpleTypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008977 * Either the memberTypes [attribute] of the <union> element must
8978 * be non-empty or there must be at least one simpleType [child].
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008979 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008980 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008981 XML_SCHEMAP_SRC_UNION_MEMBERTYPES_OR_SIMPLETYPES,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008982 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008983 "Either the attribute 'memberTypes' or "
8984 "at least one <simpleType> child must be present", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008985 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008986 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00008987}
8988
8989/**
8990 * xmlSchemaParseList:
8991 * @ctxt: a schema validation context
8992 * @schema: the schema being built
8993 * @node: a subtree containing XML Schema informations
8994 *
8995 * parse a XML schema List definition
8996 * *WARNING* this interface is highly subject to change
8997 *
William M. Bracke7091952004-05-11 15:09:58 +00008998 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00008999 * 1 in case of success.
9000 */
9001static xmlSchemaTypePtr
9002xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009003 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00009004{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009005 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00009006 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00009007 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00009008
9009 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9010 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009011 /* Not a component, don't create it. */
9012 type = ctxt->ctxtType;
9013 /*
9014 * Mark the type as being of variety "list".
9015 */
9016 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009017 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00009018 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
Rob Richardsc6947bb2008-06-29 15:04:41 +00009019 * then the �simple ur-type definition�."
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00009020 */
9021 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00009022 /*
9023 * Check for illegal attributes.
9024 */
9025 attr = node->properties;
9026 while (attr != NULL) {
9027 if (attr->ns == NULL) {
9028 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9029 (!xmlStrEqual(attr->name, BAD_CAST "itemType"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009030 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009031 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009032 }
9033 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009034 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009035 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009036 }
9037 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009038 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009039
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009040 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009041
William M. Brack2f2a6632004-08-20 23:09:47 +00009042 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009043 * Attribute "itemType". NOTE that we will use the "ref" and "refNs"
9044 * fields for holding the reference to the itemType.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009045 *
9046 * REVAMP TODO: Use the "base" and "baseNs" fields, since we will remove
9047 * the "ref" fields.
William M. Brack2f2a6632004-08-20 23:09:47 +00009048 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009049 xmlSchemaPValAttrQName(ctxt, schema, NULL,
9050 node, "itemType", &(type->baseNs), &(type->base));
William M. Brack2f2a6632004-08-20 23:09:47 +00009051 /*
9052 * And now for the children...
9053 */
Daniel Veillard4255d502002-04-16 15:50:10 +00009054 child = node->children;
9055 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009056 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009057 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009058 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009059 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009060 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009061 /*
9062 * src-list-itemType-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009063 * Either the itemType [attribute] or the <simpleType> [child] of
9064 * the <list> element must be present, but not both.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009065 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009066 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009067 xmlSchemaPCustomErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009068 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009069 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +00009070 "The attribute 'itemType' and the <simpleType> child "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009071 "are mutually exclusive", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009072 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009073 type->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Brack2f2a6632004-08-20 23:09:47 +00009074 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009075 child = child->next;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009076 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009077 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009078 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009079 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009080 "Either the attribute 'itemType' or the <simpleType> child "
9081 "must be present", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009082 }
9083 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009084 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009085 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009086 NULL, node, child, NULL, "(annotation?, simpleType?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00009087 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009088 if ((type->base == NULL) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009089 (type->subtypes == NULL) &&
9090 (xmlSchemaGetPropNode(node, "itemType") == NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009091 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009092 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009093 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009094 "Either the attribute 'itemType' or the <simpleType> child "
9095 "must be present", NULL);
9096 }
9097 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009098}
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009099
Daniel Veillard4255d502002-04-16 15:50:10 +00009100/**
9101 * xmlSchemaParseSimpleType:
9102 * @ctxt: a schema validation context
9103 * @schema: the schema being built
9104 * @node: a subtree containing XML Schema informations
9105 *
9106 * parse a XML schema Simple Type definition
9107 * *WARNING* this interface is highly subject to change
9108 *
William M. Bracke7091952004-05-11 15:09:58 +00009109 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillardc0826a72004-08-10 14:17:33 +00009110 * 1 in case of success.
Daniel Veillard4255d502002-04-16 15:50:10 +00009111 */
9112static xmlSchemaTypePtr
9113xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00009114 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00009115{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009116 xmlSchemaTypePtr type, oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009117 xmlNodePtr child = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009118 const xmlChar *attrValue = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009119 xmlAttrPtr attr;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009120 int hasRestriction = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00009121
9122 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9123 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009124
Daniel Veillardc0826a72004-08-10 14:17:33 +00009125 if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009126 attr = xmlSchemaGetPropNode(node, "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009127 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009128 xmlSchemaPMissingAttrErr(ctxt,
9129 XML_SCHEMAP_S4S_ATTR_MISSING,
9130 NULL, node,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009131 "name", NULL);
9132 return (NULL);
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009133 } else {
9134 if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009135 NULL, attr,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009136 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0)
9137 return (NULL);
9138 /*
9139 * Skip built-in types.
9140 */
9141 if (ctxt->isS4S) {
9142 xmlSchemaTypePtr biType;
9143
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009144 if (ctxt->isRedefine) {
9145 /*
9146 * REDEFINE: Disallow redefinition of built-in-types.
9147 * TODO: It seems that the spec does not say anything
9148 * about this case.
9149 */
9150 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009151 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009152 "Redefinition of built-in simple types is not "
9153 "supported", NULL);
9154 return(NULL);
9155 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009156 biType = xmlSchemaGetPredefinedType(attrValue, xmlSchemaNs);
9157 if (biType != NULL)
9158 return (biType);
9159 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00009160 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009161 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009162 /*
9163 * TargetNamespace:
Rob Richardsc6947bb2008-06-29 15:04:41 +00009164 * SPEC "The �actual value� of the targetNamespace [attribute]
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009165 * of the <schema> ancestor element information item if present,
Rob Richardsc6947bb2008-06-29 15:04:41 +00009166 * otherwise �absent�.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009167 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00009168 if (topLevel == 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009169#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009170 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009171#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +00009172 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00009173 * Parse as local simple type definition.
Daniel Veillard01fa6152004-06-29 17:04:39 +00009174 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009175#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00009176 snprintf(buf, 39, "#ST%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009177 type = xmlSchemaAddType(ctxt, schema,
9178 XML_SCHEMA_TYPE_SIMPLE,
9179 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009180 ctxt->targetNamespace, node, 0);
9181#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009182 type = xmlSchemaAddType(ctxt, schema,
9183 XML_SCHEMA_TYPE_SIMPLE,
9184 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009185#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +00009186 if (type == NULL)
9187 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009188 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009189 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009190 /*
9191 * Check for illegal attributes.
9192 */
9193 attr = node->properties;
9194 while (attr != NULL) {
9195 if (attr->ns == NULL) {
9196 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009197 xmlSchemaPIllegalAttrErr(ctxt,
Daniel Veillarddee23482008-04-11 12:58:43 +00009198 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillard01fa6152004-06-29 17:04:39 +00009199 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009200 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009201 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009202 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009203 }
9204 attr = attr->next;
Daniel Veillard01fa6152004-06-29 17:04:39 +00009205 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009206 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00009207 /*
9208 * Parse as global simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009209 *
9210 * Note that attrValue is the value of the attribute "name" here.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009211 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009212 type = xmlSchemaAddType(ctxt, schema, XML_SCHEMA_TYPE_SIMPLE,
9213 attrValue, ctxt->targetNamespace, node, 1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009214 if (type == NULL)
9215 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009216 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009217 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009218 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
9219 /*
9220 * Check for illegal attributes.
9221 */
9222 attr = node->properties;
9223 while (attr != NULL) {
9224 if (attr->ns == NULL) {
9225 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9226 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009227 (!xmlStrEqual(attr->name, BAD_CAST "final"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009228 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009229 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009230 }
9231 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009232 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009233 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009234 }
9235 attr = attr->next;
9236 }
9237 /*
9238 * Attribute "final".
9239 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009240 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009241 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009242 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9243 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
9244 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9245 type->flags |= XML_SCHEMAS_TYPE_FINAL_LIST;
9246 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9247 type->flags |= XML_SCHEMAS_TYPE_FINAL_UNION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009248 } else {
9249 attrValue = xmlSchemaGetProp(ctxt, node, "final");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009250 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
9251 -1, -1, XML_SCHEMAS_TYPE_FINAL_RESTRICTION, -1,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009252 XML_SCHEMAS_TYPE_FINAL_LIST,
9253 XML_SCHEMAS_TYPE_FINAL_UNION) != 0) {
9254
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009255 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009256 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009257 WXS_BASIC_CAST type, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009258 NULL, "(#all | List of (list | union | restriction)",
Daniel Veillardc0826a72004-08-10 14:17:33 +00009259 attrValue, NULL, NULL, NULL);
9260 }
9261 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009262 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009263 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009264 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009265 /*
9266 * And now for the children...
9267 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009268 oldCtxtType = ctxt->ctxtType;
Daniel Veillarddee23482008-04-11 12:58:43 +00009269
Daniel Veillardc0826a72004-08-10 14:17:33 +00009270 ctxt->ctxtType = type;
Daniel Veillarddee23482008-04-11 12:58:43 +00009271
Daniel Veillard4255d502002-04-16 15:50:10 +00009272 child = node->children;
9273 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009274 type->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009275 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009276 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009277 if (child == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009278 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009279 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009280 "(annotation?, (restriction | list | union))");
Daniel Veillarddee23482008-04-11 12:58:43 +00009281 } else if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009282 xmlSchemaParseRestriction(ctxt, schema, child,
Daniel Veillarddee23482008-04-11 12:58:43 +00009283 XML_SCHEMA_TYPE_SIMPLE);
9284 hasRestriction = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009285 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009286 } else if (IS_SCHEMA(child, "list")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009287 xmlSchemaParseList(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009288 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009289 } else if (IS_SCHEMA(child, "union")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009290 xmlSchemaParseUnion(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009291 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009292 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009293 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009294 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009295 NULL, node, child, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009296 "(annotation?, (restriction | list | union))");
Daniel Veillard4255d502002-04-16 15:50:10 +00009297 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009298 /*
9299 * REDEFINE: SPEC src-redefine (5)
9300 * "Within the [children], each <simpleType> must have a
Rob Richardsc6947bb2008-06-29 15:04:41 +00009301 * <restriction> among its [children] ... the �actual value� of whose
9302 * base [attribute] must be the same as the �actual value� of its own
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009303 * name attribute plus target namespace;"
9304 */
9305 if (topLevel && ctxt->isRedefine && (! hasRestriction)) {
9306 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009307 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009308 "<simpleType> must have a <restriction> child", NULL);
9309 }
Daniel Veillarddee23482008-04-11 12:58:43 +00009310
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009311 ctxt->ctxtType = oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009312 return (type);
9313}
9314
Daniel Veillard4255d502002-04-16 15:50:10 +00009315/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009316 * xmlSchemaParseModelGroupDefRef:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009317 * @ctxt: the parser context
9318 * @schema: the schema being built
9319 * @node: the node
Daniel Veillard4255d502002-04-16 15:50:10 +00009320 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009321 * Parses a reference to a model group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00009322 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009323 * We will return a particle component with a qname-component or
9324 * NULL in case of an error.
Daniel Veillard4255d502002-04-16 15:50:10 +00009325 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009326static xmlSchemaTreeItemPtr
9327xmlSchemaParseModelGroupDefRef(xmlSchemaParserCtxtPtr ctxt,
9328 xmlSchemaPtr schema,
9329 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00009330{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009331 xmlSchemaParticlePtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009332 xmlNodePtr child = NULL;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009333 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009334 const xmlChar *ref = NULL, *refNs = NULL;
9335 int min, max;
9336
9337 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009338 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009339
9340 attr = xmlSchemaGetPropNode(node, "ref");
9341 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009342 xmlSchemaPMissingAttrErr(ctxt,
9343 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009344 NULL, node, "ref", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009345 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009346 } else if (xmlSchemaPValAttrNodeQName(ctxt, schema, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009347 attr, &refNs, &ref) != 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009348 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009349 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009350 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009351 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009352 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009353 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009354 /*
9355 * Check for illegal attributes.
9356 */
9357 attr = node->properties;
9358 while (attr != NULL) {
9359 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009360 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009361 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9362 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
9363 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009364 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009365 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009366 }
9367 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009368 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009369 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009370 }
9371 attr = attr->next;
9372 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009373 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00009374 item = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009375 if (item == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +00009376 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009377 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009378 * Create a qname-reference and set as the term; it will be substituted
9379 * for the model group after the reference has been resolved.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009380 */
9381 item->children = (xmlSchemaTreeItemPtr)
Daniel Veillarddee23482008-04-11 12:58:43 +00009382 xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_GROUP, ref, refNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009383 xmlSchemaPCheckParticleCorrect_2(ctxt, item, node, min, max);
9384 /*
9385 * And now for the children...
9386 */
9387 child = node->children;
9388 /* TODO: Is annotation even allowed for a model group reference? */
9389 if (IS_SCHEMA(child, "annotation")) {
9390 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009391 * TODO: What to do exactly with the annotation?
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009392 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009393 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009394 child = child->next;
9395 }
9396 if (child != NULL) {
9397 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009398 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009399 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009400 "(annotation?)");
9401 }
9402 /*
9403 * Corresponds to no component at all if minOccurs==maxOccurs==0.
9404 */
9405 if ((min == 0) && (max == 0))
9406 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009407
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009408 return ((xmlSchemaTreeItemPtr) item);
9409}
9410
9411/**
9412 * xmlSchemaParseModelGroupDefinition:
9413 * @ctxt: a schema validation context
9414 * @schema: the schema being built
9415 * @node: a subtree containing XML Schema informations
9416 *
9417 * Parses a XML schema model group definition.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009418 *
9419 * Note that the contraint src-redefine (6.2) can't be applied until
9420 * references have been resolved. So we will do this at the
9421 * component fixup level.
Daniel Veillarddee23482008-04-11 12:58:43 +00009422 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009423 * *WARNING* this interface is highly subject to change
9424 *
9425 * Returns -1 in case of error, 0 if the declaration is improper and
9426 * 1 in case of success.
9427 */
9428static xmlSchemaModelGroupDefPtr
9429xmlSchemaParseModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
9430 xmlSchemaPtr schema,
9431 xmlNodePtr node)
9432{
9433 xmlSchemaModelGroupDefPtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009434 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009435 xmlAttrPtr attr;
9436 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00009437
9438 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009439 return (NULL);
9440
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009441 attr = xmlSchemaGetPropNode(node, "name");
9442 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009443 xmlSchemaPMissingAttrErr(ctxt,
9444 XML_SCHEMAP_S4S_ATTR_MISSING,
9445 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009446 "name", NULL);
9447 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009448 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009449 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
9450 return (NULL);
9451 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009452 item = xmlSchemaAddModelGroupDefinition(ctxt, schema, name,
9453 ctxt->targetNamespace, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009454 if (item == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009455 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009456 /*
9457 * Check for illegal attributes.
9458 */
9459 attr = node->properties;
9460 while (attr != NULL) {
9461 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009462 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009463 (!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009464 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009465 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009466 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009467 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009468 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009469 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009470 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009471 attr = attr->next;
9472 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009473 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009474 /*
9475 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009476 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009477 child = node->children;
9478 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009479 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009480 child = child->next;
9481 }
9482 if (IS_SCHEMA(child, "all")) {
9483 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9484 XML_SCHEMA_TYPE_ALL, 0);
9485 child = child->next;
9486 } else if (IS_SCHEMA(child, "choice")) {
9487 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9488 XML_SCHEMA_TYPE_CHOICE, 0);
9489 child = child->next;
9490 } else if (IS_SCHEMA(child, "sequence")) {
9491 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9492 XML_SCHEMA_TYPE_SEQUENCE, 0);
9493 child = child->next;
9494 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009495
Daniel Veillarddee23482008-04-11 12:58:43 +00009496
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009497
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009498 if (child != NULL) {
9499 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009500 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009501 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009502 "(annotation?, (all | choice | sequence)?)");
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009503 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009504 return (item);
Daniel Veillard4255d502002-04-16 15:50:10 +00009505}
9506
9507/**
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009508 * xmlSchemaCleanupDoc:
9509 * @ctxt: a schema validation context
9510 * @node: the root of the document.
9511 *
9512 * removes unwanted nodes in a schemas document tree
9513 */
9514static void
9515xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root)
9516{
9517 xmlNodePtr delete, cur;
9518
9519 if ((ctxt == NULL) || (root == NULL)) return;
9520
9521 /*
9522 * Remove all the blank text nodes
9523 */
9524 delete = NULL;
9525 cur = root;
9526 while (cur != NULL) {
9527 if (delete != NULL) {
9528 xmlUnlinkNode(delete);
9529 xmlFreeNode(delete);
9530 delete = NULL;
9531 }
9532 if (cur->type == XML_TEXT_NODE) {
9533 if (IS_BLANK_NODE(cur)) {
9534 if (xmlNodeGetSpacePreserve(cur) != 1) {
9535 delete = cur;
9536 }
9537 }
9538 } else if ((cur->type != XML_ELEMENT_NODE) &&
9539 (cur->type != XML_CDATA_SECTION_NODE)) {
9540 delete = cur;
9541 goto skip_children;
9542 }
9543
9544 /*
9545 * Skip to next node
9546 */
9547 if (cur->children != NULL) {
9548 if ((cur->children->type != XML_ENTITY_DECL) &&
9549 (cur->children->type != XML_ENTITY_REF_NODE) &&
9550 (cur->children->type != XML_ENTITY_NODE)) {
9551 cur = cur->children;
9552 continue;
9553 }
9554 }
9555 skip_children:
9556 if (cur->next != NULL) {
9557 cur = cur->next;
9558 continue;
9559 }
9560
9561 do {
9562 cur = cur->parent;
9563 if (cur == NULL)
9564 break;
9565 if (cur == root) {
9566 cur = NULL;
9567 break;
9568 }
9569 if (cur->next != NULL) {
9570 cur = cur->next;
9571 break;
9572 }
9573 } while (cur != NULL);
9574 }
9575 if (delete != NULL) {
9576 xmlUnlinkNode(delete);
9577 xmlFreeNode(delete);
9578 delete = NULL;
9579 }
9580}
9581
William M. Brack2f2a6632004-08-20 23:09:47 +00009582
William M. Brack2f2a6632004-08-20 23:09:47 +00009583static void
9584xmlSchemaClearSchemaDefaults(xmlSchemaPtr schema)
9585{
9586 if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
9587 schema->flags ^= XML_SCHEMAS_QUALIF_ELEM;
9588
9589 if (schema->flags & XML_SCHEMAS_QUALIF_ATTR)
9590 schema->flags ^= XML_SCHEMAS_QUALIF_ATTR;
9591
9592 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
9593 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_EXTENSION;
9594 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9595 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION;
9596 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9597 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_LIST;
9598 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9599 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_UNION;
9600
9601 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
9602 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION;
9603 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
9604 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION;
9605 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
9606 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION;
9607}
9608
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009609static int
9610xmlSchemaParseSchemaElement(xmlSchemaParserCtxtPtr ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009611 xmlSchemaPtr schema,
9612 xmlNodePtr node)
9613{
9614 xmlAttrPtr attr;
9615 const xmlChar *val;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009616 int res = 0, oldErrs = ctxt->nberrors;
William M. Brack2f2a6632004-08-20 23:09:47 +00009617
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009618 /*
9619 * Those flags should be moved to the parser context flags,
9620 * since they are not visible at the component level. I.e.
9621 * they are used if processing schema *documents* only.
9622 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009623 res = xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009624 HFAILURE;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009625
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009626 /*
9627 * Since the version is of type xs:token, we won't bother to
9628 * check it.
9629 */
9630 /* REMOVED:
9631 attr = xmlSchemaGetPropNode(node, "version");
9632 if (attr != NULL) {
9633 res = xmlSchemaPValAttrNode(ctxt, NULL, NULL, attr,
Daniel Veillarddee23482008-04-11 12:58:43 +00009634 xmlSchemaGetBuiltInType(XML_SCHEMAS_TOKEN), &val);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009635 HFAILURE;
9636 }
9637 */
9638 attr = xmlSchemaGetPropNode(node, "targetNamespace");
9639 if (attr != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009640 res = xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009641 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
9642 HFAILURE;
9643 if (res != 0) {
9644 ctxt->stop = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
9645 goto exit;
9646 }
9647 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009648 attr = xmlSchemaGetPropNode(node, "elementFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009649 if (attr != NULL) {
9650 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009651 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9652 XML_SCHEMAS_QUALIF_ELEM);
9653 HFAILURE;
9654 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009655 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009656 XML_SCHEMAP_ELEMFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009657 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009658 "(qualified | unqualified)", val, NULL, NULL, NULL);
9659 }
9660 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009661 attr = xmlSchemaGetPropNode(node, "attributeFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009662 if (attr != NULL) {
9663 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009664 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9665 XML_SCHEMAS_QUALIF_ATTR);
9666 HFAILURE;
9667 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009668 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009669 XML_SCHEMAP_ATTRFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009670 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009671 "(qualified | unqualified)", val, NULL, NULL, NULL);
9672 }
9673 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009674 attr = xmlSchemaGetPropNode(node, "finalDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009675 if (attr != NULL) {
9676 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009677 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009678 XML_SCHEMAS_FINAL_DEFAULT_EXTENSION,
9679 XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION,
9680 -1,
9681 XML_SCHEMAS_FINAL_DEFAULT_LIST,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009682 XML_SCHEMAS_FINAL_DEFAULT_UNION);
9683 HFAILURE;
9684 if (res != 0) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009685 xmlSchemaPSimpleTypeErr(ctxt,
9686 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009687 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009688 "(#all | List of (extension | restriction | list | union))",
9689 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009690 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009691 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009692 attr = xmlSchemaGetPropNode(node, "blockDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009693 if (attr != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +00009694 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009695 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009696 XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION,
9697 XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009698 XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION, -1, -1);
9699 HFAILURE;
9700 if (res != 0) {
9701 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009702 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009703 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009704 "(#all | List of (extension | restriction | substitution))",
9705 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009706 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009707 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009708
9709exit:
9710 if (oldErrs != ctxt->nberrors)
9711 res = ctxt->err;
9712 return(res);
9713exit_failure:
9714 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +00009715}
9716
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009717/**
9718 * xmlSchemaParseSchemaTopLevel:
9719 * @ctxt: a schema validation context
9720 * @schema: the schemas
9721 * @nodes: the list of top level nodes
9722 *
9723 * Returns the internal XML Schema structure built from the resource or
9724 * NULL in case of error
9725 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009726static int
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009727xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt,
9728 xmlSchemaPtr schema, xmlNodePtr nodes)
9729{
9730 xmlNodePtr child;
9731 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009732 int res = 0, oldErrs, tmpOldErrs;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009733
9734 if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009735 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009736
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009737 oldErrs = ctxt->nberrors;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009738 child = nodes;
9739 while ((IS_SCHEMA(child, "include")) ||
9740 (IS_SCHEMA(child, "import")) ||
9741 (IS_SCHEMA(child, "redefine")) ||
9742 (IS_SCHEMA(child, "annotation"))) {
9743 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009744 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009745 if (schema->annot == NULL)
9746 schema->annot = annot;
9747 else
9748 xmlSchemaFreeAnnot(annot);
9749 } else if (IS_SCHEMA(child, "import")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009750 tmpOldErrs = ctxt->nberrors;
9751 res = xmlSchemaParseImport(ctxt, schema, child);
9752 HFAILURE;
9753 HSTOP(ctxt);
9754 if (tmpOldErrs != ctxt->nberrors)
Daniel Veillarddee23482008-04-11 12:58:43 +00009755 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009756 } else if (IS_SCHEMA(child, "include")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009757 tmpOldErrs = ctxt->nberrors;
9758 res = xmlSchemaParseInclude(ctxt, schema, child);
9759 HFAILURE;
9760 HSTOP(ctxt);
9761 if (tmpOldErrs != ctxt->nberrors)
Daniel Veillarddee23482008-04-11 12:58:43 +00009762 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009763 } else if (IS_SCHEMA(child, "redefine")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009764 tmpOldErrs = ctxt->nberrors;
9765 res = xmlSchemaParseRedefine(ctxt, schema, child);
9766 HFAILURE;
9767 HSTOP(ctxt);
9768 if (tmpOldErrs != ctxt->nberrors)
9769 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009770 }
9771 child = child->next;
9772 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009773 /*
9774 * URGENT TODO: Change the functions to return int results.
9775 * We need especially to catch internal errors.
9776 */
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009777 while (child != NULL) {
9778 if (IS_SCHEMA(child, "complexType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009779 xmlSchemaParseComplexType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009780 child = child->next;
9781 } else if (IS_SCHEMA(child, "simpleType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009782 xmlSchemaParseSimpleType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009783 child = child->next;
9784 } else if (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009785 xmlSchemaParseElement(ctxt, schema, child, NULL, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009786 child = child->next;
9787 } else if (IS_SCHEMA(child, "attribute")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009788 xmlSchemaParseGlobalAttribute(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009789 child = child->next;
9790 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009791 xmlSchemaParseAttributeGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009792 child = child->next;
9793 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009794 xmlSchemaParseModelGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009795 child = child->next;
9796 } else if (IS_SCHEMA(child, "notation")) {
9797 xmlSchemaParseNotation(ctxt, schema, child);
9798 child = child->next;
9799 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009800 xmlSchemaPContentErr(ctxt,
9801 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009802 NULL, child->parent, child,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009803 NULL, "((include | import | redefine | annotation)*, "
9804 "(((simpleType | complexType | group | attributeGroup) "
9805 "| element | attribute | notation), annotation*)*)");
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009806 child = child->next;
9807 }
9808 while (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009809 /*
9810 * TODO: We should add all annotations.
9811 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009812 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009813 if (schema->annot == NULL)
9814 schema->annot = annot;
9815 else
9816 xmlSchemaFreeAnnot(annot);
9817 child = child->next;
9818 }
9819 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009820exit:
Daniel Veillard01fa6152004-06-29 17:04:39 +00009821 ctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009822 if (oldErrs != ctxt->nberrors)
9823 res = ctxt->err;
9824 return(res);
9825exit_failure:
9826 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009827}
9828
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009829static xmlSchemaSchemaRelationPtr
9830xmlSchemaSchemaRelationCreate(void)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009831{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009832 xmlSchemaSchemaRelationPtr ret;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009833
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009834 ret = (xmlSchemaSchemaRelationPtr)
9835 xmlMalloc(sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009836 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009837 xmlSchemaPErrMemory(NULL, "allocating schema relation", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009838 return(NULL);
9839 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009840 memset(ret, 0, sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009841 return(ret);
9842}
9843
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009844#if 0
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009845static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009846xmlSchemaSchemaRelationFree(xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009847{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009848 xmlFree(rel);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009849}
9850#endif
9851
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009852static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009853xmlSchemaRedefListFree(xmlSchemaRedefPtr redef)
9854{
9855 xmlSchemaRedefPtr prev;
9856
9857 while (redef != NULL) {
9858 prev = redef;
9859 redef = redef->next;
9860 xmlFree(prev);
9861 }
9862}
9863
9864static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009865xmlSchemaConstructionCtxtFree(xmlSchemaConstructionCtxtPtr con)
9866{
9867 /*
9868 * After the construction context has been freed, there will be
9869 * no schema graph available any more. Only the schema buckets
9870 * will stay alive, which are put into the "schemasImports" and
9871 * "includes" slots of the xmlSchema.
9872 */
9873 if (con->buckets != NULL)
9874 xmlSchemaItemListFree(con->buckets);
9875 if (con->pending != NULL)
9876 xmlSchemaItemListFree(con->pending);
9877 if (con->substGroups != NULL)
9878 xmlHashFree(con->substGroups,
9879 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009880 if (con->redefs != NULL)
9881 xmlSchemaRedefListFree(con->redefs);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009882 if (con->dict != NULL)
9883 xmlDictFree(con->dict);
9884 xmlFree(con);
9885}
9886
Daniel Veillarddee23482008-04-11 12:58:43 +00009887static xmlSchemaConstructionCtxtPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009888xmlSchemaConstructionCtxtCreate(xmlDictPtr dict)
9889{
9890 xmlSchemaConstructionCtxtPtr ret;
9891
9892 ret = (xmlSchemaConstructionCtxtPtr)
9893 xmlMalloc(sizeof(xmlSchemaConstructionCtxt));
9894 if (ret == NULL) {
9895 xmlSchemaPErrMemory(NULL,
9896 "allocating schema construction context", NULL);
9897 return (NULL);
9898 }
9899 memset(ret, 0, sizeof(xmlSchemaConstructionCtxt));
9900
9901 ret->buckets = xmlSchemaItemListCreate();
9902 if (ret->buckets == NULL) {
9903 xmlSchemaPErrMemory(NULL,
9904 "allocating list of schema buckets", NULL);
9905 xmlFree(ret);
9906 return (NULL);
9907 }
9908 ret->pending = xmlSchemaItemListCreate();
9909 if (ret->pending == NULL) {
9910 xmlSchemaPErrMemory(NULL,
9911 "allocating list of pending global components", NULL);
9912 xmlSchemaConstructionCtxtFree(ret);
9913 return (NULL);
9914 }
9915 ret->dict = dict;
9916 xmlDictReference(dict);
9917 return(ret);
9918}
9919
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009920static xmlSchemaParserCtxtPtr
9921xmlSchemaParserCtxtCreate(void)
9922{
9923 xmlSchemaParserCtxtPtr ret;
9924
9925 ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
9926 if (ret == NULL) {
9927 xmlSchemaPErrMemory(NULL, "allocating schema parser context",
9928 NULL);
9929 return (NULL);
9930 }
9931 memset(ret, 0, sizeof(xmlSchemaParserCtxt));
9932 ret->type = XML_SCHEMA_CTXT_PARSER;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009933 ret->attrProhibs = xmlSchemaItemListCreate();
9934 if (ret->attrProhibs == NULL) {
9935 xmlFree(ret);
9936 return(NULL);
9937 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009938 return(ret);
9939}
9940
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009941/**
9942 * xmlSchemaNewParserCtxtUseDict:
9943 * @URL: the location of the schema
9944 * @dict: the dictionary to be used
9945 *
9946 * Create an XML Schemas parse context for that file/resource expected
9947 * to contain an XML Schemas file.
9948 *
9949 * Returns the parser context or NULL in case of error
9950 */
9951static xmlSchemaParserCtxtPtr
9952xmlSchemaNewParserCtxtUseDict(const char *URL, xmlDictPtr dict)
9953{
9954 xmlSchemaParserCtxtPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009955
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009956 ret = xmlSchemaParserCtxtCreate();
Daniel Veillarddee23482008-04-11 12:58:43 +00009957 if (ret == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009958 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009959 ret->dict = dict;
Daniel Veillarddee23482008-04-11 12:58:43 +00009960 xmlDictReference(dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009961 if (URL != NULL)
9962 ret->URL = xmlDictLookup(dict, (const xmlChar *) URL, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009963 return (ret);
9964}
9965
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009966static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009967xmlSchemaCreatePCtxtOnVCtxt(xmlSchemaValidCtxtPtr vctxt)
9968{
9969 if (vctxt->pctxt == NULL) {
9970 if (vctxt->schema != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009971 vctxt->pctxt =
9972 xmlSchemaNewParserCtxtUseDict("*", vctxt->schema->dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009973 else
9974 vctxt->pctxt = xmlSchemaNewParserCtxt("*");
9975 if (vctxt->pctxt == NULL) {
9976 VERROR_INT("xmlSchemaCreatePCtxtOnVCtxt",
9977 "failed to create a temp. parser context");
9978 return (-1);
9979 }
9980 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009981 xmlSchemaSetParserErrors(vctxt->pctxt, vctxt->error,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00009982 vctxt->warning, vctxt->errCtxt);
9983 xmlSchemaSetParserStructuredErrors(vctxt->pctxt, vctxt->serror,
9984 vctxt->errCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009985 }
9986 return (0);
9987}
9988
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009989/**
9990 * xmlSchemaGetSchemaBucket:
9991 * @pctxt: the schema parser context
9992 * @schemaLocation: the URI of the schema document
9993 *
9994 * Returns a schema bucket if it was already parsed.
9995 *
9996 * Returns a schema bucket if it was already parsed from
9997 * @schemaLocation, NULL otherwise.
9998 */
9999static xmlSchemaBucketPtr
10000xmlSchemaGetSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
10001 const xmlChar *schemaLocation)
10002{
10003 xmlSchemaBucketPtr cur;
10004 xmlSchemaItemListPtr list;
10005
10006 list = pctxt->constructor->buckets;
10007 if (list->nbItems == 0)
10008 return(NULL);
10009 else {
10010 int i;
10011 for (i = 0; i < list->nbItems; i++) {
10012 cur = (xmlSchemaBucketPtr) list->items[i];
10013 /* Pointer comparison! */
10014 if (cur->schemaLocation == schemaLocation)
10015 return(cur);
10016 }
10017 }
10018 return(NULL);
10019}
10020
10021static xmlSchemaBucketPtr
10022xmlSchemaGetChameleonSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
10023 const xmlChar *schemaLocation,
10024 const xmlChar *targetNamespace)
10025{
10026 xmlSchemaBucketPtr cur;
10027 xmlSchemaItemListPtr list;
10028
10029 list = pctxt->constructor->buckets;
10030 if (list->nbItems == 0)
10031 return(NULL);
10032 else {
10033 int i;
10034 for (i = 0; i < list->nbItems; i++) {
10035 cur = (xmlSchemaBucketPtr) list->items[i];
10036 /* Pointer comparison! */
10037 if ((cur->origTargetNamespace == NULL) &&
10038 (cur->schemaLocation == schemaLocation) &&
10039 (cur->targetNamespace == targetNamespace))
10040 return(cur);
10041 }
10042 }
10043 return(NULL);
10044}
10045
10046
10047#define IS_BAD_SCHEMA_DOC(b) \
10048 (((b)->doc == NULL) && ((b)->schemaLocation != NULL))
10049
10050static xmlSchemaBucketPtr
10051xmlSchemaGetSchemaBucketByTNS(xmlSchemaParserCtxtPtr pctxt,
10052 const xmlChar *targetNamespace,
10053 int imported)
10054{
10055 xmlSchemaBucketPtr cur;
10056 xmlSchemaItemListPtr list;
10057
10058 list = pctxt->constructor->buckets;
10059 if (list->nbItems == 0)
10060 return(NULL);
10061 else {
10062 int i;
10063 for (i = 0; i < list->nbItems; i++) {
10064 cur = (xmlSchemaBucketPtr) list->items[i];
10065 if ((! IS_BAD_SCHEMA_DOC(cur)) &&
10066 (cur->origTargetNamespace == targetNamespace) &&
10067 ((imported && cur->imported) ||
10068 ((!imported) && (!cur->imported))))
10069 return(cur);
10070 }
10071 }
10072 return(NULL);
10073}
10074
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010075static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010076xmlSchemaParseNewDocWithContext(xmlSchemaParserCtxtPtr pctxt,
10077 xmlSchemaPtr schema,
10078 xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010079{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010080 int oldFlags;
10081 xmlDocPtr oldDoc;
10082 xmlNodePtr node;
10083 int ret, oldErrs;
10084 xmlSchemaBucketPtr oldbucket = pctxt->constructor->bucket;
Daniel Veillarddee23482008-04-11 12:58:43 +000010085
10086 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010087 * Save old values; reset the *main* schema.
10088 * URGENT TODO: This is not good; move the per-document information
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000010089 * to the parser. Get rid of passing the main schema to the
10090 * parsing functions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010091 */
10092 oldFlags = schema->flags;
10093 oldDoc = schema->doc;
10094 if (schema->flags != 0)
10095 xmlSchemaClearSchemaDefaults(schema);
Daniel Veillarddee23482008-04-11 12:58:43 +000010096 schema->doc = bucket->doc;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010097 pctxt->schema = schema;
Daniel Veillarddee23482008-04-11 12:58:43 +000010098 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010099 * Keep the current target namespace on the parser *not* on the
10100 * main schema.
10101 */
10102 pctxt->targetNamespace = bucket->targetNamespace;
10103 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010104
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010105 if ((bucket->targetNamespace != NULL) &&
10106 xmlStrEqual(bucket->targetNamespace, xmlSchemaNs)) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010107 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010108 * We are parsing the schema for schemas!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010109 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010110 pctxt->isS4S = 1;
Daniel Veillarddee23482008-04-11 12:58:43 +000010111 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010112 /* Mark it as parsed, even if parsing fails. */
10113 bucket->parsed++;
10114 /* Compile the schema doc. */
10115 node = xmlDocGetRootElement(bucket->doc);
10116 ret = xmlSchemaParseSchemaElement(pctxt, schema, node);
10117 if (ret != 0)
10118 goto exit;
10119 /* An empty schema; just get out. */
10120 if (node->children == NULL)
10121 goto exit;
10122 oldErrs = pctxt->nberrors;
10123 ret = xmlSchemaParseSchemaTopLevel(pctxt, schema, node->children);
10124 if (ret != 0)
10125 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010126 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010127 * TODO: Not nice, but I'm not 100% sure we will get always an error
10128 * as a result of the obove functions; so better rely on pctxt->err
10129 * as well.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010130 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010131 if ((ret == 0) && (oldErrs != pctxt->nberrors)) {
10132 ret = pctxt->err;
10133 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010134 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010135
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010136exit:
10137 WXS_CONSTRUCTOR(pctxt)->bucket = oldbucket;
10138 /* Restore schema values. */
10139 schema->doc = oldDoc;
10140 schema->flags = oldFlags;
10141 return(ret);
10142}
10143
10144static int
10145xmlSchemaParseNewDoc(xmlSchemaParserCtxtPtr pctxt,
10146 xmlSchemaPtr schema,
10147 xmlSchemaBucketPtr bucket)
10148{
10149 xmlSchemaParserCtxtPtr newpctxt;
10150 int res = 0;
10151
10152 if (bucket == NULL)
10153 return(0);
10154 if (bucket->parsed) {
10155 PERROR_INT("xmlSchemaParseNewDoc",
10156 "reparsing a schema doc");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010157 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010158 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010159 if (bucket->doc == NULL) {
10160 PERROR_INT("xmlSchemaParseNewDoc",
10161 "parsing a schema doc, but there's no doc");
10162 return(-1);
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +000010163 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010164 if (pctxt->constructor == NULL) {
10165 PERROR_INT("xmlSchemaParseNewDoc",
10166 "no constructor");
10167 return(-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000010168 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010169 /* Create and init the temporary parser context. */
10170 newpctxt = xmlSchemaNewParserCtxtUseDict(
10171 (const char *) bucket->schemaLocation, pctxt->dict);
10172 if (newpctxt == NULL)
10173 return(-1);
10174 newpctxt->constructor = pctxt->constructor;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010175 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000010176 * TODO: Can we avoid that the parser knows about the main schema?
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010177 * It would be better if he knows about the current schema bucket
10178 * only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010179 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010180 newpctxt->schema = schema;
10181 xmlSchemaSetParserErrors(newpctxt, pctxt->error, pctxt->warning,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000010182 pctxt->errCtxt);
10183 xmlSchemaSetParserStructuredErrors(newpctxt, pctxt->serror,
10184 pctxt->errCtxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010185 newpctxt->counter = pctxt->counter;
Daniel Veillarddee23482008-04-11 12:58:43 +000010186
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010187
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010188 res = xmlSchemaParseNewDocWithContext(newpctxt, schema, bucket);
Daniel Veillarddee23482008-04-11 12:58:43 +000010189
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010190 /* Channel back errors and cleanup the temporary parser context. */
10191 if (res != 0)
10192 pctxt->err = res;
10193 pctxt->nberrors += newpctxt->nberrors;
10194 pctxt->counter = newpctxt->counter;
Daniel Veillarddee23482008-04-11 12:58:43 +000010195 newpctxt->constructor = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010196 /* Free the parser context. */
10197 xmlSchemaFreeParserCtxt(newpctxt);
10198 return(res);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010199}
William M. Brack2f2a6632004-08-20 23:09:47 +000010200
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010201static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010202xmlSchemaSchemaRelationAddChild(xmlSchemaBucketPtr bucket,
10203 xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010204{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010205 xmlSchemaSchemaRelationPtr cur = bucket->relations;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010206
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010207 if (cur == NULL) {
10208 bucket->relations = rel;
10209 return;
10210 }
10211 while (cur->next != NULL)
10212 cur = cur->next;
10213 cur->next = rel;
10214}
10215
10216
10217static const xmlChar *
10218xmlSchemaBuildAbsoluteURI(xmlDictPtr dict, const xmlChar* location,
10219 xmlNodePtr ctxtNode)
Daniel Veillarddee23482008-04-11 12:58:43 +000010220{
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010221 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010222 * Build an absolue location URI.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010223 */
Daniel Veillarddee23482008-04-11 12:58:43 +000010224 if (location != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010225 if (ctxtNode == NULL)
10226 return(location);
10227 else {
10228 xmlChar *base, *URI;
10229 const xmlChar *ret = NULL;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010230
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010231 base = xmlNodeGetBase(ctxtNode->doc, ctxtNode);
10232 if (base == NULL) {
10233 URI = xmlBuildURI(location, ctxtNode->doc->URL);
10234 } else {
10235 URI = xmlBuildURI(location, base);
10236 xmlFree(base);
10237 }
10238 if (URI != NULL) {
10239 ret = xmlDictLookup(dict, URI, -1);
10240 xmlFree(URI);
10241 return(ret);
10242 }
10243 }
10244 }
10245 return(NULL);
10246}
Daniel Veillarddee23482008-04-11 12:58:43 +000010247
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010248
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010249
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010250/**
10251 * xmlSchemaAddSchemaDoc:
10252 * @pctxt: a schema validation context
10253 * @schema: the schema being built
10254 * @node: a subtree containing XML Schema informations
10255 *
10256 * Parse an included (and to-be-redefined) XML schema document.
10257 *
10258 * Returns 0 on success, a positive error code on errors and
10259 * -1 in case of an internal or API error.
10260 */
10261
10262static int
10263xmlSchemaAddSchemaDoc(xmlSchemaParserCtxtPtr pctxt,
10264 int type, /* import or include or redefine */
10265 const xmlChar *schemaLocation,
10266 xmlDocPtr schemaDoc,
10267 const char *schemaBuffer,
10268 int schemaBufferLen,
10269 xmlNodePtr invokingNode,
Daniel Veillarddee23482008-04-11 12:58:43 +000010270 const xmlChar *sourceTargetNamespace,
10271 const xmlChar *importNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010272 xmlSchemaBucketPtr *bucket)
10273{
10274 const xmlChar *targetNamespace = NULL;
10275 xmlSchemaSchemaRelationPtr relation = NULL;
10276 xmlDocPtr doc = NULL;
10277 int res = 0, err = 0, located = 0, preserveDoc = 0;
10278 xmlSchemaBucketPtr bkt = NULL;
10279
10280 if (bucket != NULL)
10281 *bucket = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000010282
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010283 switch (type) {
10284 case XML_SCHEMA_SCHEMA_IMPORT:
10285 case XML_SCHEMA_SCHEMA_MAIN:
10286 err = XML_SCHEMAP_SRC_IMPORT;
10287 break;
10288 case XML_SCHEMA_SCHEMA_INCLUDE:
10289 err = XML_SCHEMAP_SRC_INCLUDE;
10290 break;
10291 case XML_SCHEMA_SCHEMA_REDEFINE:
10292 err = XML_SCHEMAP_SRC_REDEFINE;
10293 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000010294 }
10295
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010296
10297 /* Special handling for the main schema:
10298 * skip the location and relation logic and just parse the doc.
10299 * We need just a bucket to be returned in this case.
Daniel Veillarddee23482008-04-11 12:58:43 +000010300 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010301 if ((type == XML_SCHEMA_SCHEMA_MAIN) || (! WXS_HAS_BUCKETS(pctxt)))
Daniel Veillarddee23482008-04-11 12:58:43 +000010302 goto doc_load;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010303
Daniel Veillarddee23482008-04-11 12:58:43 +000010304 /* Note that we expect the location to be an absulute URI. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010305 if (schemaLocation != NULL) {
10306 bkt = xmlSchemaGetSchemaBucket(pctxt, schemaLocation);
10307 if ((bkt != NULL) &&
10308 (pctxt->constructor->bucket == bkt)) {
10309 /* Report self-imports/inclusions/redefinitions. */
Daniel Veillarddee23482008-04-11 12:58:43 +000010310
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010311 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10312 invokingNode, NULL,
10313 "The schema must not import/include/redefine itself",
10314 NULL, NULL);
10315 goto exit;
10316 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000010317 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010318 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010319 * Create a relation for the graph of schemas.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010320 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010321 relation = xmlSchemaSchemaRelationCreate();
10322 if (relation == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000010323 return(-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010324 xmlSchemaSchemaRelationAddChild(pctxt->constructor->bucket,
10325 relation);
10326 relation->type = type;
10327
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010328 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010329 * Save the namespace import information.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010330 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010331 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000010332 relation->importNamespace = importNamespace;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010333 if (schemaLocation == NULL) {
10334 /*
10335 * No location; this is just an import of the namespace.
10336 * Note that we don't assign a bucket to the relation
10337 * in this case.
10338 */
10339 goto exit;
10340 }
10341 targetNamespace = importNamespace;
10342 }
10343
10344 /* Did we already fetch the doc? */
Daniel Veillarddee23482008-04-11 12:58:43 +000010345 if (bkt != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010346 if ((WXS_IS_BUCKET_IMPMAIN(type)) && (! bkt->imported)) {
Jason Childsd9960722009-08-07 19:01:32 +020010347 /*
10348 * We included/redefined and then try to import a schema,
10349 * but the new location provided for import was different.
10350 */
Daniel Veillard734e7662007-06-26 11:30:31 +000010351 if (schemaLocation == NULL)
10352 schemaLocation = BAD_CAST "in_memory_buffer";
Jason Childsd9960722009-08-07 19:01:32 +020010353 if (!xmlStrEqual(schemaLocation,
10354 bkt->schemaLocation)) {
10355 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10356 invokingNode, NULL,
10357 "The schema document '%s' cannot be imported, since "
10358 "it was already included or redefined",
10359 schemaLocation, NULL);
10360 goto exit;
10361 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010362 } else if ((! WXS_IS_BUCKET_IMPMAIN(type)) && (bkt->imported)) {
Jason Childsd9960722009-08-07 19:01:32 +020010363 /*
10364 * We imported and then try to include/redefine a schema,
10365 * but the new location provided for the include/redefine
10366 * was different.
10367 */
Daniel Veillard734e7662007-06-26 11:30:31 +000010368 if (schemaLocation == NULL)
10369 schemaLocation = BAD_CAST "in_memory_buffer";
Jason Childsd9960722009-08-07 19:01:32 +020010370 if (!xmlStrEqual(schemaLocation,
10371 bkt->schemaLocation)) {
10372 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10373 invokingNode, NULL,
10374 "The schema document '%s' cannot be included or "
10375 "redefined, since it was already imported",
10376 schemaLocation, NULL);
10377 goto exit;
10378 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010379 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010380 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010381
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010382 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010383 /*
10384 * Given that the schemaLocation [attribute] is only a hint, it is open
10385 * to applications to ignore all but the first <import> for a given
Rob Richardsc6947bb2008-06-29 15:04:41 +000010386 * namespace, regardless of the �actual value� of schemaLocation, but
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010387 * such a strategy risks missing useful information when new
10388 * schemaLocations are offered.
10389 *
10390 * We will use the first <import> that comes with a location.
10391 * Further <import>s *with* a location, will result in an error.
10392 * TODO: Better would be to just report a warning here, but
10393 * we'll try it this way until someone complains.
10394 *
10395 * Schema Document Location Strategy:
10396 * 3 Based on the namespace name, identify an existing schema document,
10397 * either as a resource which is an XML document or a <schema> element
10398 * information item, in some local schema repository;
10399 * 5 Attempt to resolve the namespace name to locate such a resource.
10400 *
10401 * NOTE: (3) and (5) are not supported.
Daniel Veillarddee23482008-04-11 12:58:43 +000010402 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010403 if (bkt != NULL) {
10404 relation->bucket = bkt;
10405 goto exit;
10406 }
10407 bkt = xmlSchemaGetSchemaBucketByTNS(pctxt,
10408 importNamespace, 1);
10409
Daniel Veillarddee23482008-04-11 12:58:43 +000010410 if (bkt != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010411 relation->bucket = bkt;
10412 if (bkt->schemaLocation == NULL) {
10413 /* First given location of the schema; load the doc. */
10414 bkt->schemaLocation = schemaLocation;
10415 } else {
10416 if (!xmlStrEqual(schemaLocation,
10417 bkt->schemaLocation)) {
10418 /*
10419 * Additional location given; just skip it.
10420 * URGENT TODO: We should report a warning here.
10421 * res = XML_SCHEMAP_SRC_IMPORT;
10422 */
Daniel Veillard734e7662007-06-26 11:30:31 +000010423 if (schemaLocation == NULL)
10424 schemaLocation = BAD_CAST "in_memory_buffer";
10425
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010426 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10427 XML_SCHEMAP_WARN_SKIP_SCHEMA,
10428 invokingNode, NULL,
10429 "Skipping import of schema located at '%s' for the "
10430 "namespace '%s', since this namespace was already "
10431 "imported with the schema located at '%s'",
10432 schemaLocation, importNamespace, bkt->schemaLocation);
10433 }
10434 goto exit;
10435 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010436 }
10437 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010438 * No bucket + first location: load the doc and create a
10439 * bucket.
10440 */
10441 } else {
10442 /* <include> and <redefine> */
10443 if (bkt != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000010444
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010445 if ((bkt->origTargetNamespace == NULL) &&
10446 (bkt->targetNamespace != sourceTargetNamespace)) {
10447 xmlSchemaBucketPtr chamel;
Daniel Veillarddee23482008-04-11 12:58:43 +000010448
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010449 /*
10450 * Chameleon include/redefine: skip loading only if it was
10451 * aleady build for the targetNamespace of the including
10452 * schema.
10453 */
10454 /*
10455 * URGENT TODO: If the schema is a chameleon-include then copy
10456 * the components into the including schema and modify the
10457 * targetNamespace of those components, do nothing otherwise.
10458 * NOTE: This is currently worked-around by compiling the
10459 * chameleon for every destinct including targetNamespace; thus
10460 * not performant at the moment.
10461 * TODO: Check when the namespace in wildcards for chameleons
10462 * needs to be converted: before we built wildcard intersections
10463 * or after.
10464 * Answer: after!
10465 */
10466 chamel = xmlSchemaGetChameleonSchemaBucket(pctxt,
10467 schemaLocation, sourceTargetNamespace);
10468 if (chamel != NULL) {
10469 /* A fitting chameleon was already parsed; NOP. */
10470 relation->bucket = chamel;
10471 goto exit;
10472 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010473 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010474 * We need to parse the chameleon again for a different
10475 * targetNamespace.
10476 * CHAMELEON TODO: Optimize this by only parsing the
10477 * chameleon once, and then copying the components to
10478 * the new targetNamespace.
10479 */
10480 bkt = NULL;
10481 } else {
10482 relation->bucket = bkt;
10483 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000010484 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010485 }
10486 }
10487 if ((bkt != NULL) && (bkt->doc != NULL)) {
10488 PERROR_INT("xmlSchemaAddSchemaDoc",
10489 "trying to load a schema doc, but a doc is already "
10490 "assigned to the schema bucket");
10491 goto exit_failure;
10492 }
10493
10494doc_load:
10495 /*
10496 * Load the document.
10497 */
10498 if (schemaDoc != NULL) {
10499 doc = schemaDoc;
10500 /* Don' free this one, since it was provided by the caller. */
10501 preserveDoc = 1;
10502 /* TODO: Does the context or the doc hold the location? */
10503 if (schemaDoc->URL != NULL)
10504 schemaLocation = xmlDictLookup(pctxt->dict,
10505 schemaDoc->URL, -1);
Daniel Veillard734e7662007-06-26 11:30:31 +000010506 else
10507 schemaLocation = BAD_CAST "in_memory_buffer";
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010508 } else if ((schemaLocation != NULL) || (schemaBuffer != NULL)) {
10509 xmlParserCtxtPtr parserCtxt;
10510
10511 parserCtxt = xmlNewParserCtxt();
10512 if (parserCtxt == NULL) {
10513 xmlSchemaPErrMemory(NULL, "xmlSchemaGetDoc, "
10514 "allocating a parser context", NULL);
10515 goto exit_failure;
10516 }
10517 if ((pctxt->dict != NULL) && (parserCtxt->dict != NULL)) {
10518 /*
10519 * TODO: Do we have to burden the schema parser dict with all
10520 * the content of the schema doc?
10521 */
10522 xmlDictFree(parserCtxt->dict);
10523 parserCtxt->dict = pctxt->dict;
10524 xmlDictReference(parserCtxt->dict);
10525 }
10526 if (schemaLocation != NULL) {
10527 /* Parse from file. */
10528 doc = xmlCtxtReadFile(parserCtxt, (const char *) schemaLocation,
10529 NULL, SCHEMAS_PARSE_OPTIONS);
10530 } else if (schemaBuffer != NULL) {
10531 /* Parse from memory buffer. */
10532 doc = xmlCtxtReadMemory(parserCtxt, schemaBuffer, schemaBufferLen,
10533 NULL, NULL, SCHEMAS_PARSE_OPTIONS);
10534 schemaLocation = xmlStrdup(BAD_CAST "in_memory_buffer");
10535 if (doc != NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000010536 doc->URL = schemaLocation;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010537 }
10538 /*
10539 * For <import>:
10540 * 2.1 The referent is (a fragment of) a resource which is an
10541 * XML document (see clause 1.1), which in turn corresponds to
10542 * a <schema> element information item in a well-formed information
10543 * set, which in turn corresponds to a valid schema.
10544 * TODO: (2.1) fragments of XML documents are not supported.
10545 *
10546 * 2.2 The referent is a <schema> element information item in
10547 * a well-formed information set, which in turn corresponds
10548 * to a valid schema.
10549 * TODO: (2.2) is not supported.
10550 */
10551 if (doc == NULL) {
10552 xmlErrorPtr lerr;
10553 lerr = xmlGetLastError();
10554 /*
10555 * Check if this a parser error, or if the document could
10556 * just not be located.
10557 * TODO: Try to find specific error codes to react only on
10558 * localisation failures.
10559 */
10560 if ((lerr == NULL) || (lerr->domain != XML_FROM_IO)) {
10561 /*
10562 * We assume a parser error here.
10563 */
10564 located = 1;
10565 /* TODO: Error code ?? */
10566 res = XML_SCHEMAP_SRC_IMPORT_2_1;
10567 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10568 invokingNode, NULL,
10569 "Failed to parse the XML resource '%s'",
Daniel Veillard734e7662007-06-26 11:30:31 +000010570 schemaLocation, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010571 }
10572 }
10573 xmlFreeParserCtxt(parserCtxt);
10574 if ((doc == NULL) && located)
10575 goto exit_error;
10576 } else {
10577 xmlSchemaPErr(pctxt, NULL,
10578 XML_SCHEMAP_NOTHING_TO_PARSE,
10579 "No information for parsing was provided with the "
10580 "given schema parser context.\n",
10581 NULL, NULL);
10582 goto exit_failure;
10583 }
10584 /*
10585 * Preprocess the document.
10586 */
10587 if (doc != NULL) {
10588 xmlNodePtr docElem = NULL;
10589
Daniel Veillarddee23482008-04-11 12:58:43 +000010590 located = 1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010591 docElem = xmlDocGetRootElement(doc);
10592 if (docElem == NULL) {
10593 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOROOT,
Daniel Veillarddee23482008-04-11 12:58:43 +000010594 invokingNode, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010595 "The document '%s' has no document element",
10596 schemaLocation, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010597 goto exit_error;
10598 }
10599 /*
10600 * Remove all the blank text nodes.
10601 */
10602 xmlSchemaCleanupDoc(pctxt, docElem);
10603 /*
10604 * Check the schema's top level element.
10605 */
10606 if (!IS_SCHEMA(docElem, "schema")) {
10607 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOT_SCHEMA,
10608 invokingNode, NULL,
10609 "The XML document '%s' is not a schema document",
10610 schemaLocation, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010611 goto exit_error;
10612 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010613 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010614 * Note that we don't apply a type check for the
10615 * targetNamespace value here.
10616 */
10617 targetNamespace = xmlSchemaGetProp(pctxt, docElem,
10618 "targetNamespace");
10619 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010620
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010621/* after_doc_loading: */
10622 if ((bkt == NULL) && located) {
10623 /* Only create a bucket if the schema was located. */
10624 bkt = xmlSchemaBucketCreate(pctxt, type,
10625 targetNamespace);
10626 if (bkt == NULL)
10627 goto exit_failure;
10628 }
10629 if (bkt != NULL) {
10630 bkt->schemaLocation = schemaLocation;
10631 bkt->located = located;
10632 if (doc != NULL) {
10633 bkt->doc = doc;
10634 bkt->targetNamespace = targetNamespace;
10635 bkt->origTargetNamespace = targetNamespace;
10636 if (preserveDoc)
10637 bkt->preserveDoc = 1;
10638 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010639 if (WXS_IS_BUCKET_IMPMAIN(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010640 bkt->imported++;
10641 /*
10642 * Add it to the graph of schemas.
10643 */
10644 if (relation != NULL)
10645 relation->bucket = bkt;
10646 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010647
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010648exit:
10649 /*
10650 * Return the bucket explicitely; this is needed for the
10651 * main schema.
10652 */
10653 if (bucket != NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000010654 *bucket = bkt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010655 return (0);
10656
10657exit_error:
10658 if ((doc != NULL) && (! preserveDoc)) {
10659 xmlFreeDoc(doc);
10660 if (bkt != NULL)
10661 bkt->doc = NULL;
10662 }
10663 return(pctxt->err);
10664
10665exit_failure:
10666 if ((doc != NULL) && (! preserveDoc)) {
10667 xmlFreeDoc(doc);
10668 if (bkt != NULL)
10669 bkt->doc = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000010670 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010671 return (-1);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010672}
10673
William M. Brack2f2a6632004-08-20 23:09:47 +000010674/**
10675 * xmlSchemaParseImport:
10676 * @ctxt: a schema validation context
10677 * @schema: the schema being built
10678 * @node: a subtree containing XML Schema informations
10679 *
10680 * parse a XML schema Import definition
10681 * *WARNING* this interface is highly subject to change
10682 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010683 * Returns 0 in case of success, a positive error code if
10684 * not valid and -1 in case of an internal error.
William M. Brack2f2a6632004-08-20 23:09:47 +000010685 */
10686static int
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010687xmlSchemaParseImport(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
William M. Brack2f2a6632004-08-20 23:09:47 +000010688 xmlNodePtr node)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010689{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010690 xmlNodePtr child;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010691 const xmlChar *namespaceName = NULL, *schemaLocation = NULL;
10692 const xmlChar *thisTargetNamespace;
William M. Brack2f2a6632004-08-20 23:09:47 +000010693 xmlAttrPtr attr;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010694 int ret = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010695 xmlSchemaBucketPtr bucket = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000010696
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010697 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
William M. Brack2f2a6632004-08-20 23:09:47 +000010698 return (-1);
10699
10700 /*
10701 * Check for illegal attributes.
10702 */
10703 attr = node->properties;
10704 while (attr != NULL) {
10705 if (attr->ns == NULL) {
10706 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10707 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
10708 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010709 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010710 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010711 }
10712 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010713 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010714 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010715 }
10716 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010717 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010718 /*
10719 * Extract and validate attributes.
10720 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010721 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010722 "namespace", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010723 &namespaceName) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010724 xmlSchemaPSimpleTypeErr(pctxt,
10725 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010726 NULL, node,
10727 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010728 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010729 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010730 }
10731
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010732 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010733 "schemaLocation", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
William M. Brack2f2a6632004-08-20 23:09:47 +000010734 &schemaLocation) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010735 xmlSchemaPSimpleTypeErr(pctxt,
10736 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010737 NULL, node,
10738 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010739 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010740 return (pctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010741 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010742 /*
10743 * And now for the children...
10744 */
10745 child = node->children;
10746 if (IS_SCHEMA(child, "annotation")) {
10747 /*
10748 * the annotation here is simply discarded ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010749 * TODO: really?
William M. Brack2f2a6632004-08-20 23:09:47 +000010750 */
10751 child = child->next;
10752 }
10753 if (child != NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010754 xmlSchemaPContentErr(pctxt,
10755 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010756 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000010757 "(annotation?)");
10758 }
10759 /*
10760 * Apply additional constraints.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010761 *
10762 * Note that it is important to use the original @targetNamespace
10763 * (or none at all), to rule out imports of schemas _with_ a
10764 * @targetNamespace if the importing schema is a chameleon schema
10765 * (with no @targetNamespace).
William M. Brack2f2a6632004-08-20 23:09:47 +000010766 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010767 thisTargetNamespace = WXS_BUCKET(pctxt)->origTargetNamespace;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010768 if (namespaceName != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000010769 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000010770 * 1.1 If the namespace [attribute] is present, then its �actual value�
10771 * must not match the �actual value� of the enclosing <schema>'s
William M. Brack2f2a6632004-08-20 23:09:47 +000010772 * targetNamespace [attribute].
10773 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010774 if (xmlStrEqual(thisTargetNamespace, namespaceName)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010775 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010776 XML_SCHEMAP_SRC_IMPORT_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010777 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010778 "The value of the attribute 'namespace' must not match "
10779 "the target namespace '%s' of the importing schema",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010780 thisTargetNamespace);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010781 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010782 }
10783 } else {
10784 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010785 * 1.2 If the namespace [attribute] is not present, then the enclosing
William M. Brack2f2a6632004-08-20 23:09:47 +000010786 * <schema> must have a targetNamespace [attribute].
10787 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010788 if (thisTargetNamespace == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010789 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010790 XML_SCHEMAP_SRC_IMPORT_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010791 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010792 "The attribute 'namespace' must be existent if "
10793 "the importing schema has no target namespace",
10794 NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010795 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010796 }
10797 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010798 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010799 * Locate and acquire the schema document.
William M. Brack2f2a6632004-08-20 23:09:47 +000010800 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010801 if (schemaLocation != NULL)
10802 schemaLocation = xmlSchemaBuildAbsoluteURI(pctxt->dict,
10803 schemaLocation, node);
10804 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010805 schemaLocation, NULL, NULL, 0, node, thisTargetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010806 namespaceName, &bucket);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010807
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010808 if (ret != 0)
10809 return(ret);
10810
10811 /*
10812 * For <import>: "It is *not* an error for the application
10813 * schema reference strategy to fail."
10814 * So just don't parse if no schema document was found.
10815 * Note that we will get no bucket if the schema could not be
10816 * located or if there was no schemaLocation.
10817 */
10818 if ((bucket == NULL) && (schemaLocation != NULL)) {
10819 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10820 XML_SCHEMAP_WARN_UNLOCATED_SCHEMA,
10821 node, NULL,
10822 "Failed to locate a schema at location '%s'. "
10823 "Skipping the import", schemaLocation, NULL, NULL);
10824 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010825
10826 if ((bucket != NULL) && CAN_PARSE_SCHEMA(bucket)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010827 ret = xmlSchemaParseNewDoc(pctxt, schema, bucket);
10828 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010829
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010830 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000010831}
10832
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010833static int
10834xmlSchemaParseIncludeOrRedefineAttrs(xmlSchemaParserCtxtPtr pctxt,
10835 xmlSchemaPtr schema,
10836 xmlNodePtr node,
10837 xmlChar **schemaLocation,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010838 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010839{
10840 xmlAttrPtr attr;
10841
10842 if ((pctxt == NULL) || (schema == NULL) || (node == NULL) ||
10843 (schemaLocation == NULL))
10844 return (-1);
10845
10846 *schemaLocation = NULL;
10847 /*
10848 * Check for illegal attributes.
10849 * Applies for both <include> and <redefine>.
10850 */
10851 attr = node->properties;
10852 while (attr != NULL) {
10853 if (attr->ns == NULL) {
10854 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10855 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
10856 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010857 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010858 }
10859 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
10860 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010861 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010862 }
10863 attr = attr->next;
10864 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010865 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010866 /*
10867 * Preliminary step, extract the URI-Reference and make an URI
10868 * from the base.
10869 */
10870 /*
10871 * Attribute "schemaLocation" is mandatory.
10872 */
10873 attr = xmlSchemaGetPropNode(node, "schemaLocation");
10874 if (attr != NULL) {
10875 xmlChar *base = NULL;
10876 xmlChar *uri = NULL;
10877
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010878 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010879 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
10880 (const xmlChar **) schemaLocation) != 0)
10881 goto exit_error;
10882 base = xmlNodeGetBase(node->doc, node);
10883 if (base == NULL) {
10884 uri = xmlBuildURI(*schemaLocation, node->doc->URL);
10885 } else {
10886 uri = xmlBuildURI(*schemaLocation, base);
10887 xmlFree(base);
10888 }
10889 if (uri == NULL) {
10890 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
10891 "could not build an URI from the schemaLocation")
10892 goto exit_failure;
10893 }
10894 (*schemaLocation) = (xmlChar *) xmlDictLookup(pctxt->dict, uri, -1);
10895 xmlFree(uri);
10896 } else {
10897 xmlSchemaPMissingAttrErr(pctxt,
10898 XML_SCHEMAP_S4S_ATTR_MISSING,
10899 NULL, node, "schemaLocation", NULL);
10900 goto exit_error;
10901 }
10902 /*
10903 * Report self-inclusion and self-redefinition.
10904 */
10905 if (xmlStrEqual(*schemaLocation, pctxt->URL)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010906 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010907 xmlSchemaPCustomErr(pctxt,
10908 XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010909 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010910 "The schema document '%s' cannot redefine itself.",
Daniel Veillarddee23482008-04-11 12:58:43 +000010911 *schemaLocation);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010912 } else {
10913 xmlSchemaPCustomErr(pctxt,
10914 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010915 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010916 "The schema document '%s' cannot include itself.",
10917 *schemaLocation);
10918 }
10919 goto exit_error;
10920 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010921
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010922 return(0);
10923exit_error:
10924 return(pctxt->err);
10925exit_failure:
10926 return(-1);
10927}
10928
10929static int
10930xmlSchemaParseIncludeOrRedefine(xmlSchemaParserCtxtPtr pctxt,
10931 xmlSchemaPtr schema,
10932 xmlNodePtr node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010933 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010934{
10935 xmlNodePtr child = NULL;
10936 const xmlChar *schemaLocation = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010937 int res = 0; /* hasRedefinitions = 0 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010938 int isChameleon = 0, wasChameleon = 0;
10939 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010940
10941 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
10942 return (-1);
10943
10944 /*
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010945 * Parse attributes. Note that the returned schemaLocation will
10946 * be already converted to an absolute URI.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010947 */
10948 res = xmlSchemaParseIncludeOrRedefineAttrs(pctxt, schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010949 node, (xmlChar **) (&schemaLocation), type);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010950 if (res != 0)
Daniel Veillarddee23482008-04-11 12:58:43 +000010951 return(res);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010952 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010953 * Load and add the schema document.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010954 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010955 res = xmlSchemaAddSchemaDoc(pctxt, type, schemaLocation, NULL,
10956 NULL, 0, node, pctxt->targetNamespace, NULL, &bucket);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010957 if (res != 0)
Daniel Veillarddee23482008-04-11 12:58:43 +000010958 return(res);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010959 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010960 * If we get no schema bucket back, then this means that the schema
10961 * document could not be located or was broken XML or was not
10962 * a schema document.
Daniel Veillarddee23482008-04-11 12:58:43 +000010963 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010964 if ((bucket == NULL) || (bucket->doc == NULL)) {
10965 if (type == XML_SCHEMA_SCHEMA_INCLUDE) {
10966 /*
10967 * WARNING for <include>:
10968 * We will raise an error if the schema cannot be located
10969 * for inclusions, since the that was the feedback from the
10970 * schema people. I.e. the following spec piece will *not* be
10971 * satisfied:
Rob Richardsc6947bb2008-06-29 15:04:41 +000010972 * SPEC src-include: "It is not an error for the �actual value� of the
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010973 * schemaLocation [attribute] to fail to resolve it all, in which
10974 * case no corresponding inclusion is performed.
10975 * So do we need a warning report here?"
10976 */
10977 res = XML_SCHEMAP_SRC_INCLUDE;
10978 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10979 node, NULL,
10980 "Failed to load the document '%s' for inclusion",
10981 schemaLocation, NULL);
10982 } else {
10983 /*
10984 * NOTE: This was changed to raise an error even if no redefinitions
10985 * are specified.
10986 *
10987 * SPEC src-redefine (1)
10988 * "If there are any element information items among the [children]
Rob Richardsc6947bb2008-06-29 15:04:41 +000010989 * other than <annotation> then the �actual value� of the
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010990 * schemaLocation [attribute] must successfully resolve."
10991 * TODO: Ask the WG if a the location has always to resolve
10992 * here as well!
10993 */
10994 res = XML_SCHEMAP_SRC_REDEFINE;
10995 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10996 node, NULL,
10997 "Failed to load the document '%s' for redefinition",
10998 schemaLocation, NULL);
10999 }
11000 } else {
11001 /*
11002 * Check targetNamespace sanity before parsing the new schema.
11003 * TODO: Note that we won't check further content if the
11004 * targetNamespace was bad.
Daniel Veillarddee23482008-04-11 12:58:43 +000011005 */
11006 if (bucket->origTargetNamespace != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011007 /*
11008 * SPEC src-include (2.1)
Rob Richardsc6947bb2008-06-29 15:04:41 +000011009 * "SII has a targetNamespace [attribute], and its �actual
11010 * value� is identical to the �actual value� of the targetNamespace
11011 * [attribute] of SII� (which must have such an [attribute])."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011012 */
11013 if (pctxt->targetNamespace == NULL) {
11014 xmlSchemaCustomErr(ACTXT_CAST pctxt,
11015 XML_SCHEMAP_SRC_INCLUDE,
11016 node, NULL,
11017 "The target namespace of the included/redefined schema "
11018 "'%s' has to be absent, since the including/redefining "
11019 "schema has no target namespace",
11020 schemaLocation, NULL);
11021 goto exit_error;
11022 } else if (!xmlStrEqual(bucket->origTargetNamespace,
11023 pctxt->targetNamespace)) {
11024 /* TODO: Change error function. */
11025 xmlSchemaPCustomErrExt(pctxt,
11026 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011027 NULL, node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011028 "The target namespace '%s' of the included/redefined "
11029 "schema '%s' differs from '%s' of the "
11030 "including/redefining schema",
11031 bucket->origTargetNamespace, schemaLocation,
11032 pctxt->targetNamespace);
11033 goto exit_error;
11034 }
Daniel Veillarddee23482008-04-11 12:58:43 +000011035 } else if (pctxt->targetNamespace != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011036 /*
11037 * Chameleons: the original target namespace will
11038 * differ from the resulting namespace.
11039 */
11040 isChameleon = 1;
11041 if (bucket->parsed &&
Jason Childsedc68aa2009-08-07 20:29:33 +020011042 bucket->origTargetNamespace != NULL) {
11043 xmlSchemaCustomErr(ACTXT_CAST pctxt,
11044 XML_SCHEMAP_SRC_INCLUDE,
11045 node, NULL,
11046 "The target namespace of the included/redefined schema "
11047 "'%s' has to be absent or the same as the "
11048 "including/redefining schema's target namespace",
11049 schemaLocation, NULL);
11050 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011051 }
11052 bucket->targetNamespace = pctxt->targetNamespace;
11053 }
Daniel Veillarddee23482008-04-11 12:58:43 +000011054 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011055 /*
11056 * Parse the schema.
Daniel Veillarddee23482008-04-11 12:58:43 +000011057 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011058 if (bucket && (!bucket->parsed) && (bucket->doc != NULL)) {
11059 if (isChameleon) {
11060 /* TODO: Get rid of this flag on the schema itself. */
11061 if ((schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) == 0) {
11062 schema->flags |= XML_SCHEMAS_INCLUDING_CONVERT_NS;
11063 } else
11064 wasChameleon = 1;
11065 }
11066 xmlSchemaParseNewDoc(pctxt, schema, bucket);
11067 /* Restore chameleon flag. */
11068 if (isChameleon && (!wasChameleon))
11069 schema->flags ^= XML_SCHEMAS_INCLUDING_CONVERT_NS;
11070 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011071 /*
11072 * And now for the children...
11073 */
Daniel Veillarddee23482008-04-11 12:58:43 +000011074 child = node->children;
11075 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011076 /*
11077 * Parse (simpleType | complexType | group | attributeGroup))*
11078 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011079 pctxt->redefined = bucket;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011080 /*
11081 * How to proceed if the redefined schema was not located?
11082 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011083 pctxt->isRedefine = 1;
11084 while (IS_SCHEMA(child, "annotation") ||
11085 IS_SCHEMA(child, "simpleType") ||
11086 IS_SCHEMA(child, "complexType") ||
11087 IS_SCHEMA(child, "group") ||
11088 IS_SCHEMA(child, "attributeGroup")) {
11089 if (IS_SCHEMA(child, "annotation")) {
11090 /*
11091 * TODO: discard or not?
11092 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011093 } else if (IS_SCHEMA(child, "simpleType")) {
11094 xmlSchemaParseSimpleType(pctxt, schema, child, 1);
11095 } else if (IS_SCHEMA(child, "complexType")) {
11096 xmlSchemaParseComplexType(pctxt, schema, child, 1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011097 /* hasRedefinitions = 1; */
Daniel Veillarddee23482008-04-11 12:58:43 +000011098 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011099 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011100 xmlSchemaParseModelGroupDefinition(pctxt,
11101 schema, child);
11102 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011103 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011104 xmlSchemaParseAttributeGroupDefinition(pctxt, schema,
11105 child);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011106 }
11107 child = child->next;
11108 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011109 pctxt->redefined = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011110 pctxt->isRedefine = 0;
11111 } else {
11112 if (IS_SCHEMA(child, "annotation")) {
11113 /*
11114 * TODO: discard or not?
11115 */
11116 child = child->next;
11117 }
Daniel Veillarddee23482008-04-11 12:58:43 +000011118 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011119 if (child != NULL) {
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011120 res = XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011121 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
11122 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011123 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011124 "(annotation | (simpleType | complexType | group | attributeGroup))*");
11125 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011126 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011127 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011128 "(annotation?)");
Daniel Veillarddee23482008-04-11 12:58:43 +000011129 }
11130 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011131 return(res);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011132
11133exit_error:
11134 return(pctxt->err);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011135}
11136
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011137static int
11138xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11139 xmlNodePtr node)
11140{
11141 int res;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011142#ifndef ENABLE_REDEFINE
11143 TODO
11144 return(0);
11145#endif
11146 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11147 XML_SCHEMA_SCHEMA_REDEFINE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011148 if (res != 0)
11149 return(res);
11150 return(0);
11151}
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011152
11153static int
11154xmlSchemaParseInclude(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11155 xmlNodePtr node)
11156{
11157 int res;
11158
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011159 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11160 XML_SCHEMA_SCHEMA_INCLUDE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011161 if (res != 0)
11162 return(res);
11163 return(0);
11164}
11165
Daniel Veillardbd2904b2003-11-25 15:38:59 +000011166/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011167 * xmlSchemaParseModelGroup:
Daniel Veillard4255d502002-04-16 15:50:10 +000011168 * @ctxt: a schema validation context
11169 * @schema: the schema being built
11170 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011171 * @type: the "compositor" type
11172 * @particleNeeded: if a a model group with a particle
Daniel Veillard4255d502002-04-16 15:50:10 +000011173 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011174 * parse a XML schema Sequence definition.
11175 * Applies parts of:
11176 * Schema Representation Constraint:
11177 * Redefinition Constraints and Semantics (src-redefine)
11178 * (6.1), (6.1.1), (6.1.2)
11179 *
Daniel Veillarddee23482008-04-11 12:58:43 +000011180 * Schema Component Constraint:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011181 * All Group Limited (cos-all-limited) (2)
11182 * TODO: Actually this should go to component-level checks,
11183 * but is done here due to performance. Move it to an other layer
11184 * is schema construction via an API is implemented.
11185 *
Daniel Veillard4255d502002-04-16 15:50:10 +000011186 * *WARNING* this interface is highly subject to change
11187 *
William M. Bracke7091952004-05-11 15:09:58 +000011188 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +000011189 * 1 in case of success.
11190 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011191static xmlSchemaTreeItemPtr
11192xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
11193 xmlNodePtr node, xmlSchemaTypeType type,
11194 int withParticle)
Daniel Veillard4255d502002-04-16 15:50:10 +000011195{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011196 xmlSchemaModelGroupPtr item;
11197 xmlSchemaParticlePtr particle = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000011198 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011199 xmlAttrPtr attr;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011200 int min = 1, max = 1, isElemRef, hasRefs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000011201
11202 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011203 return (NULL);
11204 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011205 * Create a model group with the given compositor.
William M. Brack2f2a6632004-08-20 23:09:47 +000011206 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011207 item = xmlSchemaAddModelGroup(ctxt, schema, type, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011208 if (item == NULL)
11209 return (NULL);
11210
11211 if (withParticle) {
11212 if (type == XML_SCHEMA_TYPE_ALL) {
11213 min = xmlGetMinOccurs(ctxt, node, 0, 1, 1, "(0 | 1)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011214 max = xmlGetMaxOccurs(ctxt, node, 1, 1, 1, "1");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011215 } else {
11216 /* choice + sequence */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011217 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
11218 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
11219 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011220 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011221 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
11222 /*
11223 * Create a particle
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011224 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000011225 particle = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011226 if (particle == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011227 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011228 particle->children = (xmlSchemaTreeItemPtr) item;
11229 /*
11230 * Check for illegal attributes.
11231 */
11232 attr = node->properties;
11233 while (attr != NULL) {
11234 if (attr->ns == NULL) {
11235 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11236 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
11237 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011238 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011239 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011240 }
11241 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011242 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011243 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011244 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011245 attr = attr->next;
William M. Brack2f2a6632004-08-20 23:09:47 +000011246 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011247 } else {
11248 /*
11249 * Check for illegal attributes.
11250 */
11251 attr = node->properties;
11252 while (attr != NULL) {
11253 if (attr->ns == NULL) {
11254 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011255 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011256 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011257 }
11258 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011259 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011260 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011261 }
11262 attr = attr->next;
11263 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011264 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011265
William M. Brack2f2a6632004-08-20 23:09:47 +000011266 /*
11267 * Extract and validate attributes.
11268 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011269 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011270 /*
11271 * And now for the children...
11272 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011273 child = node->children;
11274 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011275 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011276 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011277 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011278 if (type == XML_SCHEMA_TYPE_ALL) {
11279 xmlSchemaParticlePtr part, last = NULL;
11280
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011281 while (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011282 part = (xmlSchemaParticlePtr) xmlSchemaParseElement(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011283 schema, child, &isElemRef, 0);
11284 /*
11285 * SPEC cos-all-limited (2)
11286 * "The {max occurs} of all the particles in the {particles}
11287 * of the ('all') group must be 0 or 1.
11288 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011289 if (part != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011290 if (isElemRef)
11291 hasRefs++;
11292 if (part->minOccurs > 1) {
11293 xmlSchemaPCustomErr(ctxt,
11294 XML_SCHEMAP_COS_ALL_LIMITED,
11295 NULL, child,
11296 "Invalid value for minOccurs (must be 0 or 1)",
11297 NULL);
11298 /* Reset to 1. */
11299 part->minOccurs = 1;
11300 }
11301 if (part->maxOccurs > 1) {
11302 xmlSchemaPCustomErr(ctxt,
11303 XML_SCHEMAP_COS_ALL_LIMITED,
11304 NULL, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011305 "Invalid value for maxOccurs (must be 0 or 1)",
11306 NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011307 /* Reset to 1. */
11308 part->maxOccurs = 1;
11309 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011310 if (last == NULL)
11311 item->children = (xmlSchemaTreeItemPtr) part;
11312 else
11313 last->next = (xmlSchemaTreeItemPtr) part;
11314 last = part;
11315 }
11316 child = child->next;
11317 }
11318 if (child != NULL) {
11319 xmlSchemaPContentErr(ctxt,
11320 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011321 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011322 "(annotation?, (annotation?, element*)");
11323 }
11324 } else {
11325 /* choice + sequence */
11326 xmlSchemaTreeItemPtr part = NULL, last = NULL;
11327
11328 while ((IS_SCHEMA(child, "element")) ||
11329 (IS_SCHEMA(child, "group")) ||
11330 (IS_SCHEMA(child, "any")) ||
11331 (IS_SCHEMA(child, "choice")) ||
11332 (IS_SCHEMA(child, "sequence"))) {
11333
11334 if (IS_SCHEMA(child, "element")) {
11335 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011336 xmlSchemaParseElement(ctxt, schema, child, &isElemRef, 0);
11337 if (part && isElemRef)
11338 hasRefs++;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011339 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011340 part =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011341 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011342 if (part != NULL)
11343 hasRefs++;
11344 /*
11345 * Handle redefinitions.
11346 */
11347 if (ctxt->isRedefine && ctxt->redef &&
11348 (ctxt->redef->item->type == XML_SCHEMA_TYPE_GROUP) &&
11349 part && part->children)
11350 {
11351 if ((xmlSchemaGetQNameRefName(part->children) ==
11352 ctxt->redef->refName) &&
11353 (xmlSchemaGetQNameRefTargetNs(part->children) ==
11354 ctxt->redef->refTargetNs))
11355 {
11356 /*
11357 * SPEC src-redefine:
11358 * (6.1) "If it has a <group> among its contents at
Rob Richardsc6947bb2008-06-29 15:04:41 +000011359 * some level the �actual value� of whose ref
11360 * [attribute] is the same as the �actual value� of
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011361 * its own name attribute plus target namespace, then
11362 * all of the following must be true:"
11363 * (6.1.1) "It must have exactly one such group."
11364 */
11365 if (ctxt->redefCounter != 0) {
11366 xmlChar *str = NULL;
11367
11368 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11369 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11370 "The redefining model group definition "
11371 "'%s' must not contain more than one "
11372 "reference to the redefined definition",
11373 xmlSchemaFormatQName(&str,
11374 ctxt->redef->refTargetNs,
11375 ctxt->redef->refName),
11376 NULL);
11377 FREE_AND_NULL(str)
11378 part = NULL;
11379 } else if (((WXS_PARTICLE(part))->minOccurs != 1) ||
11380 ((WXS_PARTICLE(part))->maxOccurs != 1))
11381 {
11382 xmlChar *str = NULL;
11383 /*
11384 * SPEC src-redefine:
Rob Richardsc6947bb2008-06-29 15:04:41 +000011385 * (6.1.2) "The �actual value� of both that
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011386 * group's minOccurs and maxOccurs [attribute]
Rob Richardsc6947bb2008-06-29 15:04:41 +000011387 * must be 1 (or �absent�).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011388 */
11389 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11390 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11391 "The redefining model group definition "
11392 "'%s' must not contain a reference to the "
11393 "redefined definition with a "
11394 "maxOccurs/minOccurs other than 1",
11395 xmlSchemaFormatQName(&str,
11396 ctxt->redef->refTargetNs,
11397 ctxt->redef->refName),
11398 NULL);
11399 FREE_AND_NULL(str)
11400 part = NULL;
11401 }
11402 ctxt->redef->reference = WXS_BASIC_CAST part;
11403 ctxt->redefCounter++;
Daniel Veillarddee23482008-04-11 12:58:43 +000011404 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011405 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011406 } else if (IS_SCHEMA(child, "any")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011407 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011408 xmlSchemaParseAny(ctxt, schema, child);
11409 } else if (IS_SCHEMA(child, "choice")) {
11410 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11411 XML_SCHEMA_TYPE_CHOICE, 1);
11412 } else if (IS_SCHEMA(child, "sequence")) {
11413 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11414 XML_SCHEMA_TYPE_SEQUENCE, 1);
11415 }
11416 if (part != NULL) {
11417 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011418 item->children = part;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011419 else
11420 last->next = part;
11421 last = part;
11422 }
11423 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011424 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011425 if (child != NULL) {
11426 xmlSchemaPContentErr(ctxt,
11427 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011428 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011429 "(annotation?, (element | group | choice | sequence | any)*)");
11430 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011431 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011432 if ((max == 0) && (min == 0))
11433 return (NULL);
11434 if (hasRefs) {
11435 /*
11436 * We need to resolve references.
11437 */
11438 WXS_ADD_PENDING(ctxt, item);
11439 }
11440 if (withParticle)
Daniel Veillarddee23482008-04-11 12:58:43 +000011441 return ((xmlSchemaTreeItemPtr) particle);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011442 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011443 return ((xmlSchemaTreeItemPtr) item);
Daniel Veillard4255d502002-04-16 15:50:10 +000011444}
11445
11446/**
11447 * xmlSchemaParseRestriction:
11448 * @ctxt: a schema validation context
11449 * @schema: the schema being built
11450 * @node: a subtree containing XML Schema informations
Daniel Veillard4255d502002-04-16 15:50:10 +000011451 *
11452 * parse a XML schema Restriction definition
11453 * *WARNING* this interface is highly subject to change
11454 *
11455 * Returns the type definition or NULL in case of error
11456 */
11457static xmlSchemaTypePtr
11458xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011459 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011460{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011461 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011462 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011463 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011464
11465 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11466 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011467 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011468 type = ctxt->ctxtType;
11469 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011470
11471 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011472 * Check for illegal attributes.
11473 */
11474 attr = node->properties;
11475 while (attr != NULL) {
11476 if (attr->ns == NULL) {
11477 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11478 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011479 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011480 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011481 }
11482 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011483 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011484 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011485 }
11486 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011487 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011488 /*
11489 * Extract and validate attributes.
11490 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011491 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011492 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000011493 * Attribute
William M. Brack2f2a6632004-08-20 23:09:47 +000011494 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011495 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011496 * Extract the base type. The "base" attribute is mandatory if inside
11497 * a complex type or if redefining.
11498 *
11499 * SPEC (1.2) "...otherwise (<restriction> has no <simpleType> "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011500 * among its [children]), the simple type definition which is
Rob Richardsc6947bb2008-06-29 15:04:41 +000011501 * the {content type} of the type definition �resolved� to by
11502 * the �actual value� of the base [attribute]"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011503 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011504 if (xmlSchemaPValAttrQName(ctxt, schema, NULL, node, "base",
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011505 &(type->baseNs), &(type->base)) == 0)
11506 {
11507 if ((type->base == NULL) && (type->type == XML_SCHEMA_TYPE_COMPLEX)) {
11508 xmlSchemaPMissingAttrErr(ctxt,
11509 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011510 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011511 } else if ((ctxt->isRedefine) &&
11512 (type->flags & XML_SCHEMAS_TYPE_GLOBAL))
11513 {
11514 if (type->base == NULL) {
11515 xmlSchemaPMissingAttrErr(ctxt,
11516 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011517 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011518 } else if ((! xmlStrEqual(type->base, type->name)) ||
11519 (! xmlStrEqual(type->baseNs, type->targetNamespace)))
11520 {
11521 xmlChar *str1 = NULL, *str2 = NULL;
11522 /*
11523 * REDEFINE: SPEC src-redefine (5)
11524 * "Within the [children], each <simpleType> must have a
Rob Richardsc6947bb2008-06-29 15:04:41 +000011525 * <restriction> among its [children] ... the �actual value� of
11526 * whose base [attribute] must be the same as the �actual value�
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011527 * of its own name attribute plus target namespace;"
11528 */
11529 xmlSchemaPCustomErrExt(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011530 NULL, node, "This is a redefinition, but the QName "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011531 "value '%s' of the 'base' attribute does not match the "
11532 "type's designation '%s'",
11533 xmlSchemaFormatQName(&str1, type->baseNs, type->base),
Daniel Veillardfef73a52006-03-27 09:38:57 +000011534 xmlSchemaFormatQName(&str2, type->targetNamespace,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011535 type->name), NULL);
11536 FREE_AND_NULL(str1);
11537 FREE_AND_NULL(str2);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011538 /* Avoid confusion and erase the values. */
11539 type->base = NULL;
11540 type->baseNs = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011541 }
Daniel Veillarddee23482008-04-11 12:58:43 +000011542 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011543 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011544 /*
11545 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011546 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011547 child = node->children;
11548 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011549 /*
11550 * Add the annotation to the simple type ancestor.
11551 */
11552 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011553 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011554 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011555 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011556 if (parentType == XML_SCHEMA_TYPE_SIMPLE) {
11557 /*
11558 * Corresponds to <simpleType><restriction><simpleType>.
11559 */
William M. Brack2f2a6632004-08-20 23:09:47 +000011560 if (IS_SCHEMA(child, "simpleType")) {
11561 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011562 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011563 * src-restriction-base-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011564 * Either the base [attribute] or the simpleType [child] of the
11565 * <restriction> element must be present, but not both.
William M. Brack2f2a6632004-08-20 23:09:47 +000011566 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011567 xmlSchemaPContentErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000011568 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011569 NULL, node, child,
William M. Brack2f2a6632004-08-20 23:09:47 +000011570 "The attribute 'base' and the <simpleType> child are "
11571 "mutually exclusive", NULL);
11572 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011573 type->baseType = (xmlSchemaTypePtr)
William M. Brack2f2a6632004-08-20 23:09:47 +000011574 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011575 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011576 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011577 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011578 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011579 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011580 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011581 "Either the attribute 'base' or a <simpleType> child "
11582 "must be present", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000011583 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011584 } else if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11585 /*
11586 * Corresponds to <complexType><complexContent><restriction>...
11587 * followed by:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011588 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011589 * Model groups <all>, <choice> and <sequence>.
11590 */
11591 if (IS_SCHEMA(child, "all")) {
11592 type->subtypes = (xmlSchemaTypePtr)
11593 xmlSchemaParseModelGroup(ctxt, schema, child,
11594 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011595 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011596 } else if (IS_SCHEMA(child, "choice")) {
11597 type->subtypes = (xmlSchemaTypePtr)
11598 xmlSchemaParseModelGroup(ctxt,
11599 schema, child, XML_SCHEMA_TYPE_CHOICE, 1);
11600 child = child->next;
11601 } else if (IS_SCHEMA(child, "sequence")) {
11602 type->subtypes = (xmlSchemaTypePtr)
11603 xmlSchemaParseModelGroup(ctxt, schema, child,
11604 XML_SCHEMA_TYPE_SEQUENCE, 1);
11605 child = child->next;
11606 /*
11607 * Model group reference <group>.
11608 */
Daniel Veillarddee23482008-04-11 12:58:43 +000011609 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011610 type->subtypes = (xmlSchemaTypePtr)
11611 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011612 /*
11613 * Note that the reference will be resolved in
11614 * xmlSchemaResolveTypeReferences();
11615 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011616 child = child->next;
11617 }
11618 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011619 /*
11620 * Corresponds to <complexType><simpleContent><restriction>...
11621 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011622 * "1.1 the simple type definition corresponding to the <simpleType>
11623 * among the [children] of <restriction> if there is one;"
11624 */
11625 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011626 /*
11627 * We will store the to-be-restricted simple type in
11628 * type->contentTypeDef *temporarily*.
11629 */
11630 type->contentTypeDef = (xmlSchemaTypePtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011631 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011632 if ( type->contentTypeDef == NULL)
11633 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011634 child = child->next;
11635 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011636 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011637
11638 if ((parentType == XML_SCHEMA_TYPE_SIMPLE) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011639 (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011640 xmlSchemaFacetPtr facet, lastfacet = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011641 /*
11642 * Corresponds to <complexType><simpleContent><restriction>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011643 * <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011644 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011645
Daniel Veillard01fa6152004-06-29 17:04:39 +000011646 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011647 * Add the facets to the simple type ancestor.
Daniel Veillard01fa6152004-06-29 17:04:39 +000011648 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000011649 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011650 * TODO: Datatypes: 4.1.3 Constraints on XML Representation of
11651 * Simple Type Definition Schema Representation Constraint:
Daniel Veillardc0826a72004-08-10 14:17:33 +000011652 * *Single Facet Value*
11653 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000011654 while ((IS_SCHEMA(child, "minInclusive")) ||
11655 (IS_SCHEMA(child, "minExclusive")) ||
11656 (IS_SCHEMA(child, "maxInclusive")) ||
11657 (IS_SCHEMA(child, "maxExclusive")) ||
11658 (IS_SCHEMA(child, "totalDigits")) ||
11659 (IS_SCHEMA(child, "fractionDigits")) ||
11660 (IS_SCHEMA(child, "pattern")) ||
11661 (IS_SCHEMA(child, "enumeration")) ||
11662 (IS_SCHEMA(child, "whiteSpace")) ||
11663 (IS_SCHEMA(child, "length")) ||
11664 (IS_SCHEMA(child, "maxLength")) ||
11665 (IS_SCHEMA(child, "minLength"))) {
11666 facet = xmlSchemaParseFacet(ctxt, schema, child);
11667 if (facet != NULL) {
11668 if (lastfacet == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011669 type->facets = facet;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011670 else
11671 lastfacet->next = facet;
11672 lastfacet = facet;
11673 lastfacet->next = NULL;
11674 }
11675 child = child->next;
11676 }
11677 /*
11678 * Create links for derivation and validation.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011679 */
11680 if (type->facets != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000011681 xmlSchemaFacetLinkPtr facetLink, lastFacetLink = NULL;
11682
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011683 facet = type->facets;
11684 do {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011685 facetLink = (xmlSchemaFacetLinkPtr)
11686 xmlMalloc(sizeof(xmlSchemaFacetLink));
Daniel Veillard01fa6152004-06-29 17:04:39 +000011687 if (facetLink == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000011688 xmlSchemaPErrMemory(ctxt, "allocating a facet link", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011689 xmlFree(facetLink);
11690 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011691 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000011692 facetLink->facet = facet;
11693 facetLink->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011694 if (lastFacetLink == NULL)
11695 type->facetSet = facetLink;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011696 else
11697 lastFacetLink->next = facetLink;
11698 lastFacetLink = facetLink;
11699 facet = facet->next;
11700 } while (facet != NULL);
11701 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011702 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011703 if (type->type == XML_SCHEMA_TYPE_COMPLEX) {
11704 /*
11705 * Attribute uses/declarations.
11706 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011707 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11708 (xmlSchemaItemListPtr *) &(type->attrUses),
11709 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
11710 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011711 /*
11712 * Attribute wildcard.
11713 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011714 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011715 type->attributeWildcard =
11716 xmlSchemaParseAnyAttribute(ctxt, schema, child);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011717 child = child->next;
11718 }
11719 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011720 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011721 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11722 xmlSchemaPContentErr(ctxt,
11723 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011724 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011725 "annotation?, (group | all | choice | sequence)?, "
11726 "((attribute | attributeGroup)*, anyAttribute?))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011727 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011728 xmlSchemaPContentErr(ctxt,
11729 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011730 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011731 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11732 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11733 "length | minLength | maxLength | enumeration | whiteSpace | "
11734 "pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))");
11735 } else {
11736 /* Simple type */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011737 xmlSchemaPContentErr(ctxt,
11738 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011739 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011740 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11741 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11742 "length | minLength | maxLength | enumeration | whiteSpace | "
11743 "pattern)*))");
11744 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011745 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011746 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011747}
11748
11749/**
11750 * xmlSchemaParseExtension:
11751 * @ctxt: a schema validation context
11752 * @schema: the schema being built
11753 * @node: a subtree containing XML Schema informations
11754 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011755 * Parses an <extension>, which is found inside a
11756 * <simpleContent> or <complexContent>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011757 * *WARNING* this interface is highly subject to change.
Daniel Veillard4255d502002-04-16 15:50:10 +000011758 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011759 * TODO: Returns the type definition or NULL in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +000011760 */
11761static xmlSchemaTypePtr
11762xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011763 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011764{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011765 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011766 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011767 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011768
11769 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11770 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011771 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011772 type = ctxt->ctxtType;
11773 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION;
Daniel Veillard4255d502002-04-16 15:50:10 +000011774
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011775 /*
11776 * Check for illegal attributes.
11777 */
11778 attr = node->properties;
11779 while (attr != NULL) {
11780 if (attr->ns == NULL) {
11781 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11782 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011783 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011784 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011785 }
11786 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011787 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011788 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011789 }
11790 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011791 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011792
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011793 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011794
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011795 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011796 * Attribute "base" - mandatory.
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011797 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011798 if ((xmlSchemaPValAttrQName(ctxt, schema, NULL, node,
11799 "base", &(type->baseNs), &(type->base)) == 0) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011800 (type->base == NULL)) {
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011801 xmlSchemaPMissingAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011802 XML_SCHEMAP_S4S_ATTR_MISSING,
11803 NULL, node, "base", NULL);
11804 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011805 /*
11806 * And now for the children...
11807 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011808 child = node->children;
11809 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011810 /*
11811 * Add the annotation to the type ancestor.
11812 */
11813 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011814 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011815 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011816 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011817 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11818 /*
11819 * Corresponds to <complexType><complexContent><extension>... and:
11820 *
11821 * Model groups <all>, <choice>, <sequence> and <group>.
11822 */
11823 if (IS_SCHEMA(child, "all")) {
11824 type->subtypes = (xmlSchemaTypePtr)
11825 xmlSchemaParseModelGroup(ctxt, schema,
11826 child, XML_SCHEMA_TYPE_ALL, 1);
11827 child = child->next;
11828 } else if (IS_SCHEMA(child, "choice")) {
11829 type->subtypes = (xmlSchemaTypePtr)
11830 xmlSchemaParseModelGroup(ctxt, schema,
11831 child, XML_SCHEMA_TYPE_CHOICE, 1);
11832 child = child->next;
11833 } else if (IS_SCHEMA(child, "sequence")) {
11834 type->subtypes = (xmlSchemaTypePtr)
11835 xmlSchemaParseModelGroup(ctxt, schema,
11836 child, XML_SCHEMA_TYPE_SEQUENCE, 1);
11837 child = child->next;
11838 } else if (IS_SCHEMA(child, "group")) {
11839 type->subtypes = (xmlSchemaTypePtr)
11840 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011841 /*
11842 * Note that the reference will be resolved in
11843 * xmlSchemaResolveTypeReferences();
11844 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011845 child = child->next;
11846 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011847 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011848 if (child != NULL) {
11849 /*
11850 * Attribute uses/declarations.
11851 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011852 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11853 (xmlSchemaItemListPtr *) &(type->attrUses),
11854 XML_SCHEMA_TYPE_EXTENSION, NULL) == -1)
11855 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011856 /*
11857 * Attribute wildcard.
11858 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011859 if (IS_SCHEMA(child, "anyAttribute")) {
11860 ctxt->ctxtType->attributeWildcard =
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011861 xmlSchemaParseAnyAttribute(ctxt, schema, child);
11862 child = child->next;
11863 }
11864 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011865 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011866 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11867 /* Complex content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011868 xmlSchemaPContentErr(ctxt,
11869 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011870 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011871 "(annotation?, ((group | all | choice | sequence)?, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011872 "((attribute | attributeGroup)*, anyAttribute?)))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011873 } else {
11874 /* Simple content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011875 xmlSchemaPContentErr(ctxt,
11876 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011877 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011878 "(annotation?, ((attribute | attributeGroup)*, "
11879 "anyAttribute?))");
11880 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011881 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011882 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011883}
11884
11885/**
11886 * xmlSchemaParseSimpleContent:
11887 * @ctxt: a schema validation context
11888 * @schema: the schema being built
11889 * @node: a subtree containing XML Schema informations
11890 *
11891 * parse a XML schema SimpleContent definition
11892 * *WARNING* this interface is highly subject to change
11893 *
11894 * Returns the type definition or NULL in case of error
11895 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011896static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011897xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011898 xmlSchemaPtr schema, xmlNodePtr node,
11899 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011900{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011901 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011902 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011903 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011904
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011905 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11906 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011907 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011908 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011909 /* Not a component, don't create it. */
11910 type = ctxt->ctxtType;
11911 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
11912 /*
11913 * Check for illegal attributes.
11914 */
11915 attr = node->properties;
11916 while (attr != NULL) {
11917 if (attr->ns == NULL) {
11918 if ((!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011919 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011920 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011921 }
11922 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011923 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011924 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011925 }
11926 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011927 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011928
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011929 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +000011930
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011931 /*
11932 * And now for the children...
11933 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011934 child = node->children;
11935 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011936 /*
11937 * Add the annotation to the complex type ancestor.
11938 */
11939 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011940 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011941 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011942 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011943 if (child == NULL) {
11944 xmlSchemaPContentErr(ctxt,
11945 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011946 NULL, node, NULL, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000011947 "(annotation?, (restriction | extension))");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011948 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011949 if (child == NULL) {
11950 xmlSchemaPContentErr(ctxt,
11951 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011952 NULL, node, NULL, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000011953 "(annotation?, (restriction | extension))");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011954 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011955 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011956 xmlSchemaParseRestriction(ctxt, schema, child,
11957 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011958 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011959 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011960 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011961 xmlSchemaParseExtension(ctxt, schema, child,
11962 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011963 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011964 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011965 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011966 if (child != NULL) {
11967 xmlSchemaPContentErr(ctxt,
11968 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011969 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011970 "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000011971 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011972 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000011973}
11974
11975/**
11976 * xmlSchemaParseComplexContent:
11977 * @ctxt: a schema validation context
11978 * @schema: the schema being built
11979 * @node: a subtree containing XML Schema informations
11980 *
11981 * parse a XML schema ComplexContent definition
11982 * *WARNING* this interface is highly subject to change
11983 *
11984 * Returns the type definition or NULL in case of error
11985 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011986static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011987xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011988 xmlSchemaPtr schema, xmlNodePtr node,
11989 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011990{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011991 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011992 xmlNodePtr child = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011993 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011994
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011995 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11996 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011997 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011998 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011999 /* Not a component, don't create it. */
12000 type = ctxt->ctxtType;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012001 /*
12002 * Check for illegal attributes.
12003 */
12004 attr = node->properties;
12005 while (attr != NULL) {
12006 if (attr->ns == NULL) {
12007 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012008 (!xmlStrEqual(attr->name, BAD_CAST "mixed")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012009 {
12010 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012011 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012012 }
12013 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
12014 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012015 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012016 }
12017 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012018 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000012019
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012020 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000012021
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012022 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012023 * Set the 'mixed' on the complex type ancestor.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012024 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012025 if (xmlGetBooleanProp(ctxt, node, "mixed", 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012026 if ((type->flags & XML_SCHEMAS_TYPE_MIXED) == 0)
12027 type->flags |= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012028 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012029 child = node->children;
12030 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012031 /*
12032 * Add the annotation to the complex type ancestor.
12033 */
12034 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000012035 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012036 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012037 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012038 if (child == NULL) {
12039 xmlSchemaPContentErr(ctxt,
12040 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012041 NULL, node, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012042 NULL, "(annotation?, (restriction | extension))");
12043 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012044 if (child == NULL) {
12045 xmlSchemaPContentErr(ctxt,
12046 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012047 NULL, node, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012048 NULL, "(annotation?, (restriction | extension))");
12049 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012050 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012051 xmlSchemaParseRestriction(ctxt, schema, child,
12052 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012053 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012054 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012055 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012056 xmlSchemaParseExtension(ctxt, schema, child,
12057 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012058 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012059 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012060 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012061 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012062 xmlSchemaPContentErr(ctxt,
12063 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012064 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012065 NULL, "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012066 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012067 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000012068}
12069
12070/**
12071 * xmlSchemaParseComplexType:
12072 * @ctxt: a schema validation context
12073 * @schema: the schema being built
12074 * @node: a subtree containing XML Schema informations
12075 *
12076 * parse a XML schema Complex Type definition
12077 * *WARNING* this interface is highly subject to change
12078 *
12079 * Returns the type definition or NULL in case of error
12080 */
12081static xmlSchemaTypePtr
12082xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +000012083 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +000012084{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012085 xmlSchemaTypePtr type, ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012086 xmlNodePtr child = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012087 const xmlChar *name = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012088 xmlAttrPtr attr;
12089 const xmlChar *attrValue;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012090#ifdef ENABLE_NAMED_LOCALS
Daniel Veillard1a380b82004-10-21 16:00:06 +000012091 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012092#endif
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012093 int final = 0, block = 0, hasRestrictionOrExtension = 0;
Daniel Veillard1a380b82004-10-21 16:00:06 +000012094
Daniel Veillard4255d502002-04-16 15:50:10 +000012095
12096 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
12097 return (NULL);
12098
Daniel Veillard01fa6152004-06-29 17:04:39 +000012099 ctxtType = ctxt->ctxtType;
12100
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012101 if (topLevel) {
12102 attr = xmlSchemaGetPropNode(node, "name");
12103 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012104 xmlSchemaPMissingAttrErr(ctxt,
12105 XML_SCHEMAP_S4S_ATTR_MISSING, NULL, node, "name", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012106 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012107 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012108 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
12109 return (NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000012110 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012111 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012112
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012113 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012114 /*
12115 * Parse as local complex type definition.
12116 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012117#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +000012118 snprintf(buf, 39, "#CT%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012119 type = xmlSchemaAddType(ctxt, schema,
12120 XML_SCHEMA_TYPE_COMPLEX,
12121 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012122 ctxt->targetNamespace, node, 0);
12123#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012124 type = xmlSchemaAddType(ctxt, schema,
12125 XML_SCHEMA_TYPE_COMPLEX,
12126 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012127#endif
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012128 if (type == NULL)
12129 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012130 name = type->name;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012131 type->node = node;
12132 type->type = XML_SCHEMA_TYPE_COMPLEX;
12133 /*
12134 * TODO: We need the target namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012135 */
12136 } else {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012137 /*
12138 * Parse as global complex type definition.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012139 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012140 type = xmlSchemaAddType(ctxt, schema,
12141 XML_SCHEMA_TYPE_COMPLEX,
12142 name, ctxt->targetNamespace, node, 1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012143 if (type == NULL)
12144 return (NULL);
12145 type->node = node;
12146 type->type = XML_SCHEMA_TYPE_COMPLEX;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012147 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +000012148 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012149 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012150 /*
12151 * Handle attributes.
12152 */
12153 attr = node->properties;
12154 while (attr != NULL) {
12155 if (attr->ns == NULL) {
12156 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
12157 /*
12158 * Attribute "id".
12159 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012160 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012161 } else if (xmlStrEqual(attr->name, BAD_CAST "mixed")) {
12162 /*
12163 * Attribute "mixed".
12164 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012165 if (xmlSchemaPGetBoolNodeValue(ctxt,
12166 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012167 type->flags |= XML_SCHEMAS_TYPE_MIXED;
12168 } else if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012169 /*
12170 * Attributes of global complex type definitions.
12171 */
12172 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
12173 /* Pass. */
12174 } else if (xmlStrEqual(attr->name, BAD_CAST "abstract")) {
12175 /*
12176 * Attribute "abstract".
12177 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012178 if (xmlSchemaPGetBoolNodeValue(ctxt,
12179 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012180 type->flags |= XML_SCHEMAS_TYPE_ABSTRACT;
12181 } else if (xmlStrEqual(attr->name, BAD_CAST "final")) {
12182 /*
12183 * Attribute "final".
12184 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012185 attrValue = xmlSchemaGetNodeContent(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012186 (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012187 if (xmlSchemaPValAttrBlockFinal(attrValue,
12188 &(type->flags),
12189 -1,
12190 XML_SCHEMAS_TYPE_FINAL_EXTENSION,
12191 XML_SCHEMAS_TYPE_FINAL_RESTRICTION,
12192 -1, -1, -1) != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012193 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012194 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012195 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012196 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012197 "(#all | List of (extension | restriction))",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012198 attrValue, NULL, NULL, NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000012199 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012200 final = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012201 } else if (xmlStrEqual(attr->name, BAD_CAST "block")) {
12202 /*
12203 * Attribute "block".
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012204 */
12205 attrValue = xmlSchemaGetNodeContent(ctxt,
12206 (xmlNodePtr) attr);
12207 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012208 -1,
12209 XML_SCHEMAS_TYPE_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012210 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012211 -1, -1, -1) != 0) {
12212 xmlSchemaPSimpleTypeErr(ctxt,
12213 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012214 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012215 "(#all | List of (extension | restriction)) ",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012216 attrValue, NULL, NULL, NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000012217 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012218 block = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012219 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012220 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012221 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012222 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012223 } else {
12224 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012225 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012226 }
12227 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000012228 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012229 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012230 }
12231 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012232 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012233 if (! block) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000012234 /*
12235 * Apply default "block" values.
12236 */
12237 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
12238 type->flags |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
12239 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
12240 type->flags |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
12241 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012242 if (! final) {
12243 /*
12244 * Apply default "block" values.
12245 */
12246 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
12247 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
12248 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
12249 type->flags |= XML_SCHEMAS_TYPE_FINAL_EXTENSION;
12250 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012251 /*
12252 * And now for the children...
12253 */
Daniel Veillard4255d502002-04-16 15:50:10 +000012254 child = node->children;
12255 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000012256 type->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012257 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012258 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012259 ctxt->ctxtType = type;
Daniel Veillard4255d502002-04-16 15:50:10 +000012260 if (IS_SCHEMA(child, "simpleContent")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012261 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012262 * <complexType><simpleContent>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012263 * 3.4.3 : 2.2
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012264 * Specifying mixed='true' when the <simpleContent>
12265 * alternative is chosen has no effect
12266 */
William M. Bracke7091952004-05-11 15:09:58 +000012267 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
12268 type->flags ^= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012269 xmlSchemaParseSimpleContent(ctxt, schema, child,
12270 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012271 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012272 } else if (IS_SCHEMA(child, "complexContent")) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012273 /*
12274 * <complexType><complexContent>...
12275 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012276 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012277 xmlSchemaParseComplexContent(ctxt, schema, child,
12278 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012279 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012280 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012281 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012282 * E.g <complexType><sequence>... or <complexType><attribute>... etc.
12283 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012284 * SPEC
12285 * "...the third alternative (neither <simpleContent> nor
12286 * <complexContent>) is chosen. This case is understood as shorthand
Rob Richardsc6947bb2008-06-29 15:04:41 +000012287 * for complex content restricting the �ur-type definition�, and the
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012288 * details of the mappings should be modified as necessary.
12289 */
12290 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
12291 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012292 /*
12293 * Parse model groups.
12294 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012295 if (IS_SCHEMA(child, "all")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012296 type->subtypes = (xmlSchemaTypePtr)
12297 xmlSchemaParseModelGroup(ctxt, schema, child,
12298 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012299 child = child->next;
12300 } else if (IS_SCHEMA(child, "choice")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012301 type->subtypes = (xmlSchemaTypePtr)
12302 xmlSchemaParseModelGroup(ctxt, schema, child,
12303 XML_SCHEMA_TYPE_CHOICE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012304 child = child->next;
12305 } else if (IS_SCHEMA(child, "sequence")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012306 type->subtypes = (xmlSchemaTypePtr)
12307 xmlSchemaParseModelGroup(ctxt, schema, child,
12308 XML_SCHEMA_TYPE_SEQUENCE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012309 child = child->next;
12310 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012311 type->subtypes = (xmlSchemaTypePtr)
12312 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012313 /*
12314 * Note that the reference will be resolved in
12315 * xmlSchemaResolveTypeReferences();
12316 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012317 child = child->next;
12318 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012319 /*
12320 * Parse attribute decls/refs.
12321 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012322 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
12323 (xmlSchemaItemListPtr *) &(type->attrUses),
12324 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
12325 return(NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012326 /*
12327 * Parse attribute wildcard.
12328 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012329 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012330 type->attributeWildcard = xmlSchemaParseAnyAttribute(ctxt, schema, child);
12331 child = child->next;
12332 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012333 }
12334 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012335 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012336 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012337 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012338 NULL, "(annotation?, (simpleContent | complexContent | "
12339 "((group | all | choice | sequence)?, ((attribute | "
12340 "attributeGroup)*, anyAttribute?))))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012341 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012342 /*
12343 * REDEFINE: SPEC src-redefine (5)
12344 */
12345 if (topLevel && ctxt->isRedefine && (! hasRestrictionOrExtension)) {
12346 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012347 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012348 "<complexType> must have a <restriction> or <extension> "
12349 "grand-child", NULL);
12350 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012351 ctxt->ctxtType = ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012352 return (type);
12353}
12354
Daniel Veillard4255d502002-04-16 15:50:10 +000012355/************************************************************************
12356 * *
12357 * Validating using Schemas *
12358 * *
12359 ************************************************************************/
12360
12361/************************************************************************
12362 * *
12363 * Reading/Writing Schemas *
12364 * *
12365 ************************************************************************/
12366
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012367#if 0 /* Will be enabled if it is clear what options are needed. */
12368/**
12369 * xmlSchemaParserCtxtSetOptions:
12370 * @ctxt: a schema parser context
12371 * @options: a combination of xmlSchemaParserOption
12372 *
12373 * Sets the options to be used during the parse.
12374 *
12375 * Returns 0 in case of success, -1 in case of an
12376 * API error.
12377 */
12378static int
12379xmlSchemaParserCtxtSetOptions(xmlSchemaParserCtxtPtr ctxt,
12380 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012381
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012382{
12383 int i;
12384
12385 if (ctxt == NULL)
12386 return (-1);
12387 /*
12388 * WARNING: Change the start value if adding to the
12389 * xmlSchemaParseOption.
12390 */
12391 for (i = 1; i < (int) sizeof(int) * 8; i++) {
12392 if (options & 1<<i) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012393 return (-1);
12394 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012395 }
12396 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012397 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012398}
12399
12400/**
12401 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012402 * @ctxt: a schema parser context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012403 *
12404 * Returns the option combination of the parser context.
12405 */
12406static int
12407xmlSchemaParserCtxtGetOptions(xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012408
12409{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012410 if (ctxt == NULL)
12411 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012412 else
12413 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012414}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012415#endif
12416
Daniel Veillard4255d502002-04-16 15:50:10 +000012417/**
12418 * xmlSchemaNewParserCtxt:
12419 * @URL: the location of the schema
12420 *
12421 * Create an XML Schemas parse context for that file/resource expected
12422 * to contain an XML Schemas file.
12423 *
12424 * Returns the parser context or NULL in case of error
12425 */
12426xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012427xmlSchemaNewParserCtxt(const char *URL)
12428{
Daniel Veillard4255d502002-04-16 15:50:10 +000012429 xmlSchemaParserCtxtPtr ret;
12430
12431 if (URL == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012432 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000012433
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012434 ret = xmlSchemaParserCtxtCreate();
12435 if (ret == NULL)
12436 return(NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012437 ret->dict = xmlDictCreate();
12438 ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012439 return (ret);
12440}
12441
12442/**
Daniel Veillard6045c902002-10-09 21:13:59 +000012443 * xmlSchemaNewMemParserCtxt:
12444 * @buffer: a pointer to a char array containing the schemas
12445 * @size: the size of the array
12446 *
12447 * Create an XML Schemas parse context for that memory buffer expected
12448 * to contain an XML Schemas file.
12449 *
12450 * Returns the parser context or NULL in case of error
12451 */
12452xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012453xmlSchemaNewMemParserCtxt(const char *buffer, int size)
12454{
Daniel Veillard6045c902002-10-09 21:13:59 +000012455 xmlSchemaParserCtxtPtr ret;
12456
12457 if ((buffer == NULL) || (size <= 0))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012458 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012459 ret = xmlSchemaParserCtxtCreate();
12460 if (ret == NULL)
12461 return(NULL);
Daniel Veillard6045c902002-10-09 21:13:59 +000012462 ret->buffer = buffer;
12463 ret->size = size;
Daniel Veillarddee23482008-04-11 12:58:43 +000012464 ret->dict = xmlDictCreate();
Daniel Veillard6045c902002-10-09 21:13:59 +000012465 return (ret);
12466}
12467
12468/**
Daniel Veillard9d751502003-10-29 13:21:47 +000012469 * xmlSchemaNewDocParserCtxt:
12470 * @doc: a preparsed document tree
12471 *
12472 * Create an XML Schemas parse context for that document.
12473 * NB. The document may be modified during the parsing process.
12474 *
12475 * Returns the parser context or NULL in case of error
12476 */
12477xmlSchemaParserCtxtPtr
12478xmlSchemaNewDocParserCtxt(xmlDocPtr doc)
12479{
12480 xmlSchemaParserCtxtPtr ret;
12481
12482 if (doc == NULL)
12483 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012484 ret = xmlSchemaParserCtxtCreate();
12485 if (ret == NULL)
12486 return(NULL);
Daniel Veillard9d751502003-10-29 13:21:47 +000012487 ret->doc = doc;
William M. Brackcf9eadf2003-12-25 13:24:05 +000012488 ret->dict = xmlDictCreate();
Daniel Veillarddda22c12004-01-24 08:31:30 +000012489 /* The application has responsibility for the document */
12490 ret->preserve = 1;
Daniel Veillard9d751502003-10-29 13:21:47 +000012491
12492 return (ret);
12493}
12494
12495/**
Daniel Veillard4255d502002-04-16 15:50:10 +000012496 * xmlSchemaFreeParserCtxt:
12497 * @ctxt: the schema parser context
12498 *
12499 * Free the resources associated to the schema parser context
12500 */
12501void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012502xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt)
12503{
Daniel Veillard4255d502002-04-16 15:50:10 +000012504 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012505 return;
Daniel Veillarddda22c12004-01-24 08:31:30 +000012506 if (ctxt->doc != NULL && !ctxt->preserve)
Daniel Veillarddee23482008-04-11 12:58:43 +000012507 xmlFreeDoc(ctxt->doc);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012508 if (ctxt->vctxt != NULL) {
12509 xmlSchemaFreeValidCtxt(ctxt->vctxt);
12510 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012511 if (ctxt->ownsConstructor && (ctxt->constructor != NULL)) {
12512 xmlSchemaConstructionCtxtFree(ctxt->constructor);
12513 ctxt->constructor = NULL;
12514 ctxt->ownsConstructor = 0;
12515 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012516 if (ctxt->attrProhibs != NULL)
12517 xmlSchemaItemListFree(ctxt->attrProhibs);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012518 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000012519 xmlFree(ctxt);
12520}
12521
12522/************************************************************************
12523 * *
12524 * Building the content models *
12525 * *
12526 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012527
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012528static void
12529xmlSchemaBuildContentModelForSubstGroup(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012530 xmlSchemaParticlePtr particle, int counter, xmlAutomataStatePtr end)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012531{
Daniel Veillarda980bef2005-07-18 21:34:03 +000012532 xmlAutomataStatePtr start, tmp;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012533 xmlSchemaElementPtr elemDecl, member;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012534 xmlSchemaSubstGroupPtr substGroup;
12535 int i;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012536
12537 elemDecl = (xmlSchemaElementPtr) particle->children;
12538 /*
12539 * Wrap the substitution group with a CHOICE.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012540 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012541 start = pctxt->state;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012542 if (end == NULL)
12543 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012544 substGroup = xmlSchemaSubstGroupGet(pctxt, elemDecl);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012545 if (substGroup == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012546 xmlSchemaPErr(pctxt, WXS_ITEM_NODE(particle),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012547 XML_SCHEMAP_INTERNAL,
12548 "Internal error: xmlSchemaBuildContentModelForSubstGroup, "
12549 "declaration is marked having a subst. group but none "
12550 "available.\n", elemDecl->name, NULL);
12551 return;
12552 }
Daniel Veillarda980bef2005-07-18 21:34:03 +000012553 if (counter >= 0) {
12554 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012555 * NOTE that we put the declaration in, even if it's abstract.
12556 * However, an error will be raised during *validation* if an element
12557 * information item shall be validated against an abstract element
12558 * declaration.
Daniel Veillarda980bef2005-07-18 21:34:03 +000012559 */
12560 tmp = xmlAutomataNewCountedTrans(pctxt->am, start, NULL, counter);
12561 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12562 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12563 /*
12564 * Add subst. group members.
12565 */
12566 for (i = 0; i < substGroup->members->nbItems; i++) {
12567 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12568 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12569 member->name, member->targetNamespace, member);
12570 }
12571 } else if (particle->maxOccurs == 1) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012572 /*
12573 * NOTE that we put the declaration in, even if it's abstract,
12574 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012575 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012576 xmlAutomataNewTransition2(pctxt->am,
12577 start, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012578 elemDecl->name, elemDecl->targetNamespace, elemDecl), end);
12579 /*
12580 * Add subst. group members.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012581 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012582 for (i = 0; i < substGroup->members->nbItems; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012583 member = (xmlSchemaElementPtr) substGroup->members->items[i];
Kasimier T. Buchcik92b394f2006-05-09 19:59:54 +000012584 /*
12585 * NOTE: This fixes bug #341150. xmlAutomataNewOnceTrans2()
12586 * was incorrectly used instead of xmlAutomataNewTransition2()
12587 * (seems like a copy&paste bug from the XML_SCHEMA_TYPE_ALL
12588 * section in xmlSchemaBuildAContentModel() ).
Daniel Veillarddee23482008-04-11 12:58:43 +000012589 * TODO: Check if xmlAutomataNewOnceTrans2() was instead
Kasimier T. Buchcik92b394f2006-05-09 19:59:54 +000012590 * intended for the above "counter" section originally. I.e.,
12591 * check xs:all with subst-groups.
12592 *
12593 * tmp = xmlAutomataNewOnceTrans2(pctxt->am, start, NULL,
12594 * member->name, member->targetNamespace,
12595 * 1, 1, member);
12596 */
12597 tmp = xmlAutomataNewTransition2(pctxt->am, start, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000012598 member->name, member->targetNamespace, member);
Daniel Veillarda980bef2005-07-18 21:34:03 +000012599 xmlAutomataNewEpsilon(pctxt->am, tmp, end);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012600 }
12601 } else {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012602 xmlAutomataStatePtr hop;
12603 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12604 UNBOUNDED : particle->maxOccurs - 1;
12605 int minOccurs = particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
12606
12607 counter =
12608 xmlAutomataNewCounter(pctxt->am, minOccurs,
12609 maxOccurs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012610 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012611
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012612 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012613 xmlAutomataNewTransition2(pctxt->am,
12614 start, NULL,
12615 elemDecl->name, elemDecl->targetNamespace, elemDecl),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012616 hop);
12617 /*
Daniel Veillarda980bef2005-07-18 21:34:03 +000012618 * Add subst. group members.
12619 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012620 for (i = 0; i < substGroup->members->nbItems; i++) {
12621 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12622 xmlAutomataNewEpsilon(pctxt->am,
12623 xmlAutomataNewTransition2(pctxt->am,
12624 start, NULL,
12625 member->name, member->targetNamespace, member),
12626 hop);
12627 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012628 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12629 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
12630 }
12631 if (particle->minOccurs == 0)
12632 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012633 pctxt->state = end;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012634}
12635
12636static void
12637xmlSchemaBuildContentModelForElement(xmlSchemaParserCtxtPtr ctxt,
12638 xmlSchemaParticlePtr particle)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012639{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012640 if (((xmlSchemaElementPtr) particle->children)->flags &
12641 XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012642 /*
12643 * Substitution groups.
12644 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012645 xmlSchemaBuildContentModelForSubstGroup(ctxt, particle, -1, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012646 } else {
12647 xmlSchemaElementPtr elemDecl;
12648 xmlAutomataStatePtr start;
12649
12650 elemDecl = (xmlSchemaElementPtr) particle->children;
12651
12652 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012653 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012654 if (particle->maxOccurs == 1) {
12655 start = ctxt->state;
12656 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012657 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12658 } else if ((particle->maxOccurs >= UNBOUNDED) &&
12659 (particle->minOccurs < 2)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012660 /* Special case. */
Daniel Veillarddee23482008-04-11 12:58:43 +000012661 start = ctxt->state;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012662 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000012663 elemDecl->name, elemDecl->targetNamespace, elemDecl);
Kasimier T. Buchcik7ca5aed2005-12-12 15:13:40 +000012664 ctxt->state = xmlAutomataNewTransition2(ctxt->am, ctxt->state, ctxt->state,
Daniel Veillarddee23482008-04-11 12:58:43 +000012665 elemDecl->name, elemDecl->targetNamespace, elemDecl);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012666 } else {
12667 int counter;
12668 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12669 UNBOUNDED : particle->maxOccurs - 1;
12670 int minOccurs = particle->minOccurs < 1 ?
12671 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012672
12673 start = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012674 counter = xmlAutomataNewCounter(ctxt->am, minOccurs, maxOccurs);
12675 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12676 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12677 xmlAutomataNewCountedTrans(ctxt->am, ctxt->state, start, counter);
12678 ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, ctxt->state,
12679 NULL, counter);
12680 }
12681 if (particle->minOccurs == 0)
12682 xmlAutomataNewEpsilon(ctxt->am, start, ctxt->state);
12683 }
12684}
12685
Daniel Veillard4255d502002-04-16 15:50:10 +000012686/**
12687 * xmlSchemaBuildAContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012688 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012689 * @particle: the particle component
12690 * @name: the complex type's name whose content is being built
Daniel Veillard4255d502002-04-16 15:50:10 +000012691 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012692 * Create the automaton for the {content type} of a complex type.
12693 *
Daniel Veillard4255d502002-04-16 15:50:10 +000012694 */
12695static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012696xmlSchemaBuildAContentModel(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012697 xmlSchemaParticlePtr particle)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012698{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012699 if (particle == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000012700 PERROR_INT("xmlSchemaBuildAContentModel", "particle is NULL");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012701 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012702 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012703 if (particle->children == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012704 /*
12705 * Just return in this case. A missing "term" of the particle
12706 * might arise due to an invalid "term" component.
12707 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012708 return;
12709 }
12710
12711 switch (particle->children->type) {
12712 case XML_SCHEMA_TYPE_ANY: {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012713 xmlAutomataStatePtr start, end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012714 xmlSchemaWildcardPtr wild;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012715 xmlSchemaWildcardNsPtr ns;
Daniel Veillard32370232002-10-16 14:08:14 +000012716
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012717 wild = (xmlSchemaWildcardPtr) particle->children;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012718
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012719 start = pctxt->state;
12720 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012721
12722 if (particle->maxOccurs == 1) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012723 if (wild->any == 1) {
12724 /*
12725 * We need to add both transitions:
12726 *
12727 * 1. the {"*", "*"} for elements in a namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012728 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012729 pctxt->state =
12730 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012731 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012732 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012733 /*
12734 * 2. the {"*"} for elements in no namespace.
12735 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012736 pctxt->state =
12737 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012738 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012739 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012740
12741 } else if (wild->nsSet != NULL) {
12742 ns = wild->nsSet;
12743 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012744 pctxt->state = start;
12745 pctxt->state = xmlAutomataNewTransition2(pctxt->am,
12746 pctxt->state, NULL, BAD_CAST "*", ns->value, wild);
12747 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012748 ns = ns->next;
12749 } while (ns != NULL);
12750
12751 } else if (wild->negNsSet != NULL) {
Daniel Veillard6e65e152005-08-09 11:09:52 +000012752 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
12753 start, end, BAD_CAST "*", wild->negNsSet->value,
12754 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012755 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000012756 } else {
12757 int counter;
12758 xmlAutomataStatePtr hop;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012759 int maxOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012760 particle->maxOccurs == UNBOUNDED ? UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012761 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012762 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012763
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012764 counter = xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12765 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012766 if (wild->any == 1) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012767 pctxt->state =
12768 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012769 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012770 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
12771 pctxt->state =
12772 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012773 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012774 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012775 } else if (wild->nsSet != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012776 ns = wild->nsSet;
12777 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012778 pctxt->state =
12779 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012780 start, NULL, BAD_CAST "*", ns->value, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012781 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012782 ns = ns->next;
12783 } while (ns != NULL);
12784
12785 } else if (wild->negNsSet != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012786 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
Daniel Veillard9efc4762005-07-19 14:33:55 +000012787 start, hop, BAD_CAST "*", wild->negNsSet->value,
12788 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012789 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012790 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12791 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012792 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012793 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012794 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012795 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012796 pctxt->state = end;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012797 break;
12798 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012799 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012800 xmlSchemaBuildContentModelForElement(pctxt, particle);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012801 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012802 case XML_SCHEMA_TYPE_SEQUENCE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012803 xmlSchemaTreeItemPtr sub;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012804
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012805 /*
12806 * If max and min occurances are default (1) then
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012807 * simply iterate over the particles of the <sequence>.
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012808 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012809 if ((particle->minOccurs == 1) && (particle->maxOccurs == 1)) {
12810 sub = particle->children->children;
12811 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012812 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012813 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012814 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012815 }
12816 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012817 xmlAutomataStatePtr oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012818
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012819 if (particle->maxOccurs >= UNBOUNDED) {
12820 if (particle->minOccurs > 1) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012821 xmlAutomataStatePtr tmp;
12822 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012823
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012824 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012825 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012826 oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012827
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012828 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012829 particle->minOccurs - 1, UNBOUNDED);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012830
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012831 sub = particle->children->children;
12832 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012833 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012834 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012835 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012836 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012837 tmp = pctxt->state;
12838 xmlAutomataNewCountedTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012839 oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012840 pctxt->state =
12841 xmlAutomataNewCounterTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012842 NULL, counter);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012843
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012844 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012845 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Daniel Veillardafc05b62005-07-17 06:11:19 +000012846 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012847 oldstate = pctxt->state;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012848
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012849 sub = particle->children->children;
12850 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012851 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012852 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012853 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012854 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012855 xmlAutomataNewEpsilon(pctxt->am, pctxt->state,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012856 oldstate);
Daniel Veillardafc05b62005-07-17 06:11:19 +000012857 /*
12858 * epsilon needed to block previous trans from
12859 * being allowed to enter back from another
12860 * construct
12861 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012862 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
12863 pctxt->state, NULL);
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 }
12868 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012869 } else if ((particle->maxOccurs > 1)
12870 || (particle->minOccurs > 1)) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012871 xmlAutomataStatePtr tmp;
12872 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012873
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012874 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012875 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012876 oldstate = pctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +000012877
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012878 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012879 particle->minOccurs - 1,
12880 particle->maxOccurs - 1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012881
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012882 sub = particle->children->children;
12883 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012884 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012885 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012886 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012887 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012888 tmp = pctxt->state;
12889 xmlAutomataNewCountedTrans(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012890 tmp, oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012891 pctxt->state =
12892 xmlAutomataNewCounterTrans(pctxt->am, tmp, NULL,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012893 counter);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012894 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012895 xmlAutomataNewEpsilon(pctxt->am,
12896 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012897 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012898 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012899 sub = particle->children->children;
12900 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012901 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012902 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012903 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012904 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012905 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012906 xmlAutomataNewEpsilon(pctxt->am, oldstate,
12907 pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012908 }
12909 }
12910 }
12911 break;
12912 }
12913 case XML_SCHEMA_TYPE_CHOICE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012914 xmlSchemaTreeItemPtr sub;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012915 xmlAutomataStatePtr start, end;
Daniel Veillardb509f152002-04-17 16:28:10 +000012916
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012917 start = pctxt->state;
12918 end = xmlAutomataNewState(pctxt->am);
Daniel Veillard7646b182002-04-20 06:41:40 +000012919
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012920 /*
12921 * iterate over the subtypes and remerge the end with an
12922 * epsilon transition
12923 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012924 if (particle->maxOccurs == 1) {
12925 sub = particle->children->children;
12926 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012927 pctxt->state = start;
12928 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012929 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012930 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012931 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012932 }
12933 } else {
12934 int counter;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012935 xmlAutomataStatePtr hop, base;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012936 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12937 UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012938 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012939 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Daniel Veillard7646b182002-04-20 06:41:40 +000012940
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012941 /*
12942 * use a counter to keep track of the number of transtions
12943 * which went through the choice.
12944 */
12945 counter =
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012946 xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12947 hop = xmlAutomataNewState(pctxt->am);
12948 base = xmlAutomataNewState(pctxt->am);
Daniel Veillard6231e842002-04-18 11:54:04 +000012949
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012950 sub = particle->children->children;
12951 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012952 pctxt->state = base;
12953 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012954 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012955 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012956 sub = sub->next;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012957 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012958 xmlAutomataNewEpsilon(pctxt->am, start, base);
12959 xmlAutomataNewCountedTrans(pctxt->am, hop, base, counter);
12960 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012961 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012962 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012963 xmlAutomataNewEpsilon(pctxt->am, start, end);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012964 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012965 pctxt->state = end;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012966 break;
12967 }
12968 case XML_SCHEMA_TYPE_ALL:{
Daniel Veillardbd56c442009-08-12 15:39:23 +020012969 xmlAutomataStatePtr start, tmp;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012970 xmlSchemaParticlePtr sub;
12971 xmlSchemaElementPtr elemDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012972
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012973 sub = (xmlSchemaParticlePtr) particle->children->children;
12974 if (sub == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012975 break;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012976 start = pctxt->state;
Daniel Veillardbd56c442009-08-12 15:39:23 +020012977 tmp = xmlAutomataNewState(pctxt->am);
12978 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, tmp);
12979 pctxt->state = tmp;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012980 while (sub != NULL) {
Daniel Veillardbd56c442009-08-12 15:39:23 +020012981 pctxt->state = tmp;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012982
12983 elemDecl = (xmlSchemaElementPtr) sub->children;
12984 if (elemDecl == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012985 PERROR_INT("xmlSchemaBuildAContentModel",
12986 "<element> particle has no term");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012987 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012988 };
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012989 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012990 * NOTE: The {max occurs} of all the particles in the
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +000012991 * {particles} of the group must be 0 or 1; this is
12992 * already ensured during the parse of the content of
12993 * <all>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012994 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012995 if (elemDecl->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
12996 int counter;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012997
Daniel Veillarda980bef2005-07-18 21:34:03 +000012998 /*
12999 * This is an abstract group, we need to share
13000 * the same counter for all the element transitions
13001 * derived from the group
13002 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013003 counter = xmlAutomataNewCounter(pctxt->am,
Daniel Veillarda980bef2005-07-18 21:34:03 +000013004 sub->minOccurs, sub->maxOccurs);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013005 xmlSchemaBuildContentModelForSubstGroup(pctxt,
13006 sub, counter, pctxt->state);
Daniel Veillarda980bef2005-07-18 21:34:03 +000013007 } else {
13008 if ((sub->minOccurs == 1) &&
13009 (sub->maxOccurs == 1)) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013010 xmlAutomataNewOnceTrans2(pctxt->am, pctxt->state,
13011 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000013012 elemDecl->name,
13013 elemDecl->targetNamespace,
13014 1, 1, elemDecl);
13015 } else if ((sub->minOccurs == 0) &&
13016 (sub->maxOccurs == 1)) {
13017
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013018 xmlAutomataNewCountTrans2(pctxt->am, pctxt->state,
13019 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000013020 elemDecl->name,
13021 elemDecl->targetNamespace,
13022 0,
13023 1,
13024 elemDecl);
13025 }
13026 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013027 sub = (xmlSchemaParticlePtr) sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013028 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013029 pctxt->state =
Daniel Veillardbd56c442009-08-12 15:39:23 +020013030 xmlAutomataNewAllTrans(pctxt->am, pctxt->state, NULL, 0);
13031 if (particle->minOccurs == 0) {
13032 xmlAutomataNewEpsilon(pctxt->am, start, pctxt->state);
13033 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013034 break;
13035 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013036 case XML_SCHEMA_TYPE_GROUP:
13037 /*
13038 * If we hit a model group definition, then this means that
13039 * it was empty, thus was not substituted for the containing
13040 * model group. Just do nothing in this case.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013041 * TODO: But the group should be substituted and not occur at
13042 * all in the content model at this point. Fix this.
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013043 */
13044 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013045 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013046 xmlSchemaInternalErr2(ACTXT_CAST pctxt,
13047 "xmlSchemaBuildAContentModel",
13048 "found unexpected term of type '%s' in content model",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013049 WXS_ITEM_TYPE_NAME(particle->children), NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013050 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000013051 }
13052}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013053
Daniel Veillard4255d502002-04-16 15:50:10 +000013054/**
13055 * xmlSchemaBuildContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000013056 * @ctxt: the schema parser context
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013057 * @type: the complex type definition
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013058 * @name: the element name
Daniel Veillard4255d502002-04-16 15:50:10 +000013059 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013060 * Builds the content model of the complex type.
Daniel Veillard4255d502002-04-16 15:50:10 +000013061 */
13062static void
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013063xmlSchemaBuildContentModel(xmlSchemaTypePtr type,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013064 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013065{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013066 if ((type->type != XML_SCHEMA_TYPE_COMPLEX) ||
13067 (type->contModel != NULL) ||
13068 ((type->contentType != XML_SCHEMA_CONTENT_ELEMENTS) &&
13069 (type->contentType != XML_SCHEMA_CONTENT_MIXED)))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013070 return;
Daniel Veillarddecd64d2002-04-18 14:41:51 +000013071
13072#ifdef DEBUG_CONTENT
13073 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013074 "Building content model for %s\n", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000013075#endif
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013076 ctxt->am = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013077 ctxt->am = xmlNewAutomata();
13078 if (ctxt->am == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013079 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013080 "Cannot create automata for complex type %s\n", type->name);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013081 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000013082 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013083 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013084 /*
13085 * Build the automaton.
13086 */
13087 xmlSchemaBuildAContentModel(ctxt, WXS_TYPE_PARTICLE(type));
Daniel Veillard4255d502002-04-16 15:50:10 +000013088 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013089 type->contModel = xmlAutomataCompile(ctxt->am);
13090 if (type->contModel == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013091 xmlSchemaPCustomErr(ctxt,
13092 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013093 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013094 "Failed to compile the content model", NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013095 } else if (xmlRegexpIsDeterminist(type->contModel) != 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013096 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000013097 XML_SCHEMAP_NOT_DETERMINISTIC,
13098 /* XML_SCHEMAS_ERR_NOTDETERMINIST, */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013099 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013100 "The content model is not determinist", NULL);
Daniel Veillarde19fc232002-04-22 16:01:24 +000013101 } else {
Daniel Veillard118aed72002-09-24 14:13:13 +000013102#ifdef DEBUG_CONTENT_REGEXP
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013103 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013104 "Content model of %s:\n", type->name);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013105 xmlRegexpPrint(stderr, type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +000013106#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +000013107 }
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013108 ctxt->state = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013109 xmlFreeAutomata(ctxt->am);
13110 ctxt->am = NULL;
13111}
13112
13113/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013114 * xmlSchemaResolveElementReferences:
Daniel Veillard4255d502002-04-16 15:50:10 +000013115 * @elem: the schema element context
13116 * @ctxt: the schema parser context
13117 *
Daniel Veillardc0826a72004-08-10 14:17:33 +000013118 * Resolves the references of an element declaration
13119 * or particle, which has an element declaration as it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013120 * term.
Daniel Veillard4255d502002-04-16 15:50:10 +000013121 */
13122static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013123xmlSchemaResolveElementReferences(xmlSchemaElementPtr elemDecl,
13124 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000013125{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013126 if ((ctxt == NULL) || (elemDecl == NULL) ||
13127 ((elemDecl != NULL) &&
13128 (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_RESOLVED)))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013129 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013130 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_RESOLVED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013131
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013132 if ((elemDecl->subtypes == NULL) && (elemDecl->namedType != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013133 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013134
Rob Richardsc6947bb2008-06-29 15:04:41 +000013135 /* (type definition) ... otherwise the type definition �resolved�
13136 * to by the �actual value� of the type [attribute] ...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013137 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013138 type = xmlSchemaGetType(ctxt->schema, elemDecl->namedType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013139 elemDecl->namedTypeNs);
13140 if (type == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013141 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013142 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013143 WXS_BASIC_CAST elemDecl, elemDecl->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013144 "type", elemDecl->namedType, elemDecl->namedTypeNs,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013145 XML_SCHEMA_TYPE_BASIC, "type definition");
13146 } else
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013147 elemDecl->subtypes = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013148 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013149 if (elemDecl->substGroup != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013150 xmlSchemaElementPtr substHead;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013151
Daniel Veillardc0826a72004-08-10 14:17:33 +000013152 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013153 * FIXME TODO: Do we need a new field in _xmlSchemaElement for
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013154 * substitutionGroup?
Daniel Veillard01fa6152004-06-29 17:04:39 +000013155 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013156 substHead = xmlSchemaGetElem(ctxt->schema, elemDecl->substGroup,
13157 elemDecl->substGroupNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013158 if (substHead == NULL) {
13159 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013160 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013161 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013162 "substitutionGroup", elemDecl->substGroup,
13163 elemDecl->substGroupNs, XML_SCHEMA_TYPE_ELEMENT, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013164 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013165 xmlSchemaResolveElementReferences(substHead, ctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013166 /*
13167 * Set the "substitution group affiliation".
13168 * NOTE that now we use the "refDecl" field for this.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013169 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013170 WXS_SUBST_HEAD(elemDecl) = substHead;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013171 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013172 * The type definitions is set to:
13173 * SPEC "...the {type definition} of the element
Rob Richardsc6947bb2008-06-29 15:04:41 +000013174 * declaration �resolved� to by the �actual value�
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013175 * of the substitutionGroup [attribute], if present"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013176 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013177 if (elemDecl->subtypes == NULL)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013178 elemDecl->subtypes = substHead->subtypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013179 }
13180 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013181 /*
13182 * SPEC "The definition of anyType serves as the default type definition
13183 * for element declarations whose XML representation does not specify one."
13184 */
13185 if ((elemDecl->subtypes == NULL) &&
13186 (elemDecl->namedType == NULL) &&
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013187 (elemDecl->substGroup == NULL))
13188 elemDecl->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
William M. Bracke7091952004-05-11 15:09:58 +000013189}
13190
13191/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013192 * xmlSchemaResolveUnionMemberTypes:
William M. Bracke7091952004-05-11 15:09:58 +000013193 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013194 * @type: the schema simple type definition
William M. Bracke7091952004-05-11 15:09:58 +000013195 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013196 * Checks and builds the "member type definitions" property of the union
13197 * simple type. This handles part (1), part (2) is done in
13198 * xmlSchemaFinishMemberTypeDefinitionsProperty()
13199 *
Daniel Veillard01fa6152004-06-29 17:04:39 +000013200 * Returns -1 in case of an internal error, 0 otherwise.
William M. Bracke7091952004-05-11 15:09:58 +000013201 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013202static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013203xmlSchemaResolveUnionMemberTypes(xmlSchemaParserCtxtPtr ctxt,
13204 xmlSchemaTypePtr type)
Daniel Veillard377e1a92004-04-16 16:30:05 +000013205{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013206
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013207 xmlSchemaTypeLinkPtr link, lastLink, newLink;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013208 xmlSchemaTypePtr memberType;
Daniel Veillard377e1a92004-04-16 16:30:05 +000013209
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013210 /*
13211 * SPEC (1) "If the <union> alternative is chosen, then [Definition:]
Rob Richardsc6947bb2008-06-29 15:04:41 +000013212 * define the explicit members as the type definitions �resolved�
13213 * to by the items in the �actual value� of the memberTypes [attribute],
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013214 * if any, followed by the type definitions corresponding to the
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013215 * <simpleType>s among the [children] of <union>, if any."
Daniel Veillard01fa6152004-06-29 17:04:39 +000013216 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013217 /*
13218 * Resolve references.
13219 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013220 link = type->memberTypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013221 lastLink = NULL;
13222 while (link != NULL) {
13223 const xmlChar *name, *nsName;
Daniel Veillardc0826a72004-08-10 14:17:33 +000013224
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013225 name = ((xmlSchemaQNameRefPtr) link->type)->name;
13226 nsName = ((xmlSchemaQNameRefPtr) link->type)->targetNamespace;
13227
13228 memberType = xmlSchemaGetType(ctxt->schema, name, nsName);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013229 if ((memberType == NULL) || (! WXS_IS_SIMPLE(memberType))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013230 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013231 WXS_BASIC_CAST type, type->node, "memberTypes",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013232 name, nsName, XML_SCHEMA_TYPE_SIMPLE, NULL);
13233 /*
13234 * Remove the member type link.
13235 */
13236 if (lastLink == NULL)
13237 type->memberTypes = link->next;
13238 else
13239 lastLink->next = link->next;
13240 newLink = link;
13241 link = link->next;
13242 xmlFree(newLink);
13243 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013244 link->type = memberType;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013245 lastLink = link;
13246 link = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013247 }
13248 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013249 /*
13250 * Add local simple types,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013251 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013252 memberType = type->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013253 while (memberType != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013254 link = (xmlSchemaTypeLinkPtr) xmlMalloc(sizeof(xmlSchemaTypeLink));
13255 if (link == NULL) {
13256 xmlSchemaPErrMemory(ctxt, "allocating a type link", NULL);
13257 return (-1);
13258 }
13259 link->type = memberType;
13260 link->next = NULL;
13261 if (lastLink == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013262 type->memberTypes = link;
13263 else
Daniel Veillard01fa6152004-06-29 17:04:39 +000013264 lastLink->next = link;
13265 lastLink = link;
13266 memberType = memberType->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013267 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013268 return (0);
Daniel Veillard377e1a92004-04-16 16:30:05 +000013269}
13270
Daniel Veillard4255d502002-04-16 15:50:10 +000013271/**
Daniel Veillard3646d642004-06-02 19:19:14 +000013272 * xmlSchemaIsDerivedFromBuiltInType:
13273 * @ctxt: the schema parser context
13274 * @type: the type definition
13275 * @valType: the value type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013276 *
Daniel Veillard3646d642004-06-02 19:19:14 +000013277 *
13278 * Returns 1 if the type has the given value type, or
13279 * is derived from such a type.
13280 */
William M. Brack803812b2004-06-03 02:11:24 +000013281static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013282xmlSchemaIsDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
Daniel Veillard3646d642004-06-02 19:19:14 +000013283{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013284 if (type == NULL)
13285 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013286 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013287 return (0);
13288 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13289 if (type->builtInType == valType)
13290 return(1);
13291 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13292 (type->builtInType == XML_SCHEMAS_ANYTYPE))
13293 return (0);
13294 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard6a0baa02005-12-10 11:11:12 +000013295 }
13296 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard3646d642004-06-02 19:19:14 +000013297}
13298
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013299#if 0
13300/**
13301 * xmlSchemaIsDerivedFromBuiltInType:
13302 * @ctxt: the schema parser context
13303 * @type: the type definition
13304 * @valType: the value type
13305 *
13306 *
13307 * Returns 1 if the type has the given value type, or
13308 * is derived from such a type.
13309 */
13310static int
13311xmlSchemaIsUserDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
13312{
13313 if (type == NULL)
13314 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013315 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013316 return (0);
13317 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13318 if (type->builtInType == valType)
13319 return(1);
13320 return (0);
13321 } else
13322 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13323
13324 return (0);
13325}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013326
13327static xmlSchemaTypePtr
13328xmlSchemaQueryBuiltInType(xmlSchemaTypePtr type)
13329{
13330 if (type == NULL)
13331 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013332 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013333 return (NULL);
13334 if (type->type == XML_SCHEMA_TYPE_BASIC)
Daniel Veillard6a0baa02005-12-10 11:11:12 +000013335 return(type);
13336 return(xmlSchemaQueryBuiltInType(type->subtypes));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013337}
Daniel Veillard15724252008-08-30 15:01:04 +000013338#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013339
Daniel Veillard3646d642004-06-02 19:19:14 +000013340/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013341 * xmlSchemaGetPrimitiveType:
Daniel Veillard01fa6152004-06-29 17:04:39 +000013342 * @type: the simpleType definition
13343 *
13344 * Returns the primitive type of the given type or
13345 * NULL in case of error.
13346 */
13347static xmlSchemaTypePtr
13348xmlSchemaGetPrimitiveType(xmlSchemaTypePtr type)
13349{
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013350
Daniel Veillard01fa6152004-06-29 17:04:39 +000013351 while (type != NULL) {
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013352 /*
13353 * Note that anySimpleType is actually not a primitive type
13354 * but we need that here.
13355 */
13356 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13357 (type->flags & XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE))
Daniel Veillard01fa6152004-06-29 17:04:39 +000013358 return (type);
13359 type = type->baseType;
13360 }
13361
13362 return (NULL);
13363}
13364
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013365#if 0
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013366/**
13367 * xmlSchemaGetBuiltInTypeAncestor:
13368 * @type: the simpleType definition
13369 *
13370 * Returns the primitive type of the given type or
13371 * NULL in case of error.
13372 */
13373static xmlSchemaTypePtr
13374xmlSchemaGetBuiltInTypeAncestor(xmlSchemaTypePtr type)
13375{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013376 if (WXS_IS_LIST(type) || WXS_IS_UNION(type))
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000013377 return (0);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013378 while (type != NULL) {
13379 if (type->type == XML_SCHEMA_TYPE_BASIC)
13380 return (type);
13381 type = type->baseType;
13382 }
13383
13384 return (NULL);
13385}
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013386#endif
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013387
Daniel Veillard01fa6152004-06-29 17:04:39 +000013388/**
Daniel Veillard50355f02004-06-08 17:52:16 +000013389 * xmlSchemaCloneWildcardNsConstraints:
13390 * @ctxt: the schema parser context
13391 * @dest: the destination wildcard
13392 * @source: the source wildcard
13393 *
13394 * Clones the namespace constraints of source
13395 * and assignes them to dest.
13396 * Returns -1 on internal error, 0 otherwise.
13397 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013398static int
13399xmlSchemaCloneWildcardNsConstraints(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013400 xmlSchemaWildcardPtr dest,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013401 xmlSchemaWildcardPtr source)
Daniel Veillard3646d642004-06-02 19:19:14 +000013402{
13403 xmlSchemaWildcardNsPtr cur, tmp, last;
13404
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013405 if ((source == NULL) || (dest == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013406 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013407 dest->any = source->any;
Daniel Veillard3646d642004-06-02 19:19:14 +000013408 cur = source->nsSet;
13409 last = NULL;
13410 while (cur != NULL) {
13411 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
13412 if (tmp == NULL)
13413 return(-1);
13414 tmp->value = cur->value;
13415 if (last == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013416 dest->nsSet = tmp;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013417 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013418 last->next = tmp;
13419 last = tmp;
13420 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013421 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013422 if (dest->negNsSet != NULL)
13423 xmlSchemaFreeWildcardNsSet(dest->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +000013424 if (source->negNsSet != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013425 dest->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13426 if (dest->negNsSet == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013427 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013428 dest->negNsSet->value = source->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013429 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013430 dest->negNsSet = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000013431 return(0);
13432}
13433
Daniel Veillard50355f02004-06-08 17:52:16 +000013434/**
13435 * xmlSchemaUnionWildcards:
13436 * @ctxt: the schema parser context
13437 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013438 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013439 *
13440 * Unions the namespace constraints of the given wildcards.
13441 * @completeWild will hold the resulting union.
13442 * Returns a positive error code on failure, -1 in case of an
13443 * internal error, 0 otherwise.
13444 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013445static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013446xmlSchemaUnionWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013447 xmlSchemaWildcardPtr completeWild,
13448 xmlSchemaWildcardPtr curWild)
13449{
13450 xmlSchemaWildcardNsPtr cur, curB, tmp;
13451
13452 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013453 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013454 * value.
13455 */
13456 if ((completeWild->any == curWild->any) &&
13457 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13458 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013459
Daniel Veillard3646d642004-06-02 19:19:14 +000013460 if ((completeWild->negNsSet == NULL) ||
13461 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013462
Daniel Veillard3646d642004-06-02 19:19:14 +000013463 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013464 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013465
13466 /*
13467 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013468 */
13469 cur = completeWild->nsSet;
13470 while (cur != NULL) {
13471 found = 0;
13472 curB = curWild->nsSet;
13473 while (curB != NULL) {
13474 if (cur->value == curB->value) {
13475 found = 1;
13476 break;
13477 }
13478 curB = curB->next;
13479 }
13480 if (!found)
13481 break;
13482 cur = cur->next;
13483 }
13484 if (found)
13485 return(0);
13486 } else
13487 return(0);
13488 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013489 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013490 /*
13491 * 2 If either O1 or O2 is any, then any must be the value
13492 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013493 if (completeWild->any != curWild->any) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013494 if (completeWild->any == 0) {
13495 completeWild->any = 1;
13496 if (completeWild->nsSet != NULL) {
13497 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13498 completeWild->nsSet = NULL;
13499 }
13500 if (completeWild->negNsSet != NULL) {
13501 xmlFree(completeWild->negNsSet);
13502 completeWild->negNsSet = NULL;
13503 }
13504 }
Daniel Veillard50355f02004-06-08 17:52:16 +000013505 return (0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013506 }
13507 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000013508 * 3 If both O1 and O2 are sets of (namespace names or �absent�),
Daniel Veillard3646d642004-06-02 19:19:14 +000013509 * then the union of those sets must be the value.
13510 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013511 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013512 int found;
13513 xmlSchemaWildcardNsPtr start;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013514
Daniel Veillard3646d642004-06-02 19:19:14 +000013515 cur = curWild->nsSet;
13516 start = completeWild->nsSet;
13517 while (cur != NULL) {
13518 found = 0;
13519 curB = start;
13520 while (curB != NULL) {
13521 if (cur->value == curB->value) {
13522 found = 1;
13523 break;
13524 }
13525 curB = curB->next;
13526 }
13527 if (!found) {
13528 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013529 if (tmp == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013530 return (-1);
13531 tmp->value = cur->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013532 tmp->next = completeWild->nsSet;
Daniel Veillard3646d642004-06-02 19:19:14 +000013533 completeWild->nsSet = tmp;
13534 }
13535 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013536 }
13537
Daniel Veillard3646d642004-06-02 19:19:14 +000013538 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013539 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013540 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013541 * 4 If the two are negations of different values (namespace names
Rob Richardsc6947bb2008-06-29 15:04:41 +000013542 * or �absent�), then a pair of not and �absent� must be the value.
Daniel Veillard3646d642004-06-02 19:19:14 +000013543 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013544 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013545 (curWild->negNsSet != NULL) &&
13546 (completeWild->negNsSet->value != curWild->negNsSet->value)) {
13547 completeWild->negNsSet->value = NULL;
Daniel Veillard50355f02004-06-08 17:52:16 +000013548
13549 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013550 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013551 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013552 * 5.
13553 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013554 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013555 (completeWild->negNsSet->value != NULL) &&
13556 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013557 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013558 (curWild->negNsSet->value != NULL) &&
13559 (completeWild->nsSet != NULL))) {
13560
13561 int nsFound, absentFound = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013562
Daniel Veillard3646d642004-06-02 19:19:14 +000013563 if (completeWild->nsSet != NULL) {
13564 cur = completeWild->nsSet;
13565 curB = curWild->negNsSet;
13566 } else {
13567 cur = curWild->nsSet;
13568 curB = completeWild->negNsSet;
13569 }
13570 nsFound = 0;
13571 while (cur != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013572 if (cur->value == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013573 absentFound = 1;
13574 else if (cur->value == curB->value)
13575 nsFound = 1;
13576 if (nsFound && absentFound)
13577 break;
13578 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013579 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013580
13581 if (nsFound && absentFound) {
13582 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013583 * 5.1 If the set S includes both the negated namespace
Rob Richardsc6947bb2008-06-29 15:04:41 +000013584 * name and �absent�, then any must be the value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013585 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013586 completeWild->any = 1;
13587 if (completeWild->nsSet != NULL) {
13588 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13589 completeWild->nsSet = NULL;
13590 }
13591 if (completeWild->negNsSet != NULL) {
13592 xmlFree(completeWild->negNsSet);
13593 completeWild->negNsSet = NULL;
13594 }
13595 } else if (nsFound && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013596 /*
13597 * 5.2 If the set S includes the negated namespace name
Rob Richardsc6947bb2008-06-29 15:04:41 +000013598 * but not �absent�, then a pair of not and �absent� must
Daniel Veillard3646d642004-06-02 19:19:14 +000013599 * be the value.
13600 */
13601 if (completeWild->nsSet != NULL) {
13602 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13603 completeWild->nsSet = NULL;
13604 }
13605 if (completeWild->negNsSet == NULL) {
13606 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13607 if (completeWild->negNsSet == NULL)
13608 return (-1);
13609 }
13610 completeWild->negNsSet->value = NULL;
13611 } else if ((!nsFound) && absentFound) {
13612 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000013613 * 5.3 If the set S includes �absent� but not the negated
Daniel Veillard3646d642004-06-02 19:19:14 +000013614 * namespace name, then the union is not expressible.
13615 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013616 xmlSchemaPErr(ctxt, completeWild->node,
Daniel Veillard3646d642004-06-02 19:19:14 +000013617 XML_SCHEMAP_UNION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013618 "The union of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013619 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013620 return(XML_SCHEMAP_UNION_NOT_EXPRESSIBLE);
Daniel Veillard3646d642004-06-02 19:19:14 +000013621 } else if ((!nsFound) && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013622 /*
13623 * 5.4 If the set S does not include either the negated namespace
Rob Richardsc6947bb2008-06-29 15:04:41 +000013624 * name or �absent�, then whichever of O1 or O2 is a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013625 * and a namespace name must be the value.
13626 */
13627 if (completeWild->negNsSet == NULL) {
13628 if (completeWild->nsSet != NULL) {
13629 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13630 completeWild->nsSet = NULL;
13631 }
13632 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13633 if (completeWild->negNsSet == NULL)
13634 return (-1);
13635 completeWild->negNsSet->value = curWild->negNsSet->value;
13636 }
13637 }
13638 return (0);
13639 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013640 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013641 * 6.
13642 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013643 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013644 (completeWild->negNsSet->value == NULL) &&
13645 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013646 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013647 (curWild->negNsSet->value == NULL) &&
13648 (completeWild->nsSet != NULL))) {
13649
13650 if (completeWild->nsSet != NULL) {
13651 cur = completeWild->nsSet;
13652 } else {
13653 cur = curWild->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013654 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013655 while (cur != NULL) {
13656 if (cur->value == NULL) {
13657 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000013658 * 6.1 If the set S includes �absent�, then any must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013659 * value.
13660 */
13661 completeWild->any = 1;
13662 if (completeWild->nsSet != NULL) {
13663 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13664 completeWild->nsSet = NULL;
13665 }
13666 if (completeWild->negNsSet != NULL) {
13667 xmlFree(completeWild->negNsSet);
13668 completeWild->negNsSet = NULL;
13669 }
13670 return (0);
13671 }
13672 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013673 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013674 if (completeWild->negNsSet == NULL) {
13675 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000013676 * 6.2 If the set S does not include �absent�, then a pair of not
13677 * and �absent� must be the value.
Daniel Veillard3646d642004-06-02 19:19:14 +000013678 */
13679 if (completeWild->nsSet != NULL) {
13680 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13681 completeWild->nsSet = NULL;
13682 }
13683 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13684 if (completeWild->negNsSet == NULL)
13685 return (-1);
13686 completeWild->negNsSet->value = NULL;
13687 }
13688 return (0);
13689 }
13690 return (0);
13691
13692}
13693
Daniel Veillard50355f02004-06-08 17:52:16 +000013694/**
13695 * xmlSchemaIntersectWildcards:
13696 * @ctxt: the schema parser context
13697 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013698 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013699 *
13700 * Intersects the namespace constraints of the given wildcards.
13701 * @completeWild will hold the resulting intersection.
13702 * Returns a positive error code on failure, -1 in case of an
13703 * internal error, 0 otherwise.
13704 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013705static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013706xmlSchemaIntersectWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013707 xmlSchemaWildcardPtr completeWild,
13708 xmlSchemaWildcardPtr curWild)
13709{
William M. Brack803812b2004-06-03 02:11:24 +000013710 xmlSchemaWildcardNsPtr cur, curB, prev, tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013711
13712 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013713 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013714 * value.
13715 */
13716 if ((completeWild->any == curWild->any) &&
13717 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13718 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013719
Daniel Veillard3646d642004-06-02 19:19:14 +000013720 if ((completeWild->negNsSet == NULL) ||
13721 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013722
Daniel Veillard3646d642004-06-02 19:19:14 +000013723 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013724 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013725
13726 /*
13727 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013728 */
13729 cur = completeWild->nsSet;
13730 while (cur != NULL) {
13731 found = 0;
13732 curB = curWild->nsSet;
13733 while (curB != NULL) {
13734 if (cur->value == curB->value) {
13735 found = 1;
13736 break;
13737 }
13738 curB = curB->next;
13739 }
13740 if (!found)
13741 break;
13742 cur = cur->next;
13743 }
13744 if (found)
13745 return(0);
13746 } else
13747 return(0);
13748 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013749 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013750 /*
13751 * 2 If either O1 or O2 is any, then the other must be the value.
13752 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013753 if ((completeWild->any != curWild->any) && (completeWild->any)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013754 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013755 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013756 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013757 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013758 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013759 * 3 If either O1 or O2 is a pair of not and a value (a namespace
Rob Richardsc6947bb2008-06-29 15:04:41 +000013760 * name or �absent�) and the other is a set of (namespace names or
13761 * �absent�), then that set, minus the negated value if it was in
13762 * the set, minus �absent� if it was in the set, must be the value.
Daniel Veillard3646d642004-06-02 19:19:14 +000013763 */
13764 if (((completeWild->negNsSet != NULL) && (curWild->nsSet != NULL)) ||
13765 ((curWild->negNsSet != NULL) && (completeWild->nsSet != NULL))) {
13766 const xmlChar *neg;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013767
Daniel Veillard3646d642004-06-02 19:19:14 +000013768 if (completeWild->nsSet == NULL) {
13769 neg = completeWild->negNsSet->value;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013770 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Daniel Veillard3646d642004-06-02 19:19:14 +000013771 return(-1);
13772 } else
13773 neg = curWild->negNsSet->value;
13774 /*
13775 * Remove absent and negated.
13776 */
13777 prev = NULL;
13778 cur = completeWild->nsSet;
13779 while (cur != NULL) {
13780 if (cur->value == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013781 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013782 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013783 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013784 prev->next = cur->next;
13785 xmlFree(cur);
13786 break;
13787 }
13788 prev = cur;
13789 cur = cur->next;
13790 }
13791 if (neg != NULL) {
13792 prev = NULL;
13793 cur = completeWild->nsSet;
13794 while (cur != NULL) {
13795 if (cur->value == neg) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013796 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013797 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 xmlFree(cur);
13801 break;
13802 }
13803 prev = cur;
13804 cur = cur->next;
13805 }
13806 }
13807
13808 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013809 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013810 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000013811 * 4 If both O1 and O2 are sets of (namespace names or �absent�),
Daniel Veillard3646d642004-06-02 19:19:14 +000013812 * then the intersection of those sets must be the value.
13813 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013814 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013815 int found;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013816
Daniel Veillard3646d642004-06-02 19:19:14 +000013817 cur = completeWild->nsSet;
13818 prev = NULL;
13819 while (cur != NULL) {
13820 found = 0;
13821 curB = curWild->nsSet;
13822 while (curB != NULL) {
13823 if (cur->value == curB->value) {
13824 found = 1;
13825 break;
13826 }
13827 curB = curB->next;
13828 }
13829 if (!found) {
13830 if (prev == NULL)
13831 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013832 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013833 prev->next = cur->next;
13834 tmp = cur->next;
13835 xmlFree(cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013836 cur = tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013837 continue;
13838 }
13839 prev = cur;
13840 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013841 }
13842
Daniel Veillard3646d642004-06-02 19:19:14 +000013843 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013844 }
13845 /* 5 If the two are negations of different namespace names,
Daniel Veillard3646d642004-06-02 19:19:14 +000013846 * then the intersection is not expressible
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013847 */
13848 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013849 (curWild->negNsSet != NULL) &&
13850 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013851 (completeWild->negNsSet->value != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013852 (curWild->negNsSet->value != NULL)) {
13853
13854 xmlSchemaPErr(ctxt, completeWild->node, XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013855 "The intersection of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013856 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013857 return(XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013858 }
13859 /*
13860 * 6 If the one is a negation of a namespace name and the other
Rob Richardsc6947bb2008-06-29 15:04:41 +000013861 * is a negation of �absent�, then the one which is the negation
Daniel Veillard3646d642004-06-02 19:19:14 +000013862 * of a namespace name must be the value.
13863 */
13864 if ((completeWild->negNsSet != NULL) && (curWild->negNsSet != NULL) &&
13865 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013866 (completeWild->negNsSet->value == NULL)) {
13867 completeWild->negNsSet->value = curWild->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013868 }
13869 return(0);
13870}
13871
Daniel Veillard50355f02004-06-08 17:52:16 +000013872/**
13873 * xmlSchemaIsWildcardNsConstraintSubset:
13874 * @ctxt: the schema parser context
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013875 * @sub: the first wildcard
13876 * @super: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013877 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013878 * Schema Component Constraint: Wildcard Subset (cos-ns-subset)
13879 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013880 * Returns 0 if the namespace constraint of @sub is an intensional
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013881 * subset of @super, 1 otherwise.
Daniel Veillard50355f02004-06-08 17:52:16 +000013882 */
13883static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013884xmlSchemaCheckCOSNSSubset(xmlSchemaWildcardPtr sub,
13885 xmlSchemaWildcardPtr super)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013886{
Daniel Veillard50355f02004-06-08 17:52:16 +000013887 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013888 * 1 super must be any.
Daniel Veillard50355f02004-06-08 17:52:16 +000013889 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013890 if (super->any)
13891 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013892 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000013893 * 2.1 sub must be a pair of not and a namespace name or �absent�.
Daniel Veillard50355f02004-06-08 17:52:16 +000013894 * 2.2 super must be a pair of not and the same value.
13895 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013896 if ((sub->negNsSet != NULL) &&
13897 (super->negNsSet != NULL) &&
13898 (sub->negNsSet->value == sub->negNsSet->value))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013899 return (0);
13900 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000013901 * 3.1 sub must be a set whose members are either namespace names or �absent�.
Daniel Veillard50355f02004-06-08 17:52:16 +000013902 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013903 if (sub->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013904 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013905 * 3.2.1 super must be the same set or a superset thereof.
Daniel Veillard50355f02004-06-08 17:52:16 +000013906 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013907 if (super->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013908 xmlSchemaWildcardNsPtr cur, curB;
13909 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013910
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013911 cur = sub->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013912 while (cur != NULL) {
13913 found = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013914 curB = super->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013915 while (curB != NULL) {
13916 if (cur->value == curB->value) {
13917 found = 1;
13918 break;
13919 }
13920 curB = curB->next;
13921 }
13922 if (!found)
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013923 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013924 cur = cur->next;
13925 }
13926 if (found)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013927 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013928 } else if (super->negNsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013929 xmlSchemaWildcardNsPtr cur;
13930 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013931 * 3.2.2 super must be a pair of not and a namespace name or
Rob Richardsc6947bb2008-06-29 15:04:41 +000013932 * �absent� and that value must not be in sub's set.
Daniel Veillard50355f02004-06-08 17:52:16 +000013933 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013934 cur = sub->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013935 while (cur != NULL) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013936 if (cur->value == super->negNsSet->value)
13937 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013938 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013939 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013940 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013941 }
13942 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013943 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013944}
13945
Daniel Veillard50355f02004-06-08 17:52:16 +000013946static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013947xmlSchemaGetEffectiveValueConstraint(xmlSchemaAttributeUsePtr attruse,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013948 int *fixed,
13949 const xmlChar **value,
13950 xmlSchemaValPtr *val)
13951{
13952 *fixed = 0;
13953 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013954 if (val != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013955 *val = NULL;
13956
Daniel Veillarddee23482008-04-11 12:58:43 +000013957 if (attruse->defValue != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013958 *value = attruse->defValue;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013959 if (val != NULL)
13960 *val = attruse->defVal;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013961 if (attruse->flags & XML_SCHEMA_ATTR_USE_FIXED)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013962 *fixed = 1;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013963 return(1);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013964 } else if ((attruse->attrDecl != NULL) &&
13965 (attruse->attrDecl->defValue != NULL)) {
13966 *value = attruse->attrDecl->defValue;
13967 if (val != NULL)
13968 *val = attruse->attrDecl->defVal;
13969 if (attruse->attrDecl->flags & XML_SCHEMAS_ATTR_FIXED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013970 *fixed = 1;
13971 return(1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013972 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013973 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013974}
Daniel Veillard3646d642004-06-02 19:19:14 +000013975/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013976 * xmlSchemaCheckCVCWildcardNamespace:
Daniel Veillard3646d642004-06-02 19:19:14 +000013977 * @wild: the wildcard
13978 * @ns: the namespace
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013979 *
13980 * Validation Rule: Wildcard allows Namespace Name
13981 * (cvc-wildcard-namespace)
13982 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013983 * Returns 0 if the given namespace matches the wildcard,
13984 * 1 otherwise and -1 on API errors.
Daniel Veillard3646d642004-06-02 19:19:14 +000013985 */
13986static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013987xmlSchemaCheckCVCWildcardNamespace(xmlSchemaWildcardPtr wild,
13988 const xmlChar* ns)
Daniel Veillard3646d642004-06-02 19:19:14 +000013989{
13990 if (wild == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013991 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013992
13993 if (wild->any)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013994 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013995 else if (wild->nsSet != NULL) {
13996 xmlSchemaWildcardNsPtr cur;
13997
13998 cur = wild->nsSet;
13999 while (cur != NULL) {
14000 if (xmlStrEqual(cur->value, ns))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014001 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000014002 cur = cur->next;
14003 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014004 } else if ((wild->negNsSet != NULL) && (ns != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000014005 (!xmlStrEqual(wild->negNsSet->value, ns)))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014006 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014007
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014008 return(1);
14009}
14010
14011#define XML_SCHEMA_ACTION_DERIVE 0
14012#define XML_SCHEMA_ACTION_REDEFINE 1
14013
14014#define WXS_ACTION_STR(a) \
14015((a) == XML_SCHEMA_ACTION_DERIVE) ? (const xmlChar *) "base" : (const xmlChar *) "redefined"
14016
14017/*
14018* Schema Component Constraint:
14019* Derivation Valid (Restriction, Complex)
14020* derivation-ok-restriction (2) - (4)
14021*
14022* ATTENTION:
14023* In XML Schema 1.1 this will be:
14024* Validation Rule:
14025* Checking complex type subsumption (practicalSubsumption) (1, 2 and 3)
14026*
14027*/
14028static int
14029xmlSchemaCheckDerivationOKRestriction2to4(xmlSchemaParserCtxtPtr pctxt,
14030 int action,
14031 xmlSchemaBasicItemPtr item,
14032 xmlSchemaBasicItemPtr baseItem,
14033 xmlSchemaItemListPtr uses,
14034 xmlSchemaItemListPtr baseUses,
14035 xmlSchemaWildcardPtr wild,
14036 xmlSchemaWildcardPtr baseWild)
Daniel Veillarddee23482008-04-11 12:58:43 +000014037{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014038 xmlSchemaAttributeUsePtr cur = NULL, bcur;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014039 int i, j, found; /* err = 0; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014040 const xmlChar *bEffValue;
14041 int effFixed;
Daniel Veillarddee23482008-04-11 12:58:43 +000014042
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014043 if (uses != NULL) {
14044 for (i = 0; i < uses->nbItems; i++) {
14045 cur = uses->items[i];
14046 found = 0;
14047 if (baseUses == NULL)
14048 goto not_found;
14049 for (j = 0; j < baseUses->nbItems; j++) {
Daniel Veillarddee23482008-04-11 12:58:43 +000014050 bcur = baseUses->items[j];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014051 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14052 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14053 (WXS_ATTRUSE_DECL_TNS(cur) ==
14054 WXS_ATTRUSE_DECL_TNS(bcur)))
14055 {
14056 /*
14057 * (2.1) "If there is an attribute use in the {attribute
14058 * uses} of the {base type definition} (call this B) whose
14059 * {attribute declaration} has the same {name} and {target
14060 * namespace}, then all of the following must be true:"
14061 */
14062 found = 1;
Daniel Veillarddee23482008-04-11 12:58:43 +000014063
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014064 if ((cur->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
14065 (bcur->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED))
14066 {
14067 xmlChar *str = NULL;
14068 /*
14069 * (2.1.1) "one of the following must be true:"
14070 * (2.1.1.1) "B's {required} is false."
14071 * (2.1.1.2) "R's {required} is true."
14072 */
14073 xmlSchemaPAttrUseErr4(pctxt,
14074 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_1,
14075 WXS_ITEM_NODE(item), item, cur,
14076 "The 'optional' attribute use is inconsistent "
14077 "with the corresponding 'required' attribute use of "
14078 "the %s %s",
14079 WXS_ACTION_STR(action),
14080 xmlSchemaGetComponentDesignation(&str, baseItem),
14081 NULL, NULL);
14082 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014083 /* err = pctxt->err; */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014084 } else if (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014085 WXS_ATTRUSE_TYPEDEF(cur),
14086 WXS_ATTRUSE_TYPEDEF(bcur), 0) != 0)
14087 {
14088 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014089
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014090 /*
14091 * SPEC (2.1.2) "R's {attribute declaration}'s
14092 * {type definition} must be validly derived from
14093 * B's {type definition} given the empty set as
Rob Richardsc6947bb2008-06-29 15:04:41 +000014094 * defined in Type Derivation OK (Simple) (�3.14.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014095 */
14096 xmlSchemaPAttrUseErr4(pctxt,
14097 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_2,
14098 WXS_ITEM_NODE(item), item, cur,
14099 "The attribute declaration's %s "
14100 "is not validly derived from "
14101 "the corresponding %s of the "
14102 "attribute declaration in the %s %s",
14103 xmlSchemaGetComponentDesignation(&strA,
14104 WXS_ATTRUSE_TYPEDEF(cur)),
14105 xmlSchemaGetComponentDesignation(&strB,
14106 WXS_ATTRUSE_TYPEDEF(bcur)),
14107 WXS_ACTION_STR(action),
14108 xmlSchemaGetComponentDesignation(&strC, baseItem));
14109 /* xmlSchemaGetComponentDesignation(&str, baseItem), */
14110 FREE_AND_NULL(strA);
14111 FREE_AND_NULL(strB);
14112 FREE_AND_NULL(strC);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014113 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014114 } else {
14115 /*
14116 * 2.1.3 [Definition:] Let the effective value
14117 * constraint of an attribute use be its {value
14118 * constraint}, if present, otherwise its {attribute
14119 * declaration}'s {value constraint} .
14120 */
14121 xmlSchemaGetEffectiveValueConstraint(bcur,
14122 &effFixed, &bEffValue, NULL);
14123 /*
14124 * 2.1.3 ... one of the following must be true
14125 *
Rob Richardsc6947bb2008-06-29 15:04:41 +000014126 * 2.1.3.1 B's �effective value constraint� is
14127 * �absent� or default.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014128 */
14129 if ((bEffValue != NULL) &&
14130 (effFixed == 1)) {
14131 const xmlChar *rEffValue = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014132
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014133 xmlSchemaGetEffectiveValueConstraint(bcur,
14134 &effFixed, &rEffValue, NULL);
14135 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000014136 * 2.1.3.2 R's �effective value constraint� is
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014137 * fixed with the same string as B's.
14138 * MAYBE TODO: Compare the computed values.
14139 * Hmm, it says "same string" so
14140 * string-equality might really be sufficient.
14141 */
14142 if ((effFixed == 0) ||
14143 (! WXS_ARE_DEFAULT_STR_EQUAL(rEffValue, bEffValue)))
14144 {
14145 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014146
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014147 xmlSchemaPAttrUseErr4(pctxt,
14148 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_3,
14149 WXS_ITEM_NODE(item), item, cur,
14150 "The effective value constraint of the "
14151 "attribute use is inconsistent with "
14152 "its correspondent in the %s %s",
14153 WXS_ACTION_STR(action),
14154 xmlSchemaGetComponentDesignation(&str,
14155 baseItem),
14156 NULL, NULL);
14157 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014158 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014159 }
14160 }
14161 }
14162 break;
14163 }
14164 }
Daniel Veillarddee23482008-04-11 12:58:43 +000014165not_found:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014166 if (!found) {
14167 /*
14168 * (2.2) "otherwise the {base type definition} must have an
14169 * {attribute wildcard} and the {target namespace} of the
Rob Richardsc6947bb2008-06-29 15:04:41 +000014170 * R's {attribute declaration} must be �valid� with respect
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014171 * to that wildcard, as defined in Wildcard allows Namespace
Rob Richardsc6947bb2008-06-29 15:04:41 +000014172 * Name (�3.10.4)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014173 */
14174 if ((baseWild == NULL) ||
14175 (xmlSchemaCheckCVCWildcardNamespace(baseWild,
14176 (WXS_ATTRUSE_DECL(cur))->targetNamespace) != 0))
14177 {
14178 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014179
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014180 xmlSchemaPAttrUseErr4(pctxt,
14181 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_2,
14182 WXS_ITEM_NODE(item), item, cur,
14183 "Neither a matching attribute use, "
14184 "nor a matching wildcard exists in the %s %s",
14185 WXS_ACTION_STR(action),
14186 xmlSchemaGetComponentDesignation(&str, baseItem),
14187 NULL, NULL);
14188 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014189 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014190 }
14191 }
14192 }
14193 }
14194 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000014195 * SPEC derivation-ok-restriction (3):
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014196 * (3) "For each attribute use in the {attribute uses} of the {base type
14197 * definition} whose {required} is true, there must be an attribute
14198 * use with an {attribute declaration} with the same {name} and
14199 * {target namespace} as its {attribute declaration} in the {attribute
14200 * uses} of the complex type definition itself whose {required} is true.
14201 */
14202 if (baseUses != NULL) {
14203 for (j = 0; j < baseUses->nbItems; j++) {
14204 bcur = baseUses->items[j];
14205 if (bcur->occurs != XML_SCHEMAS_ATTR_USE_REQUIRED)
14206 continue;
14207 found = 0;
14208 if (uses != NULL) {
14209 for (i = 0; i < uses->nbItems; i++) {
Daniel Veillarddee23482008-04-11 12:58:43 +000014210 cur = uses->items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014211 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14212 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14213 (WXS_ATTRUSE_DECL_TNS(cur) ==
14214 WXS_ATTRUSE_DECL_TNS(bcur))) {
14215 found = 1;
14216 break;
14217 }
14218 }
14219 }
14220 if (!found) {
14221 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014222
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014223 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14224 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_3,
14225 NULL, item,
14226 "A matching attribute use for the "
14227 "'required' %s of the %s %s is missing",
14228 xmlSchemaGetComponentDesignation(&strA, bcur),
14229 WXS_ACTION_STR(action),
14230 xmlSchemaGetComponentDesignation(&strB, baseItem),
14231 NULL);
14232 FREE_AND_NULL(strA);
14233 FREE_AND_NULL(strB);
14234 }
14235 }
14236 }
14237 /*
14238 * derivation-ok-restriction (4)
14239 */
14240 if (wild != NULL) {
14241 /*
14242 * (4) "If there is an {attribute wildcard}, all of the
14243 * following must be true:"
Daniel Veillarddee23482008-04-11 12:58:43 +000014244 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014245 if (baseWild == NULL) {
14246 xmlChar *str = NULL;
14247
14248 /*
14249 * (4.1) "The {base type definition} must also have one."
Daniel Veillarddee23482008-04-11 12:58:43 +000014250 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014251 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14252 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_1,
14253 NULL, item,
14254 "The %s has an attribute wildcard, "
14255 "but the %s %s '%s' does not have one",
Daniel Veillarddee23482008-04-11 12:58:43 +000014256 WXS_ITEM_TYPE_NAME(item),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014257 WXS_ACTION_STR(action),
14258 WXS_ITEM_TYPE_NAME(baseItem),
14259 xmlSchemaGetComponentQName(&str, baseItem));
14260 FREE_AND_NULL(str);
14261 return(pctxt->err);
14262 } else if ((baseWild->any == 0) &&
14263 xmlSchemaCheckCOSNSSubset(wild, baseWild))
14264 {
14265 xmlChar *str = NULL;
14266 /*
14267 * (4.2) "The complex type definition's {attribute wildcard}'s
14268 * {namespace constraint} must be a subset of the {base type
14269 * definition}'s {attribute wildcard}'s {namespace constraint},
Rob Richardsc6947bb2008-06-29 15:04:41 +000014270 * as defined by Wildcard Subset (�3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014271 */
14272 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14273 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_2,
14274 NULL, item,
14275 "The attribute wildcard is not a valid "
14276 "subset of the wildcard in the %s %s '%s'",
14277 WXS_ACTION_STR(action),
14278 WXS_ITEM_TYPE_NAME(baseItem),
14279 xmlSchemaGetComponentQName(&str, baseItem),
14280 NULL);
14281 FREE_AND_NULL(str);
14282 return(pctxt->err);
14283 }
Rob Richardsc6947bb2008-06-29 15:04:41 +000014284 /* 4.3 Unless the {base type definition} is the �ur-type
14285 * definition�, the complex type definition's {attribute
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014286 * wildcard}'s {process contents} must be identical to or
14287 * stronger than the {base type definition}'s {attribute
14288 * wildcard}'s {process contents}, where strict is stronger
14289 * than lax is stronger than skip.
14290 */
14291 if ((! WXS_IS_ANYTYPE(baseItem)) &&
14292 (wild->processContents < baseWild->processContents)) {
14293 xmlChar *str = NULL;
14294 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14295 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_3,
14296 NULL, baseItem,
14297 "The {process contents} of the attribute wildcard is "
14298 "weaker than the one in the %s %s '%s'",
14299 WXS_ACTION_STR(action),
14300 WXS_ITEM_TYPE_NAME(baseItem),
14301 xmlSchemaGetComponentQName(&str, baseItem),
14302 NULL);
14303 FREE_AND_NULL(str)
14304 return(pctxt->err);
14305 }
14306 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014307 return(0);
14308}
14309
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014310
14311static int
14312xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
14313 xmlSchemaBasicItemPtr item,
14314 xmlSchemaWildcardPtr *completeWild,
14315 xmlSchemaItemListPtr list,
14316 xmlSchemaItemListPtr prohibs);
Daniel Veillard3646d642004-06-02 19:19:14 +000014317/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014318 * xmlSchemaFixupTypeAttributeUses:
Daniel Veillard3646d642004-06-02 19:19:14 +000014319 * @ctxt: the schema parser context
14320 * @type: the complex type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014321 *
Daniel Veillard3646d642004-06-02 19:19:14 +000014322 *
14323 * Builds the wildcard and the attribute uses on the given complex type.
14324 * Returns -1 if an internal error occurs, 0 otherwise.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014325 *
14326 * ATTENTION TODO: Experimantally this uses pointer comparisons for
14327 * strings, so recheck this if we start to hardcode some schemata, since
14328 * they might not be in the same dict.
14329 * NOTE: It is allowed to "extend" the xs:anyType type.
Daniel Veillard3646d642004-06-02 19:19:14 +000014330 */
14331static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014332xmlSchemaFixupTypeAttributeUses(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014333 xmlSchemaTypePtr type)
Daniel Veillard3646d642004-06-02 19:19:14 +000014334{
14335 xmlSchemaTypePtr baseType = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014336 xmlSchemaAttributeUsePtr use;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014337 xmlSchemaItemListPtr uses, baseUses, prohibs = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000014338
Daniel Veillard01fa6152004-06-29 17:04:39 +000014339 if (type->baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014340 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014341 "no base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000014342 return (-1);
14343 }
Daniel Veillarddee23482008-04-11 12:58:43 +000014344 baseType = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014345 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014346 if (xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014347 return(-1);
14348
14349 uses = type->attrUses;
14350 baseUses = baseType->attrUses;
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014351 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014352 * Expand attribute group references. And build the 'complete'
14353 * wildcard, i.e. intersect multiple wildcards.
14354 * Move attribute prohibitions into a separate list.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014355 */
Daniel Veillarddee23482008-04-11 12:58:43 +000014356 if (uses != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014357 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014358 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014359 * This one will transfer all attr. prohibitions
14360 * into pctxt->attrProhibs.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014361 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014362 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14363 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14364 pctxt->attrProhibs) == -1)
14365 {
14366 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14367 "failed to expand attributes");
14368 }
14369 if (pctxt->attrProhibs->nbItems != 0)
14370 prohibs = pctxt->attrProhibs;
14371 } else {
14372 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14373 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14374 NULL) == -1)
14375 {
14376 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14377 "failed to expand attributes");
14378 }
14379 }
14380 }
14381 /*
14382 * Inherit the attribute uses of the base type.
14383 */
14384 if (baseUses != NULL) {
14385 int i, j;
14386 xmlSchemaAttributeUseProhibPtr pro;
14387
14388 if (WXS_IS_RESTRICTION(type)) {
14389 int usesCount;
Daniel Veillarddee23482008-04-11 12:58:43 +000014390 xmlSchemaAttributeUsePtr tmp;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014391
14392 if (uses != NULL)
14393 usesCount = uses->nbItems;
14394 else
14395 usesCount = 0;
14396
14397 /* Restriction. */
14398 for (i = 0; i < baseUses->nbItems; i++) {
14399 use = baseUses->items[i];
14400 if (prohibs) {
14401 /*
14402 * Filter out prohibited uses.
14403 */
14404 for (j = 0; j < prohibs->nbItems; j++) {
14405 pro = prohibs->items[j];
14406 if ((WXS_ATTRUSE_DECL_NAME(use) == pro->name) &&
14407 (WXS_ATTRUSE_DECL_TNS(use) ==
14408 pro->targetNamespace))
14409 {
14410 goto inherit_next;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014411 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014412 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014413 }
14414 if (usesCount) {
14415 /*
14416 * Filter out existing uses.
14417 */
14418 for (j = 0; j < usesCount; j++) {
14419 tmp = uses->items[j];
14420 if ((WXS_ATTRUSE_DECL_NAME(use) ==
14421 WXS_ATTRUSE_DECL_NAME(tmp)) &&
14422 (WXS_ATTRUSE_DECL_TNS(use) ==
14423 WXS_ATTRUSE_DECL_TNS(tmp)))
14424 {
14425 goto inherit_next;
14426 }
14427 }
14428 }
14429 if (uses == NULL) {
14430 type->attrUses = xmlSchemaItemListCreate();
14431 if (type->attrUses == NULL)
14432 goto exit_failure;
14433 uses = type->attrUses;
14434 }
14435 xmlSchemaItemListAddSize(uses, 2, use);
14436inherit_next: {}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014437 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014438 } else {
14439 /* Extension. */
Daniel Veillarddee23482008-04-11 12:58:43 +000014440 for (i = 0; i < baseUses->nbItems; i++) {
14441 use = baseUses->items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014442 if (uses == NULL) {
14443 type->attrUses = xmlSchemaItemListCreate();
14444 if (type->attrUses == NULL)
14445 goto exit_failure;
14446 uses = type->attrUses;
14447 }
Daniel Veillarddee23482008-04-11 12:58:43 +000014448 xmlSchemaItemListAddSize(uses, baseUses->nbItems, use);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014449 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014450 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014451 }
14452 /*
14453 * Shrink attr. uses.
14454 */
14455 if (uses) {
14456 if (uses->nbItems == 0) {
14457 xmlSchemaItemListFree(uses);
14458 type->attrUses = NULL;
14459 }
14460 /*
14461 * TODO: We could shrink the size of the array
14462 * to fit the actual number of items.
14463 */
14464 }
14465 /*
14466 * Compute the complete wildcard.
14467 */
Daniel Veillarddee23482008-04-11 12:58:43 +000014468 if (WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014469 if (baseType->attributeWildcard != NULL) {
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014470 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000014471 * (3.2.2.1) "If the �base wildcard� is non-�absent�, then
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014472 * the appropriate case among the following:"
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014473 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014474 if (type->attributeWildcard != NULL) {
14475 /*
14476 * Union the complete wildcard with the base wildcard.
14477 * SPEC {attribute wildcard}
14478 * (3.2.2.1.2) "otherwise a wildcard whose {process contents}
Rob Richardsc6947bb2008-06-29 15:04:41 +000014479 * and {annotation} are those of the �complete wildcard�,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014480 * and whose {namespace constraint} is the intensional union
Rob Richardsc6947bb2008-06-29 15:04:41 +000014481 * of the {namespace constraint} of the �complete wildcard�
14482 * and of the �base wildcard�, as defined in Attribute
14483 * Wildcard Union (�3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014484 */
14485 if (xmlSchemaUnionWildcards(pctxt, type->attributeWildcard,
14486 baseType->attributeWildcard) == -1)
Daniel Veillarddee23482008-04-11 12:58:43 +000014487 goto exit_failure;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014488 } else {
14489 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000014490 * (3.2.2.1.1) "If the �complete wildcard� is �absent�,
14491 * then the �base wildcard�."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014492 */
14493 type->attributeWildcard = baseType->attributeWildcard;
Daniel Veillarddee23482008-04-11 12:58:43 +000014494 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014495 } else {
14496 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000014497 * (3.2.2.2) "otherwise (the �base wildcard� is �absent�) the
14498 * �complete wildcard"
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014499 * NOOP
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014500 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014501 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014502 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014503 /*
14504 * SPEC {attribute wildcard}
14505 * (3.1) "If the <restriction> alternative is chosen, then the
Rob Richardsc6947bb2008-06-29 15:04:41 +000014506 * �complete wildcard�;"
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014507 * NOOP
14508 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014509 }
Daniel Veillarddee23482008-04-11 12:58:43 +000014510
Daniel Veillard3646d642004-06-02 19:19:14 +000014511 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014512
14513exit_failure:
14514 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000014515}
14516
14517/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014518 * xmlSchemaTypeFinalContains:
14519 * @schema: the schema
14520 * @type: the type definition
14521 * @final: the final
14522 *
14523 * Evaluates if a type definition contains the given "final".
14524 * This does take "finalDefault" into account as well.
14525 *
14526 * Returns 1 if the type does containt the given "final",
14527 * 0 otherwise.
14528 */
14529static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014530xmlSchemaTypeFinalContains(xmlSchemaTypePtr type, int final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014531{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014532 if (type == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014533 return (0);
14534 if (type->flags & final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014535 return (1);
14536 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014537 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014538}
14539
14540/**
14541 * xmlSchemaGetUnionSimpleTypeMemberTypes:
14542 * @type: the Union Simple Type
14543 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014544 * Returns a list of member types of @type if existing,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014545 * returns NULL otherwise.
14546 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014547static xmlSchemaTypeLinkPtr
Daniel Veillard01fa6152004-06-29 17:04:39 +000014548xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type)
14549{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014550 while ((type != NULL) && (type->type == XML_SCHEMA_TYPE_SIMPLE)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014551 if (type->memberTypes != NULL)
14552 return (type->memberTypes);
14553 else
14554 type = type->baseType;
14555 }
14556 return (NULL);
14557}
14558
14559/**
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014560 * xmlSchemaGetParticleTotalRangeMin:
14561 * @particle: the particle
14562 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014563 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014564 * (all and sequence) + (choice)
14565 *
14566 * Returns the minimun Effective Total Range.
14567 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014568static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014569xmlSchemaGetParticleTotalRangeMin(xmlSchemaParticlePtr particle)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014570{
14571 if ((particle->children == NULL) ||
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014572 (particle->minOccurs == 0))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014573 return (0);
14574 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014575 int min = -1, cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014576 xmlSchemaParticlePtr part =
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014577 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014578
14579 if (part == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014580 return (0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014581 while (part != NULL) {
14582 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14583 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014584 cur = part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014585 else
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014586 cur = xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014587 if (cur == 0)
14588 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014589 if ((min > cur) || (min == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014590 min = cur;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014591 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014592 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014593 return (particle->minOccurs * min);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014594 } else {
14595 /* <all> and <sequence> */
14596 int sum = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014597 xmlSchemaParticlePtr part =
14598 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014599
14600 if (part == NULL)
14601 return (0);
14602 do {
14603 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14604 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014605 sum += part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014606 else
14607 sum += xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014608 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014609 } while (part != NULL);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014610 return (particle->minOccurs * sum);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014611 }
14612}
14613
Daniel Veillard15724252008-08-30 15:01:04 +000014614#if 0
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014615/**
14616 * xmlSchemaGetParticleTotalRangeMax:
14617 * @particle: the particle
14618 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014619 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014620 * (all and sequence) + (choice)
14621 *
14622 * Returns the maximum Effective Total Range.
14623 */
14624static int
14625xmlSchemaGetParticleTotalRangeMax(xmlSchemaParticlePtr particle)
14626{
14627 if ((particle->children == NULL) ||
14628 (particle->children->children == NULL))
14629 return (0);
14630 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
14631 int max = -1, cur;
14632 xmlSchemaParticlePtr part =
14633 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014634
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014635 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14636 if (part->children == NULL)
14637 continue;
14638 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14639 (part->children->type == XML_SCHEMA_TYPE_ANY))
14640 cur = part->maxOccurs;
14641 else
14642 cur = xmlSchemaGetParticleTotalRangeMax(part);
14643 if (cur == UNBOUNDED)
14644 return (UNBOUNDED);
14645 if ((max < cur) || (max == -1))
14646 max = cur;
14647 }
14648 /* TODO: Handle overflows? */
14649 return (particle->maxOccurs * max);
14650 } else {
14651 /* <all> and <sequence> */
14652 int sum = 0, cur;
14653 xmlSchemaParticlePtr part =
14654 (xmlSchemaParticlePtr) particle->children->children;
14655
14656 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14657 if (part->children == NULL)
14658 continue;
14659 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14660 (part->children->type == XML_SCHEMA_TYPE_ANY))
14661 cur = part->maxOccurs;
14662 else
14663 cur = xmlSchemaGetParticleTotalRangeMax(part);
14664 if (cur == UNBOUNDED)
14665 return (UNBOUNDED);
14666 if ((cur > 0) && (particle->maxOccurs == UNBOUNDED))
14667 return (UNBOUNDED);
14668 sum += cur;
14669 }
14670 /* TODO: Handle overflows? */
14671 return (particle->maxOccurs * sum);
14672 }
14673}
Daniel Veillard15724252008-08-30 15:01:04 +000014674#endif
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014675
14676/**
14677 * xmlSchemaIsParticleEmptiable:
14678 * @particle: the particle
14679 *
14680 * Schema Component Constraint: Particle Emptiable
14681 * Checks whether the given particle is emptiable.
14682 *
14683 * Returns 1 if emptiable, 0 otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014684 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014685static int
14686xmlSchemaIsParticleEmptiable(xmlSchemaParticlePtr particle)
14687{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014688 /*
14689 * SPEC (1) "Its {min occurs} is 0."
14690 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014691 if ((particle == NULL) || (particle->minOccurs == 0) ||
14692 (particle->children == NULL))
14693 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014694 /*
14695 * SPEC (2) "Its {term} is a group and the minimum part of the
14696 * effective total range of that group, [...] is 0."
14697 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014698 if (WXS_IS_MODEL_GROUP(particle->children)) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014699 if (xmlSchemaGetParticleTotalRangeMin(particle) == 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014700 return (1);
14701 }
14702 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014703}
14704
14705/**
14706 * xmlSchemaCheckCOSSTDerivedOK:
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014707 * @actxt: a context
Daniel Veillard01fa6152004-06-29 17:04:39 +000014708 * @type: the derived simple type definition
14709 * @baseType: the base type definition
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014710 * @subset: the subset of ('restriction', ect.)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014711 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014712 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014713 * Type Derivation OK (Simple) (cos-st-derived-OK)
14714 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014715 * Checks wheter @type can be validly
Daniel Veillard01fa6152004-06-29 17:04:39 +000014716 * derived from @baseType.
14717 *
14718 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014719 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014720static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014721xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014722 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014723 xmlSchemaTypePtr baseType,
14724 int subset)
14725{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014726 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014727 * 1 They are the same type definition.
14728 * TODO: The identy check might have to be more complex than this.
14729 */
14730 if (type == baseType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014731 return (0);
14732 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014733 * 2.1 restriction is not in the subset, or in the {final}
14734 * of its own {base type definition};
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014735 *
14736 * NOTE that this will be used also via "xsi:type".
14737 *
14738 * TODO: Revise this, it looks strange. How can the "type"
14739 * not be fixed or *in* fixing?
Daniel Veillard01fa6152004-06-29 17:04:39 +000014740 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014741 if (WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014742 if (xmlSchemaTypeFixup(type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014743 return(-1);
14744 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014745 if (xmlSchemaTypeFixup(baseType, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014746 return(-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014747 if ((subset & SUBSET_RESTRICTION) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014748 (xmlSchemaTypeFinalContains(type->baseType,
14749 XML_SCHEMAS_TYPE_FINAL_RESTRICTION))) {
14750 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014751 }
14752 /* 2.2 */
14753 if (type->baseType == baseType) {
14754 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000014755 * 2.2.1 D's �base type definition� is B.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014756 */
14757 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014758 }
14759 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000014760 * 2.2.2 D's �base type definition� is not the �ur-type definition�
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014761 * and is validly derived from B given the subset, as defined by this
14762 * constraint.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014763 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014764 if ((! WXS_IS_ANYTYPE(type->baseType)) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014765 (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014766 baseType, subset) == 0)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014767 return (0);
14768 }
14769 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000014770 * 2.2.3 D's {variety} is list or union and B is the �simple ur-type
14771 * definition�.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014772 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014773 if (WXS_IS_ANY_SIMPLE_TYPE(baseType) &&
14774 (WXS_IS_LIST(type) || WXS_IS_UNION(type))) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014775 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014776 }
14777 /*
14778 * 2.2.4 B's {variety} is union and D is validly derived from a type
14779 * definition in B's {member type definitions} given the subset, as
Daniel Veillard01fa6152004-06-29 17:04:39 +000014780 * defined by this constraint.
14781 *
14782 * NOTE: This seems not to involve built-in types, since there is no
14783 * built-in Union Simple Type.
14784 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014785 if (WXS_IS_UNION(baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014786 xmlSchemaTypeLinkPtr cur;
14787
14788 cur = baseType->memberTypes;
14789 while (cur != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014790 if (WXS_IS_TYPE_NOT_FIXED(cur->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014791 if (xmlSchemaTypeFixup(cur->type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014792 return(-1);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014793 if (xmlSchemaCheckCOSSTDerivedOK(actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014794 type, cur->type, subset) == 0)
14795 {
14796 /*
14797 * It just has to be validly derived from at least one
14798 * member-type.
14799 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014800 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014801 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014802 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014803 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014804 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014805 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_2);
14806}
14807
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014808/**
14809 * xmlSchemaCheckTypeDefCircularInternal:
14810 * @pctxt: the schema parser context
14811 * @ctxtType: the type definition
14812 * @ancestor: an ancestor of @ctxtType
14813 *
14814 * Checks st-props-correct (2) + ct-props-correct (3).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014815 * Circular type definitions are not allowed.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014816 *
14817 * Returns XML_SCHEMAP_ST_PROPS_CORRECT_2 if the given type is
14818 * circular, 0 otherwise.
14819 */
14820static int
14821xmlSchemaCheckTypeDefCircularInternal(xmlSchemaParserCtxtPtr pctxt,
14822 xmlSchemaTypePtr ctxtType,
14823 xmlSchemaTypePtr ancestor)
14824{
14825 int ret;
14826
14827 if ((ancestor == NULL) || (ancestor->type == XML_SCHEMA_TYPE_BASIC))
14828 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014829
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014830 if (ctxtType == ancestor) {
14831 xmlSchemaPCustomErr(pctxt,
14832 XML_SCHEMAP_ST_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014833 WXS_BASIC_CAST ctxtType, WXS_ITEM_NODE(ctxtType),
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014834 "The definition is circular", NULL);
14835 return (XML_SCHEMAP_ST_PROPS_CORRECT_2);
14836 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014837 if (ancestor->flags & XML_SCHEMAS_TYPE_MARKED) {
14838 /*
14839 * Avoid inifinite recursion on circular types not yet checked.
14840 */
14841 return (0);
14842 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014843 ancestor->flags |= XML_SCHEMAS_TYPE_MARKED;
14844 ret = xmlSchemaCheckTypeDefCircularInternal(pctxt, ctxtType,
14845 ancestor->baseType);
14846 ancestor->flags ^= XML_SCHEMAS_TYPE_MARKED;
14847 return (ret);
14848}
14849
14850/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014851 * xmlSchemaCheckTypeDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014852 * @item: the complex/simple type definition
14853 * @ctxt: the parser context
14854 * @name: the name
14855 *
14856 * Checks for circular type definitions.
14857 */
14858static void
14859xmlSchemaCheckTypeDefCircular(xmlSchemaTypePtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014860 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014861{
14862 if ((item == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014863 (item->type == XML_SCHEMA_TYPE_BASIC) ||
14864 (item->baseType == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014865 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014866 xmlSchemaCheckTypeDefCircularInternal(ctxt, item,
14867 item->baseType);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014868}
Daniel Veillard01fa6152004-06-29 17:04:39 +000014869
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014870/*
14871* Simple Type Definition Representation OK (src-simple-type) 4
14872*
14873* "4 Circular union type definition is disallowed. That is, if the
14874* <union> alternative is chosen, there must not be any entries in the
14875* memberTypes [attribute] at any depth which resolve to the component
14876* corresponding to the <simpleType>."
14877*
14878* Note that this should work on the *representation* of a component,
14879* thus assumes any union types in the member types not being yet
14880* substituted. At this stage we need the variety of the types
14881* to be already computed.
14882*/
14883static int
14884xmlSchemaCheckUnionTypeDefCircularRecur(xmlSchemaParserCtxtPtr pctxt,
14885 xmlSchemaTypePtr ctxType,
14886 xmlSchemaTypeLinkPtr members)
Daniel Veillarddee23482008-04-11 12:58:43 +000014887{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014888 xmlSchemaTypeLinkPtr member;
14889 xmlSchemaTypePtr memberType;
Daniel Veillarddee23482008-04-11 12:58:43 +000014890
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014891 member = members;
14892 while (member != NULL) {
14893 memberType = member->type;
14894 while ((memberType != NULL) &&
14895 (memberType->type != XML_SCHEMA_TYPE_BASIC)) {
14896 if (memberType == ctxType) {
14897 xmlSchemaPCustomErr(pctxt,
14898 XML_SCHEMAP_SRC_SIMPLE_TYPE_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014899 WXS_BASIC_CAST ctxType, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014900 "The union type definition is circular", NULL);
14901 return (XML_SCHEMAP_SRC_SIMPLE_TYPE_4);
14902 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014903 if ((WXS_IS_UNION(memberType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014904 ((memberType->flags & XML_SCHEMAS_TYPE_MARKED) == 0))
14905 {
14906 int res;
14907 memberType->flags |= XML_SCHEMAS_TYPE_MARKED;
14908 res = xmlSchemaCheckUnionTypeDefCircularRecur(pctxt,
14909 ctxType,
14910 xmlSchemaGetUnionSimpleTypeMemberTypes(memberType));
14911 memberType->flags ^= XML_SCHEMAS_TYPE_MARKED;
14912 if (res != 0)
14913 return(res);
14914 }
14915 memberType = memberType->baseType;
14916 }
14917 member = member->next;
14918 }
14919 return(0);
14920}
14921
14922static int
14923xmlSchemaCheckUnionTypeDefCircular(xmlSchemaParserCtxtPtr pctxt,
14924 xmlSchemaTypePtr type)
14925{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014926 if (! WXS_IS_UNION(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014927 return(0);
14928 return(xmlSchemaCheckUnionTypeDefCircularRecur(pctxt, type,
14929 type->memberTypes));
14930}
14931
Daniel Veillard01fa6152004-06-29 17:04:39 +000014932/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014933 * xmlSchemaResolveTypeReferences:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014934 * @item: the complex/simple type definition
14935 * @ctxt: the parser context
14936 * @name: the name
14937 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014938 * Resolvese type definition references
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014939 */
14940static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014941xmlSchemaResolveTypeReferences(xmlSchemaTypePtr typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014942 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014943{
14944 if (typeDef == NULL)
14945 return;
14946
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014947 /*
14948 * Resolve the base type.
14949 */
14950 if (typeDef->baseType == NULL) {
14951 typeDef->baseType = xmlSchemaGetType(ctxt->schema,
14952 typeDef->base, typeDef->baseNs);
14953 if (typeDef->baseType == NULL) {
14954 xmlSchemaPResCompAttrErr(ctxt,
14955 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014956 WXS_BASIC_CAST typeDef, typeDef->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014957 "base", typeDef->base, typeDef->baseNs,
14958 XML_SCHEMA_TYPE_SIMPLE, NULL);
14959 return;
14960 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014961 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014962 if (WXS_IS_SIMPLE(typeDef)) {
14963 if (WXS_IS_UNION(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014964 /*
14965 * Resolve the memberTypes.
14966 */
14967 xmlSchemaResolveUnionMemberTypes(ctxt, typeDef);
14968 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014969 } else if (WXS_IS_LIST(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014970 /*
14971 * Resolve the itemType.
14972 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014973 if ((typeDef->subtypes == NULL) && (typeDef->base != NULL)) {
14974
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014975 typeDef->subtypes = xmlSchemaGetType(ctxt->schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014976 typeDef->base, typeDef->baseNs);
14977
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014978 if ((typeDef->subtypes == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014979 (! WXS_IS_SIMPLE(typeDef->subtypes)))
14980 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014981 typeDef->subtypes = NULL;
14982 xmlSchemaPResCompAttrErr(ctxt,
14983 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014984 WXS_BASIC_CAST typeDef, typeDef->node,
14985 "itemType", typeDef->base, typeDef->baseNs,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014986 XML_SCHEMA_TYPE_SIMPLE, NULL);
14987 }
14988 }
14989 return;
14990 }
Daniel Veillarddee23482008-04-11 12:58:43 +000014991 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014992 /*
14993 * The ball of letters below means, that if we have a particle
14994 * which has a QName-helper component as its {term}, we want
14995 * to resolve it...
14996 */
14997 else if ((WXS_TYPE_CONTENTTYPE(typeDef) != NULL) &&
14998 ((WXS_TYPE_CONTENTTYPE(typeDef))->type ==
14999 XML_SCHEMA_TYPE_PARTICLE) &&
15000 (WXS_TYPE_PARTICLE_TERM(typeDef) != NULL) &&
15001 ((WXS_TYPE_PARTICLE_TERM(typeDef))->type ==
15002 XML_SCHEMA_EXTRA_QNAMEREF))
15003 {
15004 xmlSchemaQNameRefPtr ref =
15005 WXS_QNAME_CAST WXS_TYPE_PARTICLE_TERM(typeDef);
15006 xmlSchemaModelGroupDefPtr groupDef;
15007
15008 /*
15009 * URGENT TODO: Test this.
15010 */
15011 WXS_TYPE_PARTICLE_TERM(typeDef) = NULL;
15012 /*
15013 * Resolve the MG definition reference.
15014 */
15015 groupDef =
15016 WXS_MODEL_GROUPDEF_CAST xmlSchemaGetNamedComponent(ctxt->schema,
15017 ref->itemType, ref->name, ref->targetNamespace);
15018 if (groupDef == NULL) {
15019 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
15020 NULL, WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)),
15021 "ref", ref->name, ref->targetNamespace, ref->itemType,
15022 NULL);
15023 /* Remove the particle. */
15024 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
15025 } else if (WXS_MODELGROUPDEF_MODEL(groupDef) == NULL)
15026 /* Remove the particle. */
15027 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
15028 else {
15029 /*
15030 * Assign the MG definition's {model group} to the
15031 * particle's {term}.
15032 */
15033 WXS_TYPE_PARTICLE_TERM(typeDef) = WXS_MODELGROUPDEF_MODEL(groupDef);
Daniel Veillarddee23482008-04-11 12:58:43 +000015034
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015035 if (WXS_MODELGROUPDEF_MODEL(groupDef)->type == XML_SCHEMA_TYPE_ALL) {
15036 /*
15037 * SPEC cos-all-limited (1.2)
15038 * "1.2 the {term} property of a particle with
15039 * {max occurs}=1 which is part of a pair which constitutes
15040 * the {content type} of a complex type definition."
15041 */
15042 if ((WXS_TYPE_PARTICLE(typeDef))->maxOccurs != 1) {
15043 xmlSchemaCustomErr(ACTXT_CAST ctxt,
15044 /* TODO: error code */
15045 XML_SCHEMAP_COS_ALL_LIMITED,
15046 WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)), NULL,
15047 "The particle's {max occurs} must be 1, since the "
15048 "reference resolves to an 'all' model group",
15049 NULL, NULL);
15050 }
15051 }
15052 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015053 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015054}
15055
15056
15057
15058/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000015059 * xmlSchemaCheckSTPropsCorrect:
15060 * @ctxt: the schema parser context
15061 * @type: the simple type definition
15062 *
15063 * Checks st-props-correct.
15064 *
15065 * Returns 0 if the properties are correct,
15066 * if not, a positive error code and -1 on internal
15067 * errors.
15068 */
15069static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015070xmlSchemaCheckSTPropsCorrect(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015071 xmlSchemaTypePtr type)
15072{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015073 xmlSchemaTypePtr baseType = type->baseType;
Daniel Veillardc0826a72004-08-10 14:17:33 +000015074 xmlChar *str = NULL;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015075
Daniel Veillardc0826a72004-08-10 14:17:33 +000015076 /* STATE: error funcs converted. */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015077 /*
15078 * Schema Component Constraint: Simple Type Definition Properties Correct
15079 *
15080 * NOTE: This is somehow redundant, since we actually built a simple type
15081 * to have all the needed information; this acts as an self test.
15082 */
Rob Richardsc6947bb2008-06-29 15:04:41 +000015083 /* Base type: If the datatype has been �derived� by �restriction�
15084 * then the Simple Type Definition component from which it is �derived�,
15085 * otherwise the Simple Type Definition for anySimpleType (�4.1.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000015086 */
15087 if (baseType == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015088 /*
15089 * TODO: Think about: "modulo the impact of Missing
Rob Richardsc6947bb2008-06-29 15:04:41 +000015090 * Sub-components (�5.3)."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015091 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015092 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015093 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015094 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015095 "No base type existent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015096 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015097
Daniel Veillard01fa6152004-06-29 17:04:39 +000015098 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015099 if (! WXS_IS_SIMPLE(baseType)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015100 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015101 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015102 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015103 "The base type '%s' is not a simple type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015104 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015105 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015106 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15107 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015108 if ( (WXS_IS_LIST(type) || WXS_IS_UNION(type)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015109 (WXS_IS_RESTRICTION(type) == 0) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015110 (! WXS_IS_ANY_SIMPLE_TYPE(baseType))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015111 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015112 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015113 WXS_BASIC_CAST type, NULL,
Daniel Veillardbf9c1da2008-08-26 07:46:42 +000015114 "A type, derived by list or union, must have "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015115 "the simple ur-type definition as base type, not '%s'",
15116 xmlSchemaGetComponentQName(&str, baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015117 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015118 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15119 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015120 /*
15121 * Variety: One of {atomic, list, union}.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015122 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015123 if ((! WXS_IS_ATOMIC(type)) && (! WXS_IS_UNION(type)) &&
15124 (! WXS_IS_LIST(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015125 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015126 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015127 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015128 "The variety is absent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015129 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15130 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000015131 /* TODO: Finish this. Hmm, is this finished? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015132
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015133 /*
15134 * 3 The {final} of the {base type definition} must not contain restriction.
15135 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015136 if (xmlSchemaTypeFinalContains(baseType,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015137 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
15138 xmlSchemaPCustomErr(ctxt,
15139 XML_SCHEMAP_ST_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015140 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015141 "The 'final' of its base type '%s' must not contain "
15142 "'restriction'",
15143 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015144 FREE_AND_NULL(str)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015145 return (XML_SCHEMAP_ST_PROPS_CORRECT_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015146 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015147
15148 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000015149 * 2 All simple type definitions must be derived ultimately from the �simple
15150 * ur-type definition (so� circular definitions are disallowed). That is, it
15151 * must be possible to reach a built-in primitive datatype or the �simple
15152 * ur-type definition� by repeatedly following the {base type definition}.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015153 *
15154 * NOTE: this is done in xmlSchemaCheckTypeDefCircular().
Daniel Veillard01fa6152004-06-29 17:04:39 +000015155 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015156 return (0);
15157}
15158
15159/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015160 * xmlSchemaCheckCOSSTRestricts:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015161 * @ctxt: the schema parser context
15162 * @type: the simple type definition
15163 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015164 * Schema Component Constraint:
15165 * Derivation Valid (Restriction, Simple) (cos-st-restricts)
15166
15167 * Checks if the given @type (simpleType) is derived validly by restriction.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015168 * STATUS:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015169 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015170 * Returns -1 on internal errors, 0 if the type is validly derived,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015171 * a positive error code otherwise.
15172 */
15173static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015174xmlSchemaCheckCOSSTRestricts(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015175 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015176{
Daniel Veillardc0826a72004-08-10 14:17:33 +000015177 xmlChar *str = NULL;
15178
Daniel Veillard01fa6152004-06-29 17:04:39 +000015179 if (type->type != XML_SCHEMA_TYPE_SIMPLE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015180 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15181 "given type is not a user-derived simpleType");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015182 return (-1);
15183 }
15184
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015185 if (WXS_IS_ATOMIC(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015186 xmlSchemaTypePtr primitive;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015187 /*
15188 * 1.1 The {base type definition} must be an atomic simple
Daniel Veillard01fa6152004-06-29 17:04:39 +000015189 * type definition or a built-in primitive datatype.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015190 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015191 if (! WXS_IS_ATOMIC(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015192 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015193 XML_SCHEMAP_COS_ST_RESTRICTS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015194 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015195 "The base type '%s' is not an atomic simple type",
15196 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015197 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015198 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_1);
15199 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015200 /* 1.2 The {final} of the {base type definition} must not contain
Daniel Veillard01fa6152004-06-29 17:04:39 +000015201 * restriction.
15202 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015203 /* OPTIMIZE TODO : This is already done in xmlSchemaCheckStPropsCorrect */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015204 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015205 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015206 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015207 XML_SCHEMAP_COS_ST_RESTRICTS_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015208 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015209 "The final of its base type '%s' must not contain 'restriction'",
15210 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015211 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015212 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_2);
15213 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015214
15215 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015216 * 1.3.1 DF must be an allowed constraining facet for the {primitive
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015217 * type definition}, as specified in the appropriate subsection of 3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015218 * Primitive datatypes.
15219 */
15220 if (type->facets != NULL) {
15221 xmlSchemaFacetPtr facet;
15222 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015223
Daniel Veillard01fa6152004-06-29 17:04:39 +000015224 primitive = xmlSchemaGetPrimitiveType(type);
15225 if (primitive == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015226 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15227 "failed to get primitive type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015228 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015229 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015230 facet = type->facets;
15231 do {
15232 if (xmlSchemaIsBuiltInTypeFacet(primitive, facet->type) == 0) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015233 ok = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015234 xmlSchemaPIllegalFacetAtomicErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015235 XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015236 type, primitive, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015237 }
15238 facet = facet->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015239 } while (facet != NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015240 if (ok == 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015241 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015242 }
15243 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015244 * SPEC (1.3.2) "If there is a facet of the same kind in the {facets}
15245 * of the {base type definition} (call this BF),then the DF's {value}
15246 * must be a valid restriction of BF's {value} as defined in
15247 * [XML Schemas: Datatypes]."
15248 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015249 * NOTE (1.3.2) Facet derivation constraints are currently handled in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015250 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015251 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015252 } else if (WXS_IS_LIST(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015253 xmlSchemaTypePtr itemType = NULL;
15254
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015255 itemType = type->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015256 if ((itemType == NULL) || (! WXS_IS_SIMPLE(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015257 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15258 "failed to evaluate the item type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015259 return (-1);
15260 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015261 if (WXS_IS_TYPE_NOT_FIXED(itemType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015262 xmlSchemaTypeFixup(itemType, ACTXT_CAST pctxt);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015263 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015264 * 2.1 The {item type definition} must have a {variety} of atomic or
15265 * union (in which case all the {member type definitions}
Daniel Veillard01fa6152004-06-29 17:04:39 +000015266 * must be atomic).
15267 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015268 if ((! WXS_IS_ATOMIC(itemType)) &&
15269 (! WXS_IS_UNION(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015270 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015271 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015272 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015273 "The item type '%s' does not have a variety of atomic or union",
15274 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015275 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015276 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015277 } else if (WXS_IS_UNION(itemType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015278 xmlSchemaTypeLinkPtr member;
15279
15280 member = itemType->memberTypes;
15281 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015282 if (! WXS_IS_ATOMIC(member->type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015283 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015284 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015285 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015286 "The item type is a union type, but the "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015287 "member type '%s' of this item type is not atomic",
15288 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015289 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015290 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
15291 }
15292 member = member->next;
15293 }
15294 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015295
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015296 if (WXS_IS_ANY_SIMPLE_TYPE(type->baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015297 xmlSchemaFacetPtr facet;
15298 /*
15299 * This is the case if we have: <simpleType><list ..
15300 */
15301 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015302 * 2.3.1
15303 * 2.3.1.1 The {final} of the {item type definition} must not
Daniel Veillard01fa6152004-06-29 17:04:39 +000015304 * contain list.
15305 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015306 if (xmlSchemaTypeFinalContains(itemType,
15307 XML_SCHEMAS_TYPE_FINAL_LIST)) {
15308 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015309 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015310 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015311 "The final of its item type '%s' must not contain 'list'",
15312 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015313 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015314 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1);
15315 }
15316 /*
15317 * 2.3.1.2 The {facets} must only contain the whiteSpace
15318 * facet component.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015319 * OPTIMIZE TODO: the S4S already disallows any facet
15320 * to be specified.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015321 */
15322 if (type->facets != NULL) {
15323 facet = type->facets;
15324 do {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015325 if (facet->type != XML_SCHEMA_FACET_WHITESPACE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015326 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015327 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015328 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015329 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2);
15330 }
15331 facet = facet->next;
15332 } while (facet != NULL);
15333 }
15334 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015335 * MAYBE TODO: (Hmm, not really) Datatypes states:
Rob Richardsc6947bb2008-06-29 15:04:41 +000015336 * A �list� datatype can be �derived� from an �atomic� datatype
15337 * whose �lexical space� allows space (such as string or anyURI)or
15338 * a �union� datatype any of whose {member type definitions}'s
15339 * �lexical space� allows space.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015340 */
15341 } else {
15342 /*
15343 * This is the case if we have: <simpleType><restriction ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015344 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015345 */
15346 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015347 * 2.3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015348 * 2.3.2.1 The {base type definition} must have a {variety} of list.
15349 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015350 if (! WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015351 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015352 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015353 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015354 "The base type '%s' must be a list type",
15355 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015356 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015357 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1);
15358 }
15359 /*
15360 * 2.3.2.2 The {final} of the {base type definition} must not
15361 * contain restriction.
15362 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015363 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015364 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015365 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015366 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015367 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015368 "The 'final' of the base type '%s' must not contain 'restriction'",
15369 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015370 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015371 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2);
15372 }
15373 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015374 * 2.3.2.3 The {item type definition} must be validly derived
Daniel Veillard01fa6152004-06-29 17:04:39 +000015375 * from the {base type definition}'s {item type definition} given
Rob Richardsc6947bb2008-06-29 15:04:41 +000015376 * the empty set, as defined in Type Derivation OK (Simple) (�3.14.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000015377 */
15378 {
15379 xmlSchemaTypePtr baseItemType;
15380
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015381 baseItemType = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015382 if ((baseItemType == NULL) || (! WXS_IS_SIMPLE(baseItemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015383 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15384 "failed to eval the item type of a base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015385 return (-1);
15386 }
15387 if ((itemType != baseItemType) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015388 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt, itemType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015389 baseItemType, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015390 xmlChar *strBIT = NULL, *strBT = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015391 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015392 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015393 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015394 "The item type '%s' is not validly derived from "
15395 "the item type '%s' of the base type '%s'",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015396 xmlSchemaGetComponentQName(&str, itemType),
15397 xmlSchemaGetComponentQName(&strBIT, baseItemType),
15398 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015399
15400 FREE_AND_NULL(str)
15401 FREE_AND_NULL(strBIT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015402 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015403 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3);
15404 }
15405 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015406
Daniel Veillard01fa6152004-06-29 17:04:39 +000015407 if (type->facets != NULL) {
15408 xmlSchemaFacetPtr facet;
15409 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015410 /*
15411 * 2.3.2.4 Only length, minLength, maxLength, whiteSpace, pattern
Daniel Veillard01fa6152004-06-29 17:04:39 +000015412 * and enumeration facet components are allowed among the {facets}.
15413 */
15414 facet = type->facets;
15415 do {
15416 switch (facet->type) {
15417 case XML_SCHEMA_FACET_LENGTH:
15418 case XML_SCHEMA_FACET_MINLENGTH:
15419 case XML_SCHEMA_FACET_MAXLENGTH:
15420 case XML_SCHEMA_FACET_WHITESPACE:
15421 /*
15422 * TODO: 2.5.1.2 List datatypes
Rob Richardsc6947bb2008-06-29 15:04:41 +000015423 * The value of �whiteSpace� is fixed to the value collapse.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015424 */
15425 case XML_SCHEMA_FACET_PATTERN:
15426 case XML_SCHEMA_FACET_ENUMERATION:
15427 break;
15428 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015429 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015430 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015431 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015432 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015433 * We could return, but it's nicer to report all
Daniel Veillard01fa6152004-06-29 17:04:39 +000015434 * invalid facets.
15435 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015436 ok = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015437 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015438 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015439 facet = facet->next;
15440 } while (facet != NULL);
15441 if (ok == 0)
15442 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4);
15443 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015444 * SPEC (2.3.2.5) (same as 1.3.2)
15445 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015446 * NOTE (2.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015447 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015448 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015449 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015450 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015451 } else if (WXS_IS_UNION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015452 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015453 * 3.1 The {member type definitions} must all have {variety} of
Daniel Veillard01fa6152004-06-29 17:04:39 +000015454 * atomic or list.
15455 */
15456 xmlSchemaTypeLinkPtr member;
15457
15458 member = type->memberTypes;
15459 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015460 if (WXS_IS_TYPE_NOT_FIXED(member->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015461 xmlSchemaTypeFixup(member->type, ACTXT_CAST pctxt);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015462
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015463 if ((! WXS_IS_ATOMIC(member->type)) &&
15464 (! WXS_IS_LIST(member->type))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015465 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015466 XML_SCHEMAP_COS_ST_RESTRICTS_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015467 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015468 "The member type '%s' is neither an atomic, nor a list type",
15469 xmlSchemaGetComponentQName(&str, member->type));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015470 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015471 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_1);
15472 }
15473 member = member->next;
15474 }
15475 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000015476 * 3.3.1 If the {base type definition} is the �simple ur-type
15477 * definition�
Daniel Veillard01fa6152004-06-29 17:04:39 +000015478 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015479 if (type->baseType->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015480 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015481 * 3.3.1.1 All of the {member type definitions} must have a
Daniel Veillard01fa6152004-06-29 17:04:39 +000015482 * {final} which does not contain union.
15483 */
15484 member = type->memberTypes;
15485 while (member != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015486 if (xmlSchemaTypeFinalContains(member->type,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015487 XML_SCHEMAS_TYPE_FINAL_UNION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015488 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015489 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015490 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015491 "The 'final' of member type '%s' contains 'union'",
15492 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015493 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015494 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1);
15495 }
15496 member = member->next;
15497 }
15498 /*
15499 * 3.3.1.2 The {facets} must be empty.
15500 */
15501 if (type->facetSet != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015502 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015503 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015504 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015505 "No facets allowed", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015506 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2);
15507 }
15508 } else {
15509 /*
15510 * 3.3.2.1 The {base type definition} must have a {variety} of union.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015511 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015512 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015513 if (! WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015514 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015515 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015516 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015517 "The base type '%s' is not a union type",
15518 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015519 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015520 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1);
15521 }
15522 /*
15523 * 3.3.2.2 The {final} of the {base type definition} must not contain restriction.
15524 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015525 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015526 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015527 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015528 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015529 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015530 "The 'final' of its base type '%s' must not contain 'restriction'",
15531 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015532 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015533 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2);
15534 }
15535 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015536 * 3.3.2.3 The {member type definitions}, in order, must be validly
15537 * derived from the corresponding type definitions in the {base
15538 * type definition}'s {member type definitions} given the empty set,
Rob Richardsc6947bb2008-06-29 15:04:41 +000015539 * as defined in Type Derivation OK (Simple) (�3.14.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000015540 */
15541 {
15542 xmlSchemaTypeLinkPtr baseMember;
15543
15544 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015545 * OPTIMIZE: if the type is restricting, it has no local defined
15546 * member types and inherits the member types of the base type;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015547 * thus a check for equality can be skipped.
15548 */
15549 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015550 * Even worse: I cannot see a scenario where a restricting
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015551 * union simple type can have other member types as the member
Daniel Veillard01fa6152004-06-29 17:04:39 +000015552 * types of it's base type. This check seems not necessary with
15553 * respect to the derivation process in libxml2.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015554 * But necessary if constructing types with an API.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015555 */
15556 if (type->memberTypes != NULL) {
15557 member = type->memberTypes;
15558 baseMember = xmlSchemaGetUnionSimpleTypeMemberTypes(type->baseType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015559 if ((member == NULL) && (baseMember != NULL)) {
15560 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15561 "different number of member types in base");
15562 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015563 while (member != NULL) {
15564 if (baseMember == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015565 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15566 "different number of member types in base");
Daniel Veillard14b56432006-03-09 18:41:40 +000015567 } else if ((member->type != baseMember->type) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015568 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015569 member->type, baseMember->type, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015570 xmlChar *strBMT = NULL, *strBT = NULL;
15571
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015572 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015573 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015574 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015575 "The member type %s is not validly "
15576 "derived from its corresponding member "
15577 "type %s of the base type %s",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015578 xmlSchemaGetComponentQName(&str, member->type),
15579 xmlSchemaGetComponentQName(&strBMT, baseMember->type),
15580 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015581 FREE_AND_NULL(str)
15582 FREE_AND_NULL(strBMT)
15583 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015584 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015585 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015586 member = member->next;
15587 baseMember = baseMember->next;
15588 }
15589 }
15590 }
15591 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015592 * 3.3.2.4 Only pattern and enumeration facet components are
Daniel Veillard01fa6152004-06-29 17:04:39 +000015593 * allowed among the {facets}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015594 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015595 if (type->facets != NULL) {
15596 xmlSchemaFacetPtr facet;
15597 int ok = 1;
15598
15599 facet = type->facets;
15600 do {
15601 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
15602 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015603 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015604 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015605 type, facet);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015606 ok = 0;
15607 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015608 facet = facet->next;
15609 } while (facet != NULL);
15610 if (ok == 0)
15611 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015612
Daniel Veillard01fa6152004-06-29 17:04:39 +000015613 }
15614 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015615 * SPEC (3.3.2.5) (same as 1.3.2)
15616 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015617 * NOTE (3.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015618 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015619 */
15620 }
15621 }
15622
15623 return (0);
15624}
15625
15626/**
15627 * xmlSchemaCheckSRCSimpleType:
15628 * @ctxt: the schema parser context
15629 * @type: the simple type definition
15630 *
Daniel Veillarddee23482008-04-11 12:58:43 +000015631 * Checks crc-simple-type constraints.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015632 *
15633 * Returns 0 if the constraints are satisfied,
15634 * if not a positive error code and -1 on internal
15635 * errors.
15636 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015637#if 0
Daniel Veillard01fa6152004-06-29 17:04:39 +000015638static int
15639xmlSchemaCheckSRCSimpleType(xmlSchemaParserCtxtPtr ctxt,
15640 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015641{
15642 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015643 * src-simple-type.1 The corresponding simple type definition, if any,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015644 * must satisfy the conditions set out in Constraints on Simple Type
Rob Richardsc6947bb2008-06-29 15:04:41 +000015645 * Definition Schema Components (�3.14.6).
Daniel Veillarddee23482008-04-11 12:58:43 +000015646 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015647 if (WXS_IS_RESTRICTION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015648 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015649 * src-simple-type.2 "If the <restriction> alternative is chosen,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015650 * either it must have a base [attribute] or a <simpleType> among its
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015651 * [children], but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015652 * NOTE: This is checked in the parse function of <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015653 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015654 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000015655 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015656 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015657 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015658 /* src-simple-type.3 "If the <list> alternative is chosen, either it must have
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015659 * an itemType [attribute] or a <simpleType> among its [children],
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015660 * but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015661 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015662 * NOTE: This is checked in the parse function of <list>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015663 */
Daniel Veillarddee23482008-04-11 12:58:43 +000015664 } else if (WXS_IS_UNION(type)) {
15665 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015666 * src-simple-type.4 is checked in xmlSchemaCheckUnionTypeDefCircular().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015667 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015668 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015669 return (0);
15670}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015671#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +000015672
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015673static int
15674xmlSchemaCreateVCtxtOnPCtxt(xmlSchemaParserCtxtPtr ctxt)
15675{
15676 if (ctxt->vctxt == NULL) {
15677 ctxt->vctxt = xmlSchemaNewValidCtxt(NULL);
15678 if (ctxt->vctxt == NULL) {
15679 xmlSchemaPErr(ctxt, NULL,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000015680 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015681 "Internal error: xmlSchemaCreateVCtxtOnPCtxt, "
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015682 "failed to create a temp. validation context.\n",
15683 NULL, NULL);
15684 return (-1);
15685 }
15686 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015687 xmlSchemaSetValidErrors(ctxt->vctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000015688 ctxt->error, ctxt->warning, ctxt->errCtxt);
15689 xmlSchemaSetValidStructuredErrors(ctxt->vctxt,
15690 ctxt->serror, ctxt->errCtxt);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015691 }
15692 return (0);
15693}
15694
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015695static int
15696xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
15697 xmlNodePtr node,
15698 xmlSchemaTypePtr type,
15699 const xmlChar *value,
15700 xmlSchemaValPtr *retVal,
15701 int fireErrors,
15702 int normalize,
15703 int isNormalized);
15704
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015705/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015706 * xmlSchemaParseCheckCOSValidDefault:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015707 * @pctxt: the schema parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015708 * @type: the simple type definition
15709 * @value: the default value
15710 * @node: an optional node (the holder of the value)
15711 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015712 * Schema Component Constraint: Element Default Valid (Immediate)
15713 * (cos-valid-default)
15714 * This will be used by the parser only. For the validator there's
15715 * an other version.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015716 *
15717 * Returns 0 if the constraints are satisfied,
15718 * if not, a positive error code and -1 on internal
15719 * errors.
15720 */
15721static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015722xmlSchemaParseCheckCOSValidDefault(xmlSchemaParserCtxtPtr pctxt,
15723 xmlNodePtr node,
15724 xmlSchemaTypePtr type,
15725 const xmlChar *value,
15726 xmlSchemaValPtr *val)
15727{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015728 int ret = 0;
15729
15730 /*
15731 * cos-valid-default:
15732 * Schema Component Constraint: Element Default Valid (Immediate)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015733 * For a string to be a valid default with respect to a type
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015734 * definition the appropriate case among the following must be true:
15735 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015736 if WXS_IS_COMPLEX(type) {
William M. Brack2f2a6632004-08-20 23:09:47 +000015737 /*
15738 * Complex type.
15739 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015740 * SPEC (2.1) "its {content type} must be a simple type definition
15741 * or mixed."
15742 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
Rob Richardsc6947bb2008-06-29 15:04:41 +000015743 * type}'s particle must be �emptiable� as defined by
15744 * Particle Emptiable (�3.9.6)."
William M. Brack2f2a6632004-08-20 23:09:47 +000015745 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015746 if ((! WXS_HAS_SIMPLE_CONTENT(type)) &&
15747 ((! WXS_HAS_MIXED_CONTENT(type)) || (! WXS_EMPTIABLE(type)))) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015748 /* NOTE that this covers (2.2.2) as well. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015749 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015750 XML_SCHEMAP_COS_VALID_DEFAULT_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015751 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015752 "For a string to be a valid default, the type definition "
15753 "must be a simple type or a complex type with mixed content "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015754 "and a particle emptiable", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015755 return(XML_SCHEMAP_COS_VALID_DEFAULT_2_1);
15756 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015757 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015758 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015759 * 1 If the type definition is a simple type definition, then the string
Rob Richardsc6947bb2008-06-29 15:04:41 +000015760 * must be �valid� with respect to that definition as defined by String
15761 * Valid (�3.14.4).
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015762 *
15763 * AND
15764 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015765 * 2.2.1 If the {content type} is a simple type definition, then the
Rob Richardsc6947bb2008-06-29 15:04:41 +000015766 * string must be �valid� with respect to that simple type definition
15767 * as defined by String Valid (�3.14.4).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015768 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015769 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015770 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015771 type, value, val, 1, 1, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015772 else if (WXS_HAS_SIMPLE_CONTENT(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015773 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015774 type->contentTypeDef, value, val, 1, 1, 0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015775 else
15776 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015777
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015778 if (ret < 0) {
15779 PERROR_INT("xmlSchemaParseCheckCOSValidDefault",
15780 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015781 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015782
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015783 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000015784}
15785
15786/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015787 * xmlSchemaCheckCTPropsCorrect:
William M. Brack2f2a6632004-08-20 23:09:47 +000015788 * @ctxt: the schema parser context
15789 * @type: the complex type definition
15790 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015791 *.(4.6) Constraints on Complex Type Definition Schema Components
15792 * Schema Component Constraint:
15793 * Complex Type Definition Properties Correct (ct-props-correct)
15794 * STATUS: (seems) complete
William M. Brack2f2a6632004-08-20 23:09:47 +000015795 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015796 * Returns 0 if the constraints are satisfied, a positive
15797 * error code if not and -1 if an internal error occured.
William M. Brack2f2a6632004-08-20 23:09:47 +000015798 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015799static int
15800xmlSchemaCheckCTPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
15801 xmlSchemaTypePtr type)
William M. Brack2f2a6632004-08-20 23:09:47 +000015802{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015803 /*
15804 * TODO: Correct the error code; XML_SCHEMAP_SRC_CT_1 is used temporarily.
15805 *
15806 * SPEC (1) "The values of the properties of a complex type definition must
15807 * be as described in the property tableau in The Complex Type Definition
Rob Richardsc6947bb2008-06-29 15:04:41 +000015808 * Schema Component (�3.4.1), modulo the impact of Missing
15809 * Sub-components (�5.3)."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015810 */
15811 if ((type->baseType != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015812 (WXS_IS_SIMPLE(type->baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015813 (WXS_IS_EXTENSION(type) == 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015814 /*
15815 * SPEC (2) "If the {base type definition} is a simple type definition,
15816 * the {derivation method} must be extension."
15817 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015818 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015819 XML_SCHEMAP_SRC_CT_1,
Daniel Veillarddee23482008-04-11 12:58:43 +000015820 NULL, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015821 "If the base type is a simple type, the derivation method must be "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015822 "'extension'", NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015823 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015824 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015825 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000015826 * SPEC (3) "Circular definitions are disallowed, except for the �ur-type
15827 * definition�. That is, it must be possible to reach the �ur-type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015828 * definition by repeatedly following the {base type definition}."
15829 *
15830 * NOTE (3) is done in xmlSchemaCheckTypeDefCircular().
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015831 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015832 /*
15833 * NOTE that (4) and (5) need the following:
15834 * - attribute uses need to be already inherited (apply attr. prohibitions)
15835 * - attribute group references need to be expanded already
15836 * - simple types need to be typefixed already
Daniel Veillarddee23482008-04-11 12:58:43 +000015837 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015838 if (type->attrUses &&
15839 (((xmlSchemaItemListPtr) type->attrUses)->nbItems > 1))
15840 {
15841 xmlSchemaItemListPtr uses = (xmlSchemaItemListPtr) type->attrUses;
15842 xmlSchemaAttributeUsePtr use, tmp;
15843 int i, j, hasId = 0;
15844
15845 for (i = uses->nbItems -1; i >= 0; i--) {
15846 use = uses->items[i];
Daniel Veillarddee23482008-04-11 12:58:43 +000015847
15848 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015849 * SPEC ct-props-correct
15850 * (4) "Two distinct attribute declarations in the
15851 * {attribute uses} must not have identical {name}s and
15852 * {target namespace}s."
15853 */
15854 if (i > 0) {
15855 for (j = i -1; j >= 0; j--) {
15856 tmp = uses->items[j];
15857 if ((WXS_ATTRUSE_DECL_NAME(use) ==
15858 WXS_ATTRUSE_DECL_NAME(tmp)) &&
15859 (WXS_ATTRUSE_DECL_TNS(use) ==
15860 WXS_ATTRUSE_DECL_TNS(tmp)))
15861 {
15862 xmlChar *str = NULL;
15863
15864 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15865 XML_SCHEMAP_AG_PROPS_CORRECT,
15866 NULL, WXS_BASIC_CAST type,
15867 "Duplicate %s",
15868 xmlSchemaGetComponentDesignation(&str, use),
15869 NULL);
15870 FREE_AND_NULL(str);
15871 /*
15872 * Remove the duplicate.
15873 */
15874 if (xmlSchemaItemListRemove(uses, i) == -1)
15875 goto exit_failure;
15876 goto next_use;
15877 }
15878 }
15879 }
15880 /*
15881 * SPEC ct-props-correct
15882 * (5) "Two distinct attribute declarations in the
15883 * {attribute uses} must not have {type definition}s which
15884 * are or are derived from ID."
15885 */
15886 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
15887 if (xmlSchemaIsDerivedFromBuiltInType(
15888 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
Daniel Veillarddee23482008-04-11 12:58:43 +000015889 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015890 if (hasId) {
15891 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000015892
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015893 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15894 XML_SCHEMAP_AG_PROPS_CORRECT,
15895 NULL, WXS_BASIC_CAST type,
15896 "There must not exist more than one attribute "
15897 "declaration of type 'xs:ID' "
15898 "(or derived from 'xs:ID'). The %s violates this "
15899 "constraint",
15900 xmlSchemaGetComponentDesignation(&str, use),
15901 NULL);
15902 FREE_AND_NULL(str);
15903 if (xmlSchemaItemListRemove(uses, i) == -1)
15904 goto exit_failure;
15905 }
Daniel Veillarddee23482008-04-11 12:58:43 +000015906
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015907 hasId = 1;
15908 }
15909 }
15910next_use: {}
15911 }
15912 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015913 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015914exit_failure:
15915 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +000015916}
15917
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015918static int
15919xmlSchemaAreEqualTypes(xmlSchemaTypePtr typeA,
15920 xmlSchemaTypePtr typeB)
15921{
15922 /*
15923 * TODO: This should implement component-identity
15924 * in the future.
15925 */
15926 if ((typeA == NULL) || (typeB == NULL))
15927 return (0);
15928 return (typeA == typeB);
15929}
15930
15931/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015932 * xmlSchemaCheckCOSCTDerivedOK:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015933 * @ctxt: the schema parser context
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015934 * @type: the to-be derived complex type definition
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015935 * @baseType: the base complex type definition
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015936 * @set: the given set
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015937 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015938 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015939 * Type Derivation OK (Complex) (cos-ct-derived-ok)
15940 *
15941 * STATUS: completed
15942 *
15943 * Returns 0 if the constraints are satisfied, or 1
15944 * if not.
15945 */
15946static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015947xmlSchemaCheckCOSCTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015948 xmlSchemaTypePtr type,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015949 xmlSchemaTypePtr baseType,
15950 int set)
15951{
15952 int equal = xmlSchemaAreEqualTypes(type, baseType);
15953 /* TODO: Error codes. */
15954 /*
15955 * SPEC "For a complex type definition (call it D, for derived)
15956 * to be validly derived from a type definition (call this
15957 * B, for base) given a subset of {extension, restriction}
15958 * all of the following must be true:"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015959 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015960 if (! equal) {
15961 /*
15962 * SPEC (1) "If B and D are not the same type definition, then the
15963 * {derivation method} of D must not be in the subset."
15964 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015965 if (((set & SUBSET_EXTENSION) && (WXS_IS_EXTENSION(type))) ||
15966 ((set & SUBSET_RESTRICTION) && (WXS_IS_RESTRICTION(type))))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015967 return (1);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015968 } else {
15969 /*
15970 * SPEC (2.1) "B and D must be the same type definition."
15971 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015972 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015973 }
15974 /*
15975 * SPEC (2.2) "B must be D's {base type definition}."
15976 */
15977 if (type->baseType == baseType)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015978 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015979 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000015980 * SPEC (2.3.1) "D's {base type definition} must not be the �ur-type
15981 * definition�."
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015982 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015983 if (WXS_IS_ANYTYPE(type->baseType))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015984 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015985
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015986 if (WXS_IS_COMPLEX(type->baseType)) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015987 /*
15988 * SPEC (2.3.2.1) "If D's {base type definition} is complex, then it
15989 * must be validly derived from B given the subset as defined by this
15990 * constraint."
15991 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015992 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015993 baseType, set));
15994 } else {
15995 /*
15996 * SPEC (2.3.2.2) "If D's {base type definition} is simple, then it
15997 * must be validly derived from B given the subset as defined in Type
Rob Richardsc6947bb2008-06-29 15:04:41 +000015998 * Derivation OK (Simple) (�3.14.6).
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015999 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016000 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016001 baseType, set));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016002 }
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016003}
16004
16005/**
16006 * xmlSchemaCheckCOSDerivedOK:
16007 * @type: the derived simple type definition
16008 * @baseType: the base type definition
16009 *
16010 * Calls:
16011 * Type Derivation OK (Simple) AND Type Derivation OK (Complex)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016012 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016013 * Checks wheter @type can be validly derived from @baseType.
16014 *
16015 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016016 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016017static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016018xmlSchemaCheckCOSDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016019 xmlSchemaTypePtr type,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016020 xmlSchemaTypePtr baseType,
16021 int set)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016022{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016023 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016024 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016025 else
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016026 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016027}
16028
William M. Brack2f2a6632004-08-20 23:09:47 +000016029/**
16030 * xmlSchemaCheckCOSCTExtends:
16031 * @ctxt: the schema parser context
16032 * @type: the complex type definition
16033 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016034 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016035 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016036 * Derivation Valid (Extension) (cos-ct-extends)
16037 *
16038 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016039 * missing:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016040 * (1.5)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016041 * (1.4.3.2.2.2) "Particle Valid (Extension)"
William M. Brack2f2a6632004-08-20 23:09:47 +000016042 *
16043 * Returns 0 if the constraints are satisfied, a positive
16044 * error code if not and -1 if an internal error occured.
16045 */
16046static int
16047xmlSchemaCheckCOSCTExtends(xmlSchemaParserCtxtPtr ctxt,
16048 xmlSchemaTypePtr type)
16049{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016050 xmlSchemaTypePtr base = type->baseType;
16051 /*
16052 * TODO: Correct the error code; XML_SCHEMAP_COS_CT_EXTENDS_1_1 is used
16053 * temporarily only.
William M. Brack2f2a6632004-08-20 23:09:47 +000016054 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016055 /*
16056 * SPEC (1) "If the {base type definition} is a complex type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016057 * then all of the following must be true:"
16058 */
Daniel Veillarddee23482008-04-11 12:58:43 +000016059 if (WXS_IS_COMPLEX(base)) {
William M. Brack2f2a6632004-08-20 23:09:47 +000016060 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016061 * SPEC (1.1) "The {final} of the {base type definition} must not
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016062 * contain extension."
William M. Brack2f2a6632004-08-20 23:09:47 +000016063 */
16064 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16065 xmlSchemaPCustomErr(ctxt,
16066 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016067 WXS_BASIC_CAST type, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000016068 "The 'final' of the base type definition "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016069 "contains 'extension'", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000016070 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16071 }
Daniel Veillarddee23482008-04-11 12:58:43 +000016072
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016073 /*
16074 * ATTENTION: The constrains (1.2) and (1.3) are not applied,
16075 * since they are automatically satisfied through the
16076 * inheriting mechanism.
16077 * Note that even if redefining components, the inheriting mechanism
16078 * is used.
16079 */
16080#if 0
William M. Brack2f2a6632004-08-20 23:09:47 +000016081 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016082 * SPEC (1.2) "Its {attribute uses} must be a subset of the {attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016083 * uses}
16084 * of the complex type definition itself, that is, for every attribute
16085 * use in the {attribute uses} of the {base type definition}, there
16086 * must be an attribute use in the {attribute uses} of the complex
16087 * type definition itself whose {attribute declaration} has the same
16088 * {name}, {target namespace} and {type definition} as its attribute
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016089 * declaration"
William M. Brack2f2a6632004-08-20 23:09:47 +000016090 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016091 if (base->attrUses != NULL) {
16092 int i, j, found;
16093 xmlSchemaAttributeUsePtr use, buse;
William M. Brack2f2a6632004-08-20 23:09:47 +000016094
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016095 for (i = 0; i < (WXS_LIST_CAST base->attrUses)->nbItems; i ++) {
16096 buse = (WXS_LIST_CAST base->attrUses)->items[i];
16097 found = 0;
16098 if (type->attrUses != NULL) {
16099 use = (WXS_LIST_CAST type->attrUses)->items[j];
16100 for (j = 0; j < (WXS_LIST_CAST type->attrUses)->nbItems; j ++)
16101 {
16102 if ((WXS_ATTRUSE_DECL_NAME(use) ==
16103 WXS_ATTRUSE_DECL_NAME(buse)) &&
16104 (WXS_ATTRUSE_DECL_TNS(use) ==
16105 WXS_ATTRUSE_DECL_TNS(buse)) &&
16106 (WXS_ATTRUSE_TYPEDEF(use) ==
16107 WXS_ATTRUSE_TYPEDEF(buse))
16108 {
16109 found = 1;
16110 break;
16111 }
16112 }
16113 }
16114 if (! found) {
16115 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000016116
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016117 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16118 XML_SCHEMAP_COS_CT_EXTENDS_1_2,
16119 NULL, WXS_BASIC_CAST type,
Daniel Veillarddee23482008-04-11 12:58:43 +000016120 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016121 * TODO: The report does not indicate that also the
16122 * type needs to be the same.
16123 */
16124 "This type is missing a matching correspondent "
16125 "for its {base type}'s %s in its {attribute uses}",
16126 xmlSchemaGetComponentDesignation(&str,
16127 buse->children),
16128 NULL);
16129 FREE_AND_NULL(str)
16130 }
16131 }
16132 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016133 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016134 * SPEC (1.3) "If it has an {attribute wildcard}, the complex type
16135 * definition must also have one, and the base type definition's
16136 * {attribute wildcard}'s {namespace constraint} must be a subset
16137 * of the complex type definition's {attribute wildcard}'s {namespace
Rob Richardsc6947bb2008-06-29 15:04:41 +000016138 * constraint}, as defined by Wildcard Subset (�3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016139 */
Daniel Veillarddee23482008-04-11 12:58:43 +000016140
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016141 /*
16142 * MAYBE TODO: Enable if ever needed. But this will be needed only
16143 * if created the type via a schema construction API.
16144 */
16145 if (base->attributeWildcard != NULL) {
16146 if (type->attributeWilcard == NULL) {
16147 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000016148
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016149 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16150 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16151 NULL, type,
16152 "The base %s has an attribute wildcard, "
16153 "but this type is missing an attribute wildcard",
16154 xmlSchemaGetComponentDesignation(&str, base));
16155 FREE_AND_NULL(str)
16156
16157 } else if (xmlSchemaCheckCOSNSSubset(
16158 base->attributeWildcard, type->attributeWildcard))
16159 {
16160 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000016161
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016162 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16163 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16164 NULL, type,
16165 "The attribute wildcard is not a valid "
16166 "superset of the one in the base %s",
16167 xmlSchemaGetComponentDesignation(&str, base));
16168 FREE_AND_NULL(str)
16169 }
16170 }
16171#endif
16172 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016173 * SPEC (1.4) "One of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016174 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016175 if ((type->contentTypeDef != NULL) &&
16176 (type->contentTypeDef == base->contentTypeDef)) {
16177 /*
16178 * SPEC (1.4.1) "The {content type} of the {base type definition}
16179 * and the {content type} of the complex type definition itself
16180 * must be the same simple type definition"
16181 * PASS
16182 */
16183 } else if ((type->contentType == XML_SCHEMA_CONTENT_EMPTY) &&
16184 (base->contentType == XML_SCHEMA_CONTENT_EMPTY) ) {
16185 /*
16186 * SPEC (1.4.2) "The {content type} of both the {base type
16187 * definition} and the complex type definition itself must
16188 * be empty."
16189 * PASS
16190 */
16191 } else {
16192 /*
16193 * SPEC (1.4.3) "All of the following must be true:"
16194 */
16195 if (type->subtypes == NULL) {
16196 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016197 * SPEC 1.4.3.1 The {content type} of the complex type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016198 * definition itself must specify a particle.
16199 */
16200 xmlSchemaPCustomErr(ctxt,
16201 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016202 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016203 "The content type must specify a particle", NULL);
16204 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16205 }
16206 /*
16207 * SPEC (1.4.3.2) "One of the following must be true:"
16208 */
16209 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16210 /*
16211 * SPEC (1.4.3.2.1) "The {content type} of the {base type
16212 * definition} must be empty.
16213 * PASS
16214 */
16215 } else {
16216 /*
16217 * SPEC (1.4.3.2.2) "All of the following must be true:"
16218 */
16219 if ((type->contentType != base->contentType) ||
16220 ((type->contentType != XML_SCHEMA_CONTENT_MIXED) &&
16221 (type->contentType != XML_SCHEMA_CONTENT_ELEMENTS))) {
16222 /*
16223 * SPEC (1.4.3.2.2.1) "Both {content type}s must be mixed
16224 * or both must be element-only."
16225 */
16226 xmlSchemaPCustomErr(ctxt,
16227 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016228 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016229 "The content type of both, the type and its base "
16230 "type, must either 'mixed' or 'element-only'", NULL);
16231 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016232 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016233 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016234 * URGENT TODO SPEC (1.4.3.2.2.2) "The particle of the
Rob Richardsc6947bb2008-06-29 15:04:41 +000016235 * complex type definition must be a �valid extension�
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016236 * of the {base type definition}'s particle, as defined
Rob Richardsc6947bb2008-06-29 15:04:41 +000016237 * in Particle Valid (Extension) (�3.9.6)."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016238 *
16239 * NOTE that we won't check "Particle Valid (Extension)",
16240 * since it is ensured by the derivation process in
16241 * xmlSchemaTypeFixup(). We need to implement this when heading
16242 * for a construction API
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016243 * TODO: !! This is needed to be checked if redefining a type !!
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016244 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016245 }
16246 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016247 * URGENT TODO (1.5)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016248 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016249 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016250 } else {
16251 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016252 * SPEC (2) "If the {base type definition} is a simple type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016253 * then all of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016254 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016255 if (type->contentTypeDef != base) {
16256 /*
16257 * SPEC (2.1) "The {content type} must be the same simple type
16258 * definition."
16259 */
16260 xmlSchemaPCustomErr(ctxt,
16261 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016262 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016263 "The content type must be the simple base type", NULL);
16264 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16265 }
16266 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16267 /*
16268 * SPEC (2.2) "The {final} of the {base type definition} must not
16269 * contain extension"
16270 * NOTE that this is the same as (1.1).
16271 */
16272 xmlSchemaPCustomErr(ctxt,
16273 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016274 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016275 "The 'final' of the base type definition "
16276 "contains 'extension'", NULL);
16277 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016278 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016279 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016280 return (0);
William M. Brack2f2a6632004-08-20 23:09:47 +000016281}
16282
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016283/**
16284 * xmlSchemaCheckDerivationOKRestriction:
16285 * @ctxt: the schema parser context
16286 * @type: the complex type definition
16287 *
16288 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016289 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016290 * Derivation Valid (Restriction, Complex) (derivation-ok-restriction)
16291 *
16292 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016293 * missing:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016294 * (5.4.2) ???
16295 *
16296 * ATTENTION:
16297 * In XML Schema 1.1 this will be:
16298 * Validation Rule: Checking complex type subsumption
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016299 *
16300 * Returns 0 if the constraints are satisfied, a positive
16301 * error code if not and -1 if an internal error occured.
16302 */
16303static int
16304xmlSchemaCheckDerivationOKRestriction(xmlSchemaParserCtxtPtr ctxt,
16305 xmlSchemaTypePtr type)
16306{
16307 xmlSchemaTypePtr base;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016308
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016309 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016310 * TODO: Correct the error code; XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1 is used
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016311 * temporarily only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016312 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016313 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016314 if (! WXS_IS_COMPLEX(base)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000016315 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016316 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16317 type->node, WXS_BASIC_CAST type,
16318 "The base type must be a complex type", NULL, NULL);
16319 return(ctxt->err);
16320 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016321 if (base->flags & XML_SCHEMAS_TYPE_FINAL_RESTRICTION) {
16322 /*
16323 * SPEC (1) "The {base type definition} must be a complex type
16324 * definition whose {final} does not contain restriction."
16325 */
Daniel Veillarddee23482008-04-11 12:58:43 +000016326 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016327 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16328 type->node, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016329 "The 'final' of the base type definition "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016330 "contains 'restriction'", NULL, NULL);
16331 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016332 }
16333 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016334 * SPEC (2), (3) and (4)
16335 * Those are handled in a separate function, since the
16336 * same constraints are needed for redefinition of
16337 * attribute groups as well.
16338 */
16339 if (xmlSchemaCheckDerivationOKRestriction2to4(ctxt,
16340 XML_SCHEMA_ACTION_DERIVE,
16341 WXS_BASIC_CAST type, WXS_BASIC_CAST base,
16342 type->attrUses, base->attrUses,
16343 type->attributeWildcard,
16344 base->attributeWildcard) == -1)
16345 {
16346 return(-1);
16347 }
16348 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016349 * SPEC (5) "One of the following must be true:"
16350 */
16351 if (base->builtInType == XML_SCHEMAS_ANYTYPE) {
16352 /*
16353 * SPEC (5.1) "The {base type definition} must be the
Rob Richardsc6947bb2008-06-29 15:04:41 +000016354 * �ur-type definition�."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016355 * PASS
16356 */
16357 } else if ((type->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16358 (type->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16359 /*
16360 * SPEC (5.2.1) "The {content type} of the complex type definition
16361 * must be a simple type definition"
16362 *
16363 * SPEC (5.2.2) "One of the following must be true:"
16364 */
16365 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016366 (base->contentType == XML_SCHEMA_CONTENT_BASIC))
16367 {
16368 int err;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016369 /*
16370 * SPEC (5.2.2.1) "The {content type} of the {base type
16371 * definition} must be a simple type definition from which
16372 * the {content type} is validly derived given the empty
Rob Richardsc6947bb2008-06-29 15:04:41 +000016373 * set as defined in Type Derivation OK (Simple) (�3.14.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016374 *
16375 * ATTENTION TODO: This seems not needed if the type implicitely
16376 * derived from the base type.
Daniel Veillarddee23482008-04-11 12:58:43 +000016377 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016378 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016379 err = xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST ctxt,
16380 type->contentTypeDef, base->contentTypeDef, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016381 if (err != 0) {
16382 xmlChar *strA = NULL, *strB = NULL;
16383
16384 if (err == -1)
16385 return(-1);
16386 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16387 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16388 NULL, WXS_BASIC_CAST type,
16389 "The {content type} %s is not validly derived from the "
16390 "base type's {content type} %s",
16391 xmlSchemaGetComponentDesignation(&strA,
16392 type->contentTypeDef),
16393 xmlSchemaGetComponentDesignation(&strB,
16394 base->contentTypeDef));
16395 FREE_AND_NULL(strA);
16396 FREE_AND_NULL(strB);
16397 return(ctxt->err);
16398 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016399 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
16400 (xmlSchemaIsParticleEmptiable(
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016401 (xmlSchemaParticlePtr) base->subtypes))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016402 /*
16403 * SPEC (5.2.2.2) "The {base type definition} must be mixed
Rob Richardsc6947bb2008-06-29 15:04:41 +000016404 * and have a particle which is �emptiable� as defined in
16405 * Particle Emptiable (�3.9.6)."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016406 * PASS
16407 */
16408 } else {
16409 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016410 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16411 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016412 "The content type of the base type must be either "
16413 "a simple type or 'mixed' and an emptiable particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016414 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016415 }
16416 } else if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16417 /*
16418 * SPEC (5.3.1) "The {content type} of the complex type itself must
16419 * be empty"
16420 */
16421 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16422 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016423 * SPEC (5.3.2.1) "The {content type} of the {base type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016424 * definition} must also be empty."
16425 * PASS
16426 */
16427 } else if (((base->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
16428 (base->contentType == XML_SCHEMA_CONTENT_MIXED)) &&
16429 xmlSchemaIsParticleEmptiable(
16430 (xmlSchemaParticlePtr) base->subtypes)) {
16431 /*
16432 * SPEC (5.3.2.2) "The {content type} of the {base type
16433 * definition} must be elementOnly or mixed and have a particle
Rob Richardsc6947bb2008-06-29 15:04:41 +000016434 * which is �emptiable� as defined in Particle Emptiable (�3.9.6)."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016435 * PASS
16436 */
16437 } 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 content type of the base type must be either "
16442 "empty or 'mixed' (or 'elements-only') and an emptiable "
16443 "particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016444 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016445 }
16446 } else if ((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016447 WXS_HAS_MIXED_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016448 /*
16449 * SPEC (5.4.1.1) "The {content type} of the complex type definition
16450 * itself must be element-only"
Daniel Veillarddee23482008-04-11 12:58:43 +000016451 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016452 if (WXS_HAS_MIXED_CONTENT(type) && (! WXS_HAS_MIXED_CONTENT(base))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016453 /*
16454 * SPEC (5.4.1.2) "The {content type} of the complex type
16455 * definition itself and of the {base type definition} must be
16456 * mixed"
16457 */
16458 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016459 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16460 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016461 "If the content type is 'mixed', then the content type of the "
16462 "base type must also be 'mixed'", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016463 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016464 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016465 /*
16466 * SPEC (5.4.2) "The particle of the complex type definition itself
Rob Richardsc6947bb2008-06-29 15:04:41 +000016467 * must be a �valid restriction� of the particle of the {content
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016468 * type} of the {base type definition} as defined in Particle Valid
Rob Richardsc6947bb2008-06-29 15:04:41 +000016469 * (Restriction) (�3.9.6).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016470 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016471 * URGENT TODO: (5.4.2)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016472 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016473 } else {
16474 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016475 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16476 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016477 "The type is not a valid restriction of its base type", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016478 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016479 }
16480 return (0);
16481}
16482
16483/**
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016484 * xmlSchemaCheckCTComponent:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016485 * @ctxt: the schema parser context
16486 * @type: the complex type definition
16487 *
16488 * (3.4.6) Constraints on Complex Type Definition Schema Components
16489 *
16490 * Returns 0 if the constraints are satisfied, a positive
16491 * error code if not and -1 if an internal error occured.
16492 */
16493static int
16494xmlSchemaCheckCTComponent(xmlSchemaParserCtxtPtr ctxt,
16495 xmlSchemaTypePtr type)
16496{
16497 int ret;
16498 /*
16499 * Complex Type Definition Properties Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016500 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016501 ret = xmlSchemaCheckCTPropsCorrect(ctxt, type);
16502 if (ret != 0)
16503 return (ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016504 if (WXS_IS_EXTENSION(type))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016505 ret = xmlSchemaCheckCOSCTExtends(ctxt, type);
16506 else
16507 ret = xmlSchemaCheckDerivationOKRestriction(ctxt, type);
16508 return (ret);
16509}
16510
16511/**
16512 * xmlSchemaCheckSRCCT:
16513 * @ctxt: the schema parser context
16514 * @type: the complex type definition
16515 *
16516 * (3.4.3) Constraints on XML Representations of Complex Type Definitions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016517 * Schema Representation Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016518 * Complex Type Definition Representation OK (src-ct)
16519 *
16520 * Returns 0 if the constraints are satisfied, a positive
16521 * error code if not and -1 if an internal error occured.
16522 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016523static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016524xmlSchemaCheckSRCCT(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016525 xmlSchemaTypePtr type)
16526{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016527 xmlSchemaTypePtr base;
16528 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016529
16530 /*
16531 * TODO: Adjust the error codes here, as I used
16532 * XML_SCHEMAP_SRC_CT_1 only yet.
16533 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016534 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016535 if (! WXS_HAS_SIMPLE_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016536 /*
16537 * 1 If the <complexContent> alternative is chosen, the type definition
Rob Richardsc6947bb2008-06-29 15:04:41 +000016538 * �resolved� to by the �actual value� of the base [attribute]
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016539 * must be a complex type definition;
16540 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016541 if (! WXS_IS_COMPLEX(base)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016542 xmlChar *str = NULL;
16543 xmlSchemaPCustomErr(ctxt,
16544 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016545 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016546 "If using <complexContent>, the base type is expected to be "
16547 "a complex type. The base type '%s' is a simple type",
16548 xmlSchemaFormatQName(&str, base->targetNamespace,
16549 base->name));
16550 FREE_AND_NULL(str)
16551 return (XML_SCHEMAP_SRC_CT_1);
16552 }
16553 } else {
16554 /*
16555 * SPEC
16556 * 2 If the <simpleContent> alternative is chosen, all of the
16557 * following must be true:
Rob Richardsc6947bb2008-06-29 15:04:41 +000016558 * 2.1 The type definition �resolved� to by the �actual value� of the
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016559 * base [attribute] must be one of the following:
16560 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016561 if (WXS_IS_SIMPLE(base)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016562 if (WXS_IS_EXTENSION(type) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016563 xmlChar *str = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016564 /*
16565 * 2.1.3 only if the <extension> alternative is also
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016566 * chosen, a simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016567 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016568 /* TODO: Change error code to ..._SRC_CT_2_1_3. */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016569 xmlSchemaPCustomErr(ctxt,
16570 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016571 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016572 "If using <simpleContent> and <restriction>, the base "
16573 "type must be a complex type. The base type '%s' is "
16574 "a simple type",
16575 xmlSchemaFormatQName(&str, base->targetNamespace,
16576 base->name));
16577 FREE_AND_NULL(str)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016578 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016579 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016580 } else {
16581 /* Base type is a complex type. */
16582 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16583 (base->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16584 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016585 * 2.1.1 a complex type definition whose {content type} is a
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016586 * simple type definition;
16587 * PASS
16588 */
16589 if (base->contentTypeDef == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016590 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016591 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016592 "Internal error: xmlSchemaCheckSRCCT, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016593 "'%s', base type has no content type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016594 type->name);
16595 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016596 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016597 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016598 (WXS_IS_RESTRICTION(type))) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016599
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016600 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016601 * 2.1.2 only if the <restriction> alternative is also
16602 * chosen, a complex type definition whose {content type}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016603 * is mixed and a particle emptiable.
16604 */
16605 if (! xmlSchemaIsParticleEmptiable(
16606 (xmlSchemaParticlePtr) base->subtypes)) {
16607 ret = XML_SCHEMAP_SRC_CT_1;
Daniel Veillarddee23482008-04-11 12:58:43 +000016608 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016609 /*
16610 * Attention: at this point the <simpleType> child is in
16611 * ->contentTypeDef (put there during parsing).
Daniel Veillarddee23482008-04-11 12:58:43 +000016612 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016613 if (type->contentTypeDef == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016614 xmlChar *str = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016615 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016616 * 2.2 If clause 2.1.2 above is satisfied, then there
16617 * must be a <simpleType> among the [children] of
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016618 * <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016619 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016620 /* TODO: Change error code to ..._SRC_CT_2_2. */
16621 xmlSchemaPCustomErr(ctxt,
16622 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016623 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016624 "A <simpleType> is expected among the children "
16625 "of <restriction>, if <simpleContent> is used and "
16626 "the base type '%s' is a complex type",
16627 xmlSchemaFormatQName(&str, base->targetNamespace,
16628 base->name));
16629 FREE_AND_NULL(str)
16630 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016631 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016632 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016633 ret = XML_SCHEMAP_SRC_CT_1;
16634 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016635 }
16636 if (ret > 0) {
16637 xmlChar *str = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016638 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016639 xmlSchemaPCustomErr(ctxt,
16640 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016641 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016642 "If <simpleContent> and <restriction> is used, the "
16643 "base type must be a simple type or a complex type with "
16644 "mixed content and particle emptiable. The base type "
16645 "'%s' is none of those",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016646 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016647 base->name));
16648 } else {
16649 xmlSchemaPCustomErr(ctxt,
16650 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016651 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016652 "If <simpleContent> and <extension> is used, the "
16653 "base type must be a simple type. The base type '%s' "
16654 "is a complex type",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016655 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016656 base->name));
16657 }
16658 FREE_AND_NULL(str)
16659 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016660 }
16661 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016662 * SPEC (3) "The corresponding complex type definition component must
16663 * satisfy the conditions set out in Constraints on Complex Type
Rob Richardsc6947bb2008-06-29 15:04:41 +000016664 * Definition Schema Components (�3.4.6);"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016665 * NOTE (3) will be done in xmlSchemaTypeFixup().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016666 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016667 /*
16668 * SPEC (4) If clause 2.2.1 or clause 2.2.2 in the correspondence specification
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016669 * above for {attribute wildcard} is satisfied, the intensional
16670 * intersection must be expressible, as defined in Attribute Wildcard
Rob Richardsc6947bb2008-06-29 15:04:41 +000016671 * Intersection (�3.10.6).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016672 * NOTE (4) is done in xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016673 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016674 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016675}
William M. Brack2f2a6632004-08-20 23:09:47 +000016676
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016677#ifdef ENABLE_PARTICLE_RESTRICTION
16678/**
16679 * xmlSchemaCheckParticleRangeOK:
16680 * @ctxt: the schema parser context
16681 * @type: the complex type definition
16682 *
16683 * (3.9.6) Constraints on Particle Schema Components
16684 * Schema Component Constraint:
16685 * Occurrence Range OK (range-ok)
16686 *
16687 * STATUS: complete
16688 *
16689 * Returns 0 if the constraints are satisfied, a positive
16690 * error code if not and -1 if an internal error occured.
16691 */
16692static int
16693xmlSchemaCheckParticleRangeOK(int rmin, int rmax,
16694 int bmin, int bmax)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016695{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016696 if (rmin < bmin)
16697 return (1);
16698 if ((bmax != UNBOUNDED) &&
16699 (rmax > bmax))
16700 return (1);
16701 return (0);
16702}
16703
16704/**
16705 * xmlSchemaCheckRCaseNameAndTypeOK:
16706 * @ctxt: the schema parser context
16707 * @r: the restricting element declaration particle
16708 * @b: the base element declaration particle
16709 *
16710 * (3.9.6) Constraints on Particle Schema Components
16711 * Schema Component Constraint:
16712 * Particle Restriction OK (Elt:Elt -- NameAndTypeOK)
16713 * (rcase-NameAndTypeOK)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016714 *
16715 * STATUS:
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016716 * MISSING (3.2.3)
16717 * CLARIFY: (3.2.2)
16718 *
16719 * Returns 0 if the constraints are satisfied, a positive
16720 * error code if not and -1 if an internal error occured.
16721 */
16722static int
16723xmlSchemaCheckRCaseNameAndTypeOK(xmlSchemaParserCtxtPtr ctxt,
16724 xmlSchemaParticlePtr r,
16725 xmlSchemaParticlePtr b)
16726{
16727 xmlSchemaElementPtr elemR, elemB;
16728
16729 /* TODO: Error codes (rcase-NameAndTypeOK). */
16730 elemR = (xmlSchemaElementPtr) r->children;
16731 elemB = (xmlSchemaElementPtr) b->children;
16732 /*
16733 * SPEC (1) "The declarations' {name}s and {target namespace}s are
16734 * the same."
16735 */
16736 if ((elemR != elemB) &&
16737 ((! xmlStrEqual(elemR->name, elemB->name)) ||
16738 (! xmlStrEqual(elemR->targetNamespace, elemB->targetNamespace))))
16739 return (1);
16740 /*
16741 * SPEC (2) "R's occurrence range is a valid restriction of B's
Rob Richardsc6947bb2008-06-29 15:04:41 +000016742 * occurrence range as defined by Occurrence Range OK (�3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016743 */
16744 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16745 b->minOccurs, b->maxOccurs) != 0)
16746 return (1);
16747 /*
16748 * SPEC (3.1) "Both B's declaration's {scope} and R's declaration's
16749 * {scope} are global."
16750 */
16751 if (elemR == elemB)
16752 return (0);
16753 /*
16754 * SPEC (3.2.1) "Either B's {nillable} is true or R's {nillable} is false."
16755 */
16756 if (((elemB->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) &&
16757 (elemR->flags & XML_SCHEMAS_ELEM_NILLABLE))
16758 return (1);
16759 /*
16760 * SPEC (3.2.2) "either B's declaration's {value constraint} is absent,
16761 * or is not fixed, or R's declaration's {value constraint} is fixed
16762 * with the same value."
16763 */
16764 if ((elemB->value != NULL) && (elemB->flags & XML_SCHEMAS_ELEM_FIXED) &&
16765 ((elemR->value == NULL) ||
16766 ((elemR->flags & XML_SCHEMAS_ELEM_FIXED) == 0) ||
16767 /* TODO: Equality of the initial value or normalized or canonical? */
16768 (! xmlStrEqual(elemR->value, elemB->value))))
16769 return (1);
16770 /*
16771 * TODO: SPEC (3.2.3) "R's declaration's {identity-constraint
16772 * definitions} is a subset of B's declaration's {identity-constraint
16773 * definitions}, if any."
16774 */
16775 if (elemB->idcs != NULL) {
16776 /* TODO */
16777 }
16778 /*
16779 * SPEC (3.2.4) "R's declaration's {disallowed substitutions} is a
16780 * superset of B's declaration's {disallowed substitutions}."
16781 */
16782 if (((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) &&
16783 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) == 0)) ||
16784 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) &&
16785 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) == 0)) ||
16786 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) &&
16787 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) == 0)))
16788 return (1);
16789 /*
16790 * SPEC (3.2.5) "R's {type definition} is validly derived given
16791 * {extension, list, union} from B's {type definition}"
16792 *
16793 * BADSPEC TODO: What's the point of adding "list" and "union" to the
16794 * set, if the corresponding constraints handle "restriction" and
16795 * "extension" only?
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016796 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016797 */
16798 {
16799 int set = 0;
16800
16801 set |= SUBSET_EXTENSION;
16802 set |= SUBSET_LIST;
16803 set |= SUBSET_UNION;
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016804 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST ctxt, elemR->subtypes,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016805 elemB->subtypes, set) != 0)
16806 return (1);
16807 }
16808 return (0);
16809}
16810
16811/**
16812 * xmlSchemaCheckRCaseNSCompat:
16813 * @ctxt: the schema parser context
16814 * @r: the restricting element declaration particle
16815 * @b: the base wildcard particle
16816 *
16817 * (3.9.6) Constraints on Particle Schema Components
16818 * Schema Component Constraint:
16819 * Particle Derivation OK (Elt:Any -- NSCompat)
16820 * (rcase-NSCompat)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016821 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016822 * STATUS: complete
16823 *
16824 * Returns 0 if the constraints are satisfied, a positive
16825 * error code if not and -1 if an internal error occured.
16826 */
16827static int
16828xmlSchemaCheckRCaseNSCompat(xmlSchemaParserCtxtPtr ctxt,
16829 xmlSchemaParticlePtr r,
16830 xmlSchemaParticlePtr b)
16831{
16832 /* TODO:Error codes (rcase-NSCompat). */
16833 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000016834 * SPEC "For an element declaration particle to be a �valid restriction�
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016835 * of a wildcard particle all of the following must be true:"
16836 *
Rob Richardsc6947bb2008-06-29 15:04:41 +000016837 * SPEC (1) "The element declaration's {target namespace} is �valid�
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016838 * with respect to the wildcard's {namespace constraint} as defined by
Rob Richardsc6947bb2008-06-29 15:04:41 +000016839 * Wildcard allows Namespace Name (�3.10.4)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016840 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016841 if (xmlSchemaCheckCVCWildcardNamespace((xmlSchemaWildcardPtr) b->children,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016842 ((xmlSchemaElementPtr) r->children)->targetNamespace) != 0)
16843 return (1);
16844 /*
16845 * SPEC (2) "R's occurrence range is a valid restriction of B's
Rob Richardsc6947bb2008-06-29 15:04:41 +000016846 * occurrence range as defined by Occurrence Range OK (�3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016847 */
16848 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16849 b->minOccurs, b->maxOccurs) != 0)
16850 return (1);
16851
16852 return (0);
16853}
16854
16855/**
16856 * xmlSchemaCheckRCaseRecurseAsIfGroup:
16857 * @ctxt: the schema parser context
16858 * @r: the restricting element declaration particle
16859 * @b: the base model group particle
16860 *
16861 * (3.9.6) Constraints on Particle Schema Components
16862 * Schema Component Constraint:
16863 * Particle Derivation OK (Elt:All/Choice/Sequence -- RecurseAsIfGroup)
16864 * (rcase-RecurseAsIfGroup)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016865 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016866 * STATUS: TODO
16867 *
16868 * Returns 0 if the constraints are satisfied, a positive
16869 * error code if not and -1 if an internal error occured.
16870 */
16871static int
16872xmlSchemaCheckRCaseRecurseAsIfGroup(xmlSchemaParserCtxtPtr ctxt,
16873 xmlSchemaParticlePtr r,
16874 xmlSchemaParticlePtr b)
16875{
16876 /* TODO: Error codes (rcase-RecurseAsIfGroup). */
16877 TODO
16878 return (0);
16879}
16880
16881/**
16882 * xmlSchemaCheckRCaseNSSubset:
16883 * @ctxt: the schema parser context
16884 * @r: the restricting wildcard particle
16885 * @b: the base wildcard particle
16886 *
16887 * (3.9.6) Constraints on Particle Schema Components
16888 * Schema Component Constraint:
16889 * Particle Derivation OK (Any:Any -- NSSubset)
16890 * (rcase-NSSubset)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016891 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016892 * STATUS: complete
16893 *
16894 * Returns 0 if the constraints are satisfied, a positive
16895 * error code if not and -1 if an internal error occured.
16896 */
16897static int
16898xmlSchemaCheckRCaseNSSubset(xmlSchemaParserCtxtPtr ctxt,
16899 xmlSchemaParticlePtr r,
16900 xmlSchemaParticlePtr b,
16901 int isAnyTypeBase)
16902{
16903 /* TODO: Error codes (rcase-NSSubset). */
16904 /*
16905 * SPEC (1) "R's occurrence range is a valid restriction of B's
Rob Richardsc6947bb2008-06-29 15:04:41 +000016906 * occurrence range as defined by Occurrence Range OK (�3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016907 */
16908 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16909 b->minOccurs, b->maxOccurs))
16910 return (1);
16911 /*
16912 * SPEC (2) "R's {namespace constraint} must be an intensional subset
Rob Richardsc6947bb2008-06-29 15:04:41 +000016913 * of B's {namespace constraint} as defined by Wildcard Subset (�3.10.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016914 */
16915 if (xmlSchemaCheckCOSNSSubset((xmlSchemaWildcardPtr) r->children,
16916 (xmlSchemaWildcardPtr) b->children))
16917 return (1);
16918 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000016919 * SPEC (3) "Unless B is the content model wildcard of the �ur-type
16920 * definition�, R's {process contents} must be identical to or stronger
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016921 * than B's {process contents}, where strict is stronger than lax is
16922 * stronger than skip."
16923 */
16924 if (! isAnyTypeBase) {
16925 if ( ((xmlSchemaWildcardPtr) r->children)->processContents <
16926 ((xmlSchemaWildcardPtr) b->children)->processContents)
16927 return (1);
16928 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016929
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016930 return (0);
16931}
16932
16933/**
16934 * xmlSchemaCheckCOSParticleRestrict:
16935 * @ctxt: the schema parser context
16936 * @type: the complex type definition
16937 *
16938 * (3.9.6) Constraints on Particle Schema Components
16939 * Schema Component Constraint:
16940 * Particle Valid (Restriction) (cos-particle-restrict)
16941 *
16942 * STATUS: TODO
16943 *
16944 * Returns 0 if the constraints are satisfied, a positive
16945 * error code if not and -1 if an internal error occured.
16946 */
16947static int
16948xmlSchemaCheckCOSParticleRestrict(xmlSchemaParserCtxtPtr ctxt,
16949 xmlSchemaParticlePtr r,
16950 xmlSchemaParticlePtr b)
16951{
16952 int ret = 0;
16953
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016954 /*part = WXS_TYPE_PARTICLE(type);
16955 basePart = WXS_TYPE_PARTICLE(base);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016956 */
16957
16958 TODO
16959
16960 /*
16961 * SPEC (1) "They are the same particle."
16962 */
16963 if (r == b)
16964 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016965
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016966
16967 return (0);
16968}
16969
Daniel Veillard15724252008-08-30 15:01:04 +000016970#if 0
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016971/**
16972 * xmlSchemaCheckRCaseNSRecurseCheckCardinality:
16973 * @ctxt: the schema parser context
16974 * @r: the model group particle
16975 * @b: the base wildcard particle
16976 *
16977 * (3.9.6) Constraints on Particle Schema Components
16978 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016979 * Particle Derivation OK (All/Choice/Sequence:Any --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016980 * NSRecurseCheckCardinality)
16981 * (rcase-NSRecurseCheckCardinality)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016982 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016983 * STATUS: TODO: subst-groups
16984 *
16985 * Returns 0 if the constraints are satisfied, a positive
16986 * error code if not and -1 if an internal error occured.
16987 */
16988static int
16989xmlSchemaCheckRCaseNSRecurseCheckCardinality(xmlSchemaParserCtxtPtr ctxt,
16990 xmlSchemaParticlePtr r,
16991 xmlSchemaParticlePtr b)
16992{
16993 xmlSchemaParticlePtr part;
16994 /* TODO: Error codes (rcase-NSRecurseCheckCardinality). */
16995 if ((r->children == NULL) || (r->children->children == NULL))
16996 return (-1);
16997 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000016998 * SPEC "For a group particle to be a �valid restriction� of a
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016999 * wildcard particle..."
17000 *
Rob Richardsc6947bb2008-06-29 15:04:41 +000017001 * SPEC (1) "Every member of the {particles} of the group is a �valid
17002 * restriction� of the wildcard as defined by
17003 * Particle Valid (Restriction) (�3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017004 */
17005 part = (xmlSchemaParticlePtr) r->children->children;
17006 do {
17007 if (xmlSchemaCheckCOSParticleRestrict(ctxt, part, b))
17008 return (1);
17009 part = (xmlSchemaParticlePtr) part->next;
17010 } while (part != NULL);
17011 /*
17012 * SPEC (2) "The effective total range of the group [...] is a
17013 * valid restriction of B's occurrence range as defined by
Rob Richardsc6947bb2008-06-29 15:04:41 +000017014 * Occurrence Range OK (�3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017015 */
17016 if (xmlSchemaCheckParticleRangeOK(
17017 xmlSchemaGetParticleTotalRangeMin(r),
17018 xmlSchemaGetParticleTotalRangeMax(r),
17019 b->minOccurs, b->maxOccurs) != 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017020 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017021 return (0);
17022}
Daniel Veillard15724252008-08-30 15:01:04 +000017023#endif
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017024
17025/**
17026 * xmlSchemaCheckRCaseRecurse:
17027 * @ctxt: the schema parser context
17028 * @r: the <all> or <sequence> model group particle
17029 * @b: the base <all> or <sequence> model group particle
17030 *
17031 * (3.9.6) Constraints on Particle Schema Components
17032 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017033 * Particle Derivation OK (All:All,Sequence:Sequence --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017034 Recurse)
17035 * (rcase-Recurse)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017036 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017037 * STATUS: ?
17038 * TODO: subst-groups
17039 *
17040 * Returns 0 if the constraints are satisfied, a positive
17041 * error code if not and -1 if an internal error occured.
17042 */
17043static int
17044xmlSchemaCheckRCaseRecurse(xmlSchemaParserCtxtPtr ctxt,
17045 xmlSchemaParticlePtr r,
17046 xmlSchemaParticlePtr b)
17047{
17048 /* xmlSchemaParticlePtr part; */
17049 /* TODO: Error codes (rcase-Recurse). */
17050 if ((r->children == NULL) || (b->children == NULL) ||
17051 (r->children->type != b->children->type))
17052 return (-1);
17053 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000017054 * SPEC "For an all or sequence group particle to be a �valid
17055 * restriction� of another group particle with the same {compositor}..."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017056 *
17057 * SPEC (1) "R's occurrence range is a valid restriction of B's
Rob Richardsc6947bb2008-06-29 15:04:41 +000017058 * occurrence range as defined by Occurrence Range OK (�3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017059 */
17060 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
17061 b->minOccurs, b->maxOccurs))
17062 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017063
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017064
17065 return (0);
17066}
17067
17068#endif
17069
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017070#define FACET_RESTR_MUTUAL_ERR(fac1, fac2) \
17071 xmlSchemaPCustomErrExt(pctxt, \
17072 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017073 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017074 "It is an error for both '%s' and '%s' to be specified on the "\
17075 "same type definition", \
17076 BAD_CAST xmlSchemaFacetTypeToString(fac1->type), \
17077 BAD_CAST xmlSchemaFacetTypeToString(fac2->type), NULL);
17078
17079#define FACET_RESTR_ERR(fac1, msg) \
17080 xmlSchemaPCustomErr(pctxt, \
17081 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017082 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017083 msg, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017084
17085#define FACET_RESTR_FIXED_ERR(fac) \
17086 xmlSchemaPCustomErr(pctxt, \
17087 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017088 WXS_BASIC_CAST fac, fac->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017089 "The base type's facet is 'fixed', thus the value must not " \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017090 "differ", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017091
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017092static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017093xmlSchemaDeriveFacetErr(xmlSchemaParserCtxtPtr pctxt,
17094 xmlSchemaFacetPtr facet1,
17095 xmlSchemaFacetPtr facet2,
17096 int lessGreater,
17097 int orEqual,
17098 int ofBase)
17099{
17100 xmlChar *msg = NULL;
17101
17102 msg = xmlStrdup(BAD_CAST "'");
17103 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet1->type));
17104 msg = xmlStrcat(msg, BAD_CAST "' has to be");
17105 if (lessGreater == 0)
17106 msg = xmlStrcat(msg, BAD_CAST " equal to");
17107 if (lessGreater == 1)
17108 msg = xmlStrcat(msg, BAD_CAST " greater than");
17109 else
17110 msg = xmlStrcat(msg, BAD_CAST " less than");
17111
17112 if (orEqual)
17113 msg = xmlStrcat(msg, BAD_CAST " or equal to");
17114 msg = xmlStrcat(msg, BAD_CAST " '");
17115 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet2->type));
17116 if (ofBase)
17117 msg = xmlStrcat(msg, BAD_CAST "' of the base type");
17118 else
17119 msg = xmlStrcat(msg, BAD_CAST "'");
17120
17121 xmlSchemaPCustomErr(pctxt,
17122 XML_SCHEMAP_INVALID_FACET_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017123 WXS_BASIC_CAST facet1, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017124 (const char *) msg, NULL);
17125
17126 if (msg != NULL)
17127 xmlFree(msg);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017128}
17129
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017130/*
17131* xmlSchemaDeriveAndValidateFacets:
17132*
17133* Schema Component Constraint: Simple Type Restriction (Facets)
17134* (st-restrict-facets)
17135*/
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017136static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017137xmlSchemaDeriveAndValidateFacets(xmlSchemaParserCtxtPtr pctxt,
17138 xmlSchemaTypePtr type)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017139{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017140 xmlSchemaTypePtr base = type->baseType;
17141 xmlSchemaFacetLinkPtr link, cur, last = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017142 xmlSchemaFacetPtr facet, bfacet,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017143 flength = NULL, ftotdig = NULL, ffracdig = NULL,
17144 fmaxlen = NULL, fminlen = NULL, /* facets of the current type */
17145 fmininc = NULL, fmaxinc = NULL,
17146 fminexc = NULL, fmaxexc = NULL,
17147 bflength = NULL, bftotdig = NULL, bffracdig = NULL,
17148 bfmaxlen = NULL, bfminlen = NULL, /* facets of the base type */
17149 bfmininc = NULL, bfmaxinc = NULL,
17150 bfminexc = NULL, bfmaxexc = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017151 int res; /* err = 0, fixedErr; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017152
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017153 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017154 * SPEC st-restrict-facets 1:
Daniel Veillarddee23482008-04-11 12:58:43 +000017155 * "The {variety} of R is the same as that of B."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017156 */
17157 /*
17158 * SPEC st-restrict-facets 2:
17159 * "If {variety} is atomic, the {primitive type definition}
17160 * of R is the same as that of B."
17161 *
17162 * NOTE: we leave 1 & 2 out for now, since this will be
17163 * satisfied by the derivation process.
17164 * CONSTRUCTION TODO: Maybe needed if using a construction API.
17165 */
17166 /*
17167 * SPEC st-restrict-facets 3:
17168 * "The {facets} of R are the union of S and the {facets}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017169 * of B, eliminating duplicates. To eliminate duplicates,
17170 * when a facet of the same kind occurs in both S and the
17171 * {facets} of B, the one in the {facets} of B is not
17172 * included, with the exception of enumeration and pattern
17173 * facets, for which multiple occurrences with distinct values
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017174 * are allowed."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017175 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017176
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017177 if ((type->facetSet == NULL) && (base->facetSet == NULL))
17178 return (0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017179
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017180 last = type->facetSet;
17181 if (last != NULL)
17182 while (last->next != NULL)
17183 last = last->next;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017184
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017185 for (cur = type->facetSet; cur != NULL; cur = cur->next) {
17186 facet = cur->facet;
17187 switch (facet->type) {
17188 case XML_SCHEMA_FACET_LENGTH:
17189 flength = facet; break;
17190 case XML_SCHEMA_FACET_MINLENGTH:
17191 fminlen = facet; break;
17192 case XML_SCHEMA_FACET_MININCLUSIVE:
17193 fmininc = facet; break;
17194 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17195 fminexc = facet; break;
17196 case XML_SCHEMA_FACET_MAXLENGTH:
17197 fmaxlen = facet; break;
17198 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17199 fmaxinc = facet; break;
17200 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17201 fmaxexc = facet; break;
17202 case XML_SCHEMA_FACET_TOTALDIGITS:
17203 ftotdig = facet; break;
17204 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17205 ffracdig = facet; break;
17206 default:
17207 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017208 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017209 }
17210 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17211 facet = cur->facet;
17212 switch (facet->type) {
17213 case XML_SCHEMA_FACET_LENGTH:
17214 bflength = facet; break;
17215 case XML_SCHEMA_FACET_MINLENGTH:
17216 bfminlen = facet; break;
17217 case XML_SCHEMA_FACET_MININCLUSIVE:
17218 bfmininc = facet; break;
17219 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17220 bfminexc = facet; break;
17221 case XML_SCHEMA_FACET_MAXLENGTH:
17222 bfmaxlen = facet; break;
17223 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17224 bfmaxinc = facet; break;
17225 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17226 bfmaxexc = facet; break;
17227 case XML_SCHEMA_FACET_TOTALDIGITS:
17228 bftotdig = facet; break;
17229 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17230 bffracdig = facet; break;
17231 default:
17232 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017233 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017234 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017235 /*
17236 * length and minLength or maxLength (2.2) + (3.2)
17237 */
17238 if (flength && (fminlen || fmaxlen)) {
17239 FACET_RESTR_ERR(flength, "It is an error for both 'length' and "
17240 "either of 'minLength' or 'maxLength' to be specified on "
17241 "the same type definition")
17242 }
17243 /*
17244 * Mutual exclusions in the same derivation step.
17245 */
17246 if ((fmaxinc) && (fmaxexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017247 /*
17248 * SCC "maxInclusive and maxExclusive"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017249 */
17250 FACET_RESTR_MUTUAL_ERR(fmaxinc, fmaxexc)
17251 }
17252 if ((fmininc) && (fminexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017253 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017254 * SCC "minInclusive and minExclusive"
17255 */
17256 FACET_RESTR_MUTUAL_ERR(fmininc, fminexc)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017257 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017258
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017259 if (flength && bflength) {
17260 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017261 * SCC "length valid restriction"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017262 * The values have to be equal.
17263 */
17264 res = xmlSchemaCompareValues(flength->val, bflength->val);
17265 if (res == -2)
17266 goto internal_error;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017267 if (res != 0)
17268 xmlSchemaDeriveFacetErr(pctxt, flength, bflength, 0, 0, 1);
17269 if ((res != 0) && (bflength->fixed)) {
17270 FACET_RESTR_FIXED_ERR(flength)
17271 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017272
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017273 }
17274 if (fminlen && bfminlen) {
17275 /*
17276 * SCC "minLength valid restriction"
17277 * minLength >= BASE minLength
17278 */
17279 res = xmlSchemaCompareValues(fminlen->val, bfminlen->val);
17280 if (res == -2)
17281 goto internal_error;
17282 if (res == -1)
17283 xmlSchemaDeriveFacetErr(pctxt, fminlen, bfminlen, 1, 1, 1);
17284 if ((res != 0) && (bfminlen->fixed)) {
17285 FACET_RESTR_FIXED_ERR(fminlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017286 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017287 }
17288 if (fmaxlen && bfmaxlen) {
17289 /*
17290 * SCC "maxLength valid restriction"
17291 * maxLength <= BASE minLength
17292 */
17293 res = xmlSchemaCompareValues(fmaxlen->val, bfmaxlen->val);
17294 if (res == -2)
17295 goto internal_error;
17296 if (res == 1)
17297 xmlSchemaDeriveFacetErr(pctxt, fmaxlen, bfmaxlen, -1, 1, 1);
17298 if ((res != 0) && (bfmaxlen->fixed)) {
17299 FACET_RESTR_FIXED_ERR(fmaxlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017300 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017301 }
17302 /*
17303 * SCC "length and minLength or maxLength"
17304 */
17305 if (! flength)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017306 flength = bflength;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017307 if (flength) {
17308 if (! fminlen)
Miroslav Bajtos8f58f892009-08-07 17:22:12 +020017309 fminlen = bfminlen;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017310 if (fminlen) {
17311 /* (1.1) length >= minLength */
17312 res = xmlSchemaCompareValues(flength->val, fminlen->val);
17313 if (res == -2)
17314 goto internal_error;
17315 if (res == -1)
17316 xmlSchemaDeriveFacetErr(pctxt, flength, fminlen, 1, 1, 0);
17317 }
17318 if (! fmaxlen)
17319 fmaxlen = bfmaxlen;
17320 if (fmaxlen) {
17321 /* (2.1) length <= maxLength */
17322 res = xmlSchemaCompareValues(flength->val, fmaxlen->val);
17323 if (res == -2)
17324 goto internal_error;
17325 if (res == 1)
17326 xmlSchemaDeriveFacetErr(pctxt, flength, fmaxlen, -1, 1, 0);
17327 }
17328 }
17329 if (fmaxinc) {
17330 /*
17331 * "maxInclusive"
17332 */
17333 if (fmininc) {
17334 /* SCC "maxInclusive >= minInclusive" */
17335 res = xmlSchemaCompareValues(fmaxinc->val, fmininc->val);
17336 if (res == -2)
17337 goto internal_error;
17338 if (res == -1) {
17339 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, fmininc, 1, 1, 0);
17340 }
17341 }
17342 /*
17343 * SCC "maxInclusive valid restriction"
17344 */
17345 if (bfmaxinc) {
17346 /* maxInclusive <= BASE maxInclusive */
17347 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxinc->val);
17348 if (res == -2)
17349 goto internal_error;
17350 if (res == 1)
17351 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxinc, -1, 1, 1);
17352 if ((res != 0) && (bfmaxinc->fixed)) {
17353 FACET_RESTR_FIXED_ERR(fmaxinc)
17354 }
17355 }
17356 if (bfmaxexc) {
17357 /* maxInclusive < BASE maxExclusive */
17358 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxexc->val);
17359 if (res == -2)
17360 goto internal_error;
17361 if (res != -1) {
17362 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxexc, -1, 0, 1);
17363 }
17364 }
17365 if (bfmininc) {
17366 /* maxInclusive >= BASE minInclusive */
17367 res = xmlSchemaCompareValues(fmaxinc->val, bfmininc->val);
17368 if (res == -2)
17369 goto internal_error;
17370 if (res == -1) {
17371 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmininc, 1, 1, 1);
17372 }
17373 }
17374 if (bfminexc) {
17375 /* maxInclusive > BASE minExclusive */
17376 res = xmlSchemaCompareValues(fmaxinc->val, bfminexc->val);
17377 if (res == -2)
17378 goto internal_error;
17379 if (res != 1) {
17380 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfminexc, 1, 0, 1);
17381 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017382 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017383 }
17384 if (fmaxexc) {
17385 /*
17386 * "maxExclusive >= minExclusive"
17387 */
17388 if (fminexc) {
17389 res = xmlSchemaCompareValues(fmaxexc->val, fminexc->val);
17390 if (res == -2)
17391 goto internal_error;
17392 if (res == -1) {
17393 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, fminexc, 1, 1, 0);
17394 }
17395 }
17396 /*
17397 * "maxExclusive valid restriction"
17398 */
17399 if (bfmaxexc) {
17400 /* maxExclusive <= BASE maxExclusive */
17401 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxexc->val);
17402 if (res == -2)
17403 goto internal_error;
17404 if (res == 1) {
17405 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxexc, -1, 1, 1);
17406 }
17407 if ((res != 0) && (bfmaxexc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017408 FACET_RESTR_FIXED_ERR(fmaxexc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017409 }
17410 }
17411 if (bfmaxinc) {
17412 /* maxExclusive <= BASE maxInclusive */
17413 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxinc->val);
17414 if (res == -2)
17415 goto internal_error;
17416 if (res == 1) {
17417 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxinc, -1, 1, 1);
17418 }
17419 }
17420 if (bfmininc) {
17421 /* maxExclusive > BASE minInclusive */
17422 res = xmlSchemaCompareValues(fmaxexc->val, bfmininc->val);
17423 if (res == -2)
17424 goto internal_error;
17425 if (res != 1) {
17426 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmininc, 1, 0, 1);
17427 }
17428 }
17429 if (bfminexc) {
17430 /* maxExclusive > BASE minExclusive */
17431 res = xmlSchemaCompareValues(fmaxexc->val, bfminexc->val);
17432 if (res == -2)
17433 goto internal_error;
17434 if (res != 1) {
17435 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfminexc, 1, 0, 1);
17436 }
17437 }
17438 }
17439 if (fminexc) {
17440 /*
17441 * "minExclusive < maxInclusive"
17442 */
17443 if (fmaxinc) {
17444 res = xmlSchemaCompareValues(fminexc->val, fmaxinc->val);
17445 if (res == -2)
17446 goto internal_error;
17447 if (res != -1) {
17448 xmlSchemaDeriveFacetErr(pctxt, fminexc, fmaxinc, -1, 0, 0);
17449 }
17450 }
17451 /*
17452 * "minExclusive valid restriction"
17453 */
17454 if (bfminexc) {
17455 /* minExclusive >= BASE minExclusive */
17456 res = xmlSchemaCompareValues(fminexc->val, bfminexc->val);
17457 if (res == -2)
17458 goto internal_error;
17459 if (res == -1) {
17460 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfminexc, 1, 1, 1);
17461 }
17462 if ((res != 0) && (bfminexc->fixed)) {
17463 FACET_RESTR_FIXED_ERR(fminexc)
17464 }
17465 }
17466 if (bfmaxinc) {
17467 /* minExclusive <= BASE maxInclusive */
17468 res = xmlSchemaCompareValues(fminexc->val, bfmaxinc->val);
17469 if (res == -2)
17470 goto internal_error;
17471 if (res == 1) {
17472 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxinc, -1, 1, 1);
17473 }
17474 }
17475 if (bfmininc) {
17476 /* minExclusive >= BASE minInclusive */
17477 res = xmlSchemaCompareValues(fminexc->val, bfmininc->val);
17478 if (res == -2)
17479 goto internal_error;
17480 if (res == -1) {
17481 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmininc, 1, 1, 1);
17482 }
17483 }
17484 if (bfmaxexc) {
17485 /* minExclusive < BASE maxExclusive */
17486 res = xmlSchemaCompareValues(fminexc->val, bfmaxexc->val);
17487 if (res == -2)
17488 goto internal_error;
17489 if (res != -1) {
17490 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxexc, -1, 0, 1);
17491 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017492 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017493 }
17494 if (fmininc) {
17495 /*
17496 * "minInclusive < maxExclusive"
17497 */
17498 if (fmaxexc) {
17499 res = xmlSchemaCompareValues(fmininc->val, fmaxexc->val);
17500 if (res == -2)
17501 goto internal_error;
17502 if (res != -1) {
17503 xmlSchemaDeriveFacetErr(pctxt, fmininc, fmaxexc, -1, 0, 0);
17504 }
17505 }
17506 /*
17507 * "minExclusive valid restriction"
17508 */
17509 if (bfmininc) {
17510 /* minInclusive >= BASE minInclusive */
17511 res = xmlSchemaCompareValues(fmininc->val, bfmininc->val);
17512 if (res == -2)
17513 goto internal_error;
17514 if (res == -1) {
17515 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmininc, 1, 1, 1);
17516 }
17517 if ((res != 0) && (bfmininc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017518 FACET_RESTR_FIXED_ERR(fmininc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017519 }
17520 }
17521 if (bfmaxinc) {
17522 /* minInclusive <= BASE maxInclusive */
17523 res = xmlSchemaCompareValues(fmininc->val, bfmaxinc->val);
17524 if (res == -2)
17525 goto internal_error;
Daniel Veillard0a119eb2005-07-20 13:46:00 +000017526 if (res == 1) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017527 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxinc, -1, 1, 1);
17528 }
17529 }
17530 if (bfminexc) {
17531 /* minInclusive > BASE minExclusive */
17532 res = xmlSchemaCompareValues(fmininc->val, bfminexc->val);
17533 if (res == -2)
17534 goto internal_error;
17535 if (res != 1)
17536 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfminexc, 1, 0, 1);
17537 }
17538 if (bfmaxexc) {
17539 /* minInclusive < BASE maxExclusive */
17540 res = xmlSchemaCompareValues(fmininc->val, bfmaxexc->val);
17541 if (res == -2)
17542 goto internal_error;
17543 if (res != -1)
17544 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxexc, -1, 0, 1);
17545 }
17546 }
17547 if (ftotdig && bftotdig) {
17548 /*
17549 * SCC " totalDigits valid restriction"
17550 * totalDigits <= BASE totalDigits
17551 */
17552 res = xmlSchemaCompareValues(ftotdig->val, bftotdig->val);
17553 if (res == -2)
17554 goto internal_error;
17555 if (res == 1)
17556 xmlSchemaDeriveFacetErr(pctxt, ftotdig, bftotdig,
17557 -1, 1, 1);
17558 if ((res != 0) && (bftotdig->fixed)) {
17559 FACET_RESTR_FIXED_ERR(ftotdig)
17560 }
17561 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017562 if (ffracdig && bffracdig) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017563 /*
17564 * SCC "fractionDigits valid restriction"
17565 * fractionDigits <= BASE fractionDigits
17566 */
17567 res = xmlSchemaCompareValues(ffracdig->val, bffracdig->val);
17568 if (res == -2)
17569 goto internal_error;
17570 if (res == 1)
17571 xmlSchemaDeriveFacetErr(pctxt, ffracdig, bffracdig,
17572 -1, 1, 1);
17573 if ((res != 0) && (bffracdig->fixed)) {
17574 FACET_RESTR_FIXED_ERR(ffracdig)
17575 }
17576 }
17577 /*
17578 * SCC "fractionDigits less than or equal to totalDigits"
17579 */
17580 if (! ftotdig)
17581 ftotdig = bftotdig;
17582 if (! ffracdig)
17583 ffracdig = bffracdig;
17584 if (ftotdig && ffracdig) {
17585 res = xmlSchemaCompareValues(ffracdig->val, ftotdig->val);
17586 if (res == -2)
17587 goto internal_error;
17588 if (res == 1)
17589 xmlSchemaDeriveFacetErr(pctxt, ffracdig, ftotdig,
17590 -1, 1, 0);
17591 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017592 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017593 * *Enumerations* won' be added here, since only the first set
17594 * of enumerations in the ancestor-or-self axis is used
17595 * for validation, plus we need to use the base type of those
17596 * enumerations for whitespace.
17597 *
17598 * *Patterns*: won't be add here, since they are ORed at
17599 * type level and ANDed at ancestor level. This will
17600 * happed during validation by walking the base axis
17601 * of the type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017602 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017603 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17604 bfacet = cur->facet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017605 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017606 * Special handling of enumerations and patterns.
17607 * TODO: hmm, they should not appear in the set, so remove this.
17608 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017609 if ((bfacet->type == XML_SCHEMA_FACET_PATTERN) ||
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017610 (bfacet->type == XML_SCHEMA_FACET_ENUMERATION))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017611 continue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017612 /*
17613 * Search for a duplicate facet in the current type.
17614 */
17615 link = type->facetSet;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017616 /* err = 0; */
17617 /* fixedErr = 0; */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017618 while (link != NULL) {
17619 facet = link->facet;
17620 if (facet->type == bfacet->type) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017621 switch (facet->type) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017622 case XML_SCHEMA_FACET_WHITESPACE:
17623 /*
17624 * The whitespace must be stronger.
17625 */
17626 if (facet->whitespace < bfacet->whitespace) {
Rob Richardsc6947bb2008-06-29 15:04:41 +000017627 FACET_RESTR_ERR(facet,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017628 "The 'whitespace' value has to be equal to "
17629 "or stronger than the 'whitespace' value of "
17630 "the base type")
17631 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017632 if ((bfacet->fixed) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017633 (facet->whitespace != bfacet->whitespace)) {
17634 FACET_RESTR_FIXED_ERR(facet)
17635 }
17636 break;
17637 default:
17638 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017639 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017640 /* Duplicate found. */
17641 break;
17642 }
17643 link = link->next;
17644 }
17645 /*
17646 * If no duplicate was found: add the base types's facet
17647 * to the set.
17648 */
17649 if (link == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017650 link = (xmlSchemaFacetLinkPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017651 xmlMalloc(sizeof(xmlSchemaFacetLink));
17652 if (link == NULL) {
17653 xmlSchemaPErrMemory(pctxt,
17654 "deriving facets, creating a facet link", NULL);
17655 return (-1);
17656 }
17657 link->facet = cur->facet;
17658 link->next = NULL;
17659 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017660 type->facetSet = link;
17661 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017662 last->next = link;
17663 last = link;
17664 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017665
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017666 }
17667
17668 return (0);
17669internal_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017670 PERROR_INT("xmlSchemaDeriveAndValidateFacets",
17671 "an error occured");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017672 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017673}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017674
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017675static int
17676xmlSchemaFinishMemberTypeDefinitionsProperty(xmlSchemaParserCtxtPtr pctxt,
17677 xmlSchemaTypePtr type)
17678{
17679 xmlSchemaTypeLinkPtr link, lastLink, prevLink, subLink, newLink;
17680 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017681 * The actual value is then formed by replacing any union type
Rob Richardsc6947bb2008-06-29 15:04:41 +000017682 * definition in the �explicit members� with the members of their
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017683 * {member type definitions}, in order.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017684 *
17685 * TODO: There's a bug entry at
17686 * "http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0287.html"
17687 * which indicates that we'll keep the union types the future.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017688 */
17689 link = type->memberTypes;
17690 while (link != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017691
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017692 if (WXS_IS_TYPE_NOT_FIXED(link->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000017693 xmlSchemaTypeFixup(link->type, ACTXT_CAST pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017694
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017695 if (WXS_IS_UNION(link->type)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017696 subLink = xmlSchemaGetUnionSimpleTypeMemberTypes(link->type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017697 if (subLink != NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017698 link->type = subLink->type;
17699 if (subLink->next != NULL) {
17700 lastLink = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017701 subLink = subLink->next;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017702 prevLink = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017703 while (subLink != NULL) {
17704 newLink = (xmlSchemaTypeLinkPtr)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017705 xmlMalloc(sizeof(xmlSchemaTypeLink));
17706 if (newLink == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017707 xmlSchemaPErrMemory(pctxt, "allocating a type link",
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017708 NULL);
17709 return (-1);
17710 }
17711 newLink->type = subLink->type;
17712 prevLink->next = newLink;
17713 prevLink = newLink;
17714 newLink->next = lastLink;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017715
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017716 subLink = subLink->next;
17717 }
17718 }
17719 }
17720 }
17721 link = link->next;
17722 }
17723 return (0);
17724}
17725
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017726static void
17727xmlSchemaTypeFixupOptimFacets(xmlSchemaTypePtr type)
Daniel Veillarddee23482008-04-11 12:58:43 +000017728{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017729 int has = 0, needVal = 0, normVal = 0;
17730
17731 has = (type->baseType->flags & XML_SCHEMAS_TYPE_HAS_FACETS) ? 1 : 0;
17732 if (has) {
17733 needVal = (type->baseType->flags &
17734 XML_SCHEMAS_TYPE_FACETSNEEDVALUE) ? 1 : 0;
17735 normVal = (type->baseType->flags &
17736 XML_SCHEMAS_TYPE_NORMVALUENEEDED) ? 1 : 0;
17737 }
17738 if (type->facets != NULL) {
17739 xmlSchemaFacetPtr fac;
Daniel Veillarddee23482008-04-11 12:58:43 +000017740
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017741 for (fac = type->facets; fac != NULL; fac = fac->next) {
17742 switch (fac->type) {
17743 case XML_SCHEMA_FACET_WHITESPACE:
17744 break;
17745 case XML_SCHEMA_FACET_PATTERN:
17746 normVal = 1;
17747 has = 1;
17748 break;
17749 case XML_SCHEMA_FACET_ENUMERATION:
17750 needVal = 1;
17751 normVal = 1;
17752 has = 1;
17753 break;
17754 default:
17755 has = 1;
17756 break;
17757 }
Daniel Veillarddee23482008-04-11 12:58:43 +000017758 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017759 }
17760 if (normVal)
17761 type->flags |= XML_SCHEMAS_TYPE_NORMVALUENEEDED;
17762 if (needVal)
17763 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17764 if (has)
17765 type->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
17766
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017767 if (has && (! needVal) && WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017768 xmlSchemaTypePtr prim = xmlSchemaGetPrimitiveType(type);
17769 /*
17770 * OPTIMIZE VAL TODO: Some facets need a computed value.
17771 */
17772 if ((prim->builtInType != XML_SCHEMAS_ANYSIMPLETYPE) &&
17773 (prim->builtInType != XML_SCHEMAS_STRING)) {
17774 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
Daniel Veillarddee23482008-04-11 12:58:43 +000017775 }
17776 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017777}
17778
17779static int
17780xmlSchemaTypeFixupWhitespace(xmlSchemaTypePtr type)
17781{
Daniel Veillarddee23482008-04-11 12:58:43 +000017782
17783
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017784 /*
17785 * Evaluate the whitespace-facet value.
Daniel Veillarddee23482008-04-11 12:58:43 +000017786 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017787 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017788 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17789 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017790 } else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017791 return (0);
Daniel Veillarddee23482008-04-11 12:58:43 +000017792
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017793 if (type->facetSet != NULL) {
17794 xmlSchemaFacetLinkPtr lin;
17795
17796 for (lin = type->facetSet; lin != NULL; lin = lin->next) {
17797 if (lin->facet->type == XML_SCHEMA_FACET_WHITESPACE) {
17798 switch (lin->facet->whitespace) {
17799 case XML_SCHEMAS_FACET_PRESERVE:
17800 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17801 break;
17802 case XML_SCHEMAS_FACET_REPLACE:
17803 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17804 break;
17805 case XML_SCHEMAS_FACET_COLLAPSE:
17806 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17807 break;
17808 default:
17809 return (-1);
17810 }
17811 return (0);
17812 }
17813 }
17814 }
17815 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000017816 * For all �atomic� datatypes other than string (and types �derived�
17817 * by �restriction� from it) the value of whiteSpace is fixed to
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017818 * collapse
17819 */
17820 {
17821 xmlSchemaTypePtr anc;
17822
Daniel Veillarddee23482008-04-11 12:58:43 +000017823 for (anc = type->baseType; anc != NULL &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017824 anc->builtInType != XML_SCHEMAS_ANYTYPE;
17825 anc = anc->baseType) {
17826
17827 if (anc->type == XML_SCHEMA_TYPE_BASIC) {
Daniel Veillarddee23482008-04-11 12:58:43 +000017828 if (anc->builtInType == XML_SCHEMAS_NORMSTRING) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017829 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17830
17831 } else if ((anc->builtInType == XML_SCHEMAS_STRING) ||
Daniel Veillarddee23482008-04-11 12:58:43 +000017832 (anc->builtInType == XML_SCHEMAS_ANYSIMPLETYPE)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017833 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17834
17835 } else
17836 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17837 break;
17838 }
17839 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017840 }
17841 return (0);
17842}
17843
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017844static int
17845xmlSchemaFixupSimpleTypeStageOne(xmlSchemaParserCtxtPtr pctxt,
17846 xmlSchemaTypePtr type)
Daniel Veillard4255d502002-04-16 15:50:10 +000017847{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017848 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17849 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017850 if (! WXS_IS_TYPE_NOT_FIXED_1(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017851 return(0);
17852 type->flags |= XML_SCHEMAS_TYPE_FIXUP_1;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017853
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017854 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017855 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017856 * Corresponds to <simpleType><list>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017857 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017858 if (type->subtypes == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017859 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017860 * This one is really needed, so get out.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017861 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017862 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017863 "list type has no item-type assigned");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017864 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017865 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017866 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017867 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017868 * Corresponds to <simpleType><union>...
Daniel Veillarddee23482008-04-11 12:58:43 +000017869 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017870 if (type->memberTypes == NULL) {
17871 /*
17872 * This one is really needed, so get out.
17873 */
17874 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17875 "union type has no member-types assigned");
17876 return(-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000017877 }
17878 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017879 /*
17880 * Corresponds to <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017881 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017882 if (type->baseType == NULL) {
17883 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17884 "type has no base-type assigned");
17885 return(-1);
17886 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017887 if (WXS_IS_TYPE_NOT_FIXED_1(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017888 if (xmlSchemaFixupSimpleTypeStageOne(pctxt, type->baseType) == -1)
17889 return(-1);
17890 /*
17891 * Variety
17892 * If the <restriction> alternative is chosen, then the
17893 * {variety} of the {base type definition}.
17894 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017895 if (WXS_IS_ATOMIC(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017896 type->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017897 else if (WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017898 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017899 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017900 * Inherit the itemType.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017901 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017902 type->subtypes = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017903 } else if (WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017904 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017905 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017906 * NOTE that we won't assign the memberTypes of the base,
17907 * since this will make trouble when freeing them; we will
17908 * use a lookup function to access them instead.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017909 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017910 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017911 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017912 return(0);
17913}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017914
Daniel Veillard8651f532002-04-17 09:06:27 +000017915#ifdef DEBUG_TYPE
Daniel Veillard67952602006-01-05 15:29:44 +000017916static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017917xmlSchemaDebugFixedType(xmlSchemaParserCtxtPtr pctxt,
17918 xmlSchemaTypePtr type)
17919{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017920 if (type->node != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017921 xmlGenericError(xmlGenericErrorContext,
17922 "Type of %s : %s:%d :", name,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017923 type->node->doc->URL,
17924 xmlGetLineNo(type->node));
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017925 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017926 xmlGenericError(xmlGenericErrorContext, "Type of %s :", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017927 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017928 if ((WXS_IS_SIMPLE(type)) || (WXS_IS_COMPLEX(type))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017929 switch (type->contentType) {
17930 case XML_SCHEMA_CONTENT_SIMPLE:
17931 xmlGenericError(xmlGenericErrorContext, "simple\n");
17932 break;
17933 case XML_SCHEMA_CONTENT_ELEMENTS:
17934 xmlGenericError(xmlGenericErrorContext, "elements\n");
17935 break;
17936 case XML_SCHEMA_CONTENT_UNKNOWN:
17937 xmlGenericError(xmlGenericErrorContext, "unknown !!!\n");
17938 break;
17939 case XML_SCHEMA_CONTENT_EMPTY:
17940 xmlGenericError(xmlGenericErrorContext, "empty\n");
17941 break;
17942 case XML_SCHEMA_CONTENT_MIXED:
17943 if (xmlSchemaIsParticleEmptiable((xmlSchemaParticlePtr)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017944 type->subtypes))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017945 xmlGenericError(xmlGenericErrorContext,
17946 "mixed as emptiable particle\n");
17947 else
17948 xmlGenericError(xmlGenericErrorContext, "mixed\n");
17949 break;
17950 /* Removed, since not used. */
17951 /*
17952 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
17953 xmlGenericError(xmlGenericErrorContext, "mixed or elems\n");
17954 break;
17955 */
17956 case XML_SCHEMA_CONTENT_BASIC:
17957 xmlGenericError(xmlGenericErrorContext, "basic\n");
17958 break;
17959 default:
17960 xmlGenericError(xmlGenericErrorContext,
17961 "not registered !!!\n");
17962 break;
17963 }
Daniel Veillard8651f532002-04-17 09:06:27 +000017964 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017965}
Daniel Veillard8651f532002-04-17 09:06:27 +000017966#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017967
17968/*
17969* 3.14.6 Constraints on Simple Type Definition Schema Components
17970*/
17971static int
17972xmlSchemaFixupSimpleTypeStageTwo(xmlSchemaParserCtxtPtr pctxt,
17973 xmlSchemaTypePtr type)
17974{
17975 int res, olderrs = pctxt->nberrors;
17976
17977 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17978 return(-1);
17979
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017980 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017981 return(0);
17982
17983 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
17984 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
17985
17986 if (type->baseType == NULL) {
17987 PERROR_INT("xmlSchemaFixupSimpleTypeStageTwo",
17988 "missing baseType");
17989 goto exit_failure;
17990 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017991 if (WXS_IS_TYPE_NOT_FIXED(type->baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000017992 xmlSchemaTypeFixup(type->baseType, ACTXT_CAST pctxt);
Daniel Veillarddee23482008-04-11 12:58:43 +000017993 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017994 * If a member type of a union is a union itself, we need to substitute
17995 * that member type for its member types.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017996 * NOTE that this might change in WXS 1.1; i.e. we will keep the union
17997 * types in WXS 1.1.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017998 */
17999 if ((type->memberTypes != NULL) &&
18000 (xmlSchemaFinishMemberTypeDefinitionsProperty(pctxt, type) == -1))
Daniel Veillarddee23482008-04-11 12:58:43 +000018001 return(-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018002 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000018003 * SPEC src-simple-type 1
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018004 * "The corresponding simple type definition, if any, must satisfy
18005 * the conditions set out in Constraints on Simple Type Definition
Rob Richardsc6947bb2008-06-29 15:04:41 +000018006 * Schema Components (�3.14.6)."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018007 */
18008 /*
18009 * Schema Component Constraint: Simple Type Definition Properties Correct
18010 * (st-props-correct)
18011 */
18012 res = xmlSchemaCheckSTPropsCorrect(pctxt, type);
18013 HFAILURE HERROR
Daniel Veillarddee23482008-04-11 12:58:43 +000018014 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018015 * Schema Component Constraint: Derivation Valid (Restriction, Simple)
18016 * (cos-st-restricts)
18017 */
18018 res = xmlSchemaCheckCOSSTRestricts(pctxt, type);
18019 HFAILURE HERROR
18020 /*
18021 * TODO: Removed the error report, since it got annoying to get an
18022 * extra error report, if anything failed until now.
18023 * Enable this if needed.
18024 *
18025 * xmlSchemaPErr(ctxt, type->node,
18026 * XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
18027 * "Simple type '%s' does not satisfy the constraints "
18028 * "on simple type definitions.\n",
18029 * type->name, NULL);
18030 */
18031 /*
18032 * Schema Component Constraint: Simple Type Restriction (Facets)
18033 * (st-restrict-facets)
18034 */
18035 res = xmlSchemaCheckFacetValues(type, pctxt);
18036 HFAILURE HERROR
18037 if ((type->facetSet != NULL) ||
18038 (type->baseType->facetSet != NULL)) {
18039 res = xmlSchemaDeriveAndValidateFacets(pctxt, type);
18040 HFAILURE HERROR
18041 }
18042 /*
18043 * Whitespace value.
18044 */
18045 res = xmlSchemaTypeFixupWhitespace(type);
18046 HFAILURE HERROR
Daniel Veillarddee23482008-04-11 12:58:43 +000018047 xmlSchemaTypeFixupOptimFacets(type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018048
18049exit_error:
18050#ifdef DEBUG_TYPE
18051 xmlSchemaDebugFixedType(pctxt, type);
18052#endif
18053 if (olderrs != pctxt->nberrors)
18054 return(pctxt->err);
18055 return(0);
18056
18057exit_failure:
18058#ifdef DEBUG_TYPE
18059 xmlSchemaDebugFixedType(pctxt, type);
18060#endif
18061 return(-1);
18062}
18063
18064static int
18065xmlSchemaFixupComplexType(xmlSchemaParserCtxtPtr pctxt,
18066 xmlSchemaTypePtr type)
18067{
18068 int res = 0, olderrs = pctxt->nberrors;
18069 xmlSchemaTypePtr baseType = type->baseType;
18070
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018071 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018072 return(0);
18073 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
18074 if (baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018075 PERROR_INT("xmlSchemaFixupComplexType",
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018076 "missing baseType");
18077 goto exit_failure;
Daniel Veillarddee23482008-04-11 12:58:43 +000018078 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018079 /*
18080 * Fixup the base type.
18081 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018082 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018083 xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018084 if (baseType->flags & XML_SCHEMAS_TYPE_INTERNAL_INVALID) {
18085 /*
18086 * Skip fixup if the base type is invalid.
18087 * TODO: Generate a warning!
18088 */
18089 return(0);
Daniel Veillarddee23482008-04-11 12:58:43 +000018090 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018091 /*
18092 * This basically checks if the base type can be derived.
18093 */
18094 res = xmlSchemaCheckSRCCT(pctxt, type);
Daniel Veillarddee23482008-04-11 12:58:43 +000018095 HFAILURE HERROR
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018096 /*
18097 * Fixup the content type.
18098 */
18099 if (type->contentType == XML_SCHEMA_CONTENT_SIMPLE) {
18100 /*
18101 * Corresponds to <complexType><simpleContent>...
18102 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018103 if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018104 (baseType->contentTypeDef != NULL) &&
18105 (WXS_IS_RESTRICTION(type))) {
18106 xmlSchemaTypePtr contentBase, content;
18107#ifdef ENABLE_NAMED_LOCALS
18108 char buf[30];
18109 const xmlChar *tmpname;
18110#endif
18111 /*
18112 * SPEC (1) If <restriction> + base type is <complexType>,
18113 * "whose own {content type} is a simple type..."
18114 */
18115 if (type->contentTypeDef != NULL) {
18116 /*
18117 * SPEC (1.1) "the simple type definition corresponding to the
18118 * <simpleType> among the [children] of <restriction> if there
18119 * is one;"
18120 * Note that this "<simpleType> among the [children]" was put
18121 * into ->contentTypeDef during parsing.
18122 */
18123 contentBase = type->contentTypeDef;
18124 type->contentTypeDef = NULL;
18125 } else {
18126 /*
18127 * (1.2) "...otherwise (<restriction> has no <simpleType>
18128 * among its [children]), the simple type definition which
18129 * is the {content type} of the ... base type."
18130 */
18131 contentBase = baseType->contentTypeDef;
18132 }
18133 /*
18134 * SPEC
18135 * "... a simple type definition which restricts the simple
18136 * type definition identified in clause 1.1 or clause 1.2
18137 * with a set of facet components"
18138 *
18139 * Create the anonymous simple type, which will be the content
18140 * type of the complex type.
18141 */
18142#ifdef ENABLE_NAMED_LOCALS
18143 snprintf(buf, 29, "#scST%d", ++(pctxt->counter));
18144 tmpname = xmlDictLookup(pctxt->dict, BAD_CAST buf, -1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018145 content = xmlSchemaAddType(pctxt, pctxt->schema,
18146 XML_SCHEMA_TYPE_SIMPLE, tmpname, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018147 type->node, 0);
18148#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018149 content = xmlSchemaAddType(pctxt, pctxt->schema,
18150 XML_SCHEMA_TYPE_SIMPLE, NULL, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018151 type->node, 0);
18152#endif
18153 if (content == NULL)
18154 goto exit_failure;
18155 /*
18156 * We will use the same node as for the <complexType>
18157 * to have it somehow anchored in the schema doc.
18158 */
18159 content->type = XML_SCHEMA_TYPE_SIMPLE;
18160 content->baseType = contentBase;
18161 /*
18162 * Move the facets, previously anchored on the
18163 * complexType during parsing.
18164 */
18165 content->facets = type->facets;
18166 type->facets = NULL;
18167 content->facetSet = type->facetSet;
18168 type->facetSet = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000018169
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018170 type->contentTypeDef = content;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018171 if (WXS_IS_TYPE_NOT_FIXED(contentBase))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018172 xmlSchemaTypeFixup(contentBase, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018173 /*
18174 * Fixup the newly created type. We don't need to check
18175 * for circularity here.
18176 */
18177 res = xmlSchemaFixupSimpleTypeStageOne(pctxt, content);
Daniel Veillarddee23482008-04-11 12:58:43 +000018178 HFAILURE HERROR
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018179 res = xmlSchemaFixupSimpleTypeStageTwo(pctxt, content);
Daniel Veillarddee23482008-04-11 12:58:43 +000018180 HFAILURE HERROR
18181
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018182 } else if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018183 (baseType->contentType == XML_SCHEMA_CONTENT_MIXED) &&
18184 (WXS_IS_RESTRICTION(type))) {
18185 /*
18186 * SPEC (2) If <restriction> + base is a mixed <complexType> with
18187 * an emptiable particle, then a simple type definition which
18188 * restricts the <restriction>'s <simpleType> child.
18189 */
18190 if ((type->contentTypeDef == NULL) ||
18191 (type->contentTypeDef->baseType == NULL)) {
18192 /*
18193 * TODO: Check if this ever happens.
18194 */
18195 xmlSchemaPCustomErr(pctxt,
18196 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018197 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018198 "Internal error: xmlSchemaTypeFixup, "
18199 "complex type '%s': the <simpleContent><restriction> "
18200 "is missing a <simpleType> child, but was not catched "
18201 "by xmlSchemaCheckSRCCT()", type->name);
18202 goto exit_failure;
18203 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018204 } else if ((WXS_IS_COMPLEX(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018205 /*
18206 * SPEC (3) If <extension> + base is <complexType> with
18207 * <simpleType> content, "...then the {content type} of that
18208 * complex type definition"
18209 */
18210 if (baseType->contentTypeDef == NULL) {
18211 /*
18212 * TODO: Check if this ever happens. xmlSchemaCheckSRCCT
18213 * should have catched this already.
18214 */
18215 xmlSchemaPCustomErr(pctxt,
18216 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018217 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018218 "Internal error: xmlSchemaTypeFixup, "
18219 "complex type '%s': the <extension>ed base type is "
18220 "a complex type with no simple content type",
18221 type->name);
18222 goto exit_failure;
18223 }
18224 type->contentTypeDef = baseType->contentTypeDef;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018225 } else if ((WXS_IS_SIMPLE(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018226 /*
18227 * SPEC (4) <extension> + base is <simpleType>
18228 * "... then that simple type definition"
18229 */
18230 type->contentTypeDef = baseType;
18231 } else {
18232 /*
18233 * TODO: Check if this ever happens.
18234 */
18235 xmlSchemaPCustomErr(pctxt,
18236 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018237 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018238 "Internal error: xmlSchemaTypeFixup, "
18239 "complex type '%s' with <simpleContent>: unhandled "
18240 "derivation case", type->name);
18241 goto exit_failure;
18242 }
18243 } else {
18244 int dummySequence = 0;
18245 xmlSchemaParticlePtr particle =
18246 (xmlSchemaParticlePtr) type->subtypes;
18247 /*
18248 * Corresponds to <complexType><complexContent>...
18249 *
18250 * NOTE that the effective mixed was already set during parsing of
18251 * <complexType> and <complexContent>; its flag value is
18252 * XML_SCHEMAS_TYPE_MIXED.
18253 *
18254 * Compute the "effective content":
18255 * (2.1.1) + (2.1.2) + (2.1.3)
18256 */
18257 if ((particle == NULL) ||
18258 ((particle->type == XML_SCHEMA_TYPE_PARTICLE) &&
18259 ((particle->children->type == XML_SCHEMA_TYPE_ALL) ||
18260 (particle->children->type == XML_SCHEMA_TYPE_SEQUENCE) ||
18261 ((particle->children->type == XML_SCHEMA_TYPE_CHOICE) &&
18262 (particle->minOccurs == 0))) &&
18263 ( ((xmlSchemaTreeItemPtr) particle->children)->children == NULL))) {
18264 if (type->flags & XML_SCHEMAS_TYPE_MIXED) {
18265 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000018266 * SPEC (2.1.4) "If the �effective mixed� is true, then
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018267 * a particle whose properties are as follows:..."
18268 *
18269 * Empty sequence model group with
18270 * minOccurs/maxOccurs = 1 (i.e. a "particle emptiable").
18271 * NOTE that we sill assign it the <complexType> node to
18272 * somehow anchor it in the doc.
18273 */
18274 if ((particle == NULL) ||
18275 (particle->children->type != XML_SCHEMA_TYPE_SEQUENCE)) {
18276 /*
18277 * Create the particle.
18278 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000018279 particle = xmlSchemaAddParticle(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018280 type->node, 1, 1);
18281 if (particle == NULL)
18282 goto exit_failure;
18283 /*
18284 * Create the model group.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018285 */ /* URGENT TODO: avoid adding to pending items. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018286 particle->children = (xmlSchemaTreeItemPtr)
18287 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18288 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18289 if (particle->children == NULL)
18290 goto exit_failure;
Daniel Veillarddee23482008-04-11 12:58:43 +000018291
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018292 type->subtypes = (xmlSchemaTypePtr) particle;
18293 }
18294 dummySequence = 1;
18295 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18296 } else {
18297 /*
18298 * SPEC (2.1.5) "otherwise empty"
18299 */
18300 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
18301 }
18302 } else {
18303 /*
18304 * SPEC (2.2) "otherwise the particle corresponding to the
18305 * <all>, <choice>, <group> or <sequence> among the
18306 * [children]."
18307 */
18308 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18309 }
18310 /*
18311 * Compute the "content type".
18312 */
18313 if (WXS_IS_RESTRICTION(type)) {
18314 /*
18315 * SPEC (3.1) "If <restriction>..."
18316 * (3.1.1) + (3.1.2) */
18317 if (type->contentType != XML_SCHEMA_CONTENT_EMPTY) {
18318 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18319 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18320 }
18321 } else {
18322 /*
18323 * SPEC (3.2) "If <extension>..."
18324 */
18325 if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18326 /*
18327 * SPEC (3.2.1)
Rob Richardsc6947bb2008-06-29 15:04:41 +000018328 * "If the �effective content� is empty, then the
Kasimier T. Buchcikf896d442006-07-12 15:18:08 +000018329 * {content type} of the [...] base ..."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018330 */
18331 type->contentType = baseType->contentType;
18332 type->subtypes = baseType->subtypes;
18333 /*
Kasimier T. Buchcikf896d442006-07-12 15:18:08 +000018334 * Fixes bug #347316:
18335 * This is the case when the base type has a simple
18336 * type definition as content.
18337 */
18338 type->contentTypeDef = baseType->contentTypeDef;
18339 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018340 * NOTE that the effective mixed is ignored here.
18341 */
18342 } else if (baseType->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18343 /*
18344 * SPEC (3.2.2)
18345 */
18346 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18347 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18348 } else {
18349 /*
18350 * SPEC (3.2.3)
18351 */
18352 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18353 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18354 /*
18355 * "A model group whose {compositor} is sequence and whose
18356 * {particles} are..."
18357 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018358 if ((WXS_TYPE_PARTICLE(type) != NULL) &&
18359 (WXS_TYPE_PARTICLE_TERM(type) != NULL) &&
18360 ((WXS_TYPE_PARTICLE_TERM(type))->type ==
18361 XML_SCHEMA_TYPE_ALL))
18362 {
18363 /*
18364 * SPEC cos-all-limited (1)
18365 */
18366 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18367 /* TODO: error code */
18368 XML_SCHEMAP_COS_ALL_LIMITED,
18369 WXS_ITEM_NODE(type), NULL,
18370 "The type has an 'all' model group in its "
18371 "{content type} and thus cannot be derived from "
18372 "a non-empty type, since this would produce a "
18373 "'sequence' model group containing the 'all' "
18374 "model group; 'all' model groups are not "
18375 "allowed to appear inside other model groups",
18376 NULL, NULL);
18377
18378 } else if ((WXS_TYPE_PARTICLE(baseType) != NULL) &&
18379 (WXS_TYPE_PARTICLE_TERM(baseType) != NULL) &&
18380 ((WXS_TYPE_PARTICLE_TERM(baseType))->type ==
18381 XML_SCHEMA_TYPE_ALL))
18382 {
18383 /*
18384 * SPEC cos-all-limited (1)
18385 */
18386 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18387 /* TODO: error code */
18388 XML_SCHEMAP_COS_ALL_LIMITED,
18389 WXS_ITEM_NODE(type), NULL,
18390 "A type cannot be derived by extension from a type "
18391 "which has an 'all' model group in its "
18392 "{content type}, since this would produce a "
18393 "'sequence' model group containing the 'all' "
18394 "model group; 'all' model groups are not "
18395 "allowed to appear inside other model groups",
18396 NULL, NULL);
18397
18398 } else if (! dummySequence) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018399 xmlSchemaTreeItemPtr effectiveContent =
18400 (xmlSchemaTreeItemPtr) type->subtypes;
18401 /*
18402 * Create the particle.
18403 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000018404 particle = xmlSchemaAddParticle(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018405 type->node, 1, 1);
18406 if (particle == NULL)
18407 goto exit_failure;
18408 /*
18409 * Create the "sequence" model group.
18410 */
18411 particle->children = (xmlSchemaTreeItemPtr)
18412 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18413 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18414 if (particle->children == NULL)
18415 goto exit_failure;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018416 WXS_TYPE_CONTENTTYPE(type) = (xmlSchemaTypePtr) particle;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018417 /*
18418 * SPEC "the particle of the {content type} of
18419 * the ... base ..."
18420 * Create a duplicate of the base type's particle
18421 * and assign its "term" to it.
18422 */
18423 particle->children->children =
18424 (xmlSchemaTreeItemPtr) xmlSchemaAddParticle(pctxt,
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000018425 type->node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018426 ((xmlSchemaParticlePtr) type->subtypes)->minOccurs,
18427 ((xmlSchemaParticlePtr) type->subtypes)->maxOccurs);
18428 if (particle->children->children == NULL)
18429 goto exit_failure;
18430 particle = (xmlSchemaParticlePtr)
18431 particle->children->children;
18432 particle->children =
18433 ((xmlSchemaParticlePtr) baseType->subtypes)->children;
18434 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000018435 * SPEC "followed by the �effective content�."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018436 */
18437 particle->next = effectiveContent;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018438 /*
18439 * This all will result in:
18440 * new-particle
18441 * --> new-sequence(
18442 * new-particle
18443 * --> base-model,
18444 * this-particle
18445 * --> this-model
18446 * )
18447 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018448 } else {
18449 /*
18450 * This is the case when there is already an empty
18451 * <sequence> with minOccurs==maxOccurs==1.
18452 * Just add the base types's content type.
18453 * NOTE that, although we miss to add an intermediate
18454 * <sequence>, this should produce no difference to
18455 * neither the regex compilation of the content model,
18456 * nor to the complex type contraints.
18457 */
18458 particle->children->children =
18459 (xmlSchemaTreeItemPtr) baseType->subtypes;
18460 }
18461 }
18462 }
18463 }
18464 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018465 * Now fixup attribute uses:
18466 * - expand attr. group references
18467 * - intersect attribute wildcards
18468 * - inherit attribute uses of the base type
18469 * - inherit or union attr. wildcards if extending
18470 * - apply attr. use prohibitions if restricting
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018471 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018472 res = xmlSchemaFixupTypeAttributeUses(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018473 HFAILURE HERROR
18474 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018475 * Apply the complex type component constraints; this will not
18476 * check attributes, since this is done in
18477 * xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018478 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018479 res = xmlSchemaCheckCTComponent(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018480 HFAILURE HERROR
18481
18482#ifdef DEBUG_TYPE
18483 xmlSchemaDebugFixedType(pctxt, type);
18484#endif
18485 if (olderrs != pctxt->nberrors)
18486 return(pctxt->err);
18487 else
18488 return(0);
18489
18490exit_error:
18491 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18492#ifdef DEBUG_TYPE
18493 xmlSchemaDebugFixedType(pctxt, type);
18494#endif
18495 return(pctxt->err);
18496
18497exit_failure:
18498 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18499#ifdef DEBUG_TYPE
18500 xmlSchemaDebugFixedType(pctxt, type);
18501#endif
18502 return(-1);
18503}
18504
18505
18506/**
18507 * xmlSchemaTypeFixup:
18508 * @typeDecl: the schema type definition
18509 * @ctxt: the schema parser context
18510 *
18511 * Fixes the content model of the type.
18512 * URGENT TODO: We need an int result!
18513 */
18514static int
18515xmlSchemaTypeFixup(xmlSchemaTypePtr type,
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018516 xmlSchemaAbstractCtxtPtr actxt)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018517{
18518 if (type == NULL)
18519 return(0);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018520 if (actxt->type != XML_SCHEMA_CTXT_PARSER) {
18521 AERROR_INT("xmlSchemaTypeFixup",
18522 "this function needs a parser context");
18523 return(-1);
18524 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018525 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018526 return(0);
18527 if (type->type == XML_SCHEMA_TYPE_COMPLEX)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018528 return(xmlSchemaFixupComplexType(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018529 else if (type->type == XML_SCHEMA_TYPE_SIMPLE)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018530 return(xmlSchemaFixupSimpleTypeStageTwo(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018531 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000018532}
18533
18534/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018535 * xmlSchemaCheckFacet:
18536 * @facet: the facet
18537 * @typeDecl: the schema type definition
Daniel Veillard81562d22005-06-15 13:27:56 +000018538 * @pctxt: the schema parser context or NULL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018539 * @name: the optional name of the type
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018540 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018541 * Checks and computes the values of facets.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018542 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018543 * Returns 0 if valid, a positive error code if not valid and
18544 * -1 in case of an internal or API error.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018545 */
18546int
18547xmlSchemaCheckFacet(xmlSchemaFacetPtr facet,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018548 xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018549 xmlSchemaParserCtxtPtr pctxt,
18550 const xmlChar * name ATTRIBUTE_UNUSED)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018551{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018552 int ret = 0, ctxtGiven;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018553
Daniel Veillardce682bc2004-11-05 17:22:25 +000018554 if ((facet == NULL) || (typeDecl == NULL))
18555 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018556 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018557 * TODO: will the parser context be given if used from
18558 * the relaxNG module?
18559 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018560 if (pctxt == NULL)
18561 ctxtGiven = 0;
18562 else
18563 ctxtGiven = 1;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018564
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018565 switch (facet->type) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018566 case XML_SCHEMA_FACET_MININCLUSIVE:
18567 case XML_SCHEMA_FACET_MINEXCLUSIVE:
18568 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018569 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
18570 case XML_SCHEMA_FACET_ENUMERATION: {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018571 /*
18572 * Okay we need to validate the value
18573 * at that point.
18574 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000018575 xmlSchemaTypePtr base;
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018576
18577 /* 4.3.5.5 Constraints on enumeration Schema Components
18578 * Schema Component Constraint: enumeration valid restriction
Rob Richardsc6947bb2008-06-29 15:04:41 +000018579 * It is an �error� if any member of {value} is not in the
18580 * �value space� of {base type definition}.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018581 *
18582 * minInclusive, maxInclusive, minExclusive, maxExclusive:
Rob Richardsc6947bb2008-06-29 15:04:41 +000018583 * The value �must� be in the
18584 * �value space� of the �base type�.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018585 */
18586 /*
18587 * This function is intended to deliver a compiled value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018588 * on the facet. In this implementation of XML Schemata the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018589 * type holding a facet, won't be a built-in type.
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018590 * Thus to ensure that other API
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018591 * calls (relaxng) do work, if the given type is a built-in
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018592 * type, we will assume that the given built-in type *is
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018593 * already* the base type.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018594 */
18595 if (typeDecl->type != XML_SCHEMA_TYPE_BASIC) {
18596 base = typeDecl->baseType;
18597 if (base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018598 PERROR_INT("xmlSchemaCheckFacet",
18599 "a type user derived type has no base type");
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018600 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018601 }
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018602 } else
18603 base = typeDecl;
Daniel Veillarddee23482008-04-11 12:58:43 +000018604
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018605 if (! ctxtGiven) {
18606 /*
18607 * A context is needed if called from RelaxNG.
Daniel Veillarddee23482008-04-11 12:58:43 +000018608 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018609 pctxt = xmlSchemaNewParserCtxt("*");
18610 if (pctxt == NULL)
18611 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000018612 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018613 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018614 * NOTE: This call does not check the content nodes,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018615 * since they are not available:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018616 * facet->node is just the node holding the facet
18617 * definition, *not* the attribute holding the *value*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018618 * of the facet.
Daniel Veillarddee23482008-04-11 12:58:43 +000018619 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018620 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018621 ACTXT_CAST pctxt, facet->node, base,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018622 facet->value, &(facet->val), 1, 1, 0);
18623 if (ret != 0) {
18624 if (ret < 0) {
18625 /* No error message for RelaxNG. */
Daniel Veillarddee23482008-04-11 12:58:43 +000018626 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018627 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018628 XML_SCHEMAP_INTERNAL, facet->node, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000018629 "Internal error: xmlSchemaCheckFacet, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018630 "failed to validate the value '%s' of the "
18631 "facet '%s' against the base type",
18632 facet->value, xmlSchemaFacetTypeToString(facet->type));
18633 }
18634 goto internal_error;
18635 }
18636 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18637 /* No error message for RelaxNG. */
18638 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018639 xmlChar *str = NULL;
18640
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018641 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018642 ret, facet->node, WXS_BASIC_CAST facet,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018643 "The value '%s' of the facet does not validate "
18644 "against the base type '%s'",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018645 facet->value,
18646 xmlSchemaFormatQName(&str,
18647 base->targetNamespace, base->name));
18648 FREE_AND_NULL(str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018649 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018650 goto exit;
18651 } else if (facet->val == NULL) {
18652 if (ctxtGiven) {
18653 PERROR_INT("xmlSchemaCheckFacet",
18654 "value was not computed");
18655 }
18656 TODO
18657 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018658 break;
18659 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018660 case XML_SCHEMA_FACET_PATTERN:
18661 facet->regexp = xmlRegexpCompile(facet->value);
18662 if (facet->regexp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018663 ret = XML_SCHEMAP_REGEXP_INVALID;
18664 /* No error message for RelaxNG. */
18665 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018666 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018667 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018668 "The value '%s' of the facet 'pattern' is not a "
18669 "valid regular expression",
18670 facet->value, NULL);
18671 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018672 }
18673 break;
18674 case XML_SCHEMA_FACET_TOTALDIGITS:
18675 case XML_SCHEMA_FACET_FRACTIONDIGITS:
18676 case XML_SCHEMA_FACET_LENGTH:
18677 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018678 case XML_SCHEMA_FACET_MINLENGTH:
18679
18680 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
18681 ret = xmlSchemaValidatePredefinedType(
18682 xmlSchemaGetBuiltInType(XML_SCHEMAS_PINTEGER),
18683 facet->value, &(facet->val));
18684 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018685 ret = xmlSchemaValidatePredefinedType(
18686 xmlSchemaGetBuiltInType(XML_SCHEMAS_NNINTEGER),
18687 facet->value, &(facet->val));
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018688 }
18689 if (ret != 0) {
18690 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018691 /* No error message for RelaxNG. */
18692 if (ctxtGiven) {
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018693 PERROR_INT("xmlSchemaCheckFacet",
18694 "validating facet value");
18695 }
18696 goto internal_error;
18697 }
18698 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18699 /* No error message for RelaxNG. */
18700 if (ctxtGiven) {
18701 /* error code */
18702 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
18703 ret, facet->node, WXS_BASIC_CAST typeDecl,
Daniel Veillarddee23482008-04-11 12:58:43 +000018704 "The value '%s' of the facet '%s' is not a valid '%s'",
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018705 facet->value,
18706 xmlSchemaFacetTypeToString(facet->type),
Daniel Veillarddee23482008-04-11 12:58:43 +000018707 (facet->type != XML_SCHEMA_FACET_TOTALDIGITS) ?
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018708 BAD_CAST "nonNegativeInteger" :
18709 BAD_CAST "positiveInteger",
18710 NULL);
18711 }
18712 }
18713 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000018714
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018715 case XML_SCHEMA_FACET_WHITESPACE:{
18716 if (xmlStrEqual(facet->value, BAD_CAST "preserve")) {
18717 facet->whitespace = XML_SCHEMAS_FACET_PRESERVE;
18718 } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) {
18719 facet->whitespace = XML_SCHEMAS_FACET_REPLACE;
18720 } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) {
18721 facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE;
18722 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018723 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18724 /* No error message for RelaxNG. */
18725 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018726 /* error was previously: XML_SCHEMAP_INVALID_WHITE_SPACE */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018727 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018728 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018729 "The value '%s' of the facet 'whitespace' is not "
18730 "valid", facet->value, NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018731 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018732 }
18733 }
18734 default:
18735 break;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018736 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018737exit:
18738 if ((! ctxtGiven) && (pctxt != NULL))
18739 xmlSchemaFreeParserCtxt(pctxt);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018740 return (ret);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018741internal_error:
18742 if ((! ctxtGiven) && (pctxt != NULL))
18743 xmlSchemaFreeParserCtxt(pctxt);
18744 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018745}
18746
18747/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018748 * xmlSchemaCheckFacetValues:
Daniel Veillard4255d502002-04-16 15:50:10 +000018749 * @typeDecl: the schema type definition
18750 * @ctxt: the schema parser context
18751 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018752 * Checks the default values types, especially for facets
Daniel Veillard4255d502002-04-16 15:50:10 +000018753 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018754static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018755xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018756 xmlSchemaParserCtxtPtr pctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000018757{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018758 int res, olderrs = pctxt->nberrors;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018759 const xmlChar *name = typeDecl->name;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018760 /*
18761 * NOTE: It is intended to use the facets list, instead
18762 * of facetSet.
18763 */
18764 if (typeDecl->facets != NULL) {
18765 xmlSchemaFacetPtr facet = typeDecl->facets;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018766
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018767 /*
18768 * Temporarily assign the "schema" to the validation context
18769 * of the parser context. This is needed for NOTATION validation.
18770 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018771 if (pctxt->vctxt == NULL) {
18772 if (xmlSchemaCreateVCtxtOnPCtxt(pctxt) == -1)
18773 return(-1);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018774 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018775 pctxt->vctxt->schema = pctxt->schema;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018776 while (facet != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018777 res = xmlSchemaCheckFacet(facet, typeDecl, pctxt, name);
18778 HFAILURE
Daniel Veillard01fa6152004-06-29 17:04:39 +000018779 facet = facet->next;
18780 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018781 pctxt->vctxt->schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018782 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018783 if (olderrs != pctxt->nberrors)
18784 return(pctxt->err);
18785 return(0);
18786exit_failure:
18787 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000018788}
18789
18790/**
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018791 * xmlSchemaGetCircModelGrDefRef:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018792 * @ctxtMGroup: the searched model group
18793 * @selfMGroup: the second searched model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018794 * @particle: the first particle
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018795 *
18796 * This one is intended to be used by
18797 * xmlSchemaCheckGroupDefCircular only.
18798 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018799 * Returns the particle with the circular model group definition reference,
18800 * otherwise NULL.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018801 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018802static xmlSchemaTreeItemPtr
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018803xmlSchemaGetCircModelGrDefRef(xmlSchemaModelGroupDefPtr groupDef,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018804 xmlSchemaTreeItemPtr particle)
18805{
18806 xmlSchemaTreeItemPtr circ = NULL;
18807 xmlSchemaTreeItemPtr term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018808 xmlSchemaModelGroupDefPtr gdef;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018809
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018810 for (; particle != NULL; particle = particle->next) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018811 term = particle->children;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018812 if (term == NULL)
18813 continue;
18814 switch (term->type) {
18815 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018816 gdef = (xmlSchemaModelGroupDefPtr) term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018817 if (gdef == groupDef)
18818 return (particle);
18819 /*
18820 * Mark this model group definition to avoid infinite
18821 * recursion on circular references not yet examined.
18822 */
18823 if (gdef->flags & XML_SCHEMA_MODEL_GROUP_DEF_MARKED)
18824 continue;
18825 if (gdef->children != NULL) {
18826 gdef->flags |= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18827 circ = xmlSchemaGetCircModelGrDefRef(groupDef,
18828 gdef->children->children);
18829 gdef->flags ^= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18830 if (circ != NULL)
18831 return (circ);
18832 }
18833 break;
18834 case XML_SCHEMA_TYPE_SEQUENCE:
18835 case XML_SCHEMA_TYPE_CHOICE:
18836 case XML_SCHEMA_TYPE_ALL:
18837 circ = xmlSchemaGetCircModelGrDefRef(groupDef, term->children);
18838 if (circ != NULL)
18839 return (circ);
18840 break;
18841 default:
18842 break;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018843 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018844 }
18845 return (NULL);
18846}
18847
18848/**
18849 * xmlSchemaCheckGroupDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018850 * @item: the model group definition
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018851 * @ctxt: the parser context
18852 * @name: the name
18853 *
18854 * Checks for circular references to model group definitions.
18855 */
18856static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018857xmlSchemaCheckGroupDefCircular(xmlSchemaModelGroupDefPtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018858 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018859{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018860 /*
18861 * Schema Component Constraint: Model Group Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018862 * 2 Circular groups are disallowed. That is, within the {particles}
18863 * of a group there must not be at any depth a particle whose {term}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018864 * is the group itself.
18865 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018866 if ((item == NULL) ||
18867 (item->type != XML_SCHEMA_TYPE_GROUP) ||
18868 (item->children == NULL))
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018869 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018870 {
18871 xmlSchemaTreeItemPtr circ;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018872
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018873 circ = xmlSchemaGetCircModelGrDefRef(item, item->children->children);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018874 if (circ != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018875 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018876 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018877 * TODO: The error report is not adequate: this constraint
18878 * is defined for model groups but not definitions, but since
18879 * there cannot be any circular model groups without a model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018880 * definition (if not using a construction API), we check those
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018881 * defintions only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018882 */
18883 xmlSchemaPCustomErr(ctxt,
18884 XML_SCHEMAP_MG_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018885 NULL, WXS_ITEM_NODE(circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018886 "Circular reference to the model group definition '%s' "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018887 "defined", xmlSchemaFormatQName(&str,
18888 item->targetNamespace, item->name));
18889 FREE_AND_NULL(str)
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018890 /*
18891 * NOTE: We will cut the reference to avoid further
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018892 * confusion of the processor. This is a fatal error.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018893 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018894 circ->children = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018895 }
18896 }
18897}
18898
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018899/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018900 * xmlSchemaModelGroupToModelGroupDefFixup:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018901 * @ctxt: the parser context
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018902 * @mg: the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018903 *
18904 * Assigns the model group of model group definitions to the "term"
18905 * of the referencing particle.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018906 * In xmlSchemaResolveModelGroupParticleReferences the model group
18907 * definitions were assigned to the "term", since needed for the
18908 * circularity check.
18909 *
18910 * Schema Component Constraint:
18911 * All Group Limited (cos-all-limited) (1.2)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018912 */
18913static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018914xmlSchemaModelGroupToModelGroupDefFixup(
18915 xmlSchemaParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
18916 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018917{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018918 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
18919
18920 while (particle != NULL) {
18921 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
18922 ((WXS_PARTICLE_TERM(particle))->type !=
18923 XML_SCHEMA_TYPE_GROUP))
18924 {
18925 particle = WXS_PTC_CAST particle->next;
18926 continue;
Daniel Veillarddee23482008-04-11 12:58:43 +000018927 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018928 if (WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle)) == NULL) {
18929 /*
18930 * TODO: Remove the particle.
18931 */
18932 WXS_PARTICLE_TERM(particle) = NULL;
18933 particle = WXS_PTC_CAST particle->next;
18934 continue;
18935 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018936 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018937 * Assign the model group to the {term} of the particle.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018938 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018939 WXS_PARTICLE_TERM(particle) =
18940 WXS_TREE_CAST WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle));
18941
18942 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018943 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018944}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018945
18946/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018947 * xmlSchemaCheckAttrGroupCircularRecur:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018948 * @ctxtGr: the searched attribute group
18949 * @attr: the current attribute list to be processed
18950 *
18951 * This one is intended to be used by
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018952 * xmlSchemaCheckAttrGroupCircular only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018953 *
18954 * Returns the circular attribute grou reference, otherwise NULL.
18955 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018956static xmlSchemaQNameRefPtr
18957xmlSchemaCheckAttrGroupCircularRecur(xmlSchemaAttributeGroupPtr ctxtGr,
18958 xmlSchemaItemListPtr list)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018959{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018960 xmlSchemaAttributeGroupPtr gr;
18961 xmlSchemaQNameRefPtr ref, circ;
18962 int i;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018963 /*
18964 * We will search for an attribute group reference which
18965 * references the context attribute group.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018966 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018967 for (i = 0; i < list->nbItems; i++) {
18968 ref = list->items[i];
18969 if ((ref->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
18970 (ref->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
18971 (ref->item != NULL))
18972 {
18973 gr = WXS_ATTR_GROUP_CAST ref->item;
18974 if (gr == ctxtGr)
18975 return(ref);
18976 if (gr->flags & XML_SCHEMAS_ATTRGROUP_MARKED)
Daniel Veillarddee23482008-04-11 12:58:43 +000018977 continue;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018978 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018979 * Mark as visited to avoid infinite recursion on
18980 * circular references not yet examined.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018981 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018982 if ((gr->attrUses) &&
18983 (gr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS))
18984 {
18985 gr->flags |= XML_SCHEMAS_ATTRGROUP_MARKED;
18986 circ = xmlSchemaCheckAttrGroupCircularRecur(ctxtGr,
Daniel Veillarddee23482008-04-11 12:58:43 +000018987 (xmlSchemaItemListPtr) gr->attrUses);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018988 gr->flags ^= XML_SCHEMAS_ATTRGROUP_MARKED;
18989 if (circ != NULL)
18990 return (circ);
18991 }
Daniel Veillarddee23482008-04-11 12:58:43 +000018992
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018993 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018994 }
18995 return (NULL);
18996}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018997
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018998/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018999 * xmlSchemaCheckAttrGroupCircular:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019000 * attrGr: the attribute group definition
19001 * @ctxt: the parser context
19002 * @name: the name
19003 *
19004 * Checks for circular references of attribute groups.
19005 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019006static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019007xmlSchemaCheckAttrGroupCircular(xmlSchemaAttributeGroupPtr attrGr,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019008 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019009{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019010 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019011 * Schema Representation Constraint:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019012 * Attribute Group Definition Representation OK
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019013 * 3 Circular group reference is disallowed outside <redefine>.
19014 * That is, unless this element information item's parent is
19015 * <redefine>, then among the [children], if any, there must
19016 * not be an <attributeGroup> with ref [attribute] which resolves
19017 * to the component corresponding to this <attributeGroup>. Indirect
19018 * circularity is also ruled out. That is, when QName resolution
Rob Richardsc6947bb2008-06-29 15:04:41 +000019019 * (Schema Document) (�3.15.3) is applied to a �QName� arising from
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019020 * any <attributeGroup>s with a ref [attribute] among the [children],
Rob Richardsc6947bb2008-06-29 15:04:41 +000019021 * it must not be the case that a �QName� is encountered at any depth
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019022 * which resolves to the component corresponding to this <attributeGroup>.
19023 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019024 if (attrGr->attrUses == NULL)
19025 return(0);
19026 else if ((attrGr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS) == 0)
19027 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019028 else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019029 xmlSchemaQNameRefPtr circ;
Daniel Veillarddee23482008-04-11 12:58:43 +000019030
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019031 circ = xmlSchemaCheckAttrGroupCircularRecur(attrGr,
Daniel Veillarddee23482008-04-11 12:58:43 +000019032 (xmlSchemaItemListPtr) attrGr->attrUses);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019033 if (circ != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019034 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019035 /*
19036 * TODO: Report the referenced attr group as QName.
19037 */
19038 xmlSchemaPCustomErr(ctxt,
19039 XML_SCHEMAP_SRC_ATTRIBUTE_GROUP_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019040 NULL, WXS_ITEM_NODE(WXS_BASIC_CAST circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019041 "Circular reference to the attribute group '%s' "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019042 "defined", xmlSchemaGetComponentQName(&str, attrGr));
19043 FREE_AND_NULL(str);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019044 /*
19045 * NOTE: We will cut the reference to avoid further
19046 * confusion of the processor.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019047 * BADSPEC TODO: The spec should define how to process in this case.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019048 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019049 circ->item = NULL;
19050 return(ctxt->err);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019051 }
19052 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019053 return(0);
19054}
19055
19056static int
19057xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19058 xmlSchemaAttributeGroupPtr attrGr);
19059
19060/**
19061 * xmlSchemaExpandAttributeGroupRefs:
19062 * @pctxt: the parser context
19063 * @node: the node of the component holding the attribute uses
Daniel Veillarddee23482008-04-11 12:58:43 +000019064 * @completeWild: the intersected wildcard to be returned
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019065 * @list: the attribute uses
19066 *
19067 * Substitutes contained attribute group references
19068 * for their attribute uses. Wilcards are intersected.
19069 * Attribute use prohibitions are removed from the list
19070 * and returned via the @prohibs list.
19071 * Pointlessness of attr. prohibs, if a matching attr. decl
19072 * is existent a well, are checked.
19073 */
19074static int
19075xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
19076 xmlSchemaBasicItemPtr item,
19077 xmlSchemaWildcardPtr *completeWild,
19078 xmlSchemaItemListPtr list,
19079 xmlSchemaItemListPtr prohibs)
19080{
19081 xmlSchemaAttributeGroupPtr gr;
19082 xmlSchemaAttributeUsePtr use;
19083 xmlSchemaItemListPtr sublist;
19084 int i, j;
19085 int created = (*completeWild == NULL) ? 0 : 1;
19086
19087 if (prohibs)
19088 prohibs->nbItems = 0;
19089
19090 for (i = 0; i < list->nbItems; i++) {
19091 use = list->items[i];
19092
Daniel Veillarddee23482008-04-11 12:58:43 +000019093 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019094 if (prohibs == NULL) {
19095 PERROR_INT("xmlSchemaExpandAttributeGroupRefs",
19096 "unexpected attr prohibition found");
19097 return(-1);
19098 }
19099 /*
19100 * Remove from attribute uses.
19101 */
19102 if (xmlSchemaItemListRemove(list, i) == -1)
19103 return(-1);
19104 i--;
19105 /*
19106 * Note that duplicate prohibitions were already
19107 * handled at parsing time.
Daniel Veillarddee23482008-04-11 12:58:43 +000019108 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019109 /*
19110 * Add to list of prohibitions.
19111 */
19112 xmlSchemaItemListAddSize(prohibs, 2, use);
19113 continue;
19114 }
19115 if ((use->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
19116 ((WXS_QNAME_CAST use)->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP))
19117 {
19118 if ((WXS_QNAME_CAST use)->item == NULL)
19119 return(-1);
19120 gr = WXS_ATTR_GROUP_CAST (WXS_QNAME_CAST use)->item;
19121 /*
19122 * Expand the referenced attr. group.
19123 * TODO: remove this, this is done in a previous step, so
19124 * already done here.
19125 */
19126 if ((gr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED) == 0) {
19127 if (xmlSchemaAttributeGroupExpandRefs(pctxt, gr) == -1)
19128 return(-1);
19129 }
19130 /*
19131 * Build the 'complete' wildcard; i.e. intersect multiple
19132 * wildcards.
19133 */
19134 if (gr->attributeWildcard != NULL) {
19135 if (*completeWild == NULL) {
19136 *completeWild = gr->attributeWildcard;
19137 } else {
19138 if (! created) {
19139 xmlSchemaWildcardPtr tmpWild;
19140
19141 /*
19142 * Copy the first encountered wildcard as context,
19143 * except for the annotation.
19144 *
19145 * Although the complete wildcard might not correspond
19146 * to any node in the schema, we will anchor it on
19147 * the node of the owner component.
19148 */
19149 tmpWild = xmlSchemaAddWildcard(pctxt, pctxt->schema,
19150 XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
19151 WXS_ITEM_NODE(item));
19152 if (tmpWild == NULL)
19153 return(-1);
19154 if (xmlSchemaCloneWildcardNsConstraints(pctxt,
19155 tmpWild, *completeWild) == -1)
19156 return (-1);
19157 tmpWild->processContents = (*completeWild)->processContents;
19158 *completeWild = tmpWild;
19159 created = 1;
19160 }
Daniel Veillarddee23482008-04-11 12:58:43 +000019161
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019162 if (xmlSchemaIntersectWildcards(pctxt, *completeWild,
19163 gr->attributeWildcard) == -1)
19164 return(-1);
19165 }
19166 }
19167 /*
19168 * Just remove the reference if the referenced group does not
19169 * contain any attribute uses.
19170 */
William M. Brack06559b32007-03-14 09:34:15 +000019171 sublist = ((xmlSchemaItemListPtr) gr->attrUses);
19172 if ((sublist == NULL) || sublist->nbItems == 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019173 if (xmlSchemaItemListRemove(list, i) == -1)
19174 return(-1);
19175 i--;
19176 continue;
19177 }
19178 /*
19179 * Add the attribute uses.
19180 */
William M. Brack06559b32007-03-14 09:34:15 +000019181 list->items[i] = sublist->items[0];
19182 if (sublist->nbItems != 1) {
19183 for (j = 1; j < sublist->nbItems; j++) {
19184 i++;
19185 if (xmlSchemaItemListInsert(list,
19186 sublist->items[j], i) == -1)
19187 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019188 }
William M. Brack06559b32007-03-14 09:34:15 +000019189 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019190 }
19191
19192 }
19193 /*
19194 * Handle pointless prohibitions of declared attributes.
19195 */
19196 if (prohibs && (prohibs->nbItems != 0) && (list->nbItems != 0)) {
19197 xmlSchemaAttributeUseProhibPtr prohib;
19198
19199 for (i = prohibs->nbItems -1; i >= 0; i--) {
19200 prohib = prohibs->items[i];
19201 for (j = 0; j < list->nbItems; j++) {
19202 use = list->items[j];
19203
19204 if ((prohib->name == WXS_ATTRUSE_DECL_NAME(use)) &&
19205 (prohib->targetNamespace == WXS_ATTRUSE_DECL_TNS(use)))
19206 {
19207 xmlChar *str = NULL;
19208
19209 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
19210 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
19211 prohib->node, NULL,
19212 "Skipping pointless attribute use prohibition "
19213 "'%s', since a corresponding attribute use "
19214 "exists already in the type definition",
19215 xmlSchemaFormatQName(&str,
19216 prohib->targetNamespace, prohib->name),
19217 NULL, NULL);
19218 FREE_AND_NULL(str);
19219 /*
19220 * Remove the prohibition.
19221 */
19222 if (xmlSchemaItemListRemove(prohibs, i) == -1)
19223 return(-1);
19224 break;
19225 }
19226 }
19227 }
19228 }
19229 return(0);
19230}
19231
19232/**
19233 * xmlSchemaAttributeGroupExpandRefs:
19234 * @pctxt: the parser context
Daniel Veillarddee23482008-04-11 12:58:43 +000019235 * @attrGr: the attribute group definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019236 *
19237 * Computation of:
19238 * {attribute uses} property
19239 * {attribute wildcard} property
19240 *
19241 * Substitutes contained attribute group references
19242 * for their attribute uses. Wilcards are intersected.
19243 */
19244static int
19245xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19246 xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillarddee23482008-04-11 12:58:43 +000019247{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019248 if ((attrGr->attrUses == NULL) ||
19249 (attrGr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED))
19250 return(0);
19251
19252 attrGr->flags |= XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED;
19253 if (xmlSchemaExpandAttributeGroupRefs(pctxt, WXS_BASIC_CAST attrGr,
19254 &(attrGr->attributeWildcard), attrGr->attrUses, NULL) == -1)
Daniel Veillarddee23482008-04-11 12:58:43 +000019255 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019256 return(0);
19257}
19258
19259/**
19260 * xmlSchemaAttributeGroupExpandRefs:
19261 * @pctxt: the parser context
Daniel Veillarddee23482008-04-11 12:58:43 +000019262 * @attrGr: the attribute group definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019263 *
19264 * Substitutes contained attribute group references
19265 * for their attribute uses. Wilcards are intersected.
Daniel Veillarddee23482008-04-11 12:58:43 +000019266 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019267 * Schema Component Constraint:
Daniel Veillarddee23482008-04-11 12:58:43 +000019268 * Attribute Group Definition Properties Correct (ag-props-correct)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019269 */
19270static int
19271xmlSchemaCheckAGPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19272 xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillarddee23482008-04-11 12:58:43 +000019273{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019274 /*
19275 * SPEC ag-props-correct
19276 * (1) "The values of the properties of an attribute group definition
19277 * must be as described in the property tableau in The Attribute
Rob Richardsc6947bb2008-06-29 15:04:41 +000019278 * Group Definition Schema Component (�3.6.1), modulo the impact of
19279 * Missing Sub-components (�5.3);"
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019280 */
Daniel Veillarddee23482008-04-11 12:58:43 +000019281
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019282 if ((attrGr->attrUses != NULL) &&
19283 (WXS_LIST_CAST attrGr->attrUses)->nbItems > 1)
19284 {
19285 xmlSchemaItemListPtr uses = WXS_LIST_CAST attrGr->attrUses;
19286 xmlSchemaAttributeUsePtr use, tmp;
19287 int i, j, hasId = 0;
19288
19289 for (i = uses->nbItems -1; i >= 0; i--) {
Daniel Veillarddee23482008-04-11 12:58:43 +000019290 use = uses->items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019291 /*
19292 * SPEC ag-props-correct
19293 * (2) "Two distinct members of the {attribute uses} must not have
19294 * {attribute declaration}s both of whose {name}s match and whose
19295 * {target namespace}s are identical."
19296 */
19297 if (i > 0) {
19298 for (j = i -1; j >= 0; j--) {
19299 tmp = uses->items[j];
19300 if ((WXS_ATTRUSE_DECL_NAME(use) ==
19301 WXS_ATTRUSE_DECL_NAME(tmp)) &&
19302 (WXS_ATTRUSE_DECL_TNS(use) ==
19303 WXS_ATTRUSE_DECL_TNS(tmp)))
19304 {
19305 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000019306
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019307 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19308 XML_SCHEMAP_AG_PROPS_CORRECT,
19309 attrGr->node, WXS_BASIC_CAST attrGr,
19310 "Duplicate %s",
19311 xmlSchemaGetComponentDesignation(&str, use),
19312 NULL);
19313 FREE_AND_NULL(str);
19314 /*
19315 * Remove the duplicate.
19316 */
19317 if (xmlSchemaItemListRemove(uses, i) == -1)
19318 return(-1);
19319 goto next_use;
19320 }
19321 }
19322 }
19323 /*
19324 * SPEC ag-props-correct
19325 * (3) "Two distinct members of the {attribute uses} must not have
19326 * {attribute declaration}s both of whose {type definition}s are or
19327 * are derived from ID."
19328 * TODO: Does 'derived' include member-types of unions?
19329 */
Daniel Veillarddee23482008-04-11 12:58:43 +000019330 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019331 if (xmlSchemaIsDerivedFromBuiltInType(
19332 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
Daniel Veillarddee23482008-04-11 12:58:43 +000019333 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019334 if (hasId) {
19335 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000019336
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019337 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19338 XML_SCHEMAP_AG_PROPS_CORRECT,
19339 attrGr->node, WXS_BASIC_CAST attrGr,
19340 "There must not exist more than one attribute "
19341 "declaration of type 'xs:ID' "
19342 "(or derived from 'xs:ID'). The %s violates this "
19343 "constraint",
19344 xmlSchemaGetComponentDesignation(&str, use),
19345 NULL);
19346 FREE_AND_NULL(str);
19347 if (xmlSchemaItemListRemove(uses, i) == -1)
19348 return(-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000019349 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019350 hasId = 1;
19351 }
19352 }
19353next_use: {}
19354 }
19355 }
19356 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019357}
19358
19359/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019360 * xmlSchemaResolveAttrGroupReferences:
Daniel Veillard13e04c62002-04-23 17:51:29 +000019361 * @attrgrpDecl: the schema attribute definition
19362 * @ctxt: the schema parser context
19363 * @name: the attribute name
19364 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019365 * Resolves references to attribute group definitions.
Daniel Veillard13e04c62002-04-23 17:51:29 +000019366 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019367static int
19368xmlSchemaResolveAttrGroupReferences(xmlSchemaQNameRefPtr ref,
19369 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard13e04c62002-04-23 17:51:29 +000019370{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019371 xmlSchemaAttributeGroupPtr group;
Daniel Veillard13e04c62002-04-23 17:51:29 +000019372
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019373 if (ref->item != NULL)
19374 return(0);
19375 group = xmlSchemaGetAttributeGroup(ctxt->schema,
19376 ref->name,
19377 ref->targetNamespace);
19378 if (group == NULL) {
19379 xmlSchemaPResCompAttrErr(ctxt,
19380 XML_SCHEMAP_SRC_RESOLVE,
19381 NULL, ref->node,
19382 "ref", ref->name, ref->targetNamespace,
19383 ref->itemType, NULL);
19384 return(ctxt->err);
Daniel Veillard3646d642004-06-02 19:19:14 +000019385 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019386 ref->item = WXS_BASIC_CAST group;
19387 return(0);
Daniel Veillard13e04c62002-04-23 17:51:29 +000019388}
19389
19390/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019391 * xmlSchemaCheckAttrPropsCorrect:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019392 * @item: an schema attribute declaration/use
19393 * @ctxt: a schema parser context
19394 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019395 *
19396 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019397 * Schema Component Constraint:
19398 * Attribute Declaration Properties Correct (a-props-correct)
Daniel Veillard4255d502002-04-16 15:50:10 +000019399 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019400 * Validates the value constraints of an attribute declaration/use.
19401 * NOTE that this needs the simle type definitions to be already
19402 * builded and checked.
Daniel Veillard4255d502002-04-16 15:50:10 +000019403 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019404static int
19405xmlSchemaCheckAttrPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19406 xmlSchemaAttributePtr attr)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019407{
19408
19409 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019410 * SPEC a-props-correct (1)
19411 * "The values of the properties of an attribute declaration must
19412 * be as described in the property tableau in The Attribute
Rob Richardsc6947bb2008-06-29 15:04:41 +000019413 * Declaration Schema Component (�3.2.1), modulo the impact of
19414 * Missing Sub-components (�5.3)."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019415 */
Daniel Veillarddee23482008-04-11 12:58:43 +000019416
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019417 if (WXS_ATTR_TYPEDEF(attr) == NULL)
19418 return(0);
19419
19420 if (attr->defValue != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019421 int ret;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019422
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019423 /*
19424 * SPEC a-props-correct (3)
19425 * "If the {type definition} is or is derived from ID then there
19426 * must not be a {value constraint}."
19427 */
19428 if (xmlSchemaIsDerivedFromBuiltInType(
19429 WXS_ATTR_TYPEDEF(attr), XML_SCHEMAS_ID))
19430 {
19431 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19432 XML_SCHEMAP_A_PROPS_CORRECT_3,
19433 NULL, WXS_BASIC_CAST attr,
19434 "Value constraints are not allowed if the type definition "
19435 "is or is derived from xs:ID",
19436 NULL, NULL);
19437 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019438 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019439 /*
19440 * SPEC a-props-correct (2)
19441 * "if there is a {value constraint}, the canonical lexical
Rob Richardsc6947bb2008-06-29 15:04:41 +000019442 * representation of its value must be �valid� with respect
19443 * to the {type definition} as defined in String Valid (�3.14.4)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019444 * TODO: Don't care about the *cononical* stuff here, this requirement
19445 * will be removed in WXS 1.1 anyway.
19446 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019447 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019448 attr->node, WXS_ATTR_TYPEDEF(attr),
19449 attr->defValue, &(attr->defVal),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019450 1, 1, 0);
19451 if (ret != 0) {
19452 if (ret < 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019453 PERROR_INT("xmlSchemaCheckAttrPropsCorrect",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019454 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019455 return(-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019456 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019457 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019458 XML_SCHEMAP_A_PROPS_CORRECT_2,
19459 NULL, WXS_BASIC_CAST attr,
19460 "The value of the value constraint is not valid",
19461 NULL, NULL);
19462 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019463 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019464 }
Daniel Veillarddee23482008-04-11 12:58:43 +000019465
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019466 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019467}
19468
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019469static xmlSchemaElementPtr
19470xmlSchemaCheckSubstGroupCircular(xmlSchemaElementPtr elemDecl,
19471 xmlSchemaElementPtr ancestor)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019472{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019473 xmlSchemaElementPtr ret;
19474
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019475 if (WXS_SUBST_HEAD(ancestor) == NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019476 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019477 if (WXS_SUBST_HEAD(ancestor) == elemDecl)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019478 return (ancestor);
19479
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019480 if (WXS_SUBST_HEAD(ancestor)->flags & XML_SCHEMAS_ELEM_CIRCULAR)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019481 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019482 WXS_SUBST_HEAD(ancestor)->flags |= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019483 ret = xmlSchemaCheckSubstGroupCircular(elemDecl,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019484 WXS_SUBST_HEAD(ancestor));
19485 WXS_SUBST_HEAD(ancestor)->flags ^= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019486
19487 return (ret);
19488}
19489
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019490/**
19491 * xmlSchemaCheckElemPropsCorrect:
19492 * @ctxt: a schema parser context
19493 * @decl: the element declaration
19494 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019495 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019496 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019497 * Element Declaration Properties Correct (e-props-correct)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019498 *
19499 * STATUS:
19500 * missing: (6)
19501 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019502static int
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019503xmlSchemaCheckElemPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19504 xmlSchemaElementPtr elemDecl)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019505{
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019506 int ret = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019507 xmlSchemaTypePtr typeDef = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019508 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019509 * SPEC (1) "The values of the properties of an element declaration
19510 * must be as described in the property tableau in The Element
Rob Richardsc6947bb2008-06-29 15:04:41 +000019511 * Declaration Schema Component (�3.3.1), modulo the impact of Missing
19512 * Sub-components (�5.3)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019513 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019514 if (WXS_SUBST_HEAD(elemDecl) != NULL) {
19515 xmlSchemaElementPtr head = WXS_SUBST_HEAD(elemDecl), circ;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019516
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019517 xmlSchemaCheckElementDeclComponent(head, pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019518 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000019519 * SPEC (3) "If there is a non-�absent� {substitution group
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019520 * affiliation}, then {scope} must be global."
19521 */
19522 if ((elemDecl->flags & XML_SCHEMAS_ELEM_GLOBAL) == 0) {
19523 xmlSchemaPCustomErr(pctxt,
19524 XML_SCHEMAP_E_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019525 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019526 "Only global element declarations can have a "
19527 "substitution group affiliation", NULL);
19528 ret = XML_SCHEMAP_E_PROPS_CORRECT_3;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019529 }
19530 /*
19531 * TODO: SPEC (6) "Circular substitution groups are disallowed.
19532 * That is, it must not be possible to return to an element declaration
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019533 * by repeatedly following the {substitution group affiliation}
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019534 * property."
19535 */
19536 if (head == elemDecl)
19537 circ = head;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019538 else if (WXS_SUBST_HEAD(head) != NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019539 circ = xmlSchemaCheckSubstGroupCircular(head, head);
19540 else
19541 circ = NULL;
19542 if (circ != NULL) {
19543 xmlChar *strA = NULL, *strB = NULL;
19544
19545 xmlSchemaPCustomErrExt(pctxt,
19546 XML_SCHEMAP_E_PROPS_CORRECT_6,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019547 WXS_BASIC_CAST circ, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019548 "The element declaration '%s' defines a circular "
19549 "substitution group to element declaration '%s'",
19550 xmlSchemaGetComponentQName(&strA, circ),
19551 xmlSchemaGetComponentQName(&strB, head),
19552 NULL);
19553 FREE_AND_NULL(strA)
19554 FREE_AND_NULL(strB)
19555 ret = XML_SCHEMAP_E_PROPS_CORRECT_6;
19556 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019557 /*
19558 * SPEC (4) "If there is a {substitution group affiliation},
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019559 * the {type definition}
19560 * of the element declaration must be validly derived from the {type
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019561 * definition} of the {substitution group affiliation}, given the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019562 * of the {substitution group exclusions} of the {substitution group
Rob Richardsc6947bb2008-06-29 15:04:41 +000019563 * affiliation}, as defined in Type Derivation OK (Complex) (�3.4.6)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019564 * (if the {type definition} is complex) or as defined in
Rob Richardsc6947bb2008-06-29 15:04:41 +000019565 * Type Derivation OK (Simple) (�3.14.6) (if the {type definition} is
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019566 * simple)."
19567 *
19568 * NOTE: {substitution group exclusions} means the values of the
19569 * attribute "final".
19570 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019571
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019572 if (typeDef != WXS_ELEM_TYPEDEF(WXS_SUBST_HEAD(elemDecl))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019573 int set = 0;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019574
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019575 if (head->flags & XML_SCHEMAS_ELEM_FINAL_EXTENSION)
19576 set |= SUBSET_EXTENSION;
19577 if (head->flags & XML_SCHEMAS_ELEM_FINAL_RESTRICTION)
19578 set |= SUBSET_RESTRICTION;
19579
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000019580 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST pctxt, typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019581 WXS_ELEM_TYPEDEF(head), set) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019582 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
19583
19584 ret = XML_SCHEMAP_E_PROPS_CORRECT_4;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019585 xmlSchemaPCustomErrExt(pctxt,
19586 XML_SCHEMAP_E_PROPS_CORRECT_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019587 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019588 "The type definition '%s' was "
19589 "either rejected by the substitution group "
19590 "affiliation '%s', or not validly derived from its type "
19591 "definition '%s'",
19592 xmlSchemaGetComponentQName(&strA, typeDef),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019593 xmlSchemaGetComponentQName(&strB, head),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019594 xmlSchemaGetComponentQName(&strC, WXS_ELEM_TYPEDEF(head)));
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019595 FREE_AND_NULL(strA)
19596 FREE_AND_NULL(strB)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019597 FREE_AND_NULL(strC)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019598 }
19599 }
19600 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019601 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019602 * SPEC (5) "If the {type definition} or {type definition}'s
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019603 * {content type}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019604 * is or is derived from ID then there must not be a {value constraint}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019605 * Note: The use of ID as a type definition for elements goes beyond
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019606 * XML 1.0, and should be avoided if backwards compatibility is desired"
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019607 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019608 if ((elemDecl->value != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019609 ((WXS_IS_SIMPLE(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019610 xmlSchemaIsDerivedFromBuiltInType(typeDef, XML_SCHEMAS_ID)) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019611 (WXS_IS_COMPLEX(typeDef) &&
19612 WXS_HAS_SIMPLE_CONTENT(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019613 xmlSchemaIsDerivedFromBuiltInType(typeDef->contentTypeDef,
19614 XML_SCHEMAS_ID)))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019615
19616 ret = XML_SCHEMAP_E_PROPS_CORRECT_5;
19617 xmlSchemaPCustomErr(pctxt,
19618 XML_SCHEMAP_E_PROPS_CORRECT_5,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019619 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019620 "The type definition (or type definition's content type) is or "
19621 "is derived from ID; value constraints are not allowed in "
19622 "conjunction with such a type definition", NULL);
19623 } else if (elemDecl->value != NULL) {
19624 int vcret;
19625 xmlNodePtr node = NULL;
19626
19627 /*
19628 * SPEC (2) "If there is a {value constraint}, the canonical lexical
Rob Richardsc6947bb2008-06-29 15:04:41 +000019629 * representation of its value must be �valid� with respect to the
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019630 * {type definition} as defined in Element Default Valid (Immediate)
Rob Richardsc6947bb2008-06-29 15:04:41 +000019631 * (�3.3.6)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019632 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019633 if (typeDef == NULL) {
19634 xmlSchemaPErr(pctxt, elemDecl->node,
19635 XML_SCHEMAP_INTERNAL,
19636 "Internal error: xmlSchemaCheckElemPropsCorrect, "
19637 "type is missing... skipping validation of "
19638 "the value constraint", NULL, NULL);
19639 return (-1);
19640 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019641 if (elemDecl->node != NULL) {
19642 if (elemDecl->flags & XML_SCHEMAS_ELEM_FIXED)
19643 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19644 BAD_CAST "fixed");
19645 else
19646 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19647 BAD_CAST "default");
19648 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019649 vcret = xmlSchemaParseCheckCOSValidDefault(pctxt, node,
19650 typeDef, elemDecl->value, &(elemDecl->defVal));
19651 if (vcret != 0) {
19652 if (vcret < 0) {
19653 PERROR_INT("xmlSchemaElemCheckValConstr",
19654 "failed to validate the value constraint of an "
19655 "element declaration");
19656 return (-1);
19657 }
19658 return (vcret);
19659 }
19660 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019661
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019662 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019663}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019664
19665/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019666 * xmlSchemaCheckElemSubstGroup:
19667 * @ctxt: a schema parser context
19668 * @decl: the element declaration
19669 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019670 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019671 * Schema Component Constraint:
19672 * Substitution Group (cos-equiv-class)
19673 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019674 * In Libxml2 the subst. groups will be precomputed, in terms of that
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019675 * a list will be built for each subst. group head, holding all direct
19676 * referents to this head.
19677 * NOTE that this function needs:
19678 * 1. circular subst. groups to be checked beforehand
19679 * 2. the declaration's type to be derived from the head's type
19680 *
19681 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019682 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019683 */
19684static void
19685xmlSchemaCheckElemSubstGroup(xmlSchemaParserCtxtPtr ctxt,
19686 xmlSchemaElementPtr elemDecl)
19687{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019688 if ((WXS_SUBST_HEAD(elemDecl) == NULL) ||
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019689 /* SPEC (1) "Its {abstract} is false." */
19690 (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT))
19691 return;
19692 {
19693 xmlSchemaElementPtr head;
19694 xmlSchemaTypePtr headType, type;
19695 int set, methSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019696 /*
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019697 * SPEC (2) "It is validly substitutable for HEAD subject to HEAD's
19698 * {disallowed substitutions} as the blocking constraint, as defined in
Rob Richardsc6947bb2008-06-29 15:04:41 +000019699 * Substitution Group OK (Transitive) (�3.3.6)."
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019700 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019701 for (head = WXS_SUBST_HEAD(elemDecl); head != NULL;
19702 head = WXS_SUBST_HEAD(head)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019703 set = 0;
19704 methSet = 0;
19705 /*
19706 * The blocking constraints.
19707 */
19708 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION)
19709 continue;
19710 headType = head->subtypes;
19711 type = elemDecl->subtypes;
19712 if (headType == type)
19713 goto add_member;
19714 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION)
19715 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19716 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION)
19717 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19718 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019719 * SPEC: Substitution Group OK (Transitive) (2.3)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019720 * "The set of all {derivation method}s involved in the
19721 * derivation of D's {type definition} from C's {type definition}
19722 * does not intersect with the union of the blocking constraint,
19723 * C's {prohibited substitutions} (if C is complex, otherwise the
19724 * empty set) and the {prohibited substitutions} (respectively the
19725 * empty set) of any intermediate {type definition}s in the
19726 * derivation of D's {type definition} from C's {type definition}."
19727 */
19728 /*
19729 * OPTIMIZE TODO: Optimize this a bit, since, if traversing the
19730 * subst.head axis, the methSet does not need to be computed for
19731 * the full depth over and over.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019732 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019733 /*
19734 * The set of all {derivation method}s involved in the derivation
19735 */
19736 while ((type != NULL) && (type != headType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019737 if ((WXS_IS_EXTENSION(type)) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019738 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19739 methSet |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019740
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019741 if (WXS_IS_RESTRICTION(type) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019742 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19743 methSet |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19744
19745 type = type->baseType;
19746 }
19747 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019748 * The {prohibited substitutions} of all intermediate types +
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019749 * the head's type.
19750 */
19751 type = elemDecl->subtypes->baseType;
19752 while (type != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019753 if (WXS_IS_COMPLEX(type)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019754 if ((type->flags &
19755 XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19756 ((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) == 0))
19757 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19758 if ((type->flags &
19759 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19760 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19761 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19762 } else
19763 break;
19764 if (type == headType)
19765 break;
19766 type = type->baseType;
19767 }
19768 if ((set != 0) &&
19769 (((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19770 (methSet & XML_SCHEMAS_TYPE_BLOCK_EXTENSION)) ||
19771 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19772 (methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION)))) {
19773 continue;
19774 }
19775add_member:
19776 xmlSchemaAddElementSubstitutionMember(ctxt, head, elemDecl);
19777 if ((head->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) == 0)
19778 head->flags |= XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD;
19779 }
19780 }
19781}
19782
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019783#ifdef WXS_ELEM_DECL_CONS_ENABLED /* enable when finished */
19784/**
19785 * xmlSchemaCheckElementDeclComponent
19786 * @pctxt: the schema parser context
19787 * @ctxtComponent: the context component (an element declaration)
19788 * @ctxtParticle: the first particle of the context component
19789 * @searchParticle: the element declaration particle to be analysed
19790 *
Daniel Veillarddee23482008-04-11 12:58:43 +000019791 * Schema Component Constraint: Element Declarations Consistent
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019792 */
19793static int
19794xmlSchemaCheckElementDeclConsistent(xmlSchemaParserCtxtPtr pctxt,
19795 xmlSchemaBasicItemPtr ctxtComponent,
Daniel Veillarddee23482008-04-11 12:58:43 +000019796 xmlSchemaParticlePtr ctxtParticle,
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019797 xmlSchemaParticlePtr searchParticle,
19798 xmlSchemaParticlePtr curParticle,
19799 int search)
19800{
19801 return(0);
19802
19803 int ret = 0;
19804 xmlSchemaParticlePtr cur = curParticle;
Daniel Veillarddee23482008-04-11 12:58:43 +000019805 if (curParticle == NULL) {
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019806 return(0);
19807 }
19808 if (WXS_PARTICLE_TERM(curParticle) == NULL) {
19809 /*
19810 * Just return in this case. A missing "term" of the particle
19811 * might arise due to an invalid "term" component.
19812 */
19813 return(0);
Daniel Veillarddee23482008-04-11 12:58:43 +000019814 }
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019815 while (cur != NULL) {
19816 switch (WXS_PARTICLE_TERM(cur)->type) {
19817 case XML_SCHEMA_TYPE_ANY:
19818 break;
19819 case XML_SCHEMA_TYPE_ELEMENT:
19820 if (search == 0) {
19821 ret = xmlSchemaCheckElementDeclConsistent(pctxt,
19822 ctxtComponent, ctxtParticle, cur, ctxtParticle, 1);
19823 if (ret != 0)
Daniel Veillarddee23482008-04-11 12:58:43 +000019824 return(ret);
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019825 } else {
19826 xmlSchemaElementPtr elem =
19827 WXS_ELEM_CAST(WXS_PARTICLE_TERM(cur));
19828 /*
19829 * SPEC Element Declarations Consistent:
19830 * "If the {particles} contains, either directly,
19831 * indirectly (that is, within the {particles} of a
Rob Richardsc6947bb2008-06-29 15:04:41 +000019832 * contained model group, recursively) or �implicitly�
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019833 * two or more element declaration particles with
19834 * the same {name} and {target namespace}, then
19835 * all their type definitions must be the same
19836 * top-level definition [...]"
19837 */
19838 if (xmlStrEqual(WXS_PARTICLE_TERM_AS_ELEM(cur)->name,
19839 WXS_PARTICLE_TERM_AS_ELEM(searchParticle)->name) &&
19840 xmlStrEqual(WXS_PARTICLE_TERM_AS_ELEM(cur)->targetNamespace,
19841 WXS_PARTICLE_TERM_AS_ELEM(searchParticle)->targetNamespace))
19842 {
19843 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000019844
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019845 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19846 /* TODO: error code */
19847 XML_SCHEMAP_COS_NONAMBIG,
19848 WXS_ITEM_NODE(cur), NULL,
19849 "In the content model of %s, there are multiple "
19850 "element declarations for '%s' with different "
19851 "type definitions",
19852 xmlSchemaGetComponentDesignation(&strA,
19853 ctxtComponent),
19854 xmlSchemaFormatQName(&strB,
19855 WXS_PARTICLE_TERM_AS_ELEM(cur)->targetNamespace,
19856 WXS_PARTICLE_TERM_AS_ELEM(cur)->name));
19857 FREE_AND_NULL(strA);
19858 FREE_AND_NULL(strB);
19859 return(XML_SCHEMAP_COS_NONAMBIG);
19860 }
Daniel Veillarddee23482008-04-11 12:58:43 +000019861 }
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019862 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000019863 case XML_SCHEMA_TYPE_SEQUENCE: {
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019864 break;
19865 }
19866 case XML_SCHEMA_TYPE_CHOICE:{
19867 /*
19868 xmlSchemaTreeItemPtr sub;
Daniel Veillarddee23482008-04-11 12:58:43 +000019869
19870 sub = WXS_PARTICLE_TERM(particle)->children; (xmlSchemaParticlePtr)
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019871 while (sub != NULL) {
19872 ret = xmlSchemaCheckElementDeclConsistent(pctxt, ctxtComponent,
19873 ctxtParticle, ctxtElem);
19874 if (ret != 0)
19875 return(ret);
19876 sub = sub->next;
19877 }
19878 */
19879 break;
19880 }
19881 case XML_SCHEMA_TYPE_ALL:
19882 break;
19883 case XML_SCHEMA_TYPE_GROUP:
19884 break;
19885 default:
19886 xmlSchemaInternalErr2(ACTXT_CAST pctxt,
19887 "xmlSchemaCheckElementDeclConsistent",
19888 "found unexpected term of type '%s' in content model",
19889 WXS_ITEM_TYPE_NAME(WXS_PARTICLE_TERM(cur)), NULL);
19890 return(-1);
19891 }
19892 cur = (xmlSchemaParticlePtr) cur->next;
19893 }
19894
19895exit:
19896 return(ret);
19897}
19898#endif
19899
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019900/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019901 * xmlSchemaCheckElementDeclComponent
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019902 * @item: an schema element declaration/particle
19903 * @ctxt: a schema parser context
19904 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019905 *
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019906 * Validates the value constraints of an element declaration.
Daniel Veillarddee23482008-04-11 12:58:43 +000019907 * Adds substitution group members.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019908 */
19909static void
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019910xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019911 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019912{
19913 if (elemDecl == NULL)
19914 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019915 if (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_CHECKED)
19916 return;
19917 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_CHECKED;
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019918 if (xmlSchemaCheckElemPropsCorrect(ctxt, elemDecl) == 0) {
19919 /*
19920 * Adds substitution group members.
19921 */
Daniel Veillarddee23482008-04-11 12:58:43 +000019922 xmlSchemaCheckElemSubstGroup(ctxt, elemDecl);
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019923 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019924}
19925
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019926/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019927 * xmlSchemaResolveModelGroupParticleReferences:
19928 * @particle: a particle component
19929 * @ctxt: a parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019930 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019931 * Resolves references of a model group's {particles} to
19932 * model group definitions and to element declarations.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019933 */
19934static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019935xmlSchemaResolveModelGroupParticleReferences(
19936 xmlSchemaParserCtxtPtr ctxt,
19937 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019938{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019939 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
19940 xmlSchemaQNameRefPtr ref;
19941 xmlSchemaBasicItemPtr refItem;
19942
19943 /*
19944 * URGENT TODO: Test this.
19945 */
19946 while (particle != NULL) {
19947 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
19948 ((WXS_PARTICLE_TERM(particle))->type !=
19949 XML_SCHEMA_EXTRA_QNAMEREF))
19950 {
19951 goto next_particle;
Daniel Veillarddee23482008-04-11 12:58:43 +000019952 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019953 ref = WXS_QNAME_CAST WXS_PARTICLE_TERM(particle);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019954 /*
19955 * Resolve the reference.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019956 * NULL the {term} by default.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019957 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019958 particle->children = NULL;
19959
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019960 refItem = xmlSchemaGetNamedComponent(ctxt->schema,
19961 ref->itemType, ref->name, ref->targetNamespace);
19962 if (refItem == NULL) {
19963 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019964 NULL, WXS_ITEM_NODE(particle), "ref", ref->name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019965 ref->targetNamespace, ref->itemType, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019966 /* TODO: remove the particle. */
19967 goto next_particle;
Daniel Veillarddee23482008-04-11 12:58:43 +000019968 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019969 if (refItem->type == XML_SCHEMA_TYPE_GROUP) {
19970 if (WXS_MODELGROUPDEF_MODEL(refItem) == NULL)
19971 /* TODO: remove the particle. */
19972 goto next_particle;
19973 /*
19974 * NOTE that we will assign the model group definition
19975 * itself to the "term" of the particle. This will ease
19976 * the check for circular model group definitions. After
19977 * that the "term" will be assigned the model group of the
19978 * model group definition.
19979 */
19980 if ((WXS_MODELGROUPDEF_MODEL(refItem))->type ==
19981 XML_SCHEMA_TYPE_ALL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019982 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019983 * SPEC cos-all-limited (1)
19984 * SPEC cos-all-limited (1.2)
19985 * "It appears only as the value of one or both of the
19986 * following properties:"
19987 * (1.1) "the {model group} property of a model group
19988 * definition."
19989 * (1.2) "the {term} property of a particle [... of] the "
19990 * {content type} of a complex type definition."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019991 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019992 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19993 /* TODO: error code */
19994 XML_SCHEMAP_COS_ALL_LIMITED,
19995 WXS_ITEM_NODE(particle), NULL,
19996 "A model group definition is referenced, but "
19997 "it contains an 'all' model group, which "
19998 "cannot be contained by model groups",
19999 NULL, NULL);
20000 /* TODO: remove the particle. */
20001 goto next_particle;
20002 }
20003 particle->children = (xmlSchemaTreeItemPtr) refItem;
20004 } else {
20005 /*
20006 * TODO: Are referenced element declarations the only
20007 * other components we expect here?
20008 */
20009 particle->children = (xmlSchemaTreeItemPtr) refItem;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000020010 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020011next_particle:
20012 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000020013 }
20014}
20015
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020016static int
20017xmlSchemaAreValuesEqual(xmlSchemaValPtr x,
Daniel Veillarddee23482008-04-11 12:58:43 +000020018 xmlSchemaValPtr y)
20019{
20020 xmlSchemaTypePtr tx, ty, ptx, pty;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020021 int ret;
20022
20023 while (x != NULL) {
20024 /* Same types. */
20025 tx = xmlSchemaGetBuiltInType(xmlSchemaGetValType(x));
20026 ty = xmlSchemaGetBuiltInType(xmlSchemaGetValType(y));
20027 ptx = xmlSchemaGetPrimitiveType(tx);
20028 pty = xmlSchemaGetPrimitiveType(ty);
20029 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000020030 * (1) if a datatype T' is �derived� by �restriction� from an
20031 * atomic datatype T then the �value space� of T' is a subset of
20032 * the �value space� of T. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020033 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000020034 * (2) if datatypes T' and T'' are �derived� by �restriction�
20035 * from a common atomic ancestor T then the �value space�s of T'
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020036 * and T'' may overlap.
20037 */
20038 if (ptx != pty)
20039 return(0);
20040 /*
20041 * We assume computed values to be normalized, so do a fast
20042 * string comparison for string based types.
20043 */
20044 if ((ptx->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020045 WXS_IS_ANY_SIMPLE_TYPE(ptx)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020046 if (! xmlStrEqual(
20047 xmlSchemaValueGetAsString(x),
20048 xmlSchemaValueGetAsString(y)))
20049 return (0);
20050 } else {
20051 ret = xmlSchemaCompareValuesWhtsp(
20052 x, XML_SCHEMA_WHITESPACE_PRESERVE,
20053 y, XML_SCHEMA_WHITESPACE_PRESERVE);
20054 if (ret == -2)
20055 return(-1);
20056 if (ret != 0)
20057 return(0);
20058 }
20059 /*
20060 * Lists.
20061 */
20062 x = xmlSchemaValueGetNext(x);
20063 if (x != NULL) {
20064 y = xmlSchemaValueGetNext(y);
20065 if (y == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000020066 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020067 } else if (xmlSchemaValueGetNext(y) != NULL)
20068 return (0);
20069 else
20070 return (1);
20071 }
20072 return (0);
20073}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000020074
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020075/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020076 * xmlSchemaResolveAttrUseReferences:
20077 * @item: an attribute use
20078 * @ctxt: a parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020079 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020080 * Resolves the referenced attribute declaration.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020081 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020082static int
20083xmlSchemaResolveAttrUseReferences(xmlSchemaAttributeUsePtr ause,
20084 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000020085{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020086 if ((ctxt == NULL) || (ause == NULL))
20087 return(-1);
20088 if ((ause->attrDecl == NULL) ||
20089 (ause->attrDecl->type != XML_SCHEMA_EXTRA_QNAMEREF))
20090 return(0);
20091
20092 {
20093 xmlSchemaQNameRefPtr ref = WXS_QNAME_CAST ause->attrDecl;
20094
20095 /*
20096 * TODO: Evaluate, what errors could occur if the declaration is not
20097 * found.
20098 */
20099 ause->attrDecl = xmlSchemaGetAttributeDecl(ctxt->schema,
20100 ref->name, ref->targetNamespace);
20101 if (ause->attrDecl == NULL) {
20102 xmlSchemaPResCompAttrErr(ctxt,
20103 XML_SCHEMAP_SRC_RESOLVE,
20104 WXS_BASIC_CAST ause, ause->node,
20105 "ref", ref->name, ref->targetNamespace,
20106 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
20107 return(ctxt->err);;
20108 }
20109 }
20110 return(0);
20111}
20112
20113/**
20114 * xmlSchemaCheckAttrUsePropsCorrect:
20115 * @ctxt: a parser context
Daniel Veillarddee23482008-04-11 12:58:43 +000020116 * @use: an attribute use
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020117 *
20118 * Schema Component Constraint:
20119 * Attribute Use Correct (au-props-correct)
Daniel Veillarddee23482008-04-11 12:58:43 +000020120 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020121 */
20122static int
20123xmlSchemaCheckAttrUsePropsCorrect(xmlSchemaParserCtxtPtr ctxt,
20124 xmlSchemaAttributeUsePtr use)
20125{
20126 if ((ctxt == NULL) || (use == NULL))
20127 return(-1);
20128 if ((use->defValue == NULL) || (WXS_ATTRUSE_DECL(use) == NULL) ||
20129 ((WXS_ATTRUSE_DECL(use))->type != XML_SCHEMA_TYPE_ATTRIBUTE))
Daniel Veillarddee23482008-04-11 12:58:43 +000020130 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020131
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020132 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020133 * SPEC au-props-correct (1)
20134 * "The values of the properties of an attribute use must be as
20135 * described in the property tableau in The Attribute Use Schema
Rob Richardsc6947bb2008-06-29 15:04:41 +000020136 * Component (�3.5.1), modulo the impact of Missing
20137 * Sub-components (�5.3)."
Daniel Veillardc0826a72004-08-10 14:17:33 +000020138 */
Daniel Veillarddee23482008-04-11 12:58:43 +000020139
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020140 if (((WXS_ATTRUSE_DECL(use))->defValue != NULL) &&
20141 ((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMAS_ATTR_FIXED) &&
20142 ((use->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
20143 {
20144 xmlSchemaPCustomErr(ctxt,
20145 XML_SCHEMAP_AU_PROPS_CORRECT_2,
20146 WXS_BASIC_CAST use, NULL,
20147 "The attribute declaration has a 'fixed' value constraint "
20148 ", thus the attribute use must also have a 'fixed' value "
20149 "constraint",
20150 NULL);
20151 return(ctxt->err);
20152 }
20153 /*
20154 * Compute and check the value constraint's value.
20155 */
20156 if ((use->defVal != NULL) && (WXS_ATTRUSE_TYPEDEF(use) != NULL)) {
20157 int ret;
20158 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000020159 * TODO: The spec seems to be missing a check of the
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020160 * value constraint of the attribute use. We will do it here.
20161 */
20162 /*
20163 * SPEC a-props-correct (3)
20164 */
20165 if (xmlSchemaIsDerivedFromBuiltInType(
20166 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
20167 {
20168 xmlSchemaCustomErr(ACTXT_CAST ctxt,
20169 XML_SCHEMAP_AU_PROPS_CORRECT,
20170 NULL, WXS_BASIC_CAST use,
20171 "Value constraints are not allowed if the type definition "
20172 "is or is derived from xs:ID",
20173 NULL, NULL);
20174 return(ctxt->err);
20175 }
Daniel Veillarddee23482008-04-11 12:58:43 +000020176
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020177 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST ctxt,
20178 use->node, WXS_ATTRUSE_TYPEDEF(use),
20179 use->defValue, &(use->defVal),
20180 1, 1, 0);
20181 if (ret != 0) {
20182 if (ret < 0) {
20183 PERROR_INT2("xmlSchemaCheckAttrUsePropsCorrect",
20184 "calling xmlSchemaVCheckCVCSimpleType()");
20185 return(-1);
20186 }
20187 xmlSchemaCustomErr(ACTXT_CAST ctxt,
20188 XML_SCHEMAP_AU_PROPS_CORRECT,
20189 NULL, WXS_BASIC_CAST use,
20190 "The value of the value constraint is not valid",
20191 NULL, NULL);
20192 return(ctxt->err);
20193 }
20194 }
20195 /*
20196 * SPEC au-props-correct (2)
20197 * "If the {attribute declaration} has a fixed
20198 * {value constraint}, then if the attribute use itself has a
20199 * {value constraint}, it must also be fixed and its value must match
20200 * that of the {attribute declaration}'s {value constraint}."
20201 */
20202 if (((WXS_ATTRUSE_DECL(use))->defVal != NULL) &&
20203 (((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
20204 {
20205 if (! xmlSchemaAreValuesEqual(use->defVal,
20206 (WXS_ATTRUSE_DECL(use))->defVal))
20207 {
20208 xmlSchemaPCustomErr(ctxt,
20209 XML_SCHEMAP_AU_PROPS_CORRECT_2,
20210 WXS_BASIC_CAST use, NULL,
20211 "The 'fixed' value constraint of the attribute use "
20212 "must match the attribute declaration's value "
20213 "constraint '%s'",
20214 (WXS_ATTRUSE_DECL(use))->defValue);
20215 }
20216 return(ctxt->err);
20217 }
20218 return(0);
20219}
20220
20221
20222
20223
20224/**
20225 * xmlSchemaResolveAttrTypeReferences:
20226 * @item: an attribute declaration
Daniel Veillarddee23482008-04-11 12:58:43 +000020227 * @ctxt: a parser context
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020228 *
20229 * Resolves the referenced type definition component.
20230 */
20231static int
20232xmlSchemaResolveAttrTypeReferences(xmlSchemaAttributePtr item,
20233 xmlSchemaParserCtxtPtr ctxt)
20234{
Daniel Veillard01fa6152004-06-29 17:04:39 +000020235 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020236 * The simple type definition corresponding to the <simpleType> element
20237 * information item in the [children], if present, otherwise the simple
Rob Richardsc6947bb2008-06-29 15:04:41 +000020238 * type definition �resolved� to by the �actual value� of the type
20239 * [attribute], if present, otherwise the �simple ur-type definition�.
Daniel Veillard01fa6152004-06-29 17:04:39 +000020240 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020241 if (item->flags & XML_SCHEMAS_ATTR_INTERNAL_RESOLVED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020242 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020243 item->flags |= XML_SCHEMAS_ATTR_INTERNAL_RESOLVED;
20244 if (item->subtypes != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020245 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020246 if (item->typeName != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000020247 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000020248
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020249 type = xmlSchemaGetType(ctxt->schema, item->typeName,
20250 item->typeNs);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020251 if ((type == NULL) || (! WXS_IS_SIMPLE(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000020252 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000020253 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020254 WXS_BASIC_CAST item, item->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020255 "type", item->typeName, item->typeNs,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020256 XML_SCHEMA_TYPE_SIMPLE, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020257 return(ctxt->err);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020258 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020259 item->subtypes = type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020260
Daniel Veillard3646d642004-06-02 19:19:14 +000020261 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020262 /*
20263 * The type defaults to the xs:anySimpleType.
20264 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020265 item->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
20266 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020267 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000020268}
20269
20270/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020271 * xmlSchemaResolveIDCKeyReferences:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020272 * @idc: the identity-constraint definition
20273 * @ctxt: the schema parser context
20274 * @name: the attribute name
20275 *
20276 * Resolve keyRef references to key/unique IDCs.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020277 * Schema Component Constraint:
20278 * Identity-constraint Definition Properties Correct (c-props-correct)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020279 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020280static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020281xmlSchemaResolveIDCKeyReferences(xmlSchemaIDCPtr idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020282 xmlSchemaParserCtxtPtr pctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020283{
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020284 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020285 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020286 if (idc->ref->name != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020287 idc->ref->item = (xmlSchemaBasicItemPtr)
20288 xmlSchemaGetIDC(pctxt->schema, idc->ref->name,
20289 idc->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020290 if (idc->ref->item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020291 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020292 * TODO: It is actually not an error to fail to resolve
20293 * at this stage. BUT we need to be that strict!
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020294 */
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020295 xmlSchemaPResCompAttrErr(pctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020296 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020297 WXS_BASIC_CAST idc, idc->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020298 "refer", idc->ref->name,
20299 idc->ref->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020300 XML_SCHEMA_TYPE_IDC_KEY, NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020301 return(pctxt->err);
20302 } else if (idc->ref->item->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
20303 /*
20304 * SPEC c-props-correct (1)
20305 */
20306 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20307 XML_SCHEMAP_C_PROPS_CORRECT,
20308 NULL, WXS_BASIC_CAST idc,
20309 "The keyref references a keyref",
20310 NULL, NULL);
20311 idc->ref->item = NULL;
20312 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020313 } else {
20314 if (idc->nbFields !=
20315 ((xmlSchemaIDCPtr) idc->ref->item)->nbFields) {
20316 xmlChar *str = NULL;
20317 xmlSchemaIDCPtr refer;
Daniel Veillarddee23482008-04-11 12:58:43 +000020318
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020319 refer = (xmlSchemaIDCPtr) idc->ref->item;
20320 /*
20321 * SPEC c-props-correct(2)
20322 * "If the {identity-constraint category} is keyref,
20323 * the cardinality of the {fields} must equal that of
20324 * the {fields} of the {referenced key}.
20325 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020326 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020327 XML_SCHEMAP_C_PROPS_CORRECT,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020328 NULL, WXS_BASIC_CAST idc,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020329 "The cardinality of the keyref differs from the "
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020330 "cardinality of the referenced key/unique '%s'",
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020331 xmlSchemaFormatQName(&str, refer->targetNamespace,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020332 refer->name),
20333 NULL);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020334 FREE_AND_NULL(str)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020335 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020336 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020337 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020338 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020339 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020340}
20341
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020342static int
20343xmlSchemaResolveAttrUseProhibReferences(xmlSchemaAttributeUseProhibPtr prohib,
20344 xmlSchemaParserCtxtPtr pctxt)
20345{
20346 if (xmlSchemaGetAttributeDecl(pctxt->schema, prohib->name,
20347 prohib->targetNamespace) == NULL) {
20348
20349 xmlSchemaPResCompAttrErr(pctxt,
20350 XML_SCHEMAP_SRC_RESOLVE,
20351 NULL, prohib->node,
20352 "ref", prohib->name, prohib->targetNamespace,
20353 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
20354 return(XML_SCHEMAP_SRC_RESOLVE);
20355 }
20356 return(0);
20357}
20358
20359#define WXS_REDEFINED_TYPE(c) \
20360(((xmlSchemaTypePtr) item)->flags & XML_SCHEMAS_TYPE_REDEFINED)
20361
20362#define WXS_REDEFINED_MODEL_GROUP_DEF(c) \
20363(((xmlSchemaModelGroupDefPtr) item)->flags & XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20364
20365#define WXS_REDEFINED_ATTR_GROUP(c) \
20366(((xmlSchemaAttributeGroupPtr) item)->flags & XML_SCHEMAS_ATTRGROUP_REDEFINED)
20367
20368static int
20369xmlSchemaCheckSRCRedefineFirst(xmlSchemaParserCtxtPtr pctxt)
20370{
20371 int err = 0;
20372 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20373 xmlSchemaBasicItemPtr prev, item;
20374 int wasRedefined;
20375
20376 if (redef == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000020377 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020378
20379 do {
20380 item = redef->item;
20381 /*
20382 * First try to locate the redefined component in the
20383 * schema graph starting with the redefined schema.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020384 * NOTE: According to this schema bug entry:
20385 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005OctDec/0019.html
20386 * it's not clear if the referenced component needs to originate
20387 * from the <redefine>d schema _document_ or the schema; the latter
20388 * would include all imported and included sub-schemas of the
20389 * <redefine>d schema. Currenlty we latter approach is used.
20390 * SUPPLEMENT: It seems that the WG moves towards the latter
20391 * approach, so we are doing it right.
Daniel Veillarddee23482008-04-11 12:58:43 +000020392 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020393 */
20394 prev = xmlSchemaFindRedefCompInGraph(
20395 redef->targetBucket, item->type,
20396 redef->refName, redef->refTargetNs);
20397 if (prev == NULL) {
20398 xmlChar *str = NULL;
20399 xmlNodePtr node;
20400
20401 /*
20402 * SPEC src-redefine:
Rob Richardsc6947bb2008-06-29 15:04:41 +000020403 * (6.2.1) "The �actual value� of its own name attribute plus
20404 * target namespace must successfully �resolve� to a model
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020405 * group definition in I."
Rob Richardsc6947bb2008-06-29 15:04:41 +000020406 * (7.2.1) "The �actual value� of its own name attribute plus
20407 * target namespace must successfully �resolve� to an attribute
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020408 * group definition in I."
20409
20410 *
20411 * Note that, if we are redefining with the use of references
20412 * to components, the spec assumes the src-resolve to be used;
20413 * but this won't assure that we search only *inside* the
20414 * redefined schema.
20415 */
20416 if (redef->reference)
20417 node = WXS_ITEM_NODE(redef->reference);
20418 else
20419 node = WXS_ITEM_NODE(item);
20420 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20421 /*
20422 * TODO: error code.
20423 * Probably XML_SCHEMAP_SRC_RESOLVE, if this is using the
Daniel Veillarddee23482008-04-11 12:58:43 +000020424 * reference kind.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020425 */
20426 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
Kasimier T. Buchcik5d2998b2005-11-22 17:36:01 +000020427 "The %s '%s' to be redefined could not be found in "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020428 "the redefined schema",
20429 WXS_ITEM_TYPE_NAME(item),
20430 xmlSchemaFormatQName(&str, redef->refTargetNs,
20431 redef->refName));
Daniel Veillarddee23482008-04-11 12:58:43 +000020432 FREE_AND_NULL(str);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020433 err = pctxt->err;
20434 redef = redef->next;
20435 continue;
20436 }
20437 /*
20438 * TODO: Obtaining and setting the redefinition state is really
20439 * clumsy.
20440 */
20441 wasRedefined = 0;
20442 switch (item->type) {
20443 case XML_SCHEMA_TYPE_COMPLEX:
20444 case XML_SCHEMA_TYPE_SIMPLE:
20445 if ((WXS_TYPE_CAST prev)->flags &
20446 XML_SCHEMAS_TYPE_REDEFINED)
20447 {
20448 wasRedefined = 1;
20449 break;
20450 }
20451 /* Mark it as redefined. */
20452 (WXS_TYPE_CAST prev)->flags |= XML_SCHEMAS_TYPE_REDEFINED;
20453 /*
20454 * Assign the redefined type to the
20455 * base type of the redefining type.
20456 * TODO: How
20457 */
Daniel Veillarddee23482008-04-11 12:58:43 +000020458 ((xmlSchemaTypePtr) item)->baseType =
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020459 (xmlSchemaTypePtr) prev;
20460 break;
20461 case XML_SCHEMA_TYPE_GROUP:
20462 if ((WXS_MODEL_GROUPDEF_CAST prev)->flags &
20463 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20464 {
20465 wasRedefined = 1;
20466 break;
20467 }
20468 /* Mark it as redefined. */
20469 (WXS_MODEL_GROUPDEF_CAST prev)->flags |=
20470 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED;
20471 if (redef->reference != NULL) {
20472 /*
20473 * Overwrite the QName-reference with the
20474 * referenced model group def.
20475 */
20476 (WXS_PTC_CAST redef->reference)->children =
20477 WXS_TREE_CAST prev;
20478 }
20479 redef->target = prev;
20480 break;
20481 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20482 if ((WXS_ATTR_GROUP_CAST prev)->flags &
20483 XML_SCHEMAS_ATTRGROUP_REDEFINED)
20484 {
20485 wasRedefined = 1;
20486 break;
20487 }
20488 (WXS_ATTR_GROUP_CAST prev)->flags |=
20489 XML_SCHEMAS_ATTRGROUP_REDEFINED;
20490 if (redef->reference != NULL) {
20491 /*
20492 * Assign the redefined attribute group to the
20493 * QName-reference component.
20494 * This is the easy case, since we will just
20495 * expand the redefined group.
20496 */
20497 (WXS_QNAME_CAST redef->reference)->item = prev;
20498 redef->target = NULL;
20499 } else {
20500 /*
20501 * This is the complicated case: we need
20502 * to apply src-redefine (7.2.2) at a later
20503 * stage, i.e. when attribute group references
20504 * have beed expanded and simple types have
20505 * beed fixed.
20506 */
20507 redef->target = prev;
20508 }
20509 break;
20510 default:
20511 PERROR_INT("xmlSchemaResolveRedefReferences",
20512 "Unexpected redefined component type");
Daniel Veillarddee23482008-04-11 12:58:43 +000020513 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020514 }
20515 if (wasRedefined) {
20516 xmlChar *str = NULL;
20517 xmlNodePtr node;
20518
20519 if (redef->reference)
20520 node = WXS_ITEM_NODE(redef->reference);
20521 else
20522 node = WXS_ITEM_NODE(redef->item);
Daniel Veillarddee23482008-04-11 12:58:43 +000020523
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020524 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20525 /* TODO: error code. */
20526 XML_SCHEMAP_SRC_REDEFINE,
20527 node, NULL,
20528 "The referenced %s was already redefined. Multiple "
20529 "redefinition of the same component is not supported",
20530 xmlSchemaGetComponentDesignation(&str, prev),
20531 NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000020532 FREE_AND_NULL(str)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020533 err = pctxt->err;
20534 redef = redef->next;
20535 continue;
20536 }
Daniel Veillarddee23482008-04-11 12:58:43 +000020537 redef = redef->next;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020538 } while (redef != NULL);
20539
20540 return(err);
20541}
20542
20543static int
20544xmlSchemaCheckSRCRedefineSecond(xmlSchemaParserCtxtPtr pctxt)
20545{
20546 int err = 0;
20547 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20548 xmlSchemaBasicItemPtr item;
20549
20550 if (redef == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000020551 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020552
20553 do {
20554 if (redef->target == NULL) {
20555 redef = redef->next;
20556 continue;
20557 }
20558 item = redef->item;
Daniel Veillarddee23482008-04-11 12:58:43 +000020559
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020560 switch (item->type) {
20561 case XML_SCHEMA_TYPE_SIMPLE:
20562 case XML_SCHEMA_TYPE_COMPLEX:
20563 /*
20564 * Since the spec wants the {name} of the redefined
20565 * type to be 'absent', we'll NULL it.
20566 */
20567 (WXS_TYPE_CAST redef->target)->name = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000020568
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020569 /*
20570 * TODO: Seems like there's nothing more to do. The normal
20571 * inheritance mechanism is used. But not 100% sure.
20572 */
20573 break;
20574 case XML_SCHEMA_TYPE_GROUP:
20575 /*
20576 * URGENT TODO:
20577 * SPEC src-redefine:
20578 * (6.2.2) "The {model group} of the model group definition
20579 * which corresponds to it per XML Representation of Model
Rob Richardsc6947bb2008-06-29 15:04:41 +000020580 * Group Definition Schema Components (�3.7.2) must be a
20581 * �valid restriction� of the {model group} of that model
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020582 * group definition in I, as defined in Particle Valid
Rob Richardsc6947bb2008-06-29 15:04:41 +000020583 * (Restriction) (�3.9.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020584 */
20585 break;
20586 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20587 /*
20588 * SPEC src-redefine:
20589 * (7.2.2) "The {attribute uses} and {attribute wildcard} of
20590 * the attribute group definition which corresponds to it
20591 * per XML Representation of Attribute Group Definition Schema
Rob Richardsc6947bb2008-06-29 15:04:41 +000020592 * Components (�3.6.2) must be �valid restrictions� of the
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020593 * {attribute uses} and {attribute wildcard} of that attribute
20594 * group definition in I, as defined in clause 2, clause 3 and
20595 * clause 4 of Derivation Valid (Restriction, Complex)
Rob Richardsc6947bb2008-06-29 15:04:41 +000020596 * (�3.4.6) (where references to the base type definition are
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020597 * understood as references to the attribute group definition
20598 * in I)."
20599 */
20600 err = xmlSchemaCheckDerivationOKRestriction2to4(pctxt,
20601 XML_SCHEMA_ACTION_REDEFINE,
20602 item, redef->target,
20603 (WXS_ATTR_GROUP_CAST item)->attrUses,
20604 (WXS_ATTR_GROUP_CAST redef->target)->attrUses,
20605 (WXS_ATTR_GROUP_CAST item)->attributeWildcard,
20606 (WXS_ATTR_GROUP_CAST redef->target)->attributeWildcard);
20607 if (err == -1)
20608 return(-1);
20609 break;
20610 default:
20611 break;
20612 }
20613 redef = redef->next;
20614 } while (redef != NULL);
20615 return(0);
20616}
Daniel Veillarddee23482008-04-11 12:58:43 +000020617
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020618
20619static int
20620xmlSchemaAddComponents(xmlSchemaParserCtxtPtr pctxt,
20621 xmlSchemaBucketPtr bucket)
20622{
20623 xmlSchemaBasicItemPtr item;
20624 int err;
20625 xmlHashTablePtr *table;
20626 const xmlChar *name;
20627 int i;
20628
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020629#define WXS_GET_GLOBAL_HASH(c, slot) { \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020630 if (WXS_IS_BUCKET_IMPMAIN((c)->type)) \
20631 table = &(WXS_IMPBUCKET((c))->schema->slot); \
20632 else \
20633 table = &(WXS_INCBUCKET((c))->ownerImport->schema->slot); }
20634
20635 /*
20636 * Add global components to the schema's hash tables.
20637 * This is the place where duplicate components will be
20638 * detected.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020639 * TODO: I think normally we should support imports of the
20640 * same namespace from multiple locations. We don't do currently,
20641 * but if we do then according to:
20642 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2224
20643 * we would need, if imported directly, to import redefined
20644 * components as well to be able to catch clashing components.
20645 * (I hope I'll still know what this means after some months :-()
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020646 */
20647 if (bucket == NULL)
20648 return(-1);
20649 if (bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED)
20650 return(0);
Daniel Veillarddee23482008-04-11 12:58:43 +000020651 bucket->flags |= XML_SCHEMA_BUCKET_COMPS_ADDED;
20652
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020653 for (i = 0; i < bucket->globals->nbItems; i++) {
20654 item = bucket->globals->items[i];
20655 table = NULL;
20656 switch (item->type) {
20657 case XML_SCHEMA_TYPE_COMPLEX:
20658 case XML_SCHEMA_TYPE_SIMPLE:
20659 if (WXS_REDEFINED_TYPE(item))
20660 continue;
20661 name = (WXS_TYPE_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020662 WXS_GET_GLOBAL_HASH(bucket, typeDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020663 break;
20664 case XML_SCHEMA_TYPE_ELEMENT:
20665 name = (WXS_ELEM_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020666 WXS_GET_GLOBAL_HASH(bucket, elemDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020667 break;
20668 case XML_SCHEMA_TYPE_ATTRIBUTE:
20669 name = (WXS_ATTR_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020670 WXS_GET_GLOBAL_HASH(bucket, attrDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020671 break;
20672 case XML_SCHEMA_TYPE_GROUP:
20673 if (WXS_REDEFINED_MODEL_GROUP_DEF(item))
20674 continue;
20675 name = (WXS_MODEL_GROUPDEF_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020676 WXS_GET_GLOBAL_HASH(bucket, groupDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020677 break;
20678 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20679 if (WXS_REDEFINED_ATTR_GROUP(item))
20680 continue;
20681 name = (WXS_ATTR_GROUP_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020682 WXS_GET_GLOBAL_HASH(bucket, attrgrpDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020683 break;
20684 case XML_SCHEMA_TYPE_IDC_KEY:
20685 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20686 case XML_SCHEMA_TYPE_IDC_KEYREF:
20687 name = (WXS_IDC_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020688 WXS_GET_GLOBAL_HASH(bucket, idcDef)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020689 break;
20690 case XML_SCHEMA_TYPE_NOTATION:
20691 name = ((xmlSchemaNotationPtr) item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020692 WXS_GET_GLOBAL_HASH(bucket, notaDecl)
Daniel Veillarddee23482008-04-11 12:58:43 +000020693 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020694 default:
20695 PERROR_INT("xmlSchemaAddComponents",
20696 "Unexpected global component type");
Daniel Veillarddee23482008-04-11 12:58:43 +000020697 continue;
20698 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020699 if (*table == NULL) {
20700 *table = xmlHashCreateDict(10, pctxt->dict);
20701 if (*table == NULL) {
20702 PERROR_INT("xmlSchemaAddComponents",
20703 "failed to create a component hash table");
20704 return(-1);
20705 }
Daniel Veillarddee23482008-04-11 12:58:43 +000020706 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020707 err = xmlHashAddEntry(*table, name, item);
Daniel Veillarddee23482008-04-11 12:58:43 +000020708 if (err != 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020709 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000020710
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020711 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20712 XML_SCHEMAP_REDEFINED_TYPE,
20713 WXS_ITEM_NODE(item),
20714 WXS_BASIC_CAST item,
20715 "A global %s '%s' does already exist",
20716 WXS_ITEM_TYPE_NAME(item),
20717 xmlSchemaGetComponentQName(&str, item));
Daniel Veillarddee23482008-04-11 12:58:43 +000020718 FREE_AND_NULL(str);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020719 }
20720 }
20721 /*
20722 * Process imported/included schemas.
20723 */
20724 if (bucket->relations != NULL) {
20725 xmlSchemaSchemaRelationPtr rel = bucket->relations;
20726 do {
20727 if ((rel->bucket != NULL) &&
20728 ((rel->bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED) == 0)) {
20729 if (xmlSchemaAddComponents(pctxt, rel->bucket) == -1)
20730 return(-1);
20731 }
20732 rel = rel->next;
20733 } while (rel != NULL);
20734 }
20735 return(0);
20736}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020737
20738static int
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020739xmlSchemaFixupComponents(xmlSchemaParserCtxtPtr pctxt,
20740 xmlSchemaBucketPtr rootBucket)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020741{
20742 xmlSchemaConstructionCtxtPtr con = pctxt->constructor;
20743 xmlSchemaTreeItemPtr item, *items;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020744 int nbItems, i, ret = 0;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020745 xmlSchemaBucketPtr oldbucket = con->bucket;
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000020746 xmlSchemaElementPtr elemDecl;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020747
20748#define FIXHFAILURE if (pctxt->err == XML_SCHEMAP_INTERNAL) goto exit_failure;
20749
20750 if ((con->pending == NULL) ||
20751 (con->pending->nbItems == 0))
Daniel Veillarddee23482008-04-11 12:58:43 +000020752 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020753
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020754 /*
20755 * Since xmlSchemaFixupComplexType() will create new particles
20756 * (local components), and those particle components need a bucket
20757 * on the constructor, we'll assure here that the constructor has
20758 * a bucket.
Daniel Veillarddee23482008-04-11 12:58:43 +000020759 * TODO: Think about storing locals _only_ on the main bucket.
20760 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020761 if (con->bucket == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000020762 con->bucket = rootBucket;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020763
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020764 /* TODO:
20765 * SPEC (src-redefine):
20766 * (6.2) "If it has no such self-reference, then all of the
20767 * following must be true:"
Daniel Veillarddee23482008-04-11 12:58:43 +000020768
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020769 * (6.2.2) The {model group} of the model group definition which
20770 * corresponds to it per XML Representation of Model Group
Rob Richardsc6947bb2008-06-29 15:04:41 +000020771 * Definition Schema Components (�3.7.2) must be a �valid
20772 * restriction� of the {model group} of that model group definition
20773 * in I, as defined in Particle Valid (Restriction) (�3.9.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020774 */
20775 xmlSchemaCheckSRCRedefineFirst(pctxt);
20776
20777 /*
20778 * Add global components to the schemata's hash tables.
Daniel Veillarddee23482008-04-11 12:58:43 +000020779 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020780 xmlSchemaAddComponents(pctxt, rootBucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020781
20782 pctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020783 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20784 nbItems = con->pending->nbItems;
20785 /*
20786 * Now that we have parsed *all* the schema document(s) and converted
20787 * them to schema components, we can resolve references, apply component
20788 * constraints, create the FSA from the content model, etc.
Daniel Veillarddee23482008-04-11 12:58:43 +000020789 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020790 /*
20791 * Resolve references of..
20792 *
20793 * 1. element declarations:
20794 * - the type definition
20795 * - the substitution group affiliation
20796 * 2. simple/complex types:
20797 * - the base type definition
20798 * - the memberTypes of union types
20799 * - the itemType of list types
20800 * 3. attributes declarations and attribute uses:
20801 * - the type definition
20802 * - if an attribute use, then the attribute declaration
20803 * 4. attribute group references:
20804 * - the attribute group definition
20805 * 5. particles:
20806 * - the term of the particle (e.g. a model group)
20807 * 6. IDC key-references:
20808 * - the referenced IDC 'key' or 'unique' definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020809 * 7. Attribute prohibitions which had a "ref" attribute.
Daniel Veillarddee23482008-04-11 12:58:43 +000020810 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020811 for (i = 0; i < nbItems; i++) {
20812 item = items[i];
20813 switch (item->type) {
20814 case XML_SCHEMA_TYPE_ELEMENT:
20815 xmlSchemaResolveElementReferences(
20816 (xmlSchemaElementPtr) item, pctxt);
20817 FIXHFAILURE;
20818 break;
20819 case XML_SCHEMA_TYPE_COMPLEX:
20820 case XML_SCHEMA_TYPE_SIMPLE:
20821 xmlSchemaResolveTypeReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020822 (xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020823 FIXHFAILURE;
20824 break;
20825 case XML_SCHEMA_TYPE_ATTRIBUTE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020826 xmlSchemaResolveAttrTypeReferences(
20827 (xmlSchemaAttributePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020828 FIXHFAILURE;
20829 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020830 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20831 xmlSchemaResolveAttrUseReferences(
20832 (xmlSchemaAttributeUsePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020833 FIXHFAILURE;
20834 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020835 case XML_SCHEMA_EXTRA_QNAMEREF:
Daniel Veillarddee23482008-04-11 12:58:43 +000020836 if ((WXS_QNAME_CAST item)->itemType ==
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020837 XML_SCHEMA_TYPE_ATTRIBUTEGROUP)
20838 {
20839 xmlSchemaResolveAttrGroupReferences(
20840 WXS_QNAME_CAST item, pctxt);
20841 }
20842 FIXHFAILURE;
20843 break;
20844 case XML_SCHEMA_TYPE_SEQUENCE:
20845 case XML_SCHEMA_TYPE_CHOICE:
20846 case XML_SCHEMA_TYPE_ALL:
20847 xmlSchemaResolveModelGroupParticleReferences(pctxt,
20848 WXS_MODEL_GROUP_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020849 FIXHFAILURE;
20850 break;
20851 case XML_SCHEMA_TYPE_IDC_KEY:
20852 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20853 case XML_SCHEMA_TYPE_IDC_KEYREF:
20854 xmlSchemaResolveIDCKeyReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020855 (xmlSchemaIDCPtr) item, pctxt);
20856 FIXHFAILURE;
20857 break;
20858 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
20859 /*
20860 * Handle attribue prohibition which had a
20861 * "ref" attribute.
20862 */
20863 xmlSchemaResolveAttrUseProhibReferences(
20864 WXS_ATTR_PROHIB_CAST item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020865 FIXHFAILURE;
20866 break;
20867 default:
20868 break;
20869 }
20870 }
20871 if (pctxt->nberrors != 0)
20872 goto exit_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000020873
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020874 /*
20875 * Now that all references are resolved we
20876 * can check for circularity of...
Daniel Veillarddee23482008-04-11 12:58:43 +000020877 * 1. the base axis of type definitions
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020878 * 2. nested model group definitions
20879 * 3. nested attribute group definitions
20880 * TODO: check for circual substitution groups.
20881 */
20882 for (i = 0; i < nbItems; i++) {
20883 item = items[i];
20884 /*
20885 * Let's better stop on the first error here.
20886 */
20887 switch (item->type) {
20888 case XML_SCHEMA_TYPE_COMPLEX:
20889 case XML_SCHEMA_TYPE_SIMPLE:
20890 xmlSchemaCheckTypeDefCircular(
20891 (xmlSchemaTypePtr) item, pctxt);
20892 FIXHFAILURE;
20893 if (pctxt->nberrors != 0)
20894 goto exit_error;
20895 break;
20896 case XML_SCHEMA_TYPE_GROUP:
20897 xmlSchemaCheckGroupDefCircular(
20898 (xmlSchemaModelGroupDefPtr) item, pctxt);
20899 FIXHFAILURE;
20900 if (pctxt->nberrors != 0)
20901 goto exit_error;
20902 break;
20903 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20904 xmlSchemaCheckAttrGroupCircular(
20905 (xmlSchemaAttributeGroupPtr) item, pctxt);
20906 FIXHFAILURE;
20907 if (pctxt->nberrors != 0)
20908 goto exit_error;
20909 break;
20910 default:
20911 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000020912 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020913 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020914 if (pctxt->nberrors != 0)
20915 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020916 /*
20917 * Model group definition references:
20918 * Such a reference is reflected by a particle at the component
20919 * level. Until now the 'term' of such particles pointed
20920 * to the model group definition; this was done, in order to
20921 * ease circularity checks. Now we need to set the 'term' of
20922 * such particles to the model group of the model group definition.
20923 */
20924 for (i = 0; i < nbItems; i++) {
20925 item = items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020926 switch (item->type) {
20927 case XML_SCHEMA_TYPE_SEQUENCE:
Daniel Veillarddee23482008-04-11 12:58:43 +000020928 case XML_SCHEMA_TYPE_CHOICE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020929 xmlSchemaModelGroupToModelGroupDefFixup(pctxt,
20930 WXS_MODEL_GROUP_CAST item);
20931 break;
20932 default:
20933 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020934 }
20935 }
20936 if (pctxt->nberrors != 0)
20937 goto exit_error;
20938 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020939 * Expand attribute group references of attribute group definitions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020940 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020941 for (i = 0; i < nbItems; i++) {
20942 item = items[i];
20943 switch (item->type) {
20944 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20945 if ((! WXS_ATTR_GROUP_EXPANDED(item)) &&
20946 WXS_ATTR_GROUP_HAS_REFS(item))
20947 {
20948 xmlSchemaAttributeGroupExpandRefs(pctxt,
20949 WXS_ATTR_GROUP_CAST item);
20950 FIXHFAILURE;
20951 }
20952 break;
20953 default:
20954 break;
20955 }
20956 }
20957 if (pctxt->nberrors != 0)
20958 goto exit_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000020959 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020960 * First compute the variety of simple types. This is needed as
20961 * a seperate step, since otherwise we won't be able to detect
20962 * circular union types in all cases.
20963 */
20964 for (i = 0; i < nbItems; i++) {
20965 item = items[i];
20966 switch (item->type) {
20967 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020968 if (WXS_IS_TYPE_NOT_FIXED_1((xmlSchemaTypePtr) item)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020969 xmlSchemaFixupSimpleTypeStageOne(pctxt,
20970 (xmlSchemaTypePtr) item);
20971 FIXHFAILURE;
20972 }
20973 break;
20974 default:
20975 break;
20976 }
20977 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020978 if (pctxt->nberrors != 0)
20979 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020980 /*
20981 * Detect circular union types. Note that this needs the variety to
20982 * be already computed.
20983 */
20984 for (i = 0; i < nbItems; i++) {
20985 item = items[i];
20986 switch (item->type) {
20987 case XML_SCHEMA_TYPE_SIMPLE:
20988 if (((xmlSchemaTypePtr) item)->memberTypes != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000020989 xmlSchemaCheckUnionTypeDefCircular(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020990 (xmlSchemaTypePtr) item);
20991 FIXHFAILURE;
20992 }
20993 break;
20994 default:
20995 break;
20996 }
20997 }
20998 if (pctxt->nberrors != 0)
20999 goto exit_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000021000
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021001 /*
21002 * Do the complete type fixup for simple types.
21003 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021004 for (i = 0; i < nbItems; i++) {
21005 item = items[i];
21006 switch (item->type) {
21007 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021008 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
21009 xmlSchemaFixupSimpleTypeStageTwo(pctxt, WXS_TYPE_CAST item);
21010 FIXHFAILURE;
21011 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021012 break;
21013 default:
21014 break;
21015 }
21016 }
21017 if (pctxt->nberrors != 0)
21018 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021019 /*
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021020 * At this point we need build and check all simple types.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021021 */
21022 /*
21023 * Apply contraints for attribute declarations.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021024 */
21025 for (i = 0; i < nbItems; i++) {
21026 item = items[i];
21027 switch (item->type) {
Daniel Veillarddee23482008-04-11 12:58:43 +000021028 case XML_SCHEMA_TYPE_ATTRIBUTE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021029 xmlSchemaCheckAttrPropsCorrect(pctxt, WXS_ATTR_CAST item);
21030 FIXHFAILURE;
Daniel Veillarddee23482008-04-11 12:58:43 +000021031 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021032 default:
21033 break;
21034 }
21035 }
21036 if (pctxt->nberrors != 0)
21037 goto exit_error;
21038 /*
21039 * Apply constraints for attribute uses.
Daniel Veillarddee23482008-04-11 12:58:43 +000021040 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021041 for (i = 0; i < nbItems; i++) {
21042 item = items[i];
21043 switch (item->type) {
21044 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
21045 if (((xmlSchemaAttributeUsePtr)item)->defValue != NULL) {
21046 xmlSchemaCheckAttrUsePropsCorrect(pctxt,
21047 WXS_ATTR_USE_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021048 FIXHFAILURE;
21049 }
21050 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021051 default:
21052 break;
21053 }
21054 }
21055 if (pctxt->nberrors != 0)
21056 goto exit_error;
21057
21058 /*
21059 * Apply constraints for attribute group definitions.
21060 */
21061 for (i = 0; i < nbItems; i++) {
21062 item = items[i];
21063 switch (item->type) {
21064 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
21065 if (( (WXS_ATTR_GROUP_CAST item)->attrUses != NULL) &&
21066 ( (WXS_LIST_CAST (WXS_ATTR_GROUP_CAST item)->attrUses)->nbItems > 1))
21067 {
21068 xmlSchemaCheckAGPropsCorrect(pctxt, WXS_ATTR_GROUP_CAST item);
21069 FIXHFAILURE;
21070 }
21071 break;
21072 default:
21073 break;
21074 }
21075 }
21076 if (pctxt->nberrors != 0)
21077 goto exit_error;
21078
21079 /*
21080 * Apply constraints for redefinitions.
21081 */
21082 if (WXS_CONSTRUCTOR(pctxt)->redefs != NULL)
21083 xmlSchemaCheckSRCRedefineSecond(pctxt);
21084 if (pctxt->nberrors != 0)
21085 goto exit_error;
21086
Daniel Veillarddee23482008-04-11 12:58:43 +000021087 /*
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021088 * Complex types are builded and checked.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021089 */
21090 for (i = 0; i < nbItems; i++) {
21091 item = con->pending->items[i];
21092 switch (item->type) {
21093 case XML_SCHEMA_TYPE_COMPLEX:
21094 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
21095 xmlSchemaFixupComplexType(pctxt, WXS_TYPE_CAST item);
21096 FIXHFAILURE;
21097 }
21098 break;
21099 default:
21100 break;
21101 }
21102 }
21103 if (pctxt->nberrors != 0)
21104 goto exit_error;
21105
21106 /*
21107 * The list could have changed, since xmlSchemaFixupComplexType()
21108 * will create particles and model groups in some cases.
21109 */
21110 items = (xmlSchemaTreeItemPtr *) con->pending->items;
Daniel Veillarddee23482008-04-11 12:58:43 +000021111 nbItems = con->pending->nbItems;
21112
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021113 /*
21114 * Apply some constraints for element declarations.
21115 */
21116 for (i = 0; i < nbItems; i++) {
21117 item = items[i];
21118 switch (item->type) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021119 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021120 elemDecl = (xmlSchemaElementPtr) item;
Daniel Veillarddee23482008-04-11 12:58:43 +000021121
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021122 if ((elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_CHECKED) == 0)
21123 {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021124 xmlSchemaCheckElementDeclComponent(
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021125 (xmlSchemaElementPtr) elemDecl, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021126 FIXHFAILURE;
21127 }
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021128
21129#ifdef WXS_ELEM_DECL_CONS_ENABLED
21130 /*
21131 * Schema Component Constraint: Element Declarations Consistent
21132 * Apply this constraint to local types of element declarations.
21133 */
21134 if ((WXS_ELEM_TYPEDEF(elemDecl) != NULL) &&
21135 (WXS_IS_COMPLEX(WXS_ELEM_TYPEDEF(elemDecl))) &&
21136 (WXS_TYPE_IS_LOCAL(WXS_ELEM_TYPEDEF(elemDecl))))
21137 {
21138 xmlSchemaCheckElementDeclConsistent(pctxt,
21139 WXS_BASIC_CAST elemDecl,
21140 WXS_TYPE_PARTICLE(WXS_ELEM_TYPEDEF(elemDecl)),
21141 NULL, NULL, 0);
21142 }
21143#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021144 break;
21145 default:
21146 break;
21147 }
21148 }
21149 if (pctxt->nberrors != 0)
21150 goto exit_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000021151
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021152 /*
21153 * Finally we can build the automaton from the content model of
21154 * complex types.
21155 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021156
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021157 for (i = 0; i < nbItems; i++) {
21158 item = items[i];
21159 switch (item->type) {
21160 case XML_SCHEMA_TYPE_COMPLEX:
21161 xmlSchemaBuildContentModel((xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021162 /* FIXHFAILURE; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021163 break;
21164 default:
21165 break;
21166 }
21167 }
21168 if (pctxt->nberrors != 0)
21169 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021170 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021171 * URGENT TODO: cos-element-consistent
Daniel Veillarddee23482008-04-11 12:58:43 +000021172 */
21173 goto exit;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021174
Daniel Veillarddee23482008-04-11 12:58:43 +000021175exit_error:
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021176 ret = pctxt->err;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000021177 goto exit;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021178
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021179exit_failure:
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021180 ret = -1;
21181
21182exit:
21183 /*
21184 * Reset the constructor. This is needed for XSI acquisition, since
21185 * those items will be processed over and over again for every XSI
21186 * if not cleared here.
21187 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000021188 con->bucket = oldbucket;
Daniel Veillarddee23482008-04-11 12:58:43 +000021189 con->pending->nbItems = 0;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021190 if (con->substGroups != NULL) {
21191 xmlHashFree(con->substGroups,
21192 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
21193 con->substGroups = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000021194 }
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021195 if (con->redefs != NULL) {
21196 xmlSchemaRedefListFree(con->redefs);
21197 con->redefs = NULL;
21198 }
21199 return(ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021200}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021201/**
Daniel Veillard4255d502002-04-16 15:50:10 +000021202 * xmlSchemaParse:
21203 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021204 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021205 * parse a schema definition resource and build an internal
Daniel Veillard4255d502002-04-16 15:50:10 +000021206 * XML Shema struture which can be used to validate instances.
Daniel Veillard4255d502002-04-16 15:50:10 +000021207 *
21208 * Returns the internal XML Schema structure built from the resource or
21209 * NULL in case of error
21210 */
21211xmlSchemaPtr
21212xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt)
21213{
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021214 xmlSchemaPtr mainSchema = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021215 xmlSchemaBucketPtr bucket = NULL;
21216 int res;
Daniel Veillard4255d502002-04-16 15:50:10 +000021217
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021218 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021219 * This one is used if the schema to be parsed was specified via
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021220 * the API; i.e. not automatically by the validated instance document.
21221 */
21222
Daniel Veillard4255d502002-04-16 15:50:10 +000021223 xmlSchemaInitTypes();
21224
Daniel Veillard6045c902002-10-09 21:13:59 +000021225 if (ctxt == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +000021226 return (NULL);
21227
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021228 /* TODO: Init the context. Is this all we need?*/
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021229 ctxt->nberrors = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021230 ctxt->err = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000021231 ctxt->counter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000021232
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021233 /* Create the *main* schema. */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021234 mainSchema = xmlSchemaNewSchema(ctxt);
21235 if (mainSchema == NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021236 goto exit_failure;
Daniel Veillard4255d502002-04-16 15:50:10 +000021237 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021238 * Create the schema constructor.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021239 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021240 if (ctxt->constructor == NULL) {
21241 ctxt->constructor = xmlSchemaConstructionCtxtCreate(ctxt->dict);
21242 if (ctxt->constructor == NULL)
21243 return(NULL);
21244 /* Take ownership of the constructor to be able to free it. */
21245 ctxt->ownsConstructor = 1;
21246 }
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021247 ctxt->constructor->mainSchema = mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021248 /*
21249 * Locate and add the schema document.
21250 */
21251 res = xmlSchemaAddSchemaDoc(ctxt, XML_SCHEMA_SCHEMA_MAIN,
21252 ctxt->URL, ctxt->doc, ctxt->buffer, ctxt->size, NULL,
21253 NULL, NULL, &bucket);
21254 if (res == -1)
21255 goto exit_failure;
21256 if (res != 0)
Daniel Veillarddee23482008-04-11 12:58:43 +000021257 goto exit;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021258
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021259 if (bucket == NULL) {
21260 /* TODO: Error code, actually we failed to *locate* the schema. */
Daniel Veillarddee23482008-04-11 12:58:43 +000021261 if (ctxt->URL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021262 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21263 NULL, NULL,
21264 "Failed to locate the main schema resource at '%s'",
21265 ctxt->URL, NULL);
21266 else
21267 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21268 NULL, NULL,
21269 "Failed to locate the main schema resource",
21270 NULL, NULL);
21271 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000021272 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021273 /* Then do the parsing for good. */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021274 if (xmlSchemaParseNewDocWithContext(ctxt, mainSchema, bucket) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021275 goto exit_failure;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021276 if (ctxt->nberrors != 0)
21277 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000021278
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021279 mainSchema->doc = bucket->doc;
21280 mainSchema->preserve = ctxt->preserve;
Daniel Veillarddee23482008-04-11 12:58:43 +000021281
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021282 ctxt->schema = mainSchema;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021283
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021284 if (xmlSchemaFixupComponents(ctxt, WXS_CONSTRUCTOR(ctxt)->mainBucket) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021285 goto exit_failure;
21286
21287 /*
21288 * TODO: This is not nice, since we cannot distinguish from the
21289 * result if there was an internal error or not.
21290 */
Daniel Veillarddee23482008-04-11 12:58:43 +000021291exit:
21292 if (ctxt->nberrors != 0) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021293 if (mainSchema) {
21294 xmlSchemaFree(mainSchema);
21295 mainSchema = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021296 }
21297 if (ctxt->constructor) {
21298 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21299 ctxt->constructor = NULL;
21300 ctxt->ownsConstructor = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021301 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021302 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021303 ctxt->schema = NULL;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021304 return(mainSchema);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021305exit_failure:
Daniel Veillarddee23482008-04-11 12:58:43 +000021306 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021307 * Quite verbose, but should catch internal errors, which were
21308 * not communitated.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021309 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021310 if (mainSchema) {
21311 xmlSchemaFree(mainSchema);
21312 mainSchema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021313 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021314 if (ctxt->constructor) {
21315 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21316 ctxt->constructor = NULL;
21317 ctxt->ownsConstructor = 0;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021318 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021319 PERROR_INT2("xmlSchemaParse",
Daniel Veillarddee23482008-04-11 12:58:43 +000021320 "An internal error occured");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021321 ctxt->schema = NULL;
21322 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000021323}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021324
Daniel Veillard4255d502002-04-16 15:50:10 +000021325/**
Daniel Veillard01c13b52002-12-10 15:19:08 +000021326 * xmlSchemaSetParserErrors:
Daniel Veillard4255d502002-04-16 15:50:10 +000021327 * @ctxt: a schema validation context
Daniel Veillard01c13b52002-12-10 15:19:08 +000021328 * @err: the error callback
21329 * @warn: the warning callback
21330 * @ctx: contextual data for the callbacks
Daniel Veillard4255d502002-04-16 15:50:10 +000021331 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021332 * Set the callback functions used to handle errors for a validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021333 */
21334void
21335xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021336 xmlSchemaValidityErrorFunc err,
21337 xmlSchemaValidityWarningFunc warn, void *ctx)
21338{
Daniel Veillard4255d502002-04-16 15:50:10 +000021339 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021340 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000021341 ctxt->error = err;
21342 ctxt->warning = warn;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021343 ctxt->errCtxt = ctx;
21344 if (ctxt->vctxt != NULL)
21345 xmlSchemaSetValidErrors(ctxt->vctxt, err, warn, ctx);
21346}
21347
21348/**
21349 * xmlSchemaSetParserStructuredErrors:
21350 * @ctxt: a schema parser context
21351 * @serror: the structured error function
21352 * @ctx: the functions context
21353 *
21354 * Set the structured error callback
21355 */
21356void
21357xmlSchemaSetParserStructuredErrors(xmlSchemaParserCtxtPtr ctxt,
21358 xmlStructuredErrorFunc serror,
21359 void *ctx)
21360{
21361 if (ctxt == NULL)
21362 return;
21363 ctxt->serror = serror;
21364 ctxt->errCtxt = ctx;
21365 if (ctxt->vctxt != NULL)
21366 xmlSchemaSetValidStructuredErrors(ctxt->vctxt, serror, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000021367}
21368
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021369/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000021370 * xmlSchemaGetParserErrors:
21371 * @ctxt: a XMl-Schema parser context
21372 * @err: the error callback result
21373 * @warn: the warning callback result
21374 * @ctx: contextual data for the callbacks result
21375 *
21376 * Get the callback information used to handle errors for a parser context
21377 *
21378 * Returns -1 in case of failure, 0 otherwise
21379 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021380int
Daniel Veillard259f0df2004-08-18 09:13:18 +000021381xmlSchemaGetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021382 xmlSchemaValidityErrorFunc * err,
21383 xmlSchemaValidityWarningFunc * warn, void **ctx)
Daniel Veillard259f0df2004-08-18 09:13:18 +000021384{
21385 if (ctxt == NULL)
21386 return(-1);
21387 if (err != NULL)
21388 *err = ctxt->error;
21389 if (warn != NULL)
21390 *warn = ctxt->warning;
21391 if (ctx != NULL)
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021392 *ctx = ctxt->errCtxt;
Daniel Veillard259f0df2004-08-18 09:13:18 +000021393 return(0);
21394}
21395
21396/**
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021397 * xmlSchemaFacetTypeToString:
21398 * @type: the facet type
21399 *
21400 * Convert the xmlSchemaTypeType to a char string.
21401 *
21402 * Returns the char string representation of the facet type if the
21403 * type is a facet and an "Internal Error" string otherwise.
21404 */
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021405static const xmlChar *
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021406xmlSchemaFacetTypeToString(xmlSchemaTypeType type)
21407{
21408 switch (type) {
21409 case XML_SCHEMA_FACET_PATTERN:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021410 return (BAD_CAST "pattern");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021411 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021412 return (BAD_CAST "maxExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021413 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021414 return (BAD_CAST "maxInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021415 case XML_SCHEMA_FACET_MINEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021416 return (BAD_CAST "minExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021417 case XML_SCHEMA_FACET_MININCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021418 return (BAD_CAST "minInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021419 case XML_SCHEMA_FACET_WHITESPACE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021420 return (BAD_CAST "whiteSpace");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021421 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021422 return (BAD_CAST "enumeration");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021423 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021424 return (BAD_CAST "length");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021425 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021426 return (BAD_CAST "maxLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021427 case XML_SCHEMA_FACET_MINLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021428 return (BAD_CAST "minLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021429 case XML_SCHEMA_FACET_TOTALDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021430 return (BAD_CAST "totalDigits");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021431 case XML_SCHEMA_FACET_FRACTIONDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021432 return (BAD_CAST "fractionDigits");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021433 default:
21434 break;
21435 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021436 return (BAD_CAST "Internal Error");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021437}
21438
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021439static xmlSchemaWhitespaceValueType
Daniel Veillardc0826a72004-08-10 14:17:33 +000021440xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type)
21441{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021442 /*
21443 * The normalization type can be changed only for types which are derived
Daniel Veillardc0826a72004-08-10 14:17:33 +000021444 * from xsd:string.
21445 */
21446 if (type->type == XML_SCHEMA_TYPE_BASIC) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021447 /*
21448 * Note that we assume a whitespace of preserve for anySimpleType.
21449 */
21450 if ((type->builtInType == XML_SCHEMAS_STRING) ||
21451 (type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
21452 return(XML_SCHEMA_WHITESPACE_PRESERVE);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +000021453 else if (type->builtInType == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021454 return(XML_SCHEMA_WHITESPACE_REPLACE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021455 else {
21456 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000021457 * For all �atomic� datatypes other than string (and types �derived�
21458 * by �restriction� from it) the value of whiteSpace is fixed to
Daniel Veillardc0826a72004-08-10 14:17:33 +000021459 * collapse
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021460 * Note that this includes built-in list datatypes.
Daniel Veillardc0826a72004-08-10 14:17:33 +000021461 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021462 return(XML_SCHEMA_WHITESPACE_COLLAPSE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021463 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021464 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021465 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021466 * For list types the facet "whiteSpace" is fixed to "collapse".
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021467 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021468 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021469 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021470 return (XML_SCHEMA_WHITESPACE_UNKNOWN);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021471 } else if (WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021472 if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE)
21473 return (XML_SCHEMA_WHITESPACE_PRESERVE);
21474 else if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_REPLACE)
21475 return (XML_SCHEMA_WHITESPACE_REPLACE);
21476 else
21477 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021478 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021479 return (-1);
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021480}
21481
Daniel Veillard4255d502002-04-16 15:50:10 +000021482/************************************************************************
21483 * *
21484 * Simple type validation *
21485 * *
21486 ************************************************************************/
Daniel Veillard377e1a92004-04-16 16:30:05 +000021487
Daniel Veillard4255d502002-04-16 15:50:10 +000021488
21489/************************************************************************
21490 * *
21491 * DOM Validation code *
21492 * *
21493 ************************************************************************/
21494
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021495/**
21496 * xmlSchemaAssembleByLocation:
21497 * @pctxt: a schema parser context
21498 * @vctxt: a schema validation context
21499 * @schema: the existing schema
21500 * @node: the node that fired the assembling
21501 * @nsName: the namespace name of the new schema
21502 * @location: the location of the schema
21503 *
21504 * Expands an existing schema by an additional schema.
21505 *
21506 * Returns 0 if the new schema is correct, a positive error code
21507 * number otherwise and -1 in case of an internal or API error.
21508 */
21509static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021510xmlSchemaAssembleByLocation(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021511 xmlSchemaPtr schema,
21512 xmlNodePtr node,
21513 const xmlChar *nsName,
21514 const xmlChar *location)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021515{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021516 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021517 xmlSchemaParserCtxtPtr pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021518 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021519
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021520 if ((vctxt == NULL) || (schema == NULL))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021521 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021522
21523 if (vctxt->pctxt == NULL) {
21524 VERROR_INT("xmlSchemaAssembleByLocation",
21525 "no parser context available");
21526 return(-1);
21527 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021528 pctxt = vctxt->pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021529 if (pctxt->constructor == NULL) {
21530 PERROR_INT("xmlSchemaAssembleByLocation",
21531 "no constructor");
21532 return(-1);
21533 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021534 /*
21535 * Acquire the schema document.
21536 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021537 location = xmlSchemaBuildAbsoluteURI(pctxt->dict,
21538 location, node);
21539 /*
21540 * Note that we pass XML_SCHEMA_SCHEMA_IMPORT here;
21541 * the process will automatically change this to
21542 * XML_SCHEMA_SCHEMA_MAIN if it is the first schema document.
21543 */
21544 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
Daniel Veillarddee23482008-04-11 12:58:43 +000021545 location, NULL, NULL, 0, node, NULL, nsName,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021546 &bucket);
21547 if (ret != 0)
Daniel Veillarddee23482008-04-11 12:58:43 +000021548 return(ret);
21549 if (bucket == NULL) {
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021550 /*
21551 * Generate a warning that the document could not be located.
21552 */
21553 xmlSchemaCustomWarning(ACTXT_CAST vctxt, XML_SCHEMAV_MISC,
21554 node, NULL,
21555 "The document at location '%s' could not be acquired",
21556 location, NULL, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021557 return(ret);
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021558 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021559 /*
21560 * The first located schema will be handled as if all other
21561 * schemas imported by XSI were imported by this first schema.
21562 */
21563 if ((bucket != NULL) &&
21564 (WXS_CONSTRUCTOR(pctxt)->bucket == NULL))
21565 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
21566 /*
21567 * TODO: Is this handled like an import? I.e. is it not an error
21568 * if the schema cannot be located?
21569 */
21570 if ((bucket == NULL) || (! CAN_PARSE_SCHEMA(bucket)))
21571 return(0);
21572 /*
21573 * We will reuse the parser context for every schema imported
21574 * directly via XSI. So reset the context.
21575 */
21576 pctxt->nberrors = 0;
21577 pctxt->err = 0;
21578 pctxt->doc = bucket->doc;
Daniel Veillarddee23482008-04-11 12:58:43 +000021579
21580 ret = xmlSchemaParseNewDocWithContext(pctxt, schema, bucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021581 if (ret == -1) {
21582 pctxt->doc = NULL;
21583 goto exit_failure;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021584 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021585 /* Paranoid error channelling. */
21586 if ((ret == 0) && (pctxt->nberrors != 0))
Daniel Veillarddee23482008-04-11 12:58:43 +000021587 ret = pctxt->err;
21588 if (pctxt->nberrors == 0) {
21589 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021590 * Only bother to fixup pending components, if there was
21591 * no error yet.
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021592 * For every XSI acquired schema (and its sub-schemata) we will
21593 * fixup the components.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021594 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021595 xmlSchemaFixupComponents(pctxt, bucket);
Kasimier T. Buchcik215406f2005-12-07 12:14:09 +000021596 ret = pctxt->err;
21597 /*
21598 * Not nice, but we need somehow to channel the schema parser
21599 * error to the validation context.
21600 */
21601 if ((ret != 0) && (vctxt->err == 0))
21602 vctxt->err = ret;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021603 vctxt->nberrors += pctxt->nberrors;
21604 } else {
Daniel Veillarddee23482008-04-11 12:58:43 +000021605 /* Add to validation error sum. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021606 vctxt->nberrors += pctxt->nberrors;
21607 }
21608 pctxt->doc = NULL;
21609 return(ret);
21610exit_failure:
21611 pctxt->doc = NULL;
21612 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021613}
21614
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021615static xmlSchemaAttrInfoPtr
Daniel Veillarddee23482008-04-11 12:58:43 +000021616xmlSchemaGetMetaAttrInfo(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021617 int metaType)
21618{
21619 if (vctxt->nbAttrInfos == 0)
21620 return (NULL);
21621 {
21622 int i;
21623 xmlSchemaAttrInfoPtr iattr;
21624
21625 for (i = 0; i < vctxt->nbAttrInfos; i++) {
21626 iattr = vctxt->attrInfos[i];
21627 if (iattr->metaType == metaType)
21628 return (iattr);
21629 }
21630
21631 }
21632 return (NULL);
21633}
21634
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021635/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021636 * xmlSchemaAssembleByXSI:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021637 * @vctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021638 *
21639 * Expands an existing schema by an additional schema using
21640 * the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute
21641 * of an instance. If xsi:noNamespaceSchemaLocation is used, @noNamespace
21642 * must be set to 1.
21643 *
21644 * Returns 0 if the new schema is correct, a positive error code
21645 * number otherwise and -1 in case of an internal or API error.
21646 */
21647static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021648xmlSchemaAssembleByXSI(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021649{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021650 const xmlChar *cur, *end;
21651 const xmlChar *nsname = NULL, *location;
21652 int count = 0;
21653 int ret = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021654 xmlSchemaAttrInfoPtr iattr;
21655
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021656 /*
21657 * Parse the value; we will assume an even number of values
21658 * to be given (this is how Xerces and XSV work).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021659 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021660 * URGENT TODO: !! This needs to work for both
21661 * @noNamespaceSchemaLocation AND @schemaLocation on the same
21662 * element !!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021663 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021664 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
21665 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC);
21666 if (iattr == NULL)
Kasimier T. Buchcik81755ea2005-11-23 16:39:27 +000021667 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021668 XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC);
21669 if (iattr == NULL)
21670 return (0);
21671 cur = iattr->value;
21672 do {
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021673 /*
21674 * TODO: Move the string parsing mechanism away from here.
21675 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021676 if (iattr->metaType == XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021677 /*
21678 * Get the namespace name.
21679 */
21680 while (IS_BLANK_CH(*cur))
21681 cur++;
21682 end = cur;
21683 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21684 end++;
21685 if (end == cur)
21686 break;
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021687 count++; /* TODO: Don't use the schema's dict. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021688 nsname = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021689 cur = end;
21690 }
21691 /*
21692 * Get the URI.
21693 */
21694 while (IS_BLANK_CH(*cur))
21695 cur++;
21696 end = cur;
21697 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21698 end++;
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021699 if (end == cur) {
21700 if (iattr->metaType ==
21701 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC)
21702 {
21703 /*
21704 * If using @schemaLocation then tuples are expected.
21705 * I.e. the namespace name *and* the document's URI.
Daniel Veillarddee23482008-04-11 12:58:43 +000021706 */
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021707 xmlSchemaCustomWarning(ACTXT_CAST vctxt, XML_SCHEMAV_MISC,
21708 iattr->node, NULL,
21709 "The value must consist of tuples: the target namespace "
21710 "name and the document's URI", NULL, NULL, NULL);
21711 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021712 break;
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021713 }
21714 count++; /* TODO: Don't use the schema's dict. */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021715 location = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021716 cur = end;
21717 ret = xmlSchemaAssembleByLocation(vctxt, vctxt->schema,
21718 iattr->node, nsname, location);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021719 if (ret == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021720 VERROR_INT("xmlSchemaAssembleByXSI",
21721 "assembling schemata");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021722 return (-1);
21723 }
21724 } while (*cur != 0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021725 return (ret);
21726}
21727
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021728static const xmlChar *
21729xmlSchemaLookupNamespace(xmlSchemaValidCtxtPtr vctxt,
21730 const xmlChar *prefix)
21731{
21732 if (vctxt->sax != NULL) {
21733 int i, j;
21734 xmlSchemaNodeInfoPtr inode;
Daniel Veillarddee23482008-04-11 12:58:43 +000021735
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021736 for (i = vctxt->depth; i >= 0; i--) {
21737 if (vctxt->elemInfos[i]->nbNsBindings != 0) {
21738 inode = vctxt->elemInfos[i];
21739 for (j = 0; j < inode->nbNsBindings * 2; j += 2) {
21740 if (((prefix == NULL) &&
21741 (inode->nsBindings[j] == NULL)) ||
21742 ((prefix != NULL) && xmlStrEqual(prefix,
21743 inode->nsBindings[j]))) {
21744
21745 /*
21746 * Note that the namespace bindings are already
21747 * in a string dict.
21748 */
Daniel Veillarddee23482008-04-11 12:58:43 +000021749 return (inode->nsBindings[j+1]);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021750 }
21751 }
21752 }
21753 }
21754 return (NULL);
Daniel Veillard438ebbd2008-05-12 12:58:46 +000021755#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021756 } else if (vctxt->reader != NULL) {
21757 xmlChar *nsName;
Daniel Veillarddee23482008-04-11 12:58:43 +000021758
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021759 nsName = xmlTextReaderLookupNamespace(vctxt->reader, prefix);
21760 if (nsName != NULL) {
21761 const xmlChar *ret;
21762
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021763 ret = xmlDictLookup(vctxt->dict, nsName, -1);
21764 xmlFree(nsName);
21765 return (ret);
21766 } else
21767 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021768#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021769 } else {
21770 xmlNsPtr ns;
21771
21772 if ((vctxt->inode->node == NULL) ||
21773 (vctxt->inode->node->doc == NULL)) {
21774 VERROR_INT("xmlSchemaLookupNamespace",
21775 "no node or node's doc avaliable");
21776 return (NULL);
21777 }
21778 ns = xmlSearchNs(vctxt->inode->node->doc,
21779 vctxt->inode->node, prefix);
21780 if (ns != NULL)
21781 return (ns->href);
21782 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021783 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021784}
21785
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021786/*
21787* This one works on the schema of the validation context.
21788*/
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021789static int
Daniel Veillarddee23482008-04-11 12:58:43 +000021790xmlSchemaValidateNotation(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021791 xmlSchemaPtr schema,
21792 xmlNodePtr node,
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021793 const xmlChar *value,
21794 xmlSchemaValPtr *val,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021795 int valNeeded)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021796{
21797 int ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021798
21799 if (vctxt && (vctxt->schema == NULL)) {
21800 VERROR_INT("xmlSchemaValidateNotation",
21801 "a schema is needed on the validation context");
21802 return (-1);
21803 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021804 ret = xmlValidateQName(value, 1);
21805 if (ret != 0)
21806 return (ret);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021807 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021808 xmlChar *localName = NULL;
21809 xmlChar *prefix = NULL;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021810
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021811 localName = xmlSplitQName2(value, &prefix);
21812 if (prefix != NULL) {
21813 const xmlChar *nsName = NULL;
21814
Daniel Veillarddee23482008-04-11 12:58:43 +000021815 if (vctxt != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021816 nsName = xmlSchemaLookupNamespace(vctxt, BAD_CAST prefix);
21817 else if (node != NULL) {
21818 xmlNsPtr ns = xmlSearchNs(node->doc, node, prefix);
21819 if (ns != NULL)
21820 nsName = ns->href;
21821 } else {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021822 xmlFree(prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021823 xmlFree(localName);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021824 return (1);
21825 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021826 if (nsName == NULL) {
21827 xmlFree(prefix);
21828 xmlFree(localName);
21829 return (1);
21830 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021831 if (xmlSchemaGetNotation(schema, localName, nsName) != NULL) {
Daniel Veillard77fbc3e2008-03-24 14:06:19 +000021832 if ((valNeeded) && (val != NULL)) {
21833 (*val) = xmlSchemaNewNOTATIONValue(xmlStrdup(localName),
21834 xmlStrdup(nsName));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021835 if (*val == NULL)
21836 ret = -1;
21837 }
21838 } else
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021839 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021840 xmlFree(prefix);
21841 xmlFree(localName);
21842 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021843 if (xmlSchemaGetNotation(schema, value, NULL) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021844 if (valNeeded && (val != NULL)) {
21845 (*val) = xmlSchemaNewNOTATIONValue(
21846 BAD_CAST xmlStrdup(value), NULL);
21847 if (*val == NULL)
21848 ret = -1;
21849 }
21850 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021851 return (1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021852 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021853 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021854 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021855}
21856
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000021857static int
21858xmlSchemaVAddNodeQName(xmlSchemaValidCtxtPtr vctxt,
21859 const xmlChar* lname,
21860 const xmlChar* nsname)
21861{
21862 int i;
21863
21864 lname = xmlDictLookup(vctxt->dict, lname, -1);
21865 if (lname == NULL)
21866 return(-1);
21867 if (nsname != NULL) {
21868 nsname = xmlDictLookup(vctxt->dict, nsname, -1);
21869 if (nsname == NULL)
21870 return(-1);
21871 }
21872 for (i = 0; i < vctxt->nodeQNames->nbItems; i += 2) {
21873 if ((vctxt->nodeQNames->items [i] == lname) &&
21874 (vctxt->nodeQNames->items[i +1] == nsname))
21875 /* Already there */
21876 return(i);
21877 }
21878 /* Add new entry. */
21879 i = vctxt->nodeQNames->nbItems;
21880 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) lname);
21881 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) nsname);
21882 return(i);
21883}
21884
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021885/************************************************************************
21886 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021887 * Validation of identity-constraints (IDC) *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021888 * *
21889 ************************************************************************/
21890
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021891/**
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021892 * xmlSchemaAugmentIDC:
21893 * @idcDef: the IDC definition
21894 *
21895 * Creates an augmented IDC definition item.
21896 *
21897 * Returns the item, or NULL on internal errors.
21898 */
21899static void
21900xmlSchemaAugmentIDC(xmlSchemaIDCPtr idcDef,
21901 xmlSchemaValidCtxtPtr vctxt)
21902{
21903 xmlSchemaIDCAugPtr aidc;
21904
21905 aidc = (xmlSchemaIDCAugPtr) xmlMalloc(sizeof(xmlSchemaIDCAug));
21906 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021907 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021908 "xmlSchemaAugmentIDC: allocating an augmented IDC definition",
21909 NULL);
21910 return;
21911 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021912 aidc->keyrefDepth = -1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021913 aidc->def = idcDef;
21914 aidc->next = NULL;
21915 if (vctxt->aidcs == NULL)
21916 vctxt->aidcs = aidc;
21917 else {
21918 aidc->next = vctxt->aidcs;
21919 vctxt->aidcs = aidc;
21920 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021921 /*
21922 * Save if we have keyrefs at all.
21923 */
21924 if ((vctxt->hasKeyrefs == 0) &&
21925 (idcDef->type == XML_SCHEMA_TYPE_IDC_KEYREF))
21926 vctxt->hasKeyrefs = 1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021927}
21928
21929/**
Daniel Veillard3888f472007-08-23 09:29:03 +000021930 * xmlSchemaAugmentImportedIDC:
21931 * @imported: the imported schema
21932 *
21933 * Creates an augmented IDC definition for the imported schema.
21934 */
21935static void
21936xmlSchemaAugmentImportedIDC(xmlSchemaImportPtr imported, xmlSchemaValidCtxtPtr vctxt) {
21937 if (imported->schema->idcDef != NULL) {
21938 xmlHashScan(imported->schema->idcDef ,
21939 (xmlHashScanner) xmlSchemaAugmentIDC, vctxt);
21940 }
21941}
21942
21943/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021944 * xmlSchemaIDCNewBinding:
21945 * @idcDef: the IDC definition of this binding
21946 *
21947 * Creates a new IDC binding.
21948 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021949 * Returns the new IDC binding, NULL on internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021950 */
21951static xmlSchemaPSVIIDCBindingPtr
21952xmlSchemaIDCNewBinding(xmlSchemaIDCPtr idcDef)
21953{
21954 xmlSchemaPSVIIDCBindingPtr ret;
21955
21956 ret = (xmlSchemaPSVIIDCBindingPtr) xmlMalloc(
21957 sizeof(xmlSchemaPSVIIDCBinding));
21958 if (ret == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021959 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021960 "allocating a PSVI IDC binding item", NULL);
21961 return (NULL);
21962 }
21963 memset(ret, 0, sizeof(xmlSchemaPSVIIDCBinding));
21964 ret->definition = idcDef;
21965 return (ret);
21966}
21967
21968/**
21969 * xmlSchemaIDCStoreNodeTableItem:
21970 * @vctxt: the WXS validation context
21971 * @item: the IDC node table item
21972 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021973 * The validation context is used to store IDC node table items.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021974 * They are stored to avoid copying them if IDC node-tables are merged
21975 * with corresponding parent IDC node-tables (bubbling).
21976 *
21977 * Returns 0 if succeeded, -1 on internal errors.
21978 */
21979static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021980xmlSchemaIDCStoreNodeTableItem(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021981 xmlSchemaPSVIIDCNodePtr item)
21982{
21983 /*
21984 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021985 */
21986 if (vctxt->idcNodes == NULL) {
21987 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021988 xmlMalloc(20 * sizeof(xmlSchemaPSVIIDCNodePtr));
21989 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021990 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021991 "allocating the IDC node table item list", NULL);
21992 return (-1);
21993 }
21994 vctxt->sizeIdcNodes = 20;
21995 } else if (vctxt->sizeIdcNodes <= vctxt->nbIdcNodes) {
21996 vctxt->sizeIdcNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021997 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
21998 xmlRealloc(vctxt->idcNodes, vctxt->sizeIdcNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021999 sizeof(xmlSchemaPSVIIDCNodePtr));
22000 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022001 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022002 "re-allocating the IDC node table item list", NULL);
22003 return (-1);
22004 }
22005 }
22006 vctxt->idcNodes[vctxt->nbIdcNodes++] = item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022007
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022008 return (0);
22009}
22010
22011/**
22012 * xmlSchemaIDCStoreKey:
22013 * @vctxt: the WXS validation context
22014 * @item: the IDC key
22015 *
22016 * The validation context is used to store an IDC key.
22017 *
22018 * Returns 0 if succeeded, -1 on internal errors.
22019 */
22020static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022021xmlSchemaIDCStoreKey(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022022 xmlSchemaPSVIIDCKeyPtr key)
22023{
22024 /*
22025 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022026 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022027 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022028 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022029 xmlMalloc(40 * sizeof(xmlSchemaPSVIIDCKeyPtr));
22030 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022031 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022032 "allocating the IDC key storage list", NULL);
22033 return (-1);
22034 }
22035 vctxt->sizeIdcKeys = 40;
22036 } else if (vctxt->sizeIdcKeys <= vctxt->nbIdcKeys) {
22037 vctxt->sizeIdcKeys *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022038 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
22039 xmlRealloc(vctxt->idcKeys, vctxt->sizeIdcKeys *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022040 sizeof(xmlSchemaPSVIIDCKeyPtr));
22041 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022042 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022043 "re-allocating the IDC key storage list", NULL);
22044 return (-1);
22045 }
22046 }
22047 vctxt->idcKeys[vctxt->nbIdcKeys++] = key;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022048
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022049 return (0);
22050}
22051
22052/**
22053 * xmlSchemaIDCAppendNodeTableItem:
22054 * @bind: the IDC binding
22055 * @ntItem: the node-table item
22056 *
22057 * Appends the IDC node-table item to the binding.
22058 *
22059 * Returns 0 on success and -1 on internal errors.
22060 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022061static int
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022062xmlSchemaIDCAppendNodeTableItem(xmlSchemaPSVIIDCBindingPtr bind,
22063 xmlSchemaPSVIIDCNodePtr ntItem)
22064{
22065 if (bind->nodeTable == NULL) {
22066 bind->sizeNodes = 10;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022067 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022068 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
22069 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022070 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022071 "allocating an array of IDC node-table items", NULL);
22072 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022073 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022074 } else if (bind->sizeNodes <= bind->nbNodes) {
22075 bind->sizeNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022076 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
22077 xmlRealloc(bind->nodeTable, bind->sizeNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022078 sizeof(xmlSchemaPSVIIDCNodePtr));
22079 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022080 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022081 "re-allocating an array of IDC node-table items", NULL);
22082 return(-1);
22083 }
22084 }
22085 bind->nodeTable[bind->nbNodes++] = ntItem;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022086 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022087}
22088
22089/**
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022090 * xmlSchemaIDCAcquireBinding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022091 * @vctxt: the WXS validation context
22092 * @matcher: the IDC matcher
22093 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022094 * Looks up an PSVI IDC binding, for the IDC definition and
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022095 * of the given matcher. If none found, a new one is created
22096 * and added to the IDC table.
22097 *
22098 * Returns an IDC binding or NULL on internal errors.
22099 */
22100static xmlSchemaPSVIIDCBindingPtr
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022101xmlSchemaIDCAcquireBinding(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022102 xmlSchemaIDCMatcherPtr matcher)
22103{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022104 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022105
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022106 ielem = vctxt->elemInfos[matcher->depth];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022107
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022108 if (ielem->idcTable == NULL) {
22109 ielem->idcTable = xmlSchemaIDCNewBinding(matcher->aidc->def);
22110 if (ielem->idcTable == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022111 return (NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022112 return(ielem->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022113 } else {
22114 xmlSchemaPSVIIDCBindingPtr bind = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022115
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022116 bind = ielem->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022117 do {
22118 if (bind->definition == matcher->aidc->def)
22119 return(bind);
22120 if (bind->next == NULL) {
22121 bind->next = xmlSchemaIDCNewBinding(matcher->aidc->def);
22122 if (bind->next == NULL)
22123 return (NULL);
22124 return(bind->next);
22125 }
22126 bind = bind->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022127 } while (bind != NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022128 }
22129 return (NULL);
22130}
22131
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022132static xmlSchemaItemListPtr
22133xmlSchemaIDCAcquireTargetList(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
22134 xmlSchemaIDCMatcherPtr matcher)
22135{
22136 if (matcher->targets == NULL)
22137 matcher->targets = xmlSchemaItemListCreate();
22138 return(matcher->targets);
22139}
22140
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022141/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022142 * xmlSchemaIDCFreeKey:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022143 * @key: the IDC key
22144 *
22145 * Frees an IDC key together with its compiled value.
22146 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022147static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022148xmlSchemaIDCFreeKey(xmlSchemaPSVIIDCKeyPtr key)
22149{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022150 if (key->val != NULL)
22151 xmlSchemaFreeValue(key->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022152 xmlFree(key);
22153}
22154
22155/**
22156 * xmlSchemaIDCFreeBinding:
22157 *
22158 * Frees an IDC binding. Note that the node table-items
22159 * are not freed.
22160 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022161static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022162xmlSchemaIDCFreeBinding(xmlSchemaPSVIIDCBindingPtr bind)
22163{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022164 if (bind->nodeTable != NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022165 xmlFree(bind->nodeTable);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022166 if (bind->dupls != NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000022167 xmlSchemaItemListFree(bind->dupls);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022168 xmlFree(bind);
22169}
22170
22171/**
22172 * xmlSchemaIDCFreeIDCTable:
22173 * @bind: the first IDC binding in the list
22174 *
22175 * Frees an IDC table, i.e. all the IDC bindings in the list.
22176 */
22177static void
22178xmlSchemaIDCFreeIDCTable(xmlSchemaPSVIIDCBindingPtr bind)
22179{
22180 xmlSchemaPSVIIDCBindingPtr prev;
22181
22182 while (bind != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022183 prev = bind;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022184 bind = bind->next;
22185 xmlSchemaIDCFreeBinding(prev);
22186 }
22187}
22188
22189/**
22190 * xmlSchemaIDCFreeMatcherList:
22191 * @matcher: the first IDC matcher in the list
22192 *
22193 * Frees a list of IDC matchers.
22194 */
22195static void
22196xmlSchemaIDCFreeMatcherList(xmlSchemaIDCMatcherPtr matcher)
22197{
22198 xmlSchemaIDCMatcherPtr next;
22199
22200 while (matcher != NULL) {
22201 next = matcher->next;
Daniel Veillarddee23482008-04-11 12:58:43 +000022202 if (matcher->keySeqs != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022203 int i;
22204 for (i = 0; i < matcher->sizeKeySeqs; i++)
22205 if (matcher->keySeqs[i] != NULL)
22206 xmlFree(matcher->keySeqs[i]);
Daniel Veillarddee23482008-04-11 12:58:43 +000022207 xmlFree(matcher->keySeqs);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022208 }
22209 if (matcher->targets != NULL) {
22210 if (matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) {
22211 int i;
22212 xmlSchemaPSVIIDCNodePtr idcNode;
22213 /*
22214 * Node-table items for keyrefs are not stored globally
22215 * to the validation context, since they are not bubbled.
22216 * We need to free them here.
22217 */
22218 for (i = 0; i < matcher->targets->nbItems; i++) {
22219 idcNode =
22220 (xmlSchemaPSVIIDCNodePtr) matcher->targets->items[i];
22221 xmlFree(idcNode->keys);
22222 xmlFree(idcNode);
22223 }
22224 }
22225 xmlSchemaItemListFree(matcher->targets);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022226 }
22227 xmlFree(matcher);
22228 matcher = next;
22229 }
22230}
22231
22232/**
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000022233 * xmlSchemaIDCReleaseMatcherList:
22234 * @vctxt: the WXS validation context
22235 * @matcher: the first IDC matcher in the list
22236 *
22237 * Caches a list of IDC matchers for reuse.
22238 */
22239static void
22240xmlSchemaIDCReleaseMatcherList(xmlSchemaValidCtxtPtr vctxt,
22241 xmlSchemaIDCMatcherPtr matcher)
22242{
22243 xmlSchemaIDCMatcherPtr next;
22244
22245 while (matcher != NULL) {
22246 next = matcher->next;
Daniel Veillarddee23482008-04-11 12:58:43 +000022247 if (matcher->keySeqs != NULL) {
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000022248 int i;
22249 /*
22250 * Don't free the array, but only the content.
22251 */
22252 for (i = 0; i < matcher->sizeKeySeqs; i++)
22253 if (matcher->keySeqs[i] != NULL) {
22254 xmlFree(matcher->keySeqs[i]);
22255 matcher->keySeqs[i] = NULL;
22256 }
22257 }
22258 if (matcher->targets) {
22259 if (matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) {
22260 int i;
22261 xmlSchemaPSVIIDCNodePtr idcNode;
22262 /*
22263 * Node-table items for keyrefs are not stored globally
22264 * to the validation context, since they are not bubbled.
22265 * We need to free them here.
22266 */
22267 for (i = 0; i < matcher->targets->nbItems; i++) {
22268 idcNode =
22269 (xmlSchemaPSVIIDCNodePtr) matcher->targets->items[i];
22270 xmlFree(idcNode->keys);
22271 xmlFree(idcNode);
22272 }
22273 }
22274 xmlSchemaItemListFree(matcher->targets);
22275 matcher->targets = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022276 }
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000022277 matcher->next = NULL;
22278 /*
22279 * Cache the matcher.
22280 */
22281 if (vctxt->idcMatcherCache != NULL)
22282 matcher->nextCached = vctxt->idcMatcherCache;
22283 vctxt->idcMatcherCache = matcher;
22284
22285 matcher = next;
22286 }
22287}
22288
22289/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022290 * xmlSchemaIDCAddStateObject:
22291 * @vctxt: the WXS validation context
22292 * @matcher: the IDC matcher
22293 * @sel: the XPath information
22294 * @parent: the parent "selector" state object if any
22295 * @type: "selector" or "field"
22296 *
22297 * Creates/reuses and activates state objects for the given
22298 * XPath information; if the XPath expression consists of unions,
22299 * multiple state objects are created for every unioned expression.
22300 *
22301 * Returns 0 on success and -1 on internal errors.
22302 */
22303static int
22304xmlSchemaIDCAddStateObject(xmlSchemaValidCtxtPtr vctxt,
22305 xmlSchemaIDCMatcherPtr matcher,
22306 xmlSchemaIDCSelectPtr sel,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022307 int type)
22308{
22309 xmlSchemaIDCStateObjPtr sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022310
22311 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022312 * Reuse the state objects from the pool.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022313 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022314 if (vctxt->xpathStatePool != NULL) {
22315 sto = vctxt->xpathStatePool;
22316 vctxt->xpathStatePool = sto->next;
22317 sto->next = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022318 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022319 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022320 * Create a new state object.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022321 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022322 sto = (xmlSchemaIDCStateObjPtr) xmlMalloc(sizeof(xmlSchemaIDCStateObj));
22323 if (sto == NULL) {
22324 xmlSchemaVErrMemory(NULL,
22325 "allocating an IDC state object", NULL);
22326 return (-1);
22327 }
22328 memset(sto, 0, sizeof(xmlSchemaIDCStateObj));
Daniel Veillarddee23482008-04-11 12:58:43 +000022329 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022330 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000022331 * Add to global list.
22332 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022333 if (vctxt->xpathStates != NULL)
22334 sto->next = vctxt->xpathStates;
22335 vctxt->xpathStates = sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022336
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022337 /*
22338 * Free the old xpath validation context.
22339 */
22340 if (sto->xpathCtxt != NULL)
22341 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
22342
22343 /*
22344 * Create a new XPath (pattern) validation context.
22345 */
22346 sto->xpathCtxt = (void *) xmlPatternGetStreamCtxt(
22347 (xmlPatternPtr) sel->xpathComp);
22348 if (sto->xpathCtxt == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022349 VERROR_INT("xmlSchemaIDCAddStateObject",
22350 "failed to create an XPath validation context");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022351 return (-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000022352 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022353 sto->type = type;
22354 sto->depth = vctxt->depth;
22355 sto->matcher = matcher;
22356 sto->sel = sel;
22357 sto->nbHistory = 0;
Daniel Veillarddee23482008-04-11 12:58:43 +000022358
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022359#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022360 xmlGenericError(xmlGenericErrorContext, "IDC: STO push '%s'\n",
22361 sto->sel->xpath);
22362#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022363 return (0);
22364}
22365
22366/**
22367 * xmlSchemaXPathEvaluate:
22368 * @vctxt: the WXS validation context
22369 * @nodeType: the nodeType of the current node
22370 *
22371 * Evaluates all active XPath state objects.
22372 *
22373 * Returns the number of IC "field" state objects which resolved to
22374 * this node, 0 if none resolved and -1 on internal errors.
22375 */
22376static int
22377xmlSchemaXPathEvaluate(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022378 xmlElementType nodeType)
22379{
22380 xmlSchemaIDCStateObjPtr sto, head = NULL, first;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022381 int res, resolved = 0, depth = vctxt->depth;
Daniel Veillarddee23482008-04-11 12:58:43 +000022382
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022383 if (vctxt->xpathStates == NULL)
22384 return (0);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022385
22386 if (nodeType == XML_ATTRIBUTE_NODE)
22387 depth++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022388#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022389 {
22390 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022391 xmlGenericError(xmlGenericErrorContext,
22392 "IDC: EVAL on %s, depth %d, type %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022393 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22394 vctxt->inode->localName), depth, nodeType);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022395 FREE_AND_NULL(str)
22396 }
22397#endif
22398 /*
22399 * Process all active XPath state objects.
22400 */
22401 first = vctxt->xpathStates;
22402 sto = first;
22403 while (sto != head) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022404#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022405 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR)
Daniel Veillarddee23482008-04-11 12:58:43 +000022406 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] selector '%s'\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022407 sto->matcher->aidc->def->name, sto->sel->xpath);
22408 else
Daniel Veillarddee23482008-04-11 12:58:43 +000022409 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] field '%s'\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022410 sto->matcher->aidc->def->name, sto->sel->xpath);
22411#endif
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022412 if (nodeType == XML_ELEMENT_NODE)
22413 res = xmlStreamPush((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022414 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022415 else
22416 res = xmlStreamPushAttr((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022417 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022418
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022419 if (res == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022420 VERROR_INT("xmlSchemaXPathEvaluate",
22421 "calling xmlStreamPush()");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022422 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022423 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022424 if (res == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022425 goto next_sto;
22426 /*
22427 * Full match.
22428 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022429#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022430 xmlGenericError(xmlGenericErrorContext, "IDC: "
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022431 "MATCH\n");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022432#endif
22433 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022434 * Register a match in the state object history.
22435 */
22436 if (sto->history == NULL) {
22437 sto->history = (int *) xmlMalloc(5 * sizeof(int));
22438 if (sto->history == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022439 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022440 "allocating the state object history", NULL);
22441 return(-1);
22442 }
Daniel Veillard6f9b0872006-08-12 14:09:01 +000022443 sto->sizeHistory = 5;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022444 } else if (sto->sizeHistory <= sto->nbHistory) {
22445 sto->sizeHistory *= 2;
22446 sto->history = (int *) xmlRealloc(sto->history,
22447 sto->sizeHistory * sizeof(int));
22448 if (sto->history == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022449 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022450 "re-allocating the state object history", NULL);
22451 return(-1);
22452 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022453 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022454 sto->history[sto->nbHistory++] = depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022455
22456#ifdef DEBUG_IDC
22457 xmlGenericError(xmlGenericErrorContext, "IDC: push match '%d'\n",
22458 vctxt->depth);
22459#endif
22460
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022461 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
22462 xmlSchemaIDCSelectPtr sel;
22463 /*
22464 * Activate state objects for the IDC fields of
22465 * the IDC selector.
22466 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022467#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022468 xmlGenericError(xmlGenericErrorContext, "IDC: "
22469 "activating field states\n");
22470#endif
22471 sel = sto->matcher->aidc->def->fields;
22472 while (sel != NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022473 if (xmlSchemaIDCAddStateObject(vctxt, sto->matcher,
22474 sel, XPATH_STATE_OBJ_TYPE_IDC_FIELD) == -1)
22475 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022476 sel = sel->next;
22477 }
22478 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
22479 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000022480 * An IDC key node was found by the IDC field.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022481 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022482#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022483 xmlGenericError(xmlGenericErrorContext,
22484 "IDC: key found\n");
22485#endif
22486 /*
22487 * Notify that the character value of this node is
22488 * needed.
22489 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022490 if (resolved == 0) {
22491 if ((vctxt->inode->flags &
22492 XML_SCHEMA_NODE_INFO_VALUE_NEEDED) == 0)
22493 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
22494 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022495 resolved++;
22496 }
22497next_sto:
22498 if (sto->next == NULL) {
22499 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022500 * Evaluate field state objects created on this node as well.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022501 */
22502 head = first;
22503 sto = vctxt->xpathStates;
22504 } else
22505 sto = sto->next;
22506 }
22507 return (resolved);
22508}
22509
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022510static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022511xmlSchemaFormatIDCKeySequence(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022512 xmlChar **buf,
22513 xmlSchemaPSVIIDCKeyPtr *seq,
22514 int count)
22515{
22516 int i, res;
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022517 xmlChar *value = NULL;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022518
22519 *buf = xmlStrdup(BAD_CAST "[");
22520 for (i = 0; i < count; i++) {
22521 *buf = xmlStrcat(*buf, BAD_CAST "'");
Daniel Veillarddee23482008-04-11 12:58:43 +000022522 res = xmlSchemaGetCanonValueWhtspExt(seq[i]->val,
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022523 xmlSchemaGetWhiteSpaceFacetValue(seq[i]->type),
22524 &value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022525 if (res == 0)
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022526 *buf = xmlStrcat(*buf, BAD_CAST value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022527 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022528 VERROR_INT("xmlSchemaFormatIDCKeySequence",
22529 "failed to compute a canonical value");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022530 *buf = xmlStrcat(*buf, BAD_CAST "???");
22531 }
22532 if (i < count -1)
22533 *buf = xmlStrcat(*buf, BAD_CAST "', ");
22534 else
22535 *buf = xmlStrcat(*buf, BAD_CAST "'");
22536 if (value != NULL) {
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022537 xmlFree(value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022538 value = NULL;
22539 }
22540 }
22541 *buf = xmlStrcat(*buf, BAD_CAST "]");
22542
22543 return (BAD_CAST *buf);
22544}
22545
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022546/**
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000022547 * xmlSchemaXPathPop:
22548 * @vctxt: the WXS validation context
22549 *
22550 * Pops all XPath states.
22551 *
22552 * Returns 0 on success and -1 on internal errors.
22553 */
22554static int
22555xmlSchemaXPathPop(xmlSchemaValidCtxtPtr vctxt)
22556{
22557 xmlSchemaIDCStateObjPtr sto;
22558 int res;
22559
22560 if (vctxt->xpathStates == NULL)
22561 return(0);
22562 sto = vctxt->xpathStates;
22563 do {
22564 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22565 if (res == -1)
22566 return (-1);
22567 sto = sto->next;
22568 } while (sto != NULL);
22569 return(0);
22570}
22571
22572/**
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022573 * xmlSchemaXPathProcessHistory:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022574 * @vctxt: the WXS validation context
22575 * @type: the simple/complex type of the current node if any at all
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022576 * @val: the precompiled value
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022577 *
22578 * Processes and pops the history items of the IDC state objects.
22579 * IDC key-sequences are validated/created on IDC bindings.
Daniel Veillarddee23482008-04-11 12:58:43 +000022580 *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022581 * Returns 0 on success and -1 on internal errors.
22582 */
22583static int
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022584xmlSchemaXPathProcessHistory(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022585 int depth)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022586{
22587 xmlSchemaIDCStateObjPtr sto, nextsto;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022588 int res, matchDepth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022589 xmlSchemaPSVIIDCKeyPtr key = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022590 xmlSchemaTypePtr type = vctxt->inode->typeDef, simpleType = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022591
22592 if (vctxt->xpathStates == NULL)
22593 return (0);
22594 sto = vctxt->xpathStates;
22595
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022596#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022597 {
22598 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022599 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022600 "IDC: BACK on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022601 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22602 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022603 FREE_AND_NULL(str)
22604 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022605#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022606 /*
22607 * Evaluate the state objects.
22608 */
22609 while (sto != NULL) {
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000022610 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22611 if (res == -1) {
22612 VERROR_INT("xmlSchemaXPathProcessHistory",
22613 "calling xmlStreamPop()");
22614 return (-1);
22615 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022616#ifdef DEBUG_IDC
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000022617 xmlGenericError(xmlGenericErrorContext, "IDC: stream pop '%s'\n",
22618 sto->sel->xpath);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022619#endif
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022620 if (sto->nbHistory == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022621 goto deregister_check;
22622
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022623 matchDepth = sto->history[sto->nbHistory -1];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022624
22625 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022626 * Only matches at the current depth are of interest.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022627 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022628 if (matchDepth != depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022629 sto = sto->next;
22630 continue;
Daniel Veillarddee23482008-04-11 12:58:43 +000022631 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022632 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022633 /*
22634 * NOTE: According to
22635 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2198
22636 * ... the simple-content of complex types is also allowed.
22637 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022638
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022639 if (WXS_IS_COMPLEX(type)) {
22640 if (WXS_HAS_SIMPLE_CONTENT(type)) {
22641 /*
22642 * Sanity check for complex types with simple content.
22643 */
22644 simpleType = type->contentTypeDef;
22645 if (simpleType == NULL) {
22646 VERROR_INT("xmlSchemaXPathProcessHistory",
22647 "field resolves to a CT with simple content "
22648 "but the CT is missing the ST definition");
22649 return (-1);
22650 }
22651 } else
22652 simpleType = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022653 } else
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022654 simpleType = type;
22655 if (simpleType == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022656 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022657
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022658 /*
22659 * Not qualified if the field resolves to a node of non
22660 * simple type.
Daniel Veillarddee23482008-04-11 12:58:43 +000022661 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022662 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Daniel Veillarddee23482008-04-11 12:58:43 +000022663 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022664 WXS_BASIC_CAST sto->matcher->aidc->def,
22665 "The XPath '%s' of a field of %s does evaluate to a node of "
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022666 "non-simple type",
22667 sto->sel->xpath,
22668 xmlSchemaGetIDCDesignation(&str, sto->matcher->aidc->def));
22669 FREE_AND_NULL(str);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022670 sto->nbHistory--;
22671 goto deregister_check;
22672 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022673
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022674 if ((key == NULL) && (vctxt->inode->val == NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022675 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022676 * Failed to provide the normalized value; maybe
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022677 * the value was invalid.
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022678 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022679 VERROR(XML_SCHEMAV_CVC_IDC,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022680 WXS_BASIC_CAST sto->matcher->aidc->def,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000022681 "Warning: No precomputed value available, the value "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022682 "was either invalid or something strange happend");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022683 sto->nbHistory--;
22684 goto deregister_check;
22685 } else {
22686 xmlSchemaIDCMatcherPtr matcher = sto->matcher;
22687 xmlSchemaPSVIIDCKeyPtr *keySeq;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022688 int pos, idx;
Daniel Veillarddee23482008-04-11 12:58:43 +000022689
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022690 /*
22691 * The key will be anchored on the matcher's list of
22692 * key-sequences. The position in this list is determined
22693 * by the target node's depth relative to the matcher's
22694 * depth of creation (i.e. the depth of the scope element).
Daniel Veillarddee23482008-04-11 12:58:43 +000022695 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022696 * Element Depth Pos List-entries
22697 * <scope> 0 NULL
22698 * <bar> 1 NULL
22699 * <target/> 2 2 target
22700 * <bar>
22701 * </scope>
22702 *
22703 * The size of the list is only dependant on the depth of
22704 * the tree.
22705 * An entry will be NULLed in selector_leave, i.e. when
Daniel Veillarddee23482008-04-11 12:58:43 +000022706 * we hit the target's
22707 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022708 pos = sto->depth - matcher->depth;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022709 idx = sto->sel->index;
Daniel Veillarddee23482008-04-11 12:58:43 +000022710
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022711 /*
22712 * Create/grow the array of key-sequences.
22713 */
22714 if (matcher->keySeqs == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022715 if (pos > 9)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022716 matcher->sizeKeySeqs = pos * 2;
22717 else
22718 matcher->sizeKeySeqs = 10;
Daniel Veillarddee23482008-04-11 12:58:43 +000022719 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022720 xmlMalloc(matcher->sizeKeySeqs *
Daniel Veillarddee23482008-04-11 12:58:43 +000022721 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22722 if (matcher->keySeqs == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022723 xmlSchemaVErrMemory(NULL,
22724 "allocating an array of key-sequences",
22725 NULL);
22726 return(-1);
22727 }
22728 memset(matcher->keySeqs, 0,
22729 matcher->sizeKeySeqs *
22730 sizeof(xmlSchemaPSVIIDCKeyPtr *));
Daniel Veillarddee23482008-04-11 12:58:43 +000022731 } else if (pos >= matcher->sizeKeySeqs) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022732 int i = matcher->sizeKeySeqs;
Daniel Veillarddee23482008-04-11 12:58:43 +000022733
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022734 matcher->sizeKeySeqs *= 2;
22735 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22736 xmlRealloc(matcher->keySeqs,
22737 matcher->sizeKeySeqs *
22738 sizeof(xmlSchemaPSVIIDCKeyPtr *));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022739 if (matcher->keySeqs == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022740 xmlSchemaVErrMemory(NULL,
22741 "reallocating an array of key-sequences",
22742 NULL);
22743 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022744 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022745 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022746 * The array needs to be NULLed.
22747 * TODO: Use memset?
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022748 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022749 for (; i < matcher->sizeKeySeqs; i++)
22750 matcher->keySeqs[i] = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022751 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022752
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022753 /*
22754 * Get/create the key-sequence.
22755 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022756 keySeq = matcher->keySeqs[pos];
22757 if (keySeq == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022758 goto create_sequence;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022759 } else if (keySeq[idx] != NULL) {
22760 xmlChar *str = NULL;
22761 /*
22762 * cvc-identity-constraint:
Rob Richardsc6947bb2008-06-29 15:04:41 +000022763 * 3 For each node in the �target node set� all
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022764 * of the {fields}, with that node as the context
22765 * node, evaluate to either an empty node-set or
22766 * a node-set with exactly one member, which must
22767 * have a simple type.
Daniel Veillarddee23482008-04-11 12:58:43 +000022768 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022769 * The key was already set; report an error.
22770 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022771 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022772 XML_SCHEMAV_CVC_IDC, NULL,
22773 WXS_BASIC_CAST matcher->aidc->def,
22774 "The XPath '%s' of a field of %s evaluates to a "
22775 "node-set with more than one member",
22776 sto->sel->xpath,
22777 xmlSchemaGetIDCDesignation(&str, matcher->aidc->def));
22778 FREE_AND_NULL(str);
22779 sto->nbHistory--;
22780 goto deregister_check;
22781 } else
Daniel Veillarddee23482008-04-11 12:58:43 +000022782 goto create_key;
22783
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022784create_sequence:
22785 /*
22786 * Create a key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022787 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022788 keySeq = (xmlSchemaPSVIIDCKeyPtr *) xmlMalloc(
Daniel Veillarddee23482008-04-11 12:58:43 +000022789 matcher->aidc->def->nbFields *
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022790 sizeof(xmlSchemaPSVIIDCKeyPtr));
22791 if (keySeq == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022792 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022793 "allocating an IDC key-sequence", NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000022794 return(-1);
22795 }
22796 memset(keySeq, 0, matcher->aidc->def->nbFields *
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022797 sizeof(xmlSchemaPSVIIDCKeyPtr));
22798 matcher->keySeqs[pos] = keySeq;
22799create_key:
22800 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022801 * Create a key once per node only.
Daniel Veillarddee23482008-04-11 12:58:43 +000022802 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022803 if (key == NULL) {
22804 key = (xmlSchemaPSVIIDCKeyPtr) xmlMalloc(
22805 sizeof(xmlSchemaPSVIIDCKey));
22806 if (key == NULL) {
22807 xmlSchemaVErrMemory(NULL,
22808 "allocating a IDC key", NULL);
22809 xmlFree(keySeq);
22810 matcher->keySeqs[pos] = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022811 return(-1);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022812 }
22813 /*
22814 * Consume the compiled value.
22815 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022816 key->type = simpleType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022817 key->val = vctxt->inode->val;
22818 vctxt->inode->val = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022819 /*
22820 * Store the key in a global list.
22821 */
22822 if (xmlSchemaIDCStoreKey(vctxt, key) == -1) {
22823 xmlSchemaIDCFreeKey(key);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022824 return (-1);
22825 }
22826 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022827 keySeq[idx] = key;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022828 }
22829 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022830
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022831 xmlSchemaPSVIIDCKeyPtr **keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022832 /* xmlSchemaPSVIIDCBindingPtr bind; */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022833 xmlSchemaPSVIIDCNodePtr ntItem;
22834 xmlSchemaIDCMatcherPtr matcher;
22835 xmlSchemaIDCPtr idc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022836 xmlSchemaItemListPtr targets;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022837 int pos, i, j, nbKeys;
22838 /*
22839 * Here we have the following scenario:
22840 * An IDC 'selector' state object resolved to a target node,
Daniel Veillarddee23482008-04-11 12:58:43 +000022841 * during the time this target node was in the
22842 * ancestor-or-self axis, the 'field' state object(s) looked
22843 * out for matching nodes to create a key-sequence for this
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022844 * target node. Now we are back to this target node and need
Daniel Veillarddee23482008-04-11 12:58:43 +000022845 * to put the key-sequence, together with the target node
22846 * itself, into the node-table of the corresponding IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022847 * binding.
22848 */
22849 matcher = sto->matcher;
22850 idc = matcher->aidc->def;
22851 nbKeys = idc->nbFields;
Daniel Veillarddee23482008-04-11 12:58:43 +000022852 pos = depth - matcher->depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022853 /*
22854 * Check if the matcher has any key-sequences at all, plus
22855 * if it has a key-sequence for the current target node.
Daniel Veillarddee23482008-04-11 12:58:43 +000022856 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022857 if ((matcher->keySeqs == NULL) ||
22858 (matcher->sizeKeySeqs <= pos)) {
22859 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22860 goto selector_key_error;
22861 else
22862 goto selector_leave;
22863 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022864
22865 keySeq = &(matcher->keySeqs[pos]);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022866 if (*keySeq == NULL) {
22867 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22868 goto selector_key_error;
22869 else
22870 goto selector_leave;
22871 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022872
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022873 for (i = 0; i < nbKeys; i++) {
22874 if ((*keySeq)[i] == NULL) {
22875 /*
22876 * Not qualified, if not all fields did resolve.
22877 */
22878 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY) {
22879 /*
22880 * All fields of a "key" IDC must resolve.
22881 */
22882 goto selector_key_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000022883 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022884 goto selector_leave;
22885 }
22886 }
22887 /*
22888 * All fields did resolve.
22889 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022890
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022891 /*
22892 * 4.1 If the {identity-constraint category} is unique(/key),
Rob Richardsc6947bb2008-06-29 15:04:41 +000022893 * then no two members of the �qualified node set� have
22894 * �key-sequences� whose members are pairwise equal, as
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022895 * defined by Equal in [XML Schemas: Datatypes].
22896 *
22897 * Get the IDC binding from the matcher and check for
22898 * duplicate key-sequences.
22899 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022900#if 0
22901 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22902#endif
22903 targets = xmlSchemaIDCAcquireTargetList(vctxt, matcher);
Daniel Veillarddee23482008-04-11 12:58:43 +000022904 if ((idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) &&
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022905 (targets->nbItems != 0)) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022906 xmlSchemaPSVIIDCKeyPtr ckey, bkey, *bkeySeq;
Daniel Veillarddee23482008-04-11 12:58:43 +000022907
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022908 i = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022909 res = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022910 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022911 * Compare the key-sequences, key by key.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022912 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022913 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022914 bkeySeq =
22915 ((xmlSchemaPSVIIDCNodePtr) targets->items[i])->keys;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022916 for (j = 0; j < nbKeys; j++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022917 ckey = (*keySeq)[j];
Daniel Veillarddee23482008-04-11 12:58:43 +000022918 bkey = bkeySeq[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022919 res = xmlSchemaAreValuesEqual(ckey->val, bkey->val);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022920 if (res == -1) {
22921 return (-1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022922 } else if (res == 0) {
22923 /*
22924 * One of the keys differs, so the key-sequence
22925 * won't be equal; get out.
22926 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022927 break;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022928 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022929 }
22930 if (res == 1) {
22931 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022932 * Duplicate key-sequence found.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022933 */
22934 break;
22935 }
22936 i++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022937 } while (i < targets->nbItems);
22938 if (i != targets->nbItems) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022939 xmlChar *str = NULL, *strB = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022940 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022941 * TODO: Try to report the key-sequence.
22942 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022943 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022944 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022945 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022946 "Duplicate key-sequence %s in %s",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022947 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022948 (*keySeq), nbKeys),
22949 xmlSchemaGetIDCDesignation(&strB, idc));
22950 FREE_AND_NULL(str);
22951 FREE_AND_NULL(strB);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022952 goto selector_leave;
22953 }
22954 }
22955 /*
22956 * Add a node-table item to the IDC binding.
22957 */
22958 ntItem = (xmlSchemaPSVIIDCNodePtr) xmlMalloc(
22959 sizeof(xmlSchemaPSVIIDCNode));
22960 if (ntItem == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022961 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022962 "allocating an IDC node-table item", NULL);
22963 xmlFree(*keySeq);
22964 *keySeq = NULL;
22965 return(-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000022966 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022967 memset(ntItem, 0, sizeof(xmlSchemaPSVIIDCNode));
Daniel Veillarddee23482008-04-11 12:58:43 +000022968
22969 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022970 * Store the node-table item in a global list.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022971 */
22972 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) {
22973 if (xmlSchemaIDCStoreNodeTableItem(vctxt, ntItem) == -1) {
22974 xmlFree(ntItem);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022975 xmlFree(*keySeq);
22976 *keySeq = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022977 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022978 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022979 ntItem->nodeQNameID = -1;
22980 } else {
22981 /*
22982 * Save a cached QName for this node on the IDC node, to be
22983 * able to report it, even if the node is not saved.
22984 */
22985 ntItem->nodeQNameID = xmlSchemaVAddNodeQName(vctxt,
22986 vctxt->inode->localName, vctxt->inode->nsName);
22987 if (ntItem->nodeQNameID == -1) {
22988 xmlFree(ntItem);
22989 xmlFree(*keySeq);
22990 *keySeq = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022991 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022992 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022993 }
22994 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022995 * Init the node-table item: Save the node, position and
22996 * consume the key-sequence.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022997 */
22998 ntItem->node = vctxt->node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022999 ntItem->nodeLine = vctxt->inode->nodeLine;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023000 ntItem->keys = *keySeq;
23001 *keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023002#if 0
Daniel Veillarddee23482008-04-11 12:58:43 +000023003 if (xmlSchemaIDCAppendNodeTableItem(bind, ntItem) == -1)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023004#endif
23005 if (xmlSchemaItemListAdd(targets, ntItem) == -1) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023006 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023007 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023008 * Free the item, since keyref items won't be
23009 * put on a global list.
23010 */
23011 xmlFree(ntItem->keys);
23012 xmlFree(ntItem);
23013 }
23014 return (-1);
23015 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023016
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023017 goto selector_leave;
23018selector_key_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023019 {
23020 xmlChar *str = NULL;
23021 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000023022 * 4.2.1 (KEY) The �target node set� and the
23023 * �qualified node set� are equal, that is, every
23024 * member of the �target node set� is also a member
23025 * of the �qualified node set� and vice versa.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023026 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023027 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023028 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023029 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023030 "Not all fields of %s evaluate to a node",
23031 xmlSchemaGetIDCDesignation(&str, idc), NULL);
23032 FREE_AND_NULL(str);
23033 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023034selector_leave:
23035 /*
23036 * Free the key-sequence if not added to the IDC table.
23037 */
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000023038 if ((keySeq != NULL) && (*keySeq != NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023039 xmlFree(*keySeq);
23040 *keySeq = NULL;
23041 }
23042 } /* if selector */
Daniel Veillarddee23482008-04-11 12:58:43 +000023043
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023044 sto->nbHistory--;
23045
23046deregister_check:
23047 /*
23048 * Deregister state objects if they reach the depth of creation.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023049 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000023050 if ((sto->nbHistory == 0) && (sto->depth == depth)) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023051#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023052 xmlGenericError(xmlGenericErrorContext, "IDC: STO pop '%s'\n",
23053 sto->sel->xpath);
23054#endif
23055 if (vctxt->xpathStates != sto) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023056 VERROR_INT("xmlSchemaXPathProcessHistory",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023057 "The state object to be removed is not the first "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023058 "in the list");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023059 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023060 nextsto = sto->next;
23061 /*
23062 * Unlink from the list of active XPath state objects.
23063 */
23064 vctxt->xpathStates = sto->next;
23065 sto->next = vctxt->xpathStatePool;
23066 /*
23067 * Link it to the pool of reusable state objects.
23068 */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023069 vctxt->xpathStatePool = sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023070 sto = nextsto;
23071 } else
23072 sto = sto->next;
23073 } /* while (sto != NULL) */
23074 return (0);
23075}
23076
23077/**
23078 * xmlSchemaIDCRegisterMatchers:
23079 * @vctxt: the WXS validation context
23080 * @elemDecl: the element declaration
23081 *
23082 * Creates helper objects to evaluate IDC selectors/fields
23083 * successively.
23084 *
23085 * Returns 0 if OK and -1 on internal errors.
23086 */
23087static int
23088xmlSchemaIDCRegisterMatchers(xmlSchemaValidCtxtPtr vctxt,
23089 xmlSchemaElementPtr elemDecl)
23090{
23091 xmlSchemaIDCMatcherPtr matcher, last = NULL;
23092 xmlSchemaIDCPtr idc, refIdc;
23093 xmlSchemaIDCAugPtr aidc;
Daniel Veillarddee23482008-04-11 12:58:43 +000023094
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023095 idc = (xmlSchemaIDCPtr) elemDecl->idcs;
23096 if (idc == NULL)
23097 return (0);
Daniel Veillarddee23482008-04-11 12:58:43 +000023098
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023099#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023100 {
23101 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000023102 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023103 "IDC: REGISTER on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023104 (char *) xmlSchemaFormatQName(&str, vctxt->inode->nsName,
23105 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023106 FREE_AND_NULL(str)
23107 }
23108#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023109 if (vctxt->inode->idcMatchers != NULL) {
23110 VERROR_INT("xmlSchemaIDCRegisterMatchers",
23111 "The chain of IDC matchers is expected to be empty");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023112 return (-1);
23113 }
23114 do {
23115 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
23116 /*
23117 * Since IDCs bubbles are expensive we need to know the
23118 * depth at which the bubbles should stop; this will be
23119 * the depth of the top-most keyref IDC. If no keyref
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023120 * references a key/unique IDC, the keyrefDepth will
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023121 * be -1, indicating that no bubbles are needed.
23122 */
23123 refIdc = (xmlSchemaIDCPtr) idc->ref->item;
23124 if (refIdc != NULL) {
23125 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023126 * Remember that we have keyrefs on this node.
23127 */
23128 vctxt->inode->hasKeyrefs = 1;
23129 /*
23130 * Lookup the referenced augmented IDC info.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023131 */
23132 aidc = vctxt->aidcs;
23133 while (aidc != NULL) {
23134 if (aidc->def == refIdc)
23135 break;
23136 aidc = aidc->next;
23137 }
23138 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023139 VERROR_INT("xmlSchemaIDCRegisterMatchers",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023140 "Could not find an augmented IDC item for an IDC "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023141 "definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023142 return (-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000023143 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023144 if ((aidc->keyrefDepth == -1) ||
23145 (vctxt->depth < aidc->keyrefDepth))
23146 aidc->keyrefDepth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023147 }
23148 }
23149 /*
23150 * Lookup the augmented IDC item for the IDC definition.
23151 */
23152 aidc = vctxt->aidcs;
23153 while (aidc != NULL) {
23154 if (aidc->def == idc)
23155 break;
23156 aidc = aidc->next;
23157 }
23158 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023159 VERROR_INT("xmlSchemaIDCRegisterMatchers",
23160 "Could not find an augmented IDC item for an IDC definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023161 return (-1);
23162 }
23163 /*
23164 * Create an IDC matcher for every IDC definition.
23165 */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023166 if (vctxt->idcMatcherCache != NULL) {
23167 /*
23168 * Reuse a cached matcher.
23169 */
23170 matcher = vctxt->idcMatcherCache;
23171 vctxt->idcMatcherCache = matcher->nextCached;
23172 matcher->nextCached = NULL;
23173 } else {
Daniel Veillarddee23482008-04-11 12:58:43 +000023174 matcher = (xmlSchemaIDCMatcherPtr)
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023175 xmlMalloc(sizeof(xmlSchemaIDCMatcher));
23176 if (matcher == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023177 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023178 "allocating an IDC matcher", NULL);
23179 return (-1);
23180 }
23181 memset(matcher, 0, sizeof(xmlSchemaIDCMatcher));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023182 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023183 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023184 vctxt->inode->idcMatchers = matcher;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023185 else
23186 last->next = matcher;
23187 last = matcher;
23188
23189 matcher->type = IDC_MATCHER;
Daniel Veillarddee23482008-04-11 12:58:43 +000023190 matcher->depth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023191 matcher->aidc = aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023192 matcher->idcType = aidc->def->type;
Daniel Veillarddee23482008-04-11 12:58:43 +000023193#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023194 xmlGenericError(xmlGenericErrorContext, "IDC: register matcher\n");
Daniel Veillarddee23482008-04-11 12:58:43 +000023195#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023196 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000023197 * Init the automaton state object.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023198 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023199 if (xmlSchemaIDCAddStateObject(vctxt, matcher,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023200 idc->selector, XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) == -1)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023201 return (-1);
23202
23203 idc = idc->next;
23204 } while (idc != NULL);
23205 return (0);
23206}
23207
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023208static int
23209xmlSchemaIDCFillNodeTables(xmlSchemaValidCtxtPtr vctxt,
23210 xmlSchemaNodeInfoPtr ielem)
23211{
23212 xmlSchemaPSVIIDCBindingPtr bind;
23213 int res, i, j, k, nbTargets, nbFields, nbDupls, nbNodeTable;
23214 xmlSchemaPSVIIDCKeyPtr *keys, *ntkeys;
23215 xmlSchemaPSVIIDCNodePtr *targets, *dupls;
Daniel Veillarddee23482008-04-11 12:58:43 +000023216
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023217 xmlSchemaIDCMatcherPtr matcher = ielem->idcMatchers;
23218 /* vctxt->createIDCNodeTables */
23219 while (matcher != NULL) {
23220 /*
23221 * Skip keyref IDCs and empty IDC target-lists.
23222 */
23223 if ((matcher->aidc->def->type == XML_SCHEMA_TYPE_IDC_KEYREF) ||
23224 WXS_ILIST_IS_EMPTY(matcher->targets))
23225 {
23226 matcher = matcher->next;
23227 continue;
23228 }
23229 /*
23230 * If we _want_ the IDC node-table to be created in any case
23231 * then do so. Otherwise create them only if keyrefs need them.
23232 */
23233 if ((! vctxt->createIDCNodeTables) &&
23234 ((matcher->aidc->keyrefDepth == -1) ||
23235 (matcher->aidc->keyrefDepth > vctxt->depth)))
23236 {
23237 matcher = matcher->next;
23238 continue;
23239 }
23240 /*
23241 * Get/create the IDC binding on this element for the IDC definition.
23242 */
23243 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
23244
23245 if (! WXS_ILIST_IS_EMPTY(bind->dupls)) {
23246 dupls = (xmlSchemaPSVIIDCNodePtr *) bind->dupls->items;
23247 nbDupls = bind->dupls->nbItems;
23248 } else {
23249 dupls = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000023250 nbDupls = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023251 }
23252 if (bind->nodeTable != NULL) {
23253 nbNodeTable = bind->nbNodes;
23254 } else {
23255 nbNodeTable = 0;
23256 }
23257
23258 if ((nbNodeTable == 0) && (nbDupls == 0)) {
23259 /*
23260 * Transfer all IDC target-nodes to the IDC node-table.
23261 */
23262 bind->nodeTable =
Daniel Veillarddee23482008-04-11 12:58:43 +000023263 (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023264 bind->sizeNodes = matcher->targets->sizeItems;
23265 bind->nbNodes = matcher->targets->nbItems;
23266
23267 matcher->targets->items = NULL;
23268 matcher->targets->sizeItems = 0;
Daniel Veillarddee23482008-04-11 12:58:43 +000023269 matcher->targets->nbItems = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023270 } else {
23271 /*
23272 * Compare the key-sequences and add to the IDC node-table.
23273 */
23274 nbTargets = matcher->targets->nbItems;
Daniel Veillarddee23482008-04-11 12:58:43 +000023275 targets = (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023276 nbFields = matcher->aidc->def->nbFields;
23277 i = 0;
23278 do {
23279 keys = targets[i]->keys;
Daniel Veillarddee23482008-04-11 12:58:43 +000023280 if (nbDupls) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023281 /*
23282 * Search in already found duplicates first.
23283 */
23284 j = 0;
Daniel Veillarddee23482008-04-11 12:58:43 +000023285 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023286 if (nbFields == 1) {
23287 res = xmlSchemaAreValuesEqual(keys[0]->val,
23288 dupls[j]->keys[0]->val);
23289 if (res == -1)
23290 goto internal_error;
23291 if (res == 1) {
23292 /*
23293 * Equal key-sequence.
23294 */
23295 goto next_target;
23296 }
23297 } else {
23298 res = 0;
23299 ntkeys = dupls[j]->keys;
23300 for (k = 0; k < nbFields; k++) {
23301 res = xmlSchemaAreValuesEqual(keys[k]->val,
23302 ntkeys[k]->val);
23303 if (res == -1)
23304 goto internal_error;
23305 if (res == 0) {
23306 /*
23307 * One of the keys differs.
23308 */
23309 break;
23310 }
23311 }
23312 if (res == 1) {
23313 /*
23314 * Equal key-sequence found.
23315 */
23316 goto next_target;
23317 }
23318 }
23319 j++;
Daniel Veillarddee23482008-04-11 12:58:43 +000023320 } while (j < nbDupls);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023321 }
23322 if (nbNodeTable) {
23323 j = 0;
Daniel Veillarddee23482008-04-11 12:58:43 +000023324 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023325 if (nbFields == 1) {
23326 res = xmlSchemaAreValuesEqual(keys[0]->val,
23327 bind->nodeTable[j]->keys[0]->val);
23328 if (res == -1)
23329 goto internal_error;
23330 if (res == 0) {
23331 /*
23332 * The key-sequence differs.
23333 */
23334 goto next_node_table_entry;
23335 }
23336 } else {
23337 res = 0;
23338 ntkeys = bind->nodeTable[j]->keys;
23339 for (k = 0; k < nbFields; k++) {
23340 res = xmlSchemaAreValuesEqual(keys[k]->val,
23341 ntkeys[k]->val);
23342 if (res == -1)
23343 goto internal_error;
23344 if (res == 0) {
23345 /*
23346 * One of the keys differs.
23347 */
23348 goto next_node_table_entry;
23349 }
23350 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023351 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023352 /*
23353 * Add the duplicate to the list of duplicates.
23354 */
23355 if (bind->dupls == NULL) {
23356 bind->dupls = xmlSchemaItemListCreate();
23357 if (bind->dupls == NULL)
23358 goto internal_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000023359 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023360 if (xmlSchemaItemListAdd(bind->dupls, bind->nodeTable[j]) == -1)
23361 goto internal_error;
23362 /*
23363 * Remove the duplicate entry from the IDC node-table.
23364 */
23365 bind->nodeTable[j] = bind->nodeTable[bind->nbNodes -1];
23366 bind->nbNodes--;
23367
23368 goto next_target;
23369
23370next_node_table_entry:
23371 j++;
23372 } while (j < nbNodeTable);
23373 }
23374 /*
23375 * If everything is fine, then add the IDC target-node to
23376 * the IDC node-table.
23377 */
23378 if (xmlSchemaIDCAppendNodeTableItem(bind, targets[i]) == -1)
23379 goto internal_error;
23380
23381next_target:
23382 i++;
23383 } while (i < nbTargets);
23384 }
23385 matcher = matcher->next;
23386 }
23387 return(0);
23388
23389internal_error:
23390 return(-1);
23391}
23392
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023393/**
Daniel Veillarddee23482008-04-11 12:58:43 +000023394 * xmlSchemaBubbleIDCNodeTables:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023395 * @depth: the current tree depth
23396 *
Daniel Veillarddee23482008-04-11 12:58:43 +000023397 * Merges IDC bindings of an element at @depth into the corresponding IDC
23398 * bindings of its parent element. If a duplicate note-table entry is found,
23399 * both, the parent node-table entry and child entry are discarded from the
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023400 * node-table of the parent.
23401 *
23402 * Returns 0 if OK and -1 on internal errors.
23403 */
23404static int
23405xmlSchemaBubbleIDCNodeTables(xmlSchemaValidCtxtPtr vctxt)
23406{
23407 xmlSchemaPSVIIDCBindingPtr bind; /* IDC bindings of the current node. */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023408 xmlSchemaPSVIIDCBindingPtr *parTable, parBind = NULL; /* parent IDC bindings. */
23409 xmlSchemaPSVIIDCNodePtr node, parNode = NULL, *dupls, *parNodes; /* node-table entries. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023410 xmlSchemaIDCAugPtr aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023411 int i, j, k, ret = 0, nbFields, oldNum, oldDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023412
Daniel Veillarddee23482008-04-11 12:58:43 +000023413 bind = vctxt->inode->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023414 if (bind == NULL) {
23415 /* Fine, no table, no bubbles. */
23416 return (0);
23417 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023418
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023419 parTable = &(vctxt->elemInfos[vctxt->depth -1]->idcTable);
23420 /*
23421 * Walk all bindings; create new or add to existing bindings.
23422 * Remove duplicate key-sequences.
23423 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023424 while (bind != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023425
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023426 if ((bind->nbNodes == 0) && WXS_ILIST_IS_EMPTY(bind->dupls))
23427 goto next_binding;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023428 /*
23429 * Check if the key/unique IDC table needs to be bubbled.
23430 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023431 if (! vctxt->createIDCNodeTables) {
23432 aidc = vctxt->aidcs;
23433 do {
23434 if (aidc->def == bind->definition) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023435 if ((aidc->keyrefDepth == -1) ||
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023436 (aidc->keyrefDepth >= vctxt->depth)) {
23437 goto next_binding;
23438 }
23439 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023440 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023441 aidc = aidc->next;
23442 } while (aidc != NULL);
23443 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023444
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023445 if (parTable != NULL)
23446 parBind = *parTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023447 /*
23448 * Search a matching parent binding for the
23449 * IDC definition.
23450 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023451 while (parBind != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023452 if (parBind->definition == bind->definition)
23453 break;
23454 parBind = parBind->next;
23455 }
23456
23457 if (parBind != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023458 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000023459 * Compare every node-table entry of the child node,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023460 * i.e. the key-sequence within, ...
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023461 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023462 oldNum = parBind->nbNodes; /* Skip newly added items. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023463
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023464 if (! WXS_ILIST_IS_EMPTY(parBind->dupls)) {
23465 oldDupls = parBind->dupls->nbItems;
23466 dupls = (xmlSchemaPSVIIDCNodePtr *) parBind->dupls->items;
23467 } else {
23468 dupls = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000023469 oldDupls = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023470 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023471
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023472 parNodes = parBind->nodeTable;
23473 nbFields = bind->definition->nbFields;
Daniel Veillarddee23482008-04-11 12:58:43 +000023474
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023475 for (i = 0; i < bind->nbNodes; i++) {
23476 node = bind->nodeTable[i];
23477 if (node == NULL)
23478 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023479 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023480 * ...with every key-sequence of the parent node, already
23481 * evaluated to be a duplicate key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023482 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023483 if (oldDupls) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023484 j = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023485 while (j < oldDupls) {
23486 if (nbFields == 1) {
23487 ret = xmlSchemaAreValuesEqual(
23488 node->keys[0]->val,
23489 dupls[j]->keys[0]->val);
23490 if (ret == -1)
23491 goto internal_error;
23492 if (ret == 0) {
23493 j++;
23494 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023495 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023496 } else {
23497 parNode = dupls[j];
23498 for (k = 0; k < nbFields; k++) {
23499 ret = xmlSchemaAreValuesEqual(
23500 node->keys[k]->val,
23501 parNode->keys[k]->val);
23502 if (ret == -1)
23503 goto internal_error;
23504 if (ret == 0)
23505 break;
23506 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023507 }
23508 if (ret == 1)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023509 /* Duplicate found. */
23510 break;
23511 j++;
23512 }
23513 if (j != oldDupls) {
23514 /* Duplicate found. Skip this entry. */
23515 continue;
23516 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023517 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023518 /*
23519 * ... and with every key-sequence of the parent node.
23520 */
23521 if (oldNum) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023522 j = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023523 while (j < oldNum) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +000023524 parNode = parNodes[j];
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023525 if (nbFields == 1) {
23526 ret = xmlSchemaAreValuesEqual(
23527 node->keys[0]->val,
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +000023528 parNode->keys[0]->val);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023529 if (ret == -1)
23530 goto internal_error;
23531 if (ret == 0) {
23532 j++;
23533 continue;
23534 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023535 } else {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023536 for (k = 0; k < nbFields; k++) {
23537 ret = xmlSchemaAreValuesEqual(
23538 node->keys[k]->val,
23539 parNode->keys[k]->val);
23540 if (ret == -1)
23541 goto internal_error;
23542 if (ret == 0)
23543 break;
23544 }
23545 }
23546 if (ret == 1)
23547 /* Duplicate found. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023548 break;
23549 j++;
23550 }
23551 if (j != oldNum) {
23552 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023553 * Handle duplicates. Move the duplicate in
23554 * the parent's node-table to the list of
23555 * duplicates.
Daniel Veillarddee23482008-04-11 12:58:43 +000023556 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023557 oldNum--;
23558 parBind->nbNodes--;
23559 /*
23560 * Move last old item to pos of duplicate.
23561 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023562 parNodes[j] = parNodes[oldNum];
Daniel Veillarddee23482008-04-11 12:58:43 +000023563
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023564 if (parBind->nbNodes != oldNum) {
23565 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000023566 * If new items exist, move last new item to
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023567 * last of old items.
23568 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023569 parNodes[oldNum] =
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023570 parNodes[parBind->nbNodes];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023571 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023572 if (parBind->dupls == NULL) {
23573 parBind->dupls = xmlSchemaItemListCreate();
23574 if (parBind->dupls == NULL)
23575 goto internal_error;
23576 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023577 xmlSchemaItemListAdd(parBind->dupls, parNode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023578 } else {
23579 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000023580 * Add the node-table entry (node and key-sequence) of
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023581 * the child node to the node table of the parent node.
23582 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023583 if (parBind->nodeTable == NULL) {
23584 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023585 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023586 if (parBind->nodeTable == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023587 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023588 "allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023589 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023590 }
23591 parBind->sizeNodes = 1;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023592 } else if (parBind->nbNodes >= parBind->sizeNodes) {
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023593 parBind->sizeNodes *= 2;
Daniel Veillarddee23482008-04-11 12:58:43 +000023594 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23595 xmlRealloc(parBind->nodeTable, parBind->sizeNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023596 sizeof(xmlSchemaPSVIIDCNodePtr));
23597 if (parBind->nodeTable == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023598 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023599 "re-allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023600 goto internal_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000023601 }
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000023602 }
23603 parNodes = parBind->nodeTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023604 /*
23605 * Append the new node-table entry to the 'new node-table
23606 * entries' section.
23607 */
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000023608 parNodes[parBind->nbNodes++] = node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023609 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023610
Daniel Veillarddee23482008-04-11 12:58:43 +000023611 }
23612
23613 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023614 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023615 /*
23616 * No binding for the IDC was found: create a new one and
23617 * copy all node-tables.
23618 */
23619 parBind = xmlSchemaIDCNewBinding(bind->definition);
23620 if (parBind == NULL)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023621 goto internal_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000023622
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023623 /*
23624 * TODO: Hmm, how to optimize the initial number of
23625 * allocated entries?
23626 */
23627 if (bind->nbNodes != 0) {
23628 /*
23629 * Add all IDC node-table entries.
23630 */
23631 if (! vctxt->psviExposeIDCNodeTables) {
23632 /*
23633 * Just move the entries.
23634 * NOTE: this is quite save here, since
23635 * all the keyref lookups have already been
23636 * performed.
23637 */
23638 parBind->nodeTable = bind->nodeTable;
23639 bind->nodeTable = NULL;
23640 parBind->sizeNodes = bind->sizeNodes;
23641 bind->sizeNodes = 0;
23642 parBind->nbNodes = bind->nbNodes;
23643 bind->nbNodes = 0;
23644 } else {
23645 /*
23646 * Copy the entries.
23647 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023648 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023649 xmlMalloc(bind->nbNodes *
23650 sizeof(xmlSchemaPSVIIDCNodePtr));
23651 if (parBind->nodeTable == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023652 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023653 "allocating an array of IDC node-table "
23654 "items", NULL);
23655 xmlSchemaIDCFreeBinding(parBind);
23656 goto internal_error;
23657 }
23658 parBind->sizeNodes = bind->nbNodes;
23659 parBind->nbNodes = bind->nbNodes;
23660 memcpy(parBind->nodeTable, bind->nodeTable,
23661 bind->nbNodes * sizeof(xmlSchemaPSVIIDCNodePtr));
23662 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023663 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023664 if (bind->dupls) {
23665 /*
23666 * Move the duplicates.
23667 */
23668 if (parBind->dupls != NULL)
23669 xmlSchemaItemListFree(parBind->dupls);
23670 parBind->dupls = bind->dupls;
Daniel Veillarddee23482008-04-11 12:58:43 +000023671 bind->dupls = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023672 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023673 if (*parTable == NULL)
23674 *parTable = parBind;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023675 else {
23676 parBind->next = *parTable;
23677 *parTable = parBind;
Daniel Veillarddee23482008-04-11 12:58:43 +000023678 }
23679 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023680
23681next_binding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023682 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023683 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023684 return (0);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023685
23686internal_error:
23687 return(-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023688}
23689
23690/**
23691 * xmlSchemaCheckCVCIDCKeyRef:
23692 * @vctxt: the WXS validation context
23693 * @elemDecl: the element declaration
23694 *
23695 * Check the cvc-idc-keyref constraints.
23696 */
23697static int
23698xmlSchemaCheckCVCIDCKeyRef(xmlSchemaValidCtxtPtr vctxt)
23699{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023700 xmlSchemaIDCMatcherPtr matcher;
23701 xmlSchemaPSVIIDCBindingPtr bind;
Daniel Veillarddee23482008-04-11 12:58:43 +000023702
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023703 matcher = vctxt->inode->idcMatchers;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023704 /*
23705 * Find a keyref.
23706 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023707 while (matcher != NULL) {
23708 if ((matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) &&
23709 matcher->targets &&
23710 matcher->targets->nbItems)
23711 {
23712 int i, j, k, res, nbFields, hasDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023713 xmlSchemaPSVIIDCKeyPtr *refKeys, *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023714 xmlSchemaPSVIIDCNodePtr refNode = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023715
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023716 nbFields = matcher->aidc->def->nbFields;
23717
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023718 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023719 * Find the IDC node-table for the referenced IDC key/unique.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023720 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023721 bind = vctxt->inode->idcTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023722 while (bind != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023723 if ((xmlSchemaIDCPtr) matcher->aidc->def->ref->item ==
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023724 bind->definition)
23725 break;
23726 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023727 }
23728 hasDupls = (bind && bind->dupls && bind->dupls->nbItems) ? 1 : 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023729 /*
23730 * Search for a matching key-sequences.
23731 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023732 for (i = 0; i < matcher->targets->nbItems; i++) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023733 res = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023734 refNode = matcher->targets->items[i];
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023735 if (bind != NULL) {
23736 refKeys = refNode->keys;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023737 for (j = 0; j < bind->nbNodes; j++) {
23738 keys = bind->nodeTable[j]->keys;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023739 for (k = 0; k < nbFields; k++) {
23740 res = xmlSchemaAreValuesEqual(keys[k]->val,
23741 refKeys[k]->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023742 if (res == 0)
23743 break;
23744 else if (res == -1) {
23745 return (-1);
23746 }
23747 }
23748 if (res == 1) {
23749 /*
23750 * Match found.
23751 */
23752 break;
23753 }
23754 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023755 if ((res == 0) && hasDupls) {
23756 /*
23757 * Search in duplicates
23758 */
23759 for (j = 0; j < bind->dupls->nbItems; j++) {
23760 keys = ((xmlSchemaPSVIIDCNodePtr)
23761 bind->dupls->items[j])->keys;
23762 for (k = 0; k < nbFields; k++) {
23763 res = xmlSchemaAreValuesEqual(keys[k]->val,
23764 refKeys[k]->val);
23765 if (res == 0)
23766 break;
23767 else if (res == -1) {
23768 return (-1);
23769 }
23770 }
23771 if (res == 1) {
23772 /*
23773 * Match in duplicates found.
23774 */
23775 xmlChar *str = NULL, *strB = NULL;
23776 xmlSchemaKeyrefErr(vctxt,
23777 XML_SCHEMAV_CVC_IDC, refNode,
23778 (xmlSchemaTypePtr) matcher->aidc->def,
23779 "More than one match found for "
23780 "key-sequence %s of keyref '%s'",
23781 xmlSchemaFormatIDCKeySequence(vctxt, &str,
23782 refNode->keys, nbFields),
23783 xmlSchemaGetComponentQName(&strB,
23784 matcher->aidc->def));
23785 FREE_AND_NULL(str);
23786 FREE_AND_NULL(strB);
23787 break;
23788 }
23789 }
23790 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023791 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023792
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023793 if (res == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023794 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023795 xmlSchemaKeyrefErr(vctxt,
23796 XML_SCHEMAV_CVC_IDC, refNode,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023797 (xmlSchemaTypePtr) matcher->aidc->def,
23798 "No match found for key-sequence %s of keyref '%s'",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000023799 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023800 refNode->keys, nbFields),
23801 xmlSchemaGetComponentQName(&strB, matcher->aidc->def));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023802 FREE_AND_NULL(str);
23803 FREE_AND_NULL(strB);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023804 }
23805 }
23806 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023807 matcher = matcher->next;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023808 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023809 /* TODO: Return an error if any error encountered. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023810 return (0);
23811}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023812
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023813/************************************************************************
23814 * *
23815 * XML Reader validation code *
23816 * *
23817 ************************************************************************/
23818
23819static xmlSchemaAttrInfoPtr
23820xmlSchemaGetFreshAttrInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023821{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023822 xmlSchemaAttrInfoPtr iattr;
23823 /*
23824 * Grow/create list of attribute infos.
23825 */
23826 if (vctxt->attrInfos == NULL) {
23827 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23828 xmlMalloc(sizeof(xmlSchemaAttrInfoPtr));
23829 vctxt->sizeAttrInfos = 1;
23830 if (vctxt->attrInfos == NULL) {
23831 xmlSchemaVErrMemory(vctxt,
23832 "allocating attribute info list", NULL);
23833 return (NULL);
23834 }
23835 } else if (vctxt->sizeAttrInfos <= vctxt->nbAttrInfos) {
23836 vctxt->sizeAttrInfos++;
23837 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23838 xmlRealloc(vctxt->attrInfos,
23839 vctxt->sizeAttrInfos * sizeof(xmlSchemaAttrInfoPtr));
23840 if (vctxt->attrInfos == NULL) {
23841 xmlSchemaVErrMemory(vctxt,
23842 "re-allocating attribute info list", NULL);
23843 return (NULL);
23844 }
23845 } else {
23846 iattr = vctxt->attrInfos[vctxt->nbAttrInfos++];
23847 if (iattr->localName != NULL) {
23848 VERROR_INT("xmlSchemaGetFreshAttrInfo",
23849 "attr info not cleared");
23850 return (NULL);
23851 }
23852 iattr->nodeType = XML_ATTRIBUTE_NODE;
23853 return (iattr);
23854 }
23855 /*
23856 * Create an attribute info.
23857 */
23858 iattr = (xmlSchemaAttrInfoPtr)
23859 xmlMalloc(sizeof(xmlSchemaAttrInfo));
23860 if (iattr == NULL) {
23861 xmlSchemaVErrMemory(vctxt, "creating new attribute info", NULL);
23862 return (NULL);
23863 }
23864 memset(iattr, 0, sizeof(xmlSchemaAttrInfo));
23865 iattr->nodeType = XML_ATTRIBUTE_NODE;
23866 vctxt->attrInfos[vctxt->nbAttrInfos++] = iattr;
23867
23868 return (iattr);
23869}
23870
23871static int
23872xmlSchemaValidatorPushAttribute(xmlSchemaValidCtxtPtr vctxt,
23873 xmlNodePtr attrNode,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023874 int nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023875 const xmlChar *localName,
Daniel Veillarddee23482008-04-11 12:58:43 +000023876 const xmlChar *nsName,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023877 int ownedNames,
23878 xmlChar *value,
23879 int ownedValue)
23880{
23881 xmlSchemaAttrInfoPtr attr;
23882
23883 attr = xmlSchemaGetFreshAttrInfo(vctxt);
23884 if (attr == NULL) {
23885 VERROR_INT("xmlSchemaPushAttribute",
23886 "calling xmlSchemaGetFreshAttrInfo()");
23887 return (-1);
23888 }
23889 attr->node = attrNode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023890 attr->nodeLine = nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023891 attr->state = XML_SCHEMAS_ATTR_UNKNOWN;
23892 attr->localName = localName;
23893 attr->nsName = nsName;
23894 if (ownedNames)
23895 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
23896 /*
23897 * Evaluate if it's an XSI attribute.
23898 */
23899 if (nsName != NULL) {
23900 if (xmlStrEqual(localName, BAD_CAST "nil")) {
23901 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023902 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NIL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023903 }
23904 } else if (xmlStrEqual(localName, BAD_CAST "type")) {
23905 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23906 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_TYPE;
23907 }
23908 } else if (xmlStrEqual(localName, BAD_CAST "schemaLocation")) {
23909 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23910 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC;
23911 }
23912 } else if (xmlStrEqual(localName, BAD_CAST "noNamespaceSchemaLocation")) {
23913 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23914 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC;
23915 }
23916 } else if (xmlStrEqual(attr->nsName, xmlNamespaceNs)) {
23917 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XMLNS;
23918 }
23919 }
23920 attr->value = value;
23921 if (ownedValue)
23922 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
23923 if (attr->metaType != 0)
23924 attr->state = XML_SCHEMAS_ATTR_META;
23925 return (0);
23926}
23927
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023928/**
23929 * xmlSchemaClearElemInfo:
23930 * @vctxt: the WXS validation context
23931 * @ielem: the element information item
23932 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023933static void
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023934xmlSchemaClearElemInfo(xmlSchemaValidCtxtPtr vctxt,
23935 xmlSchemaNodeInfoPtr ielem)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023936{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023937 ielem->hasKeyrefs = 0;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000023938 ielem->appliedXPath = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023939 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
23940 FREE_AND_NULL(ielem->localName);
23941 FREE_AND_NULL(ielem->nsName);
23942 } else {
23943 ielem->localName = NULL;
23944 ielem->nsName = NULL;
23945 }
23946 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
23947 FREE_AND_NULL(ielem->value);
23948 } else {
23949 ielem->value = NULL;
23950 }
23951 if (ielem->val != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023952 /*
23953 * PSVI TODO: Be careful not to free it when the value is
23954 * exposed via PSVI.
23955 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023956 xmlSchemaFreeValue(ielem->val);
23957 ielem->val = NULL;
23958 }
23959 if (ielem->idcMatchers != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023960 /*
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023961 * REVISIT OPTIMIZE TODO: Use a pool of IDC matchers.
23962 * Does it work?
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023963 */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023964 xmlSchemaIDCReleaseMatcherList(vctxt, ielem->idcMatchers);
23965#if 0
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023966 xmlSchemaIDCFreeMatcherList(ielem->idcMatchers);
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023967#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023968 ielem->idcMatchers = NULL;
23969 }
23970 if (ielem->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023971 /*
23972 * OPTIMIZE TODO: Use a pool of IDC tables??.
23973 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023974 xmlSchemaIDCFreeIDCTable(ielem->idcTable);
23975 ielem->idcTable = NULL;
23976 }
23977 if (ielem->regexCtxt != NULL) {
23978 xmlRegFreeExecCtxt(ielem->regexCtxt);
23979 ielem->regexCtxt = NULL;
23980 }
23981 if (ielem->nsBindings != NULL) {
23982 xmlFree((xmlChar **)ielem->nsBindings);
23983 ielem->nsBindings = NULL;
23984 ielem->nbNsBindings = 0;
23985 ielem->sizeNsBindings = 0;
23986 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023987}
23988
23989/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023990 * xmlSchemaGetFreshElemInfo:
23991 * @vctxt: the schema validation context
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023992 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023993 * Creates/reuses and initializes the element info item for
23994 * the currect tree depth.
23995 *
23996 * Returns the element info item or NULL on API or internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023997 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023998static xmlSchemaNodeInfoPtr
23999xmlSchemaGetFreshElemInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024000{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024001 xmlSchemaNodeInfoPtr info = NULL;
24002
24003 if (vctxt->depth > vctxt->sizeElemInfos) {
24004 VERROR_INT("xmlSchemaGetFreshElemInfo",
24005 "inconsistent depth encountered");
24006 return (NULL);
24007 }
24008 if (vctxt->elemInfos == NULL) {
24009 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
24010 xmlMalloc(10 * sizeof(xmlSchemaNodeInfoPtr));
24011 if (vctxt->elemInfos == NULL) {
24012 xmlSchemaVErrMemory(vctxt,
24013 "allocating the element info array", NULL);
24014 return (NULL);
24015 }
24016 memset(vctxt->elemInfos, 0, 10 * sizeof(xmlSchemaNodeInfoPtr));
24017 vctxt->sizeElemInfos = 10;
24018 } else if (vctxt->sizeElemInfos <= vctxt->depth) {
24019 int i = vctxt->sizeElemInfos;
24020
24021 vctxt->sizeElemInfos *= 2;
24022 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
24023 xmlRealloc(vctxt->elemInfos, vctxt->sizeElemInfos *
24024 sizeof(xmlSchemaNodeInfoPtr));
24025 if (vctxt->elemInfos == NULL) {
24026 xmlSchemaVErrMemory(vctxt,
24027 "re-allocating the element info array", NULL);
24028 return (NULL);
24029 }
24030 /*
24031 * We need the new memory to be NULLed.
24032 * TODO: Use memset instead?
24033 */
24034 for (; i < vctxt->sizeElemInfos; i++)
24035 vctxt->elemInfos[i] = NULL;
24036 } else
24037 info = vctxt->elemInfos[vctxt->depth];
24038
24039 if (info == NULL) {
24040 info = (xmlSchemaNodeInfoPtr)
24041 xmlMalloc(sizeof(xmlSchemaNodeInfo));
24042 if (info == NULL) {
24043 xmlSchemaVErrMemory(vctxt,
24044 "allocating an element info", NULL);
24045 return (NULL);
24046 }
24047 vctxt->elemInfos[vctxt->depth] = info;
24048 } else {
24049 if (info->localName != NULL) {
24050 VERROR_INT("xmlSchemaGetFreshElemInfo",
24051 "elem info has not been cleared");
24052 return (NULL);
24053 }
24054 }
24055 memset(info, 0, sizeof(xmlSchemaNodeInfo));
24056 info->nodeType = XML_ELEMENT_NODE;
24057 info->depth = vctxt->depth;
24058
24059 return (info);
24060}
24061
24062#define ACTIVATE_ATTRIBUTE(item) vctxt->inode = (xmlSchemaNodeInfoPtr) item;
24063#define ACTIVATE_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth];
24064#define ACTIVATE_PARENT_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth -1];
24065
24066static int
24067xmlSchemaValidateFacets(xmlSchemaAbstractCtxtPtr actxt,
24068 xmlNodePtr node,
24069 xmlSchemaTypePtr type,
24070 xmlSchemaValType valType,
24071 const xmlChar * value,
24072 xmlSchemaValPtr val,
24073 unsigned long length,
24074 int fireErrors)
24075{
24076 int ret, error = 0;
24077
24078 xmlSchemaTypePtr tmpType;
24079 xmlSchemaFacetLinkPtr facetLink;
24080 xmlSchemaFacetPtr facet;
24081 unsigned long len = 0;
24082 xmlSchemaWhitespaceValueType ws;
24083
24084 /*
24085 * In Libxml2, derived built-in types have currently no explicit facets.
24086 */
24087 if (type->type == XML_SCHEMA_TYPE_BASIC)
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000024088 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024089
24090 /*
24091 * NOTE: Do not jump away, if the facetSet of the given type is
24092 * empty: until now, "pattern" and "enumeration" facets of the
24093 * *base types* need to be checked as well.
24094 */
24095 if (type->facetSet == NULL)
24096 goto pattern_and_enum;
24097
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024098 if (! WXS_IS_ATOMIC(type)) {
24099 if (WXS_IS_LIST(type))
24100 goto WXS_IS_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024101 else
24102 goto pattern_and_enum;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024103 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024104 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024105 * Whitespace handling is only of importance for string-based
24106 * types.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024107 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024108 tmpType = xmlSchemaGetPrimitiveType(type);
24109 if ((tmpType->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024110 WXS_IS_ANY_SIMPLE_TYPE(tmpType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024111 ws = xmlSchemaGetWhiteSpaceFacetValue(type);
24112 } else
24113 ws = XML_SCHEMA_WHITESPACE_COLLAPSE;
24114 /*
24115 * If the value was not computed (for string or
24116 * anySimpleType based types), then use the provided
24117 * type.
24118 */
24119 if (val == NULL)
24120 valType = valType;
24121 else
24122 valType = xmlSchemaGetValType(val);
Daniel Veillarddee23482008-04-11 12:58:43 +000024123
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024124 ret = 0;
24125 for (facetLink = type->facetSet; facetLink != NULL;
24126 facetLink = facetLink->next) {
24127 /*
24128 * Skip the pattern "whiteSpace": it is used to
24129 * format the character content beforehand.
24130 */
24131 switch (facetLink->facet->type) {
24132 case XML_SCHEMA_FACET_WHITESPACE:
24133 case XML_SCHEMA_FACET_PATTERN:
24134 case XML_SCHEMA_FACET_ENUMERATION:
24135 continue;
24136 case XML_SCHEMA_FACET_LENGTH:
24137 case XML_SCHEMA_FACET_MINLENGTH:
24138 case XML_SCHEMA_FACET_MAXLENGTH:
24139 ret = xmlSchemaValidateLengthFacetWhtsp(facetLink->facet,
24140 valType, value, val, &len, ws);
24141 break;
24142 default:
24143 ret = xmlSchemaValidateFacetWhtsp(facetLink->facet, ws,
24144 valType, value, val, ws);
24145 break;
24146 }
24147 if (ret < 0) {
24148 AERROR_INT("xmlSchemaValidateFacets",
24149 "validating against a atomic type facet");
24150 return (-1);
24151 } else if (ret > 0) {
24152 if (fireErrors)
24153 xmlSchemaFacetErr(actxt, ret, node,
24154 value, len, type, facetLink->facet, NULL, NULL, NULL);
24155 else
24156 return (ret);
24157 if (error == 0)
24158 error = ret;
24159 }
24160 ret = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024161 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024162
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024163WXS_IS_LIST:
24164 if (! WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024165 goto pattern_and_enum;
24166 /*
24167 * "length", "minLength" and "maxLength" of list types.
24168 */
24169 ret = 0;
24170 for (facetLink = type->facetSet; facetLink != NULL;
24171 facetLink = facetLink->next) {
Daniel Veillarddee23482008-04-11 12:58:43 +000024172
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024173 switch (facetLink->facet->type) {
24174 case XML_SCHEMA_FACET_LENGTH:
24175 case XML_SCHEMA_FACET_MINLENGTH:
Daniel Veillarddee23482008-04-11 12:58:43 +000024176 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024177 ret = xmlSchemaValidateListSimpleTypeFacet(facetLink->facet,
24178 value, length, NULL);
24179 break;
24180 default:
24181 continue;
24182 }
24183 if (ret < 0) {
24184 AERROR_INT("xmlSchemaValidateFacets",
24185 "validating against a list type facet");
24186 return (-1);
24187 } else if (ret > 0) {
Daniel Veillarddee23482008-04-11 12:58:43 +000024188 if (fireErrors)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024189 xmlSchemaFacetErr(actxt, ret, node,
24190 value, length, type, facetLink->facet, NULL, NULL, NULL);
24191 else
24192 return (ret);
24193 if (error == 0)
24194 error = ret;
24195 }
24196 ret = 0;
24197 }
24198
24199pattern_and_enum:
24200 if (error >= 0) {
24201 int found = 0;
24202 /*
24203 * Process enumerations. Facet values are in the value space
24204 * of the defining type's base type. This seems to be a bug in the
24205 * XML Schema 1.0 spec. Use the whitespace type of the base type.
24206 * Only the first set of enumerations in the ancestor-or-self axis
24207 * is used for validation.
24208 */
24209 ret = 0;
24210 tmpType = type;
24211 do {
24212 for (facet = tmpType->facets; facet != NULL; facet = facet->next) {
24213 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
24214 continue;
24215 found = 1;
24216 ret = xmlSchemaAreValuesEqual(facet->val, val);
24217 if (ret == 1)
24218 break;
24219 else if (ret < 0) {
24220 AERROR_INT("xmlSchemaValidateFacets",
24221 "validating against an enumeration facet");
24222 return (-1);
24223 }
24224 }
24225 if (ret != 0)
24226 break;
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +000024227 /*
24228 * Break on the first set of enumerations. Any additional
24229 * enumerations which might be existent on the ancestors
24230 * of the current type are restricted by this set; thus
24231 * *must* *not* be taken into account.
24232 */
24233 if (found)
24234 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024235 tmpType = tmpType->baseType;
24236 } while ((tmpType != NULL) &&
24237 (tmpType->type != XML_SCHEMA_TYPE_BASIC));
24238 if (found && (ret == 0)) {
24239 ret = XML_SCHEMAV_CVC_ENUMERATION_VALID;
24240 if (fireErrors) {
24241 xmlSchemaFacetErr(actxt, ret, node,
24242 value, 0, type, NULL, NULL, NULL, NULL);
24243 } else
24244 return (ret);
24245 if (error == 0)
24246 error = ret;
24247 }
24248 }
24249
24250 if (error >= 0) {
24251 int found;
24252 /*
24253 * Process patters. Pattern facets are ORed at type level
24254 * and ANDed if derived. Walk the base type axis.
24255 */
24256 tmpType = type;
24257 facet = NULL;
24258 do {
24259 found = 0;
24260 for (facetLink = tmpType->facetSet; facetLink != NULL;
24261 facetLink = facetLink->next) {
24262 if (facetLink->facet->type != XML_SCHEMA_FACET_PATTERN)
24263 continue;
24264 found = 1;
Daniel Veillarddee23482008-04-11 12:58:43 +000024265 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024266 * NOTE that for patterns, @value needs to be the
24267 * normalized vaule.
24268 */
24269 ret = xmlRegexpExec(facetLink->facet->regexp, value);
24270 if (ret == 1)
24271 break;
24272 else if (ret < 0) {
24273 AERROR_INT("xmlSchemaValidateFacets",
24274 "validating against a pattern facet");
24275 return (-1);
24276 } else {
Daniel Veillarddee23482008-04-11 12:58:43 +000024277 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024278 * Save the last non-validating facet.
24279 */
24280 facet = facetLink->facet;
24281 }
24282 }
24283 if (found && (ret != 1)) {
24284 ret = XML_SCHEMAV_CVC_PATTERN_VALID;
24285 if (fireErrors) {
24286 xmlSchemaFacetErr(actxt, ret, node,
24287 value, 0, type, facet, NULL, NULL, NULL);
24288 } else
24289 return (ret);
24290 if (error == 0)
24291 error = ret;
24292 break;
24293 }
24294 tmpType = tmpType->baseType;
24295 } while ((tmpType != NULL) && (tmpType->type != XML_SCHEMA_TYPE_BASIC));
24296 }
24297
24298 return (error);
24299}
Daniel Veillarddee23482008-04-11 12:58:43 +000024300
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024301static xmlChar *
24302xmlSchemaNormalizeValue(xmlSchemaTypePtr type,
24303 const xmlChar *value)
24304{
Daniel Veillarddee23482008-04-11 12:58:43 +000024305 switch (xmlSchemaGetWhiteSpaceFacetValue(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024306 case XML_SCHEMA_WHITESPACE_COLLAPSE:
24307 return (xmlSchemaCollapseString(value));
24308 case XML_SCHEMA_WHITESPACE_REPLACE:
24309 return (xmlSchemaWhiteSpaceReplace(value));
24310 default:
24311 return (NULL);
24312 }
24313}
24314
24315static int
24316xmlSchemaValidateQName(xmlSchemaValidCtxtPtr vctxt,
24317 const xmlChar *value,
24318 xmlSchemaValPtr *val,
24319 int valNeeded)
24320{
24321 int ret;
24322 const xmlChar *nsName;
24323 xmlChar *local, *prefix = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000024324
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024325 ret = xmlValidateQName(value, 1);
24326 if (ret != 0) {
24327 if (ret == -1) {
24328 VERROR_INT("xmlSchemaValidateQName",
24329 "calling xmlValidateQName()");
24330 return (-1);
24331 }
24332 return( XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1);
24333 }
24334 /*
24335 * NOTE: xmlSplitQName2 will always return a duplicated
24336 * strings.
24337 */
24338 local = xmlSplitQName2(value, &prefix);
24339 if (local == NULL)
24340 local = xmlStrdup(value);
24341 /*
24342 * OPTIMIZE TODO: Use flags for:
24343 * - is there any namespace binding?
24344 * - is there a default namespace?
24345 */
24346 nsName = xmlSchemaLookupNamespace(vctxt, prefix);
Daniel Veillarddee23482008-04-11 12:58:43 +000024347
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024348 if (prefix != NULL) {
24349 xmlFree(prefix);
24350 /*
24351 * A namespace must be found if the prefix is
24352 * NOT NULL.
24353 */
24354 if (nsName == NULL) {
24355 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024356 xmlSchemaCustomErr(ACTXT_CAST vctxt, ret, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024357 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024358 "The QName value '%s' has no "
24359 "corresponding namespace declaration in "
24360 "scope", value, NULL);
24361 if (local != NULL)
24362 xmlFree(local);
24363 return (ret);
24364 }
24365 }
24366 if (valNeeded && val) {
24367 if (nsName != NULL)
24368 *val = xmlSchemaNewQNameValue(
24369 BAD_CAST xmlStrdup(nsName), BAD_CAST local);
24370 else
24371 *val = xmlSchemaNewQNameValue(NULL,
24372 BAD_CAST local);
24373 } else
24374 xmlFree(local);
24375 return (0);
24376}
24377
24378/*
24379* cvc-simple-type
24380*/
24381static int
24382xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
24383 xmlNodePtr node,
24384 xmlSchemaTypePtr type,
24385 const xmlChar *value,
24386 xmlSchemaValPtr *retVal,
24387 int fireErrors,
24388 int normalize,
24389 int isNormalized)
24390{
24391 int ret = 0, valNeeded = (retVal) ? 1 : 0;
24392 xmlSchemaValPtr val = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024393 /* xmlSchemaWhitespaceValueType ws; */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024394 xmlChar *normValue = NULL;
24395
24396#define NORMALIZE(atype) \
24397 if ((! isNormalized) && \
24398 (normalize || (type->flags & XML_SCHEMAS_TYPE_NORMVALUENEEDED))) { \
24399 normValue = xmlSchemaNormalizeValue(atype, value); \
24400 if (normValue != NULL) \
24401 value = normValue; \
24402 isNormalized = 1; \
24403 }
Daniel Veillarddee23482008-04-11 12:58:43 +000024404
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024405 if ((retVal != NULL) && (*retVal != NULL)) {
24406 xmlSchemaFreeValue(*retVal);
24407 *retVal = NULL;
24408 }
24409 /*
24410 * 3.14.4 Simple Type Definition Validation Rules
24411 * Validation Rule: String Valid
24412 */
24413 /*
24414 * 1 It is schema-valid with respect to that definition as defined
24415 * by Datatype Valid in [XML Schemas: Datatypes].
24416 */
24417 /*
24418 * 2.1 If The definition is ENTITY or is validly derived from ENTITY given
Rob Richardsc6947bb2008-06-29 15:04:41 +000024419 * the empty set, as defined in Type Derivation OK (Simple) (�3.14.6), then
24420 * the string must be a �declared entity name�.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024421 */
24422 /*
24423 * 2.2 If The definition is ENTITIES or is validly derived from ENTITIES
Rob Richardsc6947bb2008-06-29 15:04:41 +000024424 * given the empty set, as defined in Type Derivation OK (Simple) (�3.14.6),
24425 * then every whitespace-delimited substring of the string must be a �declared
24426 * entity name�.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024427 */
24428 /*
24429 * 2.3 otherwise no further condition applies.
24430 */
24431 if ((! valNeeded) && (type->flags & XML_SCHEMAS_TYPE_FACETSNEEDVALUE))
24432 valNeeded = 1;
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000024433 if (value == NULL)
24434 value = BAD_CAST "";
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024435 if (WXS_IS_ANY_SIMPLE_TYPE(type) || WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024436 xmlSchemaTypePtr biType; /* The built-in type. */
24437 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000024438 * SPEC (1.2.1) "if {variety} is �atomic� then the string must �match�
24439 * a literal in the �lexical space� of {base type definition}"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024440 */
24441 /*
24442 * Whitespace-normalize.
24443 */
24444 NORMALIZE(type);
24445 if (type->type != XML_SCHEMA_TYPE_BASIC) {
24446 /*
24447 * Get the built-in type.
24448 */
24449 biType = type->baseType;
24450 while ((biType != NULL) &&
24451 (biType->type != XML_SCHEMA_TYPE_BASIC))
24452 biType = biType->baseType;
24453
24454 if (biType == NULL) {
24455 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24456 "could not get the built-in type");
24457 goto internal_error;
24458 }
24459 } else
24460 biType = type;
24461 /*
24462 * NOTATIONs need to be processed here, since they need
24463 * to lookup in the hashtable of NOTATION declarations of the schema.
24464 */
Daniel Veillarddee23482008-04-11 12:58:43 +000024465 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
24466 switch (biType->builtInType) {
24467 case XML_SCHEMAS_NOTATION:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024468 ret = xmlSchemaValidateNotation(
24469 (xmlSchemaValidCtxtPtr) actxt,
24470 ((xmlSchemaValidCtxtPtr) actxt)->schema,
24471 NULL, value, &val, valNeeded);
24472 break;
24473 case XML_SCHEMAS_QNAME:
24474 ret = xmlSchemaValidateQName((xmlSchemaValidCtxtPtr) actxt,
24475 value, &val, valNeeded);
24476 break;
24477 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024478 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024479 if (valNeeded)
24480 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
Wayne Jensen99f7ac72009-08-07 21:27:44 +020024481 value, &val, node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024482 else
24483 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
Wayne Jensen99f7ac72009-08-07 21:27:44 +020024484 value, NULL, node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024485 break;
24486 }
Daniel Veillarddee23482008-04-11 12:58:43 +000024487 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
24488 switch (biType->builtInType) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024489 case XML_SCHEMAS_NOTATION:
24490 ret = xmlSchemaValidateNotation(NULL,
24491 ((xmlSchemaParserCtxtPtr) actxt)->schema, node,
24492 value, &val, valNeeded);
24493 break;
24494 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024495 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024496 if (valNeeded)
24497 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24498 value, &val, node);
24499 else
24500 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24501 value, NULL, node);
24502 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000024503 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024504 } else {
24505 /*
24506 * Validation via a public API is not implemented yet.
24507 */
24508 TODO
24509 goto internal_error;
24510 }
24511 if (ret != 0) {
24512 if (ret < 0) {
24513 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24514 "validating against a built-in type");
24515 goto internal_error;
24516 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024517 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024518 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24519 else
Daniel Veillarddee23482008-04-11 12:58:43 +000024520 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024521 }
24522 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24523 /*
24524 * Check facets.
24525 */
24526 ret = xmlSchemaValidateFacets(actxt, node, type,
24527 (xmlSchemaValType) biType->builtInType, value, val,
24528 0, fireErrors);
24529 if (ret != 0) {
24530 if (ret < 0) {
24531 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24532 "validating facets of atomic simple type");
24533 goto internal_error;
24534 }
Daniel Veillarddee23482008-04-11 12:58:43 +000024535 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024536 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24537 else
Daniel Veillarddee23482008-04-11 12:58:43 +000024538 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024539 }
24540 }
24541 if (fireErrors && (ret > 0))
24542 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024543 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024544
24545 xmlSchemaTypePtr itemType;
24546 const xmlChar *cur, *end;
24547 xmlChar *tmpValue = NULL;
24548 unsigned long len = 0;
24549 xmlSchemaValPtr prevVal = NULL, curVal = NULL;
Rob Richardsc6947bb2008-06-29 15:04:41 +000024550 /* 1.2.2 if {variety} is �list� then the string must be a sequence
24551 * of white space separated tokens, each of which �match�es a literal
24552 * in the �lexical space� of {item type definition}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024553 */
24554 /*
24555 * Note that XML_SCHEMAS_TYPE_NORMVALUENEEDED will be set if
24556 * the list type has an enum or pattern facet.
24557 */
24558 NORMALIZE(type);
24559 /*
24560 * VAL TODO: Optimize validation of empty values.
24561 * VAL TODO: We do not have computed values for lists.
24562 */
Daniel Veillarddee23482008-04-11 12:58:43 +000024563 itemType = WXS_LIST_ITEMTYPE(type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024564 cur = value;
24565 do {
24566 while (IS_BLANK_CH(*cur))
24567 cur++;
24568 end = cur;
24569 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
24570 end++;
24571 if (end == cur)
24572 break;
24573 tmpValue = xmlStrndup(cur, end - cur);
24574 len++;
24575
24576 if (valNeeded)
24577 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24578 tmpValue, &curVal, fireErrors, 0, 1);
24579 else
24580 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24581 tmpValue, NULL, fireErrors, 0, 1);
24582 FREE_AND_NULL(tmpValue);
24583 if (curVal != NULL) {
24584 /*
24585 * Add to list of computed values.
24586 */
24587 if (val == NULL)
24588 val = curVal;
24589 else
24590 xmlSchemaValueAppend(prevVal, curVal);
24591 prevVal = curVal;
24592 curVal = NULL;
24593 }
24594 if (ret != 0) {
24595 if (ret < 0) {
24596 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24597 "validating an item of list simple type");
24598 goto internal_error;
24599 }
24600 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24601 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000024602 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024603 cur = end;
24604 } while (*cur != 0);
24605 FREE_AND_NULL(tmpValue);
24606 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24607 /*
24608 * Apply facets (pattern, enumeration).
24609 */
24610 ret = xmlSchemaValidateFacets(actxt, node, type,
24611 XML_SCHEMAS_UNKNOWN, value, val,
24612 len, fireErrors);
24613 if (ret != 0) {
24614 if (ret < 0) {
24615 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24616 "validating facets of list simple type");
24617 goto internal_error;
24618 }
24619 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24620 }
24621 }
24622 if (fireErrors && (ret > 0)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000024623 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024624 * Report the normalized value.
24625 */
24626 normalize = 1;
24627 NORMALIZE(type);
24628 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24629 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024630 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024631 xmlSchemaTypeLinkPtr memberLink;
24632 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000024633 * TODO: For all datatypes �derived� by �union� whiteSpace does
24634 * not apply directly; however, the normalization behavior of �union�
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024635 * types is controlled by the value of whiteSpace on that one of the
Rob Richardsc6947bb2008-06-29 15:04:41 +000024636 * �memberTypes� against which the �union� is successfully validated.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024637 *
24638 * This means that the value is normalized by the first validating
24639 * member type, then the facets of the union type are applied. This
24640 * needs changing of the value!
24641 */
24642
24643 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000024644 * 1.2.3 if {variety} is �union� then the string must �match� a
24645 * literal in the �lexical space� of at least one member of
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024646 * {member type definitions}
24647 */
24648 memberLink = xmlSchemaGetUnionSimpleTypeMemberTypes(type);
24649 if (memberLink == NULL) {
24650 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24651 "union simple type has no member types");
24652 goto internal_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000024653 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024654 /*
24655 * Always normalize union type values, since we currently
24656 * cannot store the whitespace information with the value
24657 * itself; otherwise a later value-comparison would be
24658 * not possible.
24659 */
24660 while (memberLink != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000024661 if (valNeeded)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024662 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24663 memberLink->type, value, &val, 0, 1, 0);
24664 else
24665 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24666 memberLink->type, value, NULL, 0, 1, 0);
24667 if (ret <= 0)
24668 break;
24669 memberLink = memberLink->next;
24670 }
24671 if (ret != 0) {
24672 if (ret < 0) {
24673 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24674 "validating members of union simple type");
24675 goto internal_error;
24676 }
24677 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24678 }
24679 /*
24680 * Apply facets (pattern, enumeration).
24681 */
24682 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24683 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000024684 * The normalization behavior of �union� types is controlled by
24685 * the value of whiteSpace on that one of the �memberTypes�
24686 * against which the �union� is successfully validated.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024687 */
24688 NORMALIZE(memberLink->type);
24689 ret = xmlSchemaValidateFacets(actxt, node, type,
24690 XML_SCHEMAS_UNKNOWN, value, val,
24691 0, fireErrors);
24692 if (ret != 0) {
24693 if (ret < 0) {
24694 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24695 "validating facets of union simple type");
24696 goto internal_error;
24697 }
Daniel Veillarddee23482008-04-11 12:58:43 +000024698 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024699 }
24700 }
24701 if (fireErrors && (ret > 0))
24702 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24703 }
24704
24705 if (normValue != NULL)
24706 xmlFree(normValue);
24707 if (ret == 0) {
24708 if (retVal != NULL)
24709 *retVal = val;
24710 else if (val != NULL)
24711 xmlSchemaFreeValue(val);
24712 } else if (val != NULL)
24713 xmlSchemaFreeValue(val);
24714 return (ret);
24715internal_error:
24716 if (normValue != NULL)
24717 xmlFree(normValue);
24718 if (val != NULL)
24719 xmlSchemaFreeValue(val);
24720 return (-1);
24721}
24722
24723static int
24724xmlSchemaVExpandQName(xmlSchemaValidCtxtPtr vctxt,
24725 const xmlChar *value,
24726 const xmlChar **nsName,
24727 const xmlChar **localName)
24728{
24729 int ret = 0;
24730
24731 if ((nsName == NULL) || (localName == NULL))
24732 return (-1);
24733 *nsName = NULL;
24734 *localName = NULL;
24735
24736 ret = xmlValidateQName(value, 1);
24737 if (ret == -1)
24738 return (-1);
24739 if (ret > 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024740 xmlSchemaSimpleTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024741 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
24742 value, xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), 1);
24743 return (1);
24744 }
24745 {
24746 xmlChar *local = NULL;
24747 xmlChar *prefix;
24748
24749 /*
24750 * NOTE: xmlSplitQName2 will return a duplicated
24751 * string.
24752 */
24753 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024754 if (local == NULL)
24755 *localName = xmlDictLookup(vctxt->dict, value, -1);
24756 else {
24757 *localName = xmlDictLookup(vctxt->dict, local, -1);
24758 xmlFree(local);
24759 }
24760
24761 *nsName = xmlSchemaLookupNamespace(vctxt, prefix);
24762
24763 if (prefix != NULL) {
24764 xmlFree(prefix);
24765 /*
24766 * A namespace must be found if the prefix is NOT NULL.
24767 */
24768 if (*nsName == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024769 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024770 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024771 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024772 "The QName value '%s' has no "
24773 "corresponding namespace declaration in scope",
24774 value, NULL);
24775 return (2);
24776 }
24777 }
24778 }
24779 return (0);
24780}
24781
24782static int
24783xmlSchemaProcessXSIType(xmlSchemaValidCtxtPtr vctxt,
24784 xmlSchemaAttrInfoPtr iattr,
24785 xmlSchemaTypePtr *localType,
24786 xmlSchemaElementPtr elemDecl)
24787{
24788 int ret = 0;
24789 /*
24790 * cvc-elt (3.3.4) : (4)
24791 * AND
24792 * Schema-Validity Assessment (Element) (cvc-assess-elt)
24793 * (1.2.1.2.1) - (1.2.1.2.4)
24794 * Handle 'xsi:type'.
24795 */
24796 if (localType == NULL)
24797 return (-1);
24798 *localType = NULL;
24799 if (iattr == NULL)
24800 return (0);
24801 else {
24802 const xmlChar *nsName = NULL, *local = NULL;
24803 /*
24804 * TODO: We should report a *warning* that the type was overriden
24805 * by the instance.
24806 */
24807 ACTIVATE_ATTRIBUTE(iattr);
24808 /*
24809 * (cvc-elt) (3.3.4) : (4.1)
24810 * (cvc-assess-elt) (1.2.1.2.2)
24811 */
24812 ret = xmlSchemaVExpandQName(vctxt, iattr->value,
24813 &nsName, &local);
24814 if (ret != 0) {
24815 if (ret < 0) {
24816 VERROR_INT("xmlSchemaValidateElementByDeclaration",
24817 "calling xmlSchemaQNameExpand() to validate the "
24818 "attribute 'xsi:type'");
24819 goto internal_error;
24820 }
24821 goto exit;
24822 }
24823 /*
24824 * (cvc-elt) (3.3.4) : (4.2)
24825 * (cvc-assess-elt) (1.2.1.2.3)
24826 */
24827 *localType = xmlSchemaGetType(vctxt->schema, local, nsName);
24828 if (*localType == NULL) {
24829 xmlChar *str = NULL;
24830
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024831 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024832 XML_SCHEMAV_CVC_ELT_4_2, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024833 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024834 "The QName value '%s' of the xsi:type attribute does not "
24835 "resolve to a type definition",
24836 xmlSchemaFormatQName(&str, nsName, local), NULL);
24837 FREE_AND_NULL(str);
24838 ret = vctxt->err;
24839 goto exit;
24840 }
24841 if (elemDecl != NULL) {
24842 int set = 0;
24843
24844 /*
24845 * SPEC cvc-elt (3.3.4) : (4.3) (Type Derivation OK)
Rob Richardsc6947bb2008-06-29 15:04:41 +000024846 * "The �local type definition� must be validly
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024847 * derived from the {type definition} given the union of
24848 * the {disallowed substitutions} and the {type definition}'s
24849 * {prohibited substitutions}, as defined in
Rob Richardsc6947bb2008-06-29 15:04:41 +000024850 * Type Derivation OK (Complex) (�3.4.6)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024851 * (if it is a complex type definition),
24852 * or given {disallowed substitutions} as defined in Type
Rob Richardsc6947bb2008-06-29 15:04:41 +000024853 * Derivation OK (Simple) (�3.14.6) (if it is a simple type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024854 * definition)."
24855 *
24856 * {disallowed substitutions}: the "block" on the element decl.
24857 * {prohibited substitutions}: the "block" on the type def.
24858 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024859 /*
24860 * OPTIMIZE TODO: We could map types already evaluated
24861 * to be validly derived from other types to avoid checking
24862 * this over and over for the same types.
24863 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024864 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) ||
24865 (elemDecl->subtypes->flags &
24866 XML_SCHEMAS_TYPE_BLOCK_EXTENSION))
24867 set |= SUBSET_EXTENSION;
24868
24869 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) ||
24870 (elemDecl->subtypes->flags &
24871 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION))
24872 set |= SUBSET_RESTRICTION;
24873
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024874 /*
24875 * REMOVED and CHANGED since this produced a parser context
24876 * which adds to the string dict of the schema. So this would
24877 * change the schema and we don't want this. We don't need
24878 * the parser context anymore.
24879 *
24880 * if ((vctxt->pctxt == NULL) &&
24881 * (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
24882 * return (-1);
24883 */
24884
24885 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST vctxt, *localType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024886 elemDecl->subtypes, set) != 0) {
24887 xmlChar *str = NULL;
24888
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024889 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024890 XML_SCHEMAV_CVC_ELT_4_3, NULL, NULL,
24891 "The type definition '%s', specified by xsi:type, is "
24892 "blocked or not validly derived from the type definition "
24893 "of the element declaration",
24894 xmlSchemaFormatQName(&str,
24895 (*localType)->targetNamespace,
24896 (*localType)->name),
24897 NULL);
24898 FREE_AND_NULL(str);
24899 ret = vctxt->err;
24900 *localType = NULL;
24901 }
24902 }
24903 }
24904exit:
24905 ACTIVATE_ELEM;
24906 return (ret);
24907internal_error:
24908 ACTIVATE_ELEM;
24909 return (-1);
24910}
24911
24912static int
24913xmlSchemaValidateElemDecl(xmlSchemaValidCtxtPtr vctxt)
24914{
24915 xmlSchemaElementPtr elemDecl = vctxt->inode->decl;
Daniel Veillard14b56432006-03-09 18:41:40 +000024916 xmlSchemaTypePtr actualType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024917
24918 /*
24919 * cvc-elt (3.3.4) : 1
24920 */
24921 if (elemDecl == NULL) {
24922 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL,
24923 "No matching declaration available");
24924 return (vctxt->err);
24925 }
Daniel Veillard14b56432006-03-09 18:41:40 +000024926 actualType = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024927 /*
24928 * cvc-elt (3.3.4) : 2
24929 */
24930 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) {
24931 VERROR(XML_SCHEMAV_CVC_ELT_2, NULL,
24932 "The element declaration is abstract");
24933 return (vctxt->err);
24934 }
24935 if (actualType == NULL) {
24936 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24937 "The type definition is absent");
24938 return (XML_SCHEMAV_CVC_TYPE_1);
24939 }
24940 if (vctxt->nbAttrInfos != 0) {
24941 int ret;
24942 xmlSchemaAttrInfoPtr iattr;
24943 /*
24944 * cvc-elt (3.3.4) : 3
24945 * Handle 'xsi:nil'.
24946 */
24947 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24948 XML_SCHEMA_ATTR_INFO_META_XSI_NIL);
24949 if (iattr) {
24950 ACTIVATE_ATTRIBUTE(iattr);
24951 /*
24952 * Validate the value.
24953 */
24954 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024955 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024956 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
24957 iattr->value, &(iattr->val), 1, 0, 0);
24958 ACTIVATE_ELEM;
24959 if (ret < 0) {
24960 VERROR_INT("xmlSchemaValidateElemDecl",
24961 "calling xmlSchemaVCheckCVCSimpleType() to "
24962 "validate the attribute 'xsi:nil'");
24963 return (-1);
24964 }
24965 if (ret == 0) {
24966 if ((elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) {
24967 /*
24968 * cvc-elt (3.3.4) : 3.1
24969 */
24970 VERROR(XML_SCHEMAV_CVC_ELT_3_1, NULL,
24971 "The element is not 'nillable'");
24972 /* Does not return an error on purpose. */
24973 } else {
24974 if (xmlSchemaValueGetAsBoolean(iattr->val)) {
24975 /*
24976 * cvc-elt (3.3.4) : 3.2.2
24977 */
24978 if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) &&
24979 (elemDecl->value != NULL)) {
24980 VERROR(XML_SCHEMAV_CVC_ELT_3_2_2, NULL,
24981 "The element cannot be 'nilled' because "
24982 "there is a fixed value constraint defined "
24983 "for it");
24984 /* Does not return an error on purpose. */
24985 } else
24986 vctxt->inode->flags |=
24987 XML_SCHEMA_ELEM_INFO_NILLED;
24988 }
24989 }
24990 }
24991 }
24992 /*
24993 * cvc-elt (3.3.4) : 4
24994 * Handle 'xsi:type'.
24995 */
24996 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24997 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
24998 if (iattr) {
24999 xmlSchemaTypePtr localType = NULL;
25000
25001 ret = xmlSchemaProcessXSIType(vctxt, iattr, &localType,
25002 elemDecl);
25003 if (ret != 0) {
25004 if (ret == -1) {
25005 VERROR_INT("xmlSchemaValidateElemDecl",
25006 "calling xmlSchemaProcessXSIType() to "
25007 "process the attribute 'xsi:type'");
25008 return (-1);
25009 }
25010 /* Does not return an error on purpose. */
25011 }
25012 if (localType != NULL) {
25013 vctxt->inode->flags |= XML_SCHEMA_ELEM_INFO_LOCAL_TYPE;
25014 actualType = localType;
25015 }
25016 }
25017 }
25018 /*
25019 * IDC: Register identity-constraint XPath matchers.
25020 */
25021 if ((elemDecl->idcs != NULL) &&
25022 (xmlSchemaIDCRegisterMatchers(vctxt, elemDecl) == -1))
25023 return (-1);
25024 /*
25025 * No actual type definition.
25026 */
25027 if (actualType == NULL) {
25028 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
25029 "The type definition is absent");
25030 return (XML_SCHEMAV_CVC_TYPE_1);
25031 }
25032 /*
25033 * Remember the actual type definition.
25034 */
25035 vctxt->inode->typeDef = actualType;
25036
25037 return (0);
25038}
25039
25040static int
25041xmlSchemaVAttributesSimple(xmlSchemaValidCtxtPtr vctxt)
25042{
25043 xmlSchemaAttrInfoPtr iattr;
25044 int ret = 0, i;
25045
25046 /*
25047 * SPEC cvc-type (3.1.1)
25048 * "The attributes of must be empty, excepting those whose namespace
25049 * name is identical to http://www.w3.org/2001/XMLSchema-instance and
25050 * whose local name is one of type, nil, schemaLocation or
25051 * noNamespaceSchemaLocation."
25052 */
25053 if (vctxt->nbAttrInfos == 0)
25054 return (0);
25055 for (i = 0; i < vctxt->nbAttrInfos; i++) {
25056 iattr = vctxt->attrInfos[i];
25057 if (! iattr->metaType) {
25058 ACTIVATE_ATTRIBUTE(iattr)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025059 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025060 XML_SCHEMAV_CVC_TYPE_3_1_1, iattr, NULL);
25061 ret = XML_SCHEMAV_CVC_TYPE_3_1_1;
25062 }
25063 }
25064 ACTIVATE_ELEM
25065 return (ret);
25066}
25067
25068/*
25069* Cleanup currently used attribute infos.
25070*/
25071static void
25072xmlSchemaClearAttrInfos(xmlSchemaValidCtxtPtr vctxt)
25073{
25074 int i;
25075 xmlSchemaAttrInfoPtr attr;
25076
25077 if (vctxt->nbAttrInfos == 0)
25078 return;
25079 for (i = 0; i < vctxt->nbAttrInfos; i++) {
25080 attr = vctxt->attrInfos[i];
25081 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
25082 if (attr->localName != NULL)
25083 xmlFree((xmlChar *) attr->localName);
25084 if (attr->nsName != NULL)
25085 xmlFree((xmlChar *) attr->nsName);
25086 }
25087 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
25088 if (attr->value != NULL)
25089 xmlFree((xmlChar *) attr->value);
25090 }
25091 if (attr->val != NULL) {
25092 xmlSchemaFreeValue(attr->val);
25093 attr->val = NULL;
25094 }
25095 memset(attr, 0, sizeof(xmlSchemaAttrInfo));
25096 }
25097 vctxt->nbAttrInfos = 0;
25098}
25099
25100/*
25101* 3.4.4 Complex Type Definition Validation Rules
25102* Element Locally Valid (Complex Type) (cvc-complex-type)
25103* 3.2.4 Attribute Declaration Validation Rules
25104* Validation Rule: Attribute Locally Valid (cvc-attribute)
25105* Attribute Locally Valid (Use) (cvc-au)
25106*
25107* Only "assessed" attribute information items will be visible to
25108* IDCs. I.e. not "lax" (without declaration) and "skip" wild attributes.
25109*/
25110static int
25111xmlSchemaVAttributesComplex(xmlSchemaValidCtxtPtr vctxt)
25112{
Daniel Veillarddee23482008-04-11 12:58:43 +000025113 xmlSchemaTypePtr type = vctxt->inode->typeDef;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025114 xmlSchemaItemListPtr attrUseList;
25115 xmlSchemaAttributeUsePtr attrUse = NULL;
25116 xmlSchemaAttributePtr attrDecl = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000025117 xmlSchemaAttrInfoPtr iattr, tmpiattr;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025118 int i, j, found, nbAttrs, nbUses;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025119 int xpathRes = 0, res, wildIDs = 0, fixed;
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025120 xmlNodePtr defAttrOwnerElem = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025121
25122 /*
25123 * SPEC (cvc-attribute)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025124 * (1) "The declaration must not be �absent� (see Missing
25125 * Sub-components (�5.3) for how this can fail to be
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025126 * the case)."
25127 * (2) "Its {type definition} must not be absent."
25128 *
25129 * NOTE (1) + (2): This is not handled here, since we currently do not
25130 * allow validation against schemas which have missing sub-components.
25131 *
25132 * SPEC (cvc-complex-type)
25133 * (3) "For each attribute information item in the element information
25134 * item's [attributes] excepting those whose [namespace name] is
25135 * identical to http://www.w3.org/2001/XMLSchema-instance and whose
25136 * [local name] is one of type, nil, schemaLocation or
25137 * noNamespaceSchemaLocation, the appropriate case among the following
25138 * must be true:
25139 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025140 */
25141 attrUseList = (xmlSchemaItemListPtr) type->attrUses;
25142 /*
25143 * @nbAttrs is the number of attributes present in the instance.
25144 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025145 nbAttrs = vctxt->nbAttrInfos;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025146 if (attrUseList != NULL)
25147 nbUses = attrUseList->nbItems;
25148 else
25149 nbUses = 0;
25150 for (i = 0; i < nbUses; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025151 found = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025152 attrUse = attrUseList->items[i];
25153 attrDecl = WXS_ATTRUSE_DECL(attrUse);
25154 for (j = 0; j < nbAttrs; j++) {
25155 iattr = vctxt->attrInfos[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025156 /*
25157 * SPEC (cvc-complex-type) (3)
25158 * Skip meta attributes.
25159 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025160 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025161 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025162 if (iattr->localName[0] != attrDecl->name[0])
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025163 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025164 if (!xmlStrEqual(iattr->localName, attrDecl->name))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025165 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025166 if (!xmlStrEqual(iattr->nsName, attrDecl->targetNamespace))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025167 continue;
25168 found = 1;
25169 /*
25170 * SPEC (cvc-complex-type)
25171 * (3.1) "If there is among the {attribute uses} an attribute
25172 * use with an {attribute declaration} whose {name} matches
25173 * the attribute information item's [local name] and whose
25174 * {target namespace} is identical to the attribute information
Rob Richardsc6947bb2008-06-29 15:04:41 +000025175 * item's [namespace name] (where an �absent� {target namespace}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025176 * is taken to be identical to a [namespace name] with no value),
Rob Richardsc6947bb2008-06-29 15:04:41 +000025177 * then the attribute information must be �valid� with respect
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025178 * to that attribute use as per Attribute Locally Valid (Use)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025179 * (�3.5.4). In this case the {attribute declaration} of that
25180 * attribute use is the �context-determined declaration� for the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025181 * attribute information item with respect to Schema-Validity
Rob Richardsc6947bb2008-06-29 15:04:41 +000025182 * Assessment (Attribute) (�3.2.4) and
25183 * Assessment Outcome (Attribute) (�3.2.5).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025184 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025185 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
25186 iattr->use = attrUse;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025187 /*
25188 * Context-determined declaration.
25189 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025190 iattr->decl = attrDecl;
25191 iattr->typeDef = attrDecl->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025192 break;
25193 }
25194
25195 if (found)
25196 continue;
25197
25198 if (attrUse->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED) {
25199 /*
25200 * Handle non-existent, required attributes.
25201 *
25202 * SPEC (cvc-complex-type)
25203 * (4) "The {attribute declaration} of each attribute use in
25204 * the {attribute uses} whose {required} is true matches one
25205 * of the attribute information items in the element information
25206 * item's [attributes] as per clause 3.1 above."
25207 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025208 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
25209 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025210 VERROR_INT(
25211 "xmlSchemaVAttributesComplex",
25212 "calling xmlSchemaGetFreshAttrInfo()");
25213 return (-1);
25214 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025215 tmpiattr->state = XML_SCHEMAS_ATTR_ERR_MISSING;
25216 tmpiattr->use = attrUse;
Daniel Veillarddee23482008-04-11 12:58:43 +000025217 tmpiattr->decl = attrDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025218 } else if ((attrUse->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
25219 ((attrUse->defValue != NULL) ||
25220 (attrDecl->defValue != NULL))) {
25221 /*
25222 * Handle non-existent, optional, default/fixed attributes.
25223 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025224 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
25225 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025226 VERROR_INT(
25227 "xmlSchemaVAttributesComplex",
25228 "calling xmlSchemaGetFreshAttrInfo()");
25229 return (-1);
25230 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025231 tmpiattr->state = XML_SCHEMAS_ATTR_DEFAULT;
25232 tmpiattr->use = attrUse;
25233 tmpiattr->decl = attrDecl;
25234 tmpiattr->typeDef = attrDecl->subtypes;
25235 tmpiattr->localName = attrDecl->name;
25236 tmpiattr->nsName = attrDecl->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025237 }
25238 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025239
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025240 if (vctxt->nbAttrInfos == 0)
25241 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025242 nbUses = vctxt->nbAttrInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025243 /*
25244 * Validate against the wildcard.
25245 */
25246 if (type->attributeWildcard != NULL) {
25247 /*
25248 * SPEC (cvc-complex-type)
25249 * (3.2.1) "There must be an {attribute wildcard}."
25250 */
25251 for (i = 0; i < nbAttrs; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025252 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025253 /*
25254 * SPEC (cvc-complex-type) (3)
25255 * Skip meta attributes.
25256 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025257 if (iattr->state != XML_SCHEMAS_ATTR_UNKNOWN)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025258 continue;
25259 /*
25260 * SPEC (cvc-complex-type)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025261 * (3.2.2) "The attribute information item must be �valid� with
25262 * respect to it as defined in Item Valid (Wildcard) (�3.10.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025263 *
25264 * SPEC Item Valid (Wildcard) (cvc-wildcard)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025265 * "... its [namespace name] must be �valid� with respect to
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025266 * the wildcard constraint, as defined in Wildcard allows
Rob Richardsc6947bb2008-06-29 15:04:41 +000025267 * Namespace Name (�3.10.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025268 */
25269 if (xmlSchemaCheckCVCWildcardNamespace(type->attributeWildcard,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025270 iattr->nsName) == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025271 /*
25272 * Handle processContents.
25273 *
25274 * SPEC (cvc-wildcard):
25275 * processContents | context-determined declaration:
25276 * "strict" "mustFind"
25277 * "lax" "none"
25278 * "skip" "skip"
25279 */
25280 if (type->attributeWildcard->processContents ==
25281 XML_SCHEMAS_ANY_SKIP) {
25282 /*
25283 * context-determined declaration = "skip"
25284 *
25285 * SPEC PSVI Assessment Outcome (Attribute)
25286 * [validity] = "notKnown"
25287 * [validation attempted] = "none"
25288 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025289 iattr->state = XML_SCHEMAS_ATTR_WILD_SKIP;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025290 continue;
25291 }
25292 /*
25293 * Find an attribute declaration.
25294 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025295 iattr->decl = xmlSchemaGetAttributeDecl(vctxt->schema,
25296 iattr->localName, iattr->nsName);
25297 if (iattr->decl != NULL) {
25298 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025299 /*
25300 * SPEC (cvc-complex-type)
25301 * (5) "Let [Definition:] the wild IDs be the set of
25302 * all attribute information item to which clause 3.2
Rob Richardsc6947bb2008-06-29 15:04:41 +000025303 * applied and whose �validation� resulted in a
25304 * �context-determined declaration� of mustFind or no
25305 * �context-determined declaration� at all, and whose
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025306 * [local name] and [namespace name] resolve (as
Rob Richardsc6947bb2008-06-29 15:04:41 +000025307 * defined by QName resolution (Instance) (�3.15.4)) to
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025308 * an attribute declaration whose {type definition} is
25309 * or is derived from ID. Then all of the following
25310 * must be true:"
25311 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025312 iattr->typeDef = WXS_ATTR_TYPEDEF(iattr->decl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025313 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025314 iattr->typeDef, XML_SCHEMAS_ID)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025315 /*
25316 * SPEC (5.1) "There must be no more than one
Rob Richardsc6947bb2008-06-29 15:04:41 +000025317 * item in �wild IDs�."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025318 */
25319 if (wildIDs != 0) {
25320 /* VAL TODO */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025321 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025322 TODO
25323 continue;
25324 }
25325 wildIDs++;
25326 /*
25327 * SPEC (cvc-complex-type)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025328 * (5.2) "If �wild IDs� is non-empty, there must not
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025329 * be any attribute uses among the {attribute uses}
25330 * whose {attribute declaration}'s {type definition}
25331 * is or is derived from ID."
25332 */
Daniel Veillard2cdd17e2009-07-27 21:16:13 +020025333 if (attrUseList != NULL) {
25334 for (j = 0; j < attrUseList->nbItems; j++) {
25335 if (xmlSchemaIsDerivedFromBuiltInType(
25336 WXS_ATTRUSE_TYPEDEF(attrUseList->items[j]),
25337 XML_SCHEMAS_ID)) {
25338 /* URGENT VAL TODO: implement */
25339 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID;
25340 TODO
25341 break;
25342 }
25343 }
25344 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025345 }
25346 } else if (type->attributeWildcard->processContents ==
25347 XML_SCHEMAS_ANY_LAX) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025348 iattr->state = XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025349 /*
25350 * SPEC PSVI Assessment Outcome (Attribute)
25351 * [validity] = "notKnown"
25352 * [validation attempted] = "none"
25353 */
25354 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025355 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025356 }
25357 }
25358 }
25359 }
25360
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025361 if (vctxt->nbAttrInfos == 0)
25362 return (0);
25363
25364 /*
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025365 * Get the owner element; needed for creation of default attributes.
25366 * This fixes bug #341337, reported by David Grohmann.
25367 */
25368 if (vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) {
25369 xmlSchemaNodeInfoPtr ielem = vctxt->elemInfos[vctxt->depth];
25370 if (ielem && ielem->node && ielem->node->doc)
25371 defAttrOwnerElem = ielem->node;
25372 }
25373 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025374 * Validate values, create default attributes, evaluate IDCs.
25375 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025376 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025377 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025378 /*
25379 * VAL TODO: Note that we won't try to resolve IDCs to
25380 * "lax" and "skip" validated attributes. Check what to
25381 * do in this case.
25382 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025383 if ((iattr->state != XML_SCHEMAS_ATTR_ASSESSED) &&
25384 (iattr->state != XML_SCHEMAS_ATTR_DEFAULT))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025385 continue;
25386 /*
25387 * VAL TODO: What to do if the type definition is missing?
25388 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025389 if (iattr->typeDef == NULL) {
25390 iattr->state = XML_SCHEMAS_ATTR_ERR_NO_TYPE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025391 continue;
25392 }
25393
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025394 ACTIVATE_ATTRIBUTE(iattr);
Kasimier T. Buchcik828f6542005-06-09 11:23:39 +000025395 fixed = 0;
25396 xpathRes = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025397
25398 if (vctxt->xpathStates != NULL) {
25399 /*
25400 * Evaluate IDCs.
25401 */
25402 xpathRes = xmlSchemaXPathEvaluate(vctxt,
25403 XML_ATTRIBUTE_NODE);
25404 if (xpathRes == -1) {
25405 VERROR_INT("xmlSchemaVAttributesComplex",
25406 "calling xmlSchemaXPathEvaluate()");
25407 goto internal_error;
25408 }
25409 }
25410
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025411 if (iattr->state == XML_SCHEMAS_ATTR_DEFAULT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025412 /*
25413 * Default/fixed attributes.
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025414 * We need the value only if we need to resolve IDCs or
25415 * will create default attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025416 */
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025417 if ((xpathRes) || (defAttrOwnerElem)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025418 if (iattr->use->defValue != NULL) {
25419 iattr->value = (xmlChar *) iattr->use->defValue;
25420 iattr->val = iattr->use->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025421 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025422 iattr->value = (xmlChar *) iattr->decl->defValue;
25423 iattr->val = iattr->decl->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025424 }
25425 /*
25426 * IDCs will consume the precomputed default value,
25427 * so we need to clone it.
25428 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025429 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025430 VERROR_INT("xmlSchemaVAttributesComplex",
25431 "default/fixed value on an attribute use was "
25432 "not precomputed");
25433 goto internal_error;
25434 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025435 iattr->val = xmlSchemaCopyValue(iattr->val);
25436 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025437 VERROR_INT("xmlSchemaVAttributesComplex",
25438 "calling xmlSchemaCopyValue()");
25439 goto internal_error;
25440 }
25441 }
25442 /*
25443 * PSVI: Add the default attribute to the current element.
25444 * VAL TODO: Should we use the *normalized* value? This currently
25445 * uses the *initial* value.
25446 */
Daniel Veillarddee23482008-04-11 12:58:43 +000025447
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025448 if (defAttrOwnerElem) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025449 xmlChar *normValue;
25450 const xmlChar *value;
25451
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025452 value = iattr->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025453 /*
25454 * Normalize the value.
25455 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025456 normValue = xmlSchemaNormalizeValue(iattr->typeDef,
25457 iattr->value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025458 if (normValue != NULL)
25459 value = BAD_CAST normValue;
25460
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025461 if (iattr->nsName == NULL) {
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025462 if (xmlNewProp(defAttrOwnerElem,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025463 iattr->localName, value) == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025464 VERROR_INT("xmlSchemaVAttributesComplex",
25465 "callling xmlNewProp()");
25466 if (normValue != NULL)
25467 xmlFree(normValue);
25468 goto internal_error;
25469 }
25470 } else {
25471 xmlNsPtr ns;
25472
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025473 ns = xmlSearchNsByHref(defAttrOwnerElem->doc,
25474 defAttrOwnerElem, iattr->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025475 if (ns == NULL) {
25476 xmlChar prefix[12];
25477 int counter = 0;
25478
25479 /*
25480 * Create a namespace declaration on the validation
25481 * root node if no namespace declaration is in scope.
25482 */
25483 do {
25484 snprintf((char *) prefix, 12, "p%d", counter++);
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025485 ns = xmlSearchNs(defAttrOwnerElem->doc,
25486 defAttrOwnerElem, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025487 if (counter > 1000) {
25488 VERROR_INT(
25489 "xmlSchemaVAttributesComplex",
25490 "could not compute a ns prefix for a "
25491 "default/fixed attribute");
25492 if (normValue != NULL)
25493 xmlFree(normValue);
25494 goto internal_error;
25495 }
25496 } while (ns != NULL);
25497 ns = xmlNewNs(vctxt->validationRoot,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025498 iattr->nsName, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025499 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025500 /*
25501 * TODO:
25502 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0406.html
25503 * If we have QNames: do we need to ensure there's a
25504 * prefix defined for the QName?
25505 */
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025506 xmlNewNsProp(defAttrOwnerElem, ns, iattr->localName, value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025507 }
25508 if (normValue != NULL)
25509 xmlFree(normValue);
25510 }
25511 /*
25512 * Go directly to IDC evaluation.
25513 */
25514 goto eval_idcs;
25515 }
25516 /*
25517 * Validate the value.
25518 */
25519 if (vctxt->value != NULL) {
25520 /*
25521 * Free last computed value; just for safety reasons.
25522 */
25523 xmlSchemaFreeValue(vctxt->value);
25524 vctxt->value = NULL;
25525 }
25526 /*
25527 * Note that the attribute *use* can be unavailable, if
25528 * the attribute was a wild attribute.
25529 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025530 if ((iattr->decl->flags & XML_SCHEMAS_ATTR_FIXED) ||
25531 ((iattr->use != NULL) &&
25532 (iattr->use->flags & XML_SCHEMAS_ATTR_FIXED)))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025533 fixed = 1;
25534 else
25535 fixed = 0;
25536 /*
25537 * SPEC (cvc-attribute)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025538 * (3) "The item's �normalized value� must be locally �valid�
Daniel Veillarddee23482008-04-11 12:58:43 +000025539 * with respect to that {type definition} as per
Rob Richardsc6947bb2008-06-29 15:04:41 +000025540 * String Valid (�3.14.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025541 *
25542 * VAL TODO: Do we already have the
25543 * "normalized attribute value" here?
25544 */
25545 if (xpathRes || fixed) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025546 iattr->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025547 /*
25548 * Request a computed value.
25549 */
25550 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025551 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025552 iattr->node, iattr->typeDef, iattr->value, &(iattr->val),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025553 1, 1, 0);
25554 } else {
25555 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025556 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025557 iattr->node, iattr->typeDef, iattr->value, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025558 1, 0, 0);
25559 }
Daniel Veillarddee23482008-04-11 12:58:43 +000025560
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025561 if (res != 0) {
25562 if (res == -1) {
25563 VERROR_INT("xmlSchemaVAttributesComplex",
25564 "calling xmlSchemaStreamValidateSimpleTypeValue()");
25565 goto internal_error;
25566 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025567 iattr->state = XML_SCHEMAS_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025568 /*
25569 * SPEC PSVI Assessment Outcome (Attribute)
25570 * [validity] = "invalid"
25571 */
25572 goto eval_idcs;
25573 }
25574
Daniel Veillarddee23482008-04-11 12:58:43 +000025575 if (fixed) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025576 /*
25577 * SPEC Attribute Locally Valid (Use) (cvc-au)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025578 * "For an attribute information item to be�valid�
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025579 * with respect to an attribute use its *normalized*
Rob Richardsc6947bb2008-06-29 15:04:41 +000025580 * value� must match the *canonical* lexical
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025581 * representation of the attribute use's {value
25582 * constraint}value, if it is present and fixed."
25583 *
25584 * VAL TODO: The requirement for the *canonical* value
25585 * will be removed in XML Schema 1.1.
25586 */
25587 /*
25588 * SPEC Attribute Locally Valid (cvc-attribute)
Rob Richardsc6947bb2008-06-29 15:04:41 +000025589 * (4) "The item's *actual* value� must match the *value* of
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025590 * the {value constraint}, if it is present and fixed."
25591 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025592 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025593 /* VAL TODO: A value was not precomputed. */
25594 TODO
25595 goto eval_idcs;
25596 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025597 if ((iattr->use != NULL) &&
25598 (iattr->use->defValue != NULL)) {
25599 if (iattr->use->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025600 /* VAL TODO: A default value was not precomputed. */
25601 TODO
25602 goto eval_idcs;
25603 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025604 iattr->vcValue = iattr->use->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025605 /*
25606 if (xmlSchemaCompareValuesWhtsp(attr->val,
25607 (xmlSchemaWhitespaceValueType) ws,
25608 attr->use->defVal,
25609 (xmlSchemaWhitespaceValueType) ws) != 0) {
25610 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025611 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->use->defVal))
25612 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025613 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025614 if (iattr->decl->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025615 /* VAL TODO: A default value was not precomputed. */
25616 TODO
25617 goto eval_idcs;
25618 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025619 iattr->vcValue = iattr->decl->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025620 /*
25621 if (xmlSchemaCompareValuesWhtsp(attr->val,
25622 (xmlSchemaWhitespaceValueType) ws,
25623 attrDecl->defVal,
25624 (xmlSchemaWhitespaceValueType) ws) != 0) {
25625 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025626 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->decl->defVal))
25627 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025628 }
25629 /*
25630 * [validity] = "valid"
25631 */
25632 }
25633eval_idcs:
25634 /*
25635 * Evaluate IDCs.
25636 */
25637 if (xpathRes) {
25638 if (xmlSchemaXPathProcessHistory(vctxt,
25639 vctxt->depth +1) == -1) {
25640 VERROR_INT("xmlSchemaVAttributesComplex",
25641 "calling xmlSchemaXPathEvaluate()");
25642 goto internal_error;
25643 }
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000025644 } else if (vctxt->xpathStates != NULL)
25645 xmlSchemaXPathPop(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025646 }
25647
25648 /*
25649 * Report errors.
25650 */
25651 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025652 iattr = vctxt->attrInfos[i];
25653 if ((iattr->state == XML_SCHEMAS_ATTR_META) ||
25654 (iattr->state == XML_SCHEMAS_ATTR_ASSESSED) ||
25655 (iattr->state == XML_SCHEMAS_ATTR_WILD_SKIP) ||
25656 (iattr->state == XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025657 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025658 ACTIVATE_ATTRIBUTE(iattr);
25659 switch (iattr->state) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025660 case XML_SCHEMAS_ATTR_ERR_MISSING: {
25661 xmlChar *str = NULL;
25662 ACTIVATE_ELEM;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025663 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025664 XML_SCHEMAV_CVC_COMPLEX_TYPE_4, NULL, NULL,
25665 "The attribute '%s' is required but missing",
25666 xmlSchemaFormatQName(&str,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025667 iattr->decl->targetNamespace,
25668 iattr->decl->name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025669 NULL);
25670 FREE_AND_NULL(str)
25671 break;
25672 }
25673 case XML_SCHEMAS_ATTR_ERR_NO_TYPE:
25674 VERROR(XML_SCHEMAV_CVC_ATTRIBUTE_2, NULL,
25675 "The type definition is absent");
25676 break;
25677 case XML_SCHEMAS_ATTR_ERR_FIXED_VALUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025678 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025679 XML_SCHEMAV_CVC_AU, NULL, NULL,
25680 "The value '%s' does not match the fixed "
Daniel Veillarddee23482008-04-11 12:58:43 +000025681 "value constraint '%s'",
25682 iattr->value, iattr->vcValue);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025683 break;
25684 case XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL:
25685 VERROR(XML_SCHEMAV_CVC_WILDCARD, NULL,
25686 "No matching global attribute declaration available, but "
25687 "demanded by the strict wildcard");
25688 break;
25689 case XML_SCHEMAS_ATTR_UNKNOWN:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025690 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025691 break;
25692 /*
25693 * MAYBE VAL TODO: One might report different error messages
25694 * for the following errors.
25695 */
25696 if (type->attributeWildcard == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025697 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025698 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_1, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025699 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025700 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025701 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_2, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025702 }
25703 break;
25704 default:
25705 break;
25706 }
25707 }
25708
25709 ACTIVATE_ELEM;
25710 return (0);
25711internal_error:
25712 ACTIVATE_ELEM;
25713 return (-1);
25714}
25715
25716static int
25717xmlSchemaValidateElemWildcard(xmlSchemaValidCtxtPtr vctxt,
25718 int *skip)
25719{
25720 xmlSchemaWildcardPtr wild = (xmlSchemaWildcardPtr) vctxt->inode->decl;
25721 /*
25722 * The namespace of the element was already identified to be
25723 * matching the wildcard.
25724 */
25725 if ((skip == NULL) || (wild == NULL) ||
25726 (wild->type != XML_SCHEMA_TYPE_ANY)) {
25727 VERROR_INT("xmlSchemaValidateElemWildcard",
25728 "bad arguments");
25729 return (-1);
25730 }
25731 *skip = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025732 if (wild->processContents == XML_SCHEMAS_ANY_SKIP) {
25733 /*
25734 * URGENT VAL TODO: Either we need to position the stream to the
25735 * next sibling, or walk the whole subtree.
25736 */
25737 *skip = 1;
25738 return (0);
25739 }
25740 {
25741 xmlSchemaElementPtr decl = NULL;
25742
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025743 decl = xmlSchemaGetElem(vctxt->schema,
Daniel Veillarddee23482008-04-11 12:58:43 +000025744 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025745 if (decl != NULL) {
25746 vctxt->inode->decl = decl;
25747 return (0);
25748 }
25749 }
25750 if (wild->processContents == XML_SCHEMAS_ANY_STRICT) {
25751 /* VAL TODO: Change to proper error code. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025752 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL, /* WXS_BASIC_CAST wild */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025753 "No matching global element declaration available, but "
25754 "demanded by the strict wildcard");
25755 return (vctxt->err);
25756 }
25757 if (vctxt->nbAttrInfos != 0) {
25758 xmlSchemaAttrInfoPtr iattr;
25759 /*
25760 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25761 * (1.2.1.2.1) - (1.2.1.2.3 )
25762 *
25763 * Use the xsi:type attribute for the type definition.
25764 */
25765 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25766 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
25767 if (iattr != NULL) {
25768 if (xmlSchemaProcessXSIType(vctxt, iattr,
25769 &(vctxt->inode->typeDef), NULL) == -1) {
25770 VERROR_INT("xmlSchemaValidateElemWildcard",
25771 "calling xmlSchemaProcessXSIType() to "
25772 "process the attribute 'xsi:nil'");
25773 return (-1);
25774 }
25775 /*
25776 * Don't return an error on purpose.
25777 */
25778 return (0);
25779 }
25780 }
25781 /*
25782 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25783 *
25784 * Fallback to "anyType".
25785 */
25786 vctxt->inode->typeDef =
25787 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
25788 return (0);
25789}
25790
25791/*
25792* xmlSchemaCheckCOSValidDefault:
25793*
25794* This will be called if: not nilled, no content and a default/fixed
25795* value is provided.
25796*/
25797
25798static int
25799xmlSchemaCheckCOSValidDefault(xmlSchemaValidCtxtPtr vctxt,
25800 const xmlChar *value,
25801 xmlSchemaValPtr *val)
Daniel Veillarddee23482008-04-11 12:58:43 +000025802{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025803 int ret = 0;
25804 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25805
25806 /*
25807 * cos-valid-default:
25808 * Schema Component Constraint: Element Default Valid (Immediate)
Daniel Veillarddee23482008-04-11 12:58:43 +000025809 * For a string to be a valid default with respect to a type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025810 * definition the appropriate case among the following must be true:
Daniel Veillarddee23482008-04-11 12:58:43 +000025811 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025812 if WXS_IS_COMPLEX(inode->typeDef) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025813 /*
25814 * Complex type.
25815 *
25816 * SPEC (2.1) "its {content type} must be a simple type definition
25817 * or mixed."
25818 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
Rob Richardsc6947bb2008-06-29 15:04:41 +000025819 * type}'s particle must be �emptiable� as defined by
25820 * Particle Emptiable (�3.9.6)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025821 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025822 if ((! WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) &&
25823 ((! WXS_HAS_MIXED_CONTENT(inode->typeDef)) ||
25824 (! WXS_EMPTIABLE(inode->typeDef)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025825 ret = XML_SCHEMAP_COS_VALID_DEFAULT_2_1;
25826 /* NOTE that this covers (2.2.2) as well. */
25827 VERROR(ret, NULL,
25828 "For a string to be a valid default, the type definition "
25829 "must be a simple type or a complex type with simple content "
25830 "or mixed content and a particle emptiable");
25831 return(ret);
25832 }
Daniel Veillarddee23482008-04-11 12:58:43 +000025833 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025834 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000025835 * 1 If the type definition is a simple type definition, then the string
Rob Richardsc6947bb2008-06-29 15:04:41 +000025836 * must be �valid� with respect to that definition as defined by String
25837 * Valid (�3.14.4).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025838 *
25839 * AND
25840 *
Daniel Veillarddee23482008-04-11 12:58:43 +000025841 * 2.2.1 If the {content type} is a simple type definition, then the
Rob Richardsc6947bb2008-06-29 15:04:41 +000025842 * string must be �valid� with respect to that simple type definition
25843 * as defined by String Valid (�3.14.4).
Daniel Veillarddee23482008-04-11 12:58:43 +000025844 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025845 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025846
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025847 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025848 NULL, inode->typeDef, value, val, 1, 1, 0);
25849
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025850 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025851
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025852 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025853 NULL, inode->typeDef->contentTypeDef, value, val, 1, 1, 0);
25854 }
25855 if (ret < 0) {
25856 VERROR_INT("xmlSchemaCheckCOSValidDefault",
25857 "calling xmlSchemaVCheckCVCSimpleType()");
Daniel Veillarddee23482008-04-11 12:58:43 +000025858 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025859 return (ret);
25860}
25861
25862static void
25863xmlSchemaVContentModelCallback(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
25864 const xmlChar * name ATTRIBUTE_UNUSED,
25865 xmlSchemaElementPtr item,
25866 xmlSchemaNodeInfoPtr inode)
25867{
25868 inode->decl = item;
25869#ifdef DEBUG_CONTENT
25870 {
25871 xmlChar *str = NULL;
25872
25873 if (item->type == XML_SCHEMA_TYPE_ELEMENT) {
25874 xmlGenericError(xmlGenericErrorContext,
25875 "AUTOMATON callback for '%s' [declaration]\n",
25876 xmlSchemaFormatQName(&str,
25877 inode->localName, inode->nsName));
25878 } else {
25879 xmlGenericError(xmlGenericErrorContext,
25880 "AUTOMATON callback for '%s' [wildcard]\n",
25881 xmlSchemaFormatQName(&str,
25882 inode->localName, inode->nsName));
25883
25884 }
25885 FREE_AND_NULL(str)
25886 }
25887#endif
25888}
25889
25890static int
25891xmlSchemaValidatorPushElem(xmlSchemaValidCtxtPtr vctxt)
Daniel Veillarddee23482008-04-11 12:58:43 +000025892{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025893 vctxt->inode = xmlSchemaGetFreshElemInfo(vctxt);
25894 if (vctxt->inode == NULL) {
25895 VERROR_INT("xmlSchemaValidatorPushElem",
25896 "calling xmlSchemaGetFreshElemInfo()");
25897 return (-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000025898 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025899 vctxt->nbAttrInfos = 0;
25900 return (0);
25901}
25902
25903static int
25904xmlSchemaVCheckINodeDataType(xmlSchemaValidCtxtPtr vctxt,
25905 xmlSchemaNodeInfoPtr inode,
25906 xmlSchemaTypePtr type,
25907 const xmlChar *value)
25908{
25909 if (inode->flags & XML_SCHEMA_NODE_INFO_VALUE_NEEDED)
25910 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025911 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025912 type, value, &(inode->val), 1, 1, 0));
25913 else
25914 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025915 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025916 type, value, NULL, 1, 0, 0));
25917}
25918
25919
25920
Daniel Veillarddee23482008-04-11 12:58:43 +000025921/*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025922* Process END of element.
25923*/
25924static int
25925xmlSchemaValidatorPopElem(xmlSchemaValidCtxtPtr vctxt)
25926{
25927 int ret = 0;
25928 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25929
25930 if (vctxt->nbAttrInfos != 0)
25931 xmlSchemaClearAttrInfos(vctxt);
25932 if (inode->flags & XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED) {
25933 /*
25934 * This element was not expected;
25935 * we will not validate child elements of broken parents.
25936 * Skip validation of all content of the parent.
25937 */
25938 vctxt->skipDepth = vctxt->depth -1;
25939 goto end_elem;
Daniel Veillarddee23482008-04-11 12:58:43 +000025940 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025941 if ((inode->typeDef == NULL) ||
25942 (inode->flags & XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE)) {
25943 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025944 * 1. the type definition might be missing if the element was
25945 * error prone
25946 * 2. it might be abstract.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025947 */
25948 goto end_elem;
25949 }
25950 /*
25951 * Check the content model.
25952 */
25953 if ((inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) ||
25954 (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)) {
25955
25956 /*
25957 * Workaround for "anyType".
25958 */
25959 if (inode->typeDef->builtInType == XML_SCHEMAS_ANYTYPE)
Daniel Veillarddee23482008-04-11 12:58:43 +000025960 goto character_content;
25961
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025962 if ((inode->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) == 0) {
25963 xmlChar *values[10];
25964 int terminal, nbval = 10, nbneg;
25965
25966 if (inode->regexCtxt == NULL) {
25967 /*
25968 * Create the regex context.
25969 */
25970 inode->regexCtxt =
25971 xmlRegNewExecCtxt(inode->typeDef->contModel,
25972 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
25973 vctxt);
25974 if (inode->regexCtxt == NULL) {
25975 VERROR_INT("xmlSchemaValidatorPopElem",
25976 "failed to create a regex context");
25977 goto internal_error;
25978 }
25979#ifdef DEBUG_AUTOMATA
25980 xmlGenericError(xmlGenericErrorContext,
25981 "AUTOMATON create on '%s'\n", inode->localName);
Daniel Veillarddee23482008-04-11 12:58:43 +000025982#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025983 }
25984 /*
25985 * Get hold of the still expected content, since a further
25986 * call to xmlRegExecPushString() will loose this information.
Daniel Veillarddee23482008-04-11 12:58:43 +000025987 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025988 xmlRegExecNextValues(inode->regexCtxt,
25989 &nbval, &nbneg, &values[0], &terminal);
25990 ret = xmlRegExecPushString(inode->regexCtxt, NULL, NULL);
Daniel Veillard65fcf272007-07-11 17:55:30 +000025991 if ((ret<0) || ((ret==0) && (!INODE_NILLED(inode)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025992 /*
25993 * Still missing something.
25994 */
25995 ret = 1;
25996 inode->flags |=
25997 XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025998 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025999 XML_SCHEMAV_ELEMENT_CONTENT, NULL, NULL,
26000 "Missing child element(s)",
26001 nbval, nbneg, values);
26002#ifdef DEBUG_AUTOMATA
26003 xmlGenericError(xmlGenericErrorContext,
26004 "AUTOMATON missing ERROR on '%s'\n",
26005 inode->localName);
26006#endif
26007 } else {
26008 /*
26009 * Content model is satisfied.
26010 */
26011 ret = 0;
26012#ifdef DEBUG_AUTOMATA
26013 xmlGenericError(xmlGenericErrorContext,
26014 "AUTOMATON succeeded on '%s'\n",
26015 inode->localName);
26016#endif
26017 }
26018
26019 }
26020 }
26021 if (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)
26022 goto end_elem;
26023
26024character_content:
26025
26026 if (vctxt->value != NULL) {
26027 xmlSchemaFreeValue(vctxt->value);
26028 vctxt->value = NULL;
26029 }
26030 /*
26031 * Check character content.
26032 */
26033 if (inode->decl == NULL) {
26034 /*
26035 * Speedup if no declaration exists.
26036 */
Daniel Veillarddee23482008-04-11 12:58:43 +000026037 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026038 ret = xmlSchemaVCheckINodeDataType(vctxt,
26039 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026040 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026041 ret = xmlSchemaVCheckINodeDataType(vctxt,
26042 inode, inode->typeDef->contentTypeDef,
26043 inode->value);
Daniel Veillarddee23482008-04-11 12:58:43 +000026044 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026045 if (ret < 0) {
26046 VERROR_INT("xmlSchemaValidatorPopElem",
26047 "calling xmlSchemaVCheckCVCSimpleType()");
26048 goto internal_error;
26049 }
26050 goto end_elem;
26051 }
26052 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026053 * cvc-elt (3.3.4) : 5
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026054 * The appropriate case among the following must be true:
26055 */
26056 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026057 * cvc-elt (3.3.4) : 5.1
26058 * If the declaration has a {value constraint},
26059 * the item has neither element nor character [children] and
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026060 * clause 3.2 has not applied, then all of the following must be true:
26061 */
26062 if ((inode->decl->value != NULL) &&
Daniel Veillarddee23482008-04-11 12:58:43 +000026063 (inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026064 (! INODE_NILLED(inode))) {
26065 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026066 * cvc-elt (3.3.4) : 5.1.1
Rob Richardsc6947bb2008-06-29 15:04:41 +000026067 * If the �actual type definition� is a �local type definition�
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026068 * then the canonical lexical representation of the {value constraint}
Rob Richardsc6947bb2008-06-29 15:04:41 +000026069 * value must be a valid default for the �actual type definition� as
26070 * defined in Element Default Valid (Immediate) (�3.3.6).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026071 */
Daniel Veillarddee23482008-04-11 12:58:43 +000026072 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026073 * NOTE: 'local' above means types acquired by xsi:type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026074 * NOTE: Although the *canonical* value is stated, it is not
26075 * relevant if canonical or not. Additionally XML Schema 1.1
26076 * will removed this requirement as well.
26077 */
26078 if (inode->flags & XML_SCHEMA_ELEM_INFO_LOCAL_TYPE) {
26079
26080 ret = xmlSchemaCheckCOSValidDefault(vctxt,
26081 inode->decl->value, &(inode->val));
26082 if (ret != 0) {
26083 if (ret < 0) {
26084 VERROR_INT("xmlSchemaValidatorPopElem",
26085 "calling xmlSchemaCheckCOSValidDefault()");
26086 goto internal_error;
26087 }
26088 goto end_elem;
26089 }
26090 /*
26091 * Stop here, to avoid redundant validation of the value
26092 * (see following).
26093 */
26094 goto default_psvi;
Daniel Veillarddee23482008-04-11 12:58:43 +000026095 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026096 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026097 * cvc-elt (3.3.4) : 5.1.2
26098 * The element information item with the canonical lexical
26099 * representation of the {value constraint} value used as its
Rob Richardsc6947bb2008-06-29 15:04:41 +000026100 * �normalized value� must be �valid� with respect to the
26101 * �actual type definition� as defined by Element Locally Valid (Type)
26102 * (�3.3.4).
Daniel Veillarddee23482008-04-11 12:58:43 +000026103 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026104 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026105 ret = xmlSchemaVCheckINodeDataType(vctxt,
26106 inode, inode->typeDef, inode->decl->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026107 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026108 ret = xmlSchemaVCheckINodeDataType(vctxt,
26109 inode, inode->typeDef->contentTypeDef,
Daniel Veillarddee23482008-04-11 12:58:43 +000026110 inode->decl->value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026111 }
26112 if (ret != 0) {
26113 if (ret < 0) {
26114 VERROR_INT("xmlSchemaValidatorPopElem",
26115 "calling xmlSchemaVCheckCVCSimpleType()");
26116 goto internal_error;
26117 }
26118 goto end_elem;
26119 }
26120
26121default_psvi:
26122 /*
26123 * PSVI: Create a text node on the instance element.
26124 */
26125 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
26126 (inode->node != NULL)) {
26127 xmlNodePtr textChild;
26128 xmlChar *normValue;
26129 /*
26130 * VAL TODO: Normalize the value.
Daniel Veillarddee23482008-04-11 12:58:43 +000026131 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026132 normValue = xmlSchemaNormalizeValue(inode->typeDef,
26133 inode->decl->value);
26134 if (normValue != NULL) {
26135 textChild = xmlNewText(BAD_CAST normValue);
26136 xmlFree(normValue);
26137 } else
26138 textChild = xmlNewText(inode->decl->value);
26139 if (textChild == NULL) {
26140 VERROR_INT("xmlSchemaValidatorPopElem",
26141 "calling xmlNewText()");
26142 goto internal_error;
26143 } else
Daniel Veillarddee23482008-04-11 12:58:43 +000026144 xmlAddChild(inode->node, textChild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026145 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026146
26147 } else if (! INODE_NILLED(inode)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026148 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000026149 * 5.2.1 The element information item must be �valid� with respect
26150 * to the �actual type definition� as defined by Element Locally
26151 * Valid (Type) (�3.3.4).
Daniel Veillarddee23482008-04-11 12:58:43 +000026152 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026153 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026154 /*
26155 * SPEC (cvc-type) (3.1)
26156 * "If the type definition is a simple type definition, ..."
26157 * (3.1.3) "If clause 3.2 of Element Locally Valid
Rob Richardsc6947bb2008-06-29 15:04:41 +000026158 * (Element) (�3.3.4) did not apply, then the �normalized value�
26159 * must be �valid� with respect to the type definition as defined
26160 * by String Valid (�3.14.4).
Daniel Veillarddee23482008-04-11 12:58:43 +000026161 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026162 ret = xmlSchemaVCheckINodeDataType(vctxt,
26163 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026164 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026165 /*
26166 * SPEC (cvc-type) (3.2) "If the type definition is a complex type
26167 * definition, then the element information item must be
Rob Richardsc6947bb2008-06-29 15:04:41 +000026168 * �valid� with respect to the type definition as per
26169 * Element Locally Valid (Complex Type) (�3.4.4);"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026170 *
26171 * SPEC (cvc-complex-type) (2.2)
Daniel Veillarddee23482008-04-11 12:58:43 +000026172 * "If the {content type} is a simple type definition, ...
Rob Richardsc6947bb2008-06-29 15:04:41 +000026173 * the �normalized value� of the element information item is
26174 * �valid� with respect to that simple type definition as
26175 * defined by String Valid (�3.14.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026176 */
26177 ret = xmlSchemaVCheckINodeDataType(vctxt,
26178 inode, inode->typeDef->contentTypeDef, inode->value);
Daniel Veillarddee23482008-04-11 12:58:43 +000026179 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026180 if (ret != 0) {
26181 if (ret < 0) {
26182 VERROR_INT("xmlSchemaValidatorPopElem",
26183 "calling xmlSchemaVCheckCVCSimpleType()");
26184 goto internal_error;
26185 }
26186 goto end_elem;
26187 }
26188 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026189 * 5.2.2 If there is a fixed {value constraint} and clause 3.2 has
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026190 * not applied, all of the following must be true:
26191 */
26192 if ((inode->decl->value != NULL) &&
26193 (inode->decl->flags & XML_SCHEMAS_ELEM_FIXED)) {
26194
26195 /*
26196 * TODO: We will need a computed value, when comparison is
26197 * done on computed values.
26198 */
26199 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026200 * 5.2.2.1 The element information item must have no element
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026201 * information item [children].
26202 */
26203 if (inode->flags &
26204 XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT) {
26205 ret = XML_SCHEMAV_CVC_ELT_5_2_2_1;
26206 VERROR(ret, NULL,
26207 "The content must not containt element nodes since "
26208 "there is a fixed value constraint");
26209 goto end_elem;
26210 } else {
26211 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026212 * 5.2.2.2 The appropriate case among the following must
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026213 * be true:
Daniel Veillarddee23482008-04-11 12:58:43 +000026214 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026215 if (WXS_HAS_MIXED_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026216 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000026217 * 5.2.2.2.1 If the {content type} of the �actual type
26218 * definition� is mixed, then the *initial value* of the
Daniel Veillarddee23482008-04-11 12:58:43 +000026219 * item must match the canonical lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026220 * of the {value constraint} value.
26221 *
Daniel Veillarddee23482008-04-11 12:58:43 +000026222 * ... the *initial value* of an element information
26223 * item is the string composed of, in order, the
26224 * [character code] of each character information item in
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026225 * the [children] of that element information item.
Daniel Veillarddee23482008-04-11 12:58:43 +000026226 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026227 if (! xmlStrEqual(inode->value, inode->decl->value)){
Daniel Veillarddee23482008-04-11 12:58:43 +000026228 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026229 * VAL TODO: Report invalid & expected values as well.
26230 * VAL TODO: Implement the canonical stuff.
26231 */
26232 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_1;
Daniel Veillarddee23482008-04-11 12:58:43 +000026233 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026234 ret, NULL, NULL,
26235 "The initial value '%s' does not match the fixed "
26236 "value constraint '%s'",
26237 inode->value, inode->decl->value);
26238 goto end_elem;
26239 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026240 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026241 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000026242 * 5.2.2.2.2 If the {content type} of the �actual type
26243 * definition� is a simple type definition, then the
Daniel Veillarddee23482008-04-11 12:58:43 +000026244 * *actual value* of the item must match the canonical
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026245 * lexical representation of the {value constraint} value.
26246 */
26247 /*
26248 * VAL TODO: *actual value* is the normalized value, impl.
26249 * this.
26250 * VAL TODO: Report invalid & expected values as well.
26251 * VAL TODO: Implement a comparison with the computed values.
26252 */
26253 if (! xmlStrEqual(inode->value,
26254 inode->decl->value)) {
26255 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_2;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026256 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026257 ret, NULL, NULL,
26258 "The actual value '%s' does not match the fixed "
Daniel Veillarddee23482008-04-11 12:58:43 +000026259 "value constraint '%s'",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026260 inode->value,
26261 inode->decl->value);
26262 goto end_elem;
Daniel Veillarddee23482008-04-11 12:58:43 +000026263 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026264 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026265 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026266 }
26267 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026268
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026269end_elem:
26270 if (vctxt->depth < 0) {
26271 /* TODO: raise error? */
26272 return (0);
26273 }
26274 if (vctxt->depth == vctxt->skipDepth)
26275 vctxt->skipDepth = -1;
26276 /*
26277 * Evaluate the history of XPath state objects.
Daniel Veillarddee23482008-04-11 12:58:43 +000026278 */
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000026279 if (inode->appliedXPath &&
26280 (xmlSchemaXPathProcessHistory(vctxt, vctxt->depth) == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026281 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026282 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026283 * MAYBE TODO:
Rob Richardsc6947bb2008-06-29 15:04:41 +000026284 * SPEC (6) "The element information item must be �valid� with
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026285 * respect to each of the {identity-constraint definitions} as per
Rob Richardsc6947bb2008-06-29 15:04:41 +000026286 * Identity-constraint Satisfied (�3.11.4)."
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026287 */
26288 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026289 * PSVI TODO: If we expose IDC node-tables via PSVI then the tables
26290 * need to be built in any case.
26291 * We will currently build IDC node-tables and bubble them only if
26292 * keyrefs do exist.
26293 */
Daniel Veillarddee23482008-04-11 12:58:43 +000026294
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026295 /*
26296 * Add the current IDC target-nodes to the IDC node-tables.
26297 */
26298 if ((inode->idcMatchers != NULL) &&
26299 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
26300 {
26301 if (xmlSchemaIDCFillNodeTables(vctxt, inode) == -1)
26302 goto internal_error;
26303 }
26304 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026305 * Validate IDC keyrefs.
26306 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026307 if (vctxt->inode->hasKeyrefs)
26308 if (xmlSchemaCheckCVCIDCKeyRef(vctxt) == -1)
26309 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026310 /*
26311 * Merge/free the IDC table.
26312 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026313 if (inode->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026314#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026315 xmlSchemaDebugDumpIDCTable(stdout,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026316 inode->nsName,
26317 inode->localName,
26318 inode->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026319#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026320 if ((vctxt->depth > 0) &&
26321 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
26322 {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026323 /*
26324 * Merge the IDC node table with the table of the parent node.
26325 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026326 if (xmlSchemaBubbleIDCNodeTables(vctxt) == -1)
26327 goto internal_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000026328 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026329 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026330 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026331 * Clear the current ielem.
26332 * VAL TODO: Don't free the PSVI IDC tables if they are
26333 * requested for the PSVI.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026334 */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000026335 xmlSchemaClearElemInfo(vctxt, inode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026336 /*
26337 * Skip further processing if we are on the validation root.
26338 */
26339 if (vctxt->depth == 0) {
26340 vctxt->depth--;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026341 vctxt->inode = NULL;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000026342 return (0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026343 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026344 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026345 * Reset the keyrefDepth if needed.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026346 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026347 if (vctxt->aidcs != NULL) {
26348 xmlSchemaIDCAugPtr aidc = vctxt->aidcs;
26349 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026350 if (aidc->keyrefDepth == vctxt->depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026351 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026352 * A 'keyrefDepth' of a key/unique IDC matches the current
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026353 * depth, this means that we are leaving the scope of the
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026354 * top-most keyref IDC which refers to this IDC.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026355 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026356 aidc->keyrefDepth = -1;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026357 }
26358 aidc = aidc->next;
26359 } while (aidc != NULL);
26360 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026361 vctxt->depth--;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026362 vctxt->inode = vctxt->elemInfos[vctxt->depth];
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000026363 /*
Rob Richardsc6947bb2008-06-29 15:04:41 +000026364 * VAL TODO: 7 If the element information item is the �validation root�, it must be
26365 * �valid� per Validation Root Valid (ID/IDREF) (�3.3.4).
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026366 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026367 return (ret);
26368
26369internal_error:
26370 vctxt->err = -1;
26371 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026372}
26373
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026374/*
26375* 3.4.4 Complex Type Definition Validation Rules
26376* Validation Rule: Element Locally Valid (Complex Type) (cvc-complex-type)
26377*/
Daniel Veillardc0826a72004-08-10 14:17:33 +000026378static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026379xmlSchemaValidateChildElem(xmlSchemaValidCtxtPtr vctxt)
William M. Brack2f2a6632004-08-20 23:09:47 +000026380{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026381 xmlSchemaNodeInfoPtr pielem;
26382 xmlSchemaTypePtr ptype;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026383 int ret = 0;
Daniel Veillard3646d642004-06-02 19:19:14 +000026384
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026385 if (vctxt->depth <= 0) {
26386 VERROR_INT("xmlSchemaValidateChildElem",
26387 "not intended for the validation root");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026388 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026389 }
26390 pielem = vctxt->elemInfos[vctxt->depth -1];
26391 if (pielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26392 pielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026393 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026394 * Handle 'nilled' elements.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026395 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026396 if (INODE_NILLED(pielem)) {
26397 /*
26398 * SPEC (cvc-elt) (3.3.4) : (3.2.1)
26399 */
26400 ACTIVATE_PARENT_ELEM;
26401 ret = XML_SCHEMAV_CVC_ELT_3_2_1;
26402 VERROR(ret, NULL,
26403 "Neither character nor element content is allowed, "
26404 "because the element was 'nilled'");
26405 ACTIVATE_ELEM;
26406 goto unexpected_elem;
26407 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026408
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026409 ptype = pielem->typeDef;
26410
26411 if (ptype->builtInType == XML_SCHEMAS_ANYTYPE) {
26412 /*
26413 * Workaround for "anyType": we have currently no content model
26414 * assigned for "anyType", so handle it explicitely.
26415 * "anyType" has an unbounded, lax "any" wildcard.
26416 */
26417 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26418 vctxt->inode->localName,
26419 vctxt->inode->nsName);
26420
26421 if (vctxt->inode->decl == NULL) {
26422 xmlSchemaAttrInfoPtr iattr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026423 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026424 * Process "xsi:type".
26425 * SPEC (cvc-assess-elt) (1.2.1.2.1) - (1.2.1.2.3)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026426 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026427 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
26428 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
26429 if (iattr != NULL) {
26430 ret = xmlSchemaProcessXSIType(vctxt, iattr,
26431 &(vctxt->inode->typeDef), NULL);
26432 if (ret != 0) {
26433 if (ret == -1) {
26434 VERROR_INT("xmlSchemaValidateChildElem",
26435 "calling xmlSchemaProcessXSIType() to "
26436 "process the attribute 'xsi:nil'");
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000026437 return (-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000026438 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026439 return (ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +000026440 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026441 } else {
26442 /*
26443 * Fallback to "anyType".
26444 *
26445 * SPEC (cvc-assess-elt)
Rob Richardsc6947bb2008-06-29 15:04:41 +000026446 * "If the item cannot be �strictly assessed�, [...]
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026447 * an element information item's schema validity may be laxly
Rob Richardsc6947bb2008-06-29 15:04:41 +000026448 * assessed if its �context-determined declaration� is not
26449 * skip by �validating� with respect to the �ur-type
26450 * definition� as per Element Locally Valid (Type) (�3.3.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026451 */
26452 vctxt->inode->typeDef =
26453 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026454 }
26455 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026456 return (0);
26457 }
26458
26459 switch (ptype->contentType) {
26460 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026461 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026462 * SPEC (2.1) "If the {content type} is empty, then the
26463 * element information item has no character or element
26464 * information item [children]."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026465 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026466 ACTIVATE_PARENT_ELEM
26467 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1;
26468 VERROR(ret, NULL,
26469 "Element content is not allowed, "
26470 "because the content type is empty");
26471 ACTIVATE_ELEM
26472 goto unexpected_elem;
26473 break;
26474
26475 case XML_SCHEMA_CONTENT_MIXED:
26476 case XML_SCHEMA_CONTENT_ELEMENTS: {
26477 xmlRegExecCtxtPtr regexCtxt;
26478 xmlChar *values[10];
26479 int terminal, nbval = 10, nbneg;
26480
26481 /* VAL TODO: Optimized "anyType" validation.*/
26482
26483 if (ptype->contModel == NULL) {
26484 VERROR_INT("xmlSchemaValidateChildElem",
26485 "type has elem content but no content model");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026486 return (-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000026487 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026488 /*
26489 * Safety belf for evaluation if the cont. model was already
26490 * examined to be invalid.
26491 */
26492 if (pielem->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) {
26493 VERROR_INT("xmlSchemaValidateChildElem",
26494 "validating elem, but elem content is already invalid");
26495 return (-1);
26496 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +000026497
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026498 regexCtxt = pielem->regexCtxt;
26499 if (regexCtxt == NULL) {
26500 /*
26501 * Create the regex context.
26502 */
26503 regexCtxt = xmlRegNewExecCtxt(ptype->contModel,
26504 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
26505 vctxt);
26506 if (regexCtxt == NULL) {
26507 VERROR_INT("xmlSchemaValidateChildElem",
26508 "failed to create a regex context");
26509 return (-1);
26510 }
26511 pielem->regexCtxt = regexCtxt;
26512#ifdef DEBUG_AUTOMATA
26513 xmlGenericError(xmlGenericErrorContext, "AUTOMATA create on '%s'\n",
26514 pielem->localName);
26515#endif
26516 }
26517
26518 /*
26519 * SPEC (2.4) "If the {content type} is element-only or mixed,
26520 * then the sequence of the element information item's
26521 * element information item [children], if any, taken in
Rob Richardsc6947bb2008-06-29 15:04:41 +000026522 * order, is �valid� with respect to the {content type}'s
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026523 * particle, as defined in Element Sequence Locally Valid
Rob Richardsc6947bb2008-06-29 15:04:41 +000026524 * (Particle) (�3.9.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026525 */
26526 ret = xmlRegExecPushString2(regexCtxt,
26527 vctxt->inode->localName,
26528 vctxt->inode->nsName,
26529 vctxt->inode);
26530#ifdef DEBUG_AUTOMATA
26531 if (ret < 0)
26532 xmlGenericError(xmlGenericErrorContext,
26533 "AUTOMATON push ERROR for '%s' on '%s'\n",
26534 vctxt->inode->localName, pielem->localName);
26535 else
26536 xmlGenericError(xmlGenericErrorContext,
26537 "AUTOMATON push OK for '%s' on '%s'\n",
26538 vctxt->inode->localName, pielem->localName);
26539#endif
26540 if (vctxt->err == XML_SCHEMAV_INTERNAL) {
26541 VERROR_INT("xmlSchemaValidateChildElem",
26542 "calling xmlRegExecPushString2()");
26543 return (-1);
26544 }
26545 if (ret < 0) {
26546 xmlRegExecErrInfo(regexCtxt, NULL, &nbval, &nbneg,
26547 &values[0], &terminal);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026548 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026549 XML_SCHEMAV_ELEMENT_CONTENT, NULL,NULL,
26550 "This element is not expected",
26551 nbval, nbneg, values);
26552 ret = vctxt->err;
26553 goto unexpected_elem;
26554 } else
26555 ret = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026556 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026557 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026558 case XML_SCHEMA_CONTENT_SIMPLE:
26559 case XML_SCHEMA_CONTENT_BASIC:
26560 ACTIVATE_PARENT_ELEM
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026561 if (WXS_IS_COMPLEX(ptype)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026562 /*
26563 * SPEC (cvc-complex-type) (2.2)
26564 * "If the {content type} is a simple type definition, then
26565 * the element information item has no element information
26566 * item [children], ..."
26567 */
26568 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_2;
26569 VERROR(ret, NULL, "Element content is not allowed, "
26570 "because the content type is a simple type definition");
26571 } else {
26572 /*
26573 * SPEC (cvc-type) (3.1.2) "The element information item must
26574 * have no element information item [children]."
26575 */
26576 ret = XML_SCHEMAV_CVC_TYPE_3_1_2;
26577 VERROR(ret, NULL, "Element content is not allowed, "
26578 "because the type definition is simple");
26579 }
26580 ACTIVATE_ELEM
26581 ret = vctxt->err;
26582 goto unexpected_elem;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026583 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026584
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026585 default:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026586 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026587 }
26588 return (ret);
26589unexpected_elem:
26590 /*
26591 * Pop this element and set the skipDepth to skip
26592 * all further content of the parent element.
26593 */
26594 vctxt->skipDepth = vctxt->depth;
26595 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED;
26596 pielem->flags |= XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
26597 return (ret);
26598}
26599
26600#define XML_SCHEMA_PUSH_TEXT_PERSIST 1
26601#define XML_SCHEMA_PUSH_TEXT_CREATED 2
26602#define XML_SCHEMA_PUSH_TEXT_VOLATILE 3
26603
26604static int
26605xmlSchemaVPushText(xmlSchemaValidCtxtPtr vctxt,
26606 int nodeType, const xmlChar *value, int len,
26607 int mode, int *consumed)
26608{
26609 /*
26610 * Unfortunately we have to duplicate the text sometimes.
26611 * OPTIMIZE: Maybe we could skip it, if:
26612 * 1. content type is simple
26613 * 2. whitespace is "collapse"
26614 * 3. it consists of whitespace only
26615 *
26616 * Process character content.
26617 */
26618 if (consumed != NULL)
26619 *consumed = 0;
26620 if (INODE_NILLED(vctxt->inode)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000026621 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026622 * SPEC cvc-elt (3.3.4 - 3.2.1)
26623 * "The element information item must have no character or
26624 * element information item [children]."
26625 */
26626 VERROR(XML_SCHEMAV_CVC_ELT_3_2_1, NULL,
26627 "Neither character nor element content is allowed "
26628 "because the element is 'nilled'");
26629 return (vctxt->err);
26630 }
26631 /*
26632 * SPEC (2.1) "If the {content type} is empty, then the
26633 * element information item has no character or element
26634 * information item [children]."
26635 */
26636 if (vctxt->inode->typeDef->contentType ==
Daniel Veillarddee23482008-04-11 12:58:43 +000026637 XML_SCHEMA_CONTENT_EMPTY) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026638 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1, NULL,
26639 "Character content is not allowed, "
26640 "because the content type is empty");
26641 return (vctxt->err);
26642 }
26643
26644 if (vctxt->inode->typeDef->contentType ==
26645 XML_SCHEMA_CONTENT_ELEMENTS) {
26646 if ((nodeType != XML_TEXT_NODE) ||
26647 (! xmlSchemaIsBlank((xmlChar *) value, len))) {
Daniel Veillarddee23482008-04-11 12:58:43 +000026648 /*
26649 * SPEC cvc-complex-type (2.3)
26650 * "If the {content type} is element-only, then the
26651 * element information item has no character information
26652 * item [children] other than those whose [character
26653 * code] is defined as a white space in [XML 1.0 (Second
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026654 * Edition)]."
26655 */
26656 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_3, NULL,
26657 "Character content other than whitespace is not allowed "
26658 "because the content type is 'element-only'");
26659 return (vctxt->err);
26660 }
26661 return (0);
26662 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026663
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026664 if ((value == NULL) || (value[0] == 0))
26665 return (0);
26666 /*
26667 * Save the value.
26668 * NOTE that even if the content type is *mixed*, we need the
26669 * *initial value* for default/fixed value constraints.
26670 */
26671 if ((vctxt->inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) &&
26672 ((vctxt->inode->decl == NULL) ||
26673 (vctxt->inode->decl->value == NULL)))
26674 return (0);
Daniel Veillarddee23482008-04-11 12:58:43 +000026675
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026676 if (vctxt->inode->value == NULL) {
26677 /*
26678 * Set the value.
26679 */
26680 switch (mode) {
26681 case XML_SCHEMA_PUSH_TEXT_PERSIST:
26682 /*
26683 * When working on a tree.
26684 */
26685 vctxt->inode->value = value;
26686 break;
26687 case XML_SCHEMA_PUSH_TEXT_CREATED:
26688 /*
26689 * When working with the reader.
26690 * The value will be freed by the element info.
26691 */
26692 vctxt->inode->value = value;
26693 if (consumed != NULL)
26694 *consumed = 1;
26695 vctxt->inode->flags |=
26696 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26697 break;
26698 case XML_SCHEMA_PUSH_TEXT_VOLATILE:
26699 /*
26700 * When working with SAX.
26701 * The value will be freed by the element info.
26702 */
26703 if (len != -1)
26704 vctxt->inode->value = BAD_CAST xmlStrndup(value, len);
26705 else
26706 vctxt->inode->value = BAD_CAST xmlStrdup(value);
26707 vctxt->inode->flags |=
26708 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26709 break;
26710 default:
26711 break;
26712 }
Kasimier T. Buchcik5bb0c082005-12-20 10:48:33 +000026713 } else {
26714 if (len < 0)
26715 len = xmlStrlen(value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026716 /*
26717 * Concat the value.
Daniel Veillarddee23482008-04-11 12:58:43 +000026718 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026719 if (vctxt->inode->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000026720 vctxt->inode->value = BAD_CAST xmlStrncat(
26721 (xmlChar *) vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026722 } else {
26723 vctxt->inode->value =
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026724 BAD_CAST xmlStrncatNew(vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026725 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26726 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026727 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026728
26729 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000026730}
26731
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026732static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026733xmlSchemaValidateElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000026734{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026735 int ret = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000026736
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026737 if ((vctxt->skipDepth != -1) &&
26738 (vctxt->depth >= vctxt->skipDepth)) {
26739 VERROR_INT("xmlSchemaValidateElem",
26740 "in skip-state");
26741 goto internal_error;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026742 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026743 if (vctxt->xsiAssemble) {
Daniel Veillarddee23482008-04-11 12:58:43 +000026744 /*
Kasimier T. Buchcik215406f2005-12-07 12:14:09 +000026745 * We will stop validation if there was an error during
26746 * dynamic schema construction.
26747 * Note that we simply set @skipDepth to 0, this could
26748 * mean that a streaming document via SAX would be
26749 * still read to the end but it won't be validated any more.
26750 * TODO: If we are sure how to stop the validation at once
26751 * for all input scenarios, then this should be changed to
26752 * instantly stop the validation.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026753 */
Kasimier T. Buchcik215406f2005-12-07 12:14:09 +000026754 ret = xmlSchemaAssembleByXSI(vctxt);
26755 if (ret != 0) {
26756 if (ret == -1)
26757 goto internal_error;
26758 vctxt->skipDepth = 0;
26759 return(ret);
26760 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026761 }
26762 if (vctxt->depth > 0) {
26763 /*
26764 * Validate this element against the content model
26765 * of the parent.
26766 */
26767 ret = xmlSchemaValidateChildElem(vctxt);
26768 if (ret != 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026769 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026770 VERROR_INT("xmlSchemaValidateElem",
26771 "calling xmlSchemaStreamValidateChildElement()");
26772 goto internal_error;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026773 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026774 goto exit;
26775 }
26776 if (vctxt->depth == vctxt->skipDepth)
26777 goto exit;
26778 if ((vctxt->inode->decl == NULL) &&
26779 (vctxt->inode->typeDef == NULL)) {
26780 VERROR_INT("xmlSchemaValidateElem",
26781 "the child element was valid but neither the "
26782 "declaration nor the type was set");
26783 goto internal_error;
26784 }
26785 } else {
26786 /*
26787 * Get the declaration of the validation root.
26788 */
26789 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26790 vctxt->inode->localName,
26791 vctxt->inode->nsName);
26792 if (vctxt->inode->decl == NULL) {
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026793 ret = XML_SCHEMAV_CVC_ELT_1;
26794 VERROR(ret, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026795 "No matching global declaration available "
26796 "for the validation root");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026797 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026798 }
26799 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026800
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026801 if (vctxt->inode->decl == NULL)
26802 goto type_validation;
26803
26804 if (vctxt->inode->decl->type == XML_SCHEMA_TYPE_ANY) {
26805 int skip;
26806 /*
26807 * Wildcards.
26808 */
26809 ret = xmlSchemaValidateElemWildcard(vctxt, &skip);
26810 if (ret != 0) {
26811 if (ret < 0) {
26812 VERROR_INT("xmlSchemaValidateElem",
26813 "calling xmlSchemaValidateElemWildcard()");
26814 goto internal_error;
26815 }
26816 goto exit;
26817 }
26818 if (skip) {
26819 vctxt->skipDepth = vctxt->depth;
26820 goto exit;
26821 }
26822 /*
26823 * The declaration might be set by the wildcard validation,
26824 * when the processContents is "lax" or "strict".
26825 */
26826 if (vctxt->inode->decl->type != XML_SCHEMA_TYPE_ELEMENT) {
26827 /*
26828 * Clear the "decl" field to not confuse further processing.
26829 */
26830 vctxt->inode->decl = NULL;
26831 goto type_validation;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026832 }
Daniel Veillard4255d502002-04-16 15:50:10 +000026833 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026834 /*
26835 * Validate against the declaration.
26836 */
26837 ret = xmlSchemaValidateElemDecl(vctxt);
26838 if (ret != 0) {
26839 if (ret < 0) {
26840 VERROR_INT("xmlSchemaValidateElem",
26841 "calling xmlSchemaValidateElemDecl()");
26842 goto internal_error;
26843 }
26844 goto exit;
26845 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026846 /*
26847 * Validate against the type definition.
26848 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026849type_validation:
26850
26851 if (vctxt->inode->typeDef == NULL) {
26852 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26853 ret = XML_SCHEMAV_CVC_TYPE_1;
26854 VERROR(ret, NULL,
26855 "The type definition is absent");
26856 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000026857 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026858 if (vctxt->inode->typeDef->flags & XML_SCHEMAS_TYPE_ABSTRACT) {
26859 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26860 ret = XML_SCHEMAV_CVC_TYPE_2;
26861 VERROR(ret, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000026862 "The type definition is abstract");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026863 goto exit;
26864 }
26865 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026866 * Evaluate IDCs. Do it here, since new IDC matchers are registered
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026867 * during validation against the declaration. This must be done
26868 * _before_ attribute validation.
26869 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026870 if (vctxt->xpathStates != NULL) {
26871 ret = xmlSchemaXPathEvaluate(vctxt, XML_ELEMENT_NODE);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000026872 vctxt->inode->appliedXPath = 1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026873 if (ret == -1) {
26874 VERROR_INT("xmlSchemaValidateElem",
26875 "calling xmlSchemaXPathEvaluate()");
26876 goto internal_error;
26877 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026878 }
26879 /*
26880 * Validate attributes.
26881 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026882 if (WXS_IS_COMPLEX(vctxt->inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026883 if ((vctxt->nbAttrInfos != 0) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026884 (vctxt->inode->typeDef->attrUses != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026885
26886 ret = xmlSchemaVAttributesComplex(vctxt);
26887 }
26888 } else if (vctxt->nbAttrInfos != 0) {
26889
26890 ret = xmlSchemaVAttributesSimple(vctxt);
26891 }
26892 /*
26893 * Clear registered attributes.
26894 */
26895 if (vctxt->nbAttrInfos != 0)
26896 xmlSchemaClearAttrInfos(vctxt);
26897 if (ret == -1) {
26898 VERROR_INT("xmlSchemaValidateElem",
26899 "calling attributes validation");
26900 goto internal_error;
26901 }
26902 /*
26903 * Don't return an error if attributes are invalid on purpose.
26904 */
26905 ret = 0;
26906
26907exit:
26908 if (ret != 0)
26909 vctxt->skipDepth = vctxt->depth;
26910 return (ret);
26911internal_error:
26912 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026913}
26914
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026915#ifdef XML_SCHEMA_READER_ENABLED
26916static int
26917xmlSchemaVReaderWalk(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026918{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026919 const int WHTSP = 13, SIGN_WHTSP = 14, END_ELEM = 15;
26920 int depth, nodeType, ret = 0, consumed;
26921 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026922
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026923 vctxt->depth = -1;
26924 ret = xmlTextReaderRead(vctxt->reader);
26925 /*
26926 * Move to the document element.
26927 */
26928 while (ret == 1) {
26929 nodeType = xmlTextReaderNodeType(vctxt->reader);
26930 if (nodeType == XML_ELEMENT_NODE)
26931 goto root_found;
26932 ret = xmlTextReaderRead(vctxt->reader);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026933 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026934 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026935
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026936root_found:
26937
26938 do {
26939 depth = xmlTextReaderDepth(vctxt->reader);
26940 nodeType = xmlTextReaderNodeType(vctxt->reader);
26941
26942 if (nodeType == XML_ELEMENT_NODE) {
Daniel Veillarddee23482008-04-11 12:58:43 +000026943
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026944 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026945 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26946 VERROR_INT("xmlSchemaVReaderWalk",
26947 "calling xmlSchemaValidatorPushElem()");
26948 goto internal_error;
26949 }
26950 ielem = vctxt->inode;
26951 ielem->localName = xmlTextReaderLocalName(vctxt->reader);
26952 ielem->nsName = xmlTextReaderNamespaceUri(vctxt->reader);
26953 ielem->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
26954 /*
26955 * Is the element empty?
26956 */
26957 ret = xmlTextReaderIsEmptyElement(vctxt->reader);
26958 if (ret == -1) {
26959 VERROR_INT("xmlSchemaVReaderWalk",
26960 "calling xmlTextReaderIsEmptyElement()");
26961 goto internal_error;
26962 }
26963 if (ret) {
26964 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26965 }
26966 /*
26967 * Register attributes.
26968 */
26969 vctxt->nbAttrInfos = 0;
26970 ret = xmlTextReaderMoveToFirstAttribute(vctxt->reader);
26971 if (ret == -1) {
26972 VERROR_INT("xmlSchemaVReaderWalk",
26973 "calling xmlTextReaderMoveToFirstAttribute()");
26974 goto internal_error;
26975 }
26976 if (ret == 1) {
26977 do {
26978 /*
26979 * VAL TODO: How do we know that the reader works on a
26980 * node tree, to be able to pass a node here?
26981 */
26982 if (xmlSchemaValidatorPushAttribute(vctxt, NULL,
26983 (const xmlChar *) xmlTextReaderLocalName(vctxt->reader),
26984 xmlTextReaderNamespaceUri(vctxt->reader), 1,
26985 xmlTextReaderValue(vctxt->reader), 1) == -1) {
26986
26987 VERROR_INT("xmlSchemaVReaderWalk",
26988 "calling xmlSchemaValidatorPushAttribute()");
26989 goto internal_error;
26990 }
26991 ret = xmlTextReaderMoveToNextAttribute(vctxt->reader);
26992 if (ret == -1) {
26993 VERROR_INT("xmlSchemaVReaderWalk",
26994 "calling xmlTextReaderMoveToFirstAttribute()");
26995 goto internal_error;
26996 }
26997 } while (ret == 1);
26998 /*
26999 * Back to element position.
27000 */
27001 ret = xmlTextReaderMoveToElement(vctxt->reader);
27002 if (ret == -1) {
27003 VERROR_INT("xmlSchemaVReaderWalk",
27004 "calling xmlTextReaderMoveToElement()");
27005 goto internal_error;
27006 }
27007 }
27008 /*
27009 * Validate the element.
27010 */
27011 ret= xmlSchemaValidateElem(vctxt);
27012 if (ret != 0) {
27013 if (ret == -1) {
27014 VERROR_INT("xmlSchemaVReaderWalk",
27015 "calling xmlSchemaValidateElem()");
27016 goto internal_error;
27017 }
27018 goto exit;
27019 }
27020 if (vctxt->depth == vctxt->skipDepth) {
27021 int curDepth;
27022 /*
27023 * Skip all content.
27024 */
27025 if ((ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY) == 0) {
27026 ret = xmlTextReaderRead(vctxt->reader);
27027 curDepth = xmlTextReaderDepth(vctxt->reader);
27028 while ((ret == 1) && (curDepth != depth)) {
27029 ret = xmlTextReaderRead(vctxt->reader);
27030 curDepth = xmlTextReaderDepth(vctxt->reader);
27031 }
27032 if (ret < 0) {
27033 /*
27034 * VAL TODO: A reader error occured; what to do here?
27035 */
27036 ret = 1;
27037 goto exit;
27038 }
27039 }
27040 goto leave_elem;
27041 }
27042 /*
27043 * READER VAL TODO: Is an END_ELEM really never called
27044 * if the elem is empty?
27045 */
27046 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
27047 goto leave_elem;
27048 } else if (nodeType == END_ELEM) {
27049 /*
27050 * Process END of element.
27051 */
27052leave_elem:
27053 ret = xmlSchemaValidatorPopElem(vctxt);
27054 if (ret != 0) {
27055 if (ret < 0) {
27056 VERROR_INT("xmlSchemaVReaderWalk",
27057 "calling xmlSchemaValidatorPopElem()");
27058 goto internal_error;
27059 }
27060 goto exit;
27061 }
27062 if (vctxt->depth >= 0)
27063 ielem = vctxt->inode;
27064 else
27065 ielem = NULL;
27066 } else if ((nodeType == XML_TEXT_NODE) ||
27067 (nodeType == XML_CDATA_SECTION_NODE) ||
27068 (nodeType == WHTSP) ||
27069 (nodeType == SIGN_WHTSP)) {
27070 /*
27071 * Process character content.
27072 */
27073 xmlChar *value;
27074
27075 if ((nodeType == WHTSP) || (nodeType == SIGN_WHTSP))
27076 nodeType = XML_TEXT_NODE;
27077
27078 value = xmlTextReaderValue(vctxt->reader);
27079 ret = xmlSchemaVPushText(vctxt, nodeType, BAD_CAST value,
27080 -1, XML_SCHEMA_PUSH_TEXT_CREATED, &consumed);
27081 if (! consumed)
27082 xmlFree(value);
27083 if (ret == -1) {
27084 VERROR_INT("xmlSchemaVReaderWalk",
27085 "calling xmlSchemaVPushText()");
27086 goto internal_error;
27087 }
27088 } else if ((nodeType == XML_ENTITY_NODE) ||
27089 (nodeType == XML_ENTITY_REF_NODE)) {
27090 /*
27091 * VAL TODO: What to do with entities?
27092 */
27093 TODO
27094 }
27095 /*
27096 * Read next node.
27097 */
27098 ret = xmlTextReaderRead(vctxt->reader);
27099 } while (ret == 1);
27100
27101exit:
27102 return (ret);
27103internal_error:
27104 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000027105}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027106#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000027107
27108/************************************************************************
27109 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027110 * SAX validation handlers *
Daniel Veillard4255d502002-04-16 15:50:10 +000027111 * *
27112 ************************************************************************/
27113
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027114/*
27115* Process text content.
27116*/
27117static void
Daniel Veillarddee23482008-04-11 12:58:43 +000027118xmlSchemaSAXHandleText(void *ctx,
27119 const xmlChar * ch,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027120 int len)
27121{
27122 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27123
27124 if (vctxt->depth < 0)
27125 return;
27126 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27127 return;
27128 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
27129 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27130 if (xmlSchemaVPushText(vctxt, XML_TEXT_NODE, ch, len,
27131 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
27132 VERROR_INT("xmlSchemaSAXHandleCDataSection",
27133 "calling xmlSchemaVPushText()");
27134 vctxt->err = -1;
27135 xmlStopParser(vctxt->parserCtxt);
27136 }
27137}
27138
27139/*
27140* Process CDATA content.
27141*/
27142static void
Daniel Veillarddee23482008-04-11 12:58:43 +000027143xmlSchemaSAXHandleCDataSection(void *ctx,
27144 const xmlChar * ch,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027145 int len)
Daniel Veillarddee23482008-04-11 12:58:43 +000027146{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027147 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27148
27149 if (vctxt->depth < 0)
27150 return;
27151 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27152 return;
27153 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
27154 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27155 if (xmlSchemaVPushText(vctxt, XML_CDATA_SECTION_NODE, ch, len,
27156 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
27157 VERROR_INT("xmlSchemaSAXHandleCDataSection",
27158 "calling xmlSchemaVPushText()");
27159 vctxt->err = -1;
27160 xmlStopParser(vctxt->parserCtxt);
27161 }
27162}
27163
27164static void
27165xmlSchemaSAXHandleReference(void *ctx ATTRIBUTE_UNUSED,
27166 const xmlChar * name ATTRIBUTE_UNUSED)
27167{
27168 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27169
27170 if (vctxt->depth < 0)
27171 return;
27172 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27173 return;
27174 /* SAX VAL TODO: What to do here? */
27175 TODO
27176}
27177
27178static void
27179xmlSchemaSAXHandleStartElementNs(void *ctx,
Daniel Veillarddee23482008-04-11 12:58:43 +000027180 const xmlChar * localname,
27181 const xmlChar * prefix ATTRIBUTE_UNUSED,
27182 const xmlChar * URI,
27183 int nb_namespaces,
27184 const xmlChar ** namespaces,
27185 int nb_attributes,
27186 int nb_defaulted ATTRIBUTE_UNUSED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027187 const xmlChar ** attributes)
Daniel Veillarddee23482008-04-11 12:58:43 +000027188{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027189 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27190 int ret;
27191 xmlSchemaNodeInfoPtr ielem;
27192 int i, j;
Daniel Veillarddee23482008-04-11 12:58:43 +000027193
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027194 /*
27195 * SAX VAL TODO: What to do with nb_defaulted?
27196 */
27197 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027198 * Skip elements if inside a "skip" wildcard or invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027199 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027200 vctxt->depth++;
27201 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027202 return;
27203 /*
27204 * Push the element.
27205 */
27206 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
27207 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
27208 "calling xmlSchemaValidatorPushElem()");
27209 goto internal_error;
27210 }
27211 ielem = vctxt->inode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027212 /*
27213 * TODO: Is this OK?
27214 */
27215 ielem->nodeLine = xmlSAX2GetLineNumber(vctxt->parserCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027216 ielem->localName = localname;
27217 ielem->nsName = URI;
27218 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
27219 /*
27220 * Register namespaces on the elem info.
Daniel Veillarddee23482008-04-11 12:58:43 +000027221 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027222 if (nb_namespaces != 0) {
27223 /*
27224 * Although the parser builds its own namespace list,
27225 * we have no access to it, so we'll use an own one.
27226 */
Daniel Veillarddee23482008-04-11 12:58:43 +000027227 for (i = 0, j = 0; i < nb_namespaces; i++, j += 2) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027228 /*
27229 * Store prefix and namespace name.
Daniel Veillarddee23482008-04-11 12:58:43 +000027230 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027231 if (ielem->nsBindings == NULL) {
27232 ielem->nsBindings =
27233 (const xmlChar **) xmlMalloc(10 *
27234 sizeof(const xmlChar *));
27235 if (ielem->nsBindings == NULL) {
27236 xmlSchemaVErrMemory(vctxt,
27237 "allocating namespace bindings for SAX validation",
27238 NULL);
27239 goto internal_error;
27240 }
27241 ielem->nbNsBindings = 0;
27242 ielem->sizeNsBindings = 5;
27243 } else if (ielem->sizeNsBindings <= ielem->nbNsBindings) {
27244 ielem->sizeNsBindings *= 2;
27245 ielem->nsBindings =
27246 (const xmlChar **) xmlRealloc(
27247 (void *) ielem->nsBindings,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027248 ielem->sizeNsBindings * 2 * sizeof(const xmlChar *));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027249 if (ielem->nsBindings == NULL) {
27250 xmlSchemaVErrMemory(vctxt,
27251 "re-allocating namespace bindings for SAX validation",
27252 NULL);
27253 goto internal_error;
27254 }
27255 }
27256
27257 ielem->nsBindings[ielem->nbNsBindings * 2] = namespaces[j];
27258 if (namespaces[j+1][0] == 0) {
27259 /*
27260 * Handle xmlns="".
27261 */
27262 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] = NULL;
27263 } else
27264 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] =
27265 namespaces[j+1];
Daniel Veillarddee23482008-04-11 12:58:43 +000027266 ielem->nbNsBindings++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027267 }
27268 }
27269 /*
27270 * Register attributes.
27271 * SAX VAL TODO: We are not adding namespace declaration
27272 * attributes yet.
27273 */
27274 if (nb_attributes != 0) {
27275 xmlChar *value;
27276
27277 for (j = 0, i = 0; i < nb_attributes; i++, j += 5) {
27278 /*
27279 * Duplicate the value.
Daniel Veillarddee23482008-04-11 12:58:43 +000027280 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027281 value = xmlStrndup(attributes[j+3],
27282 attributes[j+4] - attributes[j+3]);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027283 /*
27284 * TODO: Set the node line.
27285 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027286 ret = xmlSchemaValidatorPushAttribute(vctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027287 NULL, ielem->nodeLine, attributes[j], attributes[j+2], 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027288 value, 1);
27289 if (ret == -1) {
27290 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
27291 "calling xmlSchemaValidatorPushAttribute()");
27292 goto internal_error;
27293 }
27294 }
27295 }
27296 /*
27297 * Validate the element.
27298 */
27299 ret = xmlSchemaValidateElem(vctxt);
27300 if (ret != 0) {
27301 if (ret == -1) {
27302 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
27303 "calling xmlSchemaValidateElem()");
27304 goto internal_error;
27305 }
27306 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000027307 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027308
27309exit:
27310 return;
27311internal_error:
27312 vctxt->err = -1;
27313 xmlStopParser(vctxt->parserCtxt);
27314 return;
27315}
27316
27317static void
27318xmlSchemaSAXHandleEndElementNs(void *ctx,
27319 const xmlChar * localname ATTRIBUTE_UNUSED,
27320 const xmlChar * prefix ATTRIBUTE_UNUSED,
27321 const xmlChar * URI ATTRIBUTE_UNUSED)
27322{
27323 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27324 int res;
27325
27326 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027327 * Skip elements if inside a "skip" wildcard or if invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027328 */
27329 if (vctxt->skipDepth != -1) {
27330 if (vctxt->depth > vctxt->skipDepth) {
27331 vctxt->depth--;
27332 return;
27333 } else
27334 vctxt->skipDepth = -1;
27335 }
27336 /*
27337 * SAX VAL TODO: Just a temporary check.
27338 */
27339 if ((!xmlStrEqual(vctxt->inode->localName, localname)) ||
27340 (!xmlStrEqual(vctxt->inode->nsName, URI))) {
27341 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
27342 "elem pop mismatch");
27343 }
27344 res = xmlSchemaValidatorPopElem(vctxt);
27345 if (res != 0) {
27346 if (res < 0) {
27347 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
27348 "calling xmlSchemaValidatorPopElem()");
27349 goto internal_error;
27350 }
27351 goto exit;
27352 }
27353exit:
27354 return;
27355internal_error:
27356 vctxt->err = -1;
27357 xmlStopParser(vctxt->parserCtxt);
27358 return;
27359}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027360
Daniel Veillard4255d502002-04-16 15:50:10 +000027361/************************************************************************
27362 * *
27363 * Validation interfaces *
27364 * *
27365 ************************************************************************/
27366
27367/**
27368 * xmlSchemaNewValidCtxt:
27369 * @schema: a precompiled XML Schemas
27370 *
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027371 * Create an XML Schemas validation context based on the given schema.
Daniel Veillard4255d502002-04-16 15:50:10 +000027372 *
27373 * Returns the validation context or NULL in case of error
27374 */
27375xmlSchemaValidCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027376xmlSchemaNewValidCtxt(xmlSchemaPtr schema)
27377{
Daniel Veillard4255d502002-04-16 15:50:10 +000027378 xmlSchemaValidCtxtPtr ret;
27379
27380 ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt));
27381 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027382 xmlSchemaVErrMemory(NULL, "allocating validation context", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000027383 return (NULL);
27384 }
27385 memset(ret, 0, sizeof(xmlSchemaValidCtxt));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027386 ret->type = XML_SCHEMA_CTXT_VALIDATOR;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027387 ret->dict = xmlDictCreate();
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027388 ret->nodeQNames = xmlSchemaItemListCreate();
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027389 ret->schema = schema;
Daniel Veillard4255d502002-04-16 15:50:10 +000027390 return (ret);
27391}
27392
27393/**
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027394 * xmlSchemaClearValidCtxt:
27395 * @ctxt: the schema validation context
27396 *
27397 * Free the resources associated to the schema validation context;
27398 * leaves some fields alive intended for reuse of the context.
27399 */
27400static void
27401xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt)
27402{
27403 if (vctxt == NULL)
27404 return;
27405
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027406 /*
27407 * TODO: Should we clear the flags?
27408 * Might be problematic if one reuses the context
27409 * and assumes that the options remain the same.
27410 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000027411 vctxt->flags = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027412 vctxt->validationRoot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027413 vctxt->doc = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000027414#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027415 vctxt->reader = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000027416#endif
Daniel Veillarddee23482008-04-11 12:58:43 +000027417 vctxt->hasKeyrefs = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027418
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027419 if (vctxt->value != NULL) {
27420 xmlSchemaFreeValue(vctxt->value);
27421 vctxt->value = NULL;
27422 }
27423 /*
27424 * Augmented IDC information.
27425 */
27426 if (vctxt->aidcs != NULL) {
27427 xmlSchemaIDCAugPtr cur = vctxt->aidcs, next;
27428 do {
27429 next = cur->next;
27430 xmlFree(cur);
27431 cur = next;
27432 } while (cur != NULL);
27433 vctxt->aidcs = NULL;
27434 }
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027435 if (vctxt->idcMatcherCache != NULL) {
27436 xmlSchemaIDCMatcherPtr matcher = vctxt->idcMatcherCache, tmp;
27437
27438 while (matcher) {
27439 tmp = matcher;
27440 matcher = matcher->nextCached;
27441 xmlSchemaIDCFreeMatcherList(tmp);
27442 }
27443 vctxt->idcMatcherCache = NULL;
27444 }
27445
27446
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027447 if (vctxt->idcNodes != NULL) {
27448 int i;
27449 xmlSchemaPSVIIDCNodePtr item;
27450
27451 for (i = 0; i < vctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027452 item = vctxt->idcNodes[i];
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027453 xmlFree(item->keys);
27454 xmlFree(item);
27455 }
27456 xmlFree(vctxt->idcNodes);
27457 vctxt->idcNodes = NULL;
Daniel Veillard1131e112006-08-07 11:02:54 +000027458 vctxt->nbIdcNodes = 0;
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027459 vctxt->sizeIdcNodes = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027460 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027461 /*
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027462 * Note that we won't delete the XPath state pool here.
27463 */
27464 if (vctxt->xpathStates != NULL) {
27465 xmlSchemaFreeIDCStateObjList(vctxt->xpathStates);
27466 vctxt->xpathStates = NULL;
27467 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027468 /*
27469 * Attribute info.
27470 */
27471 if (vctxt->nbAttrInfos != 0) {
27472 xmlSchemaClearAttrInfos(vctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027473 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027474 /*
27475 * Element info.
27476 */
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027477 if (vctxt->elemInfos != NULL) {
27478 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027479 xmlSchemaNodeInfoPtr ei;
27480
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027481 for (i = 0; i < vctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027482 ei = vctxt->elemInfos[i];
27483 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027484 break;
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027485 xmlSchemaClearElemInfo(vctxt, ei);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027486 }
Daniel Veillarddee23482008-04-11 12:58:43 +000027487 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027488 xmlSchemaItemListClear(vctxt->nodeQNames);
27489 /* Recreate the dict. */
27490 xmlDictFree(vctxt->dict);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027491 /*
27492 * TODO: Is is save to recreate it? Do we have a scenario
27493 * where the user provides the dict?
27494 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027495 vctxt->dict = xmlDictCreate();
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027496}
27497
27498/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027499 * xmlSchemaFreeValidCtxt:
27500 * @ctxt: the schema validation context
27501 *
27502 * Free the resources associated to the schema validation context
27503 */
27504void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027505xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt)
27506{
Daniel Veillard4255d502002-04-16 15:50:10 +000027507 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027508 return;
Daniel Veillard88c58912002-04-23 07:12:20 +000027509 if (ctxt->value != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027510 xmlSchemaFreeValue(ctxt->value);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027511 if (ctxt->pctxt != NULL)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027512 xmlSchemaFreeParserCtxt(ctxt->pctxt);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027513 if (ctxt->idcNodes != NULL) {
27514 int i;
27515 xmlSchemaPSVIIDCNodePtr item;
27516
27517 for (i = 0; i < ctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027518 item = ctxt->idcNodes[i];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027519 xmlFree(item->keys);
27520 xmlFree(item);
27521 }
27522 xmlFree(ctxt->idcNodes);
27523 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027524 if (ctxt->idcKeys != NULL) {
27525 int i;
27526 for (i = 0; i < ctxt->nbIdcKeys; i++)
27527 xmlSchemaIDCFreeKey(ctxt->idcKeys[i]);
27528 xmlFree(ctxt->idcKeys);
27529 }
27530
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027531 if (ctxt->xpathStates != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027532 xmlSchemaFreeIDCStateObjList(ctxt->xpathStates);
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027533 ctxt->xpathStates = NULL;
27534 }
27535 if (ctxt->xpathStatePool != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027536 xmlSchemaFreeIDCStateObjList(ctxt->xpathStatePool);
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027537 ctxt->xpathStatePool = NULL;
27538 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027539
27540 /*
27541 * Augmented IDC information.
27542 */
27543 if (ctxt->aidcs != NULL) {
27544 xmlSchemaIDCAugPtr cur = ctxt->aidcs, next;
27545 do {
27546 next = cur->next;
27547 xmlFree(cur);
27548 cur = next;
27549 } while (cur != NULL);
27550 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027551 if (ctxt->attrInfos != NULL) {
27552 int i;
27553 xmlSchemaAttrInfoPtr attr;
27554
27555 /* Just a paranoid call to the cleanup. */
27556 if (ctxt->nbAttrInfos != 0)
27557 xmlSchemaClearAttrInfos(ctxt);
27558 for (i = 0; i < ctxt->sizeAttrInfos; i++) {
27559 attr = ctxt->attrInfos[i];
27560 xmlFree(attr);
27561 }
27562 xmlFree(ctxt->attrInfos);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000027563 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027564 if (ctxt->elemInfos != NULL) {
27565 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027566 xmlSchemaNodeInfoPtr ei;
27567
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027568 for (i = 0; i < ctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027569 ei = ctxt->elemInfos[i];
27570 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027571 break;
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027572 xmlSchemaClearElemInfo(ctxt, ei);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027573 xmlFree(ei);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027574 }
27575 xmlFree(ctxt->elemInfos);
27576 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027577 if (ctxt->nodeQNames != NULL)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027578 xmlSchemaItemListFree(ctxt->nodeQNames);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027579 if (ctxt->dict != NULL)
27580 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000027581 xmlFree(ctxt);
27582}
27583
27584/**
Daniel Veillardf10ae122005-07-10 19:03:16 +000027585 * xmlSchemaIsValid:
27586 * @ctxt: the schema validation context
27587 *
27588 * Check if any error was detected during validation.
Daniel Veillarddee23482008-04-11 12:58:43 +000027589 *
Daniel Veillardf10ae122005-07-10 19:03:16 +000027590 * Returns 1 if valid so far, 0 if errors were detected, and -1 in case
27591 * of internal error.
27592 */
27593int
27594xmlSchemaIsValid(xmlSchemaValidCtxtPtr ctxt)
27595{
27596 if (ctxt == NULL)
27597 return(-1);
27598 return(ctxt->err == 0);
27599}
27600
27601/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027602 * xmlSchemaSetValidErrors:
27603 * @ctxt: a schema validation context
27604 * @err: the error function
27605 * @warn: the warning function
Daniel Veillarda9b66d02002-12-11 14:23:49 +000027606 * @ctx: the functions context
Daniel Veillard4255d502002-04-16 15:50:10 +000027607 *
William M. Brack2f2a6632004-08-20 23:09:47 +000027608 * Set the error and warning callback informations
Daniel Veillard4255d502002-04-16 15:50:10 +000027609 */
27610void
27611xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027612 xmlSchemaValidityErrorFunc err,
27613 xmlSchemaValidityWarningFunc warn, void *ctx)
27614{
Daniel Veillard4255d502002-04-16 15:50:10 +000027615 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027616 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000027617 ctxt->error = err;
27618 ctxt->warning = warn;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027619 ctxt->errCtxt = ctx;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027620 if (ctxt->pctxt != NULL)
27621 xmlSchemaSetParserErrors(ctxt->pctxt, err, warn, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000027622}
27623
27624/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027625 * xmlSchemaSetValidStructuredErrors:
27626 * @ctxt: a schema validation context
27627 * @serror: the structured error function
27628 * @ctx: the functions context
27629 *
27630 * Set the structured error callback
27631 */
27632void
27633xmlSchemaSetValidStructuredErrors(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027634 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027635{
27636 if (ctxt == NULL)
27637 return;
27638 ctxt->serror = serror;
27639 ctxt->error = NULL;
27640 ctxt->warning = NULL;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027641 ctxt->errCtxt = ctx;
27642 if (ctxt->pctxt != NULL)
27643 xmlSchemaSetParserStructuredErrors(ctxt->pctxt, serror, ctx);
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027644}
27645
27646/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000027647 * xmlSchemaGetValidErrors:
Daniel Veillarddee23482008-04-11 12:58:43 +000027648 * @ctxt: a XML-Schema validation context
Daniel Veillard259f0df2004-08-18 09:13:18 +000027649 * @err: the error function result
27650 * @warn: the warning function result
27651 * @ctx: the functions context result
27652 *
27653 * Get the error and warning callback informations
27654 *
27655 * Returns -1 in case of error and 0 otherwise
27656 */
27657int
27658xmlSchemaGetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027659 xmlSchemaValidityErrorFunc * err,
27660 xmlSchemaValidityWarningFunc * warn, void **ctx)
Daniel Veillard259f0df2004-08-18 09:13:18 +000027661{
27662 if (ctxt == NULL)
27663 return (-1);
27664 if (err != NULL)
27665 *err = ctxt->error;
27666 if (warn != NULL)
27667 *warn = ctxt->warning;
27668 if (ctx != NULL)
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027669 *ctx = ctxt->errCtxt;
Daniel Veillard259f0df2004-08-18 09:13:18 +000027670 return (0);
27671}
27672
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027673
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027674/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027675 * xmlSchemaSetValidOptions:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027676 * @ctxt: a schema validation context
27677 * @options: a combination of xmlSchemaValidOption
27678 *
27679 * Sets the options to be used during the validation.
27680 *
27681 * Returns 0 in case of success, -1 in case of an
27682 * API error.
27683 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027684int
27685xmlSchemaSetValidOptions(xmlSchemaValidCtxtPtr ctxt,
27686 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027687
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027688{
27689 int i;
27690
27691 if (ctxt == NULL)
27692 return (-1);
27693 /*
27694 * WARNING: Change the start value if adding to the
27695 * xmlSchemaValidOption.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027696 * TODO: Is there an other, more easy to maintain,
27697 * way?
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027698 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027699 for (i = 1; i < (int) sizeof(int) * 8; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027700 if (options & 1<<i)
27701 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027702 }
27703 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027704 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027705}
27706
27707/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027708 * xmlSchemaValidCtxtGetOptions:
Daniel Veillarddee23482008-04-11 12:58:43 +000027709 * @ctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027710 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027711 * Get the validation context options.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027712 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027713 * Returns the option combination or -1 on error.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027714 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027715int
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027716xmlSchemaValidCtxtGetOptions(xmlSchemaValidCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027717
27718{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027719 if (ctxt == NULL)
27720 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027721 else
27722 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027723}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027724
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027725static int
27726xmlSchemaVDocWalk(xmlSchemaValidCtxtPtr vctxt)
27727{
27728 xmlAttrPtr attr;
27729 int ret = 0;
27730 xmlSchemaNodeInfoPtr ielem = NULL;
27731 xmlNodePtr node, valRoot;
27732 const xmlChar *nsName;
27733
27734 /* DOC VAL TODO: Move this to the start function. */
27735 valRoot = xmlDocGetRootElement(vctxt->doc);
27736 if (valRoot == NULL) {
27737 /* VAL TODO: Error code? */
27738 VERROR(1, NULL, "The document has no document element");
27739 return (1);
27740 }
27741 vctxt->depth = -1;
27742 vctxt->validationRoot = valRoot;
27743 node = valRoot;
27744 while (node != NULL) {
27745 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27746 goto next_sibling;
27747 if (node->type == XML_ELEMENT_NODE) {
27748
27749 /*
27750 * Init the node-info.
27751 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027752 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027753 if (xmlSchemaValidatorPushElem(vctxt) == -1)
27754 goto internal_error;
27755 ielem = vctxt->inode;
27756 ielem->node = node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027757 ielem->nodeLine = node->line;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027758 ielem->localName = node->name;
27759 if (node->ns != NULL)
27760 ielem->nsName = node->ns->href;
27761 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
27762 /*
27763 * Register attributes.
27764 * DOC VAL TODO: We do not register namespace declaration
27765 * attributes yet.
27766 */
27767 vctxt->nbAttrInfos = 0;
27768 if (node->properties != NULL) {
27769 attr = node->properties;
27770 do {
27771 if (attr->ns != NULL)
27772 nsName = attr->ns->href;
27773 else
27774 nsName = NULL;
27775 ret = xmlSchemaValidatorPushAttribute(vctxt,
27776 (xmlNodePtr) attr,
Daniel Veillarddee23482008-04-11 12:58:43 +000027777 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027778 * Note that we give it the line number of the
27779 * parent element.
27780 */
27781 ielem->nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027782 attr->name, nsName, 0,
27783 xmlNodeListGetString(attr->doc, attr->children, 1), 1);
27784 if (ret == -1) {
27785 VERROR_INT("xmlSchemaDocWalk",
27786 "calling xmlSchemaValidatorPushAttribute()");
27787 goto internal_error;
27788 }
27789 attr = attr->next;
27790 } while (attr);
27791 }
27792 /*
27793 * Validate the element.
27794 */
27795 ret = xmlSchemaValidateElem(vctxt);
27796 if (ret != 0) {
27797 if (ret == -1) {
27798 VERROR_INT("xmlSchemaDocWalk",
27799 "calling xmlSchemaValidateElem()");
27800 goto internal_error;
27801 }
27802 /*
27803 * Don't stop validation; just skip the content
27804 * of this element.
27805 */
27806 goto leave_node;
27807 }
27808 if ((vctxt->skipDepth != -1) &&
27809 (vctxt->depth >= vctxt->skipDepth))
27810 goto leave_node;
27811 } else if ((node->type == XML_TEXT_NODE) ||
27812 (node->type == XML_CDATA_SECTION_NODE)) {
27813 /*
27814 * Process character content.
27815 */
Daniel Veillard14b56432006-03-09 18:41:40 +000027816 if ((ielem != NULL) && (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027817 ielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27818 ret = xmlSchemaVPushText(vctxt, node->type, node->content,
27819 -1, XML_SCHEMA_PUSH_TEXT_PERSIST, NULL);
27820 if (ret < 0) {
27821 VERROR_INT("xmlSchemaVDocWalk",
27822 "calling xmlSchemaVPushText()");
27823 goto internal_error;
27824 }
27825 /*
27826 * DOC VAL TODO: Should we skip further validation of the
27827 * element content here?
27828 */
27829 } else if ((node->type == XML_ENTITY_NODE) ||
27830 (node->type == XML_ENTITY_REF_NODE)) {
27831 /*
27832 * DOC VAL TODO: What to do with entities?
Daniel Veillarddee23482008-04-11 12:58:43 +000027833 */
Kasimier T. Buchcik146a9822006-05-04 17:19:07 +000027834 VERROR_INT("xmlSchemaVDocWalk",
27835 "there is at least one entity reference in the node-tree "
27836 "currently being validated. Processing of entities with "
27837 "this XML Schema processor is not supported (yet). Please "
27838 "substitute entities before validation.");
27839 goto internal_error;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027840 } else {
27841 goto leave_node;
27842 /*
27843 * DOC VAL TODO: XInclude nodes, etc.
27844 */
27845 }
27846 /*
27847 * Walk the doc.
27848 */
27849 if (node->children != NULL) {
27850 node = node->children;
27851 continue;
27852 }
27853leave_node:
27854 if (node->type == XML_ELEMENT_NODE) {
27855 /*
27856 * Leaving the scope of an element.
27857 */
27858 if (node != vctxt->inode->node) {
27859 VERROR_INT("xmlSchemaVDocWalk",
27860 "element position mismatch");
27861 goto internal_error;
27862 }
27863 ret = xmlSchemaValidatorPopElem(vctxt);
27864 if (ret != 0) {
27865 if (ret < 0) {
27866 VERROR_INT("xmlSchemaVDocWalk",
27867 "calling xmlSchemaValidatorPopElem()");
27868 goto internal_error;
27869 }
27870 }
27871 if (node == valRoot)
27872 goto exit;
27873 }
27874next_sibling:
27875 if (node->next != NULL)
27876 node = node->next;
27877 else {
27878 node = node->parent;
27879 goto leave_node;
27880 }
27881 }
27882
27883exit:
27884 return (ret);
27885internal_error:
27886 return (-1);
27887}
27888
27889static int
Daniel Veillardf10ae122005-07-10 19:03:16 +000027890xmlSchemaPreRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027891 /*
27892 * Some initialization.
Daniel Veillarddee23482008-04-11 12:58:43 +000027893 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027894 vctxt->err = 0;
27895 vctxt->nberrors = 0;
27896 vctxt->depth = -1;
27897 vctxt->skipDepth = -1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027898 vctxt->xsiAssemble = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027899 vctxt->hasKeyrefs = 0;
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000027900#ifdef ENABLE_IDC_NODE_TABLES_TEST
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027901 vctxt->createIDCNodeTables = 1;
27902#else
27903 vctxt->createIDCNodeTables = 0;
27904#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027905 /*
27906 * Create a schema + parser if necessary.
27907 */
27908 if (vctxt->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027909 xmlSchemaParserCtxtPtr pctxt;
Daniel Veillarddee23482008-04-11 12:58:43 +000027910
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027911 vctxt->xsiAssemble = 1;
Daniel Veillarddee23482008-04-11 12:58:43 +000027912 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027913 * If not schema was given then we will create a schema
27914 * dynamically using XSI schema locations.
27915 *
27916 * Create the schema parser context.
27917 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027918 if ((vctxt->pctxt == NULL) &&
27919 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
27920 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027921 pctxt = vctxt->pctxt;
27922 pctxt->xsiAssemble = 1;
27923 /*
27924 * Create the schema.
27925 */
27926 vctxt->schema = xmlSchemaNewSchema(pctxt);
27927 if (vctxt->schema == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000027928 return (-1);
27929 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027930 * Create the schema construction context.
27931 */
27932 pctxt->constructor = xmlSchemaConstructionCtxtCreate(pctxt->dict);
27933 if (pctxt->constructor == NULL)
27934 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000027935 pctxt->constructor->mainSchema = vctxt->schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027936 /*
27937 * Take ownership of the constructor to be able to free it.
27938 */
27939 pctxt->ownsConstructor = 1;
Daniel Veillarddee23482008-04-11 12:58:43 +000027940 }
27941 /*
27942 * Augment the IDC definitions for the main schema and all imported ones
Daniel Veillard3888f472007-08-23 09:29:03 +000027943 * NOTE: main schema if the first in the imported list
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027944 */
Daniel Veillard3888f472007-08-23 09:29:03 +000027945 xmlHashScan(vctxt->schema->schemasImports,(xmlHashScanner)xmlSchemaAugmentImportedIDC, vctxt);
Daniel Veillarddee23482008-04-11 12:58:43 +000027946
Daniel Veillardf10ae122005-07-10 19:03:16 +000027947 return(0);
27948}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027949
Daniel Veillardf10ae122005-07-10 19:03:16 +000027950static void
27951xmlSchemaPostRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027952 if (vctxt->xsiAssemble) {
27953 if (vctxt->schema != NULL) {
27954 xmlSchemaFree(vctxt->schema);
27955 vctxt->schema = NULL;
27956 }
27957 }
27958 xmlSchemaClearValidCtxt(vctxt);
Daniel Veillardf10ae122005-07-10 19:03:16 +000027959}
27960
27961static int
27962xmlSchemaVStart(xmlSchemaValidCtxtPtr vctxt)
27963{
27964 int ret = 0;
27965
27966 if (xmlSchemaPreRun(vctxt) < 0)
27967 return(-1);
27968
27969 if (vctxt->doc != NULL) {
27970 /*
27971 * Tree validation.
27972 */
27973 ret = xmlSchemaVDocWalk(vctxt);
27974#ifdef LIBXML_READER_ENABLED
27975 } else if (vctxt->reader != NULL) {
27976 /*
27977 * XML Reader validation.
27978 */
27979#ifdef XML_SCHEMA_READER_ENABLED
27980 ret = xmlSchemaVReaderWalk(vctxt);
27981#endif
27982#endif
27983 } else if ((vctxt->sax != NULL) && (vctxt->parserCtxt != NULL)) {
27984 /*
27985 * SAX validation.
27986 */
27987 ret = xmlParseDocument(vctxt->parserCtxt);
27988 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027989 VERROR_INT("xmlSchemaVStart",
Daniel Veillardf10ae122005-07-10 19:03:16 +000027990 "no instance to validate");
27991 ret = -1;
27992 }
27993
27994 xmlSchemaPostRun(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027995 if (ret == 0)
27996 ret = vctxt->err;
27997 return (ret);
27998}
27999
28000/**
28001 * xmlSchemaValidateOneElement:
28002 * @ctxt: a schema validation context
28003 * @elem: an element node
28004 *
28005 * Validate a branch of a tree, starting with the given @elem.
28006 *
28007 * Returns 0 if the element and its subtree is valid, a positive error
28008 * code number otherwise and -1 in case of an internal or API error.
28009 */
28010int
28011xmlSchemaValidateOneElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem)
28012{
28013 if ((ctxt == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
28014 return (-1);
28015
28016 if (ctxt->schema == NULL)
28017 return (-1);
28018
28019 ctxt->doc = elem->doc;
28020 ctxt->node = elem;
28021 ctxt->validationRoot = elem;
28022 return(xmlSchemaVStart(ctxt));
28023}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000028024
Daniel Veillard259f0df2004-08-18 09:13:18 +000028025/**
Daniel Veillard4255d502002-04-16 15:50:10 +000028026 * xmlSchemaValidateDoc:
28027 * @ctxt: a schema validation context
28028 * @doc: a parsed document tree
28029 *
28030 * Validate a document tree in memory.
28031 *
28032 * Returns 0 if the document is schemas valid, a positive error code
28033 * number otherwise and -1 in case of internal or API error.
28034 */
28035int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028036xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc)
28037{
Daniel Veillard4255d502002-04-16 15:50:10 +000028038 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028039 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000028040
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028041 ctxt->doc = doc;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000028042 ctxt->node = xmlDocGetRootElement(doc);
28043 if (ctxt->node == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000028044 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000028045 XML_SCHEMAV_DOCUMENT_ELEMENT_MISSING,
28046 (xmlNodePtr) doc, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028047 "The document has no document element", NULL, NULL);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000028048 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028049 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000028050 ctxt->validationRoot = ctxt->node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028051 return (xmlSchemaVStart(ctxt));
Daniel Veillard4255d502002-04-16 15:50:10 +000028052}
28053
Daniel Veillardcdc82732005-07-08 15:04:06 +000028054
28055/************************************************************************
28056 * *
28057 * Function and data for SAX streaming API *
28058 * *
28059 ************************************************************************/
28060typedef struct _xmlSchemaSplitSAXData xmlSchemaSplitSAXData;
28061typedef xmlSchemaSplitSAXData *xmlSchemaSplitSAXDataPtr;
28062
28063struct _xmlSchemaSplitSAXData {
28064 xmlSAXHandlerPtr user_sax;
28065 void *user_data;
28066 xmlSchemaValidCtxtPtr ctxt;
28067 xmlSAXHandlerPtr schemas_sax;
28068};
28069
Daniel Veillard971771e2005-07-09 17:32:57 +000028070#define XML_SAX_PLUG_MAGIC 0xdc43ba21
28071
28072struct _xmlSchemaSAXPlug {
28073 unsigned int magic;
28074
28075 /* the original callbacks informations */
28076 xmlSAXHandlerPtr *user_sax_ptr;
28077 xmlSAXHandlerPtr user_sax;
28078 void **user_data_ptr;
28079 void *user_data;
28080
28081 /* the block plugged back and validation informations */
28082 xmlSAXHandler schemas_sax;
28083 xmlSchemaValidCtxtPtr ctxt;
28084};
28085
Daniel Veillardcdc82732005-07-08 15:04:06 +000028086/* All those functions just bounces to the user provided SAX handlers */
28087static void
28088internalSubsetSplit(void *ctx, const xmlChar *name,
28089 const xmlChar *ExternalID, const xmlChar *SystemID)
28090{
Daniel Veillard971771e2005-07-09 17:32:57 +000028091 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028092 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28093 (ctxt->user_sax->internalSubset != NULL))
28094 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
28095 SystemID);
28096}
28097
28098static int
28099isStandaloneSplit(void *ctx)
28100{
Daniel Veillard971771e2005-07-09 17:32:57 +000028101 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028102 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28103 (ctxt->user_sax->isStandalone != NULL))
28104 return(ctxt->user_sax->isStandalone(ctxt->user_data));
28105 return(0);
28106}
28107
28108static int
28109hasInternalSubsetSplit(void *ctx)
28110{
Daniel Veillard971771e2005-07-09 17:32:57 +000028111 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028112 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28113 (ctxt->user_sax->hasInternalSubset != NULL))
28114 return(ctxt->user_sax->hasInternalSubset(ctxt->user_data));
28115 return(0);
28116}
28117
28118static int
28119hasExternalSubsetSplit(void *ctx)
28120{
Daniel Veillard971771e2005-07-09 17:32:57 +000028121 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028122 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28123 (ctxt->user_sax->hasExternalSubset != NULL))
28124 return(ctxt->user_sax->hasExternalSubset(ctxt->user_data));
28125 return(0);
28126}
28127
28128static void
28129externalSubsetSplit(void *ctx, const xmlChar *name,
28130 const xmlChar *ExternalID, const xmlChar *SystemID)
28131{
Daniel Veillard971771e2005-07-09 17:32:57 +000028132 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028133 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
Daniel Veillardb3e45362008-03-26 13:39:31 +000028134 (ctxt->user_sax->externalSubset != NULL))
28135 ctxt->user_sax->externalSubset(ctxt->user_data, name, ExternalID,
Daniel Veillardcdc82732005-07-08 15:04:06 +000028136 SystemID);
28137}
28138
28139static xmlParserInputPtr
28140resolveEntitySplit(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
28141{
Daniel Veillard971771e2005-07-09 17:32:57 +000028142 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028143 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28144 (ctxt->user_sax->resolveEntity != NULL))
28145 return(ctxt->user_sax->resolveEntity(ctxt->user_data, publicId,
28146 systemId));
28147 return(NULL);
28148}
28149
28150static xmlEntityPtr
28151getEntitySplit(void *ctx, const xmlChar *name)
28152{
Daniel Veillard971771e2005-07-09 17:32:57 +000028153 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028154 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28155 (ctxt->user_sax->getEntity != NULL))
28156 return(ctxt->user_sax->getEntity(ctxt->user_data, name));
28157 return(NULL);
28158}
28159
28160static xmlEntityPtr
28161getParameterEntitySplit(void *ctx, const xmlChar *name)
28162{
Daniel Veillard971771e2005-07-09 17:32:57 +000028163 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028164 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28165 (ctxt->user_sax->getParameterEntity != NULL))
28166 return(ctxt->user_sax->getParameterEntity(ctxt->user_data, name));
28167 return(NULL);
28168}
28169
28170
28171static void
28172entityDeclSplit(void *ctx, const xmlChar *name, int type,
28173 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
28174{
Daniel Veillard971771e2005-07-09 17:32:57 +000028175 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028176 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28177 (ctxt->user_sax->entityDecl != NULL))
28178 ctxt->user_sax->entityDecl(ctxt->user_data, name, type, publicId,
28179 systemId, content);
28180}
28181
28182static void
28183attributeDeclSplit(void *ctx, const xmlChar * elem,
28184 const xmlChar * name, int type, int def,
28185 const xmlChar * defaultValue, xmlEnumerationPtr tree)
28186{
Daniel Veillard971771e2005-07-09 17:32:57 +000028187 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028188 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28189 (ctxt->user_sax->attributeDecl != NULL)) {
28190 ctxt->user_sax->attributeDecl(ctxt->user_data, elem, name, type,
28191 def, defaultValue, tree);
28192 } else {
28193 xmlFreeEnumeration(tree);
28194 }
28195}
28196
28197static void
28198elementDeclSplit(void *ctx, const xmlChar *name, int type,
28199 xmlElementContentPtr content)
28200{
Daniel Veillard971771e2005-07-09 17:32:57 +000028201 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028202 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28203 (ctxt->user_sax->elementDecl != NULL))
28204 ctxt->user_sax->elementDecl(ctxt->user_data, name, type, content);
28205}
28206
28207static void
28208notationDeclSplit(void *ctx, const xmlChar *name,
28209 const xmlChar *publicId, const xmlChar *systemId)
28210{
Daniel Veillard971771e2005-07-09 17:32:57 +000028211 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028212 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28213 (ctxt->user_sax->notationDecl != NULL))
28214 ctxt->user_sax->notationDecl(ctxt->user_data, name, publicId,
28215 systemId);
28216}
28217
28218static void
28219unparsedEntityDeclSplit(void *ctx, const xmlChar *name,
28220 const xmlChar *publicId, const xmlChar *systemId,
28221 const xmlChar *notationName)
28222{
Daniel Veillard971771e2005-07-09 17:32:57 +000028223 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028224 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28225 (ctxt->user_sax->unparsedEntityDecl != NULL))
28226 ctxt->user_sax->unparsedEntityDecl(ctxt->user_data, name, publicId,
28227 systemId, notationName);
28228}
28229
28230static void
28231setDocumentLocatorSplit(void *ctx, xmlSAXLocatorPtr loc)
28232{
Daniel Veillard971771e2005-07-09 17:32:57 +000028233 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028234 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28235 (ctxt->user_sax->setDocumentLocator != NULL))
28236 ctxt->user_sax->setDocumentLocator(ctxt->user_data, loc);
28237}
28238
28239static void
28240startDocumentSplit(void *ctx)
28241{
Daniel Veillard971771e2005-07-09 17:32:57 +000028242 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028243 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28244 (ctxt->user_sax->startDocument != NULL))
28245 ctxt->user_sax->startDocument(ctxt->user_data);
28246}
28247
28248static void
28249endDocumentSplit(void *ctx)
28250{
Daniel Veillard971771e2005-07-09 17:32:57 +000028251 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028252 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28253 (ctxt->user_sax->endDocument != NULL))
28254 ctxt->user_sax->endDocument(ctxt->user_data);
28255}
28256
28257static void
28258processingInstructionSplit(void *ctx, const xmlChar *target,
28259 const xmlChar *data)
28260{
Daniel Veillard971771e2005-07-09 17:32:57 +000028261 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028262 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28263 (ctxt->user_sax->processingInstruction != NULL))
28264 ctxt->user_sax->processingInstruction(ctxt->user_data, target, data);
28265}
28266
28267static void
28268commentSplit(void *ctx, const xmlChar *value)
28269{
Daniel Veillard971771e2005-07-09 17:32:57 +000028270 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028271 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28272 (ctxt->user_sax->comment != NULL))
28273 ctxt->user_sax->comment(ctxt->user_data, value);
28274}
28275
28276/*
28277 * Varargs error callbacks to the user application, harder ...
28278 */
28279
Daniel Veillardffa3c742005-07-21 13:24:09 +000028280static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000028281warningSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028282 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028283 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28284 (ctxt->user_sax->warning != NULL)) {
28285 TODO
28286 }
28287}
Daniel Veillardffa3c742005-07-21 13:24:09 +000028288static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000028289errorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028290 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028291 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28292 (ctxt->user_sax->error != NULL)) {
28293 TODO
28294 }
28295}
Daniel Veillardffa3c742005-07-21 13:24:09 +000028296static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000028297fatalErrorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028298 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028299 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28300 (ctxt->user_sax->fatalError != NULL)) {
28301 TODO
28302 }
28303}
28304
28305/*
28306 * Those are function where both the user handler and the schemas handler
28307 * need to be called.
28308 */
28309static void
28310charactersSplit(void *ctx, const xmlChar *ch, int len)
28311{
Daniel Veillard971771e2005-07-09 17:32:57 +000028312 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028313 if (ctxt == NULL)
28314 return;
28315 if ((ctxt->user_sax != NULL) && (ctxt->user_sax->characters != NULL))
28316 ctxt->user_sax->characters(ctxt->user_data, ch, len);
28317 if (ctxt->ctxt != NULL)
28318 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
28319}
28320
28321static void
28322ignorableWhitespaceSplit(void *ctx, const xmlChar *ch, int len)
28323{
Daniel Veillard971771e2005-07-09 17:32:57 +000028324 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028325 if (ctxt == NULL)
28326 return;
28327 if ((ctxt->user_sax != NULL) &&
28328 (ctxt->user_sax->ignorableWhitespace != NULL))
28329 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, ch, len);
28330 if (ctxt->ctxt != NULL)
28331 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
28332}
28333
28334static void
28335cdataBlockSplit(void *ctx, const xmlChar *value, int len)
28336{
Daniel Veillard971771e2005-07-09 17:32:57 +000028337 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028338 if (ctxt == NULL)
28339 return;
28340 if ((ctxt->user_sax != NULL) &&
Daniel Veillard7d0ca5e2008-02-05 08:43:52 +000028341 (ctxt->user_sax->cdataBlock != NULL))
28342 ctxt->user_sax->cdataBlock(ctxt->user_data, value, len);
Daniel Veillardcdc82732005-07-08 15:04:06 +000028343 if (ctxt->ctxt != NULL)
28344 xmlSchemaSAXHandleCDataSection(ctxt->ctxt, value, len);
28345}
28346
28347static void
28348referenceSplit(void *ctx, const xmlChar *name)
28349{
Daniel Veillard971771e2005-07-09 17:32:57 +000028350 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillard14b56432006-03-09 18:41:40 +000028351 if (ctxt == NULL)
28352 return;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028353 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28354 (ctxt->user_sax->reference != NULL))
28355 ctxt->user_sax->reference(ctxt->user_data, name);
28356 if (ctxt->ctxt != NULL)
28357 xmlSchemaSAXHandleReference(ctxt->user_data, name);
28358}
28359
28360static void
Daniel Veillarddee23482008-04-11 12:58:43 +000028361startElementNsSplit(void *ctx, const xmlChar * localname,
28362 const xmlChar * prefix, const xmlChar * URI,
28363 int nb_namespaces, const xmlChar ** namespaces,
28364 int nb_attributes, int nb_defaulted,
Daniel Veillardcdc82732005-07-08 15:04:06 +000028365 const xmlChar ** attributes) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028366 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028367 if (ctxt == NULL)
28368 return;
28369 if ((ctxt->user_sax != NULL) &&
28370 (ctxt->user_sax->startElementNs != NULL))
28371 ctxt->user_sax->startElementNs(ctxt->user_data, localname, prefix,
28372 URI, nb_namespaces, namespaces,
28373 nb_attributes, nb_defaulted,
28374 attributes);
28375 if (ctxt->ctxt != NULL)
28376 xmlSchemaSAXHandleStartElementNs(ctxt->ctxt, localname, prefix,
28377 URI, nb_namespaces, namespaces,
28378 nb_attributes, nb_defaulted,
28379 attributes);
28380}
28381
28382static void
Daniel Veillarddee23482008-04-11 12:58:43 +000028383endElementNsSplit(void *ctx, const xmlChar * localname,
Daniel Veillardcdc82732005-07-08 15:04:06 +000028384 const xmlChar * prefix, const xmlChar * URI) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028385 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028386 if (ctxt == NULL)
28387 return;
28388 if ((ctxt->user_sax != NULL) &&
28389 (ctxt->user_sax->endElementNs != NULL))
28390 ctxt->user_sax->endElementNs(ctxt->user_data, localname, prefix, URI);
28391 if (ctxt->ctxt != NULL)
28392 xmlSchemaSAXHandleEndElementNs(ctxt->ctxt, localname, prefix, URI);
28393}
28394
Daniel Veillard4255d502002-04-16 15:50:10 +000028395/**
Daniel Veillard971771e2005-07-09 17:32:57 +000028396 * xmlSchemaSAXPlug:
28397 * @ctxt: a schema validation context
Daniel Veillard1f33c4d2005-07-10 21:38:31 +000028398 * @sax: a pointer to the original xmlSAXHandlerPtr
28399 * @user_data: a pointer to the original SAX user data pointer
Daniel Veillard971771e2005-07-09 17:32:57 +000028400 *
28401 * Plug a SAX based validation layer in a SAX parsing event flow.
28402 * The original @saxptr and @dataptr data are replaced by new pointers
28403 * but the calls to the original will be maintained.
28404 *
28405 * Returns a pointer to a data structure needed to unplug the validation layer
28406 * or NULL in case of errors.
28407 */
28408xmlSchemaSAXPlugPtr
28409xmlSchemaSAXPlug(xmlSchemaValidCtxtPtr ctxt,
28410 xmlSAXHandlerPtr *sax, void **user_data)
28411{
28412 xmlSchemaSAXPlugPtr ret;
28413 xmlSAXHandlerPtr old_sax;
28414
28415 if ((ctxt == NULL) || (sax == NULL) || (user_data == NULL))
28416 return(NULL);
28417
28418 /*
28419 * We only allow to plug into SAX2 event streams
28420 */
28421 old_sax = *sax;
28422 if ((old_sax != NULL) && (old_sax->initialized != XML_SAX2_MAGIC))
28423 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000028424 if ((old_sax != NULL) &&
Daniel Veillard971771e2005-07-09 17:32:57 +000028425 (old_sax->startElementNs == NULL) && (old_sax->endElementNs == NULL) &&
28426 ((old_sax->startElement != NULL) || (old_sax->endElement != NULL)))
28427 return(NULL);
28428
28429 /*
28430 * everything seems right allocate the local data needed for that layer
28431 */
28432 ret = (xmlSchemaSAXPlugPtr) xmlMalloc(sizeof(xmlSchemaSAXPlugStruct));
28433 if (ret == NULL) {
28434 return(NULL);
28435 }
28436 memset(ret, 0, sizeof(xmlSchemaSAXPlugStruct));
28437 ret->magic = XML_SAX_PLUG_MAGIC;
28438 ret->schemas_sax.initialized = XML_SAX2_MAGIC;
28439 ret->ctxt = ctxt;
28440 ret->user_sax_ptr = sax;
28441 ret->user_sax = old_sax;
Daniel Veillarddee23482008-04-11 12:58:43 +000028442 if (old_sax == NULL) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028443 /*
28444 * go direct, no need for the split block and functions.
28445 */
28446 ret->schemas_sax.startElementNs = xmlSchemaSAXHandleStartElementNs;
28447 ret->schemas_sax.endElementNs = xmlSchemaSAXHandleEndElementNs;
28448 /*
28449 * Note that we use the same text-function for both, to prevent
28450 * the parser from testing for ignorable whitespace.
28451 */
28452 ret->schemas_sax.ignorableWhitespace = xmlSchemaSAXHandleText;
28453 ret->schemas_sax.characters = xmlSchemaSAXHandleText;
28454
28455 ret->schemas_sax.cdataBlock = xmlSchemaSAXHandleCDataSection;
28456 ret->schemas_sax.reference = xmlSchemaSAXHandleReference;
28457
28458 ret->user_data = ctxt;
28459 *user_data = ctxt;
28460 } else {
28461 /*
28462 * for each callback unused by Schemas initialize it to the Split
Daniel Veillarddee23482008-04-11 12:58:43 +000028463 * routine only if non NULL in the user block, this can speed up
Daniel Veillard971771e2005-07-09 17:32:57 +000028464 * things at the SAX level.
28465 */
28466 if (old_sax->internalSubset != NULL)
28467 ret->schemas_sax.internalSubset = internalSubsetSplit;
28468 if (old_sax->isStandalone != NULL)
28469 ret->schemas_sax.isStandalone = isStandaloneSplit;
28470 if (old_sax->hasInternalSubset != NULL)
28471 ret->schemas_sax.hasInternalSubset = hasInternalSubsetSplit;
28472 if (old_sax->hasExternalSubset != NULL)
28473 ret->schemas_sax.hasExternalSubset = hasExternalSubsetSplit;
28474 if (old_sax->resolveEntity != NULL)
28475 ret->schemas_sax.resolveEntity = resolveEntitySplit;
28476 if (old_sax->getEntity != NULL)
28477 ret->schemas_sax.getEntity = getEntitySplit;
28478 if (old_sax->entityDecl != NULL)
28479 ret->schemas_sax.entityDecl = entityDeclSplit;
28480 if (old_sax->notationDecl != NULL)
28481 ret->schemas_sax.notationDecl = notationDeclSplit;
28482 if (old_sax->attributeDecl != NULL)
28483 ret->schemas_sax.attributeDecl = attributeDeclSplit;
28484 if (old_sax->elementDecl != NULL)
28485 ret->schemas_sax.elementDecl = elementDeclSplit;
28486 if (old_sax->unparsedEntityDecl != NULL)
28487 ret->schemas_sax.unparsedEntityDecl = unparsedEntityDeclSplit;
28488 if (old_sax->setDocumentLocator != NULL)
28489 ret->schemas_sax.setDocumentLocator = setDocumentLocatorSplit;
28490 if (old_sax->startDocument != NULL)
28491 ret->schemas_sax.startDocument = startDocumentSplit;
28492 if (old_sax->endDocument != NULL)
28493 ret->schemas_sax.endDocument = endDocumentSplit;
28494 if (old_sax->processingInstruction != NULL)
28495 ret->schemas_sax.processingInstruction = processingInstructionSplit;
28496 if (old_sax->comment != NULL)
28497 ret->schemas_sax.comment = commentSplit;
28498 if (old_sax->warning != NULL)
28499 ret->schemas_sax.warning = warningSplit;
28500 if (old_sax->error != NULL)
28501 ret->schemas_sax.error = errorSplit;
28502 if (old_sax->fatalError != NULL)
28503 ret->schemas_sax.fatalError = fatalErrorSplit;
28504 if (old_sax->getParameterEntity != NULL)
28505 ret->schemas_sax.getParameterEntity = getParameterEntitySplit;
28506 if (old_sax->externalSubset != NULL)
28507 ret->schemas_sax.externalSubset = externalSubsetSplit;
28508
28509 /*
28510 * the 6 schemas callback have to go to the splitter functions
28511 * Note that we use the same text-function for ignorableWhitespace
28512 * if possible, to prevent the parser from testing for ignorable
28513 * whitespace.
28514 */
28515 ret->schemas_sax.characters = charactersSplit;
28516 if ((old_sax->ignorableWhitespace != NULL) &&
28517 (old_sax->ignorableWhitespace != old_sax->characters))
28518 ret->schemas_sax.ignorableWhitespace = ignorableWhitespaceSplit;
28519 else
28520 ret->schemas_sax.ignorableWhitespace = charactersSplit;
28521 ret->schemas_sax.cdataBlock = cdataBlockSplit;
28522 ret->schemas_sax.reference = referenceSplit;
28523 ret->schemas_sax.startElementNs = startElementNsSplit;
28524 ret->schemas_sax.endElementNs = endElementNsSplit;
28525
28526 ret->user_data_ptr = user_data;
28527 ret->user_data = *user_data;
28528 *user_data = ret;
28529 }
28530
28531 /*
28532 * plug the pointers back.
28533 */
28534 *sax = &(ret->schemas_sax);
Daniel Veillardf10ae122005-07-10 19:03:16 +000028535 ctxt->sax = *sax;
28536 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28537 xmlSchemaPreRun(ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028538 return(ret);
28539}
28540
28541/**
28542 * xmlSchemaSAXUnplug:
28543 * @plug: a data structure returned by xmlSchemaSAXPlug
28544 *
28545 * Unplug a SAX based validation layer in a SAX parsing event flow.
28546 * The original pointers used in the call are restored.
28547 *
28548 * Returns 0 in case of success and -1 in case of failure.
28549 */
28550int
28551xmlSchemaSAXUnplug(xmlSchemaSAXPlugPtr plug)
28552{
28553 xmlSAXHandlerPtr *sax;
28554 void **user_data;
28555
28556 if ((plug == NULL) || (plug->magic != XML_SAX_PLUG_MAGIC))
28557 return(-1);
28558 plug->magic = 0;
28559
Daniel Veillardf10ae122005-07-10 19:03:16 +000028560 xmlSchemaPostRun(plug->ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028561 /* restore the data */
28562 sax = plug->user_sax_ptr;
28563 *sax = plug->user_sax;
28564 if (plug->user_sax != NULL) {
28565 user_data = plug->user_data_ptr;
28566 *user_data = plug->user_data;
28567 }
28568
28569 /* free and return */
28570 xmlFree(plug);
28571 return(0);
28572}
28573
28574/**
Daniel Veillard4255d502002-04-16 15:50:10 +000028575 * xmlSchemaValidateStream:
28576 * @ctxt: a schema validation context
28577 * @input: the input to use for reading the data
28578 * @enc: an optional encoding information
28579 * @sax: a SAX handler for the resulting events
28580 * @user_data: the context to provide to the SAX handler.
28581 *
Daniel Veillardcdc82732005-07-08 15:04:06 +000028582 * Validate an input based on a flow of SAX event from the parser
28583 * and forward the events to the @sax handler with the provided @user_data
28584 * the user provided @sax handler must be a SAX2 one.
Daniel Veillard4255d502002-04-16 15:50:10 +000028585 *
28586 * Returns 0 if the document is schemas valid, a positive error code
28587 * number otherwise and -1 in case of internal or API error.
28588 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028589int
Daniel Veillard4255d502002-04-16 15:50:10 +000028590xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028591 xmlParserInputBufferPtr input, xmlCharEncoding enc,
28592 xmlSAXHandlerPtr sax, void *user_data)
28593{
Daniel Veillard971771e2005-07-09 17:32:57 +000028594 xmlSchemaSAXPlugPtr plug = NULL;
28595 xmlSAXHandlerPtr old_sax = NULL;
28596 xmlParserCtxtPtr pctxt = NULL;
28597 xmlParserInputPtr inputStream = NULL;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028598 int ret;
28599
Daniel Veillard4255d502002-04-16 15:50:10 +000028600 if ((ctxt == NULL) || (input == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028601 return (-1);
Daniel Veillardcdc82732005-07-08 15:04:06 +000028602
Daniel Veillardcdc82732005-07-08 15:04:06 +000028603 /*
28604 * prepare the parser
28605 */
28606 pctxt = xmlNewParserCtxt();
28607 if (pctxt == NULL)
28608 return (-1);
28609 old_sax = pctxt->sax;
Daniel Veillard971771e2005-07-09 17:32:57 +000028610 pctxt->sax = sax;
28611 pctxt->userData = user_data;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028612#if 0
28613 if (options)
28614 xmlCtxtUseOptions(pctxt, options);
28615#endif
Daniel Veillarddee23482008-04-11 12:58:43 +000028616 pctxt->linenumbers = 1;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028617
Daniel Veillardf0af8ec2005-07-08 17:27:33 +000028618 inputStream = xmlNewIOInputStream(pctxt, input, enc);;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028619 if (inputStream == NULL) {
28620 ret = -1;
28621 goto done;
28622 }
28623 inputPush(pctxt, inputStream);
28624 ctxt->parserCtxt = pctxt;
28625 ctxt->input = input;
28626
28627 /*
Daniel Veillard971771e2005-07-09 17:32:57 +000028628 * Plug the validation and launch the parsing
Daniel Veillardcdc82732005-07-08 15:04:06 +000028629 */
Daniel Veillard971771e2005-07-09 17:32:57 +000028630 plug = xmlSchemaSAXPlug(ctxt, &(pctxt->sax), &(pctxt->userData));
28631 if (plug == NULL) {
28632 ret = -1;
28633 goto done;
28634 }
28635 ctxt->input = input;
28636 ctxt->enc = enc;
28637 ctxt->sax = pctxt->sax;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028638 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28639 ret = xmlSchemaVStart(ctxt);
28640
28641 if ((ret == 0) && (! ctxt->parserCtxt->wellFormed)) {
28642 ret = ctxt->parserCtxt->errNo;
28643 if (ret == 0)
28644 ret = 1;
Daniel Veillard33fe2882008-04-10 08:09:25 +000028645 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028646
28647done:
Daniel Veillard971771e2005-07-09 17:32:57 +000028648 ctxt->parserCtxt = NULL;
28649 ctxt->sax = NULL;
28650 ctxt->input = NULL;
28651 if (plug != NULL) {
28652 xmlSchemaSAXUnplug(plug);
28653 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028654 /* cleanup */
Daniel Veillard971771e2005-07-09 17:32:57 +000028655 if (pctxt != NULL) {
28656 pctxt->sax = old_sax;
28657 xmlFreeParserCtxt(pctxt);
28658 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028659 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +000028660}
28661
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028662/**
28663 * xmlSchemaValidateFile:
28664 * @ctxt: a schema validation context
Daniel Veillard81562d22005-06-15 13:27:56 +000028665 * @filename: the URI of the instance
28666 * @options: a future set of options, currently unused
28667 *
28668 * Do a schemas validation of the given resource, it will use the
28669 * SAX streamable validation internally.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028670 *
28671 * Returns 0 if the document is valid, a positive error code
28672 * number otherwise and -1 in case of an internal or API error.
28673 */
28674int
28675xmlSchemaValidateFile(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000028676 const char * filename,
28677 int options ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028678{
28679 int ret;
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028680 xmlParserInputBufferPtr input;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028681
28682 if ((ctxt == NULL) || (filename == NULL))
28683 return (-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000028684
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028685 input = xmlParserInputBufferCreateFilename(filename,
28686 XML_CHAR_ENCODING_NONE);
28687 if (input == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028688 return (-1);
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028689 ret = xmlSchemaValidateStream(ctxt, input, XML_CHAR_ENCODING_NONE,
Daniel Veillarddee23482008-04-11 12:58:43 +000028690 NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028691 return (ret);
Daniel Veillard81562d22005-06-15 13:27:56 +000028692}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028693
Daniel Veillarddee23482008-04-11 12:58:43 +000028694/**
28695 * xmlSchemaValidCtxtGetParserCtxt:
28696 * @ctxt: a schema validation context
28697 *
28698 * allow access to the parser context of the schema validation context
28699 *
28700 * Returns the parser context of the schema validation context or NULL
28701 * in case of error.
28702 */
28703xmlParserCtxtPtr
28704xmlSchemaValidCtxtGetParserCtxt(xmlSchemaValidCtxtPtr ctxt)
28705{
28706 if (ctxt == NULL)
28707 return(NULL);
28708 return (ctxt->parserCtxt);
28709}
28710
Daniel Veillard5d4644e2005-04-01 13:11:58 +000028711#define bottom_xmlschemas
28712#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +000028713#endif /* LIBXML_SCHEMAS_ENABLED */