blob: d42afb776e95c611f8376920f0a5c73a72aa37ec [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)
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800110#define TODO \
Daniel Veillard4255d502002-04-16 15:50:10 +0000111 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
Jan Pokornýbb654fe2016-04-13 16:56:07 +0200620 xmlDictPtr dict; /* dictionary for interned string names */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000621 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 Veillard97fa5b32012-08-14 11:01:07 +0800978 char *filename;
Daniel Veillard4255d502002-04-16 15:50:10 +0000979
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000980 int err;
981 int nberrors;
Daniel Veillard4255d502002-04-16 15:50:10 +0000982
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000983 xmlNodePtr node;
984 xmlNodePtr cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000985 /* xmlSchemaTypePtr type; */
Daniel Veillard4255d502002-04-16 15:50:10 +0000986
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000987 xmlRegExecCtxtPtr regexp;
988 xmlSchemaValPtr value;
Daniel Veillard4255d502002-04-16 15:50:10 +0000989
Daniel Veillardc0826a72004-08-10 14:17:33 +0000990 int valueWS;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000991 int options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000992 xmlNodePtr validationRoot;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000993 xmlSchemaParserCtxtPtr pctxt;
994 int xsiAssemble;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000995
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000996 int depth;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000997 xmlSchemaNodeInfoPtr *elemInfos; /* array of element informations */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000998 int sizeElemInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000999 xmlSchemaNodeInfoPtr inode; /* the current element information */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001000
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00001001 xmlSchemaIDCAugPtr aidcs; /* a list of augmented IDC informations */
1002
1003 xmlSchemaIDCStateObjPtr xpathStates; /* first active state object. */
1004 xmlSchemaIDCStateObjPtr xpathStatePool; /* first stored state object. */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +00001005 xmlSchemaIDCMatcherPtr idcMatcherCache; /* Cache for IDC matcher objects. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001006
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00001007 xmlSchemaPSVIIDCNodePtr *idcNodes; /* list of all IDC node-table entries*/
1008 int nbIdcNodes;
1009 int sizeIdcNodes;
1010
1011 xmlSchemaPSVIIDCKeyPtr *idcKeys; /* list of all IDC node-table entries */
1012 int nbIdcKeys;
1013 int sizeIdcKeys;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001014
1015 int flags;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001016
1017 xmlDictPtr dict;
1018
Daniel Veillard39e5c892005-07-03 22:48:50 +00001019#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001020 xmlTextReaderPtr reader;
Daniel Veillard39e5c892005-07-03 22:48:50 +00001021#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001022
1023 xmlSchemaAttrInfoPtr *attrInfos;
1024 int nbAttrInfos;
1025 int sizeAttrInfos;
1026
1027 int skipDepth;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00001028 xmlSchemaItemListPtr nodeQNames;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00001029 int hasKeyrefs;
1030 int createIDCNodeTables;
1031 int psviExposeIDCNodeTables;
Daniel Veillard97fa5b32012-08-14 11:01:07 +08001032
1033 /* Locator for error reporting in streaming mode */
1034 xmlSchemaValidityLocatorFunc locFunc;
1035 void *locCtxt;
Daniel Veillard4255d502002-04-16 15:50:10 +00001036};
1037
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00001038/**
1039 * xmlSchemaSubstGroup:
1040 *
1041 *
1042 */
1043typedef struct _xmlSchemaSubstGroup xmlSchemaSubstGroup;
1044typedef xmlSchemaSubstGroup *xmlSchemaSubstGroupPtr;
1045struct _xmlSchemaSubstGroup {
1046 xmlSchemaElementPtr head;
1047 xmlSchemaItemListPtr members;
1048};
1049
Daniel Veillard4255d502002-04-16 15:50:10 +00001050/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001051 * *
1052 * Some predeclarations *
1053 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00001054 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001055
Daniel Veillardbd2904b2003-11-25 15:38:59 +00001056static int xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt,
1057 xmlSchemaPtr schema,
1058 xmlNodePtr node);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00001059static int xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr ctxt,
1060 xmlSchemaPtr schema,
1061 xmlNodePtr node);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001062static int
1063xmlSchemaTypeFixup(xmlSchemaTypePtr type,
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +00001064 xmlSchemaAbstractCtxtPtr ctxt);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00001065static const xmlChar *
Daniel Veillard01fa6152004-06-29 17:04:39 +00001066xmlSchemaFacetTypeToString(xmlSchemaTypeType type);
1067static int
William M. Brack2f2a6632004-08-20 23:09:47 +00001068xmlSchemaParseImport(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1069 xmlNodePtr node);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001070static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001071xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
1072 xmlSchemaParserCtxtPtr ctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +00001073static void
1074xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001075static xmlSchemaWhitespaceValueType
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001076xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001077static xmlSchemaTreeItemPtr
1078xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1079 xmlNodePtr node, xmlSchemaTypeType type,
1080 int withParticle);
1081static const xmlChar *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001082xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001083static xmlSchemaTypeLinkPtr
1084xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001085static void
1086xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
1087 const char *funcName,
David Kilzer4472c3a2016-05-13 15:13:17 +08001088 const char *message) LIBXML_ATTR_FORMAT(3,0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001089static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +00001090xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001091 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001092 xmlSchemaTypePtr baseType,
1093 int subset);
1094static void
1095xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001096 xmlSchemaParserCtxtPtr ctxt);
1097static void
1098xmlSchemaComponentListFree(xmlSchemaItemListPtr list);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001099static xmlSchemaQNameRefPtr
1100xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
1101 xmlSchemaPtr schema,
1102 xmlNodePtr node);
William M. Brack87640d52004-04-17 14:58:15 +00001103
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001104/************************************************************************
1105 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001106 * Helper functions *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001107 * *
1108 ************************************************************************/
1109
1110/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001111 * xmlSchemaItemTypeToStr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001112 * @type: the type of the schema item
1113 *
1114 * Returns the component name of a schema item.
1115 */
1116static const xmlChar *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001117xmlSchemaItemTypeToStr(xmlSchemaTypeType type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001118{
1119 switch (type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001120 case XML_SCHEMA_TYPE_BASIC:
1121 return(BAD_CAST "simple type definition");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001122 case XML_SCHEMA_TYPE_SIMPLE:
1123 return(BAD_CAST "simple type definition");
1124 case XML_SCHEMA_TYPE_COMPLEX:
1125 return(BAD_CAST "complex type definition");
1126 case XML_SCHEMA_TYPE_ELEMENT:
1127 return(BAD_CAST "element declaration");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001128 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1129 return(BAD_CAST "attribute use");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001130 case XML_SCHEMA_TYPE_ATTRIBUTE:
1131 return(BAD_CAST "attribute declaration");
1132 case XML_SCHEMA_TYPE_GROUP:
1133 return(BAD_CAST "model group definition");
1134 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1135 return(BAD_CAST "attribute group definition");
1136 case XML_SCHEMA_TYPE_NOTATION:
1137 return(BAD_CAST "notation declaration");
1138 case XML_SCHEMA_TYPE_SEQUENCE:
1139 return(BAD_CAST "model group (sequence)");
1140 case XML_SCHEMA_TYPE_CHOICE:
1141 return(BAD_CAST "model group (choice)");
1142 case XML_SCHEMA_TYPE_ALL:
1143 return(BAD_CAST "model group (all)");
1144 case XML_SCHEMA_TYPE_PARTICLE:
1145 return(BAD_CAST "particle");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001146 case XML_SCHEMA_TYPE_IDC_UNIQUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001147 return(BAD_CAST "unique identity-constraint");
1148 /* return(BAD_CAST "IDC (unique)"); */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001149 case XML_SCHEMA_TYPE_IDC_KEY:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001150 return(BAD_CAST "key identity-constraint");
1151 /* return(BAD_CAST "IDC (key)"); */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001152 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001153 return(BAD_CAST "keyref identity-constraint");
1154 /* return(BAD_CAST "IDC (keyref)"); */
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00001155 case XML_SCHEMA_TYPE_ANY:
1156 return(BAD_CAST "wildcard (any)");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001157 case XML_SCHEMA_EXTRA_QNAMEREF:
1158 return(BAD_CAST "[helper component] QName reference");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001159 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
1160 return(BAD_CAST "[helper component] attribute use prohibition");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001161 default:
1162 return(BAD_CAST "Not a schema component");
1163 }
1164}
1165
1166/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001167 * xmlSchemaGetComponentTypeStr:
1168 * @type: the type of the schema item
1169 *
1170 * Returns the component name of a schema item.
1171 */
1172static const xmlChar *
1173xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item)
1174{
1175 switch (item->type) {
1176 case XML_SCHEMA_TYPE_BASIC:
1177 if (WXS_IS_COMPLEX(WXS_TYPE_CAST item))
1178 return(BAD_CAST "complex type definition");
1179 else
Daniel Veillarddee23482008-04-11 12:58:43 +00001180 return(BAD_CAST "simple type definition");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001181 default:
1182 return(xmlSchemaItemTypeToStr(item->type));
1183 }
1184}
1185
1186/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001187 * xmlSchemaGetComponentNode:
1188 * @item: a schema component
1189 *
1190 * Returns node associated with the schema component.
1191 * NOTE that such a node need not be available; plus, a component's
1192 * node need not to reflect the component directly, since there is no
1193 * one-to-one relationship between the XML Schema representation and
1194 * the component representation.
1195 */
1196static xmlNodePtr
1197xmlSchemaGetComponentNode(xmlSchemaBasicItemPtr item)
1198{
1199 switch (item->type) {
1200 case XML_SCHEMA_TYPE_ELEMENT:
1201 return (((xmlSchemaElementPtr) item)->node);
1202 case XML_SCHEMA_TYPE_ATTRIBUTE:
1203 return (((xmlSchemaAttributePtr) item)->node);
1204 case XML_SCHEMA_TYPE_COMPLEX:
1205 case XML_SCHEMA_TYPE_SIMPLE:
1206 return (((xmlSchemaTypePtr) item)->node);
1207 case XML_SCHEMA_TYPE_ANY:
1208 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1209 return (((xmlSchemaWildcardPtr) item)->node);
1210 case XML_SCHEMA_TYPE_PARTICLE:
1211 return (((xmlSchemaParticlePtr) item)->node);
1212 case XML_SCHEMA_TYPE_SEQUENCE:
1213 case XML_SCHEMA_TYPE_CHOICE:
1214 case XML_SCHEMA_TYPE_ALL:
1215 return (((xmlSchemaModelGroupPtr) item)->node);
1216 case XML_SCHEMA_TYPE_GROUP:
1217 return (((xmlSchemaModelGroupDefPtr) item)->node);
1218 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1219 return (((xmlSchemaAttributeGroupPtr) item)->node);
1220 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1221 case XML_SCHEMA_TYPE_IDC_KEY:
1222 case XML_SCHEMA_TYPE_IDC_KEYREF:
1223 return (((xmlSchemaIDCPtr) item)->node);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001224 case XML_SCHEMA_EXTRA_QNAMEREF:
1225 return(((xmlSchemaQNameRefPtr) item)->node);
1226 /* TODO: What to do with NOTATIONs?
1227 case XML_SCHEMA_TYPE_NOTATION:
1228 return (((xmlSchemaNotationPtr) item)->node);
1229 */
1230 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1231 return (((xmlSchemaAttributeUsePtr) item)->node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001232 default:
1233 return (NULL);
1234 }
1235}
1236
1237#if 0
1238/**
1239 * xmlSchemaGetNextComponent:
1240 * @item: a schema component
1241 *
1242 * Returns the next sibling of the schema component.
1243 */
1244static xmlSchemaBasicItemPtr
1245xmlSchemaGetNextComponent(xmlSchemaBasicItemPtr item)
1246{
1247 switch (item->type) {
1248 case XML_SCHEMA_TYPE_ELEMENT:
1249 return ((xmlSchemaBasicItemPtr) ((xmlSchemaElementPtr) item)->next);
1250 case XML_SCHEMA_TYPE_ATTRIBUTE:
1251 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributePtr) item)->next);
1252 case XML_SCHEMA_TYPE_COMPLEX:
1253 case XML_SCHEMA_TYPE_SIMPLE:
1254 return ((xmlSchemaBasicItemPtr) ((xmlSchemaTypePtr) item)->next);
1255 case XML_SCHEMA_TYPE_ANY:
1256 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1257 return (NULL);
1258 case XML_SCHEMA_TYPE_PARTICLE:
1259 return ((xmlSchemaBasicItemPtr) ((xmlSchemaParticlePtr) item)->next);
1260 case XML_SCHEMA_TYPE_SEQUENCE:
1261 case XML_SCHEMA_TYPE_CHOICE:
1262 case XML_SCHEMA_TYPE_ALL:
1263 return (NULL);
1264 case XML_SCHEMA_TYPE_GROUP:
1265 return (NULL);
1266 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1267 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributeGroupPtr) item)->next);
1268 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1269 case XML_SCHEMA_TYPE_IDC_KEY:
1270 case XML_SCHEMA_TYPE_IDC_KEYREF:
1271 return ((xmlSchemaBasicItemPtr) ((xmlSchemaIDCPtr) item)->next);
1272 default:
1273 return (NULL);
1274 }
1275}
1276#endif
1277
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001278
1279/**
1280 * xmlSchemaFormatQName:
1281 * @buf: the string buffer
1282 * @namespaceName: the namespace name
1283 * @localName: the local name
1284 *
1285 * Returns the given QName in the format "{namespaceName}localName" or
1286 * just "localName" if @namespaceName is NULL.
1287 *
1288 * Returns the localName if @namespaceName is NULL, a formatted
1289 * string otherwise.
1290 */
1291static const xmlChar*
1292xmlSchemaFormatQName(xmlChar **buf,
1293 const xmlChar *namespaceName,
1294 const xmlChar *localName)
1295{
1296 FREE_AND_NULL(*buf)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001297 if (namespaceName != NULL) {
1298 *buf = xmlStrdup(BAD_CAST "{");
1299 *buf = xmlStrcat(*buf, namespaceName);
1300 *buf = xmlStrcat(*buf, BAD_CAST "}");
1301 }
1302 if (localName != NULL) {
1303 if (namespaceName == NULL)
1304 return(localName);
1305 *buf = xmlStrcat(*buf, localName);
1306 } else {
1307 *buf = xmlStrcat(*buf, BAD_CAST "(NULL)");
Daniel Veillarddee23482008-04-11 12:58:43 +00001308 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001309 return ((const xmlChar *) *buf);
1310}
1311
Daniel Veillarddee23482008-04-11 12:58:43 +00001312static const xmlChar*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001313xmlSchemaFormatQNameNs(xmlChar **buf, xmlNsPtr ns, const xmlChar *localName)
1314{
1315 if (ns != NULL)
1316 return (xmlSchemaFormatQName(buf, ns->href, localName));
1317 else
1318 return (xmlSchemaFormatQName(buf, NULL, localName));
1319}
1320
1321static const xmlChar *
1322xmlSchemaGetComponentName(xmlSchemaBasicItemPtr item)
1323{
1324 switch (item->type) {
1325 case XML_SCHEMA_TYPE_ELEMENT:
1326 return (((xmlSchemaElementPtr) item)->name);
1327 case XML_SCHEMA_TYPE_ATTRIBUTE:
1328 return (((xmlSchemaAttributePtr) item)->name);
1329 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1330 return (((xmlSchemaAttributeGroupPtr) item)->name);
1331 case XML_SCHEMA_TYPE_BASIC:
1332 case XML_SCHEMA_TYPE_SIMPLE:
1333 case XML_SCHEMA_TYPE_COMPLEX:
1334 return (((xmlSchemaTypePtr) item)->name);
1335 case XML_SCHEMA_TYPE_GROUP:
1336 return (((xmlSchemaModelGroupDefPtr) item)->name);
1337 case XML_SCHEMA_TYPE_IDC_KEY:
1338 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1339 case XML_SCHEMA_TYPE_IDC_KEYREF:
1340 return (((xmlSchemaIDCPtr) item)->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001341 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1342 if (WXS_ATTRUSE_DECL(item) != NULL) {
1343 return(xmlSchemaGetComponentName(
1344 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1345 } else
1346 return(NULL);
1347 case XML_SCHEMA_EXTRA_QNAMEREF:
1348 return (((xmlSchemaQNameRefPtr) item)->name);
1349 case XML_SCHEMA_TYPE_NOTATION:
1350 return (((xmlSchemaNotationPtr) item)->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001351 default:
1352 /*
1353 * Other components cannot have names.
1354 */
1355 break;
1356 }
1357 return (NULL);
1358}
1359
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001360#define xmlSchemaGetQNameRefName(r) (WXS_QNAME_CAST (r))->name
1361#define xmlSchemaGetQNameRefTargetNs(r) (WXS_QNAME_CAST (r))->targetNamespace
1362/*
1363static const xmlChar *
1364xmlSchemaGetQNameRefName(void *ref)
1365{
1366 return(((xmlSchemaQNameRefPtr) ref)->name);
1367}
1368
1369static const xmlChar *
1370xmlSchemaGetQNameRefTargetNs(void *ref)
1371{
1372 return(((xmlSchemaQNameRefPtr) ref)->targetNamespace);
1373}
1374*/
1375
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001376static const xmlChar *
1377xmlSchemaGetComponentTargetNs(xmlSchemaBasicItemPtr item)
1378{
1379 switch (item->type) {
1380 case XML_SCHEMA_TYPE_ELEMENT:
1381 return (((xmlSchemaElementPtr) item)->targetNamespace);
1382 case XML_SCHEMA_TYPE_ATTRIBUTE:
1383 return (((xmlSchemaAttributePtr) item)->targetNamespace);
1384 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1385 return (((xmlSchemaAttributeGroupPtr) item)->targetNamespace);
1386 case XML_SCHEMA_TYPE_BASIC:
1387 return (BAD_CAST "http://www.w3.org/2001/XMLSchema");
1388 case XML_SCHEMA_TYPE_SIMPLE:
1389 case XML_SCHEMA_TYPE_COMPLEX:
1390 return (((xmlSchemaTypePtr) item)->targetNamespace);
1391 case XML_SCHEMA_TYPE_GROUP:
1392 return (((xmlSchemaModelGroupDefPtr) item)->targetNamespace);
1393 case XML_SCHEMA_TYPE_IDC_KEY:
1394 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1395 case XML_SCHEMA_TYPE_IDC_KEYREF:
1396 return (((xmlSchemaIDCPtr) item)->targetNamespace);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001397 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1398 if (WXS_ATTRUSE_DECL(item) != NULL) {
1399 return(xmlSchemaGetComponentTargetNs(
1400 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1401 }
1402 /* TODO: Will returning NULL break something? */
1403 break;
1404 case XML_SCHEMA_EXTRA_QNAMEREF:
1405 return (((xmlSchemaQNameRefPtr) item)->targetNamespace);
1406 case XML_SCHEMA_TYPE_NOTATION:
1407 return (((xmlSchemaNotationPtr) item)->targetNamespace);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001408 default:
1409 /*
1410 * Other components cannot have names.
1411 */
1412 break;
1413 }
1414 return (NULL);
1415}
1416
1417static const xmlChar*
1418xmlSchemaGetComponentQName(xmlChar **buf,
1419 void *item)
1420{
1421 return (xmlSchemaFormatQName(buf,
1422 xmlSchemaGetComponentTargetNs((xmlSchemaBasicItemPtr) item),
1423 xmlSchemaGetComponentName((xmlSchemaBasicItemPtr) item)));
1424}
1425
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001426static const xmlChar*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001427xmlSchemaGetComponentDesignation(xmlChar **buf, void *item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001428{
1429 xmlChar *str = NULL;
1430
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001431 *buf = xmlStrcat(*buf, WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001432 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001433 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str,
1434 (xmlSchemaBasicItemPtr) item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001435 *buf = xmlStrcat(*buf, BAD_CAST "'");
1436 FREE_AND_NULL(str);
1437 return(*buf);
1438}
1439
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001440static const xmlChar*
1441xmlSchemaGetIDCDesignation(xmlChar **buf, xmlSchemaIDCPtr idc)
1442{
1443 return(xmlSchemaGetComponentDesignation(buf, idc));
1444}
1445
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001446/**
1447 * xmlSchemaWildcardPCToString:
1448 * @pc: the type of processContents
1449 *
1450 * Returns a string representation of the type of
1451 * processContents.
1452 */
1453static const xmlChar *
1454xmlSchemaWildcardPCToString(int pc)
1455{
1456 switch (pc) {
1457 case XML_SCHEMAS_ANY_SKIP:
1458 return (BAD_CAST "skip");
1459 case XML_SCHEMAS_ANY_LAX:
1460 return (BAD_CAST "lax");
1461 case XML_SCHEMAS_ANY_STRICT:
1462 return (BAD_CAST "strict");
1463 default:
1464 return (BAD_CAST "invalid process contents");
1465 }
1466}
1467
1468/**
1469 * xmlSchemaGetCanonValueWhtspExt:
1470 * @val: the precomputed value
1471 * @retValue: the returned value
1472 * @ws: the whitespace type of the value
1473 *
Jan Pokorný7a7cad62013-11-29 23:26:26 +01001474 * Get a the canonical representation of the value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001475 * The caller has to free the returned retValue.
1476 *
1477 * Returns 0 if the value could be built and -1 in case of
1478 * API errors or if the value type is not supported yet.
1479 */
1480static int
1481xmlSchemaGetCanonValueWhtspExt(xmlSchemaValPtr val,
1482 xmlSchemaWhitespaceValueType ws,
1483 xmlChar **retValue)
1484{
1485 int list;
1486 xmlSchemaValType valType;
1487 const xmlChar *value, *value2 = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00001488
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001489
1490 if ((retValue == NULL) || (val == NULL))
1491 return (-1);
1492 list = xmlSchemaValueGetNext(val) ? 1 : 0;
1493 *retValue = NULL;
1494 do {
Daniel Veillarddee23482008-04-11 12:58:43 +00001495 value = NULL;
1496 valType = xmlSchemaGetValType(val);
1497 switch (valType) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001498 case XML_SCHEMAS_STRING:
1499 case XML_SCHEMAS_NORMSTRING:
1500 case XML_SCHEMAS_ANYSIMPLETYPE:
1501 value = xmlSchemaValueGetAsString(val);
1502 if (value != NULL) {
1503 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
1504 value2 = xmlSchemaCollapseString(value);
1505 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
1506 value2 = xmlSchemaWhiteSpaceReplace(value);
1507 if (value2 != NULL)
1508 value = value2;
1509 }
Daniel Veillarddee23482008-04-11 12:58:43 +00001510 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001511 default:
1512 if (xmlSchemaGetCanonValue(val, &value2) == -1) {
1513 if (value2 != NULL)
1514 xmlFree((xmlChar *) value2);
1515 goto internal_error;
1516 }
1517 value = value2;
1518 }
1519 if (*retValue == NULL)
1520 if (value == NULL) {
1521 if (! list)
1522 *retValue = xmlStrdup(BAD_CAST "");
1523 } else
1524 *retValue = xmlStrdup(value);
1525 else if (value != NULL) {
1526 /* List. */
1527 *retValue = xmlStrcat((xmlChar *) *retValue, BAD_CAST " ");
1528 *retValue = xmlStrcat((xmlChar *) *retValue, value);
1529 }
1530 FREE_AND_NULL(value2)
1531 val = xmlSchemaValueGetNext(val);
1532 } while (val != NULL);
1533
1534 return (0);
1535internal_error:
1536 if (*retValue != NULL)
1537 xmlFree((xmlChar *) (*retValue));
1538 if (value2 != NULL)
1539 xmlFree((xmlChar *) value2);
1540 return (-1);
1541}
1542
1543/**
1544 * xmlSchemaFormatItemForReport:
1545 * @buf: the string buffer
1546 * @itemDes: the designation of the item
1547 * @itemName: the name of the item
Daniel Veillarddee23482008-04-11 12:58:43 +00001548 * @item: the item as an object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001549 * @itemNode: the node of the item
1550 * @local: the local name
1551 * @parsing: if the function is used during the parse
1552 *
1553 * Returns a representation of the given item used
Daniel Veillarddee23482008-04-11 12:58:43 +00001554 * for error reports.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001555 *
Daniel Veillarddee23482008-04-11 12:58:43 +00001556 * The following order is used to build the resulting
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001557 * designation if the arguments are not NULL:
1558 * 1a. If itemDes not NULL -> itemDes
1559 * 1b. If (itemDes not NULL) and (itemName not NULL)
1560 * -> itemDes + itemName
1561 * 2. If the preceding was NULL and (item not NULL) -> item
1562 * 3. If the preceding was NULL and (itemNode not NULL) -> itemNode
Daniel Veillarddee23482008-04-11 12:58:43 +00001563 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001564 * If the itemNode is an attribute node, the name of the attribute
1565 * will be appended to the result.
1566 *
1567 * Returns the formatted string and sets @buf to the resulting value.
Daniel Veillarddee23482008-04-11 12:58:43 +00001568 */
1569static xmlChar*
1570xmlSchemaFormatItemForReport(xmlChar **buf,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001571 const xmlChar *itemDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001572 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001573 xmlNodePtr itemNode)
1574{
1575 xmlChar *str = NULL;
1576 int named = 1;
1577
1578 if (*buf != NULL) {
1579 xmlFree(*buf);
1580 *buf = NULL;
1581 }
Daniel Veillarddee23482008-04-11 12:58:43 +00001582
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001583 if (itemDes != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +00001584 *buf = xmlStrdup(itemDes);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001585 } else if (item != NULL) {
1586 switch (item->type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001587 case XML_SCHEMA_TYPE_BASIC: {
1588 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1589
1590 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001591 *buf = xmlStrdup(BAD_CAST "atomic type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001592 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001593 *buf = xmlStrdup(BAD_CAST "list type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001594 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001595 *buf = xmlStrdup(BAD_CAST "union type 'xs:");
1596 else
1597 *buf = xmlStrdup(BAD_CAST "simple type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001598 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001599 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001600 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001601 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001602 case XML_SCHEMA_TYPE_SIMPLE: {
1603 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1604
1605 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001606 *buf = xmlStrdup(BAD_CAST"");
1607 } else {
1608 *buf = xmlStrdup(BAD_CAST "local ");
1609 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001610 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001611 *buf = xmlStrcat(*buf, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001612 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001613 *buf = xmlStrcat(*buf, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001614 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001615 *buf = xmlStrcat(*buf, BAD_CAST "union type");
1616 else
1617 *buf = xmlStrcat(*buf, BAD_CAST "simple type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001618 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001619 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001620 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001621 *buf = xmlStrcat(*buf, BAD_CAST "'");
1622 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001623 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001624 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001625 case XML_SCHEMA_TYPE_COMPLEX: {
1626 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1627
1628 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001629 *buf = xmlStrdup(BAD_CAST "");
1630 else
1631 *buf = xmlStrdup(BAD_CAST "local ");
1632 *buf = xmlStrcat(*buf, BAD_CAST "complex type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001633 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001634 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001635 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001636 *buf = xmlStrcat(*buf, BAD_CAST "'");
1637 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001638 }
1639 break;
1640 case XML_SCHEMA_TYPE_ATTRIBUTE_USE: {
1641 xmlSchemaAttributeUsePtr ause;
Daniel Veillarddee23482008-04-11 12:58:43 +00001642
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001643 ause = WXS_ATTR_USE_CAST item;
1644 *buf = xmlStrdup(BAD_CAST "attribute use ");
1645 if (WXS_ATTRUSE_DECL(ause) != NULL) {
1646 *buf = xmlStrcat(*buf, BAD_CAST "'");
1647 *buf = xmlStrcat(*buf,
1648 xmlSchemaGetComponentQName(&str, WXS_ATTRUSE_DECL(ause)));
1649 FREE_AND_NULL(str)
1650 *buf = xmlStrcat(*buf, BAD_CAST "'");
1651 } else {
1652 *buf = xmlStrcat(*buf, BAD_CAST "(unknown)");
1653 }
1654 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001655 break;
1656 case XML_SCHEMA_TYPE_ATTRIBUTE: {
1657 xmlSchemaAttributePtr attr;
Daniel Veillarddee23482008-04-11 12:58:43 +00001658
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001659 attr = (xmlSchemaAttributePtr) item;
1660 *buf = xmlStrdup(BAD_CAST "attribute decl.");
1661 *buf = xmlStrcat(*buf, BAD_CAST " '");
1662 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1663 attr->targetNamespace, attr->name));
1664 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001665 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001666 }
1667 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001668 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1669 xmlSchemaGetComponentDesignation(buf, item);
1670 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001671 case XML_SCHEMA_TYPE_ELEMENT: {
1672 xmlSchemaElementPtr elem;
1673
Daniel Veillarddee23482008-04-11 12:58:43 +00001674 elem = (xmlSchemaElementPtr) item;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001675 *buf = xmlStrdup(BAD_CAST "element decl.");
1676 *buf = xmlStrcat(*buf, BAD_CAST " '");
1677 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1678 elem->targetNamespace, elem->name));
1679 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001680 }
1681 break;
1682 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1683 case XML_SCHEMA_TYPE_IDC_KEY:
Daniel Veillarddee23482008-04-11 12:58:43 +00001684 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001685 if (item->type == XML_SCHEMA_TYPE_IDC_UNIQUE)
1686 *buf = xmlStrdup(BAD_CAST "unique '");
1687 else if (item->type == XML_SCHEMA_TYPE_IDC_KEY)
1688 *buf = xmlStrdup(BAD_CAST "key '");
1689 else
1690 *buf = xmlStrdup(BAD_CAST "keyRef '");
1691 *buf = xmlStrcat(*buf, ((xmlSchemaIDCPtr) item)->name);
1692 *buf = xmlStrcat(*buf, BAD_CAST "'");
1693 break;
1694 case XML_SCHEMA_TYPE_ANY:
1695 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1696 *buf = xmlStrdup(xmlSchemaWildcardPCToString(
1697 ((xmlSchemaWildcardPtr) item)->processContents));
1698 *buf = xmlStrcat(*buf, BAD_CAST " wildcard");
1699 break;
1700 case XML_SCHEMA_FACET_MININCLUSIVE:
1701 case XML_SCHEMA_FACET_MINEXCLUSIVE:
1702 case XML_SCHEMA_FACET_MAXINCLUSIVE:
1703 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
1704 case XML_SCHEMA_FACET_TOTALDIGITS:
1705 case XML_SCHEMA_FACET_FRACTIONDIGITS:
1706 case XML_SCHEMA_FACET_PATTERN:
1707 case XML_SCHEMA_FACET_ENUMERATION:
1708 case XML_SCHEMA_FACET_WHITESPACE:
1709 case XML_SCHEMA_FACET_LENGTH:
1710 case XML_SCHEMA_FACET_MAXLENGTH:
1711 case XML_SCHEMA_FACET_MINLENGTH:
1712 *buf = xmlStrdup(BAD_CAST "facet '");
1713 *buf = xmlStrcat(*buf, xmlSchemaFacetTypeToString(item->type));
1714 *buf = xmlStrcat(*buf, BAD_CAST "'");
1715 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001716 case XML_SCHEMA_TYPE_GROUP: {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001717 *buf = xmlStrdup(BAD_CAST "model group def.");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001718 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001719 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001720 *buf = xmlStrcat(*buf, BAD_CAST "'");
1721 FREE_AND_NULL(str)
1722 }
1723 break;
1724 case XML_SCHEMA_TYPE_SEQUENCE:
1725 case XML_SCHEMA_TYPE_CHOICE:
1726 case XML_SCHEMA_TYPE_ALL:
1727 case XML_SCHEMA_TYPE_PARTICLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001728 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1729 break;
1730 case XML_SCHEMA_TYPE_NOTATION: {
1731 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1732 *buf = xmlStrcat(*buf, BAD_CAST " '");
1733 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
1734 *buf = xmlStrcat(*buf, BAD_CAST "'");
1735 FREE_AND_NULL(str);
1736 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001737 default:
1738 named = 0;
1739 }
Daniel Veillarddee23482008-04-11 12:58:43 +00001740 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001741 named = 0;
1742
1743 if ((named == 0) && (itemNode != NULL)) {
1744 xmlNodePtr elem;
1745
1746 if (itemNode->type == XML_ATTRIBUTE_NODE)
1747 elem = itemNode->parent;
Daniel Veillarddee23482008-04-11 12:58:43 +00001748 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001749 elem = itemNode;
1750 *buf = xmlStrdup(BAD_CAST "Element '");
1751 if (elem->ns != NULL) {
1752 *buf = xmlStrcat(*buf,
1753 xmlSchemaFormatQName(&str, elem->ns->href, elem->name));
1754 FREE_AND_NULL(str)
1755 } else
1756 *buf = xmlStrcat(*buf, elem->name);
1757 *buf = xmlStrcat(*buf, BAD_CAST "'");
Daniel Veillarddee23482008-04-11 12:58:43 +00001758
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001759 }
1760 if ((itemNode != NULL) && (itemNode->type == XML_ATTRIBUTE_NODE)) {
1761 *buf = xmlStrcat(*buf, BAD_CAST ", attribute '");
1762 if (itemNode->ns != NULL) {
1763 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1764 itemNode->ns->href, itemNode->name));
1765 FREE_AND_NULL(str)
1766 } else
1767 *buf = xmlStrcat(*buf, itemNode->name);
1768 *buf = xmlStrcat(*buf, BAD_CAST "'");
1769 }
1770 FREE_AND_NULL(str)
Daniel Veillarddee23482008-04-11 12:58:43 +00001771
David Kilzer502f6a62016-05-23 14:58:41 +08001772 return (xmlEscapeFormatString(buf));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001773}
1774
1775/**
1776 * xmlSchemaFormatFacetEnumSet:
1777 * @buf: the string buffer
1778 * @type: the type holding the enumeration facets
1779 *
1780 * Builds a string consisting of all enumeration elements.
1781 *
1782 * Returns a string of all enumeration elements.
1783 */
1784static const xmlChar *
1785xmlSchemaFormatFacetEnumSet(xmlSchemaAbstractCtxtPtr actxt,
1786 xmlChar **buf, xmlSchemaTypePtr type)
1787{
1788 xmlSchemaFacetPtr facet;
1789 xmlSchemaWhitespaceValueType ws;
1790 xmlChar *value = NULL;
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +00001791 int res, found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001792
1793 if (*buf != NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +00001794 xmlFree(*buf);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001795 *buf = NULL;
1796
1797 do {
1798 /*
1799 * Use the whitespace type of the base type.
Daniel Veillarddee23482008-04-11 12:58:43 +00001800 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001801 ws = xmlSchemaGetWhiteSpaceFacetValue(type->baseType);
1802 for (facet = type->facets; facet != NULL; facet = facet->next) {
1803 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
1804 continue;
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +00001805 found = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001806 res = xmlSchemaGetCanonValueWhtspExt(facet->val,
1807 ws, &value);
1808 if (res == -1) {
1809 xmlSchemaInternalErr(actxt,
1810 "xmlSchemaFormatFacetEnumSet",
1811 "compute the canonical lexical representation");
1812 if (*buf != NULL)
1813 xmlFree(*buf);
1814 *buf = NULL;
1815 return (NULL);
1816 }
1817 if (*buf == NULL)
1818 *buf = xmlStrdup(BAD_CAST "'");
1819 else
1820 *buf = xmlStrcat(*buf, BAD_CAST ", '");
1821 *buf = xmlStrcat(*buf, BAD_CAST value);
1822 *buf = xmlStrcat(*buf, BAD_CAST "'");
1823 if (value != NULL) {
1824 xmlFree((xmlChar *)value);
1825 value = NULL;
1826 }
1827 }
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +00001828 /*
1829 * The enumeration facet of a type restricts the enumeration
1830 * facet of the ancestor type; i.e., such restricted enumerations
1831 * do not belong to the set of the given type. Thus we break
1832 * on the first found enumeration.
1833 */
1834 if (found)
1835 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001836 type = type->baseType;
1837 } while ((type != NULL) && (type->type != XML_SCHEMA_TYPE_BASIC));
1838
1839 return ((const xmlChar *) *buf);
1840}
1841
1842/************************************************************************
1843 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001844 * Error functions *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001845 * *
1846 ************************************************************************/
1847
1848#if 0
1849static void
1850xmlSchemaErrMemory(const char *msg)
1851{
1852 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1853 msg);
1854}
1855#endif
1856
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001857static void
1858xmlSchemaPSimpleErr(const char *msg)
1859{
1860 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1861 msg);
1862}
1863
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001864/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001865 * xmlSchemaPErrMemory:
1866 * @node: a context node
1867 * @extra: extra informations
1868 *
1869 * Handle an out of memory condition
1870 */
1871static void
1872xmlSchemaPErrMemory(xmlSchemaParserCtxtPtr ctxt,
1873 const char *extra, xmlNodePtr node)
1874{
1875 if (ctxt != NULL)
1876 ctxt->nberrors++;
1877 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL,
1878 extra);
1879}
1880
1881/**
1882 * xmlSchemaPErr:
1883 * @ctxt: the parsing context
1884 * @node: the context node
1885 * @error: the error code
1886 * @msg: the error message
1887 * @str1: extra data
1888 * @str2: extra data
Daniel Veillarddee23482008-04-11 12:58:43 +00001889 *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001890 * Handle a parser error
1891 */
David Kilzer4472c3a2016-05-13 15:13:17 +08001892static void LIBXML_ATTR_FORMAT(4,0)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001893xmlSchemaPErr(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1894 const char *msg, const xmlChar * str1, const xmlChar * str2)
1895{
1896 xmlGenericErrorFunc channel = NULL;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001897 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001898 void *data = NULL;
1899
1900 if (ctxt != NULL) {
1901 ctxt->nberrors++;
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00001902 ctxt->err = error;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001903 channel = ctxt->error;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00001904 data = ctxt->errCtxt;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001905 schannel = ctxt->serror;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001906 }
Daniel Veillard659e71e2003-10-10 14:10:40 +00001907 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001908 error, XML_ERR_ERROR, NULL, 0,
1909 (const char *) str1, (const char *) str2, NULL, 0, 0,
1910 msg, str1, str2);
1911}
1912
1913/**
1914 * xmlSchemaPErr2:
1915 * @ctxt: the parsing context
1916 * @node: the context node
1917 * @node: the current child
1918 * @error: the error code
1919 * @msg: the error message
1920 * @str1: extra data
1921 * @str2: extra data
Daniel Veillarddee23482008-04-11 12:58:43 +00001922 *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001923 * Handle a parser error
1924 */
David Kilzer4472c3a2016-05-13 15:13:17 +08001925static void LIBXML_ATTR_FORMAT(5,0)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001926xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
1927 xmlNodePtr child, int error,
1928 const char *msg, const xmlChar * str1, const xmlChar * str2)
1929{
1930 if (child != NULL)
1931 xmlSchemaPErr(ctxt, child, error, msg, str1, str2);
1932 else
1933 xmlSchemaPErr(ctxt, node, error, msg, str1, str2);
1934}
1935
Daniel Veillard01fa6152004-06-29 17:04:39 +00001936
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001937/**
Daniel Veillard3646d642004-06-02 19:19:14 +00001938 * xmlSchemaPErrExt:
1939 * @ctxt: the parsing context
1940 * @node: the context node
Daniel Veillarddee23482008-04-11 12:58:43 +00001941 * @error: the error code
Daniel Veillard3646d642004-06-02 19:19:14 +00001942 * @strData1: extra data
1943 * @strData2: extra data
1944 * @strData3: extra data
1945 * @msg: the message
1946 * @str1: extra parameter for the message display
1947 * @str2: extra parameter for the message display
1948 * @str3: extra parameter for the message display
1949 * @str4: extra parameter for the message display
1950 * @str5: extra parameter for the message display
Daniel Veillarddee23482008-04-11 12:58:43 +00001951 *
Daniel Veillard3646d642004-06-02 19:19:14 +00001952 * Handle a parser error
1953 */
David Kilzer4472c3a2016-05-13 15:13:17 +08001954static void LIBXML_ATTR_FORMAT(7,0)
Daniel Veillard3646d642004-06-02 19:19:14 +00001955xmlSchemaPErrExt(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
Daniel Veillarddee23482008-04-11 12:58:43 +00001956 const xmlChar * strData1, const xmlChar * strData2,
1957 const xmlChar * strData3, const char *msg, const xmlChar * str1,
Daniel Veillard3646d642004-06-02 19:19:14 +00001958 const xmlChar * str2, const xmlChar * str3, const xmlChar * str4,
1959 const xmlChar * str5)
1960{
1961
1962 xmlGenericErrorFunc channel = NULL;
1963 xmlStructuredErrorFunc schannel = NULL;
1964 void *data = NULL;
1965
1966 if (ctxt != NULL) {
1967 ctxt->nberrors++;
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00001968 ctxt->err = error;
Daniel Veillard3646d642004-06-02 19:19:14 +00001969 channel = ctxt->error;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00001970 data = ctxt->errCtxt;
Daniel Veillard3646d642004-06-02 19:19:14 +00001971 schannel = ctxt->serror;
1972 }
1973 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
1974 error, XML_ERR_ERROR, NULL, 0,
Daniel Veillarddee23482008-04-11 12:58:43 +00001975 (const char *) strData1, (const char *) strData2,
1976 (const char *) strData3, 0, 0, msg, str1, str2,
William M. Brack803812b2004-06-03 02:11:24 +00001977 str3, str4, str5);
Daniel Veillard3646d642004-06-02 19:19:14 +00001978}
Daniel Veillard01fa6152004-06-29 17:04:39 +00001979
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001980/************************************************************************
1981 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001982 * Allround error functions *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001983 * *
1984 ************************************************************************/
Daniel Veillard3646d642004-06-02 19:19:14 +00001985
1986/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001987 * xmlSchemaVTypeErrMemory:
1988 * @node: a context node
1989 * @extra: extra informations
1990 *
1991 * Handle an out of memory condition
1992 */
1993static void
1994xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt,
1995 const char *extra, xmlNodePtr node)
1996{
1997 if (ctxt != NULL) {
1998 ctxt->nberrors++;
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00001999 ctxt->err = XML_SCHEMAV_INTERNAL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002000 }
2001 __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL,
2002 extra);
2003}
2004
David Kilzer4472c3a2016-05-13 15:13:17 +08002005static void LIBXML_ATTR_FORMAT(2,0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002006xmlSchemaPSimpleInternalErr(xmlNodePtr node,
2007 const char *msg, const xmlChar *str)
2008{
2009 __xmlSimpleError(XML_FROM_SCHEMASP, XML_SCHEMAP_INTERNAL, node,
2010 msg, (const char *) str);
2011}
2012
2013#define WXS_ERROR_TYPE_ERROR 1
2014#define WXS_ERROR_TYPE_WARNING 2
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002015/**
David Kilzer4472c3a2016-05-13 15:13:17 +08002016 * xmlSchemaErr4Line:
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002017 * @ctxt: the validation context
David Kilzer4472c3a2016-05-13 15:13:17 +08002018 * @errorLevel: the error level
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002019 * @error: the error code
David Kilzer4472c3a2016-05-13 15:13:17 +08002020 * @node: the context node
2021 * @line: the line number
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002022 * @msg: the error message
2023 * @str1: extra data
2024 * @str2: extra data
2025 * @str3: extra data
David Kilzer4472c3a2016-05-13 15:13:17 +08002026 * @str4: extra data
Daniel Veillarddee23482008-04-11 12:58:43 +00002027 *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002028 * Handle a validation error
2029 */
David Kilzer4472c3a2016-05-13 15:13:17 +08002030static void LIBXML_ATTR_FORMAT(6,0)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002031xmlSchemaErr4Line(xmlSchemaAbstractCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002032 xmlErrorLevel errorLevel,
2033 int error, xmlNodePtr node, int line, const char *msg,
2034 const xmlChar *str1, const xmlChar *str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002035 const xmlChar *str3, const xmlChar *str4)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002036{
Daniel Veillard659e71e2003-10-10 14:10:40 +00002037 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002038 xmlGenericErrorFunc channel = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002039 void *data = NULL;
2040
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002041 if (ctxt != NULL) {
2042 if (ctxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2043 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctxt;
Daniel Veillard971771e2005-07-09 17:32:57 +00002044 const char *file = NULL;
Juergen Keild201e712014-08-07 11:42:07 +08002045 int col = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002046 if (errorLevel != XML_ERR_WARNING) {
2047 vctxt->nberrors++;
2048 vctxt->err = error;
Daniel Veillarddee23482008-04-11 12:58:43 +00002049 channel = vctxt->error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002050 } else {
2051 channel = vctxt->warning;
2052 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002053 schannel = vctxt->serror;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00002054 data = vctxt->errCtxt;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002055
2056 /*
2057 * Error node. If we specify a line number, then
2058 * do not channel any node to the error function.
2059 */
2060 if (line == 0) {
2061 if ((node == NULL) &&
2062 (vctxt->depth >= 0) &&
2063 (vctxt->inode != NULL)) {
2064 node = vctxt->inode->node;
2065 }
2066 /*
2067 * Get filename and line if no node-tree.
2068 */
2069 if ((node == NULL) &&
2070 (vctxt->parserCtxt != NULL) &&
2071 (vctxt->parserCtxt->input != NULL)) {
2072 file = vctxt->parserCtxt->input->filename;
2073 line = vctxt->parserCtxt->input->line;
Juergen Keild201e712014-08-07 11:42:07 +08002074 col = vctxt->parserCtxt->input->col;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002075 }
2076 } else {
2077 /*
2078 * Override the given node's (if any) position
2079 * and channel only the given line number.
2080 */
2081 node = NULL;
2082 /*
2083 * Get filename.
2084 */
2085 if (vctxt->doc != NULL)
2086 file = (const char *) vctxt->doc->URL;
2087 else if ((vctxt->parserCtxt != NULL) &&
2088 (vctxt->parserCtxt->input != NULL))
2089 file = vctxt->parserCtxt->input->filename;
Daniel Veillarddee23482008-04-11 12:58:43 +00002090 }
Daniel Veillard97fa5b32012-08-14 11:01:07 +08002091 if (vctxt->locFunc != NULL) {
2092 if ((file == NULL) || (line == 0)) {
2093 unsigned long l;
2094 const char *f;
2095 vctxt->locFunc(vctxt->locCtxt, &f, &l);
2096 if (file == NULL)
2097 file = f;
2098 if (line == 0)
2099 line = (int) l;
2100 }
2101 }
2102 if ((file == NULL) && (vctxt->filename != NULL))
2103 file = vctxt->filename;
2104
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002105 __xmlRaiseError(schannel, channel, data, ctxt,
2106 node, XML_FROM_SCHEMASV,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002107 error, errorLevel, file, line,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002108 (const char *) str1, (const char *) str2,
Juergen Keild201e712014-08-07 11:42:07 +08002109 (const char *) str3, 0, col, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002110
2111 } else if (ctxt->type == XML_SCHEMA_CTXT_PARSER) {
2112 xmlSchemaParserCtxtPtr pctxt = (xmlSchemaParserCtxtPtr) ctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002113 if (errorLevel != XML_ERR_WARNING) {
2114 pctxt->nberrors++;
2115 pctxt->err = error;
Daniel Veillarddee23482008-04-11 12:58:43 +00002116 channel = pctxt->error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002117 } else {
2118 channel = pctxt->warning;
2119 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002120 schannel = pctxt->serror;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00002121 data = pctxt->errCtxt;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002122 __xmlRaiseError(schannel, channel, data, ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002123 node, XML_FROM_SCHEMASP, error,
2124 errorLevel, NULL, 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002125 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002126 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002127 } else {
2128 TODO
2129 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002130 }
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002131}
2132
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002133/**
2134 * xmlSchemaErr3:
2135 * @ctxt: the validation context
2136 * @node: the context node
2137 * @error: the error code
2138 * @msg: the error message
2139 * @str1: extra data
2140 * @str2: extra data
2141 * @str3: extra data
Daniel Veillarddee23482008-04-11 12:58:43 +00002142 *
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002143 * Handle a validation error
2144 */
David Kilzer4472c3a2016-05-13 15:13:17 +08002145static void LIBXML_ATTR_FORMAT(4,0)
Daniel Veillarddee23482008-04-11 12:58:43 +00002146xmlSchemaErr3(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002147 int error, xmlNodePtr node, const char *msg,
2148 const xmlChar *str1, const xmlChar *str2, const xmlChar *str3)
2149{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002150 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2151 msg, str1, str2, str3, NULL);
2152}
2153
David Kilzer4472c3a2016-05-13 15:13:17 +08002154static void LIBXML_ATTR_FORMAT(4,0)
Daniel Veillarddee23482008-04-11 12:58:43 +00002155xmlSchemaErr4(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002156 int error, xmlNodePtr node, const char *msg,
2157 const xmlChar *str1, const xmlChar *str2,
2158 const xmlChar *str3, const xmlChar *str4)
2159{
2160 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2161 msg, str1, str2, str3, str4);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002162}
2163
David Kilzer4472c3a2016-05-13 15:13:17 +08002164static void LIBXML_ATTR_FORMAT(4,0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002165xmlSchemaErr(xmlSchemaAbstractCtxtPtr actxt,
2166 int error, xmlNodePtr node, const char *msg,
2167 const xmlChar *str1, const xmlChar *str2)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002168{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002169 xmlSchemaErr4(actxt, error, node, msg, str1, str2, NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002170}
2171
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002172static xmlChar *
2173xmlSchemaFormatNodeForError(xmlChar ** msg,
2174 xmlSchemaAbstractCtxtPtr actxt,
2175 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002176{
2177 xmlChar *str = NULL;
2178
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002179 *msg = NULL;
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00002180 if ((node != NULL) &&
2181 (node->type != XML_ELEMENT_NODE) &&
2182 (node->type != XML_ATTRIBUTE_NODE))
2183 {
Daniel Veillarddee23482008-04-11 12:58:43 +00002184 /*
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00002185 * Don't try to format other nodes than element and
2186 * attribute nodes.
David Kilzer4472c3a2016-05-13 15:13:17 +08002187 * Play safe and return an empty string.
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00002188 */
2189 *msg = xmlStrdup(BAD_CAST "");
2190 return(*msg);
2191 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002192 if (node != NULL) {
2193 /*
2194 * Work on tree nodes.
2195 */
2196 if (node->type == XML_ATTRIBUTE_NODE) {
2197 xmlNodePtr elem = node->parent;
Daniel Veillarddee23482008-04-11 12:58:43 +00002198
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002199 *msg = xmlStrdup(BAD_CAST "Element '");
2200 if (elem->ns != NULL)
2201 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2202 elem->ns->href, elem->name));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002203 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002204 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2205 NULL, elem->name));
2206 FREE_AND_NULL(str);
2207 *msg = xmlStrcat(*msg, BAD_CAST "', ");
Daniel Veillarddee23482008-04-11 12:58:43 +00002208 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002209 } else {
2210 *msg = xmlStrdup(BAD_CAST "Element '");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002211 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002212 if (node->ns != NULL)
2213 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2214 node->ns->href, node->name));
Daniel Veillardc0826a72004-08-10 14:17:33 +00002215 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002216 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2217 NULL, node->name));
2218 FREE_AND_NULL(str);
2219 *msg = xmlStrcat(*msg, BAD_CAST "': ");
2220 } else if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2221 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) actxt;
2222 /*
2223 * Work on node infos.
Daniel Veillarddee23482008-04-11 12:58:43 +00002224 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002225 if (vctxt->inode->nodeType == XML_ATTRIBUTE_NODE) {
2226 xmlSchemaNodeInfoPtr ielem =
2227 vctxt->elemInfos[vctxt->depth];
2228
2229 *msg = xmlStrdup(BAD_CAST "Element '");
2230 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2231 ielem->nsName, ielem->localName));
2232 FREE_AND_NULL(str);
2233 *msg = xmlStrcat(*msg, BAD_CAST "', ");
Daniel Veillarddee23482008-04-11 12:58:43 +00002234 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002235 } else {
2236 *msg = xmlStrdup(BAD_CAST "Element '");
2237 }
2238 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2239 vctxt->inode->nsName, vctxt->inode->localName));
2240 FREE_AND_NULL(str);
2241 *msg = xmlStrcat(*msg, BAD_CAST "': ");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002242 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
Daniel Veillarddee23482008-04-11 12:58:43 +00002243 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002244 * Hmm, no node while parsing?
2245 * Return an empty string, in case NULL will break something.
2246 */
2247 *msg = xmlStrdup(BAD_CAST "");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002248 } else {
2249 TODO
2250 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002251 }
David Kilzer502f6a62016-05-23 14:58:41 +08002252
2253 /*
2254 * xmlSchemaFormatItemForReport() also returns an escaped format
2255 * string, so do this before calling it below (in the future).
2256 */
2257 xmlEscapeFormatString(msg);
2258
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002259 /*
2260 * VAL TODO: The output of the given schema component is currently
2261 * disabled.
2262 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002263#if 0
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002264 if ((type != NULL) && (xmlSchemaIsGlobalItem(type))) {
2265 *msg = xmlStrcat(*msg, BAD_CAST " [");
2266 *msg = xmlStrcat(*msg, xmlSchemaFormatItemForReport(&str,
2267 NULL, type, NULL, 0));
2268 FREE_AND_NULL(str)
2269 *msg = xmlStrcat(*msg, BAD_CAST "]");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002270 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002271#endif
2272 return (*msg);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002273}
2274
David Kilzer4472c3a2016-05-13 15:13:17 +08002275static void LIBXML_ATTR_FORMAT(3,0)
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002276xmlSchemaInternalErr2(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002277 const char *funcName,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002278 const char *message,
2279 const xmlChar *str1,
2280 const xmlChar *str2)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002281{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002282 xmlChar *msg = NULL;
2283
Daniel Veillard14b56432006-03-09 18:41:40 +00002284 if (actxt == NULL)
2285 return;
David Kilzer4472c3a2016-05-13 15:13:17 +08002286 msg = xmlStrdup(BAD_CAST "Internal error: %s, ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002287 msg = xmlStrcat(msg, BAD_CAST message);
2288 msg = xmlStrcat(msg, BAD_CAST ".\n");
2289
2290 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR)
David Kilzer4472c3a2016-05-13 15:13:17 +08002291 xmlSchemaErr3(actxt, XML_SCHEMAV_INTERNAL, NULL,
2292 (const char *) msg, (const xmlChar *) funcName, str1, str2);
Daniel Veillarddee23482008-04-11 12:58:43 +00002293 else if (actxt->type == XML_SCHEMA_CTXT_PARSER)
David Kilzer4472c3a2016-05-13 15:13:17 +08002294 xmlSchemaErr3(actxt, XML_SCHEMAP_INTERNAL, NULL,
2295 (const char *) msg, (const xmlChar *) funcName, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002296
2297 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002298}
2299
David Kilzer4472c3a2016-05-13 15:13:17 +08002300static void LIBXML_ATTR_FORMAT(3,0)
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002301xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
2302 const char *funcName,
2303 const char *message)
2304{
2305 xmlSchemaInternalErr2(actxt, funcName, message, NULL, NULL);
2306}
2307
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002308#if 0
David Kilzer4472c3a2016-05-13 15:13:17 +08002309static void LIBXML_ATTR_FORMAT(3,0)
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002310xmlSchemaPInternalErr(xmlSchemaParserCtxtPtr pctxt,
2311 const char *funcName,
2312 const char *message,
2313 const xmlChar *str1,
2314 const xmlChar *str2)
2315{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002316 xmlSchemaInternalErr2(ACTXT_CAST pctxt, funcName, message,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002317 str1, str2);
2318}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002319#endif
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002320
David Kilzer4472c3a2016-05-13 15:13:17 +08002321static void LIBXML_ATTR_FORMAT(5,0)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002322xmlSchemaCustomErr4(xmlSchemaAbstractCtxtPtr actxt,
2323 xmlParserErrors error,
2324 xmlNodePtr node,
2325 xmlSchemaBasicItemPtr item,
2326 const char *message,
2327 const xmlChar *str1, const xmlChar *str2,
2328 const xmlChar *str3, const xmlChar *str4)
2329{
2330 xmlChar *msg = NULL;
2331
2332 if ((node == NULL) && (item != NULL) &&
2333 (actxt->type == XML_SCHEMA_CTXT_PARSER)) {
2334 node = WXS_ITEM_NODE(item);
2335 xmlSchemaFormatItemForReport(&msg, NULL, item, NULL);
2336 msg = xmlStrcat(msg, BAD_CAST ": ");
2337 } else
2338 xmlSchemaFormatNodeForError(&msg, actxt, node);
2339 msg = xmlStrcat(msg, (const xmlChar *) message);
Daniel Veillarddee23482008-04-11 12:58:43 +00002340 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002341 xmlSchemaErr4(actxt, error, node,
2342 (const char *) msg, str1, str2, str3, str4);
2343 FREE_AND_NULL(msg)
2344}
2345
David Kilzer4472c3a2016-05-13 15:13:17 +08002346static void LIBXML_ATTR_FORMAT(5,0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002347xmlSchemaCustomErr(xmlSchemaAbstractCtxtPtr actxt,
2348 xmlParserErrors error,
2349 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002350 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002351 const char *message,
2352 const xmlChar *str1,
2353 const xmlChar *str2)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002354{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002355 xmlSchemaCustomErr4(actxt, error, node, item,
Daniel Veillarddee23482008-04-11 12:58:43 +00002356 message, str1, str2, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002357}
2358
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002359
2360
David Kilzer4472c3a2016-05-13 15:13:17 +08002361static void LIBXML_ATTR_FORMAT(5,0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002362xmlSchemaCustomWarning(xmlSchemaAbstractCtxtPtr actxt,
2363 xmlParserErrors error,
2364 xmlNodePtr node,
2365 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2366 const char *message,
2367 const xmlChar *str1,
2368 const xmlChar *str2,
2369 const xmlChar *str3)
2370{
2371 xmlChar *msg = NULL;
2372
2373 xmlSchemaFormatNodeForError(&msg, actxt, node);
2374 msg = xmlStrcat(msg, (const xmlChar *) message);
Daniel Veillarddee23482008-04-11 12:58:43 +00002375 msg = xmlStrcat(msg, BAD_CAST ".\n");
2376
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002377 /* URGENT TODO: Set the error code to something sane. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002378 xmlSchemaErr4Line(actxt, XML_ERR_WARNING, error, node, 0,
2379 (const char *) msg, str1, str2, str3, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002380
2381 FREE_AND_NULL(msg)
2382}
2383
2384
2385
David Kilzer4472c3a2016-05-13 15:13:17 +08002386static void LIBXML_ATTR_FORMAT(5,0)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002387xmlSchemaKeyrefErr(xmlSchemaValidCtxtPtr vctxt,
2388 xmlParserErrors error,
2389 xmlSchemaPSVIIDCNodePtr idcNode,
2390 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2391 const char *message,
2392 const xmlChar *str1,
2393 const xmlChar *str2)
2394{
2395 xmlChar *msg = NULL, *qname = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002396
2397 msg = xmlStrdup(BAD_CAST "Element '%s': ");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002398 msg = xmlStrcat(msg, (const xmlChar *) message);
2399 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002400 xmlSchemaErr4Line(ACTXT_CAST vctxt, XML_ERR_ERROR,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002401 error, NULL, idcNode->nodeLine, (const char *) msg,
2402 xmlSchemaFormatQName(&qname,
2403 vctxt->nodeQNames->items[idcNode->nodeQNameID +1],
Daniel Veillarddee23482008-04-11 12:58:43 +00002404 vctxt->nodeQNames->items[idcNode->nodeQNameID]),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002405 str1, str2, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002406 FREE_AND_NULL(qname);
2407 FREE_AND_NULL(msg);
2408}
2409
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002410static int
2411xmlSchemaEvalErrorNodeType(xmlSchemaAbstractCtxtPtr actxt,
2412 xmlNodePtr node)
2413{
2414 if (node != NULL)
2415 return (node->type);
2416 if ((actxt->type == XML_SCHEMA_CTXT_VALIDATOR) &&
2417 (((xmlSchemaValidCtxtPtr) actxt)->inode != NULL))
2418 return ( ((xmlSchemaValidCtxtPtr) actxt)->inode->nodeType);
2419 return (-1);
2420}
2421
2422static int
2423xmlSchemaIsGlobalItem(xmlSchemaTypePtr item)
2424{
2425 switch (item->type) {
2426 case XML_SCHEMA_TYPE_COMPLEX:
2427 case XML_SCHEMA_TYPE_SIMPLE:
2428 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL)
2429 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002430 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002431 case XML_SCHEMA_TYPE_GROUP:
2432 return (1);
2433 case XML_SCHEMA_TYPE_ELEMENT:
2434 if ( ((xmlSchemaElementPtr) item)->flags &
2435 XML_SCHEMAS_ELEM_GLOBAL)
2436 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002437 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002438 case XML_SCHEMA_TYPE_ATTRIBUTE:
2439 if ( ((xmlSchemaAttributePtr) item)->flags &
2440 XML_SCHEMAS_ATTR_GLOBAL)
2441 return(1);
2442 break;
2443 /* Note that attribute groups are always global. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002444 default:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002445 return(1);
2446 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002447 return (0);
2448}
2449
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002450static void
2451xmlSchemaSimpleTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2452 xmlParserErrors error,
2453 xmlNodePtr node,
2454 const xmlChar *value,
2455 xmlSchemaTypePtr type,
2456 int displayValue)
2457{
2458 xmlChar *msg = NULL;
2459
2460 xmlSchemaFormatNodeForError(&msg, actxt, node);
2461
2462 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2463 XML_ATTRIBUTE_NODE))
2464 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
2465 else
2466 msg = xmlStrcat(msg, BAD_CAST "The character content is not a valid "
2467 "value of ");
2468
2469 if (! xmlSchemaIsGlobalItem(type))
2470 msg = xmlStrcat(msg, BAD_CAST "the local ");
2471 else
2472 msg = xmlStrcat(msg, BAD_CAST "the ");
2473
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002474 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002475 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002476 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002477 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002478 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002479 msg = xmlStrcat(msg, BAD_CAST "union type");
2480
2481 if (xmlSchemaIsGlobalItem(type)) {
2482 xmlChar *str = NULL;
2483 msg = xmlStrcat(msg, BAD_CAST " '");
2484 if (type->builtInType != 0) {
2485 msg = xmlStrcat(msg, BAD_CAST "xs:");
David Kilzer502f6a62016-05-23 14:58:41 +08002486 str = xmlStrdup(type->name);
2487 } else {
2488 const xmlChar *qName = xmlSchemaFormatQName(&str, type->targetNamespace, type->name);
2489 if (!str)
2490 str = xmlStrdup(qName);
2491 }
2492 msg = xmlStrcat(msg, xmlEscapeFormatString(&str));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002493 msg = xmlStrcat(msg, BAD_CAST "'");
2494 FREE_AND_NULL(str);
2495 }
2496 msg = xmlStrcat(msg, BAD_CAST ".\n");
2497 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2498 XML_ATTRIBUTE_NODE))
2499 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2500 else
2501 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2502 FREE_AND_NULL(msg)
2503}
2504
Daniel Veillardc0826a72004-08-10 14:17:33 +00002505static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002506xmlSchemaFormatErrorNodeQName(xmlChar ** str,
2507 xmlSchemaNodeInfoPtr ni,
2508 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002509{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002510 if (node != NULL) {
2511 if (node->ns != NULL)
2512 return (xmlSchemaFormatQName(str, node->ns->href, node->name));
2513 else
2514 return (xmlSchemaFormatQName(str, NULL, node->name));
2515 } else if (ni != NULL)
2516 return (xmlSchemaFormatQName(str, ni->nsName, ni->localName));
2517 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002518}
2519
Daniel Veillardc0826a72004-08-10 14:17:33 +00002520static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002521xmlSchemaIllegalAttrErr(xmlSchemaAbstractCtxtPtr actxt,
2522 xmlParserErrors error,
2523 xmlSchemaAttrInfoPtr ni,
2524 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002525{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002526 xmlChar *msg = NULL, *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002527
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002528 xmlSchemaFormatNodeForError(&msg, actxt, node);
2529 msg = xmlStrcat(msg, BAD_CAST "The attribute '%s' is not allowed.\n");
2530 xmlSchemaErr(actxt, error, node, (const char *) msg,
2531 xmlSchemaFormatErrorNodeQName(&str, (xmlSchemaNodeInfoPtr) ni, node),
Daniel Veillarddee23482008-04-11 12:58:43 +00002532 NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002533 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002534 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002535}
2536
David Kilzer4472c3a2016-05-13 15:13:17 +08002537static void LIBXML_ATTR_FORMAT(5,0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002538xmlSchemaComplexTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2539 xmlParserErrors error,
2540 xmlNodePtr node,
2541 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002542 const char *message,
2543 int nbval,
2544 int nbneg,
2545 xmlChar **values)
2546{
2547 xmlChar *str = NULL, *msg = NULL;
2548 xmlChar *localName, *nsName;
2549 const xmlChar *cur, *end;
Daniel Veillard6a0baa02005-12-10 11:11:12 +00002550 int i;
Daniel Veillarddee23482008-04-11 12:58:43 +00002551
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002552 xmlSchemaFormatNodeForError(&msg, actxt, node);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002553 msg = xmlStrcat(msg, (const xmlChar *) message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002554 msg = xmlStrcat(msg, BAD_CAST ".");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002555 /*
2556 * Note that is does not make sense to report that we have a
2557 * wildcard here, since the wildcard might be unfolded into
2558 * multiple transitions.
2559 */
2560 if (nbval + nbneg > 0) {
2561 if (nbval + nbneg > 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002562 str = xmlStrdup(BAD_CAST " Expected is one of ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002563 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002564 str = xmlStrdup(BAD_CAST " Expected is ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002565 nsName = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002566
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002567 for (i = 0; i < nbval + nbneg; i++) {
2568 cur = values[i];
Daniel Veillard77005e62005-07-19 16:26:18 +00002569 if (cur == NULL)
2570 continue;
2571 if ((cur[0] == 'n') && (cur[1] == 'o') && (cur[2] == 't') &&
2572 (cur[3] == ' ')) {
Daniel Veillard77005e62005-07-19 16:26:18 +00002573 cur += 4;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002574 str = xmlStrcat(str, BAD_CAST "##other");
Daniel Veillard77005e62005-07-19 16:26:18 +00002575 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002576 /*
2577 * Get the local name.
2578 */
2579 localName = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00002580
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002581 end = cur;
2582 if (*end == '*') {
2583 localName = xmlStrdup(BAD_CAST "*");
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002584 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002585 } else {
2586 while ((*end != 0) && (*end != '|'))
2587 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002588 localName = xmlStrncat(localName, BAD_CAST cur, end - cur);
Daniel Veillarddee23482008-04-11 12:58:43 +00002589 }
2590 if (*end != 0) {
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002591 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002592 /*
2593 * Skip "*|*" if they come with negated expressions, since
2594 * they represent the same negated wildcard.
2595 */
2596 if ((nbneg == 0) || (*end != '*') || (*localName != '*')) {
2597 /*
2598 * Get the namespace name.
2599 */
2600 cur = end;
2601 if (*end == '*') {
2602 nsName = xmlStrdup(BAD_CAST "{*}");
2603 } else {
2604 while (*end != 0)
2605 end++;
Daniel Veillarddee23482008-04-11 12:58:43 +00002606
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002607 if (i >= nbval)
2608 nsName = xmlStrdup(BAD_CAST "{##other:");
2609 else
2610 nsName = xmlStrdup(BAD_CAST "{");
Daniel Veillarddee23482008-04-11 12:58:43 +00002611
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002612 nsName = xmlStrncat(nsName, BAD_CAST cur, end - cur);
2613 nsName = xmlStrcat(nsName, BAD_CAST "}");
2614 }
2615 str = xmlStrcat(str, BAD_CAST nsName);
2616 FREE_AND_NULL(nsName)
2617 } else {
2618 FREE_AND_NULL(localName);
2619 continue;
2620 }
Daniel Veillarddee23482008-04-11 12:58:43 +00002621 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002622 str = xmlStrcat(str, BAD_CAST localName);
2623 FREE_AND_NULL(localName);
Daniel Veillarddee23482008-04-11 12:58:43 +00002624
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002625 if (i < nbval + nbneg -1)
2626 str = xmlStrcat(str, BAD_CAST ", ");
Daniel Veillarddee23482008-04-11 12:58:43 +00002627 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002628 str = xmlStrcat(str, BAD_CAST " ).\n");
David Kilzer502f6a62016-05-23 14:58:41 +08002629 msg = xmlStrcat(msg, xmlEscapeFormatString(&str));
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002630 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002631 } else
2632 msg = xmlStrcat(msg, BAD_CAST "\n");
2633 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002634 xmlFree(msg);
2635}
2636
David Kilzer4472c3a2016-05-13 15:13:17 +08002637static void LIBXML_ATTR_FORMAT(8,0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002638xmlSchemaFacetErr(xmlSchemaAbstractCtxtPtr actxt,
2639 xmlParserErrors error,
2640 xmlNodePtr node,
2641 const xmlChar *value,
2642 unsigned long length,
2643 xmlSchemaTypePtr type,
2644 xmlSchemaFacetPtr facet,
2645 const char *message,
2646 const xmlChar *str1,
2647 const xmlChar *str2)
2648{
2649 xmlChar *str = NULL, *msg = NULL;
2650 xmlSchemaTypeType facetType;
2651 int nodeType = xmlSchemaEvalErrorNodeType(actxt, node);
2652
2653 xmlSchemaFormatNodeForError(&msg, actxt, node);
2654 if (error == XML_SCHEMAV_CVC_ENUMERATION_VALID) {
2655 facetType = XML_SCHEMA_FACET_ENUMERATION;
2656 /*
2657 * If enumerations are validated, one must not expect the
2658 * facet to be given.
Daniel Veillarddee23482008-04-11 12:58:43 +00002659 */
2660 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002661 facetType = facet->type;
2662 msg = xmlStrcat(msg, BAD_CAST "[");
2663 msg = xmlStrcat(msg, BAD_CAST "facet '");
2664 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facetType));
2665 msg = xmlStrcat(msg, BAD_CAST "'] ");
2666 if (message == NULL) {
2667 /*
2668 * Use a default message.
2669 */
2670 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
2671 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
2672 (facetType == XML_SCHEMA_FACET_MAXLENGTH)) {
2673
2674 char len[25], actLen[25];
2675
2676 /* FIXME, TODO: What is the max expected string length of the
2677 * this value?
2678 */
2679 if (nodeType == XML_ATTRIBUTE_NODE)
2680 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has a length of '%s'; ");
2681 else
2682 msg = xmlStrcat(msg, BAD_CAST "The value has a length of '%s'; ");
2683
2684 snprintf(len, 24, "%lu", xmlSchemaGetFacetValueAsULong(facet));
2685 snprintf(actLen, 24, "%lu", length);
2686
2687 if (facetType == XML_SCHEMA_FACET_LENGTH)
Daniel Veillarddee23482008-04-11 12:58:43 +00002688 msg = xmlStrcat(msg,
2689 BAD_CAST "this differs from the allowed length of '%s'.\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002690 else if (facetType == XML_SCHEMA_FACET_MAXLENGTH)
Daniel Veillarddee23482008-04-11 12:58:43 +00002691 msg = xmlStrcat(msg,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002692 BAD_CAST "this exceeds the allowed maximum length of '%s'.\n");
2693 else if (facetType == XML_SCHEMA_FACET_MINLENGTH)
Daniel Veillarddee23482008-04-11 12:58:43 +00002694 msg = xmlStrcat(msg,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002695 BAD_CAST "this underruns the allowed minimum length of '%s'.\n");
Daniel Veillarddee23482008-04-11 12:58:43 +00002696
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002697 if (nodeType == XML_ATTRIBUTE_NODE)
2698 xmlSchemaErr3(actxt, error, node, (const char *) msg,
2699 value, (const xmlChar *) actLen, (const xmlChar *) len);
Daniel Veillarddee23482008-04-11 12:58:43 +00002700 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002701 xmlSchemaErr(actxt, error, node, (const char *) msg,
2702 (const xmlChar *) actLen, (const xmlChar *) len);
Daniel Veillarddee23482008-04-11 12:58:43 +00002703
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002704 } else if (facetType == XML_SCHEMA_FACET_ENUMERATION) {
2705 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not an element "
2706 "of the set {%s}.\n");
Daniel Veillarddee23482008-04-11 12:58:43 +00002707 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002708 xmlSchemaFormatFacetEnumSet(actxt, &str, type));
2709 } else if (facetType == XML_SCHEMA_FACET_PATTERN) {
2710 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not accepted "
2711 "by the pattern '%s'.\n");
Daniel Veillarddee23482008-04-11 12:58:43 +00002712 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002713 facet->value);
2714 } else if (facetType == XML_SCHEMA_FACET_MININCLUSIVE) {
2715 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is less than the "
2716 "minimum value allowed ('%s').\n");
2717 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2718 facet->value);
2719 } else if (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) {
2720 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is greater than the "
2721 "maximum value allowed ('%s').\n");
2722 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2723 facet->value);
2724 } else if (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00002725 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be greater than "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002726 "'%s'.\n");
2727 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2728 facet->value);
2729 } else if (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00002730 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be less than "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002731 "'%s'.\n");
2732 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2733 facet->value);
2734 } else if (facetType == XML_SCHEMA_FACET_TOTALDIGITS) {
2735 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more "
2736 "digits than are allowed ('%s').\n");
2737 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2738 facet->value);
2739 } else if (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) {
2740 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more fractional "
2741 "digits than are allowed ('%s').\n");
2742 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2743 facet->value);
Daniel Veillarddee23482008-04-11 12:58:43 +00002744 } else if (nodeType == XML_ATTRIBUTE_NODE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002745 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not facet-valid.\n");
Daniel Veillarddee23482008-04-11 12:58:43 +00002746 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2747 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002748 msg = xmlStrcat(msg, BAD_CAST "The value is not facet-valid.\n");
2749 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2750 }
2751 } else {
2752 msg = xmlStrcat(msg, (const xmlChar *) message);
2753 msg = xmlStrcat(msg, BAD_CAST ".\n");
2754 xmlSchemaErr(actxt, error, node, (const char *) msg, str1, str2);
Daniel Veillarddee23482008-04-11 12:58:43 +00002755 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002756 FREE_AND_NULL(str)
2757 xmlFree(msg);
2758}
2759
2760#define VERROR(err, type, msg) \
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002761 xmlSchemaCustomErr(ACTXT_CAST vctxt, err, NULL, type, msg, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002762
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002763#define VERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST vctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002764
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002765#define PERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST pctxt, func, msg);
2766#define PERROR_INT2(func, msg) xmlSchemaInternalErr(ACTXT_CAST ctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002767
2768#define AERROR_INT(func, msg) xmlSchemaInternalErr(actxt, func, msg);
2769
2770
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002771/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00002772 * xmlSchemaPMissingAttrErr:
2773 * @ctxt: the schema validation context
2774 * @ownerDes: the designation of the owner
2775 * @ownerName: the name of the owner
2776 * @ownerItem: the owner as a schema object
2777 * @ownerElem: the owner as an element node
2778 * @node: the parent element node of the missing attribute node
2779 * @type: the corresponding type of the attribute node
2780 *
2781 * Reports an illegal attribute.
2782 */
2783static void
2784xmlSchemaPMissingAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002785 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002786 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002787 xmlNodePtr ownerElem,
2788 const char *name,
2789 const char *message)
2790{
2791 xmlChar *des = NULL;
2792
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002793 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
2794
Daniel Veillardc0826a72004-08-10 14:17:33 +00002795 if (message != NULL)
2796 xmlSchemaPErr(ctxt, ownerElem, error, "%s: %s.\n", BAD_CAST des, BAD_CAST message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002797 else
2798 xmlSchemaPErr(ctxt, ownerElem, error,
2799 "%s: The attribute '%s' is required but missing.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002800 BAD_CAST des, BAD_CAST name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002801 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002802}
2803
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002804
Daniel Veillardc0826a72004-08-10 14:17:33 +00002805/**
2806 * xmlSchemaPResCompAttrErr:
2807 * @ctxt: the schema validation context
2808 * @error: the error code
2809 * @ownerDes: the designation of the owner
2810 * @ownerItem: the owner as a schema object
2811 * @ownerElem: the owner as an element node
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002812 * @name: the name of the attribute holding the QName
Daniel Veillardc0826a72004-08-10 14:17:33 +00002813 * @refName: the referenced local name
2814 * @refURI: the referenced namespace URI
2815 * @message: optional message
2816 *
2817 * Used to report QName attribute values that failed to resolve
2818 * to schema components.
2819 */
2820static void
2821xmlSchemaPResCompAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002822 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002823 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002824 xmlNodePtr ownerElem,
2825 const char *name,
2826 const xmlChar *refName,
2827 const xmlChar *refURI,
2828 xmlSchemaTypeType refType,
2829 const char *refTypeStr)
2830{
2831 xmlChar *des = NULL, *strA = NULL;
2832
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002833 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002834 if (refTypeStr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002835 refTypeStr = (const char *) xmlSchemaItemTypeToStr(refType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002836 xmlSchemaPErrExt(ctxt, ownerElem, error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002837 NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002838 "%s, attribute '%s': The QName value '%s' does not resolve to a(n) "
2839 "%s.\n", BAD_CAST des, BAD_CAST name,
2840 xmlSchemaFormatQName(&strA, refURI, refName),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002841 BAD_CAST refTypeStr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002842 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002843 FREE_AND_NULL(strA)
2844}
2845
William M. Brack2f2a6632004-08-20 23:09:47 +00002846/**
2847 * xmlSchemaPCustomAttrErr:
2848 * @ctxt: the schema parser context
2849 * @error: the error code
2850 * @ownerDes: the designation of the owner
2851 * @ownerItem: the owner as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002852 * @attr: the illegal attribute node
William M. Brack2f2a6632004-08-20 23:09:47 +00002853 *
2854 * Reports an illegal attribute during the parse.
2855 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002856static void
2857xmlSchemaPCustomAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002858 xmlParserErrors error,
William M. Brack2f2a6632004-08-20 23:09:47 +00002859 xmlChar **ownerDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002860 xmlSchemaBasicItemPtr ownerItem,
William M. Brack2f2a6632004-08-20 23:09:47 +00002861 xmlAttrPtr attr,
2862 const char *msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002863{
2864 xmlChar *des = NULL;
2865
2866 if (ownerDes == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002867 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002868 else if (*ownerDes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002869 xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002870 des = *ownerDes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002871 } else
2872 des = *ownerDes;
Daniel Veillard14b56432006-03-09 18:41:40 +00002873 if (attr == NULL) {
2874 xmlSchemaPErrExt(ctxt, NULL, error, NULL, NULL, NULL,
2875 "%s, attribute '%s': %s.\n",
Daniel Veillardaac7c682006-03-10 13:40:16 +00002876 BAD_CAST des, (const xmlChar *) "Unknown",
2877 (const xmlChar *) msg, NULL, NULL);
Daniel Veillard14b56432006-03-09 18:41:40 +00002878 } else {
2879 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
2880 "%s, attribute '%s': %s.\n",
2881 BAD_CAST des, attr->name, (const xmlChar *) msg, NULL, NULL);
2882 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00002883 if (ownerDes == NULL)
2884 FREE_AND_NULL(des);
2885}
2886
2887/**
2888 * xmlSchemaPIllegalAttrErr:
William M. Brack2f2a6632004-08-20 23:09:47 +00002889 * @ctxt: the schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00002890 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00002891 * @ownerDes: the designation of the attribute's owner
2892 * @ownerItem: the attribute's owner item
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002893 * @attr: the illegal attribute node
Daniel Veillardc0826a72004-08-10 14:17:33 +00002894 *
William M. Brack2f2a6632004-08-20 23:09:47 +00002895 * Reports an illegal attribute during the parse.
Daniel Veillardc0826a72004-08-10 14:17:33 +00002896 */
2897static void
2898xmlSchemaPIllegalAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002899 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002900 xmlSchemaBasicItemPtr ownerComp ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002901 xmlAttrPtr attr)
2902{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002903 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002904
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002905 xmlSchemaFormatNodeForError(&strA, ACTXT_CAST ctxt, attr->parent);
2906 xmlSchemaErr4(ACTXT_CAST ctxt, error, (xmlNodePtr) attr,
2907 "%sThe attribute '%s' is not allowed.\n", BAD_CAST strA,
2908 xmlSchemaFormatQNameNs(&strB, attr->ns, attr->name),
2909 NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002910 FREE_AND_NULL(strA);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002911 FREE_AND_NULL(strB);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002912}
2913
William M. Brack2f2a6632004-08-20 23:09:47 +00002914/**
2915 * xmlSchemaPCustomErr:
2916 * @ctxt: the schema parser context
2917 * @error: the error code
2918 * @itemDes: the designation of the schema item
2919 * @item: the schema item
2920 * @itemElem: the node of the schema item
2921 * @message: the error message
2922 * @str1: an optional param for the error message
2923 * @str2: an optional param for the error message
2924 * @str3: an optional param for the error message
2925 *
2926 * Reports an error during parsing.
2927 */
David Kilzer4472c3a2016-05-13 15:13:17 +08002928static void LIBXML_ATTR_FORMAT(5,0)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002929xmlSchemaPCustomErrExt(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002930 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002931 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002932 xmlNodePtr itemElem,
2933 const char *message,
2934 const xmlChar *str1,
2935 const xmlChar *str2,
2936 const xmlChar *str3)
2937{
2938 xmlChar *des = NULL, *msg = NULL;
2939
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002940 xmlSchemaFormatItemForReport(&des, NULL, item, itemElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002941 msg = xmlStrdup(BAD_CAST "%s: ");
2942 msg = xmlStrcat(msg, (const xmlChar *) message);
2943 msg = xmlStrcat(msg, BAD_CAST ".\n");
2944 if ((itemElem == NULL) && (item != NULL))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002945 itemElem = WXS_ITEM_NODE(item);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002946 xmlSchemaPErrExt(ctxt, itemElem, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002947 (const char *) msg, BAD_CAST des, str1, str2, str3, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002948 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002949 FREE_AND_NULL(msg);
2950}
2951
William M. Brack2f2a6632004-08-20 23:09:47 +00002952/**
2953 * xmlSchemaPCustomErr:
2954 * @ctxt: the schema parser context
2955 * @error: the error code
2956 * @itemDes: the designation of the schema item
2957 * @item: the schema item
2958 * @itemElem: the node of the schema item
2959 * @message: the error message
2960 * @str1: the optional param for the error message
2961 *
2962 * Reports an error during parsing.
2963 */
David Kilzer4472c3a2016-05-13 15:13:17 +08002964static void LIBXML_ATTR_FORMAT(5,0)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002965xmlSchemaPCustomErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002966 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002967 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002968 xmlNodePtr itemElem,
2969 const char *message,
2970 const xmlChar *str1)
2971{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002972 xmlSchemaPCustomErrExt(ctxt, error, item, itemElem, message,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002973 str1, NULL, NULL);
2974}
2975
William M. Brack2f2a6632004-08-20 23:09:47 +00002976/**
2977 * xmlSchemaPAttrUseErr:
2978 * @ctxt: the schema parser context
2979 * @error: the error code
2980 * @itemDes: the designation of the schema type
2981 * @item: the schema type
2982 * @itemElem: the node of the schema type
2983 * @attr: the invalid schema attribute
2984 * @message: the error message
2985 * @str1: the optional param for the error message
2986 *
2987 * Reports an attribute use error during parsing.
2988 */
David Kilzer4472c3a2016-05-13 15:13:17 +08002989static void LIBXML_ATTR_FORMAT(6,0)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002990xmlSchemaPAttrUseErr4(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002991 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002992 xmlNodePtr node,
2993 xmlSchemaBasicItemPtr ownerItem,
2994 const xmlSchemaAttributeUsePtr attruse,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002995 const char *message,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002996 const xmlChar *str1, const xmlChar *str2,
2997 const xmlChar *str3,const xmlChar *str4)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002998{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002999 xmlChar *str = NULL, *msg = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00003000
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003001 xmlSchemaFormatItemForReport(&msg, NULL, ownerItem, NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00003002 msg = xmlStrcat(msg, BAD_CAST ", ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003003 msg = xmlStrcat(msg,
3004 BAD_CAST xmlSchemaFormatItemForReport(&str, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003005 WXS_BASIC_CAST attruse, NULL));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003006 FREE_AND_NULL(str);
3007 msg = xmlStrcat(msg, BAD_CAST ": ");
Daniel Veillardc0826a72004-08-10 14:17:33 +00003008 msg = xmlStrcat(msg, (const xmlChar *) message);
3009 msg = xmlStrcat(msg, BAD_CAST ".\n");
Daniel Veillarddee23482008-04-11 12:58:43 +00003010 xmlSchemaErr4(ACTXT_CAST ctxt, error, node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003011 (const char *) msg, str1, str2, str3, str4);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003012 xmlFree(msg);
3013}
3014
William M. Brack2f2a6632004-08-20 23:09:47 +00003015/**
3016 * xmlSchemaPIllegalFacetAtomicErr:
3017 * @ctxt: the schema parser context
3018 * @error: the error code
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003019 * @type: the schema type
3020 * @baseType: the base type of type
William M. Brack2f2a6632004-08-20 23:09:47 +00003021 * @facet: the illegal facet
3022 *
3023 * Reports an illegal facet for atomic simple types.
3024 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003025static void
3026xmlSchemaPIllegalFacetAtomicErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003027 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003028 xmlSchemaTypePtr type,
3029 xmlSchemaTypePtr baseType,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003030 xmlSchemaFacetPtr facet)
3031{
3032 xmlChar *des = NULL, *strT = NULL;
3033
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003034 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type, type->node);
3035 xmlSchemaPErrExt(ctxt, type->node, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003036 "%s: The facet '%s' is not allowed on types derived from the "
3037 "type %s.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00003038 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003039 xmlSchemaFormatItemForReport(&strT, NULL, WXS_BASIC_CAST baseType, NULL),
Daniel Veillardc0826a72004-08-10 14:17:33 +00003040 NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003041 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003042 FREE_AND_NULL(strT);
3043}
3044
William M. Brack2f2a6632004-08-20 23:09:47 +00003045/**
3046 * xmlSchemaPIllegalFacetListUnionErr:
3047 * @ctxt: the schema parser context
3048 * @error: the error code
3049 * @itemDes: the designation of the schema item involved
3050 * @item: the schema item involved
3051 * @facet: the illegal facet
3052 *
3053 * Reports an illegal facet for <list> and <union>.
3054 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003055static void
3056xmlSchemaPIllegalFacetListUnionErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003057 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003058 xmlSchemaTypePtr type,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003059 xmlSchemaFacetPtr facet)
3060{
Daniel Veillardb2947172006-03-27 09:45:01 +00003061 xmlChar *des = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003062
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003063 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type,
3064 type->node);
3065 xmlSchemaPErr(ctxt, type->node, error,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003066 "%s: The facet '%s' is not allowed.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00003067 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003068 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003069}
3070
3071/**
3072 * xmlSchemaPMutualExclAttrErr:
3073 * @ctxt: the schema validation context
3074 * @error: the error code
3075 * @elemDes: the designation of the parent element node
3076 * @attr: the bad attribute node
3077 * @type: the corresponding type of the attribute node
3078 *
3079 * Reports an illegal attribute.
3080 */
3081static void
3082xmlSchemaPMutualExclAttrErr(xmlSchemaParserCtxtPtr ctxt,
3083 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003084 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003085 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003086 const char *name1,
3087 const char *name2)
3088{
3089 xmlChar *des = NULL;
3090
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003091 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003092 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003093 "%s: The attributes '%s' and '%s' are mutually exclusive.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003094 BAD_CAST des, BAD_CAST name1, BAD_CAST name2, NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003095 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003096}
3097
3098/**
3099 * xmlSchemaPSimpleTypeErr:
3100 * @ctxt: the schema validation context
3101 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00003102 * @type: the type specifier
Daniel Veillardc0826a72004-08-10 14:17:33 +00003103 * @ownerDes: the designation of the owner
Daniel Veillarddee23482008-04-11 12:58:43 +00003104 * @ownerItem: the schema object if existent
Daniel Veillardc0826a72004-08-10 14:17:33 +00003105 * @node: the validated node
3106 * @value: the validated value
3107 *
3108 * Reports a simple type validation error.
3109 * TODO: Should this report the value of an element as well?
3110 */
David Kilzer4472c3a2016-05-13 15:13:17 +08003111static void LIBXML_ATTR_FORMAT(8,0)
Daniel Veillarddee23482008-04-11 12:58:43 +00003112xmlSchemaPSimpleTypeErr(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003113 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003114 xmlSchemaBasicItemPtr ownerItem ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003115 xmlNodePtr node,
William M. Brack2f2a6632004-08-20 23:09:47 +00003116 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003117 const char *expected,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003118 const xmlChar *value,
3119 const char *message,
3120 const xmlChar *str1,
3121 const xmlChar *str2)
3122{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003123 xmlChar *msg = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00003124
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003125 xmlSchemaFormatNodeForError(&msg, ACTXT_CAST ctxt, node);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003126 if (message == NULL) {
3127 /*
3128 * Use default messages.
Daniel Veillarddee23482008-04-11 12:58:43 +00003129 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003130 if (type != NULL) {
3131 if (node->type == XML_ATTRIBUTE_NODE)
3132 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
3133 else
3134 msg = xmlStrcat(msg, BAD_CAST "The character content is not a "
Daniel Veillarddee23482008-04-11 12:58:43 +00003135 "valid value of ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003136 if (! xmlSchemaIsGlobalItem(type))
3137 msg = xmlStrcat(msg, BAD_CAST "the local ");
3138 else
3139 msg = xmlStrcat(msg, BAD_CAST "the ");
Daniel Veillarddee23482008-04-11 12:58:43 +00003140
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003141 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003142 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003143 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003144 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003145 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003146 msg = xmlStrcat(msg, BAD_CAST "union type");
Daniel Veillarddee23482008-04-11 12:58:43 +00003147
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003148 if (xmlSchemaIsGlobalItem(type)) {
3149 xmlChar *str = NULL;
3150 msg = xmlStrcat(msg, BAD_CAST " '");
3151 if (type->builtInType != 0) {
3152 msg = xmlStrcat(msg, BAD_CAST "xs:");
David Kilzer502f6a62016-05-23 14:58:41 +08003153 str = xmlStrdup(type->name);
3154 } else {
3155 const xmlChar *qName = xmlSchemaFormatQName(&str, type->targetNamespace, type->name);
3156 if (!str)
3157 str = xmlStrdup(qName);
3158 }
3159 msg = xmlStrcat(msg, xmlEscapeFormatString(&str));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003160 msg = xmlStrcat(msg, BAD_CAST "'.");
3161 FREE_AND_NULL(str);
3162 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003163 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003164 if (node->type == XML_ATTRIBUTE_NODE)
3165 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not valid.");
3166 else
3167 msg = xmlStrcat(msg, BAD_CAST "The character content is not "
3168 "valid.");
Daniel Veillarddee23482008-04-11 12:58:43 +00003169 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003170 if (expected) {
David Kilzer502f6a62016-05-23 14:58:41 +08003171 xmlChar *expectedEscaped = xmlCharStrdup(expected);
Chun-wei Fand77e5fc2016-05-31 21:04:50 +08003172 msg = xmlStrcat(msg, BAD_CAST " Expected is '");
David Kilzer502f6a62016-05-23 14:58:41 +08003173 msg = xmlStrcat(msg, xmlEscapeFormatString(&expectedEscaped));
3174 FREE_AND_NULL(expectedEscaped);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003175 msg = xmlStrcat(msg, BAD_CAST "'.\n");
3176 } else
3177 msg = xmlStrcat(msg, BAD_CAST "\n");
Daniel Veillardc0826a72004-08-10 14:17:33 +00003178 if (node->type == XML_ATTRIBUTE_NODE)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003179 xmlSchemaPErr(ctxt, node, error, (const char *) msg, value, NULL);
3180 else
3181 xmlSchemaPErr(ctxt, node, error, (const char *) msg, NULL, NULL);
3182 } else {
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003183 msg = xmlStrcat(msg, BAD_CAST message);
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00003184 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003185 xmlSchemaPErrExt(ctxt, node, error, NULL, NULL, NULL,
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003186 (const char*) msg, str1, str2, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003187 }
Daniel Veillarddee23482008-04-11 12:58:43 +00003188 /* Cleanup. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003189 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003190}
3191
William M. Brack2f2a6632004-08-20 23:09:47 +00003192/**
3193 * xmlSchemaPContentErr:
3194 * @ctxt: the schema parser context
3195 * @error: the error code
3196 * @onwerDes: the designation of the holder of the content
3197 * @ownerItem: the owner item of the holder of the content
3198 * @ownerElem: the node of the holder of the content
3199 * @child: the invalid child node
3200 * @message: the optional error message
3201 * @content: the optional string describing the correct content
3202 *
3203 * Reports an error concerning the content of a schema element.
3204 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003205static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003206xmlSchemaPContentErr(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003207 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003208 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003209 xmlNodePtr ownerElem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003210 xmlNodePtr child,
3211 const char *message,
3212 const char *content)
3213{
3214 xmlChar *des = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003215
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003216 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003217 if (message != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003218 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3219 "%s: %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003220 BAD_CAST des, BAD_CAST message);
3221 else {
3222 if (content != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003223 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3224 "%s: The content is not valid. Expected is %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003225 BAD_CAST des, BAD_CAST content);
3226 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003227 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3228 "%s: The content is not valid.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003229 BAD_CAST des, NULL);
3230 }
3231 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003232 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003233}
3234
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003235/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003236 * *
3237 * Streamable error functions *
3238 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003239 ************************************************************************/
Kasimier T. Buchcik8b418172004-11-17 13:14:27 +00003240
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003241
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003242
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003243
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003244/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003245 * *
3246 * Validation helper functions *
3247 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003248 ************************************************************************/
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003249
Daniel Veillardc0826a72004-08-10 14:17:33 +00003250
Daniel Veillard4255d502002-04-16 15:50:10 +00003251/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003252 * *
3253 * Allocation functions *
3254 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00003255 ************************************************************************/
3256
3257/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003258 * xmlSchemaNewSchemaForParserCtxt:
William M. Brack08171912003-12-29 02:52:11 +00003259 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003260 *
3261 * Allocate a new Schema structure.
3262 *
3263 * Returns the newly allocated structure or NULL in case or error
3264 */
3265static xmlSchemaPtr
3266xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt)
3267{
3268 xmlSchemaPtr ret;
3269
3270 ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema));
3271 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003272 xmlSchemaPErrMemory(ctxt, "allocating schema", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003273 return (NULL);
3274 }
3275 memset(ret, 0, sizeof(xmlSchema));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003276 ret->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +00003277 xmlDictReference(ret->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00003278
3279 return (ret);
3280}
3281
3282/**
3283 * xmlSchemaNewFacet:
Daniel Veillard4255d502002-04-16 15:50:10 +00003284 *
3285 * Allocate a new Facet structure.
3286 *
3287 * Returns the newly allocated structure or NULL in case or error
3288 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003289xmlSchemaFacetPtr
3290xmlSchemaNewFacet(void)
Daniel Veillard4255d502002-04-16 15:50:10 +00003291{
3292 xmlSchemaFacetPtr ret;
3293
3294 ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet));
3295 if (ret == NULL) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003296 return (NULL);
3297 }
3298 memset(ret, 0, sizeof(xmlSchemaFacet));
3299
3300 return (ret);
3301}
3302
3303/**
3304 * xmlSchemaNewAnnot:
William M. Brack08171912003-12-29 02:52:11 +00003305 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003306 * @node: a node
3307 *
3308 * Allocate a new annotation structure.
3309 *
3310 * Returns the newly allocated structure or NULL in case or error
3311 */
3312static xmlSchemaAnnotPtr
3313xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
3314{
3315 xmlSchemaAnnotPtr ret;
3316
3317 ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot));
3318 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003319 xmlSchemaPErrMemory(ctxt, "allocating annotation", node);
Daniel Veillard4255d502002-04-16 15:50:10 +00003320 return (NULL);
3321 }
3322 memset(ret, 0, sizeof(xmlSchemaAnnot));
3323 ret->content = node;
3324 return (ret);
3325}
3326
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003327static xmlSchemaItemListPtr
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003328xmlSchemaItemListCreate(void)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003329{
3330 xmlSchemaItemListPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003331
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003332 ret = xmlMalloc(sizeof(xmlSchemaItemList));
3333 if (ret == NULL) {
3334 xmlSchemaPErrMemory(NULL,
3335 "allocating an item list structure", NULL);
3336 return (NULL);
3337 }
3338 memset(ret, 0, sizeof(xmlSchemaItemList));
3339 return (ret);
3340}
3341
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003342static void
3343xmlSchemaItemListClear(xmlSchemaItemListPtr list)
3344{
3345 if (list->items != NULL) {
3346 xmlFree(list->items);
3347 list->items = NULL;
3348 }
3349 list->nbItems = 0;
3350 list->sizeItems = 0;
3351}
3352
3353static int
3354xmlSchemaItemListAdd(xmlSchemaItemListPtr list, void *item)
3355{
3356 if (list->items == NULL) {
3357 list->items = (void **) xmlMalloc(
3358 20 * sizeof(void *));
3359 if (list->items == NULL) {
3360 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3361 return(-1);
3362 }
3363 list->sizeItems = 20;
3364 } else if (list->sizeItems <= list->nbItems) {
3365 list->sizeItems *= 2;
3366 list->items = (void **) xmlRealloc(list->items,
3367 list->sizeItems * sizeof(void *));
3368 if (list->items == NULL) {
3369 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3370 list->sizeItems = 0;
3371 return(-1);
3372 }
3373 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003374 list->items[list->nbItems++] = item;
3375 return(0);
3376}
3377
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003378static int
3379xmlSchemaItemListAddSize(xmlSchemaItemListPtr list,
3380 int initialSize,
3381 void *item)
3382{
3383 if (list->items == NULL) {
3384 if (initialSize <= 0)
3385 initialSize = 1;
3386 list->items = (void **) xmlMalloc(
3387 initialSize * sizeof(void *));
3388 if (list->items == NULL) {
3389 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3390 return(-1);
3391 }
3392 list->sizeItems = initialSize;
3393 } else if (list->sizeItems <= list->nbItems) {
3394 list->sizeItems *= 2;
3395 list->items = (void **) xmlRealloc(list->items,
3396 list->sizeItems * sizeof(void *));
3397 if (list->items == NULL) {
3398 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3399 list->sizeItems = 0;
3400 return(-1);
3401 }
3402 }
3403 list->items[list->nbItems++] = item;
3404 return(0);
3405}
3406
3407static int
3408xmlSchemaItemListInsert(xmlSchemaItemListPtr list, void *item, int idx)
Daniel Veillarddee23482008-04-11 12:58:43 +00003409{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003410 if (list->items == NULL) {
3411 list->items = (void **) xmlMalloc(
3412 20 * sizeof(void *));
3413 if (list->items == NULL) {
3414 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3415 return(-1);
3416 }
3417 list->sizeItems = 20;
3418 } else if (list->sizeItems <= list->nbItems) {
3419 list->sizeItems *= 2;
3420 list->items = (void **) xmlRealloc(list->items,
3421 list->sizeItems * sizeof(void *));
3422 if (list->items == NULL) {
3423 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3424 list->sizeItems = 0;
3425 return(-1);
3426 }
3427 }
3428 /*
3429 * Just append if the index is greater/equal than the item count.
3430 */
3431 if (idx >= list->nbItems) {
3432 list->items[list->nbItems++] = item;
3433 } else {
3434 int i;
3435 for (i = list->nbItems; i > idx; i--)
3436 list->items[i] = list->items[i-1];
3437 list->items[idx] = item;
3438 list->nbItems++;
3439 }
3440 return(0);
3441}
3442
3443#if 0 /* enable if ever needed */
3444static int
3445xmlSchemaItemListInsertSize(xmlSchemaItemListPtr list,
3446 int initialSize,
3447 void *item,
3448 int idx)
Daniel Veillarddee23482008-04-11 12:58:43 +00003449{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003450 if (list->items == NULL) {
3451 if (initialSize <= 0)
3452 initialSize = 1;
3453 list->items = (void **) xmlMalloc(
3454 initialSize * sizeof(void *));
3455 if (list->items == NULL) {
3456 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3457 return(-1);
3458 }
3459 list->sizeItems = initialSize;
3460 } else if (list->sizeItems <= list->nbItems) {
3461 list->sizeItems *= 2;
3462 list->items = (void **) xmlRealloc(list->items,
3463 list->sizeItems * sizeof(void *));
3464 if (list->items == NULL) {
3465 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3466 list->sizeItems = 0;
3467 return(-1);
3468 }
3469 }
3470 /*
3471 * Just append if the index is greater/equal than the item count.
3472 */
3473 if (idx >= list->nbItems) {
3474 list->items[list->nbItems++] = item;
3475 } else {
3476 int i;
3477 for (i = list->nbItems; i > idx; i--)
3478 list->items[i] = list->items[i-1];
3479 list->items[idx] = item;
3480 list->nbItems++;
3481 }
3482 return(0);
3483}
3484#endif
3485
3486static int
3487xmlSchemaItemListRemove(xmlSchemaItemListPtr list, int idx)
3488{
3489 int i;
3490 if ((list->items == NULL) || (idx >= list->nbItems)) {
3491 xmlSchemaPSimpleErr("Internal error: xmlSchemaItemListRemove, "
3492 "index error.\n");
3493 return(-1);
3494 }
3495
3496 if (list->nbItems == 1) {
3497 /* TODO: Really free the list? */
3498 xmlFree(list->items);
3499 list->items = NULL;
3500 list->nbItems = 0;
3501 list->sizeItems = 0;
3502 } else if (list->nbItems -1 == idx) {
3503 list->nbItems--;
Daniel Veillarddee23482008-04-11 12:58:43 +00003504 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003505 for (i = idx; i < list->nbItems -1; i++)
3506 list->items[i] = list->items[i+1];
3507 list->nbItems--;
3508 }
3509 return(0);
3510}
3511
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003512/**
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003513 * xmlSchemaItemListFree:
3514 * @annot: a schema type structure
3515 *
3516 * Deallocate a annotation structure
3517 */
3518static void
3519xmlSchemaItemListFree(xmlSchemaItemListPtr list)
3520{
3521 if (list == NULL)
3522 return;
3523 if (list->items != NULL)
3524 xmlFree(list->items);
3525 xmlFree(list);
3526}
3527
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003528static void
3529xmlSchemaBucketFree(xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003530{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003531 if (bucket == NULL)
3532 return;
3533 if (bucket->globals != NULL) {
3534 xmlSchemaComponentListFree(bucket->globals);
3535 xmlSchemaItemListFree(bucket->globals);
3536 }
3537 if (bucket->locals != NULL) {
3538 xmlSchemaComponentListFree(bucket->locals);
Daniel Veillarddee23482008-04-11 12:58:43 +00003539 xmlSchemaItemListFree(bucket->locals);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003540 }
3541 if (bucket->relations != NULL) {
3542 xmlSchemaSchemaRelationPtr prev, cur = bucket->relations;
3543 do {
Daniel Veillarddee23482008-04-11 12:58:43 +00003544 prev = cur;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003545 cur = cur->next;
3546 xmlFree(prev);
3547 } while (cur != NULL);
3548 }
3549 if ((! bucket->preserveDoc) && (bucket->doc != NULL)) {
3550 xmlFreeDoc(bucket->doc);
Daniel Veillarddee23482008-04-11 12:58:43 +00003551 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003552 if (bucket->type == XML_SCHEMA_SCHEMA_IMPORT) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003553 if (WXS_IMPBUCKET(bucket)->schema != NULL)
3554 xmlSchemaFree(WXS_IMPBUCKET(bucket)->schema);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003555 }
3556 xmlFree(bucket);
3557}
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003558
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003559static xmlSchemaBucketPtr
3560xmlSchemaBucketCreate(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003561 int type, const xmlChar *targetNamespace)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003562{
3563 xmlSchemaBucketPtr ret;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003564 int size;
3565 xmlSchemaPtr mainSchema;
3566
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003567 if (WXS_CONSTRUCTOR(pctxt)->mainSchema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003568 PERROR_INT("xmlSchemaBucketCreate",
3569 "no main schema on constructor");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003570 return(NULL);
3571 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003572 mainSchema = WXS_CONSTRUCTOR(pctxt)->mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003573 /* Create the schema bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003574 if (WXS_IS_BUCKET_INCREDEF(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003575 size = sizeof(xmlSchemaInclude);
3576 else
3577 size = sizeof(xmlSchemaImport);
3578 ret = (xmlSchemaBucketPtr) xmlMalloc(size);
3579 if (ret == NULL) {
3580 xmlSchemaPErrMemory(NULL, "allocating schema bucket", NULL);
3581 return(NULL);
3582 }
3583 memset(ret, 0, size);
3584 ret->targetNamespace = targetNamespace;
3585 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003586 ret->globals = xmlSchemaItemListCreate();
3587 if (ret->globals == NULL) {
3588 xmlFree(ret);
3589 return(NULL);
3590 }
3591 ret->locals = xmlSchemaItemListCreate();
3592 if (ret->locals == NULL) {
3593 xmlFree(ret);
3594 return(NULL);
3595 }
Daniel Veillarddee23482008-04-11 12:58:43 +00003596 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003597 * The following will assure that only the first bucket is marked as
3598 * XML_SCHEMA_SCHEMA_MAIN and it points to the *main* schema.
3599 * For each following import buckets an xmlSchema will be created.
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003600 * An xmlSchema will be created for every distinct targetNamespace.
3601 * We assign the targetNamespace to the schemata here.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003602 */
3603 if (! WXS_HAS_BUCKETS(pctxt)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003604 if (WXS_IS_BUCKET_INCREDEF(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003605 PERROR_INT("xmlSchemaBucketCreate",
3606 "first bucket but it's an include or redefine");
3607 xmlSchemaBucketFree(ret);
3608 return(NULL);
3609 }
Daniel Veillarddee23482008-04-11 12:58:43 +00003610 /* Force the type to be XML_SCHEMA_SCHEMA_MAIN. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003611 ret->type = XML_SCHEMA_SCHEMA_MAIN;
3612 /* Point to the *main* schema. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003613 WXS_CONSTRUCTOR(pctxt)->mainBucket = ret;
3614 WXS_IMPBUCKET(ret)->schema = mainSchema;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003615 /*
3616 * Ensure that the main schema gets a targetNamespace.
3617 */
3618 mainSchema->targetNamespace = targetNamespace;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003619 } else {
Daniel Veillarddee23482008-04-11 12:58:43 +00003620 if (type == XML_SCHEMA_SCHEMA_MAIN) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003621 PERROR_INT("xmlSchemaBucketCreate",
3622 "main bucket but it's not the first one");
3623 xmlSchemaBucketFree(ret);
3624 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00003625 } else if (type == XML_SCHEMA_SCHEMA_IMPORT) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003626 /*
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003627 * Create a schema for imports and assign the
3628 * targetNamespace.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003629 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003630 WXS_IMPBUCKET(ret)->schema = xmlSchemaNewSchema(pctxt);
3631 if (WXS_IMPBUCKET(ret)->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003632 xmlSchemaBucketFree(ret);
3633 return(NULL);
3634 }
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003635 WXS_IMPBUCKET(ret)->schema->targetNamespace = targetNamespace;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003636 }
Daniel Veillarddee23482008-04-11 12:58:43 +00003637 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003638 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003639 int res;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00003640 /*
3641 * Imports go into the "schemasImports" slot of the main *schema*.
3642 * Note that we create an import entry for the main schema as well; i.e.,
3643 * even if there's only one schema, we'll get an import.
3644 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003645 if (mainSchema->schemasImports == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003646 mainSchema->schemasImports = xmlHashCreateDict(5,
3647 WXS_CONSTRUCTOR(pctxt)->dict);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003648 if (mainSchema->schemasImports == NULL) {
3649 xmlSchemaBucketFree(ret);
3650 return(NULL);
3651 }
3652 }
3653 if (targetNamespace == NULL)
3654 res = xmlHashAddEntry(mainSchema->schemasImports,
3655 XML_SCHEMAS_NO_NAMESPACE, ret);
3656 else
3657 res = xmlHashAddEntry(mainSchema->schemasImports,
3658 targetNamespace, ret);
3659 if (res != 0) {
3660 PERROR_INT("xmlSchemaBucketCreate",
3661 "failed to add the schema bucket to the hash");
3662 xmlSchemaBucketFree(ret);
3663 return(NULL);
3664 }
3665 } else {
3666 /* Set the @ownerImport of an include bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003667 if (WXS_IS_BUCKET_IMPMAIN(WXS_CONSTRUCTOR(pctxt)->bucket->type))
3668 WXS_INCBUCKET(ret)->ownerImport =
3669 WXS_IMPBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003670 else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003671 WXS_INCBUCKET(ret)->ownerImport =
3672 WXS_INCBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket)->ownerImport;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003673
3674 /* Includes got into the "includes" slot of the *main* schema. */
3675 if (mainSchema->includes == NULL) {
3676 mainSchema->includes = xmlSchemaItemListCreate();
3677 if (mainSchema->includes == NULL) {
3678 xmlSchemaBucketFree(ret);
3679 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00003680 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003681 }
3682 xmlSchemaItemListAdd(mainSchema->includes, ret);
3683 }
Daniel Veillarddee23482008-04-11 12:58:43 +00003684 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003685 * Add to list of all buckets; this is used for lookup
3686 * during schema construction time only.
3687 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003688 if (xmlSchemaItemListAdd(WXS_CONSTRUCTOR(pctxt)->buckets, ret) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003689 return(NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003690 return(ret);
3691}
3692
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003693static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003694xmlSchemaAddItemSize(xmlSchemaItemListPtr *list, int initialSize, void *item)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003695{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003696 if (*list == NULL) {
3697 *list = xmlSchemaItemListCreate();
3698 if (*list == NULL)
3699 return(-1);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003700 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003701 xmlSchemaItemListAddSize(*list, initialSize, item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003702 return(0);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003703}
3704
3705/**
Daniel Veillardfdc91562002-07-01 21:52:03 +00003706 * xmlSchemaFreeAnnot:
3707 * @annot: a schema type structure
3708 *
3709 * Deallocate a annotation structure
3710 */
3711static void
3712xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot)
3713{
3714 if (annot == NULL)
3715 return;
Kasimier T. Buchcik004b5462005-08-08 12:43:09 +00003716 if (annot->next == NULL) {
3717 xmlFree(annot);
3718 } else {
3719 xmlSchemaAnnotPtr prev;
3720
3721 do {
3722 prev = annot;
3723 annot = annot->next;
3724 xmlFree(prev);
3725 } while (annot != NULL);
3726 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00003727}
3728
3729/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003730 * xmlSchemaFreeNotation:
3731 * @schema: a schema notation structure
3732 *
3733 * Deallocate a Schema Notation structure.
3734 */
3735static void
3736xmlSchemaFreeNotation(xmlSchemaNotationPtr nota)
3737{
3738 if (nota == NULL)
3739 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003740 xmlFree(nota);
3741}
3742
3743/**
3744 * xmlSchemaFreeAttribute:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003745 * @attr: an attribute declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00003746 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003747 * Deallocates an attribute declaration structure.
Daniel Veillard4255d502002-04-16 15:50:10 +00003748 */
3749static void
3750xmlSchemaFreeAttribute(xmlSchemaAttributePtr attr)
3751{
3752 if (attr == NULL)
3753 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003754 if (attr->annot != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003755 xmlSchemaFreeAnnot(attr->annot);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003756 if (attr->defVal != NULL)
3757 xmlSchemaFreeValue(attr->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003758 xmlFree(attr);
3759}
3760
3761/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003762 * xmlSchemaFreeAttributeUse:
3763 * @use: an attribute use
3764 *
3765 * Deallocates an attribute use structure.
3766 */
3767static void
3768xmlSchemaFreeAttributeUse(xmlSchemaAttributeUsePtr use)
3769{
3770 if (use == NULL)
3771 return;
3772 if (use->annot != NULL)
3773 xmlSchemaFreeAnnot(use->annot);
3774 if (use->defVal != NULL)
3775 xmlSchemaFreeValue(use->defVal);
3776 xmlFree(use);
3777}
3778
3779/**
3780 * xmlSchemaFreeAttributeUseProhib:
3781 * @prohib: an attribute use prohibition
3782 *
3783 * Deallocates an attribute use structure.
3784 */
3785static void
3786xmlSchemaFreeAttributeUseProhib(xmlSchemaAttributeUseProhibPtr prohib)
3787{
3788 if (prohib == NULL)
3789 return;
3790 xmlFree(prohib);
3791}
3792
3793/**
Daniel Veillard3646d642004-06-02 19:19:14 +00003794 * xmlSchemaFreeWildcardNsSet:
3795 * set: a schema wildcard namespace
3796 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003797 * Deallocates a list of wildcard constraint structures.
Daniel Veillard3646d642004-06-02 19:19:14 +00003798 */
3799static void
3800xmlSchemaFreeWildcardNsSet(xmlSchemaWildcardNsPtr set)
3801{
3802 xmlSchemaWildcardNsPtr next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003803
Daniel Veillard3646d642004-06-02 19:19:14 +00003804 while (set != NULL) {
3805 next = set->next;
3806 xmlFree(set);
3807 set = next;
3808 }
3809}
3810
3811/**
3812 * xmlSchemaFreeWildcard:
Daniel Veillard01fa6152004-06-29 17:04:39 +00003813 * @wildcard: a wildcard structure
Daniel Veillard3646d642004-06-02 19:19:14 +00003814 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003815 * Deallocates a wildcard structure.
Daniel Veillard3646d642004-06-02 19:19:14 +00003816 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00003817void
Daniel Veillard3646d642004-06-02 19:19:14 +00003818xmlSchemaFreeWildcard(xmlSchemaWildcardPtr wildcard)
3819{
3820 if (wildcard == NULL)
3821 return;
3822 if (wildcard->annot != NULL)
3823 xmlSchemaFreeAnnot(wildcard->annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003824 if (wildcard->nsSet != NULL)
3825 xmlSchemaFreeWildcardNsSet(wildcard->nsSet);
3826 if (wildcard->negNsSet != NULL)
3827 xmlFree(wildcard->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +00003828 xmlFree(wildcard);
3829}
3830
3831/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003832 * xmlSchemaFreeAttributeGroup:
3833 * @schema: a schema attribute group structure
3834 *
3835 * Deallocate a Schema Attribute Group structure.
3836 */
3837static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003838xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillard4255d502002-04-16 15:50:10 +00003839{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003840 if (attrGr == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00003841 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003842 if (attrGr->annot != NULL)
3843 xmlSchemaFreeAnnot(attrGr->annot);
3844 if (attrGr->attrUses != NULL)
3845 xmlSchemaItemListFree(WXS_LIST_CAST attrGr->attrUses);
3846 xmlFree(attrGr);
Daniel Veillard3646d642004-06-02 19:19:14 +00003847}
3848
3849/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003850 * xmlSchemaFreeQNameRef:
3851 * @item: a QName reference structure
3852 *
3853 * Deallocatea a QName reference structure.
3854 */
3855static void
3856xmlSchemaFreeQNameRef(xmlSchemaQNameRefPtr item)
3857{
3858 xmlFree(item);
3859}
3860
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003861/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00003862 * xmlSchemaFreeTypeLinkList:
3863 * @alink: a type link
3864 *
3865 * Deallocate a list of types.
3866 */
3867static void
3868xmlSchemaFreeTypeLinkList(xmlSchemaTypeLinkPtr link)
3869{
3870 xmlSchemaTypeLinkPtr next;
3871
3872 while (link != NULL) {
3873 next = link->next;
3874 xmlFree(link);
3875 link = next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003876 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003877}
3878
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003879static void
3880xmlSchemaFreeIDCStateObjList(xmlSchemaIDCStateObjPtr sto)
3881{
3882 xmlSchemaIDCStateObjPtr next;
3883 while (sto != NULL) {
3884 next = sto->next;
3885 if (sto->history != NULL)
3886 xmlFree(sto->history);
3887 if (sto->xpathCtxt != NULL)
3888 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
3889 xmlFree(sto);
3890 sto = next;
3891 }
3892}
3893
3894/**
3895 * xmlSchemaFreeIDC:
3896 * @idc: a identity-constraint definition
3897 *
3898 * Deallocates an identity-constraint definition.
3899 */
3900static void
3901xmlSchemaFreeIDC(xmlSchemaIDCPtr idcDef)
3902{
3903 xmlSchemaIDCSelectPtr cur, prev;
3904
3905 if (idcDef == NULL)
3906 return;
3907 if (idcDef->annot != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003908 xmlSchemaFreeAnnot(idcDef->annot);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003909 /* Selector */
3910 if (idcDef->selector != NULL) {
3911 if (idcDef->selector->xpathComp != NULL)
3912 xmlFreePattern((xmlPatternPtr) idcDef->selector->xpathComp);
3913 xmlFree(idcDef->selector);
3914 }
3915 /* Fields */
3916 if (idcDef->fields != NULL) {
3917 cur = idcDef->fields;
3918 do {
3919 prev = cur;
3920 cur = cur->next;
3921 if (prev->xpathComp != NULL)
3922 xmlFreePattern((xmlPatternPtr) prev->xpathComp);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003923 xmlFree(prev);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003924 } while (cur != NULL);
3925 }
3926 xmlFree(idcDef);
3927}
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003928
Daniel Veillard01fa6152004-06-29 17:04:39 +00003929/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003930 * xmlSchemaFreeElement:
3931 * @schema: a schema element structure
3932 *
3933 * Deallocate a Schema Element structure.
3934 */
3935static void
3936xmlSchemaFreeElement(xmlSchemaElementPtr elem)
3937{
3938 if (elem == NULL)
3939 return;
Daniel Veillard32370232002-10-16 14:08:14 +00003940 if (elem->annot != NULL)
3941 xmlSchemaFreeAnnot(elem->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003942 if (elem->contModel != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003943 xmlRegFreeRegexp(elem->contModel);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003944 if (elem->defVal != NULL)
3945 xmlSchemaFreeValue(elem->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003946 xmlFree(elem);
3947}
3948
3949/**
3950 * xmlSchemaFreeFacet:
3951 * @facet: a schema facet structure
3952 *
3953 * Deallocate a Schema Facet structure.
3954 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003955void
Daniel Veillard4255d502002-04-16 15:50:10 +00003956xmlSchemaFreeFacet(xmlSchemaFacetPtr facet)
3957{
3958 if (facet == NULL)
3959 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003960 if (facet->val != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003961 xmlSchemaFreeValue(facet->val);
Daniel Veillard4255d502002-04-16 15:50:10 +00003962 if (facet->regexp != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003963 xmlRegFreeRegexp(facet->regexp);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003964 if (facet->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003965 xmlSchemaFreeAnnot(facet->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003966 xmlFree(facet);
3967}
3968
3969/**
3970 * xmlSchemaFreeType:
3971 * @type: a schema type structure
3972 *
3973 * Deallocate a Schema Type structure.
3974 */
3975void
3976xmlSchemaFreeType(xmlSchemaTypePtr type)
3977{
3978 if (type == NULL)
3979 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003980 if (type->annot != NULL)
Daniel Veillard32370232002-10-16 14:08:14 +00003981 xmlSchemaFreeAnnot(type->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003982 if (type->facets != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003983 xmlSchemaFacetPtr facet, next;
Daniel Veillard4255d502002-04-16 15:50:10 +00003984
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003985 facet = type->facets;
3986 while (facet != NULL) {
3987 next = facet->next;
3988 xmlSchemaFreeFacet(facet);
3989 facet = next;
3990 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003991 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003992 if (type->attrUses != NULL)
3993 xmlSchemaItemListFree((xmlSchemaItemListPtr) type->attrUses);
Daniel Veillard01fa6152004-06-29 17:04:39 +00003994 if (type->memberTypes != NULL)
3995 xmlSchemaFreeTypeLinkList(type->memberTypes);
3996 if (type->facetSet != NULL) {
3997 xmlSchemaFacetLinkPtr next, link;
3998
3999 link = type->facetSet;
4000 do {
4001 next = link->next;
4002 xmlFree(link);
4003 link = next;
4004 } while (link != NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004005 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004006 if (type->contModel != NULL)
4007 xmlRegFreeRegexp(type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +00004008 xmlFree(type);
4009}
4010
4011/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004012 * xmlSchemaFreeModelGroupDef:
4013 * @item: a schema model group definition
4014 *
4015 * Deallocates a schema model group definition.
4016 */
4017static void
4018xmlSchemaFreeModelGroupDef(xmlSchemaModelGroupDefPtr item)
4019{
4020 if (item->annot != NULL)
4021 xmlSchemaFreeAnnot(item->annot);
4022 xmlFree(item);
4023}
4024
4025/**
4026 * xmlSchemaFreeModelGroup:
4027 * @item: a schema model group
4028 *
4029 * Deallocates a schema model group structure.
4030 */
4031static void
4032xmlSchemaFreeModelGroup(xmlSchemaModelGroupPtr item)
4033{
4034 if (item->annot != NULL)
4035 xmlSchemaFreeAnnot(item->annot);
4036 xmlFree(item);
4037}
4038
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004039static void
4040xmlSchemaComponentListFree(xmlSchemaItemListPtr list)
4041{
4042 if ((list == NULL) || (list->nbItems == 0))
4043 return;
4044 {
4045 xmlSchemaTreeItemPtr item;
4046 xmlSchemaTreeItemPtr *items = (xmlSchemaTreeItemPtr *) list->items;
4047 int i;
4048
4049 for (i = 0; i < list->nbItems; i++) {
4050 item = items[i];
4051 if (item == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +00004052 continue;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004053 switch (item->type) {
4054 case XML_SCHEMA_TYPE_SIMPLE:
4055 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004056 xmlSchemaFreeType((xmlSchemaTypePtr) item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004057 break;
4058 case XML_SCHEMA_TYPE_ATTRIBUTE:
4059 xmlSchemaFreeAttribute((xmlSchemaAttributePtr) item);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004060 break;
4061 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
4062 xmlSchemaFreeAttributeUse((xmlSchemaAttributeUsePtr) item);
4063 break;
4064 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
4065 xmlSchemaFreeAttributeUseProhib(
4066 (xmlSchemaAttributeUseProhibPtr) item);
4067 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004068 case XML_SCHEMA_TYPE_ELEMENT:
4069 xmlSchemaFreeElement((xmlSchemaElementPtr) item);
4070 break;
4071 case XML_SCHEMA_TYPE_PARTICLE:
4072 if (item->annot != NULL)
4073 xmlSchemaFreeAnnot(item->annot);
4074 xmlFree(item);
4075 break;
4076 case XML_SCHEMA_TYPE_SEQUENCE:
4077 case XML_SCHEMA_TYPE_CHOICE:
4078 case XML_SCHEMA_TYPE_ALL:
4079 xmlSchemaFreeModelGroup((xmlSchemaModelGroupPtr) item);
4080 break;
4081 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
4082 xmlSchemaFreeAttributeGroup(
4083 (xmlSchemaAttributeGroupPtr) item);
4084 break;
4085 case XML_SCHEMA_TYPE_GROUP:
4086 xmlSchemaFreeModelGroupDef(
4087 (xmlSchemaModelGroupDefPtr) item);
4088 break;
4089 case XML_SCHEMA_TYPE_ANY:
4090 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
4091 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr) item);
4092 break;
4093 case XML_SCHEMA_TYPE_IDC_KEY:
4094 case XML_SCHEMA_TYPE_IDC_UNIQUE:
4095 case XML_SCHEMA_TYPE_IDC_KEYREF:
4096 xmlSchemaFreeIDC((xmlSchemaIDCPtr) item);
4097 break;
4098 case XML_SCHEMA_TYPE_NOTATION:
4099 xmlSchemaFreeNotation((xmlSchemaNotationPtr) item);
4100 break;
4101 case XML_SCHEMA_EXTRA_QNAMEREF:
4102 xmlSchemaFreeQNameRef((xmlSchemaQNameRefPtr) item);
4103 break;
4104 default: {
4105 /* TODO: This should never be hit. */
4106 xmlSchemaPSimpleInternalErr(NULL,
4107 "Internal error: xmlSchemaComponentListFree, "
4108 "unexpected component type '%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004109 (const xmlChar *) WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004110 }
4111 break;
4112 }
4113 }
4114 list->nbItems = 0;
4115 }
4116}
4117
Daniel Veillardb0f397e2003-12-23 23:30:53 +00004118/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004119 * xmlSchemaFree:
4120 * @schema: a schema structure
4121 *
4122 * Deallocate a Schema structure.
4123 */
4124void
4125xmlSchemaFree(xmlSchemaPtr schema)
4126{
4127 if (schema == NULL)
4128 return;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004129 /* @volatiles is not used anymore :-/ */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004130 if (schema->volatiles != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004131 TODO
4132 /*
4133 * Note that those slots are not responsible for freeing
4134 * schema components anymore; this will now be done by
4135 * the schema buckets.
4136 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004137 if (schema->notaDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004138 xmlHashFree(schema->notaDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004139 if (schema->attrDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004140 xmlHashFree(schema->attrDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004141 if (schema->attrgrpDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004142 xmlHashFree(schema->attrgrpDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004143 if (schema->elemDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004144 xmlHashFree(schema->elemDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004145 if (schema->typeDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004146 xmlHashFree(schema->typeDecl, NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004147 if (schema->groupDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004148 xmlHashFree(schema->groupDecl, NULL);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004149 if (schema->idcDef != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004150 xmlHashFree(schema->idcDef, NULL);
4151
Daniel Veillard1d913862003-11-21 00:28:39 +00004152 if (schema->schemasImports != NULL)
4153 xmlHashFree(schema->schemasImports,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004154 (xmlHashDeallocator) xmlSchemaBucketFree);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004155 if (schema->includes != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004156 xmlSchemaItemListPtr list = (xmlSchemaItemListPtr) schema->includes;
4157 int i;
4158 for (i = 0; i < list->nbItems; i++) {
Daniel Veillarddee23482008-04-11 12:58:43 +00004159 xmlSchemaBucketFree((xmlSchemaBucketPtr) list->items[i]);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004160 }
4161 xmlSchemaItemListFree(list);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004162 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004163 if (schema->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004164 xmlSchemaFreeAnnot(schema->annot);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004165 /* Never free the doc here, since this will be done by the buckets. */
4166
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004167 xmlDictFree(schema->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00004168 xmlFree(schema);
4169}
4170
4171/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004172 * *
4173 * Debug functions *
4174 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00004175 ************************************************************************/
4176
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004177#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004178
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004179static void
4180xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output); /* forward */
4181
Daniel Veillard4255d502002-04-16 15:50:10 +00004182/**
4183 * xmlSchemaElementDump:
4184 * @elem: an element
4185 * @output: the file output
4186 *
4187 * Dump the element
4188 */
4189static void
4190xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004191 const xmlChar * name ATTRIBUTE_UNUSED,
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004192 const xmlChar * namespace ATTRIBUTE_UNUSED,
4193 const xmlChar * context ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00004194{
4195 if (elem == NULL)
4196 return;
4197
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004198
4199 fprintf(output, "Element");
4200 if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL)
4201 fprintf(output, " (global)");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004202 fprintf(output, ": '%s' ", elem->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004203 if (namespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004204 fprintf(output, "ns '%s'", namespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004205 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004206#if 0
Daniel Veillard4255d502002-04-16 15:50:10 +00004207 if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004208 fprintf(output, " min %d ", elem->minOccurs);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004209 if (elem->maxOccurs >= UNBOUNDED)
4210 fprintf(output, "max: unbounded\n");
4211 else if (elem->maxOccurs != 1)
4212 fprintf(output, "max: %d\n", elem->maxOccurs);
4213 else
4214 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004215 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004216#endif
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004217 /*
4218 * Misc other properties.
4219 */
4220 if ((elem->flags & XML_SCHEMAS_ELEM_NILLABLE) ||
4221 (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) ||
4222 (elem->flags & XML_SCHEMAS_ELEM_FIXED) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004223 (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004224 fprintf(output, " props: ");
4225 if (elem->flags & XML_SCHEMAS_ELEM_FIXED)
4226 fprintf(output, "[fixed] ");
4227 if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)
4228 fprintf(output, "[default] ");
4229 if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT)
4230 fprintf(output, "[abstract] ");
4231 if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE)
4232 fprintf(output, "[nillable] ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004233 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004234 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004235 /*
4236 * Default/fixed value.
4237 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004238 if (elem->value != NULL)
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004239 fprintf(output, " value: '%s'\n", elem->value);
4240 /*
4241 * Type.
4242 */
4243 if (elem->namedType != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004244 fprintf(output, " type: '%s' ", elem->namedType);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004245 if (elem->namedTypeNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004246 fprintf(output, "ns '%s'\n", elem->namedTypeNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004247 else
4248 fprintf(output, "\n");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004249 } else if (elem->subtypes != NULL) {
4250 /*
4251 * Dump local types.
4252 */
4253 xmlSchemaTypeDump(elem->subtypes, output);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004254 }
4255 /*
4256 * Substitution group.
4257 */
4258 if (elem->substGroup != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004259 fprintf(output, " substitutionGroup: '%s' ", elem->substGroup);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004260 if (elem->substGroupNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004261 fprintf(output, "ns '%s'\n", elem->substGroupNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004262 else
4263 fprintf(output, "\n");
4264 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004265}
4266
4267/**
4268 * xmlSchemaAnnotDump:
4269 * @output: the file output
4270 * @annot: a annotation
4271 *
4272 * Dump the annotation
4273 */
4274static void
4275xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot)
4276{
4277 xmlChar *content;
4278
4279 if (annot == NULL)
4280 return;
4281
4282 content = xmlNodeGetContent(annot->content);
4283 if (content != NULL) {
4284 fprintf(output, " Annot: %s\n", content);
4285 xmlFree(content);
4286 } else
4287 fprintf(output, " Annot: empty\n");
4288}
4289
4290/**
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004291 * xmlSchemaContentModelDump:
4292 * @particle: the schema particle
4293 * @output: the file output
4294 * @depth: the depth used for intentation
Daniel Veillard4255d502002-04-16 15:50:10 +00004295 *
4296 * Dump a SchemaType structure
4297 */
4298static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004299xmlSchemaContentModelDump(xmlSchemaParticlePtr particle, FILE * output, int depth)
4300{
4301 xmlChar *str = NULL;
4302 xmlSchemaTreeItemPtr term;
4303 char shift[100];
4304 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004305
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004306 if (particle == NULL)
4307 return;
4308 for (i = 0;((i < depth) && (i < 25));i++)
4309 shift[2 * i] = shift[2 * i + 1] = ' ';
4310 shift[2 * i] = shift[2 * i + 1] = 0;
Daniel Veillardbccae2d2009-06-04 11:22:45 +02004311 fprintf(output, "%s", shift);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004312 if (particle->children == NULL) {
4313 fprintf(output, "MISSING particle term\n");
4314 return;
4315 }
4316 term = particle->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004317 if (term == NULL) {
4318 fprintf(output, "(NULL)");
4319 } else {
4320 switch (term->type) {
4321 case XML_SCHEMA_TYPE_ELEMENT:
4322 fprintf(output, "ELEM '%s'", xmlSchemaFormatQName(&str,
4323 ((xmlSchemaElementPtr)term)->targetNamespace,
4324 ((xmlSchemaElementPtr)term)->name));
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004325 FREE_AND_NULL(str);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004326 break;
4327 case XML_SCHEMA_TYPE_SEQUENCE:
4328 fprintf(output, "SEQUENCE");
4329 break;
4330 case XML_SCHEMA_TYPE_CHOICE:
4331 fprintf(output, "CHOICE");
4332 break;
4333 case XML_SCHEMA_TYPE_ALL:
4334 fprintf(output, "ALL");
4335 break;
4336 case XML_SCHEMA_TYPE_ANY:
4337 fprintf(output, "ANY");
4338 break;
4339 default:
4340 fprintf(output, "UNKNOWN\n");
4341 return;
4342 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004343 }
4344 if (particle->minOccurs != 1)
4345 fprintf(output, " min: %d", particle->minOccurs);
4346 if (particle->maxOccurs >= UNBOUNDED)
4347 fprintf(output, " max: unbounded");
4348 else if (particle->maxOccurs != 1)
4349 fprintf(output, " max: %d", particle->maxOccurs);
4350 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004351 if (term &&
4352 ((term->type == XML_SCHEMA_TYPE_SEQUENCE) ||
4353 (term->type == XML_SCHEMA_TYPE_CHOICE) ||
4354 (term->type == XML_SCHEMA_TYPE_ALL)) &&
4355 (term->children != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004356 xmlSchemaContentModelDump((xmlSchemaParticlePtr) term->children,
4357 output, depth +1);
4358 }
4359 if (particle->next != NULL)
4360 xmlSchemaContentModelDump((xmlSchemaParticlePtr) particle->next,
4361 output, depth);
4362}
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004363
4364/**
4365 * xmlSchemaAttrUsesDump:
4366 * @uses: attribute uses list
Daniel Veillarddee23482008-04-11 12:58:43 +00004367 * @output: the file output
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004368 *
4369 * Dumps a list of attribute use components.
4370 */
4371static void
4372xmlSchemaAttrUsesDump(xmlSchemaItemListPtr uses, FILE * output)
4373{
4374 xmlSchemaAttributeUsePtr use;
4375 xmlSchemaAttributeUseProhibPtr prohib;
4376 xmlSchemaQNameRefPtr ref;
4377 const xmlChar *name, *tns;
4378 xmlChar *str = NULL;
4379 int i;
4380
4381 if ((uses == NULL) || (uses->nbItems == 0))
4382 return;
4383
Daniel Veillarddee23482008-04-11 12:58:43 +00004384 fprintf(output, " attributes:\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004385 for (i = 0; i < uses->nbItems; i++) {
4386 use = uses->items[i];
4387 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
4388 fprintf(output, " [prohibition] ");
4389 prohib = (xmlSchemaAttributeUseProhibPtr) use;
4390 name = prohib->name;
4391 tns = prohib->targetNamespace;
4392 } else if (use->type == XML_SCHEMA_EXTRA_QNAMEREF) {
4393 fprintf(output, " [reference] ");
4394 ref = (xmlSchemaQNameRefPtr) use;
4395 name = ref->name;
4396 tns = ref->targetNamespace;
4397 } else {
4398 fprintf(output, " [use] ");
4399 name = WXS_ATTRUSE_DECL_NAME(use);
4400 tns = WXS_ATTRUSE_DECL_TNS(use);
4401 }
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004402 fprintf(output, "'%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004403 (const char *) xmlSchemaFormatQName(&str, tns, name));
4404 FREE_AND_NULL(str);
4405 }
4406}
4407
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004408/**
4409 * xmlSchemaTypeDump:
4410 * @output: the file output
4411 * @type: a type structure
4412 *
4413 * Dump a SchemaType structure
4414 */
4415static void
Daniel Veillard4255d502002-04-16 15:50:10 +00004416xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output)
4417{
4418 if (type == NULL) {
4419 fprintf(output, "Type: NULL\n");
4420 return;
4421 }
4422 fprintf(output, "Type: ");
4423 if (type->name != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004424 fprintf(output, "'%s' ", type->name);
Daniel Veillard4255d502002-04-16 15:50:10 +00004425 else
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004426 fprintf(output, "(no name) ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004427 if (type->targetNamespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004428 fprintf(output, "ns '%s' ", type->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004429 switch (type->type) {
4430 case XML_SCHEMA_TYPE_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004431 fprintf(output, "[basic] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004432 break;
4433 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004434 fprintf(output, "[simple] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004435 break;
4436 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004437 fprintf(output, "[complex] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004438 break;
4439 case XML_SCHEMA_TYPE_SEQUENCE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004440 fprintf(output, "[sequence] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004441 break;
4442 case XML_SCHEMA_TYPE_CHOICE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004443 fprintf(output, "[choice] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004444 break;
4445 case XML_SCHEMA_TYPE_ALL:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004446 fprintf(output, "[all] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004447 break;
4448 case XML_SCHEMA_TYPE_UR:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004449 fprintf(output, "[ur] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004450 break;
4451 case XML_SCHEMA_TYPE_RESTRICTION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004452 fprintf(output, "[restriction] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004453 break;
4454 case XML_SCHEMA_TYPE_EXTENSION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004455 fprintf(output, "[extension] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004456 break;
4457 default:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004458 fprintf(output, "[unknown type %d] ", type->type);
Daniel Veillard4255d502002-04-16 15:50:10 +00004459 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004460 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004461 fprintf(output, "content: ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004462 switch (type->contentType) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004463 case XML_SCHEMA_CONTENT_UNKNOWN:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004464 fprintf(output, "[unknown] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004465 break;
4466 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004467 fprintf(output, "[empty] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004468 break;
4469 case XML_SCHEMA_CONTENT_ELEMENTS:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004470 fprintf(output, "[element] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004471 break;
4472 case XML_SCHEMA_CONTENT_MIXED:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004473 fprintf(output, "[mixed] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004474 break;
4475 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004476 /* not used. */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004477 break;
4478 case XML_SCHEMA_CONTENT_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004479 fprintf(output, "[basic] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004480 break;
4481 case XML_SCHEMA_CONTENT_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004482 fprintf(output, "[simple] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004483 break;
4484 case XML_SCHEMA_CONTENT_ANY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004485 fprintf(output, "[any] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004486 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004487 }
4488 fprintf(output, "\n");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004489 if (type->base != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004490 fprintf(output, " base type: '%s'", type->base);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004491 if (type->baseNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004492 fprintf(output, " ns '%s'\n", type->baseNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004493 else
4494 fprintf(output, "\n");
4495 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004496 if (type->attrUses != NULL)
4497 xmlSchemaAttrUsesDump(type->attrUses, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004498 if (type->annot != NULL)
4499 xmlSchemaAnnotDump(output, type->annot);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004500#ifdef DUMP_CONTENT_MODEL
4501 if ((type->type == XML_SCHEMA_TYPE_COMPLEX) &&
4502 (type->subtypes != NULL)) {
4503 xmlSchemaContentModelDump((xmlSchemaParticlePtr) type->subtypes,
4504 output, 1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004505 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004506#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004507}
4508
4509/**
4510 * xmlSchemaDump:
4511 * @output: the file output
4512 * @schema: a schema structure
4513 *
4514 * Dump a Schema structure.
4515 */
4516void
4517xmlSchemaDump(FILE * output, xmlSchemaPtr schema)
4518{
Daniel Veillardce682bc2004-11-05 17:22:25 +00004519 if (output == NULL)
4520 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004521 if (schema == NULL) {
4522 fprintf(output, "Schemas: NULL\n");
4523 return;
4524 }
4525 fprintf(output, "Schemas: ");
4526 if (schema->name != NULL)
4527 fprintf(output, "%s, ", schema->name);
4528 else
4529 fprintf(output, "no name, ");
4530 if (schema->targetNamespace != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00004531 fprintf(output, "%s", (const char *) schema->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004532 else
4533 fprintf(output, "no target namespace");
4534 fprintf(output, "\n");
4535 if (schema->annot != NULL)
4536 xmlSchemaAnnotDump(output, schema->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00004537 xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump,
4538 output);
4539 xmlHashScanFull(schema->elemDecl,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004540 (xmlHashScannerFull) xmlSchemaElementDump, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004541}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004542
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004543#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004544/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004545 * xmlSchemaDebugDumpIDCTable:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004546 * @vctxt: the WXS validation context
4547 *
4548 * Displays the current IDC table for debug purposes.
4549 */
4550static void
4551xmlSchemaDebugDumpIDCTable(FILE * output,
4552 const xmlChar *namespaceName,
4553 const xmlChar *localName,
4554 xmlSchemaPSVIIDCBindingPtr bind)
4555{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004556 xmlChar *str = NULL;
4557 const xmlChar *value;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004558 xmlSchemaPSVIIDCNodePtr tab;
4559 xmlSchemaPSVIIDCKeyPtr key;
4560 int i, j, res;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004561
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004562 fprintf(output, "IDC: TABLES on '%s'\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004563 xmlSchemaFormatQName(&str, namespaceName, localName));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004564 FREE_AND_NULL(str)
4565
4566 if (bind == NULL)
4567 return;
4568 do {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004569 fprintf(output, "IDC: BINDING '%s' (%d)\n",
Daniel Veillarddee23482008-04-11 12:58:43 +00004570 xmlSchemaGetComponentQName(&str,
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004571 bind->definition), bind->nbNodes);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004572 FREE_AND_NULL(str)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004573 for (i = 0; i < bind->nbNodes; i++) {
4574 tab = bind->nodeTable[i];
4575 fprintf(output, " ( ");
4576 for (j = 0; j < bind->definition->nbFields; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004577 key = tab->keys[j];
4578 if ((key != NULL) && (key->val != NULL)) {
4579 res = xmlSchemaGetCanonValue(key->val, &value);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004580 if (res >= 0)
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004581 fprintf(output, "'%s' ", value);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004582 else
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004583 fprintf(output, "CANON-VALUE-FAILED ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004584 if (res == 0)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004585 FREE_AND_NULL(value)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004586 } else if (key != NULL)
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004587 fprintf(output, "(no val), ");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004588 else
4589 fprintf(output, "(key missing), ");
4590 }
4591 fprintf(output, ")\n");
4592 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004593 if (bind->dupls && bind->dupls->nbItems) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004594 fprintf(output, "IDC: dupls (%d):\n", bind->dupls->nbItems);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004595 for (i = 0; i < bind->dupls->nbItems; i++) {
4596 tab = bind->dupls->items[i];
4597 fprintf(output, " ( ");
4598 for (j = 0; j < bind->definition->nbFields; j++) {
4599 key = tab->keys[j];
4600 if ((key != NULL) && (key->val != NULL)) {
4601 res = xmlSchemaGetCanonValue(key->val, &value);
4602 if (res >= 0)
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004603 fprintf(output, "'%s' ", value);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004604 else
4605 fprintf(output, "CANON-VALUE-FAILED ");
4606 if (res == 0)
4607 FREE_AND_NULL(value)
4608 } else if (key != NULL)
4609 fprintf(output, "(no val), ");
4610 else
4611 fprintf(output, "(key missing), ");
4612 }
4613 fprintf(output, ")\n");
4614 }
4615 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004616 bind = bind->next;
4617 } while (bind != NULL);
4618}
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00004619#endif /* DEBUG_IDC */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004620#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4255d502002-04-16 15:50:10 +00004621
4622/************************************************************************
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004623 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004624 * Utilities *
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004625 * *
4626 ************************************************************************/
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004627
Daniel Veillardc0826a72004-08-10 14:17:33 +00004628/**
4629 * xmlSchemaGetPropNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004630 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004631 * @name: the name of the attribute
4632 *
4633 * Seeks an attribute with a name of @name in
4634 * no namespace.
4635 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004636 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004637 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004638static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004639xmlSchemaGetPropNode(xmlNodePtr node, const char *name)
Daniel Veillard01fa6152004-06-29 17:04:39 +00004640{
4641 xmlAttrPtr prop;
4642
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004643 if ((node == NULL) || (name == NULL))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004644 return(NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004645 prop = node->properties;
4646 while (prop != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004647 if ((prop->ns == NULL) && xmlStrEqual(prop->name, BAD_CAST name))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004648 return(prop);
4649 prop = prop->next;
4650 }
4651 return (NULL);
4652}
4653
4654/**
4655 * xmlSchemaGetPropNodeNs:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004656 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004657 * @uri: the uri
4658 * @name: the name of the attribute
4659 *
4660 * Seeks an attribute with a local name of @name and
4661 * a namespace URI of @uri.
4662 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004663 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004664 */
4665static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004666xmlSchemaGetPropNodeNs(xmlNodePtr node, const char *uri, const char *name)
Daniel Veillardc0826a72004-08-10 14:17:33 +00004667{
4668 xmlAttrPtr prop;
4669
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004670 if ((node == NULL) || (name == NULL))
Daniel Veillarddee23482008-04-11 12:58:43 +00004671 return(NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004672 prop = node->properties;
4673 while (prop != NULL) {
4674 if ((prop->ns != NULL) &&
4675 xmlStrEqual(prop->name, BAD_CAST name) &&
4676 xmlStrEqual(prop->ns->href, BAD_CAST uri))
Daniel Veillard01fa6152004-06-29 17:04:39 +00004677 return(prop);
4678 prop = prop->next;
4679 }
4680 return (NULL);
4681}
4682
4683static const xmlChar *
4684xmlSchemaGetNodeContent(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
4685{
4686 xmlChar *val;
4687 const xmlChar *ret;
4688
4689 val = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004690 if (val == NULL)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00004691 val = xmlStrdup((xmlChar *)"");
Daniel Veillard01fa6152004-06-29 17:04:39 +00004692 ret = xmlDictLookup(ctxt->dict, val, -1);
4693 xmlFree(val);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004694 return(ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004695}
4696
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004697static const xmlChar *
4698xmlSchemaGetNodeContentNoDict(xmlNodePtr node)
4699{
4700 return((const xmlChar*) xmlNodeGetContent(node));
4701}
4702
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004703/**
4704 * xmlSchemaGetProp:
4705 * @ctxt: the parser context
4706 * @node: the node
4707 * @name: the property name
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004708 *
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004709 * Read a attribute value and internalize the string
4710 *
4711 * Returns the string or NULL if not present.
4712 */
4713static const xmlChar *
4714xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
4715 const char *name)
4716{
4717 xmlChar *val;
4718 const xmlChar *ret;
4719
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004720 val = xmlGetNoNsProp(node, BAD_CAST name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004721 if (val == NULL)
4722 return(NULL);
4723 ret = xmlDictLookup(ctxt->dict, val, -1);
4724 xmlFree(val);
4725 return(ret);
4726}
4727
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004728/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004729 * *
4730 * Parsing functions *
4731 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00004732 ************************************************************************/
4733
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004734#define WXS_FIND_GLOBAL_ITEM(slot) \
4735 if (xmlStrEqual(nsName, schema->targetNamespace)) { \
4736 ret = xmlHashLookup(schema->slot, name); \
4737 if (ret != NULL) goto exit; \
4738 } \
4739 if (xmlHashSize(schema->schemasImports) > 1) { \
4740 xmlSchemaImportPtr import; \
4741 if (nsName == NULL) \
4742 import = xmlHashLookup(schema->schemasImports, \
4743 XML_SCHEMAS_NO_NAMESPACE); \
4744 else \
4745 import = xmlHashLookup(schema->schemasImports, nsName); \
4746 if (import == NULL) \
4747 goto exit; \
4748 ret = xmlHashLookup(import->schema->slot, name); \
4749 }
4750
Daniel Veillard4255d502002-04-16 15:50:10 +00004751/**
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004752 * xmlSchemaGetElem:
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004753 * @schema: the schema context
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004754 * @name: the element name
4755 * @ns: the element namespace
4756 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004757 * Lookup a global element declaration in the schema.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004758 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004759 * Returns the element declaration or NULL if not found.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004760 */
4761static xmlSchemaElementPtr
4762xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004763 const xmlChar * nsName)
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004764{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004765 xmlSchemaElementPtr ret = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00004766
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004767 if ((name == NULL) || (schema == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004768 return(NULL);
4769 if (schema != NULL) {
4770 WXS_FIND_GLOBAL_ITEM(elemDecl)
Daniel Veillarddee23482008-04-11 12:58:43 +00004771 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004772exit:
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004773#ifdef DEBUG
4774 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004775 if (nsName == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004776 fprintf(stderr, "Unable to lookup element decl. %s", name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004777 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004778 fprintf(stderr, "Unable to lookup element decl. %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004779 nsName);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004780 }
4781#endif
4782 return (ret);
4783}
4784
4785/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004786 * xmlSchemaGetType:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004787 * @schema: the main schema
4788 * @name: the type's name
4789 * nsName: the type's namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00004790 *
4791 * Lookup a type in the schemas or the predefined types
4792 *
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004793 * Returns the group definition or NULL if not found.
Daniel Veillard4255d502002-04-16 15:50:10 +00004794 */
4795static xmlSchemaTypePtr
4796xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004797 const xmlChar * nsName)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004798{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004799 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00004800
4801 if (name == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +00004802 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004803 /* First try the built-in types. */
Daniel Veillarddee23482008-04-11 12:58:43 +00004804 if ((nsName != NULL) && xmlStrEqual(nsName, xmlSchemaNs)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004805 ret = xmlSchemaGetPredefinedType(name, nsName);
4806 if (ret != NULL)
4807 goto exit;
4808 /*
4809 * Note that we try the parsed schemas as well here
4810 * since one might have parsed the S4S, which contain more
4811 * than the built-in types.
4812 * TODO: Can we optimize this?
4813 */
4814 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004815 if (schema != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004816 WXS_FIND_GLOBAL_ITEM(typeDecl)
Daniel Veillarddee23482008-04-11 12:58:43 +00004817 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004818exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004819
Daniel Veillard4255d502002-04-16 15:50:10 +00004820#ifdef DEBUG
4821 if (ret == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004822 if (nsName == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004823 fprintf(stderr, "Unable to lookup type %s", name);
4824 else
4825 fprintf(stderr, "Unable to lookup type %s:%s", name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004826 nsName);
Daniel Veillard4255d502002-04-16 15:50:10 +00004827 }
4828#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004829 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004830}
4831
Daniel Veillard3646d642004-06-02 19:19:14 +00004832/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004833 * xmlSchemaGetAttributeDecl:
4834 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004835 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004836 * @ns: the target namespace of the attribute
Daniel Veillard3646d642004-06-02 19:19:14 +00004837 *
4838 * Lookup a an attribute in the schema or imported schemas
4839 *
4840 * Returns the attribute declaration or NULL if not found.
4841 */
4842static xmlSchemaAttributePtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004843xmlSchemaGetAttributeDecl(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 xmlSchemaAttributePtr 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(attrDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004852 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004853exit:
Daniel Veillard3646d642004-06-02 19:19:14 +00004854#ifdef DEBUG
4855 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004856 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004857 fprintf(stderr, "Unable to lookup attribute %s", name);
4858 else
4859 fprintf(stderr, "Unable to lookup attribute %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004860 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004861 }
4862#endif
4863 return (ret);
4864}
4865
4866/**
4867 * xmlSchemaGetAttributeGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004868 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004869 * @name: the name of the attribute group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004870 * @ns: the target namespace of the attribute group
Daniel Veillard3646d642004-06-02 19:19:14 +00004871 *
4872 * Lookup a an attribute group in the schema or imported schemas
4873 *
4874 * Returns the attribute group definition or NULL if not found.
4875 */
4876static xmlSchemaAttributeGroupPtr
4877xmlSchemaGetAttributeGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004878 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004879{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004880 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004881
4882 if ((name == NULL) || (schema == NULL))
4883 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004884 if (schema != NULL) {
4885 WXS_FIND_GLOBAL_ITEM(attrgrpDecl)
4886 }
4887exit:
4888 /* TODO:
4889 if ((ret != NULL) && (ret->redef != NULL)) {
4890 * Return the last redefinition. *
4891 ret = ret->redef;
Daniel Veillard3646d642004-06-02 19:19:14 +00004892 }
William M. Brack2f2a6632004-08-20 23:09:47 +00004893 */
Daniel Veillard3646d642004-06-02 19:19:14 +00004894#ifdef DEBUG
4895 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004896 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004897 fprintf(stderr, "Unable to lookup attribute group %s", name);
4898 else
4899 fprintf(stderr, "Unable to lookup attribute group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004900 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004901 }
4902#endif
4903 return (ret);
4904}
4905
4906/**
4907 * xmlSchemaGetGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004908 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004909 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004910 * @ns: the target namespace of the group
Daniel Veillard3646d642004-06-02 19:19:14 +00004911 *
4912 * Lookup a group in the schema or imported schemas
4913 *
4914 * Returns the group definition or NULL if not found.
4915 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004916static xmlSchemaModelGroupDefPtr
Daniel Veillard3646d642004-06-02 19:19:14 +00004917xmlSchemaGetGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004918 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004919{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004920 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004921
4922 if ((name == NULL) || (schema == NULL))
4923 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004924 if (schema != NULL) {
4925 WXS_FIND_GLOBAL_ITEM(groupDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004926 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004927exit:
Daniel Veillarddee23482008-04-11 12:58:43 +00004928
Daniel Veillard3646d642004-06-02 19:19:14 +00004929#ifdef DEBUG
4930 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004931 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004932 fprintf(stderr, "Unable to lookup group %s", name);
4933 else
4934 fprintf(stderr, "Unable to lookup group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004935 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004936 }
4937#endif
4938 return (ret);
4939}
4940
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004941static xmlSchemaNotationPtr
4942xmlSchemaGetNotation(xmlSchemaPtr schema,
4943 const xmlChar *name,
4944 const xmlChar *nsName)
4945{
4946 xmlSchemaNotationPtr ret = NULL;
4947
4948 if ((name == NULL) || (schema == NULL))
4949 return (NULL);
4950 if (schema != NULL) {
4951 WXS_FIND_GLOBAL_ITEM(notaDecl)
4952 }
4953exit:
4954 return (ret);
4955}
4956
4957static xmlSchemaIDCPtr
4958xmlSchemaGetIDC(xmlSchemaPtr schema,
4959 const xmlChar *name,
4960 const xmlChar *nsName)
4961{
4962 xmlSchemaIDCPtr ret = NULL;
4963
4964 if ((name == NULL) || (schema == NULL))
4965 return (NULL);
4966 if (schema != NULL) {
4967 WXS_FIND_GLOBAL_ITEM(idcDef)
4968 }
4969exit:
4970 return (ret);
4971}
4972
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004973/**
4974 * xmlSchemaGetNamedComponent:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004975 * @schema: the schema
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004976 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004977 * @ns: the target namespace of the group
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004978 *
4979 * Lookup a group in the schema or imported schemas
4980 *
4981 * Returns the group definition or NULL if not found.
4982 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004983static xmlSchemaBasicItemPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004984xmlSchemaGetNamedComponent(xmlSchemaPtr schema,
4985 xmlSchemaTypeType itemType,
4986 const xmlChar *name,
4987 const xmlChar *targetNs)
4988{
4989 switch (itemType) {
4990 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004991 return ((xmlSchemaBasicItemPtr) xmlSchemaGetGroup(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004992 name, targetNs));
4993 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004994 return ((xmlSchemaBasicItemPtr) xmlSchemaGetElem(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004995 name, targetNs));
4996 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004997 TODO
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004998 return (NULL);
4999 }
5000}
5001
Daniel Veillard4255d502002-04-16 15:50:10 +00005002/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005003 * *
5004 * Parsing functions *
5005 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00005006 ************************************************************************/
5007
5008#define IS_BLANK_NODE(n) \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005009 (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content, -1)))
Daniel Veillard4255d502002-04-16 15:50:10 +00005010
5011/**
5012 * xmlSchemaIsBlank:
5013 * @str: a string
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005014 * @len: the length of the string or -1
Daniel Veillard4255d502002-04-16 15:50:10 +00005015 *
5016 * Check if a string is ignorable
5017 *
5018 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
5019 */
5020static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005021xmlSchemaIsBlank(xmlChar * str, int len)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005022{
Daniel Veillard4255d502002-04-16 15:50:10 +00005023 if (str == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005024 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005025 if (len < 0) {
5026 while (*str != 0) {
5027 if (!(IS_BLANK_CH(*str)))
5028 return (0);
5029 str++;
5030 }
5031 } else while ((*str != 0) && (len != 0)) {
5032 if (!(IS_BLANK_CH(*str)))
5033 return (0);
5034 str++;
5035 len--;
Daniel Veillard4255d502002-04-16 15:50:10 +00005036 }
Daniel Veillarddee23482008-04-11 12:58:43 +00005037
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005038 return (1);
Daniel Veillard4255d502002-04-16 15:50:10 +00005039}
5040
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005041#define WXS_COMP_NAME(c, t) ((t) (c))->name
5042#define WXS_COMP_TNS(c, t) ((t) (c))->targetNamespace
5043/*
5044* xmlSchemaFindRedefCompInGraph:
5045* ATTENTION TODO: This uses pointer comp. for strings.
5046*/
5047static xmlSchemaBasicItemPtr
5048xmlSchemaFindRedefCompInGraph(xmlSchemaBucketPtr bucket,
5049 xmlSchemaTypeType type,
5050 const xmlChar *name,
5051 const xmlChar *nsName)
5052{
5053 xmlSchemaBasicItemPtr ret;
5054 int i;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005055
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005056 if ((bucket == NULL) || (name == NULL))
5057 return(NULL);
5058 if ((bucket->globals == NULL) ||
5059 (bucket->globals->nbItems == 0))
5060 goto subschemas;
5061 /*
5062 * Search in global components.
5063 */
5064 for (i = 0; i < bucket->globals->nbItems; i++) {
5065 ret = bucket->globals->items[i];
5066 if (ret->type == type) {
5067 switch (type) {
5068 case XML_SCHEMA_TYPE_COMPLEX:
5069 case XML_SCHEMA_TYPE_SIMPLE:
5070 if ((WXS_COMP_NAME(ret, xmlSchemaTypePtr) == name) &&
5071 (WXS_COMP_TNS(ret, xmlSchemaTypePtr) ==
5072 nsName))
5073 {
5074 return(ret);
5075 }
5076 break;
5077 case XML_SCHEMA_TYPE_GROUP:
5078 if ((WXS_COMP_NAME(ret,
5079 xmlSchemaModelGroupDefPtr) == name) &&
5080 (WXS_COMP_TNS(ret,
5081 xmlSchemaModelGroupDefPtr) == nsName))
5082 {
5083 return(ret);
5084 }
5085 break;
5086 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
5087 if ((WXS_COMP_NAME(ret,
5088 xmlSchemaAttributeGroupPtr) == name) &&
5089 (WXS_COMP_TNS(ret,
5090 xmlSchemaAttributeGroupPtr) == nsName))
5091 {
5092 return(ret);
5093 }
Kasimier T. Buchcik5d2998b2005-11-22 17:36:01 +00005094 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005095 default:
5096 /* Should not be hit. */
5097 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00005098 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005099 }
5100 }
5101subschemas:
5102 /*
5103 * Process imported/included schemas.
5104 */
5105 if (bucket->relations != NULL) {
5106 xmlSchemaSchemaRelationPtr rel = bucket->relations;
5107
5108 /*
5109 * TODO: Marking the bucket will not avoid multiple searches
5110 * in the same schema, but avoids at least circularity.
5111 */
5112 bucket->flags |= XML_SCHEMA_BUCKET_MARKED;
5113 do {
5114 if ((rel->bucket != NULL) &&
5115 ((rel->bucket->flags & XML_SCHEMA_BUCKET_MARKED) == 0)) {
5116 ret = xmlSchemaFindRedefCompInGraph(rel->bucket,
5117 type, name, nsName);
5118 if (ret != NULL)
5119 return(ret);
5120 }
5121 rel = rel->next;
5122 } while (rel != NULL);
5123 bucket->flags ^= XML_SCHEMA_BUCKET_MARKED;
5124 }
5125 return(NULL);
5126}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005127
5128/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005129 * xmlSchemaAddNotation:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005130 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005131 * @schema: the schema being built
5132 * @name: the item name
5133 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00005134 * Add an XML schema annotation declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00005135 * *WARNING* this interface is highly subject to change
5136 *
5137 * Returns the new struture or NULL in case of error
5138 */
5139static xmlSchemaNotationPtr
5140xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005141 const xmlChar *name, const xmlChar *nsName,
5142 xmlNodePtr node ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00005143{
5144 xmlSchemaNotationPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005145
5146 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5147 return (NULL);
5148
Daniel Veillard4255d502002-04-16 15:50:10 +00005149 ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation));
5150 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005151 xmlSchemaPErrMemory(ctxt, "add annotation", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005152 return (NULL);
5153 }
5154 memset(ret, 0, sizeof(xmlSchemaNotation));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005155 ret->type = XML_SCHEMA_TYPE_NOTATION;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005156 ret->name = name;
5157 ret->targetNamespace = nsName;
5158 /* TODO: do we need the node to be set?
5159 * ret->node = node;*/
5160 WXS_ADD_GLOBAL(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005161 return (ret);
5162}
5163
Daniel Veillard4255d502002-04-16 15:50:10 +00005164/**
5165 * xmlSchemaAddAttribute:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005166 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005167 * @schema: the schema being built
5168 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005169 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005170 *
5171 * Add an XML schema Attrribute declaration
5172 * *WARNING* this interface is highly subject to change
5173 *
5174 * Returns the new struture or NULL in case of error
5175 */
5176static xmlSchemaAttributePtr
5177xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005178 const xmlChar * name, const xmlChar * nsName,
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00005179 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005180{
5181 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005182
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005183 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005184 return (NULL);
5185
5186 ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute));
5187 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005188 xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005189 return (NULL);
5190 }
5191 memset(ret, 0, sizeof(xmlSchemaAttribute));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005192 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE;
5193 ret->node = node;
5194 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005195 ret->targetNamespace = nsName;
5196
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005197 if (topLevel)
5198 WXS_ADD_GLOBAL(ctxt, ret);
5199 else
5200 WXS_ADD_LOCAL(ctxt, ret);
5201 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005202 return (ret);
5203}
5204
5205/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005206 * xmlSchemaAddAttributeUse:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005207 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005208 * @schema: the schema being built
5209 * @name: the item name
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005210 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005211 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005212 * Add an XML schema Attrribute declaration
5213 * *WARNING* this interface is highly subject to change
5214 *
5215 * Returns the new struture or NULL in case of error
5216 */
5217static xmlSchemaAttributeUsePtr
5218xmlSchemaAddAttributeUse(xmlSchemaParserCtxtPtr pctxt,
5219 xmlNodePtr node)
5220{
5221 xmlSchemaAttributeUsePtr ret = NULL;
5222
5223 if (pctxt == NULL)
5224 return (NULL);
5225
5226 ret = (xmlSchemaAttributeUsePtr) xmlMalloc(sizeof(xmlSchemaAttributeUse));
5227 if (ret == NULL) {
5228 xmlSchemaPErrMemory(pctxt, "allocating attribute", NULL);
5229 return (NULL);
5230 }
5231 memset(ret, 0, sizeof(xmlSchemaAttributeUse));
5232 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE_USE;
5233 ret->node = node;
5234
5235 WXS_ADD_LOCAL(pctxt, ret);
5236 return (ret);
5237}
5238
5239/*
5240* xmlSchemaAddRedef:
5241*
5242* Adds a redefinition information. This is used at a later stage to:
5243* resolve references to the redefined components and to check constraints.
5244*/
5245static xmlSchemaRedefPtr
Daniel Veillarddee23482008-04-11 12:58:43 +00005246xmlSchemaAddRedef(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005247 xmlSchemaBucketPtr targetBucket,
5248 void *item,
5249 const xmlChar *refName,
5250 const xmlChar *refTargetNs)
5251{
5252 xmlSchemaRedefPtr ret;
5253
5254 ret = (xmlSchemaRedefPtr)
5255 xmlMalloc(sizeof(xmlSchemaRedef));
5256 if (ret == NULL) {
5257 xmlSchemaPErrMemory(pctxt,
5258 "allocating redefinition info", NULL);
5259 return (NULL);
5260 }
5261 memset(ret, 0, sizeof(xmlSchemaRedef));
5262 ret->item = item;
5263 ret->targetBucket = targetBucket;
5264 ret->refName = refName;
5265 ret->refTargetNs = refTargetNs;
5266 if (WXS_CONSTRUCTOR(pctxt)->redefs == NULL)
5267 WXS_CONSTRUCTOR(pctxt)->redefs = ret;
5268 else
5269 WXS_CONSTRUCTOR(pctxt)->lastRedef->next = ret;
5270 WXS_CONSTRUCTOR(pctxt)->lastRedef = ret;
5271
5272 return (ret);
5273}
5274
5275/**
5276 * xmlSchemaAddAttributeGroupDefinition:
5277 * @ctxt: a schema parser context
5278 * @schema: the schema being built
5279 * @name: the item name
5280 * @nsName: the target namespace
5281 * @node: the corresponding node
5282 *
5283 * Add an XML schema Attrribute Group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00005284 *
5285 * Returns the new struture or NULL in case of error
5286 */
5287static xmlSchemaAttributeGroupPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005288xmlSchemaAddAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
5289 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
5290 const xmlChar *name,
5291 const xmlChar *nsName,
5292 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00005293{
5294 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005295
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005296 if ((pctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005297 return (NULL);
5298
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005299 ret = (xmlSchemaAttributeGroupPtr)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005300 xmlMalloc(sizeof(xmlSchemaAttributeGroup));
Daniel Veillard4255d502002-04-16 15:50:10 +00005301 if (ret == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005302 xmlSchemaPErrMemory(pctxt, "allocating attribute group", NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005303 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005304 }
5305 memset(ret, 0, sizeof(xmlSchemaAttributeGroup));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005306 ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005307 ret->name = name;
5308 ret->targetNamespace = nsName;
Daniel Veillarddee23482008-04-11 12:58:43 +00005309 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005310
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005311 /* TODO: Remove the flag. */
5312 ret->flags |= XML_SCHEMAS_ATTRGROUP_GLOBAL;
5313 if (pctxt->isRedefine) {
5314 pctxt->redef = xmlSchemaAddRedef(pctxt, pctxt->redefined,
5315 ret, name, nsName);
5316 if (pctxt->redef == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005317 xmlFree(ret);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005318 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00005319 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005320 pctxt->redefCounter = 0;
5321 }
5322 WXS_ADD_GLOBAL(pctxt, ret);
5323 WXS_ADD_PENDING(pctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005324 return (ret);
5325}
5326
5327/**
5328 * xmlSchemaAddElement:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005329 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005330 * @schema: the schema being built
5331 * @name: the type name
5332 * @namespace: the type namespace
5333 *
5334 * Add an XML schema Element declaration
5335 * *WARNING* this interface is highly subject to change
5336 *
5337 * Returns the new struture or NULL in case of error
5338 */
5339static xmlSchemaElementPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005340xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005341 const xmlChar * name, const xmlChar * nsName,
William M. Brack2f2a6632004-08-20 23:09:47 +00005342 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005343{
5344 xmlSchemaElementPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005345
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005346 if ((ctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005347 return (NULL);
5348
Daniel Veillard4255d502002-04-16 15:50:10 +00005349 ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement));
5350 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005351 xmlSchemaPErrMemory(ctxt, "allocating element", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005352 return (NULL);
5353 }
5354 memset(ret, 0, sizeof(xmlSchemaElement));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005355 ret->type = XML_SCHEMA_TYPE_ELEMENT;
5356 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005357 ret->targetNamespace = nsName;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005358 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005359
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005360 if (topLevel)
5361 WXS_ADD_GLOBAL(ctxt, ret);
5362 else
5363 WXS_ADD_LOCAL(ctxt, ret);
5364 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005365 return (ret);
5366}
5367
5368/**
5369 * xmlSchemaAddType:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005370 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005371 * @schema: the schema being built
5372 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005373 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005374 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00005375 * Add an XML schema item
Daniel Veillard4255d502002-04-16 15:50:10 +00005376 * *WARNING* this interface is highly subject to change
5377 *
5378 * Returns the new struture or NULL in case of error
5379 */
5380static xmlSchemaTypePtr
5381xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005382 xmlSchemaTypeType type,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00005383 const xmlChar * name, const xmlChar * nsName,
5384 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005385{
5386 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005387
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005388 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005389 return (NULL);
5390
5391 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
5392 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005393 xmlSchemaPErrMemory(ctxt, "allocating type", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005394 return (NULL);
5395 }
5396 memset(ret, 0, sizeof(xmlSchemaType));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005397 ret->type = type;
5398 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005399 ret->targetNamespace = nsName;
5400 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005401 if (topLevel) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005402 if (ctxt->isRedefine) {
5403 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5404 ret, name, nsName);
5405 if (ctxt->redef == NULL) {
5406 xmlFree(ret);
5407 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00005408 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005409 ctxt->redefCounter = 0;
5410 }
5411 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005412 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005413 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillarddee23482008-04-11 12:58:43 +00005414 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005415 return (ret);
5416}
5417
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005418static xmlSchemaQNameRefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005419xmlSchemaNewQNameRef(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005420 xmlSchemaTypeType refType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005421 const xmlChar *refName,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005422 const xmlChar *refNs)
5423{
5424 xmlSchemaQNameRefPtr ret;
5425
5426 ret = (xmlSchemaQNameRefPtr)
5427 xmlMalloc(sizeof(xmlSchemaQNameRef));
5428 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005429 xmlSchemaPErrMemory(pctxt,
5430 "allocating QName reference item", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005431 return (NULL);
5432 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005433 ret->node = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005434 ret->type = XML_SCHEMA_EXTRA_QNAMEREF;
5435 ret->name = refName;
5436 ret->targetNamespace = refNs;
5437 ret->item = NULL;
5438 ret->itemType = refType;
5439 /*
5440 * Store the reference item in the schema.
5441 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005442 WXS_ADD_LOCAL(pctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005443 return (ret);
5444}
5445
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005446static xmlSchemaAttributeUseProhibPtr
5447xmlSchemaAddAttributeUseProhib(xmlSchemaParserCtxtPtr pctxt)
5448{
5449 xmlSchemaAttributeUseProhibPtr ret;
5450
5451 ret = (xmlSchemaAttributeUseProhibPtr)
5452 xmlMalloc(sizeof(xmlSchemaAttributeUseProhib));
5453 if (ret == NULL) {
5454 xmlSchemaPErrMemory(pctxt,
5455 "allocating attribute use prohibition", NULL);
5456 return (NULL);
5457 }
5458 memset(ret, 0, sizeof(xmlSchemaAttributeUseProhib));
5459 ret->type = XML_SCHEMA_EXTRA_ATTR_USE_PROHIB;
5460 WXS_ADD_LOCAL(pctxt, ret);
5461 return (ret);
5462}
5463
5464
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005465/**
5466 * xmlSchemaAddModelGroup:
5467 * @ctxt: a schema parser context
5468 * @schema: the schema being built
5469 * @type: the "compositor" type of the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005470 * @node: the node in the schema doc
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005471 *
5472 * Adds a schema model group
5473 * *WARNING* this interface is highly subject to change
5474 *
5475 * Returns the new struture or NULL in case of error
5476 */
5477static xmlSchemaModelGroupPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005478xmlSchemaAddModelGroup(xmlSchemaParserCtxtPtr ctxt,
5479 xmlSchemaPtr schema,
5480 xmlSchemaTypeType type,
5481 xmlNodePtr node)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005482{
5483 xmlSchemaModelGroupPtr ret = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005484
5485 if ((ctxt == NULL) || (schema == NULL))
5486 return (NULL);
5487
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005488 ret = (xmlSchemaModelGroupPtr)
5489 xmlMalloc(sizeof(xmlSchemaModelGroup));
5490 if (ret == NULL) {
5491 xmlSchemaPErrMemory(ctxt, "allocating model group component",
5492 NULL);
5493 return (NULL);
5494 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005495 memset(ret, 0, sizeof(xmlSchemaModelGroup));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005496 ret->type = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005497 ret->node = node;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005498 WXS_ADD_LOCAL(ctxt, ret);
5499 if ((type == XML_SCHEMA_TYPE_SEQUENCE) ||
5500 (type == XML_SCHEMA_TYPE_CHOICE))
5501 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005502 return (ret);
5503}
5504
5505
5506/**
5507 * xmlSchemaAddParticle:
5508 * @ctxt: a schema parser context
5509 * @schema: the schema being built
5510 * @node: the corresponding node in the schema doc
5511 * @min: the minOccurs
5512 * @max: the maxOccurs
5513 *
5514 * Adds an XML schema particle component.
5515 * *WARNING* this interface is highly subject to change
5516 *
5517 * Returns the new struture or NULL in case of error
5518 */
5519static xmlSchemaParticlePtr
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00005520xmlSchemaAddParticle(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005521 xmlNodePtr node, int min, int max)
5522{
5523 xmlSchemaParticlePtr ret = NULL;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00005524 if (ctxt == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005525 return (NULL);
5526
5527#ifdef DEBUG
5528 fprintf(stderr, "Adding particle component\n");
5529#endif
5530 ret = (xmlSchemaParticlePtr)
5531 xmlMalloc(sizeof(xmlSchemaParticle));
5532 if (ret == NULL) {
5533 xmlSchemaPErrMemory(ctxt, "allocating particle component",
5534 NULL);
5535 return (NULL);
5536 }
5537 ret->type = XML_SCHEMA_TYPE_PARTICLE;
5538 ret->annot = NULL;
5539 ret->node = node;
5540 ret->minOccurs = min;
5541 ret->maxOccurs = max;
5542 ret->next = NULL;
5543 ret->children = NULL;
5544
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005545 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillarddee23482008-04-11 12:58:43 +00005546 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005547 * Note that addition to pending components will be done locally
5548 * to the specific parsing function, since the most particles
5549 * need not to be fixed up (i.e. the reference to be resolved).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005550 * REMOVED: WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005551 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005552 return (ret);
5553}
5554
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005555/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005556 * xmlSchemaAddModelGroupDefinition:
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005557 * @ctxt: a schema validation context
5558 * @schema: the schema being built
5559 * @name: the group name
5560 *
5561 * Add an XML schema Group definition
5562 *
5563 * Returns the new struture or NULL in case of error
5564 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005565static xmlSchemaModelGroupDefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005566xmlSchemaAddModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
5567 xmlSchemaPtr schema,
5568 const xmlChar *name,
5569 const xmlChar *nsName,
5570 xmlNodePtr node)
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005571{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005572 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005573
5574 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5575 return (NULL);
5576
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005577 ret = (xmlSchemaModelGroupDefPtr)
5578 xmlMalloc(sizeof(xmlSchemaModelGroupDef));
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005579 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005580 xmlSchemaPErrMemory(ctxt, "adding group", NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005581 return (NULL);
5582 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005583 memset(ret, 0, sizeof(xmlSchemaModelGroupDef));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005584 ret->name = name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005585 ret->type = XML_SCHEMA_TYPE_GROUP;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005586 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005587 ret->targetNamespace = nsName;
5588
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005589 if (ctxt->isRedefine) {
5590 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5591 ret, name, nsName);
5592 if (ctxt->redef == NULL) {
5593 xmlFree(ret);
5594 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00005595 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005596 ctxt->redefCounter = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005597 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005598 WXS_ADD_GLOBAL(ctxt, ret);
5599 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005600 return (ret);
5601}
5602
Daniel Veillard3646d642004-06-02 19:19:14 +00005603/**
5604 * xmlSchemaNewWildcardNs:
5605 * @ctxt: a schema validation context
5606 *
5607 * Creates a new wildcard namespace constraint.
5608 *
5609 * Returns the new struture or NULL in case of error
5610 */
5611static xmlSchemaWildcardNsPtr
5612xmlSchemaNewWildcardNsConstraint(xmlSchemaParserCtxtPtr ctxt)
5613{
5614 xmlSchemaWildcardNsPtr ret;
5615
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005616 ret = (xmlSchemaWildcardNsPtr)
Daniel Veillard3646d642004-06-02 19:19:14 +00005617 xmlMalloc(sizeof(xmlSchemaWildcardNs));
5618 if (ret == NULL) {
5619 xmlSchemaPErrMemory(ctxt, "creating wildcard namespace constraint", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005620 return (NULL);
Daniel Veillard3646d642004-06-02 19:19:14 +00005621 }
5622 ret->value = NULL;
5623 ret->next = NULL;
5624 return (ret);
5625}
5626
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005627static xmlSchemaIDCPtr
5628xmlSchemaAddIDC(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5629 const xmlChar *name, const xmlChar *nsName,
5630 int category, xmlNodePtr node)
5631{
Daniel Veillarddee23482008-04-11 12:58:43 +00005632 xmlSchemaIDCPtr ret = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005633
5634 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5635 return (NULL);
5636
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005637 ret = (xmlSchemaIDCPtr) xmlMalloc(sizeof(xmlSchemaIDC));
5638 if (ret == NULL) {
5639 xmlSchemaPErrMemory(ctxt,
5640 "allocating an identity-constraint definition", NULL);
5641 return (NULL);
5642 }
5643 memset(ret, 0, sizeof(xmlSchemaIDC));
5644 /* The target namespace of the parent element declaration. */
5645 ret->targetNamespace = nsName;
5646 ret->name = name;
5647 ret->type = category;
Daniel Veillarddee23482008-04-11 12:58:43 +00005648 ret->node = node;
5649
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005650 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005651 /*
5652 * Only keyrefs need to be fixup up.
5653 */
5654 if (category == XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005655 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005656 return (ret);
5657}
5658
Daniel Veillard3646d642004-06-02 19:19:14 +00005659/**
5660 * xmlSchemaAddWildcard:
5661 * @ctxt: a schema validation context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005662 * @schema: a schema
5663 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005664 * Adds a wildcard.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005665 * It corresponds to a xsd:anyAttribute and xsd:any.
Daniel Veillard3646d642004-06-02 19:19:14 +00005666 *
5667 * Returns the new struture or NULL in case of error
5668 */
5669static xmlSchemaWildcardPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005670xmlSchemaAddWildcard(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5671 xmlSchemaTypeType type, xmlNodePtr node)
Daniel Veillard3646d642004-06-02 19:19:14 +00005672{
5673 xmlSchemaWildcardPtr ret = NULL;
5674
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005675 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard3646d642004-06-02 19:19:14 +00005676 return (NULL);
5677
5678 ret = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
5679 if (ret == NULL) {
5680 xmlSchemaPErrMemory(ctxt, "adding wildcard", NULL);
5681 return (NULL);
5682 }
5683 memset(ret, 0, sizeof(xmlSchemaWildcard));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005684 ret->type = type;
Daniel Veillarddee23482008-04-11 12:58:43 +00005685 ret->node = node;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005686 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillard3646d642004-06-02 19:19:14 +00005687 return (ret);
5688}
5689
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005690static void
5691xmlSchemaSubstGroupFree(xmlSchemaSubstGroupPtr group)
5692{
5693 if (group == NULL)
5694 return;
5695 if (group->members != NULL)
5696 xmlSchemaItemListFree(group->members);
5697 xmlFree(group);
5698}
5699
5700static xmlSchemaSubstGroupPtr
5701xmlSchemaSubstGroupAdd(xmlSchemaParserCtxtPtr pctxt,
5702 xmlSchemaElementPtr head)
5703{
5704 xmlSchemaSubstGroupPtr ret;
5705
5706 /* Init subst group hash. */
5707 if (WXS_SUBST_GROUPS(pctxt) == NULL) {
5708 WXS_SUBST_GROUPS(pctxt) = xmlHashCreateDict(10, pctxt->dict);
5709 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5710 return(NULL);
5711 }
5712 /* Create a new substitution group. */
5713 ret = (xmlSchemaSubstGroupPtr) xmlMalloc(sizeof(xmlSchemaSubstGroup));
5714 if (ret == NULL) {
5715 xmlSchemaPErrMemory(NULL,
5716 "allocating a substitution group container", NULL);
5717 return(NULL);
5718 }
5719 memset(ret, 0, sizeof(xmlSchemaSubstGroup));
5720 ret->head = head;
5721 /* Create list of members. */
5722 ret->members = xmlSchemaItemListCreate();
5723 if (ret->members == NULL) {
5724 xmlSchemaSubstGroupFree(ret);
5725 return(NULL);
5726 }
5727 /* Add subst group to hash. */
5728 if (xmlHashAddEntry2(WXS_SUBST_GROUPS(pctxt),
5729 head->name, head->targetNamespace, ret) != 0) {
5730 PERROR_INT("xmlSchemaSubstGroupAdd",
5731 "failed to add a new substitution container");
5732 xmlSchemaSubstGroupFree(ret);
5733 return(NULL);
5734 }
5735 return(ret);
5736}
5737
5738static xmlSchemaSubstGroupPtr
5739xmlSchemaSubstGroupGet(xmlSchemaParserCtxtPtr pctxt,
5740 xmlSchemaElementPtr head)
Daniel Veillarddee23482008-04-11 12:58:43 +00005741{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005742 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5743 return(NULL);
5744 return(xmlHashLookup2(WXS_SUBST_GROUPS(pctxt),
5745 head->name, head->targetNamespace));
5746
5747}
5748
5749/**
5750 * xmlSchemaAddElementSubstitutionMember:
5751 * @pctxt: a schema parser context
5752 * @head: the head of the substitution group
5753 * @member: the new member of the substitution group
5754 *
5755 * Allocate a new annotation structure.
5756 *
5757 * Returns the newly allocated structure or NULL in case or error
5758 */
5759static int
5760xmlSchemaAddElementSubstitutionMember(xmlSchemaParserCtxtPtr pctxt,
5761 xmlSchemaElementPtr head,
5762 xmlSchemaElementPtr member)
5763{
5764 xmlSchemaSubstGroupPtr substGroup = NULL;
5765
5766 if ((pctxt == NULL) || (head == NULL) || (member == NULL))
5767 return (-1);
5768
5769 substGroup = xmlSchemaSubstGroupGet(pctxt, head);
5770 if (substGroup == NULL)
5771 substGroup = xmlSchemaSubstGroupAdd(pctxt, head);
5772 if (substGroup == NULL)
5773 return(-1);
5774 if (xmlSchemaItemListAdd(substGroup->members, member) == -1)
5775 return(-1);
5776 return(0);
5777}
5778
Daniel Veillard4255d502002-04-16 15:50:10 +00005779/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005780 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00005781 * Utilities for parsing *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005782 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00005783 ************************************************************************/
5784
Daniel Veillard4255d502002-04-16 15:50:10 +00005785/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00005786 * xmlSchemaPValAttrNodeQNameValue:
5787 * @ctxt: a schema parser context
5788 * @schema: the schema context
5789 * @ownerDes: the designation of the parent element
5790 * @ownerItem: the parent as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005791 * @value: the QName value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005792 * @local: the resulting local part if found, the attribute value otherwise
5793 * @uri: the resulting namespace URI if found
5794 *
5795 * Extracts the local name and the URI of a QName value and validates it.
5796 * This one is intended to be used on attribute values that
5797 * should resolve to schema components.
5798 *
5799 * Returns 0, in case the QName is valid, a positive error code
5800 * if not valid and -1 if an internal error occurs.
5801 */
5802static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005803xmlSchemaPValAttrNodeQNameValue(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005804 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005805 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005806 xmlAttrPtr attr,
5807 const xmlChar *value,
5808 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005809 const xmlChar **local)
5810{
5811 const xmlChar *pref;
5812 xmlNsPtr ns;
5813 int len, ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005814
Daniel Veillardc0826a72004-08-10 14:17:33 +00005815 *uri = NULL;
5816 *local = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005817 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005818 if (ret > 0) {
5819 xmlSchemaPSimpleTypeErr(ctxt,
5820 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5821 ownerItem, (xmlNodePtr) attr,
5822 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
5823 NULL, value, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005824 *local = value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005825 return (ctxt->err);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005826 } else if (ret < 0)
5827 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005828
5829 if (!strchr((char *) value, ':')) {
Daniel Veillard24505b02005-07-28 23:49:35 +00005830 ns = xmlSearchNs(attr->doc, attr->parent, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005831 if (ns)
5832 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
5833 else if (schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005834 /* TODO: move XML_SCHEMAS_INCLUDING_CONVERT_NS to the
5835 * parser context. */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005836 /*
5837 * This one takes care of included schemas with no
5838 * target namespace.
5839 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005840 *uri = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005841 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005842 *local = xmlDictLookup(ctxt->dict, value, -1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005843 return (0);
5844 }
5845 /*
5846 * At this point xmlSplitQName3 has to return a local name.
5847 */
5848 *local = xmlSplitQName3(value, &len);
5849 *local = xmlDictLookup(ctxt->dict, *local, -1);
5850 pref = xmlDictLookup(ctxt->dict, value, len);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005851 ns = xmlSearchNs(attr->doc, attr->parent, pref);
5852 if (ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005853 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005854 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005855 ownerItem, (xmlNodePtr) attr,
5856 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), NULL, value,
5857 "The value '%s' of simple type 'xs:QName' has no "
5858 "corresponding namespace declaration in scope", value, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005859 return (ctxt->err);
5860 } else {
5861 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005862 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00005863 return (0);
5864}
5865
5866/**
5867 * xmlSchemaPValAttrNodeQName:
5868 * @ctxt: a schema parser context
5869 * @schema: the schema context
5870 * @ownerDes: the designation of the owner element
5871 * @ownerItem: the owner as a schema object
5872 * @attr: the attribute node
5873 * @local: the resulting local part if found, the attribute value otherwise
5874 * @uri: the resulting namespace URI if found
5875 *
5876 * Extracts and validates the QName of an attribute value.
5877 * This one is intended to be used on attribute values that
5878 * should resolve to schema components.
5879 *
5880 * Returns 0, in case the QName is valid, a positive error code
5881 * if not valid and -1 if an internal error occurs.
5882 */
5883static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005884xmlSchemaPValAttrNodeQName(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005885 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005886 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005887 xmlAttrPtr attr,
5888 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005889 const xmlChar **local)
5890{
5891 const xmlChar *value;
5892
5893 value = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005894 return (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005895 ownerItem, attr, value, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005896}
5897
5898/**
5899 * xmlSchemaPValAttrQName:
5900 * @ctxt: a schema parser context
5901 * @schema: the schema context
5902 * @ownerDes: the designation of the parent element
5903 * @ownerItem: the owner as a schema object
5904 * @ownerElem: the parent node of the attribute
5905 * @name: the name of the attribute
5906 * @local: the resulting local part if found, the attribute value otherwise
5907 * @uri: the resulting namespace URI if found
5908 *
5909 * Extracts and validates the QName of an attribute value.
5910 *
5911 * Returns 0, in case the QName is valid, a positive error code
5912 * if not valid and -1 if an internal error occurs.
5913 */
5914static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005915xmlSchemaPValAttrQName(xmlSchemaParserCtxtPtr ctxt,
5916 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005917 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005918 xmlNodePtr ownerElem,
5919 const char *name,
5920 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005921 const xmlChar **local)
5922{
5923 xmlAttrPtr attr;
5924
5925 attr = xmlSchemaGetPropNode(ownerElem, name);
5926 if (attr == NULL) {
5927 *local = NULL;
5928 *uri = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005929 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005930 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005931 return (xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005932 ownerItem, attr, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005933}
5934
5935/**
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005936 * xmlSchemaPValAttrID:
5937 * @ctxt: a schema parser context
5938 * @schema: the schema context
5939 * @ownerDes: the designation of the parent element
5940 * @ownerItem: the owner as a schema object
5941 * @ownerElem: the parent node of the attribute
5942 * @name: the name of the attribute
5943 *
5944 * Extracts and validates the ID of an attribute value.
5945 *
5946 * Returns 0, in case the ID is valid, a positive error code
5947 * if not valid and -1 if an internal error occurs.
5948 */
5949static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005950xmlSchemaPValAttrNodeID(xmlSchemaParserCtxtPtr ctxt, xmlAttrPtr attr)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005951{
5952 int ret;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005953 const xmlChar *value;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005954
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005955 if (attr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005956 return(0);
5957 value = xmlSchemaGetNodeContentNoDict((xmlNodePtr) attr);
5958 ret = xmlValidateNCName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005959 if (ret == 0) {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005960 /*
5961 * NOTE: the IDness might have already be declared in the DTD
5962 */
5963 if (attr->atype != XML_ATTRIBUTE_ID) {
5964 xmlIDPtr res;
5965 xmlChar *strip;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005966
5967 /*
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005968 * TODO: Use xmlSchemaStrip here; it's not exported at this
5969 * moment.
5970 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005971 strip = xmlSchemaCollapseString(value);
5972 if (strip != NULL) {
5973 xmlFree((xmlChar *) value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005974 value = strip;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005975 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005976 res = xmlAddID(NULL, attr->doc, value, attr);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005977 if (res == NULL) {
5978 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005979 xmlSchemaPSimpleTypeErr(ctxt,
5980 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005981 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005982 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5983 NULL, NULL, "Duplicate value '%s' of simple "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005984 "type 'xs:ID'", value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005985 } else
5986 attr->atype = XML_ATTRIBUTE_ID;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005987 }
5988 } else if (ret > 0) {
5989 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005990 xmlSchemaPSimpleTypeErr(ctxt,
5991 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005992 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005993 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5994 NULL, NULL, "The value '%s' of simple type 'xs:ID' is "
5995 "not a valid 'xs:NCName'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005996 value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005997 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005998 if (value != NULL)
5999 xmlFree((xmlChar *)value);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00006000
6001 return (ret);
6002}
6003
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006004static int
6005xmlSchemaPValAttrID(xmlSchemaParserCtxtPtr ctxt,
6006 xmlNodePtr ownerElem,
6007 const xmlChar *name)
6008{
6009 xmlAttrPtr attr;
6010
6011 attr = xmlSchemaGetPropNode(ownerElem, (const char *) name);
6012 if (attr == NULL)
6013 return(0);
6014 return(xmlSchemaPValAttrNodeID(ctxt, attr));
6015
6016}
6017
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00006018/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006019 * xmlGetMaxOccurs:
6020 * @ctxt: a schema validation context
6021 * @node: a subtree containing XML Schema informations
6022 *
6023 * Get the maxOccurs property
6024 *
6025 * Returns the default if not found, or the value
6026 */
6027static int
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006028xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
6029 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006030{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006031 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00006032 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006033 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006034
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006035 attr = xmlSchemaGetPropNode(node, "maxOccurs");
6036 if (attr == NULL)
6037 return (def);
6038 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00006039
6040 if (xmlStrEqual(val, (const xmlChar *) "unbounded")) {
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006041 if (max != UNBOUNDED) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006042 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006043 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6044 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006045 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006046 val, NULL, NULL, NULL);
6047 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006048 } else
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006049 return (UNBOUNDED); /* encoding it with -1 might be another option */
Daniel Veillard4255d502002-04-16 15:50:10 +00006050 }
6051
6052 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006053 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006054 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006055 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006056 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006057 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6058 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006059 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006060 val, NULL, NULL, NULL);
6061 return (def);
6062 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006063 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006064 ret = ret * 10 + (*cur - '0');
6065 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006066 }
William M. Brack76e95df2003-10-18 16:20:14 +00006067 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006068 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006069 /*
6070 * TODO: Restrict the maximal value to Integer.
6071 */
6072 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006073 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006074 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6075 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006076 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006077 val, NULL, NULL, NULL);
6078 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006079 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006080 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006081}
6082
6083/**
6084 * xmlGetMinOccurs:
6085 * @ctxt: a schema validation context
6086 * @node: a subtree containing XML Schema informations
6087 *
6088 * Get the minOccurs property
6089 *
6090 * Returns the default if not found, or the value
6091 */
6092static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006093xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006094 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006095{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006096 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00006097 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006098 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006099
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006100 attr = xmlSchemaGetPropNode(node, "minOccurs");
6101 if (attr == NULL)
6102 return (def);
6103 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00006104 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006105 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006106 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006107 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006108 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006109 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6110 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006111 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006112 val, NULL, NULL, NULL);
6113 return (def);
6114 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006115 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006116 ret = ret * 10 + (*cur - '0');
6117 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006118 }
William M. Brack76e95df2003-10-18 16:20:14 +00006119 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006120 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006121 /*
6122 * TODO: Restrict the maximal value to Integer.
6123 */
6124 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006125 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006126 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6127 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006128 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006129 val, NULL, NULL, NULL);
6130 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006131 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006132 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006133}
6134
6135/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006136 * xmlSchemaPGetBoolNodeValue:
6137 * @ctxt: a schema validation context
6138 * @ownerDes: owner designation
6139 * @ownerItem: the owner as a schema item
6140 * @node: the node holding the value
6141 *
6142 * Converts a boolean string value into 1 or 0.
6143 *
6144 * Returns 0 or 1.
6145 */
6146static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006147xmlSchemaPGetBoolNodeValue(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006148 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006149 xmlNodePtr node)
6150{
6151 xmlChar *value = NULL;
6152 int res = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006153
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006154 value = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006155 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006156 * 3.2.2.1 Lexical representation
Jan Pokorný761c9e92013-11-29 23:26:27 +01006157 * An instance of a datatype that is defined as `boolean`
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006158 * can have the following legal literals {true, false, 1, 0}.
6159 */
6160 if (xmlStrEqual(BAD_CAST value, BAD_CAST "true"))
6161 res = 1;
6162 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "false"))
6163 res = 0;
6164 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "1"))
6165 res = 1;
6166 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006167 res = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006168 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006169 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006170 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006171 ownerItem, node,
6172 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6173 NULL, BAD_CAST value,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006174 NULL, NULL, NULL);
6175 }
6176 if (value != NULL)
6177 xmlFree(value);
6178 return (res);
6179}
6180
6181/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006182 * xmlGetBooleanProp:
6183 * @ctxt: a schema validation context
6184 * @node: a subtree containing XML Schema informations
6185 * @name: the attribute name
6186 * @def: the default value
6187 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006188 * Evaluate if a boolean property is set
Daniel Veillard4255d502002-04-16 15:50:10 +00006189 *
6190 * Returns the default if not found, 0 if found to be false,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006191 * 1 if found to be true
Daniel Veillard4255d502002-04-16 15:50:10 +00006192 */
6193static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006194xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006195 xmlNodePtr node,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006196 const char *name, int def)
6197{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006198 const xmlChar *val;
Daniel Veillard4255d502002-04-16 15:50:10 +00006199
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006200 val = xmlSchemaGetProp(ctxt, node, name);
Daniel Veillard4255d502002-04-16 15:50:10 +00006201 if (val == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006202 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006203 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006204 * 3.2.2.1 Lexical representation
Jan Pokorný761c9e92013-11-29 23:26:27 +01006205 * An instance of a datatype that is defined as `boolean`
Daniel Veillardc0826a72004-08-10 14:17:33 +00006206 * can have the following legal literals {true, false, 1, 0}.
6207 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006208 if (xmlStrEqual(val, BAD_CAST "true"))
6209 def = 1;
6210 else if (xmlStrEqual(val, BAD_CAST "false"))
6211 def = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006212 else if (xmlStrEqual(val, BAD_CAST "1"))
6213 def = 1;
6214 else if (xmlStrEqual(val, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006215 def = 0;
6216 else {
6217 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006218 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006219 NULL,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00006220 (xmlNodePtr) xmlSchemaGetPropNode(node, name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006221 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6222 NULL, val, NULL, NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006223 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006224 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006225}
6226
6227/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006228 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00006229 * Shema extraction from an Infoset *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006230 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00006231 ************************************************************************/
6232static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr
6233 ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00006234 xmlNodePtr node,
6235 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006236static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr
6237 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006238 xmlSchemaPtr schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006239 xmlNodePtr node,
Daniel Veillard3646d642004-06-02 19:19:14 +00006240 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006241static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr
6242 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006243 xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006244 xmlNodePtr node,
6245 xmlSchemaTypeType parentType);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006246static xmlSchemaBasicItemPtr
6247xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
6248 xmlSchemaPtr schema,
6249 xmlNodePtr node,
6250 xmlSchemaItemListPtr uses,
6251 int parentType);
Daniel Veillard4255d502002-04-16 15:50:10 +00006252static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt,
6253 xmlSchemaPtr schema,
6254 xmlNodePtr node);
Daniel Veillard3646d642004-06-02 19:19:14 +00006255static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006256xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6257 xmlSchemaPtr schema, xmlNodePtr node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006258
6259/**
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006260 * xmlSchemaPValAttrNodeValue:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006261 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00006262 * @ctxt: a schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00006263 * @ownerDes: the designation of the parent element
6264 * @ownerItem: the schema object owner if existent
6265 * @attr: the schema attribute node being validated
6266 * @value: the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006267 * @type: the built-in type to be validated against
Daniel Veillard01fa6152004-06-29 17:04:39 +00006268 *
6269 * Validates a value against the given built-in type.
6270 * This one is intended to be used internally for validation
6271 * of schema attribute values during parsing of the schema.
6272 *
6273 * Returns 0 if the value is valid, a positive error code
6274 * number otherwise and -1 in case of an internal or API error.
6275 */
6276static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006277xmlSchemaPValAttrNodeValue(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006278 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006279 xmlAttrPtr attr,
6280 const xmlChar *value,
6281 xmlSchemaTypePtr type)
Daniel Veillard01fa6152004-06-29 17:04:39 +00006282{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006283
6284 int ret = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006285
6286 /*
6287 * NOTE: Should we move this to xmlschematypes.c? Hmm, but this
6288 * one is really meant to be used internally, so better not.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006289 */
6290 if ((pctxt == NULL) || (type == NULL) || (attr == NULL))
Daniel Veillard01fa6152004-06-29 17:04:39 +00006291 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006292 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6293 PERROR_INT("xmlSchemaPValAttrNodeValue",
6294 "the given type is not a built-in type");
6295 return (-1);
6296 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006297 switch (type->builtInType) {
6298 case XML_SCHEMAS_NCNAME:
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006299 case XML_SCHEMAS_QNAME:
6300 case XML_SCHEMAS_ANYURI:
6301 case XML_SCHEMAS_TOKEN:
6302 case XML_SCHEMAS_LANGUAGE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006303 ret = xmlSchemaValPredefTypeNode(type, value, NULL,
6304 (xmlNodePtr) attr);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006305 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006306 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006307 PERROR_INT("xmlSchemaPValAttrNodeValue",
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00006308 "validation using the given type is not supported while "
6309 "parsing a schema");
Daniel Veillard01fa6152004-06-29 17:04:39 +00006310 return (-1);
6311 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006312 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006313 /*
6314 * TODO: Should we use the S4S error codes instead?
6315 */
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006316 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006317 PERROR_INT("xmlSchemaPValAttrNodeValue",
6318 "failed to validate a schema attribute value");
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006319 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006320 } else if (ret > 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006321 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006322 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
6323 else
6324 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Daniel Veillarddee23482008-04-11 12:58:43 +00006325 xmlSchemaPSimpleTypeErr(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006326 ret, ownerItem, (xmlNodePtr) attr,
6327 type, NULL, value, NULL, NULL, NULL);
6328 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006329 return (ret);
6330}
6331
6332/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006333 * xmlSchemaPValAttrNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006334 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006335 * @ctxt: a schema parser context
6336 * @ownerDes: the designation of the parent element
6337 * @ownerItem: the schema object owner if existent
6338 * @attr: the schema attribute node being validated
6339 * @type: the built-in type to be validated against
6340 * @value: the resulting value if any
6341 *
6342 * Extracts and validates a value against the given built-in type.
6343 * This one is intended to be used internally for validation
6344 * of schema attribute values during parsing of the schema.
6345 *
6346 * Returns 0 if the value is valid, a positive error code
6347 * number otherwise and -1 in case of an internal or API error.
6348 */
6349static int
6350xmlSchemaPValAttrNode(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006351 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006352 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006353 xmlSchemaTypePtr type,
6354 const xmlChar **value)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006355{
Daniel Veillardc0826a72004-08-10 14:17:33 +00006356 const xmlChar *val;
6357
6358 if ((ctxt == NULL) || (type == NULL) || (attr == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006359 return (-1);
6360
Daniel Veillardc0826a72004-08-10 14:17:33 +00006361 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
6362 if (value != NULL)
6363 *value = val;
6364
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006365 return (xmlSchemaPValAttrNodeValue(ctxt, ownerItem, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006366 val, type));
Daniel Veillardc0826a72004-08-10 14:17:33 +00006367}
6368
6369/**
6370 * xmlSchemaPValAttr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006371 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006372 * @ctxt: a schema parser context
6373 * @node: the element node of the attribute
6374 * @ownerDes: the designation of the parent element
6375 * @ownerItem: the schema object owner if existent
6376 * @ownerElem: the owner element node
6377 * @name: the name of the schema attribute node
6378 * @type: the built-in type to be validated against
6379 * @value: the resulting value if any
6380 *
6381 * Extracts and validates a value against the given built-in type.
6382 * This one is intended to be used internally for validation
6383 * of schema attribute values during parsing of the schema.
6384 *
6385 * Returns 0 if the value is valid, a positive error code
6386 * number otherwise and -1 in case of an internal or API error.
6387 */
6388static int
Daniel Veillarddee23482008-04-11 12:58:43 +00006389xmlSchemaPValAttr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006390 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006391 xmlNodePtr ownerElem,
6392 const char *name,
6393 xmlSchemaTypePtr type,
6394 const xmlChar **value)
6395{
6396 xmlAttrPtr attr;
6397
6398 if ((ctxt == NULL) || (type == NULL)) {
6399 if (value != NULL)
6400 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006401 return (-1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006402 }
6403 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6404 if (value != NULL)
6405 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006406 xmlSchemaPErr(ctxt, ownerElem,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00006407 XML_SCHEMAP_INTERNAL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006408 "Internal error: xmlSchemaPValAttr, the given "
6409 "type '%s' is not a built-in type.\n",
6410 type->name, NULL);
6411 return (-1);
6412 }
6413 attr = xmlSchemaGetPropNode(ownerElem, name);
6414 if (attr == NULL) {
6415 if (value != NULL)
6416 *value = NULL;
6417 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006418 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006419 return (xmlSchemaPValAttrNode(ctxt, ownerItem, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006420 type, value));
6421}
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006422
6423static int
6424xmlSchemaCheckReference(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006425 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006426 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006427 xmlAttrPtr attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006428 const xmlChar *namespaceName)
6429{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006430 /* TODO: Pointer comparison instead? */
6431 if (xmlStrEqual(pctxt->targetNamespace, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006432 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006433 if (xmlStrEqual(xmlSchemaNs, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006434 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006435 /*
6436 * Check if the referenced namespace was <import>ed.
6437 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006438 if (WXS_BUCKET(pctxt)->relations != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006439 xmlSchemaSchemaRelationPtr rel;
6440
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006441 rel = WXS_BUCKET(pctxt)->relations;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006442 do {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006443 if (WXS_IS_BUCKET_IMPMAIN(rel->type) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006444 xmlStrEqual(namespaceName, rel->importNamespace))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006445 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006446 rel = rel->next;
6447 } while (rel != NULL);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006448 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006449 /*
6450 * No matching <import>ed namespace found.
6451 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006452 {
6453 xmlNodePtr n = (attr != NULL) ? (xmlNodePtr) attr : node;
6454
6455 if (namespaceName == NULL)
6456 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6457 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6458 "References from this schema to components in no "
6459 "namespace are not allowed, since not indicated by an "
6460 "import statement", NULL, NULL);
6461 else
6462 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6463 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6464 "References from this schema to components in the "
6465 "namespace '%s' are not allowed, since not indicated by an "
6466 "import statement", namespaceName, NULL);
6467 }
6468 return (XML_SCHEMAP_SRC_RESOLVE);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006469}
6470
Daniel Veillardc0826a72004-08-10 14:17:33 +00006471/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006472 * xmlSchemaParseLocalAttributes:
Daniel Veillard4255d502002-04-16 15:50:10 +00006473 * @ctxt: a schema validation context
6474 * @schema: the schema being built
6475 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006476 * @type: the hosting type where the attributes will be anchored
Daniel Veillard4255d502002-04-16 15:50:10 +00006477 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006478 * Parses attribute uses and attribute declarations and
Daniel Veillarddee23482008-04-11 12:58:43 +00006479 * attribute group references.
Daniel Veillard4255d502002-04-16 15:50:10 +00006480 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006481static int
6482xmlSchemaParseLocalAttributes(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6483 xmlNodePtr *child, xmlSchemaItemListPtr *list,
6484 int parentType, int *hasRefs)
Daniel Veillard4255d502002-04-16 15:50:10 +00006485{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006486 void *item;
Daniel Veillard4255d502002-04-16 15:50:10 +00006487
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006488 while ((IS_SCHEMA((*child), "attribute")) ||
6489 (IS_SCHEMA((*child), "attributeGroup"))) {
6490 if (IS_SCHEMA((*child), "attribute")) {
6491 item = xmlSchemaParseLocalAttribute(ctxt, schema, *child,
6492 *list, parentType);
6493 } else {
6494 item = xmlSchemaParseAttributeGroupRef(ctxt, schema, *child);
6495 if ((item != NULL) && (hasRefs != NULL))
6496 *hasRefs = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006497 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006498 if (item != NULL) {
6499 if (*list == NULL) {
6500 /* TODO: Customize grow factor. */
6501 *list = xmlSchemaItemListCreate();
6502 if (*list == NULL)
6503 return(-1);
6504 }
6505 if (xmlSchemaItemListAddSize(*list, 2, item) == -1)
6506 return(-1);
6507 }
6508 *child = (*child)->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006509 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006510 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00006511}
6512
6513/**
6514 * xmlSchemaParseAnnotation:
6515 * @ctxt: a schema validation context
6516 * @schema: the schema being built
6517 * @node: a subtree containing XML Schema informations
6518 *
6519 * parse a XML schema Attrribute declaration
6520 * *WARNING* this interface is highly subject to change
6521 *
William M. Bracke7091952004-05-11 15:09:58 +00006522 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00006523 * 1 in case of success.
6524 */
6525static xmlSchemaAnnotPtr
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006526xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int needed)
Daniel Veillard4255d502002-04-16 15:50:10 +00006527{
6528 xmlSchemaAnnotPtr ret;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006529 xmlNodePtr child = NULL;
6530 xmlAttrPtr attr;
6531 int barked = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00006532
Daniel Veillardc0826a72004-08-10 14:17:33 +00006533 /*
6534 * INFO: S4S completed.
6535 */
6536 /*
6537 * id = ID
6538 * {any attributes with non-schema namespace . . .}>
6539 * Content: (appinfo | documentation)*
6540 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006541 if ((ctxt == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00006542 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006543 if (needed)
6544 ret = xmlSchemaNewAnnot(ctxt, node);
6545 else
6546 ret = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006547 attr = node->properties;
6548 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006549 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006550 (!xmlStrEqual(attr->name, BAD_CAST "id"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006551 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006552 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006553
6554 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006555 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006556 }
6557 attr = attr->next;
6558 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006559 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006560 /*
6561 * And now for the children...
6562 */
6563 child = node->children;
6564 while (child != NULL) {
6565 if (IS_SCHEMA(child, "appinfo")) {
6566 /* TODO: make available the content of "appinfo". */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006567 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006568 * source = anyURI
6569 * {any attributes with non-schema namespace . . .}>
6570 * Content: ({any})*
6571 */
6572 attr = child->properties;
6573 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006574 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006575 (!xmlStrEqual(attr->name, BAD_CAST "source"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006576 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006577 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006578
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006579 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006580 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006581 }
6582 attr = attr->next;
6583 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006584 xmlSchemaPValAttr(ctxt, NULL, child, "source",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006585 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006586 child = child->next;
6587 } else if (IS_SCHEMA(child, "documentation")) {
6588 /* TODO: make available the content of "documentation". */
6589 /*
6590 * source = anyURI
6591 * {any attributes with non-schema namespace . . .}>
6592 * Content: ({any})*
6593 */
6594 attr = child->properties;
6595 while (attr != NULL) {
6596 if (attr->ns == NULL) {
6597 if (!xmlStrEqual(attr->name, BAD_CAST "source")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006598 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006599 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006600 }
6601 } else {
6602 if (xmlStrEqual(attr->ns->href, xmlSchemaNs) ||
6603 (xmlStrEqual(attr->name, BAD_CAST "lang") &&
6604 (!xmlStrEqual(attr->ns->href, XML_XML_NAMESPACE)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006605
6606 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006607 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006608 }
6609 }
6610 attr = attr->next;
6611 }
6612 /*
6613 * Attribute "xml:lang".
6614 */
6615 attr = xmlSchemaGetPropNodeNs(child, (const char *) XML_XML_NAMESPACE, "lang");
6616 if (attr != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006617 xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006618 xmlSchemaGetBuiltInType(XML_SCHEMAS_LANGUAGE), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006619 child = child->next;
6620 } else {
6621 if (!barked)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006622 xmlSchemaPContentErr(ctxt,
6623 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006624 NULL, node, child, NULL, "(appinfo | documentation)*");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006625 barked = 1;
6626 child = child->next;
6627 }
6628 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006629
Daniel Veillard4255d502002-04-16 15:50:10 +00006630 return (ret);
6631}
6632
6633/**
6634 * xmlSchemaParseFacet:
6635 * @ctxt: a schema validation context
6636 * @schema: the schema being built
6637 * @node: a subtree containing XML Schema informations
6638 *
6639 * parse a XML schema Facet declaration
6640 * *WARNING* this interface is highly subject to change
6641 *
6642 * Returns the new type structure or NULL in case of error
6643 */
6644static xmlSchemaFacetPtr
6645xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006646 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006647{
6648 xmlSchemaFacetPtr facet;
6649 xmlNodePtr child = NULL;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006650 const xmlChar *value;
Daniel Veillard4255d502002-04-16 15:50:10 +00006651
6652 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6653 return (NULL);
6654
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00006655 facet = xmlSchemaNewFacet();
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006656 if (facet == NULL) {
6657 xmlSchemaPErrMemory(ctxt, "allocating facet", node);
6658 return (NULL);
6659 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006660 facet->node = node;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006661 value = xmlSchemaGetProp(ctxt, node, "value");
Daniel Veillard4255d502002-04-16 15:50:10 +00006662 if (value == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006663 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE,
6664 "Facet %s has no value\n", node->name, NULL);
6665 xmlSchemaFreeFacet(facet);
Daniel Veillard4255d502002-04-16 15:50:10 +00006666 return (NULL);
6667 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006668 if (IS_SCHEMA(node, "minInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006669 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006670 } else if (IS_SCHEMA(node, "minExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006671 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006672 } else if (IS_SCHEMA(node, "maxInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006673 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006674 } else if (IS_SCHEMA(node, "maxExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006675 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006676 } else if (IS_SCHEMA(node, "totalDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006677 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006678 } else if (IS_SCHEMA(node, "fractionDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006679 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006680 } else if (IS_SCHEMA(node, "pattern")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006681 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006682 } else if (IS_SCHEMA(node, "enumeration")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006683 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006684 } else if (IS_SCHEMA(node, "whiteSpace")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006685 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006686 } else if (IS_SCHEMA(node, "length")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006687 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006688 } else if (IS_SCHEMA(node, "maxLength")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006689 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
6690 } else if (IS_SCHEMA(node, "minLength")) {
6691 facet->type = XML_SCHEMA_FACET_MINLENGTH;
6692 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006693 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE,
6694 "Unknown facet type %s\n", node->name, NULL);
6695 xmlSchemaFreeFacet(facet);
6696 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006697 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006698 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +00006699 facet->value = value;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006700 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
6701 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
6702 const xmlChar *fixed;
6703
6704 fixed = xmlSchemaGetProp(ctxt, node, "fixed");
6705 if (fixed != NULL) {
6706 if (xmlStrEqual(fixed, BAD_CAST "true"))
6707 facet->fixed = 1;
6708 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006709 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006710 child = node->children;
6711
6712 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006713 facet->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006714 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006715 }
6716 if (child != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006717 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD,
6718 "Facet %s has unexpected child content\n",
6719 node->name, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006720 }
6721 return (facet);
6722}
6723
6724/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006725 * xmlSchemaParseWildcardNs:
6726 * @ctxt: a schema parser context
6727 * @wildc: the wildcard, already created
6728 * @node: a subtree containing XML Schema informations
6729 *
6730 * Parses the attribute "processContents" and "namespace"
6731 * of a xsd:anyAttribute and xsd:any.
6732 * *WARNING* this interface is highly subject to change
6733 *
6734 * Returns 0 if everything goes fine, a positive error code
6735 * if something is not valid and -1 if an internal error occurs.
6736 */
6737static int
6738xmlSchemaParseWildcardNs(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006739 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006740 xmlSchemaWildcardPtr wildc,
6741 xmlNodePtr node)
6742{
6743 const xmlChar *pc, *ns, *dictnsItem;
6744 int ret = 0;
6745 xmlChar *nsItem;
6746 xmlSchemaWildcardNsPtr tmp, lastNs = NULL;
6747 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006748
Daniel Veillardc0826a72004-08-10 14:17:33 +00006749 pc = xmlSchemaGetProp(ctxt, node, "processContents");
6750 if ((pc == NULL)
6751 || (xmlStrEqual(pc, (const xmlChar *) "strict"))) {
6752 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
6753 } else if (xmlStrEqual(pc, (const xmlChar *) "skip")) {
6754 wildc->processContents = XML_SCHEMAS_ANY_SKIP;
6755 } else if (xmlStrEqual(pc, (const xmlChar *) "lax")) {
6756 wildc->processContents = XML_SCHEMAS_ANY_LAX;
6757 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006758 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006759 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006760 NULL, node,
6761 NULL, "(strict | skip | lax)", pc,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006762 NULL, NULL, NULL);
6763 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006764 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006765 }
6766 /*
6767 * Build the namespace constraints.
6768 */
6769 attr = xmlSchemaGetPropNode(node, "namespace");
6770 ns = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00006771 if ((attr == NULL) || (xmlStrEqual(ns, BAD_CAST "##any")))
Daniel Veillardc0826a72004-08-10 14:17:33 +00006772 wildc->any = 1;
6773 else if (xmlStrEqual(ns, BAD_CAST "##other")) {
6774 wildc->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006775 if (wildc->negNsSet == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006776 return (-1);
6777 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006778 wildc->negNsSet->value = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006779 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006780 const xmlChar *end, *cur;
6781
6782 cur = ns;
6783 do {
6784 while (IS_BLANK_CH(*cur))
6785 cur++;
6786 end = cur;
6787 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
6788 end++;
6789 if (end == cur)
6790 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006791 nsItem = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006792 if ((xmlStrEqual(nsItem, BAD_CAST "##other")) ||
6793 (xmlStrEqual(nsItem, BAD_CAST "##any"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006794 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006795 XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006796 NULL, (xmlNodePtr) attr,
6797 NULL,
6798 "((##any | ##other) | List of (xs:anyURI | "
6799 "(##targetNamespace | ##local)))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00006800 nsItem, NULL, NULL, NULL);
6801 ret = XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER;
6802 } else {
6803 if (xmlStrEqual(nsItem, BAD_CAST "##targetNamespace")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006804 dictnsItem = ctxt->targetNamespace;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006805 } else if (xmlStrEqual(nsItem, BAD_CAST "##local")) {
6806 dictnsItem = NULL;
6807 } else {
6808 /*
6809 * Validate the item (anyURI).
6810 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006811 xmlSchemaPValAttrNodeValue(ctxt, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006812 nsItem, xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI));
6813 dictnsItem = xmlDictLookup(ctxt->dict, nsItem, -1);
6814 }
6815 /*
6816 * Avoid dublicate namespaces.
6817 */
6818 tmp = wildc->nsSet;
6819 while (tmp != NULL) {
6820 if (dictnsItem == tmp->value)
6821 break;
6822 tmp = tmp->next;
6823 }
6824 if (tmp == NULL) {
6825 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
6826 if (tmp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006827 xmlFree(nsItem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006828 return (-1);
6829 }
6830 tmp->value = dictnsItem;
6831 tmp->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006832 if (wildc->nsSet == NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006833 wildc->nsSet = tmp;
Daniel Veillard76d36452009-09-07 11:19:33 +02006834 else if (lastNs != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006835 lastNs->next = tmp;
6836 lastNs = tmp;
6837 }
6838
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006839 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006840 xmlFree(nsItem);
6841 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006842 } while (*cur != 0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006843 }
6844 return (ret);
6845}
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006846
6847static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006848xmlSchemaPCheckParticleCorrect_2(xmlSchemaParserCtxtPtr ctxt,
6849 xmlSchemaParticlePtr item ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006850 xmlNodePtr node,
6851 int minOccurs,
6852 int maxOccurs) {
6853
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006854 if ((maxOccurs == 0) && ( minOccurs == 0))
6855 return (0);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006856 if (maxOccurs != UNBOUNDED) {
6857 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006858 * TODO: Maybe we should better not create the particle,
6859 * if min/max is invalid, since it could confuse the build of the
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006860 * content model.
6861 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006862 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006863 * 3.9.6 Schema Component Constraint: Particle Correct
6864 *
6865 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006866 if (maxOccurs < 1) {
6867 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006868 * 2.2 {max occurs} must be greater than or equal to 1.
6869 */
6870 xmlSchemaPCustomAttrErr(ctxt,
6871 XML_SCHEMAP_P_PROPS_CORRECT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006872 NULL, NULL,
6873 xmlSchemaGetPropNode(node, "maxOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006874 "The value must be greater than or equal to 1");
6875 return (XML_SCHEMAP_P_PROPS_CORRECT_2_2);
6876 } else if (minOccurs > maxOccurs) {
6877 /*
6878 * 2.1 {min occurs} must not be greater than {max occurs}.
6879 */
6880 xmlSchemaPCustomAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006881 XML_SCHEMAP_P_PROPS_CORRECT_2_1,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006882 NULL, NULL,
6883 xmlSchemaGetPropNode(node, "minOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006884 "The value must not be greater than the value of 'maxOccurs'");
6885 return (XML_SCHEMAP_P_PROPS_CORRECT_2_1);
6886 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006887 }
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006888 return (0);
6889}
6890
Daniel Veillardc0826a72004-08-10 14:17:33 +00006891/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006892 * xmlSchemaParseAny:
6893 * @ctxt: a schema validation context
6894 * @schema: the schema being built
6895 * @node: a subtree containing XML Schema informations
6896 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006897 * Parsea a XML schema <any> element. A particle and wildcard
6898 * will be created (except if minOccurs==maxOccurs==0, in this case
6899 * nothing will be created).
Daniel Veillard4255d502002-04-16 15:50:10 +00006900 * *WARNING* this interface is highly subject to change
6901 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006902 * Returns the particle or NULL in case of error or if minOccurs==maxOccurs==0
Daniel Veillard4255d502002-04-16 15:50:10 +00006903 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006904static xmlSchemaParticlePtr
Daniel Veillard4255d502002-04-16 15:50:10 +00006905xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6906 xmlNodePtr node)
6907{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006908 xmlSchemaParticlePtr particle;
Daniel Veillard4255d502002-04-16 15:50:10 +00006909 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006910 xmlSchemaWildcardPtr wild;
6911 int min, max;
6912 xmlAttrPtr attr;
6913 xmlSchemaAnnotPtr annot = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00006914
6915 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6916 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006917 /*
6918 * Check for illegal attributes.
6919 */
6920 attr = node->properties;
6921 while (attr != NULL) {
6922 if (attr->ns == NULL) {
6923 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6924 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
6925 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
6926 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6927 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006928 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006929 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006930 }
6931 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006932 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006933 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006934 }
6935 attr = attr->next;
6936 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006937 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006938 /*
6939 * minOccurs/maxOccurs.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006940 */
6941 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
6942 "(xs:nonNegativeInteger | unbounded)");
6943 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1,
6944 "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006945 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
6946 /*
6947 * Create & parse the wildcard.
6948 */
6949 wild = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY, node);
6950 if (wild == NULL)
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006951 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006952 xmlSchemaParseWildcardNs(ctxt, schema, wild, node);
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006953 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006954 * And now for the children...
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006955 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006956 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00006957 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00006958 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006959 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006960 }
6961 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006962 xmlSchemaPContentErr(ctxt,
6963 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006964 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006965 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006966 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006967 /*
6968 * No component if minOccurs==maxOccurs==0.
6969 */
6970 if ((min == 0) && (max == 0)) {
6971 /* Don't free the wildcard, since it's already on the list. */
6972 return (NULL);
6973 }
6974 /*
6975 * Create the particle.
6976 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00006977 particle = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006978 if (particle == NULL)
6979 return (NULL);
6980 particle->annot = annot;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006981 particle->children = (xmlSchemaTreeItemPtr) wild;
Daniel Veillard4255d502002-04-16 15:50:10 +00006982
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006983 return (particle);
Daniel Veillard4255d502002-04-16 15:50:10 +00006984}
6985
6986/**
6987 * xmlSchemaParseNotation:
6988 * @ctxt: a schema validation context
6989 * @schema: the schema being built
6990 * @node: a subtree containing XML Schema informations
6991 *
6992 * parse a XML schema Notation declaration
6993 *
6994 * Returns the new structure or NULL in case of error
6995 */
6996static xmlSchemaNotationPtr
6997xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006998 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006999{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00007000 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00007001 xmlSchemaNotationPtr ret;
7002 xmlNodePtr child = NULL;
7003
7004 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
7005 return (NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00007006 name = xmlSchemaGetProp(ctxt, node, "name");
Daniel Veillard4255d502002-04-16 15:50:10 +00007007 if (name == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007008 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME,
7009 "Notation has no name\n", NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00007010 return (NULL);
7011 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007012 ret = xmlSchemaAddNotation(ctxt, schema, name,
7013 ctxt->targetNamespace, node);
7014 if (ret == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00007015 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007016 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00007017
Daniel Veillard4255d502002-04-16 15:50:10 +00007018 child = node->children;
7019 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007020 ret->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007021 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007022 }
7023 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007024 xmlSchemaPContentErr(ctxt,
7025 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007026 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007027 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00007028 }
7029
7030 return (ret);
7031}
7032
7033/**
7034 * xmlSchemaParseAnyAttribute:
7035 * @ctxt: a schema validation context
7036 * @schema: the schema being built
7037 * @node: a subtree containing XML Schema informations
7038 *
7039 * parse a XML schema AnyAttrribute declaration
7040 * *WARNING* this interface is highly subject to change
7041 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007042 * Returns a wildcard or NULL.
Daniel Veillard4255d502002-04-16 15:50:10 +00007043 */
Daniel Veillard3646d642004-06-02 19:19:14 +00007044static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007045xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
7046 xmlSchemaPtr schema, xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007047{
Daniel Veillard3646d642004-06-02 19:19:14 +00007048 xmlSchemaWildcardPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007049 xmlNodePtr child = NULL;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007050 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00007051
7052 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
7053 return (NULL);
7054
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00007055 ret = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
7056 node);
Daniel Veillard4255d502002-04-16 15:50:10 +00007057 if (ret == NULL) {
7058 return (NULL);
7059 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007060 /*
7061 * Check for illegal attributes.
7062 */
7063 attr = node->properties;
7064 while (attr != NULL) {
7065 if (attr->ns == NULL) {
7066 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7067 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
7068 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007069 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007070 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007071 }
7072 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007073 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007074 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007075 }
7076 attr = attr->next;
7077 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007078 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007079 /*
7080 * Parse the namespace list.
7081 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007082 if (xmlSchemaParseWildcardNs(ctxt, schema, ret, node) != 0)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007083 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007084 /*
7085 * And now for the children...
7086 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007087 child = node->children;
7088 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007089 ret->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007090 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007091 }
7092 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00007093 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007094 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007095 NULL, node, child,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007096 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00007097 }
7098
7099 return (ret);
7100}
7101
7102
7103/**
7104 * xmlSchemaParseAttribute:
7105 * @ctxt: a schema validation context
7106 * @schema: the schema being built
7107 * @node: a subtree containing XML Schema informations
7108 *
7109 * parse a XML schema Attrribute declaration
7110 * *WARNING* this interface is highly subject to change
7111 *
William M. Bracke7091952004-05-11 15:09:58 +00007112 * Returns the attribute declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00007113 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007114static xmlSchemaBasicItemPtr
7115xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
7116 xmlSchemaPtr schema,
7117 xmlNodePtr node,
7118 xmlSchemaItemListPtr uses,
7119 int parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +00007120{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007121 const xmlChar *attrValue, *name = NULL, *ns = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00007122 xmlSchemaAttributeUsePtr use = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007123 xmlNodePtr child = NULL;
7124 xmlAttrPtr attr;
7125 const xmlChar *tmpNs = NULL, *tmpName = NULL, *defValue = NULL;
7126 int isRef = 0, occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7127 int nberrors, hasForm = 0, defValueType = 0;
7128
7129#define WXS_ATTR_DEF_VAL_DEFAULT 1
7130#define WXS_ATTR_DEF_VAL_FIXED 2
7131
7132 /*
7133 * 3.2.3 Constraints on XML Representations of Attribute Declarations
7134 */
7135
7136 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7137 return (NULL);
7138 attr = xmlSchemaGetPropNode(node, "ref");
7139 if (attr != NULL) {
7140 if (xmlSchemaPValAttrNodeQName(pctxt, schema,
7141 NULL, attr, &tmpNs, &tmpName) != 0) {
7142 return (NULL);
7143 }
7144 if (xmlSchemaCheckReference(pctxt, schema, node, attr, tmpNs) != 0)
7145 return(NULL);
7146 isRef = 1;
7147 }
7148 nberrors = pctxt->nberrors;
7149 /*
7150 * Check for illegal attributes.
7151 */
7152 attr = node->properties;
7153 while (attr != NULL) {
7154 if (attr->ns == NULL) {
7155 if (isRef) {
7156 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7157 xmlSchemaPValAttrNodeID(pctxt, attr);
7158 goto attr_next;
7159 } else if (xmlStrEqual(attr->name, BAD_CAST "ref")) {
7160 goto attr_next;
7161 }
7162 } else {
7163 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
7164 goto attr_next;
7165 } else if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7166 xmlSchemaPValAttrNodeID(pctxt, attr);
7167 goto attr_next;
7168 } else if (xmlStrEqual(attr->name, BAD_CAST "type")) {
7169 xmlSchemaPValAttrNodeQName(pctxt, schema, NULL,
7170 attr, &tmpNs, &tmpName);
7171 goto attr_next;
7172 } else if (xmlStrEqual(attr->name, BAD_CAST "form")) {
7173 /*
7174 * Evaluate the target namespace
7175 */
Daniel Veillarddee23482008-04-11 12:58:43 +00007176 hasForm = 1;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007177 attrValue = xmlSchemaGetNodeContent(pctxt,
7178 (xmlNodePtr) attr);
7179 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
7180 ns = pctxt->targetNamespace;
7181 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified"))
7182 {
7183 xmlSchemaPSimpleTypeErr(pctxt,
7184 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
7185 NULL, (xmlNodePtr) attr,
7186 NULL, "(qualified | unqualified)",
7187 attrValue, NULL, NULL, NULL);
7188 }
7189 goto attr_next;
7190 }
7191 }
7192 if (xmlStrEqual(attr->name, BAD_CAST "use")) {
7193
7194 attrValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7195 /* TODO: Maybe we need to normalize the value beforehand. */
7196 if (xmlStrEqual(attrValue, BAD_CAST "optional"))
7197 occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7198 else if (xmlStrEqual(attrValue, BAD_CAST "prohibited"))
7199 occurs = XML_SCHEMAS_ATTR_USE_PROHIBITED;
7200 else if (xmlStrEqual(attrValue, BAD_CAST "required"))
7201 occurs = XML_SCHEMAS_ATTR_USE_REQUIRED;
7202 else {
7203 xmlSchemaPSimpleTypeErr(pctxt,
7204 XML_SCHEMAP_INVALID_ATTR_USE,
7205 NULL, (xmlNodePtr) attr,
7206 NULL, "(optional | prohibited | required)",
7207 attrValue, NULL, NULL, NULL);
7208 }
7209 goto attr_next;
7210 } else if (xmlStrEqual(attr->name, BAD_CAST "default")) {
7211 /*
7212 * 3.2.3 : 1
7213 * default and fixed must not both be present.
7214 */
7215 if (defValue) {
7216 xmlSchemaPMutualExclAttrErr(pctxt,
7217 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7218 NULL, attr, "default", "fixed");
7219 } else {
7220 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7221 defValueType = WXS_ATTR_DEF_VAL_DEFAULT;
7222 }
7223 goto attr_next;
7224 } else if (xmlStrEqual(attr->name, BAD_CAST "fixed")) {
7225 /*
7226 * 3.2.3 : 1
7227 * default and fixed must not both be present.
7228 */
7229 if (defValue) {
7230 xmlSchemaPMutualExclAttrErr(pctxt,
7231 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7232 NULL, attr, "default", "fixed");
7233 } else {
7234 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7235 defValueType = WXS_ATTR_DEF_VAL_FIXED;
7236 }
7237 goto attr_next;
7238 }
7239 } else if (! xmlStrEqual(attr->ns->href, xmlSchemaNs))
7240 goto attr_next;
7241
7242 xmlSchemaPIllegalAttrErr(pctxt,
7243 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7244
7245attr_next:
7246 attr = attr->next;
Daniel Veillarddee23482008-04-11 12:58:43 +00007247 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007248 /*
7249 * 3.2.3 : 2
7250 * If default and use are both present, use must have
7251 * the actual value optional.
7252 */
7253 if ((defValueType == WXS_ATTR_DEF_VAL_DEFAULT) &&
7254 (occurs != XML_SCHEMAS_ATTR_USE_OPTIONAL)) {
7255 xmlSchemaPSimpleTypeErr(pctxt,
7256 XML_SCHEMAP_SRC_ATTRIBUTE_2,
7257 NULL, node, NULL,
7258 "(optional | prohibited | required)", NULL,
7259 "The value of the attribute 'use' must be 'optional' "
7260 "if the attribute 'default' is present",
7261 NULL, NULL);
7262 }
7263 /*
7264 * We want correct attributes.
7265 */
7266 if (nberrors != pctxt->nberrors)
7267 return(NULL);
7268 if (! isRef) {
7269 xmlSchemaAttributePtr attrDecl;
7270
7271 /* TODO: move XML_SCHEMAS_QUALIF_ATTR to the parser. */
7272 if ((! hasForm) && (schema->flags & XML_SCHEMAS_QUALIF_ATTR))
Daniel Veillarddee23482008-04-11 12:58:43 +00007273 ns = pctxt->targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007274 /*
7275 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7276 * TODO: Move this to the component layer.
7277 */
7278 if (xmlStrEqual(ns, xmlSchemaInstanceNs)) {
7279 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7280 XML_SCHEMAP_NO_XSI,
7281 node, NULL,
7282 "The target namespace must not match '%s'",
7283 xmlSchemaInstanceNs, NULL);
7284 }
7285 attr = xmlSchemaGetPropNode(node, "name");
7286 if (attr == NULL) {
7287 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7288 NULL, node, "name", NULL);
7289 return (NULL);
7290 }
7291 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7292 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7293 return (NULL);
7294 }
7295 /*
7296 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7297 * TODO: Move this to the component layer.
7298 */
7299 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
7300 xmlSchemaPSimpleTypeErr(pctxt,
7301 XML_SCHEMAP_NO_XMLNS,
7302 NULL, (xmlNodePtr) attr,
7303 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7304 "The value of the attribute must not match 'xmlns'",
7305 NULL, NULL);
7306 return (NULL);
7307 }
7308 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED)
7309 goto check_children;
7310 /*
7311 * Create the attribute use component.
7312 */
7313 use = xmlSchemaAddAttributeUse(pctxt, node);
7314 if (use == NULL)
7315 return(NULL);
7316 use->occurs = occurs;
7317 /*
7318 * Create the attribute declaration.
7319 */
7320 attrDecl = xmlSchemaAddAttribute(pctxt, schema, name, ns, node, 0);
7321 if (attrDecl == NULL)
7322 return (NULL);
7323 if (tmpName != NULL) {
7324 attrDecl->typeName = tmpName;
7325 attrDecl->typeNs = tmpNs;
7326 }
7327 use->attrDecl = attrDecl;
7328 /*
7329 * Value constraint.
Daniel Veillarddee23482008-04-11 12:58:43 +00007330 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007331 if (defValue != NULL) {
7332 attrDecl->defValue = defValue;
7333 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7334 attrDecl->flags |= XML_SCHEMAS_ATTR_FIXED;
7335 }
7336 } else if (occurs != XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7337 xmlSchemaQNameRefPtr ref;
7338
7339 /*
7340 * Create the attribute use component.
7341 */
7342 use = xmlSchemaAddAttributeUse(pctxt, node);
7343 if (use == NULL)
7344 return(NULL);
7345 /*
7346 * We need to resolve the reference at later stage.
7347 */
7348 WXS_ADD_PENDING(pctxt, use);
7349 use->occurs = occurs;
7350 /*
7351 * Create a QName reference to the attribute declaration.
7352 */
7353 ref = xmlSchemaNewQNameRef(pctxt, XML_SCHEMA_TYPE_ATTRIBUTE,
7354 tmpName, tmpNs);
7355 if (ref == NULL)
7356 return(NULL);
7357 /*
7358 * Assign the reference. This will be substituted for the
7359 * referenced attribute declaration when the QName is resolved.
7360 */
7361 use->attrDecl = WXS_ATTR_CAST ref;
7362 /*
7363 * Value constraint.
7364 */
7365 if (defValue != NULL)
7366 use->defValue = defValue;
7367 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7368 use->flags |= XML_SCHEMA_ATTR_USE_FIXED;
7369 }
Daniel Veillarddee23482008-04-11 12:58:43 +00007370
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007371check_children:
7372 /*
7373 * And now for the children...
7374 */
7375 child = node->children;
7376 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED) {
Daniel Veillarddee23482008-04-11 12:58:43 +00007377 xmlSchemaAttributeUseProhibPtr prohib;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007378
7379 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007380 xmlSchemaParseAnnotation(pctxt, child, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007381 child = child->next;
7382 }
7383 if (child != NULL) {
7384 xmlSchemaPContentErr(pctxt,
7385 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7386 NULL, node, child, NULL,
7387 "(annotation?)");
7388 }
7389 /*
7390 * Check for pointlessness of attribute prohibitions.
7391 */
Daniel Veillarddee23482008-04-11 12:58:43 +00007392 if (parentType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) {
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 attribute use prohibition, since it is "
7397 "pointless inside an <attributeGroup>",
7398 NULL, NULL, NULL);
7399 return(NULL);
7400 } else if (parentType == XML_SCHEMA_TYPE_EXTENSION) {
7401 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7402 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7403 node, NULL,
7404 "Skipping attribute use prohibition, since it is "
7405 "pointless when extending a type",
7406 NULL, NULL, NULL);
7407 return(NULL);
7408 }
7409 if (! isRef) {
Daniel Veillarddee23482008-04-11 12:58:43 +00007410 tmpName = name;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007411 tmpNs = ns;
7412 }
7413 /*
7414 * Check for duplicate attribute prohibitions.
7415 */
7416 if (uses) {
7417 int i;
Daniel Veillarddee23482008-04-11 12:58:43 +00007418
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007419 for (i = 0; i < uses->nbItems; i++) {
7420 use = uses->items[i];
7421 if ((use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) &&
7422 (tmpName == (WXS_ATTR_PROHIB_CAST use)->name) &&
7423 (tmpNs == (WXS_ATTR_PROHIB_CAST use)->targetNamespace))
7424 {
7425 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00007426
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007427 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7428 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7429 node, NULL,
7430 "Skipping duplicate attribute use prohibition '%s'",
7431 xmlSchemaFormatQName(&str, tmpNs, tmpName),
7432 NULL, NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00007433 FREE_AND_NULL(str)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007434 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00007435 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007436 }
7437 }
7438 /*
7439 * Create the attribute prohibition helper component.
7440 */
7441 prohib = xmlSchemaAddAttributeUseProhib(pctxt);
7442 if (prohib == NULL)
7443 return(NULL);
7444 prohib->node = node;
7445 prohib->name = tmpName;
7446 prohib->targetNamespace = tmpNs;
7447 if (isRef) {
7448 /*
7449 * We need at least to resolve to the attribute declaration.
7450 */
7451 WXS_ADD_PENDING(pctxt, prohib);
7452 }
7453 return(WXS_BASIC_CAST prohib);
Daniel Veillarddee23482008-04-11 12:58:43 +00007454 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007455 if (IS_SCHEMA(child, "annotation")) {
7456 /*
7457 * TODO: Should this go into the attr decl?
7458 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007459 use->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007460 child = child->next;
7461 }
7462 if (isRef) {
7463 if (child != NULL) {
7464 if (IS_SCHEMA(child, "simpleType"))
7465 /*
7466 * 3.2.3 : 3.2
7467 * If ref is present, then all of <simpleType>,
7468 * form and type must be absent.
7469 */
7470 xmlSchemaPContentErr(pctxt,
7471 XML_SCHEMAP_SRC_ATTRIBUTE_3_2,
7472 NULL, node, child, NULL,
7473 "(annotation?)");
7474 else
7475 xmlSchemaPContentErr(pctxt,
7476 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7477 NULL, node, child, NULL,
7478 "(annotation?)");
7479 }
7480 } else {
7481 if (IS_SCHEMA(child, "simpleType")) {
7482 if (WXS_ATTRUSE_DECL(use)->typeName != NULL) {
7483 /*
7484 * 3.2.3 : 4
7485 * type and <simpleType> must not both be present.
7486 */
7487 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7488 NULL, node, child,
7489 "The attribute 'type' and the <simpleType> child "
7490 "are mutually exclusive", NULL);
7491 } else
7492 WXS_ATTRUSE_TYPEDEF(use) =
7493 xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7494 child = child->next;
7495 }
7496 if (child != NULL)
7497 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7498 NULL, node, child, NULL,
7499 "(annotation?, simpleType?)");
7500 }
Daniel Veillarddee23482008-04-11 12:58:43 +00007501 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007502 return (WXS_BASIC_CAST use);
7503}
7504
7505
7506static xmlSchemaAttributePtr
7507xmlSchemaParseGlobalAttribute(xmlSchemaParserCtxtPtr pctxt,
7508 xmlSchemaPtr schema,
7509 xmlNodePtr node)
7510{
7511 const xmlChar *attrValue;
Daniel Veillard4255d502002-04-16 15:50:10 +00007512 xmlSchemaAttributePtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007513 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007514 xmlAttrPtr attr;
William M. Bracke7091952004-05-11 15:09:58 +00007515
7516 /*
7517 * Note that the w3c spec assumes the schema to be validated with schema
7518 * for schemas beforehand.
7519 *
7520 * 3.2.3 Constraints on XML Representations of Attribute Declarations
William M. Bracke7091952004-05-11 15:09:58 +00007521 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007522 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
Daniel Veillarddee23482008-04-11 12:58:43 +00007523 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007524 /*
7525 * 3.2.3 : 3.1
7526 * One of ref or name must be present, but not both
7527 */
7528 attr = xmlSchemaGetPropNode(node, "name");
7529 if (attr == NULL) {
7530 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7531 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007532 return (NULL);
7533 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007534 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7535 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0) {
7536 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007537 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007538 /*
7539 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7540 * TODO: Move this to the component layer.
7541 */
7542 if (xmlStrEqual(attrValue, BAD_CAST "xmlns")) {
7543 xmlSchemaPSimpleTypeErr(pctxt,
7544 XML_SCHEMAP_NO_XMLNS,
7545 NULL, (xmlNodePtr) attr,
7546 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7547 "The value of the attribute must not match 'xmlns'",
7548 NULL, NULL);
7549 return (NULL);
7550 }
7551 /*
7552 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7553 * TODO: Move this to the component layer.
7554 * Or better leave it here and add it to the component layer
7555 * if we have a schema construction API.
7556 */
7557 if (xmlStrEqual(pctxt->targetNamespace, xmlSchemaInstanceNs)) {
7558 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7559 XML_SCHEMAP_NO_XSI, node, NULL,
7560 "The target namespace must not match '%s'",
7561 xmlSchemaInstanceNs, NULL);
7562 }
7563
7564 ret = xmlSchemaAddAttribute(pctxt, schema, attrValue,
7565 pctxt->targetNamespace, node, 1);
7566 if (ret == NULL)
7567 return (NULL);
7568 ret->flags |= XML_SCHEMAS_ATTR_GLOBAL;
Daniel Veillarddee23482008-04-11 12:58:43 +00007569
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007570 /*
7571 * Check for illegal attributes.
7572 */
7573 attr = node->properties;
7574 while (attr != NULL) {
7575 if (attr->ns == NULL) {
7576 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7577 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
7578 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
7579 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7580 (!xmlStrEqual(attr->name, BAD_CAST "type")))
7581 {
7582 xmlSchemaPIllegalAttrErr(pctxt,
Daniel Veillarddee23482008-04-11 12:58:43 +00007583 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007584 }
7585 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7586 xmlSchemaPIllegalAttrErr(pctxt,
7587 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7588 }
7589 attr = attr->next;
7590 }
7591 xmlSchemaPValAttrQName(pctxt, schema, NULL,
7592 node, "type", &ret->typeNs, &ret->typeName);
Daniel Veillarddee23482008-04-11 12:58:43 +00007593
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007594 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
William M. Bracke7091952004-05-11 15:09:58 +00007595 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007596 * Attribute "fixed".
William M. Bracke7091952004-05-11 15:09:58 +00007597 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007598 ret->defValue = xmlSchemaGetProp(pctxt, node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007599 if (ret->defValue != NULL)
7600 ret->flags |= XML_SCHEMAS_ATTR_FIXED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007601 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007602 * Attribute "default".
7603 */
7604 attr = xmlSchemaGetPropNode(node, "default");
7605 if (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007606 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007607 * 3.2.3 : 1
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007608 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00007609 */
7610 if (ret->flags & XML_SCHEMAS_ATTR_FIXED) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007611 xmlSchemaPMutualExclAttrErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_1,
7612 WXS_BASIC_CAST ret, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007613 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007614 ret->defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007615 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00007616 /*
7617 * And now for the children...
7618 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007619 child = node->children;
7620 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007621 ret->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007622 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007623 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007624 if (IS_SCHEMA(child, "simpleType")) {
7625 if (ret->typeName != NULL) {
7626 /*
7627 * 3.2.3 : 4
7628 * type and <simpleType> must not both be present.
7629 */
7630 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7631 NULL, node, child,
7632 "The attribute 'type' and the <simpleType> child "
7633 "are mutually exclusive", NULL);
7634 } else
7635 ret->subtypes = xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7636 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007637 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007638 if (child != NULL)
7639 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7640 NULL, node, child, NULL,
7641 "(annotation?, simpleType?)");
7642
Daniel Veillard4255d502002-04-16 15:50:10 +00007643 return (ret);
7644}
7645
7646/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007647 * xmlSchemaParseAttributeGroupRef:
Daniel Veillard4255d502002-04-16 15:50:10 +00007648 * @ctxt: a schema validation context
7649 * @schema: the schema being built
7650 * @node: a subtree containing XML Schema informations
7651 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007652 * Parse an attribute group definition reference.
7653 * Note that a reference to an attribute group does not
7654 * correspond to any component at all.
Daniel Veillard4255d502002-04-16 15:50:10 +00007655 * *WARNING* this interface is highly subject to change
7656 *
7657 * Returns the attribute group or NULL in case of error.
7658 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007659static xmlSchemaQNameRefPtr
7660xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
7661 xmlSchemaPtr schema,
7662 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007663{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007664 xmlSchemaQNameRefPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007665 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007666 xmlAttrPtr attr;
7667 const xmlChar *refNs = NULL, *ref = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00007668
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007669 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00007670 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007671
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007672 attr = xmlSchemaGetPropNode(node, "ref");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007673 if (attr == NULL) {
7674 xmlSchemaPMissingAttrErr(pctxt,
7675 XML_SCHEMAP_S4S_ATTR_MISSING,
7676 NULL, node, "ref", NULL);
7677 return (NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00007678 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007679 xmlSchemaPValAttrNodeQName(pctxt, schema,
7680 NULL, attr, &refNs, &ref);
7681 if (xmlSchemaCheckReference(pctxt, schema, node, attr, refNs) != 0)
7682 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +00007683
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007684 /*
7685 * Check for illegal attributes.
7686 */
7687 attr = node->properties;
7688 while (attr != NULL) {
7689 if (attr->ns == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007690 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007691 (!xmlStrEqual(attr->name, BAD_CAST "id")))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007692 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007693 xmlSchemaPIllegalAttrErr(pctxt,
7694 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007695 }
7696 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007697 xmlSchemaPIllegalAttrErr(pctxt,
7698 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007699 }
7700 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007701 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00007702 /* Attribute ID */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007703 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7704
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007705 /*
7706 * And now for the children...
7707 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007708 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00007709 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007710 /*
7711 * TODO: We do not have a place to store the annotation, do we?
7712 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007713 xmlSchemaParseAnnotation(pctxt, child, 0);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007714 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007715 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007716 if (child != NULL) {
7717 xmlSchemaPContentErr(pctxt,
7718 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7719 NULL, node, child, NULL,
7720 "(annotation?)");
7721 }
7722
7723 /*
7724 * Handle attribute group redefinitions.
7725 */
7726 if (pctxt->isRedefine && pctxt->redef &&
7727 (pctxt->redef->item->type ==
7728 XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
7729 (ref == pctxt->redef->refName) &&
7730 (refNs == pctxt->redef->refTargetNs))
7731 {
7732 /*
7733 * SPEC src-redefine:
7734 * (7.1) "If it has an <attributeGroup> among its contents
Jan Pokorný761c9e92013-11-29 23:26:27 +01007735 * the `actual value` of whose ref [attribute] is the same
7736 * as the `actual value` of its own name attribute plus
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007737 * target namespace, then it must have exactly one such group."
7738 */
7739 if (pctxt->redefCounter != 0) {
7740 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +00007741
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007742 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7743 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
7744 "The redefining attribute group definition "
7745 "'%s' must not contain more than one "
7746 "reference to the redefined definition",
7747 xmlSchemaFormatQName(&str, refNs, ref), NULL);
7748 FREE_AND_NULL(str);
7749 return(NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007750 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007751 pctxt->redefCounter++;
7752 /*
7753 * URGENT TODO: How to ensure that the reference will not be
7754 * handled by the normal component resolution mechanism?
7755 */
7756 ret = xmlSchemaNewQNameRef(pctxt,
7757 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7758 if (ret == NULL)
7759 return(NULL);
7760 ret->node = node;
7761 pctxt->redef->reference = WXS_BASIC_CAST ret;
7762 } else {
7763 /*
7764 * Create a QName-reference helper component. We will substitute this
7765 * component for the attribute uses of the referenced attribute group
7766 * definition.
7767 */
7768 ret = xmlSchemaNewQNameRef(pctxt,
7769 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7770 if (ret == NULL)
7771 return(NULL);
7772 ret->node = node;
7773 /* Add to pending items, to be able to resolve the reference. */
7774 WXS_ADD_PENDING(pctxt, ret);
Daniel Veillarddee23482008-04-11 12:58:43 +00007775 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007776 return (ret);
7777}
7778
7779/**
7780 * xmlSchemaParseAttributeGroupDefinition:
7781 * @pctxt: a schema validation context
7782 * @schema: the schema being built
7783 * @node: a subtree containing XML Schema informations
7784 *
7785 * parse a XML schema Attribute Group declaration
7786 * *WARNING* this interface is highly subject to change
7787 *
7788 * Returns the attribute group definition or NULL in case of error.
7789 */
7790static xmlSchemaAttributeGroupPtr
7791xmlSchemaParseAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
7792 xmlSchemaPtr schema,
7793 xmlNodePtr node)
7794{
7795 const xmlChar *name;
7796 xmlSchemaAttributeGroupPtr ret;
7797 xmlNodePtr child = NULL;
7798 xmlAttrPtr attr;
7799 int hasRefs = 0;
7800
7801 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7802 return (NULL);
7803
7804 attr = xmlSchemaGetPropNode(node, "name");
7805 if (attr == NULL) {
7806 xmlSchemaPMissingAttrErr(pctxt,
7807 XML_SCHEMAP_S4S_ATTR_MISSING,
7808 NULL, node, "name", NULL);
7809 return (NULL);
7810 }
7811 /*
7812 * The name is crucial, exit if invalid.
7813 */
7814 if (xmlSchemaPValAttrNode(pctxt,
7815 NULL, attr,
7816 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7817 return (NULL);
7818 }
7819 ret = xmlSchemaAddAttributeGroupDefinition(pctxt, schema,
7820 name, pctxt->targetNamespace, node);
7821 if (ret == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +00007822 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007823 /*
7824 * Check for illegal attributes.
7825 */
7826 attr = node->properties;
7827 while (attr != NULL) {
7828 if (attr->ns == NULL) {
7829 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7830 (!xmlStrEqual(attr->name, BAD_CAST "id")))
7831 {
7832 xmlSchemaPIllegalAttrErr(pctxt,
7833 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7834 }
7835 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7836 xmlSchemaPIllegalAttrErr(pctxt,
7837 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7838 }
7839 attr = attr->next;
7840 }
7841 /* Attribute ID */
7842 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7843 /*
7844 * And now for the children...
7845 */
7846 child = node->children;
7847 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00007848 ret->annot = xmlSchemaParseAnnotation(pctxt, child, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007849 child = child->next;
7850 }
7851 /*
7852 * Parse contained attribute decls/refs.
7853 */
7854 if (xmlSchemaParseLocalAttributes(pctxt, schema, &child,
7855 (xmlSchemaItemListPtr *) &(ret->attrUses),
7856 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, &hasRefs) == -1)
7857 return(NULL);
7858 if (hasRefs)
7859 ret->flags |= XML_SCHEMAS_ATTRGROUP_HAS_REFS;
7860 /*
7861 * Parse the attribute wildcard.
7862 */
7863 if (IS_SCHEMA(child, "anyAttribute")) {
7864 ret->attributeWildcard = xmlSchemaParseAnyAttribute(pctxt,
7865 schema, child);
7866 child = child->next;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007867 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007868 if (child != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007869 xmlSchemaPContentErr(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007870 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007871 NULL, node, child, NULL,
7872 "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))");
Daniel Veillard4255d502002-04-16 15:50:10 +00007873 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007874 return (ret);
7875}
7876
7877/**
William M. Brack2f2a6632004-08-20 23:09:47 +00007878 * xmlSchemaPValAttrFormDefault:
7879 * @value: the value
7880 * @flags: the flags to be modified
7881 * @flagQualified: the specific flag for "qualified"
7882 *
7883 * Returns 0 if the value is valid, 1 otherwise.
7884 */
7885static int
7886xmlSchemaPValAttrFormDefault(const xmlChar *value,
7887 int *flags,
7888 int flagQualified)
7889{
7890 if (xmlStrEqual(value, BAD_CAST "qualified")) {
7891 if ((*flags & flagQualified) == 0)
7892 *flags |= flagQualified;
7893 } else if (!xmlStrEqual(value, BAD_CAST "unqualified"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007894 return (1);
7895
William M. Brack2f2a6632004-08-20 23:09:47 +00007896 return (0);
7897}
7898
7899/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00007900 * xmlSchemaPValAttrBlockFinal:
7901 * @value: the value
7902 * @flags: the flags to be modified
7903 * @flagAll: the specific flag for "#all"
7904 * @flagExtension: the specific flag for "extension"
7905 * @flagRestriction: the specific flag for "restriction"
7906 * @flagSubstitution: the specific flag for "substitution"
7907 * @flagList: the specific flag for "list"
7908 * @flagUnion: the specific flag for "union"
7909 *
7910 * Validates the value of the attribute "final" and "block". The value
7911 * is converted into the specified flag values and returned in @flags.
7912 *
7913 * Returns 0 if the value is valid, 1 otherwise.
7914 */
7915
7916static int
7917xmlSchemaPValAttrBlockFinal(const xmlChar *value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007918 int *flags,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007919 int flagAll,
7920 int flagExtension,
7921 int flagRestriction,
7922 int flagSubstitution,
7923 int flagList,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007924 int flagUnion)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007925{
7926 int ret = 0;
7927
7928 /*
7929 * TODO: This does not check for dublicate entries.
7930 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00007931 if ((flags == NULL) || (value == NULL))
7932 return (-1);
7933 if (value[0] == 0)
7934 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007935 if (xmlStrEqual(value, BAD_CAST "#all")) {
7936 if (flagAll != -1)
7937 *flags |= flagAll;
7938 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007939 if (flagExtension != -1)
7940 *flags |= flagExtension;
7941 if (flagRestriction != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007942 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007943 if (flagSubstitution != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007944 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007945 if (flagList != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007946 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007947 if (flagUnion != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007948 *flags |= flagUnion;
7949 }
7950 } else {
7951 const xmlChar *end, *cur = value;
7952 xmlChar *item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007953
Daniel Veillardc0826a72004-08-10 14:17:33 +00007954 do {
7955 while (IS_BLANK_CH(*cur))
7956 cur++;
7957 end = cur;
7958 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
7959 end++;
7960 if (end == cur)
7961 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007962 item = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007963 if (xmlStrEqual(item, BAD_CAST "extension")) {
7964 if (flagExtension != -1) {
7965 if ((*flags & flagExtension) == 0)
7966 *flags |= flagExtension;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007967 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007968 ret = 1;
7969 } else if (xmlStrEqual(item, BAD_CAST "restriction")) {
7970 if (flagRestriction != -1) {
7971 if ((*flags & flagRestriction) == 0)
7972 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007973 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007974 ret = 1;
7975 } else if (xmlStrEqual(item, BAD_CAST "substitution")) {
7976 if (flagSubstitution != -1) {
7977 if ((*flags & flagSubstitution) == 0)
7978 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007979 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007980 ret = 1;
7981 } else if (xmlStrEqual(item, BAD_CAST "list")) {
7982 if (flagList != -1) {
7983 if ((*flags & flagList) == 0)
7984 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007985 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007986 ret = 1;
7987 } else if (xmlStrEqual(item, BAD_CAST "union")) {
7988 if (flagUnion != -1) {
7989 if ((*flags & flagUnion) == 0)
7990 *flags |= flagUnion;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007991 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007992 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007993 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007994 ret = 1;
7995 if (item != NULL)
7996 xmlFree(item);
7997 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007998 } while ((ret == 0) && (*cur != 0));
7999 }
8000
Daniel Veillardc0826a72004-08-10 14:17:33 +00008001 return (ret);
8002}
8003
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008004static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008005xmlSchemaCheckCSelectorXPath(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008006 xmlSchemaIDCPtr idc,
8007 xmlSchemaIDCSelectPtr selector,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008008 xmlAttrPtr attr,
8009 int isField)
8010{
8011 xmlNodePtr node;
8012
8013 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008014 * c-selector-xpath:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008015 * Schema Component Constraint: Selector Value OK
8016 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008017 * TODO: 1 The {selector} must be a valid XPath expression, as defined
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008018 * in [XPath].
8019 */
8020 if (selector == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008021 xmlSchemaPErr(ctxt, idc->node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008022 XML_SCHEMAP_INTERNAL,
8023 "Internal error: xmlSchemaCheckCSelectorXPath, "
8024 "the selector is not specified.\n", NULL, NULL);
8025 return (-1);
8026 }
8027 if (attr == NULL)
8028 node = idc->node;
8029 else
8030 node = (xmlNodePtr) attr;
8031 if (selector->xpath == NULL) {
8032 xmlSchemaPCustomErr(ctxt,
8033 /* TODO: Adjust error code. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008034 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008035 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008036 "The XPath expression of the selector is not valid", NULL);
8037 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
8038 } else {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008039 const xmlChar **nsArray = NULL;
8040 xmlNsPtr *nsList = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008041 /*
8042 * Compile the XPath expression.
8043 */
8044 /*
8045 * TODO: We need the array of in-scope namespaces for compilation.
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008046 * TODO: Call xmlPatterncompile with different options for selector/
8047 * field.
8048 */
Daniel Veillard14b56432006-03-09 18:41:40 +00008049 if (attr == NULL)
Daniel Veillardaac7c682006-03-10 13:40:16 +00008050 nsList = NULL;
Daniel Veillard14b56432006-03-09 18:41:40 +00008051 else
8052 nsList = xmlGetNsList(attr->doc, attr->parent);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008053 /*
8054 * Build an array of prefixes and namespaces.
8055 */
8056 if (nsList != NULL) {
8057 int i, count = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008058
8059 for (i = 0; nsList[i] != NULL; i++)
8060 count++;
8061
8062 nsArray = (const xmlChar **) xmlMalloc(
8063 (count * 2 + 1) * sizeof(const xmlChar *));
8064 if (nsArray == NULL) {
8065 xmlSchemaPErrMemory(ctxt, "allocating a namespace array",
8066 NULL);
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00008067 xmlFree(nsList);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008068 return (-1);
8069 }
8070 for (i = 0; i < count; i++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008071 nsArray[2 * i] = nsList[i]->href;
8072 nsArray[2 * i + 1] = nsList[i]->prefix;
8073 }
8074 nsArray[count * 2] = NULL;
8075 xmlFree(nsList);
8076 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008077 /*
8078 * TODO: Differentiate between "selector" and "field".
8079 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008080 if (isField)
8081 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008082 NULL, XML_PATTERN_XSFIELD, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008083 else
8084 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008085 NULL, XML_PATTERN_XSSEL, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008086 if (nsArray != NULL)
8087 xmlFree((xmlChar **) nsArray);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008088
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00008089 if (selector->xpathComp == NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008090 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00008091 /* TODO: Adjust error code? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008092 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008093 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008094 "The XPath expression '%s' could not be "
8095 "compiled", selector->xpath);
8096 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008097 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008098 }
8099 return (0);
8100}
8101
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008102#define ADD_ANNOTATION(annot) \
8103 xmlSchemaAnnotPtr cur = item->annot; \
8104 if (item->annot == NULL) { \
8105 item->annot = annot; \
8106 return (annot); \
8107 } \
8108 cur = item->annot; \
8109 if (cur->next != NULL) { \
8110 cur = cur->next; \
8111 } \
8112 cur->next = annot;
8113
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008114/**
8115 * xmlSchemaAssignAnnotation:
8116 * @item: the schema component
8117 * @annot: the annotation
8118 *
8119 * Adds the annotation to the given schema component.
8120 *
8121 * Returns the given annotaion.
8122 */
8123static xmlSchemaAnnotPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008124xmlSchemaAddAnnotation(xmlSchemaAnnotItemPtr annItem,
8125 xmlSchemaAnnotPtr annot)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008126{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008127 if ((annItem == NULL) || (annot == NULL))
8128 return (NULL);
8129 switch (annItem->type) {
8130 case XML_SCHEMA_TYPE_ELEMENT: {
8131 xmlSchemaElementPtr item = (xmlSchemaElementPtr) annItem;
8132 ADD_ANNOTATION(annot)
8133 }
8134 break;
8135 case XML_SCHEMA_TYPE_ATTRIBUTE: {
8136 xmlSchemaAttributePtr item = (xmlSchemaAttributePtr) annItem;
8137 ADD_ANNOTATION(annot)
8138 }
8139 break;
8140 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
8141 case XML_SCHEMA_TYPE_ANY: {
8142 xmlSchemaWildcardPtr item = (xmlSchemaWildcardPtr) annItem;
8143 ADD_ANNOTATION(annot)
8144 }
8145 break;
8146 case XML_SCHEMA_TYPE_PARTICLE:
8147 case XML_SCHEMA_TYPE_IDC_KEY:
8148 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008149 case XML_SCHEMA_TYPE_IDC_UNIQUE: {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008150 xmlSchemaAnnotItemPtr item = (xmlSchemaAnnotItemPtr) annItem;
8151 ADD_ANNOTATION(annot)
8152 }
8153 break;
8154 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: {
8155 xmlSchemaAttributeGroupPtr item =
8156 (xmlSchemaAttributeGroupPtr) annItem;
8157 ADD_ANNOTATION(annot)
8158 }
8159 break;
8160 case XML_SCHEMA_TYPE_NOTATION: {
8161 xmlSchemaNotationPtr item = (xmlSchemaNotationPtr) annItem;
8162 ADD_ANNOTATION(annot)
8163 }
8164 break;
8165 case XML_SCHEMA_FACET_MININCLUSIVE:
8166 case XML_SCHEMA_FACET_MINEXCLUSIVE:
8167 case XML_SCHEMA_FACET_MAXINCLUSIVE:
8168 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
8169 case XML_SCHEMA_FACET_TOTALDIGITS:
8170 case XML_SCHEMA_FACET_FRACTIONDIGITS:
8171 case XML_SCHEMA_FACET_PATTERN:
8172 case XML_SCHEMA_FACET_ENUMERATION:
8173 case XML_SCHEMA_FACET_WHITESPACE:
8174 case XML_SCHEMA_FACET_LENGTH:
8175 case XML_SCHEMA_FACET_MAXLENGTH:
8176 case XML_SCHEMA_FACET_MINLENGTH: {
8177 xmlSchemaFacetPtr item = (xmlSchemaFacetPtr) annItem;
8178 ADD_ANNOTATION(annot)
8179 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008180 break;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008181 case XML_SCHEMA_TYPE_SIMPLE:
8182 case XML_SCHEMA_TYPE_COMPLEX: {
8183 xmlSchemaTypePtr item = (xmlSchemaTypePtr) annItem;
8184 ADD_ANNOTATION(annot)
8185 }
8186 break;
8187 case XML_SCHEMA_TYPE_GROUP: {
8188 xmlSchemaModelGroupDefPtr item = (xmlSchemaModelGroupDefPtr) annItem;
8189 ADD_ANNOTATION(annot)
8190 }
8191 break;
8192 case XML_SCHEMA_TYPE_SEQUENCE:
8193 case XML_SCHEMA_TYPE_CHOICE:
8194 case XML_SCHEMA_TYPE_ALL: {
8195 xmlSchemaModelGroupPtr item = (xmlSchemaModelGroupPtr) annItem;
8196 ADD_ANNOTATION(annot)
8197 }
8198 break;
8199 default:
8200 xmlSchemaPCustomErr(NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008201 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008202 NULL, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008203 "Internal error: xmlSchemaAddAnnotation, "
8204 "The item is not a annotated schema component", NULL);
8205 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008206 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008207 return (annot);
8208}
8209
8210/**
8211 * xmlSchemaParseIDCSelectorAndField:
8212 * @ctxt: a schema validation context
8213 * @schema: the schema being built
8214 * @node: a subtree containing XML Schema informations
8215 *
8216 * Parses a XML Schema identity-contraint definition's
8217 * <selector> and <field> elements.
8218 *
8219 * Returns the parsed identity-constraint definition.
8220 */
8221static xmlSchemaIDCSelectPtr
Daniel Veillarddee23482008-04-11 12:58:43 +00008222xmlSchemaParseIDCSelectorAndField(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008223 xmlSchemaIDCPtr idc,
8224 xmlNodePtr node,
8225 int isField)
8226{
8227 xmlSchemaIDCSelectPtr item;
8228 xmlNodePtr child = NULL;
8229 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008230
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008231 /*
8232 * Check for illegal attributes.
8233 */
8234 attr = node->properties;
8235 while (attr != NULL) {
8236 if (attr->ns == NULL) {
8237 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8238 (!xmlStrEqual(attr->name, BAD_CAST "xpath"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008239 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008240 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008241 }
8242 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008243 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008244 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008245 }
8246 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008247 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008248 /*
8249 * Create the item.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008250 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008251 item = (xmlSchemaIDCSelectPtr) xmlMalloc(sizeof(xmlSchemaIDCSelect));
8252 if (item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008253 xmlSchemaPErrMemory(ctxt,
8254 "allocating a 'selector' of an identity-constraint definition",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008255 NULL);
8256 return (NULL);
8257 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008258 memset(item, 0, sizeof(xmlSchemaIDCSelect));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008259 /*
8260 * Attribute "xpath" (mandatory).
8261 */
8262 attr = xmlSchemaGetPropNode(node, "xpath");
8263 if (attr == NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +08008264 xmlSchemaPMissingAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008265 XML_SCHEMAP_S4S_ATTR_MISSING,
8266 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008267 "name", NULL);
8268 } else {
8269 item->xpath = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8270 /*
8271 * URGENT TODO: "field"s have an other syntax than "selector"s.
8272 */
8273
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008274 if (xmlSchemaCheckCSelectorXPath(ctxt, idc, item, attr,
8275 isField) == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008276 xmlSchemaPErr(ctxt,
8277 (xmlNodePtr) attr,
8278 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008279 "Internal error: xmlSchemaParseIDCSelectorAndField, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008280 "validating the XPath expression of a IDC selector.\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008281 NULL, NULL);
8282 }
8283
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008284 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008285 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008286 /*
8287 * And now for the children...
8288 */
8289 child = node->children;
8290 if (IS_SCHEMA(child, "annotation")) {
8291 /*
8292 * Add the annotation to the parent IDC.
8293 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008294 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) idc,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008295 xmlSchemaParseAnnotation(ctxt, child, 1));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008296 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008297 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008298 if (child != NULL) {
8299 xmlSchemaPContentErr(ctxt,
8300 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008301 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008302 NULL, "(annotation?)");
8303 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008304
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008305 return (item);
8306}
8307
8308/**
8309 * xmlSchemaParseIDC:
8310 * @ctxt: a schema validation context
8311 * @schema: the schema being built
8312 * @node: a subtree containing XML Schema informations
8313 *
8314 * Parses a XML Schema identity-contraint definition.
8315 *
8316 * Returns the parsed identity-constraint definition.
8317 */
8318static xmlSchemaIDCPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008319xmlSchemaParseIDC(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008320 xmlSchemaPtr schema,
8321 xmlNodePtr node,
8322 xmlSchemaTypeType idcCategory,
8323 const xmlChar *targetNamespace)
8324{
8325 xmlSchemaIDCPtr item = NULL;
8326 xmlNodePtr child = NULL;
8327 xmlAttrPtr attr;
8328 const xmlChar *name = NULL;
8329 xmlSchemaIDCSelectPtr field = NULL, lastField = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008330
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008331 /*
8332 * Check for illegal attributes.
8333 */
8334 attr = node->properties;
8335 while (attr != NULL) {
8336 if (attr->ns == NULL) {
8337 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8338 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8339 ((idcCategory != XML_SCHEMA_TYPE_IDC_KEYREF) ||
8340 (!xmlStrEqual(attr->name, BAD_CAST "refer")))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008341 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008342 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008343 }
8344 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008345 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008346 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008347 }
8348 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008349 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008350 /*
8351 * Attribute "name" (mandatory).
8352 */
8353 attr = xmlSchemaGetPropNode(node, "name");
8354 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008355 xmlSchemaPMissingAttrErr(ctxt,
8356 XML_SCHEMAP_S4S_ATTR_MISSING,
8357 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008358 "name", NULL);
8359 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008360 } else if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008361 NULL, attr,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008362 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
8363 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008364 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008365 /* Create the component. */
8366 item = xmlSchemaAddIDC(ctxt, schema, name, targetNamespace,
8367 idcCategory, node);
8368 if (item == NULL)
8369 return(NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008370
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008371 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008372 if (idcCategory == XML_SCHEMA_TYPE_IDC_KEYREF) {
8373 /*
8374 * Attribute "refer" (mandatory).
8375 */
8376 attr = xmlSchemaGetPropNode(node, "refer");
8377 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008378 xmlSchemaPMissingAttrErr(ctxt,
8379 XML_SCHEMAP_S4S_ATTR_MISSING,
8380 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008381 "refer", NULL);
8382 } else {
8383 /*
8384 * Create a reference item.
8385 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008386 item->ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_IDC_KEY,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008387 NULL, NULL);
8388 if (item->ref == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008389 return (NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008390 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008391 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008392 &(item->ref->targetNamespace),
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008393 &(item->ref->name));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008394 xmlSchemaCheckReference(ctxt, schema, node, attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008395 item->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008396 }
8397 }
8398 /*
8399 * And now for the children...
8400 */
8401 child = node->children;
8402 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008403 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008404 child = child->next;
8405 }
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008406 if (child == NULL) {
8407 xmlSchemaPContentErr(ctxt,
8408 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008409 NULL, node, child,
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008410 "A child element is missing",
8411 "(annotation?, (selector, field+))");
8412 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008413 /*
8414 * Child element <selector>.
8415 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008416 if (IS_SCHEMA(child, "selector")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008417 item->selector = xmlSchemaParseIDCSelectorAndField(ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008418 item, child, 0);
8419 child = child->next;
8420 /*
8421 * Child elements <field>.
8422 */
8423 if (IS_SCHEMA(child, "field")) {
8424 do {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008425 field = xmlSchemaParseIDCSelectorAndField(ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008426 item, child, 1);
8427 if (field != NULL) {
8428 field->index = item->nbFields;
8429 item->nbFields++;
8430 if (lastField != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008431 lastField->next = field;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008432 else
8433 item->fields = field;
8434 lastField = field;
8435 }
8436 child = child->next;
8437 } while (IS_SCHEMA(child, "field"));
8438 } else {
8439 xmlSchemaPContentErr(ctxt,
8440 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008441 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008442 NULL, "(annotation?, (selector, field+))");
8443 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008444 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008445 if (child != NULL) {
8446 xmlSchemaPContentErr(ctxt,
8447 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008448 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008449 NULL, "(annotation?, (selector, field+))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008450 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008451
8452 return (item);
8453}
8454
Daniel Veillardc0826a72004-08-10 14:17:33 +00008455/**
Daniel Veillard4255d502002-04-16 15:50:10 +00008456 * xmlSchemaParseElement:
8457 * @ctxt: a schema validation context
8458 * @schema: the schema being built
8459 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008460 * @topLevel: indicates if this is global declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00008461 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008462 * Parses a XML schema element declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00008463 * *WARNING* this interface is highly subject to change
8464 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008465 * Returns the element declaration or a particle; NULL in case
8466 * of an error or if the particle has minOccurs==maxOccurs==0.
Daniel Veillard4255d502002-04-16 15:50:10 +00008467 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008468static xmlSchemaBasicItemPtr
Daniel Veillard4255d502002-04-16 15:50:10 +00008469xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008470 xmlNodePtr node, int *isElemRef, int topLevel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008471{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008472 xmlSchemaElementPtr decl = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008473 xmlSchemaParticlePtr particle = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008474 xmlSchemaAnnotPtr annot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008475 xmlNodePtr child = NULL;
8476 xmlAttrPtr attr, nameAttr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008477 int min, max, isRef = 0;
8478 xmlChar *des = NULL;
William M. Bracke7091952004-05-11 15:09:58 +00008479
8480 /* 3.3.3 Constraints on XML Representations of Element Declarations */
8481 /* TODO: Complete implementation of 3.3.6 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008482
Daniel Veillard4255d502002-04-16 15:50:10 +00008483 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008484 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008485
8486 if (isElemRef != NULL)
8487 *isElemRef = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008488 /*
8489 * If we get a "ref" attribute on a local <element> we will assume it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008490 * a reference - even if there's a "name" attribute; this seems to be more
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008491 * robust.
8492 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008493 nameAttr = xmlSchemaGetPropNode(node, "name");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008494 attr = xmlSchemaGetPropNode(node, "ref");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008495 if ((topLevel) || (attr == NULL)) {
8496 if (nameAttr == NULL) {
8497 xmlSchemaPMissingAttrErr(ctxt,
8498 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008499 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008500 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008501 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008502 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00008503 isRef = 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008504
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008505 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008506 child = node->children;
8507 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008508 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008509 child = child->next;
8510 }
8511 /*
8512 * Skip particle part if a global declaration.
8513 */
8514 if (topLevel)
8515 goto declaration_part;
8516 /*
8517 * The particle part ==================================================
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008518 */
8519 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
8520 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1, "(xs:nonNegativeInteger | unbounded)");
8521 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00008522 particle = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008523 if (particle == NULL)
8524 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008525
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008526 /* ret->flags |= XML_SCHEMAS_ELEM_REF; */
8527
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008528 if (isRef) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008529 const xmlChar *refNs = NULL, *ref = NULL;
8530 xmlSchemaQNameRefPtr refer = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008531 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008532 * The reference part =============================================
Daniel Veillardc0826a72004-08-10 14:17:33 +00008533 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008534 if (isElemRef != NULL)
8535 *isElemRef = 1;
8536
Daniel Veillardc0826a72004-08-10 14:17:33 +00008537 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008538 NULL, attr, &refNs, &ref);
8539 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008540 /*
8541 * SPEC (3.3.3 : 2.1) "One of ref or name must be present, but not both"
Daniel Veillardc0826a72004-08-10 14:17:33 +00008542 */
8543 if (nameAttr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008544 xmlSchemaPMutualExclAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008545 XML_SCHEMAP_SRC_ELEMENT_2_1, NULL, nameAttr, "ref", "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008546 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008547 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008548 * Check for illegal attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008549 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008550 attr = node->properties;
8551 while (attr != NULL) {
8552 if (attr->ns == NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008553 if (xmlStrEqual(attr->name, BAD_CAST "ref") ||
8554 xmlStrEqual(attr->name, BAD_CAST "name") ||
8555 xmlStrEqual(attr->name, BAD_CAST "id") ||
8556 xmlStrEqual(attr->name, BAD_CAST "maxOccurs") ||
8557 xmlStrEqual(attr->name, BAD_CAST "minOccurs"))
8558 {
8559 attr = attr->next;
8560 continue;
8561 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008562 /* SPEC (3.3.3 : 2.2) */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008563 xmlSchemaPCustomAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008564 XML_SCHEMAP_SRC_ELEMENT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008565 NULL, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008566 "Only the attributes 'minOccurs', 'maxOccurs' and "
8567 "'id' are allowed in addition to 'ref'");
8568 break;
8569 }
8570 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
8571 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008572 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008573 }
8574 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008575 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008576 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008577 * No children except <annotation> expected.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008578 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008579 if (child != NULL) {
8580 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008581 NULL, node, child, NULL, "(annotation?)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008582 }
8583 if ((min == 0) && (max == 0))
8584 goto return_null;
8585 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008586 * Create the reference item and attach it to the particle.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008587 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008588 refer = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_ELEMENT,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008589 ref, refNs);
8590 if (refer == NULL)
8591 goto return_null;
8592 particle->children = (xmlSchemaTreeItemPtr) refer;
8593 particle->annot = annot;
8594 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008595 * Add the particle to pending components, since the reference
8596 * need to be resolved.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008597 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008598 WXS_ADD_PENDING(ctxt, particle);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008599 return ((xmlSchemaBasicItemPtr) particle);
8600 }
8601 /*
8602 * The declaration part ===============================================
8603 */
8604declaration_part:
8605 {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008606 const xmlChar *ns = NULL, *fixed, *name, *attrValue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008607 xmlSchemaIDCPtr curIDC = NULL, lastIDC = NULL;
8608
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008609 if (xmlSchemaPValAttrNode(ctxt, NULL, nameAttr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008610 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008611 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008612 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008613 * Evaluate the target namespace.
8614 */
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008615 if (topLevel) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008616 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008617 } else {
8618 attr = xmlSchemaGetPropNode(node, "form");
8619 if (attr != NULL) {
8620 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8621 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008622 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008623 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008624 xmlSchemaPSimpleTypeErr(ctxt,
8625 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
8626 NULL, (xmlNodePtr) attr,
8627 NULL, "(qualified | unqualified)",
8628 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008629 }
8630 } else if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008631 ns = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008632 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008633 decl = xmlSchemaAddElement(ctxt, name, ns, node, topLevel);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008634 if (decl == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008635 goto return_null;
William M. Bracke7091952004-05-11 15:09:58 +00008636 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008637 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008638 * Check for illegal attributes.
8639 */
William M. Bracke7091952004-05-11 15:09:58 +00008640 attr = node->properties;
8641 while (attr != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008642 if (attr->ns == NULL) {
8643 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8644 (!xmlStrEqual(attr->name, BAD_CAST "type")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008645 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008646 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008647 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008648 (!xmlStrEqual(attr->name, BAD_CAST "block")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008649 (!xmlStrEqual(attr->name, BAD_CAST "nillable")))
8650 {
8651 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008652 if ((!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008653 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008654 (!xmlStrEqual(attr->name, BAD_CAST "form")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008655 {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008656 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008657 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008658 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008659 } else if ((!xmlStrEqual(attr->name, BAD_CAST "final")) &&
8660 (!xmlStrEqual(attr->name, BAD_CAST "abstract")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008661 (!xmlStrEqual(attr->name, BAD_CAST "substitutionGroup"))) {
8662
8663 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008664 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008665 }
8666 }
8667 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008668
Daniel Veillardc0826a72004-08-10 14:17:33 +00008669 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008670 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Bracke7091952004-05-11 15:09:58 +00008671 }
8672 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008673 }
William M. Bracke7091952004-05-11 15:09:58 +00008674 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008675 * Extract/validate attributes.
8676 */
8677 if (topLevel) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008678 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008679 * Process top attributes of global element declarations here.
8680 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008681 decl->flags |= XML_SCHEMAS_ELEM_GLOBAL;
8682 decl->flags |= XML_SCHEMAS_ELEM_TOPLEVEL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008683 xmlSchemaPValAttrQName(ctxt, schema,
8684 NULL, node, "substitutionGroup",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008685 &(decl->substGroupNs), &(decl->substGroup));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008686 if (xmlGetBooleanProp(ctxt, node, "abstract", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008687 decl->flags |= XML_SCHEMAS_ELEM_ABSTRACT;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008688 /*
8689 * Attribute "final".
8690 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008691 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008692 if (attr == NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008693 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
8694 decl->flags |= XML_SCHEMAS_ELEM_FINAL_EXTENSION;
8695 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
8696 decl->flags |= XML_SCHEMAS_ELEM_FINAL_RESTRICTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008697 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008698 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8699 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008700 -1,
8701 XML_SCHEMAS_ELEM_FINAL_EXTENSION,
8702 XML_SCHEMAS_ELEM_FINAL_RESTRICTION, -1, -1, -1) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008703 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008704 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008705 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008706 NULL, "(#all | List of (extension | restriction))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00008707 attrValue, NULL, NULL, NULL);
8708 }
8709 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008710 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008711 /*
8712 * Attribute "block".
8713 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008714 attr = xmlSchemaGetPropNode(node, "block");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008715 if (attr == NULL) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00008716 /*
8717 * Apply default "block" values.
8718 */
8719 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
8720 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_RESTRICTION;
8721 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
8722 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_EXTENSION;
8723 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
8724 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008725 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008726 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8727 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008728 -1,
8729 XML_SCHEMAS_ELEM_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008730 XML_SCHEMAS_ELEM_BLOCK_RESTRICTION,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008731 XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION, -1, -1) != 0) {
8732 xmlSchemaPSimpleTypeErr(ctxt,
8733 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008734 NULL, (xmlNodePtr) attr,
William M. Brack2f2a6632004-08-20 23:09:47 +00008735 NULL, "(#all | List of (extension | "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008736 "restriction | substitution))", attrValue,
8737 NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008738 }
8739 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008740 if (xmlGetBooleanProp(ctxt, node, "nillable", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008741 decl->flags |= XML_SCHEMAS_ELEM_NILLABLE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008742
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008743 attr = xmlSchemaGetPropNode(node, "type");
8744 if (attr != NULL) {
8745 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008746 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008747 &(decl->namedTypeNs), &(decl->namedType));
8748 xmlSchemaCheckReference(ctxt, schema, node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008749 attr, decl->namedTypeNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008750 }
8751 decl->value = xmlSchemaGetProp(ctxt, node, "default");
8752 attr = xmlSchemaGetPropNode(node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008753 if (attr != NULL) {
8754 fixed = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008755 if (decl->value != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008756 /*
8757 * 3.3.3 : 1
8758 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008759 */
8760 xmlSchemaPMutualExclAttrErr(ctxt,
8761 XML_SCHEMAP_SRC_ELEMENT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008762 NULL, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008763 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008764 decl->flags |= XML_SCHEMAS_ELEM_FIXED;
8765 decl->value = fixed;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008766 }
William M. Bracke7091952004-05-11 15:09:58 +00008767 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008768 /*
8769 * And now for the children...
8770 */
William M. Bracke7091952004-05-11 15:09:58 +00008771 if (IS_SCHEMA(child, "complexType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008772 /*
8773 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008774 * "type" and either <simpleType> or <complexType> are mutually
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008775 * exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008776 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008777 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008778 xmlSchemaPContentErr(ctxt,
8779 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008780 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008781 "The attribute 'type' and the <complexType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008782 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008783 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008784 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseComplexType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008785 child = child->next;
8786 } else if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008787 /*
8788 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008789 * "type" and either <simpleType> or <complexType> are
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008790 * mutually exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008791 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008792 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008793 xmlSchemaPContentErr(ctxt,
8794 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008795 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008796 "The attribute 'type' and the <simpleType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008797 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008798 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008799 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008800 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008801 }
William M. Bracke7091952004-05-11 15:09:58 +00008802 while ((IS_SCHEMA(child, "unique")) ||
Daniel Veillardc0826a72004-08-10 14:17:33 +00008803 (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008804 if (IS_SCHEMA(child, "unique")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008805 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008806 XML_SCHEMA_TYPE_IDC_UNIQUE, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008807 } else if (IS_SCHEMA(child, "key")) {
8808 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008809 XML_SCHEMA_TYPE_IDC_KEY, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008810 } else if (IS_SCHEMA(child, "keyref")) {
8811 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008812 XML_SCHEMA_TYPE_IDC_KEYREF, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008813 }
8814 if (lastIDC != NULL)
8815 lastIDC->next = curIDC;
8816 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008817 decl->idcs = (void *) curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008818 lastIDC = curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008819 child = child->next;
William M. Bracke7091952004-05-11 15:09:58 +00008820 }
8821 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008822 xmlSchemaPContentErr(ctxt,
8823 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008824 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008825 NULL, "(annotation?, ((simpleType | complexType)?, "
8826 "(unique | key | keyref)*))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008827 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008828 decl->annot = annot;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008829 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008830 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008831 * NOTE: Element Declaration Representation OK 4. will be checked at a
Daniel Veillardc0826a72004-08-10 14:17:33 +00008832 * different layer.
8833 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008834 FREE_AND_NULL(des)
8835 if (topLevel)
8836 return ((xmlSchemaBasicItemPtr) decl);
8837 else {
8838 particle->children = (xmlSchemaTreeItemPtr) decl;
8839 return ((xmlSchemaBasicItemPtr) particle);
8840 }
8841
8842return_null:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008843 FREE_AND_NULL(des);
8844 if (annot != NULL) {
8845 if (particle != NULL)
8846 particle->annot = NULL;
8847 if (decl != NULL)
8848 decl->annot = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008849 xmlSchemaFreeAnnot(annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008850 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008851 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00008852}
8853
8854/**
8855 * xmlSchemaParseUnion:
8856 * @ctxt: a schema validation context
8857 * @schema: the schema being built
8858 * @node: a subtree containing XML Schema informations
8859 *
8860 * parse a XML schema Union definition
8861 * *WARNING* this interface is highly subject to change
8862 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008863 * Returns -1 in case of internal error, 0 in case of success and a positive
8864 * error code otherwise.
Daniel Veillard4255d502002-04-16 15:50:10 +00008865 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008866static int
Daniel Veillard4255d502002-04-16 15:50:10 +00008867xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008868 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008869{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008870 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008871 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008872 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008873 const xmlChar *cur = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00008874
8875 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008876 return (-1);
8877 /* Not a component, don't create it. */
8878 type = ctxt->ctxtType;
8879 /*
8880 * Mark the simple type as being of variety "union".
8881 */
8882 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008883 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008884 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
Jan Pokorný761c9e92013-11-29 23:26:27 +01008885 * then the `simple ur-type definition`."
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008886 */
8887 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008888 /*
8889 * Check for illegal attributes.
8890 */
8891 attr = node->properties;
8892 while (attr != NULL) {
8893 if (attr->ns == NULL) {
8894 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8895 (!xmlStrEqual(attr->name, BAD_CAST "memberTypes"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008896 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008897 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008898 }
8899 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008900 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008901 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008902 }
8903 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008904 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008905 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +00008906 /*
8907 * Attribute "memberTypes". This is a list of QNames.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008908 * TODO: Check the value to contain anything.
William M. Brack2f2a6632004-08-20 23:09:47 +00008909 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008910 attr = xmlSchemaGetPropNode(node, "memberTypes");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008911 if (attr != NULL) {
8912 const xmlChar *end;
8913 xmlChar *tmp;
8914 const xmlChar *localName, *nsName;
8915 xmlSchemaTypeLinkPtr link, lastLink = NULL;
8916 xmlSchemaQNameRefPtr ref;
8917
8918 cur = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008919 type->base = cur;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008920 do {
8921 while (IS_BLANK_CH(*cur))
8922 cur++;
8923 end = cur;
8924 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
8925 end++;
8926 if (end == cur)
8927 break;
8928 tmp = xmlStrndup(cur, end - cur);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008929 if (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008930 NULL, attr, BAD_CAST tmp, &nsName, &localName) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008931 /*
8932 * Create the member type link.
8933 */
8934 link = (xmlSchemaTypeLinkPtr)
8935 xmlMalloc(sizeof(xmlSchemaTypeLink));
8936 if (link == NULL) {
8937 xmlSchemaPErrMemory(ctxt, "xmlSchemaParseUnion, "
8938 "allocating a type link", NULL);
8939 return (-1);
8940 }
8941 link->type = NULL;
8942 link->next = NULL;
8943 if (lastLink == NULL)
8944 type->memberTypes = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008945 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008946 lastLink->next = link;
8947 lastLink = link;
8948 /*
8949 * Create a reference item.
8950 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008951 ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_SIMPLE,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008952 localName, nsName);
8953 if (ref == NULL) {
8954 FREE_AND_NULL(tmp)
8955 return (-1);
8956 }
8957 /*
8958 * Assign the reference to the link, it will be resolved
8959 * later during fixup of the union simple type.
8960 */
8961 link->type = (xmlSchemaTypePtr) ref;
8962 }
8963 FREE_AND_NULL(tmp)
8964 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008965 } while (*cur != 0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008966
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008967 }
William M. Brack2f2a6632004-08-20 23:09:47 +00008968 /*
8969 * And now for the children...
8970 */
Daniel Veillard4255d502002-04-16 15:50:10 +00008971 child = node->children;
8972 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008973 /*
8974 * Add the annotation to the simple type ancestor.
8975 */
8976 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00008977 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008978 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00008979 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008980 if (IS_SCHEMA(child, "simpleType")) {
8981 xmlSchemaTypePtr subtype, last = NULL;
8982
8983 /*
8984 * Anchor the member types in the "subtypes" field of the
8985 * simple type.
8986 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008987 while (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008988 subtype = (xmlSchemaTypePtr)
8989 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
8990 if (subtype != NULL) {
8991 if (last == NULL) {
8992 type->subtypes = subtype;
8993 last = subtype;
8994 } else {
8995 last->next = subtype;
8996 last = subtype;
8997 }
8998 last->next = NULL;
8999 }
9000 child = child->next;
9001 }
Daniel Veillard4255d502002-04-16 15:50:10 +00009002 }
9003 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009004 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009005 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009006 NULL, node, child, NULL, "(annotation?, simpleType*)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009007 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009008 if ((attr == NULL) && (type->subtypes == NULL)) {
9009 /*
9010 * src-union-memberTypes-or-simpleTypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009011 * Either the memberTypes [attribute] of the <union> element must
9012 * be non-empty or there must be at least one simpleType [child].
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009013 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009014 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009015 XML_SCHEMAP_SRC_UNION_MEMBERTYPES_OR_SIMPLETYPES,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009016 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009017 "Either the attribute 'memberTypes' or "
9018 "at least one <simpleType> child must be present", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009019 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009020 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00009021}
9022
9023/**
9024 * xmlSchemaParseList:
9025 * @ctxt: a schema validation context
9026 * @schema: the schema being built
9027 * @node: a subtree containing XML Schema informations
9028 *
9029 * parse a XML schema List definition
9030 * *WARNING* this interface is highly subject to change
9031 *
William M. Bracke7091952004-05-11 15:09:58 +00009032 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00009033 * 1 in case of success.
9034 */
9035static xmlSchemaTypePtr
9036xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009037 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00009038{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009039 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00009040 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00009041 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00009042
9043 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9044 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009045 /* Not a component, don't create it. */
9046 type = ctxt->ctxtType;
9047 /*
9048 * Mark the type as being of variety "list".
9049 */
9050 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009051 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00009052 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
Jan Pokorný761c9e92013-11-29 23:26:27 +01009053 * then the `simple ur-type definition`."
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00009054 */
9055 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00009056 /*
9057 * Check for illegal attributes.
9058 */
9059 attr = node->properties;
9060 while (attr != NULL) {
9061 if (attr->ns == NULL) {
9062 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9063 (!xmlStrEqual(attr->name, BAD_CAST "itemType"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009064 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009065 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009066 }
9067 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009068 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009069 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009070 }
9071 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009072 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009073
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009074 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009075
William M. Brack2f2a6632004-08-20 23:09:47 +00009076 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009077 * Attribute "itemType". NOTE that we will use the "ref" and "refNs"
9078 * fields for holding the reference to the itemType.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009079 *
9080 * REVAMP TODO: Use the "base" and "baseNs" fields, since we will remove
9081 * the "ref" fields.
William M. Brack2f2a6632004-08-20 23:09:47 +00009082 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009083 xmlSchemaPValAttrQName(ctxt, schema, NULL,
9084 node, "itemType", &(type->baseNs), &(type->base));
William M. Brack2f2a6632004-08-20 23:09:47 +00009085 /*
9086 * And now for the children...
9087 */
Daniel Veillard4255d502002-04-16 15:50:10 +00009088 child = node->children;
9089 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009090 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009091 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009092 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009093 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009094 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009095 /*
9096 * src-list-itemType-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009097 * Either the itemType [attribute] or the <simpleType> [child] of
9098 * the <list> element must be present, but not both.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009099 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009100 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009101 xmlSchemaPCustomErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009102 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009103 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +00009104 "The attribute 'itemType' and the <simpleType> child "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009105 "are mutually exclusive", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009106 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009107 type->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Brack2f2a6632004-08-20 23:09:47 +00009108 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009109 child = child->next;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009110 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009111 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009112 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009113 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009114 "Either the attribute 'itemType' or the <simpleType> child "
9115 "must be present", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009116 }
9117 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009118 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009119 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009120 NULL, node, child, NULL, "(annotation?, simpleType?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00009121 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009122 if ((type->base == NULL) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009123 (type->subtypes == NULL) &&
9124 (xmlSchemaGetPropNode(node, "itemType") == NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009125 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009126 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009127 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009128 "Either the attribute 'itemType' or the <simpleType> child "
9129 "must be present", NULL);
9130 }
9131 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009132}
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009133
Daniel Veillard4255d502002-04-16 15:50:10 +00009134/**
9135 * xmlSchemaParseSimpleType:
9136 * @ctxt: a schema validation context
9137 * @schema: the schema being built
9138 * @node: a subtree containing XML Schema informations
9139 *
9140 * parse a XML schema Simple Type definition
9141 * *WARNING* this interface is highly subject to change
9142 *
William M. Bracke7091952004-05-11 15:09:58 +00009143 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillardc0826a72004-08-10 14:17:33 +00009144 * 1 in case of success.
Daniel Veillard4255d502002-04-16 15:50:10 +00009145 */
9146static xmlSchemaTypePtr
9147xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00009148 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00009149{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009150 xmlSchemaTypePtr type, oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009151 xmlNodePtr child = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009152 const xmlChar *attrValue = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009153 xmlAttrPtr attr;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009154 int hasRestriction = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00009155
9156 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9157 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009158
Daniel Veillardc0826a72004-08-10 14:17:33 +00009159 if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009160 attr = xmlSchemaGetPropNode(node, "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009161 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009162 xmlSchemaPMissingAttrErr(ctxt,
9163 XML_SCHEMAP_S4S_ATTR_MISSING,
9164 NULL, node,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009165 "name", NULL);
9166 return (NULL);
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009167 } else {
9168 if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009169 NULL, attr,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009170 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0)
9171 return (NULL);
9172 /*
9173 * Skip built-in types.
9174 */
9175 if (ctxt->isS4S) {
9176 xmlSchemaTypePtr biType;
9177
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009178 if (ctxt->isRedefine) {
9179 /*
9180 * REDEFINE: Disallow redefinition of built-in-types.
9181 * TODO: It seems that the spec does not say anything
9182 * about this case.
9183 */
9184 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009185 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009186 "Redefinition of built-in simple types is not "
9187 "supported", NULL);
9188 return(NULL);
9189 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009190 biType = xmlSchemaGetPredefinedType(attrValue, xmlSchemaNs);
9191 if (biType != NULL)
9192 return (biType);
9193 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00009194 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009195 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009196 /*
9197 * TargetNamespace:
Jan Pokorný761c9e92013-11-29 23:26:27 +01009198 * SPEC "The `actual value` of the targetNamespace [attribute]
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009199 * of the <schema> ancestor element information item if present,
Jan Pokorný761c9e92013-11-29 23:26:27 +01009200 * otherwise `absent`.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009201 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00009202 if (topLevel == 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009203#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009204 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009205#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +00009206 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00009207 * Parse as local simple type definition.
Daniel Veillard01fa6152004-06-29 17:04:39 +00009208 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009209#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00009210 snprintf(buf, 39, "#ST%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009211 type = xmlSchemaAddType(ctxt, schema,
9212 XML_SCHEMA_TYPE_SIMPLE,
9213 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009214 ctxt->targetNamespace, node, 0);
9215#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009216 type = xmlSchemaAddType(ctxt, schema,
9217 XML_SCHEMA_TYPE_SIMPLE,
9218 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009219#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +00009220 if (type == NULL)
9221 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009222 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009223 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009224 /*
9225 * Check for illegal attributes.
9226 */
9227 attr = node->properties;
9228 while (attr != NULL) {
9229 if (attr->ns == NULL) {
9230 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009231 xmlSchemaPIllegalAttrErr(ctxt,
Daniel Veillarddee23482008-04-11 12:58:43 +00009232 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillard01fa6152004-06-29 17:04:39 +00009233 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009234 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009235 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009236 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009237 }
9238 attr = attr->next;
Daniel Veillard01fa6152004-06-29 17:04:39 +00009239 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009240 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00009241 /*
9242 * Parse as global simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009243 *
9244 * Note that attrValue is the value of the attribute "name" here.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009245 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009246 type = xmlSchemaAddType(ctxt, schema, XML_SCHEMA_TYPE_SIMPLE,
9247 attrValue, ctxt->targetNamespace, node, 1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009248 if (type == NULL)
9249 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009250 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009251 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009252 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
9253 /*
9254 * Check for illegal attributes.
9255 */
9256 attr = node->properties;
9257 while (attr != NULL) {
9258 if (attr->ns == NULL) {
9259 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9260 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009261 (!xmlStrEqual(attr->name, BAD_CAST "final"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009262 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009263 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009264 }
9265 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009266 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009267 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009268 }
9269 attr = attr->next;
9270 }
9271 /*
9272 * Attribute "final".
9273 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009274 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009275 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009276 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9277 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
9278 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9279 type->flags |= XML_SCHEMAS_TYPE_FINAL_LIST;
9280 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9281 type->flags |= XML_SCHEMAS_TYPE_FINAL_UNION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009282 } else {
9283 attrValue = xmlSchemaGetProp(ctxt, node, "final");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009284 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
9285 -1, -1, XML_SCHEMAS_TYPE_FINAL_RESTRICTION, -1,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009286 XML_SCHEMAS_TYPE_FINAL_LIST,
9287 XML_SCHEMAS_TYPE_FINAL_UNION) != 0) {
9288
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009289 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009290 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009291 WXS_BASIC_CAST type, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009292 NULL, "(#all | List of (list | union | restriction)",
Daniel Veillardc0826a72004-08-10 14:17:33 +00009293 attrValue, NULL, NULL, NULL);
9294 }
9295 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009296 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009297 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009298 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009299 /*
9300 * And now for the children...
9301 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009302 oldCtxtType = ctxt->ctxtType;
Daniel Veillarddee23482008-04-11 12:58:43 +00009303
Daniel Veillardc0826a72004-08-10 14:17:33 +00009304 ctxt->ctxtType = type;
Daniel Veillarddee23482008-04-11 12:58:43 +00009305
Daniel Veillard4255d502002-04-16 15:50:10 +00009306 child = node->children;
9307 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009308 type->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009309 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009310 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009311 if (child == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009312 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009313 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009314 "(annotation?, (restriction | list | union))");
Daniel Veillarddee23482008-04-11 12:58:43 +00009315 } else if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009316 xmlSchemaParseRestriction(ctxt, schema, child,
Daniel Veillarddee23482008-04-11 12:58:43 +00009317 XML_SCHEMA_TYPE_SIMPLE);
9318 hasRestriction = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009319 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009320 } else if (IS_SCHEMA(child, "list")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009321 xmlSchemaParseList(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009322 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009323 } else if (IS_SCHEMA(child, "union")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009324 xmlSchemaParseUnion(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009325 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009326 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009327 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009328 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009329 NULL, node, child, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009330 "(annotation?, (restriction | list | union))");
Daniel Veillard4255d502002-04-16 15:50:10 +00009331 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009332 /*
9333 * REDEFINE: SPEC src-redefine (5)
9334 * "Within the [children], each <simpleType> must have a
Jan Pokorný761c9e92013-11-29 23:26:27 +01009335 * <restriction> among its [children] ... the `actual value` of whose
9336 * base [attribute] must be the same as the `actual value` of its own
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009337 * name attribute plus target namespace;"
9338 */
9339 if (topLevel && ctxt->isRedefine && (! hasRestriction)) {
9340 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009341 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009342 "<simpleType> must have a <restriction> child", NULL);
9343 }
Daniel Veillarddee23482008-04-11 12:58:43 +00009344
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009345 ctxt->ctxtType = oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009346 return (type);
9347}
9348
Daniel Veillard4255d502002-04-16 15:50:10 +00009349/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009350 * xmlSchemaParseModelGroupDefRef:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009351 * @ctxt: the parser context
9352 * @schema: the schema being built
9353 * @node: the node
Daniel Veillard4255d502002-04-16 15:50:10 +00009354 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009355 * Parses a reference to a model group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00009356 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009357 * We will return a particle component with a qname-component or
9358 * NULL in case of an error.
Daniel Veillard4255d502002-04-16 15:50:10 +00009359 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009360static xmlSchemaTreeItemPtr
9361xmlSchemaParseModelGroupDefRef(xmlSchemaParserCtxtPtr ctxt,
9362 xmlSchemaPtr schema,
9363 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00009364{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009365 xmlSchemaParticlePtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009366 xmlNodePtr child = NULL;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009367 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009368 const xmlChar *ref = NULL, *refNs = NULL;
9369 int min, max;
9370
9371 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009372 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009373
9374 attr = xmlSchemaGetPropNode(node, "ref");
9375 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009376 xmlSchemaPMissingAttrErr(ctxt,
9377 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009378 NULL, node, "ref", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009379 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009380 } else if (xmlSchemaPValAttrNodeQName(ctxt, schema, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009381 attr, &refNs, &ref) != 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009382 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009383 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009384 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009385 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009386 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009387 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009388 /*
9389 * Check for illegal attributes.
9390 */
9391 attr = node->properties;
9392 while (attr != NULL) {
9393 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009394 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009395 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9396 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
9397 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009398 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009399 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009400 }
9401 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009402 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009403 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009404 }
9405 attr = attr->next;
9406 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009407 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +00009408 item = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009409 if (item == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +00009410 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009411 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009412 * Create a qname-reference and set as the term; it will be substituted
9413 * for the model group after the reference has been resolved.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009414 */
9415 item->children = (xmlSchemaTreeItemPtr)
Daniel Veillarddee23482008-04-11 12:58:43 +00009416 xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_GROUP, ref, refNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009417 xmlSchemaPCheckParticleCorrect_2(ctxt, item, node, min, max);
9418 /*
9419 * And now for the children...
9420 */
9421 child = node->children;
9422 /* TODO: Is annotation even allowed for a model group reference? */
9423 if (IS_SCHEMA(child, "annotation")) {
9424 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009425 * TODO: What to do exactly with the annotation?
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009426 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009427 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009428 child = child->next;
9429 }
9430 if (child != NULL) {
9431 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009432 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009433 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009434 "(annotation?)");
9435 }
9436 /*
9437 * Corresponds to no component at all if minOccurs==maxOccurs==0.
9438 */
9439 if ((min == 0) && (max == 0))
9440 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009441
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009442 return ((xmlSchemaTreeItemPtr) item);
9443}
9444
9445/**
9446 * xmlSchemaParseModelGroupDefinition:
9447 * @ctxt: a schema validation context
9448 * @schema: the schema being built
9449 * @node: a subtree containing XML Schema informations
9450 *
9451 * Parses a XML schema model group definition.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009452 *
9453 * Note that the contraint src-redefine (6.2) can't be applied until
9454 * references have been resolved. So we will do this at the
9455 * component fixup level.
Daniel Veillarddee23482008-04-11 12:58:43 +00009456 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009457 * *WARNING* this interface is highly subject to change
9458 *
9459 * Returns -1 in case of error, 0 if the declaration is improper and
9460 * 1 in case of success.
9461 */
9462static xmlSchemaModelGroupDefPtr
9463xmlSchemaParseModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
9464 xmlSchemaPtr schema,
9465 xmlNodePtr node)
9466{
9467 xmlSchemaModelGroupDefPtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009468 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009469 xmlAttrPtr attr;
9470 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00009471
9472 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009473 return (NULL);
9474
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009475 attr = xmlSchemaGetPropNode(node, "name");
9476 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009477 xmlSchemaPMissingAttrErr(ctxt,
9478 XML_SCHEMAP_S4S_ATTR_MISSING,
9479 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009480 "name", NULL);
9481 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009482 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009483 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
9484 return (NULL);
9485 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009486 item = xmlSchemaAddModelGroupDefinition(ctxt, schema, name,
9487 ctxt->targetNamespace, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009488 if (item == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009489 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009490 /*
9491 * Check for illegal attributes.
9492 */
9493 attr = node->properties;
9494 while (attr != NULL) {
9495 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009496 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009497 (!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009498 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009499 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009500 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009501 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009502 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009503 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009504 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009505 attr = attr->next;
9506 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009507 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009508 /*
9509 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009510 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009511 child = node->children;
9512 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009513 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009514 child = child->next;
9515 }
9516 if (IS_SCHEMA(child, "all")) {
9517 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9518 XML_SCHEMA_TYPE_ALL, 0);
9519 child = child->next;
9520 } else if (IS_SCHEMA(child, "choice")) {
9521 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9522 XML_SCHEMA_TYPE_CHOICE, 0);
9523 child = child->next;
9524 } else if (IS_SCHEMA(child, "sequence")) {
9525 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9526 XML_SCHEMA_TYPE_SEQUENCE, 0);
9527 child = child->next;
9528 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009529
Daniel Veillarddee23482008-04-11 12:58:43 +00009530
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009531
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009532 if (child != NULL) {
9533 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009534 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009535 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009536 "(annotation?, (all | choice | sequence)?)");
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009537 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009538 return (item);
Daniel Veillard4255d502002-04-16 15:50:10 +00009539}
9540
9541/**
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009542 * xmlSchemaCleanupDoc:
9543 * @ctxt: a schema validation context
9544 * @node: the root of the document.
9545 *
9546 * removes unwanted nodes in a schemas document tree
9547 */
9548static void
9549xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root)
9550{
9551 xmlNodePtr delete, cur;
9552
9553 if ((ctxt == NULL) || (root == NULL)) return;
9554
9555 /*
9556 * Remove all the blank text nodes
9557 */
9558 delete = NULL;
9559 cur = root;
9560 while (cur != NULL) {
9561 if (delete != NULL) {
9562 xmlUnlinkNode(delete);
9563 xmlFreeNode(delete);
9564 delete = NULL;
9565 }
9566 if (cur->type == XML_TEXT_NODE) {
9567 if (IS_BLANK_NODE(cur)) {
9568 if (xmlNodeGetSpacePreserve(cur) != 1) {
9569 delete = cur;
9570 }
9571 }
9572 } else if ((cur->type != XML_ELEMENT_NODE) &&
9573 (cur->type != XML_CDATA_SECTION_NODE)) {
9574 delete = cur;
9575 goto skip_children;
9576 }
9577
9578 /*
9579 * Skip to next node
9580 */
9581 if (cur->children != NULL) {
9582 if ((cur->children->type != XML_ENTITY_DECL) &&
9583 (cur->children->type != XML_ENTITY_REF_NODE) &&
9584 (cur->children->type != XML_ENTITY_NODE)) {
9585 cur = cur->children;
9586 continue;
9587 }
9588 }
9589 skip_children:
9590 if (cur->next != NULL) {
9591 cur = cur->next;
9592 continue;
9593 }
9594
9595 do {
9596 cur = cur->parent;
9597 if (cur == NULL)
9598 break;
9599 if (cur == root) {
9600 cur = NULL;
9601 break;
9602 }
9603 if (cur->next != NULL) {
9604 cur = cur->next;
9605 break;
9606 }
9607 } while (cur != NULL);
9608 }
9609 if (delete != NULL) {
9610 xmlUnlinkNode(delete);
9611 xmlFreeNode(delete);
9612 delete = NULL;
9613 }
9614}
9615
William M. Brack2f2a6632004-08-20 23:09:47 +00009616
William M. Brack2f2a6632004-08-20 23:09:47 +00009617static void
9618xmlSchemaClearSchemaDefaults(xmlSchemaPtr schema)
9619{
9620 if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
9621 schema->flags ^= XML_SCHEMAS_QUALIF_ELEM;
9622
9623 if (schema->flags & XML_SCHEMAS_QUALIF_ATTR)
9624 schema->flags ^= XML_SCHEMAS_QUALIF_ATTR;
9625
9626 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
9627 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_EXTENSION;
9628 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9629 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION;
9630 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9631 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_LIST;
9632 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9633 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_UNION;
9634
9635 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
9636 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION;
9637 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
9638 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION;
9639 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
9640 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION;
9641}
9642
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009643static int
9644xmlSchemaParseSchemaElement(xmlSchemaParserCtxtPtr ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009645 xmlSchemaPtr schema,
9646 xmlNodePtr node)
9647{
9648 xmlAttrPtr attr;
9649 const xmlChar *val;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009650 int res = 0, oldErrs = ctxt->nberrors;
William M. Brack2f2a6632004-08-20 23:09:47 +00009651
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009652 /*
9653 * Those flags should be moved to the parser context flags,
9654 * since they are not visible at the component level. I.e.
9655 * they are used if processing schema *documents* only.
9656 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009657 res = xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009658 HFAILURE;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009659
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009660 /*
9661 * Since the version is of type xs:token, we won't bother to
9662 * check it.
9663 */
9664 /* REMOVED:
9665 attr = xmlSchemaGetPropNode(node, "version");
9666 if (attr != NULL) {
9667 res = xmlSchemaPValAttrNode(ctxt, NULL, NULL, attr,
Daniel Veillarddee23482008-04-11 12:58:43 +00009668 xmlSchemaGetBuiltInType(XML_SCHEMAS_TOKEN), &val);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009669 HFAILURE;
9670 }
9671 */
9672 attr = xmlSchemaGetPropNode(node, "targetNamespace");
9673 if (attr != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009674 res = xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009675 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
9676 HFAILURE;
9677 if (res != 0) {
9678 ctxt->stop = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
9679 goto exit;
9680 }
9681 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009682 attr = xmlSchemaGetPropNode(node, "elementFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009683 if (attr != NULL) {
9684 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009685 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9686 XML_SCHEMAS_QUALIF_ELEM);
9687 HFAILURE;
9688 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009689 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009690 XML_SCHEMAP_ELEMFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009691 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009692 "(qualified | unqualified)", val, NULL, NULL, NULL);
9693 }
9694 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009695 attr = xmlSchemaGetPropNode(node, "attributeFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009696 if (attr != NULL) {
9697 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009698 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9699 XML_SCHEMAS_QUALIF_ATTR);
9700 HFAILURE;
9701 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009702 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009703 XML_SCHEMAP_ATTRFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009704 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009705 "(qualified | unqualified)", val, NULL, NULL, NULL);
9706 }
9707 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009708 attr = xmlSchemaGetPropNode(node, "finalDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009709 if (attr != NULL) {
9710 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009711 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009712 XML_SCHEMAS_FINAL_DEFAULT_EXTENSION,
9713 XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION,
9714 -1,
9715 XML_SCHEMAS_FINAL_DEFAULT_LIST,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009716 XML_SCHEMAS_FINAL_DEFAULT_UNION);
9717 HFAILURE;
9718 if (res != 0) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009719 xmlSchemaPSimpleTypeErr(ctxt,
9720 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009721 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009722 "(#all | List of (extension | restriction | list | union))",
9723 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009724 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009725 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009726 attr = xmlSchemaGetPropNode(node, "blockDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009727 if (attr != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +00009728 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009729 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009730 XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION,
9731 XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009732 XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION, -1, -1);
9733 HFAILURE;
9734 if (res != 0) {
9735 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009736 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009737 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009738 "(#all | List of (extension | restriction | substitution))",
9739 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009740 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009741 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009742
9743exit:
9744 if (oldErrs != ctxt->nberrors)
9745 res = ctxt->err;
9746 return(res);
9747exit_failure:
9748 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +00009749}
9750
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009751/**
9752 * xmlSchemaParseSchemaTopLevel:
9753 * @ctxt: a schema validation context
9754 * @schema: the schemas
9755 * @nodes: the list of top level nodes
9756 *
9757 * Returns the internal XML Schema structure built from the resource or
9758 * NULL in case of error
9759 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009760static int
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009761xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt,
9762 xmlSchemaPtr schema, xmlNodePtr nodes)
9763{
9764 xmlNodePtr child;
9765 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009766 int res = 0, oldErrs, tmpOldErrs;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009767
9768 if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009769 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009770
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009771 oldErrs = ctxt->nberrors;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009772 child = nodes;
9773 while ((IS_SCHEMA(child, "include")) ||
9774 (IS_SCHEMA(child, "import")) ||
9775 (IS_SCHEMA(child, "redefine")) ||
9776 (IS_SCHEMA(child, "annotation"))) {
9777 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009778 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009779 if (schema->annot == NULL)
9780 schema->annot = annot;
9781 else
9782 xmlSchemaFreeAnnot(annot);
9783 } else if (IS_SCHEMA(child, "import")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009784 tmpOldErrs = ctxt->nberrors;
9785 res = xmlSchemaParseImport(ctxt, schema, child);
9786 HFAILURE;
9787 HSTOP(ctxt);
9788 if (tmpOldErrs != ctxt->nberrors)
Daniel Veillarddee23482008-04-11 12:58:43 +00009789 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009790 } else if (IS_SCHEMA(child, "include")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009791 tmpOldErrs = ctxt->nberrors;
9792 res = xmlSchemaParseInclude(ctxt, schema, child);
9793 HFAILURE;
9794 HSTOP(ctxt);
9795 if (tmpOldErrs != ctxt->nberrors)
Daniel Veillarddee23482008-04-11 12:58:43 +00009796 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009797 } else if (IS_SCHEMA(child, "redefine")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009798 tmpOldErrs = ctxt->nberrors;
9799 res = xmlSchemaParseRedefine(ctxt, schema, child);
9800 HFAILURE;
9801 HSTOP(ctxt);
9802 if (tmpOldErrs != ctxt->nberrors)
9803 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009804 }
9805 child = child->next;
9806 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009807 /*
9808 * URGENT TODO: Change the functions to return int results.
9809 * We need especially to catch internal errors.
9810 */
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009811 while (child != NULL) {
9812 if (IS_SCHEMA(child, "complexType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009813 xmlSchemaParseComplexType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009814 child = child->next;
9815 } else if (IS_SCHEMA(child, "simpleType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009816 xmlSchemaParseSimpleType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009817 child = child->next;
9818 } else if (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009819 xmlSchemaParseElement(ctxt, schema, child, NULL, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009820 child = child->next;
9821 } else if (IS_SCHEMA(child, "attribute")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009822 xmlSchemaParseGlobalAttribute(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009823 child = child->next;
9824 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009825 xmlSchemaParseAttributeGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009826 child = child->next;
9827 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009828 xmlSchemaParseModelGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009829 child = child->next;
9830 } else if (IS_SCHEMA(child, "notation")) {
9831 xmlSchemaParseNotation(ctxt, schema, child);
9832 child = child->next;
9833 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009834 xmlSchemaPContentErr(ctxt,
9835 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009836 NULL, child->parent, child,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009837 NULL, "((include | import | redefine | annotation)*, "
9838 "(((simpleType | complexType | group | attributeGroup) "
9839 "| element | attribute | notation), annotation*)*)");
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009840 child = child->next;
9841 }
9842 while (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009843 /*
9844 * TODO: We should add all annotations.
9845 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +00009846 annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009847 if (schema->annot == NULL)
9848 schema->annot = annot;
9849 else
9850 xmlSchemaFreeAnnot(annot);
9851 child = child->next;
9852 }
9853 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009854exit:
Daniel Veillard01fa6152004-06-29 17:04:39 +00009855 ctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009856 if (oldErrs != ctxt->nberrors)
9857 res = ctxt->err;
9858 return(res);
9859exit_failure:
9860 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009861}
9862
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009863static xmlSchemaSchemaRelationPtr
9864xmlSchemaSchemaRelationCreate(void)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009865{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009866 xmlSchemaSchemaRelationPtr ret;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009867
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009868 ret = (xmlSchemaSchemaRelationPtr)
9869 xmlMalloc(sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009870 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009871 xmlSchemaPErrMemory(NULL, "allocating schema relation", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009872 return(NULL);
9873 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009874 memset(ret, 0, sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009875 return(ret);
9876}
9877
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009878#if 0
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009879static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009880xmlSchemaSchemaRelationFree(xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009881{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009882 xmlFree(rel);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009883}
9884#endif
9885
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009886static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009887xmlSchemaRedefListFree(xmlSchemaRedefPtr redef)
9888{
9889 xmlSchemaRedefPtr prev;
9890
9891 while (redef != NULL) {
9892 prev = redef;
9893 redef = redef->next;
9894 xmlFree(prev);
9895 }
9896}
9897
9898static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009899xmlSchemaConstructionCtxtFree(xmlSchemaConstructionCtxtPtr con)
9900{
9901 /*
9902 * After the construction context has been freed, there will be
9903 * no schema graph available any more. Only the schema buckets
9904 * will stay alive, which are put into the "schemasImports" and
9905 * "includes" slots of the xmlSchema.
9906 */
9907 if (con->buckets != NULL)
9908 xmlSchemaItemListFree(con->buckets);
9909 if (con->pending != NULL)
9910 xmlSchemaItemListFree(con->pending);
9911 if (con->substGroups != NULL)
9912 xmlHashFree(con->substGroups,
9913 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009914 if (con->redefs != NULL)
9915 xmlSchemaRedefListFree(con->redefs);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009916 if (con->dict != NULL)
9917 xmlDictFree(con->dict);
9918 xmlFree(con);
9919}
9920
Daniel Veillarddee23482008-04-11 12:58:43 +00009921static xmlSchemaConstructionCtxtPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009922xmlSchemaConstructionCtxtCreate(xmlDictPtr dict)
9923{
9924 xmlSchemaConstructionCtxtPtr ret;
9925
9926 ret = (xmlSchemaConstructionCtxtPtr)
9927 xmlMalloc(sizeof(xmlSchemaConstructionCtxt));
9928 if (ret == NULL) {
9929 xmlSchemaPErrMemory(NULL,
9930 "allocating schema construction context", NULL);
9931 return (NULL);
9932 }
9933 memset(ret, 0, sizeof(xmlSchemaConstructionCtxt));
9934
9935 ret->buckets = xmlSchemaItemListCreate();
9936 if (ret->buckets == NULL) {
9937 xmlSchemaPErrMemory(NULL,
9938 "allocating list of schema buckets", NULL);
9939 xmlFree(ret);
9940 return (NULL);
9941 }
9942 ret->pending = xmlSchemaItemListCreate();
9943 if (ret->pending == NULL) {
9944 xmlSchemaPErrMemory(NULL,
9945 "allocating list of pending global components", NULL);
9946 xmlSchemaConstructionCtxtFree(ret);
9947 return (NULL);
9948 }
9949 ret->dict = dict;
9950 xmlDictReference(dict);
9951 return(ret);
9952}
9953
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009954static xmlSchemaParserCtxtPtr
9955xmlSchemaParserCtxtCreate(void)
9956{
9957 xmlSchemaParserCtxtPtr ret;
9958
9959 ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
9960 if (ret == NULL) {
9961 xmlSchemaPErrMemory(NULL, "allocating schema parser context",
9962 NULL);
9963 return (NULL);
9964 }
9965 memset(ret, 0, sizeof(xmlSchemaParserCtxt));
9966 ret->type = XML_SCHEMA_CTXT_PARSER;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009967 ret->attrProhibs = xmlSchemaItemListCreate();
9968 if (ret->attrProhibs == NULL) {
9969 xmlFree(ret);
9970 return(NULL);
9971 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009972 return(ret);
9973}
9974
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009975/**
9976 * xmlSchemaNewParserCtxtUseDict:
9977 * @URL: the location of the schema
9978 * @dict: the dictionary to be used
9979 *
9980 * Create an XML Schemas parse context for that file/resource expected
9981 * to contain an XML Schemas file.
9982 *
9983 * Returns the parser context or NULL in case of error
9984 */
9985static xmlSchemaParserCtxtPtr
9986xmlSchemaNewParserCtxtUseDict(const char *URL, xmlDictPtr dict)
9987{
9988 xmlSchemaParserCtxtPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009989
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009990 ret = xmlSchemaParserCtxtCreate();
Daniel Veillarddee23482008-04-11 12:58:43 +00009991 if (ret == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009992 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009993 ret->dict = dict;
Daniel Veillarddee23482008-04-11 12:58:43 +00009994 xmlDictReference(dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009995 if (URL != NULL)
9996 ret->URL = xmlDictLookup(dict, (const xmlChar *) URL, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009997 return (ret);
9998}
9999
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010000static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010001xmlSchemaCreatePCtxtOnVCtxt(xmlSchemaValidCtxtPtr vctxt)
10002{
10003 if (vctxt->pctxt == NULL) {
10004 if (vctxt->schema != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010005 vctxt->pctxt =
10006 xmlSchemaNewParserCtxtUseDict("*", vctxt->schema->dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010007 else
10008 vctxt->pctxt = xmlSchemaNewParserCtxt("*");
10009 if (vctxt->pctxt == NULL) {
10010 VERROR_INT("xmlSchemaCreatePCtxtOnVCtxt",
10011 "failed to create a temp. parser context");
10012 return (-1);
10013 }
10014 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010015 xmlSchemaSetParserErrors(vctxt->pctxt, vctxt->error,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000010016 vctxt->warning, vctxt->errCtxt);
10017 xmlSchemaSetParserStructuredErrors(vctxt->pctxt, vctxt->serror,
10018 vctxt->errCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010019 }
10020 return (0);
10021}
10022
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010023/**
10024 * xmlSchemaGetSchemaBucket:
10025 * @pctxt: the schema parser context
10026 * @schemaLocation: the URI of the schema document
10027 *
10028 * Returns a schema bucket if it was already parsed.
10029 *
10030 * Returns a schema bucket if it was already parsed from
10031 * @schemaLocation, NULL otherwise.
10032 */
10033static xmlSchemaBucketPtr
10034xmlSchemaGetSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
10035 const xmlChar *schemaLocation)
10036{
10037 xmlSchemaBucketPtr cur;
10038 xmlSchemaItemListPtr list;
10039
10040 list = pctxt->constructor->buckets;
10041 if (list->nbItems == 0)
10042 return(NULL);
10043 else {
10044 int i;
10045 for (i = 0; i < list->nbItems; i++) {
10046 cur = (xmlSchemaBucketPtr) list->items[i];
10047 /* Pointer comparison! */
10048 if (cur->schemaLocation == schemaLocation)
10049 return(cur);
10050 }
10051 }
10052 return(NULL);
10053}
10054
10055static xmlSchemaBucketPtr
10056xmlSchemaGetChameleonSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
10057 const xmlChar *schemaLocation,
10058 const xmlChar *targetNamespace)
10059{
10060 xmlSchemaBucketPtr cur;
10061 xmlSchemaItemListPtr list;
10062
10063 list = pctxt->constructor->buckets;
10064 if (list->nbItems == 0)
10065 return(NULL);
10066 else {
10067 int i;
10068 for (i = 0; i < list->nbItems; i++) {
10069 cur = (xmlSchemaBucketPtr) list->items[i];
10070 /* Pointer comparison! */
10071 if ((cur->origTargetNamespace == NULL) &&
10072 (cur->schemaLocation == schemaLocation) &&
10073 (cur->targetNamespace == targetNamespace))
10074 return(cur);
10075 }
10076 }
10077 return(NULL);
10078}
10079
10080
10081#define IS_BAD_SCHEMA_DOC(b) \
10082 (((b)->doc == NULL) && ((b)->schemaLocation != NULL))
10083
10084static xmlSchemaBucketPtr
10085xmlSchemaGetSchemaBucketByTNS(xmlSchemaParserCtxtPtr pctxt,
10086 const xmlChar *targetNamespace,
10087 int imported)
10088{
10089 xmlSchemaBucketPtr cur;
10090 xmlSchemaItemListPtr list;
10091
10092 list = pctxt->constructor->buckets;
10093 if (list->nbItems == 0)
10094 return(NULL);
10095 else {
10096 int i;
10097 for (i = 0; i < list->nbItems; i++) {
10098 cur = (xmlSchemaBucketPtr) list->items[i];
10099 if ((! IS_BAD_SCHEMA_DOC(cur)) &&
10100 (cur->origTargetNamespace == targetNamespace) &&
10101 ((imported && cur->imported) ||
10102 ((!imported) && (!cur->imported))))
10103 return(cur);
10104 }
10105 }
10106 return(NULL);
10107}
10108
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010109static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010110xmlSchemaParseNewDocWithContext(xmlSchemaParserCtxtPtr pctxt,
10111 xmlSchemaPtr schema,
10112 xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010113{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010114 int oldFlags;
10115 xmlDocPtr oldDoc;
10116 xmlNodePtr node;
10117 int ret, oldErrs;
10118 xmlSchemaBucketPtr oldbucket = pctxt->constructor->bucket;
Daniel Veillarddee23482008-04-11 12:58:43 +000010119
10120 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010121 * Save old values; reset the *main* schema.
10122 * URGENT TODO: This is not good; move the per-document information
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000010123 * to the parser. Get rid of passing the main schema to the
10124 * parsing functions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010125 */
10126 oldFlags = schema->flags;
10127 oldDoc = schema->doc;
10128 if (schema->flags != 0)
10129 xmlSchemaClearSchemaDefaults(schema);
Daniel Veillarddee23482008-04-11 12:58:43 +000010130 schema->doc = bucket->doc;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010131 pctxt->schema = schema;
Daniel Veillarddee23482008-04-11 12:58:43 +000010132 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010133 * Keep the current target namespace on the parser *not* on the
10134 * main schema.
10135 */
10136 pctxt->targetNamespace = bucket->targetNamespace;
10137 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010138
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010139 if ((bucket->targetNamespace != NULL) &&
10140 xmlStrEqual(bucket->targetNamespace, xmlSchemaNs)) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010141 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010142 * We are parsing the schema for schemas!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010143 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010144 pctxt->isS4S = 1;
Daniel Veillarddee23482008-04-11 12:58:43 +000010145 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010146 /* Mark it as parsed, even if parsing fails. */
10147 bucket->parsed++;
10148 /* Compile the schema doc. */
10149 node = xmlDocGetRootElement(bucket->doc);
10150 ret = xmlSchemaParseSchemaElement(pctxt, schema, node);
10151 if (ret != 0)
10152 goto exit;
10153 /* An empty schema; just get out. */
10154 if (node->children == NULL)
10155 goto exit;
10156 oldErrs = pctxt->nberrors;
10157 ret = xmlSchemaParseSchemaTopLevel(pctxt, schema, node->children);
10158 if (ret != 0)
10159 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010160 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010161 * TODO: Not nice, but I'm not 100% sure we will get always an error
10162 * as a result of the obove functions; so better rely on pctxt->err
10163 * as well.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010164 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010165 if ((ret == 0) && (oldErrs != pctxt->nberrors)) {
10166 ret = pctxt->err;
10167 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010168 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010169
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010170exit:
10171 WXS_CONSTRUCTOR(pctxt)->bucket = oldbucket;
10172 /* Restore schema values. */
10173 schema->doc = oldDoc;
10174 schema->flags = oldFlags;
10175 return(ret);
10176}
10177
10178static int
10179xmlSchemaParseNewDoc(xmlSchemaParserCtxtPtr pctxt,
10180 xmlSchemaPtr schema,
10181 xmlSchemaBucketPtr bucket)
10182{
10183 xmlSchemaParserCtxtPtr newpctxt;
10184 int res = 0;
10185
10186 if (bucket == NULL)
10187 return(0);
10188 if (bucket->parsed) {
10189 PERROR_INT("xmlSchemaParseNewDoc",
10190 "reparsing a schema doc");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010191 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010192 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010193 if (bucket->doc == NULL) {
10194 PERROR_INT("xmlSchemaParseNewDoc",
10195 "parsing a schema doc, but there's no doc");
10196 return(-1);
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +000010197 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010198 if (pctxt->constructor == NULL) {
10199 PERROR_INT("xmlSchemaParseNewDoc",
10200 "no constructor");
10201 return(-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000010202 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010203 /* Create and init the temporary parser context. */
10204 newpctxt = xmlSchemaNewParserCtxtUseDict(
10205 (const char *) bucket->schemaLocation, pctxt->dict);
10206 if (newpctxt == NULL)
10207 return(-1);
10208 newpctxt->constructor = pctxt->constructor;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010209 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000010210 * TODO: Can we avoid that the parser knows about the main schema?
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010211 * It would be better if he knows about the current schema bucket
10212 * only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010213 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010214 newpctxt->schema = schema;
10215 xmlSchemaSetParserErrors(newpctxt, pctxt->error, pctxt->warning,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000010216 pctxt->errCtxt);
10217 xmlSchemaSetParserStructuredErrors(newpctxt, pctxt->serror,
10218 pctxt->errCtxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010219 newpctxt->counter = pctxt->counter;
Daniel Veillarddee23482008-04-11 12:58:43 +000010220
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010221
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010222 res = xmlSchemaParseNewDocWithContext(newpctxt, schema, bucket);
Daniel Veillarddee23482008-04-11 12:58:43 +000010223
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010224 /* Channel back errors and cleanup the temporary parser context. */
10225 if (res != 0)
10226 pctxt->err = res;
10227 pctxt->nberrors += newpctxt->nberrors;
10228 pctxt->counter = newpctxt->counter;
Daniel Veillarddee23482008-04-11 12:58:43 +000010229 newpctxt->constructor = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010230 /* Free the parser context. */
10231 xmlSchemaFreeParserCtxt(newpctxt);
10232 return(res);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010233}
William M. Brack2f2a6632004-08-20 23:09:47 +000010234
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010235static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010236xmlSchemaSchemaRelationAddChild(xmlSchemaBucketPtr bucket,
10237 xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010238{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010239 xmlSchemaSchemaRelationPtr cur = bucket->relations;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010240
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010241 if (cur == NULL) {
10242 bucket->relations = rel;
10243 return;
10244 }
10245 while (cur->next != NULL)
10246 cur = cur->next;
10247 cur->next = rel;
10248}
10249
10250
10251static const xmlChar *
10252xmlSchemaBuildAbsoluteURI(xmlDictPtr dict, const xmlChar* location,
10253 xmlNodePtr ctxtNode)
Daniel Veillarddee23482008-04-11 12:58:43 +000010254{
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010255 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010256 * Build an absolue location URI.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010257 */
Daniel Veillarddee23482008-04-11 12:58:43 +000010258 if (location != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010259 if (ctxtNode == NULL)
10260 return(location);
10261 else {
10262 xmlChar *base, *URI;
10263 const xmlChar *ret = NULL;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010264
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010265 base = xmlNodeGetBase(ctxtNode->doc, ctxtNode);
10266 if (base == NULL) {
10267 URI = xmlBuildURI(location, ctxtNode->doc->URL);
10268 } else {
10269 URI = xmlBuildURI(location, base);
10270 xmlFree(base);
10271 }
10272 if (URI != NULL) {
10273 ret = xmlDictLookup(dict, URI, -1);
10274 xmlFree(URI);
10275 return(ret);
10276 }
10277 }
10278 }
10279 return(NULL);
10280}
Daniel Veillarddee23482008-04-11 12:58:43 +000010281
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010282
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010283
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010284/**
10285 * xmlSchemaAddSchemaDoc:
10286 * @pctxt: a schema validation context
10287 * @schema: the schema being built
10288 * @node: a subtree containing XML Schema informations
10289 *
10290 * Parse an included (and to-be-redefined) XML schema document.
10291 *
10292 * Returns 0 on success, a positive error code on errors and
10293 * -1 in case of an internal or API error.
10294 */
10295
10296static int
10297xmlSchemaAddSchemaDoc(xmlSchemaParserCtxtPtr pctxt,
10298 int type, /* import or include or redefine */
10299 const xmlChar *schemaLocation,
10300 xmlDocPtr schemaDoc,
10301 const char *schemaBuffer,
10302 int schemaBufferLen,
10303 xmlNodePtr invokingNode,
Daniel Veillarddee23482008-04-11 12:58:43 +000010304 const xmlChar *sourceTargetNamespace,
10305 const xmlChar *importNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010306 xmlSchemaBucketPtr *bucket)
10307{
10308 const xmlChar *targetNamespace = NULL;
10309 xmlSchemaSchemaRelationPtr relation = NULL;
10310 xmlDocPtr doc = NULL;
10311 int res = 0, err = 0, located = 0, preserveDoc = 0;
10312 xmlSchemaBucketPtr bkt = NULL;
10313
10314 if (bucket != NULL)
10315 *bucket = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000010316
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010317 switch (type) {
10318 case XML_SCHEMA_SCHEMA_IMPORT:
10319 case XML_SCHEMA_SCHEMA_MAIN:
10320 err = XML_SCHEMAP_SRC_IMPORT;
10321 break;
10322 case XML_SCHEMA_SCHEMA_INCLUDE:
10323 err = XML_SCHEMAP_SRC_INCLUDE;
10324 break;
10325 case XML_SCHEMA_SCHEMA_REDEFINE:
10326 err = XML_SCHEMAP_SRC_REDEFINE;
10327 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000010328 }
10329
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010330
10331 /* Special handling for the main schema:
10332 * skip the location and relation logic and just parse the doc.
10333 * We need just a bucket to be returned in this case.
Daniel Veillarddee23482008-04-11 12:58:43 +000010334 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010335 if ((type == XML_SCHEMA_SCHEMA_MAIN) || (! WXS_HAS_BUCKETS(pctxt)))
Daniel Veillarddee23482008-04-11 12:58:43 +000010336 goto doc_load;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010337
Daniel Veillarddee23482008-04-11 12:58:43 +000010338 /* Note that we expect the location to be an absulute URI. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010339 if (schemaLocation != NULL) {
10340 bkt = xmlSchemaGetSchemaBucket(pctxt, schemaLocation);
10341 if ((bkt != NULL) &&
10342 (pctxt->constructor->bucket == bkt)) {
10343 /* Report self-imports/inclusions/redefinitions. */
Daniel Veillarddee23482008-04-11 12:58:43 +000010344
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010345 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10346 invokingNode, NULL,
10347 "The schema must not import/include/redefine itself",
10348 NULL, NULL);
10349 goto exit;
10350 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000010351 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010352 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010353 * Create a relation for the graph of schemas.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010354 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010355 relation = xmlSchemaSchemaRelationCreate();
10356 if (relation == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000010357 return(-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010358 xmlSchemaSchemaRelationAddChild(pctxt->constructor->bucket,
10359 relation);
10360 relation->type = type;
10361
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010362 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010363 * Save the namespace import information.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010364 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010365 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000010366 relation->importNamespace = importNamespace;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010367 if (schemaLocation == NULL) {
10368 /*
10369 * No location; this is just an import of the namespace.
10370 * Note that we don't assign a bucket to the relation
10371 * in this case.
10372 */
10373 goto exit;
10374 }
10375 targetNamespace = importNamespace;
10376 }
10377
10378 /* Did we already fetch the doc? */
Daniel Veillarddee23482008-04-11 12:58:43 +000010379 if (bkt != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010380 if ((WXS_IS_BUCKET_IMPMAIN(type)) && (! bkt->imported)) {
Jason Childsd9960722009-08-07 19:01:32 +020010381 /*
10382 * We included/redefined and then try to import a schema,
10383 * but the new location provided for import was different.
10384 */
Daniel Veillard734e7662007-06-26 11:30:31 +000010385 if (schemaLocation == NULL)
10386 schemaLocation = BAD_CAST "in_memory_buffer";
Jason Childsd9960722009-08-07 19:01:32 +020010387 if (!xmlStrEqual(schemaLocation,
10388 bkt->schemaLocation)) {
10389 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10390 invokingNode, NULL,
10391 "The schema document '%s' cannot be imported, since "
10392 "it was already included or redefined",
10393 schemaLocation, NULL);
10394 goto exit;
10395 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010396 } else if ((! WXS_IS_BUCKET_IMPMAIN(type)) && (bkt->imported)) {
Jason Childsd9960722009-08-07 19:01:32 +020010397 /*
10398 * We imported and then try to include/redefine a schema,
10399 * but the new location provided for the include/redefine
10400 * was different.
10401 */
Daniel Veillard734e7662007-06-26 11:30:31 +000010402 if (schemaLocation == NULL)
10403 schemaLocation = BAD_CAST "in_memory_buffer";
Jason Childsd9960722009-08-07 19:01:32 +020010404 if (!xmlStrEqual(schemaLocation,
10405 bkt->schemaLocation)) {
10406 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10407 invokingNode, NULL,
10408 "The schema document '%s' cannot be included or "
10409 "redefined, since it was already imported",
10410 schemaLocation, NULL);
10411 goto exit;
10412 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010413 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010414 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010415
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010416 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010417 /*
10418 * Given that the schemaLocation [attribute] is only a hint, it is open
10419 * to applications to ignore all but the first <import> for a given
Jan Pokorný761c9e92013-11-29 23:26:27 +010010420 * namespace, regardless of the `actual value` of schemaLocation, but
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010421 * such a strategy risks missing useful information when new
10422 * schemaLocations are offered.
10423 *
10424 * We will use the first <import> that comes with a location.
10425 * Further <import>s *with* a location, will result in an error.
10426 * TODO: Better would be to just report a warning here, but
10427 * we'll try it this way until someone complains.
10428 *
10429 * Schema Document Location Strategy:
10430 * 3 Based on the namespace name, identify an existing schema document,
10431 * either as a resource which is an XML document or a <schema> element
10432 * information item, in some local schema repository;
10433 * 5 Attempt to resolve the namespace name to locate such a resource.
10434 *
10435 * NOTE: (3) and (5) are not supported.
Daniel Veillarddee23482008-04-11 12:58:43 +000010436 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010437 if (bkt != NULL) {
10438 relation->bucket = bkt;
10439 goto exit;
10440 }
10441 bkt = xmlSchemaGetSchemaBucketByTNS(pctxt,
10442 importNamespace, 1);
10443
Daniel Veillarddee23482008-04-11 12:58:43 +000010444 if (bkt != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010445 relation->bucket = bkt;
10446 if (bkt->schemaLocation == NULL) {
10447 /* First given location of the schema; load the doc. */
10448 bkt->schemaLocation = schemaLocation;
10449 } else {
10450 if (!xmlStrEqual(schemaLocation,
10451 bkt->schemaLocation)) {
10452 /*
10453 * Additional location given; just skip it.
10454 * URGENT TODO: We should report a warning here.
10455 * res = XML_SCHEMAP_SRC_IMPORT;
10456 */
Daniel Veillard734e7662007-06-26 11:30:31 +000010457 if (schemaLocation == NULL)
10458 schemaLocation = BAD_CAST "in_memory_buffer";
10459
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010460 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10461 XML_SCHEMAP_WARN_SKIP_SCHEMA,
10462 invokingNode, NULL,
10463 "Skipping import of schema located at '%s' for the "
10464 "namespace '%s', since this namespace was already "
10465 "imported with the schema located at '%s'",
10466 schemaLocation, importNamespace, bkt->schemaLocation);
10467 }
10468 goto exit;
10469 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010470 }
10471 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010472 * No bucket + first location: load the doc and create a
10473 * bucket.
10474 */
10475 } else {
10476 /* <include> and <redefine> */
10477 if (bkt != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000010478
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010479 if ((bkt->origTargetNamespace == NULL) &&
10480 (bkt->targetNamespace != sourceTargetNamespace)) {
10481 xmlSchemaBucketPtr chamel;
Daniel Veillarddee23482008-04-11 12:58:43 +000010482
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010483 /*
10484 * Chameleon include/redefine: skip loading only if it was
10485 * aleady build for the targetNamespace of the including
10486 * schema.
10487 */
10488 /*
10489 * URGENT TODO: If the schema is a chameleon-include then copy
10490 * the components into the including schema and modify the
10491 * targetNamespace of those components, do nothing otherwise.
10492 * NOTE: This is currently worked-around by compiling the
10493 * chameleon for every destinct including targetNamespace; thus
10494 * not performant at the moment.
10495 * TODO: Check when the namespace in wildcards for chameleons
10496 * needs to be converted: before we built wildcard intersections
10497 * or after.
10498 * Answer: after!
10499 */
10500 chamel = xmlSchemaGetChameleonSchemaBucket(pctxt,
10501 schemaLocation, sourceTargetNamespace);
10502 if (chamel != NULL) {
10503 /* A fitting chameleon was already parsed; NOP. */
10504 relation->bucket = chamel;
10505 goto exit;
10506 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010507 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010508 * We need to parse the chameleon again for a different
10509 * targetNamespace.
10510 * CHAMELEON TODO: Optimize this by only parsing the
10511 * chameleon once, and then copying the components to
10512 * the new targetNamespace.
10513 */
10514 bkt = NULL;
10515 } else {
10516 relation->bucket = bkt;
10517 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000010518 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010519 }
10520 }
10521 if ((bkt != NULL) && (bkt->doc != NULL)) {
10522 PERROR_INT("xmlSchemaAddSchemaDoc",
10523 "trying to load a schema doc, but a doc is already "
10524 "assigned to the schema bucket");
10525 goto exit_failure;
10526 }
10527
10528doc_load:
10529 /*
10530 * Load the document.
10531 */
10532 if (schemaDoc != NULL) {
10533 doc = schemaDoc;
10534 /* Don' free this one, since it was provided by the caller. */
10535 preserveDoc = 1;
10536 /* TODO: Does the context or the doc hold the location? */
10537 if (schemaDoc->URL != NULL)
10538 schemaLocation = xmlDictLookup(pctxt->dict,
10539 schemaDoc->URL, -1);
Daniel Veillard734e7662007-06-26 11:30:31 +000010540 else
10541 schemaLocation = BAD_CAST "in_memory_buffer";
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010542 } else if ((schemaLocation != NULL) || (schemaBuffer != NULL)) {
10543 xmlParserCtxtPtr parserCtxt;
10544
10545 parserCtxt = xmlNewParserCtxt();
10546 if (parserCtxt == NULL) {
10547 xmlSchemaPErrMemory(NULL, "xmlSchemaGetDoc, "
10548 "allocating a parser context", NULL);
10549 goto exit_failure;
10550 }
10551 if ((pctxt->dict != NULL) && (parserCtxt->dict != NULL)) {
10552 /*
10553 * TODO: Do we have to burden the schema parser dict with all
10554 * the content of the schema doc?
10555 */
10556 xmlDictFree(parserCtxt->dict);
10557 parserCtxt->dict = pctxt->dict;
10558 xmlDictReference(parserCtxt->dict);
10559 }
10560 if (schemaLocation != NULL) {
10561 /* Parse from file. */
10562 doc = xmlCtxtReadFile(parserCtxt, (const char *) schemaLocation,
10563 NULL, SCHEMAS_PARSE_OPTIONS);
10564 } else if (schemaBuffer != NULL) {
10565 /* Parse from memory buffer. */
10566 doc = xmlCtxtReadMemory(parserCtxt, schemaBuffer, schemaBufferLen,
10567 NULL, NULL, SCHEMAS_PARSE_OPTIONS);
Rob Richards410d2c42009-09-24 11:00:16 -040010568 schemaLocation = BAD_CAST "in_memory_buffer";
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010569 if (doc != NULL)
Rob Richards410d2c42009-09-24 11:00:16 -040010570 doc->URL = xmlStrdup(schemaLocation);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010571 }
10572 /*
10573 * For <import>:
10574 * 2.1 The referent is (a fragment of) a resource which is an
10575 * XML document (see clause 1.1), which in turn corresponds to
10576 * a <schema> element information item in a well-formed information
10577 * set, which in turn corresponds to a valid schema.
10578 * TODO: (2.1) fragments of XML documents are not supported.
10579 *
10580 * 2.2 The referent is a <schema> element information item in
10581 * a well-formed information set, which in turn corresponds
10582 * to a valid schema.
10583 * TODO: (2.2) is not supported.
10584 */
10585 if (doc == NULL) {
10586 xmlErrorPtr lerr;
10587 lerr = xmlGetLastError();
10588 /*
10589 * Check if this a parser error, or if the document could
10590 * just not be located.
10591 * TODO: Try to find specific error codes to react only on
10592 * localisation failures.
10593 */
10594 if ((lerr == NULL) || (lerr->domain != XML_FROM_IO)) {
10595 /*
10596 * We assume a parser error here.
10597 */
10598 located = 1;
10599 /* TODO: Error code ?? */
10600 res = XML_SCHEMAP_SRC_IMPORT_2_1;
10601 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10602 invokingNode, NULL,
10603 "Failed to parse the XML resource '%s'",
Daniel Veillard734e7662007-06-26 11:30:31 +000010604 schemaLocation, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010605 }
10606 }
10607 xmlFreeParserCtxt(parserCtxt);
10608 if ((doc == NULL) && located)
10609 goto exit_error;
10610 } else {
10611 xmlSchemaPErr(pctxt, NULL,
10612 XML_SCHEMAP_NOTHING_TO_PARSE,
10613 "No information for parsing was provided with the "
10614 "given schema parser context.\n",
10615 NULL, NULL);
10616 goto exit_failure;
10617 }
10618 /*
10619 * Preprocess the document.
10620 */
10621 if (doc != NULL) {
10622 xmlNodePtr docElem = NULL;
10623
Daniel Veillarddee23482008-04-11 12:58:43 +000010624 located = 1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010625 docElem = xmlDocGetRootElement(doc);
10626 if (docElem == NULL) {
10627 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOROOT,
Daniel Veillarddee23482008-04-11 12:58:43 +000010628 invokingNode, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010629 "The document '%s' has no document element",
10630 schemaLocation, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010631 goto exit_error;
10632 }
10633 /*
10634 * Remove all the blank text nodes.
10635 */
10636 xmlSchemaCleanupDoc(pctxt, docElem);
10637 /*
10638 * Check the schema's top level element.
10639 */
10640 if (!IS_SCHEMA(docElem, "schema")) {
10641 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOT_SCHEMA,
10642 invokingNode, NULL,
10643 "The XML document '%s' is not a schema document",
10644 schemaLocation, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010645 goto exit_error;
10646 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010647 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010648 * Note that we don't apply a type check for the
10649 * targetNamespace value here.
10650 */
10651 targetNamespace = xmlSchemaGetProp(pctxt, docElem,
10652 "targetNamespace");
10653 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010654
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010655/* after_doc_loading: */
10656 if ((bkt == NULL) && located) {
10657 /* Only create a bucket if the schema was located. */
10658 bkt = xmlSchemaBucketCreate(pctxt, type,
10659 targetNamespace);
10660 if (bkt == NULL)
10661 goto exit_failure;
10662 }
10663 if (bkt != NULL) {
10664 bkt->schemaLocation = schemaLocation;
10665 bkt->located = located;
10666 if (doc != NULL) {
10667 bkt->doc = doc;
10668 bkt->targetNamespace = targetNamespace;
10669 bkt->origTargetNamespace = targetNamespace;
10670 if (preserveDoc)
10671 bkt->preserveDoc = 1;
10672 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010673 if (WXS_IS_BUCKET_IMPMAIN(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010674 bkt->imported++;
10675 /*
10676 * Add it to the graph of schemas.
10677 */
10678 if (relation != NULL)
10679 relation->bucket = bkt;
10680 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010681
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010682exit:
10683 /*
10684 * Return the bucket explicitely; this is needed for the
10685 * main schema.
10686 */
10687 if (bucket != NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000010688 *bucket = bkt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010689 return (0);
10690
10691exit_error:
10692 if ((doc != NULL) && (! preserveDoc)) {
10693 xmlFreeDoc(doc);
10694 if (bkt != NULL)
10695 bkt->doc = NULL;
10696 }
10697 return(pctxt->err);
10698
10699exit_failure:
10700 if ((doc != NULL) && (! preserveDoc)) {
10701 xmlFreeDoc(doc);
10702 if (bkt != NULL)
10703 bkt->doc = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000010704 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010705 return (-1);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010706}
10707
William M. Brack2f2a6632004-08-20 23:09:47 +000010708/**
10709 * xmlSchemaParseImport:
10710 * @ctxt: a schema validation context
10711 * @schema: the schema being built
10712 * @node: a subtree containing XML Schema informations
10713 *
10714 * parse a XML schema Import definition
10715 * *WARNING* this interface is highly subject to change
10716 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010717 * Returns 0 in case of success, a positive error code if
10718 * not valid and -1 in case of an internal error.
William M. Brack2f2a6632004-08-20 23:09:47 +000010719 */
10720static int
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010721xmlSchemaParseImport(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
William M. Brack2f2a6632004-08-20 23:09:47 +000010722 xmlNodePtr node)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010723{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010724 xmlNodePtr child;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010725 const xmlChar *namespaceName = NULL, *schemaLocation = NULL;
10726 const xmlChar *thisTargetNamespace;
William M. Brack2f2a6632004-08-20 23:09:47 +000010727 xmlAttrPtr attr;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010728 int ret = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010729 xmlSchemaBucketPtr bucket = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000010730
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010731 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
William M. Brack2f2a6632004-08-20 23:09:47 +000010732 return (-1);
10733
10734 /*
10735 * Check for illegal attributes.
10736 */
10737 attr = node->properties;
10738 while (attr != NULL) {
10739 if (attr->ns == NULL) {
10740 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10741 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
10742 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010743 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010744 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010745 }
10746 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010747 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010748 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010749 }
10750 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010751 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010752 /*
10753 * Extract and validate attributes.
10754 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010755 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010756 "namespace", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010757 &namespaceName) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010758 xmlSchemaPSimpleTypeErr(pctxt,
10759 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010760 NULL, node,
10761 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010762 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010763 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010764 }
10765
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010766 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010767 "schemaLocation", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
William M. Brack2f2a6632004-08-20 23:09:47 +000010768 &schemaLocation) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010769 xmlSchemaPSimpleTypeErr(pctxt,
10770 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010771 NULL, node,
10772 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Gauravf3d79412013-11-28 22:53:54 +080010773 NULL, schemaLocation, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010774 return (pctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010775 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010776 /*
10777 * And now for the children...
10778 */
10779 child = node->children;
10780 if (IS_SCHEMA(child, "annotation")) {
10781 /*
10782 * the annotation here is simply discarded ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010783 * TODO: really?
William M. Brack2f2a6632004-08-20 23:09:47 +000010784 */
10785 child = child->next;
10786 }
10787 if (child != NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010788 xmlSchemaPContentErr(pctxt,
10789 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010790 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000010791 "(annotation?)");
10792 }
10793 /*
10794 * Apply additional constraints.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010795 *
10796 * Note that it is important to use the original @targetNamespace
10797 * (or none at all), to rule out imports of schemas _with_ a
10798 * @targetNamespace if the importing schema is a chameleon schema
10799 * (with no @targetNamespace).
William M. Brack2f2a6632004-08-20 23:09:47 +000010800 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010801 thisTargetNamespace = WXS_BUCKET(pctxt)->origTargetNamespace;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010802 if (namespaceName != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000010803 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010010804 * 1.1 If the namespace [attribute] is present, then its `actual value`
10805 * must not match the `actual value` of the enclosing <schema>'s
William M. Brack2f2a6632004-08-20 23:09:47 +000010806 * targetNamespace [attribute].
10807 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010808 if (xmlStrEqual(thisTargetNamespace, namespaceName)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010809 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010810 XML_SCHEMAP_SRC_IMPORT_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010811 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010812 "The value of the attribute 'namespace' must not match "
10813 "the target namespace '%s' of the importing schema",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010814 thisTargetNamespace);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010815 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010816 }
10817 } else {
10818 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010819 * 1.2 If the namespace [attribute] is not present, then the enclosing
William M. Brack2f2a6632004-08-20 23:09:47 +000010820 * <schema> must have a targetNamespace [attribute].
10821 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010822 if (thisTargetNamespace == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010823 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010824 XML_SCHEMAP_SRC_IMPORT_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010825 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010826 "The attribute 'namespace' must be existent if "
10827 "the importing schema has no target namespace",
10828 NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010829 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010830 }
10831 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010832 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010833 * Locate and acquire the schema document.
William M. Brack2f2a6632004-08-20 23:09:47 +000010834 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010835 if (schemaLocation != NULL)
10836 schemaLocation = xmlSchemaBuildAbsoluteURI(pctxt->dict,
10837 schemaLocation, node);
10838 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010839 schemaLocation, NULL, NULL, 0, node, thisTargetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010840 namespaceName, &bucket);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010841
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010842 if (ret != 0)
10843 return(ret);
10844
10845 /*
10846 * For <import>: "It is *not* an error for the application
10847 * schema reference strategy to fail."
10848 * So just don't parse if no schema document was found.
10849 * Note that we will get no bucket if the schema could not be
10850 * located or if there was no schemaLocation.
10851 */
10852 if ((bucket == NULL) && (schemaLocation != NULL)) {
10853 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10854 XML_SCHEMAP_WARN_UNLOCATED_SCHEMA,
10855 node, NULL,
10856 "Failed to locate a schema at location '%s'. "
10857 "Skipping the import", schemaLocation, NULL, NULL);
10858 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010859
10860 if ((bucket != NULL) && CAN_PARSE_SCHEMA(bucket)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010861 ret = xmlSchemaParseNewDoc(pctxt, schema, bucket);
10862 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010863
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010864 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000010865}
10866
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010867static int
10868xmlSchemaParseIncludeOrRedefineAttrs(xmlSchemaParserCtxtPtr pctxt,
10869 xmlSchemaPtr schema,
10870 xmlNodePtr node,
10871 xmlChar **schemaLocation,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010872 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010873{
10874 xmlAttrPtr attr;
10875
10876 if ((pctxt == NULL) || (schema == NULL) || (node == NULL) ||
10877 (schemaLocation == NULL))
10878 return (-1);
10879
10880 *schemaLocation = NULL;
10881 /*
10882 * Check for illegal attributes.
10883 * Applies for both <include> and <redefine>.
10884 */
10885 attr = node->properties;
10886 while (attr != NULL) {
10887 if (attr->ns == NULL) {
10888 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10889 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
10890 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010891 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010892 }
10893 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
10894 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010895 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010896 }
10897 attr = attr->next;
10898 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010899 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010900 /*
10901 * Preliminary step, extract the URI-Reference and make an URI
10902 * from the base.
10903 */
10904 /*
10905 * Attribute "schemaLocation" is mandatory.
10906 */
10907 attr = xmlSchemaGetPropNode(node, "schemaLocation");
10908 if (attr != NULL) {
10909 xmlChar *base = NULL;
10910 xmlChar *uri = NULL;
10911
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010912 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010913 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
10914 (const xmlChar **) schemaLocation) != 0)
10915 goto exit_error;
10916 base = xmlNodeGetBase(node->doc, node);
10917 if (base == NULL) {
10918 uri = xmlBuildURI(*schemaLocation, node->doc->URL);
10919 } else {
10920 uri = xmlBuildURI(*schemaLocation, base);
10921 xmlFree(base);
10922 }
10923 if (uri == NULL) {
10924 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
10925 "could not build an URI from the schemaLocation")
10926 goto exit_failure;
10927 }
10928 (*schemaLocation) = (xmlChar *) xmlDictLookup(pctxt->dict, uri, -1);
10929 xmlFree(uri);
10930 } else {
10931 xmlSchemaPMissingAttrErr(pctxt,
10932 XML_SCHEMAP_S4S_ATTR_MISSING,
10933 NULL, node, "schemaLocation", NULL);
10934 goto exit_error;
10935 }
10936 /*
10937 * Report self-inclusion and self-redefinition.
10938 */
10939 if (xmlStrEqual(*schemaLocation, pctxt->URL)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010940 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010941 xmlSchemaPCustomErr(pctxt,
10942 XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010943 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010944 "The schema document '%s' cannot redefine itself.",
Daniel Veillarddee23482008-04-11 12:58:43 +000010945 *schemaLocation);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010946 } else {
10947 xmlSchemaPCustomErr(pctxt,
10948 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010949 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010950 "The schema document '%s' cannot include itself.",
10951 *schemaLocation);
10952 }
10953 goto exit_error;
10954 }
Daniel Veillarddee23482008-04-11 12:58:43 +000010955
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010956 return(0);
10957exit_error:
10958 return(pctxt->err);
10959exit_failure:
10960 return(-1);
10961}
10962
10963static int
10964xmlSchemaParseIncludeOrRedefine(xmlSchemaParserCtxtPtr pctxt,
10965 xmlSchemaPtr schema,
10966 xmlNodePtr node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010967 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010968{
10969 xmlNodePtr child = NULL;
10970 const xmlChar *schemaLocation = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010971 int res = 0; /* hasRedefinitions = 0 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010972 int isChameleon = 0, wasChameleon = 0;
10973 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010974
10975 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
10976 return (-1);
10977
10978 /*
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010979 * Parse attributes. Note that the returned schemaLocation will
10980 * be already converted to an absolute URI.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010981 */
10982 res = xmlSchemaParseIncludeOrRedefineAttrs(pctxt, schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010983 node, (xmlChar **) (&schemaLocation), type);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010984 if (res != 0)
Daniel Veillarddee23482008-04-11 12:58:43 +000010985 return(res);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010986 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010987 * Load and add the schema document.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010988 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010989 res = xmlSchemaAddSchemaDoc(pctxt, type, schemaLocation, NULL,
10990 NULL, 0, node, pctxt->targetNamespace, NULL, &bucket);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010991 if (res != 0)
Daniel Veillarddee23482008-04-11 12:58:43 +000010992 return(res);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010993 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010994 * If we get no schema bucket back, then this means that the schema
10995 * document could not be located or was broken XML or was not
10996 * a schema document.
Daniel Veillarddee23482008-04-11 12:58:43 +000010997 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010998 if ((bucket == NULL) || (bucket->doc == NULL)) {
10999 if (type == XML_SCHEMA_SCHEMA_INCLUDE) {
11000 /*
11001 * WARNING for <include>:
11002 * We will raise an error if the schema cannot be located
11003 * for inclusions, since the that was the feedback from the
11004 * schema people. I.e. the following spec piece will *not* be
11005 * satisfied:
Jan Pokorný761c9e92013-11-29 23:26:27 +010011006 * SPEC src-include: "It is not an error for the `actual value` of the
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011007 * schemaLocation [attribute] to fail to resolve it all, in which
11008 * case no corresponding inclusion is performed.
11009 * So do we need a warning report here?"
11010 */
11011 res = XML_SCHEMAP_SRC_INCLUDE;
11012 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
11013 node, NULL,
11014 "Failed to load the document '%s' for inclusion",
11015 schemaLocation, NULL);
11016 } else {
11017 /*
11018 * NOTE: This was changed to raise an error even if no redefinitions
11019 * are specified.
11020 *
11021 * SPEC src-redefine (1)
11022 * "If there are any element information items among the [children]
Jan Pokorný761c9e92013-11-29 23:26:27 +010011023 * other than <annotation> then the `actual value` of the
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011024 * schemaLocation [attribute] must successfully resolve."
11025 * TODO: Ask the WG if a the location has always to resolve
11026 * here as well!
11027 */
11028 res = XML_SCHEMAP_SRC_REDEFINE;
11029 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
11030 node, NULL,
11031 "Failed to load the document '%s' for redefinition",
11032 schemaLocation, NULL);
11033 }
11034 } else {
11035 /*
11036 * Check targetNamespace sanity before parsing the new schema.
11037 * TODO: Note that we won't check further content if the
11038 * targetNamespace was bad.
Daniel Veillarddee23482008-04-11 12:58:43 +000011039 */
11040 if (bucket->origTargetNamespace != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011041 /*
11042 * SPEC src-include (2.1)
Jan Pokorný761c9e92013-11-29 23:26:27 +010011043 * "SII has a targetNamespace [attribute], and its `actual
11044 * value` is identical to the `actual value` of the targetNamespace
11045 * [attribute] of SII' (which must have such an [attribute])."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011046 */
11047 if (pctxt->targetNamespace == NULL) {
11048 xmlSchemaCustomErr(ACTXT_CAST pctxt,
11049 XML_SCHEMAP_SRC_INCLUDE,
11050 node, NULL,
11051 "The target namespace of the included/redefined schema "
11052 "'%s' has to be absent, since the including/redefining "
11053 "schema has no target namespace",
11054 schemaLocation, NULL);
11055 goto exit_error;
11056 } else if (!xmlStrEqual(bucket->origTargetNamespace,
11057 pctxt->targetNamespace)) {
11058 /* TODO: Change error function. */
11059 xmlSchemaPCustomErrExt(pctxt,
11060 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011061 NULL, node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011062 "The target namespace '%s' of the included/redefined "
11063 "schema '%s' differs from '%s' of the "
11064 "including/redefining schema",
11065 bucket->origTargetNamespace, schemaLocation,
11066 pctxt->targetNamespace);
11067 goto exit_error;
11068 }
Daniel Veillarddee23482008-04-11 12:58:43 +000011069 } else if (pctxt->targetNamespace != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011070 /*
11071 * Chameleons: the original target namespace will
11072 * differ from the resulting namespace.
11073 */
11074 isChameleon = 1;
11075 if (bucket->parsed &&
Jason Childsedc68aa2009-08-07 20:29:33 +020011076 bucket->origTargetNamespace != NULL) {
11077 xmlSchemaCustomErr(ACTXT_CAST pctxt,
11078 XML_SCHEMAP_SRC_INCLUDE,
11079 node, NULL,
11080 "The target namespace of the included/redefined schema "
11081 "'%s' has to be absent or the same as the "
11082 "including/redefining schema's target namespace",
11083 schemaLocation, NULL);
11084 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011085 }
11086 bucket->targetNamespace = pctxt->targetNamespace;
11087 }
Daniel Veillarddee23482008-04-11 12:58:43 +000011088 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011089 /*
11090 * Parse the schema.
Daniel Veillarddee23482008-04-11 12:58:43 +000011091 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011092 if (bucket && (!bucket->parsed) && (bucket->doc != NULL)) {
11093 if (isChameleon) {
11094 /* TODO: Get rid of this flag on the schema itself. */
11095 if ((schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) == 0) {
11096 schema->flags |= XML_SCHEMAS_INCLUDING_CONVERT_NS;
11097 } else
11098 wasChameleon = 1;
11099 }
11100 xmlSchemaParseNewDoc(pctxt, schema, bucket);
11101 /* Restore chameleon flag. */
11102 if (isChameleon && (!wasChameleon))
11103 schema->flags ^= XML_SCHEMAS_INCLUDING_CONVERT_NS;
11104 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011105 /*
11106 * And now for the children...
11107 */
Daniel Veillarddee23482008-04-11 12:58:43 +000011108 child = node->children;
11109 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011110 /*
11111 * Parse (simpleType | complexType | group | attributeGroup))*
11112 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011113 pctxt->redefined = bucket;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011114 /*
11115 * How to proceed if the redefined schema was not located?
11116 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011117 pctxt->isRedefine = 1;
11118 while (IS_SCHEMA(child, "annotation") ||
11119 IS_SCHEMA(child, "simpleType") ||
11120 IS_SCHEMA(child, "complexType") ||
11121 IS_SCHEMA(child, "group") ||
11122 IS_SCHEMA(child, "attributeGroup")) {
11123 if (IS_SCHEMA(child, "annotation")) {
11124 /*
11125 * TODO: discard or not?
11126 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011127 } else if (IS_SCHEMA(child, "simpleType")) {
11128 xmlSchemaParseSimpleType(pctxt, schema, child, 1);
11129 } else if (IS_SCHEMA(child, "complexType")) {
11130 xmlSchemaParseComplexType(pctxt, schema, child, 1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011131 /* hasRedefinitions = 1; */
Daniel Veillarddee23482008-04-11 12:58:43 +000011132 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011133 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011134 xmlSchemaParseModelGroupDefinition(pctxt,
11135 schema, child);
11136 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011137 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011138 xmlSchemaParseAttributeGroupDefinition(pctxt, schema,
11139 child);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011140 }
11141 child = child->next;
11142 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011143 pctxt->redefined = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011144 pctxt->isRedefine = 0;
11145 } else {
11146 if (IS_SCHEMA(child, "annotation")) {
11147 /*
11148 * TODO: discard or not?
11149 */
11150 child = child->next;
11151 }
Daniel Veillarddee23482008-04-11 12:58:43 +000011152 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011153 if (child != NULL) {
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011154 res = XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011155 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
11156 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011157 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011158 "(annotation | (simpleType | complexType | group | attributeGroup))*");
11159 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011160 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011161 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011162 "(annotation?)");
Daniel Veillarddee23482008-04-11 12:58:43 +000011163 }
11164 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011165 return(res);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011166
11167exit_error:
11168 return(pctxt->err);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011169}
11170
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011171static int
11172xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11173 xmlNodePtr node)
11174{
11175 int res;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011176#ifndef ENABLE_REDEFINE
11177 TODO
11178 return(0);
11179#endif
11180 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11181 XML_SCHEMA_SCHEMA_REDEFINE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011182 if (res != 0)
11183 return(res);
11184 return(0);
11185}
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011186
11187static int
11188xmlSchemaParseInclude(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11189 xmlNodePtr node)
11190{
11191 int res;
11192
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011193 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11194 XML_SCHEMA_SCHEMA_INCLUDE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011195 if (res != 0)
11196 return(res);
11197 return(0);
11198}
11199
Daniel Veillardbd2904b2003-11-25 15:38:59 +000011200/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011201 * xmlSchemaParseModelGroup:
Daniel Veillard4255d502002-04-16 15:50:10 +000011202 * @ctxt: a schema validation context
11203 * @schema: the schema being built
11204 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011205 * @type: the "compositor" type
11206 * @particleNeeded: if a a model group with a particle
Daniel Veillard4255d502002-04-16 15:50:10 +000011207 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011208 * parse a XML schema Sequence definition.
11209 * Applies parts of:
11210 * Schema Representation Constraint:
11211 * Redefinition Constraints and Semantics (src-redefine)
11212 * (6.1), (6.1.1), (6.1.2)
11213 *
Daniel Veillarddee23482008-04-11 12:58:43 +000011214 * Schema Component Constraint:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011215 * All Group Limited (cos-all-limited) (2)
11216 * TODO: Actually this should go to component-level checks,
11217 * but is done here due to performance. Move it to an other layer
11218 * is schema construction via an API is implemented.
11219 *
Daniel Veillard4255d502002-04-16 15:50:10 +000011220 * *WARNING* this interface is highly subject to change
11221 *
William M. Bracke7091952004-05-11 15:09:58 +000011222 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +000011223 * 1 in case of success.
11224 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011225static xmlSchemaTreeItemPtr
11226xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
11227 xmlNodePtr node, xmlSchemaTypeType type,
11228 int withParticle)
Daniel Veillard4255d502002-04-16 15:50:10 +000011229{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011230 xmlSchemaModelGroupPtr item;
11231 xmlSchemaParticlePtr particle = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000011232 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011233 xmlAttrPtr attr;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011234 int min = 1, max = 1, isElemRef, hasRefs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000011235
11236 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011237 return (NULL);
11238 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011239 * Create a model group with the given compositor.
William M. Brack2f2a6632004-08-20 23:09:47 +000011240 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011241 item = xmlSchemaAddModelGroup(ctxt, schema, type, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011242 if (item == NULL)
11243 return (NULL);
11244
11245 if (withParticle) {
11246 if (type == XML_SCHEMA_TYPE_ALL) {
11247 min = xmlGetMinOccurs(ctxt, node, 0, 1, 1, "(0 | 1)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011248 max = xmlGetMaxOccurs(ctxt, node, 1, 1, 1, "1");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011249 } else {
11250 /* choice + sequence */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011251 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
11252 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
11253 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011254 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011255 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
11256 /*
11257 * Create a particle
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011258 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000011259 particle = xmlSchemaAddParticle(ctxt, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011260 if (particle == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011261 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011262 particle->children = (xmlSchemaTreeItemPtr) item;
11263 /*
11264 * Check for illegal attributes.
11265 */
11266 attr = node->properties;
11267 while (attr != NULL) {
11268 if (attr->ns == NULL) {
11269 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11270 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
11271 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011272 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011273 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011274 }
11275 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011276 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011277 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011278 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011279 attr = attr->next;
William M. Brack2f2a6632004-08-20 23:09:47 +000011280 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011281 } else {
11282 /*
11283 * Check for illegal attributes.
11284 */
11285 attr = node->properties;
11286 while (attr != NULL) {
11287 if (attr->ns == NULL) {
11288 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011289 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011290 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011291 }
11292 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011293 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011294 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011295 }
11296 attr = attr->next;
11297 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011298 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011299
William M. Brack2f2a6632004-08-20 23:09:47 +000011300 /*
11301 * Extract and validate attributes.
11302 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011303 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011304 /*
11305 * And now for the children...
11306 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011307 child = node->children;
11308 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011309 item->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011310 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011311 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011312 if (type == XML_SCHEMA_TYPE_ALL) {
11313 xmlSchemaParticlePtr part, last = NULL;
11314
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011315 while (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011316 part = (xmlSchemaParticlePtr) xmlSchemaParseElement(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011317 schema, child, &isElemRef, 0);
11318 /*
11319 * SPEC cos-all-limited (2)
11320 * "The {max occurs} of all the particles in the {particles}
11321 * of the ('all') group must be 0 or 1.
11322 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011323 if (part != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011324 if (isElemRef)
11325 hasRefs++;
11326 if (part->minOccurs > 1) {
11327 xmlSchemaPCustomErr(ctxt,
11328 XML_SCHEMAP_COS_ALL_LIMITED,
11329 NULL, child,
11330 "Invalid value for minOccurs (must be 0 or 1)",
11331 NULL);
11332 /* Reset to 1. */
11333 part->minOccurs = 1;
11334 }
11335 if (part->maxOccurs > 1) {
11336 xmlSchemaPCustomErr(ctxt,
11337 XML_SCHEMAP_COS_ALL_LIMITED,
11338 NULL, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011339 "Invalid value for maxOccurs (must be 0 or 1)",
11340 NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011341 /* Reset to 1. */
11342 part->maxOccurs = 1;
11343 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011344 if (last == NULL)
11345 item->children = (xmlSchemaTreeItemPtr) part;
11346 else
11347 last->next = (xmlSchemaTreeItemPtr) part;
11348 last = part;
11349 }
11350 child = child->next;
11351 }
11352 if (child != NULL) {
11353 xmlSchemaPContentErr(ctxt,
11354 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011355 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011356 "(annotation?, (annotation?, element*)");
11357 }
11358 } else {
11359 /* choice + sequence */
11360 xmlSchemaTreeItemPtr part = NULL, last = NULL;
11361
11362 while ((IS_SCHEMA(child, "element")) ||
11363 (IS_SCHEMA(child, "group")) ||
11364 (IS_SCHEMA(child, "any")) ||
11365 (IS_SCHEMA(child, "choice")) ||
11366 (IS_SCHEMA(child, "sequence"))) {
11367
11368 if (IS_SCHEMA(child, "element")) {
11369 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011370 xmlSchemaParseElement(ctxt, schema, child, &isElemRef, 0);
11371 if (part && isElemRef)
11372 hasRefs++;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011373 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011374 part =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011375 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011376 if (part != NULL)
11377 hasRefs++;
11378 /*
11379 * Handle redefinitions.
11380 */
11381 if (ctxt->isRedefine && ctxt->redef &&
11382 (ctxt->redef->item->type == XML_SCHEMA_TYPE_GROUP) &&
11383 part && part->children)
11384 {
11385 if ((xmlSchemaGetQNameRefName(part->children) ==
11386 ctxt->redef->refName) &&
11387 (xmlSchemaGetQNameRefTargetNs(part->children) ==
11388 ctxt->redef->refTargetNs))
11389 {
11390 /*
11391 * SPEC src-redefine:
11392 * (6.1) "If it has a <group> among its contents at
Jan Pokorný761c9e92013-11-29 23:26:27 +010011393 * some level the `actual value` of whose ref
11394 * [attribute] is the same as the `actual value` of
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011395 * its own name attribute plus target namespace, then
11396 * all of the following must be true:"
11397 * (6.1.1) "It must have exactly one such group."
11398 */
11399 if (ctxt->redefCounter != 0) {
11400 xmlChar *str = NULL;
11401
11402 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11403 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11404 "The redefining model group definition "
11405 "'%s' must not contain more than one "
11406 "reference to the redefined definition",
11407 xmlSchemaFormatQName(&str,
11408 ctxt->redef->refTargetNs,
11409 ctxt->redef->refName),
11410 NULL);
11411 FREE_AND_NULL(str)
11412 part = NULL;
11413 } else if (((WXS_PARTICLE(part))->minOccurs != 1) ||
11414 ((WXS_PARTICLE(part))->maxOccurs != 1))
11415 {
11416 xmlChar *str = NULL;
11417 /*
11418 * SPEC src-redefine:
Jan Pokorný761c9e92013-11-29 23:26:27 +010011419 * (6.1.2) "The `actual value` of both that
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011420 * group's minOccurs and maxOccurs [attribute]
Jan Pokorný761c9e92013-11-29 23:26:27 +010011421 * must be 1 (or `absent`).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011422 */
11423 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11424 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11425 "The redefining model group definition "
11426 "'%s' must not contain a reference to the "
11427 "redefined definition with a "
11428 "maxOccurs/minOccurs other than 1",
11429 xmlSchemaFormatQName(&str,
11430 ctxt->redef->refTargetNs,
11431 ctxt->redef->refName),
11432 NULL);
11433 FREE_AND_NULL(str)
11434 part = NULL;
11435 }
11436 ctxt->redef->reference = WXS_BASIC_CAST part;
11437 ctxt->redefCounter++;
Daniel Veillarddee23482008-04-11 12:58:43 +000011438 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011439 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011440 } else if (IS_SCHEMA(child, "any")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011441 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011442 xmlSchemaParseAny(ctxt, schema, child);
11443 } else if (IS_SCHEMA(child, "choice")) {
11444 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11445 XML_SCHEMA_TYPE_CHOICE, 1);
11446 } else if (IS_SCHEMA(child, "sequence")) {
11447 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11448 XML_SCHEMA_TYPE_SEQUENCE, 1);
11449 }
11450 if (part != NULL) {
11451 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011452 item->children = part;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011453 else
11454 last->next = part;
11455 last = part;
11456 }
11457 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011458 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011459 if (child != NULL) {
11460 xmlSchemaPContentErr(ctxt,
11461 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011462 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011463 "(annotation?, (element | group | choice | sequence | any)*)");
11464 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011465 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011466 if ((max == 0) && (min == 0))
11467 return (NULL);
11468 if (hasRefs) {
11469 /*
11470 * We need to resolve references.
11471 */
11472 WXS_ADD_PENDING(ctxt, item);
11473 }
11474 if (withParticle)
Daniel Veillarddee23482008-04-11 12:58:43 +000011475 return ((xmlSchemaTreeItemPtr) particle);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011476 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011477 return ((xmlSchemaTreeItemPtr) item);
Daniel Veillard4255d502002-04-16 15:50:10 +000011478}
11479
11480/**
11481 * xmlSchemaParseRestriction:
11482 * @ctxt: a schema validation context
11483 * @schema: the schema being built
11484 * @node: a subtree containing XML Schema informations
Daniel Veillard4255d502002-04-16 15:50:10 +000011485 *
11486 * parse a XML schema Restriction definition
11487 * *WARNING* this interface is highly subject to change
11488 *
11489 * Returns the type definition or NULL in case of error
11490 */
11491static xmlSchemaTypePtr
11492xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011493 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011494{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011495 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011496 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011497 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011498
11499 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11500 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011501 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011502 type = ctxt->ctxtType;
11503 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011504
11505 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011506 * Check for illegal attributes.
11507 */
11508 attr = node->properties;
11509 while (attr != NULL) {
11510 if (attr->ns == NULL) {
11511 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11512 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011513 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011514 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011515 }
11516 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011517 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011518 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011519 }
11520 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011521 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011522 /*
11523 * Extract and validate attributes.
11524 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011525 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011526 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000011527 * Attribute
William M. Brack2f2a6632004-08-20 23:09:47 +000011528 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011529 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011530 * Extract the base type. The "base" attribute is mandatory if inside
11531 * a complex type or if redefining.
11532 *
11533 * SPEC (1.2) "...otherwise (<restriction> has no <simpleType> "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011534 * among its [children]), the simple type definition which is
Jan Pokorný761c9e92013-11-29 23:26:27 +010011535 * the {content type} of the type definition `resolved` to by
11536 * the `actual value` of the base [attribute]"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011537 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011538 if (xmlSchemaPValAttrQName(ctxt, schema, NULL, node, "base",
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011539 &(type->baseNs), &(type->base)) == 0)
11540 {
11541 if ((type->base == NULL) && (type->type == XML_SCHEMA_TYPE_COMPLEX)) {
11542 xmlSchemaPMissingAttrErr(ctxt,
11543 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011544 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011545 } else if ((ctxt->isRedefine) &&
11546 (type->flags & XML_SCHEMAS_TYPE_GLOBAL))
11547 {
11548 if (type->base == NULL) {
11549 xmlSchemaPMissingAttrErr(ctxt,
11550 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011551 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011552 } else if ((! xmlStrEqual(type->base, type->name)) ||
11553 (! xmlStrEqual(type->baseNs, type->targetNamespace)))
11554 {
11555 xmlChar *str1 = NULL, *str2 = NULL;
11556 /*
11557 * REDEFINE: SPEC src-redefine (5)
11558 * "Within the [children], each <simpleType> must have a
Jan Pokorný761c9e92013-11-29 23:26:27 +010011559 * <restriction> among its [children] ... the `actual value` of
11560 * whose base [attribute] must be the same as the `actual value`
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011561 * of its own name attribute plus target namespace;"
11562 */
11563 xmlSchemaPCustomErrExt(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011564 NULL, node, "This is a redefinition, but the QName "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011565 "value '%s' of the 'base' attribute does not match the "
11566 "type's designation '%s'",
11567 xmlSchemaFormatQName(&str1, type->baseNs, type->base),
Daniel Veillardfef73a52006-03-27 09:38:57 +000011568 xmlSchemaFormatQName(&str2, type->targetNamespace,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011569 type->name), NULL);
11570 FREE_AND_NULL(str1);
11571 FREE_AND_NULL(str2);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011572 /* Avoid confusion and erase the values. */
11573 type->base = NULL;
11574 type->baseNs = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011575 }
Daniel Veillarddee23482008-04-11 12:58:43 +000011576 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011577 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011578 /*
11579 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011580 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011581 child = node->children;
11582 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011583 /*
11584 * Add the annotation to the simple type ancestor.
11585 */
11586 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011587 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011588 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011589 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011590 if (parentType == XML_SCHEMA_TYPE_SIMPLE) {
11591 /*
11592 * Corresponds to <simpleType><restriction><simpleType>.
11593 */
William M. Brack2f2a6632004-08-20 23:09:47 +000011594 if (IS_SCHEMA(child, "simpleType")) {
11595 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011596 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011597 * src-restriction-base-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011598 * Either the base [attribute] or the simpleType [child] of the
11599 * <restriction> element must be present, but not both.
William M. Brack2f2a6632004-08-20 23:09:47 +000011600 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011601 xmlSchemaPContentErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000011602 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011603 NULL, node, child,
William M. Brack2f2a6632004-08-20 23:09:47 +000011604 "The attribute 'base' and the <simpleType> child are "
11605 "mutually exclusive", NULL);
11606 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011607 type->baseType = (xmlSchemaTypePtr)
William M. Brack2f2a6632004-08-20 23:09:47 +000011608 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011609 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011610 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011611 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011612 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011613 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011614 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011615 "Either the attribute 'base' or a <simpleType> child "
11616 "must be present", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000011617 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011618 } else if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11619 /*
11620 * Corresponds to <complexType><complexContent><restriction>...
11621 * followed by:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011622 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011623 * Model groups <all>, <choice> and <sequence>.
11624 */
11625 if (IS_SCHEMA(child, "all")) {
11626 type->subtypes = (xmlSchemaTypePtr)
11627 xmlSchemaParseModelGroup(ctxt, schema, child,
11628 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011629 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011630 } else if (IS_SCHEMA(child, "choice")) {
11631 type->subtypes = (xmlSchemaTypePtr)
11632 xmlSchemaParseModelGroup(ctxt,
11633 schema, child, XML_SCHEMA_TYPE_CHOICE, 1);
11634 child = child->next;
11635 } else if (IS_SCHEMA(child, "sequence")) {
11636 type->subtypes = (xmlSchemaTypePtr)
11637 xmlSchemaParseModelGroup(ctxt, schema, child,
11638 XML_SCHEMA_TYPE_SEQUENCE, 1);
11639 child = child->next;
11640 /*
11641 * Model group reference <group>.
11642 */
Daniel Veillarddee23482008-04-11 12:58:43 +000011643 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011644 type->subtypes = (xmlSchemaTypePtr)
11645 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011646 /*
11647 * Note that the reference will be resolved in
11648 * xmlSchemaResolveTypeReferences();
11649 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011650 child = child->next;
11651 }
11652 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011653 /*
11654 * Corresponds to <complexType><simpleContent><restriction>...
11655 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011656 * "1.1 the simple type definition corresponding to the <simpleType>
11657 * among the [children] of <restriction> if there is one;"
11658 */
11659 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011660 /*
11661 * We will store the to-be-restricted simple type in
11662 * type->contentTypeDef *temporarily*.
11663 */
11664 type->contentTypeDef = (xmlSchemaTypePtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011665 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011666 if ( type->contentTypeDef == NULL)
11667 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011668 child = child->next;
11669 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011670 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011671
11672 if ((parentType == XML_SCHEMA_TYPE_SIMPLE) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011673 (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011674 xmlSchemaFacetPtr facet, lastfacet = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011675 /*
11676 * Corresponds to <complexType><simpleContent><restriction>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011677 * <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011678 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011679
Daniel Veillard01fa6152004-06-29 17:04:39 +000011680 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011681 * Add the facets to the simple type ancestor.
Daniel Veillard01fa6152004-06-29 17:04:39 +000011682 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000011683 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011684 * TODO: Datatypes: 4.1.3 Constraints on XML Representation of
11685 * Simple Type Definition Schema Representation Constraint:
Daniel Veillardc0826a72004-08-10 14:17:33 +000011686 * *Single Facet Value*
11687 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000011688 while ((IS_SCHEMA(child, "minInclusive")) ||
11689 (IS_SCHEMA(child, "minExclusive")) ||
11690 (IS_SCHEMA(child, "maxInclusive")) ||
11691 (IS_SCHEMA(child, "maxExclusive")) ||
11692 (IS_SCHEMA(child, "totalDigits")) ||
11693 (IS_SCHEMA(child, "fractionDigits")) ||
11694 (IS_SCHEMA(child, "pattern")) ||
11695 (IS_SCHEMA(child, "enumeration")) ||
11696 (IS_SCHEMA(child, "whiteSpace")) ||
11697 (IS_SCHEMA(child, "length")) ||
11698 (IS_SCHEMA(child, "maxLength")) ||
11699 (IS_SCHEMA(child, "minLength"))) {
11700 facet = xmlSchemaParseFacet(ctxt, schema, child);
11701 if (facet != NULL) {
11702 if (lastfacet == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011703 type->facets = facet;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011704 else
11705 lastfacet->next = facet;
11706 lastfacet = facet;
11707 lastfacet->next = NULL;
11708 }
11709 child = child->next;
11710 }
11711 /*
11712 * Create links for derivation and validation.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011713 */
11714 if (type->facets != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000011715 xmlSchemaFacetLinkPtr facetLink, lastFacetLink = NULL;
11716
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011717 facet = type->facets;
11718 do {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011719 facetLink = (xmlSchemaFacetLinkPtr)
11720 xmlMalloc(sizeof(xmlSchemaFacetLink));
Daniel Veillard01fa6152004-06-29 17:04:39 +000011721 if (facetLink == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000011722 xmlSchemaPErrMemory(ctxt, "allocating a facet link", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011723 xmlFree(facetLink);
11724 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011725 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000011726 facetLink->facet = facet;
11727 facetLink->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011728 if (lastFacetLink == NULL)
11729 type->facetSet = facetLink;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011730 else
11731 lastFacetLink->next = facetLink;
11732 lastFacetLink = facetLink;
11733 facet = facet->next;
11734 } while (facet != NULL);
11735 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011736 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011737 if (type->type == XML_SCHEMA_TYPE_COMPLEX) {
11738 /*
11739 * Attribute uses/declarations.
11740 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011741 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11742 (xmlSchemaItemListPtr *) &(type->attrUses),
11743 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
11744 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011745 /*
11746 * Attribute wildcard.
11747 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011748 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011749 type->attributeWildcard =
11750 xmlSchemaParseAnyAttribute(ctxt, schema, child);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011751 child = child->next;
11752 }
11753 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011754 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011755 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11756 xmlSchemaPContentErr(ctxt,
11757 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011758 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011759 "annotation?, (group | all | choice | sequence)?, "
11760 "((attribute | attributeGroup)*, anyAttribute?))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011761 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011762 xmlSchemaPContentErr(ctxt,
11763 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011764 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011765 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11766 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11767 "length | minLength | maxLength | enumeration | whiteSpace | "
11768 "pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))");
11769 } else {
11770 /* Simple type */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011771 xmlSchemaPContentErr(ctxt,
11772 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011773 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011774 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11775 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11776 "length | minLength | maxLength | enumeration | whiteSpace | "
11777 "pattern)*))");
11778 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011779 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011780 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011781}
11782
11783/**
11784 * xmlSchemaParseExtension:
11785 * @ctxt: a schema validation context
11786 * @schema: the schema being built
11787 * @node: a subtree containing XML Schema informations
11788 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011789 * Parses an <extension>, which is found inside a
11790 * <simpleContent> or <complexContent>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011791 * *WARNING* this interface is highly subject to change.
Daniel Veillard4255d502002-04-16 15:50:10 +000011792 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011793 * TODO: Returns the type definition or NULL in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +000011794 */
11795static xmlSchemaTypePtr
11796xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011797 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011798{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011799 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011800 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011801 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011802
11803 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11804 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011805 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011806 type = ctxt->ctxtType;
11807 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION;
Daniel Veillard4255d502002-04-16 15:50:10 +000011808
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011809 /*
11810 * Check for illegal attributes.
11811 */
11812 attr = node->properties;
11813 while (attr != NULL) {
11814 if (attr->ns == NULL) {
11815 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11816 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011817 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011818 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011819 }
11820 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011821 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011822 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011823 }
11824 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011825 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011826
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011827 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011828
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011829 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011830 * Attribute "base" - mandatory.
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011831 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011832 if ((xmlSchemaPValAttrQName(ctxt, schema, NULL, node,
11833 "base", &(type->baseNs), &(type->base)) == 0) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011834 (type->base == NULL)) {
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011835 xmlSchemaPMissingAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011836 XML_SCHEMAP_S4S_ATTR_MISSING,
11837 NULL, node, "base", NULL);
11838 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011839 /*
11840 * And now for the children...
11841 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011842 child = node->children;
11843 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011844 /*
11845 * Add the annotation to the type ancestor.
11846 */
11847 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011848 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011849 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011850 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011851 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11852 /*
11853 * Corresponds to <complexType><complexContent><extension>... and:
11854 *
11855 * Model groups <all>, <choice>, <sequence> and <group>.
11856 */
11857 if (IS_SCHEMA(child, "all")) {
11858 type->subtypes = (xmlSchemaTypePtr)
11859 xmlSchemaParseModelGroup(ctxt, schema,
11860 child, XML_SCHEMA_TYPE_ALL, 1);
11861 child = child->next;
11862 } else if (IS_SCHEMA(child, "choice")) {
11863 type->subtypes = (xmlSchemaTypePtr)
11864 xmlSchemaParseModelGroup(ctxt, schema,
11865 child, XML_SCHEMA_TYPE_CHOICE, 1);
11866 child = child->next;
11867 } else if (IS_SCHEMA(child, "sequence")) {
11868 type->subtypes = (xmlSchemaTypePtr)
11869 xmlSchemaParseModelGroup(ctxt, schema,
11870 child, XML_SCHEMA_TYPE_SEQUENCE, 1);
11871 child = child->next;
11872 } else if (IS_SCHEMA(child, "group")) {
11873 type->subtypes = (xmlSchemaTypePtr)
11874 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011875 /*
11876 * Note that the reference will be resolved in
11877 * xmlSchemaResolveTypeReferences();
11878 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011879 child = child->next;
11880 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011881 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011882 if (child != NULL) {
11883 /*
11884 * Attribute uses/declarations.
11885 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011886 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11887 (xmlSchemaItemListPtr *) &(type->attrUses),
11888 XML_SCHEMA_TYPE_EXTENSION, NULL) == -1)
11889 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011890 /*
11891 * Attribute wildcard.
11892 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011893 if (IS_SCHEMA(child, "anyAttribute")) {
11894 ctxt->ctxtType->attributeWildcard =
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011895 xmlSchemaParseAnyAttribute(ctxt, schema, child);
11896 child = child->next;
11897 }
11898 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011899 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011900 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11901 /* Complex content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011902 xmlSchemaPContentErr(ctxt,
11903 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011904 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011905 "(annotation?, ((group | all | choice | sequence)?, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011906 "((attribute | attributeGroup)*, anyAttribute?)))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011907 } else {
11908 /* Simple content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011909 xmlSchemaPContentErr(ctxt,
11910 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011911 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011912 "(annotation?, ((attribute | attributeGroup)*, "
11913 "anyAttribute?))");
11914 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011915 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011916 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011917}
11918
11919/**
11920 * xmlSchemaParseSimpleContent:
11921 * @ctxt: a schema validation context
11922 * @schema: the schema being built
11923 * @node: a subtree containing XML Schema informations
11924 *
11925 * parse a XML schema SimpleContent definition
11926 * *WARNING* this interface is highly subject to change
11927 *
11928 * Returns the type definition or NULL in case of error
11929 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011930static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011931xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011932 xmlSchemaPtr schema, xmlNodePtr node,
11933 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011934{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011935 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011936 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011937 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011938
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011939 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11940 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011941 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011942 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011943 /* Not a component, don't create it. */
11944 type = ctxt->ctxtType;
11945 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
11946 /*
11947 * Check for illegal attributes.
11948 */
11949 attr = node->properties;
11950 while (attr != NULL) {
11951 if (attr->ns == NULL) {
11952 if ((!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011953 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011954 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011955 }
11956 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011957 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011958 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011959 }
11960 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011961 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011962
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011963 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +000011964
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011965 /*
11966 * And now for the children...
11967 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011968 child = node->children;
11969 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011970 /*
11971 * Add the annotation to the complex type ancestor.
11972 */
11973 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000011974 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011975 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011976 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011977 if (child == NULL) {
11978 xmlSchemaPContentErr(ctxt,
11979 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011980 NULL, node, NULL, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000011981 "(annotation?, (restriction | extension))");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011982 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011983 if (child == NULL) {
11984 xmlSchemaPContentErr(ctxt,
11985 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011986 NULL, node, NULL, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000011987 "(annotation?, (restriction | extension))");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011988 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011989 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011990 xmlSchemaParseRestriction(ctxt, schema, child,
11991 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011992 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011993 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011994 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011995 xmlSchemaParseExtension(ctxt, schema, child,
11996 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011997 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011998 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011999 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012000 if (child != NULL) {
12001 xmlSchemaPContentErr(ctxt,
12002 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012003 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012004 "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012005 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012006 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000012007}
12008
12009/**
12010 * xmlSchemaParseComplexContent:
12011 * @ctxt: a schema validation context
12012 * @schema: the schema being built
12013 * @node: a subtree containing XML Schema informations
12014 *
12015 * parse a XML schema ComplexContent definition
12016 * *WARNING* this interface is highly subject to change
12017 *
12018 * Returns the type definition or NULL in case of error
12019 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012020static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012021xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012022 xmlSchemaPtr schema, xmlNodePtr node,
12023 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000012024{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012025 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000012026 xmlNodePtr child = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012027 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000012028
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012029 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
12030 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012031 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012032 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012033 /* Not a component, don't create it. */
12034 type = ctxt->ctxtType;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012035 /*
12036 * Check for illegal attributes.
12037 */
12038 attr = node->properties;
12039 while (attr != NULL) {
12040 if (attr->ns == NULL) {
12041 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012042 (!xmlStrEqual(attr->name, BAD_CAST "mixed")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012043 {
12044 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012045 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012046 }
12047 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
12048 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012049 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012050 }
12051 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012052 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000012053
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012054 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000012055
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012056 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012057 * Set the 'mixed' on the complex type ancestor.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012058 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012059 if (xmlGetBooleanProp(ctxt, node, "mixed", 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012060 if ((type->flags & XML_SCHEMAS_TYPE_MIXED) == 0)
12061 type->flags |= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012062 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012063 child = node->children;
12064 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012065 /*
12066 * Add the annotation to the complex type ancestor.
12067 */
12068 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000012069 xmlSchemaParseAnnotation(ctxt, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012070 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012071 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012072 if (child == NULL) {
12073 xmlSchemaPContentErr(ctxt,
12074 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012075 NULL, node, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012076 NULL, "(annotation?, (restriction | extension))");
12077 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012078 if (child == NULL) {
12079 xmlSchemaPContentErr(ctxt,
12080 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012081 NULL, node, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012082 NULL, "(annotation?, (restriction | extension))");
12083 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012084 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012085 xmlSchemaParseRestriction(ctxt, schema, child,
12086 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012087 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012088 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012089 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012090 xmlSchemaParseExtension(ctxt, schema, child,
12091 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012092 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012093 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012094 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012095 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012096 xmlSchemaPContentErr(ctxt,
12097 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012098 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012099 NULL, "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012100 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012101 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000012102}
12103
12104/**
12105 * xmlSchemaParseComplexType:
12106 * @ctxt: a schema validation context
12107 * @schema: the schema being built
12108 * @node: a subtree containing XML Schema informations
12109 *
12110 * parse a XML schema Complex Type definition
12111 * *WARNING* this interface is highly subject to change
12112 *
12113 * Returns the type definition or NULL in case of error
12114 */
12115static xmlSchemaTypePtr
12116xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +000012117 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +000012118{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012119 xmlSchemaTypePtr type, ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012120 xmlNodePtr child = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012121 const xmlChar *name = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012122 xmlAttrPtr attr;
12123 const xmlChar *attrValue;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012124#ifdef ENABLE_NAMED_LOCALS
Daniel Veillard1a380b82004-10-21 16:00:06 +000012125 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012126#endif
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012127 int final = 0, block = 0, hasRestrictionOrExtension = 0;
Daniel Veillard1a380b82004-10-21 16:00:06 +000012128
Daniel Veillard4255d502002-04-16 15:50:10 +000012129
12130 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
12131 return (NULL);
12132
Daniel Veillard01fa6152004-06-29 17:04:39 +000012133 ctxtType = ctxt->ctxtType;
12134
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012135 if (topLevel) {
12136 attr = xmlSchemaGetPropNode(node, "name");
12137 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012138 xmlSchemaPMissingAttrErr(ctxt,
12139 XML_SCHEMAP_S4S_ATTR_MISSING, NULL, node, "name", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012140 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012141 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012142 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
12143 return (NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000012144 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012145 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012146
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012147 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012148 /*
12149 * Parse as local complex type definition.
12150 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012151#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +000012152 snprintf(buf, 39, "#CT%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012153 type = xmlSchemaAddType(ctxt, schema,
12154 XML_SCHEMA_TYPE_COMPLEX,
12155 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012156 ctxt->targetNamespace, node, 0);
12157#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012158 type = xmlSchemaAddType(ctxt, schema,
12159 XML_SCHEMA_TYPE_COMPLEX,
12160 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012161#endif
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012162 if (type == NULL)
12163 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012164 name = type->name;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012165 type->node = node;
12166 type->type = XML_SCHEMA_TYPE_COMPLEX;
12167 /*
12168 * TODO: We need the target namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012169 */
12170 } else {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012171 /*
12172 * Parse as global complex type definition.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012173 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012174 type = xmlSchemaAddType(ctxt, schema,
12175 XML_SCHEMA_TYPE_COMPLEX,
12176 name, ctxt->targetNamespace, node, 1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012177 if (type == NULL)
12178 return (NULL);
12179 type->node = node;
12180 type->type = XML_SCHEMA_TYPE_COMPLEX;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012181 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +000012182 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012183 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012184 /*
12185 * Handle attributes.
12186 */
12187 attr = node->properties;
12188 while (attr != NULL) {
12189 if (attr->ns == NULL) {
12190 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
12191 /*
12192 * Attribute "id".
12193 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012194 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012195 } else if (xmlStrEqual(attr->name, BAD_CAST "mixed")) {
12196 /*
12197 * Attribute "mixed".
12198 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012199 if (xmlSchemaPGetBoolNodeValue(ctxt,
12200 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012201 type->flags |= XML_SCHEMAS_TYPE_MIXED;
12202 } else if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012203 /*
12204 * Attributes of global complex type definitions.
12205 */
12206 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
12207 /* Pass. */
12208 } else if (xmlStrEqual(attr->name, BAD_CAST "abstract")) {
12209 /*
12210 * Attribute "abstract".
12211 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012212 if (xmlSchemaPGetBoolNodeValue(ctxt,
12213 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012214 type->flags |= XML_SCHEMAS_TYPE_ABSTRACT;
12215 } else if (xmlStrEqual(attr->name, BAD_CAST "final")) {
12216 /*
12217 * Attribute "final".
12218 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012219 attrValue = xmlSchemaGetNodeContent(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012220 (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012221 if (xmlSchemaPValAttrBlockFinal(attrValue,
12222 &(type->flags),
12223 -1,
12224 XML_SCHEMAS_TYPE_FINAL_EXTENSION,
12225 XML_SCHEMAS_TYPE_FINAL_RESTRICTION,
12226 -1, -1, -1) != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012227 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012228 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012229 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012230 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012231 "(#all | List of (extension | restriction))",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012232 attrValue, NULL, NULL, NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000012233 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012234 final = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012235 } else if (xmlStrEqual(attr->name, BAD_CAST "block")) {
12236 /*
12237 * Attribute "block".
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012238 */
12239 attrValue = xmlSchemaGetNodeContent(ctxt,
12240 (xmlNodePtr) attr);
12241 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012242 -1,
12243 XML_SCHEMAS_TYPE_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012244 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012245 -1, -1, -1) != 0) {
12246 xmlSchemaPSimpleTypeErr(ctxt,
12247 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012248 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012249 "(#all | List of (extension | restriction)) ",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012250 attrValue, NULL, NULL, NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000012251 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012252 block = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012253 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012254 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012255 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012256 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012257 } else {
12258 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012259 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012260 }
12261 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000012262 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012263 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012264 }
12265 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012266 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012267 if (! block) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000012268 /*
12269 * Apply default "block" values.
12270 */
12271 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
12272 type->flags |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
12273 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
12274 type->flags |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
12275 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012276 if (! final) {
12277 /*
12278 * Apply default "block" values.
12279 */
12280 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
12281 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
12282 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
12283 type->flags |= XML_SCHEMAS_TYPE_FINAL_EXTENSION;
12284 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012285 /*
12286 * And now for the children...
12287 */
Daniel Veillard4255d502002-04-16 15:50:10 +000012288 child = node->children;
12289 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000012290 type->annot = xmlSchemaParseAnnotation(ctxt, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012291 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012292 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012293 ctxt->ctxtType = type;
Daniel Veillard4255d502002-04-16 15:50:10 +000012294 if (IS_SCHEMA(child, "simpleContent")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012295 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012296 * <complexType><simpleContent>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012297 * 3.4.3 : 2.2
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012298 * Specifying mixed='true' when the <simpleContent>
12299 * alternative is chosen has no effect
12300 */
William M. Bracke7091952004-05-11 15:09:58 +000012301 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
12302 type->flags ^= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012303 xmlSchemaParseSimpleContent(ctxt, schema, child,
12304 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012305 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012306 } else if (IS_SCHEMA(child, "complexContent")) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012307 /*
12308 * <complexType><complexContent>...
12309 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012310 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012311 xmlSchemaParseComplexContent(ctxt, schema, child,
12312 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012313 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012314 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012315 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012316 * E.g <complexType><sequence>... or <complexType><attribute>... etc.
12317 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012318 * SPEC
12319 * "...the third alternative (neither <simpleContent> nor
12320 * <complexContent>) is chosen. This case is understood as shorthand
Jan Pokorný761c9e92013-11-29 23:26:27 +010012321 * for complex content restricting the `ur-type definition`, and the
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012322 * details of the mappings should be modified as necessary.
12323 */
12324 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
12325 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012326 /*
12327 * Parse model groups.
12328 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012329 if (IS_SCHEMA(child, "all")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012330 type->subtypes = (xmlSchemaTypePtr)
12331 xmlSchemaParseModelGroup(ctxt, schema, child,
12332 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012333 child = child->next;
12334 } else if (IS_SCHEMA(child, "choice")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012335 type->subtypes = (xmlSchemaTypePtr)
12336 xmlSchemaParseModelGroup(ctxt, schema, child,
12337 XML_SCHEMA_TYPE_CHOICE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012338 child = child->next;
12339 } else if (IS_SCHEMA(child, "sequence")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012340 type->subtypes = (xmlSchemaTypePtr)
12341 xmlSchemaParseModelGroup(ctxt, schema, child,
12342 XML_SCHEMA_TYPE_SEQUENCE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012343 child = child->next;
12344 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012345 type->subtypes = (xmlSchemaTypePtr)
12346 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012347 /*
12348 * Note that the reference will be resolved in
12349 * xmlSchemaResolveTypeReferences();
12350 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012351 child = child->next;
12352 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012353 /*
12354 * Parse attribute decls/refs.
12355 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012356 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
12357 (xmlSchemaItemListPtr *) &(type->attrUses),
12358 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
12359 return(NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012360 /*
12361 * Parse attribute wildcard.
12362 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012363 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012364 type->attributeWildcard = xmlSchemaParseAnyAttribute(ctxt, schema, child);
12365 child = child->next;
12366 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012367 }
12368 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012369 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012370 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012371 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012372 NULL, "(annotation?, (simpleContent | complexContent | "
12373 "((group | all | choice | sequence)?, ((attribute | "
12374 "attributeGroup)*, anyAttribute?))))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012375 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012376 /*
12377 * REDEFINE: SPEC src-redefine (5)
12378 */
12379 if (topLevel && ctxt->isRedefine && (! hasRestrictionOrExtension)) {
12380 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012381 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012382 "<complexType> must have a <restriction> or <extension> "
12383 "grand-child", NULL);
12384 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012385 ctxt->ctxtType = ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012386 return (type);
12387}
12388
Daniel Veillard4255d502002-04-16 15:50:10 +000012389/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080012390 * *
12391 * Validating using Schemas *
12392 * *
Daniel Veillard4255d502002-04-16 15:50:10 +000012393 ************************************************************************/
12394
12395/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080012396 * *
12397 * Reading/Writing Schemas *
12398 * *
Daniel Veillard4255d502002-04-16 15:50:10 +000012399 ************************************************************************/
12400
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012401#if 0 /* Will be enabled if it is clear what options are needed. */
12402/**
12403 * xmlSchemaParserCtxtSetOptions:
12404 * @ctxt: a schema parser context
12405 * @options: a combination of xmlSchemaParserOption
12406 *
12407 * Sets the options to be used during the parse.
12408 *
12409 * Returns 0 in case of success, -1 in case of an
12410 * API error.
12411 */
12412static int
12413xmlSchemaParserCtxtSetOptions(xmlSchemaParserCtxtPtr ctxt,
12414 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012415
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012416{
12417 int i;
12418
12419 if (ctxt == NULL)
12420 return (-1);
12421 /*
12422 * WARNING: Change the start value if adding to the
12423 * xmlSchemaParseOption.
12424 */
12425 for (i = 1; i < (int) sizeof(int) * 8; i++) {
12426 if (options & 1<<i) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012427 return (-1);
12428 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012429 }
12430 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012431 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012432}
12433
12434/**
12435 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012436 * @ctxt: a schema parser context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012437 *
12438 * Returns the option combination of the parser context.
12439 */
12440static int
12441xmlSchemaParserCtxtGetOptions(xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012442
12443{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012444 if (ctxt == NULL)
12445 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012446 else
12447 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012448}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012449#endif
12450
Daniel Veillard4255d502002-04-16 15:50:10 +000012451/**
12452 * xmlSchemaNewParserCtxt:
12453 * @URL: the location of the schema
12454 *
12455 * Create an XML Schemas parse context for that file/resource expected
12456 * to contain an XML Schemas file.
12457 *
12458 * Returns the parser context or NULL in case of error
12459 */
12460xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012461xmlSchemaNewParserCtxt(const char *URL)
12462{
Daniel Veillard4255d502002-04-16 15:50:10 +000012463 xmlSchemaParserCtxtPtr ret;
12464
12465 if (URL == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012466 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000012467
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012468 ret = xmlSchemaParserCtxtCreate();
12469 if (ret == NULL)
12470 return(NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012471 ret->dict = xmlDictCreate();
12472 ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012473 return (ret);
12474}
12475
12476/**
Daniel Veillard6045c902002-10-09 21:13:59 +000012477 * xmlSchemaNewMemParserCtxt:
12478 * @buffer: a pointer to a char array containing the schemas
12479 * @size: the size of the array
12480 *
12481 * Create an XML Schemas parse context for that memory buffer expected
12482 * to contain an XML Schemas file.
12483 *
12484 * Returns the parser context or NULL in case of error
12485 */
12486xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012487xmlSchemaNewMemParserCtxt(const char *buffer, int size)
12488{
Daniel Veillard6045c902002-10-09 21:13:59 +000012489 xmlSchemaParserCtxtPtr ret;
12490
12491 if ((buffer == NULL) || (size <= 0))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012492 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012493 ret = xmlSchemaParserCtxtCreate();
12494 if (ret == NULL)
12495 return(NULL);
Daniel Veillard6045c902002-10-09 21:13:59 +000012496 ret->buffer = buffer;
12497 ret->size = size;
Daniel Veillarddee23482008-04-11 12:58:43 +000012498 ret->dict = xmlDictCreate();
Daniel Veillard6045c902002-10-09 21:13:59 +000012499 return (ret);
12500}
12501
12502/**
Daniel Veillard9d751502003-10-29 13:21:47 +000012503 * xmlSchemaNewDocParserCtxt:
12504 * @doc: a preparsed document tree
12505 *
12506 * Create an XML Schemas parse context for that document.
12507 * NB. The document may be modified during the parsing process.
12508 *
12509 * Returns the parser context or NULL in case of error
12510 */
12511xmlSchemaParserCtxtPtr
12512xmlSchemaNewDocParserCtxt(xmlDocPtr doc)
12513{
12514 xmlSchemaParserCtxtPtr ret;
12515
12516 if (doc == NULL)
12517 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012518 ret = xmlSchemaParserCtxtCreate();
12519 if (ret == NULL)
12520 return(NULL);
Daniel Veillard9d751502003-10-29 13:21:47 +000012521 ret->doc = doc;
William M. Brackcf9eadf2003-12-25 13:24:05 +000012522 ret->dict = xmlDictCreate();
Daniel Veillarddda22c12004-01-24 08:31:30 +000012523 /* The application has responsibility for the document */
12524 ret->preserve = 1;
Daniel Veillard9d751502003-10-29 13:21:47 +000012525
12526 return (ret);
12527}
12528
12529/**
Daniel Veillard4255d502002-04-16 15:50:10 +000012530 * xmlSchemaFreeParserCtxt:
12531 * @ctxt: the schema parser context
12532 *
12533 * Free the resources associated to the schema parser context
12534 */
12535void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012536xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt)
12537{
Daniel Veillard4255d502002-04-16 15:50:10 +000012538 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012539 return;
Daniel Veillarddda22c12004-01-24 08:31:30 +000012540 if (ctxt->doc != NULL && !ctxt->preserve)
Daniel Veillarddee23482008-04-11 12:58:43 +000012541 xmlFreeDoc(ctxt->doc);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012542 if (ctxt->vctxt != NULL) {
12543 xmlSchemaFreeValidCtxt(ctxt->vctxt);
12544 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012545 if (ctxt->ownsConstructor && (ctxt->constructor != NULL)) {
12546 xmlSchemaConstructionCtxtFree(ctxt->constructor);
12547 ctxt->constructor = NULL;
12548 ctxt->ownsConstructor = 0;
12549 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012550 if (ctxt->attrProhibs != NULL)
12551 xmlSchemaItemListFree(ctxt->attrProhibs);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012552 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000012553 xmlFree(ctxt);
12554}
12555
12556/************************************************************************
12557 * *
12558 * Building the content models *
12559 * *
12560 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012561
Daniel Veillard4013e832009-08-26 17:24:31 +020012562/**
12563 * xmlSchemaBuildContentModelForSubstGroup:
12564 *
12565 * Returns 1 if nillable, 0 otherwise
12566 */
12567static int
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012568xmlSchemaBuildContentModelForSubstGroup(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012569 xmlSchemaParticlePtr particle, int counter, xmlAutomataStatePtr end)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012570{
Daniel Veillarda980bef2005-07-18 21:34:03 +000012571 xmlAutomataStatePtr start, tmp;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012572 xmlSchemaElementPtr elemDecl, member;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012573 xmlSchemaSubstGroupPtr substGroup;
12574 int i;
Daniel Veillard4013e832009-08-26 17:24:31 +020012575 int ret = 0;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012576
12577 elemDecl = (xmlSchemaElementPtr) particle->children;
12578 /*
12579 * Wrap the substitution group with a CHOICE.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012580 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012581 start = pctxt->state;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012582 if (end == NULL)
12583 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012584 substGroup = xmlSchemaSubstGroupGet(pctxt, elemDecl);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012585 if (substGroup == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012586 xmlSchemaPErr(pctxt, WXS_ITEM_NODE(particle),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012587 XML_SCHEMAP_INTERNAL,
12588 "Internal error: xmlSchemaBuildContentModelForSubstGroup, "
12589 "declaration is marked having a subst. group but none "
12590 "available.\n", elemDecl->name, NULL);
Daniel Veillard4013e832009-08-26 17:24:31 +020012591 return(0);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012592 }
Daniel Veillarda980bef2005-07-18 21:34:03 +000012593 if (counter >= 0) {
12594 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012595 * NOTE that we put the declaration in, even if it's abstract.
12596 * However, an error will be raised during *validation* if an element
12597 * information item shall be validated against an abstract element
12598 * declaration.
Daniel Veillarda980bef2005-07-18 21:34:03 +000012599 */
12600 tmp = xmlAutomataNewCountedTrans(pctxt->am, start, NULL, counter);
12601 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12602 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12603 /*
12604 * Add subst. group members.
12605 */
12606 for (i = 0; i < substGroup->members->nbItems; i++) {
12607 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12608 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12609 member->name, member->targetNamespace, member);
12610 }
12611 } else if (particle->maxOccurs == 1) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012612 /*
12613 * NOTE that we put the declaration in, even if it's abstract,
12614 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012615 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012616 xmlAutomataNewTransition2(pctxt->am,
12617 start, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012618 elemDecl->name, elemDecl->targetNamespace, elemDecl), end);
12619 /*
12620 * Add subst. group members.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012621 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012622 for (i = 0; i < substGroup->members->nbItems; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012623 member = (xmlSchemaElementPtr) substGroup->members->items[i];
Kasimier T. Buchcik92b394f2006-05-09 19:59:54 +000012624 /*
12625 * NOTE: This fixes bug #341150. xmlAutomataNewOnceTrans2()
12626 * was incorrectly used instead of xmlAutomataNewTransition2()
12627 * (seems like a copy&paste bug from the XML_SCHEMA_TYPE_ALL
12628 * section in xmlSchemaBuildAContentModel() ).
Daniel Veillarddee23482008-04-11 12:58:43 +000012629 * TODO: Check if xmlAutomataNewOnceTrans2() was instead
Kasimier T. Buchcik92b394f2006-05-09 19:59:54 +000012630 * intended for the above "counter" section originally. I.e.,
12631 * check xs:all with subst-groups.
12632 *
12633 * tmp = xmlAutomataNewOnceTrans2(pctxt->am, start, NULL,
12634 * member->name, member->targetNamespace,
12635 * 1, 1, member);
12636 */
12637 tmp = xmlAutomataNewTransition2(pctxt->am, start, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000012638 member->name, member->targetNamespace, member);
Daniel Veillarda980bef2005-07-18 21:34:03 +000012639 xmlAutomataNewEpsilon(pctxt->am, tmp, end);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012640 }
12641 } else {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012642 xmlAutomataStatePtr hop;
12643 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12644 UNBOUNDED : particle->maxOccurs - 1;
12645 int minOccurs = particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
12646
12647 counter =
12648 xmlAutomataNewCounter(pctxt->am, minOccurs,
12649 maxOccurs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012650 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012651
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012652 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012653 xmlAutomataNewTransition2(pctxt->am,
12654 start, NULL,
12655 elemDecl->name, elemDecl->targetNamespace, elemDecl),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012656 hop);
12657 /*
Daniel Veillarda980bef2005-07-18 21:34:03 +000012658 * Add subst. group members.
12659 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012660 for (i = 0; i < substGroup->members->nbItems; i++) {
12661 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12662 xmlAutomataNewEpsilon(pctxt->am,
12663 xmlAutomataNewTransition2(pctxt->am,
12664 start, NULL,
12665 member->name, member->targetNamespace, member),
12666 hop);
12667 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012668 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12669 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
12670 }
Daniel Veillard4013e832009-08-26 17:24:31 +020012671 if (particle->minOccurs == 0) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012672 xmlAutomataNewEpsilon(pctxt->am, start, end);
Daniel Veillard4013e832009-08-26 17:24:31 +020012673 ret = 1;
12674 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012675 pctxt->state = end;
Daniel Veillard4013e832009-08-26 17:24:31 +020012676 return(ret);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012677}
12678
Daniel Veillard4013e832009-08-26 17:24:31 +020012679/**
12680 * xmlSchemaBuildContentModelForElement:
12681 *
12682 * Returns 1 if nillable, 0 otherwise
12683 */
12684static int
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012685xmlSchemaBuildContentModelForElement(xmlSchemaParserCtxtPtr ctxt,
12686 xmlSchemaParticlePtr particle)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012687{
Daniel Veillard4013e832009-08-26 17:24:31 +020012688 int ret = 0;
12689
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012690 if (((xmlSchemaElementPtr) particle->children)->flags &
12691 XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012692 /*
12693 * Substitution groups.
12694 */
Daniel Veillard4013e832009-08-26 17:24:31 +020012695 ret = xmlSchemaBuildContentModelForSubstGroup(ctxt, particle, -1, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012696 } else {
12697 xmlSchemaElementPtr elemDecl;
12698 xmlAutomataStatePtr start;
12699
12700 elemDecl = (xmlSchemaElementPtr) particle->children;
12701
12702 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT)
Daniel Veillard4013e832009-08-26 17:24:31 +020012703 return(0);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012704 if (particle->maxOccurs == 1) {
12705 start = ctxt->state;
12706 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012707 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12708 } else if ((particle->maxOccurs >= UNBOUNDED) &&
12709 (particle->minOccurs < 2)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012710 /* Special case. */
Daniel Veillarddee23482008-04-11 12:58:43 +000012711 start = ctxt->state;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012712 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000012713 elemDecl->name, elemDecl->targetNamespace, elemDecl);
Kasimier T. Buchcik7ca5aed2005-12-12 15:13:40 +000012714 ctxt->state = xmlAutomataNewTransition2(ctxt->am, ctxt->state, ctxt->state,
Daniel Veillarddee23482008-04-11 12:58:43 +000012715 elemDecl->name, elemDecl->targetNamespace, elemDecl);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012716 } else {
12717 int counter;
12718 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12719 UNBOUNDED : particle->maxOccurs - 1;
12720 int minOccurs = particle->minOccurs < 1 ?
12721 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012722
12723 start = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012724 counter = xmlAutomataNewCounter(ctxt->am, minOccurs, maxOccurs);
12725 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12726 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12727 xmlAutomataNewCountedTrans(ctxt->am, ctxt->state, start, counter);
12728 ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, ctxt->state,
12729 NULL, counter);
12730 }
Daniel Veillard4013e832009-08-26 17:24:31 +020012731 if (particle->minOccurs == 0) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012732 xmlAutomataNewEpsilon(ctxt->am, start, ctxt->state);
Daniel Veillard4013e832009-08-26 17:24:31 +020012733 ret = 1;
12734 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012735 }
Daniel Veillard4013e832009-08-26 17:24:31 +020012736 return(ret);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012737}
12738
Daniel Veillard4255d502002-04-16 15:50:10 +000012739/**
12740 * xmlSchemaBuildAContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012741 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012742 * @particle: the particle component
12743 * @name: the complex type's name whose content is being built
Daniel Veillard4255d502002-04-16 15:50:10 +000012744 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012745 * Create the automaton for the {content type} of a complex type.
12746 *
Daniel Veillard4013e832009-08-26 17:24:31 +020012747 * Returns 1 if the content is nillable, 0 otherwise
Daniel Veillard4255d502002-04-16 15:50:10 +000012748 */
Daniel Veillard4013e832009-08-26 17:24:31 +020012749static int
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012750xmlSchemaBuildAContentModel(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012751 xmlSchemaParticlePtr particle)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012752{
Daniel Veillard4013e832009-08-26 17:24:31 +020012753 int ret = 0, tmp2;
12754
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012755 if (particle == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000012756 PERROR_INT("xmlSchemaBuildAContentModel", "particle is NULL");
Daniel Veillard4013e832009-08-26 17:24:31 +020012757 return(1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012758 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012759 if (particle->children == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012760 /*
12761 * Just return in this case. A missing "term" of the particle
12762 * might arise due to an invalid "term" component.
12763 */
Daniel Veillard4013e832009-08-26 17:24:31 +020012764 return(1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012765 }
12766
12767 switch (particle->children->type) {
12768 case XML_SCHEMA_TYPE_ANY: {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012769 xmlAutomataStatePtr start, end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012770 xmlSchemaWildcardPtr wild;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012771 xmlSchemaWildcardNsPtr ns;
Daniel Veillard32370232002-10-16 14:08:14 +000012772
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012773 wild = (xmlSchemaWildcardPtr) particle->children;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012774
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012775 start = pctxt->state;
12776 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012777
12778 if (particle->maxOccurs == 1) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012779 if (wild->any == 1) {
12780 /*
12781 * We need to add both transitions:
12782 *
12783 * 1. the {"*", "*"} for elements in a namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012784 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012785 pctxt->state =
12786 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012787 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012788 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012789 /*
12790 * 2. the {"*"} for elements in no namespace.
12791 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012792 pctxt->state =
12793 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012794 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012795 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012796
12797 } else if (wild->nsSet != NULL) {
12798 ns = wild->nsSet;
12799 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012800 pctxt->state = start;
12801 pctxt->state = xmlAutomataNewTransition2(pctxt->am,
12802 pctxt->state, NULL, BAD_CAST "*", ns->value, wild);
12803 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012804 ns = ns->next;
12805 } while (ns != NULL);
12806
12807 } else if (wild->negNsSet != NULL) {
Daniel Veillard6e65e152005-08-09 11:09:52 +000012808 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
12809 start, end, BAD_CAST "*", wild->negNsSet->value,
12810 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012811 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000012812 } else {
12813 int counter;
12814 xmlAutomataStatePtr hop;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012815 int maxOccurs =
Daniel Veillard4013e832009-08-26 17:24:31 +020012816 particle->maxOccurs == UNBOUNDED ? UNBOUNDED :
12817 particle->maxOccurs - 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012818 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012819 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012820
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012821 counter = xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12822 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012823 if (wild->any == 1) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012824 pctxt->state =
12825 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012826 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012827 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
12828 pctxt->state =
12829 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012830 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012831 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012832 } else if (wild->nsSet != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012833 ns = wild->nsSet;
12834 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012835 pctxt->state =
12836 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012837 start, NULL, BAD_CAST "*", ns->value, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012838 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012839 ns = ns->next;
12840 } while (ns != NULL);
12841
12842 } else if (wild->negNsSet != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012843 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
Daniel Veillard9efc4762005-07-19 14:33:55 +000012844 start, hop, BAD_CAST "*", wild->negNsSet->value,
12845 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012846 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012847 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12848 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012849 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012850 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012851 xmlAutomataNewEpsilon(pctxt->am, start, end);
Daniel Veillard4013e832009-08-26 17:24:31 +020012852 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012853 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012854 pctxt->state = end;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012855 break;
12856 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012857 case XML_SCHEMA_TYPE_ELEMENT:
Daniel Veillard4013e832009-08-26 17:24:31 +020012858 ret = xmlSchemaBuildContentModelForElement(pctxt, particle);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012859 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012860 case XML_SCHEMA_TYPE_SEQUENCE:{
Daniel Veillard4013e832009-08-26 17:24:31 +020012861 xmlSchemaTreeItemPtr sub;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012862
Daniel Veillard4013e832009-08-26 17:24:31 +020012863 ret = 1;
12864 /*
12865 * If max and min occurances are default (1) then
12866 * simply iterate over the particles of the <sequence>.
12867 */
12868 if ((particle->minOccurs == 1) && (particle->maxOccurs == 1)) {
12869 sub = particle->children->children;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012870
Daniel Veillard4013e832009-08-26 17:24:31 +020012871 while (sub != NULL) {
12872 tmp2 = xmlSchemaBuildAContentModel(pctxt,
12873 (xmlSchemaParticlePtr) sub);
12874 if (tmp2 != 1) ret = 0;
12875 sub = sub->next;
12876 }
12877 } else {
12878 xmlAutomataStatePtr oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012879
Daniel Veillard4013e832009-08-26 17:24:31 +020012880 if (particle->maxOccurs >= UNBOUNDED) {
12881 if (particle->minOccurs > 1) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012882 xmlAutomataStatePtr tmp;
12883 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012884
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012885 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Daniel Veillard4013e832009-08-26 17:24:31 +020012886 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012887 oldstate = pctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +000012888
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012889 counter = xmlAutomataNewCounter(pctxt->am,
Daniel Veillard4013e832009-08-26 17:24:31 +020012890 particle->minOccurs - 1, UNBOUNDED);
Daniel Veillard4255d502002-04-16 15:50:10 +000012891
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012892 sub = particle->children->children;
12893 while (sub != NULL) {
Daniel Veillard4013e832009-08-26 17:24:31 +020012894 tmp2 = xmlSchemaBuildAContentModel(pctxt,
12895 (xmlSchemaParticlePtr) sub);
12896 if (tmp2 != 1) ret = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012897 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012898 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012899 tmp = pctxt->state;
Daniel Veillard4013e832009-08-26 17:24:31 +020012900 xmlAutomataNewCountedTrans(pctxt->am, tmp,
12901 oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012902 pctxt->state =
Daniel Veillard4013e832009-08-26 17:24:31 +020012903 xmlAutomataNewCounterTrans(pctxt->am, tmp,
12904 NULL, counter);
12905 if (ret == 1)
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012906 xmlAutomataNewEpsilon(pctxt->am,
Daniel Veillard4013e832009-08-26 17:24:31 +020012907 oldstate, pctxt->state);
12908
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012909 } else {
Daniel Veillard4013e832009-08-26 17:24:31 +020012910 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
12911 oldstate, NULL);
12912 oldstate = pctxt->state;
12913
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012914 sub = particle->children->children;
12915 while (sub != NULL) {
Daniel Veillard4013e832009-08-26 17:24:31 +020012916 tmp2 = xmlSchemaBuildAContentModel(pctxt,
12917 (xmlSchemaParticlePtr) sub);
12918 if (tmp2 != 1) ret = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012919 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012920 }
Daniel Veillard4013e832009-08-26 17:24:31 +020012921 xmlAutomataNewEpsilon(pctxt->am, pctxt->state,
12922 oldstate);
12923 /*
12924 * epsilon needed to block previous trans from
12925 * being allowed to enter back from another
12926 * construct
12927 */
12928 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
12929 pctxt->state, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012930 if (particle->minOccurs == 0) {
Daniel Veillard4013e832009-08-26 17:24:31 +020012931 xmlAutomataNewEpsilon(pctxt->am,
12932 oldstate, pctxt->state);
12933 ret = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012934 }
12935 }
Daniel Veillard4013e832009-08-26 17:24:31 +020012936 } else if ((particle->maxOccurs > 1)
12937 || (particle->minOccurs > 1)) {
12938 xmlAutomataStatePtr tmp;
12939 int counter;
Daniel Veillardb509f152002-04-17 16:28:10 +000012940
Daniel Veillard4013e832009-08-26 17:24:31 +020012941 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
12942 oldstate, NULL);
12943 oldstate = pctxt->state;
Daniel Veillard7646b182002-04-20 06:41:40 +000012944
Daniel Veillard4013e832009-08-26 17:24:31 +020012945 counter = xmlAutomataNewCounter(pctxt->am,
12946 particle->minOccurs - 1,
12947 particle->maxOccurs - 1);
12948
12949 sub = particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012950 while (sub != NULL) {
Daniel Veillard4013e832009-08-26 17:24:31 +020012951 tmp2 = xmlSchemaBuildAContentModel(pctxt,
12952 (xmlSchemaParticlePtr) sub);
12953 if (tmp2 != 1) ret = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012954 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012955 }
Daniel Veillard4013e832009-08-26 17:24:31 +020012956 tmp = pctxt->state;
12957 xmlAutomataNewCountedTrans(pctxt->am,
12958 tmp, oldstate, counter);
12959 pctxt->state =
12960 xmlAutomataNewCounterTrans(pctxt->am, tmp, NULL,
12961 counter);
12962 if ((particle->minOccurs == 0) || (ret == 1)) {
12963 xmlAutomataNewEpsilon(pctxt->am,
12964 oldstate, pctxt->state);
12965 ret = 1;
12966 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012967 } else {
Daniel Veillard4013e832009-08-26 17:24:31 +020012968 sub = particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012969 while (sub != NULL) {
Daniel Veillard4013e832009-08-26 17:24:31 +020012970 tmp2 = xmlSchemaBuildAContentModel(pctxt,
12971 (xmlSchemaParticlePtr) sub);
12972 if (tmp2 != 1) ret = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012973 sub = sub->next;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012974 }
Daniel Veillardc70d1852012-08-23 23:28:04 +080012975
12976 /*
12977 * epsilon needed to block previous trans from
12978 * being allowed to enter back from another
12979 * construct
12980 */
12981 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
12982 pctxt->state, NULL);
12983
Daniel Veillard4013e832009-08-26 17:24:31 +020012984 if (particle->minOccurs == 0) {
12985 xmlAutomataNewEpsilon(pctxt->am, oldstate,
12986 pctxt->state);
12987 ret = 1;
12988 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012989 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012990 }
Daniel Veillard4013e832009-08-26 17:24:31 +020012991 break;
12992 }
12993 case XML_SCHEMA_TYPE_CHOICE:{
12994 xmlSchemaTreeItemPtr sub;
12995 xmlAutomataStatePtr start, end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012996
Daniel Veillard4013e832009-08-26 17:24:31 +020012997 ret = 0;
12998 start = pctxt->state;
12999 end = xmlAutomataNewState(pctxt->am);
13000
13001 /*
13002 * iterate over the subtypes and remerge the end with an
13003 * epsilon transition
13004 */
13005 if (particle->maxOccurs == 1) {
13006 sub = particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013007 while (sub != NULL) {
Daniel Veillard4013e832009-08-26 17:24:31 +020013008 pctxt->state = start;
13009 tmp2 = xmlSchemaBuildAContentModel(pctxt,
13010 (xmlSchemaParticlePtr) sub);
13011 if (tmp2 == 1) ret = 1;
13012 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
13013 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013014 }
Daniel Veillard4013e832009-08-26 17:24:31 +020013015 } else {
13016 int counter;
13017 xmlAutomataStatePtr hop, base;
13018 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
13019 UNBOUNDED : particle->maxOccurs - 1;
13020 int minOccurs =
13021 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
13022
13023 /*
13024 * use a counter to keep track of the number of transtions
13025 * which went through the choice.
13026 */
13027 counter =
13028 xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
13029 hop = xmlAutomataNewState(pctxt->am);
13030 base = xmlAutomataNewState(pctxt->am);
13031
13032 sub = particle->children->children;
13033 while (sub != NULL) {
13034 pctxt->state = base;
13035 tmp2 = xmlSchemaBuildAContentModel(pctxt,
13036 (xmlSchemaParticlePtr) sub);
13037 if (tmp2 == 1) ret = 1;
13038 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
13039 sub = sub->next;
Daniel Veillardbd56c442009-08-12 15:39:23 +020013040 }
Daniel Veillard4013e832009-08-26 17:24:31 +020013041 xmlAutomataNewEpsilon(pctxt->am, start, base);
13042 xmlAutomataNewCountedTrans(pctxt->am, hop, base, counter);
13043 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
13044 if (ret == 1)
13045 xmlAutomataNewEpsilon(pctxt->am, base, end);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013046 }
Daniel Veillard4013e832009-08-26 17:24:31 +020013047 if (particle->minOccurs == 0) {
13048 xmlAutomataNewEpsilon(pctxt->am, start, end);
13049 ret = 1;
13050 }
13051 pctxt->state = end;
13052 break;
13053 }
13054 case XML_SCHEMA_TYPE_ALL:{
13055 xmlAutomataStatePtr start, tmp;
13056 xmlSchemaParticlePtr sub;
13057 xmlSchemaElementPtr elemDecl;
13058
13059 ret = 1;
13060
13061 sub = (xmlSchemaParticlePtr) particle->children->children;
13062 if (sub == NULL)
13063 break;
13064
13065 ret = 0;
13066
13067 start = pctxt->state;
13068 tmp = xmlAutomataNewState(pctxt->am);
13069 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, tmp);
13070 pctxt->state = tmp;
13071 while (sub != NULL) {
13072 pctxt->state = tmp;
13073
13074 elemDecl = (xmlSchemaElementPtr) sub->children;
13075 if (elemDecl == NULL) {
13076 PERROR_INT("xmlSchemaBuildAContentModel",
13077 "<element> particle has no term");
13078 return(ret);
13079 };
13080 /*
13081 * NOTE: The {max occurs} of all the particles in the
13082 * {particles} of the group must be 0 or 1; this is
13083 * already ensured during the parse of the content of
13084 * <all>.
13085 */
13086 if (elemDecl->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
13087 int counter;
13088
13089 /*
13090 * This is an abstract group, we need to share
13091 * the same counter for all the element transitions
13092 * derived from the group
13093 */
13094 counter = xmlAutomataNewCounter(pctxt->am,
13095 sub->minOccurs, sub->maxOccurs);
13096 xmlSchemaBuildContentModelForSubstGroup(pctxt,
13097 sub, counter, pctxt->state);
13098 } else {
13099 if ((sub->minOccurs == 1) &&
13100 (sub->maxOccurs == 1)) {
13101 xmlAutomataNewOnceTrans2(pctxt->am, pctxt->state,
13102 pctxt->state,
13103 elemDecl->name,
13104 elemDecl->targetNamespace,
13105 1, 1, elemDecl);
13106 } else if ((sub->minOccurs == 0) &&
13107 (sub->maxOccurs == 1)) {
13108
13109 xmlAutomataNewCountTrans2(pctxt->am, pctxt->state,
13110 pctxt->state,
13111 elemDecl->name,
13112 elemDecl->targetNamespace,
13113 0,
13114 1,
13115 elemDecl);
13116 }
13117 }
13118 sub = (xmlSchemaParticlePtr) sub->next;
13119 }
13120 pctxt->state =
13121 xmlAutomataNewAllTrans(pctxt->am, pctxt->state, NULL, 0);
13122 if (particle->minOccurs == 0) {
13123 xmlAutomataNewEpsilon(pctxt->am, start, pctxt->state);
13124 ret = 1;
13125 }
13126 break;
13127 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013128 case XML_SCHEMA_TYPE_GROUP:
13129 /*
13130 * If we hit a model group definition, then this means that
13131 * it was empty, thus was not substituted for the containing
13132 * model group. Just do nothing in this case.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013133 * TODO: But the group should be substituted and not occur at
13134 * all in the content model at this point. Fix this.
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013135 */
Daniel Veillard4013e832009-08-26 17:24:31 +020013136 ret = 1;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000013137 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013138 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013139 xmlSchemaInternalErr2(ACTXT_CAST pctxt,
13140 "xmlSchemaBuildAContentModel",
13141 "found unexpected term of type '%s' in content model",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013142 WXS_ITEM_TYPE_NAME(particle->children), NULL);
Daniel Veillard4013e832009-08-26 17:24:31 +020013143 return(ret);
Daniel Veillard4255d502002-04-16 15:50:10 +000013144 }
Daniel Veillard4013e832009-08-26 17:24:31 +020013145 return(ret);
Daniel Veillard4255d502002-04-16 15:50:10 +000013146}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013147
Daniel Veillard4255d502002-04-16 15:50:10 +000013148/**
13149 * xmlSchemaBuildContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000013150 * @ctxt: the schema parser context
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013151 * @type: the complex type definition
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013152 * @name: the element name
Daniel Veillard4255d502002-04-16 15:50:10 +000013153 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013154 * Builds the content model of the complex type.
Daniel Veillard4255d502002-04-16 15:50:10 +000013155 */
13156static void
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013157xmlSchemaBuildContentModel(xmlSchemaTypePtr type,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013158 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013159{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013160 if ((type->type != XML_SCHEMA_TYPE_COMPLEX) ||
13161 (type->contModel != NULL) ||
13162 ((type->contentType != XML_SCHEMA_CONTENT_ELEMENTS) &&
13163 (type->contentType != XML_SCHEMA_CONTENT_MIXED)))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013164 return;
Daniel Veillarddecd64d2002-04-18 14:41:51 +000013165
13166#ifdef DEBUG_CONTENT
13167 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013168 "Building content model for %s\n", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000013169#endif
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013170 ctxt->am = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013171 ctxt->am = xmlNewAutomata();
13172 if (ctxt->am == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013173 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013174 "Cannot create automata for complex type %s\n", type->name);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013175 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000013176 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013177 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013178 /*
13179 * Build the automaton.
13180 */
13181 xmlSchemaBuildAContentModel(ctxt, WXS_TYPE_PARTICLE(type));
Daniel Veillard4255d502002-04-16 15:50:10 +000013182 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013183 type->contModel = xmlAutomataCompile(ctxt->am);
13184 if (type->contModel == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013185 xmlSchemaPCustomErr(ctxt,
13186 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013187 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013188 "Failed to compile the content model", NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013189 } else if (xmlRegexpIsDeterminist(type->contModel) != 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013190 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000013191 XML_SCHEMAP_NOT_DETERMINISTIC,
13192 /* XML_SCHEMAS_ERR_NOTDETERMINIST, */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013193 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013194 "The content model is not determinist", NULL);
Daniel Veillarde19fc232002-04-22 16:01:24 +000013195 } else {
Daniel Veillard118aed72002-09-24 14:13:13 +000013196#ifdef DEBUG_CONTENT_REGEXP
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013197 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013198 "Content model of %s:\n", type->name);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013199 xmlRegexpPrint(stderr, type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +000013200#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +000013201 }
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013202 ctxt->state = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013203 xmlFreeAutomata(ctxt->am);
13204 ctxt->am = NULL;
13205}
13206
13207/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013208 * xmlSchemaResolveElementReferences:
Daniel Veillard4255d502002-04-16 15:50:10 +000013209 * @elem: the schema element context
13210 * @ctxt: the schema parser context
13211 *
Daniel Veillardc0826a72004-08-10 14:17:33 +000013212 * Resolves the references of an element declaration
13213 * or particle, which has an element declaration as it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013214 * term.
Daniel Veillard4255d502002-04-16 15:50:10 +000013215 */
13216static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013217xmlSchemaResolveElementReferences(xmlSchemaElementPtr elemDecl,
13218 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000013219{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013220 if ((ctxt == NULL) || (elemDecl == NULL) ||
13221 ((elemDecl != NULL) &&
13222 (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_RESOLVED)))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013223 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013224 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_RESOLVED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013225
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013226 if ((elemDecl->subtypes == NULL) && (elemDecl->namedType != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013227 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013228
Jan Pokorný761c9e92013-11-29 23:26:27 +010013229 /* (type definition) ... otherwise the type definition `resolved`
13230 * to by the `actual value` of the type [attribute] ...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013231 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013232 type = xmlSchemaGetType(ctxt->schema, elemDecl->namedType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013233 elemDecl->namedTypeNs);
13234 if (type == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013235 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013236 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013237 WXS_BASIC_CAST elemDecl, elemDecl->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013238 "type", elemDecl->namedType, elemDecl->namedTypeNs,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013239 XML_SCHEMA_TYPE_BASIC, "type definition");
13240 } else
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013241 elemDecl->subtypes = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013242 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013243 if (elemDecl->substGroup != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013244 xmlSchemaElementPtr substHead;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013245
Daniel Veillardc0826a72004-08-10 14:17:33 +000013246 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013247 * FIXME TODO: Do we need a new field in _xmlSchemaElement for
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013248 * substitutionGroup?
Daniel Veillard01fa6152004-06-29 17:04:39 +000013249 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013250 substHead = xmlSchemaGetElem(ctxt->schema, elemDecl->substGroup,
13251 elemDecl->substGroupNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013252 if (substHead == NULL) {
13253 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013254 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013255 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013256 "substitutionGroup", elemDecl->substGroup,
13257 elemDecl->substGroupNs, XML_SCHEMA_TYPE_ELEMENT, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013258 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013259 xmlSchemaResolveElementReferences(substHead, ctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013260 /*
13261 * Set the "substitution group affiliation".
13262 * NOTE that now we use the "refDecl" field for this.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013263 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013264 WXS_SUBST_HEAD(elemDecl) = substHead;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013265 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013266 * The type definitions is set to:
13267 * SPEC "...the {type definition} of the element
Jan Pokorný761c9e92013-11-29 23:26:27 +010013268 * declaration `resolved` to by the `actual value`
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013269 * of the substitutionGroup [attribute], if present"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013270 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013271 if (elemDecl->subtypes == NULL)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013272 elemDecl->subtypes = substHead->subtypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013273 }
13274 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013275 /*
13276 * SPEC "The definition of anyType serves as the default type definition
13277 * for element declarations whose XML representation does not specify one."
13278 */
13279 if ((elemDecl->subtypes == NULL) &&
13280 (elemDecl->namedType == NULL) &&
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013281 (elemDecl->substGroup == NULL))
13282 elemDecl->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
William M. Bracke7091952004-05-11 15:09:58 +000013283}
13284
13285/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013286 * xmlSchemaResolveUnionMemberTypes:
William M. Bracke7091952004-05-11 15:09:58 +000013287 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013288 * @type: the schema simple type definition
William M. Bracke7091952004-05-11 15:09:58 +000013289 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013290 * Checks and builds the "member type definitions" property of the union
13291 * simple type. This handles part (1), part (2) is done in
13292 * xmlSchemaFinishMemberTypeDefinitionsProperty()
13293 *
Daniel Veillard01fa6152004-06-29 17:04:39 +000013294 * Returns -1 in case of an internal error, 0 otherwise.
William M. Bracke7091952004-05-11 15:09:58 +000013295 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013296static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013297xmlSchemaResolveUnionMemberTypes(xmlSchemaParserCtxtPtr ctxt,
13298 xmlSchemaTypePtr type)
Daniel Veillard377e1a92004-04-16 16:30:05 +000013299{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013300
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013301 xmlSchemaTypeLinkPtr link, lastLink, newLink;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013302 xmlSchemaTypePtr memberType;
Daniel Veillard377e1a92004-04-16 16:30:05 +000013303
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013304 /*
13305 * SPEC (1) "If the <union> alternative is chosen, then [Definition:]
Jan Pokorný761c9e92013-11-29 23:26:27 +010013306 * define the explicit members as the type definitions `resolved`
13307 * to by the items in the `actual value` of the memberTypes [attribute],
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013308 * if any, followed by the type definitions corresponding to the
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013309 * <simpleType>s among the [children] of <union>, if any."
Daniel Veillard01fa6152004-06-29 17:04:39 +000013310 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013311 /*
13312 * Resolve references.
13313 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013314 link = type->memberTypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013315 lastLink = NULL;
13316 while (link != NULL) {
13317 const xmlChar *name, *nsName;
Daniel Veillardc0826a72004-08-10 14:17:33 +000013318
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013319 name = ((xmlSchemaQNameRefPtr) link->type)->name;
13320 nsName = ((xmlSchemaQNameRefPtr) link->type)->targetNamespace;
13321
13322 memberType = xmlSchemaGetType(ctxt->schema, name, nsName);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013323 if ((memberType == NULL) || (! WXS_IS_SIMPLE(memberType))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013324 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013325 WXS_BASIC_CAST type, type->node, "memberTypes",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013326 name, nsName, XML_SCHEMA_TYPE_SIMPLE, NULL);
13327 /*
13328 * Remove the member type link.
13329 */
13330 if (lastLink == NULL)
13331 type->memberTypes = link->next;
13332 else
13333 lastLink->next = link->next;
13334 newLink = link;
13335 link = link->next;
13336 xmlFree(newLink);
13337 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013338 link->type = memberType;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013339 lastLink = link;
13340 link = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013341 }
13342 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013343 /*
13344 * Add local simple types,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013345 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013346 memberType = type->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013347 while (memberType != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013348 link = (xmlSchemaTypeLinkPtr) xmlMalloc(sizeof(xmlSchemaTypeLink));
13349 if (link == NULL) {
13350 xmlSchemaPErrMemory(ctxt, "allocating a type link", NULL);
13351 return (-1);
13352 }
13353 link->type = memberType;
13354 link->next = NULL;
13355 if (lastLink == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013356 type->memberTypes = link;
13357 else
Daniel Veillard01fa6152004-06-29 17:04:39 +000013358 lastLink->next = link;
13359 lastLink = link;
13360 memberType = memberType->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013361 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013362 return (0);
Daniel Veillard377e1a92004-04-16 16:30:05 +000013363}
13364
Daniel Veillard4255d502002-04-16 15:50:10 +000013365/**
Daniel Veillard3646d642004-06-02 19:19:14 +000013366 * xmlSchemaIsDerivedFromBuiltInType:
13367 * @ctxt: the schema parser context
13368 * @type: the type definition
13369 * @valType: the value type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013370 *
Daniel Veillard3646d642004-06-02 19:19:14 +000013371 *
13372 * Returns 1 if the type has the given value type, or
13373 * is derived from such a type.
13374 */
William M. Brack803812b2004-06-03 02:11:24 +000013375static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013376xmlSchemaIsDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
Daniel Veillard3646d642004-06-02 19:19:14 +000013377{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013378 if (type == NULL)
13379 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013380 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013381 return (0);
13382 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13383 if (type->builtInType == valType)
13384 return(1);
13385 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13386 (type->builtInType == XML_SCHEMAS_ANYTYPE))
13387 return (0);
13388 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard6a0baa02005-12-10 11:11:12 +000013389 }
13390 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard3646d642004-06-02 19:19:14 +000013391}
13392
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013393#if 0
13394/**
13395 * xmlSchemaIsDerivedFromBuiltInType:
13396 * @ctxt: the schema parser context
13397 * @type: the type definition
13398 * @valType: the value type
13399 *
13400 *
13401 * Returns 1 if the type has the given value type, or
13402 * is derived from such a type.
13403 */
13404static int
13405xmlSchemaIsUserDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
13406{
13407 if (type == NULL)
13408 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013409 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013410 return (0);
13411 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13412 if (type->builtInType == valType)
13413 return(1);
13414 return (0);
13415 } else
13416 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13417
13418 return (0);
13419}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013420
13421static xmlSchemaTypePtr
13422xmlSchemaQueryBuiltInType(xmlSchemaTypePtr type)
13423{
13424 if (type == NULL)
13425 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013426 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013427 return (NULL);
13428 if (type->type == XML_SCHEMA_TYPE_BASIC)
Daniel Veillard6a0baa02005-12-10 11:11:12 +000013429 return(type);
13430 return(xmlSchemaQueryBuiltInType(type->subtypes));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013431}
Daniel Veillard15724252008-08-30 15:01:04 +000013432#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013433
Daniel Veillard3646d642004-06-02 19:19:14 +000013434/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013435 * xmlSchemaGetPrimitiveType:
Daniel Veillard01fa6152004-06-29 17:04:39 +000013436 * @type: the simpleType definition
13437 *
13438 * Returns the primitive type of the given type or
13439 * NULL in case of error.
13440 */
13441static xmlSchemaTypePtr
13442xmlSchemaGetPrimitiveType(xmlSchemaTypePtr type)
13443{
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013444
Daniel Veillard01fa6152004-06-29 17:04:39 +000013445 while (type != NULL) {
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013446 /*
13447 * Note that anySimpleType is actually not a primitive type
13448 * but we need that here.
13449 */
13450 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13451 (type->flags & XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE))
Daniel Veillard01fa6152004-06-29 17:04:39 +000013452 return (type);
13453 type = type->baseType;
13454 }
13455
13456 return (NULL);
13457}
13458
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013459#if 0
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013460/**
13461 * xmlSchemaGetBuiltInTypeAncestor:
13462 * @type: the simpleType definition
13463 *
13464 * Returns the primitive type of the given type or
13465 * NULL in case of error.
13466 */
13467static xmlSchemaTypePtr
13468xmlSchemaGetBuiltInTypeAncestor(xmlSchemaTypePtr type)
13469{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013470 if (WXS_IS_LIST(type) || WXS_IS_UNION(type))
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000013471 return (0);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013472 while (type != NULL) {
13473 if (type->type == XML_SCHEMA_TYPE_BASIC)
13474 return (type);
13475 type = type->baseType;
13476 }
13477
13478 return (NULL);
13479}
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013480#endif
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013481
Daniel Veillard01fa6152004-06-29 17:04:39 +000013482/**
Daniel Veillard50355f02004-06-08 17:52:16 +000013483 * xmlSchemaCloneWildcardNsConstraints:
13484 * @ctxt: the schema parser context
13485 * @dest: the destination wildcard
13486 * @source: the source wildcard
13487 *
13488 * Clones the namespace constraints of source
13489 * and assignes them to dest.
13490 * Returns -1 on internal error, 0 otherwise.
13491 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013492static int
13493xmlSchemaCloneWildcardNsConstraints(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013494 xmlSchemaWildcardPtr dest,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013495 xmlSchemaWildcardPtr source)
Daniel Veillard3646d642004-06-02 19:19:14 +000013496{
13497 xmlSchemaWildcardNsPtr cur, tmp, last;
13498
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013499 if ((source == NULL) || (dest == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013500 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013501 dest->any = source->any;
Daniel Veillard3646d642004-06-02 19:19:14 +000013502 cur = source->nsSet;
13503 last = NULL;
13504 while (cur != NULL) {
13505 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
13506 if (tmp == NULL)
13507 return(-1);
13508 tmp->value = cur->value;
13509 if (last == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013510 dest->nsSet = tmp;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013511 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013512 last->next = tmp;
13513 last = tmp;
13514 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013515 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013516 if (dest->negNsSet != NULL)
13517 xmlSchemaFreeWildcardNsSet(dest->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +000013518 if (source->negNsSet != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013519 dest->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13520 if (dest->negNsSet == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013521 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013522 dest->negNsSet->value = source->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013523 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013524 dest->negNsSet = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000013525 return(0);
13526}
13527
Daniel Veillard50355f02004-06-08 17:52:16 +000013528/**
13529 * xmlSchemaUnionWildcards:
13530 * @ctxt: the schema parser context
13531 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013532 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013533 *
13534 * Unions the namespace constraints of the given wildcards.
13535 * @completeWild will hold the resulting union.
13536 * Returns a positive error code on failure, -1 in case of an
13537 * internal error, 0 otherwise.
13538 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013539static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013540xmlSchemaUnionWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013541 xmlSchemaWildcardPtr completeWild,
13542 xmlSchemaWildcardPtr curWild)
13543{
13544 xmlSchemaWildcardNsPtr cur, curB, tmp;
13545
13546 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013547 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013548 * value.
13549 */
13550 if ((completeWild->any == curWild->any) &&
13551 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13552 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013553
Daniel Veillard3646d642004-06-02 19:19:14 +000013554 if ((completeWild->negNsSet == NULL) ||
13555 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013556
Daniel Veillard3646d642004-06-02 19:19:14 +000013557 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013558 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013559
13560 /*
13561 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013562 */
13563 cur = completeWild->nsSet;
13564 while (cur != NULL) {
13565 found = 0;
13566 curB = curWild->nsSet;
13567 while (curB != NULL) {
13568 if (cur->value == curB->value) {
13569 found = 1;
13570 break;
13571 }
13572 curB = curB->next;
13573 }
13574 if (!found)
13575 break;
13576 cur = cur->next;
13577 }
13578 if (found)
13579 return(0);
13580 } else
13581 return(0);
13582 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013583 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013584 /*
13585 * 2 If either O1 or O2 is any, then any must be the value
13586 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013587 if (completeWild->any != curWild->any) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013588 if (completeWild->any == 0) {
13589 completeWild->any = 1;
13590 if (completeWild->nsSet != NULL) {
13591 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13592 completeWild->nsSet = NULL;
13593 }
13594 if (completeWild->negNsSet != NULL) {
13595 xmlFree(completeWild->negNsSet);
13596 completeWild->negNsSet = NULL;
13597 }
13598 }
Daniel Veillard50355f02004-06-08 17:52:16 +000013599 return (0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013600 }
13601 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010013602 * 3 If both O1 and O2 are sets of (namespace names or `absent`),
Daniel Veillard3646d642004-06-02 19:19:14 +000013603 * then the union of those sets must be the value.
13604 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013605 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013606 int found;
13607 xmlSchemaWildcardNsPtr start;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013608
Daniel Veillard3646d642004-06-02 19:19:14 +000013609 cur = curWild->nsSet;
13610 start = completeWild->nsSet;
13611 while (cur != NULL) {
13612 found = 0;
13613 curB = start;
13614 while (curB != NULL) {
13615 if (cur->value == curB->value) {
13616 found = 1;
13617 break;
13618 }
13619 curB = curB->next;
13620 }
13621 if (!found) {
13622 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013623 if (tmp == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013624 return (-1);
13625 tmp->value = cur->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013626 tmp->next = completeWild->nsSet;
Daniel Veillard3646d642004-06-02 19:19:14 +000013627 completeWild->nsSet = tmp;
13628 }
13629 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013630 }
13631
Daniel Veillard3646d642004-06-02 19:19:14 +000013632 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013633 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013634 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013635 * 4 If the two are negations of different values (namespace names
Jan Pokorný761c9e92013-11-29 23:26:27 +010013636 * or `absent`), then a pair of not and `absent` must be the value.
Daniel Veillard3646d642004-06-02 19:19:14 +000013637 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013638 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013639 (curWild->negNsSet != NULL) &&
13640 (completeWild->negNsSet->value != curWild->negNsSet->value)) {
13641 completeWild->negNsSet->value = NULL;
Daniel Veillard50355f02004-06-08 17:52:16 +000013642
13643 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013644 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013645 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013646 * 5.
13647 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013648 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013649 (completeWild->negNsSet->value != NULL) &&
13650 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013651 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013652 (curWild->negNsSet->value != NULL) &&
13653 (completeWild->nsSet != NULL))) {
13654
13655 int nsFound, absentFound = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013656
Daniel Veillard3646d642004-06-02 19:19:14 +000013657 if (completeWild->nsSet != NULL) {
13658 cur = completeWild->nsSet;
13659 curB = curWild->negNsSet;
13660 } else {
13661 cur = curWild->nsSet;
13662 curB = completeWild->negNsSet;
13663 }
13664 nsFound = 0;
13665 while (cur != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013666 if (cur->value == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013667 absentFound = 1;
13668 else if (cur->value == curB->value)
13669 nsFound = 1;
13670 if (nsFound && absentFound)
13671 break;
13672 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013673 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013674
13675 if (nsFound && absentFound) {
13676 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013677 * 5.1 If the set S includes both the negated namespace
Jan Pokorný761c9e92013-11-29 23:26:27 +010013678 * name and `absent`, then any must be the value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013679 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013680 completeWild->any = 1;
13681 if (completeWild->nsSet != NULL) {
13682 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13683 completeWild->nsSet = NULL;
13684 }
13685 if (completeWild->negNsSet != NULL) {
13686 xmlFree(completeWild->negNsSet);
13687 completeWild->negNsSet = NULL;
13688 }
13689 } else if (nsFound && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013690 /*
13691 * 5.2 If the set S includes the negated namespace name
Jan Pokorný761c9e92013-11-29 23:26:27 +010013692 * but not `absent`, then a pair of not and `absent` must
Daniel Veillard3646d642004-06-02 19:19:14 +000013693 * be the value.
13694 */
13695 if (completeWild->nsSet != NULL) {
13696 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13697 completeWild->nsSet = NULL;
13698 }
13699 if (completeWild->negNsSet == NULL) {
13700 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13701 if (completeWild->negNsSet == NULL)
13702 return (-1);
13703 }
13704 completeWild->negNsSet->value = NULL;
13705 } else if ((!nsFound) && absentFound) {
13706 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010013707 * 5.3 If the set S includes `absent` but not the negated
Daniel Veillard3646d642004-06-02 19:19:14 +000013708 * namespace name, then the union is not expressible.
13709 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013710 xmlSchemaPErr(ctxt, completeWild->node,
Daniel Veillard3646d642004-06-02 19:19:14 +000013711 XML_SCHEMAP_UNION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013712 "The union of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013713 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013714 return(XML_SCHEMAP_UNION_NOT_EXPRESSIBLE);
Daniel Veillard3646d642004-06-02 19:19:14 +000013715 } else if ((!nsFound) && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013716 /*
13717 * 5.4 If the set S does not include either the negated namespace
Jan Pokorný761c9e92013-11-29 23:26:27 +010013718 * name or `absent`, then whichever of O1 or O2 is a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013719 * and a namespace name must be the value.
13720 */
13721 if (completeWild->negNsSet == NULL) {
13722 if (completeWild->nsSet != NULL) {
13723 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13724 completeWild->nsSet = NULL;
13725 }
13726 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13727 if (completeWild->negNsSet == NULL)
13728 return (-1);
13729 completeWild->negNsSet->value = curWild->negNsSet->value;
13730 }
13731 }
13732 return (0);
13733 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013734 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013735 * 6.
13736 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013737 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013738 (completeWild->negNsSet->value == NULL) &&
13739 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013740 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013741 (curWild->negNsSet->value == NULL) &&
13742 (completeWild->nsSet != NULL))) {
13743
13744 if (completeWild->nsSet != NULL) {
13745 cur = completeWild->nsSet;
13746 } else {
13747 cur = curWild->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013748 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013749 while (cur != NULL) {
13750 if (cur->value == NULL) {
13751 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010013752 * 6.1 If the set S includes `absent`, then any must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013753 * value.
13754 */
13755 completeWild->any = 1;
13756 if (completeWild->nsSet != NULL) {
13757 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13758 completeWild->nsSet = NULL;
13759 }
13760 if (completeWild->negNsSet != NULL) {
13761 xmlFree(completeWild->negNsSet);
13762 completeWild->negNsSet = NULL;
13763 }
13764 return (0);
13765 }
13766 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013767 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013768 if (completeWild->negNsSet == NULL) {
13769 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010013770 * 6.2 If the set S does not include `absent`, then a pair of not
13771 * and `absent` must be the value.
Daniel Veillard3646d642004-06-02 19:19:14 +000013772 */
13773 if (completeWild->nsSet != NULL) {
13774 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13775 completeWild->nsSet = NULL;
13776 }
13777 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13778 if (completeWild->negNsSet == NULL)
13779 return (-1);
13780 completeWild->negNsSet->value = NULL;
13781 }
13782 return (0);
13783 }
13784 return (0);
13785
13786}
13787
Daniel Veillard50355f02004-06-08 17:52:16 +000013788/**
13789 * xmlSchemaIntersectWildcards:
13790 * @ctxt: the schema parser context
13791 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013792 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013793 *
13794 * Intersects the namespace constraints of the given wildcards.
13795 * @completeWild will hold the resulting intersection.
13796 * Returns a positive error code on failure, -1 in case of an
13797 * internal error, 0 otherwise.
13798 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013799static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013800xmlSchemaIntersectWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013801 xmlSchemaWildcardPtr completeWild,
13802 xmlSchemaWildcardPtr curWild)
13803{
William M. Brack803812b2004-06-03 02:11:24 +000013804 xmlSchemaWildcardNsPtr cur, curB, prev, tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013805
13806 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013807 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013808 * value.
13809 */
13810 if ((completeWild->any == curWild->any) &&
13811 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13812 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013813
Daniel Veillard3646d642004-06-02 19:19:14 +000013814 if ((completeWild->negNsSet == NULL) ||
13815 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013816
Daniel Veillard3646d642004-06-02 19:19:14 +000013817 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013818 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013819
13820 /*
13821 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013822 */
13823 cur = completeWild->nsSet;
13824 while (cur != NULL) {
13825 found = 0;
13826 curB = curWild->nsSet;
13827 while (curB != NULL) {
13828 if (cur->value == curB->value) {
13829 found = 1;
13830 break;
13831 }
13832 curB = curB->next;
13833 }
13834 if (!found)
13835 break;
13836 cur = cur->next;
13837 }
13838 if (found)
13839 return(0);
13840 } else
13841 return(0);
13842 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013843 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013844 /*
13845 * 2 If either O1 or O2 is any, then the other must be the value.
13846 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013847 if ((completeWild->any != curWild->any) && (completeWild->any)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013848 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013849 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013850 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013851 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013852 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013853 * 3 If either O1 or O2 is a pair of not and a value (a namespace
Jan Pokorný761c9e92013-11-29 23:26:27 +010013854 * name or `absent`) and the other is a set of (namespace names or
13855 * `absent`), then that set, minus the negated value if it was in
13856 * the set, minus `absent` if it was in the set, must be the value.
Daniel Veillard3646d642004-06-02 19:19:14 +000013857 */
13858 if (((completeWild->negNsSet != NULL) && (curWild->nsSet != NULL)) ||
13859 ((curWild->negNsSet != NULL) && (completeWild->nsSet != NULL))) {
13860 const xmlChar *neg;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013861
Daniel Veillard3646d642004-06-02 19:19:14 +000013862 if (completeWild->nsSet == NULL) {
13863 neg = completeWild->negNsSet->value;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013864 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Daniel Veillard3646d642004-06-02 19:19:14 +000013865 return(-1);
13866 } else
13867 neg = curWild->negNsSet->value;
13868 /*
13869 * Remove absent and negated.
13870 */
13871 prev = NULL;
13872 cur = completeWild->nsSet;
13873 while (cur != NULL) {
13874 if (cur->value == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013875 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013876 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013877 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013878 prev->next = cur->next;
13879 xmlFree(cur);
13880 break;
13881 }
13882 prev = cur;
13883 cur = cur->next;
13884 }
13885 if (neg != NULL) {
13886 prev = NULL;
13887 cur = completeWild->nsSet;
13888 while (cur != NULL) {
13889 if (cur->value == neg) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013890 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013891 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013892 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013893 prev->next = cur->next;
13894 xmlFree(cur);
13895 break;
13896 }
13897 prev = cur;
13898 cur = cur->next;
13899 }
13900 }
13901
13902 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013903 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013904 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010013905 * 4 If both O1 and O2 are sets of (namespace names or `absent`),
Daniel Veillard3646d642004-06-02 19:19:14 +000013906 * then the intersection of those sets must be the value.
13907 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013908 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013909 int found;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013910
Daniel Veillard3646d642004-06-02 19:19:14 +000013911 cur = completeWild->nsSet;
13912 prev = NULL;
13913 while (cur != NULL) {
13914 found = 0;
13915 curB = curWild->nsSet;
13916 while (curB != NULL) {
13917 if (cur->value == curB->value) {
13918 found = 1;
13919 break;
13920 }
13921 curB = curB->next;
13922 }
13923 if (!found) {
13924 if (prev == NULL)
13925 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013926 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013927 prev->next = cur->next;
13928 tmp = cur->next;
13929 xmlFree(cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013930 cur = tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013931 continue;
13932 }
13933 prev = cur;
13934 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013935 }
13936
Daniel Veillard3646d642004-06-02 19:19:14 +000013937 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013938 }
13939 /* 5 If the two are negations of different namespace names,
Daniel Veillard3646d642004-06-02 19:19:14 +000013940 * then the intersection is not expressible
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013941 */
13942 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013943 (curWild->negNsSet != NULL) &&
13944 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013945 (completeWild->negNsSet->value != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013946 (curWild->negNsSet->value != NULL)) {
13947
13948 xmlSchemaPErr(ctxt, completeWild->node, XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013949 "The intersection of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013950 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013951 return(XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013952 }
13953 /*
13954 * 6 If the one is a negation of a namespace name and the other
Jan Pokorný761c9e92013-11-29 23:26:27 +010013955 * is a negation of `absent`, then the one which is the negation
Daniel Veillard3646d642004-06-02 19:19:14 +000013956 * of a namespace name must be the value.
13957 */
13958 if ((completeWild->negNsSet != NULL) && (curWild->negNsSet != NULL) &&
13959 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013960 (completeWild->negNsSet->value == NULL)) {
13961 completeWild->negNsSet->value = curWild->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013962 }
13963 return(0);
13964}
13965
Daniel Veillard50355f02004-06-08 17:52:16 +000013966/**
13967 * xmlSchemaIsWildcardNsConstraintSubset:
13968 * @ctxt: the schema parser context
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013969 * @sub: the first wildcard
13970 * @super: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013971 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013972 * Schema Component Constraint: Wildcard Subset (cos-ns-subset)
13973 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013974 * Returns 0 if the namespace constraint of @sub is an intensional
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013975 * subset of @super, 1 otherwise.
Daniel Veillard50355f02004-06-08 17:52:16 +000013976 */
13977static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013978xmlSchemaCheckCOSNSSubset(xmlSchemaWildcardPtr sub,
13979 xmlSchemaWildcardPtr super)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013980{
Daniel Veillard50355f02004-06-08 17:52:16 +000013981 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013982 * 1 super must be any.
Daniel Veillard50355f02004-06-08 17:52:16 +000013983 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013984 if (super->any)
13985 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013986 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010013987 * 2.1 sub must be a pair of not and a namespace name or `absent`.
Daniel Veillard50355f02004-06-08 17:52:16 +000013988 * 2.2 super must be a pair of not and the same value.
13989 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013990 if ((sub->negNsSet != NULL) &&
13991 (super->negNsSet != NULL) &&
Ryan Sleevi5cf1deb2012-02-29 10:56:32 +080013992 (sub->negNsSet->value == super->negNsSet->value))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013993 return (0);
13994 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010013995 * 3.1 sub must be a set whose members are either namespace names or `absent`.
Daniel Veillard50355f02004-06-08 17:52:16 +000013996 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013997 if (sub->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013998 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013999 * 3.2.1 super must be the same set or a superset thereof.
Daniel Veillard50355f02004-06-08 17:52:16 +000014000 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014001 if (super->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000014002 xmlSchemaWildcardNsPtr cur, curB;
14003 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014004
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014005 cur = sub->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000014006 while (cur != NULL) {
14007 found = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014008 curB = super->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000014009 while (curB != NULL) {
14010 if (cur->value == curB->value) {
14011 found = 1;
14012 break;
14013 }
14014 curB = curB->next;
14015 }
14016 if (!found)
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014017 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000014018 cur = cur->next;
14019 }
14020 if (found)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014021 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014022 } else if (super->negNsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000014023 xmlSchemaWildcardNsPtr cur;
14024 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014025 * 3.2.2 super must be a pair of not and a namespace name or
Jan Pokorný761c9e92013-11-29 23:26:27 +010014026 * `absent` and that value must not be in sub's set.
Daniel Veillard50355f02004-06-08 17:52:16 +000014027 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014028 cur = sub->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014029 while (cur != NULL) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014030 if (cur->value == super->negNsSet->value)
14031 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000014032 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014033 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014034 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000014035 }
14036 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014037 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000014038}
14039
Daniel Veillard50355f02004-06-08 17:52:16 +000014040static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014041xmlSchemaGetEffectiveValueConstraint(xmlSchemaAttributeUsePtr attruse,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014042 int *fixed,
14043 const xmlChar **value,
14044 xmlSchemaValPtr *val)
14045{
14046 *fixed = 0;
14047 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014048 if (val != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014049 *val = NULL;
14050
Daniel Veillarddee23482008-04-11 12:58:43 +000014051 if (attruse->defValue != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014052 *value = attruse->defValue;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000014053 if (val != NULL)
14054 *val = attruse->defVal;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014055 if (attruse->flags & XML_SCHEMA_ATTR_USE_FIXED)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014056 *fixed = 1;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014057 return(1);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000014058 } else if ((attruse->attrDecl != NULL) &&
14059 (attruse->attrDecl->defValue != NULL)) {
14060 *value = attruse->attrDecl->defValue;
14061 if (val != NULL)
14062 *val = attruse->attrDecl->defVal;
14063 if (attruse->attrDecl->flags & XML_SCHEMAS_ATTR_FIXED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014064 *fixed = 1;
14065 return(1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014066 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014067 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000014068}
Daniel Veillard3646d642004-06-02 19:19:14 +000014069/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014070 * xmlSchemaCheckCVCWildcardNamespace:
Daniel Veillard3646d642004-06-02 19:19:14 +000014071 * @wild: the wildcard
14072 * @ns: the namespace
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014073 *
14074 * Validation Rule: Wildcard allows Namespace Name
14075 * (cvc-wildcard-namespace)
14076 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014077 * Returns 0 if the given namespace matches the wildcard,
14078 * 1 otherwise and -1 on API errors.
Daniel Veillard3646d642004-06-02 19:19:14 +000014079 */
14080static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014081xmlSchemaCheckCVCWildcardNamespace(xmlSchemaWildcardPtr wild,
14082 const xmlChar* ns)
Daniel Veillard3646d642004-06-02 19:19:14 +000014083{
14084 if (wild == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014085 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000014086
14087 if (wild->any)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014088 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000014089 else if (wild->nsSet != NULL) {
14090 xmlSchemaWildcardNsPtr cur;
14091
14092 cur = wild->nsSet;
14093 while (cur != NULL) {
14094 if (xmlStrEqual(cur->value, ns))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014095 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000014096 cur = cur->next;
14097 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014098 } else if ((wild->negNsSet != NULL) && (ns != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000014099 (!xmlStrEqual(wild->negNsSet->value, ns)))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014100 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014101
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014102 return(1);
14103}
14104
14105#define XML_SCHEMA_ACTION_DERIVE 0
14106#define XML_SCHEMA_ACTION_REDEFINE 1
14107
14108#define WXS_ACTION_STR(a) \
14109((a) == XML_SCHEMA_ACTION_DERIVE) ? (const xmlChar *) "base" : (const xmlChar *) "redefined"
14110
14111/*
14112* Schema Component Constraint:
14113* Derivation Valid (Restriction, Complex)
14114* derivation-ok-restriction (2) - (4)
14115*
14116* ATTENTION:
14117* In XML Schema 1.1 this will be:
14118* Validation Rule:
14119* Checking complex type subsumption (practicalSubsumption) (1, 2 and 3)
14120*
14121*/
14122static int
14123xmlSchemaCheckDerivationOKRestriction2to4(xmlSchemaParserCtxtPtr pctxt,
14124 int action,
14125 xmlSchemaBasicItemPtr item,
14126 xmlSchemaBasicItemPtr baseItem,
14127 xmlSchemaItemListPtr uses,
14128 xmlSchemaItemListPtr baseUses,
14129 xmlSchemaWildcardPtr wild,
14130 xmlSchemaWildcardPtr baseWild)
Daniel Veillarddee23482008-04-11 12:58:43 +000014131{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014132 xmlSchemaAttributeUsePtr cur = NULL, bcur;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014133 int i, j, found; /* err = 0; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014134 const xmlChar *bEffValue;
14135 int effFixed;
Daniel Veillarddee23482008-04-11 12:58:43 +000014136
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014137 if (uses != NULL) {
14138 for (i = 0; i < uses->nbItems; i++) {
14139 cur = uses->items[i];
14140 found = 0;
14141 if (baseUses == NULL)
14142 goto not_found;
14143 for (j = 0; j < baseUses->nbItems; j++) {
Daniel Veillarddee23482008-04-11 12:58:43 +000014144 bcur = baseUses->items[j];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014145 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14146 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14147 (WXS_ATTRUSE_DECL_TNS(cur) ==
14148 WXS_ATTRUSE_DECL_TNS(bcur)))
14149 {
14150 /*
14151 * (2.1) "If there is an attribute use in the {attribute
14152 * uses} of the {base type definition} (call this B) whose
14153 * {attribute declaration} has the same {name} and {target
14154 * namespace}, then all of the following must be true:"
14155 */
14156 found = 1;
Daniel Veillarddee23482008-04-11 12:58:43 +000014157
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014158 if ((cur->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
14159 (bcur->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED))
14160 {
14161 xmlChar *str = NULL;
14162 /*
14163 * (2.1.1) "one of the following must be true:"
14164 * (2.1.1.1) "B's {required} is false."
14165 * (2.1.1.2) "R's {required} is true."
14166 */
14167 xmlSchemaPAttrUseErr4(pctxt,
14168 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_1,
14169 WXS_ITEM_NODE(item), item, cur,
14170 "The 'optional' attribute use is inconsistent "
14171 "with the corresponding 'required' attribute use of "
14172 "the %s %s",
14173 WXS_ACTION_STR(action),
14174 xmlSchemaGetComponentDesignation(&str, baseItem),
14175 NULL, NULL);
14176 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014177 /* err = pctxt->err; */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014178 } else if (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014179 WXS_ATTRUSE_TYPEDEF(cur),
14180 WXS_ATTRUSE_TYPEDEF(bcur), 0) != 0)
14181 {
14182 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014183
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014184 /*
14185 * SPEC (2.1.2) "R's {attribute declaration}'s
14186 * {type definition} must be validly derived from
14187 * B's {type definition} given the empty set as
Jan Pokorný761c9e92013-11-29 23:26:27 +010014188 * defined in Type Derivation OK (Simple) ($3.14.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014189 */
14190 xmlSchemaPAttrUseErr4(pctxt,
14191 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_2,
14192 WXS_ITEM_NODE(item), item, cur,
14193 "The attribute declaration's %s "
14194 "is not validly derived from "
14195 "the corresponding %s of the "
14196 "attribute declaration in the %s %s",
14197 xmlSchemaGetComponentDesignation(&strA,
14198 WXS_ATTRUSE_TYPEDEF(cur)),
14199 xmlSchemaGetComponentDesignation(&strB,
14200 WXS_ATTRUSE_TYPEDEF(bcur)),
14201 WXS_ACTION_STR(action),
14202 xmlSchemaGetComponentDesignation(&strC, baseItem));
14203 /* xmlSchemaGetComponentDesignation(&str, baseItem), */
14204 FREE_AND_NULL(strA);
14205 FREE_AND_NULL(strB);
14206 FREE_AND_NULL(strC);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014207 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014208 } else {
14209 /*
14210 * 2.1.3 [Definition:] Let the effective value
14211 * constraint of an attribute use be its {value
14212 * constraint}, if present, otherwise its {attribute
14213 * declaration}'s {value constraint} .
14214 */
14215 xmlSchemaGetEffectiveValueConstraint(bcur,
14216 &effFixed, &bEffValue, NULL);
14217 /*
14218 * 2.1.3 ... one of the following must be true
14219 *
Jan Pokorný761c9e92013-11-29 23:26:27 +010014220 * 2.1.3.1 B's `effective value constraint` is
14221 * `absent` or default.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014222 */
14223 if ((bEffValue != NULL) &&
14224 (effFixed == 1)) {
14225 const xmlChar *rEffValue = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014226
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014227 xmlSchemaGetEffectiveValueConstraint(bcur,
14228 &effFixed, &rEffValue, NULL);
14229 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010014230 * 2.1.3.2 R's `effective value constraint` is
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014231 * fixed with the same string as B's.
14232 * MAYBE TODO: Compare the computed values.
14233 * Hmm, it says "same string" so
14234 * string-equality might really be sufficient.
14235 */
14236 if ((effFixed == 0) ||
14237 (! WXS_ARE_DEFAULT_STR_EQUAL(rEffValue, bEffValue)))
14238 {
14239 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014240
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014241 xmlSchemaPAttrUseErr4(pctxt,
14242 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_3,
14243 WXS_ITEM_NODE(item), item, cur,
14244 "The effective value constraint of the "
14245 "attribute use is inconsistent with "
14246 "its correspondent in the %s %s",
14247 WXS_ACTION_STR(action),
14248 xmlSchemaGetComponentDesignation(&str,
14249 baseItem),
14250 NULL, NULL);
14251 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014252 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014253 }
14254 }
14255 }
14256 break;
14257 }
14258 }
Daniel Veillarddee23482008-04-11 12:58:43 +000014259not_found:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014260 if (!found) {
14261 /*
14262 * (2.2) "otherwise the {base type definition} must have an
14263 * {attribute wildcard} and the {target namespace} of the
Jan Pokorný761c9e92013-11-29 23:26:27 +010014264 * R's {attribute declaration} must be `valid` with respect
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014265 * to that wildcard, as defined in Wildcard allows Namespace
Jan Pokorný761c9e92013-11-29 23:26:27 +010014266 * Name ($3.10.4)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014267 */
14268 if ((baseWild == NULL) ||
14269 (xmlSchemaCheckCVCWildcardNamespace(baseWild,
14270 (WXS_ATTRUSE_DECL(cur))->targetNamespace) != 0))
14271 {
14272 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014273
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014274 xmlSchemaPAttrUseErr4(pctxt,
14275 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_2,
14276 WXS_ITEM_NODE(item), item, cur,
14277 "Neither a matching attribute use, "
14278 "nor a matching wildcard exists in the %s %s",
14279 WXS_ACTION_STR(action),
14280 xmlSchemaGetComponentDesignation(&str, baseItem),
14281 NULL, NULL);
14282 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014283 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014284 }
14285 }
14286 }
14287 }
14288 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000014289 * SPEC derivation-ok-restriction (3):
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014290 * (3) "For each attribute use in the {attribute uses} of the {base type
14291 * definition} whose {required} is true, there must be an attribute
14292 * use with an {attribute declaration} with the same {name} and
14293 * {target namespace} as its {attribute declaration} in the {attribute
14294 * uses} of the complex type definition itself whose {required} is true.
14295 */
14296 if (baseUses != NULL) {
14297 for (j = 0; j < baseUses->nbItems; j++) {
14298 bcur = baseUses->items[j];
14299 if (bcur->occurs != XML_SCHEMAS_ATTR_USE_REQUIRED)
14300 continue;
14301 found = 0;
14302 if (uses != NULL) {
14303 for (i = 0; i < uses->nbItems; i++) {
Daniel Veillarddee23482008-04-11 12:58:43 +000014304 cur = uses->items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014305 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14306 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14307 (WXS_ATTRUSE_DECL_TNS(cur) ==
14308 WXS_ATTRUSE_DECL_TNS(bcur))) {
14309 found = 1;
14310 break;
14311 }
14312 }
14313 }
14314 if (!found) {
14315 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014316
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014317 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14318 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_3,
14319 NULL, item,
14320 "A matching attribute use for the "
14321 "'required' %s of the %s %s is missing",
14322 xmlSchemaGetComponentDesignation(&strA, bcur),
14323 WXS_ACTION_STR(action),
14324 xmlSchemaGetComponentDesignation(&strB, baseItem),
14325 NULL);
14326 FREE_AND_NULL(strA);
14327 FREE_AND_NULL(strB);
14328 }
14329 }
14330 }
14331 /*
14332 * derivation-ok-restriction (4)
14333 */
14334 if (wild != NULL) {
14335 /*
14336 * (4) "If there is an {attribute wildcard}, all of the
14337 * following must be true:"
Daniel Veillarddee23482008-04-11 12:58:43 +000014338 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014339 if (baseWild == NULL) {
14340 xmlChar *str = NULL;
14341
14342 /*
14343 * (4.1) "The {base type definition} must also have one."
Daniel Veillarddee23482008-04-11 12:58:43 +000014344 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014345 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14346 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_1,
14347 NULL, item,
14348 "The %s has an attribute wildcard, "
14349 "but the %s %s '%s' does not have one",
Daniel Veillarddee23482008-04-11 12:58:43 +000014350 WXS_ITEM_TYPE_NAME(item),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014351 WXS_ACTION_STR(action),
14352 WXS_ITEM_TYPE_NAME(baseItem),
14353 xmlSchemaGetComponentQName(&str, baseItem));
14354 FREE_AND_NULL(str);
14355 return(pctxt->err);
14356 } else if ((baseWild->any == 0) &&
14357 xmlSchemaCheckCOSNSSubset(wild, baseWild))
14358 {
14359 xmlChar *str = NULL;
14360 /*
14361 * (4.2) "The complex type definition's {attribute wildcard}'s
14362 * {namespace constraint} must be a subset of the {base type
14363 * definition}'s {attribute wildcard}'s {namespace constraint},
Jan Pokorný761c9e92013-11-29 23:26:27 +010014364 * as defined by Wildcard Subset ($3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014365 */
14366 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14367 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_2,
14368 NULL, item,
14369 "The attribute wildcard is not a valid "
14370 "subset of the wildcard in the %s %s '%s'",
14371 WXS_ACTION_STR(action),
14372 WXS_ITEM_TYPE_NAME(baseItem),
14373 xmlSchemaGetComponentQName(&str, baseItem),
14374 NULL);
14375 FREE_AND_NULL(str);
14376 return(pctxt->err);
14377 }
Jan Pokorný761c9e92013-11-29 23:26:27 +010014378 /* 4.3 Unless the {base type definition} is the `ur-type
14379 * definition`, the complex type definition's {attribute
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014380 * wildcard}'s {process contents} must be identical to or
14381 * stronger than the {base type definition}'s {attribute
14382 * wildcard}'s {process contents}, where strict is stronger
14383 * than lax is stronger than skip.
14384 */
14385 if ((! WXS_IS_ANYTYPE(baseItem)) &&
14386 (wild->processContents < baseWild->processContents)) {
14387 xmlChar *str = NULL;
14388 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14389 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_3,
14390 NULL, baseItem,
14391 "The {process contents} of the attribute wildcard is "
14392 "weaker than the one in the %s %s '%s'",
14393 WXS_ACTION_STR(action),
14394 WXS_ITEM_TYPE_NAME(baseItem),
14395 xmlSchemaGetComponentQName(&str, baseItem),
14396 NULL);
14397 FREE_AND_NULL(str)
14398 return(pctxt->err);
14399 }
14400 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014401 return(0);
14402}
14403
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014404
14405static int
14406xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
14407 xmlSchemaBasicItemPtr item,
14408 xmlSchemaWildcardPtr *completeWild,
14409 xmlSchemaItemListPtr list,
14410 xmlSchemaItemListPtr prohibs);
Daniel Veillard3646d642004-06-02 19:19:14 +000014411/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014412 * xmlSchemaFixupTypeAttributeUses:
Daniel Veillard3646d642004-06-02 19:19:14 +000014413 * @ctxt: the schema parser context
14414 * @type: the complex type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014415 *
Daniel Veillard3646d642004-06-02 19:19:14 +000014416 *
14417 * Builds the wildcard and the attribute uses on the given complex type.
14418 * Returns -1 if an internal error occurs, 0 otherwise.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014419 *
14420 * ATTENTION TODO: Experimantally this uses pointer comparisons for
14421 * strings, so recheck this if we start to hardcode some schemata, since
14422 * they might not be in the same dict.
14423 * NOTE: It is allowed to "extend" the xs:anyType type.
Daniel Veillard3646d642004-06-02 19:19:14 +000014424 */
14425static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014426xmlSchemaFixupTypeAttributeUses(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014427 xmlSchemaTypePtr type)
Daniel Veillard3646d642004-06-02 19:19:14 +000014428{
14429 xmlSchemaTypePtr baseType = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000014430 xmlSchemaAttributeUsePtr use;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014431 xmlSchemaItemListPtr uses, baseUses, prohibs = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000014432
Daniel Veillard01fa6152004-06-29 17:04:39 +000014433 if (type->baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014434 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014435 "no base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000014436 return (-1);
14437 }
Daniel Veillarddee23482008-04-11 12:58:43 +000014438 baseType = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014439 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014440 if (xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014441 return(-1);
14442
14443 uses = type->attrUses;
14444 baseUses = baseType->attrUses;
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014445 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014446 * Expand attribute group references. And build the 'complete'
14447 * wildcard, i.e. intersect multiple wildcards.
14448 * Move attribute prohibitions into a separate list.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014449 */
Daniel Veillarddee23482008-04-11 12:58:43 +000014450 if (uses != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014451 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014452 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014453 * This one will transfer all attr. prohibitions
14454 * into pctxt->attrProhibs.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014455 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014456 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14457 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14458 pctxt->attrProhibs) == -1)
14459 {
14460 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14461 "failed to expand attributes");
14462 }
14463 if (pctxt->attrProhibs->nbItems != 0)
14464 prohibs = pctxt->attrProhibs;
14465 } else {
14466 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14467 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14468 NULL) == -1)
14469 {
14470 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14471 "failed to expand attributes");
14472 }
14473 }
14474 }
14475 /*
14476 * Inherit the attribute uses of the base type.
14477 */
14478 if (baseUses != NULL) {
14479 int i, j;
14480 xmlSchemaAttributeUseProhibPtr pro;
14481
14482 if (WXS_IS_RESTRICTION(type)) {
14483 int usesCount;
Daniel Veillarddee23482008-04-11 12:58:43 +000014484 xmlSchemaAttributeUsePtr tmp;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014485
14486 if (uses != NULL)
14487 usesCount = uses->nbItems;
14488 else
14489 usesCount = 0;
14490
14491 /* Restriction. */
14492 for (i = 0; i < baseUses->nbItems; i++) {
14493 use = baseUses->items[i];
14494 if (prohibs) {
14495 /*
14496 * Filter out prohibited uses.
14497 */
14498 for (j = 0; j < prohibs->nbItems; j++) {
14499 pro = prohibs->items[j];
14500 if ((WXS_ATTRUSE_DECL_NAME(use) == pro->name) &&
14501 (WXS_ATTRUSE_DECL_TNS(use) ==
14502 pro->targetNamespace))
14503 {
14504 goto inherit_next;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014505 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014506 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014507 }
14508 if (usesCount) {
14509 /*
14510 * Filter out existing uses.
14511 */
14512 for (j = 0; j < usesCount; j++) {
14513 tmp = uses->items[j];
14514 if ((WXS_ATTRUSE_DECL_NAME(use) ==
14515 WXS_ATTRUSE_DECL_NAME(tmp)) &&
14516 (WXS_ATTRUSE_DECL_TNS(use) ==
14517 WXS_ATTRUSE_DECL_TNS(tmp)))
14518 {
14519 goto inherit_next;
14520 }
14521 }
14522 }
14523 if (uses == NULL) {
14524 type->attrUses = xmlSchemaItemListCreate();
14525 if (type->attrUses == NULL)
14526 goto exit_failure;
14527 uses = type->attrUses;
14528 }
14529 xmlSchemaItemListAddSize(uses, 2, use);
14530inherit_next: {}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014531 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014532 } else {
14533 /* Extension. */
Daniel Veillarddee23482008-04-11 12:58:43 +000014534 for (i = 0; i < baseUses->nbItems; i++) {
14535 use = baseUses->items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014536 if (uses == NULL) {
14537 type->attrUses = xmlSchemaItemListCreate();
14538 if (type->attrUses == NULL)
14539 goto exit_failure;
14540 uses = type->attrUses;
14541 }
Daniel Veillarddee23482008-04-11 12:58:43 +000014542 xmlSchemaItemListAddSize(uses, baseUses->nbItems, use);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014543 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014544 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014545 }
14546 /*
14547 * Shrink attr. uses.
14548 */
14549 if (uses) {
14550 if (uses->nbItems == 0) {
14551 xmlSchemaItemListFree(uses);
14552 type->attrUses = NULL;
14553 }
14554 /*
14555 * TODO: We could shrink the size of the array
14556 * to fit the actual number of items.
14557 */
14558 }
14559 /*
14560 * Compute the complete wildcard.
14561 */
Daniel Veillarddee23482008-04-11 12:58:43 +000014562 if (WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014563 if (baseType->attributeWildcard != NULL) {
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014564 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010014565 * (3.2.2.1) "If the `base wildcard` is non-`absent`, then
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014566 * the appropriate case among the following:"
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014567 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014568 if (type->attributeWildcard != NULL) {
14569 /*
14570 * Union the complete wildcard with the base wildcard.
14571 * SPEC {attribute wildcard}
14572 * (3.2.2.1.2) "otherwise a wildcard whose {process contents}
Jan Pokorný761c9e92013-11-29 23:26:27 +010014573 * and {annotation} are those of the `complete wildcard`,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014574 * and whose {namespace constraint} is the intensional union
Jan Pokorný761c9e92013-11-29 23:26:27 +010014575 * of the {namespace constraint} of the `complete wildcard`
14576 * and of the `base wildcard`, as defined in Attribute
14577 * Wildcard Union ($3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014578 */
14579 if (xmlSchemaUnionWildcards(pctxt, type->attributeWildcard,
14580 baseType->attributeWildcard) == -1)
Daniel Veillarddee23482008-04-11 12:58:43 +000014581 goto exit_failure;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014582 } else {
14583 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010014584 * (3.2.2.1.1) "If the `complete wildcard` is `absent`,
14585 * then the `base wildcard`."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014586 */
14587 type->attributeWildcard = baseType->attributeWildcard;
Daniel Veillarddee23482008-04-11 12:58:43 +000014588 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014589 } else {
14590 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010014591 * (3.2.2.2) "otherwise (the `base wildcard` is `absent`) the
14592 * `complete wildcard`"
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014593 * NOOP
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014594 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014595 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014596 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014597 /*
14598 * SPEC {attribute wildcard}
14599 * (3.1) "If the <restriction> alternative is chosen, then the
Jan Pokorný761c9e92013-11-29 23:26:27 +010014600 * `complete wildcard`;"
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014601 * NOOP
14602 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014603 }
Daniel Veillarddee23482008-04-11 12:58:43 +000014604
Daniel Veillard3646d642004-06-02 19:19:14 +000014605 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014606
14607exit_failure:
14608 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000014609}
14610
14611/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014612 * xmlSchemaTypeFinalContains:
14613 * @schema: the schema
14614 * @type: the type definition
14615 * @final: the final
14616 *
14617 * Evaluates if a type definition contains the given "final".
14618 * This does take "finalDefault" into account as well.
14619 *
14620 * Returns 1 if the type does containt the given "final",
14621 * 0 otherwise.
14622 */
14623static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014624xmlSchemaTypeFinalContains(xmlSchemaTypePtr type, int final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014625{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014626 if (type == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014627 return (0);
14628 if (type->flags & final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014629 return (1);
14630 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014631 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014632}
14633
14634/**
14635 * xmlSchemaGetUnionSimpleTypeMemberTypes:
14636 * @type: the Union Simple Type
14637 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014638 * Returns a list of member types of @type if existing,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014639 * returns NULL otherwise.
14640 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014641static xmlSchemaTypeLinkPtr
Daniel Veillard01fa6152004-06-29 17:04:39 +000014642xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type)
14643{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014644 while ((type != NULL) && (type->type == XML_SCHEMA_TYPE_SIMPLE)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014645 if (type->memberTypes != NULL)
14646 return (type->memberTypes);
14647 else
14648 type = type->baseType;
14649 }
14650 return (NULL);
14651}
14652
14653/**
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014654 * xmlSchemaGetParticleTotalRangeMin:
14655 * @particle: the particle
14656 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014657 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014658 * (all and sequence) + (choice)
14659 *
14660 * Returns the minimun Effective Total Range.
14661 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014662static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014663xmlSchemaGetParticleTotalRangeMin(xmlSchemaParticlePtr particle)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014664{
14665 if ((particle->children == NULL) ||
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014666 (particle->minOccurs == 0))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014667 return (0);
14668 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014669 int min = -1, cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014670 xmlSchemaParticlePtr part =
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014671 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014672
14673 if (part == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014674 return (0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014675 while (part != NULL) {
14676 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14677 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014678 cur = part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014679 else
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014680 cur = xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014681 if (cur == 0)
14682 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014683 if ((min > cur) || (min == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014684 min = cur;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014685 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014686 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014687 return (particle->minOccurs * min);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014688 } else {
14689 /* <all> and <sequence> */
14690 int sum = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014691 xmlSchemaParticlePtr part =
14692 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014693
14694 if (part == NULL)
14695 return (0);
14696 do {
14697 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14698 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014699 sum += part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014700 else
14701 sum += xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014702 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014703 } while (part != NULL);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014704 return (particle->minOccurs * sum);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014705 }
14706}
14707
Daniel Veillard15724252008-08-30 15:01:04 +000014708#if 0
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014709/**
14710 * xmlSchemaGetParticleTotalRangeMax:
14711 * @particle: the particle
14712 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014713 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014714 * (all and sequence) + (choice)
14715 *
14716 * Returns the maximum Effective Total Range.
14717 */
14718static int
14719xmlSchemaGetParticleTotalRangeMax(xmlSchemaParticlePtr particle)
14720{
14721 if ((particle->children == NULL) ||
14722 (particle->children->children == NULL))
14723 return (0);
14724 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
14725 int max = -1, cur;
14726 xmlSchemaParticlePtr part =
14727 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014728
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014729 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14730 if (part->children == NULL)
14731 continue;
14732 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14733 (part->children->type == XML_SCHEMA_TYPE_ANY))
14734 cur = part->maxOccurs;
14735 else
14736 cur = xmlSchemaGetParticleTotalRangeMax(part);
14737 if (cur == UNBOUNDED)
14738 return (UNBOUNDED);
14739 if ((max < cur) || (max == -1))
14740 max = cur;
14741 }
14742 /* TODO: Handle overflows? */
14743 return (particle->maxOccurs * max);
14744 } else {
14745 /* <all> and <sequence> */
14746 int sum = 0, cur;
14747 xmlSchemaParticlePtr part =
14748 (xmlSchemaParticlePtr) particle->children->children;
14749
14750 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14751 if (part->children == NULL)
14752 continue;
14753 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14754 (part->children->type == XML_SCHEMA_TYPE_ANY))
14755 cur = part->maxOccurs;
14756 else
14757 cur = xmlSchemaGetParticleTotalRangeMax(part);
14758 if (cur == UNBOUNDED)
14759 return (UNBOUNDED);
14760 if ((cur > 0) && (particle->maxOccurs == UNBOUNDED))
14761 return (UNBOUNDED);
14762 sum += cur;
14763 }
14764 /* TODO: Handle overflows? */
14765 return (particle->maxOccurs * sum);
14766 }
14767}
Daniel Veillard15724252008-08-30 15:01:04 +000014768#endif
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014769
14770/**
14771 * xmlSchemaIsParticleEmptiable:
14772 * @particle: the particle
14773 *
14774 * Schema Component Constraint: Particle Emptiable
14775 * Checks whether the given particle is emptiable.
14776 *
14777 * Returns 1 if emptiable, 0 otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014778 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014779static int
14780xmlSchemaIsParticleEmptiable(xmlSchemaParticlePtr particle)
14781{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014782 /*
14783 * SPEC (1) "Its {min occurs} is 0."
14784 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014785 if ((particle == NULL) || (particle->minOccurs == 0) ||
14786 (particle->children == NULL))
14787 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014788 /*
14789 * SPEC (2) "Its {term} is a group and the minimum part of the
14790 * effective total range of that group, [...] is 0."
14791 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014792 if (WXS_IS_MODEL_GROUP(particle->children)) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014793 if (xmlSchemaGetParticleTotalRangeMin(particle) == 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014794 return (1);
14795 }
14796 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014797}
14798
14799/**
14800 * xmlSchemaCheckCOSSTDerivedOK:
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014801 * @actxt: a context
Daniel Veillard01fa6152004-06-29 17:04:39 +000014802 * @type: the derived simple type definition
14803 * @baseType: the base type definition
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014804 * @subset: the subset of ('restriction', ect.)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014805 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014806 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014807 * Type Derivation OK (Simple) (cos-st-derived-OK)
14808 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014809 * Checks wheter @type can be validly
Daniel Veillard01fa6152004-06-29 17:04:39 +000014810 * derived from @baseType.
14811 *
14812 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014813 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014814static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014815xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014816 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014817 xmlSchemaTypePtr baseType,
14818 int subset)
14819{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014820 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014821 * 1 They are the same type definition.
14822 * TODO: The identy check might have to be more complex than this.
14823 */
14824 if (type == baseType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014825 return (0);
14826 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014827 * 2.1 restriction is not in the subset, or in the {final}
14828 * of its own {base type definition};
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014829 *
14830 * NOTE that this will be used also via "xsi:type".
14831 *
14832 * TODO: Revise this, it looks strange. How can the "type"
14833 * not be fixed or *in* fixing?
Daniel Veillard01fa6152004-06-29 17:04:39 +000014834 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014835 if (WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014836 if (xmlSchemaTypeFixup(type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014837 return(-1);
14838 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014839 if (xmlSchemaTypeFixup(baseType, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014840 return(-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014841 if ((subset & SUBSET_RESTRICTION) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014842 (xmlSchemaTypeFinalContains(type->baseType,
14843 XML_SCHEMAS_TYPE_FINAL_RESTRICTION))) {
14844 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014845 }
14846 /* 2.2 */
14847 if (type->baseType == baseType) {
14848 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010014849 * 2.2.1 D's `base type definition` is B.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014850 */
14851 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014852 }
14853 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010014854 * 2.2.2 D's `base type definition` is not the `ur-type definition`
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014855 * and is validly derived from B given the subset, as defined by this
14856 * constraint.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014857 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014858 if ((! WXS_IS_ANYTYPE(type->baseType)) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014859 (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014860 baseType, subset) == 0)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014861 return (0);
14862 }
14863 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010014864 * 2.2.3 D's {variety} is list or union and B is the `simple ur-type
14865 * definition`.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014866 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014867 if (WXS_IS_ANY_SIMPLE_TYPE(baseType) &&
14868 (WXS_IS_LIST(type) || WXS_IS_UNION(type))) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014869 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014870 }
14871 /*
14872 * 2.2.4 B's {variety} is union and D is validly derived from a type
14873 * definition in B's {member type definitions} given the subset, as
Daniel Veillard01fa6152004-06-29 17:04:39 +000014874 * defined by this constraint.
14875 *
14876 * NOTE: This seems not to involve built-in types, since there is no
14877 * built-in Union Simple Type.
14878 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014879 if (WXS_IS_UNION(baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014880 xmlSchemaTypeLinkPtr cur;
14881
14882 cur = baseType->memberTypes;
14883 while (cur != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014884 if (WXS_IS_TYPE_NOT_FIXED(cur->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014885 if (xmlSchemaTypeFixup(cur->type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014886 return(-1);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014887 if (xmlSchemaCheckCOSSTDerivedOK(actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014888 type, cur->type, subset) == 0)
14889 {
14890 /*
14891 * It just has to be validly derived from at least one
14892 * member-type.
14893 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014894 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014895 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014896 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014897 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014898 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014899 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_2);
14900}
14901
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014902/**
14903 * xmlSchemaCheckTypeDefCircularInternal:
14904 * @pctxt: the schema parser context
14905 * @ctxtType: the type definition
14906 * @ancestor: an ancestor of @ctxtType
14907 *
14908 * Checks st-props-correct (2) + ct-props-correct (3).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014909 * Circular type definitions are not allowed.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014910 *
14911 * Returns XML_SCHEMAP_ST_PROPS_CORRECT_2 if the given type is
14912 * circular, 0 otherwise.
14913 */
14914static int
14915xmlSchemaCheckTypeDefCircularInternal(xmlSchemaParserCtxtPtr pctxt,
14916 xmlSchemaTypePtr ctxtType,
14917 xmlSchemaTypePtr ancestor)
14918{
14919 int ret;
14920
14921 if ((ancestor == NULL) || (ancestor->type == XML_SCHEMA_TYPE_BASIC))
14922 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014923
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014924 if (ctxtType == ancestor) {
14925 xmlSchemaPCustomErr(pctxt,
14926 XML_SCHEMAP_ST_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014927 WXS_BASIC_CAST ctxtType, WXS_ITEM_NODE(ctxtType),
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014928 "The definition is circular", NULL);
14929 return (XML_SCHEMAP_ST_PROPS_CORRECT_2);
14930 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014931 if (ancestor->flags & XML_SCHEMAS_TYPE_MARKED) {
14932 /*
14933 * Avoid inifinite recursion on circular types not yet checked.
14934 */
14935 return (0);
14936 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014937 ancestor->flags |= XML_SCHEMAS_TYPE_MARKED;
14938 ret = xmlSchemaCheckTypeDefCircularInternal(pctxt, ctxtType,
14939 ancestor->baseType);
14940 ancestor->flags ^= XML_SCHEMAS_TYPE_MARKED;
14941 return (ret);
14942}
14943
14944/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014945 * xmlSchemaCheckTypeDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014946 * @item: the complex/simple type definition
14947 * @ctxt: the parser context
14948 * @name: the name
14949 *
14950 * Checks for circular type definitions.
14951 */
14952static void
14953xmlSchemaCheckTypeDefCircular(xmlSchemaTypePtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014954 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014955{
14956 if ((item == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014957 (item->type == XML_SCHEMA_TYPE_BASIC) ||
14958 (item->baseType == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014959 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014960 xmlSchemaCheckTypeDefCircularInternal(ctxt, item,
14961 item->baseType);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014962}
Daniel Veillard01fa6152004-06-29 17:04:39 +000014963
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014964/*
14965* Simple Type Definition Representation OK (src-simple-type) 4
14966*
14967* "4 Circular union type definition is disallowed. That is, if the
14968* <union> alternative is chosen, there must not be any entries in the
14969* memberTypes [attribute] at any depth which resolve to the component
14970* corresponding to the <simpleType>."
14971*
14972* Note that this should work on the *representation* of a component,
14973* thus assumes any union types in the member types not being yet
14974* substituted. At this stage we need the variety of the types
14975* to be already computed.
14976*/
14977static int
14978xmlSchemaCheckUnionTypeDefCircularRecur(xmlSchemaParserCtxtPtr pctxt,
14979 xmlSchemaTypePtr ctxType,
14980 xmlSchemaTypeLinkPtr members)
Daniel Veillarddee23482008-04-11 12:58:43 +000014981{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014982 xmlSchemaTypeLinkPtr member;
14983 xmlSchemaTypePtr memberType;
Daniel Veillarddee23482008-04-11 12:58:43 +000014984
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014985 member = members;
14986 while (member != NULL) {
14987 memberType = member->type;
14988 while ((memberType != NULL) &&
14989 (memberType->type != XML_SCHEMA_TYPE_BASIC)) {
14990 if (memberType == ctxType) {
14991 xmlSchemaPCustomErr(pctxt,
14992 XML_SCHEMAP_SRC_SIMPLE_TYPE_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014993 WXS_BASIC_CAST ctxType, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014994 "The union type definition is circular", NULL);
14995 return (XML_SCHEMAP_SRC_SIMPLE_TYPE_4);
14996 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014997 if ((WXS_IS_UNION(memberType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014998 ((memberType->flags & XML_SCHEMAS_TYPE_MARKED) == 0))
14999 {
15000 int res;
15001 memberType->flags |= XML_SCHEMAS_TYPE_MARKED;
15002 res = xmlSchemaCheckUnionTypeDefCircularRecur(pctxt,
15003 ctxType,
15004 xmlSchemaGetUnionSimpleTypeMemberTypes(memberType));
15005 memberType->flags ^= XML_SCHEMAS_TYPE_MARKED;
15006 if (res != 0)
15007 return(res);
15008 }
15009 memberType = memberType->baseType;
15010 }
15011 member = member->next;
15012 }
15013 return(0);
15014}
15015
15016static int
15017xmlSchemaCheckUnionTypeDefCircular(xmlSchemaParserCtxtPtr pctxt,
15018 xmlSchemaTypePtr type)
15019{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015020 if (! WXS_IS_UNION(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015021 return(0);
15022 return(xmlSchemaCheckUnionTypeDefCircularRecur(pctxt, type,
15023 type->memberTypes));
15024}
15025
Daniel Veillard01fa6152004-06-29 17:04:39 +000015026/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015027 * xmlSchemaResolveTypeReferences:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015028 * @item: the complex/simple type definition
15029 * @ctxt: the parser context
15030 * @name: the name
15031 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015032 * Resolvese type definition references
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015033 */
15034static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015035xmlSchemaResolveTypeReferences(xmlSchemaTypePtr typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015036 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015037{
15038 if (typeDef == NULL)
15039 return;
15040
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015041 /*
15042 * Resolve the base type.
15043 */
15044 if (typeDef->baseType == NULL) {
15045 typeDef->baseType = xmlSchemaGetType(ctxt->schema,
15046 typeDef->base, typeDef->baseNs);
15047 if (typeDef->baseType == NULL) {
15048 xmlSchemaPResCompAttrErr(ctxt,
15049 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015050 WXS_BASIC_CAST typeDef, typeDef->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015051 "base", typeDef->base, typeDef->baseNs,
15052 XML_SCHEMA_TYPE_SIMPLE, NULL);
15053 return;
15054 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015055 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015056 if (WXS_IS_SIMPLE(typeDef)) {
15057 if (WXS_IS_UNION(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015058 /*
15059 * Resolve the memberTypes.
15060 */
15061 xmlSchemaResolveUnionMemberTypes(ctxt, typeDef);
15062 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015063 } else if (WXS_IS_LIST(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015064 /*
15065 * Resolve the itemType.
15066 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015067 if ((typeDef->subtypes == NULL) && (typeDef->base != NULL)) {
15068
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015069 typeDef->subtypes = xmlSchemaGetType(ctxt->schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015070 typeDef->base, typeDef->baseNs);
15071
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015072 if ((typeDef->subtypes == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015073 (! WXS_IS_SIMPLE(typeDef->subtypes)))
15074 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015075 typeDef->subtypes = NULL;
15076 xmlSchemaPResCompAttrErr(ctxt,
15077 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015078 WXS_BASIC_CAST typeDef, typeDef->node,
15079 "itemType", typeDef->base, typeDef->baseNs,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015080 XML_SCHEMA_TYPE_SIMPLE, NULL);
15081 }
15082 }
15083 return;
15084 }
Daniel Veillarddee23482008-04-11 12:58:43 +000015085 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015086 /*
15087 * The ball of letters below means, that if we have a particle
15088 * which has a QName-helper component as its {term}, we want
15089 * to resolve it...
15090 */
15091 else if ((WXS_TYPE_CONTENTTYPE(typeDef) != NULL) &&
15092 ((WXS_TYPE_CONTENTTYPE(typeDef))->type ==
15093 XML_SCHEMA_TYPE_PARTICLE) &&
15094 (WXS_TYPE_PARTICLE_TERM(typeDef) != NULL) &&
15095 ((WXS_TYPE_PARTICLE_TERM(typeDef))->type ==
15096 XML_SCHEMA_EXTRA_QNAMEREF))
15097 {
15098 xmlSchemaQNameRefPtr ref =
15099 WXS_QNAME_CAST WXS_TYPE_PARTICLE_TERM(typeDef);
15100 xmlSchemaModelGroupDefPtr groupDef;
15101
15102 /*
15103 * URGENT TODO: Test this.
15104 */
15105 WXS_TYPE_PARTICLE_TERM(typeDef) = NULL;
15106 /*
15107 * Resolve the MG definition reference.
15108 */
15109 groupDef =
15110 WXS_MODEL_GROUPDEF_CAST xmlSchemaGetNamedComponent(ctxt->schema,
15111 ref->itemType, ref->name, ref->targetNamespace);
15112 if (groupDef == NULL) {
15113 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
15114 NULL, WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)),
15115 "ref", ref->name, ref->targetNamespace, ref->itemType,
15116 NULL);
15117 /* Remove the particle. */
15118 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
15119 } else if (WXS_MODELGROUPDEF_MODEL(groupDef) == NULL)
15120 /* Remove the particle. */
15121 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
15122 else {
15123 /*
15124 * Assign the MG definition's {model group} to the
15125 * particle's {term}.
15126 */
15127 WXS_TYPE_PARTICLE_TERM(typeDef) = WXS_MODELGROUPDEF_MODEL(groupDef);
Daniel Veillarddee23482008-04-11 12:58:43 +000015128
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015129 if (WXS_MODELGROUPDEF_MODEL(groupDef)->type == XML_SCHEMA_TYPE_ALL) {
15130 /*
15131 * SPEC cos-all-limited (1.2)
15132 * "1.2 the {term} property of a particle with
15133 * {max occurs}=1 which is part of a pair which constitutes
15134 * the {content type} of a complex type definition."
15135 */
15136 if ((WXS_TYPE_PARTICLE(typeDef))->maxOccurs != 1) {
15137 xmlSchemaCustomErr(ACTXT_CAST ctxt,
15138 /* TODO: error code */
15139 XML_SCHEMAP_COS_ALL_LIMITED,
15140 WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)), NULL,
15141 "The particle's {max occurs} must be 1, since the "
15142 "reference resolves to an 'all' model group",
15143 NULL, NULL);
15144 }
15145 }
15146 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015147 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015148}
15149
15150
15151
15152/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000015153 * xmlSchemaCheckSTPropsCorrect:
15154 * @ctxt: the schema parser context
15155 * @type: the simple type definition
15156 *
15157 * Checks st-props-correct.
15158 *
15159 * Returns 0 if the properties are correct,
15160 * if not, a positive error code and -1 on internal
15161 * errors.
15162 */
15163static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015164xmlSchemaCheckSTPropsCorrect(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015165 xmlSchemaTypePtr type)
15166{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015167 xmlSchemaTypePtr baseType = type->baseType;
Daniel Veillardc0826a72004-08-10 14:17:33 +000015168 xmlChar *str = NULL;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015169
Daniel Veillardc0826a72004-08-10 14:17:33 +000015170 /* STATE: error funcs converted. */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015171 /*
15172 * Schema Component Constraint: Simple Type Definition Properties Correct
15173 *
15174 * NOTE: This is somehow redundant, since we actually built a simple type
15175 * to have all the needed information; this acts as an self test.
15176 */
Jan Pokorný761c9e92013-11-29 23:26:27 +010015177 /* Base type: If the datatype has been `derived` by `restriction`
15178 * then the Simple Type Definition component from which it is `derived`,
15179 * otherwise the Simple Type Definition for anySimpleType ($4.1.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000015180 */
15181 if (baseType == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015182 /*
15183 * TODO: Think about: "modulo the impact of Missing
Jan Pokorný761c9e92013-11-29 23:26:27 +010015184 * Sub-components ($5.3)."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015185 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015186 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015187 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015188 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015189 "No base type existent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015190 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015191
Daniel Veillard01fa6152004-06-29 17:04:39 +000015192 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015193 if (! WXS_IS_SIMPLE(baseType)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015194 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015195 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015196 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015197 "The base type '%s' is not a simple type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015198 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015199 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015200 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15201 }
Toyoda Eizi8bb12982011-08-04 16:40:48 +080015202 if ((WXS_IS_LIST(type) || WXS_IS_UNION(type)) &&
15203 (WXS_IS_RESTRICTION(type) == 0) &&
15204 ((! WXS_IS_ANY_SIMPLE_TYPE(baseType)) &&
15205 (baseType->type != XML_SCHEMA_TYPE_SIMPLE))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015206 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015207 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015208 WXS_BASIC_CAST type, NULL,
Daniel Veillardbf9c1da2008-08-26 07:46:42 +000015209 "A type, derived by list or union, must have "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015210 "the simple ur-type definition as base type, not '%s'",
15211 xmlSchemaGetComponentQName(&str, baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015212 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015213 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15214 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015215 /*
15216 * Variety: One of {atomic, list, union}.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015217 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015218 if ((! WXS_IS_ATOMIC(type)) && (! WXS_IS_UNION(type)) &&
15219 (! WXS_IS_LIST(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015220 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015221 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015222 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015223 "The variety is absent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015224 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15225 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000015226 /* TODO: Finish this. Hmm, is this finished? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015227
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015228 /*
15229 * 3 The {final} of the {base type definition} must not contain restriction.
15230 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015231 if (xmlSchemaTypeFinalContains(baseType,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015232 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
15233 xmlSchemaPCustomErr(ctxt,
15234 XML_SCHEMAP_ST_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015235 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015236 "The 'final' of its base type '%s' must not contain "
15237 "'restriction'",
15238 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015239 FREE_AND_NULL(str)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015240 return (XML_SCHEMAP_ST_PROPS_CORRECT_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015241 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015242
15243 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010015244 * 2 All simple type definitions must be derived ultimately from the `simple
15245 * ur-type definition` (so circular definitions are disallowed). That is, it
15246 * must be possible to reach a built-in primitive datatype or the `simple
15247 * ur-type definition` by repeatedly following the {base type definition}.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015248 *
15249 * NOTE: this is done in xmlSchemaCheckTypeDefCircular().
Daniel Veillard01fa6152004-06-29 17:04:39 +000015250 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015251 return (0);
15252}
15253
15254/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015255 * xmlSchemaCheckCOSSTRestricts:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015256 * @ctxt: the schema parser context
15257 * @type: the simple type definition
15258 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015259 * Schema Component Constraint:
15260 * Derivation Valid (Restriction, Simple) (cos-st-restricts)
15261
15262 * Checks if the given @type (simpleType) is derived validly by restriction.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015263 * STATUS:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015264 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015265 * Returns -1 on internal errors, 0 if the type is validly derived,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015266 * a positive error code otherwise.
15267 */
15268static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015269xmlSchemaCheckCOSSTRestricts(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015270 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015271{
Daniel Veillardc0826a72004-08-10 14:17:33 +000015272 xmlChar *str = NULL;
15273
Daniel Veillard01fa6152004-06-29 17:04:39 +000015274 if (type->type != XML_SCHEMA_TYPE_SIMPLE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015275 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15276 "given type is not a user-derived simpleType");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015277 return (-1);
15278 }
15279
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015280 if (WXS_IS_ATOMIC(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015281 xmlSchemaTypePtr primitive;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015282 /*
15283 * 1.1 The {base type definition} must be an atomic simple
Daniel Veillard01fa6152004-06-29 17:04:39 +000015284 * type definition or a built-in primitive datatype.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015285 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015286 if (! WXS_IS_ATOMIC(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015287 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015288 XML_SCHEMAP_COS_ST_RESTRICTS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015289 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015290 "The base type '%s' is not an atomic simple type",
15291 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015292 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015293 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_1);
15294 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015295 /* 1.2 The {final} of the {base type definition} must not contain
Daniel Veillard01fa6152004-06-29 17:04:39 +000015296 * restriction.
15297 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015298 /* OPTIMIZE TODO : This is already done in xmlSchemaCheckStPropsCorrect */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015299 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015300 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015301 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015302 XML_SCHEMAP_COS_ST_RESTRICTS_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015303 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015304 "The final of its base type '%s' must not contain 'restriction'",
15305 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015306 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015307 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_2);
15308 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015309
15310 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015311 * 1.3.1 DF must be an allowed constraining facet for the {primitive
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015312 * type definition}, as specified in the appropriate subsection of 3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015313 * Primitive datatypes.
15314 */
15315 if (type->facets != NULL) {
15316 xmlSchemaFacetPtr facet;
15317 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015318
Daniel Veillard01fa6152004-06-29 17:04:39 +000015319 primitive = xmlSchemaGetPrimitiveType(type);
15320 if (primitive == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015321 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15322 "failed to get primitive type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015323 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015324 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015325 facet = type->facets;
15326 do {
15327 if (xmlSchemaIsBuiltInTypeFacet(primitive, facet->type) == 0) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015328 ok = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015329 xmlSchemaPIllegalFacetAtomicErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015330 XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015331 type, primitive, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015332 }
15333 facet = facet->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015334 } while (facet != NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015335 if (ok == 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015336 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015337 }
15338 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015339 * SPEC (1.3.2) "If there is a facet of the same kind in the {facets}
15340 * of the {base type definition} (call this BF),then the DF's {value}
15341 * must be a valid restriction of BF's {value} as defined in
15342 * [XML Schemas: Datatypes]."
15343 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015344 * NOTE (1.3.2) Facet derivation constraints are currently handled in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015345 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015346 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015347 } else if (WXS_IS_LIST(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015348 xmlSchemaTypePtr itemType = NULL;
15349
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015350 itemType = type->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015351 if ((itemType == NULL) || (! WXS_IS_SIMPLE(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015352 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15353 "failed to evaluate the item type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015354 return (-1);
15355 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015356 if (WXS_IS_TYPE_NOT_FIXED(itemType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015357 xmlSchemaTypeFixup(itemType, ACTXT_CAST pctxt);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015358 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015359 * 2.1 The {item type definition} must have a {variety} of atomic or
15360 * union (in which case all the {member type definitions}
Daniel Veillard01fa6152004-06-29 17:04:39 +000015361 * must be atomic).
15362 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015363 if ((! WXS_IS_ATOMIC(itemType)) &&
15364 (! WXS_IS_UNION(itemType))) {
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_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015367 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015368 "The item type '%s' does not have a variety of atomic or union",
15369 xmlSchemaGetComponentQName(&str, itemType));
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_1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015372 } else if (WXS_IS_UNION(itemType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015373 xmlSchemaTypeLinkPtr member;
15374
15375 member = itemType->memberTypes;
15376 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015377 if (! WXS_IS_ATOMIC(member->type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015378 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015379 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015380 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015381 "The item type is a union type, but the "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015382 "member type '%s' of this item type is not atomic",
15383 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015384 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015385 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
15386 }
15387 member = member->next;
15388 }
15389 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015390
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015391 if (WXS_IS_ANY_SIMPLE_TYPE(type->baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015392 xmlSchemaFacetPtr facet;
15393 /*
15394 * This is the case if we have: <simpleType><list ..
15395 */
15396 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015397 * 2.3.1
15398 * 2.3.1.1 The {final} of the {item type definition} must not
Daniel Veillard01fa6152004-06-29 17:04:39 +000015399 * contain list.
15400 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015401 if (xmlSchemaTypeFinalContains(itemType,
15402 XML_SCHEMAS_TYPE_FINAL_LIST)) {
15403 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015404 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015405 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015406 "The final of its item type '%s' must not contain 'list'",
15407 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015408 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015409 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1);
15410 }
15411 /*
15412 * 2.3.1.2 The {facets} must only contain the whiteSpace
15413 * facet component.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015414 * OPTIMIZE TODO: the S4S already disallows any facet
15415 * to be specified.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015416 */
15417 if (type->facets != NULL) {
15418 facet = type->facets;
15419 do {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015420 if (facet->type != XML_SCHEMA_FACET_WHITESPACE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015421 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015422 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015423 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015424 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2);
15425 }
15426 facet = facet->next;
15427 } while (facet != NULL);
15428 }
15429 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015430 * MAYBE TODO: (Hmm, not really) Datatypes states:
Jan Pokorný761c9e92013-11-29 23:26:27 +010015431 * A `list` datatype can be `derived` from an `atomic` datatype
15432 * whose `lexical space` allows space (such as string or anyURI)or
15433 * a `union` datatype any of whose {member type definitions}'s
15434 * `lexical space` allows space.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015435 */
15436 } else {
15437 /*
15438 * This is the case if we have: <simpleType><restriction ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015439 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015440 */
15441 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015442 * 2.3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015443 * 2.3.2.1 The {base type definition} must have a {variety} of list.
15444 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015445 if (! WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015446 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015447 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015448 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015449 "The base type '%s' must be a list type",
15450 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015451 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015452 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1);
15453 }
15454 /*
15455 * 2.3.2.2 The {final} of the {base type definition} must not
15456 * contain restriction.
15457 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015458 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015459 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015460 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015461 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015462 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015463 "The 'final' of the base type '%s' must not contain 'restriction'",
15464 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015465 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015466 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2);
15467 }
15468 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015469 * 2.3.2.3 The {item type definition} must be validly derived
Daniel Veillard01fa6152004-06-29 17:04:39 +000015470 * from the {base type definition}'s {item type definition} given
Jan Pokorný761c9e92013-11-29 23:26:27 +010015471 * the empty set, as defined in Type Derivation OK (Simple) ($3.14.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000015472 */
15473 {
15474 xmlSchemaTypePtr baseItemType;
15475
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015476 baseItemType = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015477 if ((baseItemType == NULL) || (! WXS_IS_SIMPLE(baseItemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015478 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15479 "failed to eval the item type of a base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015480 return (-1);
15481 }
15482 if ((itemType != baseItemType) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015483 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt, itemType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015484 baseItemType, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015485 xmlChar *strBIT = NULL, *strBT = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015486 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015487 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015488 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015489 "The item type '%s' is not validly derived from "
15490 "the item type '%s' of the base type '%s'",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015491 xmlSchemaGetComponentQName(&str, itemType),
15492 xmlSchemaGetComponentQName(&strBIT, baseItemType),
15493 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015494
15495 FREE_AND_NULL(str)
15496 FREE_AND_NULL(strBIT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015497 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015498 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3);
15499 }
15500 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015501
Daniel Veillard01fa6152004-06-29 17:04:39 +000015502 if (type->facets != NULL) {
15503 xmlSchemaFacetPtr facet;
15504 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015505 /*
15506 * 2.3.2.4 Only length, minLength, maxLength, whiteSpace, pattern
Daniel Veillard01fa6152004-06-29 17:04:39 +000015507 * and enumeration facet components are allowed among the {facets}.
15508 */
15509 facet = type->facets;
15510 do {
15511 switch (facet->type) {
15512 case XML_SCHEMA_FACET_LENGTH:
15513 case XML_SCHEMA_FACET_MINLENGTH:
15514 case XML_SCHEMA_FACET_MAXLENGTH:
15515 case XML_SCHEMA_FACET_WHITESPACE:
15516 /*
15517 * TODO: 2.5.1.2 List datatypes
Jan Pokorný761c9e92013-11-29 23:26:27 +010015518 * The value of `whiteSpace` is fixed to the value collapse.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015519 */
15520 case XML_SCHEMA_FACET_PATTERN:
15521 case XML_SCHEMA_FACET_ENUMERATION:
15522 break;
15523 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015524 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015525 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015526 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015527 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015528 * We could return, but it's nicer to report all
Daniel Veillard01fa6152004-06-29 17:04:39 +000015529 * invalid facets.
15530 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015531 ok = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015532 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015533 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015534 facet = facet->next;
15535 } while (facet != NULL);
15536 if (ok == 0)
15537 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4);
15538 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015539 * SPEC (2.3.2.5) (same as 1.3.2)
15540 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015541 * NOTE (2.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015542 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015543 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015544 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015545 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015546 } else if (WXS_IS_UNION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015547 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015548 * 3.1 The {member type definitions} must all have {variety} of
Daniel Veillard01fa6152004-06-29 17:04:39 +000015549 * atomic or list.
15550 */
15551 xmlSchemaTypeLinkPtr member;
15552
15553 member = type->memberTypes;
15554 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015555 if (WXS_IS_TYPE_NOT_FIXED(member->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015556 xmlSchemaTypeFixup(member->type, ACTXT_CAST pctxt);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015557
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015558 if ((! WXS_IS_ATOMIC(member->type)) &&
15559 (! WXS_IS_LIST(member->type))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015560 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015561 XML_SCHEMAP_COS_ST_RESTRICTS_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015562 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015563 "The member type '%s' is neither an atomic, nor a list type",
15564 xmlSchemaGetComponentQName(&str, member->type));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015565 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015566 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_1);
15567 }
15568 member = member->next;
15569 }
15570 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010015571 * 3.3.1 If the {base type definition} is the `simple ur-type
15572 * definition`
Daniel Veillard01fa6152004-06-29 17:04:39 +000015573 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015574 if (type->baseType->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015575 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015576 * 3.3.1.1 All of the {member type definitions} must have a
Daniel Veillard01fa6152004-06-29 17:04:39 +000015577 * {final} which does not contain union.
15578 */
15579 member = type->memberTypes;
15580 while (member != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015581 if (xmlSchemaTypeFinalContains(member->type,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015582 XML_SCHEMAS_TYPE_FINAL_UNION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015583 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015584 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015585 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015586 "The 'final' of member type '%s' contains 'union'",
15587 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015588 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015589 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1);
15590 }
15591 member = member->next;
15592 }
15593 /*
15594 * 3.3.1.2 The {facets} must be empty.
15595 */
15596 if (type->facetSet != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015597 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015598 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015599 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015600 "No facets allowed", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015601 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2);
15602 }
15603 } else {
15604 /*
15605 * 3.3.2.1 The {base type definition} must have a {variety} of union.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015606 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015607 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015608 if (! WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015609 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015610 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015611 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015612 "The base type '%s' is not a union type",
15613 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015614 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015615 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1);
15616 }
15617 /*
15618 * 3.3.2.2 The {final} of the {base type definition} must not contain restriction.
15619 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015620 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015621 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015622 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015623 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015624 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015625 "The 'final' of its base type '%s' must not contain 'restriction'",
15626 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015627 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015628 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2);
15629 }
15630 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015631 * 3.3.2.3 The {member type definitions}, in order, must be validly
15632 * derived from the corresponding type definitions in the {base
15633 * type definition}'s {member type definitions} given the empty set,
Jan Pokorný761c9e92013-11-29 23:26:27 +010015634 * as defined in Type Derivation OK (Simple) ($3.14.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000015635 */
15636 {
15637 xmlSchemaTypeLinkPtr baseMember;
15638
15639 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015640 * OPTIMIZE: if the type is restricting, it has no local defined
15641 * member types and inherits the member types of the base type;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015642 * thus a check for equality can be skipped.
15643 */
15644 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015645 * Even worse: I cannot see a scenario where a restricting
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015646 * union simple type can have other member types as the member
Daniel Veillard01fa6152004-06-29 17:04:39 +000015647 * types of it's base type. This check seems not necessary with
15648 * respect to the derivation process in libxml2.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015649 * But necessary if constructing types with an API.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015650 */
15651 if (type->memberTypes != NULL) {
15652 member = type->memberTypes;
15653 baseMember = xmlSchemaGetUnionSimpleTypeMemberTypes(type->baseType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015654 if ((member == NULL) && (baseMember != NULL)) {
15655 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15656 "different number of member types in base");
15657 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015658 while (member != NULL) {
15659 if (baseMember == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015660 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15661 "different number of member types in base");
Daniel Veillard14b56432006-03-09 18:41:40 +000015662 } else if ((member->type != baseMember->type) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015663 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015664 member->type, baseMember->type, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015665 xmlChar *strBMT = NULL, *strBT = NULL;
15666
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015667 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015668 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015669 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015670 "The member type %s is not validly "
15671 "derived from its corresponding member "
15672 "type %s of the base type %s",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015673 xmlSchemaGetComponentQName(&str, member->type),
15674 xmlSchemaGetComponentQName(&strBMT, baseMember->type),
15675 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015676 FREE_AND_NULL(str)
15677 FREE_AND_NULL(strBMT)
15678 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015679 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015680 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015681 member = member->next;
Daniel Veillardd44b9362009-09-07 12:15:08 +020015682 if (baseMember != NULL)
15683 baseMember = baseMember->next;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015684 }
15685 }
15686 }
15687 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015688 * 3.3.2.4 Only pattern and enumeration facet components are
Daniel Veillard01fa6152004-06-29 17:04:39 +000015689 * allowed among the {facets}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015690 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015691 if (type->facets != NULL) {
15692 xmlSchemaFacetPtr facet;
15693 int ok = 1;
15694
15695 facet = type->facets;
15696 do {
15697 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
15698 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015699 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015700 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015701 type, facet);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015702 ok = 0;
15703 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015704 facet = facet->next;
15705 } while (facet != NULL);
15706 if (ok == 0)
15707 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015708
Daniel Veillard01fa6152004-06-29 17:04:39 +000015709 }
15710 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015711 * SPEC (3.3.2.5) (same as 1.3.2)
15712 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015713 * NOTE (3.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015714 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015715 */
15716 }
15717 }
15718
15719 return (0);
15720}
15721
15722/**
15723 * xmlSchemaCheckSRCSimpleType:
15724 * @ctxt: the schema parser context
15725 * @type: the simple type definition
15726 *
Daniel Veillarddee23482008-04-11 12:58:43 +000015727 * Checks crc-simple-type constraints.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015728 *
15729 * Returns 0 if the constraints are satisfied,
15730 * if not a positive error code and -1 on internal
15731 * errors.
15732 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015733#if 0
Daniel Veillard01fa6152004-06-29 17:04:39 +000015734static int
15735xmlSchemaCheckSRCSimpleType(xmlSchemaParserCtxtPtr ctxt,
15736 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015737{
15738 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015739 * src-simple-type.1 The corresponding simple type definition, if any,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015740 * must satisfy the conditions set out in Constraints on Simple Type
Jan Pokorný761c9e92013-11-29 23:26:27 +010015741 * Definition Schema Components ($3.14.6).
Daniel Veillarddee23482008-04-11 12:58:43 +000015742 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015743 if (WXS_IS_RESTRICTION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015744 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015745 * src-simple-type.2 "If the <restriction> alternative is chosen,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015746 * either it must have a base [attribute] or a <simpleType> among its
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015747 * [children], but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015748 * NOTE: This is checked in the parse function of <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015749 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015750 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000015751 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015752 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015753 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015754 /* src-simple-type.3 "If the <list> alternative is chosen, either it must have
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015755 * an itemType [attribute] or a <simpleType> among its [children],
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015756 * but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015757 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015758 * NOTE: This is checked in the parse function of <list>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015759 */
Daniel Veillarddee23482008-04-11 12:58:43 +000015760 } else if (WXS_IS_UNION(type)) {
15761 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015762 * src-simple-type.4 is checked in xmlSchemaCheckUnionTypeDefCircular().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015763 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015764 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015765 return (0);
15766}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015767#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +000015768
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015769static int
15770xmlSchemaCreateVCtxtOnPCtxt(xmlSchemaParserCtxtPtr ctxt)
15771{
15772 if (ctxt->vctxt == NULL) {
15773 ctxt->vctxt = xmlSchemaNewValidCtxt(NULL);
15774 if (ctxt->vctxt == NULL) {
15775 xmlSchemaPErr(ctxt, NULL,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000015776 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015777 "Internal error: xmlSchemaCreateVCtxtOnPCtxt, "
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015778 "failed to create a temp. validation context.\n",
15779 NULL, NULL);
15780 return (-1);
15781 }
15782 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015783 xmlSchemaSetValidErrors(ctxt->vctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000015784 ctxt->error, ctxt->warning, ctxt->errCtxt);
15785 xmlSchemaSetValidStructuredErrors(ctxt->vctxt,
15786 ctxt->serror, ctxt->errCtxt);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015787 }
15788 return (0);
15789}
15790
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015791static int
15792xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
15793 xmlNodePtr node,
15794 xmlSchemaTypePtr type,
15795 const xmlChar *value,
15796 xmlSchemaValPtr *retVal,
15797 int fireErrors,
15798 int normalize,
15799 int isNormalized);
15800
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015801/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015802 * xmlSchemaParseCheckCOSValidDefault:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015803 * @pctxt: the schema parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015804 * @type: the simple type definition
15805 * @value: the default value
15806 * @node: an optional node (the holder of the value)
15807 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015808 * Schema Component Constraint: Element Default Valid (Immediate)
15809 * (cos-valid-default)
15810 * This will be used by the parser only. For the validator there's
15811 * an other version.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015812 *
15813 * Returns 0 if the constraints are satisfied,
15814 * if not, a positive error code and -1 on internal
15815 * errors.
15816 */
15817static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015818xmlSchemaParseCheckCOSValidDefault(xmlSchemaParserCtxtPtr pctxt,
15819 xmlNodePtr node,
15820 xmlSchemaTypePtr type,
15821 const xmlChar *value,
15822 xmlSchemaValPtr *val)
15823{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015824 int ret = 0;
15825
15826 /*
15827 * cos-valid-default:
15828 * Schema Component Constraint: Element Default Valid (Immediate)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015829 * For a string to be a valid default with respect to a type
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015830 * definition the appropriate case among the following must be true:
15831 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015832 if WXS_IS_COMPLEX(type) {
William M. Brack2f2a6632004-08-20 23:09:47 +000015833 /*
15834 * Complex type.
15835 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015836 * SPEC (2.1) "its {content type} must be a simple type definition
15837 * or mixed."
15838 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
Jan Pokorný761c9e92013-11-29 23:26:27 +010015839 * type}'s particle must be `emptiable` as defined by
15840 * Particle Emptiable ($3.9.6)."
William M. Brack2f2a6632004-08-20 23:09:47 +000015841 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015842 if ((! WXS_HAS_SIMPLE_CONTENT(type)) &&
15843 ((! WXS_HAS_MIXED_CONTENT(type)) || (! WXS_EMPTIABLE(type)))) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015844 /* NOTE that this covers (2.2.2) as well. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015845 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015846 XML_SCHEMAP_COS_VALID_DEFAULT_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015847 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015848 "For a string to be a valid default, the type definition "
15849 "must be a simple type or a complex type with mixed content "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015850 "and a particle emptiable", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015851 return(XML_SCHEMAP_COS_VALID_DEFAULT_2_1);
15852 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015853 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015854 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015855 * 1 If the type definition is a simple type definition, then the string
Jan Pokorný761c9e92013-11-29 23:26:27 +010015856 * must be `valid` with respect to that definition as defined by String
15857 * Valid ($3.14.4).
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015858 *
15859 * AND
15860 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015861 * 2.2.1 If the {content type} is a simple type definition, then the
Jan Pokorný761c9e92013-11-29 23:26:27 +010015862 * string must be `valid` with respect to that simple type definition
15863 * as defined by String Valid ($3.14.4).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015864 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015865 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015866 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015867 type, value, val, 1, 1, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015868 else if (WXS_HAS_SIMPLE_CONTENT(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015869 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015870 type->contentTypeDef, value, val, 1, 1, 0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015871 else
15872 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015873
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015874 if (ret < 0) {
15875 PERROR_INT("xmlSchemaParseCheckCOSValidDefault",
15876 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015877 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015878
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015879 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000015880}
15881
15882/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015883 * xmlSchemaCheckCTPropsCorrect:
William M. Brack2f2a6632004-08-20 23:09:47 +000015884 * @ctxt: the schema parser context
15885 * @type: the complex type definition
15886 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015887 *.(4.6) Constraints on Complex Type Definition Schema Components
15888 * Schema Component Constraint:
15889 * Complex Type Definition Properties Correct (ct-props-correct)
15890 * STATUS: (seems) complete
William M. Brack2f2a6632004-08-20 23:09:47 +000015891 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015892 * Returns 0 if the constraints are satisfied, a positive
15893 * error code if not and -1 if an internal error occured.
William M. Brack2f2a6632004-08-20 23:09:47 +000015894 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015895static int
15896xmlSchemaCheckCTPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
15897 xmlSchemaTypePtr type)
William M. Brack2f2a6632004-08-20 23:09:47 +000015898{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015899 /*
15900 * TODO: Correct the error code; XML_SCHEMAP_SRC_CT_1 is used temporarily.
15901 *
15902 * SPEC (1) "The values of the properties of a complex type definition must
15903 * be as described in the property tableau in The Complex Type Definition
Jan Pokorný761c9e92013-11-29 23:26:27 +010015904 * Schema Component ($3.4.1), modulo the impact of Missing
15905 * Sub-components ($5.3)."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015906 */
15907 if ((type->baseType != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015908 (WXS_IS_SIMPLE(type->baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015909 (WXS_IS_EXTENSION(type) == 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015910 /*
15911 * SPEC (2) "If the {base type definition} is a simple type definition,
15912 * the {derivation method} must be extension."
15913 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015914 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015915 XML_SCHEMAP_SRC_CT_1,
Daniel Veillarddee23482008-04-11 12:58:43 +000015916 NULL, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015917 "If the base type is a simple type, the derivation method must be "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015918 "'extension'", NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015919 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015920 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015921 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010015922 * SPEC (3) "Circular definitions are disallowed, except for the `ur-type
15923 * definition`. That is, it must be possible to reach the `ur-type
15924 * definition` by repeatedly following the {base type definition}."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015925 *
15926 * NOTE (3) is done in xmlSchemaCheckTypeDefCircular().
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015927 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015928 /*
15929 * NOTE that (4) and (5) need the following:
15930 * - attribute uses need to be already inherited (apply attr. prohibitions)
15931 * - attribute group references need to be expanded already
15932 * - simple types need to be typefixed already
Daniel Veillarddee23482008-04-11 12:58:43 +000015933 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015934 if (type->attrUses &&
15935 (((xmlSchemaItemListPtr) type->attrUses)->nbItems > 1))
15936 {
15937 xmlSchemaItemListPtr uses = (xmlSchemaItemListPtr) type->attrUses;
15938 xmlSchemaAttributeUsePtr use, tmp;
15939 int i, j, hasId = 0;
15940
15941 for (i = uses->nbItems -1; i >= 0; i--) {
15942 use = uses->items[i];
Daniel Veillarddee23482008-04-11 12:58:43 +000015943
15944 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015945 * SPEC ct-props-correct
15946 * (4) "Two distinct attribute declarations in the
15947 * {attribute uses} must not have identical {name}s and
15948 * {target namespace}s."
15949 */
15950 if (i > 0) {
15951 for (j = i -1; j >= 0; j--) {
15952 tmp = uses->items[j];
15953 if ((WXS_ATTRUSE_DECL_NAME(use) ==
15954 WXS_ATTRUSE_DECL_NAME(tmp)) &&
15955 (WXS_ATTRUSE_DECL_TNS(use) ==
15956 WXS_ATTRUSE_DECL_TNS(tmp)))
15957 {
15958 xmlChar *str = NULL;
15959
15960 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15961 XML_SCHEMAP_AG_PROPS_CORRECT,
15962 NULL, WXS_BASIC_CAST type,
15963 "Duplicate %s",
15964 xmlSchemaGetComponentDesignation(&str, use),
15965 NULL);
15966 FREE_AND_NULL(str);
15967 /*
15968 * Remove the duplicate.
15969 */
15970 if (xmlSchemaItemListRemove(uses, i) == -1)
15971 goto exit_failure;
15972 goto next_use;
15973 }
15974 }
15975 }
15976 /*
15977 * SPEC ct-props-correct
15978 * (5) "Two distinct attribute declarations in the
15979 * {attribute uses} must not have {type definition}s which
15980 * are or are derived from ID."
15981 */
15982 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
15983 if (xmlSchemaIsDerivedFromBuiltInType(
15984 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
Daniel Veillarddee23482008-04-11 12:58:43 +000015985 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015986 if (hasId) {
15987 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000015988
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015989 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15990 XML_SCHEMAP_AG_PROPS_CORRECT,
15991 NULL, WXS_BASIC_CAST type,
15992 "There must not exist more than one attribute "
15993 "declaration of type 'xs:ID' "
15994 "(or derived from 'xs:ID'). The %s violates this "
15995 "constraint",
15996 xmlSchemaGetComponentDesignation(&str, use),
15997 NULL);
15998 FREE_AND_NULL(str);
15999 if (xmlSchemaItemListRemove(uses, i) == -1)
16000 goto exit_failure;
16001 }
Daniel Veillarddee23482008-04-11 12:58:43 +000016002
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016003 hasId = 1;
16004 }
16005 }
16006next_use: {}
16007 }
16008 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016009 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016010exit_failure:
16011 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +000016012}
16013
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016014static int
16015xmlSchemaAreEqualTypes(xmlSchemaTypePtr typeA,
16016 xmlSchemaTypePtr typeB)
16017{
16018 /*
16019 * TODO: This should implement component-identity
16020 * in the future.
16021 */
16022 if ((typeA == NULL) || (typeB == NULL))
16023 return (0);
16024 return (typeA == typeB);
16025}
16026
16027/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000016028 * xmlSchemaCheckCOSCTDerivedOK:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016029 * @ctxt: the schema parser context
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000016030 * @type: the to-be derived complex type definition
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016031 * @baseType: the base complex type definition
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000016032 * @set: the given set
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016033 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016034 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016035 * Type Derivation OK (Complex) (cos-ct-derived-ok)
16036 *
16037 * STATUS: completed
16038 *
16039 * Returns 0 if the constraints are satisfied, or 1
16040 * if not.
16041 */
16042static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016043xmlSchemaCheckCOSCTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016044 xmlSchemaTypePtr type,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016045 xmlSchemaTypePtr baseType,
16046 int set)
16047{
16048 int equal = xmlSchemaAreEqualTypes(type, baseType);
16049 /* TODO: Error codes. */
16050 /*
16051 * SPEC "For a complex type definition (call it D, for derived)
16052 * to be validly derived from a type definition (call this
16053 * B, for base) given a subset of {extension, restriction}
16054 * all of the following must be true:"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016055 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016056 if (! equal) {
16057 /*
16058 * SPEC (1) "If B and D are not the same type definition, then the
16059 * {derivation method} of D must not be in the subset."
16060 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016061 if (((set & SUBSET_EXTENSION) && (WXS_IS_EXTENSION(type))) ||
16062 ((set & SUBSET_RESTRICTION) && (WXS_IS_RESTRICTION(type))))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016063 return (1);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016064 } else {
16065 /*
16066 * SPEC (2.1) "B and D must be the same type definition."
16067 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016068 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016069 }
16070 /*
16071 * SPEC (2.2) "B must be D's {base type definition}."
16072 */
16073 if (type->baseType == baseType)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016074 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016075 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010016076 * SPEC (2.3.1) "D's {base type definition} must not be the `ur-type
16077 * definition`."
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016078 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016079 if (WXS_IS_ANYTYPE(type->baseType))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000016080 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016081
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016082 if (WXS_IS_COMPLEX(type->baseType)) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016083 /*
16084 * SPEC (2.3.2.1) "If D's {base type definition} is complex, then it
16085 * must be validly derived from B given the subset as defined by this
16086 * constraint."
16087 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016088 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016089 baseType, set));
16090 } else {
16091 /*
16092 * SPEC (2.3.2.2) "If D's {base type definition} is simple, then it
16093 * must be validly derived from B given the subset as defined in Type
Jan Pokorný761c9e92013-11-29 23:26:27 +010016094 * Derivation OK (Simple) ($3.14.6).
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016095 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016096 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016097 baseType, set));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016098 }
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016099}
16100
16101/**
16102 * xmlSchemaCheckCOSDerivedOK:
16103 * @type: the derived simple type definition
16104 * @baseType: the base type definition
16105 *
16106 * Calls:
16107 * Type Derivation OK (Simple) AND Type Derivation OK (Complex)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016108 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016109 * Checks wheter @type can be validly derived from @baseType.
16110 *
16111 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016112 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016113static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016114xmlSchemaCheckCOSDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016115 xmlSchemaTypePtr type,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016116 xmlSchemaTypePtr baseType,
16117 int set)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016118{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016119 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016120 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016121 else
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016122 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016123}
16124
William M. Brack2f2a6632004-08-20 23:09:47 +000016125/**
16126 * xmlSchemaCheckCOSCTExtends:
16127 * @ctxt: the schema parser context
16128 * @type: the complex type definition
16129 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016130 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016131 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016132 * Derivation Valid (Extension) (cos-ct-extends)
16133 *
16134 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016135 * missing:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016136 * (1.5)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016137 * (1.4.3.2.2.2) "Particle Valid (Extension)"
William M. Brack2f2a6632004-08-20 23:09:47 +000016138 *
16139 * Returns 0 if the constraints are satisfied, a positive
16140 * error code if not and -1 if an internal error occured.
16141 */
16142static int
16143xmlSchemaCheckCOSCTExtends(xmlSchemaParserCtxtPtr ctxt,
16144 xmlSchemaTypePtr type)
16145{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016146 xmlSchemaTypePtr base = type->baseType;
16147 /*
16148 * TODO: Correct the error code; XML_SCHEMAP_COS_CT_EXTENDS_1_1 is used
16149 * temporarily only.
William M. Brack2f2a6632004-08-20 23:09:47 +000016150 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016151 /*
16152 * SPEC (1) "If the {base type definition} is a complex type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016153 * then all of the following must be true:"
16154 */
Daniel Veillarddee23482008-04-11 12:58:43 +000016155 if (WXS_IS_COMPLEX(base)) {
William M. Brack2f2a6632004-08-20 23:09:47 +000016156 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016157 * SPEC (1.1) "The {final} of the {base type definition} must not
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016158 * contain extension."
William M. Brack2f2a6632004-08-20 23:09:47 +000016159 */
16160 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16161 xmlSchemaPCustomErr(ctxt,
16162 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016163 WXS_BASIC_CAST type, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000016164 "The 'final' of the base type definition "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016165 "contains 'extension'", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000016166 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16167 }
Daniel Veillarddee23482008-04-11 12:58:43 +000016168
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016169 /*
16170 * ATTENTION: The constrains (1.2) and (1.3) are not applied,
16171 * since they are automatically satisfied through the
16172 * inheriting mechanism.
16173 * Note that even if redefining components, the inheriting mechanism
16174 * is used.
16175 */
16176#if 0
William M. Brack2f2a6632004-08-20 23:09:47 +000016177 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016178 * SPEC (1.2) "Its {attribute uses} must be a subset of the {attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016179 * uses}
16180 * of the complex type definition itself, that is, for every attribute
16181 * use in the {attribute uses} of the {base type definition}, there
16182 * must be an attribute use in the {attribute uses} of the complex
16183 * type definition itself whose {attribute declaration} has the same
16184 * {name}, {target namespace} and {type definition} as its attribute
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016185 * declaration"
William M. Brack2f2a6632004-08-20 23:09:47 +000016186 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016187 if (base->attrUses != NULL) {
16188 int i, j, found;
16189 xmlSchemaAttributeUsePtr use, buse;
William M. Brack2f2a6632004-08-20 23:09:47 +000016190
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016191 for (i = 0; i < (WXS_LIST_CAST base->attrUses)->nbItems; i ++) {
16192 buse = (WXS_LIST_CAST base->attrUses)->items[i];
16193 found = 0;
16194 if (type->attrUses != NULL) {
16195 use = (WXS_LIST_CAST type->attrUses)->items[j];
16196 for (j = 0; j < (WXS_LIST_CAST type->attrUses)->nbItems; j ++)
16197 {
16198 if ((WXS_ATTRUSE_DECL_NAME(use) ==
16199 WXS_ATTRUSE_DECL_NAME(buse)) &&
16200 (WXS_ATTRUSE_DECL_TNS(use) ==
16201 WXS_ATTRUSE_DECL_TNS(buse)) &&
16202 (WXS_ATTRUSE_TYPEDEF(use) ==
16203 WXS_ATTRUSE_TYPEDEF(buse))
16204 {
16205 found = 1;
16206 break;
16207 }
16208 }
16209 }
16210 if (! found) {
16211 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000016212
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016213 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16214 XML_SCHEMAP_COS_CT_EXTENDS_1_2,
16215 NULL, WXS_BASIC_CAST type,
Daniel Veillarddee23482008-04-11 12:58:43 +000016216 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016217 * TODO: The report does not indicate that also the
16218 * type needs to be the same.
16219 */
16220 "This type is missing a matching correspondent "
16221 "for its {base type}'s %s in its {attribute uses}",
16222 xmlSchemaGetComponentDesignation(&str,
16223 buse->children),
16224 NULL);
16225 FREE_AND_NULL(str)
16226 }
16227 }
16228 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016229 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016230 * SPEC (1.3) "If it has an {attribute wildcard}, the complex type
16231 * definition must also have one, and the base type definition's
16232 * {attribute wildcard}'s {namespace constraint} must be a subset
16233 * of the complex type definition's {attribute wildcard}'s {namespace
Jan Pokorný761c9e92013-11-29 23:26:27 +010016234 * constraint}, as defined by Wildcard Subset ($3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016235 */
Daniel Veillarddee23482008-04-11 12:58:43 +000016236
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016237 /*
16238 * MAYBE TODO: Enable if ever needed. But this will be needed only
16239 * if created the type via a schema construction API.
16240 */
16241 if (base->attributeWildcard != NULL) {
16242 if (type->attributeWilcard == NULL) {
16243 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000016244
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016245 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16246 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16247 NULL, type,
16248 "The base %s has an attribute wildcard, "
16249 "but this type is missing an attribute wildcard",
16250 xmlSchemaGetComponentDesignation(&str, base));
16251 FREE_AND_NULL(str)
16252
16253 } else if (xmlSchemaCheckCOSNSSubset(
16254 base->attributeWildcard, type->attributeWildcard))
16255 {
16256 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000016257
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016258 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16259 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16260 NULL, type,
16261 "The attribute wildcard is not a valid "
16262 "superset of the one in the base %s",
16263 xmlSchemaGetComponentDesignation(&str, base));
16264 FREE_AND_NULL(str)
16265 }
16266 }
16267#endif
16268 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016269 * SPEC (1.4) "One of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016270 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016271 if ((type->contentTypeDef != NULL) &&
16272 (type->contentTypeDef == base->contentTypeDef)) {
16273 /*
16274 * SPEC (1.4.1) "The {content type} of the {base type definition}
16275 * and the {content type} of the complex type definition itself
16276 * must be the same simple type definition"
16277 * PASS
16278 */
16279 } else if ((type->contentType == XML_SCHEMA_CONTENT_EMPTY) &&
16280 (base->contentType == XML_SCHEMA_CONTENT_EMPTY) ) {
16281 /*
16282 * SPEC (1.4.2) "The {content type} of both the {base type
16283 * definition} and the complex type definition itself must
16284 * be empty."
16285 * PASS
16286 */
16287 } else {
16288 /*
16289 * SPEC (1.4.3) "All of the following must be true:"
16290 */
16291 if (type->subtypes == NULL) {
16292 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016293 * SPEC 1.4.3.1 The {content type} of the complex type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016294 * definition itself must specify a particle.
16295 */
16296 xmlSchemaPCustomErr(ctxt,
16297 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016298 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016299 "The content type must specify a particle", NULL);
16300 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16301 }
16302 /*
16303 * SPEC (1.4.3.2) "One of the following must be true:"
16304 */
16305 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16306 /*
16307 * SPEC (1.4.3.2.1) "The {content type} of the {base type
16308 * definition} must be empty.
16309 * PASS
16310 */
16311 } else {
16312 /*
16313 * SPEC (1.4.3.2.2) "All of the following must be true:"
16314 */
16315 if ((type->contentType != base->contentType) ||
16316 ((type->contentType != XML_SCHEMA_CONTENT_MIXED) &&
16317 (type->contentType != XML_SCHEMA_CONTENT_ELEMENTS))) {
16318 /*
16319 * SPEC (1.4.3.2.2.1) "Both {content type}s must be mixed
16320 * or both must be element-only."
16321 */
16322 xmlSchemaPCustomErr(ctxt,
16323 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016324 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016325 "The content type of both, the type and its base "
16326 "type, must either 'mixed' or 'element-only'", NULL);
16327 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016328 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016329 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016330 * URGENT TODO SPEC (1.4.3.2.2.2) "The particle of the
Jan Pokorný761c9e92013-11-29 23:26:27 +010016331 * complex type definition must be a `valid extension`
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016332 * of the {base type definition}'s particle, as defined
Jan Pokorný761c9e92013-11-29 23:26:27 +010016333 * in Particle Valid (Extension) ($3.9.6)."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016334 *
16335 * NOTE that we won't check "Particle Valid (Extension)",
16336 * since it is ensured by the derivation process in
16337 * xmlSchemaTypeFixup(). We need to implement this when heading
16338 * for a construction API
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016339 * TODO: !! This is needed to be checked if redefining a type !!
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016340 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016341 }
16342 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016343 * URGENT TODO (1.5)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016344 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016345 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016346 } else {
16347 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016348 * SPEC (2) "If the {base type definition} is a simple type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016349 * then all of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016350 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016351 if (type->contentTypeDef != base) {
16352 /*
16353 * SPEC (2.1) "The {content type} must be the same simple type
16354 * definition."
16355 */
16356 xmlSchemaPCustomErr(ctxt,
16357 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016358 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016359 "The content type must be the simple base type", NULL);
16360 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16361 }
16362 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16363 /*
16364 * SPEC (2.2) "The {final} of the {base type definition} must not
16365 * contain extension"
16366 * NOTE that this is the same as (1.1).
16367 */
16368 xmlSchemaPCustomErr(ctxt,
16369 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016370 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016371 "The 'final' of the base type definition "
16372 "contains 'extension'", NULL);
16373 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016374 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016375 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016376 return (0);
William M. Brack2f2a6632004-08-20 23:09:47 +000016377}
16378
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016379/**
16380 * xmlSchemaCheckDerivationOKRestriction:
16381 * @ctxt: the schema parser context
16382 * @type: the complex type definition
16383 *
16384 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016385 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016386 * Derivation Valid (Restriction, Complex) (derivation-ok-restriction)
16387 *
16388 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016389 * missing:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016390 * (5.4.2) ???
16391 *
16392 * ATTENTION:
16393 * In XML Schema 1.1 this will be:
16394 * Validation Rule: Checking complex type subsumption
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016395 *
16396 * Returns 0 if the constraints are satisfied, a positive
16397 * error code if not and -1 if an internal error occured.
16398 */
16399static int
16400xmlSchemaCheckDerivationOKRestriction(xmlSchemaParserCtxtPtr ctxt,
16401 xmlSchemaTypePtr type)
16402{
16403 xmlSchemaTypePtr base;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016404
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016405 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016406 * TODO: Correct the error code; XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1 is used
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016407 * temporarily only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016408 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016409 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016410 if (! WXS_IS_COMPLEX(base)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000016411 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016412 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16413 type->node, WXS_BASIC_CAST type,
16414 "The base type must be a complex type", NULL, NULL);
16415 return(ctxt->err);
16416 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016417 if (base->flags & XML_SCHEMAS_TYPE_FINAL_RESTRICTION) {
16418 /*
16419 * SPEC (1) "The {base type definition} must be a complex type
16420 * definition whose {final} does not contain restriction."
16421 */
Daniel Veillarddee23482008-04-11 12:58:43 +000016422 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016423 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16424 type->node, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016425 "The 'final' of the base type definition "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016426 "contains 'restriction'", NULL, NULL);
16427 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016428 }
16429 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016430 * SPEC (2), (3) and (4)
16431 * Those are handled in a separate function, since the
16432 * same constraints are needed for redefinition of
16433 * attribute groups as well.
16434 */
16435 if (xmlSchemaCheckDerivationOKRestriction2to4(ctxt,
16436 XML_SCHEMA_ACTION_DERIVE,
16437 WXS_BASIC_CAST type, WXS_BASIC_CAST base,
16438 type->attrUses, base->attrUses,
16439 type->attributeWildcard,
16440 base->attributeWildcard) == -1)
16441 {
16442 return(-1);
16443 }
16444 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016445 * SPEC (5) "One of the following must be true:"
16446 */
16447 if (base->builtInType == XML_SCHEMAS_ANYTYPE) {
16448 /*
16449 * SPEC (5.1) "The {base type definition} must be the
Jan Pokorný761c9e92013-11-29 23:26:27 +010016450 * `ur-type definition`."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016451 * PASS
16452 */
16453 } else if ((type->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16454 (type->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16455 /*
16456 * SPEC (5.2.1) "The {content type} of the complex type definition
16457 * must be a simple type definition"
16458 *
16459 * SPEC (5.2.2) "One of the following must be true:"
16460 */
16461 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016462 (base->contentType == XML_SCHEMA_CONTENT_BASIC))
16463 {
16464 int err;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016465 /*
16466 * SPEC (5.2.2.1) "The {content type} of the {base type
16467 * definition} must be a simple type definition from which
16468 * the {content type} is validly derived given the empty
Jan Pokorný761c9e92013-11-29 23:26:27 +010016469 * set as defined in Type Derivation OK (Simple) ($3.14.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016470 *
16471 * ATTENTION TODO: This seems not needed if the type implicitely
16472 * derived from the base type.
Daniel Veillarddee23482008-04-11 12:58:43 +000016473 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016474 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016475 err = xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST ctxt,
16476 type->contentTypeDef, base->contentTypeDef, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016477 if (err != 0) {
16478 xmlChar *strA = NULL, *strB = NULL;
16479
16480 if (err == -1)
16481 return(-1);
16482 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16483 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16484 NULL, WXS_BASIC_CAST type,
16485 "The {content type} %s is not validly derived from the "
16486 "base type's {content type} %s",
16487 xmlSchemaGetComponentDesignation(&strA,
16488 type->contentTypeDef),
16489 xmlSchemaGetComponentDesignation(&strB,
16490 base->contentTypeDef));
16491 FREE_AND_NULL(strA);
16492 FREE_AND_NULL(strB);
16493 return(ctxt->err);
16494 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016495 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
16496 (xmlSchemaIsParticleEmptiable(
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016497 (xmlSchemaParticlePtr) base->subtypes))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016498 /*
16499 * SPEC (5.2.2.2) "The {base type definition} must be mixed
Jan Pokorný761c9e92013-11-29 23:26:27 +010016500 * and have a particle which is `emptiable` as defined in
16501 * Particle Emptiable ($3.9.6)."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016502 * PASS
16503 */
16504 } else {
16505 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016506 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16507 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016508 "The content type of the base type must be either "
16509 "a simple type or 'mixed' and an emptiable particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016510 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016511 }
16512 } else if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16513 /*
16514 * SPEC (5.3.1) "The {content type} of the complex type itself must
16515 * be empty"
16516 */
16517 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16518 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016519 * SPEC (5.3.2.1) "The {content type} of the {base type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016520 * definition} must also be empty."
16521 * PASS
16522 */
16523 } else if (((base->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
16524 (base->contentType == XML_SCHEMA_CONTENT_MIXED)) &&
16525 xmlSchemaIsParticleEmptiable(
16526 (xmlSchemaParticlePtr) base->subtypes)) {
16527 /*
16528 * SPEC (5.3.2.2) "The {content type} of the {base type
16529 * definition} must be elementOnly or mixed and have a particle
Jan Pokorný761c9e92013-11-29 23:26:27 +010016530 * which is `emptiable` as defined in Particle Emptiable ($3.9.6)."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016531 * PASS
16532 */
16533 } else {
16534 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016535 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16536 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016537 "The content type of the base type must be either "
16538 "empty or 'mixed' (or 'elements-only') and an emptiable "
16539 "particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016540 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016541 }
16542 } else if ((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016543 WXS_HAS_MIXED_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016544 /*
16545 * SPEC (5.4.1.1) "The {content type} of the complex type definition
16546 * itself must be element-only"
Daniel Veillarddee23482008-04-11 12:58:43 +000016547 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016548 if (WXS_HAS_MIXED_CONTENT(type) && (! WXS_HAS_MIXED_CONTENT(base))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016549 /*
16550 * SPEC (5.4.1.2) "The {content type} of the complex type
16551 * definition itself and of the {base type definition} must be
16552 * mixed"
16553 */
16554 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016555 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16556 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016557 "If the content type is 'mixed', then the content type of the "
16558 "base type must also be 'mixed'", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016559 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016560 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016561 /*
16562 * SPEC (5.4.2) "The particle of the complex type definition itself
Jan Pokorný761c9e92013-11-29 23:26:27 +010016563 * must be a `valid restriction` of the particle of the {content
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016564 * type} of the {base type definition} as defined in Particle Valid
Jan Pokorný761c9e92013-11-29 23:26:27 +010016565 * (Restriction) ($3.9.6).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016566 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016567 * URGENT TODO: (5.4.2)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016568 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016569 } else {
16570 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016571 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16572 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016573 "The type is not a valid restriction of its base type", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016574 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016575 }
16576 return (0);
16577}
16578
16579/**
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016580 * xmlSchemaCheckCTComponent:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016581 * @ctxt: the schema parser context
16582 * @type: the complex type definition
16583 *
16584 * (3.4.6) Constraints on Complex Type Definition Schema Components
16585 *
16586 * Returns 0 if the constraints are satisfied, a positive
16587 * error code if not and -1 if an internal error occured.
16588 */
16589static int
16590xmlSchemaCheckCTComponent(xmlSchemaParserCtxtPtr ctxt,
16591 xmlSchemaTypePtr type)
16592{
16593 int ret;
16594 /*
16595 * Complex Type Definition Properties Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016596 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016597 ret = xmlSchemaCheckCTPropsCorrect(ctxt, type);
16598 if (ret != 0)
16599 return (ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016600 if (WXS_IS_EXTENSION(type))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016601 ret = xmlSchemaCheckCOSCTExtends(ctxt, type);
16602 else
16603 ret = xmlSchemaCheckDerivationOKRestriction(ctxt, type);
16604 return (ret);
16605}
16606
16607/**
16608 * xmlSchemaCheckSRCCT:
16609 * @ctxt: the schema parser context
16610 * @type: the complex type definition
16611 *
16612 * (3.4.3) Constraints on XML Representations of Complex Type Definitions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016613 * Schema Representation Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016614 * Complex Type Definition Representation OK (src-ct)
16615 *
16616 * Returns 0 if the constraints are satisfied, a positive
16617 * error code if not and -1 if an internal error occured.
16618 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016619static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016620xmlSchemaCheckSRCCT(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016621 xmlSchemaTypePtr type)
16622{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016623 xmlSchemaTypePtr base;
16624 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016625
16626 /*
16627 * TODO: Adjust the error codes here, as I used
16628 * XML_SCHEMAP_SRC_CT_1 only yet.
16629 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016630 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016631 if (! WXS_HAS_SIMPLE_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016632 /*
16633 * 1 If the <complexContent> alternative is chosen, the type definition
Jan Pokorný761c9e92013-11-29 23:26:27 +010016634 * `resolved` to by the `actual value` of the base [attribute]
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016635 * must be a complex type definition;
16636 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016637 if (! WXS_IS_COMPLEX(base)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016638 xmlChar *str = NULL;
16639 xmlSchemaPCustomErr(ctxt,
16640 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016641 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016642 "If using <complexContent>, the base type is expected to be "
16643 "a complex type. The base type '%s' is a simple type",
16644 xmlSchemaFormatQName(&str, base->targetNamespace,
16645 base->name));
16646 FREE_AND_NULL(str)
16647 return (XML_SCHEMAP_SRC_CT_1);
16648 }
16649 } else {
16650 /*
16651 * SPEC
16652 * 2 If the <simpleContent> alternative is chosen, all of the
16653 * following must be true:
Jan Pokorný761c9e92013-11-29 23:26:27 +010016654 * 2.1 The type definition `resolved` to by the `actual value` of the
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016655 * base [attribute] must be one of the following:
16656 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016657 if (WXS_IS_SIMPLE(base)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016658 if (WXS_IS_EXTENSION(type) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016659 xmlChar *str = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016660 /*
16661 * 2.1.3 only if the <extension> alternative is also
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016662 * chosen, a simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016663 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016664 /* TODO: Change error code to ..._SRC_CT_2_1_3. */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016665 xmlSchemaPCustomErr(ctxt,
16666 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016667 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016668 "If using <simpleContent> and <restriction>, the base "
16669 "type must be a complex type. The base type '%s' is "
16670 "a simple type",
16671 xmlSchemaFormatQName(&str, base->targetNamespace,
16672 base->name));
16673 FREE_AND_NULL(str)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016674 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016675 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016676 } else {
16677 /* Base type is a complex type. */
16678 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16679 (base->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16680 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016681 * 2.1.1 a complex type definition whose {content type} is a
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016682 * simple type definition;
16683 * PASS
16684 */
16685 if (base->contentTypeDef == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016686 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016687 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016688 "Internal error: xmlSchemaCheckSRCCT, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016689 "'%s', base type has no content type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016690 type->name);
16691 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016692 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016693 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016694 (WXS_IS_RESTRICTION(type))) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016695
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016696 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016697 * 2.1.2 only if the <restriction> alternative is also
16698 * chosen, a complex type definition whose {content type}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016699 * is mixed and a particle emptiable.
16700 */
16701 if (! xmlSchemaIsParticleEmptiable(
16702 (xmlSchemaParticlePtr) base->subtypes)) {
16703 ret = XML_SCHEMAP_SRC_CT_1;
Daniel Veillarddee23482008-04-11 12:58:43 +000016704 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016705 /*
16706 * Attention: at this point the <simpleType> child is in
16707 * ->contentTypeDef (put there during parsing).
Daniel Veillarddee23482008-04-11 12:58:43 +000016708 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016709 if (type->contentTypeDef == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016710 xmlChar *str = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016711 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016712 * 2.2 If clause 2.1.2 above is satisfied, then there
16713 * must be a <simpleType> among the [children] of
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016714 * <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016715 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016716 /* TODO: Change error code to ..._SRC_CT_2_2. */
16717 xmlSchemaPCustomErr(ctxt,
16718 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016719 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016720 "A <simpleType> is expected among the children "
16721 "of <restriction>, if <simpleContent> is used and "
16722 "the base type '%s' is a complex type",
16723 xmlSchemaFormatQName(&str, base->targetNamespace,
16724 base->name));
16725 FREE_AND_NULL(str)
16726 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016727 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016728 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016729 ret = XML_SCHEMAP_SRC_CT_1;
16730 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016731 }
16732 if (ret > 0) {
16733 xmlChar *str = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016734 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016735 xmlSchemaPCustomErr(ctxt,
16736 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016737 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016738 "If <simpleContent> and <restriction> is used, the "
16739 "base type must be a simple type or a complex type with "
16740 "mixed content and particle emptiable. The base type "
16741 "'%s' is none of those",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016742 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016743 base->name));
16744 } else {
16745 xmlSchemaPCustomErr(ctxt,
16746 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016747 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016748 "If <simpleContent> and <extension> is used, the "
16749 "base type must be a simple type. The base type '%s' "
16750 "is a complex type",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016751 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016752 base->name));
16753 }
16754 FREE_AND_NULL(str)
16755 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016756 }
16757 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016758 * SPEC (3) "The corresponding complex type definition component must
16759 * satisfy the conditions set out in Constraints on Complex Type
Jan Pokorný761c9e92013-11-29 23:26:27 +010016760 * Definition Schema Components ($3.4.6);"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016761 * NOTE (3) will be done in xmlSchemaTypeFixup().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016762 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016763 /*
16764 * SPEC (4) If clause 2.2.1 or clause 2.2.2 in the correspondence specification
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016765 * above for {attribute wildcard} is satisfied, the intensional
16766 * intersection must be expressible, as defined in Attribute Wildcard
Jan Pokorný761c9e92013-11-29 23:26:27 +010016767 * Intersection ($3.10.6).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016768 * NOTE (4) is done in xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016769 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016770 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016771}
William M. Brack2f2a6632004-08-20 23:09:47 +000016772
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016773#ifdef ENABLE_PARTICLE_RESTRICTION
16774/**
16775 * xmlSchemaCheckParticleRangeOK:
16776 * @ctxt: the schema parser context
16777 * @type: the complex type definition
16778 *
16779 * (3.9.6) Constraints on Particle Schema Components
16780 * Schema Component Constraint:
16781 * Occurrence Range OK (range-ok)
16782 *
16783 * STATUS: complete
16784 *
16785 * Returns 0 if the constraints are satisfied, a positive
16786 * error code if not and -1 if an internal error occured.
16787 */
16788static int
16789xmlSchemaCheckParticleRangeOK(int rmin, int rmax,
16790 int bmin, int bmax)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016791{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016792 if (rmin < bmin)
16793 return (1);
16794 if ((bmax != UNBOUNDED) &&
16795 (rmax > bmax))
16796 return (1);
16797 return (0);
16798}
16799
16800/**
16801 * xmlSchemaCheckRCaseNameAndTypeOK:
16802 * @ctxt: the schema parser context
16803 * @r: the restricting element declaration particle
16804 * @b: the base element declaration particle
16805 *
16806 * (3.9.6) Constraints on Particle Schema Components
16807 * Schema Component Constraint:
16808 * Particle Restriction OK (Elt:Elt -- NameAndTypeOK)
16809 * (rcase-NameAndTypeOK)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016810 *
16811 * STATUS:
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016812 * MISSING (3.2.3)
16813 * CLARIFY: (3.2.2)
16814 *
16815 * Returns 0 if the constraints are satisfied, a positive
16816 * error code if not and -1 if an internal error occured.
16817 */
16818static int
16819xmlSchemaCheckRCaseNameAndTypeOK(xmlSchemaParserCtxtPtr ctxt,
16820 xmlSchemaParticlePtr r,
16821 xmlSchemaParticlePtr b)
16822{
16823 xmlSchemaElementPtr elemR, elemB;
16824
16825 /* TODO: Error codes (rcase-NameAndTypeOK). */
16826 elemR = (xmlSchemaElementPtr) r->children;
16827 elemB = (xmlSchemaElementPtr) b->children;
16828 /*
16829 * SPEC (1) "The declarations' {name}s and {target namespace}s are
16830 * the same."
16831 */
16832 if ((elemR != elemB) &&
16833 ((! xmlStrEqual(elemR->name, elemB->name)) ||
16834 (! xmlStrEqual(elemR->targetNamespace, elemB->targetNamespace))))
16835 return (1);
16836 /*
16837 * SPEC (2) "R's occurrence range is a valid restriction of B's
Jan Pokorný761c9e92013-11-29 23:26:27 +010016838 * occurrence range as defined by Occurrence Range OK ($3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016839 */
16840 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16841 b->minOccurs, b->maxOccurs) != 0)
16842 return (1);
16843 /*
16844 * SPEC (3.1) "Both B's declaration's {scope} and R's declaration's
16845 * {scope} are global."
16846 */
16847 if (elemR == elemB)
16848 return (0);
16849 /*
16850 * SPEC (3.2.1) "Either B's {nillable} is true or R's {nillable} is false."
16851 */
16852 if (((elemB->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) &&
16853 (elemR->flags & XML_SCHEMAS_ELEM_NILLABLE))
16854 return (1);
16855 /*
16856 * SPEC (3.2.2) "either B's declaration's {value constraint} is absent,
16857 * or is not fixed, or R's declaration's {value constraint} is fixed
16858 * with the same value."
16859 */
16860 if ((elemB->value != NULL) && (elemB->flags & XML_SCHEMAS_ELEM_FIXED) &&
16861 ((elemR->value == NULL) ||
16862 ((elemR->flags & XML_SCHEMAS_ELEM_FIXED) == 0) ||
16863 /* TODO: Equality of the initial value or normalized or canonical? */
16864 (! xmlStrEqual(elemR->value, elemB->value))))
16865 return (1);
16866 /*
16867 * TODO: SPEC (3.2.3) "R's declaration's {identity-constraint
16868 * definitions} is a subset of B's declaration's {identity-constraint
16869 * definitions}, if any."
16870 */
16871 if (elemB->idcs != NULL) {
16872 /* TODO */
16873 }
16874 /*
16875 * SPEC (3.2.4) "R's declaration's {disallowed substitutions} is a
16876 * superset of B's declaration's {disallowed substitutions}."
16877 */
16878 if (((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) &&
16879 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) == 0)) ||
16880 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) &&
16881 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) == 0)) ||
16882 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) &&
16883 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) == 0)))
16884 return (1);
16885 /*
16886 * SPEC (3.2.5) "R's {type definition} is validly derived given
16887 * {extension, list, union} from B's {type definition}"
16888 *
16889 * BADSPEC TODO: What's the point of adding "list" and "union" to the
16890 * set, if the corresponding constraints handle "restriction" and
16891 * "extension" only?
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016892 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016893 */
16894 {
16895 int set = 0;
16896
16897 set |= SUBSET_EXTENSION;
16898 set |= SUBSET_LIST;
16899 set |= SUBSET_UNION;
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016900 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST ctxt, elemR->subtypes,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016901 elemB->subtypes, set) != 0)
16902 return (1);
16903 }
16904 return (0);
16905}
16906
16907/**
16908 * xmlSchemaCheckRCaseNSCompat:
16909 * @ctxt: the schema parser context
16910 * @r: the restricting element declaration particle
16911 * @b: the base wildcard particle
16912 *
16913 * (3.9.6) Constraints on Particle Schema Components
16914 * Schema Component Constraint:
16915 * Particle Derivation OK (Elt:Any -- NSCompat)
16916 * (rcase-NSCompat)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016917 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016918 * STATUS: complete
16919 *
16920 * Returns 0 if the constraints are satisfied, a positive
16921 * error code if not and -1 if an internal error occured.
16922 */
16923static int
16924xmlSchemaCheckRCaseNSCompat(xmlSchemaParserCtxtPtr ctxt,
16925 xmlSchemaParticlePtr r,
16926 xmlSchemaParticlePtr b)
16927{
16928 /* TODO:Error codes (rcase-NSCompat). */
16929 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010016930 * SPEC "For an element declaration particle to be a `valid restriction`
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016931 * of a wildcard particle all of the following must be true:"
16932 *
Jan Pokorný761c9e92013-11-29 23:26:27 +010016933 * SPEC (1) "The element declaration's {target namespace} is `valid`
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016934 * with respect to the wildcard's {namespace constraint} as defined by
Jan Pokorný761c9e92013-11-29 23:26:27 +010016935 * Wildcard allows Namespace Name ($3.10.4)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016936 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016937 if (xmlSchemaCheckCVCWildcardNamespace((xmlSchemaWildcardPtr) b->children,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016938 ((xmlSchemaElementPtr) r->children)->targetNamespace) != 0)
16939 return (1);
16940 /*
16941 * SPEC (2) "R's occurrence range is a valid restriction of B's
Jan Pokorný761c9e92013-11-29 23:26:27 +010016942 * occurrence range as defined by Occurrence Range OK ($3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016943 */
16944 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16945 b->minOccurs, b->maxOccurs) != 0)
16946 return (1);
16947
16948 return (0);
16949}
16950
16951/**
16952 * xmlSchemaCheckRCaseRecurseAsIfGroup:
16953 * @ctxt: the schema parser context
16954 * @r: the restricting element declaration particle
16955 * @b: the base model group particle
16956 *
16957 * (3.9.6) Constraints on Particle Schema Components
16958 * Schema Component Constraint:
16959 * Particle Derivation OK (Elt:All/Choice/Sequence -- RecurseAsIfGroup)
16960 * (rcase-RecurseAsIfGroup)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016961 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016962 * STATUS: TODO
16963 *
16964 * Returns 0 if the constraints are satisfied, a positive
16965 * error code if not and -1 if an internal error occured.
16966 */
16967static int
16968xmlSchemaCheckRCaseRecurseAsIfGroup(xmlSchemaParserCtxtPtr ctxt,
16969 xmlSchemaParticlePtr r,
16970 xmlSchemaParticlePtr b)
16971{
16972 /* TODO: Error codes (rcase-RecurseAsIfGroup). */
16973 TODO
16974 return (0);
16975}
16976
16977/**
16978 * xmlSchemaCheckRCaseNSSubset:
16979 * @ctxt: the schema parser context
16980 * @r: the restricting wildcard particle
16981 * @b: the base wildcard particle
16982 *
16983 * (3.9.6) Constraints on Particle Schema Components
16984 * Schema Component Constraint:
16985 * Particle Derivation OK (Any:Any -- NSSubset)
16986 * (rcase-NSSubset)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016987 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016988 * STATUS: complete
16989 *
16990 * Returns 0 if the constraints are satisfied, a positive
16991 * error code if not and -1 if an internal error occured.
16992 */
16993static int
16994xmlSchemaCheckRCaseNSSubset(xmlSchemaParserCtxtPtr ctxt,
16995 xmlSchemaParticlePtr r,
16996 xmlSchemaParticlePtr b,
16997 int isAnyTypeBase)
16998{
16999 /* TODO: Error codes (rcase-NSSubset). */
17000 /*
17001 * SPEC (1) "R's occurrence range is a valid restriction of B's
Jan Pokorný761c9e92013-11-29 23:26:27 +010017002 * occurrence range as defined by Occurrence Range OK ($3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017003 */
17004 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
17005 b->minOccurs, b->maxOccurs))
17006 return (1);
17007 /*
17008 * SPEC (2) "R's {namespace constraint} must be an intensional subset
Jan Pokorný761c9e92013-11-29 23:26:27 +010017009 * of B's {namespace constraint} as defined by Wildcard Subset ($3.10.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017010 */
17011 if (xmlSchemaCheckCOSNSSubset((xmlSchemaWildcardPtr) r->children,
17012 (xmlSchemaWildcardPtr) b->children))
17013 return (1);
17014 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010017015 * SPEC (3) "Unless B is the content model wildcard of the `ur-type
17016 * definition`, R's {process contents} must be identical to or stronger
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017017 * than B's {process contents}, where strict is stronger than lax is
17018 * stronger than skip."
17019 */
17020 if (! isAnyTypeBase) {
17021 if ( ((xmlSchemaWildcardPtr) r->children)->processContents <
17022 ((xmlSchemaWildcardPtr) b->children)->processContents)
17023 return (1);
17024 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017025
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017026 return (0);
17027}
17028
17029/**
17030 * xmlSchemaCheckCOSParticleRestrict:
17031 * @ctxt: the schema parser context
17032 * @type: the complex type definition
17033 *
17034 * (3.9.6) Constraints on Particle Schema Components
17035 * Schema Component Constraint:
17036 * Particle Valid (Restriction) (cos-particle-restrict)
17037 *
17038 * STATUS: TODO
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
17044xmlSchemaCheckCOSParticleRestrict(xmlSchemaParserCtxtPtr ctxt,
17045 xmlSchemaParticlePtr r,
17046 xmlSchemaParticlePtr b)
17047{
17048 int ret = 0;
17049
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017050 /*part = WXS_TYPE_PARTICLE(type);
17051 basePart = WXS_TYPE_PARTICLE(base);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017052 */
17053
17054 TODO
17055
17056 /*
17057 * SPEC (1) "They are the same particle."
17058 */
17059 if (r == b)
17060 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017061
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017062
17063 return (0);
17064}
17065
Daniel Veillard15724252008-08-30 15:01:04 +000017066#if 0
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017067/**
17068 * xmlSchemaCheckRCaseNSRecurseCheckCardinality:
17069 * @ctxt: the schema parser context
17070 * @r: the model group particle
17071 * @b: the base wildcard particle
17072 *
17073 * (3.9.6) Constraints on Particle Schema Components
17074 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017075 * Particle Derivation OK (All/Choice/Sequence:Any --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017076 * NSRecurseCheckCardinality)
17077 * (rcase-NSRecurseCheckCardinality)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017078 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017079 * STATUS: TODO: subst-groups
17080 *
17081 * Returns 0 if the constraints are satisfied, a positive
17082 * error code if not and -1 if an internal error occured.
17083 */
17084static int
17085xmlSchemaCheckRCaseNSRecurseCheckCardinality(xmlSchemaParserCtxtPtr ctxt,
17086 xmlSchemaParticlePtr r,
17087 xmlSchemaParticlePtr b)
17088{
17089 xmlSchemaParticlePtr part;
17090 /* TODO: Error codes (rcase-NSRecurseCheckCardinality). */
17091 if ((r->children == NULL) || (r->children->children == NULL))
17092 return (-1);
17093 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010017094 * SPEC "For a group particle to be a `valid restriction` of a
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017095 * wildcard particle..."
17096 *
Jan Pokorný761c9e92013-11-29 23:26:27 +010017097 * SPEC (1) "Every member of the {particles} of the group is a `valid
17098 * restriction` of the wildcard as defined by
17099 * Particle Valid (Restriction) ($3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017100 */
17101 part = (xmlSchemaParticlePtr) r->children->children;
17102 do {
17103 if (xmlSchemaCheckCOSParticleRestrict(ctxt, part, b))
17104 return (1);
17105 part = (xmlSchemaParticlePtr) part->next;
17106 } while (part != NULL);
17107 /*
17108 * SPEC (2) "The effective total range of the group [...] is a
17109 * valid restriction of B's occurrence range as defined by
Jan Pokorný761c9e92013-11-29 23:26:27 +010017110 * Occurrence Range OK ($3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017111 */
17112 if (xmlSchemaCheckParticleRangeOK(
17113 xmlSchemaGetParticleTotalRangeMin(r),
17114 xmlSchemaGetParticleTotalRangeMax(r),
17115 b->minOccurs, b->maxOccurs) != 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017116 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017117 return (0);
17118}
Daniel Veillard15724252008-08-30 15:01:04 +000017119#endif
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017120
17121/**
17122 * xmlSchemaCheckRCaseRecurse:
17123 * @ctxt: the schema parser context
17124 * @r: the <all> or <sequence> model group particle
17125 * @b: the base <all> or <sequence> model group particle
17126 *
17127 * (3.9.6) Constraints on Particle Schema Components
17128 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017129 * Particle Derivation OK (All:All,Sequence:Sequence --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017130 Recurse)
17131 * (rcase-Recurse)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017132 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017133 * STATUS: ?
17134 * TODO: subst-groups
17135 *
17136 * Returns 0 if the constraints are satisfied, a positive
17137 * error code if not and -1 if an internal error occured.
17138 */
17139static int
17140xmlSchemaCheckRCaseRecurse(xmlSchemaParserCtxtPtr ctxt,
17141 xmlSchemaParticlePtr r,
17142 xmlSchemaParticlePtr b)
17143{
17144 /* xmlSchemaParticlePtr part; */
17145 /* TODO: Error codes (rcase-Recurse). */
17146 if ((r->children == NULL) || (b->children == NULL) ||
17147 (r->children->type != b->children->type))
17148 return (-1);
17149 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010017150 * SPEC "For an all or sequence group particle to be a `valid
17151 * restriction` of another group particle with the same {compositor}..."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017152 *
17153 * SPEC (1) "R's occurrence range is a valid restriction of B's
Jan Pokorný761c9e92013-11-29 23:26:27 +010017154 * occurrence range as defined by Occurrence Range OK ($3.9.6)."
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017155 */
17156 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
17157 b->minOccurs, b->maxOccurs))
17158 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017159
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017160
17161 return (0);
17162}
17163
17164#endif
17165
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017166#define FACET_RESTR_MUTUAL_ERR(fac1, fac2) \
17167 xmlSchemaPCustomErrExt(pctxt, \
17168 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017169 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017170 "It is an error for both '%s' and '%s' to be specified on the "\
17171 "same type definition", \
17172 BAD_CAST xmlSchemaFacetTypeToString(fac1->type), \
17173 BAD_CAST xmlSchemaFacetTypeToString(fac2->type), NULL);
17174
17175#define FACET_RESTR_ERR(fac1, msg) \
17176 xmlSchemaPCustomErr(pctxt, \
17177 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017178 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017179 msg, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017180
17181#define FACET_RESTR_FIXED_ERR(fac) \
17182 xmlSchemaPCustomErr(pctxt, \
17183 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017184 WXS_BASIC_CAST fac, fac->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017185 "The base type's facet is 'fixed', thus the value must not " \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017186 "differ", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017187
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017188static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017189xmlSchemaDeriveFacetErr(xmlSchemaParserCtxtPtr pctxt,
17190 xmlSchemaFacetPtr facet1,
17191 xmlSchemaFacetPtr facet2,
17192 int lessGreater,
17193 int orEqual,
17194 int ofBase)
17195{
17196 xmlChar *msg = NULL;
17197
17198 msg = xmlStrdup(BAD_CAST "'");
17199 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet1->type));
17200 msg = xmlStrcat(msg, BAD_CAST "' has to be");
17201 if (lessGreater == 0)
17202 msg = xmlStrcat(msg, BAD_CAST " equal to");
17203 if (lessGreater == 1)
17204 msg = xmlStrcat(msg, BAD_CAST " greater than");
17205 else
17206 msg = xmlStrcat(msg, BAD_CAST " less than");
17207
17208 if (orEqual)
17209 msg = xmlStrcat(msg, BAD_CAST " or equal to");
17210 msg = xmlStrcat(msg, BAD_CAST " '");
17211 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet2->type));
17212 if (ofBase)
17213 msg = xmlStrcat(msg, BAD_CAST "' of the base type");
17214 else
17215 msg = xmlStrcat(msg, BAD_CAST "'");
17216
17217 xmlSchemaPCustomErr(pctxt,
17218 XML_SCHEMAP_INVALID_FACET_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017219 WXS_BASIC_CAST facet1, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017220 (const char *) msg, NULL);
17221
17222 if (msg != NULL)
17223 xmlFree(msg);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017224}
17225
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017226/*
17227* xmlSchemaDeriveAndValidateFacets:
17228*
17229* Schema Component Constraint: Simple Type Restriction (Facets)
17230* (st-restrict-facets)
17231*/
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017232static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017233xmlSchemaDeriveAndValidateFacets(xmlSchemaParserCtxtPtr pctxt,
17234 xmlSchemaTypePtr type)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017235{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017236 xmlSchemaTypePtr base = type->baseType;
17237 xmlSchemaFacetLinkPtr link, cur, last = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017238 xmlSchemaFacetPtr facet, bfacet,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017239 flength = NULL, ftotdig = NULL, ffracdig = NULL,
17240 fmaxlen = NULL, fminlen = NULL, /* facets of the current type */
17241 fmininc = NULL, fmaxinc = NULL,
17242 fminexc = NULL, fmaxexc = NULL,
17243 bflength = NULL, bftotdig = NULL, bffracdig = NULL,
17244 bfmaxlen = NULL, bfminlen = NULL, /* facets of the base type */
17245 bfmininc = NULL, bfmaxinc = NULL,
17246 bfminexc = NULL, bfmaxexc = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017247 int res; /* err = 0, fixedErr; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017248
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017249 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017250 * SPEC st-restrict-facets 1:
Daniel Veillarddee23482008-04-11 12:58:43 +000017251 * "The {variety} of R is the same as that of B."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017252 */
17253 /*
17254 * SPEC st-restrict-facets 2:
17255 * "If {variety} is atomic, the {primitive type definition}
17256 * of R is the same as that of B."
17257 *
17258 * NOTE: we leave 1 & 2 out for now, since this will be
17259 * satisfied by the derivation process.
17260 * CONSTRUCTION TODO: Maybe needed if using a construction API.
17261 */
17262 /*
17263 * SPEC st-restrict-facets 3:
17264 * "The {facets} of R are the union of S and the {facets}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017265 * of B, eliminating duplicates. To eliminate duplicates,
17266 * when a facet of the same kind occurs in both S and the
17267 * {facets} of B, the one in the {facets} of B is not
17268 * included, with the exception of enumeration and pattern
17269 * facets, for which multiple occurrences with distinct values
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017270 * are allowed."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017271 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017272
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017273 if ((type->facetSet == NULL) && (base->facetSet == NULL))
17274 return (0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017275
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017276 last = type->facetSet;
17277 if (last != NULL)
17278 while (last->next != NULL)
17279 last = last->next;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017280
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017281 for (cur = type->facetSet; cur != NULL; cur = cur->next) {
17282 facet = cur->facet;
17283 switch (facet->type) {
17284 case XML_SCHEMA_FACET_LENGTH:
17285 flength = facet; break;
17286 case XML_SCHEMA_FACET_MINLENGTH:
17287 fminlen = facet; break;
17288 case XML_SCHEMA_FACET_MININCLUSIVE:
17289 fmininc = facet; break;
17290 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17291 fminexc = facet; break;
17292 case XML_SCHEMA_FACET_MAXLENGTH:
17293 fmaxlen = facet; break;
17294 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17295 fmaxinc = facet; break;
17296 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17297 fmaxexc = facet; break;
17298 case XML_SCHEMA_FACET_TOTALDIGITS:
17299 ftotdig = facet; break;
17300 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17301 ffracdig = facet; break;
17302 default:
17303 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017304 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017305 }
17306 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17307 facet = cur->facet;
17308 switch (facet->type) {
17309 case XML_SCHEMA_FACET_LENGTH:
17310 bflength = facet; break;
17311 case XML_SCHEMA_FACET_MINLENGTH:
17312 bfminlen = facet; break;
17313 case XML_SCHEMA_FACET_MININCLUSIVE:
17314 bfmininc = facet; break;
17315 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17316 bfminexc = facet; break;
17317 case XML_SCHEMA_FACET_MAXLENGTH:
17318 bfmaxlen = facet; break;
17319 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17320 bfmaxinc = facet; break;
17321 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17322 bfmaxexc = facet; break;
17323 case XML_SCHEMA_FACET_TOTALDIGITS:
17324 bftotdig = facet; break;
17325 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17326 bffracdig = facet; break;
17327 default:
17328 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017329 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017330 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017331 /*
17332 * length and minLength or maxLength (2.2) + (3.2)
17333 */
17334 if (flength && (fminlen || fmaxlen)) {
17335 FACET_RESTR_ERR(flength, "It is an error for both 'length' and "
17336 "either of 'minLength' or 'maxLength' to be specified on "
17337 "the same type definition")
17338 }
17339 /*
17340 * Mutual exclusions in the same derivation step.
17341 */
17342 if ((fmaxinc) && (fmaxexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017343 /*
17344 * SCC "maxInclusive and maxExclusive"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017345 */
17346 FACET_RESTR_MUTUAL_ERR(fmaxinc, fmaxexc)
17347 }
17348 if ((fmininc) && (fminexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017349 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017350 * SCC "minInclusive and minExclusive"
17351 */
17352 FACET_RESTR_MUTUAL_ERR(fmininc, fminexc)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017353 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017354
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017355 if (flength && bflength) {
17356 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017357 * SCC "length valid restriction"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017358 * The values have to be equal.
17359 */
17360 res = xmlSchemaCompareValues(flength->val, bflength->val);
17361 if (res == -2)
17362 goto internal_error;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017363 if (res != 0)
17364 xmlSchemaDeriveFacetErr(pctxt, flength, bflength, 0, 0, 1);
17365 if ((res != 0) && (bflength->fixed)) {
17366 FACET_RESTR_FIXED_ERR(flength)
17367 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017368
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017369 }
17370 if (fminlen && bfminlen) {
17371 /*
17372 * SCC "minLength valid restriction"
17373 * minLength >= BASE minLength
17374 */
17375 res = xmlSchemaCompareValues(fminlen->val, bfminlen->val);
17376 if (res == -2)
17377 goto internal_error;
17378 if (res == -1)
17379 xmlSchemaDeriveFacetErr(pctxt, fminlen, bfminlen, 1, 1, 1);
17380 if ((res != 0) && (bfminlen->fixed)) {
17381 FACET_RESTR_FIXED_ERR(fminlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017382 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017383 }
17384 if (fmaxlen && bfmaxlen) {
17385 /*
17386 * SCC "maxLength valid restriction"
17387 * maxLength <= BASE minLength
17388 */
17389 res = xmlSchemaCompareValues(fmaxlen->val, bfmaxlen->val);
17390 if (res == -2)
17391 goto internal_error;
17392 if (res == 1)
17393 xmlSchemaDeriveFacetErr(pctxt, fmaxlen, bfmaxlen, -1, 1, 1);
17394 if ((res != 0) && (bfmaxlen->fixed)) {
17395 FACET_RESTR_FIXED_ERR(fmaxlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017396 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017397 }
17398 /*
17399 * SCC "length and minLength or maxLength"
17400 */
17401 if (! flength)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017402 flength = bflength;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017403 if (flength) {
17404 if (! fminlen)
Miroslav Bajtos8f58f892009-08-07 17:22:12 +020017405 fminlen = bfminlen;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017406 if (fminlen) {
17407 /* (1.1) length >= minLength */
17408 res = xmlSchemaCompareValues(flength->val, fminlen->val);
17409 if (res == -2)
17410 goto internal_error;
17411 if (res == -1)
17412 xmlSchemaDeriveFacetErr(pctxt, flength, fminlen, 1, 1, 0);
17413 }
17414 if (! fmaxlen)
17415 fmaxlen = bfmaxlen;
17416 if (fmaxlen) {
17417 /* (2.1) length <= maxLength */
17418 res = xmlSchemaCompareValues(flength->val, fmaxlen->val);
17419 if (res == -2)
17420 goto internal_error;
17421 if (res == 1)
17422 xmlSchemaDeriveFacetErr(pctxt, flength, fmaxlen, -1, 1, 0);
17423 }
17424 }
17425 if (fmaxinc) {
17426 /*
17427 * "maxInclusive"
17428 */
17429 if (fmininc) {
17430 /* SCC "maxInclusive >= minInclusive" */
17431 res = xmlSchemaCompareValues(fmaxinc->val, fmininc->val);
17432 if (res == -2)
17433 goto internal_error;
17434 if (res == -1) {
17435 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, fmininc, 1, 1, 0);
17436 }
17437 }
17438 /*
17439 * SCC "maxInclusive valid restriction"
17440 */
17441 if (bfmaxinc) {
17442 /* maxInclusive <= BASE maxInclusive */
17443 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxinc->val);
17444 if (res == -2)
17445 goto internal_error;
17446 if (res == 1)
17447 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxinc, -1, 1, 1);
17448 if ((res != 0) && (bfmaxinc->fixed)) {
17449 FACET_RESTR_FIXED_ERR(fmaxinc)
17450 }
17451 }
17452 if (bfmaxexc) {
17453 /* maxInclusive < BASE maxExclusive */
17454 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxexc->val);
17455 if (res == -2)
17456 goto internal_error;
17457 if (res != -1) {
17458 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxexc, -1, 0, 1);
17459 }
17460 }
17461 if (bfmininc) {
17462 /* maxInclusive >= BASE minInclusive */
17463 res = xmlSchemaCompareValues(fmaxinc->val, bfmininc->val);
17464 if (res == -2)
17465 goto internal_error;
17466 if (res == -1) {
17467 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmininc, 1, 1, 1);
17468 }
17469 }
17470 if (bfminexc) {
17471 /* maxInclusive > BASE minExclusive */
17472 res = xmlSchemaCompareValues(fmaxinc->val, bfminexc->val);
17473 if (res == -2)
17474 goto internal_error;
17475 if (res != 1) {
17476 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfminexc, 1, 0, 1);
17477 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017478 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017479 }
17480 if (fmaxexc) {
17481 /*
17482 * "maxExclusive >= minExclusive"
17483 */
17484 if (fminexc) {
17485 res = xmlSchemaCompareValues(fmaxexc->val, fminexc->val);
17486 if (res == -2)
17487 goto internal_error;
17488 if (res == -1) {
17489 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, fminexc, 1, 1, 0);
17490 }
17491 }
17492 /*
17493 * "maxExclusive valid restriction"
17494 */
17495 if (bfmaxexc) {
17496 /* maxExclusive <= BASE maxExclusive */
17497 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxexc->val);
17498 if (res == -2)
17499 goto internal_error;
17500 if (res == 1) {
17501 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxexc, -1, 1, 1);
17502 }
17503 if ((res != 0) && (bfmaxexc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017504 FACET_RESTR_FIXED_ERR(fmaxexc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017505 }
17506 }
17507 if (bfmaxinc) {
17508 /* maxExclusive <= BASE maxInclusive */
17509 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxinc->val);
17510 if (res == -2)
17511 goto internal_error;
17512 if (res == 1) {
17513 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxinc, -1, 1, 1);
17514 }
17515 }
17516 if (bfmininc) {
17517 /* maxExclusive > BASE minInclusive */
17518 res = xmlSchemaCompareValues(fmaxexc->val, bfmininc->val);
17519 if (res == -2)
17520 goto internal_error;
17521 if (res != 1) {
17522 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmininc, 1, 0, 1);
17523 }
17524 }
17525 if (bfminexc) {
17526 /* maxExclusive > BASE minExclusive */
17527 res = xmlSchemaCompareValues(fmaxexc->val, bfminexc->val);
17528 if (res == -2)
17529 goto internal_error;
17530 if (res != 1) {
17531 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfminexc, 1, 0, 1);
17532 }
17533 }
17534 }
17535 if (fminexc) {
17536 /*
17537 * "minExclusive < maxInclusive"
17538 */
17539 if (fmaxinc) {
17540 res = xmlSchemaCompareValues(fminexc->val, fmaxinc->val);
17541 if (res == -2)
17542 goto internal_error;
17543 if (res != -1) {
17544 xmlSchemaDeriveFacetErr(pctxt, fminexc, fmaxinc, -1, 0, 0);
17545 }
17546 }
17547 /*
17548 * "minExclusive valid restriction"
17549 */
17550 if (bfminexc) {
17551 /* minExclusive >= BASE minExclusive */
17552 res = xmlSchemaCompareValues(fminexc->val, bfminexc->val);
17553 if (res == -2)
17554 goto internal_error;
17555 if (res == -1) {
17556 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfminexc, 1, 1, 1);
17557 }
17558 if ((res != 0) && (bfminexc->fixed)) {
17559 FACET_RESTR_FIXED_ERR(fminexc)
17560 }
17561 }
17562 if (bfmaxinc) {
17563 /* minExclusive <= BASE maxInclusive */
17564 res = xmlSchemaCompareValues(fminexc->val, bfmaxinc->val);
17565 if (res == -2)
17566 goto internal_error;
17567 if (res == 1) {
17568 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxinc, -1, 1, 1);
17569 }
17570 }
17571 if (bfmininc) {
17572 /* minExclusive >= BASE minInclusive */
17573 res = xmlSchemaCompareValues(fminexc->val, bfmininc->val);
17574 if (res == -2)
17575 goto internal_error;
17576 if (res == -1) {
17577 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmininc, 1, 1, 1);
17578 }
17579 }
17580 if (bfmaxexc) {
17581 /* minExclusive < BASE maxExclusive */
17582 res = xmlSchemaCompareValues(fminexc->val, bfmaxexc->val);
17583 if (res == -2)
17584 goto internal_error;
17585 if (res != -1) {
17586 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxexc, -1, 0, 1);
17587 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017588 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017589 }
17590 if (fmininc) {
17591 /*
17592 * "minInclusive < maxExclusive"
17593 */
17594 if (fmaxexc) {
17595 res = xmlSchemaCompareValues(fmininc->val, fmaxexc->val);
17596 if (res == -2)
17597 goto internal_error;
17598 if (res != -1) {
17599 xmlSchemaDeriveFacetErr(pctxt, fmininc, fmaxexc, -1, 0, 0);
17600 }
17601 }
17602 /*
17603 * "minExclusive valid restriction"
17604 */
17605 if (bfmininc) {
17606 /* minInclusive >= BASE minInclusive */
17607 res = xmlSchemaCompareValues(fmininc->val, bfmininc->val);
17608 if (res == -2)
17609 goto internal_error;
17610 if (res == -1) {
17611 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmininc, 1, 1, 1);
17612 }
17613 if ((res != 0) && (bfmininc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017614 FACET_RESTR_FIXED_ERR(fmininc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017615 }
17616 }
17617 if (bfmaxinc) {
17618 /* minInclusive <= BASE maxInclusive */
17619 res = xmlSchemaCompareValues(fmininc->val, bfmaxinc->val);
17620 if (res == -2)
17621 goto internal_error;
Daniel Veillard0a119eb2005-07-20 13:46:00 +000017622 if (res == 1) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017623 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxinc, -1, 1, 1);
17624 }
17625 }
17626 if (bfminexc) {
17627 /* minInclusive > BASE minExclusive */
17628 res = xmlSchemaCompareValues(fmininc->val, bfminexc->val);
17629 if (res == -2)
17630 goto internal_error;
17631 if (res != 1)
17632 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfminexc, 1, 0, 1);
17633 }
17634 if (bfmaxexc) {
17635 /* minInclusive < BASE maxExclusive */
17636 res = xmlSchemaCompareValues(fmininc->val, bfmaxexc->val);
17637 if (res == -2)
17638 goto internal_error;
17639 if (res != -1)
17640 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxexc, -1, 0, 1);
17641 }
17642 }
17643 if (ftotdig && bftotdig) {
17644 /*
17645 * SCC " totalDigits valid restriction"
17646 * totalDigits <= BASE totalDigits
17647 */
17648 res = xmlSchemaCompareValues(ftotdig->val, bftotdig->val);
17649 if (res == -2)
17650 goto internal_error;
17651 if (res == 1)
17652 xmlSchemaDeriveFacetErr(pctxt, ftotdig, bftotdig,
17653 -1, 1, 1);
17654 if ((res != 0) && (bftotdig->fixed)) {
17655 FACET_RESTR_FIXED_ERR(ftotdig)
17656 }
17657 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017658 if (ffracdig && bffracdig) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017659 /*
17660 * SCC "fractionDigits valid restriction"
17661 * fractionDigits <= BASE fractionDigits
17662 */
17663 res = xmlSchemaCompareValues(ffracdig->val, bffracdig->val);
17664 if (res == -2)
17665 goto internal_error;
17666 if (res == 1)
17667 xmlSchemaDeriveFacetErr(pctxt, ffracdig, bffracdig,
17668 -1, 1, 1);
17669 if ((res != 0) && (bffracdig->fixed)) {
17670 FACET_RESTR_FIXED_ERR(ffracdig)
17671 }
17672 }
17673 /*
17674 * SCC "fractionDigits less than or equal to totalDigits"
17675 */
17676 if (! ftotdig)
17677 ftotdig = bftotdig;
17678 if (! ffracdig)
17679 ffracdig = bffracdig;
17680 if (ftotdig && ffracdig) {
17681 res = xmlSchemaCompareValues(ffracdig->val, ftotdig->val);
17682 if (res == -2)
17683 goto internal_error;
17684 if (res == 1)
17685 xmlSchemaDeriveFacetErr(pctxt, ffracdig, ftotdig,
17686 -1, 1, 0);
17687 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017688 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017689 * *Enumerations* won' be added here, since only the first set
17690 * of enumerations in the ancestor-or-self axis is used
17691 * for validation, plus we need to use the base type of those
17692 * enumerations for whitespace.
17693 *
17694 * *Patterns*: won't be add here, since they are ORed at
17695 * type level and ANDed at ancestor level. This will
17696 * happed during validation by walking the base axis
17697 * of the type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017698 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017699 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17700 bfacet = cur->facet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017701 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017702 * Special handling of enumerations and patterns.
17703 * TODO: hmm, they should not appear in the set, so remove this.
17704 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017705 if ((bfacet->type == XML_SCHEMA_FACET_PATTERN) ||
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017706 (bfacet->type == XML_SCHEMA_FACET_ENUMERATION))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017707 continue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017708 /*
17709 * Search for a duplicate facet in the current type.
17710 */
17711 link = type->facetSet;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017712 /* err = 0; */
17713 /* fixedErr = 0; */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017714 while (link != NULL) {
17715 facet = link->facet;
17716 if (facet->type == bfacet->type) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017717 switch (facet->type) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017718 case XML_SCHEMA_FACET_WHITESPACE:
17719 /*
17720 * The whitespace must be stronger.
17721 */
17722 if (facet->whitespace < bfacet->whitespace) {
Rob Richardsc6947bb2008-06-29 15:04:41 +000017723 FACET_RESTR_ERR(facet,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017724 "The 'whitespace' value has to be equal to "
17725 "or stronger than the 'whitespace' value of "
17726 "the base type")
17727 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017728 if ((bfacet->fixed) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017729 (facet->whitespace != bfacet->whitespace)) {
17730 FACET_RESTR_FIXED_ERR(facet)
17731 }
17732 break;
17733 default:
17734 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017735 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017736 /* Duplicate found. */
17737 break;
17738 }
17739 link = link->next;
17740 }
17741 /*
17742 * If no duplicate was found: add the base types's facet
17743 * to the set.
17744 */
17745 if (link == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017746 link = (xmlSchemaFacetLinkPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017747 xmlMalloc(sizeof(xmlSchemaFacetLink));
17748 if (link == NULL) {
17749 xmlSchemaPErrMemory(pctxt,
17750 "deriving facets, creating a facet link", NULL);
17751 return (-1);
17752 }
17753 link->facet = cur->facet;
17754 link->next = NULL;
17755 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017756 type->facetSet = link;
17757 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017758 last->next = link;
17759 last = link;
17760 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017761
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017762 }
17763
17764 return (0);
17765internal_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017766 PERROR_INT("xmlSchemaDeriveAndValidateFacets",
17767 "an error occured");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017768 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017769}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017770
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017771static int
17772xmlSchemaFinishMemberTypeDefinitionsProperty(xmlSchemaParserCtxtPtr pctxt,
17773 xmlSchemaTypePtr type)
17774{
17775 xmlSchemaTypeLinkPtr link, lastLink, prevLink, subLink, newLink;
17776 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017777 * The actual value is then formed by replacing any union type
Jan Pokorný761c9e92013-11-29 23:26:27 +010017778 * definition in the `explicit members` with the members of their
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017779 * {member type definitions}, in order.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017780 *
17781 * TODO: There's a bug entry at
17782 * "http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0287.html"
17783 * which indicates that we'll keep the union types the future.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017784 */
17785 link = type->memberTypes;
17786 while (link != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017787
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017788 if (WXS_IS_TYPE_NOT_FIXED(link->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000017789 xmlSchemaTypeFixup(link->type, ACTXT_CAST pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017790
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017791 if (WXS_IS_UNION(link->type)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017792 subLink = xmlSchemaGetUnionSimpleTypeMemberTypes(link->type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017793 if (subLink != NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017794 link->type = subLink->type;
17795 if (subLink->next != NULL) {
17796 lastLink = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017797 subLink = subLink->next;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017798 prevLink = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017799 while (subLink != NULL) {
17800 newLink = (xmlSchemaTypeLinkPtr)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017801 xmlMalloc(sizeof(xmlSchemaTypeLink));
17802 if (newLink == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017803 xmlSchemaPErrMemory(pctxt, "allocating a type link",
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017804 NULL);
17805 return (-1);
17806 }
17807 newLink->type = subLink->type;
17808 prevLink->next = newLink;
17809 prevLink = newLink;
17810 newLink->next = lastLink;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017811
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017812 subLink = subLink->next;
17813 }
17814 }
17815 }
17816 }
17817 link = link->next;
17818 }
17819 return (0);
17820}
17821
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017822static void
17823xmlSchemaTypeFixupOptimFacets(xmlSchemaTypePtr type)
Daniel Veillarddee23482008-04-11 12:58:43 +000017824{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017825 int has = 0, needVal = 0, normVal = 0;
17826
17827 has = (type->baseType->flags & XML_SCHEMAS_TYPE_HAS_FACETS) ? 1 : 0;
17828 if (has) {
17829 needVal = (type->baseType->flags &
17830 XML_SCHEMAS_TYPE_FACETSNEEDVALUE) ? 1 : 0;
17831 normVal = (type->baseType->flags &
17832 XML_SCHEMAS_TYPE_NORMVALUENEEDED) ? 1 : 0;
17833 }
17834 if (type->facets != NULL) {
17835 xmlSchemaFacetPtr fac;
Daniel Veillarddee23482008-04-11 12:58:43 +000017836
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017837 for (fac = type->facets; fac != NULL; fac = fac->next) {
17838 switch (fac->type) {
17839 case XML_SCHEMA_FACET_WHITESPACE:
17840 break;
17841 case XML_SCHEMA_FACET_PATTERN:
17842 normVal = 1;
17843 has = 1;
17844 break;
17845 case XML_SCHEMA_FACET_ENUMERATION:
17846 needVal = 1;
17847 normVal = 1;
17848 has = 1;
17849 break;
17850 default:
17851 has = 1;
17852 break;
17853 }
Daniel Veillarddee23482008-04-11 12:58:43 +000017854 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017855 }
17856 if (normVal)
17857 type->flags |= XML_SCHEMAS_TYPE_NORMVALUENEEDED;
17858 if (needVal)
17859 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17860 if (has)
17861 type->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
17862
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017863 if (has && (! needVal) && WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017864 xmlSchemaTypePtr prim = xmlSchemaGetPrimitiveType(type);
17865 /*
17866 * OPTIMIZE VAL TODO: Some facets need a computed value.
17867 */
17868 if ((prim->builtInType != XML_SCHEMAS_ANYSIMPLETYPE) &&
17869 (prim->builtInType != XML_SCHEMAS_STRING)) {
17870 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
Daniel Veillarddee23482008-04-11 12:58:43 +000017871 }
17872 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017873}
17874
17875static int
17876xmlSchemaTypeFixupWhitespace(xmlSchemaTypePtr type)
17877{
Daniel Veillarddee23482008-04-11 12:58:43 +000017878
17879
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017880 /*
17881 * Evaluate the whitespace-facet value.
Daniel Veillarddee23482008-04-11 12:58:43 +000017882 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017883 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017884 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17885 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017886 } else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017887 return (0);
Daniel Veillarddee23482008-04-11 12:58:43 +000017888
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017889 if (type->facetSet != NULL) {
17890 xmlSchemaFacetLinkPtr lin;
17891
17892 for (lin = type->facetSet; lin != NULL; lin = lin->next) {
17893 if (lin->facet->type == XML_SCHEMA_FACET_WHITESPACE) {
17894 switch (lin->facet->whitespace) {
17895 case XML_SCHEMAS_FACET_PRESERVE:
17896 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17897 break;
17898 case XML_SCHEMAS_FACET_REPLACE:
17899 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17900 break;
17901 case XML_SCHEMAS_FACET_COLLAPSE:
17902 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17903 break;
17904 default:
17905 return (-1);
17906 }
17907 return (0);
17908 }
17909 }
17910 }
17911 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010017912 * For all `atomic` datatypes other than string (and types `derived`
17913 * by `restriction` from it) the value of whiteSpace is fixed to
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017914 * collapse
17915 */
17916 {
17917 xmlSchemaTypePtr anc;
17918
Daniel Veillarddee23482008-04-11 12:58:43 +000017919 for (anc = type->baseType; anc != NULL &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017920 anc->builtInType != XML_SCHEMAS_ANYTYPE;
17921 anc = anc->baseType) {
17922
17923 if (anc->type == XML_SCHEMA_TYPE_BASIC) {
Daniel Veillarddee23482008-04-11 12:58:43 +000017924 if (anc->builtInType == XML_SCHEMAS_NORMSTRING) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017925 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17926
17927 } else if ((anc->builtInType == XML_SCHEMAS_STRING) ||
Daniel Veillarddee23482008-04-11 12:58:43 +000017928 (anc->builtInType == XML_SCHEMAS_ANYSIMPLETYPE)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017929 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17930
17931 } else
17932 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17933 break;
17934 }
17935 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017936 }
17937 return (0);
17938}
17939
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017940static int
17941xmlSchemaFixupSimpleTypeStageOne(xmlSchemaParserCtxtPtr pctxt,
17942 xmlSchemaTypePtr type)
Daniel Veillard4255d502002-04-16 15:50:10 +000017943{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017944 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17945 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017946 if (! WXS_IS_TYPE_NOT_FIXED_1(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017947 return(0);
17948 type->flags |= XML_SCHEMAS_TYPE_FIXUP_1;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017949
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017950 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017951 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017952 * Corresponds to <simpleType><list>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017953 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017954 if (type->subtypes == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017955 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017956 * This one is really needed, so get out.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017957 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017958 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017959 "list type has no item-type assigned");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017960 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017961 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017962 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017963 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017964 * Corresponds to <simpleType><union>...
Daniel Veillarddee23482008-04-11 12:58:43 +000017965 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017966 if (type->memberTypes == NULL) {
17967 /*
17968 * This one is really needed, so get out.
17969 */
17970 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17971 "union type has no member-types assigned");
17972 return(-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000017973 }
17974 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017975 /*
17976 * Corresponds to <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017977 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017978 if (type->baseType == NULL) {
17979 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17980 "type has no base-type assigned");
17981 return(-1);
17982 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017983 if (WXS_IS_TYPE_NOT_FIXED_1(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017984 if (xmlSchemaFixupSimpleTypeStageOne(pctxt, type->baseType) == -1)
17985 return(-1);
17986 /*
17987 * Variety
17988 * If the <restriction> alternative is chosen, then the
17989 * {variety} of the {base type definition}.
17990 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017991 if (WXS_IS_ATOMIC(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017992 type->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017993 else if (WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017994 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017995 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017996 * Inherit the itemType.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017997 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017998 type->subtypes = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017999 } else if (WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018000 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018001 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018002 * NOTE that we won't assign the memberTypes of the base,
18003 * since this will make trouble when freeing them; we will
18004 * use a lookup function to access them instead.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018005 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018006 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018007 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018008 return(0);
18009}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018010
Daniel Veillard8651f532002-04-17 09:06:27 +000018011#ifdef DEBUG_TYPE
Daniel Veillard67952602006-01-05 15:29:44 +000018012static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018013xmlSchemaDebugFixedType(xmlSchemaParserCtxtPtr pctxt,
18014 xmlSchemaTypePtr type)
18015{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018016 if (type->node != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018017 xmlGenericError(xmlGenericErrorContext,
18018 "Type of %s : %s:%d :", name,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018019 type->node->doc->URL,
18020 xmlGetLineNo(type->node));
Daniel Veillarddecd64d2002-04-18 14:41:51 +000018021 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018022 xmlGenericError(xmlGenericErrorContext, "Type of %s :", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000018023 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018024 if ((WXS_IS_SIMPLE(type)) || (WXS_IS_COMPLEX(type))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018025 switch (type->contentType) {
18026 case XML_SCHEMA_CONTENT_SIMPLE:
18027 xmlGenericError(xmlGenericErrorContext, "simple\n");
18028 break;
18029 case XML_SCHEMA_CONTENT_ELEMENTS:
18030 xmlGenericError(xmlGenericErrorContext, "elements\n");
18031 break;
18032 case XML_SCHEMA_CONTENT_UNKNOWN:
18033 xmlGenericError(xmlGenericErrorContext, "unknown !!!\n");
18034 break;
18035 case XML_SCHEMA_CONTENT_EMPTY:
18036 xmlGenericError(xmlGenericErrorContext, "empty\n");
18037 break;
18038 case XML_SCHEMA_CONTENT_MIXED:
18039 if (xmlSchemaIsParticleEmptiable((xmlSchemaParticlePtr)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018040 type->subtypes))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018041 xmlGenericError(xmlGenericErrorContext,
18042 "mixed as emptiable particle\n");
18043 else
18044 xmlGenericError(xmlGenericErrorContext, "mixed\n");
18045 break;
18046 /* Removed, since not used. */
18047 /*
18048 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
18049 xmlGenericError(xmlGenericErrorContext, "mixed or elems\n");
18050 break;
18051 */
18052 case XML_SCHEMA_CONTENT_BASIC:
18053 xmlGenericError(xmlGenericErrorContext, "basic\n");
18054 break;
18055 default:
18056 xmlGenericError(xmlGenericErrorContext,
18057 "not registered !!!\n");
18058 break;
18059 }
Daniel Veillard8651f532002-04-17 09:06:27 +000018060 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018061}
Daniel Veillard8651f532002-04-17 09:06:27 +000018062#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018063
18064/*
18065* 3.14.6 Constraints on Simple Type Definition Schema Components
18066*/
18067static int
18068xmlSchemaFixupSimpleTypeStageTwo(xmlSchemaParserCtxtPtr pctxt,
18069 xmlSchemaTypePtr type)
18070{
18071 int res, olderrs = pctxt->nberrors;
18072
18073 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
18074 return(-1);
18075
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018076 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018077 return(0);
18078
18079 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
18080 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
18081
18082 if (type->baseType == NULL) {
18083 PERROR_INT("xmlSchemaFixupSimpleTypeStageTwo",
18084 "missing baseType");
18085 goto exit_failure;
18086 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018087 if (WXS_IS_TYPE_NOT_FIXED(type->baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018088 xmlSchemaTypeFixup(type->baseType, ACTXT_CAST pctxt);
Daniel Veillarddee23482008-04-11 12:58:43 +000018089 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018090 * If a member type of a union is a union itself, we need to substitute
18091 * that member type for its member types.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018092 * NOTE that this might change in WXS 1.1; i.e. we will keep the union
18093 * types in WXS 1.1.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018094 */
18095 if ((type->memberTypes != NULL) &&
18096 (xmlSchemaFinishMemberTypeDefinitionsProperty(pctxt, type) == -1))
Daniel Veillarddee23482008-04-11 12:58:43 +000018097 return(-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018098 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000018099 * SPEC src-simple-type 1
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018100 * "The corresponding simple type definition, if any, must satisfy
18101 * the conditions set out in Constraints on Simple Type Definition
Jan Pokorný761c9e92013-11-29 23:26:27 +010018102 * Schema Components ($3.14.6)."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018103 */
18104 /*
18105 * Schema Component Constraint: Simple Type Definition Properties Correct
18106 * (st-props-correct)
18107 */
18108 res = xmlSchemaCheckSTPropsCorrect(pctxt, type);
18109 HFAILURE HERROR
Daniel Veillarddee23482008-04-11 12:58:43 +000018110 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018111 * Schema Component Constraint: Derivation Valid (Restriction, Simple)
18112 * (cos-st-restricts)
18113 */
18114 res = xmlSchemaCheckCOSSTRestricts(pctxt, type);
18115 HFAILURE HERROR
18116 /*
18117 * TODO: Removed the error report, since it got annoying to get an
18118 * extra error report, if anything failed until now.
18119 * Enable this if needed.
18120 *
18121 * xmlSchemaPErr(ctxt, type->node,
18122 * XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
18123 * "Simple type '%s' does not satisfy the constraints "
18124 * "on simple type definitions.\n",
18125 * type->name, NULL);
18126 */
18127 /*
18128 * Schema Component Constraint: Simple Type Restriction (Facets)
18129 * (st-restrict-facets)
18130 */
18131 res = xmlSchemaCheckFacetValues(type, pctxt);
18132 HFAILURE HERROR
18133 if ((type->facetSet != NULL) ||
18134 (type->baseType->facetSet != NULL)) {
18135 res = xmlSchemaDeriveAndValidateFacets(pctxt, type);
18136 HFAILURE HERROR
18137 }
18138 /*
18139 * Whitespace value.
18140 */
18141 res = xmlSchemaTypeFixupWhitespace(type);
18142 HFAILURE HERROR
Daniel Veillarddee23482008-04-11 12:58:43 +000018143 xmlSchemaTypeFixupOptimFacets(type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018144
18145exit_error:
18146#ifdef DEBUG_TYPE
18147 xmlSchemaDebugFixedType(pctxt, type);
18148#endif
18149 if (olderrs != pctxt->nberrors)
18150 return(pctxt->err);
18151 return(0);
18152
18153exit_failure:
18154#ifdef DEBUG_TYPE
18155 xmlSchemaDebugFixedType(pctxt, type);
18156#endif
18157 return(-1);
18158}
18159
18160static int
18161xmlSchemaFixupComplexType(xmlSchemaParserCtxtPtr pctxt,
18162 xmlSchemaTypePtr type)
18163{
18164 int res = 0, olderrs = pctxt->nberrors;
18165 xmlSchemaTypePtr baseType = type->baseType;
18166
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018167 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018168 return(0);
18169 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
18170 if (baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018171 PERROR_INT("xmlSchemaFixupComplexType",
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018172 "missing baseType");
18173 goto exit_failure;
Daniel Veillarddee23482008-04-11 12:58:43 +000018174 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018175 /*
18176 * Fixup the base type.
18177 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018178 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018179 xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018180 if (baseType->flags & XML_SCHEMAS_TYPE_INTERNAL_INVALID) {
18181 /*
18182 * Skip fixup if the base type is invalid.
18183 * TODO: Generate a warning!
18184 */
18185 return(0);
Daniel Veillarddee23482008-04-11 12:58:43 +000018186 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018187 /*
18188 * This basically checks if the base type can be derived.
18189 */
18190 res = xmlSchemaCheckSRCCT(pctxt, type);
Daniel Veillarddee23482008-04-11 12:58:43 +000018191 HFAILURE HERROR
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018192 /*
18193 * Fixup the content type.
18194 */
18195 if (type->contentType == XML_SCHEMA_CONTENT_SIMPLE) {
18196 /*
18197 * Corresponds to <complexType><simpleContent>...
18198 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018199 if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018200 (baseType->contentTypeDef != NULL) &&
18201 (WXS_IS_RESTRICTION(type))) {
18202 xmlSchemaTypePtr contentBase, content;
18203#ifdef ENABLE_NAMED_LOCALS
18204 char buf[30];
18205 const xmlChar *tmpname;
18206#endif
18207 /*
18208 * SPEC (1) If <restriction> + base type is <complexType>,
18209 * "whose own {content type} is a simple type..."
18210 */
18211 if (type->contentTypeDef != NULL) {
18212 /*
18213 * SPEC (1.1) "the simple type definition corresponding to the
18214 * <simpleType> among the [children] of <restriction> if there
18215 * is one;"
18216 * Note that this "<simpleType> among the [children]" was put
18217 * into ->contentTypeDef during parsing.
18218 */
18219 contentBase = type->contentTypeDef;
18220 type->contentTypeDef = NULL;
18221 } else {
18222 /*
18223 * (1.2) "...otherwise (<restriction> has no <simpleType>
18224 * among its [children]), the simple type definition which
18225 * is the {content type} of the ... base type."
18226 */
18227 contentBase = baseType->contentTypeDef;
18228 }
18229 /*
18230 * SPEC
18231 * "... a simple type definition which restricts the simple
18232 * type definition identified in clause 1.1 or clause 1.2
18233 * with a set of facet components"
18234 *
18235 * Create the anonymous simple type, which will be the content
18236 * type of the complex type.
18237 */
18238#ifdef ENABLE_NAMED_LOCALS
18239 snprintf(buf, 29, "#scST%d", ++(pctxt->counter));
18240 tmpname = xmlDictLookup(pctxt->dict, BAD_CAST buf, -1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018241 content = xmlSchemaAddType(pctxt, pctxt->schema,
18242 XML_SCHEMA_TYPE_SIMPLE, tmpname, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018243 type->node, 0);
18244#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018245 content = xmlSchemaAddType(pctxt, pctxt->schema,
18246 XML_SCHEMA_TYPE_SIMPLE, NULL, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018247 type->node, 0);
18248#endif
18249 if (content == NULL)
18250 goto exit_failure;
18251 /*
18252 * We will use the same node as for the <complexType>
18253 * to have it somehow anchored in the schema doc.
18254 */
18255 content->type = XML_SCHEMA_TYPE_SIMPLE;
18256 content->baseType = contentBase;
18257 /*
18258 * Move the facets, previously anchored on the
18259 * complexType during parsing.
18260 */
18261 content->facets = type->facets;
18262 type->facets = NULL;
18263 content->facetSet = type->facetSet;
18264 type->facetSet = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000018265
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018266 type->contentTypeDef = content;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018267 if (WXS_IS_TYPE_NOT_FIXED(contentBase))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018268 xmlSchemaTypeFixup(contentBase, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018269 /*
18270 * Fixup the newly created type. We don't need to check
18271 * for circularity here.
18272 */
18273 res = xmlSchemaFixupSimpleTypeStageOne(pctxt, content);
Daniel Veillarddee23482008-04-11 12:58:43 +000018274 HFAILURE HERROR
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018275 res = xmlSchemaFixupSimpleTypeStageTwo(pctxt, content);
Daniel Veillarddee23482008-04-11 12:58:43 +000018276 HFAILURE HERROR
18277
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018278 } else if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018279 (baseType->contentType == XML_SCHEMA_CONTENT_MIXED) &&
18280 (WXS_IS_RESTRICTION(type))) {
18281 /*
18282 * SPEC (2) If <restriction> + base is a mixed <complexType> with
18283 * an emptiable particle, then a simple type definition which
18284 * restricts the <restriction>'s <simpleType> child.
18285 */
18286 if ((type->contentTypeDef == NULL) ||
18287 (type->contentTypeDef->baseType == NULL)) {
18288 /*
18289 * TODO: Check if this ever happens.
18290 */
18291 xmlSchemaPCustomErr(pctxt,
18292 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018293 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018294 "Internal error: xmlSchemaTypeFixup, "
18295 "complex type '%s': the <simpleContent><restriction> "
18296 "is missing a <simpleType> child, but was not catched "
18297 "by xmlSchemaCheckSRCCT()", type->name);
18298 goto exit_failure;
18299 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018300 } else if ((WXS_IS_COMPLEX(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018301 /*
18302 * SPEC (3) If <extension> + base is <complexType> with
18303 * <simpleType> content, "...then the {content type} of that
18304 * complex type definition"
18305 */
18306 if (baseType->contentTypeDef == NULL) {
18307 /*
18308 * TODO: Check if this ever happens. xmlSchemaCheckSRCCT
18309 * should have catched this already.
18310 */
18311 xmlSchemaPCustomErr(pctxt,
18312 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018313 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018314 "Internal error: xmlSchemaTypeFixup, "
18315 "complex type '%s': the <extension>ed base type is "
18316 "a complex type with no simple content type",
18317 type->name);
18318 goto exit_failure;
18319 }
18320 type->contentTypeDef = baseType->contentTypeDef;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018321 } else if ((WXS_IS_SIMPLE(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018322 /*
18323 * SPEC (4) <extension> + base is <simpleType>
18324 * "... then that simple type definition"
18325 */
18326 type->contentTypeDef = baseType;
18327 } else {
18328 /*
18329 * TODO: Check if this ever happens.
18330 */
18331 xmlSchemaPCustomErr(pctxt,
18332 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018333 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018334 "Internal error: xmlSchemaTypeFixup, "
18335 "complex type '%s' with <simpleContent>: unhandled "
18336 "derivation case", type->name);
18337 goto exit_failure;
18338 }
18339 } else {
18340 int dummySequence = 0;
18341 xmlSchemaParticlePtr particle =
18342 (xmlSchemaParticlePtr) type->subtypes;
18343 /*
18344 * Corresponds to <complexType><complexContent>...
18345 *
18346 * NOTE that the effective mixed was already set during parsing of
18347 * <complexType> and <complexContent>; its flag value is
18348 * XML_SCHEMAS_TYPE_MIXED.
18349 *
18350 * Compute the "effective content":
18351 * (2.1.1) + (2.1.2) + (2.1.3)
18352 */
18353 if ((particle == NULL) ||
18354 ((particle->type == XML_SCHEMA_TYPE_PARTICLE) &&
18355 ((particle->children->type == XML_SCHEMA_TYPE_ALL) ||
18356 (particle->children->type == XML_SCHEMA_TYPE_SEQUENCE) ||
18357 ((particle->children->type == XML_SCHEMA_TYPE_CHOICE) &&
18358 (particle->minOccurs == 0))) &&
18359 ( ((xmlSchemaTreeItemPtr) particle->children)->children == NULL))) {
18360 if (type->flags & XML_SCHEMAS_TYPE_MIXED) {
18361 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010018362 * SPEC (2.1.4) "If the `effective mixed` is true, then
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018363 * a particle whose properties are as follows:..."
18364 *
18365 * Empty sequence model group with
18366 * minOccurs/maxOccurs = 1 (i.e. a "particle emptiable").
18367 * NOTE that we sill assign it the <complexType> node to
18368 * somehow anchor it in the doc.
18369 */
18370 if ((particle == NULL) ||
18371 (particle->children->type != XML_SCHEMA_TYPE_SEQUENCE)) {
18372 /*
18373 * Create the particle.
18374 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000018375 particle = xmlSchemaAddParticle(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018376 type->node, 1, 1);
18377 if (particle == NULL)
18378 goto exit_failure;
18379 /*
18380 * Create the model group.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018381 */ /* URGENT TODO: avoid adding to pending items. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018382 particle->children = (xmlSchemaTreeItemPtr)
18383 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18384 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18385 if (particle->children == NULL)
18386 goto exit_failure;
Daniel Veillarddee23482008-04-11 12:58:43 +000018387
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018388 type->subtypes = (xmlSchemaTypePtr) particle;
18389 }
18390 dummySequence = 1;
18391 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18392 } else {
18393 /*
18394 * SPEC (2.1.5) "otherwise empty"
18395 */
18396 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
18397 }
18398 } else {
18399 /*
18400 * SPEC (2.2) "otherwise the particle corresponding to the
18401 * <all>, <choice>, <group> or <sequence> among the
18402 * [children]."
18403 */
18404 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18405 }
18406 /*
18407 * Compute the "content type".
18408 */
18409 if (WXS_IS_RESTRICTION(type)) {
18410 /*
18411 * SPEC (3.1) "If <restriction>..."
18412 * (3.1.1) + (3.1.2) */
18413 if (type->contentType != XML_SCHEMA_CONTENT_EMPTY) {
18414 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18415 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18416 }
18417 } else {
18418 /*
18419 * SPEC (3.2) "If <extension>..."
18420 */
18421 if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18422 /*
18423 * SPEC (3.2.1)
Jan Pokorný761c9e92013-11-29 23:26:27 +010018424 * "If the `effective content` is empty, then the
Kasimier T. Buchcikf896d442006-07-12 15:18:08 +000018425 * {content type} of the [...] base ..."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018426 */
18427 type->contentType = baseType->contentType;
18428 type->subtypes = baseType->subtypes;
18429 /*
Kasimier T. Buchcikf896d442006-07-12 15:18:08 +000018430 * Fixes bug #347316:
18431 * This is the case when the base type has a simple
18432 * type definition as content.
18433 */
18434 type->contentTypeDef = baseType->contentTypeDef;
18435 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018436 * NOTE that the effective mixed is ignored here.
18437 */
18438 } else if (baseType->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18439 /*
18440 * SPEC (3.2.2)
18441 */
18442 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18443 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18444 } else {
18445 /*
18446 * SPEC (3.2.3)
18447 */
18448 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18449 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18450 /*
18451 * "A model group whose {compositor} is sequence and whose
18452 * {particles} are..."
18453 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018454 if ((WXS_TYPE_PARTICLE(type) != NULL) &&
18455 (WXS_TYPE_PARTICLE_TERM(type) != NULL) &&
18456 ((WXS_TYPE_PARTICLE_TERM(type))->type ==
18457 XML_SCHEMA_TYPE_ALL))
18458 {
18459 /*
18460 * SPEC cos-all-limited (1)
18461 */
18462 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18463 /* TODO: error code */
18464 XML_SCHEMAP_COS_ALL_LIMITED,
18465 WXS_ITEM_NODE(type), NULL,
18466 "The type has an 'all' model group in its "
18467 "{content type} and thus cannot be derived from "
18468 "a non-empty type, since this would produce a "
18469 "'sequence' model group containing the 'all' "
18470 "model group; 'all' model groups are not "
18471 "allowed to appear inside other model groups",
18472 NULL, NULL);
18473
18474 } else if ((WXS_TYPE_PARTICLE(baseType) != NULL) &&
18475 (WXS_TYPE_PARTICLE_TERM(baseType) != NULL) &&
18476 ((WXS_TYPE_PARTICLE_TERM(baseType))->type ==
18477 XML_SCHEMA_TYPE_ALL))
18478 {
18479 /*
18480 * SPEC cos-all-limited (1)
18481 */
18482 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18483 /* TODO: error code */
18484 XML_SCHEMAP_COS_ALL_LIMITED,
18485 WXS_ITEM_NODE(type), NULL,
18486 "A type cannot be derived by extension from a type "
18487 "which has an 'all' model group in its "
18488 "{content type}, since this would produce a "
18489 "'sequence' model group containing the 'all' "
18490 "model group; 'all' model groups are not "
18491 "allowed to appear inside other model groups",
18492 NULL, NULL);
18493
18494 } else if (! dummySequence) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018495 xmlSchemaTreeItemPtr effectiveContent =
18496 (xmlSchemaTreeItemPtr) type->subtypes;
18497 /*
18498 * Create the particle.
18499 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000018500 particle = xmlSchemaAddParticle(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018501 type->node, 1, 1);
18502 if (particle == NULL)
18503 goto exit_failure;
18504 /*
18505 * Create the "sequence" model group.
18506 */
18507 particle->children = (xmlSchemaTreeItemPtr)
18508 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18509 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18510 if (particle->children == NULL)
18511 goto exit_failure;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018512 WXS_TYPE_CONTENTTYPE(type) = (xmlSchemaTypePtr) particle;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018513 /*
18514 * SPEC "the particle of the {content type} of
18515 * the ... base ..."
18516 * Create a duplicate of the base type's particle
18517 * and assign its "term" to it.
18518 */
18519 particle->children->children =
18520 (xmlSchemaTreeItemPtr) xmlSchemaAddParticle(pctxt,
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000018521 type->node,
Remi Gacogne4609e6c2012-05-11 15:31:05 +080018522 ((xmlSchemaParticlePtr) baseType->subtypes)->minOccurs,
18523 ((xmlSchemaParticlePtr) baseType->subtypes)->maxOccurs);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018524 if (particle->children->children == NULL)
18525 goto exit_failure;
18526 particle = (xmlSchemaParticlePtr)
18527 particle->children->children;
18528 particle->children =
18529 ((xmlSchemaParticlePtr) baseType->subtypes)->children;
18530 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010018531 * SPEC "followed by the `effective content`."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018532 */
18533 particle->next = effectiveContent;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018534 /*
18535 * This all will result in:
18536 * new-particle
18537 * --> new-sequence(
18538 * new-particle
18539 * --> base-model,
18540 * this-particle
18541 * --> this-model
18542 * )
18543 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018544 } else {
18545 /*
18546 * This is the case when there is already an empty
18547 * <sequence> with minOccurs==maxOccurs==1.
18548 * Just add the base types's content type.
18549 * NOTE that, although we miss to add an intermediate
18550 * <sequence>, this should produce no difference to
18551 * neither the regex compilation of the content model,
18552 * nor to the complex type contraints.
18553 */
18554 particle->children->children =
18555 (xmlSchemaTreeItemPtr) baseType->subtypes;
18556 }
18557 }
18558 }
18559 }
18560 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018561 * Now fixup attribute uses:
18562 * - expand attr. group references
18563 * - intersect attribute wildcards
18564 * - inherit attribute uses of the base type
18565 * - inherit or union attr. wildcards if extending
18566 * - apply attr. use prohibitions if restricting
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018567 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018568 res = xmlSchemaFixupTypeAttributeUses(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018569 HFAILURE HERROR
18570 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018571 * Apply the complex type component constraints; this will not
18572 * check attributes, since this is done in
18573 * xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018574 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018575 res = xmlSchemaCheckCTComponent(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018576 HFAILURE HERROR
18577
18578#ifdef DEBUG_TYPE
18579 xmlSchemaDebugFixedType(pctxt, type);
18580#endif
18581 if (olderrs != pctxt->nberrors)
18582 return(pctxt->err);
18583 else
18584 return(0);
18585
18586exit_error:
18587 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18588#ifdef DEBUG_TYPE
18589 xmlSchemaDebugFixedType(pctxt, type);
18590#endif
18591 return(pctxt->err);
18592
18593exit_failure:
18594 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18595#ifdef DEBUG_TYPE
18596 xmlSchemaDebugFixedType(pctxt, type);
18597#endif
18598 return(-1);
18599}
18600
18601
18602/**
18603 * xmlSchemaTypeFixup:
18604 * @typeDecl: the schema type definition
18605 * @ctxt: the schema parser context
18606 *
18607 * Fixes the content model of the type.
18608 * URGENT TODO: We need an int result!
18609 */
18610static int
18611xmlSchemaTypeFixup(xmlSchemaTypePtr type,
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018612 xmlSchemaAbstractCtxtPtr actxt)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018613{
18614 if (type == NULL)
18615 return(0);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018616 if (actxt->type != XML_SCHEMA_CTXT_PARSER) {
18617 AERROR_INT("xmlSchemaTypeFixup",
18618 "this function needs a parser context");
18619 return(-1);
18620 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018621 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018622 return(0);
18623 if (type->type == XML_SCHEMA_TYPE_COMPLEX)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018624 return(xmlSchemaFixupComplexType(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018625 else if (type->type == XML_SCHEMA_TYPE_SIMPLE)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018626 return(xmlSchemaFixupSimpleTypeStageTwo(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018627 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000018628}
18629
18630/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018631 * xmlSchemaCheckFacet:
18632 * @facet: the facet
18633 * @typeDecl: the schema type definition
Daniel Veillard81562d22005-06-15 13:27:56 +000018634 * @pctxt: the schema parser context or NULL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018635 * @name: the optional name of the type
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018636 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018637 * Checks and computes the values of facets.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018638 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018639 * Returns 0 if valid, a positive error code if not valid and
18640 * -1 in case of an internal or API error.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018641 */
18642int
18643xmlSchemaCheckFacet(xmlSchemaFacetPtr facet,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018644 xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018645 xmlSchemaParserCtxtPtr pctxt,
18646 const xmlChar * name ATTRIBUTE_UNUSED)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018647{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018648 int ret = 0, ctxtGiven;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018649
Daniel Veillardce682bc2004-11-05 17:22:25 +000018650 if ((facet == NULL) || (typeDecl == NULL))
18651 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018652 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018653 * TODO: will the parser context be given if used from
18654 * the relaxNG module?
18655 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018656 if (pctxt == NULL)
18657 ctxtGiven = 0;
18658 else
18659 ctxtGiven = 1;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018660
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018661 switch (facet->type) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018662 case XML_SCHEMA_FACET_MININCLUSIVE:
18663 case XML_SCHEMA_FACET_MINEXCLUSIVE:
18664 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018665 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
18666 case XML_SCHEMA_FACET_ENUMERATION: {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018667 /*
18668 * Okay we need to validate the value
18669 * at that point.
18670 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000018671 xmlSchemaTypePtr base;
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018672
18673 /* 4.3.5.5 Constraints on enumeration Schema Components
18674 * Schema Component Constraint: enumeration valid restriction
Jan Pokorný761c9e92013-11-29 23:26:27 +010018675 * It is an `error` if any member of {value} is not in the
18676 * `value space` of {base type definition}.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018677 *
18678 * minInclusive, maxInclusive, minExclusive, maxExclusive:
Jan Pokorný761c9e92013-11-29 23:26:27 +010018679 * The value `must` be in the
18680 * `value space` of the `base type`.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018681 */
18682 /*
18683 * This function is intended to deliver a compiled value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018684 * on the facet. In this implementation of XML Schemata the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018685 * type holding a facet, won't be a built-in type.
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018686 * Thus to ensure that other API
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018687 * calls (relaxng) do work, if the given type is a built-in
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018688 * type, we will assume that the given built-in type *is
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018689 * already* the base type.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018690 */
18691 if (typeDecl->type != XML_SCHEMA_TYPE_BASIC) {
18692 base = typeDecl->baseType;
18693 if (base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018694 PERROR_INT("xmlSchemaCheckFacet",
18695 "a type user derived type has no base type");
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018696 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018697 }
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018698 } else
18699 base = typeDecl;
Daniel Veillarddee23482008-04-11 12:58:43 +000018700
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018701 if (! ctxtGiven) {
18702 /*
18703 * A context is needed if called from RelaxNG.
Daniel Veillarddee23482008-04-11 12:58:43 +000018704 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018705 pctxt = xmlSchemaNewParserCtxt("*");
18706 if (pctxt == NULL)
18707 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000018708 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018709 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018710 * NOTE: This call does not check the content nodes,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018711 * since they are not available:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018712 * facet->node is just the node holding the facet
18713 * definition, *not* the attribute holding the *value*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018714 * of the facet.
Daniel Veillarddee23482008-04-11 12:58:43 +000018715 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018716 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018717 ACTXT_CAST pctxt, facet->node, base,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018718 facet->value, &(facet->val), 1, 1, 0);
18719 if (ret != 0) {
18720 if (ret < 0) {
18721 /* No error message for RelaxNG. */
Daniel Veillarddee23482008-04-11 12:58:43 +000018722 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018723 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018724 XML_SCHEMAP_INTERNAL, facet->node, NULL,
Daniel Veillarddee23482008-04-11 12:58:43 +000018725 "Internal error: xmlSchemaCheckFacet, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018726 "failed to validate the value '%s' of the "
18727 "facet '%s' against the base type",
18728 facet->value, xmlSchemaFacetTypeToString(facet->type));
18729 }
18730 goto internal_error;
18731 }
18732 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18733 /* No error message for RelaxNG. */
18734 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018735 xmlChar *str = NULL;
18736
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018737 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018738 ret, facet->node, WXS_BASIC_CAST facet,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018739 "The value '%s' of the facet does not validate "
18740 "against the base type '%s'",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018741 facet->value,
18742 xmlSchemaFormatQName(&str,
18743 base->targetNamespace, base->name));
18744 FREE_AND_NULL(str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018745 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018746 goto exit;
18747 } else if (facet->val == NULL) {
18748 if (ctxtGiven) {
18749 PERROR_INT("xmlSchemaCheckFacet",
18750 "value was not computed");
18751 }
18752 TODO
18753 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018754 break;
18755 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018756 case XML_SCHEMA_FACET_PATTERN:
18757 facet->regexp = xmlRegexpCompile(facet->value);
18758 if (facet->regexp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018759 ret = XML_SCHEMAP_REGEXP_INVALID;
18760 /* No error message for RelaxNG. */
18761 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018762 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018763 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018764 "The value '%s' of the facet 'pattern' is not a "
18765 "valid regular expression",
18766 facet->value, NULL);
18767 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018768 }
18769 break;
18770 case XML_SCHEMA_FACET_TOTALDIGITS:
18771 case XML_SCHEMA_FACET_FRACTIONDIGITS:
18772 case XML_SCHEMA_FACET_LENGTH:
18773 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018774 case XML_SCHEMA_FACET_MINLENGTH:
18775
18776 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
18777 ret = xmlSchemaValidatePredefinedType(
18778 xmlSchemaGetBuiltInType(XML_SCHEMAS_PINTEGER),
18779 facet->value, &(facet->val));
18780 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018781 ret = xmlSchemaValidatePredefinedType(
18782 xmlSchemaGetBuiltInType(XML_SCHEMAS_NNINTEGER),
18783 facet->value, &(facet->val));
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018784 }
18785 if (ret != 0) {
18786 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018787 /* No error message for RelaxNG. */
18788 if (ctxtGiven) {
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018789 PERROR_INT("xmlSchemaCheckFacet",
18790 "validating facet value");
18791 }
18792 goto internal_error;
18793 }
18794 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18795 /* No error message for RelaxNG. */
18796 if (ctxtGiven) {
18797 /* error code */
18798 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
18799 ret, facet->node, WXS_BASIC_CAST typeDecl,
Daniel Veillarddee23482008-04-11 12:58:43 +000018800 "The value '%s' of the facet '%s' is not a valid '%s'",
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018801 facet->value,
18802 xmlSchemaFacetTypeToString(facet->type),
Daniel Veillarddee23482008-04-11 12:58:43 +000018803 (facet->type != XML_SCHEMA_FACET_TOTALDIGITS) ?
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018804 BAD_CAST "nonNegativeInteger" :
18805 BAD_CAST "positiveInteger",
18806 NULL);
18807 }
18808 }
18809 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000018810
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018811 case XML_SCHEMA_FACET_WHITESPACE:{
18812 if (xmlStrEqual(facet->value, BAD_CAST "preserve")) {
18813 facet->whitespace = XML_SCHEMAS_FACET_PRESERVE;
18814 } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) {
18815 facet->whitespace = XML_SCHEMAS_FACET_REPLACE;
18816 } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) {
18817 facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE;
18818 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018819 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18820 /* No error message for RelaxNG. */
18821 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018822 /* error was previously: XML_SCHEMAP_INVALID_WHITE_SPACE */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018823 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018824 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018825 "The value '%s' of the facet 'whitespace' is not "
18826 "valid", facet->value, NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018827 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018828 }
18829 }
18830 default:
18831 break;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018832 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018833exit:
18834 if ((! ctxtGiven) && (pctxt != NULL))
18835 xmlSchemaFreeParserCtxt(pctxt);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018836 return (ret);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018837internal_error:
18838 if ((! ctxtGiven) && (pctxt != NULL))
18839 xmlSchemaFreeParserCtxt(pctxt);
18840 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018841}
18842
18843/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018844 * xmlSchemaCheckFacetValues:
Daniel Veillard4255d502002-04-16 15:50:10 +000018845 * @typeDecl: the schema type definition
18846 * @ctxt: the schema parser context
18847 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018848 * Checks the default values types, especially for facets
Daniel Veillard4255d502002-04-16 15:50:10 +000018849 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018850static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018851xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018852 xmlSchemaParserCtxtPtr pctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000018853{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018854 int res, olderrs = pctxt->nberrors;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018855 const xmlChar *name = typeDecl->name;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018856 /*
18857 * NOTE: It is intended to use the facets list, instead
18858 * of facetSet.
18859 */
18860 if (typeDecl->facets != NULL) {
18861 xmlSchemaFacetPtr facet = typeDecl->facets;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018862
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018863 /*
18864 * Temporarily assign the "schema" to the validation context
18865 * of the parser context. This is needed for NOTATION validation.
18866 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018867 if (pctxt->vctxt == NULL) {
18868 if (xmlSchemaCreateVCtxtOnPCtxt(pctxt) == -1)
18869 return(-1);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018870 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018871 pctxt->vctxt->schema = pctxt->schema;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018872 while (facet != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018873 res = xmlSchemaCheckFacet(facet, typeDecl, pctxt, name);
18874 HFAILURE
Daniel Veillard01fa6152004-06-29 17:04:39 +000018875 facet = facet->next;
18876 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018877 pctxt->vctxt->schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018878 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018879 if (olderrs != pctxt->nberrors)
18880 return(pctxt->err);
18881 return(0);
18882exit_failure:
18883 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000018884}
18885
18886/**
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018887 * xmlSchemaGetCircModelGrDefRef:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018888 * @ctxtMGroup: the searched model group
18889 * @selfMGroup: the second searched model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018890 * @particle: the first particle
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018891 *
18892 * This one is intended to be used by
18893 * xmlSchemaCheckGroupDefCircular only.
18894 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018895 * Returns the particle with the circular model group definition reference,
18896 * otherwise NULL.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018897 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018898static xmlSchemaTreeItemPtr
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018899xmlSchemaGetCircModelGrDefRef(xmlSchemaModelGroupDefPtr groupDef,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018900 xmlSchemaTreeItemPtr particle)
18901{
18902 xmlSchemaTreeItemPtr circ = NULL;
18903 xmlSchemaTreeItemPtr term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018904 xmlSchemaModelGroupDefPtr gdef;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018905
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018906 for (; particle != NULL; particle = particle->next) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018907 term = particle->children;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018908 if (term == NULL)
18909 continue;
18910 switch (term->type) {
18911 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018912 gdef = (xmlSchemaModelGroupDefPtr) term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018913 if (gdef == groupDef)
18914 return (particle);
18915 /*
18916 * Mark this model group definition to avoid infinite
18917 * recursion on circular references not yet examined.
18918 */
18919 if (gdef->flags & XML_SCHEMA_MODEL_GROUP_DEF_MARKED)
18920 continue;
18921 if (gdef->children != NULL) {
18922 gdef->flags |= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18923 circ = xmlSchemaGetCircModelGrDefRef(groupDef,
18924 gdef->children->children);
18925 gdef->flags ^= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18926 if (circ != NULL)
18927 return (circ);
18928 }
18929 break;
18930 case XML_SCHEMA_TYPE_SEQUENCE:
18931 case XML_SCHEMA_TYPE_CHOICE:
18932 case XML_SCHEMA_TYPE_ALL:
18933 circ = xmlSchemaGetCircModelGrDefRef(groupDef, term->children);
18934 if (circ != NULL)
18935 return (circ);
18936 break;
18937 default:
18938 break;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018939 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018940 }
18941 return (NULL);
18942}
18943
18944/**
18945 * xmlSchemaCheckGroupDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018946 * @item: the model group definition
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018947 * @ctxt: the parser context
18948 * @name: the name
18949 *
18950 * Checks for circular references to model group definitions.
18951 */
18952static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018953xmlSchemaCheckGroupDefCircular(xmlSchemaModelGroupDefPtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018954 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018955{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018956 /*
18957 * Schema Component Constraint: Model Group Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018958 * 2 Circular groups are disallowed. That is, within the {particles}
18959 * of a group there must not be at any depth a particle whose {term}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018960 * is the group itself.
18961 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018962 if ((item == NULL) ||
18963 (item->type != XML_SCHEMA_TYPE_GROUP) ||
18964 (item->children == NULL))
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018965 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018966 {
18967 xmlSchemaTreeItemPtr circ;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018968
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018969 circ = xmlSchemaGetCircModelGrDefRef(item, item->children->children);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018970 if (circ != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018971 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018972 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018973 * TODO: The error report is not adequate: this constraint
18974 * is defined for model groups but not definitions, but since
18975 * there cannot be any circular model groups without a model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018976 * definition (if not using a construction API), we check those
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018977 * defintions only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018978 */
18979 xmlSchemaPCustomErr(ctxt,
18980 XML_SCHEMAP_MG_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018981 NULL, WXS_ITEM_NODE(circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018982 "Circular reference to the model group definition '%s' "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018983 "defined", xmlSchemaFormatQName(&str,
18984 item->targetNamespace, item->name));
18985 FREE_AND_NULL(str)
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018986 /*
18987 * NOTE: We will cut the reference to avoid further
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018988 * confusion of the processor. This is a fatal error.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018989 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018990 circ->children = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018991 }
18992 }
18993}
18994
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018995/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018996 * xmlSchemaModelGroupToModelGroupDefFixup:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018997 * @ctxt: the parser context
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018998 * @mg: the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018999 *
19000 * Assigns the model group of model group definitions to the "term"
19001 * of the referencing particle.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019002 * In xmlSchemaResolveModelGroupParticleReferences the model group
19003 * definitions were assigned to the "term", since needed for the
19004 * circularity check.
19005 *
19006 * Schema Component Constraint:
19007 * All Group Limited (cos-all-limited) (1.2)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019008 */
19009static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019010xmlSchemaModelGroupToModelGroupDefFixup(
19011 xmlSchemaParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
19012 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019013{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019014 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
19015
19016 while (particle != NULL) {
19017 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
19018 ((WXS_PARTICLE_TERM(particle))->type !=
19019 XML_SCHEMA_TYPE_GROUP))
19020 {
19021 particle = WXS_PTC_CAST particle->next;
19022 continue;
Daniel Veillarddee23482008-04-11 12:58:43 +000019023 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019024 if (WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle)) == NULL) {
19025 /*
19026 * TODO: Remove the particle.
19027 */
19028 WXS_PARTICLE_TERM(particle) = NULL;
19029 particle = WXS_PTC_CAST particle->next;
19030 continue;
19031 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019032 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019033 * Assign the model group to the {term} of the particle.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019034 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019035 WXS_PARTICLE_TERM(particle) =
19036 WXS_TREE_CAST WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle));
19037
19038 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019039 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019040}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019041
19042/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019043 * xmlSchemaCheckAttrGroupCircularRecur:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019044 * @ctxtGr: the searched attribute group
19045 * @attr: the current attribute list to be processed
19046 *
19047 * This one is intended to be used by
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019048 * xmlSchemaCheckAttrGroupCircular only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019049 *
19050 * Returns the circular attribute grou reference, otherwise NULL.
19051 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019052static xmlSchemaQNameRefPtr
19053xmlSchemaCheckAttrGroupCircularRecur(xmlSchemaAttributeGroupPtr ctxtGr,
19054 xmlSchemaItemListPtr list)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019055{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019056 xmlSchemaAttributeGroupPtr gr;
19057 xmlSchemaQNameRefPtr ref, circ;
19058 int i;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019059 /*
19060 * We will search for an attribute group reference which
19061 * references the context attribute group.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019062 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019063 for (i = 0; i < list->nbItems; i++) {
19064 ref = list->items[i];
19065 if ((ref->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
19066 (ref->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
19067 (ref->item != NULL))
19068 {
19069 gr = WXS_ATTR_GROUP_CAST ref->item;
19070 if (gr == ctxtGr)
19071 return(ref);
19072 if (gr->flags & XML_SCHEMAS_ATTRGROUP_MARKED)
Daniel Veillarddee23482008-04-11 12:58:43 +000019073 continue;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019074 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019075 * Mark as visited to avoid infinite recursion on
19076 * circular references not yet examined.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019077 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019078 if ((gr->attrUses) &&
19079 (gr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS))
19080 {
19081 gr->flags |= XML_SCHEMAS_ATTRGROUP_MARKED;
19082 circ = xmlSchemaCheckAttrGroupCircularRecur(ctxtGr,
Daniel Veillarddee23482008-04-11 12:58:43 +000019083 (xmlSchemaItemListPtr) gr->attrUses);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019084 gr->flags ^= XML_SCHEMAS_ATTRGROUP_MARKED;
19085 if (circ != NULL)
19086 return (circ);
19087 }
Daniel Veillarddee23482008-04-11 12:58:43 +000019088
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019089 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019090 }
19091 return (NULL);
19092}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019093
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019094/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019095 * xmlSchemaCheckAttrGroupCircular:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019096 * attrGr: the attribute group definition
19097 * @ctxt: the parser context
19098 * @name: the name
19099 *
19100 * Checks for circular references of attribute groups.
19101 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019102static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019103xmlSchemaCheckAttrGroupCircular(xmlSchemaAttributeGroupPtr attrGr,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019104 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019105{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019106 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019107 * Schema Representation Constraint:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019108 * Attribute Group Definition Representation OK
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019109 * 3 Circular group reference is disallowed outside <redefine>.
19110 * That is, unless this element information item's parent is
19111 * <redefine>, then among the [children], if any, there must
19112 * not be an <attributeGroup> with ref [attribute] which resolves
19113 * to the component corresponding to this <attributeGroup>. Indirect
19114 * circularity is also ruled out. That is, when QName resolution
Jan Pokorný761c9e92013-11-29 23:26:27 +010019115 * (Schema Document) ($3.15.3) is applied to a `QName` arising from
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019116 * any <attributeGroup>s with a ref [attribute] among the [children],
Jan Pokorný761c9e92013-11-29 23:26:27 +010019117 * it must not be the case that a `QName` is encountered at any depth
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019118 * which resolves to the component corresponding to this <attributeGroup>.
19119 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019120 if (attrGr->attrUses == NULL)
19121 return(0);
19122 else if ((attrGr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS) == 0)
19123 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019124 else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019125 xmlSchemaQNameRefPtr circ;
Daniel Veillarddee23482008-04-11 12:58:43 +000019126
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019127 circ = xmlSchemaCheckAttrGroupCircularRecur(attrGr,
Daniel Veillarddee23482008-04-11 12:58:43 +000019128 (xmlSchemaItemListPtr) attrGr->attrUses);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019129 if (circ != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019130 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019131 /*
19132 * TODO: Report the referenced attr group as QName.
19133 */
19134 xmlSchemaPCustomErr(ctxt,
19135 XML_SCHEMAP_SRC_ATTRIBUTE_GROUP_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019136 NULL, WXS_ITEM_NODE(WXS_BASIC_CAST circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019137 "Circular reference to the attribute group '%s' "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019138 "defined", xmlSchemaGetComponentQName(&str, attrGr));
19139 FREE_AND_NULL(str);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019140 /*
19141 * NOTE: We will cut the reference to avoid further
19142 * confusion of the processor.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019143 * BADSPEC TODO: The spec should define how to process in this case.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019144 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019145 circ->item = NULL;
19146 return(ctxt->err);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019147 }
19148 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019149 return(0);
19150}
19151
19152static int
19153xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19154 xmlSchemaAttributeGroupPtr attrGr);
19155
19156/**
19157 * xmlSchemaExpandAttributeGroupRefs:
19158 * @pctxt: the parser context
19159 * @node: the node of the component holding the attribute uses
Daniel Veillarddee23482008-04-11 12:58:43 +000019160 * @completeWild: the intersected wildcard to be returned
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019161 * @list: the attribute uses
19162 *
19163 * Substitutes contained attribute group references
19164 * for their attribute uses. Wilcards are intersected.
19165 * Attribute use prohibitions are removed from the list
19166 * and returned via the @prohibs list.
19167 * Pointlessness of attr. prohibs, if a matching attr. decl
19168 * is existent a well, are checked.
19169 */
19170static int
19171xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
19172 xmlSchemaBasicItemPtr item,
19173 xmlSchemaWildcardPtr *completeWild,
19174 xmlSchemaItemListPtr list,
19175 xmlSchemaItemListPtr prohibs)
19176{
19177 xmlSchemaAttributeGroupPtr gr;
19178 xmlSchemaAttributeUsePtr use;
19179 xmlSchemaItemListPtr sublist;
19180 int i, j;
19181 int created = (*completeWild == NULL) ? 0 : 1;
19182
19183 if (prohibs)
19184 prohibs->nbItems = 0;
19185
19186 for (i = 0; i < list->nbItems; i++) {
19187 use = list->items[i];
19188
Daniel Veillarddee23482008-04-11 12:58:43 +000019189 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019190 if (prohibs == NULL) {
19191 PERROR_INT("xmlSchemaExpandAttributeGroupRefs",
19192 "unexpected attr prohibition found");
19193 return(-1);
19194 }
19195 /*
19196 * Remove from attribute uses.
19197 */
19198 if (xmlSchemaItemListRemove(list, i) == -1)
19199 return(-1);
19200 i--;
19201 /*
19202 * Note that duplicate prohibitions were already
19203 * handled at parsing time.
Daniel Veillarddee23482008-04-11 12:58:43 +000019204 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019205 /*
19206 * Add to list of prohibitions.
19207 */
19208 xmlSchemaItemListAddSize(prohibs, 2, use);
19209 continue;
19210 }
19211 if ((use->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
19212 ((WXS_QNAME_CAST use)->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP))
19213 {
19214 if ((WXS_QNAME_CAST use)->item == NULL)
19215 return(-1);
19216 gr = WXS_ATTR_GROUP_CAST (WXS_QNAME_CAST use)->item;
19217 /*
19218 * Expand the referenced attr. group.
19219 * TODO: remove this, this is done in a previous step, so
19220 * already done here.
19221 */
19222 if ((gr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED) == 0) {
19223 if (xmlSchemaAttributeGroupExpandRefs(pctxt, gr) == -1)
19224 return(-1);
19225 }
19226 /*
19227 * Build the 'complete' wildcard; i.e. intersect multiple
19228 * wildcards.
19229 */
19230 if (gr->attributeWildcard != NULL) {
19231 if (*completeWild == NULL) {
19232 *completeWild = gr->attributeWildcard;
19233 } else {
19234 if (! created) {
19235 xmlSchemaWildcardPtr tmpWild;
19236
19237 /*
19238 * Copy the first encountered wildcard as context,
19239 * except for the annotation.
19240 *
19241 * Although the complete wildcard might not correspond
19242 * to any node in the schema, we will anchor it on
19243 * the node of the owner component.
19244 */
19245 tmpWild = xmlSchemaAddWildcard(pctxt, pctxt->schema,
19246 XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
19247 WXS_ITEM_NODE(item));
19248 if (tmpWild == NULL)
19249 return(-1);
19250 if (xmlSchemaCloneWildcardNsConstraints(pctxt,
19251 tmpWild, *completeWild) == -1)
19252 return (-1);
19253 tmpWild->processContents = (*completeWild)->processContents;
19254 *completeWild = tmpWild;
19255 created = 1;
19256 }
Daniel Veillarddee23482008-04-11 12:58:43 +000019257
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019258 if (xmlSchemaIntersectWildcards(pctxt, *completeWild,
19259 gr->attributeWildcard) == -1)
19260 return(-1);
19261 }
19262 }
19263 /*
19264 * Just remove the reference if the referenced group does not
19265 * contain any attribute uses.
19266 */
William M. Brack06559b32007-03-14 09:34:15 +000019267 sublist = ((xmlSchemaItemListPtr) gr->attrUses);
19268 if ((sublist == NULL) || sublist->nbItems == 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019269 if (xmlSchemaItemListRemove(list, i) == -1)
19270 return(-1);
19271 i--;
19272 continue;
19273 }
19274 /*
19275 * Add the attribute uses.
19276 */
William M. Brack06559b32007-03-14 09:34:15 +000019277 list->items[i] = sublist->items[0];
19278 if (sublist->nbItems != 1) {
19279 for (j = 1; j < sublist->nbItems; j++) {
19280 i++;
19281 if (xmlSchemaItemListInsert(list,
19282 sublist->items[j], i) == -1)
19283 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019284 }
William M. Brack06559b32007-03-14 09:34:15 +000019285 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019286 }
19287
19288 }
19289 /*
19290 * Handle pointless prohibitions of declared attributes.
19291 */
19292 if (prohibs && (prohibs->nbItems != 0) && (list->nbItems != 0)) {
19293 xmlSchemaAttributeUseProhibPtr prohib;
19294
19295 for (i = prohibs->nbItems -1; i >= 0; i--) {
19296 prohib = prohibs->items[i];
19297 for (j = 0; j < list->nbItems; j++) {
19298 use = list->items[j];
19299
19300 if ((prohib->name == WXS_ATTRUSE_DECL_NAME(use)) &&
19301 (prohib->targetNamespace == WXS_ATTRUSE_DECL_TNS(use)))
19302 {
19303 xmlChar *str = NULL;
19304
19305 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
19306 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
19307 prohib->node, NULL,
19308 "Skipping pointless attribute use prohibition "
19309 "'%s', since a corresponding attribute use "
19310 "exists already in the type definition",
19311 xmlSchemaFormatQName(&str,
19312 prohib->targetNamespace, prohib->name),
19313 NULL, NULL);
19314 FREE_AND_NULL(str);
19315 /*
19316 * Remove the prohibition.
19317 */
19318 if (xmlSchemaItemListRemove(prohibs, i) == -1)
19319 return(-1);
19320 break;
19321 }
19322 }
19323 }
19324 }
19325 return(0);
19326}
19327
19328/**
19329 * xmlSchemaAttributeGroupExpandRefs:
19330 * @pctxt: the parser context
Daniel Veillarddee23482008-04-11 12:58:43 +000019331 * @attrGr: the attribute group definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019332 *
19333 * Computation of:
19334 * {attribute uses} property
19335 * {attribute wildcard} property
19336 *
19337 * Substitutes contained attribute group references
19338 * for their attribute uses. Wilcards are intersected.
19339 */
19340static int
19341xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19342 xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillarddee23482008-04-11 12:58:43 +000019343{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019344 if ((attrGr->attrUses == NULL) ||
19345 (attrGr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED))
19346 return(0);
19347
19348 attrGr->flags |= XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED;
19349 if (xmlSchemaExpandAttributeGroupRefs(pctxt, WXS_BASIC_CAST attrGr,
19350 &(attrGr->attributeWildcard), attrGr->attrUses, NULL) == -1)
Daniel Veillarddee23482008-04-11 12:58:43 +000019351 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019352 return(0);
19353}
19354
19355/**
19356 * xmlSchemaAttributeGroupExpandRefs:
19357 * @pctxt: the parser context
Daniel Veillarddee23482008-04-11 12:58:43 +000019358 * @attrGr: the attribute group definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019359 *
19360 * Substitutes contained attribute group references
19361 * for their attribute uses. Wilcards are intersected.
Daniel Veillarddee23482008-04-11 12:58:43 +000019362 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019363 * Schema Component Constraint:
Daniel Veillarddee23482008-04-11 12:58:43 +000019364 * Attribute Group Definition Properties Correct (ag-props-correct)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019365 */
19366static int
19367xmlSchemaCheckAGPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19368 xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillarddee23482008-04-11 12:58:43 +000019369{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019370 /*
19371 * SPEC ag-props-correct
19372 * (1) "The values of the properties of an attribute group definition
19373 * must be as described in the property tableau in The Attribute
Jan Pokorný761c9e92013-11-29 23:26:27 +010019374 * Group Definition Schema Component ($3.6.1), modulo the impact of
19375 * Missing Sub-components ($5.3);"
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019376 */
Daniel Veillarddee23482008-04-11 12:58:43 +000019377
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019378 if ((attrGr->attrUses != NULL) &&
19379 (WXS_LIST_CAST attrGr->attrUses)->nbItems > 1)
19380 {
19381 xmlSchemaItemListPtr uses = WXS_LIST_CAST attrGr->attrUses;
19382 xmlSchemaAttributeUsePtr use, tmp;
19383 int i, j, hasId = 0;
19384
19385 for (i = uses->nbItems -1; i >= 0; i--) {
Daniel Veillarddee23482008-04-11 12:58:43 +000019386 use = uses->items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019387 /*
19388 * SPEC ag-props-correct
19389 * (2) "Two distinct members of the {attribute uses} must not have
19390 * {attribute declaration}s both of whose {name}s match and whose
19391 * {target namespace}s are identical."
19392 */
19393 if (i > 0) {
19394 for (j = i -1; j >= 0; j--) {
19395 tmp = uses->items[j];
19396 if ((WXS_ATTRUSE_DECL_NAME(use) ==
19397 WXS_ATTRUSE_DECL_NAME(tmp)) &&
19398 (WXS_ATTRUSE_DECL_TNS(use) ==
19399 WXS_ATTRUSE_DECL_TNS(tmp)))
19400 {
19401 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000019402
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019403 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19404 XML_SCHEMAP_AG_PROPS_CORRECT,
19405 attrGr->node, WXS_BASIC_CAST attrGr,
19406 "Duplicate %s",
19407 xmlSchemaGetComponentDesignation(&str, use),
19408 NULL);
19409 FREE_AND_NULL(str);
19410 /*
19411 * Remove the duplicate.
19412 */
19413 if (xmlSchemaItemListRemove(uses, i) == -1)
19414 return(-1);
19415 goto next_use;
19416 }
19417 }
19418 }
19419 /*
19420 * SPEC ag-props-correct
19421 * (3) "Two distinct members of the {attribute uses} must not have
19422 * {attribute declaration}s both of whose {type definition}s are or
19423 * are derived from ID."
19424 * TODO: Does 'derived' include member-types of unions?
19425 */
Daniel Veillarddee23482008-04-11 12:58:43 +000019426 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019427 if (xmlSchemaIsDerivedFromBuiltInType(
19428 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
Daniel Veillarddee23482008-04-11 12:58:43 +000019429 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019430 if (hasId) {
19431 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000019432
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019433 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19434 XML_SCHEMAP_AG_PROPS_CORRECT,
19435 attrGr->node, WXS_BASIC_CAST attrGr,
19436 "There must not exist more than one attribute "
19437 "declaration of type 'xs:ID' "
19438 "(or derived from 'xs:ID'). The %s violates this "
19439 "constraint",
19440 xmlSchemaGetComponentDesignation(&str, use),
19441 NULL);
19442 FREE_AND_NULL(str);
19443 if (xmlSchemaItemListRemove(uses, i) == -1)
19444 return(-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000019445 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019446 hasId = 1;
19447 }
19448 }
19449next_use: {}
19450 }
19451 }
19452 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019453}
19454
19455/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019456 * xmlSchemaResolveAttrGroupReferences:
Daniel Veillard13e04c62002-04-23 17:51:29 +000019457 * @attrgrpDecl: the schema attribute definition
19458 * @ctxt: the schema parser context
19459 * @name: the attribute name
19460 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019461 * Resolves references to attribute group definitions.
Daniel Veillard13e04c62002-04-23 17:51:29 +000019462 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019463static int
19464xmlSchemaResolveAttrGroupReferences(xmlSchemaQNameRefPtr ref,
19465 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard13e04c62002-04-23 17:51:29 +000019466{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019467 xmlSchemaAttributeGroupPtr group;
Daniel Veillard13e04c62002-04-23 17:51:29 +000019468
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019469 if (ref->item != NULL)
19470 return(0);
19471 group = xmlSchemaGetAttributeGroup(ctxt->schema,
19472 ref->name,
19473 ref->targetNamespace);
19474 if (group == NULL) {
19475 xmlSchemaPResCompAttrErr(ctxt,
19476 XML_SCHEMAP_SRC_RESOLVE,
19477 NULL, ref->node,
19478 "ref", ref->name, ref->targetNamespace,
19479 ref->itemType, NULL);
19480 return(ctxt->err);
Daniel Veillard3646d642004-06-02 19:19:14 +000019481 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019482 ref->item = WXS_BASIC_CAST group;
19483 return(0);
Daniel Veillard13e04c62002-04-23 17:51:29 +000019484}
19485
19486/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019487 * xmlSchemaCheckAttrPropsCorrect:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019488 * @item: an schema attribute declaration/use
19489 * @ctxt: a schema parser context
19490 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019491 *
19492 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019493 * Schema Component Constraint:
19494 * Attribute Declaration Properties Correct (a-props-correct)
Daniel Veillard4255d502002-04-16 15:50:10 +000019495 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019496 * Validates the value constraints of an attribute declaration/use.
19497 * NOTE that this needs the simle type definitions to be already
19498 * builded and checked.
Daniel Veillard4255d502002-04-16 15:50:10 +000019499 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019500static int
19501xmlSchemaCheckAttrPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19502 xmlSchemaAttributePtr attr)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019503{
19504
19505 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019506 * SPEC a-props-correct (1)
19507 * "The values of the properties of an attribute declaration must
19508 * be as described in the property tableau in The Attribute
Jan Pokorný761c9e92013-11-29 23:26:27 +010019509 * Declaration Schema Component ($3.2.1), modulo the impact of
19510 * Missing Sub-components ($5.3)."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019511 */
Daniel Veillarddee23482008-04-11 12:58:43 +000019512
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019513 if (WXS_ATTR_TYPEDEF(attr) == NULL)
19514 return(0);
19515
19516 if (attr->defValue != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019517 int ret;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019518
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019519 /*
19520 * SPEC a-props-correct (3)
19521 * "If the {type definition} is or is derived from ID then there
19522 * must not be a {value constraint}."
19523 */
19524 if (xmlSchemaIsDerivedFromBuiltInType(
19525 WXS_ATTR_TYPEDEF(attr), XML_SCHEMAS_ID))
19526 {
19527 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19528 XML_SCHEMAP_A_PROPS_CORRECT_3,
19529 NULL, WXS_BASIC_CAST attr,
19530 "Value constraints are not allowed if the type definition "
19531 "is or is derived from xs:ID",
19532 NULL, NULL);
19533 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019534 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019535 /*
19536 * SPEC a-props-correct (2)
19537 * "if there is a {value constraint}, the canonical lexical
Jan Pokorný761c9e92013-11-29 23:26:27 +010019538 * representation of its value must be `valid` with respect
19539 * to the {type definition} as defined in String Valid ($3.14.4)."
Jan Pokorný7a7cad62013-11-29 23:26:26 +010019540 * TODO: Don't care about the *canonical* stuff here, this requirement
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019541 * will be removed in WXS 1.1 anyway.
19542 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019543 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019544 attr->node, WXS_ATTR_TYPEDEF(attr),
19545 attr->defValue, &(attr->defVal),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019546 1, 1, 0);
19547 if (ret != 0) {
19548 if (ret < 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019549 PERROR_INT("xmlSchemaCheckAttrPropsCorrect",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019550 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019551 return(-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019552 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019553 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019554 XML_SCHEMAP_A_PROPS_CORRECT_2,
19555 NULL, WXS_BASIC_CAST attr,
19556 "The value of the value constraint is not valid",
19557 NULL, NULL);
19558 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019559 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019560 }
Daniel Veillarddee23482008-04-11 12:58:43 +000019561
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019562 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019563}
19564
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019565static xmlSchemaElementPtr
19566xmlSchemaCheckSubstGroupCircular(xmlSchemaElementPtr elemDecl,
19567 xmlSchemaElementPtr ancestor)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019568{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019569 xmlSchemaElementPtr ret;
19570
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019571 if (WXS_SUBST_HEAD(ancestor) == NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019572 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019573 if (WXS_SUBST_HEAD(ancestor) == elemDecl)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019574 return (ancestor);
19575
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019576 if (WXS_SUBST_HEAD(ancestor)->flags & XML_SCHEMAS_ELEM_CIRCULAR)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019577 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019578 WXS_SUBST_HEAD(ancestor)->flags |= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019579 ret = xmlSchemaCheckSubstGroupCircular(elemDecl,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019580 WXS_SUBST_HEAD(ancestor));
19581 WXS_SUBST_HEAD(ancestor)->flags ^= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019582
19583 return (ret);
19584}
19585
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019586/**
19587 * xmlSchemaCheckElemPropsCorrect:
19588 * @ctxt: a schema parser context
19589 * @decl: the element declaration
19590 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019591 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019592 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019593 * Element Declaration Properties Correct (e-props-correct)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019594 *
19595 * STATUS:
19596 * missing: (6)
19597 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019598static int
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019599xmlSchemaCheckElemPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19600 xmlSchemaElementPtr elemDecl)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019601{
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019602 int ret = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019603 xmlSchemaTypePtr typeDef = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019604 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019605 * SPEC (1) "The values of the properties of an element declaration
19606 * must be as described in the property tableau in The Element
Jan Pokorný761c9e92013-11-29 23:26:27 +010019607 * Declaration Schema Component ($3.3.1), modulo the impact of Missing
19608 * Sub-components ($5.3)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019609 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019610 if (WXS_SUBST_HEAD(elemDecl) != NULL) {
19611 xmlSchemaElementPtr head = WXS_SUBST_HEAD(elemDecl), circ;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019612
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019613 xmlSchemaCheckElementDeclComponent(head, pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019614 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010019615 * SPEC (3) "If there is a non-`absent` {substitution group
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019616 * affiliation}, then {scope} must be global."
19617 */
19618 if ((elemDecl->flags & XML_SCHEMAS_ELEM_GLOBAL) == 0) {
19619 xmlSchemaPCustomErr(pctxt,
19620 XML_SCHEMAP_E_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019621 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019622 "Only global element declarations can have a "
19623 "substitution group affiliation", NULL);
19624 ret = XML_SCHEMAP_E_PROPS_CORRECT_3;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019625 }
19626 /*
19627 * TODO: SPEC (6) "Circular substitution groups are disallowed.
19628 * That is, it must not be possible to return to an element declaration
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019629 * by repeatedly following the {substitution group affiliation}
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019630 * property."
19631 */
19632 if (head == elemDecl)
19633 circ = head;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019634 else if (WXS_SUBST_HEAD(head) != NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019635 circ = xmlSchemaCheckSubstGroupCircular(head, head);
19636 else
19637 circ = NULL;
19638 if (circ != NULL) {
19639 xmlChar *strA = NULL, *strB = NULL;
19640
19641 xmlSchemaPCustomErrExt(pctxt,
19642 XML_SCHEMAP_E_PROPS_CORRECT_6,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019643 WXS_BASIC_CAST circ, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019644 "The element declaration '%s' defines a circular "
19645 "substitution group to element declaration '%s'",
19646 xmlSchemaGetComponentQName(&strA, circ),
19647 xmlSchemaGetComponentQName(&strB, head),
19648 NULL);
19649 FREE_AND_NULL(strA)
19650 FREE_AND_NULL(strB)
19651 ret = XML_SCHEMAP_E_PROPS_CORRECT_6;
19652 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019653 /*
19654 * SPEC (4) "If there is a {substitution group affiliation},
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019655 * the {type definition}
19656 * of the element declaration must be validly derived from the {type
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019657 * definition} of the {substitution group affiliation}, given the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019658 * of the {substitution group exclusions} of the {substitution group
Jan Pokorný761c9e92013-11-29 23:26:27 +010019659 * affiliation}, as defined in Type Derivation OK (Complex) ($3.4.6)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019660 * (if the {type definition} is complex) or as defined in
Jan Pokorný761c9e92013-11-29 23:26:27 +010019661 * Type Derivation OK (Simple) ($3.14.6) (if the {type definition} is
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019662 * simple)."
19663 *
19664 * NOTE: {substitution group exclusions} means the values of the
19665 * attribute "final".
19666 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019667
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019668 if (typeDef != WXS_ELEM_TYPEDEF(WXS_SUBST_HEAD(elemDecl))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019669 int set = 0;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019670
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019671 if (head->flags & XML_SCHEMAS_ELEM_FINAL_EXTENSION)
19672 set |= SUBSET_EXTENSION;
19673 if (head->flags & XML_SCHEMAS_ELEM_FINAL_RESTRICTION)
19674 set |= SUBSET_RESTRICTION;
19675
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000019676 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST pctxt, typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019677 WXS_ELEM_TYPEDEF(head), set) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019678 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
19679
19680 ret = XML_SCHEMAP_E_PROPS_CORRECT_4;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019681 xmlSchemaPCustomErrExt(pctxt,
19682 XML_SCHEMAP_E_PROPS_CORRECT_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019683 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019684 "The type definition '%s' was "
19685 "either rejected by the substitution group "
19686 "affiliation '%s', or not validly derived from its type "
19687 "definition '%s'",
19688 xmlSchemaGetComponentQName(&strA, typeDef),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019689 xmlSchemaGetComponentQName(&strB, head),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019690 xmlSchemaGetComponentQName(&strC, WXS_ELEM_TYPEDEF(head)));
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019691 FREE_AND_NULL(strA)
19692 FREE_AND_NULL(strB)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019693 FREE_AND_NULL(strC)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019694 }
19695 }
19696 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019697 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019698 * SPEC (5) "If the {type definition} or {type definition}'s
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019699 * {content type}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019700 * is or is derived from ID then there must not be a {value constraint}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019701 * Note: The use of ID as a type definition for elements goes beyond
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019702 * XML 1.0, and should be avoided if backwards compatibility is desired"
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019703 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019704 if ((elemDecl->value != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019705 ((WXS_IS_SIMPLE(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019706 xmlSchemaIsDerivedFromBuiltInType(typeDef, XML_SCHEMAS_ID)) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019707 (WXS_IS_COMPLEX(typeDef) &&
19708 WXS_HAS_SIMPLE_CONTENT(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019709 xmlSchemaIsDerivedFromBuiltInType(typeDef->contentTypeDef,
19710 XML_SCHEMAS_ID)))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019711
19712 ret = XML_SCHEMAP_E_PROPS_CORRECT_5;
19713 xmlSchemaPCustomErr(pctxt,
19714 XML_SCHEMAP_E_PROPS_CORRECT_5,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019715 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019716 "The type definition (or type definition's content type) is or "
19717 "is derived from ID; value constraints are not allowed in "
19718 "conjunction with such a type definition", NULL);
19719 } else if (elemDecl->value != NULL) {
19720 int vcret;
19721 xmlNodePtr node = NULL;
19722
19723 /*
19724 * SPEC (2) "If there is a {value constraint}, the canonical lexical
Jan Pokorný761c9e92013-11-29 23:26:27 +010019725 * representation of its value must be `valid` with respect to the
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019726 * {type definition} as defined in Element Default Valid (Immediate)
Jan Pokorný761c9e92013-11-29 23:26:27 +010019727 * ($3.3.6)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019728 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019729 if (typeDef == NULL) {
19730 xmlSchemaPErr(pctxt, elemDecl->node,
19731 XML_SCHEMAP_INTERNAL,
19732 "Internal error: xmlSchemaCheckElemPropsCorrect, "
19733 "type is missing... skipping validation of "
19734 "the value constraint", NULL, NULL);
19735 return (-1);
19736 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019737 if (elemDecl->node != NULL) {
19738 if (elemDecl->flags & XML_SCHEMAS_ELEM_FIXED)
19739 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19740 BAD_CAST "fixed");
19741 else
19742 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19743 BAD_CAST "default");
19744 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019745 vcret = xmlSchemaParseCheckCOSValidDefault(pctxt, node,
19746 typeDef, elemDecl->value, &(elemDecl->defVal));
19747 if (vcret != 0) {
19748 if (vcret < 0) {
19749 PERROR_INT("xmlSchemaElemCheckValConstr",
19750 "failed to validate the value constraint of an "
19751 "element declaration");
19752 return (-1);
19753 }
19754 return (vcret);
19755 }
19756 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019757
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019758 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019759}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019760
19761/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019762 * xmlSchemaCheckElemSubstGroup:
19763 * @ctxt: a schema parser context
19764 * @decl: the element declaration
19765 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019766 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019767 * Schema Component Constraint:
19768 * Substitution Group (cos-equiv-class)
19769 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019770 * In Libxml2 the subst. groups will be precomputed, in terms of that
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019771 * a list will be built for each subst. group head, holding all direct
19772 * referents to this head.
19773 * NOTE that this function needs:
19774 * 1. circular subst. groups to be checked beforehand
19775 * 2. the declaration's type to be derived from the head's type
19776 *
19777 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019778 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019779 */
19780static void
19781xmlSchemaCheckElemSubstGroup(xmlSchemaParserCtxtPtr ctxt,
19782 xmlSchemaElementPtr elemDecl)
19783{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019784 if ((WXS_SUBST_HEAD(elemDecl) == NULL) ||
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019785 /* SPEC (1) "Its {abstract} is false." */
19786 (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT))
19787 return;
19788 {
19789 xmlSchemaElementPtr head;
19790 xmlSchemaTypePtr headType, type;
19791 int set, methSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019792 /*
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019793 * SPEC (2) "It is validly substitutable for HEAD subject to HEAD's
19794 * {disallowed substitutions} as the blocking constraint, as defined in
Jan Pokorný761c9e92013-11-29 23:26:27 +010019795 * Substitution Group OK (Transitive) ($3.3.6)."
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019796 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019797 for (head = WXS_SUBST_HEAD(elemDecl); head != NULL;
19798 head = WXS_SUBST_HEAD(head)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019799 set = 0;
19800 methSet = 0;
19801 /*
19802 * The blocking constraints.
19803 */
19804 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION)
19805 continue;
19806 headType = head->subtypes;
19807 type = elemDecl->subtypes;
19808 if (headType == type)
19809 goto add_member;
19810 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION)
19811 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19812 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION)
19813 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19814 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019815 * SPEC: Substitution Group OK (Transitive) (2.3)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019816 * "The set of all {derivation method}s involved in the
19817 * derivation of D's {type definition} from C's {type definition}
19818 * does not intersect with the union of the blocking constraint,
19819 * C's {prohibited substitutions} (if C is complex, otherwise the
19820 * empty set) and the {prohibited substitutions} (respectively the
19821 * empty set) of any intermediate {type definition}s in the
19822 * derivation of D's {type definition} from C's {type definition}."
19823 */
19824 /*
19825 * OPTIMIZE TODO: Optimize this a bit, since, if traversing the
19826 * subst.head axis, the methSet does not need to be computed for
19827 * the full depth over and over.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019828 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019829 /*
19830 * The set of all {derivation method}s involved in the derivation
19831 */
19832 while ((type != NULL) && (type != headType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019833 if ((WXS_IS_EXTENSION(type)) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019834 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19835 methSet |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019836
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019837 if (WXS_IS_RESTRICTION(type) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019838 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19839 methSet |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19840
19841 type = type->baseType;
19842 }
19843 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019844 * The {prohibited substitutions} of all intermediate types +
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019845 * the head's type.
19846 */
19847 type = elemDecl->subtypes->baseType;
19848 while (type != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019849 if (WXS_IS_COMPLEX(type)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019850 if ((type->flags &
19851 XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19852 ((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) == 0))
19853 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19854 if ((type->flags &
19855 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19856 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19857 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19858 } else
19859 break;
19860 if (type == headType)
19861 break;
19862 type = type->baseType;
19863 }
19864 if ((set != 0) &&
19865 (((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19866 (methSet & XML_SCHEMAS_TYPE_BLOCK_EXTENSION)) ||
19867 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19868 (methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION)))) {
19869 continue;
19870 }
19871add_member:
19872 xmlSchemaAddElementSubstitutionMember(ctxt, head, elemDecl);
19873 if ((head->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) == 0)
19874 head->flags |= XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD;
19875 }
19876 }
19877}
19878
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019879#ifdef WXS_ELEM_DECL_CONS_ENABLED /* enable when finished */
19880/**
19881 * xmlSchemaCheckElementDeclComponent
19882 * @pctxt: the schema parser context
19883 * @ctxtComponent: the context component (an element declaration)
19884 * @ctxtParticle: the first particle of the context component
19885 * @searchParticle: the element declaration particle to be analysed
19886 *
Daniel Veillarddee23482008-04-11 12:58:43 +000019887 * Schema Component Constraint: Element Declarations Consistent
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019888 */
19889static int
19890xmlSchemaCheckElementDeclConsistent(xmlSchemaParserCtxtPtr pctxt,
19891 xmlSchemaBasicItemPtr ctxtComponent,
Daniel Veillarddee23482008-04-11 12:58:43 +000019892 xmlSchemaParticlePtr ctxtParticle,
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019893 xmlSchemaParticlePtr searchParticle,
19894 xmlSchemaParticlePtr curParticle,
19895 int search)
19896{
19897 return(0);
19898
19899 int ret = 0;
19900 xmlSchemaParticlePtr cur = curParticle;
Daniel Veillarddee23482008-04-11 12:58:43 +000019901 if (curParticle == NULL) {
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019902 return(0);
19903 }
19904 if (WXS_PARTICLE_TERM(curParticle) == NULL) {
19905 /*
19906 * Just return in this case. A missing "term" of the particle
19907 * might arise due to an invalid "term" component.
19908 */
19909 return(0);
Daniel Veillarddee23482008-04-11 12:58:43 +000019910 }
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019911 while (cur != NULL) {
19912 switch (WXS_PARTICLE_TERM(cur)->type) {
19913 case XML_SCHEMA_TYPE_ANY:
19914 break;
19915 case XML_SCHEMA_TYPE_ELEMENT:
19916 if (search == 0) {
19917 ret = xmlSchemaCheckElementDeclConsistent(pctxt,
19918 ctxtComponent, ctxtParticle, cur, ctxtParticle, 1);
19919 if (ret != 0)
Daniel Veillarddee23482008-04-11 12:58:43 +000019920 return(ret);
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019921 } else {
19922 xmlSchemaElementPtr elem =
19923 WXS_ELEM_CAST(WXS_PARTICLE_TERM(cur));
19924 /*
19925 * SPEC Element Declarations Consistent:
19926 * "If the {particles} contains, either directly,
19927 * indirectly (that is, within the {particles} of a
Jan Pokorný761c9e92013-11-29 23:26:27 +010019928 * contained model group, recursively) or `implicitly`
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019929 * two or more element declaration particles with
19930 * the same {name} and {target namespace}, then
19931 * all their type definitions must be the same
19932 * top-level definition [...]"
19933 */
19934 if (xmlStrEqual(WXS_PARTICLE_TERM_AS_ELEM(cur)->name,
19935 WXS_PARTICLE_TERM_AS_ELEM(searchParticle)->name) &&
19936 xmlStrEqual(WXS_PARTICLE_TERM_AS_ELEM(cur)->targetNamespace,
19937 WXS_PARTICLE_TERM_AS_ELEM(searchParticle)->targetNamespace))
19938 {
19939 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000019940
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019941 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19942 /* TODO: error code */
19943 XML_SCHEMAP_COS_NONAMBIG,
19944 WXS_ITEM_NODE(cur), NULL,
19945 "In the content model of %s, there are multiple "
19946 "element declarations for '%s' with different "
19947 "type definitions",
19948 xmlSchemaGetComponentDesignation(&strA,
19949 ctxtComponent),
19950 xmlSchemaFormatQName(&strB,
19951 WXS_PARTICLE_TERM_AS_ELEM(cur)->targetNamespace,
19952 WXS_PARTICLE_TERM_AS_ELEM(cur)->name));
19953 FREE_AND_NULL(strA);
19954 FREE_AND_NULL(strB);
19955 return(XML_SCHEMAP_COS_NONAMBIG);
19956 }
Daniel Veillarddee23482008-04-11 12:58:43 +000019957 }
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019958 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000019959 case XML_SCHEMA_TYPE_SEQUENCE: {
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019960 break;
19961 }
19962 case XML_SCHEMA_TYPE_CHOICE:{
19963 /*
19964 xmlSchemaTreeItemPtr sub;
Daniel Veillarddee23482008-04-11 12:58:43 +000019965
19966 sub = WXS_PARTICLE_TERM(particle)->children; (xmlSchemaParticlePtr)
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000019967 while (sub != NULL) {
19968 ret = xmlSchemaCheckElementDeclConsistent(pctxt, ctxtComponent,
19969 ctxtParticle, ctxtElem);
19970 if (ret != 0)
19971 return(ret);
19972 sub = sub->next;
19973 }
19974 */
19975 break;
19976 }
19977 case XML_SCHEMA_TYPE_ALL:
19978 break;
19979 case XML_SCHEMA_TYPE_GROUP:
19980 break;
19981 default:
19982 xmlSchemaInternalErr2(ACTXT_CAST pctxt,
19983 "xmlSchemaCheckElementDeclConsistent",
19984 "found unexpected term of type '%s' in content model",
19985 WXS_ITEM_TYPE_NAME(WXS_PARTICLE_TERM(cur)), NULL);
19986 return(-1);
19987 }
19988 cur = (xmlSchemaParticlePtr) cur->next;
19989 }
19990
19991exit:
19992 return(ret);
19993}
19994#endif
19995
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019996/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019997 * xmlSchemaCheckElementDeclComponent
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019998 * @item: an schema element declaration/particle
19999 * @ctxt: a schema parser context
20000 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020001 *
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020002 * Validates the value constraints of an element declaration.
Daniel Veillarddee23482008-04-11 12:58:43 +000020003 * Adds substitution group members.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020004 */
20005static void
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000020006xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020007 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000020008{
20009 if (elemDecl == NULL)
20010 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020011 if (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_CHECKED)
20012 return;
20013 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_CHECKED;
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000020014 if (xmlSchemaCheckElemPropsCorrect(ctxt, elemDecl) == 0) {
20015 /*
20016 * Adds substitution group members.
20017 */
Daniel Veillarddee23482008-04-11 12:58:43 +000020018 xmlSchemaCheckElemSubstGroup(ctxt, elemDecl);
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000020019 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020020}
20021
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000020022/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020023 * xmlSchemaResolveModelGroupParticleReferences:
20024 * @particle: a particle component
20025 * @ctxt: a parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000020026 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020027 * Resolves references of a model group's {particles} to
20028 * model group definitions and to element declarations.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000020029 */
20030static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020031xmlSchemaResolveModelGroupParticleReferences(
20032 xmlSchemaParserCtxtPtr ctxt,
20033 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000020034{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020035 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
20036 xmlSchemaQNameRefPtr ref;
20037 xmlSchemaBasicItemPtr refItem;
20038
20039 /*
20040 * URGENT TODO: Test this.
20041 */
20042 while (particle != NULL) {
20043 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
20044 ((WXS_PARTICLE_TERM(particle))->type !=
20045 XML_SCHEMA_EXTRA_QNAMEREF))
20046 {
20047 goto next_particle;
Daniel Veillarddee23482008-04-11 12:58:43 +000020048 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020049 ref = WXS_QNAME_CAST WXS_PARTICLE_TERM(particle);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020050 /*
20051 * Resolve the reference.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020052 * NULL the {term} by default.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020053 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020054 particle->children = NULL;
20055
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020056 refItem = xmlSchemaGetNamedComponent(ctxt->schema,
20057 ref->itemType, ref->name, ref->targetNamespace);
20058 if (refItem == NULL) {
20059 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020060 NULL, WXS_ITEM_NODE(particle), "ref", ref->name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020061 ref->targetNamespace, ref->itemType, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020062 /* TODO: remove the particle. */
20063 goto next_particle;
Daniel Veillarddee23482008-04-11 12:58:43 +000020064 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020065 if (refItem->type == XML_SCHEMA_TYPE_GROUP) {
20066 if (WXS_MODELGROUPDEF_MODEL(refItem) == NULL)
20067 /* TODO: remove the particle. */
20068 goto next_particle;
20069 /*
20070 * NOTE that we will assign the model group definition
20071 * itself to the "term" of the particle. This will ease
20072 * the check for circular model group definitions. After
20073 * that the "term" will be assigned the model group of the
20074 * model group definition.
20075 */
20076 if ((WXS_MODELGROUPDEF_MODEL(refItem))->type ==
20077 XML_SCHEMA_TYPE_ALL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020078 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020079 * SPEC cos-all-limited (1)
20080 * SPEC cos-all-limited (1.2)
20081 * "It appears only as the value of one or both of the
20082 * following properties:"
20083 * (1.1) "the {model group} property of a model group
20084 * definition."
20085 * (1.2) "the {term} property of a particle [... of] the "
20086 * {content type} of a complex type definition."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020087 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020088 xmlSchemaCustomErr(ACTXT_CAST ctxt,
20089 /* TODO: error code */
20090 XML_SCHEMAP_COS_ALL_LIMITED,
20091 WXS_ITEM_NODE(particle), NULL,
20092 "A model group definition is referenced, but "
20093 "it contains an 'all' model group, which "
20094 "cannot be contained by model groups",
20095 NULL, NULL);
20096 /* TODO: remove the particle. */
20097 goto next_particle;
20098 }
20099 particle->children = (xmlSchemaTreeItemPtr) refItem;
20100 } else {
20101 /*
20102 * TODO: Are referenced element declarations the only
20103 * other components we expect here?
20104 */
20105 particle->children = (xmlSchemaTreeItemPtr) refItem;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000020106 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020107next_particle:
20108 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000020109 }
20110}
20111
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020112static int
20113xmlSchemaAreValuesEqual(xmlSchemaValPtr x,
Daniel Veillarddee23482008-04-11 12:58:43 +000020114 xmlSchemaValPtr y)
20115{
20116 xmlSchemaTypePtr tx, ty, ptx, pty;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020117 int ret;
20118
20119 while (x != NULL) {
20120 /* Same types. */
20121 tx = xmlSchemaGetBuiltInType(xmlSchemaGetValType(x));
20122 ty = xmlSchemaGetBuiltInType(xmlSchemaGetValType(y));
20123 ptx = xmlSchemaGetPrimitiveType(tx);
20124 pty = xmlSchemaGetPrimitiveType(ty);
20125 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010020126 * (1) if a datatype T' is `derived` by `restriction` from an
20127 * atomic datatype T then the `value space` of T' is a subset of
20128 * the `value space` of T. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020129 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010020130 * (2) if datatypes T' and T'' are `derived` by `restriction`
20131 * from a common atomic ancestor T then the `value space`s of T'
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020132 * and T'' may overlap.
20133 */
20134 if (ptx != pty)
20135 return(0);
20136 /*
20137 * We assume computed values to be normalized, so do a fast
20138 * string comparison for string based types.
20139 */
20140 if ((ptx->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020141 WXS_IS_ANY_SIMPLE_TYPE(ptx)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020142 if (! xmlStrEqual(
20143 xmlSchemaValueGetAsString(x),
20144 xmlSchemaValueGetAsString(y)))
20145 return (0);
20146 } else {
20147 ret = xmlSchemaCompareValuesWhtsp(
20148 x, XML_SCHEMA_WHITESPACE_PRESERVE,
20149 y, XML_SCHEMA_WHITESPACE_PRESERVE);
20150 if (ret == -2)
20151 return(-1);
20152 if (ret != 0)
20153 return(0);
20154 }
20155 /*
20156 * Lists.
20157 */
20158 x = xmlSchemaValueGetNext(x);
20159 if (x != NULL) {
20160 y = xmlSchemaValueGetNext(y);
20161 if (y == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000020162 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020163 } else if (xmlSchemaValueGetNext(y) != NULL)
20164 return (0);
20165 else
20166 return (1);
20167 }
20168 return (0);
20169}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000020170
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020171/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020172 * xmlSchemaResolveAttrUseReferences:
20173 * @item: an attribute use
20174 * @ctxt: a parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020175 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020176 * Resolves the referenced attribute declaration.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020177 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020178static int
20179xmlSchemaResolveAttrUseReferences(xmlSchemaAttributeUsePtr ause,
20180 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000020181{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020182 if ((ctxt == NULL) || (ause == NULL))
20183 return(-1);
20184 if ((ause->attrDecl == NULL) ||
20185 (ause->attrDecl->type != XML_SCHEMA_EXTRA_QNAMEREF))
20186 return(0);
20187
20188 {
20189 xmlSchemaQNameRefPtr ref = WXS_QNAME_CAST ause->attrDecl;
20190
20191 /*
20192 * TODO: Evaluate, what errors could occur if the declaration is not
20193 * found.
20194 */
20195 ause->attrDecl = xmlSchemaGetAttributeDecl(ctxt->schema,
20196 ref->name, ref->targetNamespace);
20197 if (ause->attrDecl == NULL) {
20198 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillardf8e3db02012-09-11 13:26:36 +080020199 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020200 WXS_BASIC_CAST ause, ause->node,
20201 "ref", ref->name, ref->targetNamespace,
20202 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
20203 return(ctxt->err);;
20204 }
20205 }
20206 return(0);
20207}
20208
20209/**
20210 * xmlSchemaCheckAttrUsePropsCorrect:
20211 * @ctxt: a parser context
Daniel Veillarddee23482008-04-11 12:58:43 +000020212 * @use: an attribute use
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020213 *
20214 * Schema Component Constraint:
20215 * Attribute Use Correct (au-props-correct)
Daniel Veillarddee23482008-04-11 12:58:43 +000020216 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020217 */
20218static int
20219xmlSchemaCheckAttrUsePropsCorrect(xmlSchemaParserCtxtPtr ctxt,
20220 xmlSchemaAttributeUsePtr use)
20221{
20222 if ((ctxt == NULL) || (use == NULL))
20223 return(-1);
20224 if ((use->defValue == NULL) || (WXS_ATTRUSE_DECL(use) == NULL) ||
20225 ((WXS_ATTRUSE_DECL(use))->type != XML_SCHEMA_TYPE_ATTRIBUTE))
Daniel Veillarddee23482008-04-11 12:58:43 +000020226 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020227
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020228 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020229 * SPEC au-props-correct (1)
20230 * "The values of the properties of an attribute use must be as
20231 * described in the property tableau in The Attribute Use Schema
Jan Pokorný761c9e92013-11-29 23:26:27 +010020232 * Component ($3.5.1), modulo the impact of Missing
20233 * Sub-components ($5.3)."
Daniel Veillardc0826a72004-08-10 14:17:33 +000020234 */
Daniel Veillarddee23482008-04-11 12:58:43 +000020235
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020236 if (((WXS_ATTRUSE_DECL(use))->defValue != NULL) &&
20237 ((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMAS_ATTR_FIXED) &&
20238 ((use->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
20239 {
20240 xmlSchemaPCustomErr(ctxt,
20241 XML_SCHEMAP_AU_PROPS_CORRECT_2,
20242 WXS_BASIC_CAST use, NULL,
20243 "The attribute declaration has a 'fixed' value constraint "
20244 ", thus the attribute use must also have a 'fixed' value "
20245 "constraint",
20246 NULL);
20247 return(ctxt->err);
20248 }
20249 /*
20250 * Compute and check the value constraint's value.
20251 */
20252 if ((use->defVal != NULL) && (WXS_ATTRUSE_TYPEDEF(use) != NULL)) {
20253 int ret;
20254 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000020255 * TODO: The spec seems to be missing a check of the
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020256 * value constraint of the attribute use. We will do it here.
20257 */
20258 /*
20259 * SPEC a-props-correct (3)
20260 */
20261 if (xmlSchemaIsDerivedFromBuiltInType(
20262 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
20263 {
20264 xmlSchemaCustomErr(ACTXT_CAST ctxt,
20265 XML_SCHEMAP_AU_PROPS_CORRECT,
20266 NULL, WXS_BASIC_CAST use,
20267 "Value constraints are not allowed if the type definition "
20268 "is or is derived from xs:ID",
20269 NULL, NULL);
20270 return(ctxt->err);
20271 }
Daniel Veillarddee23482008-04-11 12:58:43 +000020272
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020273 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST ctxt,
20274 use->node, WXS_ATTRUSE_TYPEDEF(use),
20275 use->defValue, &(use->defVal),
20276 1, 1, 0);
20277 if (ret != 0) {
20278 if (ret < 0) {
20279 PERROR_INT2("xmlSchemaCheckAttrUsePropsCorrect",
20280 "calling xmlSchemaVCheckCVCSimpleType()");
20281 return(-1);
20282 }
20283 xmlSchemaCustomErr(ACTXT_CAST ctxt,
20284 XML_SCHEMAP_AU_PROPS_CORRECT,
20285 NULL, WXS_BASIC_CAST use,
20286 "The value of the value constraint is not valid",
20287 NULL, NULL);
20288 return(ctxt->err);
20289 }
20290 }
20291 /*
20292 * SPEC au-props-correct (2)
20293 * "If the {attribute declaration} has a fixed
20294 * {value constraint}, then if the attribute use itself has a
20295 * {value constraint}, it must also be fixed and its value must match
20296 * that of the {attribute declaration}'s {value constraint}."
20297 */
20298 if (((WXS_ATTRUSE_DECL(use))->defVal != NULL) &&
20299 (((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
20300 {
20301 if (! xmlSchemaAreValuesEqual(use->defVal,
20302 (WXS_ATTRUSE_DECL(use))->defVal))
20303 {
20304 xmlSchemaPCustomErr(ctxt,
20305 XML_SCHEMAP_AU_PROPS_CORRECT_2,
20306 WXS_BASIC_CAST use, NULL,
20307 "The 'fixed' value constraint of the attribute use "
20308 "must match the attribute declaration's value "
20309 "constraint '%s'",
20310 (WXS_ATTRUSE_DECL(use))->defValue);
20311 }
20312 return(ctxt->err);
20313 }
20314 return(0);
20315}
20316
20317
20318
20319
20320/**
20321 * xmlSchemaResolveAttrTypeReferences:
20322 * @item: an attribute declaration
Daniel Veillarddee23482008-04-11 12:58:43 +000020323 * @ctxt: a parser context
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020324 *
20325 * Resolves the referenced type definition component.
20326 */
20327static int
20328xmlSchemaResolveAttrTypeReferences(xmlSchemaAttributePtr item,
20329 xmlSchemaParserCtxtPtr ctxt)
20330{
Daniel Veillard01fa6152004-06-29 17:04:39 +000020331 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020332 * The simple type definition corresponding to the <simpleType> element
20333 * information item in the [children], if present, otherwise the simple
Jan Pokorný761c9e92013-11-29 23:26:27 +010020334 * type definition `resolved` to by the `actual value` of the type
20335 * [attribute], if present, otherwise the `simple ur-type definition`.
Daniel Veillard01fa6152004-06-29 17:04:39 +000020336 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020337 if (item->flags & XML_SCHEMAS_ATTR_INTERNAL_RESOLVED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020338 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020339 item->flags |= XML_SCHEMAS_ATTR_INTERNAL_RESOLVED;
20340 if (item->subtypes != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020341 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020342 if (item->typeName != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000020343 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000020344
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020345 type = xmlSchemaGetType(ctxt->schema, item->typeName,
20346 item->typeNs);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020347 if ((type == NULL) || (! WXS_IS_SIMPLE(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000020348 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000020349 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020350 WXS_BASIC_CAST item, item->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020351 "type", item->typeName, item->typeNs,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020352 XML_SCHEMA_TYPE_SIMPLE, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020353 return(ctxt->err);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020354 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020355 item->subtypes = type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020356
Daniel Veillard3646d642004-06-02 19:19:14 +000020357 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020358 /*
20359 * The type defaults to the xs:anySimpleType.
20360 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020361 item->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
20362 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020363 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000020364}
20365
20366/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020367 * xmlSchemaResolveIDCKeyReferences:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020368 * @idc: the identity-constraint definition
20369 * @ctxt: the schema parser context
20370 * @name: the attribute name
20371 *
20372 * Resolve keyRef references to key/unique IDCs.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020373 * Schema Component Constraint:
20374 * Identity-constraint Definition Properties Correct (c-props-correct)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020375 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020376static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020377xmlSchemaResolveIDCKeyReferences(xmlSchemaIDCPtr idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020378 xmlSchemaParserCtxtPtr pctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020379{
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020380 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020381 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020382 if (idc->ref->name != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020383 idc->ref->item = (xmlSchemaBasicItemPtr)
20384 xmlSchemaGetIDC(pctxt->schema, idc->ref->name,
20385 idc->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020386 if (idc->ref->item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020387 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020388 * TODO: It is actually not an error to fail to resolve
20389 * at this stage. BUT we need to be that strict!
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020390 */
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020391 xmlSchemaPResCompAttrErr(pctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020392 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020393 WXS_BASIC_CAST idc, idc->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020394 "refer", idc->ref->name,
20395 idc->ref->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020396 XML_SCHEMA_TYPE_IDC_KEY, NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020397 return(pctxt->err);
20398 } else if (idc->ref->item->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
20399 /*
20400 * SPEC c-props-correct (1)
20401 */
20402 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20403 XML_SCHEMAP_C_PROPS_CORRECT,
20404 NULL, WXS_BASIC_CAST idc,
20405 "The keyref references a keyref",
20406 NULL, NULL);
20407 idc->ref->item = NULL;
20408 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020409 } else {
20410 if (idc->nbFields !=
20411 ((xmlSchemaIDCPtr) idc->ref->item)->nbFields) {
20412 xmlChar *str = NULL;
20413 xmlSchemaIDCPtr refer;
Daniel Veillarddee23482008-04-11 12:58:43 +000020414
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020415 refer = (xmlSchemaIDCPtr) idc->ref->item;
20416 /*
20417 * SPEC c-props-correct(2)
20418 * "If the {identity-constraint category} is keyref,
20419 * the cardinality of the {fields} must equal that of
20420 * the {fields} of the {referenced key}.
20421 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020422 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020423 XML_SCHEMAP_C_PROPS_CORRECT,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020424 NULL, WXS_BASIC_CAST idc,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020425 "The cardinality of the keyref differs from the "
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020426 "cardinality of the referenced key/unique '%s'",
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020427 xmlSchemaFormatQName(&str, refer->targetNamespace,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020428 refer->name),
20429 NULL);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020430 FREE_AND_NULL(str)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020431 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020432 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020433 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020434 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020435 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020436}
20437
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020438static int
20439xmlSchemaResolveAttrUseProhibReferences(xmlSchemaAttributeUseProhibPtr prohib,
20440 xmlSchemaParserCtxtPtr pctxt)
20441{
20442 if (xmlSchemaGetAttributeDecl(pctxt->schema, prohib->name,
20443 prohib->targetNamespace) == NULL) {
20444
20445 xmlSchemaPResCompAttrErr(pctxt,
20446 XML_SCHEMAP_SRC_RESOLVE,
20447 NULL, prohib->node,
20448 "ref", prohib->name, prohib->targetNamespace,
20449 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
20450 return(XML_SCHEMAP_SRC_RESOLVE);
20451 }
20452 return(0);
20453}
20454
20455#define WXS_REDEFINED_TYPE(c) \
20456(((xmlSchemaTypePtr) item)->flags & XML_SCHEMAS_TYPE_REDEFINED)
20457
20458#define WXS_REDEFINED_MODEL_GROUP_DEF(c) \
20459(((xmlSchemaModelGroupDefPtr) item)->flags & XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20460
20461#define WXS_REDEFINED_ATTR_GROUP(c) \
20462(((xmlSchemaAttributeGroupPtr) item)->flags & XML_SCHEMAS_ATTRGROUP_REDEFINED)
20463
20464static int
20465xmlSchemaCheckSRCRedefineFirst(xmlSchemaParserCtxtPtr pctxt)
20466{
20467 int err = 0;
20468 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20469 xmlSchemaBasicItemPtr prev, item;
20470 int wasRedefined;
20471
20472 if (redef == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000020473 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020474
20475 do {
20476 item = redef->item;
20477 /*
20478 * First try to locate the redefined component in the
20479 * schema graph starting with the redefined schema.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020480 * NOTE: According to this schema bug entry:
20481 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005OctDec/0019.html
20482 * it's not clear if the referenced component needs to originate
20483 * from the <redefine>d schema _document_ or the schema; the latter
20484 * would include all imported and included sub-schemas of the
20485 * <redefine>d schema. Currenlty we latter approach is used.
20486 * SUPPLEMENT: It seems that the WG moves towards the latter
20487 * approach, so we are doing it right.
Daniel Veillarddee23482008-04-11 12:58:43 +000020488 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020489 */
20490 prev = xmlSchemaFindRedefCompInGraph(
20491 redef->targetBucket, item->type,
20492 redef->refName, redef->refTargetNs);
20493 if (prev == NULL) {
20494 xmlChar *str = NULL;
20495 xmlNodePtr node;
20496
20497 /*
20498 * SPEC src-redefine:
Jan Pokorný761c9e92013-11-29 23:26:27 +010020499 * (6.2.1) "The `actual value` of its own name attribute plus
20500 * target namespace must successfully `resolve` to a model
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020501 * group definition in I."
Jan Pokorný761c9e92013-11-29 23:26:27 +010020502 * (7.2.1) "The `actual value` of its own name attribute plus
20503 * target namespace must successfully `resolve` to an attribute
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020504 * group definition in I."
20505
20506 *
20507 * Note that, if we are redefining with the use of references
20508 * to components, the spec assumes the src-resolve to be used;
20509 * but this won't assure that we search only *inside* the
20510 * redefined schema.
20511 */
20512 if (redef->reference)
20513 node = WXS_ITEM_NODE(redef->reference);
20514 else
20515 node = WXS_ITEM_NODE(item);
20516 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20517 /*
20518 * TODO: error code.
20519 * Probably XML_SCHEMAP_SRC_RESOLVE, if this is using the
Daniel Veillarddee23482008-04-11 12:58:43 +000020520 * reference kind.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020521 */
20522 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
Kasimier T. Buchcik5d2998b2005-11-22 17:36:01 +000020523 "The %s '%s' to be redefined could not be found in "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020524 "the redefined schema",
20525 WXS_ITEM_TYPE_NAME(item),
20526 xmlSchemaFormatQName(&str, redef->refTargetNs,
20527 redef->refName));
Daniel Veillarddee23482008-04-11 12:58:43 +000020528 FREE_AND_NULL(str);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020529 err = pctxt->err;
20530 redef = redef->next;
20531 continue;
20532 }
20533 /*
20534 * TODO: Obtaining and setting the redefinition state is really
20535 * clumsy.
20536 */
20537 wasRedefined = 0;
20538 switch (item->type) {
20539 case XML_SCHEMA_TYPE_COMPLEX:
20540 case XML_SCHEMA_TYPE_SIMPLE:
20541 if ((WXS_TYPE_CAST prev)->flags &
20542 XML_SCHEMAS_TYPE_REDEFINED)
20543 {
20544 wasRedefined = 1;
20545 break;
20546 }
20547 /* Mark it as redefined. */
20548 (WXS_TYPE_CAST prev)->flags |= XML_SCHEMAS_TYPE_REDEFINED;
20549 /*
20550 * Assign the redefined type to the
20551 * base type of the redefining type.
20552 * TODO: How
20553 */
Daniel Veillarddee23482008-04-11 12:58:43 +000020554 ((xmlSchemaTypePtr) item)->baseType =
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020555 (xmlSchemaTypePtr) prev;
20556 break;
20557 case XML_SCHEMA_TYPE_GROUP:
20558 if ((WXS_MODEL_GROUPDEF_CAST prev)->flags &
20559 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20560 {
20561 wasRedefined = 1;
20562 break;
20563 }
20564 /* Mark it as redefined. */
20565 (WXS_MODEL_GROUPDEF_CAST prev)->flags |=
20566 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED;
20567 if (redef->reference != NULL) {
20568 /*
20569 * Overwrite the QName-reference with the
20570 * referenced model group def.
20571 */
20572 (WXS_PTC_CAST redef->reference)->children =
20573 WXS_TREE_CAST prev;
20574 }
20575 redef->target = prev;
20576 break;
20577 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20578 if ((WXS_ATTR_GROUP_CAST prev)->flags &
20579 XML_SCHEMAS_ATTRGROUP_REDEFINED)
20580 {
20581 wasRedefined = 1;
20582 break;
20583 }
20584 (WXS_ATTR_GROUP_CAST prev)->flags |=
20585 XML_SCHEMAS_ATTRGROUP_REDEFINED;
20586 if (redef->reference != NULL) {
20587 /*
20588 * Assign the redefined attribute group to the
20589 * QName-reference component.
20590 * This is the easy case, since we will just
20591 * expand the redefined group.
20592 */
20593 (WXS_QNAME_CAST redef->reference)->item = prev;
20594 redef->target = NULL;
20595 } else {
20596 /*
20597 * This is the complicated case: we need
20598 * to apply src-redefine (7.2.2) at a later
20599 * stage, i.e. when attribute group references
20600 * have beed expanded and simple types have
20601 * beed fixed.
20602 */
20603 redef->target = prev;
20604 }
20605 break;
20606 default:
20607 PERROR_INT("xmlSchemaResolveRedefReferences",
20608 "Unexpected redefined component type");
Daniel Veillarddee23482008-04-11 12:58:43 +000020609 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020610 }
20611 if (wasRedefined) {
20612 xmlChar *str = NULL;
20613 xmlNodePtr node;
20614
20615 if (redef->reference)
20616 node = WXS_ITEM_NODE(redef->reference);
20617 else
20618 node = WXS_ITEM_NODE(redef->item);
Daniel Veillarddee23482008-04-11 12:58:43 +000020619
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020620 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20621 /* TODO: error code. */
20622 XML_SCHEMAP_SRC_REDEFINE,
20623 node, NULL,
20624 "The referenced %s was already redefined. Multiple "
20625 "redefinition of the same component is not supported",
20626 xmlSchemaGetComponentDesignation(&str, prev),
20627 NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000020628 FREE_AND_NULL(str)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020629 err = pctxt->err;
20630 redef = redef->next;
20631 continue;
20632 }
Daniel Veillarddee23482008-04-11 12:58:43 +000020633 redef = redef->next;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020634 } while (redef != NULL);
20635
20636 return(err);
20637}
20638
20639static int
20640xmlSchemaCheckSRCRedefineSecond(xmlSchemaParserCtxtPtr pctxt)
20641{
20642 int err = 0;
20643 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20644 xmlSchemaBasicItemPtr item;
20645
20646 if (redef == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000020647 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020648
20649 do {
20650 if (redef->target == NULL) {
20651 redef = redef->next;
20652 continue;
20653 }
20654 item = redef->item;
Daniel Veillarddee23482008-04-11 12:58:43 +000020655
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020656 switch (item->type) {
20657 case XML_SCHEMA_TYPE_SIMPLE:
20658 case XML_SCHEMA_TYPE_COMPLEX:
20659 /*
20660 * Since the spec wants the {name} of the redefined
20661 * type to be 'absent', we'll NULL it.
20662 */
20663 (WXS_TYPE_CAST redef->target)->name = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000020664
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020665 /*
20666 * TODO: Seems like there's nothing more to do. The normal
20667 * inheritance mechanism is used. But not 100% sure.
20668 */
20669 break;
20670 case XML_SCHEMA_TYPE_GROUP:
20671 /*
20672 * URGENT TODO:
20673 * SPEC src-redefine:
20674 * (6.2.2) "The {model group} of the model group definition
20675 * which corresponds to it per XML Representation of Model
Jan Pokorný761c9e92013-11-29 23:26:27 +010020676 * Group Definition Schema Components ($3.7.2) must be a
20677 * `valid restriction` of the {model group} of that model
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020678 * group definition in I, as defined in Particle Valid
Jan Pokorný761c9e92013-11-29 23:26:27 +010020679 * (Restriction) ($3.9.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020680 */
20681 break;
20682 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20683 /*
20684 * SPEC src-redefine:
20685 * (7.2.2) "The {attribute uses} and {attribute wildcard} of
20686 * the attribute group definition which corresponds to it
20687 * per XML Representation of Attribute Group Definition Schema
Jan Pokorný761c9e92013-11-29 23:26:27 +010020688 * Components ($3.6.2) must be `valid restrictions` of the
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020689 * {attribute uses} and {attribute wildcard} of that attribute
20690 * group definition in I, as defined in clause 2, clause 3 and
20691 * clause 4 of Derivation Valid (Restriction, Complex)
Jan Pokorný761c9e92013-11-29 23:26:27 +010020692 * ($3.4.6) (where references to the base type definition are
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020693 * understood as references to the attribute group definition
20694 * in I)."
20695 */
20696 err = xmlSchemaCheckDerivationOKRestriction2to4(pctxt,
20697 XML_SCHEMA_ACTION_REDEFINE,
20698 item, redef->target,
20699 (WXS_ATTR_GROUP_CAST item)->attrUses,
20700 (WXS_ATTR_GROUP_CAST redef->target)->attrUses,
20701 (WXS_ATTR_GROUP_CAST item)->attributeWildcard,
20702 (WXS_ATTR_GROUP_CAST redef->target)->attributeWildcard);
20703 if (err == -1)
20704 return(-1);
20705 break;
20706 default:
20707 break;
20708 }
20709 redef = redef->next;
20710 } while (redef != NULL);
20711 return(0);
20712}
Daniel Veillarddee23482008-04-11 12:58:43 +000020713
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020714
20715static int
20716xmlSchemaAddComponents(xmlSchemaParserCtxtPtr pctxt,
20717 xmlSchemaBucketPtr bucket)
20718{
20719 xmlSchemaBasicItemPtr item;
20720 int err;
20721 xmlHashTablePtr *table;
20722 const xmlChar *name;
20723 int i;
20724
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020725#define WXS_GET_GLOBAL_HASH(c, slot) { \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020726 if (WXS_IS_BUCKET_IMPMAIN((c)->type)) \
20727 table = &(WXS_IMPBUCKET((c))->schema->slot); \
20728 else \
20729 table = &(WXS_INCBUCKET((c))->ownerImport->schema->slot); }
20730
20731 /*
20732 * Add global components to the schema's hash tables.
20733 * This is the place where duplicate components will be
20734 * detected.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020735 * TODO: I think normally we should support imports of the
20736 * same namespace from multiple locations. We don't do currently,
20737 * but if we do then according to:
20738 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2224
20739 * we would need, if imported directly, to import redefined
20740 * components as well to be able to catch clashing components.
20741 * (I hope I'll still know what this means after some months :-()
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020742 */
20743 if (bucket == NULL)
20744 return(-1);
20745 if (bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED)
20746 return(0);
Daniel Veillarddee23482008-04-11 12:58:43 +000020747 bucket->flags |= XML_SCHEMA_BUCKET_COMPS_ADDED;
20748
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020749 for (i = 0; i < bucket->globals->nbItems; i++) {
20750 item = bucket->globals->items[i];
20751 table = NULL;
20752 switch (item->type) {
20753 case XML_SCHEMA_TYPE_COMPLEX:
20754 case XML_SCHEMA_TYPE_SIMPLE:
20755 if (WXS_REDEFINED_TYPE(item))
20756 continue;
20757 name = (WXS_TYPE_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020758 WXS_GET_GLOBAL_HASH(bucket, typeDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020759 break;
20760 case XML_SCHEMA_TYPE_ELEMENT:
20761 name = (WXS_ELEM_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020762 WXS_GET_GLOBAL_HASH(bucket, elemDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020763 break;
20764 case XML_SCHEMA_TYPE_ATTRIBUTE:
20765 name = (WXS_ATTR_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020766 WXS_GET_GLOBAL_HASH(bucket, attrDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020767 break;
20768 case XML_SCHEMA_TYPE_GROUP:
20769 if (WXS_REDEFINED_MODEL_GROUP_DEF(item))
20770 continue;
20771 name = (WXS_MODEL_GROUPDEF_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020772 WXS_GET_GLOBAL_HASH(bucket, groupDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020773 break;
20774 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20775 if (WXS_REDEFINED_ATTR_GROUP(item))
20776 continue;
20777 name = (WXS_ATTR_GROUP_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020778 WXS_GET_GLOBAL_HASH(bucket, attrgrpDecl)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020779 break;
20780 case XML_SCHEMA_TYPE_IDC_KEY:
20781 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20782 case XML_SCHEMA_TYPE_IDC_KEYREF:
20783 name = (WXS_IDC_CAST item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020784 WXS_GET_GLOBAL_HASH(bucket, idcDef)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020785 break;
20786 case XML_SCHEMA_TYPE_NOTATION:
20787 name = ((xmlSchemaNotationPtr) item)->name;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020788 WXS_GET_GLOBAL_HASH(bucket, notaDecl)
Daniel Veillarddee23482008-04-11 12:58:43 +000020789 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020790 default:
20791 PERROR_INT("xmlSchemaAddComponents",
20792 "Unexpected global component type");
Daniel Veillarddee23482008-04-11 12:58:43 +000020793 continue;
20794 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020795 if (*table == NULL) {
20796 *table = xmlHashCreateDict(10, pctxt->dict);
20797 if (*table == NULL) {
20798 PERROR_INT("xmlSchemaAddComponents",
20799 "failed to create a component hash table");
20800 return(-1);
20801 }
Daniel Veillarddee23482008-04-11 12:58:43 +000020802 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020803 err = xmlHashAddEntry(*table, name, item);
Daniel Veillarddee23482008-04-11 12:58:43 +000020804 if (err != 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020805 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000020806
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020807 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20808 XML_SCHEMAP_REDEFINED_TYPE,
20809 WXS_ITEM_NODE(item),
20810 WXS_BASIC_CAST item,
20811 "A global %s '%s' does already exist",
20812 WXS_ITEM_TYPE_NAME(item),
20813 xmlSchemaGetComponentQName(&str, item));
Daniel Veillarddee23482008-04-11 12:58:43 +000020814 FREE_AND_NULL(str);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020815 }
20816 }
20817 /*
20818 * Process imported/included schemas.
20819 */
20820 if (bucket->relations != NULL) {
20821 xmlSchemaSchemaRelationPtr rel = bucket->relations;
20822 do {
20823 if ((rel->bucket != NULL) &&
20824 ((rel->bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED) == 0)) {
20825 if (xmlSchemaAddComponents(pctxt, rel->bucket) == -1)
20826 return(-1);
20827 }
20828 rel = rel->next;
20829 } while (rel != NULL);
20830 }
20831 return(0);
20832}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020833
20834static int
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020835xmlSchemaFixupComponents(xmlSchemaParserCtxtPtr pctxt,
20836 xmlSchemaBucketPtr rootBucket)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020837{
20838 xmlSchemaConstructionCtxtPtr con = pctxt->constructor;
20839 xmlSchemaTreeItemPtr item, *items;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020840 int nbItems, i, ret = 0;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020841 xmlSchemaBucketPtr oldbucket = con->bucket;
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000020842 xmlSchemaElementPtr elemDecl;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020843
20844#define FIXHFAILURE if (pctxt->err == XML_SCHEMAP_INTERNAL) goto exit_failure;
20845
20846 if ((con->pending == NULL) ||
20847 (con->pending->nbItems == 0))
Daniel Veillarddee23482008-04-11 12:58:43 +000020848 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020849
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020850 /*
20851 * Since xmlSchemaFixupComplexType() will create new particles
20852 * (local components), and those particle components need a bucket
20853 * on the constructor, we'll assure here that the constructor has
20854 * a bucket.
Daniel Veillarddee23482008-04-11 12:58:43 +000020855 * TODO: Think about storing locals _only_ on the main bucket.
20856 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020857 if (con->bucket == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000020858 con->bucket = rootBucket;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000020859
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020860 /* TODO:
20861 * SPEC (src-redefine):
20862 * (6.2) "If it has no such self-reference, then all of the
20863 * following must be true:"
Daniel Veillarddee23482008-04-11 12:58:43 +000020864
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020865 * (6.2.2) The {model group} of the model group definition which
20866 * corresponds to it per XML Representation of Model Group
Jan Pokorný761c9e92013-11-29 23:26:27 +010020867 * Definition Schema Components ($3.7.2) must be a `valid
20868 * restriction` of the {model group} of that model group definition
20869 * in I, as defined in Particle Valid (Restriction) ($3.9.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020870 */
20871 xmlSchemaCheckSRCRedefineFirst(pctxt);
20872
20873 /*
20874 * Add global components to the schemata's hash tables.
Daniel Veillarddee23482008-04-11 12:58:43 +000020875 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000020876 xmlSchemaAddComponents(pctxt, rootBucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020877
20878 pctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020879 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20880 nbItems = con->pending->nbItems;
20881 /*
20882 * Now that we have parsed *all* the schema document(s) and converted
20883 * them to schema components, we can resolve references, apply component
20884 * constraints, create the FSA from the content model, etc.
Daniel Veillarddee23482008-04-11 12:58:43 +000020885 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020886 /*
20887 * Resolve references of..
20888 *
20889 * 1. element declarations:
20890 * - the type definition
20891 * - the substitution group affiliation
20892 * 2. simple/complex types:
20893 * - the base type definition
20894 * - the memberTypes of union types
20895 * - the itemType of list types
20896 * 3. attributes declarations and attribute uses:
20897 * - the type definition
20898 * - if an attribute use, then the attribute declaration
20899 * 4. attribute group references:
20900 * - the attribute group definition
20901 * 5. particles:
20902 * - the term of the particle (e.g. a model group)
20903 * 6. IDC key-references:
20904 * - the referenced IDC 'key' or 'unique' definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020905 * 7. Attribute prohibitions which had a "ref" attribute.
Daniel Veillarddee23482008-04-11 12:58:43 +000020906 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020907 for (i = 0; i < nbItems; i++) {
20908 item = items[i];
20909 switch (item->type) {
20910 case XML_SCHEMA_TYPE_ELEMENT:
20911 xmlSchemaResolveElementReferences(
20912 (xmlSchemaElementPtr) item, pctxt);
20913 FIXHFAILURE;
20914 break;
20915 case XML_SCHEMA_TYPE_COMPLEX:
20916 case XML_SCHEMA_TYPE_SIMPLE:
20917 xmlSchemaResolveTypeReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020918 (xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020919 FIXHFAILURE;
20920 break;
20921 case XML_SCHEMA_TYPE_ATTRIBUTE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020922 xmlSchemaResolveAttrTypeReferences(
20923 (xmlSchemaAttributePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020924 FIXHFAILURE;
20925 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020926 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20927 xmlSchemaResolveAttrUseReferences(
20928 (xmlSchemaAttributeUsePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020929 FIXHFAILURE;
20930 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020931 case XML_SCHEMA_EXTRA_QNAMEREF:
Daniel Veillarddee23482008-04-11 12:58:43 +000020932 if ((WXS_QNAME_CAST item)->itemType ==
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020933 XML_SCHEMA_TYPE_ATTRIBUTEGROUP)
20934 {
20935 xmlSchemaResolveAttrGroupReferences(
20936 WXS_QNAME_CAST item, pctxt);
20937 }
20938 FIXHFAILURE;
20939 break;
20940 case XML_SCHEMA_TYPE_SEQUENCE:
20941 case XML_SCHEMA_TYPE_CHOICE:
20942 case XML_SCHEMA_TYPE_ALL:
20943 xmlSchemaResolveModelGroupParticleReferences(pctxt,
20944 WXS_MODEL_GROUP_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020945 FIXHFAILURE;
20946 break;
20947 case XML_SCHEMA_TYPE_IDC_KEY:
20948 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20949 case XML_SCHEMA_TYPE_IDC_KEYREF:
20950 xmlSchemaResolveIDCKeyReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020951 (xmlSchemaIDCPtr) item, pctxt);
20952 FIXHFAILURE;
20953 break;
20954 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
20955 /*
20956 * Handle attribue prohibition which had a
20957 * "ref" attribute.
20958 */
20959 xmlSchemaResolveAttrUseProhibReferences(
20960 WXS_ATTR_PROHIB_CAST item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020961 FIXHFAILURE;
20962 break;
20963 default:
20964 break;
20965 }
20966 }
20967 if (pctxt->nberrors != 0)
20968 goto exit_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000020969
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020970 /*
20971 * Now that all references are resolved we
20972 * can check for circularity of...
Daniel Veillarddee23482008-04-11 12:58:43 +000020973 * 1. the base axis of type definitions
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020974 * 2. nested model group definitions
20975 * 3. nested attribute group definitions
20976 * TODO: check for circual substitution groups.
20977 */
20978 for (i = 0; i < nbItems; i++) {
20979 item = items[i];
20980 /*
20981 * Let's better stop on the first error here.
20982 */
20983 switch (item->type) {
20984 case XML_SCHEMA_TYPE_COMPLEX:
20985 case XML_SCHEMA_TYPE_SIMPLE:
20986 xmlSchemaCheckTypeDefCircular(
20987 (xmlSchemaTypePtr) item, pctxt);
20988 FIXHFAILURE;
20989 if (pctxt->nberrors != 0)
20990 goto exit_error;
20991 break;
20992 case XML_SCHEMA_TYPE_GROUP:
20993 xmlSchemaCheckGroupDefCircular(
20994 (xmlSchemaModelGroupDefPtr) item, pctxt);
20995 FIXHFAILURE;
20996 if (pctxt->nberrors != 0)
20997 goto exit_error;
20998 break;
20999 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
21000 xmlSchemaCheckAttrGroupCircular(
21001 (xmlSchemaAttributeGroupPtr) item, pctxt);
21002 FIXHFAILURE;
21003 if (pctxt->nberrors != 0)
21004 goto exit_error;
21005 break;
21006 default:
21007 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000021008 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021009 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021010 if (pctxt->nberrors != 0)
21011 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021012 /*
21013 * Model group definition references:
21014 * Such a reference is reflected by a particle at the component
21015 * level. Until now the 'term' of such particles pointed
21016 * to the model group definition; this was done, in order to
21017 * ease circularity checks. Now we need to set the 'term' of
21018 * such particles to the model group of the model group definition.
21019 */
21020 for (i = 0; i < nbItems; i++) {
21021 item = items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021022 switch (item->type) {
21023 case XML_SCHEMA_TYPE_SEQUENCE:
Daniel Veillarddee23482008-04-11 12:58:43 +000021024 case XML_SCHEMA_TYPE_CHOICE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021025 xmlSchemaModelGroupToModelGroupDefFixup(pctxt,
21026 WXS_MODEL_GROUP_CAST item);
21027 break;
21028 default:
21029 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021030 }
21031 }
21032 if (pctxt->nberrors != 0)
21033 goto exit_error;
21034 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021035 * Expand attribute group references of attribute group definitions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021036 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021037 for (i = 0; i < nbItems; i++) {
21038 item = items[i];
21039 switch (item->type) {
21040 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
21041 if ((! WXS_ATTR_GROUP_EXPANDED(item)) &&
21042 WXS_ATTR_GROUP_HAS_REFS(item))
21043 {
21044 xmlSchemaAttributeGroupExpandRefs(pctxt,
21045 WXS_ATTR_GROUP_CAST item);
21046 FIXHFAILURE;
21047 }
21048 break;
21049 default:
21050 break;
21051 }
21052 }
21053 if (pctxt->nberrors != 0)
21054 goto exit_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000021055 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021056 * First compute the variety of simple types. This is needed as
21057 * a seperate step, since otherwise we won't be able to detect
21058 * circular union types in all cases.
21059 */
21060 for (i = 0; i < nbItems; i++) {
21061 item = items[i];
21062 switch (item->type) {
21063 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021064 if (WXS_IS_TYPE_NOT_FIXED_1((xmlSchemaTypePtr) item)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021065 xmlSchemaFixupSimpleTypeStageOne(pctxt,
21066 (xmlSchemaTypePtr) item);
21067 FIXHFAILURE;
21068 }
21069 break;
21070 default:
21071 break;
21072 }
21073 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021074 if (pctxt->nberrors != 0)
21075 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021076 /*
21077 * Detect circular union types. Note that this needs the variety to
21078 * be already computed.
21079 */
21080 for (i = 0; i < nbItems; i++) {
21081 item = items[i];
21082 switch (item->type) {
21083 case XML_SCHEMA_TYPE_SIMPLE:
21084 if (((xmlSchemaTypePtr) item)->memberTypes != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000021085 xmlSchemaCheckUnionTypeDefCircular(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021086 (xmlSchemaTypePtr) item);
21087 FIXHFAILURE;
21088 }
21089 break;
21090 default:
21091 break;
21092 }
21093 }
21094 if (pctxt->nberrors != 0)
21095 goto exit_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000021096
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021097 /*
21098 * Do the complete type fixup for simple types.
21099 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021100 for (i = 0; i < nbItems; i++) {
21101 item = items[i];
21102 switch (item->type) {
21103 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021104 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
21105 xmlSchemaFixupSimpleTypeStageTwo(pctxt, WXS_TYPE_CAST item);
21106 FIXHFAILURE;
21107 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021108 break;
21109 default:
21110 break;
21111 }
21112 }
21113 if (pctxt->nberrors != 0)
21114 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021115 /*
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021116 * At this point we need build and check all simple types.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021117 */
21118 /*
21119 * Apply contraints for attribute declarations.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021120 */
21121 for (i = 0; i < nbItems; i++) {
21122 item = items[i];
21123 switch (item->type) {
Daniel Veillarddee23482008-04-11 12:58:43 +000021124 case XML_SCHEMA_TYPE_ATTRIBUTE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021125 xmlSchemaCheckAttrPropsCorrect(pctxt, WXS_ATTR_CAST item);
21126 FIXHFAILURE;
Daniel Veillarddee23482008-04-11 12:58:43 +000021127 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021128 default:
21129 break;
21130 }
21131 }
21132 if (pctxt->nberrors != 0)
21133 goto exit_error;
21134 /*
21135 * Apply constraints for attribute uses.
Daniel Veillarddee23482008-04-11 12:58:43 +000021136 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021137 for (i = 0; i < nbItems; i++) {
21138 item = items[i];
21139 switch (item->type) {
21140 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
21141 if (((xmlSchemaAttributeUsePtr)item)->defValue != NULL) {
21142 xmlSchemaCheckAttrUsePropsCorrect(pctxt,
21143 WXS_ATTR_USE_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021144 FIXHFAILURE;
21145 }
21146 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021147 default:
21148 break;
21149 }
21150 }
21151 if (pctxt->nberrors != 0)
21152 goto exit_error;
21153
21154 /*
21155 * Apply constraints for attribute group definitions.
21156 */
21157 for (i = 0; i < nbItems; i++) {
21158 item = items[i];
21159 switch (item->type) {
21160 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
21161 if (( (WXS_ATTR_GROUP_CAST item)->attrUses != NULL) &&
21162 ( (WXS_LIST_CAST (WXS_ATTR_GROUP_CAST item)->attrUses)->nbItems > 1))
21163 {
21164 xmlSchemaCheckAGPropsCorrect(pctxt, WXS_ATTR_GROUP_CAST item);
21165 FIXHFAILURE;
21166 }
21167 break;
21168 default:
21169 break;
21170 }
21171 }
21172 if (pctxt->nberrors != 0)
21173 goto exit_error;
21174
21175 /*
21176 * Apply constraints for redefinitions.
21177 */
21178 if (WXS_CONSTRUCTOR(pctxt)->redefs != NULL)
21179 xmlSchemaCheckSRCRedefineSecond(pctxt);
21180 if (pctxt->nberrors != 0)
21181 goto exit_error;
21182
Daniel Veillarddee23482008-04-11 12:58:43 +000021183 /*
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021184 * Complex types are builded and checked.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021185 */
21186 for (i = 0; i < nbItems; i++) {
21187 item = con->pending->items[i];
21188 switch (item->type) {
21189 case XML_SCHEMA_TYPE_COMPLEX:
21190 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
21191 xmlSchemaFixupComplexType(pctxt, WXS_TYPE_CAST item);
21192 FIXHFAILURE;
21193 }
21194 break;
21195 default:
21196 break;
21197 }
21198 }
21199 if (pctxt->nberrors != 0)
21200 goto exit_error;
21201
21202 /*
21203 * The list could have changed, since xmlSchemaFixupComplexType()
21204 * will create particles and model groups in some cases.
21205 */
21206 items = (xmlSchemaTreeItemPtr *) con->pending->items;
Daniel Veillarddee23482008-04-11 12:58:43 +000021207 nbItems = con->pending->nbItems;
21208
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021209 /*
21210 * Apply some constraints for element declarations.
21211 */
21212 for (i = 0; i < nbItems; i++) {
21213 item = items[i];
21214 switch (item->type) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021215 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021216 elemDecl = (xmlSchemaElementPtr) item;
Daniel Veillarddee23482008-04-11 12:58:43 +000021217
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021218 if ((elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_CHECKED) == 0)
21219 {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021220 xmlSchemaCheckElementDeclComponent(
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021221 (xmlSchemaElementPtr) elemDecl, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021222 FIXHFAILURE;
21223 }
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000021224
21225#ifdef WXS_ELEM_DECL_CONS_ENABLED
21226 /*
21227 * Schema Component Constraint: Element Declarations Consistent
21228 * Apply this constraint to local types of element declarations.
21229 */
21230 if ((WXS_ELEM_TYPEDEF(elemDecl) != NULL) &&
21231 (WXS_IS_COMPLEX(WXS_ELEM_TYPEDEF(elemDecl))) &&
21232 (WXS_TYPE_IS_LOCAL(WXS_ELEM_TYPEDEF(elemDecl))))
21233 {
21234 xmlSchemaCheckElementDeclConsistent(pctxt,
21235 WXS_BASIC_CAST elemDecl,
21236 WXS_TYPE_PARTICLE(WXS_ELEM_TYPEDEF(elemDecl)),
21237 NULL, NULL, 0);
21238 }
21239#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021240 break;
21241 default:
21242 break;
21243 }
21244 }
21245 if (pctxt->nberrors != 0)
21246 goto exit_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000021247
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021248 /*
21249 * Finally we can build the automaton from the content model of
21250 * complex types.
21251 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021252
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021253 for (i = 0; i < nbItems; i++) {
21254 item = items[i];
21255 switch (item->type) {
21256 case XML_SCHEMA_TYPE_COMPLEX:
21257 xmlSchemaBuildContentModel((xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021258 /* FIXHFAILURE; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021259 break;
21260 default:
21261 break;
21262 }
21263 }
21264 if (pctxt->nberrors != 0)
21265 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021266 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021267 * URGENT TODO: cos-element-consistent
Daniel Veillarddee23482008-04-11 12:58:43 +000021268 */
21269 goto exit;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021270
Daniel Veillarddee23482008-04-11 12:58:43 +000021271exit_error:
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021272 ret = pctxt->err;
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000021273 goto exit;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021274
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021275exit_failure:
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021276 ret = -1;
21277
21278exit:
21279 /*
21280 * Reset the constructor. This is needed for XSI acquisition, since
21281 * those items will be processed over and over again for every XSI
21282 * if not cleared here.
21283 */
Kasimier T. Buchcik4c0aa162006-02-20 14:47:08 +000021284 con->bucket = oldbucket;
Daniel Veillarddee23482008-04-11 12:58:43 +000021285 con->pending->nbItems = 0;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021286 if (con->substGroups != NULL) {
21287 xmlHashFree(con->substGroups,
21288 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
21289 con->substGroups = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000021290 }
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021291 if (con->redefs != NULL) {
21292 xmlSchemaRedefListFree(con->redefs);
21293 con->redefs = NULL;
21294 }
21295 return(ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021296}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021297/**
Daniel Veillard4255d502002-04-16 15:50:10 +000021298 * xmlSchemaParse:
21299 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021300 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021301 * parse a schema definition resource and build an internal
Daniel Veillard4255d502002-04-16 15:50:10 +000021302 * XML Shema struture which can be used to validate instances.
Daniel Veillard4255d502002-04-16 15:50:10 +000021303 *
21304 * Returns the internal XML Schema structure built from the resource or
21305 * NULL in case of error
21306 */
21307xmlSchemaPtr
21308xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt)
21309{
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021310 xmlSchemaPtr mainSchema = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021311 xmlSchemaBucketPtr bucket = NULL;
21312 int res;
Daniel Veillard4255d502002-04-16 15:50:10 +000021313
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021314 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021315 * This one is used if the schema to be parsed was specified via
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021316 * the API; i.e. not automatically by the validated instance document.
21317 */
21318
Daniel Veillard4255d502002-04-16 15:50:10 +000021319 xmlSchemaInitTypes();
21320
Daniel Veillard6045c902002-10-09 21:13:59 +000021321 if (ctxt == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +000021322 return (NULL);
21323
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021324 /* TODO: Init the context. Is this all we need?*/
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021325 ctxt->nberrors = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021326 ctxt->err = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000021327 ctxt->counter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000021328
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021329 /* Create the *main* schema. */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021330 mainSchema = xmlSchemaNewSchema(ctxt);
21331 if (mainSchema == NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021332 goto exit_failure;
Daniel Veillard4255d502002-04-16 15:50:10 +000021333 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021334 * Create the schema constructor.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021335 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021336 if (ctxt->constructor == NULL) {
21337 ctxt->constructor = xmlSchemaConstructionCtxtCreate(ctxt->dict);
21338 if (ctxt->constructor == NULL)
21339 return(NULL);
21340 /* Take ownership of the constructor to be able to free it. */
21341 ctxt->ownsConstructor = 1;
21342 }
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021343 ctxt->constructor->mainSchema = mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021344 /*
21345 * Locate and add the schema document.
21346 */
21347 res = xmlSchemaAddSchemaDoc(ctxt, XML_SCHEMA_SCHEMA_MAIN,
21348 ctxt->URL, ctxt->doc, ctxt->buffer, ctxt->size, NULL,
21349 NULL, NULL, &bucket);
21350 if (res == -1)
21351 goto exit_failure;
21352 if (res != 0)
Daniel Veillarddee23482008-04-11 12:58:43 +000021353 goto exit;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021354
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021355 if (bucket == NULL) {
21356 /* TODO: Error code, actually we failed to *locate* the schema. */
Daniel Veillarddee23482008-04-11 12:58:43 +000021357 if (ctxt->URL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021358 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21359 NULL, NULL,
21360 "Failed to locate the main schema resource at '%s'",
21361 ctxt->URL, NULL);
21362 else
21363 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21364 NULL, NULL,
21365 "Failed to locate the main schema resource",
21366 NULL, NULL);
21367 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000021368 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021369 /* Then do the parsing for good. */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021370 if (xmlSchemaParseNewDocWithContext(ctxt, mainSchema, bucket) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021371 goto exit_failure;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021372 if (ctxt->nberrors != 0)
21373 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000021374
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021375 mainSchema->doc = bucket->doc;
21376 mainSchema->preserve = ctxt->preserve;
Daniel Veillarddee23482008-04-11 12:58:43 +000021377
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021378 ctxt->schema = mainSchema;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021379
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021380 if (xmlSchemaFixupComponents(ctxt, WXS_CONSTRUCTOR(ctxt)->mainBucket) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021381 goto exit_failure;
21382
21383 /*
21384 * TODO: This is not nice, since we cannot distinguish from the
21385 * result if there was an internal error or not.
21386 */
Daniel Veillarddee23482008-04-11 12:58:43 +000021387exit:
21388 if (ctxt->nberrors != 0) {
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021389 if (mainSchema) {
21390 xmlSchemaFree(mainSchema);
21391 mainSchema = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021392 }
21393 if (ctxt->constructor) {
21394 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21395 ctxt->constructor = NULL;
21396 ctxt->ownsConstructor = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021397 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021398 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021399 ctxt->schema = NULL;
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021400 return(mainSchema);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021401exit_failure:
Daniel Veillarddee23482008-04-11 12:58:43 +000021402 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021403 * Quite verbose, but should catch internal errors, which were
21404 * not communitated.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021405 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021406 if (mainSchema) {
21407 xmlSchemaFree(mainSchema);
21408 mainSchema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021409 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021410 if (ctxt->constructor) {
21411 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21412 ctxt->constructor = NULL;
21413 ctxt->ownsConstructor = 0;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021414 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021415 PERROR_INT2("xmlSchemaParse",
Daniel Veillarddee23482008-04-11 12:58:43 +000021416 "An internal error occured");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021417 ctxt->schema = NULL;
21418 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000021419}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021420
Daniel Veillard4255d502002-04-16 15:50:10 +000021421/**
Daniel Veillard01c13b52002-12-10 15:19:08 +000021422 * xmlSchemaSetParserErrors:
Daniel Veillard4255d502002-04-16 15:50:10 +000021423 * @ctxt: a schema validation context
Daniel Veillard01c13b52002-12-10 15:19:08 +000021424 * @err: the error callback
21425 * @warn: the warning callback
21426 * @ctx: contextual data for the callbacks
Daniel Veillard4255d502002-04-16 15:50:10 +000021427 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021428 * Set the callback functions used to handle errors for a validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021429 */
21430void
21431xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021432 xmlSchemaValidityErrorFunc err,
21433 xmlSchemaValidityWarningFunc warn, void *ctx)
21434{
Daniel Veillard4255d502002-04-16 15:50:10 +000021435 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021436 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000021437 ctxt->error = err;
21438 ctxt->warning = warn;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021439 ctxt->errCtxt = ctx;
21440 if (ctxt->vctxt != NULL)
21441 xmlSchemaSetValidErrors(ctxt->vctxt, err, warn, ctx);
21442}
21443
21444/**
21445 * xmlSchemaSetParserStructuredErrors:
21446 * @ctxt: a schema parser context
21447 * @serror: the structured error function
21448 * @ctx: the functions context
21449 *
21450 * Set the structured error callback
21451 */
21452void
21453xmlSchemaSetParserStructuredErrors(xmlSchemaParserCtxtPtr ctxt,
21454 xmlStructuredErrorFunc serror,
21455 void *ctx)
21456{
21457 if (ctxt == NULL)
21458 return;
21459 ctxt->serror = serror;
21460 ctxt->errCtxt = ctx;
21461 if (ctxt->vctxt != NULL)
21462 xmlSchemaSetValidStructuredErrors(ctxt->vctxt, serror, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000021463}
21464
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021465/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000021466 * xmlSchemaGetParserErrors:
21467 * @ctxt: a XMl-Schema parser context
21468 * @err: the error callback result
21469 * @warn: the warning callback result
21470 * @ctx: contextual data for the callbacks result
21471 *
21472 * Get the callback information used to handle errors for a parser context
21473 *
21474 * Returns -1 in case of failure, 0 otherwise
21475 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021476int
Daniel Veillard259f0df2004-08-18 09:13:18 +000021477xmlSchemaGetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021478 xmlSchemaValidityErrorFunc * err,
21479 xmlSchemaValidityWarningFunc * warn, void **ctx)
Daniel Veillard259f0df2004-08-18 09:13:18 +000021480{
21481 if (ctxt == NULL)
21482 return(-1);
21483 if (err != NULL)
21484 *err = ctxt->error;
21485 if (warn != NULL)
21486 *warn = ctxt->warning;
21487 if (ctx != NULL)
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021488 *ctx = ctxt->errCtxt;
Daniel Veillard259f0df2004-08-18 09:13:18 +000021489 return(0);
21490}
21491
21492/**
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021493 * xmlSchemaFacetTypeToString:
21494 * @type: the facet type
21495 *
21496 * Convert the xmlSchemaTypeType to a char string.
21497 *
21498 * Returns the char string representation of the facet type if the
21499 * type is a facet and an "Internal Error" string otherwise.
21500 */
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021501static const xmlChar *
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021502xmlSchemaFacetTypeToString(xmlSchemaTypeType type)
21503{
21504 switch (type) {
21505 case XML_SCHEMA_FACET_PATTERN:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021506 return (BAD_CAST "pattern");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021507 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021508 return (BAD_CAST "maxExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021509 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021510 return (BAD_CAST "maxInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021511 case XML_SCHEMA_FACET_MINEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021512 return (BAD_CAST "minExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021513 case XML_SCHEMA_FACET_MININCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021514 return (BAD_CAST "minInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021515 case XML_SCHEMA_FACET_WHITESPACE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021516 return (BAD_CAST "whiteSpace");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021517 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021518 return (BAD_CAST "enumeration");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021519 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021520 return (BAD_CAST "length");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021521 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021522 return (BAD_CAST "maxLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021523 case XML_SCHEMA_FACET_MINLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021524 return (BAD_CAST "minLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021525 case XML_SCHEMA_FACET_TOTALDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021526 return (BAD_CAST "totalDigits");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021527 case XML_SCHEMA_FACET_FRACTIONDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021528 return (BAD_CAST "fractionDigits");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021529 default:
21530 break;
21531 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021532 return (BAD_CAST "Internal Error");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021533}
21534
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021535static xmlSchemaWhitespaceValueType
Daniel Veillardc0826a72004-08-10 14:17:33 +000021536xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type)
21537{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021538 /*
21539 * The normalization type can be changed only for types which are derived
Daniel Veillardc0826a72004-08-10 14:17:33 +000021540 * from xsd:string.
21541 */
21542 if (type->type == XML_SCHEMA_TYPE_BASIC) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021543 /*
21544 * Note that we assume a whitespace of preserve for anySimpleType.
21545 */
21546 if ((type->builtInType == XML_SCHEMAS_STRING) ||
21547 (type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
21548 return(XML_SCHEMA_WHITESPACE_PRESERVE);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +000021549 else if (type->builtInType == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021550 return(XML_SCHEMA_WHITESPACE_REPLACE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021551 else {
21552 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010021553 * For all `atomic` datatypes other than string (and types `derived`
21554 * by `restriction` from it) the value of whiteSpace is fixed to
Daniel Veillardc0826a72004-08-10 14:17:33 +000021555 * collapse
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021556 * Note that this includes built-in list datatypes.
Daniel Veillardc0826a72004-08-10 14:17:33 +000021557 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021558 return(XML_SCHEMA_WHITESPACE_COLLAPSE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021559 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021560 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021561 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021562 * For list types the facet "whiteSpace" is fixed to "collapse".
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021563 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021564 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021565 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021566 return (XML_SCHEMA_WHITESPACE_UNKNOWN);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021567 } else if (WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021568 if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE)
21569 return (XML_SCHEMA_WHITESPACE_PRESERVE);
21570 else if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_REPLACE)
21571 return (XML_SCHEMA_WHITESPACE_REPLACE);
21572 else
21573 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021574 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021575 return (-1);
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021576}
21577
Daniel Veillard4255d502002-04-16 15:50:10 +000021578/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080021579 * *
21580 * Simple type validation *
21581 * *
Daniel Veillard4255d502002-04-16 15:50:10 +000021582 ************************************************************************/
Daniel Veillard377e1a92004-04-16 16:30:05 +000021583
Daniel Veillard4255d502002-04-16 15:50:10 +000021584
21585/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080021586 * *
21587 * DOM Validation code *
21588 * *
Daniel Veillard4255d502002-04-16 15:50:10 +000021589 ************************************************************************/
21590
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021591/**
21592 * xmlSchemaAssembleByLocation:
21593 * @pctxt: a schema parser context
21594 * @vctxt: a schema validation context
21595 * @schema: the existing schema
21596 * @node: the node that fired the assembling
21597 * @nsName: the namespace name of the new schema
21598 * @location: the location of the schema
21599 *
21600 * Expands an existing schema by an additional schema.
21601 *
21602 * Returns 0 if the new schema is correct, a positive error code
21603 * number otherwise and -1 in case of an internal or API error.
21604 */
21605static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021606xmlSchemaAssembleByLocation(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021607 xmlSchemaPtr schema,
21608 xmlNodePtr node,
21609 const xmlChar *nsName,
21610 const xmlChar *location)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021611{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021612 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021613 xmlSchemaParserCtxtPtr pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021614 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021615
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021616 if ((vctxt == NULL) || (schema == NULL))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021617 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021618
21619 if (vctxt->pctxt == NULL) {
21620 VERROR_INT("xmlSchemaAssembleByLocation",
21621 "no parser context available");
21622 return(-1);
21623 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021624 pctxt = vctxt->pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021625 if (pctxt->constructor == NULL) {
21626 PERROR_INT("xmlSchemaAssembleByLocation",
21627 "no constructor");
21628 return(-1);
21629 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021630 /*
21631 * Acquire the schema document.
21632 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021633 location = xmlSchemaBuildAbsoluteURI(pctxt->dict,
21634 location, node);
21635 /*
21636 * Note that we pass XML_SCHEMA_SCHEMA_IMPORT here;
21637 * the process will automatically change this to
21638 * XML_SCHEMA_SCHEMA_MAIN if it is the first schema document.
21639 */
21640 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
Daniel Veillarddee23482008-04-11 12:58:43 +000021641 location, NULL, NULL, 0, node, NULL, nsName,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021642 &bucket);
21643 if (ret != 0)
Daniel Veillarddee23482008-04-11 12:58:43 +000021644 return(ret);
21645 if (bucket == NULL) {
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021646 /*
21647 * Generate a warning that the document could not be located.
21648 */
21649 xmlSchemaCustomWarning(ACTXT_CAST vctxt, XML_SCHEMAV_MISC,
21650 node, NULL,
21651 "The document at location '%s' could not be acquired",
21652 location, NULL, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021653 return(ret);
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021654 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021655 /*
21656 * The first located schema will be handled as if all other
21657 * schemas imported by XSI were imported by this first schema.
21658 */
21659 if ((bucket != NULL) &&
21660 (WXS_CONSTRUCTOR(pctxt)->bucket == NULL))
21661 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
21662 /*
21663 * TODO: Is this handled like an import? I.e. is it not an error
21664 * if the schema cannot be located?
21665 */
21666 if ((bucket == NULL) || (! CAN_PARSE_SCHEMA(bucket)))
21667 return(0);
21668 /*
21669 * We will reuse the parser context for every schema imported
21670 * directly via XSI. So reset the context.
21671 */
21672 pctxt->nberrors = 0;
21673 pctxt->err = 0;
21674 pctxt->doc = bucket->doc;
Daniel Veillarddee23482008-04-11 12:58:43 +000021675
21676 ret = xmlSchemaParseNewDocWithContext(pctxt, schema, bucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021677 if (ret == -1) {
21678 pctxt->doc = NULL;
21679 goto exit_failure;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021680 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021681 /* Paranoid error channelling. */
21682 if ((ret == 0) && (pctxt->nberrors != 0))
Daniel Veillarddee23482008-04-11 12:58:43 +000021683 ret = pctxt->err;
21684 if (pctxt->nberrors == 0) {
21685 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021686 * Only bother to fixup pending components, if there was
21687 * no error yet.
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021688 * For every XSI acquired schema (and its sub-schemata) we will
21689 * fixup the components.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021690 */
Kasimier T. Buchcik1869be52006-02-20 13:37:55 +000021691 xmlSchemaFixupComponents(pctxt, bucket);
Kasimier T. Buchcik215406f2005-12-07 12:14:09 +000021692 ret = pctxt->err;
21693 /*
21694 * Not nice, but we need somehow to channel the schema parser
21695 * error to the validation context.
21696 */
21697 if ((ret != 0) && (vctxt->err == 0))
21698 vctxt->err = ret;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021699 vctxt->nberrors += pctxt->nberrors;
21700 } else {
Daniel Veillarddee23482008-04-11 12:58:43 +000021701 /* Add to validation error sum. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021702 vctxt->nberrors += pctxt->nberrors;
21703 }
21704 pctxt->doc = NULL;
21705 return(ret);
21706exit_failure:
21707 pctxt->doc = NULL;
21708 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021709}
21710
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021711static xmlSchemaAttrInfoPtr
Daniel Veillarddee23482008-04-11 12:58:43 +000021712xmlSchemaGetMetaAttrInfo(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021713 int metaType)
21714{
21715 if (vctxt->nbAttrInfos == 0)
21716 return (NULL);
21717 {
21718 int i;
21719 xmlSchemaAttrInfoPtr iattr;
21720
21721 for (i = 0; i < vctxt->nbAttrInfos; i++) {
21722 iattr = vctxt->attrInfos[i];
21723 if (iattr->metaType == metaType)
21724 return (iattr);
21725 }
21726
21727 }
21728 return (NULL);
21729}
21730
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021731/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021732 * xmlSchemaAssembleByXSI:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021733 * @vctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021734 *
21735 * Expands an existing schema by an additional schema using
21736 * the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute
21737 * of an instance. If xsi:noNamespaceSchemaLocation is used, @noNamespace
21738 * must be set to 1.
21739 *
21740 * Returns 0 if the new schema is correct, a positive error code
21741 * number otherwise and -1 in case of an internal or API error.
21742 */
21743static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021744xmlSchemaAssembleByXSI(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021745{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021746 const xmlChar *cur, *end;
21747 const xmlChar *nsname = NULL, *location;
21748 int count = 0;
21749 int ret = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021750 xmlSchemaAttrInfoPtr iattr;
21751
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021752 /*
21753 * Parse the value; we will assume an even number of values
21754 * to be given (this is how Xerces and XSV work).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021755 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021756 * URGENT TODO: !! This needs to work for both
21757 * @noNamespaceSchemaLocation AND @schemaLocation on the same
21758 * element !!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021759 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021760 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
21761 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC);
21762 if (iattr == NULL)
Kasimier T. Buchcik81755ea2005-11-23 16:39:27 +000021763 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021764 XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC);
21765 if (iattr == NULL)
21766 return (0);
21767 cur = iattr->value;
21768 do {
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021769 /*
21770 * TODO: Move the string parsing mechanism away from here.
21771 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021772 if (iattr->metaType == XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021773 /*
21774 * Get the namespace name.
21775 */
21776 while (IS_BLANK_CH(*cur))
21777 cur++;
21778 end = cur;
21779 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21780 end++;
21781 if (end == cur)
21782 break;
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021783 count++; /* TODO: Don't use the schema's dict. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021784 nsname = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021785 cur = end;
21786 }
21787 /*
21788 * Get the URI.
21789 */
21790 while (IS_BLANK_CH(*cur))
21791 cur++;
21792 end = cur;
21793 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21794 end++;
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021795 if (end == cur) {
21796 if (iattr->metaType ==
21797 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC)
21798 {
21799 /*
21800 * If using @schemaLocation then tuples are expected.
21801 * I.e. the namespace name *and* the document's URI.
Daniel Veillarddee23482008-04-11 12:58:43 +000021802 */
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021803 xmlSchemaCustomWarning(ACTXT_CAST vctxt, XML_SCHEMAV_MISC,
21804 iattr->node, NULL,
21805 "The value must consist of tuples: the target namespace "
21806 "name and the document's URI", NULL, NULL, NULL);
21807 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021808 break;
Kasimier T. Buchcike5f810f2006-05-30 09:41:25 +000021809 }
21810 count++; /* TODO: Don't use the schema's dict. */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021811 location = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021812 cur = end;
21813 ret = xmlSchemaAssembleByLocation(vctxt, vctxt->schema,
21814 iattr->node, nsname, location);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021815 if (ret == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021816 VERROR_INT("xmlSchemaAssembleByXSI",
21817 "assembling schemata");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021818 return (-1);
21819 }
21820 } while (*cur != 0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021821 return (ret);
21822}
21823
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021824static const xmlChar *
21825xmlSchemaLookupNamespace(xmlSchemaValidCtxtPtr vctxt,
21826 const xmlChar *prefix)
21827{
21828 if (vctxt->sax != NULL) {
21829 int i, j;
21830 xmlSchemaNodeInfoPtr inode;
Daniel Veillarddee23482008-04-11 12:58:43 +000021831
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021832 for (i = vctxt->depth; i >= 0; i--) {
21833 if (vctxt->elemInfos[i]->nbNsBindings != 0) {
21834 inode = vctxt->elemInfos[i];
21835 for (j = 0; j < inode->nbNsBindings * 2; j += 2) {
21836 if (((prefix == NULL) &&
21837 (inode->nsBindings[j] == NULL)) ||
21838 ((prefix != NULL) && xmlStrEqual(prefix,
21839 inode->nsBindings[j]))) {
21840
21841 /*
21842 * Note that the namespace bindings are already
21843 * in a string dict.
21844 */
Daniel Veillarddee23482008-04-11 12:58:43 +000021845 return (inode->nsBindings[j+1]);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021846 }
21847 }
21848 }
21849 }
21850 return (NULL);
Daniel Veillard438ebbd2008-05-12 12:58:46 +000021851#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021852 } else if (vctxt->reader != NULL) {
21853 xmlChar *nsName;
Daniel Veillarddee23482008-04-11 12:58:43 +000021854
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021855 nsName = xmlTextReaderLookupNamespace(vctxt->reader, prefix);
21856 if (nsName != NULL) {
21857 const xmlChar *ret;
21858
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021859 ret = xmlDictLookup(vctxt->dict, nsName, -1);
21860 xmlFree(nsName);
21861 return (ret);
21862 } else
21863 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021864#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021865 } else {
21866 xmlNsPtr ns;
21867
21868 if ((vctxt->inode->node == NULL) ||
21869 (vctxt->inode->node->doc == NULL)) {
21870 VERROR_INT("xmlSchemaLookupNamespace",
21871 "no node or node's doc avaliable");
21872 return (NULL);
21873 }
21874 ns = xmlSearchNs(vctxt->inode->node->doc,
21875 vctxt->inode->node, prefix);
21876 if (ns != NULL)
21877 return (ns->href);
21878 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021879 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021880}
21881
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021882/*
21883* This one works on the schema of the validation context.
21884*/
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021885static int
Daniel Veillarddee23482008-04-11 12:58:43 +000021886xmlSchemaValidateNotation(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021887 xmlSchemaPtr schema,
21888 xmlNodePtr node,
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021889 const xmlChar *value,
21890 xmlSchemaValPtr *val,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021891 int valNeeded)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021892{
21893 int ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021894
21895 if (vctxt && (vctxt->schema == NULL)) {
21896 VERROR_INT("xmlSchemaValidateNotation",
21897 "a schema is needed on the validation context");
21898 return (-1);
21899 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021900 ret = xmlValidateQName(value, 1);
21901 if (ret != 0)
21902 return (ret);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021903 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021904 xmlChar *localName = NULL;
21905 xmlChar *prefix = NULL;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021906
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021907 localName = xmlSplitQName2(value, &prefix);
21908 if (prefix != NULL) {
21909 const xmlChar *nsName = NULL;
21910
Daniel Veillarddee23482008-04-11 12:58:43 +000021911 if (vctxt != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021912 nsName = xmlSchemaLookupNamespace(vctxt, BAD_CAST prefix);
21913 else if (node != NULL) {
21914 xmlNsPtr ns = xmlSearchNs(node->doc, node, prefix);
21915 if (ns != NULL)
21916 nsName = ns->href;
21917 } else {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021918 xmlFree(prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021919 xmlFree(localName);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021920 return (1);
21921 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021922 if (nsName == NULL) {
21923 xmlFree(prefix);
21924 xmlFree(localName);
21925 return (1);
21926 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021927 if (xmlSchemaGetNotation(schema, localName, nsName) != NULL) {
Daniel Veillard77fbc3e2008-03-24 14:06:19 +000021928 if ((valNeeded) && (val != NULL)) {
21929 (*val) = xmlSchemaNewNOTATIONValue(xmlStrdup(localName),
21930 xmlStrdup(nsName));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021931 if (*val == NULL)
21932 ret = -1;
21933 }
21934 } else
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021935 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021936 xmlFree(prefix);
21937 xmlFree(localName);
21938 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021939 if (xmlSchemaGetNotation(schema, value, NULL) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021940 if (valNeeded && (val != NULL)) {
21941 (*val) = xmlSchemaNewNOTATIONValue(
21942 BAD_CAST xmlStrdup(value), NULL);
21943 if (*val == NULL)
21944 ret = -1;
21945 }
21946 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021947 return (1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021948 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021949 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021950 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021951}
21952
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000021953static int
21954xmlSchemaVAddNodeQName(xmlSchemaValidCtxtPtr vctxt,
21955 const xmlChar* lname,
21956 const xmlChar* nsname)
21957{
21958 int i;
21959
21960 lname = xmlDictLookup(vctxt->dict, lname, -1);
21961 if (lname == NULL)
21962 return(-1);
21963 if (nsname != NULL) {
21964 nsname = xmlDictLookup(vctxt->dict, nsname, -1);
21965 if (nsname == NULL)
21966 return(-1);
21967 }
21968 for (i = 0; i < vctxt->nodeQNames->nbItems; i += 2) {
21969 if ((vctxt->nodeQNames->items [i] == lname) &&
21970 (vctxt->nodeQNames->items[i +1] == nsname))
21971 /* Already there */
21972 return(i);
21973 }
21974 /* Add new entry. */
21975 i = vctxt->nodeQNames->nbItems;
21976 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) lname);
21977 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) nsname);
21978 return(i);
21979}
21980
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021981/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080021982 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021983 * Validation of identity-constraints (IDC) *
Daniel Veillardf8e3db02012-09-11 13:26:36 +080021984 * *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021985 ************************************************************************/
21986
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021987/**
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021988 * xmlSchemaAugmentIDC:
21989 * @idcDef: the IDC definition
21990 *
21991 * Creates an augmented IDC definition item.
21992 *
21993 * Returns the item, or NULL on internal errors.
21994 */
21995static void
21996xmlSchemaAugmentIDC(xmlSchemaIDCPtr idcDef,
21997 xmlSchemaValidCtxtPtr vctxt)
21998{
21999 xmlSchemaIDCAugPtr aidc;
22000
22001 aidc = (xmlSchemaIDCAugPtr) xmlMalloc(sizeof(xmlSchemaIDCAug));
22002 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022003 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022004 "xmlSchemaAugmentIDC: allocating an augmented IDC definition",
22005 NULL);
22006 return;
22007 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022008 aidc->keyrefDepth = -1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022009 aidc->def = idcDef;
22010 aidc->next = NULL;
22011 if (vctxt->aidcs == NULL)
22012 vctxt->aidcs = aidc;
22013 else {
22014 aidc->next = vctxt->aidcs;
22015 vctxt->aidcs = aidc;
22016 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022017 /*
22018 * Save if we have keyrefs at all.
22019 */
22020 if ((vctxt->hasKeyrefs == 0) &&
22021 (idcDef->type == XML_SCHEMA_TYPE_IDC_KEYREF))
22022 vctxt->hasKeyrefs = 1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022023}
22024
22025/**
Daniel Veillard3888f472007-08-23 09:29:03 +000022026 * xmlSchemaAugmentImportedIDC:
22027 * @imported: the imported schema
22028 *
22029 * Creates an augmented IDC definition for the imported schema.
22030 */
22031static void
22032xmlSchemaAugmentImportedIDC(xmlSchemaImportPtr imported, xmlSchemaValidCtxtPtr vctxt) {
22033 if (imported->schema->idcDef != NULL) {
22034 xmlHashScan(imported->schema->idcDef ,
22035 (xmlHashScanner) xmlSchemaAugmentIDC, vctxt);
22036 }
22037}
22038
22039/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022040 * xmlSchemaIDCNewBinding:
22041 * @idcDef: the IDC definition of this binding
22042 *
22043 * Creates a new IDC binding.
22044 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022045 * Returns the new IDC binding, NULL on internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022046 */
22047static xmlSchemaPSVIIDCBindingPtr
22048xmlSchemaIDCNewBinding(xmlSchemaIDCPtr idcDef)
22049{
22050 xmlSchemaPSVIIDCBindingPtr ret;
22051
22052 ret = (xmlSchemaPSVIIDCBindingPtr) xmlMalloc(
22053 sizeof(xmlSchemaPSVIIDCBinding));
22054 if (ret == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022055 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022056 "allocating a PSVI IDC binding item", NULL);
22057 return (NULL);
22058 }
22059 memset(ret, 0, sizeof(xmlSchemaPSVIIDCBinding));
22060 ret->definition = idcDef;
22061 return (ret);
22062}
22063
22064/**
22065 * xmlSchemaIDCStoreNodeTableItem:
22066 * @vctxt: the WXS validation context
22067 * @item: the IDC node table item
22068 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022069 * The validation context is used to store IDC node table items.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022070 * They are stored to avoid copying them if IDC node-tables are merged
22071 * with corresponding parent IDC node-tables (bubbling).
22072 *
22073 * Returns 0 if succeeded, -1 on internal errors.
22074 */
22075static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022076xmlSchemaIDCStoreNodeTableItem(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022077 xmlSchemaPSVIIDCNodePtr item)
22078{
22079 /*
22080 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022081 */
22082 if (vctxt->idcNodes == NULL) {
22083 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022084 xmlMalloc(20 * sizeof(xmlSchemaPSVIIDCNodePtr));
22085 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022086 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022087 "allocating the IDC node table item list", NULL);
22088 return (-1);
22089 }
22090 vctxt->sizeIdcNodes = 20;
22091 } else if (vctxt->sizeIdcNodes <= vctxt->nbIdcNodes) {
22092 vctxt->sizeIdcNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022093 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
22094 xmlRealloc(vctxt->idcNodes, vctxt->sizeIdcNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022095 sizeof(xmlSchemaPSVIIDCNodePtr));
22096 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022097 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022098 "re-allocating the IDC node table item list", NULL);
22099 return (-1);
22100 }
22101 }
22102 vctxt->idcNodes[vctxt->nbIdcNodes++] = item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022103
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022104 return (0);
22105}
22106
22107/**
22108 * xmlSchemaIDCStoreKey:
22109 * @vctxt: the WXS validation context
22110 * @item: the IDC key
22111 *
22112 * The validation context is used to store an IDC key.
22113 *
22114 * Returns 0 if succeeded, -1 on internal errors.
22115 */
22116static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022117xmlSchemaIDCStoreKey(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022118 xmlSchemaPSVIIDCKeyPtr key)
22119{
22120 /*
22121 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022122 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022123 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022124 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022125 xmlMalloc(40 * sizeof(xmlSchemaPSVIIDCKeyPtr));
22126 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022127 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022128 "allocating the IDC key storage list", NULL);
22129 return (-1);
22130 }
22131 vctxt->sizeIdcKeys = 40;
22132 } else if (vctxt->sizeIdcKeys <= vctxt->nbIdcKeys) {
22133 vctxt->sizeIdcKeys *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022134 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
22135 xmlRealloc(vctxt->idcKeys, vctxt->sizeIdcKeys *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022136 sizeof(xmlSchemaPSVIIDCKeyPtr));
22137 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022138 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022139 "re-allocating the IDC key storage list", NULL);
22140 return (-1);
22141 }
22142 }
22143 vctxt->idcKeys[vctxt->nbIdcKeys++] = key;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022144
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022145 return (0);
22146}
22147
22148/**
22149 * xmlSchemaIDCAppendNodeTableItem:
22150 * @bind: the IDC binding
22151 * @ntItem: the node-table item
22152 *
22153 * Appends the IDC node-table item to the binding.
22154 *
22155 * Returns 0 on success and -1 on internal errors.
22156 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022157static int
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022158xmlSchemaIDCAppendNodeTableItem(xmlSchemaPSVIIDCBindingPtr bind,
22159 xmlSchemaPSVIIDCNodePtr ntItem)
22160{
22161 if (bind->nodeTable == NULL) {
22162 bind->sizeNodes = 10;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022163 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022164 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
22165 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022166 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022167 "allocating an array of IDC node-table items", NULL);
22168 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022169 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022170 } else if (bind->sizeNodes <= bind->nbNodes) {
22171 bind->sizeNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022172 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
22173 xmlRealloc(bind->nodeTable, bind->sizeNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022174 sizeof(xmlSchemaPSVIIDCNodePtr));
22175 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022176 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022177 "re-allocating an array of IDC node-table items", NULL);
22178 return(-1);
22179 }
22180 }
22181 bind->nodeTable[bind->nbNodes++] = ntItem;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022182 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022183}
22184
22185/**
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022186 * xmlSchemaIDCAcquireBinding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022187 * @vctxt: the WXS validation context
22188 * @matcher: the IDC matcher
22189 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022190 * Looks up an PSVI IDC binding, for the IDC definition and
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022191 * of the given matcher. If none found, a new one is created
22192 * and added to the IDC table.
22193 *
22194 * Returns an IDC binding or NULL on internal errors.
22195 */
22196static xmlSchemaPSVIIDCBindingPtr
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022197xmlSchemaIDCAcquireBinding(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022198 xmlSchemaIDCMatcherPtr matcher)
22199{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022200 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022201
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022202 ielem = vctxt->elemInfos[matcher->depth];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022203
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022204 if (ielem->idcTable == NULL) {
22205 ielem->idcTable = xmlSchemaIDCNewBinding(matcher->aidc->def);
22206 if (ielem->idcTable == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022207 return (NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022208 return(ielem->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022209 } else {
22210 xmlSchemaPSVIIDCBindingPtr bind = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022211
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022212 bind = ielem->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022213 do {
22214 if (bind->definition == matcher->aidc->def)
22215 return(bind);
22216 if (bind->next == NULL) {
22217 bind->next = xmlSchemaIDCNewBinding(matcher->aidc->def);
22218 if (bind->next == NULL)
22219 return (NULL);
22220 return(bind->next);
22221 }
22222 bind = bind->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022223 } while (bind != NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022224 }
22225 return (NULL);
22226}
22227
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022228static xmlSchemaItemListPtr
22229xmlSchemaIDCAcquireTargetList(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
22230 xmlSchemaIDCMatcherPtr matcher)
22231{
22232 if (matcher->targets == NULL)
22233 matcher->targets = xmlSchemaItemListCreate();
22234 return(matcher->targets);
22235}
22236
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022237/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022238 * xmlSchemaIDCFreeKey:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022239 * @key: the IDC key
22240 *
22241 * Frees an IDC key together with its compiled value.
22242 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022243static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022244xmlSchemaIDCFreeKey(xmlSchemaPSVIIDCKeyPtr key)
22245{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022246 if (key->val != NULL)
22247 xmlSchemaFreeValue(key->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022248 xmlFree(key);
22249}
22250
22251/**
22252 * xmlSchemaIDCFreeBinding:
22253 *
22254 * Frees an IDC binding. Note that the node table-items
22255 * are not freed.
22256 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022257static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022258xmlSchemaIDCFreeBinding(xmlSchemaPSVIIDCBindingPtr bind)
22259{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022260 if (bind->nodeTable != NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022261 xmlFree(bind->nodeTable);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022262 if (bind->dupls != NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000022263 xmlSchemaItemListFree(bind->dupls);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022264 xmlFree(bind);
22265}
22266
22267/**
22268 * xmlSchemaIDCFreeIDCTable:
22269 * @bind: the first IDC binding in the list
22270 *
22271 * Frees an IDC table, i.e. all the IDC bindings in the list.
22272 */
22273static void
22274xmlSchemaIDCFreeIDCTable(xmlSchemaPSVIIDCBindingPtr bind)
22275{
22276 xmlSchemaPSVIIDCBindingPtr prev;
22277
22278 while (bind != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022279 prev = bind;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022280 bind = bind->next;
22281 xmlSchemaIDCFreeBinding(prev);
22282 }
22283}
22284
22285/**
22286 * xmlSchemaIDCFreeMatcherList:
22287 * @matcher: the first IDC matcher in the list
22288 *
22289 * Frees a list of IDC matchers.
22290 */
22291static void
22292xmlSchemaIDCFreeMatcherList(xmlSchemaIDCMatcherPtr matcher)
22293{
22294 xmlSchemaIDCMatcherPtr next;
22295
22296 while (matcher != NULL) {
22297 next = matcher->next;
Daniel Veillarddee23482008-04-11 12:58:43 +000022298 if (matcher->keySeqs != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022299 int i;
22300 for (i = 0; i < matcher->sizeKeySeqs; i++)
22301 if (matcher->keySeqs[i] != NULL)
22302 xmlFree(matcher->keySeqs[i]);
Daniel Veillarddee23482008-04-11 12:58:43 +000022303 xmlFree(matcher->keySeqs);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022304 }
22305 if (matcher->targets != NULL) {
22306 if (matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) {
22307 int i;
22308 xmlSchemaPSVIIDCNodePtr idcNode;
22309 /*
22310 * Node-table items for keyrefs are not stored globally
22311 * to the validation context, since they are not bubbled.
22312 * We need to free them here.
22313 */
22314 for (i = 0; i < matcher->targets->nbItems; i++) {
22315 idcNode =
22316 (xmlSchemaPSVIIDCNodePtr) matcher->targets->items[i];
22317 xmlFree(idcNode->keys);
22318 xmlFree(idcNode);
22319 }
22320 }
22321 xmlSchemaItemListFree(matcher->targets);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022322 }
22323 xmlFree(matcher);
22324 matcher = next;
22325 }
22326}
22327
22328/**
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000022329 * xmlSchemaIDCReleaseMatcherList:
22330 * @vctxt: the WXS validation context
22331 * @matcher: the first IDC matcher in the list
22332 *
22333 * Caches a list of IDC matchers for reuse.
22334 */
22335static void
22336xmlSchemaIDCReleaseMatcherList(xmlSchemaValidCtxtPtr vctxt,
22337 xmlSchemaIDCMatcherPtr matcher)
22338{
22339 xmlSchemaIDCMatcherPtr next;
22340
22341 while (matcher != NULL) {
22342 next = matcher->next;
Daniel Veillarddee23482008-04-11 12:58:43 +000022343 if (matcher->keySeqs != NULL) {
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000022344 int i;
22345 /*
22346 * Don't free the array, but only the content.
22347 */
22348 for (i = 0; i < matcher->sizeKeySeqs; i++)
22349 if (matcher->keySeqs[i] != NULL) {
22350 xmlFree(matcher->keySeqs[i]);
22351 matcher->keySeqs[i] = NULL;
22352 }
22353 }
22354 if (matcher->targets) {
22355 if (matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) {
22356 int i;
22357 xmlSchemaPSVIIDCNodePtr idcNode;
22358 /*
22359 * Node-table items for keyrefs are not stored globally
22360 * to the validation context, since they are not bubbled.
22361 * We need to free them here.
22362 */
22363 for (i = 0; i < matcher->targets->nbItems; i++) {
22364 idcNode =
22365 (xmlSchemaPSVIIDCNodePtr) matcher->targets->items[i];
22366 xmlFree(idcNode->keys);
22367 xmlFree(idcNode);
22368 }
22369 }
22370 xmlSchemaItemListFree(matcher->targets);
22371 matcher->targets = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022372 }
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000022373 matcher->next = NULL;
22374 /*
22375 * Cache the matcher.
22376 */
22377 if (vctxt->idcMatcherCache != NULL)
22378 matcher->nextCached = vctxt->idcMatcherCache;
22379 vctxt->idcMatcherCache = matcher;
22380
22381 matcher = next;
22382 }
22383}
22384
22385/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022386 * xmlSchemaIDCAddStateObject:
22387 * @vctxt: the WXS validation context
22388 * @matcher: the IDC matcher
22389 * @sel: the XPath information
22390 * @parent: the parent "selector" state object if any
22391 * @type: "selector" or "field"
22392 *
22393 * Creates/reuses and activates state objects for the given
22394 * XPath information; if the XPath expression consists of unions,
22395 * multiple state objects are created for every unioned expression.
22396 *
22397 * Returns 0 on success and -1 on internal errors.
22398 */
22399static int
22400xmlSchemaIDCAddStateObject(xmlSchemaValidCtxtPtr vctxt,
22401 xmlSchemaIDCMatcherPtr matcher,
22402 xmlSchemaIDCSelectPtr sel,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022403 int type)
22404{
22405 xmlSchemaIDCStateObjPtr sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022406
22407 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022408 * Reuse the state objects from the pool.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022409 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022410 if (vctxt->xpathStatePool != NULL) {
22411 sto = vctxt->xpathStatePool;
22412 vctxt->xpathStatePool = sto->next;
22413 sto->next = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022414 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022415 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022416 * Create a new state object.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022417 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022418 sto = (xmlSchemaIDCStateObjPtr) xmlMalloc(sizeof(xmlSchemaIDCStateObj));
22419 if (sto == NULL) {
22420 xmlSchemaVErrMemory(NULL,
22421 "allocating an IDC state object", NULL);
22422 return (-1);
22423 }
22424 memset(sto, 0, sizeof(xmlSchemaIDCStateObj));
Daniel Veillarddee23482008-04-11 12:58:43 +000022425 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022426 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000022427 * Add to global list.
22428 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022429 if (vctxt->xpathStates != NULL)
22430 sto->next = vctxt->xpathStates;
22431 vctxt->xpathStates = sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022432
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022433 /*
22434 * Free the old xpath validation context.
22435 */
22436 if (sto->xpathCtxt != NULL)
22437 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
22438
22439 /*
22440 * Create a new XPath (pattern) validation context.
22441 */
22442 sto->xpathCtxt = (void *) xmlPatternGetStreamCtxt(
22443 (xmlPatternPtr) sel->xpathComp);
22444 if (sto->xpathCtxt == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022445 VERROR_INT("xmlSchemaIDCAddStateObject",
22446 "failed to create an XPath validation context");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022447 return (-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000022448 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022449 sto->type = type;
22450 sto->depth = vctxt->depth;
22451 sto->matcher = matcher;
22452 sto->sel = sel;
22453 sto->nbHistory = 0;
Daniel Veillarddee23482008-04-11 12:58:43 +000022454
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022455#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022456 xmlGenericError(xmlGenericErrorContext, "IDC: STO push '%s'\n",
22457 sto->sel->xpath);
22458#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022459 return (0);
22460}
22461
22462/**
22463 * xmlSchemaXPathEvaluate:
22464 * @vctxt: the WXS validation context
22465 * @nodeType: the nodeType of the current node
22466 *
22467 * Evaluates all active XPath state objects.
22468 *
22469 * Returns the number of IC "field" state objects which resolved to
22470 * this node, 0 if none resolved and -1 on internal errors.
22471 */
22472static int
22473xmlSchemaXPathEvaluate(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022474 xmlElementType nodeType)
22475{
22476 xmlSchemaIDCStateObjPtr sto, head = NULL, first;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022477 int res, resolved = 0, depth = vctxt->depth;
Daniel Veillarddee23482008-04-11 12:58:43 +000022478
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022479 if (vctxt->xpathStates == NULL)
22480 return (0);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022481
22482 if (nodeType == XML_ATTRIBUTE_NODE)
22483 depth++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022484#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022485 {
22486 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022487 xmlGenericError(xmlGenericErrorContext,
22488 "IDC: EVAL on %s, depth %d, type %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022489 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22490 vctxt->inode->localName), depth, nodeType);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022491 FREE_AND_NULL(str)
22492 }
22493#endif
22494 /*
22495 * Process all active XPath state objects.
22496 */
22497 first = vctxt->xpathStates;
22498 sto = first;
22499 while (sto != head) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022500#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022501 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR)
Daniel Veillarddee23482008-04-11 12:58:43 +000022502 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] selector '%s'\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022503 sto->matcher->aidc->def->name, sto->sel->xpath);
22504 else
Daniel Veillarddee23482008-04-11 12:58:43 +000022505 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] field '%s'\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022506 sto->matcher->aidc->def->name, sto->sel->xpath);
22507#endif
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022508 if (nodeType == XML_ELEMENT_NODE)
22509 res = xmlStreamPush((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022510 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022511 else
22512 res = xmlStreamPushAttr((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022513 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022514
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022515 if (res == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022516 VERROR_INT("xmlSchemaXPathEvaluate",
22517 "calling xmlStreamPush()");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022518 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022519 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022520 if (res == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022521 goto next_sto;
22522 /*
22523 * Full match.
22524 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022525#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022526 xmlGenericError(xmlGenericErrorContext, "IDC: "
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022527 "MATCH\n");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022528#endif
22529 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022530 * Register a match in the state object history.
22531 */
22532 if (sto->history == NULL) {
22533 sto->history = (int *) xmlMalloc(5 * sizeof(int));
22534 if (sto->history == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022535 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022536 "allocating the state object history", NULL);
22537 return(-1);
22538 }
Daniel Veillard6f9b0872006-08-12 14:09:01 +000022539 sto->sizeHistory = 5;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022540 } else if (sto->sizeHistory <= sto->nbHistory) {
22541 sto->sizeHistory *= 2;
22542 sto->history = (int *) xmlRealloc(sto->history,
22543 sto->sizeHistory * sizeof(int));
22544 if (sto->history == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022545 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022546 "re-allocating the state object history", NULL);
22547 return(-1);
22548 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022549 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022550 sto->history[sto->nbHistory++] = depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022551
22552#ifdef DEBUG_IDC
22553 xmlGenericError(xmlGenericErrorContext, "IDC: push match '%d'\n",
22554 vctxt->depth);
22555#endif
22556
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022557 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
22558 xmlSchemaIDCSelectPtr sel;
22559 /*
22560 * Activate state objects for the IDC fields of
22561 * the IDC selector.
22562 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022563#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022564 xmlGenericError(xmlGenericErrorContext, "IDC: "
22565 "activating field states\n");
22566#endif
22567 sel = sto->matcher->aidc->def->fields;
22568 while (sel != NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022569 if (xmlSchemaIDCAddStateObject(vctxt, sto->matcher,
22570 sel, XPATH_STATE_OBJ_TYPE_IDC_FIELD) == -1)
22571 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022572 sel = sel->next;
22573 }
22574 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
22575 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000022576 * An IDC key node was found by the IDC field.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022577 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022578#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022579 xmlGenericError(xmlGenericErrorContext,
22580 "IDC: key found\n");
22581#endif
22582 /*
22583 * Notify that the character value of this node is
22584 * needed.
22585 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022586 if (resolved == 0) {
22587 if ((vctxt->inode->flags &
22588 XML_SCHEMA_NODE_INFO_VALUE_NEEDED) == 0)
22589 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
22590 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022591 resolved++;
22592 }
22593next_sto:
22594 if (sto->next == NULL) {
22595 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022596 * Evaluate field state objects created on this node as well.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022597 */
22598 head = first;
22599 sto = vctxt->xpathStates;
22600 } else
22601 sto = sto->next;
22602 }
22603 return (resolved);
22604}
22605
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022606static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022607xmlSchemaFormatIDCKeySequence(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022608 xmlChar **buf,
22609 xmlSchemaPSVIIDCKeyPtr *seq,
22610 int count)
22611{
22612 int i, res;
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022613 xmlChar *value = NULL;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022614
22615 *buf = xmlStrdup(BAD_CAST "[");
22616 for (i = 0; i < count; i++) {
22617 *buf = xmlStrcat(*buf, BAD_CAST "'");
Daniel Veillarddee23482008-04-11 12:58:43 +000022618 res = xmlSchemaGetCanonValueWhtspExt(seq[i]->val,
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022619 xmlSchemaGetWhiteSpaceFacetValue(seq[i]->type),
22620 &value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022621 if (res == 0)
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022622 *buf = xmlStrcat(*buf, BAD_CAST value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022623 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022624 VERROR_INT("xmlSchemaFormatIDCKeySequence",
22625 "failed to compute a canonical value");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022626 *buf = xmlStrcat(*buf, BAD_CAST "???");
22627 }
22628 if (i < count -1)
22629 *buf = xmlStrcat(*buf, BAD_CAST "', ");
22630 else
22631 *buf = xmlStrcat(*buf, BAD_CAST "'");
22632 if (value != NULL) {
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022633 xmlFree(value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022634 value = NULL;
22635 }
22636 }
22637 *buf = xmlStrcat(*buf, BAD_CAST "]");
22638
22639 return (BAD_CAST *buf);
22640}
22641
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022642/**
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000022643 * xmlSchemaXPathPop:
22644 * @vctxt: the WXS validation context
22645 *
22646 * Pops all XPath states.
22647 *
22648 * Returns 0 on success and -1 on internal errors.
22649 */
22650static int
22651xmlSchemaXPathPop(xmlSchemaValidCtxtPtr vctxt)
22652{
22653 xmlSchemaIDCStateObjPtr sto;
22654 int res;
22655
22656 if (vctxt->xpathStates == NULL)
22657 return(0);
22658 sto = vctxt->xpathStates;
22659 do {
22660 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22661 if (res == -1)
22662 return (-1);
22663 sto = sto->next;
22664 } while (sto != NULL);
22665 return(0);
22666}
22667
22668/**
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022669 * xmlSchemaXPathProcessHistory:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022670 * @vctxt: the WXS validation context
22671 * @type: the simple/complex type of the current node if any at all
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022672 * @val: the precompiled value
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022673 *
22674 * Processes and pops the history items of the IDC state objects.
22675 * IDC key-sequences are validated/created on IDC bindings.
Daniel Veillarddee23482008-04-11 12:58:43 +000022676 *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022677 * Returns 0 on success and -1 on internal errors.
22678 */
22679static int
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022680xmlSchemaXPathProcessHistory(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022681 int depth)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022682{
22683 xmlSchemaIDCStateObjPtr sto, nextsto;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022684 int res, matchDepth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022685 xmlSchemaPSVIIDCKeyPtr key = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022686 xmlSchemaTypePtr type = vctxt->inode->typeDef, simpleType = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022687
22688 if (vctxt->xpathStates == NULL)
22689 return (0);
22690 sto = vctxt->xpathStates;
22691
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022692#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022693 {
22694 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022695 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022696 "IDC: BACK on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022697 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22698 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022699 FREE_AND_NULL(str)
22700 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022701#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022702 /*
22703 * Evaluate the state objects.
22704 */
22705 while (sto != NULL) {
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000022706 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22707 if (res == -1) {
22708 VERROR_INT("xmlSchemaXPathProcessHistory",
22709 "calling xmlStreamPop()");
22710 return (-1);
22711 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022712#ifdef DEBUG_IDC
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000022713 xmlGenericError(xmlGenericErrorContext, "IDC: stream pop '%s'\n",
22714 sto->sel->xpath);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022715#endif
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022716 if (sto->nbHistory == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022717 goto deregister_check;
22718
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022719 matchDepth = sto->history[sto->nbHistory -1];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022720
22721 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022722 * Only matches at the current depth are of interest.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022723 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022724 if (matchDepth != depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022725 sto = sto->next;
22726 continue;
Daniel Veillarddee23482008-04-11 12:58:43 +000022727 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022728 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022729 /*
22730 * NOTE: According to
22731 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2198
22732 * ... the simple-content of complex types is also allowed.
22733 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022734
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022735 if (WXS_IS_COMPLEX(type)) {
22736 if (WXS_HAS_SIMPLE_CONTENT(type)) {
22737 /*
22738 * Sanity check for complex types with simple content.
22739 */
22740 simpleType = type->contentTypeDef;
22741 if (simpleType == NULL) {
22742 VERROR_INT("xmlSchemaXPathProcessHistory",
22743 "field resolves to a CT with simple content "
22744 "but the CT is missing the ST definition");
22745 return (-1);
22746 }
22747 } else
22748 simpleType = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022749 } else
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022750 simpleType = type;
22751 if (simpleType == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022752 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022753
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022754 /*
22755 * Not qualified if the field resolves to a node of non
22756 * simple type.
Daniel Veillarddee23482008-04-11 12:58:43 +000022757 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022758 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Daniel Veillarddee23482008-04-11 12:58:43 +000022759 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022760 WXS_BASIC_CAST sto->matcher->aidc->def,
22761 "The XPath '%s' of a field of %s does evaluate to a node of "
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022762 "non-simple type",
22763 sto->sel->xpath,
22764 xmlSchemaGetIDCDesignation(&str, sto->matcher->aidc->def));
22765 FREE_AND_NULL(str);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022766 sto->nbHistory--;
22767 goto deregister_check;
22768 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022769
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022770 if ((key == NULL) && (vctxt->inode->val == NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022771 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022772 * Failed to provide the normalized value; maybe
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022773 * the value was invalid.
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022774 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022775 VERROR(XML_SCHEMAV_CVC_IDC,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022776 WXS_BASIC_CAST sto->matcher->aidc->def,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000022777 "Warning: No precomputed value available, the value "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022778 "was either invalid or something strange happend");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022779 sto->nbHistory--;
22780 goto deregister_check;
22781 } else {
22782 xmlSchemaIDCMatcherPtr matcher = sto->matcher;
22783 xmlSchemaPSVIIDCKeyPtr *keySeq;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022784 int pos, idx;
Daniel Veillarddee23482008-04-11 12:58:43 +000022785
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022786 /*
22787 * The key will be anchored on the matcher's list of
22788 * key-sequences. The position in this list is determined
22789 * by the target node's depth relative to the matcher's
22790 * depth of creation (i.e. the depth of the scope element).
Daniel Veillarddee23482008-04-11 12:58:43 +000022791 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022792 * Element Depth Pos List-entries
22793 * <scope> 0 NULL
22794 * <bar> 1 NULL
22795 * <target/> 2 2 target
22796 * <bar>
22797 * </scope>
22798 *
22799 * The size of the list is only dependant on the depth of
22800 * the tree.
22801 * An entry will be NULLed in selector_leave, i.e. when
Daniel Veillarddee23482008-04-11 12:58:43 +000022802 * we hit the target's
22803 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022804 pos = sto->depth - matcher->depth;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022805 idx = sto->sel->index;
Daniel Veillarddee23482008-04-11 12:58:43 +000022806
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022807 /*
22808 * Create/grow the array of key-sequences.
22809 */
22810 if (matcher->keySeqs == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022811 if (pos > 9)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022812 matcher->sizeKeySeqs = pos * 2;
22813 else
22814 matcher->sizeKeySeqs = 10;
Daniel Veillarddee23482008-04-11 12:58:43 +000022815 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022816 xmlMalloc(matcher->sizeKeySeqs *
Daniel Veillarddee23482008-04-11 12:58:43 +000022817 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22818 if (matcher->keySeqs == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022819 xmlSchemaVErrMemory(NULL,
22820 "allocating an array of key-sequences",
22821 NULL);
22822 return(-1);
22823 }
22824 memset(matcher->keySeqs, 0,
22825 matcher->sizeKeySeqs *
22826 sizeof(xmlSchemaPSVIIDCKeyPtr *));
Daniel Veillarddee23482008-04-11 12:58:43 +000022827 } else if (pos >= matcher->sizeKeySeqs) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022828 int i = matcher->sizeKeySeqs;
Daniel Veillarddee23482008-04-11 12:58:43 +000022829
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022830 matcher->sizeKeySeqs *= 2;
22831 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22832 xmlRealloc(matcher->keySeqs,
22833 matcher->sizeKeySeqs *
22834 sizeof(xmlSchemaPSVIIDCKeyPtr *));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022835 if (matcher->keySeqs == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022836 xmlSchemaVErrMemory(NULL,
22837 "reallocating an array of key-sequences",
22838 NULL);
22839 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022840 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022841 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022842 * The array needs to be NULLed.
22843 * TODO: Use memset?
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022844 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022845 for (; i < matcher->sizeKeySeqs; i++)
22846 matcher->keySeqs[i] = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022847 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022848
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022849 /*
22850 * Get/create the key-sequence.
22851 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022852 keySeq = matcher->keySeqs[pos];
22853 if (keySeq == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022854 goto create_sequence;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022855 } else if (keySeq[idx] != NULL) {
22856 xmlChar *str = NULL;
22857 /*
22858 * cvc-identity-constraint:
Jan Pokorný761c9e92013-11-29 23:26:27 +010022859 * 3 For each node in the `target node set` all
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022860 * of the {fields}, with that node as the context
22861 * node, evaluate to either an empty node-set or
22862 * a node-set with exactly one member, which must
22863 * have a simple type.
Daniel Veillarddee23482008-04-11 12:58:43 +000022864 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022865 * The key was already set; report an error.
22866 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022867 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022868 XML_SCHEMAV_CVC_IDC, NULL,
22869 WXS_BASIC_CAST matcher->aidc->def,
22870 "The XPath '%s' of a field of %s evaluates to a "
22871 "node-set with more than one member",
22872 sto->sel->xpath,
22873 xmlSchemaGetIDCDesignation(&str, matcher->aidc->def));
22874 FREE_AND_NULL(str);
22875 sto->nbHistory--;
22876 goto deregister_check;
22877 } else
Daniel Veillarddee23482008-04-11 12:58:43 +000022878 goto create_key;
22879
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022880create_sequence:
22881 /*
22882 * Create a key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022883 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022884 keySeq = (xmlSchemaPSVIIDCKeyPtr *) xmlMalloc(
Daniel Veillarddee23482008-04-11 12:58:43 +000022885 matcher->aidc->def->nbFields *
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022886 sizeof(xmlSchemaPSVIIDCKeyPtr));
22887 if (keySeq == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022888 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022889 "allocating an IDC key-sequence", NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000022890 return(-1);
22891 }
22892 memset(keySeq, 0, matcher->aidc->def->nbFields *
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022893 sizeof(xmlSchemaPSVIIDCKeyPtr));
22894 matcher->keySeqs[pos] = keySeq;
22895create_key:
22896 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022897 * Create a key once per node only.
Daniel Veillarddee23482008-04-11 12:58:43 +000022898 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022899 if (key == NULL) {
22900 key = (xmlSchemaPSVIIDCKeyPtr) xmlMalloc(
22901 sizeof(xmlSchemaPSVIIDCKey));
22902 if (key == NULL) {
22903 xmlSchemaVErrMemory(NULL,
22904 "allocating a IDC key", NULL);
22905 xmlFree(keySeq);
22906 matcher->keySeqs[pos] = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000022907 return(-1);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022908 }
22909 /*
22910 * Consume the compiled value.
22911 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022912 key->type = simpleType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022913 key->val = vctxt->inode->val;
22914 vctxt->inode->val = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022915 /*
22916 * Store the key in a global list.
22917 */
22918 if (xmlSchemaIDCStoreKey(vctxt, key) == -1) {
22919 xmlSchemaIDCFreeKey(key);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022920 return (-1);
22921 }
22922 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022923 keySeq[idx] = key;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022924 }
22925 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
Daniel Veillarddee23482008-04-11 12:58:43 +000022926
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022927 xmlSchemaPSVIIDCKeyPtr **keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022928 /* xmlSchemaPSVIIDCBindingPtr bind; */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022929 xmlSchemaPSVIIDCNodePtr ntItem;
22930 xmlSchemaIDCMatcherPtr matcher;
22931 xmlSchemaIDCPtr idc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022932 xmlSchemaItemListPtr targets;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022933 int pos, i, j, nbKeys;
22934 /*
22935 * Here we have the following scenario:
22936 * An IDC 'selector' state object resolved to a target node,
Daniel Veillarddee23482008-04-11 12:58:43 +000022937 * during the time this target node was in the
22938 * ancestor-or-self axis, the 'field' state object(s) looked
22939 * out for matching nodes to create a key-sequence for this
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022940 * target node. Now we are back to this target node and need
Daniel Veillarddee23482008-04-11 12:58:43 +000022941 * to put the key-sequence, together with the target node
22942 * itself, into the node-table of the corresponding IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022943 * binding.
22944 */
22945 matcher = sto->matcher;
22946 idc = matcher->aidc->def;
22947 nbKeys = idc->nbFields;
Daniel Veillarddee23482008-04-11 12:58:43 +000022948 pos = depth - matcher->depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022949 /*
22950 * Check if the matcher has any key-sequences at all, plus
22951 * if it has a key-sequence for the current target node.
Daniel Veillarddee23482008-04-11 12:58:43 +000022952 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022953 if ((matcher->keySeqs == NULL) ||
22954 (matcher->sizeKeySeqs <= pos)) {
22955 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22956 goto selector_key_error;
22957 else
22958 goto selector_leave;
22959 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022960
22961 keySeq = &(matcher->keySeqs[pos]);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022962 if (*keySeq == NULL) {
22963 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22964 goto selector_key_error;
22965 else
22966 goto selector_leave;
22967 }
Daniel Veillarddee23482008-04-11 12:58:43 +000022968
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022969 for (i = 0; i < nbKeys; i++) {
22970 if ((*keySeq)[i] == NULL) {
22971 /*
22972 * Not qualified, if not all fields did resolve.
22973 */
22974 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY) {
22975 /*
22976 * All fields of a "key" IDC must resolve.
22977 */
22978 goto selector_key_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000022979 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022980 goto selector_leave;
22981 }
22982 }
22983 /*
22984 * All fields did resolve.
22985 */
Daniel Veillarddee23482008-04-11 12:58:43 +000022986
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022987 /*
22988 * 4.1 If the {identity-constraint category} is unique(/key),
Jan Pokorný761c9e92013-11-29 23:26:27 +010022989 * then no two members of the `qualified node set` have
22990 * `key-sequences` whose members are pairwise equal, as
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022991 * defined by Equal in [XML Schemas: Datatypes].
22992 *
22993 * Get the IDC binding from the matcher and check for
22994 * duplicate key-sequences.
22995 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022996#if 0
22997 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22998#endif
22999 targets = xmlSchemaIDCAcquireTargetList(vctxt, matcher);
Daniel Veillarddee23482008-04-11 12:58:43 +000023000 if ((idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) &&
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023001 (targets->nbItems != 0)) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000023002 xmlSchemaPSVIIDCKeyPtr ckey, bkey, *bkeySeq;
Daniel Veillarddee23482008-04-11 12:58:43 +000023003
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023004 i = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000023005 res = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023006 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023007 * Compare the key-sequences, key by key.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023008 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023009 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023010 bkeySeq =
23011 ((xmlSchemaPSVIIDCNodePtr) targets->items[i])->keys;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023012 for (j = 0; j < nbKeys; j++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000023013 ckey = (*keySeq)[j];
Daniel Veillarddee23482008-04-11 12:58:43 +000023014 bkey = bkeySeq[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023015 res = xmlSchemaAreValuesEqual(ckey->val, bkey->val);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023016 if (res == -1) {
23017 return (-1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023018 } else if (res == 0) {
23019 /*
23020 * One of the keys differs, so the key-sequence
23021 * won't be equal; get out.
23022 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023023 break;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023024 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023025 }
23026 if (res == 1) {
23027 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023028 * Duplicate key-sequence found.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023029 */
23030 break;
23031 }
23032 i++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023033 } while (i < targets->nbItems);
23034 if (i != targets->nbItems) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023035 xmlChar *str = NULL, *strB = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000023036 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023037 * TODO: Try to report the key-sequence.
23038 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023039 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023040 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023041 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023042 "Duplicate key-sequence %s in %s",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000023043 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023044 (*keySeq), nbKeys),
23045 xmlSchemaGetIDCDesignation(&strB, idc));
23046 FREE_AND_NULL(str);
23047 FREE_AND_NULL(strB);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023048 goto selector_leave;
23049 }
23050 }
23051 /*
23052 * Add a node-table item to the IDC binding.
23053 */
23054 ntItem = (xmlSchemaPSVIIDCNodePtr) xmlMalloc(
23055 sizeof(xmlSchemaPSVIIDCNode));
23056 if (ntItem == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023057 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023058 "allocating an IDC node-table item", NULL);
23059 xmlFree(*keySeq);
23060 *keySeq = NULL;
23061 return(-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000023062 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023063 memset(ntItem, 0, sizeof(xmlSchemaPSVIIDCNode));
Daniel Veillarddee23482008-04-11 12:58:43 +000023064
23065 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023066 * Store the node-table item in a global list.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023067 */
23068 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) {
23069 if (xmlSchemaIDCStoreNodeTableItem(vctxt, ntItem) == -1) {
23070 xmlFree(ntItem);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023071 xmlFree(*keySeq);
23072 *keySeq = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023073 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023074 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023075 ntItem->nodeQNameID = -1;
23076 } else {
23077 /*
23078 * Save a cached QName for this node on the IDC node, to be
23079 * able to report it, even if the node is not saved.
23080 */
23081 ntItem->nodeQNameID = xmlSchemaVAddNodeQName(vctxt,
23082 vctxt->inode->localName, vctxt->inode->nsName);
23083 if (ntItem->nodeQNameID == -1) {
23084 xmlFree(ntItem);
23085 xmlFree(*keySeq);
23086 *keySeq = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000023087 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023088 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023089 }
23090 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023091 * Init the node-table item: Save the node, position and
23092 * consume the key-sequence.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023093 */
23094 ntItem->node = vctxt->node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023095 ntItem->nodeLine = vctxt->inode->nodeLine;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023096 ntItem->keys = *keySeq;
23097 *keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023098#if 0
Daniel Veillarddee23482008-04-11 12:58:43 +000023099 if (xmlSchemaIDCAppendNodeTableItem(bind, ntItem) == -1)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023100#endif
23101 if (xmlSchemaItemListAdd(targets, ntItem) == -1) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023102 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023103 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023104 * Free the item, since keyref items won't be
23105 * put on a global list.
23106 */
23107 xmlFree(ntItem->keys);
23108 xmlFree(ntItem);
23109 }
23110 return (-1);
23111 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023112
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023113 goto selector_leave;
23114selector_key_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023115 {
23116 xmlChar *str = NULL;
23117 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010023118 * 4.2.1 (KEY) The `target node set` and the
23119 * `qualified node set` are equal, that is, every
23120 * member of the `target node set` is also a member
23121 * of the `qualified node set` and vice versa.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023122 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023123 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023124 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023125 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023126 "Not all fields of %s evaluate to a node",
23127 xmlSchemaGetIDCDesignation(&str, idc), NULL);
23128 FREE_AND_NULL(str);
23129 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023130selector_leave:
23131 /*
23132 * Free the key-sequence if not added to the IDC table.
23133 */
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000023134 if ((keySeq != NULL) && (*keySeq != NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023135 xmlFree(*keySeq);
23136 *keySeq = NULL;
23137 }
23138 } /* if selector */
Daniel Veillarddee23482008-04-11 12:58:43 +000023139
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023140 sto->nbHistory--;
23141
23142deregister_check:
23143 /*
23144 * Deregister state objects if they reach the depth of creation.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023145 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000023146 if ((sto->nbHistory == 0) && (sto->depth == depth)) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023147#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023148 xmlGenericError(xmlGenericErrorContext, "IDC: STO pop '%s'\n",
23149 sto->sel->xpath);
23150#endif
23151 if (vctxt->xpathStates != sto) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023152 VERROR_INT("xmlSchemaXPathProcessHistory",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023153 "The state object to be removed is not the first "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023154 "in the list");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023155 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023156 nextsto = sto->next;
23157 /*
23158 * Unlink from the list of active XPath state objects.
23159 */
23160 vctxt->xpathStates = sto->next;
23161 sto->next = vctxt->xpathStatePool;
23162 /*
23163 * Link it to the pool of reusable state objects.
23164 */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023165 vctxt->xpathStatePool = sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023166 sto = nextsto;
23167 } else
23168 sto = sto->next;
23169 } /* while (sto != NULL) */
23170 return (0);
23171}
23172
23173/**
23174 * xmlSchemaIDCRegisterMatchers:
23175 * @vctxt: the WXS validation context
23176 * @elemDecl: the element declaration
23177 *
23178 * Creates helper objects to evaluate IDC selectors/fields
23179 * successively.
23180 *
23181 * Returns 0 if OK and -1 on internal errors.
23182 */
23183static int
23184xmlSchemaIDCRegisterMatchers(xmlSchemaValidCtxtPtr vctxt,
23185 xmlSchemaElementPtr elemDecl)
23186{
23187 xmlSchemaIDCMatcherPtr matcher, last = NULL;
23188 xmlSchemaIDCPtr idc, refIdc;
23189 xmlSchemaIDCAugPtr aidc;
Daniel Veillarddee23482008-04-11 12:58:43 +000023190
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023191 idc = (xmlSchemaIDCPtr) elemDecl->idcs;
23192 if (idc == NULL)
23193 return (0);
Daniel Veillarddee23482008-04-11 12:58:43 +000023194
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023195#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023196 {
23197 xmlChar *str = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000023198 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023199 "IDC: REGISTER on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023200 (char *) xmlSchemaFormatQName(&str, vctxt->inode->nsName,
23201 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023202 FREE_AND_NULL(str)
23203 }
23204#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023205 if (vctxt->inode->idcMatchers != NULL) {
23206 VERROR_INT("xmlSchemaIDCRegisterMatchers",
23207 "The chain of IDC matchers is expected to be empty");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023208 return (-1);
23209 }
23210 do {
23211 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
23212 /*
23213 * Since IDCs bubbles are expensive we need to know the
23214 * depth at which the bubbles should stop; this will be
23215 * the depth of the top-most keyref IDC. If no keyref
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023216 * references a key/unique IDC, the keyrefDepth will
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023217 * be -1, indicating that no bubbles are needed.
23218 */
23219 refIdc = (xmlSchemaIDCPtr) idc->ref->item;
23220 if (refIdc != NULL) {
23221 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023222 * Remember that we have keyrefs on this node.
23223 */
23224 vctxt->inode->hasKeyrefs = 1;
23225 /*
23226 * Lookup the referenced augmented IDC info.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023227 */
23228 aidc = vctxt->aidcs;
23229 while (aidc != NULL) {
23230 if (aidc->def == refIdc)
23231 break;
23232 aidc = aidc->next;
23233 }
23234 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023235 VERROR_INT("xmlSchemaIDCRegisterMatchers",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023236 "Could not find an augmented IDC item for an IDC "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023237 "definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023238 return (-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000023239 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023240 if ((aidc->keyrefDepth == -1) ||
23241 (vctxt->depth < aidc->keyrefDepth))
23242 aidc->keyrefDepth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023243 }
23244 }
23245 /*
23246 * Lookup the augmented IDC item for the IDC definition.
23247 */
23248 aidc = vctxt->aidcs;
23249 while (aidc != NULL) {
23250 if (aidc->def == idc)
23251 break;
23252 aidc = aidc->next;
23253 }
23254 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023255 VERROR_INT("xmlSchemaIDCRegisterMatchers",
23256 "Could not find an augmented IDC item for an IDC definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023257 return (-1);
23258 }
23259 /*
23260 * Create an IDC matcher for every IDC definition.
23261 */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023262 if (vctxt->idcMatcherCache != NULL) {
23263 /*
23264 * Reuse a cached matcher.
23265 */
23266 matcher = vctxt->idcMatcherCache;
23267 vctxt->idcMatcherCache = matcher->nextCached;
23268 matcher->nextCached = NULL;
23269 } else {
Daniel Veillarddee23482008-04-11 12:58:43 +000023270 matcher = (xmlSchemaIDCMatcherPtr)
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023271 xmlMalloc(sizeof(xmlSchemaIDCMatcher));
23272 if (matcher == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023273 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000023274 "allocating an IDC matcher", NULL);
23275 return (-1);
23276 }
23277 memset(matcher, 0, sizeof(xmlSchemaIDCMatcher));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023278 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023279 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023280 vctxt->inode->idcMatchers = matcher;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023281 else
23282 last->next = matcher;
23283 last = matcher;
23284
23285 matcher->type = IDC_MATCHER;
Daniel Veillarddee23482008-04-11 12:58:43 +000023286 matcher->depth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023287 matcher->aidc = aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023288 matcher->idcType = aidc->def->type;
Daniel Veillarddee23482008-04-11 12:58:43 +000023289#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023290 xmlGenericError(xmlGenericErrorContext, "IDC: register matcher\n");
Daniel Veillarddee23482008-04-11 12:58:43 +000023291#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023292 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000023293 * Init the automaton state object.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023294 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023295 if (xmlSchemaIDCAddStateObject(vctxt, matcher,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023296 idc->selector, XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) == -1)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023297 return (-1);
23298
23299 idc = idc->next;
23300 } while (idc != NULL);
23301 return (0);
23302}
23303
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023304static int
23305xmlSchemaIDCFillNodeTables(xmlSchemaValidCtxtPtr vctxt,
23306 xmlSchemaNodeInfoPtr ielem)
23307{
23308 xmlSchemaPSVIIDCBindingPtr bind;
23309 int res, i, j, k, nbTargets, nbFields, nbDupls, nbNodeTable;
23310 xmlSchemaPSVIIDCKeyPtr *keys, *ntkeys;
23311 xmlSchemaPSVIIDCNodePtr *targets, *dupls;
Daniel Veillarddee23482008-04-11 12:58:43 +000023312
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023313 xmlSchemaIDCMatcherPtr matcher = ielem->idcMatchers;
23314 /* vctxt->createIDCNodeTables */
23315 while (matcher != NULL) {
23316 /*
23317 * Skip keyref IDCs and empty IDC target-lists.
23318 */
23319 if ((matcher->aidc->def->type == XML_SCHEMA_TYPE_IDC_KEYREF) ||
23320 WXS_ILIST_IS_EMPTY(matcher->targets))
23321 {
23322 matcher = matcher->next;
23323 continue;
23324 }
23325 /*
23326 * If we _want_ the IDC node-table to be created in any case
23327 * then do so. Otherwise create them only if keyrefs need them.
23328 */
23329 if ((! vctxt->createIDCNodeTables) &&
23330 ((matcher->aidc->keyrefDepth == -1) ||
23331 (matcher->aidc->keyrefDepth > vctxt->depth)))
23332 {
23333 matcher = matcher->next;
23334 continue;
23335 }
23336 /*
23337 * Get/create the IDC binding on this element for the IDC definition.
23338 */
23339 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
23340
23341 if (! WXS_ILIST_IS_EMPTY(bind->dupls)) {
23342 dupls = (xmlSchemaPSVIIDCNodePtr *) bind->dupls->items;
23343 nbDupls = bind->dupls->nbItems;
23344 } else {
23345 dupls = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000023346 nbDupls = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023347 }
23348 if (bind->nodeTable != NULL) {
23349 nbNodeTable = bind->nbNodes;
23350 } else {
23351 nbNodeTable = 0;
23352 }
23353
23354 if ((nbNodeTable == 0) && (nbDupls == 0)) {
23355 /*
23356 * Transfer all IDC target-nodes to the IDC node-table.
23357 */
23358 bind->nodeTable =
Daniel Veillarddee23482008-04-11 12:58:43 +000023359 (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023360 bind->sizeNodes = matcher->targets->sizeItems;
23361 bind->nbNodes = matcher->targets->nbItems;
23362
23363 matcher->targets->items = NULL;
23364 matcher->targets->sizeItems = 0;
Daniel Veillarddee23482008-04-11 12:58:43 +000023365 matcher->targets->nbItems = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023366 } else {
23367 /*
23368 * Compare the key-sequences and add to the IDC node-table.
23369 */
23370 nbTargets = matcher->targets->nbItems;
Daniel Veillarddee23482008-04-11 12:58:43 +000023371 targets = (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023372 nbFields = matcher->aidc->def->nbFields;
23373 i = 0;
23374 do {
23375 keys = targets[i]->keys;
Daniel Veillarddee23482008-04-11 12:58:43 +000023376 if (nbDupls) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023377 /*
23378 * Search in already found duplicates first.
23379 */
23380 j = 0;
Daniel Veillarddee23482008-04-11 12:58:43 +000023381 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023382 if (nbFields == 1) {
23383 res = xmlSchemaAreValuesEqual(keys[0]->val,
23384 dupls[j]->keys[0]->val);
23385 if (res == -1)
23386 goto internal_error;
23387 if (res == 1) {
23388 /*
23389 * Equal key-sequence.
23390 */
23391 goto next_target;
23392 }
23393 } else {
23394 res = 0;
23395 ntkeys = dupls[j]->keys;
23396 for (k = 0; k < nbFields; k++) {
23397 res = xmlSchemaAreValuesEqual(keys[k]->val,
23398 ntkeys[k]->val);
23399 if (res == -1)
23400 goto internal_error;
23401 if (res == 0) {
23402 /*
23403 * One of the keys differs.
23404 */
23405 break;
23406 }
23407 }
23408 if (res == 1) {
23409 /*
23410 * Equal key-sequence found.
23411 */
23412 goto next_target;
23413 }
23414 }
23415 j++;
Daniel Veillarddee23482008-04-11 12:58:43 +000023416 } while (j < nbDupls);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023417 }
23418 if (nbNodeTable) {
23419 j = 0;
Daniel Veillarddee23482008-04-11 12:58:43 +000023420 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023421 if (nbFields == 1) {
23422 res = xmlSchemaAreValuesEqual(keys[0]->val,
23423 bind->nodeTable[j]->keys[0]->val);
23424 if (res == -1)
23425 goto internal_error;
23426 if (res == 0) {
23427 /*
23428 * The key-sequence differs.
23429 */
23430 goto next_node_table_entry;
23431 }
23432 } else {
23433 res = 0;
23434 ntkeys = bind->nodeTable[j]->keys;
23435 for (k = 0; k < nbFields; k++) {
23436 res = xmlSchemaAreValuesEqual(keys[k]->val,
23437 ntkeys[k]->val);
23438 if (res == -1)
23439 goto internal_error;
23440 if (res == 0) {
23441 /*
23442 * One of the keys differs.
23443 */
23444 goto next_node_table_entry;
23445 }
23446 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023447 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023448 /*
23449 * Add the duplicate to the list of duplicates.
23450 */
23451 if (bind->dupls == NULL) {
23452 bind->dupls = xmlSchemaItemListCreate();
23453 if (bind->dupls == NULL)
23454 goto internal_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000023455 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023456 if (xmlSchemaItemListAdd(bind->dupls, bind->nodeTable[j]) == -1)
23457 goto internal_error;
23458 /*
23459 * Remove the duplicate entry from the IDC node-table.
23460 */
23461 bind->nodeTable[j] = bind->nodeTable[bind->nbNodes -1];
23462 bind->nbNodes--;
23463
23464 goto next_target;
23465
23466next_node_table_entry:
23467 j++;
23468 } while (j < nbNodeTable);
23469 }
23470 /*
23471 * If everything is fine, then add the IDC target-node to
23472 * the IDC node-table.
23473 */
23474 if (xmlSchemaIDCAppendNodeTableItem(bind, targets[i]) == -1)
23475 goto internal_error;
23476
23477next_target:
23478 i++;
23479 } while (i < nbTargets);
23480 }
23481 matcher = matcher->next;
23482 }
23483 return(0);
23484
23485internal_error:
23486 return(-1);
23487}
23488
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023489/**
Daniel Veillarddee23482008-04-11 12:58:43 +000023490 * xmlSchemaBubbleIDCNodeTables:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023491 * @depth: the current tree depth
23492 *
Daniel Veillarddee23482008-04-11 12:58:43 +000023493 * Merges IDC bindings of an element at @depth into the corresponding IDC
23494 * bindings of its parent element. If a duplicate note-table entry is found,
23495 * both, the parent node-table entry and child entry are discarded from the
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023496 * node-table of the parent.
23497 *
23498 * Returns 0 if OK and -1 on internal errors.
23499 */
23500static int
23501xmlSchemaBubbleIDCNodeTables(xmlSchemaValidCtxtPtr vctxt)
23502{
23503 xmlSchemaPSVIIDCBindingPtr bind; /* IDC bindings of the current node. */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023504 xmlSchemaPSVIIDCBindingPtr *parTable, parBind = NULL; /* parent IDC bindings. */
23505 xmlSchemaPSVIIDCNodePtr node, parNode = NULL, *dupls, *parNodes; /* node-table entries. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023506 xmlSchemaIDCAugPtr aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023507 int i, j, k, ret = 0, nbFields, oldNum, oldDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023508
Daniel Veillarddee23482008-04-11 12:58:43 +000023509 bind = vctxt->inode->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023510 if (bind == NULL) {
23511 /* Fine, no table, no bubbles. */
23512 return (0);
23513 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023514
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023515 parTable = &(vctxt->elemInfos[vctxt->depth -1]->idcTable);
23516 /*
23517 * Walk all bindings; create new or add to existing bindings.
23518 * Remove duplicate key-sequences.
23519 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023520 while (bind != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023521
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023522 if ((bind->nbNodes == 0) && WXS_ILIST_IS_EMPTY(bind->dupls))
23523 goto next_binding;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023524 /*
23525 * Check if the key/unique IDC table needs to be bubbled.
23526 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023527 if (! vctxt->createIDCNodeTables) {
23528 aidc = vctxt->aidcs;
23529 do {
23530 if (aidc->def == bind->definition) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023531 if ((aidc->keyrefDepth == -1) ||
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023532 (aidc->keyrefDepth >= vctxt->depth)) {
23533 goto next_binding;
23534 }
23535 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023536 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023537 aidc = aidc->next;
23538 } while (aidc != NULL);
23539 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023540
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023541 if (parTable != NULL)
23542 parBind = *parTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023543 /*
23544 * Search a matching parent binding for the
23545 * IDC definition.
23546 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023547 while (parBind != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023548 if (parBind->definition == bind->definition)
23549 break;
23550 parBind = parBind->next;
23551 }
23552
23553 if (parBind != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023554 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000023555 * Compare every node-table entry of the child node,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023556 * i.e. the key-sequence within, ...
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023557 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023558 oldNum = parBind->nbNodes; /* Skip newly added items. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023559
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023560 if (! WXS_ILIST_IS_EMPTY(parBind->dupls)) {
23561 oldDupls = parBind->dupls->nbItems;
23562 dupls = (xmlSchemaPSVIIDCNodePtr *) parBind->dupls->items;
23563 } else {
23564 dupls = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000023565 oldDupls = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023566 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023567
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023568 parNodes = parBind->nodeTable;
23569 nbFields = bind->definition->nbFields;
Daniel Veillarddee23482008-04-11 12:58:43 +000023570
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023571 for (i = 0; i < bind->nbNodes; i++) {
23572 node = bind->nodeTable[i];
23573 if (node == NULL)
23574 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023575 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023576 * ...with every key-sequence of the parent node, already
23577 * evaluated to be a duplicate key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023578 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023579 if (oldDupls) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023580 j = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023581 while (j < oldDupls) {
23582 if (nbFields == 1) {
23583 ret = xmlSchemaAreValuesEqual(
23584 node->keys[0]->val,
23585 dupls[j]->keys[0]->val);
23586 if (ret == -1)
23587 goto internal_error;
23588 if (ret == 0) {
23589 j++;
23590 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023591 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023592 } else {
23593 parNode = dupls[j];
23594 for (k = 0; k < nbFields; k++) {
23595 ret = xmlSchemaAreValuesEqual(
23596 node->keys[k]->val,
23597 parNode->keys[k]->val);
23598 if (ret == -1)
23599 goto internal_error;
23600 if (ret == 0)
23601 break;
23602 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023603 }
23604 if (ret == 1)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023605 /* Duplicate found. */
23606 break;
23607 j++;
23608 }
23609 if (j != oldDupls) {
23610 /* Duplicate found. Skip this entry. */
23611 continue;
23612 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023613 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023614 /*
23615 * ... and with every key-sequence of the parent node.
23616 */
23617 if (oldNum) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023618 j = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023619 while (j < oldNum) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +000023620 parNode = parNodes[j];
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023621 if (nbFields == 1) {
23622 ret = xmlSchemaAreValuesEqual(
23623 node->keys[0]->val,
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +000023624 parNode->keys[0]->val);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023625 if (ret == -1)
23626 goto internal_error;
23627 if (ret == 0) {
23628 j++;
23629 continue;
23630 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023631 } else {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023632 for (k = 0; k < nbFields; k++) {
23633 ret = xmlSchemaAreValuesEqual(
23634 node->keys[k]->val,
23635 parNode->keys[k]->val);
23636 if (ret == -1)
23637 goto internal_error;
23638 if (ret == 0)
23639 break;
23640 }
23641 }
23642 if (ret == 1)
23643 /* Duplicate found. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023644 break;
23645 j++;
23646 }
23647 if (j != oldNum) {
23648 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023649 * Handle duplicates. Move the duplicate in
23650 * the parent's node-table to the list of
23651 * duplicates.
Daniel Veillarddee23482008-04-11 12:58:43 +000023652 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023653 oldNum--;
23654 parBind->nbNodes--;
23655 /*
23656 * Move last old item to pos of duplicate.
23657 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023658 parNodes[j] = parNodes[oldNum];
Daniel Veillarddee23482008-04-11 12:58:43 +000023659
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023660 if (parBind->nbNodes != oldNum) {
23661 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000023662 * If new items exist, move last new item to
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023663 * last of old items.
23664 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023665 parNodes[oldNum] =
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023666 parNodes[parBind->nbNodes];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023667 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023668 if (parBind->dupls == NULL) {
23669 parBind->dupls = xmlSchemaItemListCreate();
23670 if (parBind->dupls == NULL)
23671 goto internal_error;
23672 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023673 xmlSchemaItemListAdd(parBind->dupls, parNode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023674 } else {
23675 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000023676 * Add the node-table entry (node and key-sequence) of
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023677 * the child node to the node table of the parent node.
23678 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023679 if (parBind->nodeTable == NULL) {
23680 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023681 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023682 if (parBind->nodeTable == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023683 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023684 "allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023685 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023686 }
23687 parBind->sizeNodes = 1;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023688 } else if (parBind->nbNodes >= parBind->sizeNodes) {
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023689 parBind->sizeNodes *= 2;
Daniel Veillarddee23482008-04-11 12:58:43 +000023690 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23691 xmlRealloc(parBind->nodeTable, parBind->sizeNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023692 sizeof(xmlSchemaPSVIIDCNodePtr));
23693 if (parBind->nodeTable == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023694 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023695 "re-allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023696 goto internal_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000023697 }
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000023698 }
23699 parNodes = parBind->nodeTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023700 /*
23701 * Append the new node-table entry to the 'new node-table
23702 * entries' section.
23703 */
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000023704 parNodes[parBind->nbNodes++] = node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023705 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023706
Daniel Veillarddee23482008-04-11 12:58:43 +000023707 }
23708
23709 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023710 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023711 /*
23712 * No binding for the IDC was found: create a new one and
23713 * copy all node-tables.
23714 */
23715 parBind = xmlSchemaIDCNewBinding(bind->definition);
23716 if (parBind == NULL)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023717 goto internal_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000023718
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023719 /*
23720 * TODO: Hmm, how to optimize the initial number of
23721 * allocated entries?
23722 */
23723 if (bind->nbNodes != 0) {
23724 /*
23725 * Add all IDC node-table entries.
23726 */
23727 if (! vctxt->psviExposeIDCNodeTables) {
23728 /*
23729 * Just move the entries.
23730 * NOTE: this is quite save here, since
23731 * all the keyref lookups have already been
23732 * performed.
23733 */
23734 parBind->nodeTable = bind->nodeTable;
23735 bind->nodeTable = NULL;
23736 parBind->sizeNodes = bind->sizeNodes;
23737 bind->sizeNodes = 0;
23738 parBind->nbNodes = bind->nbNodes;
23739 bind->nbNodes = 0;
23740 } else {
23741 /*
23742 * Copy the entries.
23743 */
Daniel Veillarddee23482008-04-11 12:58:43 +000023744 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023745 xmlMalloc(bind->nbNodes *
23746 sizeof(xmlSchemaPSVIIDCNodePtr));
23747 if (parBind->nodeTable == NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023748 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023749 "allocating an array of IDC node-table "
23750 "items", NULL);
23751 xmlSchemaIDCFreeBinding(parBind);
23752 goto internal_error;
23753 }
23754 parBind->sizeNodes = bind->nbNodes;
23755 parBind->nbNodes = bind->nbNodes;
23756 memcpy(parBind->nodeTable, bind->nodeTable,
23757 bind->nbNodes * sizeof(xmlSchemaPSVIIDCNodePtr));
23758 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023759 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023760 if (bind->dupls) {
23761 /*
23762 * Move the duplicates.
23763 */
23764 if (parBind->dupls != NULL)
23765 xmlSchemaItemListFree(parBind->dupls);
23766 parBind->dupls = bind->dupls;
Daniel Veillarddee23482008-04-11 12:58:43 +000023767 bind->dupls = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023768 }
Daniel Veillard76d36452009-09-07 11:19:33 +020023769 if (parTable != NULL) {
23770 if (*parTable == NULL)
23771 *parTable = parBind;
23772 else {
23773 parBind->next = *parTable;
23774 *parTable = parBind;
23775 }
23776 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023777 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023778
23779next_binding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023780 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023781 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023782 return (0);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023783
23784internal_error:
23785 return(-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023786}
23787
23788/**
23789 * xmlSchemaCheckCVCIDCKeyRef:
23790 * @vctxt: the WXS validation context
23791 * @elemDecl: the element declaration
23792 *
23793 * Check the cvc-idc-keyref constraints.
23794 */
23795static int
23796xmlSchemaCheckCVCIDCKeyRef(xmlSchemaValidCtxtPtr vctxt)
23797{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023798 xmlSchemaIDCMatcherPtr matcher;
23799 xmlSchemaPSVIIDCBindingPtr bind;
Daniel Veillarddee23482008-04-11 12:58:43 +000023800
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023801 matcher = vctxt->inode->idcMatchers;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023802 /*
23803 * Find a keyref.
23804 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023805 while (matcher != NULL) {
23806 if ((matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) &&
23807 matcher->targets &&
23808 matcher->targets->nbItems)
23809 {
23810 int i, j, k, res, nbFields, hasDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023811 xmlSchemaPSVIIDCKeyPtr *refKeys, *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023812 xmlSchemaPSVIIDCNodePtr refNode = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023813
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023814 nbFields = matcher->aidc->def->nbFields;
23815
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023816 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023817 * Find the IDC node-table for the referenced IDC key/unique.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023818 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023819 bind = vctxt->inode->idcTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023820 while (bind != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000023821 if ((xmlSchemaIDCPtr) matcher->aidc->def->ref->item ==
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023822 bind->definition)
23823 break;
23824 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023825 }
23826 hasDupls = (bind && bind->dupls && bind->dupls->nbItems) ? 1 : 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023827 /*
23828 * Search for a matching key-sequences.
23829 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023830 for (i = 0; i < matcher->targets->nbItems; i++) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023831 res = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023832 refNode = matcher->targets->items[i];
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023833 if (bind != NULL) {
23834 refKeys = refNode->keys;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023835 for (j = 0; j < bind->nbNodes; j++) {
23836 keys = bind->nodeTable[j]->keys;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023837 for (k = 0; k < nbFields; k++) {
23838 res = xmlSchemaAreValuesEqual(keys[k]->val,
23839 refKeys[k]->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023840 if (res == 0)
23841 break;
23842 else if (res == -1) {
23843 return (-1);
23844 }
23845 }
23846 if (res == 1) {
23847 /*
23848 * Match found.
23849 */
23850 break;
23851 }
23852 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023853 if ((res == 0) && hasDupls) {
23854 /*
23855 * Search in duplicates
23856 */
23857 for (j = 0; j < bind->dupls->nbItems; j++) {
23858 keys = ((xmlSchemaPSVIIDCNodePtr)
23859 bind->dupls->items[j])->keys;
23860 for (k = 0; k < nbFields; k++) {
23861 res = xmlSchemaAreValuesEqual(keys[k]->val,
23862 refKeys[k]->val);
23863 if (res == 0)
23864 break;
23865 else if (res == -1) {
23866 return (-1);
23867 }
23868 }
23869 if (res == 1) {
23870 /*
23871 * Match in duplicates found.
23872 */
23873 xmlChar *str = NULL, *strB = NULL;
23874 xmlSchemaKeyrefErr(vctxt,
23875 XML_SCHEMAV_CVC_IDC, refNode,
23876 (xmlSchemaTypePtr) matcher->aidc->def,
23877 "More than one match found for "
23878 "key-sequence %s of keyref '%s'",
23879 xmlSchemaFormatIDCKeySequence(vctxt, &str,
23880 refNode->keys, nbFields),
23881 xmlSchemaGetComponentQName(&strB,
23882 matcher->aidc->def));
23883 FREE_AND_NULL(str);
23884 FREE_AND_NULL(strB);
23885 break;
23886 }
23887 }
23888 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023889 }
Daniel Veillarddee23482008-04-11 12:58:43 +000023890
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023891 if (res == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023892 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023893 xmlSchemaKeyrefErr(vctxt,
23894 XML_SCHEMAV_CVC_IDC, refNode,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023895 (xmlSchemaTypePtr) matcher->aidc->def,
23896 "No match found for key-sequence %s of keyref '%s'",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000023897 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023898 refNode->keys, nbFields),
23899 xmlSchemaGetComponentQName(&strB, matcher->aidc->def));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023900 FREE_AND_NULL(str);
23901 FREE_AND_NULL(strB);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023902 }
23903 }
23904 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023905 matcher = matcher->next;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023906 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023907 /* TODO: Return an error if any error encountered. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023908 return (0);
23909}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023910
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023911/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080023912 * *
23913 * XML Reader validation code *
23914 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023915 ************************************************************************/
23916
23917static xmlSchemaAttrInfoPtr
23918xmlSchemaGetFreshAttrInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023919{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023920 xmlSchemaAttrInfoPtr iattr;
23921 /*
23922 * Grow/create list of attribute infos.
23923 */
23924 if (vctxt->attrInfos == NULL) {
23925 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23926 xmlMalloc(sizeof(xmlSchemaAttrInfoPtr));
23927 vctxt->sizeAttrInfos = 1;
23928 if (vctxt->attrInfos == NULL) {
23929 xmlSchemaVErrMemory(vctxt,
23930 "allocating attribute info list", NULL);
23931 return (NULL);
23932 }
23933 } else if (vctxt->sizeAttrInfos <= vctxt->nbAttrInfos) {
23934 vctxt->sizeAttrInfos++;
23935 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23936 xmlRealloc(vctxt->attrInfos,
23937 vctxt->sizeAttrInfos * sizeof(xmlSchemaAttrInfoPtr));
23938 if (vctxt->attrInfos == NULL) {
23939 xmlSchemaVErrMemory(vctxt,
23940 "re-allocating attribute info list", NULL);
23941 return (NULL);
23942 }
23943 } else {
23944 iattr = vctxt->attrInfos[vctxt->nbAttrInfos++];
23945 if (iattr->localName != NULL) {
23946 VERROR_INT("xmlSchemaGetFreshAttrInfo",
23947 "attr info not cleared");
23948 return (NULL);
23949 }
23950 iattr->nodeType = XML_ATTRIBUTE_NODE;
23951 return (iattr);
23952 }
23953 /*
23954 * Create an attribute info.
23955 */
23956 iattr = (xmlSchemaAttrInfoPtr)
23957 xmlMalloc(sizeof(xmlSchemaAttrInfo));
23958 if (iattr == NULL) {
23959 xmlSchemaVErrMemory(vctxt, "creating new attribute info", NULL);
23960 return (NULL);
23961 }
23962 memset(iattr, 0, sizeof(xmlSchemaAttrInfo));
23963 iattr->nodeType = XML_ATTRIBUTE_NODE;
23964 vctxt->attrInfos[vctxt->nbAttrInfos++] = iattr;
23965
23966 return (iattr);
23967}
23968
23969static int
23970xmlSchemaValidatorPushAttribute(xmlSchemaValidCtxtPtr vctxt,
23971 xmlNodePtr attrNode,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023972 int nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023973 const xmlChar *localName,
Daniel Veillarddee23482008-04-11 12:58:43 +000023974 const xmlChar *nsName,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023975 int ownedNames,
23976 xmlChar *value,
23977 int ownedValue)
23978{
23979 xmlSchemaAttrInfoPtr attr;
23980
23981 attr = xmlSchemaGetFreshAttrInfo(vctxt);
23982 if (attr == NULL) {
23983 VERROR_INT("xmlSchemaPushAttribute",
23984 "calling xmlSchemaGetFreshAttrInfo()");
23985 return (-1);
23986 }
23987 attr->node = attrNode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023988 attr->nodeLine = nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023989 attr->state = XML_SCHEMAS_ATTR_UNKNOWN;
23990 attr->localName = localName;
23991 attr->nsName = nsName;
23992 if (ownedNames)
23993 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
23994 /*
23995 * Evaluate if it's an XSI attribute.
23996 */
23997 if (nsName != NULL) {
23998 if (xmlStrEqual(localName, BAD_CAST "nil")) {
23999 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000024000 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NIL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024001 }
24002 } else if (xmlStrEqual(localName, BAD_CAST "type")) {
24003 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
24004 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_TYPE;
24005 }
24006 } else if (xmlStrEqual(localName, BAD_CAST "schemaLocation")) {
24007 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
24008 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC;
24009 }
24010 } else if (xmlStrEqual(localName, BAD_CAST "noNamespaceSchemaLocation")) {
24011 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
24012 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC;
24013 }
24014 } else if (xmlStrEqual(attr->nsName, xmlNamespaceNs)) {
24015 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XMLNS;
24016 }
24017 }
24018 attr->value = value;
24019 if (ownedValue)
24020 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
24021 if (attr->metaType != 0)
24022 attr->state = XML_SCHEMAS_ATTR_META;
24023 return (0);
24024}
24025
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000024026/**
24027 * xmlSchemaClearElemInfo:
24028 * @vctxt: the WXS validation context
24029 * @ielem: the element information item
24030 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024031static void
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000024032xmlSchemaClearElemInfo(xmlSchemaValidCtxtPtr vctxt,
24033 xmlSchemaNodeInfoPtr ielem)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024034{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024035 ielem->hasKeyrefs = 0;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000024036 ielem->appliedXPath = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024037 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
24038 FREE_AND_NULL(ielem->localName);
24039 FREE_AND_NULL(ielem->nsName);
24040 } else {
24041 ielem->localName = NULL;
24042 ielem->nsName = NULL;
24043 }
24044 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
24045 FREE_AND_NULL(ielem->value);
24046 } else {
24047 ielem->value = NULL;
24048 }
24049 if (ielem->val != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024050 /*
24051 * PSVI TODO: Be careful not to free it when the value is
24052 * exposed via PSVI.
24053 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024054 xmlSchemaFreeValue(ielem->val);
24055 ielem->val = NULL;
24056 }
24057 if (ielem->idcMatchers != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024058 /*
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000024059 * REVISIT OPTIMIZE TODO: Use a pool of IDC matchers.
24060 * Does it work?
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024061 */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000024062 xmlSchemaIDCReleaseMatcherList(vctxt, ielem->idcMatchers);
24063#if 0
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024064 xmlSchemaIDCFreeMatcherList(ielem->idcMatchers);
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000024065#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024066 ielem->idcMatchers = NULL;
24067 }
24068 if (ielem->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024069 /*
24070 * OPTIMIZE TODO: Use a pool of IDC tables??.
24071 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024072 xmlSchemaIDCFreeIDCTable(ielem->idcTable);
24073 ielem->idcTable = NULL;
24074 }
24075 if (ielem->regexCtxt != NULL) {
24076 xmlRegFreeExecCtxt(ielem->regexCtxt);
24077 ielem->regexCtxt = NULL;
24078 }
24079 if (ielem->nsBindings != NULL) {
24080 xmlFree((xmlChar **)ielem->nsBindings);
24081 ielem->nsBindings = NULL;
24082 ielem->nbNsBindings = 0;
24083 ielem->sizeNsBindings = 0;
24084 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024085}
24086
24087/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024088 * xmlSchemaGetFreshElemInfo:
24089 * @vctxt: the schema validation context
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024090 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024091 * Creates/reuses and initializes the element info item for
24092 * the currect tree depth.
24093 *
24094 * Returns the element info item or NULL on API or internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024095 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024096static xmlSchemaNodeInfoPtr
24097xmlSchemaGetFreshElemInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024098{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024099 xmlSchemaNodeInfoPtr info = NULL;
24100
24101 if (vctxt->depth > vctxt->sizeElemInfos) {
24102 VERROR_INT("xmlSchemaGetFreshElemInfo",
24103 "inconsistent depth encountered");
24104 return (NULL);
24105 }
24106 if (vctxt->elemInfos == NULL) {
24107 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
24108 xmlMalloc(10 * sizeof(xmlSchemaNodeInfoPtr));
24109 if (vctxt->elemInfos == NULL) {
24110 xmlSchemaVErrMemory(vctxt,
24111 "allocating the element info array", NULL);
24112 return (NULL);
24113 }
24114 memset(vctxt->elemInfos, 0, 10 * sizeof(xmlSchemaNodeInfoPtr));
24115 vctxt->sizeElemInfos = 10;
24116 } else if (vctxt->sizeElemInfos <= vctxt->depth) {
24117 int i = vctxt->sizeElemInfos;
24118
24119 vctxt->sizeElemInfos *= 2;
24120 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
24121 xmlRealloc(vctxt->elemInfos, vctxt->sizeElemInfos *
24122 sizeof(xmlSchemaNodeInfoPtr));
24123 if (vctxt->elemInfos == NULL) {
24124 xmlSchemaVErrMemory(vctxt,
24125 "re-allocating the element info array", NULL);
24126 return (NULL);
24127 }
24128 /*
24129 * We need the new memory to be NULLed.
24130 * TODO: Use memset instead?
24131 */
24132 for (; i < vctxt->sizeElemInfos; i++)
24133 vctxt->elemInfos[i] = NULL;
24134 } else
24135 info = vctxt->elemInfos[vctxt->depth];
24136
24137 if (info == NULL) {
24138 info = (xmlSchemaNodeInfoPtr)
24139 xmlMalloc(sizeof(xmlSchemaNodeInfo));
24140 if (info == NULL) {
24141 xmlSchemaVErrMemory(vctxt,
24142 "allocating an element info", NULL);
24143 return (NULL);
24144 }
24145 vctxt->elemInfos[vctxt->depth] = info;
24146 } else {
24147 if (info->localName != NULL) {
24148 VERROR_INT("xmlSchemaGetFreshElemInfo",
24149 "elem info has not been cleared");
24150 return (NULL);
24151 }
24152 }
24153 memset(info, 0, sizeof(xmlSchemaNodeInfo));
24154 info->nodeType = XML_ELEMENT_NODE;
24155 info->depth = vctxt->depth;
24156
24157 return (info);
24158}
24159
24160#define ACTIVATE_ATTRIBUTE(item) vctxt->inode = (xmlSchemaNodeInfoPtr) item;
24161#define ACTIVATE_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth];
24162#define ACTIVATE_PARENT_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth -1];
24163
24164static int
24165xmlSchemaValidateFacets(xmlSchemaAbstractCtxtPtr actxt,
24166 xmlNodePtr node,
24167 xmlSchemaTypePtr type,
24168 xmlSchemaValType valType,
24169 const xmlChar * value,
24170 xmlSchemaValPtr val,
24171 unsigned long length,
24172 int fireErrors)
24173{
24174 int ret, error = 0;
24175
24176 xmlSchemaTypePtr tmpType;
24177 xmlSchemaFacetLinkPtr facetLink;
24178 xmlSchemaFacetPtr facet;
24179 unsigned long len = 0;
24180 xmlSchemaWhitespaceValueType ws;
24181
24182 /*
24183 * In Libxml2, derived built-in types have currently no explicit facets.
24184 */
24185 if (type->type == XML_SCHEMA_TYPE_BASIC)
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000024186 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024187
24188 /*
24189 * NOTE: Do not jump away, if the facetSet of the given type is
24190 * empty: until now, "pattern" and "enumeration" facets of the
24191 * *base types* need to be checked as well.
24192 */
24193 if (type->facetSet == NULL)
24194 goto pattern_and_enum;
24195
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024196 if (! WXS_IS_ATOMIC(type)) {
24197 if (WXS_IS_LIST(type))
24198 goto WXS_IS_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024199 else
24200 goto pattern_and_enum;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024201 }
Scott Graham58b84e12015-06-30 10:47:16 +080024202
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024203 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024204 * Whitespace handling is only of importance for string-based
24205 * types.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024206 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024207 tmpType = xmlSchemaGetPrimitiveType(type);
24208 if ((tmpType->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024209 WXS_IS_ANY_SIMPLE_TYPE(tmpType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024210 ws = xmlSchemaGetWhiteSpaceFacetValue(type);
24211 } else
24212 ws = XML_SCHEMA_WHITESPACE_COLLAPSE;
Scott Graham58b84e12015-06-30 10:47:16 +080024213
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024214 /*
24215 * If the value was not computed (for string or
24216 * anySimpleType based types), then use the provided
24217 * type.
24218 */
Scott Graham58b84e12015-06-30 10:47:16 +080024219 if (val != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024220 valType = xmlSchemaGetValType(val);
Daniel Veillarddee23482008-04-11 12:58:43 +000024221
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024222 ret = 0;
24223 for (facetLink = type->facetSet; facetLink != NULL;
24224 facetLink = facetLink->next) {
24225 /*
24226 * Skip the pattern "whiteSpace": it is used to
24227 * format the character content beforehand.
24228 */
24229 switch (facetLink->facet->type) {
24230 case XML_SCHEMA_FACET_WHITESPACE:
24231 case XML_SCHEMA_FACET_PATTERN:
24232 case XML_SCHEMA_FACET_ENUMERATION:
24233 continue;
24234 case XML_SCHEMA_FACET_LENGTH:
24235 case XML_SCHEMA_FACET_MINLENGTH:
24236 case XML_SCHEMA_FACET_MAXLENGTH:
24237 ret = xmlSchemaValidateLengthFacetWhtsp(facetLink->facet,
24238 valType, value, val, &len, ws);
24239 break;
24240 default:
24241 ret = xmlSchemaValidateFacetWhtsp(facetLink->facet, ws,
24242 valType, value, val, ws);
24243 break;
24244 }
24245 if (ret < 0) {
24246 AERROR_INT("xmlSchemaValidateFacets",
24247 "validating against a atomic type facet");
24248 return (-1);
24249 } else if (ret > 0) {
24250 if (fireErrors)
24251 xmlSchemaFacetErr(actxt, ret, node,
24252 value, len, type, facetLink->facet, NULL, NULL, NULL);
24253 else
24254 return (ret);
24255 if (error == 0)
24256 error = ret;
24257 }
24258 ret = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000024259 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024260
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024261WXS_IS_LIST:
24262 if (! WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024263 goto pattern_and_enum;
24264 /*
24265 * "length", "minLength" and "maxLength" of list types.
24266 */
24267 ret = 0;
24268 for (facetLink = type->facetSet; facetLink != NULL;
24269 facetLink = facetLink->next) {
Daniel Veillarddee23482008-04-11 12:58:43 +000024270
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024271 switch (facetLink->facet->type) {
24272 case XML_SCHEMA_FACET_LENGTH:
24273 case XML_SCHEMA_FACET_MINLENGTH:
Daniel Veillarddee23482008-04-11 12:58:43 +000024274 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024275 ret = xmlSchemaValidateListSimpleTypeFacet(facetLink->facet,
24276 value, length, NULL);
24277 break;
24278 default:
24279 continue;
24280 }
24281 if (ret < 0) {
24282 AERROR_INT("xmlSchemaValidateFacets",
24283 "validating against a list type facet");
24284 return (-1);
24285 } else if (ret > 0) {
Daniel Veillarddee23482008-04-11 12:58:43 +000024286 if (fireErrors)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024287 xmlSchemaFacetErr(actxt, ret, node,
24288 value, length, type, facetLink->facet, NULL, NULL, NULL);
24289 else
24290 return (ret);
24291 if (error == 0)
24292 error = ret;
24293 }
24294 ret = 0;
24295 }
24296
24297pattern_and_enum:
24298 if (error >= 0) {
24299 int found = 0;
24300 /*
24301 * Process enumerations. Facet values are in the value space
24302 * of the defining type's base type. This seems to be a bug in the
24303 * XML Schema 1.0 spec. Use the whitespace type of the base type.
24304 * Only the first set of enumerations in the ancestor-or-self axis
24305 * is used for validation.
24306 */
24307 ret = 0;
24308 tmpType = type;
24309 do {
24310 for (facet = tmpType->facets; facet != NULL; facet = facet->next) {
24311 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
24312 continue;
24313 found = 1;
24314 ret = xmlSchemaAreValuesEqual(facet->val, val);
24315 if (ret == 1)
24316 break;
24317 else if (ret < 0) {
24318 AERROR_INT("xmlSchemaValidateFacets",
24319 "validating against an enumeration facet");
24320 return (-1);
24321 }
24322 }
24323 if (ret != 0)
24324 break;
Kasimier T. Buchcikb63d2fa2006-04-19 11:20:49 +000024325 /*
24326 * Break on the first set of enumerations. Any additional
24327 * enumerations which might be existent on the ancestors
24328 * of the current type are restricted by this set; thus
24329 * *must* *not* be taken into account.
24330 */
24331 if (found)
24332 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024333 tmpType = tmpType->baseType;
24334 } while ((tmpType != NULL) &&
24335 (tmpType->type != XML_SCHEMA_TYPE_BASIC));
24336 if (found && (ret == 0)) {
24337 ret = XML_SCHEMAV_CVC_ENUMERATION_VALID;
24338 if (fireErrors) {
24339 xmlSchemaFacetErr(actxt, ret, node,
24340 value, 0, type, NULL, NULL, NULL, NULL);
24341 } else
24342 return (ret);
24343 if (error == 0)
24344 error = ret;
24345 }
24346 }
24347
24348 if (error >= 0) {
24349 int found;
24350 /*
24351 * Process patters. Pattern facets are ORed at type level
24352 * and ANDed if derived. Walk the base type axis.
24353 */
24354 tmpType = type;
24355 facet = NULL;
24356 do {
24357 found = 0;
24358 for (facetLink = tmpType->facetSet; facetLink != NULL;
24359 facetLink = facetLink->next) {
24360 if (facetLink->facet->type != XML_SCHEMA_FACET_PATTERN)
24361 continue;
24362 found = 1;
Daniel Veillarddee23482008-04-11 12:58:43 +000024363 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024364 * NOTE that for patterns, @value needs to be the
24365 * normalized vaule.
24366 */
24367 ret = xmlRegexpExec(facetLink->facet->regexp, value);
24368 if (ret == 1)
24369 break;
24370 else if (ret < 0) {
24371 AERROR_INT("xmlSchemaValidateFacets",
24372 "validating against a pattern facet");
24373 return (-1);
24374 } else {
Daniel Veillarddee23482008-04-11 12:58:43 +000024375 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024376 * Save the last non-validating facet.
24377 */
24378 facet = facetLink->facet;
24379 }
24380 }
24381 if (found && (ret != 1)) {
24382 ret = XML_SCHEMAV_CVC_PATTERN_VALID;
24383 if (fireErrors) {
24384 xmlSchemaFacetErr(actxt, ret, node,
24385 value, 0, type, facet, NULL, NULL, NULL);
24386 } else
24387 return (ret);
24388 if (error == 0)
24389 error = ret;
24390 break;
24391 }
24392 tmpType = tmpType->baseType;
24393 } while ((tmpType != NULL) && (tmpType->type != XML_SCHEMA_TYPE_BASIC));
24394 }
24395
24396 return (error);
24397}
Daniel Veillarddee23482008-04-11 12:58:43 +000024398
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024399static xmlChar *
24400xmlSchemaNormalizeValue(xmlSchemaTypePtr type,
24401 const xmlChar *value)
24402{
Daniel Veillarddee23482008-04-11 12:58:43 +000024403 switch (xmlSchemaGetWhiteSpaceFacetValue(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024404 case XML_SCHEMA_WHITESPACE_COLLAPSE:
24405 return (xmlSchemaCollapseString(value));
24406 case XML_SCHEMA_WHITESPACE_REPLACE:
24407 return (xmlSchemaWhiteSpaceReplace(value));
24408 default:
24409 return (NULL);
24410 }
24411}
24412
24413static int
24414xmlSchemaValidateQName(xmlSchemaValidCtxtPtr vctxt,
24415 const xmlChar *value,
24416 xmlSchemaValPtr *val,
24417 int valNeeded)
24418{
24419 int ret;
24420 const xmlChar *nsName;
24421 xmlChar *local, *prefix = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000024422
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024423 ret = xmlValidateQName(value, 1);
24424 if (ret != 0) {
24425 if (ret == -1) {
24426 VERROR_INT("xmlSchemaValidateQName",
24427 "calling xmlValidateQName()");
24428 return (-1);
24429 }
24430 return( XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1);
24431 }
24432 /*
24433 * NOTE: xmlSplitQName2 will always return a duplicated
24434 * strings.
24435 */
24436 local = xmlSplitQName2(value, &prefix);
24437 if (local == NULL)
24438 local = xmlStrdup(value);
24439 /*
24440 * OPTIMIZE TODO: Use flags for:
24441 * - is there any namespace binding?
24442 * - is there a default namespace?
24443 */
24444 nsName = xmlSchemaLookupNamespace(vctxt, prefix);
Daniel Veillarddee23482008-04-11 12:58:43 +000024445
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024446 if (prefix != NULL) {
24447 xmlFree(prefix);
24448 /*
24449 * A namespace must be found if the prefix is
24450 * NOT NULL.
24451 */
24452 if (nsName == NULL) {
24453 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024454 xmlSchemaCustomErr(ACTXT_CAST vctxt, ret, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024455 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024456 "The QName value '%s' has no "
24457 "corresponding namespace declaration in "
24458 "scope", value, NULL);
24459 if (local != NULL)
24460 xmlFree(local);
24461 return (ret);
24462 }
24463 }
24464 if (valNeeded && val) {
24465 if (nsName != NULL)
24466 *val = xmlSchemaNewQNameValue(
24467 BAD_CAST xmlStrdup(nsName), BAD_CAST local);
24468 else
24469 *val = xmlSchemaNewQNameValue(NULL,
24470 BAD_CAST local);
24471 } else
24472 xmlFree(local);
24473 return (0);
24474}
24475
24476/*
24477* cvc-simple-type
24478*/
24479static int
24480xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
24481 xmlNodePtr node,
24482 xmlSchemaTypePtr type,
24483 const xmlChar *value,
24484 xmlSchemaValPtr *retVal,
24485 int fireErrors,
24486 int normalize,
24487 int isNormalized)
24488{
24489 int ret = 0, valNeeded = (retVal) ? 1 : 0;
24490 xmlSchemaValPtr val = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024491 /* xmlSchemaWhitespaceValueType ws; */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024492 xmlChar *normValue = NULL;
24493
24494#define NORMALIZE(atype) \
24495 if ((! isNormalized) && \
24496 (normalize || (type->flags & XML_SCHEMAS_TYPE_NORMVALUENEEDED))) { \
24497 normValue = xmlSchemaNormalizeValue(atype, value); \
24498 if (normValue != NULL) \
24499 value = normValue; \
24500 isNormalized = 1; \
24501 }
Daniel Veillarddee23482008-04-11 12:58:43 +000024502
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024503 if ((retVal != NULL) && (*retVal != NULL)) {
24504 xmlSchemaFreeValue(*retVal);
24505 *retVal = NULL;
24506 }
24507 /*
24508 * 3.14.4 Simple Type Definition Validation Rules
24509 * Validation Rule: String Valid
24510 */
24511 /*
24512 * 1 It is schema-valid with respect to that definition as defined
24513 * by Datatype Valid in [XML Schemas: Datatypes].
24514 */
24515 /*
24516 * 2.1 If The definition is ENTITY or is validly derived from ENTITY given
Jan Pokorný761c9e92013-11-29 23:26:27 +010024517 * the empty set, as defined in Type Derivation OK (Simple) ($3.14.6), then
24518 * the string must be a `declared entity name`.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024519 */
24520 /*
24521 * 2.2 If The definition is ENTITIES or is validly derived from ENTITIES
Jan Pokorný761c9e92013-11-29 23:26:27 +010024522 * given the empty set, as defined in Type Derivation OK (Simple) ($3.14.6),
24523 * then every whitespace-delimited substring of the string must be a `declared
24524 * entity name`.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024525 */
24526 /*
24527 * 2.3 otherwise no further condition applies.
24528 */
24529 if ((! valNeeded) && (type->flags & XML_SCHEMAS_TYPE_FACETSNEEDVALUE))
24530 valNeeded = 1;
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000024531 if (value == NULL)
24532 value = BAD_CAST "";
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024533 if (WXS_IS_ANY_SIMPLE_TYPE(type) || WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024534 xmlSchemaTypePtr biType; /* The built-in type. */
24535 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010024536 * SPEC (1.2.1) "if {variety} is `atomic` then the string must `match`
24537 * a literal in the `lexical space` of {base type definition}"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024538 */
24539 /*
24540 * Whitespace-normalize.
24541 */
24542 NORMALIZE(type);
24543 if (type->type != XML_SCHEMA_TYPE_BASIC) {
24544 /*
24545 * Get the built-in type.
24546 */
24547 biType = type->baseType;
24548 while ((biType != NULL) &&
24549 (biType->type != XML_SCHEMA_TYPE_BASIC))
24550 biType = biType->baseType;
24551
24552 if (biType == NULL) {
24553 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24554 "could not get the built-in type");
24555 goto internal_error;
24556 }
24557 } else
24558 biType = type;
24559 /*
24560 * NOTATIONs need to be processed here, since they need
24561 * to lookup in the hashtable of NOTATION declarations of the schema.
24562 */
Daniel Veillarddee23482008-04-11 12:58:43 +000024563 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
24564 switch (biType->builtInType) {
24565 case XML_SCHEMAS_NOTATION:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024566 ret = xmlSchemaValidateNotation(
24567 (xmlSchemaValidCtxtPtr) actxt,
24568 ((xmlSchemaValidCtxtPtr) actxt)->schema,
24569 NULL, value, &val, valNeeded);
24570 break;
24571 case XML_SCHEMAS_QNAME:
24572 ret = xmlSchemaValidateQName((xmlSchemaValidCtxtPtr) actxt,
24573 value, &val, valNeeded);
24574 break;
24575 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024576 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024577 if (valNeeded)
24578 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
Wayne Jensen99f7ac72009-08-07 21:27:44 +020024579 value, &val, node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024580 else
24581 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
Wayne Jensen99f7ac72009-08-07 21:27:44 +020024582 value, NULL, node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024583 break;
24584 }
Daniel Veillarddee23482008-04-11 12:58:43 +000024585 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
24586 switch (biType->builtInType) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024587 case XML_SCHEMAS_NOTATION:
24588 ret = xmlSchemaValidateNotation(NULL,
24589 ((xmlSchemaParserCtxtPtr) actxt)->schema, node,
24590 value, &val, valNeeded);
24591 break;
24592 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024593 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024594 if (valNeeded)
24595 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24596 value, &val, node);
24597 else
24598 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24599 value, NULL, node);
24600 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000024601 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024602 } else {
24603 /*
24604 * Validation via a public API is not implemented yet.
24605 */
24606 TODO
24607 goto internal_error;
24608 }
24609 if (ret != 0) {
24610 if (ret < 0) {
24611 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24612 "validating against a built-in type");
24613 goto internal_error;
24614 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024615 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024616 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24617 else
Daniel Veillarddee23482008-04-11 12:58:43 +000024618 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024619 }
24620 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24621 /*
24622 * Check facets.
24623 */
24624 ret = xmlSchemaValidateFacets(actxt, node, type,
24625 (xmlSchemaValType) biType->builtInType, value, val,
24626 0, fireErrors);
24627 if (ret != 0) {
24628 if (ret < 0) {
24629 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24630 "validating facets of atomic simple type");
24631 goto internal_error;
24632 }
Daniel Veillarddee23482008-04-11 12:58:43 +000024633 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024634 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24635 else
Daniel Veillarddee23482008-04-11 12:58:43 +000024636 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024637 }
24638 }
24639 if (fireErrors && (ret > 0))
24640 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024641 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024642
24643 xmlSchemaTypePtr itemType;
24644 const xmlChar *cur, *end;
24645 xmlChar *tmpValue = NULL;
24646 unsigned long len = 0;
24647 xmlSchemaValPtr prevVal = NULL, curVal = NULL;
Jan Pokorný761c9e92013-11-29 23:26:27 +010024648 /* 1.2.2 if {variety} is `list` then the string must be a sequence
24649 * of white space separated tokens, each of which `match`es a literal
24650 * in the `lexical space` of {item type definition}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024651 */
24652 /*
24653 * Note that XML_SCHEMAS_TYPE_NORMVALUENEEDED will be set if
24654 * the list type has an enum or pattern facet.
24655 */
24656 NORMALIZE(type);
24657 /*
24658 * VAL TODO: Optimize validation of empty values.
24659 * VAL TODO: We do not have computed values for lists.
24660 */
Daniel Veillarddee23482008-04-11 12:58:43 +000024661 itemType = WXS_LIST_ITEMTYPE(type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024662 cur = value;
24663 do {
24664 while (IS_BLANK_CH(*cur))
24665 cur++;
24666 end = cur;
24667 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
24668 end++;
24669 if (end == cur)
24670 break;
24671 tmpValue = xmlStrndup(cur, end - cur);
24672 len++;
24673
24674 if (valNeeded)
24675 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24676 tmpValue, &curVal, fireErrors, 0, 1);
24677 else
24678 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24679 tmpValue, NULL, fireErrors, 0, 1);
24680 FREE_AND_NULL(tmpValue);
24681 if (curVal != NULL) {
24682 /*
24683 * Add to list of computed values.
24684 */
24685 if (val == NULL)
24686 val = curVal;
24687 else
24688 xmlSchemaValueAppend(prevVal, curVal);
24689 prevVal = curVal;
24690 curVal = NULL;
24691 }
24692 if (ret != 0) {
24693 if (ret < 0) {
24694 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24695 "validating an item of list simple type");
24696 goto internal_error;
24697 }
24698 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24699 break;
Daniel Veillarddee23482008-04-11 12:58:43 +000024700 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024701 cur = end;
24702 } while (*cur != 0);
24703 FREE_AND_NULL(tmpValue);
24704 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24705 /*
24706 * Apply facets (pattern, enumeration).
24707 */
24708 ret = xmlSchemaValidateFacets(actxt, node, type,
24709 XML_SCHEMAS_UNKNOWN, value, val,
24710 len, fireErrors);
24711 if (ret != 0) {
24712 if (ret < 0) {
24713 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24714 "validating facets of list simple type");
24715 goto internal_error;
24716 }
24717 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24718 }
24719 }
24720 if (fireErrors && (ret > 0)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000024721 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024722 * Report the normalized value.
24723 */
24724 normalize = 1;
24725 NORMALIZE(type);
24726 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24727 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024728 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024729 xmlSchemaTypeLinkPtr memberLink;
24730 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010024731 * TODO: For all datatypes `derived` by `union` whiteSpace does
24732 * not apply directly; however, the normalization behavior of `union`
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024733 * types is controlled by the value of whiteSpace on that one of the
Jan Pokorný761c9e92013-11-29 23:26:27 +010024734 * `memberTypes` against which the `union` is successfully validated.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024735 *
24736 * This means that the value is normalized by the first validating
24737 * member type, then the facets of the union type are applied. This
24738 * needs changing of the value!
24739 */
24740
24741 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010024742 * 1.2.3 if {variety} is `union` then the string must `match` a
24743 * literal in the `lexical space` of at least one member of
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024744 * {member type definitions}
24745 */
24746 memberLink = xmlSchemaGetUnionSimpleTypeMemberTypes(type);
24747 if (memberLink == NULL) {
24748 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24749 "union simple type has no member types");
24750 goto internal_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000024751 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024752 /*
24753 * Always normalize union type values, since we currently
24754 * cannot store the whitespace information with the value
24755 * itself; otherwise a later value-comparison would be
24756 * not possible.
24757 */
24758 while (memberLink != NULL) {
Daniel Veillarddee23482008-04-11 12:58:43 +000024759 if (valNeeded)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024760 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24761 memberLink->type, value, &val, 0, 1, 0);
24762 else
24763 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24764 memberLink->type, value, NULL, 0, 1, 0);
24765 if (ret <= 0)
24766 break;
24767 memberLink = memberLink->next;
24768 }
24769 if (ret != 0) {
24770 if (ret < 0) {
24771 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24772 "validating members of union simple type");
24773 goto internal_error;
24774 }
24775 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24776 }
24777 /*
24778 * Apply facets (pattern, enumeration).
24779 */
24780 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24781 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010024782 * The normalization behavior of `union` types is controlled by
24783 * the value of whiteSpace on that one of the `memberTypes`
24784 * against which the `union` is successfully validated.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024785 */
24786 NORMALIZE(memberLink->type);
24787 ret = xmlSchemaValidateFacets(actxt, node, type,
24788 XML_SCHEMAS_UNKNOWN, value, val,
24789 0, fireErrors);
24790 if (ret != 0) {
24791 if (ret < 0) {
24792 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24793 "validating facets of union simple type");
24794 goto internal_error;
24795 }
Daniel Veillarddee23482008-04-11 12:58:43 +000024796 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024797 }
24798 }
24799 if (fireErrors && (ret > 0))
24800 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24801 }
24802
24803 if (normValue != NULL)
24804 xmlFree(normValue);
24805 if (ret == 0) {
24806 if (retVal != NULL)
24807 *retVal = val;
24808 else if (val != NULL)
24809 xmlSchemaFreeValue(val);
24810 } else if (val != NULL)
24811 xmlSchemaFreeValue(val);
24812 return (ret);
24813internal_error:
24814 if (normValue != NULL)
24815 xmlFree(normValue);
24816 if (val != NULL)
24817 xmlSchemaFreeValue(val);
24818 return (-1);
24819}
24820
24821static int
24822xmlSchemaVExpandQName(xmlSchemaValidCtxtPtr vctxt,
24823 const xmlChar *value,
24824 const xmlChar **nsName,
24825 const xmlChar **localName)
24826{
24827 int ret = 0;
24828
24829 if ((nsName == NULL) || (localName == NULL))
24830 return (-1);
24831 *nsName = NULL;
24832 *localName = NULL;
24833
24834 ret = xmlValidateQName(value, 1);
24835 if (ret == -1)
24836 return (-1);
24837 if (ret > 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024838 xmlSchemaSimpleTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024839 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
24840 value, xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), 1);
24841 return (1);
24842 }
24843 {
24844 xmlChar *local = NULL;
24845 xmlChar *prefix;
24846
24847 /*
24848 * NOTE: xmlSplitQName2 will return a duplicated
24849 * string.
24850 */
24851 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024852 if (local == NULL)
24853 *localName = xmlDictLookup(vctxt->dict, value, -1);
24854 else {
24855 *localName = xmlDictLookup(vctxt->dict, local, -1);
24856 xmlFree(local);
24857 }
24858
24859 *nsName = xmlSchemaLookupNamespace(vctxt, prefix);
24860
24861 if (prefix != NULL) {
24862 xmlFree(prefix);
24863 /*
24864 * A namespace must be found if the prefix is NOT NULL.
24865 */
24866 if (*nsName == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024867 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024868 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024869 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024870 "The QName value '%s' has no "
24871 "corresponding namespace declaration in scope",
24872 value, NULL);
24873 return (2);
24874 }
24875 }
24876 }
24877 return (0);
24878}
24879
24880static int
24881xmlSchemaProcessXSIType(xmlSchemaValidCtxtPtr vctxt,
24882 xmlSchemaAttrInfoPtr iattr,
24883 xmlSchemaTypePtr *localType,
24884 xmlSchemaElementPtr elemDecl)
24885{
24886 int ret = 0;
24887 /*
24888 * cvc-elt (3.3.4) : (4)
24889 * AND
24890 * Schema-Validity Assessment (Element) (cvc-assess-elt)
24891 * (1.2.1.2.1) - (1.2.1.2.4)
24892 * Handle 'xsi:type'.
24893 */
24894 if (localType == NULL)
24895 return (-1);
24896 *localType = NULL;
24897 if (iattr == NULL)
24898 return (0);
24899 else {
24900 const xmlChar *nsName = NULL, *local = NULL;
24901 /*
24902 * TODO: We should report a *warning* that the type was overriden
24903 * by the instance.
24904 */
24905 ACTIVATE_ATTRIBUTE(iattr);
24906 /*
24907 * (cvc-elt) (3.3.4) : (4.1)
24908 * (cvc-assess-elt) (1.2.1.2.2)
24909 */
24910 ret = xmlSchemaVExpandQName(vctxt, iattr->value,
24911 &nsName, &local);
24912 if (ret != 0) {
24913 if (ret < 0) {
24914 VERROR_INT("xmlSchemaValidateElementByDeclaration",
24915 "calling xmlSchemaQNameExpand() to validate the "
24916 "attribute 'xsi:type'");
24917 goto internal_error;
24918 }
24919 goto exit;
24920 }
24921 /*
24922 * (cvc-elt) (3.3.4) : (4.2)
24923 * (cvc-assess-elt) (1.2.1.2.3)
24924 */
24925 *localType = xmlSchemaGetType(vctxt->schema, local, nsName);
24926 if (*localType == NULL) {
24927 xmlChar *str = NULL;
24928
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024929 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024930 XML_SCHEMAV_CVC_ELT_4_2, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024931 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024932 "The QName value '%s' of the xsi:type attribute does not "
24933 "resolve to a type definition",
24934 xmlSchemaFormatQName(&str, nsName, local), NULL);
24935 FREE_AND_NULL(str);
24936 ret = vctxt->err;
24937 goto exit;
24938 }
24939 if (elemDecl != NULL) {
24940 int set = 0;
24941
24942 /*
24943 * SPEC cvc-elt (3.3.4) : (4.3) (Type Derivation OK)
Jan Pokorný761c9e92013-11-29 23:26:27 +010024944 * "The `local type definition` must be validly
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024945 * derived from the {type definition} given the union of
24946 * the {disallowed substitutions} and the {type definition}'s
24947 * {prohibited substitutions}, as defined in
Jan Pokorný761c9e92013-11-29 23:26:27 +010024948 * Type Derivation OK (Complex) ($3.4.6)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024949 * (if it is a complex type definition),
24950 * or given {disallowed substitutions} as defined in Type
Jan Pokorný761c9e92013-11-29 23:26:27 +010024951 * Derivation OK (Simple) ($3.14.6) (if it is a simple type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024952 * definition)."
24953 *
24954 * {disallowed substitutions}: the "block" on the element decl.
24955 * {prohibited substitutions}: the "block" on the type def.
24956 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024957 /*
24958 * OPTIMIZE TODO: We could map types already evaluated
24959 * to be validly derived from other types to avoid checking
24960 * this over and over for the same types.
24961 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024962 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) ||
24963 (elemDecl->subtypes->flags &
24964 XML_SCHEMAS_TYPE_BLOCK_EXTENSION))
24965 set |= SUBSET_EXTENSION;
24966
24967 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) ||
24968 (elemDecl->subtypes->flags &
24969 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION))
24970 set |= SUBSET_RESTRICTION;
24971
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024972 /*
24973 * REMOVED and CHANGED since this produced a parser context
24974 * which adds to the string dict of the schema. So this would
24975 * change the schema and we don't want this. We don't need
24976 * the parser context anymore.
24977 *
24978 * if ((vctxt->pctxt == NULL) &&
24979 * (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
24980 * return (-1);
24981 */
24982
24983 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST vctxt, *localType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024984 elemDecl->subtypes, set) != 0) {
24985 xmlChar *str = NULL;
24986
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024987 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024988 XML_SCHEMAV_CVC_ELT_4_3, NULL, NULL,
24989 "The type definition '%s', specified by xsi:type, is "
24990 "blocked or not validly derived from the type definition "
24991 "of the element declaration",
24992 xmlSchemaFormatQName(&str,
24993 (*localType)->targetNamespace,
24994 (*localType)->name),
24995 NULL);
24996 FREE_AND_NULL(str);
24997 ret = vctxt->err;
24998 *localType = NULL;
24999 }
25000 }
25001 }
25002exit:
25003 ACTIVATE_ELEM;
25004 return (ret);
25005internal_error:
25006 ACTIVATE_ELEM;
25007 return (-1);
25008}
25009
25010static int
25011xmlSchemaValidateElemDecl(xmlSchemaValidCtxtPtr vctxt)
25012{
25013 xmlSchemaElementPtr elemDecl = vctxt->inode->decl;
Daniel Veillard14b56432006-03-09 18:41:40 +000025014 xmlSchemaTypePtr actualType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025015
25016 /*
25017 * cvc-elt (3.3.4) : 1
25018 */
25019 if (elemDecl == NULL) {
25020 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL,
25021 "No matching declaration available");
25022 return (vctxt->err);
25023 }
Daniel Veillard14b56432006-03-09 18:41:40 +000025024 actualType = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025025 /*
25026 * cvc-elt (3.3.4) : 2
25027 */
25028 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) {
25029 VERROR(XML_SCHEMAV_CVC_ELT_2, NULL,
25030 "The element declaration is abstract");
25031 return (vctxt->err);
25032 }
25033 if (actualType == NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +080025034 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
25035 "The type definition is absent");
25036 return (XML_SCHEMAV_CVC_TYPE_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025037 }
25038 if (vctxt->nbAttrInfos != 0) {
25039 int ret;
25040 xmlSchemaAttrInfoPtr iattr;
25041 /*
25042 * cvc-elt (3.3.4) : 3
25043 * Handle 'xsi:nil'.
25044 */
25045 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25046 XML_SCHEMA_ATTR_INFO_META_XSI_NIL);
25047 if (iattr) {
25048 ACTIVATE_ATTRIBUTE(iattr);
25049 /*
25050 * Validate the value.
25051 */
25052 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025053 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025054 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
25055 iattr->value, &(iattr->val), 1, 0, 0);
25056 ACTIVATE_ELEM;
25057 if (ret < 0) {
25058 VERROR_INT("xmlSchemaValidateElemDecl",
25059 "calling xmlSchemaVCheckCVCSimpleType() to "
25060 "validate the attribute 'xsi:nil'");
25061 return (-1);
25062 }
25063 if (ret == 0) {
25064 if ((elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) {
25065 /*
25066 * cvc-elt (3.3.4) : 3.1
25067 */
25068 VERROR(XML_SCHEMAV_CVC_ELT_3_1, NULL,
25069 "The element is not 'nillable'");
25070 /* Does not return an error on purpose. */
25071 } else {
25072 if (xmlSchemaValueGetAsBoolean(iattr->val)) {
25073 /*
25074 * cvc-elt (3.3.4) : 3.2.2
25075 */
25076 if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) &&
25077 (elemDecl->value != NULL)) {
25078 VERROR(XML_SCHEMAV_CVC_ELT_3_2_2, NULL,
25079 "The element cannot be 'nilled' because "
25080 "there is a fixed value constraint defined "
25081 "for it");
25082 /* Does not return an error on purpose. */
25083 } else
25084 vctxt->inode->flags |=
25085 XML_SCHEMA_ELEM_INFO_NILLED;
25086 }
25087 }
25088 }
25089 }
25090 /*
25091 * cvc-elt (3.3.4) : 4
25092 * Handle 'xsi:type'.
25093 */
25094 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25095 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
25096 if (iattr) {
25097 xmlSchemaTypePtr localType = NULL;
25098
25099 ret = xmlSchemaProcessXSIType(vctxt, iattr, &localType,
25100 elemDecl);
25101 if (ret != 0) {
25102 if (ret == -1) {
25103 VERROR_INT("xmlSchemaValidateElemDecl",
25104 "calling xmlSchemaProcessXSIType() to "
25105 "process the attribute 'xsi:type'");
25106 return (-1);
25107 }
25108 /* Does not return an error on purpose. */
25109 }
25110 if (localType != NULL) {
25111 vctxt->inode->flags |= XML_SCHEMA_ELEM_INFO_LOCAL_TYPE;
25112 actualType = localType;
25113 }
25114 }
25115 }
25116 /*
25117 * IDC: Register identity-constraint XPath matchers.
25118 */
25119 if ((elemDecl->idcs != NULL) &&
25120 (xmlSchemaIDCRegisterMatchers(vctxt, elemDecl) == -1))
25121 return (-1);
25122 /*
25123 * No actual type definition.
25124 */
25125 if (actualType == NULL) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +080025126 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
25127 "The type definition is absent");
25128 return (XML_SCHEMAV_CVC_TYPE_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025129 }
25130 /*
25131 * Remember the actual type definition.
25132 */
25133 vctxt->inode->typeDef = actualType;
25134
25135 return (0);
25136}
25137
25138static int
25139xmlSchemaVAttributesSimple(xmlSchemaValidCtxtPtr vctxt)
25140{
25141 xmlSchemaAttrInfoPtr iattr;
25142 int ret = 0, i;
25143
25144 /*
25145 * SPEC cvc-type (3.1.1)
25146 * "The attributes of must be empty, excepting those whose namespace
25147 * name is identical to http://www.w3.org/2001/XMLSchema-instance and
25148 * whose local name is one of type, nil, schemaLocation or
25149 * noNamespaceSchemaLocation."
25150 */
25151 if (vctxt->nbAttrInfos == 0)
25152 return (0);
25153 for (i = 0; i < vctxt->nbAttrInfos; i++) {
25154 iattr = vctxt->attrInfos[i];
25155 if (! iattr->metaType) {
25156 ACTIVATE_ATTRIBUTE(iattr)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025157 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025158 XML_SCHEMAV_CVC_TYPE_3_1_1, iattr, NULL);
25159 ret = XML_SCHEMAV_CVC_TYPE_3_1_1;
25160 }
25161 }
25162 ACTIVATE_ELEM
25163 return (ret);
25164}
25165
25166/*
25167* Cleanup currently used attribute infos.
25168*/
25169static void
25170xmlSchemaClearAttrInfos(xmlSchemaValidCtxtPtr vctxt)
25171{
25172 int i;
25173 xmlSchemaAttrInfoPtr attr;
25174
25175 if (vctxt->nbAttrInfos == 0)
25176 return;
25177 for (i = 0; i < vctxt->nbAttrInfos; i++) {
25178 attr = vctxt->attrInfos[i];
25179 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
25180 if (attr->localName != NULL)
25181 xmlFree((xmlChar *) attr->localName);
25182 if (attr->nsName != NULL)
25183 xmlFree((xmlChar *) attr->nsName);
25184 }
25185 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
25186 if (attr->value != NULL)
25187 xmlFree((xmlChar *) attr->value);
25188 }
25189 if (attr->val != NULL) {
25190 xmlSchemaFreeValue(attr->val);
25191 attr->val = NULL;
25192 }
25193 memset(attr, 0, sizeof(xmlSchemaAttrInfo));
25194 }
25195 vctxt->nbAttrInfos = 0;
25196}
25197
25198/*
25199* 3.4.4 Complex Type Definition Validation Rules
25200* Element Locally Valid (Complex Type) (cvc-complex-type)
25201* 3.2.4 Attribute Declaration Validation Rules
25202* Validation Rule: Attribute Locally Valid (cvc-attribute)
25203* Attribute Locally Valid (Use) (cvc-au)
25204*
25205* Only "assessed" attribute information items will be visible to
25206* IDCs. I.e. not "lax" (without declaration) and "skip" wild attributes.
25207*/
25208static int
25209xmlSchemaVAttributesComplex(xmlSchemaValidCtxtPtr vctxt)
25210{
Daniel Veillarddee23482008-04-11 12:58:43 +000025211 xmlSchemaTypePtr type = vctxt->inode->typeDef;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025212 xmlSchemaItemListPtr attrUseList;
25213 xmlSchemaAttributeUsePtr attrUse = NULL;
25214 xmlSchemaAttributePtr attrDecl = NULL;
Daniel Veillarddee23482008-04-11 12:58:43 +000025215 xmlSchemaAttrInfoPtr iattr, tmpiattr;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025216 int i, j, found, nbAttrs, nbUses;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025217 int xpathRes = 0, res, wildIDs = 0, fixed;
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025218 xmlNodePtr defAttrOwnerElem = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025219
25220 /*
25221 * SPEC (cvc-attribute)
Jan Pokorný761c9e92013-11-29 23:26:27 +010025222 * (1) "The declaration must not be `absent` (see Missing
25223 * Sub-components ($5.3) for how this can fail to be
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025224 * the case)."
25225 * (2) "Its {type definition} must not be absent."
25226 *
25227 * NOTE (1) + (2): This is not handled here, since we currently do not
25228 * allow validation against schemas which have missing sub-components.
25229 *
25230 * SPEC (cvc-complex-type)
25231 * (3) "For each attribute information item in the element information
25232 * item's [attributes] excepting those whose [namespace name] is
25233 * identical to http://www.w3.org/2001/XMLSchema-instance and whose
25234 * [local name] is one of type, nil, schemaLocation or
25235 * noNamespaceSchemaLocation, the appropriate case among the following
25236 * must be true:
25237 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025238 */
25239 attrUseList = (xmlSchemaItemListPtr) type->attrUses;
25240 /*
25241 * @nbAttrs is the number of attributes present in the instance.
25242 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025243 nbAttrs = vctxt->nbAttrInfos;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025244 if (attrUseList != NULL)
25245 nbUses = attrUseList->nbItems;
25246 else
25247 nbUses = 0;
25248 for (i = 0; i < nbUses; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025249 found = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025250 attrUse = attrUseList->items[i];
25251 attrDecl = WXS_ATTRUSE_DECL(attrUse);
25252 for (j = 0; j < nbAttrs; j++) {
25253 iattr = vctxt->attrInfos[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025254 /*
25255 * SPEC (cvc-complex-type) (3)
25256 * Skip meta attributes.
25257 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025258 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025259 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025260 if (iattr->localName[0] != attrDecl->name[0])
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025261 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025262 if (!xmlStrEqual(iattr->localName, attrDecl->name))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025263 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025264 if (!xmlStrEqual(iattr->nsName, attrDecl->targetNamespace))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025265 continue;
25266 found = 1;
25267 /*
25268 * SPEC (cvc-complex-type)
25269 * (3.1) "If there is among the {attribute uses} an attribute
25270 * use with an {attribute declaration} whose {name} matches
25271 * the attribute information item's [local name] and whose
25272 * {target namespace} is identical to the attribute information
Jan Pokorný761c9e92013-11-29 23:26:27 +010025273 * item's [namespace name] (where an `absent` {target namespace}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025274 * is taken to be identical to a [namespace name] with no value),
Jan Pokorný761c9e92013-11-29 23:26:27 +010025275 * then the attribute information must be `valid` with respect
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025276 * to that attribute use as per Attribute Locally Valid (Use)
Jan Pokorný761c9e92013-11-29 23:26:27 +010025277 * ($3.5.4). In this case the {attribute declaration} of that
25278 * attribute use is the `context-determined declaration` for the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025279 * attribute information item with respect to Schema-Validity
Jan Pokorný761c9e92013-11-29 23:26:27 +010025280 * Assessment (Attribute) ($3.2.4) and
25281 * Assessment Outcome (Attribute) ($3.2.5).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025282 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025283 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
25284 iattr->use = attrUse;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025285 /*
25286 * Context-determined declaration.
25287 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025288 iattr->decl = attrDecl;
25289 iattr->typeDef = attrDecl->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025290 break;
25291 }
25292
25293 if (found)
25294 continue;
25295
25296 if (attrUse->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED) {
25297 /*
25298 * Handle non-existent, required attributes.
25299 *
25300 * SPEC (cvc-complex-type)
25301 * (4) "The {attribute declaration} of each attribute use in
25302 * the {attribute uses} whose {required} is true matches one
25303 * of the attribute information items in the element information
25304 * item's [attributes] as per clause 3.1 above."
25305 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025306 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
25307 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025308 VERROR_INT(
25309 "xmlSchemaVAttributesComplex",
25310 "calling xmlSchemaGetFreshAttrInfo()");
25311 return (-1);
25312 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025313 tmpiattr->state = XML_SCHEMAS_ATTR_ERR_MISSING;
25314 tmpiattr->use = attrUse;
Daniel Veillarddee23482008-04-11 12:58:43 +000025315 tmpiattr->decl = attrDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025316 } else if ((attrUse->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
25317 ((attrUse->defValue != NULL) ||
25318 (attrDecl->defValue != NULL))) {
25319 /*
25320 * Handle non-existent, optional, default/fixed attributes.
25321 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025322 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
25323 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025324 VERROR_INT(
25325 "xmlSchemaVAttributesComplex",
25326 "calling xmlSchemaGetFreshAttrInfo()");
25327 return (-1);
25328 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025329 tmpiattr->state = XML_SCHEMAS_ATTR_DEFAULT;
25330 tmpiattr->use = attrUse;
25331 tmpiattr->decl = attrDecl;
25332 tmpiattr->typeDef = attrDecl->subtypes;
25333 tmpiattr->localName = attrDecl->name;
25334 tmpiattr->nsName = attrDecl->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025335 }
25336 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025337
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025338 if (vctxt->nbAttrInfos == 0)
25339 return (0);
25340 /*
25341 * Validate against the wildcard.
25342 */
25343 if (type->attributeWildcard != NULL) {
25344 /*
25345 * SPEC (cvc-complex-type)
25346 * (3.2.1) "There must be an {attribute wildcard}."
25347 */
25348 for (i = 0; i < nbAttrs; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025349 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025350 /*
25351 * SPEC (cvc-complex-type) (3)
25352 * Skip meta attributes.
25353 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025354 if (iattr->state != XML_SCHEMAS_ATTR_UNKNOWN)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025355 continue;
25356 /*
25357 * SPEC (cvc-complex-type)
Jan Pokorný761c9e92013-11-29 23:26:27 +010025358 * (3.2.2) "The attribute information item must be `valid` with
25359 * respect to it as defined in Item Valid (Wildcard) ($3.10.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025360 *
25361 * SPEC Item Valid (Wildcard) (cvc-wildcard)
Jan Pokorný761c9e92013-11-29 23:26:27 +010025362 * "... its [namespace name] must be `valid` with respect to
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025363 * the wildcard constraint, as defined in Wildcard allows
Jan Pokorný761c9e92013-11-29 23:26:27 +010025364 * Namespace Name ($3.10.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025365 */
25366 if (xmlSchemaCheckCVCWildcardNamespace(type->attributeWildcard,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025367 iattr->nsName) == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025368 /*
25369 * Handle processContents.
25370 *
25371 * SPEC (cvc-wildcard):
25372 * processContents | context-determined declaration:
25373 * "strict" "mustFind"
25374 * "lax" "none"
25375 * "skip" "skip"
25376 */
25377 if (type->attributeWildcard->processContents ==
25378 XML_SCHEMAS_ANY_SKIP) {
25379 /*
25380 * context-determined declaration = "skip"
25381 *
25382 * SPEC PSVI Assessment Outcome (Attribute)
25383 * [validity] = "notKnown"
25384 * [validation attempted] = "none"
25385 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025386 iattr->state = XML_SCHEMAS_ATTR_WILD_SKIP;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025387 continue;
25388 }
25389 /*
25390 * Find an attribute declaration.
25391 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025392 iattr->decl = xmlSchemaGetAttributeDecl(vctxt->schema,
25393 iattr->localName, iattr->nsName);
25394 if (iattr->decl != NULL) {
25395 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025396 /*
25397 * SPEC (cvc-complex-type)
25398 * (5) "Let [Definition:] the wild IDs be the set of
25399 * all attribute information item to which clause 3.2
Jan Pokorný761c9e92013-11-29 23:26:27 +010025400 * applied and whose `validation` resulted in a
25401 * `context-determined declaration` of mustFind or no
25402 * `context-determined declaration` at all, and whose
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025403 * [local name] and [namespace name] resolve (as
Jan Pokorný761c9e92013-11-29 23:26:27 +010025404 * defined by QName resolution (Instance) ($3.15.4)) to
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025405 * an attribute declaration whose {type definition} is
25406 * or is derived from ID. Then all of the following
25407 * must be true:"
25408 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025409 iattr->typeDef = WXS_ATTR_TYPEDEF(iattr->decl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025410 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025411 iattr->typeDef, XML_SCHEMAS_ID)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025412 /*
25413 * SPEC (5.1) "There must be no more than one
Jan Pokorný761c9e92013-11-29 23:26:27 +010025414 * item in `wild IDs`."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025415 */
25416 if (wildIDs != 0) {
25417 /* VAL TODO */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025418 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025419 TODO
25420 continue;
25421 }
25422 wildIDs++;
25423 /*
25424 * SPEC (cvc-complex-type)
Jan Pokorný761c9e92013-11-29 23:26:27 +010025425 * (5.2) "If `wild IDs` is non-empty, there must not
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025426 * be any attribute uses among the {attribute uses}
25427 * whose {attribute declaration}'s {type definition}
25428 * is or is derived from ID."
25429 */
Daniel Veillard2cdd17e2009-07-27 21:16:13 +020025430 if (attrUseList != NULL) {
25431 for (j = 0; j < attrUseList->nbItems; j++) {
25432 if (xmlSchemaIsDerivedFromBuiltInType(
25433 WXS_ATTRUSE_TYPEDEF(attrUseList->items[j]),
25434 XML_SCHEMAS_ID)) {
25435 /* URGENT VAL TODO: implement */
25436 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID;
25437 TODO
25438 break;
25439 }
25440 }
25441 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025442 }
25443 } else if (type->attributeWildcard->processContents ==
25444 XML_SCHEMAS_ANY_LAX) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025445 iattr->state = XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025446 /*
25447 * SPEC PSVI Assessment Outcome (Attribute)
25448 * [validity] = "notKnown"
25449 * [validation attempted] = "none"
25450 */
25451 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025452 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025453 }
25454 }
25455 }
25456 }
25457
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025458 if (vctxt->nbAttrInfos == 0)
25459 return (0);
25460
25461 /*
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025462 * Get the owner element; needed for creation of default attributes.
25463 * This fixes bug #341337, reported by David Grohmann.
25464 */
25465 if (vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) {
25466 xmlSchemaNodeInfoPtr ielem = vctxt->elemInfos[vctxt->depth];
25467 if (ielem && ielem->node && ielem->node->doc)
25468 defAttrOwnerElem = ielem->node;
25469 }
25470 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025471 * Validate values, create default attributes, evaluate IDCs.
25472 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025473 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025474 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025475 /*
25476 * VAL TODO: Note that we won't try to resolve IDCs to
25477 * "lax" and "skip" validated attributes. Check what to
25478 * do in this case.
25479 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025480 if ((iattr->state != XML_SCHEMAS_ATTR_ASSESSED) &&
25481 (iattr->state != XML_SCHEMAS_ATTR_DEFAULT))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025482 continue;
25483 /*
25484 * VAL TODO: What to do if the type definition is missing?
25485 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025486 if (iattr->typeDef == NULL) {
25487 iattr->state = XML_SCHEMAS_ATTR_ERR_NO_TYPE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025488 continue;
25489 }
25490
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025491 ACTIVATE_ATTRIBUTE(iattr);
Kasimier T. Buchcik828f6542005-06-09 11:23:39 +000025492 fixed = 0;
25493 xpathRes = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025494
25495 if (vctxt->xpathStates != NULL) {
25496 /*
25497 * Evaluate IDCs.
25498 */
25499 xpathRes = xmlSchemaXPathEvaluate(vctxt,
25500 XML_ATTRIBUTE_NODE);
25501 if (xpathRes == -1) {
25502 VERROR_INT("xmlSchemaVAttributesComplex",
25503 "calling xmlSchemaXPathEvaluate()");
25504 goto internal_error;
25505 }
25506 }
25507
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025508 if (iattr->state == XML_SCHEMAS_ATTR_DEFAULT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025509 /*
25510 * Default/fixed attributes.
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025511 * We need the value only if we need to resolve IDCs or
25512 * will create default attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025513 */
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025514 if ((xpathRes) || (defAttrOwnerElem)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025515 if (iattr->use->defValue != NULL) {
25516 iattr->value = (xmlChar *) iattr->use->defValue;
25517 iattr->val = iattr->use->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025518 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025519 iattr->value = (xmlChar *) iattr->decl->defValue;
25520 iattr->val = iattr->decl->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025521 }
25522 /*
25523 * IDCs will consume the precomputed default value,
25524 * so we need to clone it.
25525 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025526 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025527 VERROR_INT("xmlSchemaVAttributesComplex",
25528 "default/fixed value on an attribute use was "
25529 "not precomputed");
25530 goto internal_error;
25531 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025532 iattr->val = xmlSchemaCopyValue(iattr->val);
25533 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025534 VERROR_INT("xmlSchemaVAttributesComplex",
25535 "calling xmlSchemaCopyValue()");
25536 goto internal_error;
25537 }
25538 }
25539 /*
25540 * PSVI: Add the default attribute to the current element.
25541 * VAL TODO: Should we use the *normalized* value? This currently
25542 * uses the *initial* value.
25543 */
Daniel Veillarddee23482008-04-11 12:58:43 +000025544
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025545 if (defAttrOwnerElem) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025546 xmlChar *normValue;
25547 const xmlChar *value;
25548
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025549 value = iattr->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025550 /*
25551 * Normalize the value.
25552 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025553 normValue = xmlSchemaNormalizeValue(iattr->typeDef,
25554 iattr->value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025555 if (normValue != NULL)
25556 value = BAD_CAST normValue;
25557
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025558 if (iattr->nsName == NULL) {
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025559 if (xmlNewProp(defAttrOwnerElem,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025560 iattr->localName, value) == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025561 VERROR_INT("xmlSchemaVAttributesComplex",
Alex Henrie73c50802015-10-26 18:33:14 +080025562 "calling xmlNewProp()");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025563 if (normValue != NULL)
25564 xmlFree(normValue);
25565 goto internal_error;
25566 }
25567 } else {
25568 xmlNsPtr ns;
25569
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025570 ns = xmlSearchNsByHref(defAttrOwnerElem->doc,
25571 defAttrOwnerElem, iattr->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025572 if (ns == NULL) {
25573 xmlChar prefix[12];
25574 int counter = 0;
25575
25576 /*
25577 * Create a namespace declaration on the validation
25578 * root node if no namespace declaration is in scope.
25579 */
25580 do {
25581 snprintf((char *) prefix, 12, "p%d", counter++);
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025582 ns = xmlSearchNs(defAttrOwnerElem->doc,
25583 defAttrOwnerElem, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025584 if (counter > 1000) {
25585 VERROR_INT(
25586 "xmlSchemaVAttributesComplex",
25587 "could not compute a ns prefix for a "
25588 "default/fixed attribute");
25589 if (normValue != NULL)
25590 xmlFree(normValue);
25591 goto internal_error;
25592 }
25593 } while (ns != NULL);
25594 ns = xmlNewNs(vctxt->validationRoot,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025595 iattr->nsName, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025596 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025597 /*
25598 * TODO:
25599 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0406.html
25600 * If we have QNames: do we need to ensure there's a
25601 * prefix defined for the QName?
25602 */
Kasimier T. Buchcik9b02e292006-05-11 16:25:44 +000025603 xmlNewNsProp(defAttrOwnerElem, ns, iattr->localName, value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025604 }
25605 if (normValue != NULL)
25606 xmlFree(normValue);
25607 }
25608 /*
25609 * Go directly to IDC evaluation.
25610 */
25611 goto eval_idcs;
25612 }
25613 /*
25614 * Validate the value.
25615 */
25616 if (vctxt->value != NULL) {
25617 /*
25618 * Free last computed value; just for safety reasons.
25619 */
25620 xmlSchemaFreeValue(vctxt->value);
25621 vctxt->value = NULL;
25622 }
25623 /*
25624 * Note that the attribute *use* can be unavailable, if
25625 * the attribute was a wild attribute.
25626 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025627 if ((iattr->decl->flags & XML_SCHEMAS_ATTR_FIXED) ||
25628 ((iattr->use != NULL) &&
25629 (iattr->use->flags & XML_SCHEMAS_ATTR_FIXED)))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025630 fixed = 1;
25631 else
25632 fixed = 0;
25633 /*
25634 * SPEC (cvc-attribute)
Jan Pokorný761c9e92013-11-29 23:26:27 +010025635 * (3) "The item's `normalized value` must be locally `valid`
Daniel Veillarddee23482008-04-11 12:58:43 +000025636 * with respect to that {type definition} as per
Jan Pokorný761c9e92013-11-29 23:26:27 +010025637 * String Valid ($3.14.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025638 *
25639 * VAL TODO: Do we already have the
25640 * "normalized attribute value" here?
25641 */
25642 if (xpathRes || fixed) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025643 iattr->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025644 /*
25645 * Request a computed value.
25646 */
25647 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025648 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025649 iattr->node, iattr->typeDef, iattr->value, &(iattr->val),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025650 1, 1, 0);
25651 } else {
25652 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025653 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025654 iattr->node, iattr->typeDef, iattr->value, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025655 1, 0, 0);
25656 }
Daniel Veillarddee23482008-04-11 12:58:43 +000025657
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025658 if (res != 0) {
25659 if (res == -1) {
25660 VERROR_INT("xmlSchemaVAttributesComplex",
25661 "calling xmlSchemaStreamValidateSimpleTypeValue()");
25662 goto internal_error;
25663 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025664 iattr->state = XML_SCHEMAS_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025665 /*
25666 * SPEC PSVI Assessment Outcome (Attribute)
25667 * [validity] = "invalid"
25668 */
25669 goto eval_idcs;
25670 }
25671
Daniel Veillarddee23482008-04-11 12:58:43 +000025672 if (fixed) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025673 /*
25674 * SPEC Attribute Locally Valid (Use) (cvc-au)
Jan Pokorný761c9e92013-11-29 23:26:27 +010025675 * "For an attribute information item to be `valid`
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025676 * with respect to an attribute use its *normalized*
Jan Pokorný761c9e92013-11-29 23:26:27 +010025677 * value must match the *canonical* lexical
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025678 * representation of the attribute use's {value
25679 * constraint}value, if it is present and fixed."
25680 *
25681 * VAL TODO: The requirement for the *canonical* value
25682 * will be removed in XML Schema 1.1.
25683 */
25684 /*
25685 * SPEC Attribute Locally Valid (cvc-attribute)
Jan Pokorný761c9e92013-11-29 23:26:27 +010025686 * (4) "The item's *actual* value must match the *value* of
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025687 * the {value constraint}, if it is present and fixed."
25688 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025689 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025690 /* VAL TODO: A value was not precomputed. */
25691 TODO
25692 goto eval_idcs;
25693 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025694 if ((iattr->use != NULL) &&
25695 (iattr->use->defValue != NULL)) {
25696 if (iattr->use->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025697 /* VAL TODO: A default value was not precomputed. */
25698 TODO
25699 goto eval_idcs;
25700 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025701 iattr->vcValue = iattr->use->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025702 /*
25703 if (xmlSchemaCompareValuesWhtsp(attr->val,
25704 (xmlSchemaWhitespaceValueType) ws,
25705 attr->use->defVal,
25706 (xmlSchemaWhitespaceValueType) ws) != 0) {
25707 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025708 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->use->defVal))
25709 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025710 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025711 if (iattr->decl->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025712 /* VAL TODO: A default value was not precomputed. */
25713 TODO
25714 goto eval_idcs;
25715 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025716 iattr->vcValue = iattr->decl->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025717 /*
25718 if (xmlSchemaCompareValuesWhtsp(attr->val,
25719 (xmlSchemaWhitespaceValueType) ws,
25720 attrDecl->defVal,
25721 (xmlSchemaWhitespaceValueType) ws) != 0) {
25722 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025723 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->decl->defVal))
25724 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025725 }
25726 /*
25727 * [validity] = "valid"
25728 */
25729 }
25730eval_idcs:
25731 /*
25732 * Evaluate IDCs.
25733 */
25734 if (xpathRes) {
25735 if (xmlSchemaXPathProcessHistory(vctxt,
25736 vctxt->depth +1) == -1) {
25737 VERROR_INT("xmlSchemaVAttributesComplex",
25738 "calling xmlSchemaXPathEvaluate()");
25739 goto internal_error;
25740 }
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000025741 } else if (vctxt->xpathStates != NULL)
25742 xmlSchemaXPathPop(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025743 }
25744
25745 /*
25746 * Report errors.
25747 */
25748 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025749 iattr = vctxt->attrInfos[i];
25750 if ((iattr->state == XML_SCHEMAS_ATTR_META) ||
25751 (iattr->state == XML_SCHEMAS_ATTR_ASSESSED) ||
25752 (iattr->state == XML_SCHEMAS_ATTR_WILD_SKIP) ||
25753 (iattr->state == XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025754 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025755 ACTIVATE_ATTRIBUTE(iattr);
25756 switch (iattr->state) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025757 case XML_SCHEMAS_ATTR_ERR_MISSING: {
25758 xmlChar *str = NULL;
25759 ACTIVATE_ELEM;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025760 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025761 XML_SCHEMAV_CVC_COMPLEX_TYPE_4, NULL, NULL,
25762 "The attribute '%s' is required but missing",
25763 xmlSchemaFormatQName(&str,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025764 iattr->decl->targetNamespace,
25765 iattr->decl->name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025766 NULL);
25767 FREE_AND_NULL(str)
25768 break;
25769 }
25770 case XML_SCHEMAS_ATTR_ERR_NO_TYPE:
25771 VERROR(XML_SCHEMAV_CVC_ATTRIBUTE_2, NULL,
25772 "The type definition is absent");
25773 break;
25774 case XML_SCHEMAS_ATTR_ERR_FIXED_VALUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025775 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025776 XML_SCHEMAV_CVC_AU, NULL, NULL,
25777 "The value '%s' does not match the fixed "
Daniel Veillarddee23482008-04-11 12:58:43 +000025778 "value constraint '%s'",
25779 iattr->value, iattr->vcValue);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025780 break;
25781 case XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL:
25782 VERROR(XML_SCHEMAV_CVC_WILDCARD, NULL,
25783 "No matching global attribute declaration available, but "
25784 "demanded by the strict wildcard");
25785 break;
25786 case XML_SCHEMAS_ATTR_UNKNOWN:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025787 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025788 break;
25789 /*
25790 * MAYBE VAL TODO: One might report different error messages
25791 * for the following errors.
25792 */
25793 if (type->attributeWildcard == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025794 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025795 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_1, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025796 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025797 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025798 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_2, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025799 }
25800 break;
25801 default:
25802 break;
25803 }
25804 }
25805
25806 ACTIVATE_ELEM;
25807 return (0);
25808internal_error:
25809 ACTIVATE_ELEM;
25810 return (-1);
25811}
25812
25813static int
25814xmlSchemaValidateElemWildcard(xmlSchemaValidCtxtPtr vctxt,
25815 int *skip)
25816{
25817 xmlSchemaWildcardPtr wild = (xmlSchemaWildcardPtr) vctxt->inode->decl;
25818 /*
25819 * The namespace of the element was already identified to be
25820 * matching the wildcard.
25821 */
25822 if ((skip == NULL) || (wild == NULL) ||
25823 (wild->type != XML_SCHEMA_TYPE_ANY)) {
25824 VERROR_INT("xmlSchemaValidateElemWildcard",
25825 "bad arguments");
25826 return (-1);
25827 }
25828 *skip = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025829 if (wild->processContents == XML_SCHEMAS_ANY_SKIP) {
25830 /*
25831 * URGENT VAL TODO: Either we need to position the stream to the
25832 * next sibling, or walk the whole subtree.
25833 */
25834 *skip = 1;
25835 return (0);
25836 }
25837 {
25838 xmlSchemaElementPtr decl = NULL;
25839
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025840 decl = xmlSchemaGetElem(vctxt->schema,
Daniel Veillarddee23482008-04-11 12:58:43 +000025841 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025842 if (decl != NULL) {
25843 vctxt->inode->decl = decl;
25844 return (0);
25845 }
25846 }
25847 if (wild->processContents == XML_SCHEMAS_ANY_STRICT) {
25848 /* VAL TODO: Change to proper error code. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025849 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL, /* WXS_BASIC_CAST wild */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025850 "No matching global element declaration available, but "
25851 "demanded by the strict wildcard");
25852 return (vctxt->err);
25853 }
25854 if (vctxt->nbAttrInfos != 0) {
25855 xmlSchemaAttrInfoPtr iattr;
25856 /*
25857 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25858 * (1.2.1.2.1) - (1.2.1.2.3 )
25859 *
25860 * Use the xsi:type attribute for the type definition.
25861 */
25862 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25863 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
25864 if (iattr != NULL) {
25865 if (xmlSchemaProcessXSIType(vctxt, iattr,
25866 &(vctxt->inode->typeDef), NULL) == -1) {
25867 VERROR_INT("xmlSchemaValidateElemWildcard",
25868 "calling xmlSchemaProcessXSIType() to "
25869 "process the attribute 'xsi:nil'");
25870 return (-1);
25871 }
25872 /*
25873 * Don't return an error on purpose.
25874 */
25875 return (0);
25876 }
25877 }
25878 /*
25879 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25880 *
25881 * Fallback to "anyType".
25882 */
25883 vctxt->inode->typeDef =
25884 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
25885 return (0);
25886}
25887
25888/*
25889* xmlSchemaCheckCOSValidDefault:
25890*
25891* This will be called if: not nilled, no content and a default/fixed
25892* value is provided.
25893*/
25894
25895static int
25896xmlSchemaCheckCOSValidDefault(xmlSchemaValidCtxtPtr vctxt,
25897 const xmlChar *value,
25898 xmlSchemaValPtr *val)
Daniel Veillarddee23482008-04-11 12:58:43 +000025899{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025900 int ret = 0;
25901 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25902
25903 /*
25904 * cos-valid-default:
25905 * Schema Component Constraint: Element Default Valid (Immediate)
Daniel Veillarddee23482008-04-11 12:58:43 +000025906 * For a string to be a valid default with respect to a type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025907 * definition the appropriate case among the following must be true:
Daniel Veillarddee23482008-04-11 12:58:43 +000025908 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025909 if WXS_IS_COMPLEX(inode->typeDef) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025910 /*
25911 * Complex type.
25912 *
25913 * SPEC (2.1) "its {content type} must be a simple type definition
25914 * or mixed."
25915 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
Jan Pokorný761c9e92013-11-29 23:26:27 +010025916 * type}'s particle must be `emptiable` as defined by
25917 * Particle Emptiable ($3.9.6)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025918 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025919 if ((! WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) &&
25920 ((! WXS_HAS_MIXED_CONTENT(inode->typeDef)) ||
25921 (! WXS_EMPTIABLE(inode->typeDef)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025922 ret = XML_SCHEMAP_COS_VALID_DEFAULT_2_1;
25923 /* NOTE that this covers (2.2.2) as well. */
25924 VERROR(ret, NULL,
25925 "For a string to be a valid default, the type definition "
25926 "must be a simple type or a complex type with simple content "
25927 "or mixed content and a particle emptiable");
25928 return(ret);
25929 }
Daniel Veillarddee23482008-04-11 12:58:43 +000025930 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025931 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000025932 * 1 If the type definition is a simple type definition, then the string
Jan Pokorný761c9e92013-11-29 23:26:27 +010025933 * must be `valid` with respect to that definition as defined by String
25934 * Valid ($3.14.4).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025935 *
25936 * AND
25937 *
Daniel Veillarddee23482008-04-11 12:58:43 +000025938 * 2.2.1 If the {content type} is a simple type definition, then the
Jan Pokorný761c9e92013-11-29 23:26:27 +010025939 * string must be `valid` with respect to that simple type definition
25940 * as defined by String Valid ($3.14.4).
Daniel Veillarddee23482008-04-11 12:58:43 +000025941 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025942 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025943
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025944 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025945 NULL, inode->typeDef, value, val, 1, 1, 0);
25946
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025947 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025948
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025949 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025950 NULL, inode->typeDef->contentTypeDef, value, val, 1, 1, 0);
25951 }
25952 if (ret < 0) {
25953 VERROR_INT("xmlSchemaCheckCOSValidDefault",
25954 "calling xmlSchemaVCheckCVCSimpleType()");
Daniel Veillarddee23482008-04-11 12:58:43 +000025955 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025956 return (ret);
25957}
25958
25959static void
25960xmlSchemaVContentModelCallback(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
25961 const xmlChar * name ATTRIBUTE_UNUSED,
25962 xmlSchemaElementPtr item,
25963 xmlSchemaNodeInfoPtr inode)
25964{
25965 inode->decl = item;
25966#ifdef DEBUG_CONTENT
25967 {
25968 xmlChar *str = NULL;
25969
25970 if (item->type == XML_SCHEMA_TYPE_ELEMENT) {
25971 xmlGenericError(xmlGenericErrorContext,
25972 "AUTOMATON callback for '%s' [declaration]\n",
25973 xmlSchemaFormatQName(&str,
25974 inode->localName, inode->nsName));
25975 } else {
25976 xmlGenericError(xmlGenericErrorContext,
25977 "AUTOMATON callback for '%s' [wildcard]\n",
25978 xmlSchemaFormatQName(&str,
25979 inode->localName, inode->nsName));
25980
25981 }
25982 FREE_AND_NULL(str)
25983 }
25984#endif
25985}
25986
25987static int
25988xmlSchemaValidatorPushElem(xmlSchemaValidCtxtPtr vctxt)
Daniel Veillarddee23482008-04-11 12:58:43 +000025989{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025990 vctxt->inode = xmlSchemaGetFreshElemInfo(vctxt);
25991 if (vctxt->inode == NULL) {
25992 VERROR_INT("xmlSchemaValidatorPushElem",
25993 "calling xmlSchemaGetFreshElemInfo()");
25994 return (-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000025995 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025996 vctxt->nbAttrInfos = 0;
25997 return (0);
25998}
25999
26000static int
26001xmlSchemaVCheckINodeDataType(xmlSchemaValidCtxtPtr vctxt,
26002 xmlSchemaNodeInfoPtr inode,
26003 xmlSchemaTypePtr type,
26004 const xmlChar *value)
26005{
26006 if (inode->flags & XML_SCHEMA_NODE_INFO_VALUE_NEEDED)
26007 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026008 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026009 type, value, &(inode->val), 1, 1, 0));
26010 else
26011 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026012 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026013 type, value, NULL, 1, 0, 0));
26014}
26015
26016
26017
Daniel Veillarddee23482008-04-11 12:58:43 +000026018/*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026019* Process END of element.
26020*/
26021static int
26022xmlSchemaValidatorPopElem(xmlSchemaValidCtxtPtr vctxt)
26023{
26024 int ret = 0;
26025 xmlSchemaNodeInfoPtr inode = vctxt->inode;
26026
26027 if (vctxt->nbAttrInfos != 0)
26028 xmlSchemaClearAttrInfos(vctxt);
26029 if (inode->flags & XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED) {
26030 /*
26031 * This element was not expected;
26032 * we will not validate child elements of broken parents.
26033 * Skip validation of all content of the parent.
26034 */
26035 vctxt->skipDepth = vctxt->depth -1;
26036 goto end_elem;
Daniel Veillarddee23482008-04-11 12:58:43 +000026037 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026038 if ((inode->typeDef == NULL) ||
26039 (inode->flags & XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE)) {
26040 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026041 * 1. the type definition might be missing if the element was
26042 * error prone
26043 * 2. it might be abstract.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026044 */
26045 goto end_elem;
26046 }
26047 /*
26048 * Check the content model.
26049 */
26050 if ((inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) ||
26051 (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)) {
26052
26053 /*
26054 * Workaround for "anyType".
26055 */
26056 if (inode->typeDef->builtInType == XML_SCHEMAS_ANYTYPE)
Daniel Veillarddee23482008-04-11 12:58:43 +000026057 goto character_content;
26058
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026059 if ((inode->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) == 0) {
26060 xmlChar *values[10];
26061 int terminal, nbval = 10, nbneg;
26062
26063 if (inode->regexCtxt == NULL) {
26064 /*
26065 * Create the regex context.
26066 */
26067 inode->regexCtxt =
26068 xmlRegNewExecCtxt(inode->typeDef->contModel,
26069 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
26070 vctxt);
26071 if (inode->regexCtxt == NULL) {
26072 VERROR_INT("xmlSchemaValidatorPopElem",
26073 "failed to create a regex context");
26074 goto internal_error;
26075 }
26076#ifdef DEBUG_AUTOMATA
26077 xmlGenericError(xmlGenericErrorContext,
26078 "AUTOMATON create on '%s'\n", inode->localName);
Daniel Veillarddee23482008-04-11 12:58:43 +000026079#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026080 }
Daniel Veillardc100e692013-02-28 19:02:32 +080026081
26082 /*
26083 * Do not check further content if the node has been nilled
26084 */
26085 if (INODE_NILLED(inode)) {
26086 ret = 0;
26087#ifdef DEBUG_AUTOMATA
26088 xmlGenericError(xmlGenericErrorContext,
26089 "AUTOMATON succeeded on nilled '%s'\n",
26090 inode->localName);
26091#endif
26092 goto skip_nilled;
26093 }
26094
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026095 /*
26096 * Get hold of the still expected content, since a further
26097 * call to xmlRegExecPushString() will loose this information.
Daniel Veillarddee23482008-04-11 12:58:43 +000026098 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026099 xmlRegExecNextValues(inode->regexCtxt,
26100 &nbval, &nbneg, &values[0], &terminal);
26101 ret = xmlRegExecPushString(inode->regexCtxt, NULL, NULL);
Daniel Veillard65fcf272007-07-11 17:55:30 +000026102 if ((ret<0) || ((ret==0) && (!INODE_NILLED(inode)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026103 /*
26104 * Still missing something.
26105 */
26106 ret = 1;
26107 inode->flags |=
26108 XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026109 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026110 XML_SCHEMAV_ELEMENT_CONTENT, NULL, NULL,
26111 "Missing child element(s)",
26112 nbval, nbneg, values);
26113#ifdef DEBUG_AUTOMATA
26114 xmlGenericError(xmlGenericErrorContext,
26115 "AUTOMATON missing ERROR on '%s'\n",
26116 inode->localName);
26117#endif
26118 } else {
26119 /*
26120 * Content model is satisfied.
26121 */
26122 ret = 0;
26123#ifdef DEBUG_AUTOMATA
26124 xmlGenericError(xmlGenericErrorContext,
26125 "AUTOMATON succeeded on '%s'\n",
26126 inode->localName);
26127#endif
26128 }
26129
26130 }
26131 }
Daniel Veillardc100e692013-02-28 19:02:32 +080026132
26133skip_nilled:
26134
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026135 if (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)
26136 goto end_elem;
26137
26138character_content:
26139
26140 if (vctxt->value != NULL) {
26141 xmlSchemaFreeValue(vctxt->value);
26142 vctxt->value = NULL;
26143 }
26144 /*
26145 * Check character content.
26146 */
26147 if (inode->decl == NULL) {
26148 /*
26149 * Speedup if no declaration exists.
26150 */
Daniel Veillarddee23482008-04-11 12:58:43 +000026151 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026152 ret = xmlSchemaVCheckINodeDataType(vctxt,
26153 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026154 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026155 ret = xmlSchemaVCheckINodeDataType(vctxt,
26156 inode, inode->typeDef->contentTypeDef,
26157 inode->value);
Daniel Veillarddee23482008-04-11 12:58:43 +000026158 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026159 if (ret < 0) {
26160 VERROR_INT("xmlSchemaValidatorPopElem",
26161 "calling xmlSchemaVCheckCVCSimpleType()");
26162 goto internal_error;
26163 }
26164 goto end_elem;
26165 }
26166 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026167 * cvc-elt (3.3.4) : 5
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026168 * The appropriate case among the following must be true:
26169 */
26170 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026171 * cvc-elt (3.3.4) : 5.1
26172 * If the declaration has a {value constraint},
26173 * the item has neither element nor character [children] and
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026174 * clause 3.2 has not applied, then all of the following must be true:
26175 */
26176 if ((inode->decl->value != NULL) &&
Daniel Veillarddee23482008-04-11 12:58:43 +000026177 (inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026178 (! INODE_NILLED(inode))) {
26179 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026180 * cvc-elt (3.3.4) : 5.1.1
Jan Pokorný761c9e92013-11-29 23:26:27 +010026181 * If the `actual type definition` is a `local type definition`
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026182 * then the canonical lexical representation of the {value constraint}
Jan Pokorný761c9e92013-11-29 23:26:27 +010026183 * value must be a valid default for the `actual type definition` as
26184 * defined in Element Default Valid (Immediate) ($3.3.6).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026185 */
Daniel Veillarddee23482008-04-11 12:58:43 +000026186 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026187 * NOTE: 'local' above means types acquired by xsi:type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026188 * NOTE: Although the *canonical* value is stated, it is not
26189 * relevant if canonical or not. Additionally XML Schema 1.1
26190 * will removed this requirement as well.
26191 */
26192 if (inode->flags & XML_SCHEMA_ELEM_INFO_LOCAL_TYPE) {
26193
26194 ret = xmlSchemaCheckCOSValidDefault(vctxt,
26195 inode->decl->value, &(inode->val));
26196 if (ret != 0) {
26197 if (ret < 0) {
26198 VERROR_INT("xmlSchemaValidatorPopElem",
26199 "calling xmlSchemaCheckCOSValidDefault()");
26200 goto internal_error;
26201 }
26202 goto end_elem;
26203 }
26204 /*
26205 * Stop here, to avoid redundant validation of the value
26206 * (see following).
26207 */
26208 goto default_psvi;
Daniel Veillarddee23482008-04-11 12:58:43 +000026209 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026210 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026211 * cvc-elt (3.3.4) : 5.1.2
26212 * The element information item with the canonical lexical
26213 * representation of the {value constraint} value used as its
Jan Pokorný761c9e92013-11-29 23:26:27 +010026214 * `normalized value` must be `valid` with respect to the
26215 * `actual type definition` as defined by Element Locally Valid (Type)
26216 * ($3.3.4).
Daniel Veillarddee23482008-04-11 12:58:43 +000026217 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026218 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026219 ret = xmlSchemaVCheckINodeDataType(vctxt,
26220 inode, inode->typeDef, inode->decl->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026221 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026222 ret = xmlSchemaVCheckINodeDataType(vctxt,
26223 inode, inode->typeDef->contentTypeDef,
Daniel Veillarddee23482008-04-11 12:58:43 +000026224 inode->decl->value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026225 }
26226 if (ret != 0) {
26227 if (ret < 0) {
26228 VERROR_INT("xmlSchemaValidatorPopElem",
26229 "calling xmlSchemaVCheckCVCSimpleType()");
26230 goto internal_error;
26231 }
26232 goto end_elem;
26233 }
26234
26235default_psvi:
26236 /*
26237 * PSVI: Create a text node on the instance element.
26238 */
26239 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
26240 (inode->node != NULL)) {
26241 xmlNodePtr textChild;
26242 xmlChar *normValue;
26243 /*
26244 * VAL TODO: Normalize the value.
Daniel Veillarddee23482008-04-11 12:58:43 +000026245 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026246 normValue = xmlSchemaNormalizeValue(inode->typeDef,
26247 inode->decl->value);
26248 if (normValue != NULL) {
26249 textChild = xmlNewText(BAD_CAST normValue);
26250 xmlFree(normValue);
26251 } else
26252 textChild = xmlNewText(inode->decl->value);
26253 if (textChild == NULL) {
26254 VERROR_INT("xmlSchemaValidatorPopElem",
26255 "calling xmlNewText()");
26256 goto internal_error;
26257 } else
Daniel Veillarddee23482008-04-11 12:58:43 +000026258 xmlAddChild(inode->node, textChild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026259 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026260
26261 } else if (! INODE_NILLED(inode)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026262 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010026263 * 5.2.1 The element information item must be `valid` with respect
26264 * to the `actual type definition` as defined by Element Locally
26265 * Valid (Type) ($3.3.4).
Daniel Veillarddee23482008-04-11 12:58:43 +000026266 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026267 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026268 /*
26269 * SPEC (cvc-type) (3.1)
26270 * "If the type definition is a simple type definition, ..."
26271 * (3.1.3) "If clause 3.2 of Element Locally Valid
Jan Pokorný761c9e92013-11-29 23:26:27 +010026272 * (Element) ($3.3.4) did not apply, then the `normalized value`
26273 * must be `valid` with respect to the type definition as defined
26274 * by String Valid ($3.14.4).
Daniel Veillarddee23482008-04-11 12:58:43 +000026275 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026276 ret = xmlSchemaVCheckINodeDataType(vctxt,
26277 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026278 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026279 /*
26280 * SPEC (cvc-type) (3.2) "If the type definition is a complex type
26281 * definition, then the element information item must be
Jan Pokorný761c9e92013-11-29 23:26:27 +010026282 * `valid` with respect to the type definition as per
26283 * Element Locally Valid (Complex Type) ($3.4.4);"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026284 *
26285 * SPEC (cvc-complex-type) (2.2)
Daniel Veillarddee23482008-04-11 12:58:43 +000026286 * "If the {content type} is a simple type definition, ...
Jan Pokorný761c9e92013-11-29 23:26:27 +010026287 * the `normalized value` of the element information item is
26288 * `valid` with respect to that simple type definition as
26289 * defined by String Valid ($3.14.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026290 */
26291 ret = xmlSchemaVCheckINodeDataType(vctxt,
26292 inode, inode->typeDef->contentTypeDef, inode->value);
Daniel Veillarddee23482008-04-11 12:58:43 +000026293 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026294 if (ret != 0) {
26295 if (ret < 0) {
26296 VERROR_INT("xmlSchemaValidatorPopElem",
26297 "calling xmlSchemaVCheckCVCSimpleType()");
26298 goto internal_error;
26299 }
26300 goto end_elem;
26301 }
26302 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026303 * 5.2.2 If there is a fixed {value constraint} and clause 3.2 has
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026304 * not applied, all of the following must be true:
26305 */
26306 if ((inode->decl->value != NULL) &&
26307 (inode->decl->flags & XML_SCHEMAS_ELEM_FIXED)) {
26308
26309 /*
26310 * TODO: We will need a computed value, when comparison is
26311 * done on computed values.
26312 */
26313 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026314 * 5.2.2.1 The element information item must have no element
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026315 * information item [children].
26316 */
26317 if (inode->flags &
26318 XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT) {
26319 ret = XML_SCHEMAV_CVC_ELT_5_2_2_1;
26320 VERROR(ret, NULL,
26321 "The content must not containt element nodes since "
26322 "there is a fixed value constraint");
26323 goto end_elem;
26324 } else {
26325 /*
Daniel Veillarddee23482008-04-11 12:58:43 +000026326 * 5.2.2.2 The appropriate case among the following must
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026327 * be true:
Daniel Veillarddee23482008-04-11 12:58:43 +000026328 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026329 if (WXS_HAS_MIXED_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026330 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010026331 * 5.2.2.2.1 If the {content type} of the `actual type
26332 * definition` is mixed, then the *initial value* of the
Daniel Veillarddee23482008-04-11 12:58:43 +000026333 * item must match the canonical lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026334 * of the {value constraint} value.
26335 *
Daniel Veillarddee23482008-04-11 12:58:43 +000026336 * ... the *initial value* of an element information
26337 * item is the string composed of, in order, the
26338 * [character code] of each character information item in
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026339 * the [children] of that element information item.
Daniel Veillarddee23482008-04-11 12:58:43 +000026340 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026341 if (! xmlStrEqual(inode->value, inode->decl->value)){
Daniel Veillarddee23482008-04-11 12:58:43 +000026342 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026343 * VAL TODO: Report invalid & expected values as well.
26344 * VAL TODO: Implement the canonical stuff.
26345 */
26346 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_1;
Daniel Veillarddee23482008-04-11 12:58:43 +000026347 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026348 ret, NULL, NULL,
26349 "The initial value '%s' does not match the fixed "
26350 "value constraint '%s'",
26351 inode->value, inode->decl->value);
26352 goto end_elem;
26353 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026354 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026355 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010026356 * 5.2.2.2.2 If the {content type} of the `actual type
26357 * definition` is a simple type definition, then the
Daniel Veillarddee23482008-04-11 12:58:43 +000026358 * *actual value* of the item must match the canonical
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026359 * lexical representation of the {value constraint} value.
26360 */
26361 /*
26362 * VAL TODO: *actual value* is the normalized value, impl.
26363 * this.
26364 * VAL TODO: Report invalid & expected values as well.
26365 * VAL TODO: Implement a comparison with the computed values.
26366 */
26367 if (! xmlStrEqual(inode->value,
26368 inode->decl->value)) {
26369 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_2;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026370 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026371 ret, NULL, NULL,
26372 "The actual value '%s' does not match the fixed "
Daniel Veillarddee23482008-04-11 12:58:43 +000026373 "value constraint '%s'",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026374 inode->value,
26375 inode->decl->value);
26376 goto end_elem;
Daniel Veillarddee23482008-04-11 12:58:43 +000026377 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026378 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026379 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026380 }
26381 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026382
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026383end_elem:
26384 if (vctxt->depth < 0) {
26385 /* TODO: raise error? */
26386 return (0);
26387 }
26388 if (vctxt->depth == vctxt->skipDepth)
26389 vctxt->skipDepth = -1;
26390 /*
26391 * Evaluate the history of XPath state objects.
Daniel Veillarddee23482008-04-11 12:58:43 +000026392 */
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000026393 if (inode->appliedXPath &&
26394 (xmlSchemaXPathProcessHistory(vctxt, vctxt->depth) == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026395 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026396 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026397 * MAYBE TODO:
Jan Pokorný761c9e92013-11-29 23:26:27 +010026398 * SPEC (6) "The element information item must be `valid` with
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026399 * respect to each of the {identity-constraint definitions} as per
Jan Pokorný761c9e92013-11-29 23:26:27 +010026400 * Identity-constraint Satisfied ($3.11.4)."
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026401 */
26402 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026403 * PSVI TODO: If we expose IDC node-tables via PSVI then the tables
26404 * need to be built in any case.
26405 * We will currently build IDC node-tables and bubble them only if
26406 * keyrefs do exist.
26407 */
Daniel Veillarddee23482008-04-11 12:58:43 +000026408
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026409 /*
26410 * Add the current IDC target-nodes to the IDC node-tables.
26411 */
26412 if ((inode->idcMatchers != NULL) &&
26413 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
26414 {
26415 if (xmlSchemaIDCFillNodeTables(vctxt, inode) == -1)
26416 goto internal_error;
26417 }
26418 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026419 * Validate IDC keyrefs.
26420 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026421 if (vctxt->inode->hasKeyrefs)
26422 if (xmlSchemaCheckCVCIDCKeyRef(vctxt) == -1)
26423 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026424 /*
26425 * Merge/free the IDC table.
26426 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026427 if (inode->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026428#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026429 xmlSchemaDebugDumpIDCTable(stdout,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026430 inode->nsName,
26431 inode->localName,
26432 inode->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026433#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026434 if ((vctxt->depth > 0) &&
26435 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
26436 {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026437 /*
26438 * Merge the IDC node table with the table of the parent node.
26439 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026440 if (xmlSchemaBubbleIDCNodeTables(vctxt) == -1)
26441 goto internal_error;
Daniel Veillarddee23482008-04-11 12:58:43 +000026442 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026443 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026444 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026445 * Clear the current ielem.
26446 * VAL TODO: Don't free the PSVI IDC tables if they are
26447 * requested for the PSVI.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026448 */
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000026449 xmlSchemaClearElemInfo(vctxt, inode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026450 /*
26451 * Skip further processing if we are on the validation root.
26452 */
26453 if (vctxt->depth == 0) {
26454 vctxt->depth--;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026455 vctxt->inode = NULL;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000026456 return (0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026457 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026458 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026459 * Reset the keyrefDepth if needed.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026460 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026461 if (vctxt->aidcs != NULL) {
26462 xmlSchemaIDCAugPtr aidc = vctxt->aidcs;
26463 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026464 if (aidc->keyrefDepth == vctxt->depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026465 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026466 * A 'keyrefDepth' of a key/unique IDC matches the current
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026467 * depth, this means that we are leaving the scope of the
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026468 * top-most keyref IDC which refers to this IDC.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026469 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026470 aidc->keyrefDepth = -1;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000026471 }
26472 aidc = aidc->next;
26473 } while (aidc != NULL);
26474 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026475 vctxt->depth--;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026476 vctxt->inode = vctxt->elemInfos[vctxt->depth];
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000026477 /*
Jan Pokorný761c9e92013-11-29 23:26:27 +010026478 * VAL TODO: 7 If the element information item is the `validation root`, it must be
26479 * `valid` per Validation Root Valid (ID/IDREF) ($3.3.4).
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026480 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026481 return (ret);
26482
26483internal_error:
26484 vctxt->err = -1;
26485 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026486}
26487
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026488/*
26489* 3.4.4 Complex Type Definition Validation Rules
26490* Validation Rule: Element Locally Valid (Complex Type) (cvc-complex-type)
26491*/
Daniel Veillardc0826a72004-08-10 14:17:33 +000026492static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026493xmlSchemaValidateChildElem(xmlSchemaValidCtxtPtr vctxt)
William M. Brack2f2a6632004-08-20 23:09:47 +000026494{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026495 xmlSchemaNodeInfoPtr pielem;
26496 xmlSchemaTypePtr ptype;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026497 int ret = 0;
Daniel Veillard3646d642004-06-02 19:19:14 +000026498
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026499 if (vctxt->depth <= 0) {
26500 VERROR_INT("xmlSchemaValidateChildElem",
26501 "not intended for the validation root");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026502 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026503 }
26504 pielem = vctxt->elemInfos[vctxt->depth -1];
26505 if (pielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26506 pielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026507 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026508 * Handle 'nilled' elements.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026509 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026510 if (INODE_NILLED(pielem)) {
26511 /*
26512 * SPEC (cvc-elt) (3.3.4) : (3.2.1)
26513 */
26514 ACTIVATE_PARENT_ELEM;
26515 ret = XML_SCHEMAV_CVC_ELT_3_2_1;
26516 VERROR(ret, NULL,
26517 "Neither character nor element content is allowed, "
26518 "because the element was 'nilled'");
26519 ACTIVATE_ELEM;
26520 goto unexpected_elem;
26521 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026522
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026523 ptype = pielem->typeDef;
26524
26525 if (ptype->builtInType == XML_SCHEMAS_ANYTYPE) {
26526 /*
26527 * Workaround for "anyType": we have currently no content model
26528 * assigned for "anyType", so handle it explicitely.
26529 * "anyType" has an unbounded, lax "any" wildcard.
26530 */
26531 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26532 vctxt->inode->localName,
26533 vctxt->inode->nsName);
26534
26535 if (vctxt->inode->decl == NULL) {
26536 xmlSchemaAttrInfoPtr iattr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026537 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026538 * Process "xsi:type".
26539 * SPEC (cvc-assess-elt) (1.2.1.2.1) - (1.2.1.2.3)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026540 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026541 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
26542 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
26543 if (iattr != NULL) {
26544 ret = xmlSchemaProcessXSIType(vctxt, iattr,
26545 &(vctxt->inode->typeDef), NULL);
26546 if (ret != 0) {
26547 if (ret == -1) {
26548 VERROR_INT("xmlSchemaValidateChildElem",
26549 "calling xmlSchemaProcessXSIType() to "
26550 "process the attribute 'xsi:nil'");
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000026551 return (-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000026552 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026553 return (ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +000026554 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026555 } else {
26556 /*
26557 * Fallback to "anyType".
26558 *
26559 * SPEC (cvc-assess-elt)
Jan Pokorný761c9e92013-11-29 23:26:27 +010026560 * "If the item cannot be `strictly assessed`, [...]
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026561 * an element information item's schema validity may be laxly
Jan Pokorný761c9e92013-11-29 23:26:27 +010026562 * assessed if its `context-determined declaration` is not
26563 * skip by `validating` with respect to the `ur-type
26564 * definition` as per Element Locally Valid (Type) ($3.3.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026565 */
26566 vctxt->inode->typeDef =
26567 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026568 }
26569 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026570 return (0);
26571 }
26572
26573 switch (ptype->contentType) {
26574 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026575 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026576 * SPEC (2.1) "If the {content type} is empty, then the
26577 * element information item has no character or element
26578 * information item [children]."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026579 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026580 ACTIVATE_PARENT_ELEM
26581 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1;
26582 VERROR(ret, NULL,
26583 "Element content is not allowed, "
26584 "because the content type is empty");
26585 ACTIVATE_ELEM
26586 goto unexpected_elem;
26587 break;
26588
26589 case XML_SCHEMA_CONTENT_MIXED:
26590 case XML_SCHEMA_CONTENT_ELEMENTS: {
26591 xmlRegExecCtxtPtr regexCtxt;
26592 xmlChar *values[10];
26593 int terminal, nbval = 10, nbneg;
26594
26595 /* VAL TODO: Optimized "anyType" validation.*/
26596
26597 if (ptype->contModel == NULL) {
26598 VERROR_INT("xmlSchemaValidateChildElem",
26599 "type has elem content but no content model");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026600 return (-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000026601 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026602 /*
26603 * Safety belf for evaluation if the cont. model was already
26604 * examined to be invalid.
26605 */
26606 if (pielem->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) {
26607 VERROR_INT("xmlSchemaValidateChildElem",
26608 "validating elem, but elem content is already invalid");
26609 return (-1);
26610 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +000026611
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026612 regexCtxt = pielem->regexCtxt;
26613 if (regexCtxt == NULL) {
26614 /*
26615 * Create the regex context.
26616 */
26617 regexCtxt = xmlRegNewExecCtxt(ptype->contModel,
26618 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
26619 vctxt);
26620 if (regexCtxt == NULL) {
26621 VERROR_INT("xmlSchemaValidateChildElem",
26622 "failed to create a regex context");
26623 return (-1);
26624 }
26625 pielem->regexCtxt = regexCtxt;
26626#ifdef DEBUG_AUTOMATA
26627 xmlGenericError(xmlGenericErrorContext, "AUTOMATA create on '%s'\n",
26628 pielem->localName);
26629#endif
26630 }
26631
26632 /*
26633 * SPEC (2.4) "If the {content type} is element-only or mixed,
26634 * then the sequence of the element information item's
26635 * element information item [children], if any, taken in
Jan Pokorný761c9e92013-11-29 23:26:27 +010026636 * order, is `valid` with respect to the {content type}'s
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026637 * particle, as defined in Element Sequence Locally Valid
Jan Pokorný761c9e92013-11-29 23:26:27 +010026638 * (Particle) ($3.9.4)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026639 */
26640 ret = xmlRegExecPushString2(regexCtxt,
26641 vctxt->inode->localName,
26642 vctxt->inode->nsName,
26643 vctxt->inode);
26644#ifdef DEBUG_AUTOMATA
26645 if (ret < 0)
26646 xmlGenericError(xmlGenericErrorContext,
26647 "AUTOMATON push ERROR for '%s' on '%s'\n",
26648 vctxt->inode->localName, pielem->localName);
26649 else
26650 xmlGenericError(xmlGenericErrorContext,
26651 "AUTOMATON push OK for '%s' on '%s'\n",
26652 vctxt->inode->localName, pielem->localName);
26653#endif
26654 if (vctxt->err == XML_SCHEMAV_INTERNAL) {
26655 VERROR_INT("xmlSchemaValidateChildElem",
26656 "calling xmlRegExecPushString2()");
26657 return (-1);
26658 }
26659 if (ret < 0) {
26660 xmlRegExecErrInfo(regexCtxt, NULL, &nbval, &nbneg,
26661 &values[0], &terminal);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026662 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026663 XML_SCHEMAV_ELEMENT_CONTENT, NULL,NULL,
26664 "This element is not expected",
26665 nbval, nbneg, values);
26666 ret = vctxt->err;
26667 goto unexpected_elem;
26668 } else
26669 ret = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026670 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026671 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026672 case XML_SCHEMA_CONTENT_SIMPLE:
26673 case XML_SCHEMA_CONTENT_BASIC:
26674 ACTIVATE_PARENT_ELEM
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026675 if (WXS_IS_COMPLEX(ptype)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026676 /*
26677 * SPEC (cvc-complex-type) (2.2)
26678 * "If the {content type} is a simple type definition, then
26679 * the element information item has no element information
26680 * item [children], ..."
26681 */
26682 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_2;
26683 VERROR(ret, NULL, "Element content is not allowed, "
26684 "because the content type is a simple type definition");
26685 } else {
26686 /*
26687 * SPEC (cvc-type) (3.1.2) "The element information item must
26688 * have no element information item [children]."
26689 */
26690 ret = XML_SCHEMAV_CVC_TYPE_3_1_2;
26691 VERROR(ret, NULL, "Element content is not allowed, "
26692 "because the type definition is simple");
26693 }
26694 ACTIVATE_ELEM
26695 ret = vctxt->err;
26696 goto unexpected_elem;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026697 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026698
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026699 default:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026700 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026701 }
26702 return (ret);
26703unexpected_elem:
26704 /*
26705 * Pop this element and set the skipDepth to skip
26706 * all further content of the parent element.
26707 */
26708 vctxt->skipDepth = vctxt->depth;
26709 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED;
26710 pielem->flags |= XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
26711 return (ret);
26712}
26713
26714#define XML_SCHEMA_PUSH_TEXT_PERSIST 1
26715#define XML_SCHEMA_PUSH_TEXT_CREATED 2
26716#define XML_SCHEMA_PUSH_TEXT_VOLATILE 3
26717
26718static int
26719xmlSchemaVPushText(xmlSchemaValidCtxtPtr vctxt,
26720 int nodeType, const xmlChar *value, int len,
26721 int mode, int *consumed)
26722{
26723 /*
26724 * Unfortunately we have to duplicate the text sometimes.
26725 * OPTIMIZE: Maybe we could skip it, if:
26726 * 1. content type is simple
26727 * 2. whitespace is "collapse"
26728 * 3. it consists of whitespace only
26729 *
26730 * Process character content.
26731 */
26732 if (consumed != NULL)
26733 *consumed = 0;
26734 if (INODE_NILLED(vctxt->inode)) {
Daniel Veillarddee23482008-04-11 12:58:43 +000026735 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026736 * SPEC cvc-elt (3.3.4 - 3.2.1)
26737 * "The element information item must have no character or
26738 * element information item [children]."
26739 */
26740 VERROR(XML_SCHEMAV_CVC_ELT_3_2_1, NULL,
26741 "Neither character nor element content is allowed "
26742 "because the element is 'nilled'");
26743 return (vctxt->err);
26744 }
26745 /*
26746 * SPEC (2.1) "If the {content type} is empty, then the
26747 * element information item has no character or element
26748 * information item [children]."
26749 */
26750 if (vctxt->inode->typeDef->contentType ==
Daniel Veillarddee23482008-04-11 12:58:43 +000026751 XML_SCHEMA_CONTENT_EMPTY) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026752 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1, NULL,
26753 "Character content is not allowed, "
26754 "because the content type is empty");
26755 return (vctxt->err);
26756 }
26757
26758 if (vctxt->inode->typeDef->contentType ==
26759 XML_SCHEMA_CONTENT_ELEMENTS) {
26760 if ((nodeType != XML_TEXT_NODE) ||
26761 (! xmlSchemaIsBlank((xmlChar *) value, len))) {
Daniel Veillarddee23482008-04-11 12:58:43 +000026762 /*
26763 * SPEC cvc-complex-type (2.3)
26764 * "If the {content type} is element-only, then the
26765 * element information item has no character information
26766 * item [children] other than those whose [character
26767 * code] is defined as a white space in [XML 1.0 (Second
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026768 * Edition)]."
26769 */
26770 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_3, NULL,
26771 "Character content other than whitespace is not allowed "
26772 "because the content type is 'element-only'");
26773 return (vctxt->err);
26774 }
26775 return (0);
26776 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026777
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026778 if ((value == NULL) || (value[0] == 0))
26779 return (0);
26780 /*
26781 * Save the value.
26782 * NOTE that even if the content type is *mixed*, we need the
26783 * *initial value* for default/fixed value constraints.
26784 */
26785 if ((vctxt->inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) &&
26786 ((vctxt->inode->decl == NULL) ||
26787 (vctxt->inode->decl->value == NULL)))
26788 return (0);
Daniel Veillarddee23482008-04-11 12:58:43 +000026789
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026790 if (vctxt->inode->value == NULL) {
26791 /*
26792 * Set the value.
26793 */
26794 switch (mode) {
26795 case XML_SCHEMA_PUSH_TEXT_PERSIST:
26796 /*
26797 * When working on a tree.
26798 */
26799 vctxt->inode->value = value;
26800 break;
26801 case XML_SCHEMA_PUSH_TEXT_CREATED:
26802 /*
26803 * When working with the reader.
26804 * The value will be freed by the element info.
26805 */
26806 vctxt->inode->value = value;
26807 if (consumed != NULL)
26808 *consumed = 1;
26809 vctxt->inode->flags |=
26810 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26811 break;
26812 case XML_SCHEMA_PUSH_TEXT_VOLATILE:
26813 /*
26814 * When working with SAX.
26815 * The value will be freed by the element info.
26816 */
26817 if (len != -1)
26818 vctxt->inode->value = BAD_CAST xmlStrndup(value, len);
26819 else
26820 vctxt->inode->value = BAD_CAST xmlStrdup(value);
26821 vctxt->inode->flags |=
26822 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26823 break;
26824 default:
26825 break;
26826 }
Kasimier T. Buchcik5bb0c082005-12-20 10:48:33 +000026827 } else {
26828 if (len < 0)
26829 len = xmlStrlen(value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026830 /*
26831 * Concat the value.
Daniel Veillarddee23482008-04-11 12:58:43 +000026832 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026833 if (vctxt->inode->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000026834 vctxt->inode->value = BAD_CAST xmlStrncat(
26835 (xmlChar *) vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026836 } else {
26837 vctxt->inode->value =
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026838 BAD_CAST xmlStrncatNew(vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026839 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26840 }
Daniel Veillarddee23482008-04-11 12:58:43 +000026841 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026842
26843 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000026844}
26845
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026846static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026847xmlSchemaValidateElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000026848{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026849 int ret = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000026850
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026851 if ((vctxt->skipDepth != -1) &&
26852 (vctxt->depth >= vctxt->skipDepth)) {
26853 VERROR_INT("xmlSchemaValidateElem",
26854 "in skip-state");
26855 goto internal_error;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026856 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026857 if (vctxt->xsiAssemble) {
Daniel Veillarddee23482008-04-11 12:58:43 +000026858 /*
Kasimier T. Buchcik215406f2005-12-07 12:14:09 +000026859 * We will stop validation if there was an error during
26860 * dynamic schema construction.
26861 * Note that we simply set @skipDepth to 0, this could
26862 * mean that a streaming document via SAX would be
26863 * still read to the end but it won't be validated any more.
26864 * TODO: If we are sure how to stop the validation at once
26865 * for all input scenarios, then this should be changed to
26866 * instantly stop the validation.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026867 */
Kasimier T. Buchcik215406f2005-12-07 12:14:09 +000026868 ret = xmlSchemaAssembleByXSI(vctxt);
26869 if (ret != 0) {
26870 if (ret == -1)
26871 goto internal_error;
26872 vctxt->skipDepth = 0;
26873 return(ret);
26874 }
Jim Panettac84e8632010-11-03 21:05:18 +010026875 /*
26876 * Augment the IDC definitions for the main schema and all imported ones
26877 * NOTE: main schema is the first in the imported list
26878 */
26879 xmlHashScan(vctxt->schema->schemasImports,(xmlHashScanner)xmlSchemaAugmentImportedIDC, vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026880 }
26881 if (vctxt->depth > 0) {
26882 /*
26883 * Validate this element against the content model
26884 * of the parent.
26885 */
26886 ret = xmlSchemaValidateChildElem(vctxt);
26887 if (ret != 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026888 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026889 VERROR_INT("xmlSchemaValidateElem",
26890 "calling xmlSchemaStreamValidateChildElement()");
26891 goto internal_error;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026892 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026893 goto exit;
26894 }
26895 if (vctxt->depth == vctxt->skipDepth)
26896 goto exit;
26897 if ((vctxt->inode->decl == NULL) &&
26898 (vctxt->inode->typeDef == NULL)) {
26899 VERROR_INT("xmlSchemaValidateElem",
26900 "the child element was valid but neither the "
26901 "declaration nor the type was set");
26902 goto internal_error;
26903 }
26904 } else {
26905 /*
26906 * Get the declaration of the validation root.
26907 */
26908 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26909 vctxt->inode->localName,
26910 vctxt->inode->nsName);
26911 if (vctxt->inode->decl == NULL) {
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026912 ret = XML_SCHEMAV_CVC_ELT_1;
26913 VERROR(ret, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026914 "No matching global declaration available "
26915 "for the validation root");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026916 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026917 }
26918 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026919
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026920 if (vctxt->inode->decl == NULL)
26921 goto type_validation;
26922
26923 if (vctxt->inode->decl->type == XML_SCHEMA_TYPE_ANY) {
26924 int skip;
26925 /*
26926 * Wildcards.
26927 */
26928 ret = xmlSchemaValidateElemWildcard(vctxt, &skip);
26929 if (ret != 0) {
26930 if (ret < 0) {
26931 VERROR_INT("xmlSchemaValidateElem",
26932 "calling xmlSchemaValidateElemWildcard()");
26933 goto internal_error;
26934 }
26935 goto exit;
26936 }
26937 if (skip) {
26938 vctxt->skipDepth = vctxt->depth;
26939 goto exit;
26940 }
26941 /*
26942 * The declaration might be set by the wildcard validation,
26943 * when the processContents is "lax" or "strict".
26944 */
26945 if (vctxt->inode->decl->type != XML_SCHEMA_TYPE_ELEMENT) {
26946 /*
26947 * Clear the "decl" field to not confuse further processing.
26948 */
26949 vctxt->inode->decl = NULL;
26950 goto type_validation;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026951 }
Daniel Veillard4255d502002-04-16 15:50:10 +000026952 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026953 /*
26954 * Validate against the declaration.
26955 */
26956 ret = xmlSchemaValidateElemDecl(vctxt);
26957 if (ret != 0) {
26958 if (ret < 0) {
26959 VERROR_INT("xmlSchemaValidateElem",
26960 "calling xmlSchemaValidateElemDecl()");
26961 goto internal_error;
26962 }
26963 goto exit;
26964 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026965 /*
26966 * Validate against the type definition.
26967 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026968type_validation:
26969
26970 if (vctxt->inode->typeDef == NULL) {
26971 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26972 ret = XML_SCHEMAV_CVC_TYPE_1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +080026973 VERROR(ret, NULL,
26974 "The type definition is absent");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026975 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000026976 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026977 if (vctxt->inode->typeDef->flags & XML_SCHEMAS_TYPE_ABSTRACT) {
26978 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26979 ret = XML_SCHEMAV_CVC_TYPE_2;
Daniel Veillardf8e3db02012-09-11 13:26:36 +080026980 VERROR(ret, NULL,
26981 "The type definition is abstract");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026982 goto exit;
26983 }
26984 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026985 * Evaluate IDCs. Do it here, since new IDC matchers are registered
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026986 * during validation against the declaration. This must be done
26987 * _before_ attribute validation.
26988 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026989 if (vctxt->xpathStates != NULL) {
26990 ret = xmlSchemaXPathEvaluate(vctxt, XML_ELEMENT_NODE);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000026991 vctxt->inode->appliedXPath = 1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026992 if (ret == -1) {
26993 VERROR_INT("xmlSchemaValidateElem",
26994 "calling xmlSchemaXPathEvaluate()");
26995 goto internal_error;
26996 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026997 }
26998 /*
26999 * Validate attributes.
27000 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000027001 if (WXS_IS_COMPLEX(vctxt->inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027002 if ((vctxt->nbAttrInfos != 0) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000027003 (vctxt->inode->typeDef->attrUses != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027004
27005 ret = xmlSchemaVAttributesComplex(vctxt);
27006 }
27007 } else if (vctxt->nbAttrInfos != 0) {
27008
27009 ret = xmlSchemaVAttributesSimple(vctxt);
27010 }
27011 /*
27012 * Clear registered attributes.
27013 */
27014 if (vctxt->nbAttrInfos != 0)
27015 xmlSchemaClearAttrInfos(vctxt);
27016 if (ret == -1) {
27017 VERROR_INT("xmlSchemaValidateElem",
27018 "calling attributes validation");
27019 goto internal_error;
27020 }
27021 /*
27022 * Don't return an error if attributes are invalid on purpose.
27023 */
27024 ret = 0;
27025
27026exit:
27027 if (ret != 0)
27028 vctxt->skipDepth = vctxt->depth;
27029 return (ret);
27030internal_error:
27031 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000027032}
27033
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027034#ifdef XML_SCHEMA_READER_ENABLED
27035static int
27036xmlSchemaVReaderWalk(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000027037{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027038 const int WHTSP = 13, SIGN_WHTSP = 14, END_ELEM = 15;
27039 int depth, nodeType, ret = 0, consumed;
27040 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000027041
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027042 vctxt->depth = -1;
27043 ret = xmlTextReaderRead(vctxt->reader);
27044 /*
27045 * Move to the document element.
27046 */
27047 while (ret == 1) {
27048 nodeType = xmlTextReaderNodeType(vctxt->reader);
27049 if (nodeType == XML_ELEMENT_NODE)
27050 goto root_found;
27051 ret = xmlTextReaderRead(vctxt->reader);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027052 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027053 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027054
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027055root_found:
27056
27057 do {
27058 depth = xmlTextReaderDepth(vctxt->reader);
27059 nodeType = xmlTextReaderNodeType(vctxt->reader);
27060
27061 if (nodeType == XML_ELEMENT_NODE) {
Daniel Veillarddee23482008-04-11 12:58:43 +000027062
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027063 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027064 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
27065 VERROR_INT("xmlSchemaVReaderWalk",
27066 "calling xmlSchemaValidatorPushElem()");
27067 goto internal_error;
27068 }
27069 ielem = vctxt->inode;
27070 ielem->localName = xmlTextReaderLocalName(vctxt->reader);
27071 ielem->nsName = xmlTextReaderNamespaceUri(vctxt->reader);
27072 ielem->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
27073 /*
27074 * Is the element empty?
27075 */
27076 ret = xmlTextReaderIsEmptyElement(vctxt->reader);
27077 if (ret == -1) {
27078 VERROR_INT("xmlSchemaVReaderWalk",
27079 "calling xmlTextReaderIsEmptyElement()");
27080 goto internal_error;
27081 }
27082 if (ret) {
27083 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
27084 }
27085 /*
27086 * Register attributes.
27087 */
27088 vctxt->nbAttrInfos = 0;
27089 ret = xmlTextReaderMoveToFirstAttribute(vctxt->reader);
27090 if (ret == -1) {
27091 VERROR_INT("xmlSchemaVReaderWalk",
27092 "calling xmlTextReaderMoveToFirstAttribute()");
27093 goto internal_error;
27094 }
27095 if (ret == 1) {
27096 do {
27097 /*
27098 * VAL TODO: How do we know that the reader works on a
27099 * node tree, to be able to pass a node here?
27100 */
27101 if (xmlSchemaValidatorPushAttribute(vctxt, NULL,
27102 (const xmlChar *) xmlTextReaderLocalName(vctxt->reader),
27103 xmlTextReaderNamespaceUri(vctxt->reader), 1,
27104 xmlTextReaderValue(vctxt->reader), 1) == -1) {
27105
27106 VERROR_INT("xmlSchemaVReaderWalk",
27107 "calling xmlSchemaValidatorPushAttribute()");
27108 goto internal_error;
27109 }
27110 ret = xmlTextReaderMoveToNextAttribute(vctxt->reader);
27111 if (ret == -1) {
27112 VERROR_INT("xmlSchemaVReaderWalk",
27113 "calling xmlTextReaderMoveToFirstAttribute()");
27114 goto internal_error;
27115 }
27116 } while (ret == 1);
27117 /*
27118 * Back to element position.
27119 */
27120 ret = xmlTextReaderMoveToElement(vctxt->reader);
27121 if (ret == -1) {
27122 VERROR_INT("xmlSchemaVReaderWalk",
27123 "calling xmlTextReaderMoveToElement()");
27124 goto internal_error;
27125 }
27126 }
27127 /*
27128 * Validate the element.
27129 */
27130 ret= xmlSchemaValidateElem(vctxt);
27131 if (ret != 0) {
27132 if (ret == -1) {
27133 VERROR_INT("xmlSchemaVReaderWalk",
27134 "calling xmlSchemaValidateElem()");
27135 goto internal_error;
27136 }
27137 goto exit;
27138 }
27139 if (vctxt->depth == vctxt->skipDepth) {
27140 int curDepth;
27141 /*
27142 * Skip all content.
27143 */
27144 if ((ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY) == 0) {
27145 ret = xmlTextReaderRead(vctxt->reader);
27146 curDepth = xmlTextReaderDepth(vctxt->reader);
27147 while ((ret == 1) && (curDepth != depth)) {
27148 ret = xmlTextReaderRead(vctxt->reader);
27149 curDepth = xmlTextReaderDepth(vctxt->reader);
27150 }
27151 if (ret < 0) {
27152 /*
27153 * VAL TODO: A reader error occured; what to do here?
27154 */
27155 ret = 1;
27156 goto exit;
27157 }
27158 }
27159 goto leave_elem;
27160 }
27161 /*
27162 * READER VAL TODO: Is an END_ELEM really never called
27163 * if the elem is empty?
27164 */
27165 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
27166 goto leave_elem;
27167 } else if (nodeType == END_ELEM) {
27168 /*
27169 * Process END of element.
27170 */
27171leave_elem:
27172 ret = xmlSchemaValidatorPopElem(vctxt);
27173 if (ret != 0) {
27174 if (ret < 0) {
27175 VERROR_INT("xmlSchemaVReaderWalk",
27176 "calling xmlSchemaValidatorPopElem()");
27177 goto internal_error;
27178 }
27179 goto exit;
27180 }
27181 if (vctxt->depth >= 0)
27182 ielem = vctxt->inode;
27183 else
27184 ielem = NULL;
27185 } else if ((nodeType == XML_TEXT_NODE) ||
27186 (nodeType == XML_CDATA_SECTION_NODE) ||
27187 (nodeType == WHTSP) ||
27188 (nodeType == SIGN_WHTSP)) {
27189 /*
27190 * Process character content.
27191 */
27192 xmlChar *value;
27193
27194 if ((nodeType == WHTSP) || (nodeType == SIGN_WHTSP))
27195 nodeType = XML_TEXT_NODE;
27196
27197 value = xmlTextReaderValue(vctxt->reader);
27198 ret = xmlSchemaVPushText(vctxt, nodeType, BAD_CAST value,
27199 -1, XML_SCHEMA_PUSH_TEXT_CREATED, &consumed);
27200 if (! consumed)
27201 xmlFree(value);
27202 if (ret == -1) {
27203 VERROR_INT("xmlSchemaVReaderWalk",
27204 "calling xmlSchemaVPushText()");
27205 goto internal_error;
27206 }
27207 } else if ((nodeType == XML_ENTITY_NODE) ||
27208 (nodeType == XML_ENTITY_REF_NODE)) {
27209 /*
27210 * VAL TODO: What to do with entities?
27211 */
27212 TODO
27213 }
27214 /*
27215 * Read next node.
27216 */
27217 ret = xmlTextReaderRead(vctxt->reader);
27218 } while (ret == 1);
27219
27220exit:
27221 return (ret);
27222internal_error:
27223 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000027224}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027225#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000027226
27227/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080027228 * *
27229 * SAX validation handlers *
27230 * *
Daniel Veillard4255d502002-04-16 15:50:10 +000027231 ************************************************************************/
27232
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027233/*
27234* Process text content.
27235*/
27236static void
Daniel Veillarddee23482008-04-11 12:58:43 +000027237xmlSchemaSAXHandleText(void *ctx,
27238 const xmlChar * ch,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027239 int len)
27240{
27241 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27242
27243 if (vctxt->depth < 0)
27244 return;
27245 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27246 return;
27247 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
27248 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27249 if (xmlSchemaVPushText(vctxt, XML_TEXT_NODE, ch, len,
27250 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
27251 VERROR_INT("xmlSchemaSAXHandleCDataSection",
27252 "calling xmlSchemaVPushText()");
27253 vctxt->err = -1;
27254 xmlStopParser(vctxt->parserCtxt);
27255 }
27256}
27257
27258/*
27259* Process CDATA content.
27260*/
27261static void
Daniel Veillarddee23482008-04-11 12:58:43 +000027262xmlSchemaSAXHandleCDataSection(void *ctx,
27263 const xmlChar * ch,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027264 int len)
Daniel Veillarddee23482008-04-11 12:58:43 +000027265{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027266 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27267
27268 if (vctxt->depth < 0)
27269 return;
27270 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27271 return;
27272 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
27273 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27274 if (xmlSchemaVPushText(vctxt, XML_CDATA_SECTION_NODE, ch, len,
27275 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
27276 VERROR_INT("xmlSchemaSAXHandleCDataSection",
27277 "calling xmlSchemaVPushText()");
27278 vctxt->err = -1;
27279 xmlStopParser(vctxt->parserCtxt);
27280 }
27281}
27282
27283static void
27284xmlSchemaSAXHandleReference(void *ctx ATTRIBUTE_UNUSED,
27285 const xmlChar * name ATTRIBUTE_UNUSED)
27286{
27287 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27288
27289 if (vctxt->depth < 0)
27290 return;
27291 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27292 return;
27293 /* SAX VAL TODO: What to do here? */
27294 TODO
27295}
27296
27297static void
27298xmlSchemaSAXHandleStartElementNs(void *ctx,
Daniel Veillarddee23482008-04-11 12:58:43 +000027299 const xmlChar * localname,
27300 const xmlChar * prefix ATTRIBUTE_UNUSED,
27301 const xmlChar * URI,
27302 int nb_namespaces,
27303 const xmlChar ** namespaces,
27304 int nb_attributes,
27305 int nb_defaulted ATTRIBUTE_UNUSED,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027306 const xmlChar ** attributes)
Daniel Veillarddee23482008-04-11 12:58:43 +000027307{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027308 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27309 int ret;
27310 xmlSchemaNodeInfoPtr ielem;
27311 int i, j;
Daniel Veillarddee23482008-04-11 12:58:43 +000027312
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027313 /*
27314 * SAX VAL TODO: What to do with nb_defaulted?
27315 */
27316 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027317 * Skip elements if inside a "skip" wildcard or invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027318 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027319 vctxt->depth++;
27320 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027321 return;
27322 /*
27323 * Push the element.
27324 */
27325 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
27326 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
27327 "calling xmlSchemaValidatorPushElem()");
27328 goto internal_error;
27329 }
27330 ielem = vctxt->inode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027331 /*
27332 * TODO: Is this OK?
27333 */
27334 ielem->nodeLine = xmlSAX2GetLineNumber(vctxt->parserCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027335 ielem->localName = localname;
27336 ielem->nsName = URI;
27337 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
27338 /*
27339 * Register namespaces on the elem info.
Daniel Veillarddee23482008-04-11 12:58:43 +000027340 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027341 if (nb_namespaces != 0) {
27342 /*
27343 * Although the parser builds its own namespace list,
27344 * we have no access to it, so we'll use an own one.
27345 */
Daniel Veillarddee23482008-04-11 12:58:43 +000027346 for (i = 0, j = 0; i < nb_namespaces; i++, j += 2) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027347 /*
27348 * Store prefix and namespace name.
Daniel Veillarddee23482008-04-11 12:58:43 +000027349 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027350 if (ielem->nsBindings == NULL) {
27351 ielem->nsBindings =
27352 (const xmlChar **) xmlMalloc(10 *
27353 sizeof(const xmlChar *));
27354 if (ielem->nsBindings == NULL) {
27355 xmlSchemaVErrMemory(vctxt,
27356 "allocating namespace bindings for SAX validation",
27357 NULL);
27358 goto internal_error;
27359 }
27360 ielem->nbNsBindings = 0;
27361 ielem->sizeNsBindings = 5;
27362 } else if (ielem->sizeNsBindings <= ielem->nbNsBindings) {
27363 ielem->sizeNsBindings *= 2;
27364 ielem->nsBindings =
27365 (const xmlChar **) xmlRealloc(
27366 (void *) ielem->nsBindings,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027367 ielem->sizeNsBindings * 2 * sizeof(const xmlChar *));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027368 if (ielem->nsBindings == NULL) {
27369 xmlSchemaVErrMemory(vctxt,
27370 "re-allocating namespace bindings for SAX validation",
27371 NULL);
27372 goto internal_error;
27373 }
27374 }
27375
27376 ielem->nsBindings[ielem->nbNsBindings * 2] = namespaces[j];
27377 if (namespaces[j+1][0] == 0) {
27378 /*
27379 * Handle xmlns="".
27380 */
27381 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] = NULL;
27382 } else
27383 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] =
27384 namespaces[j+1];
Daniel Veillarddee23482008-04-11 12:58:43 +000027385 ielem->nbNsBindings++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027386 }
27387 }
27388 /*
27389 * Register attributes.
27390 * SAX VAL TODO: We are not adding namespace declaration
27391 * attributes yet.
27392 */
27393 if (nb_attributes != 0) {
Alex Henrie31696022016-05-26 17:38:35 -060027394 int valueLen, k, l;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027395 xmlChar *value;
27396
27397 for (j = 0, i = 0; i < nb_attributes; i++, j += 5) {
27398 /*
Alex Henrief6599c52016-05-02 22:29:59 -060027399 * Duplicate the value, changing any &#38; to a literal ampersand.
27400 *
27401 * libxml2 differs from normal SAX here in that it escapes all ampersands
27402 * as &#38; instead of delivering the raw converted string. Changing the
27403 * behavior at this point would break applications that use this API, so
Alex Henrie31696022016-05-26 17:38:35 -060027404 * we are forced to work around it.
Daniel Veillarddee23482008-04-11 12:58:43 +000027405 */
Alex Henrie31696022016-05-26 17:38:35 -060027406 valueLen = attributes[j+4] - attributes[j+3];
27407 value = xmlMallocAtomic(valueLen + 1);
27408 if (value == NULL) {
27409 xmlSchemaVErrMemory(vctxt,
27410 "allocating string for decoded attribute",
27411 NULL);
27412 goto internal_error;
27413 }
27414 for (k = 0, l = 0; k < valueLen; l++) {
27415 if (k < valueLen - 4 &&
27416 attributes[j+3][k+0] == '&' &&
27417 attributes[j+3][k+1] == '#' &&
27418 attributes[j+3][k+2] == '3' &&
27419 attributes[j+3][k+3] == '8' &&
27420 attributes[j+3][k+4] == ';') {
27421 value[l] = '&';
27422 k += 5;
27423 } else {
27424 value[l] = attributes[j+3][k];
27425 k++;
27426 }
27427 }
27428 value[l] = '\0';
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027429 /*
27430 * TODO: Set the node line.
27431 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027432 ret = xmlSchemaValidatorPushAttribute(vctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027433 NULL, ielem->nodeLine, attributes[j], attributes[j+2], 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027434 value, 1);
27435 if (ret == -1) {
27436 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
27437 "calling xmlSchemaValidatorPushAttribute()");
27438 goto internal_error;
27439 }
27440 }
27441 }
27442 /*
27443 * Validate the element.
27444 */
27445 ret = xmlSchemaValidateElem(vctxt);
27446 if (ret != 0) {
27447 if (ret == -1) {
27448 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
27449 "calling xmlSchemaValidateElem()");
27450 goto internal_error;
27451 }
27452 goto exit;
Daniel Veillarddee23482008-04-11 12:58:43 +000027453 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027454
27455exit:
27456 return;
27457internal_error:
27458 vctxt->err = -1;
27459 xmlStopParser(vctxt->parserCtxt);
27460 return;
27461}
27462
27463static void
27464xmlSchemaSAXHandleEndElementNs(void *ctx,
27465 const xmlChar * localname ATTRIBUTE_UNUSED,
27466 const xmlChar * prefix ATTRIBUTE_UNUSED,
27467 const xmlChar * URI ATTRIBUTE_UNUSED)
27468{
27469 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
27470 int res;
27471
27472 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027473 * Skip elements if inside a "skip" wildcard or if invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027474 */
27475 if (vctxt->skipDepth != -1) {
27476 if (vctxt->depth > vctxt->skipDepth) {
27477 vctxt->depth--;
27478 return;
27479 } else
27480 vctxt->skipDepth = -1;
27481 }
27482 /*
27483 * SAX VAL TODO: Just a temporary check.
27484 */
27485 if ((!xmlStrEqual(vctxt->inode->localName, localname)) ||
27486 (!xmlStrEqual(vctxt->inode->nsName, URI))) {
27487 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
27488 "elem pop mismatch");
27489 }
27490 res = xmlSchemaValidatorPopElem(vctxt);
27491 if (res != 0) {
27492 if (res < 0) {
27493 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
27494 "calling xmlSchemaValidatorPopElem()");
27495 goto internal_error;
27496 }
27497 goto exit;
27498 }
27499exit:
27500 return;
27501internal_error:
27502 vctxt->err = -1;
27503 xmlStopParser(vctxt->parserCtxt);
27504 return;
27505}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027506
Daniel Veillard4255d502002-04-16 15:50:10 +000027507/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080027508 * *
27509 * Validation interfaces *
27510 * *
Daniel Veillard4255d502002-04-16 15:50:10 +000027511 ************************************************************************/
27512
27513/**
27514 * xmlSchemaNewValidCtxt:
27515 * @schema: a precompiled XML Schemas
27516 *
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027517 * Create an XML Schemas validation context based on the given schema.
Daniel Veillard4255d502002-04-16 15:50:10 +000027518 *
27519 * Returns the validation context or NULL in case of error
27520 */
27521xmlSchemaValidCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027522xmlSchemaNewValidCtxt(xmlSchemaPtr schema)
27523{
Daniel Veillard4255d502002-04-16 15:50:10 +000027524 xmlSchemaValidCtxtPtr ret;
27525
27526 ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt));
27527 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027528 xmlSchemaVErrMemory(NULL, "allocating validation context", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000027529 return (NULL);
27530 }
27531 memset(ret, 0, sizeof(xmlSchemaValidCtxt));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027532 ret->type = XML_SCHEMA_CTXT_VALIDATOR;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027533 ret->dict = xmlDictCreate();
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027534 ret->nodeQNames = xmlSchemaItemListCreate();
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027535 ret->schema = schema;
Daniel Veillard4255d502002-04-16 15:50:10 +000027536 return (ret);
27537}
27538
27539/**
Daniel Veillard97fa5b32012-08-14 11:01:07 +080027540 * xmlSchemaValidateSetFilename:
Daniel Veillardef4526a2012-08-15 09:14:31 +080027541 * @vctxt: the schema validation context
Daniel Veillard97fa5b32012-08-14 11:01:07 +080027542 * @filename: the file name
27543 *
27544 * Workaround to provide file error reporting information when this is
27545 * not provided by current APIs
27546 */
27547void
27548xmlSchemaValidateSetFilename(xmlSchemaValidCtxtPtr vctxt, const char *filename) {
27549 if (vctxt == NULL)
27550 return;
27551 if (vctxt->filename != NULL)
27552 xmlFree(vctxt->filename);
27553 if (filename != NULL)
27554 vctxt->filename = (char *) xmlStrdup((const xmlChar *) filename);
27555 else
27556 vctxt->filename = NULL;
27557}
27558
27559/**
27560 * xmlSchemaClearValidCtxt:
27561 * @vctxt: the schema validation context
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027562 *
27563 * Free the resources associated to the schema validation context;
27564 * leaves some fields alive intended for reuse of the context.
27565 */
27566static void
27567xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt)
27568{
27569 if (vctxt == NULL)
27570 return;
27571
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027572 /*
27573 * TODO: Should we clear the flags?
27574 * Might be problematic if one reuses the context
27575 * and assumes that the options remain the same.
27576 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000027577 vctxt->flags = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027578 vctxt->validationRoot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027579 vctxt->doc = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000027580#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027581 vctxt->reader = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000027582#endif
Daniel Veillarddee23482008-04-11 12:58:43 +000027583 vctxt->hasKeyrefs = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027584
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027585 if (vctxt->value != NULL) {
27586 xmlSchemaFreeValue(vctxt->value);
27587 vctxt->value = NULL;
27588 }
27589 /*
27590 * Augmented IDC information.
27591 */
27592 if (vctxt->aidcs != NULL) {
27593 xmlSchemaIDCAugPtr cur = vctxt->aidcs, next;
27594 do {
27595 next = cur->next;
27596 xmlFree(cur);
27597 cur = next;
27598 } while (cur != NULL);
27599 vctxt->aidcs = NULL;
27600 }
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027601 if (vctxt->idcMatcherCache != NULL) {
27602 xmlSchemaIDCMatcherPtr matcher = vctxt->idcMatcherCache, tmp;
27603
27604 while (matcher) {
27605 tmp = matcher;
27606 matcher = matcher->nextCached;
27607 xmlSchemaIDCFreeMatcherList(tmp);
27608 }
27609 vctxt->idcMatcherCache = NULL;
27610 }
27611
27612
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027613 if (vctxt->idcNodes != NULL) {
27614 int i;
27615 xmlSchemaPSVIIDCNodePtr item;
27616
27617 for (i = 0; i < vctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027618 item = vctxt->idcNodes[i];
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027619 xmlFree(item->keys);
27620 xmlFree(item);
27621 }
27622 xmlFree(vctxt->idcNodes);
27623 vctxt->idcNodes = NULL;
Daniel Veillard1131e112006-08-07 11:02:54 +000027624 vctxt->nbIdcNodes = 0;
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027625 vctxt->sizeIdcNodes = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027626 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027627 /*
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027628 * Note that we won't delete the XPath state pool here.
27629 */
27630 if (vctxt->xpathStates != NULL) {
27631 xmlSchemaFreeIDCStateObjList(vctxt->xpathStates);
27632 vctxt->xpathStates = NULL;
27633 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027634 /*
27635 * Attribute info.
27636 */
27637 if (vctxt->nbAttrInfos != 0) {
27638 xmlSchemaClearAttrInfos(vctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027639 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027640 /*
27641 * Element info.
27642 */
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027643 if (vctxt->elemInfos != NULL) {
27644 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027645 xmlSchemaNodeInfoPtr ei;
27646
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027647 for (i = 0; i < vctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027648 ei = vctxt->elemInfos[i];
27649 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027650 break;
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027651 xmlSchemaClearElemInfo(vctxt, ei);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027652 }
Daniel Veillarddee23482008-04-11 12:58:43 +000027653 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027654 xmlSchemaItemListClear(vctxt->nodeQNames);
27655 /* Recreate the dict. */
27656 xmlDictFree(vctxt->dict);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027657 /*
27658 * TODO: Is is save to recreate it? Do we have a scenario
27659 * where the user provides the dict?
27660 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027661 vctxt->dict = xmlDictCreate();
Daniel Veillard97fa5b32012-08-14 11:01:07 +080027662
27663 if (vctxt->filename != NULL) {
27664 xmlFree(vctxt->filename);
27665 vctxt->filename = NULL;
27666 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027667}
27668
27669/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027670 * xmlSchemaFreeValidCtxt:
27671 * @ctxt: the schema validation context
27672 *
27673 * Free the resources associated to the schema validation context
27674 */
27675void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027676xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt)
27677{
Daniel Veillard4255d502002-04-16 15:50:10 +000027678 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027679 return;
Daniel Veillard88c58912002-04-23 07:12:20 +000027680 if (ctxt->value != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027681 xmlSchemaFreeValue(ctxt->value);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027682 if (ctxt->pctxt != NULL)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027683 xmlSchemaFreeParserCtxt(ctxt->pctxt);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027684 if (ctxt->idcNodes != NULL) {
27685 int i;
27686 xmlSchemaPSVIIDCNodePtr item;
27687
27688 for (i = 0; i < ctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027689 item = ctxt->idcNodes[i];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027690 xmlFree(item->keys);
27691 xmlFree(item);
27692 }
27693 xmlFree(ctxt->idcNodes);
27694 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027695 if (ctxt->idcKeys != NULL) {
27696 int i;
27697 for (i = 0; i < ctxt->nbIdcKeys; i++)
27698 xmlSchemaIDCFreeKey(ctxt->idcKeys[i]);
27699 xmlFree(ctxt->idcKeys);
27700 }
27701
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027702 if (ctxt->xpathStates != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027703 xmlSchemaFreeIDCStateObjList(ctxt->xpathStates);
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027704 ctxt->xpathStates = NULL;
27705 }
27706 if (ctxt->xpathStatePool != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027707 xmlSchemaFreeIDCStateObjList(ctxt->xpathStatePool);
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027708 ctxt->xpathStatePool = NULL;
27709 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027710
27711 /*
27712 * Augmented IDC information.
27713 */
27714 if (ctxt->aidcs != NULL) {
27715 xmlSchemaIDCAugPtr cur = ctxt->aidcs, next;
27716 do {
27717 next = cur->next;
27718 xmlFree(cur);
27719 cur = next;
27720 } while (cur != NULL);
27721 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027722 if (ctxt->attrInfos != NULL) {
27723 int i;
27724 xmlSchemaAttrInfoPtr attr;
27725
27726 /* Just a paranoid call to the cleanup. */
27727 if (ctxt->nbAttrInfos != 0)
27728 xmlSchemaClearAttrInfos(ctxt);
27729 for (i = 0; i < ctxt->sizeAttrInfos; i++) {
27730 attr = ctxt->attrInfos[i];
27731 xmlFree(attr);
27732 }
27733 xmlFree(ctxt->attrInfos);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000027734 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027735 if (ctxt->elemInfos != NULL) {
27736 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027737 xmlSchemaNodeInfoPtr ei;
27738
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027739 for (i = 0; i < ctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027740 ei = ctxt->elemInfos[i];
27741 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027742 break;
Kasimier T. Buchcikdeb58712006-08-15 12:52:30 +000027743 xmlSchemaClearElemInfo(ctxt, ei);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027744 xmlFree(ei);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027745 }
27746 xmlFree(ctxt->elemInfos);
27747 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027748 if (ctxt->nodeQNames != NULL)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027749 xmlSchemaItemListFree(ctxt->nodeQNames);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027750 if (ctxt->dict != NULL)
27751 xmlDictFree(ctxt->dict);
Daniel Veillard97fa5b32012-08-14 11:01:07 +080027752 if (ctxt->filename != NULL)
27753 xmlFree(ctxt->filename);
Daniel Veillard4255d502002-04-16 15:50:10 +000027754 xmlFree(ctxt);
27755}
27756
27757/**
Daniel Veillardf10ae122005-07-10 19:03:16 +000027758 * xmlSchemaIsValid:
27759 * @ctxt: the schema validation context
27760 *
27761 * Check if any error was detected during validation.
Daniel Veillarddee23482008-04-11 12:58:43 +000027762 *
Daniel Veillardf10ae122005-07-10 19:03:16 +000027763 * Returns 1 if valid so far, 0 if errors were detected, and -1 in case
27764 * of internal error.
27765 */
27766int
27767xmlSchemaIsValid(xmlSchemaValidCtxtPtr ctxt)
27768{
27769 if (ctxt == NULL)
27770 return(-1);
27771 return(ctxt->err == 0);
27772}
27773
27774/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027775 * xmlSchemaSetValidErrors:
27776 * @ctxt: a schema validation context
27777 * @err: the error function
27778 * @warn: the warning function
Daniel Veillarda9b66d02002-12-11 14:23:49 +000027779 * @ctx: the functions context
Daniel Veillard4255d502002-04-16 15:50:10 +000027780 *
William M. Brack2f2a6632004-08-20 23:09:47 +000027781 * Set the error and warning callback informations
Daniel Veillard4255d502002-04-16 15:50:10 +000027782 */
27783void
27784xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027785 xmlSchemaValidityErrorFunc err,
27786 xmlSchemaValidityWarningFunc warn, void *ctx)
27787{
Daniel Veillard4255d502002-04-16 15:50:10 +000027788 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027789 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000027790 ctxt->error = err;
27791 ctxt->warning = warn;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027792 ctxt->errCtxt = ctx;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027793 if (ctxt->pctxt != NULL)
27794 xmlSchemaSetParserErrors(ctxt->pctxt, err, warn, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000027795}
27796
27797/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027798 * xmlSchemaSetValidStructuredErrors:
27799 * @ctxt: a schema validation context
27800 * @serror: the structured error function
27801 * @ctx: the functions context
27802 *
27803 * Set the structured error callback
27804 */
27805void
27806xmlSchemaSetValidStructuredErrors(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027807 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027808{
27809 if (ctxt == NULL)
27810 return;
27811 ctxt->serror = serror;
27812 ctxt->error = NULL;
27813 ctxt->warning = NULL;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027814 ctxt->errCtxt = ctx;
27815 if (ctxt->pctxt != NULL)
27816 xmlSchemaSetParserStructuredErrors(ctxt->pctxt, serror, ctx);
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027817}
27818
27819/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000027820 * xmlSchemaGetValidErrors:
Daniel Veillarddee23482008-04-11 12:58:43 +000027821 * @ctxt: a XML-Schema validation context
Daniel Veillard259f0df2004-08-18 09:13:18 +000027822 * @err: the error function result
27823 * @warn: the warning function result
27824 * @ctx: the functions context result
27825 *
27826 * Get the error and warning callback informations
27827 *
27828 * Returns -1 in case of error and 0 otherwise
27829 */
27830int
27831xmlSchemaGetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027832 xmlSchemaValidityErrorFunc * err,
27833 xmlSchemaValidityWarningFunc * warn, void **ctx)
Daniel Veillard259f0df2004-08-18 09:13:18 +000027834{
27835 if (ctxt == NULL)
27836 return (-1);
27837 if (err != NULL)
27838 *err = ctxt->error;
27839 if (warn != NULL)
27840 *warn = ctxt->warning;
27841 if (ctx != NULL)
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027842 *ctx = ctxt->errCtxt;
Daniel Veillard259f0df2004-08-18 09:13:18 +000027843 return (0);
27844}
27845
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027846
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027847/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027848 * xmlSchemaSetValidOptions:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027849 * @ctxt: a schema validation context
27850 * @options: a combination of xmlSchemaValidOption
27851 *
27852 * Sets the options to be used during the validation.
27853 *
27854 * Returns 0 in case of success, -1 in case of an
27855 * API error.
27856 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027857int
27858xmlSchemaSetValidOptions(xmlSchemaValidCtxtPtr ctxt,
27859 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027860
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027861{
27862 int i;
27863
27864 if (ctxt == NULL)
27865 return (-1);
27866 /*
27867 * WARNING: Change the start value if adding to the
27868 * xmlSchemaValidOption.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027869 * TODO: Is there an other, more easy to maintain,
27870 * way?
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027871 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027872 for (i = 1; i < (int) sizeof(int) * 8; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027873 if (options & 1<<i)
27874 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027875 }
27876 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027877 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027878}
27879
27880/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027881 * xmlSchemaValidCtxtGetOptions:
Daniel Veillarddee23482008-04-11 12:58:43 +000027882 * @ctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027883 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027884 * Get the validation context options.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027885 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027886 * Returns the option combination or -1 on error.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027887 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027888int
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027889xmlSchemaValidCtxtGetOptions(xmlSchemaValidCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027890
27891{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027892 if (ctxt == NULL)
27893 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027894 else
27895 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027896}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027897
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027898static int
27899xmlSchemaVDocWalk(xmlSchemaValidCtxtPtr vctxt)
27900{
27901 xmlAttrPtr attr;
27902 int ret = 0;
27903 xmlSchemaNodeInfoPtr ielem = NULL;
27904 xmlNodePtr node, valRoot;
27905 const xmlChar *nsName;
27906
27907 /* DOC VAL TODO: Move this to the start function. */
Csaba László1f6c42c2013-03-18 15:30:00 +080027908 if (vctxt->validationRoot != NULL)
27909 valRoot = vctxt->validationRoot;
27910 else
27911 valRoot = xmlDocGetRootElement(vctxt->doc);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027912 if (valRoot == NULL) {
27913 /* VAL TODO: Error code? */
27914 VERROR(1, NULL, "The document has no document element");
27915 return (1);
27916 }
27917 vctxt->depth = -1;
27918 vctxt->validationRoot = valRoot;
27919 node = valRoot;
27920 while (node != NULL) {
27921 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27922 goto next_sibling;
27923 if (node->type == XML_ELEMENT_NODE) {
27924
27925 /*
27926 * Init the node-info.
27927 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027928 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027929 if (xmlSchemaValidatorPushElem(vctxt) == -1)
27930 goto internal_error;
27931 ielem = vctxt->inode;
27932 ielem->node = node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027933 ielem->nodeLine = node->line;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027934 ielem->localName = node->name;
27935 if (node->ns != NULL)
27936 ielem->nsName = node->ns->href;
27937 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
27938 /*
27939 * Register attributes.
27940 * DOC VAL TODO: We do not register namespace declaration
27941 * attributes yet.
27942 */
27943 vctxt->nbAttrInfos = 0;
27944 if (node->properties != NULL) {
27945 attr = node->properties;
27946 do {
27947 if (attr->ns != NULL)
27948 nsName = attr->ns->href;
27949 else
27950 nsName = NULL;
27951 ret = xmlSchemaValidatorPushAttribute(vctxt,
27952 (xmlNodePtr) attr,
Daniel Veillarddee23482008-04-11 12:58:43 +000027953 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027954 * Note that we give it the line number of the
27955 * parent element.
27956 */
27957 ielem->nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027958 attr->name, nsName, 0,
27959 xmlNodeListGetString(attr->doc, attr->children, 1), 1);
27960 if (ret == -1) {
27961 VERROR_INT("xmlSchemaDocWalk",
27962 "calling xmlSchemaValidatorPushAttribute()");
27963 goto internal_error;
27964 }
27965 attr = attr->next;
27966 } while (attr);
27967 }
27968 /*
27969 * Validate the element.
27970 */
27971 ret = xmlSchemaValidateElem(vctxt);
27972 if (ret != 0) {
27973 if (ret == -1) {
27974 VERROR_INT("xmlSchemaDocWalk",
27975 "calling xmlSchemaValidateElem()");
27976 goto internal_error;
27977 }
27978 /*
27979 * Don't stop validation; just skip the content
27980 * of this element.
27981 */
27982 goto leave_node;
27983 }
27984 if ((vctxt->skipDepth != -1) &&
27985 (vctxt->depth >= vctxt->skipDepth))
27986 goto leave_node;
27987 } else if ((node->type == XML_TEXT_NODE) ||
27988 (node->type == XML_CDATA_SECTION_NODE)) {
27989 /*
27990 * Process character content.
27991 */
Daniel Veillard14b56432006-03-09 18:41:40 +000027992 if ((ielem != NULL) && (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027993 ielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27994 ret = xmlSchemaVPushText(vctxt, node->type, node->content,
27995 -1, XML_SCHEMA_PUSH_TEXT_PERSIST, NULL);
27996 if (ret < 0) {
27997 VERROR_INT("xmlSchemaVDocWalk",
27998 "calling xmlSchemaVPushText()");
27999 goto internal_error;
28000 }
28001 /*
28002 * DOC VAL TODO: Should we skip further validation of the
28003 * element content here?
28004 */
28005 } else if ((node->type == XML_ENTITY_NODE) ||
28006 (node->type == XML_ENTITY_REF_NODE)) {
28007 /*
28008 * DOC VAL TODO: What to do with entities?
Daniel Veillarddee23482008-04-11 12:58:43 +000028009 */
Kasimier T. Buchcik146a9822006-05-04 17:19:07 +000028010 VERROR_INT("xmlSchemaVDocWalk",
28011 "there is at least one entity reference in the node-tree "
28012 "currently being validated. Processing of entities with "
28013 "this XML Schema processor is not supported (yet). Please "
28014 "substitute entities before validation.");
28015 goto internal_error;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028016 } else {
28017 goto leave_node;
28018 /*
28019 * DOC VAL TODO: XInclude nodes, etc.
28020 */
28021 }
28022 /*
28023 * Walk the doc.
28024 */
28025 if (node->children != NULL) {
28026 node = node->children;
28027 continue;
28028 }
28029leave_node:
28030 if (node->type == XML_ELEMENT_NODE) {
28031 /*
28032 * Leaving the scope of an element.
28033 */
28034 if (node != vctxt->inode->node) {
28035 VERROR_INT("xmlSchemaVDocWalk",
28036 "element position mismatch");
28037 goto internal_error;
28038 }
28039 ret = xmlSchemaValidatorPopElem(vctxt);
28040 if (ret != 0) {
28041 if (ret < 0) {
28042 VERROR_INT("xmlSchemaVDocWalk",
28043 "calling xmlSchemaValidatorPopElem()");
28044 goto internal_error;
28045 }
28046 }
28047 if (node == valRoot)
28048 goto exit;
28049 }
28050next_sibling:
28051 if (node->next != NULL)
28052 node = node->next;
28053 else {
28054 node = node->parent;
28055 goto leave_node;
28056 }
28057 }
28058
28059exit:
28060 return (ret);
28061internal_error:
28062 return (-1);
28063}
28064
28065static int
Daniel Veillardf10ae122005-07-10 19:03:16 +000028066xmlSchemaPreRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028067 /*
28068 * Some initialization.
Daniel Veillarddee23482008-04-11 12:58:43 +000028069 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028070 vctxt->err = 0;
28071 vctxt->nberrors = 0;
28072 vctxt->depth = -1;
28073 vctxt->skipDepth = -1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000028074 vctxt->xsiAssemble = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000028075 vctxt->hasKeyrefs = 0;
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000028076#ifdef ENABLE_IDC_NODE_TABLES_TEST
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000028077 vctxt->createIDCNodeTables = 1;
28078#else
28079 vctxt->createIDCNodeTables = 0;
28080#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028081 /*
28082 * Create a schema + parser if necessary.
28083 */
28084 if (vctxt->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000028085 xmlSchemaParserCtxtPtr pctxt;
Daniel Veillarddee23482008-04-11 12:58:43 +000028086
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000028087 vctxt->xsiAssemble = 1;
Daniel Veillarddee23482008-04-11 12:58:43 +000028088 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000028089 * If not schema was given then we will create a schema
28090 * dynamically using XSI schema locations.
28091 *
28092 * Create the schema parser context.
28093 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028094 if ((vctxt->pctxt == NULL) &&
28095 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
28096 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000028097 pctxt = vctxt->pctxt;
28098 pctxt->xsiAssemble = 1;
28099 /*
28100 * Create the schema.
28101 */
28102 vctxt->schema = xmlSchemaNewSchema(pctxt);
28103 if (vctxt->schema == NULL)
Daniel Veillarddee23482008-04-11 12:58:43 +000028104 return (-1);
28105 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000028106 * Create the schema construction context.
28107 */
28108 pctxt->constructor = xmlSchemaConstructionCtxtCreate(pctxt->dict);
28109 if (pctxt->constructor == NULL)
28110 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000028111 pctxt->constructor->mainSchema = vctxt->schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000028112 /*
28113 * Take ownership of the constructor to be able to free it.
28114 */
28115 pctxt->ownsConstructor = 1;
Daniel Veillarddee23482008-04-11 12:58:43 +000028116 }
28117 /*
28118 * Augment the IDC definitions for the main schema and all imported ones
Daniel Veillard3888f472007-08-23 09:29:03 +000028119 * NOTE: main schema if the first in the imported list
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028120 */
Daniel Veillard3888f472007-08-23 09:29:03 +000028121 xmlHashScan(vctxt->schema->schemasImports,(xmlHashScanner)xmlSchemaAugmentImportedIDC, vctxt);
Daniel Veillarddee23482008-04-11 12:58:43 +000028122
Daniel Veillardf10ae122005-07-10 19:03:16 +000028123 return(0);
28124}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028125
Daniel Veillardf10ae122005-07-10 19:03:16 +000028126static void
28127xmlSchemaPostRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028128 if (vctxt->xsiAssemble) {
28129 if (vctxt->schema != NULL) {
28130 xmlSchemaFree(vctxt->schema);
28131 vctxt->schema = NULL;
28132 }
28133 }
28134 xmlSchemaClearValidCtxt(vctxt);
Daniel Veillardf10ae122005-07-10 19:03:16 +000028135}
28136
28137static int
28138xmlSchemaVStart(xmlSchemaValidCtxtPtr vctxt)
28139{
28140 int ret = 0;
28141
28142 if (xmlSchemaPreRun(vctxt) < 0)
28143 return(-1);
28144
28145 if (vctxt->doc != NULL) {
28146 /*
28147 * Tree validation.
28148 */
28149 ret = xmlSchemaVDocWalk(vctxt);
28150#ifdef LIBXML_READER_ENABLED
28151 } else if (vctxt->reader != NULL) {
28152 /*
28153 * XML Reader validation.
28154 */
28155#ifdef XML_SCHEMA_READER_ENABLED
28156 ret = xmlSchemaVReaderWalk(vctxt);
28157#endif
28158#endif
28159 } else if ((vctxt->sax != NULL) && (vctxt->parserCtxt != NULL)) {
28160 /*
28161 * SAX validation.
28162 */
28163 ret = xmlParseDocument(vctxt->parserCtxt);
28164 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000028165 VERROR_INT("xmlSchemaVStart",
Daniel Veillardf10ae122005-07-10 19:03:16 +000028166 "no instance to validate");
28167 ret = -1;
28168 }
28169
28170 xmlSchemaPostRun(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028171 if (ret == 0)
28172 ret = vctxt->err;
28173 return (ret);
28174}
28175
28176/**
28177 * xmlSchemaValidateOneElement:
28178 * @ctxt: a schema validation context
28179 * @elem: an element node
28180 *
28181 * Validate a branch of a tree, starting with the given @elem.
28182 *
28183 * Returns 0 if the element and its subtree is valid, a positive error
28184 * code number otherwise and -1 in case of an internal or API error.
28185 */
28186int
28187xmlSchemaValidateOneElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem)
28188{
28189 if ((ctxt == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
28190 return (-1);
28191
28192 if (ctxt->schema == NULL)
28193 return (-1);
28194
28195 ctxt->doc = elem->doc;
28196 ctxt->node = elem;
28197 ctxt->validationRoot = elem;
28198 return(xmlSchemaVStart(ctxt));
28199}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000028200
Daniel Veillard259f0df2004-08-18 09:13:18 +000028201/**
Daniel Veillard4255d502002-04-16 15:50:10 +000028202 * xmlSchemaValidateDoc:
28203 * @ctxt: a schema validation context
28204 * @doc: a parsed document tree
28205 *
28206 * Validate a document tree in memory.
28207 *
28208 * Returns 0 if the document is schemas valid, a positive error code
28209 * number otherwise and -1 in case of internal or API error.
28210 */
28211int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028212xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc)
28213{
Daniel Veillard4255d502002-04-16 15:50:10 +000028214 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028215 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000028216
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028217 ctxt->doc = doc;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000028218 ctxt->node = xmlDocGetRootElement(doc);
28219 if (ctxt->node == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000028220 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000028221 XML_SCHEMAV_DOCUMENT_ELEMENT_MISSING,
28222 (xmlNodePtr) doc, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028223 "The document has no document element", NULL, NULL);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000028224 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028225 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000028226 ctxt->validationRoot = ctxt->node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028227 return (xmlSchemaVStart(ctxt));
Daniel Veillard4255d502002-04-16 15:50:10 +000028228}
28229
Daniel Veillardcdc82732005-07-08 15:04:06 +000028230
28231/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080028232 * *
28233 * Function and data for SAX streaming API *
28234 * *
Daniel Veillardcdc82732005-07-08 15:04:06 +000028235 ************************************************************************/
28236typedef struct _xmlSchemaSplitSAXData xmlSchemaSplitSAXData;
28237typedef xmlSchemaSplitSAXData *xmlSchemaSplitSAXDataPtr;
28238
28239struct _xmlSchemaSplitSAXData {
28240 xmlSAXHandlerPtr user_sax;
28241 void *user_data;
28242 xmlSchemaValidCtxtPtr ctxt;
28243 xmlSAXHandlerPtr schemas_sax;
28244};
28245
Daniel Veillard971771e2005-07-09 17:32:57 +000028246#define XML_SAX_PLUG_MAGIC 0xdc43ba21
28247
28248struct _xmlSchemaSAXPlug {
28249 unsigned int magic;
28250
28251 /* the original callbacks informations */
28252 xmlSAXHandlerPtr *user_sax_ptr;
28253 xmlSAXHandlerPtr user_sax;
28254 void **user_data_ptr;
28255 void *user_data;
28256
28257 /* the block plugged back and validation informations */
28258 xmlSAXHandler schemas_sax;
28259 xmlSchemaValidCtxtPtr ctxt;
28260};
28261
Daniel Veillardcdc82732005-07-08 15:04:06 +000028262/* All those functions just bounces to the user provided SAX handlers */
28263static void
28264internalSubsetSplit(void *ctx, const xmlChar *name,
28265 const xmlChar *ExternalID, const xmlChar *SystemID)
28266{
Daniel Veillard971771e2005-07-09 17:32:57 +000028267 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028268 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28269 (ctxt->user_sax->internalSubset != NULL))
28270 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
28271 SystemID);
28272}
28273
28274static int
28275isStandaloneSplit(void *ctx)
28276{
Daniel Veillard971771e2005-07-09 17:32:57 +000028277 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028278 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28279 (ctxt->user_sax->isStandalone != NULL))
28280 return(ctxt->user_sax->isStandalone(ctxt->user_data));
28281 return(0);
28282}
28283
28284static int
28285hasInternalSubsetSplit(void *ctx)
28286{
Daniel Veillard971771e2005-07-09 17:32:57 +000028287 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028288 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28289 (ctxt->user_sax->hasInternalSubset != NULL))
28290 return(ctxt->user_sax->hasInternalSubset(ctxt->user_data));
28291 return(0);
28292}
28293
28294static int
28295hasExternalSubsetSplit(void *ctx)
28296{
Daniel Veillard971771e2005-07-09 17:32:57 +000028297 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028298 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28299 (ctxt->user_sax->hasExternalSubset != NULL))
28300 return(ctxt->user_sax->hasExternalSubset(ctxt->user_data));
28301 return(0);
28302}
28303
28304static void
28305externalSubsetSplit(void *ctx, const xmlChar *name,
28306 const xmlChar *ExternalID, const xmlChar *SystemID)
28307{
Daniel Veillard971771e2005-07-09 17:32:57 +000028308 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028309 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
Daniel Veillardb3e45362008-03-26 13:39:31 +000028310 (ctxt->user_sax->externalSubset != NULL))
28311 ctxt->user_sax->externalSubset(ctxt->user_data, name, ExternalID,
Daniel Veillardcdc82732005-07-08 15:04:06 +000028312 SystemID);
28313}
28314
28315static xmlParserInputPtr
28316resolveEntitySplit(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
28317{
Daniel Veillard971771e2005-07-09 17:32:57 +000028318 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028319 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28320 (ctxt->user_sax->resolveEntity != NULL))
28321 return(ctxt->user_sax->resolveEntity(ctxt->user_data, publicId,
28322 systemId));
28323 return(NULL);
28324}
28325
28326static xmlEntityPtr
28327getEntitySplit(void *ctx, const xmlChar *name)
28328{
Daniel Veillard971771e2005-07-09 17:32:57 +000028329 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028330 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28331 (ctxt->user_sax->getEntity != NULL))
28332 return(ctxt->user_sax->getEntity(ctxt->user_data, name));
28333 return(NULL);
28334}
28335
28336static xmlEntityPtr
28337getParameterEntitySplit(void *ctx, const xmlChar *name)
28338{
Daniel Veillard971771e2005-07-09 17:32:57 +000028339 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028340 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28341 (ctxt->user_sax->getParameterEntity != NULL))
28342 return(ctxt->user_sax->getParameterEntity(ctxt->user_data, name));
28343 return(NULL);
28344}
28345
28346
28347static void
28348entityDeclSplit(void *ctx, const xmlChar *name, int type,
28349 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
28350{
Daniel Veillard971771e2005-07-09 17:32:57 +000028351 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028352 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28353 (ctxt->user_sax->entityDecl != NULL))
28354 ctxt->user_sax->entityDecl(ctxt->user_data, name, type, publicId,
28355 systemId, content);
28356}
28357
28358static void
28359attributeDeclSplit(void *ctx, const xmlChar * elem,
28360 const xmlChar * name, int type, int def,
28361 const xmlChar * defaultValue, xmlEnumerationPtr tree)
28362{
Daniel Veillard971771e2005-07-09 17:32:57 +000028363 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028364 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28365 (ctxt->user_sax->attributeDecl != NULL)) {
28366 ctxt->user_sax->attributeDecl(ctxt->user_data, elem, name, type,
28367 def, defaultValue, tree);
28368 } else {
28369 xmlFreeEnumeration(tree);
28370 }
28371}
28372
28373static void
28374elementDeclSplit(void *ctx, const xmlChar *name, int type,
28375 xmlElementContentPtr content)
28376{
Daniel Veillard971771e2005-07-09 17:32:57 +000028377 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028378 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28379 (ctxt->user_sax->elementDecl != NULL))
28380 ctxt->user_sax->elementDecl(ctxt->user_data, name, type, content);
28381}
28382
28383static void
28384notationDeclSplit(void *ctx, const xmlChar *name,
28385 const xmlChar *publicId, const xmlChar *systemId)
28386{
Daniel Veillard971771e2005-07-09 17:32:57 +000028387 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028388 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28389 (ctxt->user_sax->notationDecl != NULL))
28390 ctxt->user_sax->notationDecl(ctxt->user_data, name, publicId,
28391 systemId);
28392}
28393
28394static void
28395unparsedEntityDeclSplit(void *ctx, const xmlChar *name,
28396 const xmlChar *publicId, const xmlChar *systemId,
28397 const xmlChar *notationName)
28398{
Daniel Veillard971771e2005-07-09 17:32:57 +000028399 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028400 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28401 (ctxt->user_sax->unparsedEntityDecl != NULL))
28402 ctxt->user_sax->unparsedEntityDecl(ctxt->user_data, name, publicId,
28403 systemId, notationName);
28404}
28405
28406static void
28407setDocumentLocatorSplit(void *ctx, xmlSAXLocatorPtr loc)
28408{
Daniel Veillard971771e2005-07-09 17:32:57 +000028409 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028410 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28411 (ctxt->user_sax->setDocumentLocator != NULL))
28412 ctxt->user_sax->setDocumentLocator(ctxt->user_data, loc);
28413}
28414
28415static void
28416startDocumentSplit(void *ctx)
28417{
Daniel Veillard971771e2005-07-09 17:32:57 +000028418 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028419 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28420 (ctxt->user_sax->startDocument != NULL))
28421 ctxt->user_sax->startDocument(ctxt->user_data);
28422}
28423
28424static void
28425endDocumentSplit(void *ctx)
28426{
Daniel Veillard971771e2005-07-09 17:32:57 +000028427 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028428 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28429 (ctxt->user_sax->endDocument != NULL))
28430 ctxt->user_sax->endDocument(ctxt->user_data);
28431}
28432
28433static void
28434processingInstructionSplit(void *ctx, const xmlChar *target,
28435 const xmlChar *data)
28436{
Daniel Veillard971771e2005-07-09 17:32:57 +000028437 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028438 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28439 (ctxt->user_sax->processingInstruction != NULL))
28440 ctxt->user_sax->processingInstruction(ctxt->user_data, target, data);
28441}
28442
28443static void
28444commentSplit(void *ctx, const xmlChar *value)
28445{
Daniel Veillard971771e2005-07-09 17:32:57 +000028446 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028447 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28448 (ctxt->user_sax->comment != NULL))
28449 ctxt->user_sax->comment(ctxt->user_data, value);
28450}
28451
28452/*
28453 * Varargs error callbacks to the user application, harder ...
28454 */
28455
Daniel Veillardffa3c742005-07-21 13:24:09 +000028456static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000028457warningSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028458 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028459 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28460 (ctxt->user_sax->warning != NULL)) {
28461 TODO
28462 }
28463}
Daniel Veillardffa3c742005-07-21 13:24:09 +000028464static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000028465errorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028466 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028467 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28468 (ctxt->user_sax->error != NULL)) {
28469 TODO
28470 }
28471}
Daniel Veillardffa3c742005-07-21 13:24:09 +000028472static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000028473fatalErrorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028474 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028475 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28476 (ctxt->user_sax->fatalError != NULL)) {
28477 TODO
28478 }
28479}
28480
28481/*
28482 * Those are function where both the user handler and the schemas handler
28483 * need to be called.
28484 */
28485static void
28486charactersSplit(void *ctx, const xmlChar *ch, int len)
28487{
Daniel Veillard971771e2005-07-09 17:32:57 +000028488 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028489 if (ctxt == NULL)
28490 return;
28491 if ((ctxt->user_sax != NULL) && (ctxt->user_sax->characters != NULL))
28492 ctxt->user_sax->characters(ctxt->user_data, ch, len);
28493 if (ctxt->ctxt != NULL)
28494 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
28495}
28496
28497static void
28498ignorableWhitespaceSplit(void *ctx, const xmlChar *ch, int len)
28499{
Daniel Veillard971771e2005-07-09 17:32:57 +000028500 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028501 if (ctxt == NULL)
28502 return;
28503 if ((ctxt->user_sax != NULL) &&
28504 (ctxt->user_sax->ignorableWhitespace != NULL))
28505 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, ch, len);
28506 if (ctxt->ctxt != NULL)
28507 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
28508}
28509
28510static void
28511cdataBlockSplit(void *ctx, const xmlChar *value, int len)
28512{
Daniel Veillard971771e2005-07-09 17:32:57 +000028513 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028514 if (ctxt == NULL)
28515 return;
28516 if ((ctxt->user_sax != NULL) &&
Daniel Veillard7d0ca5e2008-02-05 08:43:52 +000028517 (ctxt->user_sax->cdataBlock != NULL))
28518 ctxt->user_sax->cdataBlock(ctxt->user_data, value, len);
Daniel Veillardcdc82732005-07-08 15:04:06 +000028519 if (ctxt->ctxt != NULL)
28520 xmlSchemaSAXHandleCDataSection(ctxt->ctxt, value, len);
28521}
28522
28523static void
28524referenceSplit(void *ctx, const xmlChar *name)
28525{
Daniel Veillard971771e2005-07-09 17:32:57 +000028526 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillard14b56432006-03-09 18:41:40 +000028527 if (ctxt == NULL)
28528 return;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028529 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
28530 (ctxt->user_sax->reference != NULL))
28531 ctxt->user_sax->reference(ctxt->user_data, name);
28532 if (ctxt->ctxt != NULL)
28533 xmlSchemaSAXHandleReference(ctxt->user_data, name);
28534}
28535
28536static void
Daniel Veillarddee23482008-04-11 12:58:43 +000028537startElementNsSplit(void *ctx, const xmlChar * localname,
28538 const xmlChar * prefix, const xmlChar * URI,
28539 int nb_namespaces, const xmlChar ** namespaces,
28540 int nb_attributes, int nb_defaulted,
Daniel Veillardcdc82732005-07-08 15:04:06 +000028541 const xmlChar ** attributes) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028542 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028543 if (ctxt == NULL)
28544 return;
28545 if ((ctxt->user_sax != NULL) &&
28546 (ctxt->user_sax->startElementNs != NULL))
28547 ctxt->user_sax->startElementNs(ctxt->user_data, localname, prefix,
28548 URI, nb_namespaces, namespaces,
28549 nb_attributes, nb_defaulted,
28550 attributes);
28551 if (ctxt->ctxt != NULL)
28552 xmlSchemaSAXHandleStartElementNs(ctxt->ctxt, localname, prefix,
28553 URI, nb_namespaces, namespaces,
28554 nb_attributes, nb_defaulted,
28555 attributes);
28556}
28557
28558static void
Daniel Veillarddee23482008-04-11 12:58:43 +000028559endElementNsSplit(void *ctx, const xmlChar * localname,
Daniel Veillardcdc82732005-07-08 15:04:06 +000028560 const xmlChar * prefix, const xmlChar * URI) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028561 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028562 if (ctxt == NULL)
28563 return;
28564 if ((ctxt->user_sax != NULL) &&
28565 (ctxt->user_sax->endElementNs != NULL))
28566 ctxt->user_sax->endElementNs(ctxt->user_data, localname, prefix, URI);
28567 if (ctxt->ctxt != NULL)
28568 xmlSchemaSAXHandleEndElementNs(ctxt->ctxt, localname, prefix, URI);
28569}
28570
Daniel Veillard4255d502002-04-16 15:50:10 +000028571/**
Daniel Veillard971771e2005-07-09 17:32:57 +000028572 * xmlSchemaSAXPlug:
28573 * @ctxt: a schema validation context
Daniel Veillard1f33c4d2005-07-10 21:38:31 +000028574 * @sax: a pointer to the original xmlSAXHandlerPtr
28575 * @user_data: a pointer to the original SAX user data pointer
Daniel Veillard971771e2005-07-09 17:32:57 +000028576 *
28577 * Plug a SAX based validation layer in a SAX parsing event flow.
28578 * The original @saxptr and @dataptr data are replaced by new pointers
28579 * but the calls to the original will be maintained.
28580 *
28581 * Returns a pointer to a data structure needed to unplug the validation layer
28582 * or NULL in case of errors.
28583 */
28584xmlSchemaSAXPlugPtr
28585xmlSchemaSAXPlug(xmlSchemaValidCtxtPtr ctxt,
28586 xmlSAXHandlerPtr *sax, void **user_data)
28587{
28588 xmlSchemaSAXPlugPtr ret;
28589 xmlSAXHandlerPtr old_sax;
28590
28591 if ((ctxt == NULL) || (sax == NULL) || (user_data == NULL))
28592 return(NULL);
28593
28594 /*
28595 * We only allow to plug into SAX2 event streams
28596 */
28597 old_sax = *sax;
28598 if ((old_sax != NULL) && (old_sax->initialized != XML_SAX2_MAGIC))
28599 return(NULL);
Daniel Veillarddee23482008-04-11 12:58:43 +000028600 if ((old_sax != NULL) &&
Daniel Veillard971771e2005-07-09 17:32:57 +000028601 (old_sax->startElementNs == NULL) && (old_sax->endElementNs == NULL) &&
28602 ((old_sax->startElement != NULL) || (old_sax->endElement != NULL)))
28603 return(NULL);
28604
28605 /*
28606 * everything seems right allocate the local data needed for that layer
28607 */
28608 ret = (xmlSchemaSAXPlugPtr) xmlMalloc(sizeof(xmlSchemaSAXPlugStruct));
28609 if (ret == NULL) {
28610 return(NULL);
28611 }
28612 memset(ret, 0, sizeof(xmlSchemaSAXPlugStruct));
28613 ret->magic = XML_SAX_PLUG_MAGIC;
28614 ret->schemas_sax.initialized = XML_SAX2_MAGIC;
28615 ret->ctxt = ctxt;
28616 ret->user_sax_ptr = sax;
28617 ret->user_sax = old_sax;
Daniel Veillarddee23482008-04-11 12:58:43 +000028618 if (old_sax == NULL) {
Daniel Veillard971771e2005-07-09 17:32:57 +000028619 /*
28620 * go direct, no need for the split block and functions.
28621 */
28622 ret->schemas_sax.startElementNs = xmlSchemaSAXHandleStartElementNs;
28623 ret->schemas_sax.endElementNs = xmlSchemaSAXHandleEndElementNs;
28624 /*
28625 * Note that we use the same text-function for both, to prevent
28626 * the parser from testing for ignorable whitespace.
28627 */
28628 ret->schemas_sax.ignorableWhitespace = xmlSchemaSAXHandleText;
28629 ret->schemas_sax.characters = xmlSchemaSAXHandleText;
28630
28631 ret->schemas_sax.cdataBlock = xmlSchemaSAXHandleCDataSection;
28632 ret->schemas_sax.reference = xmlSchemaSAXHandleReference;
28633
28634 ret->user_data = ctxt;
28635 *user_data = ctxt;
28636 } else {
28637 /*
28638 * for each callback unused by Schemas initialize it to the Split
Daniel Veillarddee23482008-04-11 12:58:43 +000028639 * routine only if non NULL in the user block, this can speed up
Daniel Veillard971771e2005-07-09 17:32:57 +000028640 * things at the SAX level.
28641 */
28642 if (old_sax->internalSubset != NULL)
28643 ret->schemas_sax.internalSubset = internalSubsetSplit;
28644 if (old_sax->isStandalone != NULL)
28645 ret->schemas_sax.isStandalone = isStandaloneSplit;
28646 if (old_sax->hasInternalSubset != NULL)
28647 ret->schemas_sax.hasInternalSubset = hasInternalSubsetSplit;
28648 if (old_sax->hasExternalSubset != NULL)
28649 ret->schemas_sax.hasExternalSubset = hasExternalSubsetSplit;
28650 if (old_sax->resolveEntity != NULL)
28651 ret->schemas_sax.resolveEntity = resolveEntitySplit;
28652 if (old_sax->getEntity != NULL)
28653 ret->schemas_sax.getEntity = getEntitySplit;
28654 if (old_sax->entityDecl != NULL)
28655 ret->schemas_sax.entityDecl = entityDeclSplit;
28656 if (old_sax->notationDecl != NULL)
28657 ret->schemas_sax.notationDecl = notationDeclSplit;
28658 if (old_sax->attributeDecl != NULL)
28659 ret->schemas_sax.attributeDecl = attributeDeclSplit;
28660 if (old_sax->elementDecl != NULL)
28661 ret->schemas_sax.elementDecl = elementDeclSplit;
28662 if (old_sax->unparsedEntityDecl != NULL)
28663 ret->schemas_sax.unparsedEntityDecl = unparsedEntityDeclSplit;
28664 if (old_sax->setDocumentLocator != NULL)
28665 ret->schemas_sax.setDocumentLocator = setDocumentLocatorSplit;
28666 if (old_sax->startDocument != NULL)
28667 ret->schemas_sax.startDocument = startDocumentSplit;
28668 if (old_sax->endDocument != NULL)
28669 ret->schemas_sax.endDocument = endDocumentSplit;
28670 if (old_sax->processingInstruction != NULL)
28671 ret->schemas_sax.processingInstruction = processingInstructionSplit;
28672 if (old_sax->comment != NULL)
28673 ret->schemas_sax.comment = commentSplit;
28674 if (old_sax->warning != NULL)
28675 ret->schemas_sax.warning = warningSplit;
28676 if (old_sax->error != NULL)
28677 ret->schemas_sax.error = errorSplit;
28678 if (old_sax->fatalError != NULL)
28679 ret->schemas_sax.fatalError = fatalErrorSplit;
28680 if (old_sax->getParameterEntity != NULL)
28681 ret->schemas_sax.getParameterEntity = getParameterEntitySplit;
28682 if (old_sax->externalSubset != NULL)
28683 ret->schemas_sax.externalSubset = externalSubsetSplit;
28684
28685 /*
28686 * the 6 schemas callback have to go to the splitter functions
28687 * Note that we use the same text-function for ignorableWhitespace
28688 * if possible, to prevent the parser from testing for ignorable
28689 * whitespace.
28690 */
28691 ret->schemas_sax.characters = charactersSplit;
28692 if ((old_sax->ignorableWhitespace != NULL) &&
28693 (old_sax->ignorableWhitespace != old_sax->characters))
28694 ret->schemas_sax.ignorableWhitespace = ignorableWhitespaceSplit;
28695 else
28696 ret->schemas_sax.ignorableWhitespace = charactersSplit;
28697 ret->schemas_sax.cdataBlock = cdataBlockSplit;
28698 ret->schemas_sax.reference = referenceSplit;
28699 ret->schemas_sax.startElementNs = startElementNsSplit;
28700 ret->schemas_sax.endElementNs = endElementNsSplit;
28701
28702 ret->user_data_ptr = user_data;
28703 ret->user_data = *user_data;
28704 *user_data = ret;
28705 }
28706
28707 /*
28708 * plug the pointers back.
28709 */
28710 *sax = &(ret->schemas_sax);
Daniel Veillardf10ae122005-07-10 19:03:16 +000028711 ctxt->sax = *sax;
28712 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28713 xmlSchemaPreRun(ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028714 return(ret);
28715}
28716
28717/**
28718 * xmlSchemaSAXUnplug:
28719 * @plug: a data structure returned by xmlSchemaSAXPlug
28720 *
28721 * Unplug a SAX based validation layer in a SAX parsing event flow.
28722 * The original pointers used in the call are restored.
28723 *
28724 * Returns 0 in case of success and -1 in case of failure.
28725 */
28726int
28727xmlSchemaSAXUnplug(xmlSchemaSAXPlugPtr plug)
28728{
28729 xmlSAXHandlerPtr *sax;
28730 void **user_data;
28731
28732 if ((plug == NULL) || (plug->magic != XML_SAX_PLUG_MAGIC))
28733 return(-1);
28734 plug->magic = 0;
28735
Daniel Veillardf10ae122005-07-10 19:03:16 +000028736 xmlSchemaPostRun(plug->ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028737 /* restore the data */
28738 sax = plug->user_sax_ptr;
28739 *sax = plug->user_sax;
28740 if (plug->user_sax != NULL) {
28741 user_data = plug->user_data_ptr;
28742 *user_data = plug->user_data;
28743 }
28744
28745 /* free and return */
28746 xmlFree(plug);
28747 return(0);
28748}
28749
28750/**
Daniel Veillard97fa5b32012-08-14 11:01:07 +080028751 * xmlSchemaValidateSetLocator:
28752 * @vctxt: a schema validation context
28753 * @f: the locator function pointer
28754 * @ctxt: the locator context
28755 *
28756 * Allows to set a locator function to the validation context,
28757 * which will be used to provide file and line information since
28758 * those are not provided as part of the SAX validation flow
28759 * Setting @f to NULL disable the locator.
28760 */
28761
28762void
28763xmlSchemaValidateSetLocator(xmlSchemaValidCtxtPtr vctxt,
28764 xmlSchemaValidityLocatorFunc f,
28765 void *ctxt)
28766{
28767 if (vctxt == NULL) return;
28768 vctxt->locFunc = f;
28769 vctxt->locCtxt = ctxt;
28770}
28771
28772/**
28773 * xmlSchemaValidateStreamLocator:
28774 * @ctx: the xmlTextReaderPtr used
28775 * @file: returned file information
28776 * @line: returned line information
28777 *
28778 * Internal locator function for the readers
28779 *
28780 * Returns 0 in case the Schema validation could be (des)activated and
28781 * -1 in case of error.
28782 */
28783static int
28784xmlSchemaValidateStreamLocator(void *ctx, const char **file,
28785 unsigned long *line) {
28786 xmlParserCtxtPtr ctxt;
28787
28788 if ((ctx == NULL) || ((file == NULL) && (line == NULL)))
28789 return(-1);
28790
28791 if (file != NULL)
28792 *file = NULL;
28793 if (line != NULL)
28794 *line = 0;
28795
28796 ctxt = (xmlParserCtxtPtr) ctx;
28797 if (ctxt->input != NULL) {
28798 if (file != NULL)
28799 *file = ctxt->input->filename;
28800 if (line != NULL)
28801 *line = ctxt->input->line;
28802 return(0);
28803 }
28804 return(-1);
28805}
28806
28807/**
Daniel Veillard4255d502002-04-16 15:50:10 +000028808 * xmlSchemaValidateStream:
28809 * @ctxt: a schema validation context
28810 * @input: the input to use for reading the data
28811 * @enc: an optional encoding information
28812 * @sax: a SAX handler for the resulting events
28813 * @user_data: the context to provide to the SAX handler.
28814 *
Daniel Veillardcdc82732005-07-08 15:04:06 +000028815 * Validate an input based on a flow of SAX event from the parser
28816 * and forward the events to the @sax handler with the provided @user_data
28817 * the user provided @sax handler must be a SAX2 one.
Daniel Veillard4255d502002-04-16 15:50:10 +000028818 *
28819 * Returns 0 if the document is schemas valid, a positive error code
28820 * number otherwise and -1 in case of internal or API error.
28821 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028822int
Daniel Veillard4255d502002-04-16 15:50:10 +000028823xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028824 xmlParserInputBufferPtr input, xmlCharEncoding enc,
28825 xmlSAXHandlerPtr sax, void *user_data)
28826{
Daniel Veillard971771e2005-07-09 17:32:57 +000028827 xmlSchemaSAXPlugPtr plug = NULL;
28828 xmlSAXHandlerPtr old_sax = NULL;
28829 xmlParserCtxtPtr pctxt = NULL;
28830 xmlParserInputPtr inputStream = NULL;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028831 int ret;
28832
Daniel Veillard4255d502002-04-16 15:50:10 +000028833 if ((ctxt == NULL) || (input == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028834 return (-1);
Daniel Veillardcdc82732005-07-08 15:04:06 +000028835
Daniel Veillardcdc82732005-07-08 15:04:06 +000028836 /*
28837 * prepare the parser
28838 */
28839 pctxt = xmlNewParserCtxt();
28840 if (pctxt == NULL)
28841 return (-1);
28842 old_sax = pctxt->sax;
Daniel Veillard971771e2005-07-09 17:32:57 +000028843 pctxt->sax = sax;
28844 pctxt->userData = user_data;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028845#if 0
28846 if (options)
28847 xmlCtxtUseOptions(pctxt, options);
28848#endif
Daniel Veillarddee23482008-04-11 12:58:43 +000028849 pctxt->linenumbers = 1;
Daniel Veillard97fa5b32012-08-14 11:01:07 +080028850 xmlSchemaValidateSetLocator(ctxt, xmlSchemaValidateStreamLocator, pctxt);
Daniel Veillardcdc82732005-07-08 15:04:06 +000028851
Daniel Veillardf0af8ec2005-07-08 17:27:33 +000028852 inputStream = xmlNewIOInputStream(pctxt, input, enc);;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028853 if (inputStream == NULL) {
28854 ret = -1;
28855 goto done;
28856 }
28857 inputPush(pctxt, inputStream);
28858 ctxt->parserCtxt = pctxt;
28859 ctxt->input = input;
28860
28861 /*
Daniel Veillard971771e2005-07-09 17:32:57 +000028862 * Plug the validation and launch the parsing
Daniel Veillardcdc82732005-07-08 15:04:06 +000028863 */
Daniel Veillard971771e2005-07-09 17:32:57 +000028864 plug = xmlSchemaSAXPlug(ctxt, &(pctxt->sax), &(pctxt->userData));
28865 if (plug == NULL) {
28866 ret = -1;
28867 goto done;
28868 }
28869 ctxt->input = input;
28870 ctxt->enc = enc;
28871 ctxt->sax = pctxt->sax;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028872 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28873 ret = xmlSchemaVStart(ctxt);
28874
28875 if ((ret == 0) && (! ctxt->parserCtxt->wellFormed)) {
28876 ret = ctxt->parserCtxt->errNo;
28877 if (ret == 0)
28878 ret = 1;
Daniel Veillard33fe2882008-04-10 08:09:25 +000028879 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028880
28881done:
Daniel Veillard971771e2005-07-09 17:32:57 +000028882 ctxt->parserCtxt = NULL;
28883 ctxt->sax = NULL;
28884 ctxt->input = NULL;
28885 if (plug != NULL) {
28886 xmlSchemaSAXUnplug(plug);
28887 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028888 /* cleanup */
Daniel Veillard971771e2005-07-09 17:32:57 +000028889 if (pctxt != NULL) {
28890 pctxt->sax = old_sax;
28891 xmlFreeParserCtxt(pctxt);
28892 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028893 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +000028894}
28895
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028896/**
28897 * xmlSchemaValidateFile:
28898 * @ctxt: a schema validation context
Daniel Veillard81562d22005-06-15 13:27:56 +000028899 * @filename: the URI of the instance
28900 * @options: a future set of options, currently unused
28901 *
28902 * Do a schemas validation of the given resource, it will use the
28903 * SAX streamable validation internally.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028904 *
28905 * Returns 0 if the document is valid, a positive error code
28906 * number otherwise and -1 in case of an internal or API error.
28907 */
28908int
28909xmlSchemaValidateFile(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000028910 const char * filename,
28911 int options ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028912{
28913 int ret;
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028914 xmlParserInputBufferPtr input;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028915
28916 if ((ctxt == NULL) || (filename == NULL))
28917 return (-1);
Daniel Veillarddee23482008-04-11 12:58:43 +000028918
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028919 input = xmlParserInputBufferCreateFilename(filename,
28920 XML_CHAR_ENCODING_NONE);
28921 if (input == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028922 return (-1);
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028923 ret = xmlSchemaValidateStream(ctxt, input, XML_CHAR_ENCODING_NONE,
Daniel Veillarddee23482008-04-11 12:58:43 +000028924 NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028925 return (ret);
Daniel Veillard81562d22005-06-15 13:27:56 +000028926}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028927
Daniel Veillarddee23482008-04-11 12:58:43 +000028928/**
28929 * xmlSchemaValidCtxtGetParserCtxt:
28930 * @ctxt: a schema validation context
28931 *
28932 * allow access to the parser context of the schema validation context
28933 *
28934 * Returns the parser context of the schema validation context or NULL
28935 * in case of error.
28936 */
28937xmlParserCtxtPtr
28938xmlSchemaValidCtxtGetParserCtxt(xmlSchemaValidCtxtPtr ctxt)
28939{
28940 if (ctxt == NULL)
28941 return(NULL);
28942 return (ctxt->parserCtxt);
28943}
28944
Daniel Veillard5d4644e2005-04-01 13:11:58 +000028945#define bottom_xmlschemas
28946#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +000028947#endif /* LIBXML_SCHEMAS_ENABLED */