blob: 70f471d4df89ef0aebb9aef69281948424aba7f4 [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
2 * schemas.c : implementation of the XML Schema handling and
3 * schema validity checking
4 *
5 * See Copyright for the status of this software.
6 *
7 * Daniel Veillard <veillard@redhat.com>
8 */
9
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010/*
Daniel Veillardb0f397e2003-12-23 23:30:53 +000011 * TODO:
12 * - when types are redefined in includes, check that all
13 * types in the redef list are equal
14 * -> need a type equality operation.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015 * - if we don't intend to use the schema for schemas, we
Daniel Veillard01fa6152004-06-29 17:04:39 +000016 * need to validate all schema attributes (ref, type, name)
17 * against their types.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018 * - Eliminate item creation for: ??
19 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020 * URGENT TODO:
21 * - For xsi-driven schema acquisition, augment the IDCs after every
22 * acquisition episode (xmlSchemaAugmentIDC).
23 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000024 * NOTES:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025 * - Elimated item creation for: <restriction>, <extension>,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026 * <simpleContent>, <complexContent>, <list>, <union>
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000028 * PROBLEMS:
29 * - http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0337.html
30 * IDC XPath expression and chameleon includes: the targetNamespace is changed, so
31 * XPath will have trouble to resolve to this namespace, since not known.
32 *
33 *
34 * CONSTRAINTS:
35 *
36 * Schema Component Constraint:
37 * All Group Limited (cos-all-limited)
38 * Status: complete
39 * (1.2)
40 * In xmlSchemaGroupDefReferenceTermFixup() and
41 * (2)
42 * In xmlSchemaParseModelGroup()
43 * TODO: Actually this should go to component-level checks,
44 * but is done here due to performance. Move it to an other layer
45 * is schema construction via an API is implemented.
Daniel Veillardb0f397e2003-12-23 23:30:53 +000046 */
Daniel Veillard4255d502002-04-16 15:50:10 +000047#define IN_LIBXML
48#include "libxml.h"
49
50#ifdef LIBXML_SCHEMAS_ENABLED
51
52#include <string.h>
53#include <libxml/xmlmemory.h>
54#include <libxml/parser.h>
55#include <libxml/parserInternals.h>
56#include <libxml/hash.h>
Daniel Veillard5a872412002-05-22 06:40:27 +000057#include <libxml/uri.h>
Daniel Veillard4255d502002-04-16 15:50:10 +000058#include <libxml/xmlschemas.h>
59#include <libxml/schemasInternals.h>
60#include <libxml/xmlschemastypes.h>
61#include <libxml/xmlautomata.h>
62#include <libxml/xmlregexp.h>
Daniel Veillardbe9c6322003-11-22 20:37:51 +000063#include <libxml/dict.h>
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000064#include <libxml/encoding.h>
65#include <libxml/xmlIO.h>
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000066#ifdef LIBXML_PATTERN_ENABLED
67#include <libxml/pattern.h>
68#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000069#ifdef LIBXML_READER_ENABLED
70#include <libxml/xmlreader.h>
71#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000072
Daniel Veillarda84c0b32003-06-02 16:58:46 +000073/* #define DEBUG 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000074
Daniel Veillard82bbbd42003-05-11 20:16:09 +000075/* #define DEBUG_CONTENT 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000076
Daniel Veillard82bbbd42003-05-11 20:16:09 +000077/* #define DEBUG_TYPE 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000078
Daniel Veillard118aed72002-09-24 14:13:13 +000079/* #define DEBUG_CONTENT_REGEXP 1 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000080
Daniel Veillard4255d502002-04-16 15:50:10 +000081/* #define DEBUG_AUTOMATA 1 */
82
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000083/* #define DEBUG_IDC */
84
85/* #define DEBUG_IDC_NODE_TABLE */
86
87#ifdef DEBUG_IDC
88 #ifndef DEBUG_IDC_NODE_TABLE
89 #define DEBUG_IDC_NODE_TABLE
90 #endif
91#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000092
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000093/* #define ENABLE_PARTICLE_RESTRICTION 1 */
94
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000095#define ENABLE_REDEFINE
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000096
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000097/* #define ENABLE_NAMED_LOCALS */
98
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000099/* #define ENABLE_IDC_NODE_TABLES_TEST */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000100
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000101#define DUMP_CONTENT_MODEL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000102
103#ifdef LIBXML_READER_ENABLED
104/* #define XML_SCHEMA_READER_ENABLED */
105#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +0000106
Daniel Veillard4255d502002-04-16 15:50:10 +0000107#define UNBOUNDED (1 << 30)
108#define TODO \
109 xmlGenericError(xmlGenericErrorContext, \
110 "Unimplemented block at %s:%d\n", \
111 __FILE__, __LINE__);
112
William M. Brack2f2a6632004-08-20 23:09:47 +0000113#define XML_SCHEMAS_NO_NAMESPACE (const xmlChar *) "##"
Daniel Veillardc0826a72004-08-10 14:17:33 +0000114
Daniel Veillard4255d502002-04-16 15:50:10 +0000115/*
116 * The XML Schemas namespaces
117 */
118static const xmlChar *xmlSchemaNs = (const xmlChar *)
119 "http://www.w3.org/2001/XMLSchema";
120
121static const xmlChar *xmlSchemaInstanceNs = (const xmlChar *)
122 "http://www.w3.org/2001/XMLSchema-instance";
123
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000124static const xmlChar *xmlNamespaceNs = (const xmlChar *)
125 "http://www.w3.org/2000/xmlns/";
126
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000127/*
128* Come casting macros.
129*/
130#define ACTXT_CAST (xmlSchemaAbstractCtxtPtr)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +0000131#define PCTXT_CAST (xmlSchemaParserCtxtPtr)
132#define VCTXT_CAST (xmlSchemaValidCtxtPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000133#define WXS_BASIC_CAST (xmlSchemaBasicItemPtr)
134#define WXS_TREE_CAST (xmlSchemaTreeItemPtr)
135#define WXS_PTC_CAST (xmlSchemaParticlePtr)
136#define WXS_TYPE_CAST (xmlSchemaTypePtr)
137#define WXS_ELEM_CAST (xmlSchemaElementPtr)
138#define WXS_ATTR_GROUP_CAST (xmlSchemaAttributeGroupPtr)
139#define WXS_ATTR_CAST (xmlSchemaAttributePtr)
140#define WXS_ATTR_USE_CAST (xmlSchemaAttributeUsePtr)
141#define WXS_ATTR_PROHIB_CAST (xmlSchemaAttributeUseProhibPtr)
142#define WXS_MODEL_GROUPDEF_CAST (xmlSchemaModelGroupDefPtr)
143#define WXS_MODEL_GROUP_CAST (xmlSchemaModelGroupPtr)
144#define WXS_IDC_CAST (xmlSchemaIDCPtr)
145#define WXS_QNAME_CAST (xmlSchemaQNameRefPtr)
146#define WXS_LIST_CAST (xmlSchemaItemListPtr)
Daniel Veillardc0826a72004-08-10 14:17:33 +0000147
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000148/*
149* Macros to query common properties of components.
150*/
151#define WXS_ITEM_NODE(i) xmlSchemaGetComponentNode(WXS_BASIC_CAST (i))
Daniel Veillard4255d502002-04-16 15:50:10 +0000152
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000153#define WXS_ITEM_TYPE_NAME(i) xmlSchemaGetComponentTypeStr(WXS_BASIC_CAST (i))
154/*
155* Macros for element declarations.
156*/
157#define WXS_ELEM_TYPEDEF(e) (e)->subtypes
Daniel Veillardc0826a72004-08-10 14:17:33 +0000158
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000159#define WXS_SUBST_HEAD(item) (item)->refDecl
160/*
161* Macros for attribute declarations.
162*/
163#define WXS_ATTR_TYPEDEF(a) (a)->subtypes
164/*
165* Macros for attribute uses.
166*/
167#define WXS_ATTRUSE_DECL(au) WXS_ATTR_CAST (WXS_ATTR_USE_CAST (au))->attrDecl
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000168
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000169#define WXS_ATTRUSE_TYPEDEF(au) WXS_ATTR_TYPEDEF(WXS_ATTRUSE_DECL( WXS_ATTR_USE_CAST au))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000170
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000171#define WXS_ATTRUSE_DECL_NAME(au) (WXS_ATTRUSE_DECL(au))->name
172
173#define WXS_ATTRUSE_DECL_TNS(au) (WXS_ATTRUSE_DECL(au))->targetNamespace
174/*
175* Macros for attribute groups.
176*/
177#define WXS_ATTR_GROUP_HAS_REFS(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS)
178#define WXS_ATTR_GROUP_EXPANDED(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED)
179/*
180* Macros for particles.
181*/
182#define WXS_PARTICLE(p) WXS_PTC_CAST (p)
183
184#define WXS_PARTICLE_TERM(p) (WXS_PARTICLE(p))->children
185
186#define WXS_PARTICLE_MODEL(p) WXS_MODEL_GROUP_CAST WXS_PARTICLE(p)->children
187/*
188* Macros for model groups definitions.
189*/
190#define WXS_MODELGROUPDEF_MODEL(mgd) (WXS_MODEL_GROUP_CAST (mgd))->children
191/*
192* Macros for model groups.
193*/
194#define WXS_IS_MODEL_GROUP(i) \
195 (((i)->type == XML_SCHEMA_TYPE_SEQUENCE) || \
196 ((i)->type == XML_SCHEMA_TYPE_CHOICE) || \
197 ((i)->type == XML_SCHEMA_TYPE_ALL))
198
199#define WXS_MODELGROUP_PARTICLE(mg) WXS_PTC_CAST (mg)->children
200/*
201* Macros for schema buckets.
202*/
203#define WXS_IS_BUCKET_INCREDEF(t) (((t) == XML_SCHEMA_SCHEMA_INCLUDE) || \
204 ((t) == XML_SCHEMA_SCHEMA_REDEFINE))
205
206#define WXS_IS_BUCKET_IMPMAIN(t) (((t) == XML_SCHEMA_SCHEMA_MAIN) || \
207 ((t) == XML_SCHEMA_SCHEMA_IMPORT))
208
209#define WXS_IMPBUCKET(b) ((xmlSchemaImportPtr) (b))
210
211#define WXS_INCBUCKET(b) ((xmlSchemaIncludePtr) (b))
212/*
213* Macros for complex/simple types.
214*/
215#define WXS_IS_ANYTYPE(i) \
216 (( (i)->type == XML_SCHEMA_TYPE_BASIC) && \
217 ( (WXS_TYPE_CAST (i))->builtInType == XML_SCHEMAS_ANYTYPE))
218
219#define WXS_IS_COMPLEX(i) \
220 (((i)->type == XML_SCHEMA_TYPE_COMPLEX) || \
221 ((i)->builtInType == XML_SCHEMAS_ANYTYPE))
222
223#define WXS_IS_SIMPLE(item) \
224 ((item->type == XML_SCHEMA_TYPE_SIMPLE) || \
225 ((item->type == XML_SCHEMA_TYPE_BASIC) && \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000226 (item->builtInType != XML_SCHEMAS_ANYTYPE)))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000227
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000228#define WXS_IS_ANY_SIMPLE_TYPE(i) \
229 (((i)->type == XML_SCHEMA_TYPE_BASIC) && \
230 ((i)->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000231
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000232#define WXS_IS_RESTRICTION(t) \
233 ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000234
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000235#define WXS_IS_EXTENSION(t) \
236 ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION)
237
238#define WXS_IS_TYPE_NOT_FIXED(i) \
239 (((i)->type != XML_SCHEMA_TYPE_BASIC) && \
240 (((i)->flags & XML_SCHEMAS_TYPE_INTERNAL_RESOLVED) == 0))
241
242#define WXS_IS_TYPE_NOT_FIXED_1(item) \
243 (((item)->type != XML_SCHEMA_TYPE_BASIC) && \
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000244 (((item)->flags & XML_SCHEMAS_TYPE_FIXUP_1) == 0))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000245/*
246* Macros for exclusively for complex types.
247*/
248#define WXS_HAS_COMPLEX_CONTENT(item) \
249 ((item->contentType == XML_SCHEMA_CONTENT_MIXED) || \
250 (item->contentType == XML_SCHEMA_CONTENT_EMPTY) || \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000251 (item->contentType == XML_SCHEMA_CONTENT_ELEMENTS))
252
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000253#define WXS_HAS_SIMPLE_CONTENT(item) \
254 ((item->contentType == XML_SCHEMA_CONTENT_SIMPLE) || \
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000255 (item->contentType == XML_SCHEMA_CONTENT_BASIC))
256
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000257#define WXS_HAS_MIXED_CONTENT(item) \
258 (item->contentType == XML_SCHEMA_CONTENT_MIXED)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000259
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000260#define WXS_EMPTIABLE(t) \
261 (xmlSchemaIsParticleEmptiable(WXS_PTC_CAST (t)->subtypes))
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000262
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000263#define WXS_TYPE_CONTENTTYPE(t) (t)->subtypes
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000264
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000265#define WXS_TYPE_PARTICLE(t) WXS_PTC_CAST (t)->subtypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000266
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000267#define WXS_TYPE_PARTICLE_TERM(t) WXS_PARTICLE_TERM(WXS_TYPE_PARTICLE(t))
268/*
269* Macros for exclusively for simple types.
270*/
271#define WXS_LIST_ITEMTYPE(t) (t)->subtypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000272
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000273#define WXS_IS_ATOMIC(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_ATOMIC)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000274
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000275#define WXS_IS_LIST(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_LIST)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000276
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000277#define WXS_IS_UNION(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_UNION)
278/*
279* Misc parser context macros.
280*/
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000281#define WXS_CONSTRUCTOR(ctx) (ctx)->constructor
282
283#define WXS_HAS_BUCKETS(ctx) \
284( (WXS_CONSTRUCTOR((ctx))->buckets != NULL) && \
285(WXS_CONSTRUCTOR((ctx))->buckets->nbItems > 0) )
286
287#define WXS_SUBST_GROUPS(ctx) WXS_CONSTRUCTOR((ctx))->substGroups
288
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000289#define WXS_BUCKET(ctx) WXS_CONSTRUCTOR((ctx))->bucket
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000290
291#define WXS_SCHEMA(ctx) (ctx)->schema
292
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000293#define WXS_ADD_LOCAL(ctx, item) \
294 xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->locals), 10, item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000295
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000296#define WXS_ADD_GLOBAL(ctx, item) \
297 xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->globals), 5, item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000298
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000299#define WXS_ADD_PENDING(ctx, item) \
300 xmlSchemaAddItemSize(&((ctx)->constructor->pending), 10, item)
301/*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000302* xmlSchemaItemList macros.
303*/
304#define WXS_ILIST_IS_EMPTY(l) ((l == NULL) || ((l)->nbItems == 0))
305/*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000306* Misc macros.
307*/
308#define IS_SCHEMA(node, type) \
309 ((node != NULL) && (node->ns != NULL) && \
310 (xmlStrEqual(node->name, (const xmlChar *) type)) && \
311 (xmlStrEqual(node->ns->href, xmlSchemaNs)))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000312
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000313#define FREE_AND_NULL(str) if ((str) != NULL) { xmlFree((xmlChar *) (str)); str = NULL; }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000314
315/*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000316* Since we put the default/fixed values into the dict, we can
317* use pointer comparison for those values.
318* REMOVED: (xmlStrEqual((v1), (v2)))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000319*/
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000320#define WXS_ARE_DEFAULT_STR_EQUAL(v1, v2) ((v1) == (v2))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000321
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000322#define INODE_NILLED(item) (item->flags & XML_SCHEMA_ELEM_INFO_NILLED)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000323
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000324#define CAN_PARSE_SCHEMA(b) (((b)->doc != NULL) && ((b)->parsed == 0))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000325
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000326#define HFAILURE if (res == -1) goto exit_failure;
327
328#define HERROR if (res != 0) goto exit_error;
329
330#define HSTOP(ctx) if ((ctx)->stop) goto exit;
331/*
332* Some flags used for various schema constraints.
333*/
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +0000334#define SUBSET_RESTRICTION 1<<0
335#define SUBSET_EXTENSION 1<<1
336#define SUBSET_SUBSTITUTION 1<<2
337#define SUBSET_LIST 1<<3
338#define SUBSET_UNION 1<<4
339
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000340typedef struct _xmlSchemaNodeInfo xmlSchemaNodeInfo;
341typedef xmlSchemaNodeInfo *xmlSchemaNodeInfoPtr;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000342
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000343typedef struct _xmlSchemaItemList xmlSchemaItemList;
344typedef xmlSchemaItemList *xmlSchemaItemListPtr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000345struct _xmlSchemaItemList {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000346 void **items; /* used for dynamic addition of schemata */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000347 int nbItems; /* used for dynamic addition of schemata */
348 int sizeItems; /* used for dynamic addition of schemata */
349};
350
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000351#define XML_SCHEMA_CTXT_PARSER 1
352#define XML_SCHEMA_CTXT_VALIDATOR 2
353
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000354typedef struct _xmlSchemaAbstractCtxt xmlSchemaAbstractCtxt;
355typedef xmlSchemaAbstractCtxt *xmlSchemaAbstractCtxtPtr;
356struct _xmlSchemaAbstractCtxt {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000357 int type; /* E.g. XML_SCHEMA_CTXT_VALIDATOR */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000358};
359
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000360typedef struct _xmlSchemaBucket xmlSchemaBucket;
361typedef xmlSchemaBucket *xmlSchemaBucketPtr;
362
363#define XML_SCHEMA_SCHEMA_MAIN 0
364#define XML_SCHEMA_SCHEMA_IMPORT 1
365#define XML_SCHEMA_SCHEMA_INCLUDE 2
366#define XML_SCHEMA_SCHEMA_REDEFINE 3
367
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000368/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000369 * xmlSchemaSchemaRelation:
370 *
371 * Used to create a graph of schema relationships.
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000372 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000373typedef struct _xmlSchemaSchemaRelation xmlSchemaSchemaRelation;
374typedef xmlSchemaSchemaRelation *xmlSchemaSchemaRelationPtr;
375struct _xmlSchemaSchemaRelation {
376 xmlSchemaSchemaRelationPtr next;
377 int type; /* E.g. XML_SCHEMA_SCHEMA_IMPORT */
378 const xmlChar *importNamespace;
379 xmlSchemaBucketPtr bucket;
380};
381
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000382#define XML_SCHEMA_BUCKET_MARKED 1<<0
383#define XML_SCHEMA_BUCKET_COMPS_ADDED 1<<1
384
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000385struct _xmlSchemaBucket {
386 int type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000387 int flags;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000388 const xmlChar *schemaLocation;
389 const xmlChar *origTargetNamespace;
390 const xmlChar *targetNamespace;
391 xmlDocPtr doc;
392 xmlSchemaSchemaRelationPtr relations;
393 int located;
394 int parsed;
395 int imported;
396 int preserveDoc;
397 xmlSchemaItemListPtr globals; /* Global components. */
398 xmlSchemaItemListPtr locals; /* Local components. */
399};
400
401/**
402 * xmlSchemaImport:
403 * (extends xmlSchemaBucket)
404 *
405 * Reflects a schema. Holds some information
406 * about the schema and its toplevel components. Duplicate
407 * toplevel components are not checked at this level.
408 */
409typedef struct _xmlSchemaImport xmlSchemaImport;
410typedef xmlSchemaImport *xmlSchemaImportPtr;
411struct _xmlSchemaImport {
412 int type; /* Main OR import OR include. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000413 int flags;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000414 const xmlChar *schemaLocation; /* The URI of the schema document. */
415 /* For chameleon includes, @origTargetNamespace will be NULL */
416 const xmlChar *origTargetNamespace;
417 /*
418 * For chameleon includes, @targetNamespace will be the
419 * targetNamespace of the including schema.
420 */
421 const xmlChar *targetNamespace;
422 xmlDocPtr doc; /* The schema node-tree. */
423 /* @relations will hold any included/imported/redefined schemas. */
424 xmlSchemaSchemaRelationPtr relations;
425 int located;
426 int parsed;
427 int imported;
428 int preserveDoc;
429 xmlSchemaItemListPtr globals;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000430 xmlSchemaItemListPtr locals;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000431 /* The imported schema. */
432 xmlSchemaPtr schema;
433};
434
435/*
436* (extends xmlSchemaBucket)
437*/
438typedef struct _xmlSchemaInclude xmlSchemaInclude;
439typedef xmlSchemaInclude *xmlSchemaIncludePtr;
440struct _xmlSchemaInclude {
441 int type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000442 int flags;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000443 const xmlChar *schemaLocation;
444 const xmlChar *origTargetNamespace;
445 const xmlChar *targetNamespace;
446 xmlDocPtr doc;
447 xmlSchemaSchemaRelationPtr relations;
448 int located;
449 int parsed;
450 int imported;
451 int preserveDoc;
452 xmlSchemaItemListPtr globals; /* Global components. */
453 xmlSchemaItemListPtr locals; /* Local components. */
454
455 /* The owning main or import schema bucket. */
456 xmlSchemaImportPtr ownerImport;
457};
458
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000459/**
460 * xmlSchemaBasicItem:
461 *
462 * The abstract base type for schema components.
463 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000464typedef struct _xmlSchemaBasicItem xmlSchemaBasicItem;
465typedef xmlSchemaBasicItem *xmlSchemaBasicItemPtr;
466struct _xmlSchemaBasicItem {
467 xmlSchemaTypeType type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000468};
469
470/**
471 * xmlSchemaAnnotItem:
472 *
473 * The abstract base type for annotated schema components.
474 * (Extends xmlSchemaBasicItem)
475 */
476typedef struct _xmlSchemaAnnotItem xmlSchemaAnnotItem;
477typedef xmlSchemaAnnotItem *xmlSchemaAnnotItemPtr;
478struct _xmlSchemaAnnotItem {
479 xmlSchemaTypeType type;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000480 xmlSchemaAnnotPtr annot;
481};
482
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000483/**
484 * xmlSchemaTreeItem:
485 *
486 * The abstract base type for tree-like structured schema components.
487 * (Extends xmlSchemaAnnotItem)
488 */
489typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem;
490typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr;
491struct _xmlSchemaTreeItem {
492 xmlSchemaTypeType type;
493 xmlSchemaAnnotPtr annot;
494 xmlSchemaTreeItemPtr next;
495 xmlSchemaTreeItemPtr children;
496};
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000497
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000498
499#define XML_SCHEMA_ATTR_USE_FIXED 1<<0
500/**
501 * xmlSchemaAttributeUsePtr:
502 *
503 * The abstract base type for tree-like structured schema components.
504 * (Extends xmlSchemaTreeItem)
505 */
506typedef struct _xmlSchemaAttributeUse xmlSchemaAttributeUse;
507typedef xmlSchemaAttributeUse *xmlSchemaAttributeUsePtr;
508struct _xmlSchemaAttributeUse {
509 xmlSchemaTypeType type;
510 xmlSchemaAnnotPtr annot;
511 xmlSchemaAttributeUsePtr next; /* The next attr. use. */
512 /*
513 * The attr. decl. OR a QName-ref. to an attr. decl. OR
514 * a QName-ref. to an attribute group definition.
515 */
516 xmlSchemaAttributePtr attrDecl;
517
518 int flags;
519 xmlNodePtr node;
520 int occurs; /* required, optional */
521 const xmlChar * defValue;
522 xmlSchemaValPtr defVal;
523};
524
525/**
526 * xmlSchemaAttributeUseProhibPtr:
527 *
528 * A helper component to reflect attribute prohibitions.
529 * (Extends xmlSchemaBasicItem)
530 */
531typedef struct _xmlSchemaAttributeUseProhib xmlSchemaAttributeUseProhib;
532typedef xmlSchemaAttributeUseProhib *xmlSchemaAttributeUseProhibPtr;
533struct _xmlSchemaAttributeUseProhib {
534 xmlSchemaTypeType type; /* == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB */
535 xmlNodePtr node;
536 const xmlChar *name;
537 const xmlChar *targetNamespace;
538 int isRef;
539};
540
541/**
542 * xmlSchemaRedef:
543 */
544typedef struct _xmlSchemaRedef xmlSchemaRedef;
545typedef xmlSchemaRedef *xmlSchemaRedefPtr;
546struct _xmlSchemaRedef {
547 xmlSchemaRedefPtr next;
548 xmlSchemaBasicItemPtr item; /* The redefining component. */
549 xmlSchemaBasicItemPtr reference; /* The referencing component. */
550 xmlSchemaBasicItemPtr target; /* The to-be-redefined component. */
551 const xmlChar *refName; /* The name of the to-be-redefined component. */
552 const xmlChar *refTargetNs; /* The target namespace of the
553 to-be-redefined comp. */
554 xmlSchemaBucketPtr targetBucket; /* The redefined schema. */
555};
556
557/**
558 * xmlSchemaConstructionCtxt:
559 */
560typedef struct _xmlSchemaConstructionCtxt xmlSchemaConstructionCtxt;
561typedef xmlSchemaConstructionCtxt *xmlSchemaConstructionCtxtPtr;
562struct _xmlSchemaConstructionCtxt {
563 xmlSchemaPtr mainSchema; /* The main schema. */
564 xmlSchemaBucketPtr mainBucket; /* The main schema bucket */
565 xmlDictPtr dict;
566 xmlSchemaItemListPtr buckets; /* List of schema buckets. */
567 /* xmlSchemaItemListPtr relations; */ /* List of schema relations. */
568 xmlSchemaBucketPtr bucket; /* The current schema bucket */
569 xmlSchemaItemListPtr pending; /* All Components of all schemas that
570 need to be fixed. */
571 xmlHashTablePtr substGroups;
572 xmlSchemaRedefPtr redefs;
573 xmlSchemaRedefPtr lastRedef;
574};
575
576#define XML_SCHEMAS_PARSE_ERROR 1
577#define SCHEMAS_PARSE_OPTIONS XML_PARSE_NOENT
578
579struct _xmlSchemaParserCtxt {
580 int type;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +0000581 void *errCtxt; /* user specific error context */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000582 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
583 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
584 xmlSchemaValidError err;
585 int nberrors;
586 xmlStructuredErrorFunc serror;
587
588 xmlSchemaConstructionCtxtPtr constructor;
589 int ownsConstructor; /* TODO: Move this to parser *flags*. */
590
591 /* xmlSchemaPtr topschema; */
592 /* xmlHashTablePtr namespaces; */
593
594 xmlSchemaPtr schema; /* The main schema in use */
595 int counter;
596
597 const xmlChar *URL;
598 xmlDocPtr doc;
599 int preserve; /* Whether the doc should be freed */
600
601 const char *buffer;
602 int size;
603
604 /*
605 * Used to build complex element content models
606 */
607 xmlAutomataPtr am;
608 xmlAutomataStatePtr start;
609 xmlAutomataStatePtr end;
610 xmlAutomataStatePtr state;
611
612 xmlDictPtr dict; /* dictionnary for interned string names */
613 xmlSchemaTypePtr ctxtType; /* The current context simple/complex type */
614 int options;
615 xmlSchemaValidCtxtPtr vctxt;
616 int isS4S;
617 int isRedefine;
618 int xsiAssemble;
619 int stop; /* If the parser should stop; i.e. a critical error. */
620 const xmlChar *targetNamespace;
621 xmlSchemaBucketPtr redefined; /* The schema to be redefined. */
622
623 xmlSchemaRedefPtr redef; /* Used for redefinitions. */
624 int redefCounter; /* Used for redefinitions. */
625 xmlSchemaItemListPtr attrProhibs;
626};
627
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000628/**
629 * xmlSchemaQNameRef:
630 *
631 * A component reference item (not a schema component)
632 * (Extends xmlSchemaBasicItem)
633 */
634typedef struct _xmlSchemaQNameRef xmlSchemaQNameRef;
635typedef xmlSchemaQNameRef *xmlSchemaQNameRefPtr;
636struct _xmlSchemaQNameRef {
637 xmlSchemaTypeType type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000638 xmlSchemaBasicItemPtr item; /* The resolved referenced item. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000639 xmlSchemaTypeType itemType;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000640 const xmlChar *name;
641 const xmlChar *targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000642 xmlNodePtr node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000643};
644
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000645/**
646 * xmlSchemaParticle:
647 *
648 * A particle component.
649 * (Extends xmlSchemaTreeItem)
650 */
651typedef struct _xmlSchemaParticle xmlSchemaParticle;
652typedef xmlSchemaParticle *xmlSchemaParticlePtr;
653struct _xmlSchemaParticle {
654 xmlSchemaTypeType type;
655 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000656 xmlSchemaTreeItemPtr next; /* next particle */
657 xmlSchemaTreeItemPtr children; /* the "term" (e.g. a model group,
658 a group definition, a XML_SCHEMA_EXTRA_QNAMEREF (if a reference),
659 etc.) */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000660 int minOccurs;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000661 int maxOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000662 xmlNodePtr node;
663};
664
665/**
666 * xmlSchemaModelGroup:
667 *
668 * A model group component.
669 * (Extends xmlSchemaTreeItem)
670 */
671typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup;
672typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr;
673struct _xmlSchemaModelGroup {
674 xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_SEQUENCE, XML_SCHEMA_TYPE_CHOICE, XML_SCHEMA_TYPE_ALL */
675 xmlSchemaAnnotPtr annot;
676 xmlSchemaTreeItemPtr next; /* not used */
677 xmlSchemaTreeItemPtr children; /* first particle (OR "element decl" OR "wildcard") */
678 xmlNodePtr node;
679};
680
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000681#define XML_SCHEMA_MODEL_GROUP_DEF_MARKED 1<<0
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000682#define XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED 1<<1
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000683/**
684 * xmlSchemaModelGroupDef:
685 *
686 * A model group definition component.
687 * (Extends xmlSchemaTreeItem)
688 */
689typedef struct _xmlSchemaModelGroupDef xmlSchemaModelGroupDef;
690typedef xmlSchemaModelGroupDef *xmlSchemaModelGroupDefPtr;
691struct _xmlSchemaModelGroupDef {
692 xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_GROUP */
693 xmlSchemaAnnotPtr annot;
694 xmlSchemaTreeItemPtr next; /* not used */
695 xmlSchemaTreeItemPtr children; /* the "model group" */
696 const xmlChar *name;
697 const xmlChar *targetNamespace;
698 xmlNodePtr node;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000699 int flags;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000700};
701
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000702typedef struct _xmlSchemaIDC xmlSchemaIDC;
703typedef xmlSchemaIDC *xmlSchemaIDCPtr;
704
705/**
706 * xmlSchemaIDCSelect:
707 *
708 * The identity-constraint "field" and "selector" item, holding the
709 * XPath expression.
710 */
711typedef struct _xmlSchemaIDCSelect xmlSchemaIDCSelect;
712typedef xmlSchemaIDCSelect *xmlSchemaIDCSelectPtr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000713struct _xmlSchemaIDCSelect {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000714 xmlSchemaIDCSelectPtr next;
715 xmlSchemaIDCPtr idc;
716 int index; /* an index position if significant for IDC key-sequences */
717 const xmlChar *xpath; /* the XPath expression */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000718 void *xpathComp; /* the compiled XPath expression */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000719};
720
721/**
722 * xmlSchemaIDC:
723 *
724 * The identity-constraint definition component.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000725 * (Extends xmlSchemaAnnotItem)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000726 */
727
728struct _xmlSchemaIDC {
729 xmlSchemaTypeType type;
730 xmlSchemaAnnotPtr annot;
731 xmlSchemaIDCPtr next;
732 xmlNodePtr node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000733 const xmlChar *name;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000734 const xmlChar *targetNamespace;
735 xmlSchemaIDCSelectPtr selector;
736 xmlSchemaIDCSelectPtr fields;
737 int nbFields;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000738 xmlSchemaQNameRefPtr ref;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000739};
740
741/**
742 * xmlSchemaIDCAug:
743 *
744 * The augmented IDC information used for validation.
745 */
746typedef struct _xmlSchemaIDCAug xmlSchemaIDCAug;
747typedef xmlSchemaIDCAug *xmlSchemaIDCAugPtr;
748struct _xmlSchemaIDCAug {
749 xmlSchemaIDCAugPtr next; /* next in a list */
750 xmlSchemaIDCPtr def; /* the IDC definition */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000751 int keyrefDepth; /* the lowest tree level to which IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000752 tables need to be bubbled upwards */
753};
754
755/**
756 * xmlSchemaPSVIIDCKeySequence:
757 *
758 * The key sequence of a node table item.
759 */
760typedef struct _xmlSchemaPSVIIDCKey xmlSchemaPSVIIDCKey;
761typedef xmlSchemaPSVIIDCKey *xmlSchemaPSVIIDCKeyPtr;
762struct _xmlSchemaPSVIIDCKey {
763 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000764 xmlSchemaValPtr val;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000765};
766
767/**
768 * xmlSchemaPSVIIDCNode:
769 *
770 * The node table item of a node table.
771 */
772typedef struct _xmlSchemaPSVIIDCNode xmlSchemaPSVIIDCNode;
773typedef xmlSchemaPSVIIDCNode *xmlSchemaPSVIIDCNodePtr;
774struct _xmlSchemaPSVIIDCNode {
775 xmlNodePtr node;
776 xmlSchemaPSVIIDCKeyPtr *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000777 int nodeLine;
778 int nodeQNameID;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000779
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000780};
781
782/**
783 * xmlSchemaPSVIIDCBinding:
784 *
785 * The identity-constraint binding item of the [identity-constraint table].
786 */
787typedef struct _xmlSchemaPSVIIDCBinding xmlSchemaPSVIIDCBinding;
788typedef xmlSchemaPSVIIDCBinding *xmlSchemaPSVIIDCBindingPtr;
789struct _xmlSchemaPSVIIDCBinding {
790 xmlSchemaPSVIIDCBindingPtr next; /* next binding of a specific node */
791 xmlSchemaIDCPtr definition; /* the IDC definition */
792 xmlSchemaPSVIIDCNodePtr *nodeTable; /* array of key-sequences */
793 int nbNodes; /* number of entries in the node table */
794 int sizeNodes; /* size of the node table */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000795 xmlSchemaItemListPtr dupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000796};
797
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000798
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000799#define XPATH_STATE_OBJ_TYPE_IDC_SELECTOR 1
800#define XPATH_STATE_OBJ_TYPE_IDC_FIELD 2
801
802#define XPATH_STATE_OBJ_MATCHES -2
803#define XPATH_STATE_OBJ_BLOCKED -3
804
805typedef struct _xmlSchemaIDCMatcher xmlSchemaIDCMatcher;
806typedef xmlSchemaIDCMatcher *xmlSchemaIDCMatcherPtr;
807
808/**
809 * xmlSchemaIDCStateObj:
810 *
811 * The state object used to evaluate XPath expressions.
812 */
813typedef struct _xmlSchemaIDCStateObj xmlSchemaIDCStateObj;
814typedef xmlSchemaIDCStateObj *xmlSchemaIDCStateObjPtr;
815struct _xmlSchemaIDCStateObj {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000816 int type;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000817 xmlSchemaIDCStateObjPtr next; /* next if in a list */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000818 int depth; /* depth of creation */
819 int *history; /* list of (depth, state-id) tuples */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000820 int nbHistory;
821 int sizeHistory;
822 xmlSchemaIDCMatcherPtr matcher; /* the correspondent field/selector
823 matcher */
824 xmlSchemaIDCSelectPtr sel;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000825 void *xpathCtxt;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000826};
827
828#define IDC_MATCHER 0
829
830/**
831 * xmlSchemaIDCMatcher:
832 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000833 * Used to evaluate IDC selectors (and fields).
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000834 */
835struct _xmlSchemaIDCMatcher {
836 int type;
837 int depth; /* the tree depth at creation time */
838 xmlSchemaIDCMatcherPtr next; /* next in the list */
839 xmlSchemaIDCAugPtr aidc; /* the augmented IDC item */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000840 int idcType;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000841 xmlSchemaPSVIIDCKeyPtr **keySeqs; /* the key-sequences of the target
842 elements */
843 int sizeKeySeqs;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000844 int targetDepth;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000845 xmlSchemaItemListPtr targets; /* list of target-node
846 (xmlSchemaPSVIIDCNodePtr) entries */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000847};
848
849/*
850* Element info flags.
851*/
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000852#define XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES 1<<0
853#define XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES 1<<1
854#define XML_SCHEMA_ELEM_INFO_NILLED 1<<2
855#define XML_SCHEMA_ELEM_INFO_LOCAL_TYPE 1<<3
856
857#define XML_SCHEMA_NODE_INFO_VALUE_NEEDED 1<<4
858#define XML_SCHEMA_ELEM_INFO_EMPTY 1<<5
859#define XML_SCHEMA_ELEM_INFO_HAS_CONTENT 1<<6
860
861#define XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT 1<<7
862#define XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT 1<<8
863#define XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED 1<<9
864#define XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE 1<<10
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000865
866/**
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000867 * xmlSchemaNodeInfo:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000868 *
869 * Holds information of an element node.
870 */
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000871struct _xmlSchemaNodeInfo {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000872 int nodeType;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000873 xmlNodePtr node;
874 int nodeLine;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000875 const xmlChar *localName;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000876 const xmlChar *nsName;
877 const xmlChar *value;
878 xmlSchemaValPtr val; /* the pre-computed value if any */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000879 xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000880
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000881 int flags; /* combination of node info flags */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000882
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000883 int valNeeded;
884 int normVal;
885
886 xmlSchemaElementPtr decl; /* the element/attribute declaration */
887 int depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000888 xmlSchemaPSVIIDCBindingPtr idcTable; /* the table of PSVI IDC bindings
889 for the scope element*/
890 xmlSchemaIDCMatcherPtr idcMatchers; /* the IDC matchers for the scope
891 element */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000892 xmlRegExecCtxtPtr regexCtxt;
893
894 const xmlChar **nsBindings; /* Namespace bindings on this element */
895 int nbNsBindings;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000896 int sizeNsBindings;
897
898 int hasKeyrefs;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +0000899 int appliedXPath; /* Indicates that an XPath has been applied. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000900};
901
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000902#define XML_SCHEMAS_ATTR_UNKNOWN 1
903#define XML_SCHEMAS_ATTR_ASSESSED 2
904#define XML_SCHEMAS_ATTR_PROHIBITED 3
905#define XML_SCHEMAS_ATTR_ERR_MISSING 4
906#define XML_SCHEMAS_ATTR_INVALID_VALUE 5
907#define XML_SCHEMAS_ATTR_ERR_NO_TYPE 6
908#define XML_SCHEMAS_ATTR_ERR_FIXED_VALUE 7
909#define XML_SCHEMAS_ATTR_DEFAULT 8
910#define XML_SCHEMAS_ATTR_VALIDATE_VALUE 9
911#define XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL 10
912#define XML_SCHEMAS_ATTR_HAS_ATTR_USE 11
913#define XML_SCHEMAS_ATTR_HAS_ATTR_DECL 12
914#define XML_SCHEMAS_ATTR_WILD_SKIP 13
915#define XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL 14
916#define XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID 15
917#define XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID 16
918#define XML_SCHEMAS_ATTR_META 17
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000919/*
920* @metaType values of xmlSchemaAttrInfo.
921*/
922#define XML_SCHEMA_ATTR_INFO_META_XSI_TYPE 1
923#define XML_SCHEMA_ATTR_INFO_META_XSI_NIL 2
924#define XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC 3
925#define XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC 4
926#define XML_SCHEMA_ATTR_INFO_META_XMLNS 5
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000927
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000928typedef struct _xmlSchemaAttrInfo xmlSchemaAttrInfo;
929typedef xmlSchemaAttrInfo *xmlSchemaAttrInfoPtr;
930struct _xmlSchemaAttrInfo {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000931 int nodeType;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000932 xmlNodePtr node;
933 int nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000934 const xmlChar *localName;
935 const xmlChar *nsName;
936 const xmlChar *value;
937 xmlSchemaValPtr val; /* the pre-computed value if any */
938 xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
939 int flags; /* combination of node info flags */
940
941 xmlSchemaAttributePtr decl; /* the attribute declaration */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000942 xmlSchemaAttributeUsePtr use; /* the attribute use */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000943 int state;
944 int metaType;
945 const xmlChar *vcValue; /* the value constraint value */
946 xmlSchemaNodeInfoPtr parent;
947};
948
949
950#define XML_SCHEMA_VALID_CTXT_FLAG_STREAM 1
Daniel Veillard4255d502002-04-16 15:50:10 +0000951/**
952 * xmlSchemaValidCtxt:
953 *
954 * A Schemas validation context
955 */
Daniel Veillard4255d502002-04-16 15:50:10 +0000956struct _xmlSchemaValidCtxt {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000957 int type;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +0000958 void *errCtxt; /* user specific data block */
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000959 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000960 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000961 xmlStructuredErrorFunc serror;
Daniel Veillard4255d502002-04-16 15:50:10 +0000962
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000963 xmlSchemaPtr schema; /* The schema in use */
964 xmlDocPtr doc;
Daniel Veillard4255d502002-04-16 15:50:10 +0000965 xmlParserInputBufferPtr input;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000966 xmlCharEncoding enc;
967 xmlSAXHandlerPtr sax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000968 xmlParserCtxtPtr parserCtxt;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000969 void *user_data; /* TODO: What is this for? */
Daniel Veillard4255d502002-04-16 15:50:10 +0000970
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000971 int err;
972 int nberrors;
Daniel Veillard4255d502002-04-16 15:50:10 +0000973
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000974 xmlNodePtr node;
975 xmlNodePtr cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000976 /* xmlSchemaTypePtr type; */
Daniel Veillard4255d502002-04-16 15:50:10 +0000977
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000978 xmlRegExecCtxtPtr regexp;
979 xmlSchemaValPtr value;
Daniel Veillard4255d502002-04-16 15:50:10 +0000980
Daniel Veillardc0826a72004-08-10 14:17:33 +0000981 int valueWS;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000982 int options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000983 xmlNodePtr validationRoot;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000984 xmlSchemaParserCtxtPtr pctxt;
985 int xsiAssemble;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000986
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000987 int depth;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000988 xmlSchemaNodeInfoPtr *elemInfos; /* array of element informations */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000989 int sizeElemInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000990 xmlSchemaNodeInfoPtr inode; /* the current element information */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000991
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000992 xmlSchemaIDCAugPtr aidcs; /* a list of augmented IDC informations */
993
994 xmlSchemaIDCStateObjPtr xpathStates; /* first active state object. */
995 xmlSchemaIDCStateObjPtr xpathStatePool; /* first stored state object. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000996
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000997 xmlSchemaPSVIIDCNodePtr *idcNodes; /* list of all IDC node-table entries*/
998 int nbIdcNodes;
999 int sizeIdcNodes;
1000
1001 xmlSchemaPSVIIDCKeyPtr *idcKeys; /* list of all IDC node-table entries */
1002 int nbIdcKeys;
1003 int sizeIdcKeys;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001004
1005 int flags;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001006
1007 xmlDictPtr dict;
1008
Daniel Veillard39e5c892005-07-03 22:48:50 +00001009#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001010 xmlTextReaderPtr reader;
Daniel Veillard39e5c892005-07-03 22:48:50 +00001011#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001012
1013 xmlSchemaAttrInfoPtr *attrInfos;
1014 int nbAttrInfos;
1015 int sizeAttrInfos;
1016
1017 int skipDepth;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00001018 xmlSchemaItemListPtr nodeQNames;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00001019 int hasKeyrefs;
1020 int createIDCNodeTables;
1021 int psviExposeIDCNodeTables;
Daniel Veillard4255d502002-04-16 15:50:10 +00001022};
1023
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00001024/**
1025 * xmlSchemaSubstGroup:
1026 *
1027 *
1028 */
1029typedef struct _xmlSchemaSubstGroup xmlSchemaSubstGroup;
1030typedef xmlSchemaSubstGroup *xmlSchemaSubstGroupPtr;
1031struct _xmlSchemaSubstGroup {
1032 xmlSchemaElementPtr head;
1033 xmlSchemaItemListPtr members;
1034};
1035
Daniel Veillard4255d502002-04-16 15:50:10 +00001036/************************************************************************
1037 * *
1038 * Some predeclarations *
1039 * *
1040 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001041
Daniel Veillardbd2904b2003-11-25 15:38:59 +00001042static int xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt,
1043 xmlSchemaPtr schema,
1044 xmlNodePtr node);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00001045static int xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr ctxt,
1046 xmlSchemaPtr schema,
1047 xmlNodePtr node);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001048static int
1049xmlSchemaTypeFixup(xmlSchemaTypePtr type,
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +00001050 xmlSchemaAbstractCtxtPtr ctxt);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00001051static const xmlChar *
Daniel Veillard01fa6152004-06-29 17:04:39 +00001052xmlSchemaFacetTypeToString(xmlSchemaTypeType type);
1053static int
William M. Brack2f2a6632004-08-20 23:09:47 +00001054xmlSchemaParseImport(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1055 xmlNodePtr node);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001056static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001057xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
1058 xmlSchemaParserCtxtPtr ctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +00001059static void
1060xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001061static xmlSchemaWhitespaceValueType
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001062xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001063static xmlSchemaTreeItemPtr
1064xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1065 xmlNodePtr node, xmlSchemaTypeType type,
1066 int withParticle);
1067static const xmlChar *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001068xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001069static xmlSchemaTypeLinkPtr
1070xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001071static void
1072xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
1073 const char *funcName,
1074 const char *message);
1075static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +00001076xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001077 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001078 xmlSchemaTypePtr baseType,
1079 int subset);
1080static void
1081xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001082 xmlSchemaParserCtxtPtr ctxt);
1083static void
1084xmlSchemaComponentListFree(xmlSchemaItemListPtr list);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001085static xmlSchemaQNameRefPtr
1086xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
1087 xmlSchemaPtr schema,
1088 xmlNodePtr node);
William M. Brack87640d52004-04-17 14:58:15 +00001089
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001090/************************************************************************
1091 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001092 * Helper functions *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001093 * *
1094 ************************************************************************/
1095
1096/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001097 * xmlSchemaItemTypeToStr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001098 * @type: the type of the schema item
1099 *
1100 * Returns the component name of a schema item.
1101 */
1102static const xmlChar *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001103xmlSchemaItemTypeToStr(xmlSchemaTypeType type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001104{
1105 switch (type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001106 case XML_SCHEMA_TYPE_BASIC:
1107 return(BAD_CAST "simple type definition");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001108 case XML_SCHEMA_TYPE_SIMPLE:
1109 return(BAD_CAST "simple type definition");
1110 case XML_SCHEMA_TYPE_COMPLEX:
1111 return(BAD_CAST "complex type definition");
1112 case XML_SCHEMA_TYPE_ELEMENT:
1113 return(BAD_CAST "element declaration");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001114 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1115 return(BAD_CAST "attribute use");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001116 case XML_SCHEMA_TYPE_ATTRIBUTE:
1117 return(BAD_CAST "attribute declaration");
1118 case XML_SCHEMA_TYPE_GROUP:
1119 return(BAD_CAST "model group definition");
1120 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1121 return(BAD_CAST "attribute group definition");
1122 case XML_SCHEMA_TYPE_NOTATION:
1123 return(BAD_CAST "notation declaration");
1124 case XML_SCHEMA_TYPE_SEQUENCE:
1125 return(BAD_CAST "model group (sequence)");
1126 case XML_SCHEMA_TYPE_CHOICE:
1127 return(BAD_CAST "model group (choice)");
1128 case XML_SCHEMA_TYPE_ALL:
1129 return(BAD_CAST "model group (all)");
1130 case XML_SCHEMA_TYPE_PARTICLE:
1131 return(BAD_CAST "particle");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001132 case XML_SCHEMA_TYPE_IDC_UNIQUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001133 return(BAD_CAST "unique identity-constraint");
1134 /* return(BAD_CAST "IDC (unique)"); */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001135 case XML_SCHEMA_TYPE_IDC_KEY:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001136 return(BAD_CAST "key identity-constraint");
1137 /* return(BAD_CAST "IDC (key)"); */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001138 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001139 return(BAD_CAST "keyref identity-constraint");
1140 /* return(BAD_CAST "IDC (keyref)"); */
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00001141 case XML_SCHEMA_TYPE_ANY:
1142 return(BAD_CAST "wildcard (any)");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001143 case XML_SCHEMA_EXTRA_QNAMEREF:
1144 return(BAD_CAST "[helper component] QName reference");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001145 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
1146 return(BAD_CAST "[helper component] attribute use prohibition");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001147 default:
1148 return(BAD_CAST "Not a schema component");
1149 }
1150}
1151
1152/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001153 * xmlSchemaGetComponentTypeStr:
1154 * @type: the type of the schema item
1155 *
1156 * Returns the component name of a schema item.
1157 */
1158static const xmlChar *
1159xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item)
1160{
1161 switch (item->type) {
1162 case XML_SCHEMA_TYPE_BASIC:
1163 if (WXS_IS_COMPLEX(WXS_TYPE_CAST item))
1164 return(BAD_CAST "complex type definition");
1165 else
1166 return(BAD_CAST "simple type definition");
1167 default:
1168 return(xmlSchemaItemTypeToStr(item->type));
1169 }
1170}
1171
1172/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001173 * xmlSchemaGetComponentNode:
1174 * @item: a schema component
1175 *
1176 * Returns node associated with the schema component.
1177 * NOTE that such a node need not be available; plus, a component's
1178 * node need not to reflect the component directly, since there is no
1179 * one-to-one relationship between the XML Schema representation and
1180 * the component representation.
1181 */
1182static xmlNodePtr
1183xmlSchemaGetComponentNode(xmlSchemaBasicItemPtr item)
1184{
1185 switch (item->type) {
1186 case XML_SCHEMA_TYPE_ELEMENT:
1187 return (((xmlSchemaElementPtr) item)->node);
1188 case XML_SCHEMA_TYPE_ATTRIBUTE:
1189 return (((xmlSchemaAttributePtr) item)->node);
1190 case XML_SCHEMA_TYPE_COMPLEX:
1191 case XML_SCHEMA_TYPE_SIMPLE:
1192 return (((xmlSchemaTypePtr) item)->node);
1193 case XML_SCHEMA_TYPE_ANY:
1194 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1195 return (((xmlSchemaWildcardPtr) item)->node);
1196 case XML_SCHEMA_TYPE_PARTICLE:
1197 return (((xmlSchemaParticlePtr) item)->node);
1198 case XML_SCHEMA_TYPE_SEQUENCE:
1199 case XML_SCHEMA_TYPE_CHOICE:
1200 case XML_SCHEMA_TYPE_ALL:
1201 return (((xmlSchemaModelGroupPtr) item)->node);
1202 case XML_SCHEMA_TYPE_GROUP:
1203 return (((xmlSchemaModelGroupDefPtr) item)->node);
1204 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1205 return (((xmlSchemaAttributeGroupPtr) item)->node);
1206 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1207 case XML_SCHEMA_TYPE_IDC_KEY:
1208 case XML_SCHEMA_TYPE_IDC_KEYREF:
1209 return (((xmlSchemaIDCPtr) item)->node);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001210 case XML_SCHEMA_EXTRA_QNAMEREF:
1211 return(((xmlSchemaQNameRefPtr) item)->node);
1212 /* TODO: What to do with NOTATIONs?
1213 case XML_SCHEMA_TYPE_NOTATION:
1214 return (((xmlSchemaNotationPtr) item)->node);
1215 */
1216 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1217 return (((xmlSchemaAttributeUsePtr) item)->node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001218 default:
1219 return (NULL);
1220 }
1221}
1222
1223#if 0
1224/**
1225 * xmlSchemaGetNextComponent:
1226 * @item: a schema component
1227 *
1228 * Returns the next sibling of the schema component.
1229 */
1230static xmlSchemaBasicItemPtr
1231xmlSchemaGetNextComponent(xmlSchemaBasicItemPtr item)
1232{
1233 switch (item->type) {
1234 case XML_SCHEMA_TYPE_ELEMENT:
1235 return ((xmlSchemaBasicItemPtr) ((xmlSchemaElementPtr) item)->next);
1236 case XML_SCHEMA_TYPE_ATTRIBUTE:
1237 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributePtr) item)->next);
1238 case XML_SCHEMA_TYPE_COMPLEX:
1239 case XML_SCHEMA_TYPE_SIMPLE:
1240 return ((xmlSchemaBasicItemPtr) ((xmlSchemaTypePtr) item)->next);
1241 case XML_SCHEMA_TYPE_ANY:
1242 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1243 return (NULL);
1244 case XML_SCHEMA_TYPE_PARTICLE:
1245 return ((xmlSchemaBasicItemPtr) ((xmlSchemaParticlePtr) item)->next);
1246 case XML_SCHEMA_TYPE_SEQUENCE:
1247 case XML_SCHEMA_TYPE_CHOICE:
1248 case XML_SCHEMA_TYPE_ALL:
1249 return (NULL);
1250 case XML_SCHEMA_TYPE_GROUP:
1251 return (NULL);
1252 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1253 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributeGroupPtr) item)->next);
1254 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1255 case XML_SCHEMA_TYPE_IDC_KEY:
1256 case XML_SCHEMA_TYPE_IDC_KEYREF:
1257 return ((xmlSchemaBasicItemPtr) ((xmlSchemaIDCPtr) item)->next);
1258 default:
1259 return (NULL);
1260 }
1261}
1262#endif
1263
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001264
1265/**
1266 * xmlSchemaFormatQName:
1267 * @buf: the string buffer
1268 * @namespaceName: the namespace name
1269 * @localName: the local name
1270 *
1271 * Returns the given QName in the format "{namespaceName}localName" or
1272 * just "localName" if @namespaceName is NULL.
1273 *
1274 * Returns the localName if @namespaceName is NULL, a formatted
1275 * string otherwise.
1276 */
1277static const xmlChar*
1278xmlSchemaFormatQName(xmlChar **buf,
1279 const xmlChar *namespaceName,
1280 const xmlChar *localName)
1281{
1282 FREE_AND_NULL(*buf)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001283 if (namespaceName != NULL) {
1284 *buf = xmlStrdup(BAD_CAST "{");
1285 *buf = xmlStrcat(*buf, namespaceName);
1286 *buf = xmlStrcat(*buf, BAD_CAST "}");
1287 }
1288 if (localName != NULL) {
1289 if (namespaceName == NULL)
1290 return(localName);
1291 *buf = xmlStrcat(*buf, localName);
1292 } else {
1293 *buf = xmlStrcat(*buf, BAD_CAST "(NULL)");
1294 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001295 return ((const xmlChar *) *buf);
1296}
1297
1298static const xmlChar*
1299xmlSchemaFormatQNameNs(xmlChar **buf, xmlNsPtr ns, const xmlChar *localName)
1300{
1301 if (ns != NULL)
1302 return (xmlSchemaFormatQName(buf, ns->href, localName));
1303 else
1304 return (xmlSchemaFormatQName(buf, NULL, localName));
1305}
1306
1307static const xmlChar *
1308xmlSchemaGetComponentName(xmlSchemaBasicItemPtr item)
1309{
1310 switch (item->type) {
1311 case XML_SCHEMA_TYPE_ELEMENT:
1312 return (((xmlSchemaElementPtr) item)->name);
1313 case XML_SCHEMA_TYPE_ATTRIBUTE:
1314 return (((xmlSchemaAttributePtr) item)->name);
1315 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1316 return (((xmlSchemaAttributeGroupPtr) item)->name);
1317 case XML_SCHEMA_TYPE_BASIC:
1318 case XML_SCHEMA_TYPE_SIMPLE:
1319 case XML_SCHEMA_TYPE_COMPLEX:
1320 return (((xmlSchemaTypePtr) item)->name);
1321 case XML_SCHEMA_TYPE_GROUP:
1322 return (((xmlSchemaModelGroupDefPtr) item)->name);
1323 case XML_SCHEMA_TYPE_IDC_KEY:
1324 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1325 case XML_SCHEMA_TYPE_IDC_KEYREF:
1326 return (((xmlSchemaIDCPtr) item)->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001327 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1328 if (WXS_ATTRUSE_DECL(item) != NULL) {
1329 return(xmlSchemaGetComponentName(
1330 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1331 } else
1332 return(NULL);
1333 case XML_SCHEMA_EXTRA_QNAMEREF:
1334 return (((xmlSchemaQNameRefPtr) item)->name);
1335 case XML_SCHEMA_TYPE_NOTATION:
1336 return (((xmlSchemaNotationPtr) item)->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001337 default:
1338 /*
1339 * Other components cannot have names.
1340 */
1341 break;
1342 }
1343 return (NULL);
1344}
1345
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001346#define xmlSchemaGetQNameRefName(r) (WXS_QNAME_CAST (r))->name
1347#define xmlSchemaGetQNameRefTargetNs(r) (WXS_QNAME_CAST (r))->targetNamespace
1348/*
1349static const xmlChar *
1350xmlSchemaGetQNameRefName(void *ref)
1351{
1352 return(((xmlSchemaQNameRefPtr) ref)->name);
1353}
1354
1355static const xmlChar *
1356xmlSchemaGetQNameRefTargetNs(void *ref)
1357{
1358 return(((xmlSchemaQNameRefPtr) ref)->targetNamespace);
1359}
1360*/
1361
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001362static const xmlChar *
1363xmlSchemaGetComponentTargetNs(xmlSchemaBasicItemPtr item)
1364{
1365 switch (item->type) {
1366 case XML_SCHEMA_TYPE_ELEMENT:
1367 return (((xmlSchemaElementPtr) item)->targetNamespace);
1368 case XML_SCHEMA_TYPE_ATTRIBUTE:
1369 return (((xmlSchemaAttributePtr) item)->targetNamespace);
1370 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1371 return (((xmlSchemaAttributeGroupPtr) item)->targetNamespace);
1372 case XML_SCHEMA_TYPE_BASIC:
1373 return (BAD_CAST "http://www.w3.org/2001/XMLSchema");
1374 case XML_SCHEMA_TYPE_SIMPLE:
1375 case XML_SCHEMA_TYPE_COMPLEX:
1376 return (((xmlSchemaTypePtr) item)->targetNamespace);
1377 case XML_SCHEMA_TYPE_GROUP:
1378 return (((xmlSchemaModelGroupDefPtr) item)->targetNamespace);
1379 case XML_SCHEMA_TYPE_IDC_KEY:
1380 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1381 case XML_SCHEMA_TYPE_IDC_KEYREF:
1382 return (((xmlSchemaIDCPtr) item)->targetNamespace);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001383 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1384 if (WXS_ATTRUSE_DECL(item) != NULL) {
1385 return(xmlSchemaGetComponentTargetNs(
1386 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1387 }
1388 /* TODO: Will returning NULL break something? */
1389 break;
1390 case XML_SCHEMA_EXTRA_QNAMEREF:
1391 return (((xmlSchemaQNameRefPtr) item)->targetNamespace);
1392 case XML_SCHEMA_TYPE_NOTATION:
1393 return (((xmlSchemaNotationPtr) item)->targetNamespace);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001394 default:
1395 /*
1396 * Other components cannot have names.
1397 */
1398 break;
1399 }
1400 return (NULL);
1401}
1402
1403static const xmlChar*
1404xmlSchemaGetComponentQName(xmlChar **buf,
1405 void *item)
1406{
1407 return (xmlSchemaFormatQName(buf,
1408 xmlSchemaGetComponentTargetNs((xmlSchemaBasicItemPtr) item),
1409 xmlSchemaGetComponentName((xmlSchemaBasicItemPtr) item)));
1410}
1411
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001412static const xmlChar*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001413xmlSchemaGetComponentDesignation(xmlChar **buf, void *item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001414{
1415 xmlChar *str = NULL;
1416
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001417 *buf = xmlStrcat(*buf, WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001418 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001419 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str,
1420 (xmlSchemaBasicItemPtr) item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001421 *buf = xmlStrcat(*buf, BAD_CAST "'");
1422 FREE_AND_NULL(str);
1423 return(*buf);
1424}
1425
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001426static const xmlChar*
1427xmlSchemaGetIDCDesignation(xmlChar **buf, xmlSchemaIDCPtr idc)
1428{
1429 return(xmlSchemaGetComponentDesignation(buf, idc));
1430}
1431
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001432/**
1433 * xmlSchemaWildcardPCToString:
1434 * @pc: the type of processContents
1435 *
1436 * Returns a string representation of the type of
1437 * processContents.
1438 */
1439static const xmlChar *
1440xmlSchemaWildcardPCToString(int pc)
1441{
1442 switch (pc) {
1443 case XML_SCHEMAS_ANY_SKIP:
1444 return (BAD_CAST "skip");
1445 case XML_SCHEMAS_ANY_LAX:
1446 return (BAD_CAST "lax");
1447 case XML_SCHEMAS_ANY_STRICT:
1448 return (BAD_CAST "strict");
1449 default:
1450 return (BAD_CAST "invalid process contents");
1451 }
1452}
1453
1454/**
1455 * xmlSchemaGetCanonValueWhtspExt:
1456 * @val: the precomputed value
1457 * @retValue: the returned value
1458 * @ws: the whitespace type of the value
1459 *
1460 * Get a the cononical representation of the value.
1461 * The caller has to free the returned retValue.
1462 *
1463 * Returns 0 if the value could be built and -1 in case of
1464 * API errors or if the value type is not supported yet.
1465 */
1466static int
1467xmlSchemaGetCanonValueWhtspExt(xmlSchemaValPtr val,
1468 xmlSchemaWhitespaceValueType ws,
1469 xmlChar **retValue)
1470{
1471 int list;
1472 xmlSchemaValType valType;
1473 const xmlChar *value, *value2 = NULL;
1474
1475
1476 if ((retValue == NULL) || (val == NULL))
1477 return (-1);
1478 list = xmlSchemaValueGetNext(val) ? 1 : 0;
1479 *retValue = NULL;
1480 do {
1481 value = NULL;
1482 valType = xmlSchemaGetValType(val);
1483 switch (valType) {
1484 case XML_SCHEMAS_STRING:
1485 case XML_SCHEMAS_NORMSTRING:
1486 case XML_SCHEMAS_ANYSIMPLETYPE:
1487 value = xmlSchemaValueGetAsString(val);
1488 if (value != NULL) {
1489 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
1490 value2 = xmlSchemaCollapseString(value);
1491 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
1492 value2 = xmlSchemaWhiteSpaceReplace(value);
1493 if (value2 != NULL)
1494 value = value2;
1495 }
1496 break;
1497 default:
1498 if (xmlSchemaGetCanonValue(val, &value2) == -1) {
1499 if (value2 != NULL)
1500 xmlFree((xmlChar *) value2);
1501 goto internal_error;
1502 }
1503 value = value2;
1504 }
1505 if (*retValue == NULL)
1506 if (value == NULL) {
1507 if (! list)
1508 *retValue = xmlStrdup(BAD_CAST "");
1509 } else
1510 *retValue = xmlStrdup(value);
1511 else if (value != NULL) {
1512 /* List. */
1513 *retValue = xmlStrcat((xmlChar *) *retValue, BAD_CAST " ");
1514 *retValue = xmlStrcat((xmlChar *) *retValue, value);
1515 }
1516 FREE_AND_NULL(value2)
1517 val = xmlSchemaValueGetNext(val);
1518 } while (val != NULL);
1519
1520 return (0);
1521internal_error:
1522 if (*retValue != NULL)
1523 xmlFree((xmlChar *) (*retValue));
1524 if (value2 != NULL)
1525 xmlFree((xmlChar *) value2);
1526 return (-1);
1527}
1528
1529/**
1530 * xmlSchemaFormatItemForReport:
1531 * @buf: the string buffer
1532 * @itemDes: the designation of the item
1533 * @itemName: the name of the item
1534 * @item: the item as an object
1535 * @itemNode: the node of the item
1536 * @local: the local name
1537 * @parsing: if the function is used during the parse
1538 *
1539 * Returns a representation of the given item used
1540 * for error reports.
1541 *
1542 * The following order is used to build the resulting
1543 * designation if the arguments are not NULL:
1544 * 1a. If itemDes not NULL -> itemDes
1545 * 1b. If (itemDes not NULL) and (itemName not NULL)
1546 * -> itemDes + itemName
1547 * 2. If the preceding was NULL and (item not NULL) -> item
1548 * 3. If the preceding was NULL and (itemNode not NULL) -> itemNode
1549 *
1550 * If the itemNode is an attribute node, the name of the attribute
1551 * will be appended to the result.
1552 *
1553 * Returns the formatted string and sets @buf to the resulting value.
1554 */
1555static xmlChar*
1556xmlSchemaFormatItemForReport(xmlChar **buf,
1557 const xmlChar *itemDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001558 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001559 xmlNodePtr itemNode)
1560{
1561 xmlChar *str = NULL;
1562 int named = 1;
1563
1564 if (*buf != NULL) {
1565 xmlFree(*buf);
1566 *buf = NULL;
1567 }
1568
1569 if (itemDes != NULL) {
1570 *buf = xmlStrdup(itemDes);
1571 } else if (item != NULL) {
1572 switch (item->type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001573 case XML_SCHEMA_TYPE_BASIC: {
1574 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1575
1576 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001577 *buf = xmlStrdup(BAD_CAST "atomic type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001578 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001579 *buf = xmlStrdup(BAD_CAST "list type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001580 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001581 *buf = xmlStrdup(BAD_CAST "union type 'xs:");
1582 else
1583 *buf = xmlStrdup(BAD_CAST "simple type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001584 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001585 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001586 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001587 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001588 case XML_SCHEMA_TYPE_SIMPLE: {
1589 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1590
1591 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001592 *buf = xmlStrdup(BAD_CAST"");
1593 } else {
1594 *buf = xmlStrdup(BAD_CAST "local ");
1595 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001596 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001597 *buf = xmlStrcat(*buf, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001598 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001599 *buf = xmlStrcat(*buf, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001600 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001601 *buf = xmlStrcat(*buf, BAD_CAST "union type");
1602 else
1603 *buf = xmlStrcat(*buf, BAD_CAST "simple type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001604 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001605 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001606 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001607 *buf = xmlStrcat(*buf, BAD_CAST "'");
1608 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001609 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001610 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001611 case XML_SCHEMA_TYPE_COMPLEX: {
1612 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1613
1614 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001615 *buf = xmlStrdup(BAD_CAST "");
1616 else
1617 *buf = xmlStrdup(BAD_CAST "local ");
1618 *buf = xmlStrcat(*buf, BAD_CAST "complex type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001619 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001620 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001621 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001622 *buf = xmlStrcat(*buf, BAD_CAST "'");
1623 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001624 }
1625 break;
1626 case XML_SCHEMA_TYPE_ATTRIBUTE_USE: {
1627 xmlSchemaAttributeUsePtr ause;
1628
1629 ause = WXS_ATTR_USE_CAST item;
1630 *buf = xmlStrdup(BAD_CAST "attribute use ");
1631 if (WXS_ATTRUSE_DECL(ause) != NULL) {
1632 *buf = xmlStrcat(*buf, BAD_CAST "'");
1633 *buf = xmlStrcat(*buf,
1634 xmlSchemaGetComponentQName(&str, WXS_ATTRUSE_DECL(ause)));
1635 FREE_AND_NULL(str)
1636 *buf = xmlStrcat(*buf, BAD_CAST "'");
1637 } else {
1638 *buf = xmlStrcat(*buf, BAD_CAST "(unknown)");
1639 }
1640 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001641 break;
1642 case XML_SCHEMA_TYPE_ATTRIBUTE: {
1643 xmlSchemaAttributePtr attr;
1644
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001645 attr = (xmlSchemaAttributePtr) item;
1646 *buf = xmlStrdup(BAD_CAST "attribute decl.");
1647 *buf = xmlStrcat(*buf, BAD_CAST " '");
1648 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1649 attr->targetNamespace, attr->name));
1650 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001651 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001652 }
1653 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001654 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1655 xmlSchemaGetComponentDesignation(buf, item);
1656 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001657 case XML_SCHEMA_TYPE_ELEMENT: {
1658 xmlSchemaElementPtr elem;
1659
1660 elem = (xmlSchemaElementPtr) item;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001661 *buf = xmlStrdup(BAD_CAST "element decl.");
1662 *buf = xmlStrcat(*buf, BAD_CAST " '");
1663 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1664 elem->targetNamespace, elem->name));
1665 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001666 }
1667 break;
1668 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1669 case XML_SCHEMA_TYPE_IDC_KEY:
1670 case XML_SCHEMA_TYPE_IDC_KEYREF:
1671 if (item->type == XML_SCHEMA_TYPE_IDC_UNIQUE)
1672 *buf = xmlStrdup(BAD_CAST "unique '");
1673 else if (item->type == XML_SCHEMA_TYPE_IDC_KEY)
1674 *buf = xmlStrdup(BAD_CAST "key '");
1675 else
1676 *buf = xmlStrdup(BAD_CAST "keyRef '");
1677 *buf = xmlStrcat(*buf, ((xmlSchemaIDCPtr) item)->name);
1678 *buf = xmlStrcat(*buf, BAD_CAST "'");
1679 break;
1680 case XML_SCHEMA_TYPE_ANY:
1681 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1682 *buf = xmlStrdup(xmlSchemaWildcardPCToString(
1683 ((xmlSchemaWildcardPtr) item)->processContents));
1684 *buf = xmlStrcat(*buf, BAD_CAST " wildcard");
1685 break;
1686 case XML_SCHEMA_FACET_MININCLUSIVE:
1687 case XML_SCHEMA_FACET_MINEXCLUSIVE:
1688 case XML_SCHEMA_FACET_MAXINCLUSIVE:
1689 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
1690 case XML_SCHEMA_FACET_TOTALDIGITS:
1691 case XML_SCHEMA_FACET_FRACTIONDIGITS:
1692 case XML_SCHEMA_FACET_PATTERN:
1693 case XML_SCHEMA_FACET_ENUMERATION:
1694 case XML_SCHEMA_FACET_WHITESPACE:
1695 case XML_SCHEMA_FACET_LENGTH:
1696 case XML_SCHEMA_FACET_MAXLENGTH:
1697 case XML_SCHEMA_FACET_MINLENGTH:
1698 *buf = xmlStrdup(BAD_CAST "facet '");
1699 *buf = xmlStrcat(*buf, xmlSchemaFacetTypeToString(item->type));
1700 *buf = xmlStrcat(*buf, BAD_CAST "'");
1701 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001702 case XML_SCHEMA_TYPE_GROUP: {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001703 *buf = xmlStrdup(BAD_CAST "model group def.");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001704 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001705 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001706 *buf = xmlStrcat(*buf, BAD_CAST "'");
1707 FREE_AND_NULL(str)
1708 }
1709 break;
1710 case XML_SCHEMA_TYPE_SEQUENCE:
1711 case XML_SCHEMA_TYPE_CHOICE:
1712 case XML_SCHEMA_TYPE_ALL:
1713 case XML_SCHEMA_TYPE_PARTICLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001714 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1715 break;
1716 case XML_SCHEMA_TYPE_NOTATION: {
1717 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1718 *buf = xmlStrcat(*buf, BAD_CAST " '");
1719 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
1720 *buf = xmlStrcat(*buf, BAD_CAST "'");
1721 FREE_AND_NULL(str);
1722 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001723 default:
1724 named = 0;
1725 }
1726 } else
1727 named = 0;
1728
1729 if ((named == 0) && (itemNode != NULL)) {
1730 xmlNodePtr elem;
1731
1732 if (itemNode->type == XML_ATTRIBUTE_NODE)
1733 elem = itemNode->parent;
1734 else
1735 elem = itemNode;
1736 *buf = xmlStrdup(BAD_CAST "Element '");
1737 if (elem->ns != NULL) {
1738 *buf = xmlStrcat(*buf,
1739 xmlSchemaFormatQName(&str, elem->ns->href, elem->name));
1740 FREE_AND_NULL(str)
1741 } else
1742 *buf = xmlStrcat(*buf, elem->name);
1743 *buf = xmlStrcat(*buf, BAD_CAST "'");
1744
1745 }
1746 if ((itemNode != NULL) && (itemNode->type == XML_ATTRIBUTE_NODE)) {
1747 *buf = xmlStrcat(*buf, BAD_CAST ", attribute '");
1748 if (itemNode->ns != NULL) {
1749 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1750 itemNode->ns->href, itemNode->name));
1751 FREE_AND_NULL(str)
1752 } else
1753 *buf = xmlStrcat(*buf, itemNode->name);
1754 *buf = xmlStrcat(*buf, BAD_CAST "'");
1755 }
1756 FREE_AND_NULL(str)
1757
1758 return (*buf);
1759}
1760
1761/**
1762 * xmlSchemaFormatFacetEnumSet:
1763 * @buf: the string buffer
1764 * @type: the type holding the enumeration facets
1765 *
1766 * Builds a string consisting of all enumeration elements.
1767 *
1768 * Returns a string of all enumeration elements.
1769 */
1770static const xmlChar *
1771xmlSchemaFormatFacetEnumSet(xmlSchemaAbstractCtxtPtr actxt,
1772 xmlChar **buf, xmlSchemaTypePtr type)
1773{
1774 xmlSchemaFacetPtr facet;
1775 xmlSchemaWhitespaceValueType ws;
1776 xmlChar *value = NULL;
1777 int res;
1778
1779 if (*buf != NULL)
1780 xmlFree(*buf);
1781 *buf = NULL;
1782
1783 do {
1784 /*
1785 * Use the whitespace type of the base type.
1786 */
1787 ws = xmlSchemaGetWhiteSpaceFacetValue(type->baseType);
1788 for (facet = type->facets; facet != NULL; facet = facet->next) {
1789 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
1790 continue;
1791 res = xmlSchemaGetCanonValueWhtspExt(facet->val,
1792 ws, &value);
1793 if (res == -1) {
1794 xmlSchemaInternalErr(actxt,
1795 "xmlSchemaFormatFacetEnumSet",
1796 "compute the canonical lexical representation");
1797 if (*buf != NULL)
1798 xmlFree(*buf);
1799 *buf = NULL;
1800 return (NULL);
1801 }
1802 if (*buf == NULL)
1803 *buf = xmlStrdup(BAD_CAST "'");
1804 else
1805 *buf = xmlStrcat(*buf, BAD_CAST ", '");
1806 *buf = xmlStrcat(*buf, BAD_CAST value);
1807 *buf = xmlStrcat(*buf, BAD_CAST "'");
1808 if (value != NULL) {
1809 xmlFree((xmlChar *)value);
1810 value = NULL;
1811 }
1812 }
1813 type = type->baseType;
1814 } while ((type != NULL) && (type->type != XML_SCHEMA_TYPE_BASIC));
1815
1816 return ((const xmlChar *) *buf);
1817}
1818
1819/************************************************************************
1820 * *
1821 * Error functions *
1822 * *
1823 ************************************************************************/
1824
1825#if 0
1826static void
1827xmlSchemaErrMemory(const char *msg)
1828{
1829 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1830 msg);
1831}
1832#endif
1833
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001834static void
1835xmlSchemaPSimpleErr(const char *msg)
1836{
1837 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1838 msg);
1839}
1840
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001841/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001842 * xmlSchemaPErrMemory:
1843 * @node: a context node
1844 * @extra: extra informations
1845 *
1846 * Handle an out of memory condition
1847 */
1848static void
1849xmlSchemaPErrMemory(xmlSchemaParserCtxtPtr ctxt,
1850 const char *extra, xmlNodePtr node)
1851{
1852 if (ctxt != NULL)
1853 ctxt->nberrors++;
1854 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL,
1855 extra);
1856}
1857
1858/**
1859 * xmlSchemaPErr:
1860 * @ctxt: the parsing context
1861 * @node: the context node
1862 * @error: the error code
1863 * @msg: the error message
1864 * @str1: extra data
1865 * @str2: extra data
1866 *
1867 * Handle a parser error
1868 */
1869static void
1870xmlSchemaPErr(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1871 const char *msg, const xmlChar * str1, const xmlChar * str2)
1872{
1873 xmlGenericErrorFunc channel = NULL;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001874 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001875 void *data = NULL;
1876
1877 if (ctxt != NULL) {
1878 ctxt->nberrors++;
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00001879 ctxt->err = error;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001880 channel = ctxt->error;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00001881 data = ctxt->errCtxt;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001882 schannel = ctxt->serror;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001883 }
Daniel Veillard659e71e2003-10-10 14:10:40 +00001884 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001885 error, XML_ERR_ERROR, NULL, 0,
1886 (const char *) str1, (const char *) str2, NULL, 0, 0,
1887 msg, str1, str2);
1888}
1889
1890/**
1891 * xmlSchemaPErr2:
1892 * @ctxt: the parsing context
1893 * @node: the context node
1894 * @node: the current child
1895 * @error: the error code
1896 * @msg: the error message
1897 * @str1: extra data
1898 * @str2: extra data
1899 *
1900 * Handle a parser error
1901 */
1902static void
1903xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
1904 xmlNodePtr child, int error,
1905 const char *msg, const xmlChar * str1, const xmlChar * str2)
1906{
1907 if (child != NULL)
1908 xmlSchemaPErr(ctxt, child, error, msg, str1, str2);
1909 else
1910 xmlSchemaPErr(ctxt, node, error, msg, str1, str2);
1911}
1912
Daniel Veillard01fa6152004-06-29 17:04:39 +00001913
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001914/**
Daniel Veillard3646d642004-06-02 19:19:14 +00001915 * xmlSchemaPErrExt:
1916 * @ctxt: the parsing context
1917 * @node: the context node
1918 * @error: the error code
1919 * @strData1: extra data
1920 * @strData2: extra data
1921 * @strData3: extra data
1922 * @msg: the message
1923 * @str1: extra parameter for the message display
1924 * @str2: extra parameter for the message display
1925 * @str3: extra parameter for the message display
1926 * @str4: extra parameter for the message display
1927 * @str5: extra parameter for the message display
1928 *
1929 * Handle a parser error
1930 */
1931static void
1932xmlSchemaPErrExt(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1933 const xmlChar * strData1, const xmlChar * strData2,
1934 const xmlChar * strData3, const char *msg, const xmlChar * str1,
1935 const xmlChar * str2, const xmlChar * str3, const xmlChar * str4,
1936 const xmlChar * str5)
1937{
1938
1939 xmlGenericErrorFunc channel = NULL;
1940 xmlStructuredErrorFunc schannel = NULL;
1941 void *data = NULL;
1942
1943 if (ctxt != NULL) {
1944 ctxt->nberrors++;
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00001945 ctxt->err = error;
Daniel Veillard3646d642004-06-02 19:19:14 +00001946 channel = ctxt->error;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00001947 data = ctxt->errCtxt;
Daniel Veillard3646d642004-06-02 19:19:14 +00001948 schannel = ctxt->serror;
1949 }
1950 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
1951 error, XML_ERR_ERROR, NULL, 0,
1952 (const char *) strData1, (const char *) strData2,
William M. Brack803812b2004-06-03 02:11:24 +00001953 (const char *) strData3, 0, 0, msg, str1, str2,
1954 str3, str4, str5);
Daniel Veillard3646d642004-06-02 19:19:14 +00001955}
Daniel Veillard01fa6152004-06-29 17:04:39 +00001956
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001957/************************************************************************
1958 * *
1959 * Allround error functions *
1960 * *
1961 ************************************************************************/
Daniel Veillard3646d642004-06-02 19:19:14 +00001962
1963/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001964 * xmlSchemaVTypeErrMemory:
1965 * @node: a context node
1966 * @extra: extra informations
1967 *
1968 * Handle an out of memory condition
1969 */
1970static void
1971xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt,
1972 const char *extra, xmlNodePtr node)
1973{
1974 if (ctxt != NULL) {
1975 ctxt->nberrors++;
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00001976 ctxt->err = XML_SCHEMAV_INTERNAL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001977 }
1978 __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL,
1979 extra);
1980}
1981
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001982static void
1983xmlSchemaPSimpleInternalErr(xmlNodePtr node,
1984 const char *msg, const xmlChar *str)
1985{
1986 __xmlSimpleError(XML_FROM_SCHEMASP, XML_SCHEMAP_INTERNAL, node,
1987 msg, (const char *) str);
1988}
1989
1990#define WXS_ERROR_TYPE_ERROR 1
1991#define WXS_ERROR_TYPE_WARNING 2
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001992/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001993 * xmlSchemaErr3:
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001994 * @ctxt: the validation context
1995 * @node: the context node
1996 * @error: the error code
1997 * @msg: the error message
1998 * @str1: extra data
1999 * @str2: extra data
2000 * @str3: extra data
2001 *
2002 * Handle a validation error
2003 */
2004static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002005xmlSchemaErr4Line(xmlSchemaAbstractCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002006 xmlErrorLevel errorLevel,
2007 int error, xmlNodePtr node, int line, const char *msg,
2008 const xmlChar *str1, const xmlChar *str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002009 const xmlChar *str3, const xmlChar *str4)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002010{
Daniel Veillard659e71e2003-10-10 14:10:40 +00002011 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002012 xmlGenericErrorFunc channel = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002013 void *data = NULL;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002014
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002015 if (ctxt != NULL) {
2016 if (ctxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2017 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctxt;
Daniel Veillard971771e2005-07-09 17:32:57 +00002018 const char *file = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002019 if (errorLevel != XML_ERR_WARNING) {
2020 vctxt->nberrors++;
2021 vctxt->err = error;
2022 channel = vctxt->error;
2023 } else {
2024 channel = vctxt->warning;
2025 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002026 schannel = vctxt->serror;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00002027 data = vctxt->errCtxt;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002028
2029 /*
2030 * Error node. If we specify a line number, then
2031 * do not channel any node to the error function.
2032 */
2033 if (line == 0) {
2034 if ((node == NULL) &&
2035 (vctxt->depth >= 0) &&
2036 (vctxt->inode != NULL)) {
2037 node = vctxt->inode->node;
2038 }
2039 /*
2040 * Get filename and line if no node-tree.
2041 */
2042 if ((node == NULL) &&
2043 (vctxt->parserCtxt != NULL) &&
2044 (vctxt->parserCtxt->input != NULL)) {
2045 file = vctxt->parserCtxt->input->filename;
2046 line = vctxt->parserCtxt->input->line;
2047 }
2048 } else {
2049 /*
2050 * Override the given node's (if any) position
2051 * and channel only the given line number.
2052 */
2053 node = NULL;
2054 /*
2055 * Get filename.
2056 */
2057 if (vctxt->doc != NULL)
2058 file = (const char *) vctxt->doc->URL;
2059 else if ((vctxt->parserCtxt != NULL) &&
2060 (vctxt->parserCtxt->input != NULL))
2061 file = vctxt->parserCtxt->input->filename;
2062 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002063 __xmlRaiseError(schannel, channel, data, ctxt,
2064 node, XML_FROM_SCHEMASV,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002065 error, errorLevel, file, line,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002066 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002067 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002068
2069 } else if (ctxt->type == XML_SCHEMA_CTXT_PARSER) {
2070 xmlSchemaParserCtxtPtr pctxt = (xmlSchemaParserCtxtPtr) ctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002071 if (errorLevel != XML_ERR_WARNING) {
2072 pctxt->nberrors++;
2073 pctxt->err = error;
2074 channel = pctxt->error;
2075 } else {
2076 channel = pctxt->warning;
2077 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002078 schannel = pctxt->serror;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00002079 data = pctxt->errCtxt;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002080 __xmlRaiseError(schannel, channel, data, ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002081 node, XML_FROM_SCHEMASP, error,
2082 errorLevel, NULL, 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002083 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002084 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002085 } else {
2086 TODO
2087 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002088 }
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002089}
2090
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002091/**
2092 * xmlSchemaErr3:
2093 * @ctxt: the validation context
2094 * @node: the context node
2095 * @error: the error code
2096 * @msg: the error message
2097 * @str1: extra data
2098 * @str2: extra data
2099 * @str3: extra data
2100 *
2101 * Handle a validation error
2102 */
2103static void
2104xmlSchemaErr3(xmlSchemaAbstractCtxtPtr actxt,
2105 int error, xmlNodePtr node, const char *msg,
2106 const xmlChar *str1, const xmlChar *str2, const xmlChar *str3)
2107{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002108 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2109 msg, str1, str2, str3, NULL);
2110}
2111
2112static void
2113xmlSchemaErr4(xmlSchemaAbstractCtxtPtr actxt,
2114 int error, xmlNodePtr node, const char *msg,
2115 const xmlChar *str1, const xmlChar *str2,
2116 const xmlChar *str3, const xmlChar *str4)
2117{
2118 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2119 msg, str1, str2, str3, str4);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002120}
2121
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002122static void
2123xmlSchemaErr(xmlSchemaAbstractCtxtPtr actxt,
2124 int error, xmlNodePtr node, const char *msg,
2125 const xmlChar *str1, const xmlChar *str2)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002126{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002127 xmlSchemaErr4(actxt, error, node, msg, str1, str2, NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002128}
2129
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002130static xmlChar *
2131xmlSchemaFormatNodeForError(xmlChar ** msg,
2132 xmlSchemaAbstractCtxtPtr actxt,
2133 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002134{
2135 xmlChar *str = NULL;
2136
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002137 *msg = NULL;
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00002138 if ((node != NULL) &&
2139 (node->type != XML_ELEMENT_NODE) &&
2140 (node->type != XML_ATTRIBUTE_NODE))
2141 {
2142 /*
2143 * Don't try to format other nodes than element and
2144 * attribute nodes.
2145 * Play save and return an empty string.
2146 */
2147 *msg = xmlStrdup(BAD_CAST "");
2148 return(*msg);
2149 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002150 if (node != NULL) {
2151 /*
2152 * Work on tree nodes.
2153 */
2154 if (node->type == XML_ATTRIBUTE_NODE) {
2155 xmlNodePtr elem = node->parent;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002156
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002157 *msg = xmlStrdup(BAD_CAST "Element '");
2158 if (elem->ns != NULL)
2159 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2160 elem->ns->href, elem->name));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002161 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002162 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2163 NULL, elem->name));
2164 FREE_AND_NULL(str);
2165 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2166 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2167 } else {
2168 *msg = xmlStrdup(BAD_CAST "Element '");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002169 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002170 if (node->ns != NULL)
2171 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2172 node->ns->href, node->name));
Daniel Veillardc0826a72004-08-10 14:17:33 +00002173 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002174 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2175 NULL, node->name));
2176 FREE_AND_NULL(str);
2177 *msg = xmlStrcat(*msg, BAD_CAST "': ");
2178 } else if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2179 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) actxt;
2180 /*
2181 * Work on node infos.
2182 */
2183 if (vctxt->inode->nodeType == XML_ATTRIBUTE_NODE) {
2184 xmlSchemaNodeInfoPtr ielem =
2185 vctxt->elemInfos[vctxt->depth];
2186
2187 *msg = xmlStrdup(BAD_CAST "Element '");
2188 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2189 ielem->nsName, ielem->localName));
2190 FREE_AND_NULL(str);
2191 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2192 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2193 } else {
2194 *msg = xmlStrdup(BAD_CAST "Element '");
2195 }
2196 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2197 vctxt->inode->nsName, vctxt->inode->localName));
2198 FREE_AND_NULL(str);
2199 *msg = xmlStrcat(*msg, BAD_CAST "': ");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002200 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
2201 /*
2202 * Hmm, no node while parsing?
2203 * Return an empty string, in case NULL will break something.
2204 */
2205 *msg = xmlStrdup(BAD_CAST "");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002206 } else {
2207 TODO
2208 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002209 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002210 /*
2211 * VAL TODO: The output of the given schema component is currently
2212 * disabled.
2213 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002214#if 0
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002215 if ((type != NULL) && (xmlSchemaIsGlobalItem(type))) {
2216 *msg = xmlStrcat(*msg, BAD_CAST " [");
2217 *msg = xmlStrcat(*msg, xmlSchemaFormatItemForReport(&str,
2218 NULL, type, NULL, 0));
2219 FREE_AND_NULL(str)
2220 *msg = xmlStrcat(*msg, BAD_CAST "]");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002221 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002222#endif
2223 return (*msg);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002224}
2225
Daniel Veillardc0826a72004-08-10 14:17:33 +00002226static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002227xmlSchemaInternalErr2(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002228 const char *funcName,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002229 const char *message,
2230 const xmlChar *str1,
2231 const xmlChar *str2)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002232{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002233 xmlChar *msg = NULL;
2234
2235 msg = xmlStrdup(BAD_CAST "Internal error: ");
2236 msg = xmlStrcat(msg, BAD_CAST funcName);
2237 msg = xmlStrcat(msg, BAD_CAST ", ");
2238 msg = xmlStrcat(msg, BAD_CAST message);
2239 msg = xmlStrcat(msg, BAD_CAST ".\n");
2240
2241 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR)
2242 xmlSchemaErr(actxt, XML_SCHEMAV_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002243 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002244
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002245 else if (actxt->type == XML_SCHEMA_CTXT_PARSER)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002246 xmlSchemaErr(actxt, XML_SCHEMAP_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002247 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002248
2249 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002250}
2251
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002252static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002253xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
2254 const char *funcName,
2255 const char *message)
2256{
2257 xmlSchemaInternalErr2(actxt, funcName, message, NULL, NULL);
2258}
2259
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002260#if 0
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002261static void
2262xmlSchemaPInternalErr(xmlSchemaParserCtxtPtr pctxt,
2263 const char *funcName,
2264 const char *message,
2265 const xmlChar *str1,
2266 const xmlChar *str2)
2267{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002268 xmlSchemaInternalErr2(ACTXT_CAST pctxt, funcName, message,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002269 str1, str2);
2270}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002271#endif
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002272
2273static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002274xmlSchemaCustomErr4(xmlSchemaAbstractCtxtPtr actxt,
2275 xmlParserErrors error,
2276 xmlNodePtr node,
2277 xmlSchemaBasicItemPtr item,
2278 const char *message,
2279 const xmlChar *str1, const xmlChar *str2,
2280 const xmlChar *str3, const xmlChar *str4)
2281{
2282 xmlChar *msg = NULL;
2283
2284 if ((node == NULL) && (item != NULL) &&
2285 (actxt->type == XML_SCHEMA_CTXT_PARSER)) {
2286 node = WXS_ITEM_NODE(item);
2287 xmlSchemaFormatItemForReport(&msg, NULL, item, NULL);
2288 msg = xmlStrcat(msg, BAD_CAST ": ");
2289 } else
2290 xmlSchemaFormatNodeForError(&msg, actxt, node);
2291 msg = xmlStrcat(msg, (const xmlChar *) message);
2292 msg = xmlStrcat(msg, BAD_CAST ".\n");
2293 xmlSchemaErr4(actxt, error, node,
2294 (const char *) msg, str1, str2, str3, str4);
2295 FREE_AND_NULL(msg)
2296}
2297
2298static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002299xmlSchemaCustomErr(xmlSchemaAbstractCtxtPtr actxt,
2300 xmlParserErrors error,
2301 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002302 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002303 const char *message,
2304 const xmlChar *str1,
2305 const xmlChar *str2)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002306{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002307 xmlSchemaCustomErr4(actxt, error, node, item,
2308 message, str1, str2, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002309}
2310
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002311
2312
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002313static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002314xmlSchemaCustomWarning(xmlSchemaAbstractCtxtPtr actxt,
2315 xmlParserErrors error,
2316 xmlNodePtr node,
2317 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2318 const char *message,
2319 const xmlChar *str1,
2320 const xmlChar *str2,
2321 const xmlChar *str3)
2322{
2323 xmlChar *msg = NULL;
2324
2325 xmlSchemaFormatNodeForError(&msg, actxt, node);
2326 msg = xmlStrcat(msg, (const xmlChar *) message);
2327 msg = xmlStrcat(msg, BAD_CAST ".\n");
2328
2329 /* URGENT TODO: Set the error code to something sane. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002330 xmlSchemaErr4Line(actxt, XML_ERR_WARNING, error, node, 0,
2331 (const char *) msg, str1, str2, str3, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002332
2333 FREE_AND_NULL(msg)
2334}
2335
2336
2337
2338static void
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002339xmlSchemaKeyrefErr(xmlSchemaValidCtxtPtr vctxt,
2340 xmlParserErrors error,
2341 xmlSchemaPSVIIDCNodePtr idcNode,
2342 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2343 const char *message,
2344 const xmlChar *str1,
2345 const xmlChar *str2)
2346{
2347 xmlChar *msg = NULL, *qname = NULL;
2348
2349 msg = xmlStrdup(BAD_CAST "Element '%s': ");
2350 msg = xmlStrcat(msg, (const xmlChar *) message);
2351 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002352 xmlSchemaErr4Line(ACTXT_CAST vctxt, XML_ERR_ERROR,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002353 error, NULL, idcNode->nodeLine, (const char *) msg,
2354 xmlSchemaFormatQName(&qname,
2355 vctxt->nodeQNames->items[idcNode->nodeQNameID +1],
2356 vctxt->nodeQNames->items[idcNode->nodeQNameID]),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002357 str1, str2, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002358 FREE_AND_NULL(qname);
2359 FREE_AND_NULL(msg);
2360}
2361
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002362static int
2363xmlSchemaEvalErrorNodeType(xmlSchemaAbstractCtxtPtr actxt,
2364 xmlNodePtr node)
2365{
2366 if (node != NULL)
2367 return (node->type);
2368 if ((actxt->type == XML_SCHEMA_CTXT_VALIDATOR) &&
2369 (((xmlSchemaValidCtxtPtr) actxt)->inode != NULL))
2370 return ( ((xmlSchemaValidCtxtPtr) actxt)->inode->nodeType);
2371 return (-1);
2372}
2373
2374static int
2375xmlSchemaIsGlobalItem(xmlSchemaTypePtr item)
2376{
2377 switch (item->type) {
2378 case XML_SCHEMA_TYPE_COMPLEX:
2379 case XML_SCHEMA_TYPE_SIMPLE:
2380 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL)
2381 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002382 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002383 case XML_SCHEMA_TYPE_GROUP:
2384 return (1);
2385 case XML_SCHEMA_TYPE_ELEMENT:
2386 if ( ((xmlSchemaElementPtr) item)->flags &
2387 XML_SCHEMAS_ELEM_GLOBAL)
2388 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002389 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002390 case XML_SCHEMA_TYPE_ATTRIBUTE:
2391 if ( ((xmlSchemaAttributePtr) item)->flags &
2392 XML_SCHEMAS_ATTR_GLOBAL)
2393 return(1);
2394 break;
2395 /* Note that attribute groups are always global. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002396 default:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002397 return(1);
2398 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002399 return (0);
2400}
2401
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002402static void
2403xmlSchemaSimpleTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2404 xmlParserErrors error,
2405 xmlNodePtr node,
2406 const xmlChar *value,
2407 xmlSchemaTypePtr type,
2408 int displayValue)
2409{
2410 xmlChar *msg = NULL;
2411
2412 xmlSchemaFormatNodeForError(&msg, actxt, node);
2413
2414 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2415 XML_ATTRIBUTE_NODE))
2416 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
2417 else
2418 msg = xmlStrcat(msg, BAD_CAST "The character content is not a valid "
2419 "value of ");
2420
2421 if (! xmlSchemaIsGlobalItem(type))
2422 msg = xmlStrcat(msg, BAD_CAST "the local ");
2423 else
2424 msg = xmlStrcat(msg, BAD_CAST "the ");
2425
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002426 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002427 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002428 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002429 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002430 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002431 msg = xmlStrcat(msg, BAD_CAST "union type");
2432
2433 if (xmlSchemaIsGlobalItem(type)) {
2434 xmlChar *str = NULL;
2435 msg = xmlStrcat(msg, BAD_CAST " '");
2436 if (type->builtInType != 0) {
2437 msg = xmlStrcat(msg, BAD_CAST "xs:");
2438 msg = xmlStrcat(msg, type->name);
2439 } else
2440 msg = xmlStrcat(msg,
2441 xmlSchemaFormatQName(&str,
2442 type->targetNamespace, type->name));
2443 msg = xmlStrcat(msg, BAD_CAST "'");
2444 FREE_AND_NULL(str);
2445 }
2446 msg = xmlStrcat(msg, BAD_CAST ".\n");
2447 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2448 XML_ATTRIBUTE_NODE))
2449 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2450 else
2451 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2452 FREE_AND_NULL(msg)
2453}
2454
Daniel Veillardc0826a72004-08-10 14:17:33 +00002455static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002456xmlSchemaFormatErrorNodeQName(xmlChar ** str,
2457 xmlSchemaNodeInfoPtr ni,
2458 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002459{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002460 if (node != NULL) {
2461 if (node->ns != NULL)
2462 return (xmlSchemaFormatQName(str, node->ns->href, node->name));
2463 else
2464 return (xmlSchemaFormatQName(str, NULL, node->name));
2465 } else if (ni != NULL)
2466 return (xmlSchemaFormatQName(str, ni->nsName, ni->localName));
2467 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002468}
2469
Daniel Veillardc0826a72004-08-10 14:17:33 +00002470static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002471xmlSchemaIllegalAttrErr(xmlSchemaAbstractCtxtPtr actxt,
2472 xmlParserErrors error,
2473 xmlSchemaAttrInfoPtr ni,
2474 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002475{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002476 xmlChar *msg = NULL, *str = NULL;
2477
2478 xmlSchemaFormatNodeForError(&msg, actxt, node);
2479 msg = xmlStrcat(msg, BAD_CAST "The attribute '%s' is not allowed.\n");
2480 xmlSchemaErr(actxt, error, node, (const char *) msg,
2481 xmlSchemaFormatErrorNodeQName(&str, (xmlSchemaNodeInfoPtr) ni, node),
2482 NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002483 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002484 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002485}
2486
Daniel Veillardc0826a72004-08-10 14:17:33 +00002487static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002488xmlSchemaComplexTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2489 xmlParserErrors error,
2490 xmlNodePtr node,
2491 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002492 const char *message,
2493 int nbval,
2494 int nbneg,
2495 xmlChar **values)
2496{
2497 xmlChar *str = NULL, *msg = NULL;
2498 xmlChar *localName, *nsName;
2499 const xmlChar *cur, *end;
Daniel Veillard77005e62005-07-19 16:26:18 +00002500 int i, is_not;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002501
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002502 xmlSchemaFormatNodeForError(&msg, actxt, node);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002503 msg = xmlStrcat(msg, (const xmlChar *) message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002504 msg = xmlStrcat(msg, BAD_CAST ".");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002505 /*
2506 * Note that is does not make sense to report that we have a
2507 * wildcard here, since the wildcard might be unfolded into
2508 * multiple transitions.
2509 */
2510 if (nbval + nbneg > 0) {
2511 if (nbval + nbneg > 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002512 str = xmlStrdup(BAD_CAST " Expected is one of ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002513 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002514 str = xmlStrdup(BAD_CAST " Expected is ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002515 nsName = NULL;
2516
2517 for (i = 0; i < nbval + nbneg; i++) {
2518 cur = values[i];
Daniel Veillard77005e62005-07-19 16:26:18 +00002519 if (cur == NULL)
2520 continue;
2521 if ((cur[0] == 'n') && (cur[1] == 'o') && (cur[2] == 't') &&
2522 (cur[3] == ' ')) {
2523 is_not = 1;
2524 cur += 4;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002525 str = xmlStrcat(str, BAD_CAST "##other");
Daniel Veillard77005e62005-07-19 16:26:18 +00002526 } else {
2527 is_not = 0;
2528 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002529 /*
2530 * Get the local name.
2531 */
2532 localName = NULL;
2533
2534 end = cur;
2535 if (*end == '*') {
2536 localName = xmlStrdup(BAD_CAST "*");
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002537 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002538 } else {
2539 while ((*end != 0) && (*end != '|'))
2540 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002541 localName = xmlStrncat(localName, BAD_CAST cur, end - cur);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002542 }
2543 if (*end != 0) {
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002544 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002545 /*
2546 * Skip "*|*" if they come with negated expressions, since
2547 * they represent the same negated wildcard.
2548 */
2549 if ((nbneg == 0) || (*end != '*') || (*localName != '*')) {
2550 /*
2551 * Get the namespace name.
2552 */
2553 cur = end;
2554 if (*end == '*') {
2555 nsName = xmlStrdup(BAD_CAST "{*}");
2556 } else {
2557 while (*end != 0)
2558 end++;
2559
2560 if (i >= nbval)
2561 nsName = xmlStrdup(BAD_CAST "{##other:");
2562 else
2563 nsName = xmlStrdup(BAD_CAST "{");
2564
2565 nsName = xmlStrncat(nsName, BAD_CAST cur, end - cur);
2566 nsName = xmlStrcat(nsName, BAD_CAST "}");
2567 }
2568 str = xmlStrcat(str, BAD_CAST nsName);
2569 FREE_AND_NULL(nsName)
2570 } else {
2571 FREE_AND_NULL(localName);
2572 continue;
2573 }
2574 }
2575 str = xmlStrcat(str, BAD_CAST localName);
2576 FREE_AND_NULL(localName);
2577
2578 if (i < nbval + nbneg -1)
2579 str = xmlStrcat(str, BAD_CAST ", ");
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00002580 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002581 str = xmlStrcat(str, BAD_CAST " ).\n");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002582 msg = xmlStrcat(msg, BAD_CAST str);
2583 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002584 } else
2585 msg = xmlStrcat(msg, BAD_CAST "\n");
2586 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002587 xmlFree(msg);
2588}
2589
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002590static void
2591xmlSchemaFacetErr(xmlSchemaAbstractCtxtPtr actxt,
2592 xmlParserErrors error,
2593 xmlNodePtr node,
2594 const xmlChar *value,
2595 unsigned long length,
2596 xmlSchemaTypePtr type,
2597 xmlSchemaFacetPtr facet,
2598 const char *message,
2599 const xmlChar *str1,
2600 const xmlChar *str2)
2601{
2602 xmlChar *str = NULL, *msg = NULL;
2603 xmlSchemaTypeType facetType;
2604 int nodeType = xmlSchemaEvalErrorNodeType(actxt, node);
2605
2606 xmlSchemaFormatNodeForError(&msg, actxt, node);
2607 if (error == XML_SCHEMAV_CVC_ENUMERATION_VALID) {
2608 facetType = XML_SCHEMA_FACET_ENUMERATION;
2609 /*
2610 * If enumerations are validated, one must not expect the
2611 * facet to be given.
2612 */
2613 } else
2614 facetType = facet->type;
2615 msg = xmlStrcat(msg, BAD_CAST "[");
2616 msg = xmlStrcat(msg, BAD_CAST "facet '");
2617 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facetType));
2618 msg = xmlStrcat(msg, BAD_CAST "'] ");
2619 if (message == NULL) {
2620 /*
2621 * Use a default message.
2622 */
2623 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
2624 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
2625 (facetType == XML_SCHEMA_FACET_MAXLENGTH)) {
2626
2627 char len[25], actLen[25];
2628
2629 /* FIXME, TODO: What is the max expected string length of the
2630 * this value?
2631 */
2632 if (nodeType == XML_ATTRIBUTE_NODE)
2633 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has a length of '%s'; ");
2634 else
2635 msg = xmlStrcat(msg, BAD_CAST "The value has a length of '%s'; ");
2636
2637 snprintf(len, 24, "%lu", xmlSchemaGetFacetValueAsULong(facet));
2638 snprintf(actLen, 24, "%lu", length);
2639
2640 if (facetType == XML_SCHEMA_FACET_LENGTH)
2641 msg = xmlStrcat(msg,
2642 BAD_CAST "this differs from the allowed length of '%s'.\n");
2643 else if (facetType == XML_SCHEMA_FACET_MAXLENGTH)
2644 msg = xmlStrcat(msg,
2645 BAD_CAST "this exceeds the allowed maximum length of '%s'.\n");
2646 else if (facetType == XML_SCHEMA_FACET_MINLENGTH)
2647 msg = xmlStrcat(msg,
2648 BAD_CAST "this underruns the allowed minimum length of '%s'.\n");
2649
2650 if (nodeType == XML_ATTRIBUTE_NODE)
2651 xmlSchemaErr3(actxt, error, node, (const char *) msg,
2652 value, (const xmlChar *) actLen, (const xmlChar *) len);
2653 else
2654 xmlSchemaErr(actxt, error, node, (const char *) msg,
2655 (const xmlChar *) actLen, (const xmlChar *) len);
2656
2657 } else if (facetType == XML_SCHEMA_FACET_ENUMERATION) {
2658 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not an element "
2659 "of the set {%s}.\n");
2660 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2661 xmlSchemaFormatFacetEnumSet(actxt, &str, type));
2662 } else if (facetType == XML_SCHEMA_FACET_PATTERN) {
2663 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not accepted "
2664 "by the pattern '%s'.\n");
2665 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2666 facet->value);
2667 } else if (facetType == XML_SCHEMA_FACET_MININCLUSIVE) {
2668 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is less than the "
2669 "minimum value allowed ('%s').\n");
2670 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2671 facet->value);
2672 } else if (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) {
2673 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is greater than the "
2674 "maximum value allowed ('%s').\n");
2675 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2676 facet->value);
2677 } else if (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00002678 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be greater than "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002679 "'%s'.\n");
2680 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2681 facet->value);
2682 } else if (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00002683 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be less than "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002684 "'%s'.\n");
2685 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2686 facet->value);
2687 } else if (facetType == XML_SCHEMA_FACET_TOTALDIGITS) {
2688 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more "
2689 "digits than are allowed ('%s').\n");
2690 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2691 facet->value);
2692 } else if (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) {
2693 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more fractional "
2694 "digits than are allowed ('%s').\n");
2695 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2696 facet->value);
2697 } else if (nodeType == XML_ATTRIBUTE_NODE) {
2698 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not facet-valid.\n");
2699 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2700 } else {
2701 msg = xmlStrcat(msg, BAD_CAST "The value is not facet-valid.\n");
2702 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2703 }
2704 } else {
2705 msg = xmlStrcat(msg, (const xmlChar *) message);
2706 msg = xmlStrcat(msg, BAD_CAST ".\n");
2707 xmlSchemaErr(actxt, error, node, (const char *) msg, str1, str2);
2708 }
2709 FREE_AND_NULL(str)
2710 xmlFree(msg);
2711}
2712
2713#define VERROR(err, type, msg) \
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002714 xmlSchemaCustomErr(ACTXT_CAST vctxt, err, NULL, type, msg, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002715
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002716#define VERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST vctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002717
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002718#define PERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST pctxt, func, msg);
2719#define PERROR_INT2(func, msg) xmlSchemaInternalErr(ACTXT_CAST ctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002720
2721#define AERROR_INT(func, msg) xmlSchemaInternalErr(actxt, func, msg);
2722
2723
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002724/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00002725 * xmlSchemaPMissingAttrErr:
2726 * @ctxt: the schema validation context
2727 * @ownerDes: the designation of the owner
2728 * @ownerName: the name of the owner
2729 * @ownerItem: the owner as a schema object
2730 * @ownerElem: the owner as an element node
2731 * @node: the parent element node of the missing attribute node
2732 * @type: the corresponding type of the attribute node
2733 *
2734 * Reports an illegal attribute.
2735 */
2736static void
2737xmlSchemaPMissingAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002738 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002739 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002740 xmlNodePtr ownerElem,
2741 const char *name,
2742 const char *message)
2743{
2744 xmlChar *des = NULL;
2745
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002746 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
2747
Daniel Veillardc0826a72004-08-10 14:17:33 +00002748 if (message != NULL)
2749 xmlSchemaPErr(ctxt, ownerElem, error, "%s: %s.\n", BAD_CAST des, BAD_CAST message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002750 else
2751 xmlSchemaPErr(ctxt, ownerElem, error,
2752 "%s: The attribute '%s' is required but missing.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002753 BAD_CAST des, BAD_CAST name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002754 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002755}
2756
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002757
Daniel Veillardc0826a72004-08-10 14:17:33 +00002758/**
2759 * xmlSchemaPResCompAttrErr:
2760 * @ctxt: the schema validation context
2761 * @error: the error code
2762 * @ownerDes: the designation of the owner
2763 * @ownerItem: the owner as a schema object
2764 * @ownerElem: the owner as an element node
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002765 * @name: the name of the attribute holding the QName
Daniel Veillardc0826a72004-08-10 14:17:33 +00002766 * @refName: the referenced local name
2767 * @refURI: the referenced namespace URI
2768 * @message: optional message
2769 *
2770 * Used to report QName attribute values that failed to resolve
2771 * to schema components.
2772 */
2773static void
2774xmlSchemaPResCompAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002775 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002776 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002777 xmlNodePtr ownerElem,
2778 const char *name,
2779 const xmlChar *refName,
2780 const xmlChar *refURI,
2781 xmlSchemaTypeType refType,
2782 const char *refTypeStr)
2783{
2784 xmlChar *des = NULL, *strA = NULL;
2785
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002786 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002787 if (refTypeStr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002788 refTypeStr = (const char *) xmlSchemaItemTypeToStr(refType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002789 xmlSchemaPErrExt(ctxt, ownerElem, error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002790 NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002791 "%s, attribute '%s': The QName value '%s' does not resolve to a(n) "
2792 "%s.\n", BAD_CAST des, BAD_CAST name,
2793 xmlSchemaFormatQName(&strA, refURI, refName),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002794 BAD_CAST refTypeStr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002795 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002796 FREE_AND_NULL(strA)
2797}
2798
William M. Brack2f2a6632004-08-20 23:09:47 +00002799/**
2800 * xmlSchemaPCustomAttrErr:
2801 * @ctxt: the schema parser context
2802 * @error: the error code
2803 * @ownerDes: the designation of the owner
2804 * @ownerItem: the owner as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002805 * @attr: the illegal attribute node
William M. Brack2f2a6632004-08-20 23:09:47 +00002806 *
2807 * Reports an illegal attribute during the parse.
2808 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002809static void
2810xmlSchemaPCustomAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002811 xmlParserErrors error,
William M. Brack2f2a6632004-08-20 23:09:47 +00002812 xmlChar **ownerDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002813 xmlSchemaBasicItemPtr ownerItem,
William M. Brack2f2a6632004-08-20 23:09:47 +00002814 xmlAttrPtr attr,
2815 const char *msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002816{
2817 xmlChar *des = NULL;
2818
2819 if (ownerDes == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002820 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002821 else if (*ownerDes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002822 xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002823 des = *ownerDes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002824 } else
2825 des = *ownerDes;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002826 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002827 "%s, attribute '%s': %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002828 BAD_CAST des, attr->name, (const xmlChar *) msg, NULL, NULL);
2829 if (ownerDes == NULL)
2830 FREE_AND_NULL(des);
2831}
2832
2833/**
2834 * xmlSchemaPIllegalAttrErr:
William M. Brack2f2a6632004-08-20 23:09:47 +00002835 * @ctxt: the schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00002836 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00002837 * @ownerDes: the designation of the attribute's owner
2838 * @ownerItem: the attribute's owner item
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002839 * @attr: the illegal attribute node
Daniel Veillardc0826a72004-08-10 14:17:33 +00002840 *
William M. Brack2f2a6632004-08-20 23:09:47 +00002841 * Reports an illegal attribute during the parse.
Daniel Veillardc0826a72004-08-10 14:17:33 +00002842 */
2843static void
2844xmlSchemaPIllegalAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002845 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002846 xmlSchemaBasicItemPtr ownerComp ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002847 xmlAttrPtr attr)
2848{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002849 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002850
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002851 xmlSchemaFormatNodeForError(&strA, ACTXT_CAST ctxt, attr->parent);
2852 xmlSchemaErr4(ACTXT_CAST ctxt, error, (xmlNodePtr) attr,
2853 "%sThe attribute '%s' is not allowed.\n", BAD_CAST strA,
2854 xmlSchemaFormatQNameNs(&strB, attr->ns, attr->name),
2855 NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002856 FREE_AND_NULL(strA);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002857 FREE_AND_NULL(strB);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002858}
2859
William M. Brack2f2a6632004-08-20 23:09:47 +00002860/**
2861 * xmlSchemaPCustomErr:
2862 * @ctxt: the schema parser context
2863 * @error: the error code
2864 * @itemDes: the designation of the schema item
2865 * @item: the schema item
2866 * @itemElem: the node of the schema item
2867 * @message: the error message
2868 * @str1: an optional param for the error message
2869 * @str2: an optional param for the error message
2870 * @str3: an optional param for the error message
2871 *
2872 * Reports an error during parsing.
2873 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002874static void
2875xmlSchemaPCustomErrExt(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002876 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002877 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002878 xmlNodePtr itemElem,
2879 const char *message,
2880 const xmlChar *str1,
2881 const xmlChar *str2,
2882 const xmlChar *str3)
2883{
2884 xmlChar *des = NULL, *msg = NULL;
2885
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002886 xmlSchemaFormatItemForReport(&des, NULL, item, itemElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002887 msg = xmlStrdup(BAD_CAST "%s: ");
2888 msg = xmlStrcat(msg, (const xmlChar *) message);
2889 msg = xmlStrcat(msg, BAD_CAST ".\n");
2890 if ((itemElem == NULL) && (item != NULL))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002891 itemElem = WXS_ITEM_NODE(item);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002892 xmlSchemaPErrExt(ctxt, itemElem, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002893 (const char *) msg, BAD_CAST des, str1, str2, str3, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002894 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002895 FREE_AND_NULL(msg);
2896}
2897
William M. Brack2f2a6632004-08-20 23:09:47 +00002898/**
2899 * xmlSchemaPCustomErr:
2900 * @ctxt: the schema parser context
2901 * @error: the error code
2902 * @itemDes: the designation of the schema item
2903 * @item: the schema item
2904 * @itemElem: the node of the schema item
2905 * @message: the error message
2906 * @str1: the optional param for the error message
2907 *
2908 * Reports an error during parsing.
2909 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002910static void
2911xmlSchemaPCustomErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002912 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002913 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002914 xmlNodePtr itemElem,
2915 const char *message,
2916 const xmlChar *str1)
2917{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002918 xmlSchemaPCustomErrExt(ctxt, error, item, itemElem, message,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002919 str1, NULL, NULL);
2920}
2921
William M. Brack2f2a6632004-08-20 23:09:47 +00002922/**
2923 * xmlSchemaPAttrUseErr:
2924 * @ctxt: the schema parser context
2925 * @error: the error code
2926 * @itemDes: the designation of the schema type
2927 * @item: the schema type
2928 * @itemElem: the node of the schema type
2929 * @attr: the invalid schema attribute
2930 * @message: the error message
2931 * @str1: the optional param for the error message
2932 *
2933 * Reports an attribute use error during parsing.
2934 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002935static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002936xmlSchemaPAttrUseErr4(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002937 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002938 xmlNodePtr node,
2939 xmlSchemaBasicItemPtr ownerItem,
2940 const xmlSchemaAttributeUsePtr attruse,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002941 const char *message,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002942 const xmlChar *str1, const xmlChar *str2,
2943 const xmlChar *str3,const xmlChar *str4)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002944{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002945 xmlChar *str = NULL, *msg = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002946
2947 xmlSchemaFormatItemForReport(&msg, NULL, ownerItem, NULL);
2948 msg = xmlStrcat(msg, BAD_CAST ", ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002949 msg = xmlStrcat(msg,
2950 BAD_CAST xmlSchemaFormatItemForReport(&str, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002951 WXS_BASIC_CAST attruse, NULL));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002952 FREE_AND_NULL(str);
2953 msg = xmlStrcat(msg, BAD_CAST ": ");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002954 msg = xmlStrcat(msg, (const xmlChar *) message);
2955 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002956 xmlSchemaErr4(ACTXT_CAST ctxt, error, node,
2957 (const char *) msg, str1, str2, str3, str4);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002958 xmlFree(msg);
2959}
2960
William M. Brack2f2a6632004-08-20 23:09:47 +00002961/**
2962 * xmlSchemaPIllegalFacetAtomicErr:
2963 * @ctxt: the schema parser context
2964 * @error: the error code
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002965 * @type: the schema type
2966 * @baseType: the base type of type
William M. Brack2f2a6632004-08-20 23:09:47 +00002967 * @facet: the illegal facet
2968 *
2969 * Reports an illegal facet for atomic simple types.
2970 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002971static void
2972xmlSchemaPIllegalFacetAtomicErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002973 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002974 xmlSchemaTypePtr type,
2975 xmlSchemaTypePtr baseType,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002976 xmlSchemaFacetPtr facet)
2977{
2978 xmlChar *des = NULL, *strT = NULL;
2979
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002980 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type, type->node);
2981 xmlSchemaPErrExt(ctxt, type->node, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002982 "%s: The facet '%s' is not allowed on types derived from the "
2983 "type %s.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00002984 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002985 xmlSchemaFormatItemForReport(&strT, NULL, WXS_BASIC_CAST baseType, NULL),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002986 NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002987 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002988 FREE_AND_NULL(strT);
2989}
2990
William M. Brack2f2a6632004-08-20 23:09:47 +00002991/**
2992 * xmlSchemaPIllegalFacetListUnionErr:
2993 * @ctxt: the schema parser context
2994 * @error: the error code
2995 * @itemDes: the designation of the schema item involved
2996 * @item: the schema item involved
2997 * @facet: the illegal facet
2998 *
2999 * Reports an illegal facet for <list> and <union>.
3000 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003001static void
3002xmlSchemaPIllegalFacetListUnionErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003003 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003004 xmlSchemaTypePtr type,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003005 xmlSchemaFacetPtr facet)
3006{
3007 xmlChar *des = NULL, *strT = NULL;
3008
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003009 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type,
3010 type->node);
3011 xmlSchemaPErr(ctxt, type->node, error,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003012 "%s: The facet '%s' is not allowed.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00003013 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003014 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003015 FREE_AND_NULL(strT);
3016}
3017
3018/**
3019 * xmlSchemaPMutualExclAttrErr:
3020 * @ctxt: the schema validation context
3021 * @error: the error code
3022 * @elemDes: the designation of the parent element node
3023 * @attr: the bad attribute node
3024 * @type: the corresponding type of the attribute node
3025 *
3026 * Reports an illegal attribute.
3027 */
3028static void
3029xmlSchemaPMutualExclAttrErr(xmlSchemaParserCtxtPtr ctxt,
3030 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003031 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003032 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003033 const char *name1,
3034 const char *name2)
3035{
3036 xmlChar *des = NULL;
3037
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003038 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003039 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003040 "%s: The attributes '%s' and '%s' are mutually exclusive.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003041 BAD_CAST des, BAD_CAST name1, BAD_CAST name2, NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003042 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003043}
3044
3045/**
3046 * xmlSchemaPSimpleTypeErr:
3047 * @ctxt: the schema validation context
3048 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00003049 * @type: the type specifier
Daniel Veillardc0826a72004-08-10 14:17:33 +00003050 * @ownerDes: the designation of the owner
3051 * @ownerItem: the schema object if existent
3052 * @node: the validated node
3053 * @value: the validated value
3054 *
3055 * Reports a simple type validation error.
3056 * TODO: Should this report the value of an element as well?
3057 */
3058static void
3059xmlSchemaPSimpleTypeErr(xmlSchemaParserCtxtPtr ctxt,
3060 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003061 xmlSchemaBasicItemPtr ownerItem ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003062 xmlNodePtr node,
William M. Brack2f2a6632004-08-20 23:09:47 +00003063 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003064 const char *expected,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003065 const xmlChar *value,
3066 const char *message,
3067 const xmlChar *str1,
3068 const xmlChar *str2)
3069{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003070 xmlChar *msg = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003071
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003072 xmlSchemaFormatNodeForError(&msg, ACTXT_CAST ctxt, node);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003073 if (message == NULL) {
3074 /*
3075 * Use default messages.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003076 */
3077 if (type != NULL) {
3078 if (node->type == XML_ATTRIBUTE_NODE)
3079 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
3080 else
3081 msg = xmlStrcat(msg, BAD_CAST "The character content is not a "
3082 "valid value of ");
3083 if (! xmlSchemaIsGlobalItem(type))
3084 msg = xmlStrcat(msg, BAD_CAST "the local ");
3085 else
3086 msg = xmlStrcat(msg, BAD_CAST "the ");
3087
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003088 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003089 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003090 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003091 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003092 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003093 msg = xmlStrcat(msg, BAD_CAST "union type");
3094
3095 if (xmlSchemaIsGlobalItem(type)) {
3096 xmlChar *str = NULL;
3097 msg = xmlStrcat(msg, BAD_CAST " '");
3098 if (type->builtInType != 0) {
3099 msg = xmlStrcat(msg, BAD_CAST "xs:");
3100 msg = xmlStrcat(msg, type->name);
3101 } else
3102 msg = xmlStrcat(msg,
3103 xmlSchemaFormatQName(&str,
3104 type->targetNamespace, type->name));
3105 msg = xmlStrcat(msg, BAD_CAST "'.");
3106 FREE_AND_NULL(str);
3107 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003108 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003109 if (node->type == XML_ATTRIBUTE_NODE)
3110 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not valid.");
3111 else
3112 msg = xmlStrcat(msg, BAD_CAST "The character content is not "
3113 "valid.");
3114 }
3115 if (expected) {
3116 msg = xmlStrcat(msg, BAD_CAST " Expected is '");
3117 msg = xmlStrcat(msg, BAD_CAST expected);
3118 msg = xmlStrcat(msg, BAD_CAST "'.\n");
3119 } else
3120 msg = xmlStrcat(msg, BAD_CAST "\n");
Daniel Veillardc0826a72004-08-10 14:17:33 +00003121 if (node->type == XML_ATTRIBUTE_NODE)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003122 xmlSchemaPErr(ctxt, node, error, (const char *) msg, value, NULL);
3123 else
3124 xmlSchemaPErr(ctxt, node, error, (const char *) msg, NULL, NULL);
3125 } else {
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003126 msg = xmlStrcat(msg, BAD_CAST message);
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00003127 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003128 xmlSchemaPErrExt(ctxt, node, error, NULL, NULL, NULL,
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003129 (const char*) msg, str1, str2, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003130 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003131 /* Cleanup. */
3132 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003133}
3134
William M. Brack2f2a6632004-08-20 23:09:47 +00003135/**
3136 * xmlSchemaPContentErr:
3137 * @ctxt: the schema parser context
3138 * @error: the error code
3139 * @onwerDes: the designation of the holder of the content
3140 * @ownerItem: the owner item of the holder of the content
3141 * @ownerElem: the node of the holder of the content
3142 * @child: the invalid child node
3143 * @message: the optional error message
3144 * @content: the optional string describing the correct content
3145 *
3146 * Reports an error concerning the content of a schema element.
3147 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003148static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003149xmlSchemaPContentErr(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003150 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003151 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003152 xmlNodePtr ownerElem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003153 xmlNodePtr child,
3154 const char *message,
3155 const char *content)
3156{
3157 xmlChar *des = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003158
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003159 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003160 if (message != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003161 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3162 "%s: %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003163 BAD_CAST des, BAD_CAST message);
3164 else {
3165 if (content != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003166 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3167 "%s: The content is not valid. Expected is %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003168 BAD_CAST des, BAD_CAST content);
3169 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003170 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3171 "%s: The content is not valid.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003172 BAD_CAST des, NULL);
3173 }
3174 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003175 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003176}
3177
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003178/************************************************************************
3179 * *
3180 * Streamable error functions *
3181 * *
3182 ************************************************************************/
Kasimier T. Buchcik8b418172004-11-17 13:14:27 +00003183
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003184
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003185
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003186
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003187/************************************************************************
3188 * *
3189 * Validation helper functions *
3190 * *
3191 ************************************************************************/
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003192
Daniel Veillardc0826a72004-08-10 14:17:33 +00003193
Daniel Veillard4255d502002-04-16 15:50:10 +00003194/************************************************************************
3195 * *
3196 * Allocation functions *
3197 * *
3198 ************************************************************************/
3199
3200/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003201 * xmlSchemaNewSchemaForParserCtxt:
William M. Brack08171912003-12-29 02:52:11 +00003202 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003203 *
3204 * Allocate a new Schema structure.
3205 *
3206 * Returns the newly allocated structure or NULL in case or error
3207 */
3208static xmlSchemaPtr
3209xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt)
3210{
3211 xmlSchemaPtr ret;
3212
3213 ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema));
3214 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003215 xmlSchemaPErrMemory(ctxt, "allocating schema", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003216 return (NULL);
3217 }
3218 memset(ret, 0, sizeof(xmlSchema));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003219 ret->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +00003220 xmlDictReference(ret->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00003221
3222 return (ret);
3223}
3224
3225/**
3226 * xmlSchemaNewFacet:
Daniel Veillard4255d502002-04-16 15:50:10 +00003227 *
3228 * Allocate a new Facet structure.
3229 *
3230 * Returns the newly allocated structure or NULL in case or error
3231 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003232xmlSchemaFacetPtr
3233xmlSchemaNewFacet(void)
Daniel Veillard4255d502002-04-16 15:50:10 +00003234{
3235 xmlSchemaFacetPtr ret;
3236
3237 ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet));
3238 if (ret == NULL) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003239 return (NULL);
3240 }
3241 memset(ret, 0, sizeof(xmlSchemaFacet));
3242
3243 return (ret);
3244}
3245
3246/**
3247 * xmlSchemaNewAnnot:
William M. Brack08171912003-12-29 02:52:11 +00003248 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003249 * @node: a node
3250 *
3251 * Allocate a new annotation structure.
3252 *
3253 * Returns the newly allocated structure or NULL in case or error
3254 */
3255static xmlSchemaAnnotPtr
3256xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
3257{
3258 xmlSchemaAnnotPtr ret;
3259
3260 ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot));
3261 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003262 xmlSchemaPErrMemory(ctxt, "allocating annotation", node);
Daniel Veillard4255d502002-04-16 15:50:10 +00003263 return (NULL);
3264 }
3265 memset(ret, 0, sizeof(xmlSchemaAnnot));
3266 ret->content = node;
3267 return (ret);
3268}
3269
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003270static xmlSchemaItemListPtr
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003271xmlSchemaItemListCreate(void)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003272{
3273 xmlSchemaItemListPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003274
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003275 ret = xmlMalloc(sizeof(xmlSchemaItemList));
3276 if (ret == NULL) {
3277 xmlSchemaPErrMemory(NULL,
3278 "allocating an item list structure", NULL);
3279 return (NULL);
3280 }
3281 memset(ret, 0, sizeof(xmlSchemaItemList));
3282 return (ret);
3283}
3284
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003285static void
3286xmlSchemaItemListClear(xmlSchemaItemListPtr list)
3287{
3288 if (list->items != NULL) {
3289 xmlFree(list->items);
3290 list->items = NULL;
3291 }
3292 list->nbItems = 0;
3293 list->sizeItems = 0;
3294}
3295
3296static int
3297xmlSchemaItemListAdd(xmlSchemaItemListPtr list, void *item)
3298{
3299 if (list->items == NULL) {
3300 list->items = (void **) xmlMalloc(
3301 20 * sizeof(void *));
3302 if (list->items == NULL) {
3303 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3304 return(-1);
3305 }
3306 list->sizeItems = 20;
3307 } else if (list->sizeItems <= list->nbItems) {
3308 list->sizeItems *= 2;
3309 list->items = (void **) xmlRealloc(list->items,
3310 list->sizeItems * sizeof(void *));
3311 if (list->items == NULL) {
3312 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3313 list->sizeItems = 0;
3314 return(-1);
3315 }
3316 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003317 list->items[list->nbItems++] = item;
3318 return(0);
3319}
3320
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003321static int
3322xmlSchemaItemListAddSize(xmlSchemaItemListPtr list,
3323 int initialSize,
3324 void *item)
3325{
3326 if (list->items == NULL) {
3327 if (initialSize <= 0)
3328 initialSize = 1;
3329 list->items = (void **) xmlMalloc(
3330 initialSize * sizeof(void *));
3331 if (list->items == NULL) {
3332 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3333 return(-1);
3334 }
3335 list->sizeItems = initialSize;
3336 } else if (list->sizeItems <= list->nbItems) {
3337 list->sizeItems *= 2;
3338 list->items = (void **) xmlRealloc(list->items,
3339 list->sizeItems * sizeof(void *));
3340 if (list->items == NULL) {
3341 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3342 list->sizeItems = 0;
3343 return(-1);
3344 }
3345 }
3346 list->items[list->nbItems++] = item;
3347 return(0);
3348}
3349
3350static int
3351xmlSchemaItemListInsert(xmlSchemaItemListPtr list, void *item, int idx)
3352{
3353 if (list->items == NULL) {
3354 list->items = (void **) xmlMalloc(
3355 20 * sizeof(void *));
3356 if (list->items == NULL) {
3357 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3358 return(-1);
3359 }
3360 list->sizeItems = 20;
3361 } else if (list->sizeItems <= list->nbItems) {
3362 list->sizeItems *= 2;
3363 list->items = (void **) xmlRealloc(list->items,
3364 list->sizeItems * sizeof(void *));
3365 if (list->items == NULL) {
3366 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3367 list->sizeItems = 0;
3368 return(-1);
3369 }
3370 }
3371 /*
3372 * Just append if the index is greater/equal than the item count.
3373 */
3374 if (idx >= list->nbItems) {
3375 list->items[list->nbItems++] = item;
3376 } else {
3377 int i;
3378 for (i = list->nbItems; i > idx; i--)
3379 list->items[i] = list->items[i-1];
3380 list->items[idx] = item;
3381 list->nbItems++;
3382 }
3383 return(0);
3384}
3385
3386#if 0 /* enable if ever needed */
3387static int
3388xmlSchemaItemListInsertSize(xmlSchemaItemListPtr list,
3389 int initialSize,
3390 void *item,
3391 int idx)
3392{
3393 if (list->items == NULL) {
3394 if (initialSize <= 0)
3395 initialSize = 1;
3396 list->items = (void **) xmlMalloc(
3397 initialSize * sizeof(void *));
3398 if (list->items == NULL) {
3399 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3400 return(-1);
3401 }
3402 list->sizeItems = initialSize;
3403 } else if (list->sizeItems <= list->nbItems) {
3404 list->sizeItems *= 2;
3405 list->items = (void **) xmlRealloc(list->items,
3406 list->sizeItems * sizeof(void *));
3407 if (list->items == NULL) {
3408 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3409 list->sizeItems = 0;
3410 return(-1);
3411 }
3412 }
3413 /*
3414 * Just append if the index is greater/equal than the item count.
3415 */
3416 if (idx >= list->nbItems) {
3417 list->items[list->nbItems++] = item;
3418 } else {
3419 int i;
3420 for (i = list->nbItems; i > idx; i--)
3421 list->items[i] = list->items[i-1];
3422 list->items[idx] = item;
3423 list->nbItems++;
3424 }
3425 return(0);
3426}
3427#endif
3428
3429static int
3430xmlSchemaItemListRemove(xmlSchemaItemListPtr list, int idx)
3431{
3432 int i;
3433 if ((list->items == NULL) || (idx >= list->nbItems)) {
3434 xmlSchemaPSimpleErr("Internal error: xmlSchemaItemListRemove, "
3435 "index error.\n");
3436 return(-1);
3437 }
3438
3439 if (list->nbItems == 1) {
3440 /* TODO: Really free the list? */
3441 xmlFree(list->items);
3442 list->items = NULL;
3443 list->nbItems = 0;
3444 list->sizeItems = 0;
3445 } else if (list->nbItems -1 == idx) {
3446 list->nbItems--;
3447 } else {
3448 for (i = idx; i < list->nbItems -1; i++)
3449 list->items[i] = list->items[i+1];
3450 list->nbItems--;
3451 }
3452 return(0);
3453}
3454
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003455/**
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003456 * xmlSchemaItemListFree:
3457 * @annot: a schema type structure
3458 *
3459 * Deallocate a annotation structure
3460 */
3461static void
3462xmlSchemaItemListFree(xmlSchemaItemListPtr list)
3463{
3464 if (list == NULL)
3465 return;
3466 if (list->items != NULL)
3467 xmlFree(list->items);
3468 xmlFree(list);
3469}
3470
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003471static void
3472xmlSchemaBucketFree(xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003473{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003474 if (bucket == NULL)
3475 return;
3476 if (bucket->globals != NULL) {
3477 xmlSchemaComponentListFree(bucket->globals);
3478 xmlSchemaItemListFree(bucket->globals);
3479 }
3480 if (bucket->locals != NULL) {
3481 xmlSchemaComponentListFree(bucket->locals);
3482 xmlSchemaItemListFree(bucket->locals);
3483 }
3484 if (bucket->relations != NULL) {
3485 xmlSchemaSchemaRelationPtr prev, cur = bucket->relations;
3486 do {
3487 prev = cur;
3488 cur = cur->next;
3489 xmlFree(prev);
3490 } while (cur != NULL);
3491 }
3492 if ((! bucket->preserveDoc) && (bucket->doc != NULL)) {
3493 xmlFreeDoc(bucket->doc);
3494 }
3495 if (bucket->type == XML_SCHEMA_SCHEMA_IMPORT) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003496 if (WXS_IMPBUCKET(bucket)->schema != NULL)
3497 xmlSchemaFree(WXS_IMPBUCKET(bucket)->schema);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003498 }
3499 xmlFree(bucket);
3500}
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003501
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003502static xmlSchemaBucketPtr
3503xmlSchemaBucketCreate(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003504 int type, const xmlChar *targetNamespace)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003505{
3506 xmlSchemaBucketPtr ret;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003507 int size;
3508 xmlSchemaPtr mainSchema;
3509
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003510 if (WXS_CONSTRUCTOR(pctxt)->mainSchema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003511 PERROR_INT("xmlSchemaBucketCreate",
3512 "no main schema on constructor");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003513 return(NULL);
3514 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003515 mainSchema = WXS_CONSTRUCTOR(pctxt)->mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003516 /* Create the schema bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003517 if (WXS_IS_BUCKET_INCREDEF(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003518 size = sizeof(xmlSchemaInclude);
3519 else
3520 size = sizeof(xmlSchemaImport);
3521 ret = (xmlSchemaBucketPtr) xmlMalloc(size);
3522 if (ret == NULL) {
3523 xmlSchemaPErrMemory(NULL, "allocating schema bucket", NULL);
3524 return(NULL);
3525 }
3526 memset(ret, 0, size);
3527 ret->targetNamespace = targetNamespace;
3528 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003529 ret->globals = xmlSchemaItemListCreate();
3530 if (ret->globals == NULL) {
3531 xmlFree(ret);
3532 return(NULL);
3533 }
3534 ret->locals = xmlSchemaItemListCreate();
3535 if (ret->locals == NULL) {
3536 xmlFree(ret);
3537 return(NULL);
3538 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003539 /*
3540 * The following will assure that only the first bucket is marked as
3541 * XML_SCHEMA_SCHEMA_MAIN and it points to the *main* schema.
3542 * For each following import buckets an xmlSchema will be created.
3543 */
3544 if (! WXS_HAS_BUCKETS(pctxt)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003545 if (WXS_IS_BUCKET_INCREDEF(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003546 PERROR_INT("xmlSchemaBucketCreate",
3547 "first bucket but it's an include or redefine");
3548 xmlSchemaBucketFree(ret);
3549 return(NULL);
3550 }
3551 /* Force the type to be XML_SCHEMA_SCHEMA_MAIN. */
3552 ret->type = XML_SCHEMA_SCHEMA_MAIN;
3553 /* Point to the *main* schema. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003554 WXS_CONSTRUCTOR(pctxt)->mainBucket = ret;
3555 WXS_IMPBUCKET(ret)->schema = mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003556 } else {
3557 if (type == XML_SCHEMA_SCHEMA_MAIN) {
3558 PERROR_INT("xmlSchemaBucketCreate",
3559 "main bucket but it's not the first one");
3560 xmlSchemaBucketFree(ret);
3561 return(NULL);
3562 } else if (type == XML_SCHEMA_SCHEMA_IMPORT) {
3563 /*
3564 * Create a schema for imports.
3565 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003566 WXS_IMPBUCKET(ret)->schema = xmlSchemaNewSchema(pctxt);
3567 if (WXS_IMPBUCKET(ret)->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003568 xmlSchemaBucketFree(ret);
3569 return(NULL);
3570 }
3571 }
3572 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003573 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003574 int res;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003575 /* Imports go into the "schemasImports" slot of the main *schema*. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003576 if (mainSchema->schemasImports == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003577 mainSchema->schemasImports = xmlHashCreateDict(5,
3578 WXS_CONSTRUCTOR(pctxt)->dict);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003579 if (mainSchema->schemasImports == NULL) {
3580 xmlSchemaBucketFree(ret);
3581 return(NULL);
3582 }
3583 }
3584 if (targetNamespace == NULL)
3585 res = xmlHashAddEntry(mainSchema->schemasImports,
3586 XML_SCHEMAS_NO_NAMESPACE, ret);
3587 else
3588 res = xmlHashAddEntry(mainSchema->schemasImports,
3589 targetNamespace, ret);
3590 if (res != 0) {
3591 PERROR_INT("xmlSchemaBucketCreate",
3592 "failed to add the schema bucket to the hash");
3593 xmlSchemaBucketFree(ret);
3594 return(NULL);
3595 }
3596 } else {
3597 /* Set the @ownerImport of an include bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003598 if (WXS_IS_BUCKET_IMPMAIN(WXS_CONSTRUCTOR(pctxt)->bucket->type))
3599 WXS_INCBUCKET(ret)->ownerImport =
3600 WXS_IMPBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003601 else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003602 WXS_INCBUCKET(ret)->ownerImport =
3603 WXS_INCBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket)->ownerImport;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003604
3605 /* Includes got into the "includes" slot of the *main* schema. */
3606 if (mainSchema->includes == NULL) {
3607 mainSchema->includes = xmlSchemaItemListCreate();
3608 if (mainSchema->includes == NULL) {
3609 xmlSchemaBucketFree(ret);
3610 return(NULL);
3611 }
3612 }
3613 xmlSchemaItemListAdd(mainSchema->includes, ret);
3614 }
3615 /*
3616 * Add to list of all buckets; this is used for lookup
3617 * during schema construction time only.
3618 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003619 if (xmlSchemaItemListAdd(WXS_CONSTRUCTOR(pctxt)->buckets, ret) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003620 return(NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003621 return(ret);
3622}
3623
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003624static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003625xmlSchemaAddItemSize(xmlSchemaItemListPtr *list, int initialSize, void *item)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003626{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003627 if (*list == NULL) {
3628 *list = xmlSchemaItemListCreate();
3629 if (*list == NULL)
3630 return(-1);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003631 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003632 xmlSchemaItemListAddSize(*list, initialSize, item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003633 return(0);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003634}
3635
3636/**
Daniel Veillardfdc91562002-07-01 21:52:03 +00003637 * xmlSchemaFreeAnnot:
3638 * @annot: a schema type structure
3639 *
3640 * Deallocate a annotation structure
3641 */
3642static void
3643xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot)
3644{
3645 if (annot == NULL)
3646 return;
Kasimier T. Buchcik004b5462005-08-08 12:43:09 +00003647 if (annot->next == NULL) {
3648 xmlFree(annot);
3649 } else {
3650 xmlSchemaAnnotPtr prev;
3651
3652 do {
3653 prev = annot;
3654 annot = annot->next;
3655 xmlFree(prev);
3656 } while (annot != NULL);
3657 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00003658}
3659
3660/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003661 * xmlSchemaFreeNotation:
3662 * @schema: a schema notation structure
3663 *
3664 * Deallocate a Schema Notation structure.
3665 */
3666static void
3667xmlSchemaFreeNotation(xmlSchemaNotationPtr nota)
3668{
3669 if (nota == NULL)
3670 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003671 xmlFree(nota);
3672}
3673
3674/**
3675 * xmlSchemaFreeAttribute:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003676 * @attr: an attribute declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00003677 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003678 * Deallocates an attribute declaration structure.
Daniel Veillard4255d502002-04-16 15:50:10 +00003679 */
3680static void
3681xmlSchemaFreeAttribute(xmlSchemaAttributePtr attr)
3682{
3683 if (attr == NULL)
3684 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003685 if (attr->annot != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003686 xmlSchemaFreeAnnot(attr->annot);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003687 if (attr->defVal != NULL)
3688 xmlSchemaFreeValue(attr->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003689 xmlFree(attr);
3690}
3691
3692/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003693 * xmlSchemaFreeAttributeUse:
3694 * @use: an attribute use
3695 *
3696 * Deallocates an attribute use structure.
3697 */
3698static void
3699xmlSchemaFreeAttributeUse(xmlSchemaAttributeUsePtr use)
3700{
3701 if (use == NULL)
3702 return;
3703 if (use->annot != NULL)
3704 xmlSchemaFreeAnnot(use->annot);
3705 if (use->defVal != NULL)
3706 xmlSchemaFreeValue(use->defVal);
3707 xmlFree(use);
3708}
3709
3710/**
3711 * xmlSchemaFreeAttributeUseProhib:
3712 * @prohib: an attribute use prohibition
3713 *
3714 * Deallocates an attribute use structure.
3715 */
3716static void
3717xmlSchemaFreeAttributeUseProhib(xmlSchemaAttributeUseProhibPtr prohib)
3718{
3719 if (prohib == NULL)
3720 return;
3721 xmlFree(prohib);
3722}
3723
3724/**
Daniel Veillard3646d642004-06-02 19:19:14 +00003725 * xmlSchemaFreeWildcardNsSet:
3726 * set: a schema wildcard namespace
3727 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003728 * Deallocates a list of wildcard constraint structures.
Daniel Veillard3646d642004-06-02 19:19:14 +00003729 */
3730static void
3731xmlSchemaFreeWildcardNsSet(xmlSchemaWildcardNsPtr set)
3732{
3733 xmlSchemaWildcardNsPtr next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003734
Daniel Veillard3646d642004-06-02 19:19:14 +00003735 while (set != NULL) {
3736 next = set->next;
3737 xmlFree(set);
3738 set = next;
3739 }
3740}
3741
3742/**
3743 * xmlSchemaFreeWildcard:
Daniel Veillard01fa6152004-06-29 17:04:39 +00003744 * @wildcard: a wildcard structure
Daniel Veillard3646d642004-06-02 19:19:14 +00003745 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003746 * Deallocates a wildcard structure.
Daniel Veillard3646d642004-06-02 19:19:14 +00003747 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00003748void
Daniel Veillard3646d642004-06-02 19:19:14 +00003749xmlSchemaFreeWildcard(xmlSchemaWildcardPtr wildcard)
3750{
3751 if (wildcard == NULL)
3752 return;
3753 if (wildcard->annot != NULL)
3754 xmlSchemaFreeAnnot(wildcard->annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003755 if (wildcard->nsSet != NULL)
3756 xmlSchemaFreeWildcardNsSet(wildcard->nsSet);
3757 if (wildcard->negNsSet != NULL)
3758 xmlFree(wildcard->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +00003759 xmlFree(wildcard);
3760}
3761
3762/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003763 * xmlSchemaFreeAttributeGroup:
3764 * @schema: a schema attribute group structure
3765 *
3766 * Deallocate a Schema Attribute Group structure.
3767 */
3768static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003769xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillard4255d502002-04-16 15:50:10 +00003770{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003771 if (attrGr == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00003772 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003773 if (attrGr->annot != NULL)
3774 xmlSchemaFreeAnnot(attrGr->annot);
3775 if (attrGr->attrUses != NULL)
3776 xmlSchemaItemListFree(WXS_LIST_CAST attrGr->attrUses);
3777 xmlFree(attrGr);
Daniel Veillard3646d642004-06-02 19:19:14 +00003778}
3779
3780/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003781 * xmlSchemaFreeQNameRef:
3782 * @item: a QName reference structure
3783 *
3784 * Deallocatea a QName reference structure.
3785 */
3786static void
3787xmlSchemaFreeQNameRef(xmlSchemaQNameRefPtr item)
3788{
3789 xmlFree(item);
3790}
3791
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003792/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00003793 * xmlSchemaFreeTypeLinkList:
3794 * @alink: a type link
3795 *
3796 * Deallocate a list of types.
3797 */
3798static void
3799xmlSchemaFreeTypeLinkList(xmlSchemaTypeLinkPtr link)
3800{
3801 xmlSchemaTypeLinkPtr next;
3802
3803 while (link != NULL) {
3804 next = link->next;
3805 xmlFree(link);
3806 link = next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003807 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003808}
3809
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003810static void
3811xmlSchemaFreeIDCStateObjList(xmlSchemaIDCStateObjPtr sto)
3812{
3813 xmlSchemaIDCStateObjPtr next;
3814 while (sto != NULL) {
3815 next = sto->next;
3816 if (sto->history != NULL)
3817 xmlFree(sto->history);
3818 if (sto->xpathCtxt != NULL)
3819 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
3820 xmlFree(sto);
3821 sto = next;
3822 }
3823}
3824
3825/**
3826 * xmlSchemaFreeIDC:
3827 * @idc: a identity-constraint definition
3828 *
3829 * Deallocates an identity-constraint definition.
3830 */
3831static void
3832xmlSchemaFreeIDC(xmlSchemaIDCPtr idcDef)
3833{
3834 xmlSchemaIDCSelectPtr cur, prev;
3835
3836 if (idcDef == NULL)
3837 return;
3838 if (idcDef->annot != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003839 xmlSchemaFreeAnnot(idcDef->annot);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003840 /* Selector */
3841 if (idcDef->selector != NULL) {
3842 if (idcDef->selector->xpathComp != NULL)
3843 xmlFreePattern((xmlPatternPtr) idcDef->selector->xpathComp);
3844 xmlFree(idcDef->selector);
3845 }
3846 /* Fields */
3847 if (idcDef->fields != NULL) {
3848 cur = idcDef->fields;
3849 do {
3850 prev = cur;
3851 cur = cur->next;
3852 if (prev->xpathComp != NULL)
3853 xmlFreePattern((xmlPatternPtr) prev->xpathComp);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003854 xmlFree(prev);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003855 } while (cur != NULL);
3856 }
3857 xmlFree(idcDef);
3858}
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003859
Daniel Veillard01fa6152004-06-29 17:04:39 +00003860/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003861 * xmlSchemaFreeElement:
3862 * @schema: a schema element structure
3863 *
3864 * Deallocate a Schema Element structure.
3865 */
3866static void
3867xmlSchemaFreeElement(xmlSchemaElementPtr elem)
3868{
3869 if (elem == NULL)
3870 return;
Daniel Veillard32370232002-10-16 14:08:14 +00003871 if (elem->annot != NULL)
3872 xmlSchemaFreeAnnot(elem->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003873 if (elem->contModel != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003874 xmlRegFreeRegexp(elem->contModel);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003875 if (elem->defVal != NULL)
3876 xmlSchemaFreeValue(elem->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003877 xmlFree(elem);
3878}
3879
3880/**
3881 * xmlSchemaFreeFacet:
3882 * @facet: a schema facet structure
3883 *
3884 * Deallocate a Schema Facet structure.
3885 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003886void
Daniel Veillard4255d502002-04-16 15:50:10 +00003887xmlSchemaFreeFacet(xmlSchemaFacetPtr facet)
3888{
3889 if (facet == NULL)
3890 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003891 if (facet->val != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003892 xmlSchemaFreeValue(facet->val);
Daniel Veillard4255d502002-04-16 15:50:10 +00003893 if (facet->regexp != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003894 xmlRegFreeRegexp(facet->regexp);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003895 if (facet->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003896 xmlSchemaFreeAnnot(facet->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003897 xmlFree(facet);
3898}
3899
3900/**
3901 * xmlSchemaFreeType:
3902 * @type: a schema type structure
3903 *
3904 * Deallocate a Schema Type structure.
3905 */
3906void
3907xmlSchemaFreeType(xmlSchemaTypePtr type)
3908{
3909 if (type == NULL)
3910 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003911 if (type->annot != NULL)
Daniel Veillard32370232002-10-16 14:08:14 +00003912 xmlSchemaFreeAnnot(type->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003913 if (type->facets != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003914 xmlSchemaFacetPtr facet, next;
Daniel Veillard4255d502002-04-16 15:50:10 +00003915
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003916 facet = type->facets;
3917 while (facet != NULL) {
3918 next = facet->next;
3919 xmlSchemaFreeFacet(facet);
3920 facet = next;
3921 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003922 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003923 if (type->attrUses != NULL)
3924 xmlSchemaItemListFree((xmlSchemaItemListPtr) type->attrUses);
Daniel Veillard01fa6152004-06-29 17:04:39 +00003925 if (type->memberTypes != NULL)
3926 xmlSchemaFreeTypeLinkList(type->memberTypes);
3927 if (type->facetSet != NULL) {
3928 xmlSchemaFacetLinkPtr next, link;
3929
3930 link = type->facetSet;
3931 do {
3932 next = link->next;
3933 xmlFree(link);
3934 link = next;
3935 } while (link != NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003936 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00003937 if (type->contModel != NULL)
3938 xmlRegFreeRegexp(type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +00003939 xmlFree(type);
3940}
3941
3942/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003943 * xmlSchemaFreeModelGroupDef:
3944 * @item: a schema model group definition
3945 *
3946 * Deallocates a schema model group definition.
3947 */
3948static void
3949xmlSchemaFreeModelGroupDef(xmlSchemaModelGroupDefPtr item)
3950{
3951 if (item->annot != NULL)
3952 xmlSchemaFreeAnnot(item->annot);
3953 xmlFree(item);
3954}
3955
3956/**
3957 * xmlSchemaFreeModelGroup:
3958 * @item: a schema model group
3959 *
3960 * Deallocates a schema model group structure.
3961 */
3962static void
3963xmlSchemaFreeModelGroup(xmlSchemaModelGroupPtr item)
3964{
3965 if (item->annot != NULL)
3966 xmlSchemaFreeAnnot(item->annot);
3967 xmlFree(item);
3968}
3969
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003970static void
3971xmlSchemaComponentListFree(xmlSchemaItemListPtr list)
3972{
3973 if ((list == NULL) || (list->nbItems == 0))
3974 return;
3975 {
3976 xmlSchemaTreeItemPtr item;
3977 xmlSchemaTreeItemPtr *items = (xmlSchemaTreeItemPtr *) list->items;
3978 int i;
3979
3980 for (i = 0; i < list->nbItems; i++) {
3981 item = items[i];
3982 if (item == NULL)
3983 continue;
3984 switch (item->type) {
3985 case XML_SCHEMA_TYPE_SIMPLE:
3986 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003987 xmlSchemaFreeType((xmlSchemaTypePtr) item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003988 break;
3989 case XML_SCHEMA_TYPE_ATTRIBUTE:
3990 xmlSchemaFreeAttribute((xmlSchemaAttributePtr) item);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003991 break;
3992 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
3993 xmlSchemaFreeAttributeUse((xmlSchemaAttributeUsePtr) item);
3994 break;
3995 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
3996 xmlSchemaFreeAttributeUseProhib(
3997 (xmlSchemaAttributeUseProhibPtr) item);
3998 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003999 case XML_SCHEMA_TYPE_ELEMENT:
4000 xmlSchemaFreeElement((xmlSchemaElementPtr) item);
4001 break;
4002 case XML_SCHEMA_TYPE_PARTICLE:
4003 if (item->annot != NULL)
4004 xmlSchemaFreeAnnot(item->annot);
4005 xmlFree(item);
4006 break;
4007 case XML_SCHEMA_TYPE_SEQUENCE:
4008 case XML_SCHEMA_TYPE_CHOICE:
4009 case XML_SCHEMA_TYPE_ALL:
4010 xmlSchemaFreeModelGroup((xmlSchemaModelGroupPtr) item);
4011 break;
4012 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
4013 xmlSchemaFreeAttributeGroup(
4014 (xmlSchemaAttributeGroupPtr) item);
4015 break;
4016 case XML_SCHEMA_TYPE_GROUP:
4017 xmlSchemaFreeModelGroupDef(
4018 (xmlSchemaModelGroupDefPtr) item);
4019 break;
4020 case XML_SCHEMA_TYPE_ANY:
4021 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
4022 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr) item);
4023 break;
4024 case XML_SCHEMA_TYPE_IDC_KEY:
4025 case XML_SCHEMA_TYPE_IDC_UNIQUE:
4026 case XML_SCHEMA_TYPE_IDC_KEYREF:
4027 xmlSchemaFreeIDC((xmlSchemaIDCPtr) item);
4028 break;
4029 case XML_SCHEMA_TYPE_NOTATION:
4030 xmlSchemaFreeNotation((xmlSchemaNotationPtr) item);
4031 break;
4032 case XML_SCHEMA_EXTRA_QNAMEREF:
4033 xmlSchemaFreeQNameRef((xmlSchemaQNameRefPtr) item);
4034 break;
4035 default: {
4036 /* TODO: This should never be hit. */
4037 xmlSchemaPSimpleInternalErr(NULL,
4038 "Internal error: xmlSchemaComponentListFree, "
4039 "unexpected component type '%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004040 (const xmlChar *) WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004041 }
4042 break;
4043 }
4044 }
4045 list->nbItems = 0;
4046 }
4047}
4048
Daniel Veillardb0f397e2003-12-23 23:30:53 +00004049/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004050 * xmlSchemaFree:
4051 * @schema: a schema structure
4052 *
4053 * Deallocate a Schema structure.
4054 */
4055void
4056xmlSchemaFree(xmlSchemaPtr schema)
4057{
4058 if (schema == NULL)
4059 return;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004060 /* @volatiles is not used anymore :-/ */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004061 if (schema->volatiles != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004062 TODO
4063 /*
4064 * Note that those slots are not responsible for freeing
4065 * schema components anymore; this will now be done by
4066 * the schema buckets.
4067 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004068 if (schema->notaDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004069 xmlHashFree(schema->notaDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004070 if (schema->attrDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004071 xmlHashFree(schema->attrDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004072 if (schema->attrgrpDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004073 xmlHashFree(schema->attrgrpDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004074 if (schema->elemDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004075 xmlHashFree(schema->elemDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004076 if (schema->typeDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004077 xmlHashFree(schema->typeDecl, NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004078 if (schema->groupDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004079 xmlHashFree(schema->groupDecl, NULL);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004080 if (schema->idcDef != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004081 xmlHashFree(schema->idcDef, NULL);
4082
Daniel Veillard1d913862003-11-21 00:28:39 +00004083 if (schema->schemasImports != NULL)
4084 xmlHashFree(schema->schemasImports,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004085 (xmlHashDeallocator) xmlSchemaBucketFree);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004086 if (schema->includes != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004087 xmlSchemaItemListPtr list = (xmlSchemaItemListPtr) schema->includes;
4088 int i;
4089 for (i = 0; i < list->nbItems; i++) {
4090 xmlSchemaBucketFree((xmlSchemaBucketPtr) list->items[i]);
4091 }
4092 xmlSchemaItemListFree(list);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004093 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004094 if (schema->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004095 xmlSchemaFreeAnnot(schema->annot);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004096 /* Never free the doc here, since this will be done by the buckets. */
4097
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004098 xmlDictFree(schema->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00004099 xmlFree(schema);
4100}
4101
4102/************************************************************************
4103 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00004104 * Debug functions *
4105 * *
4106 ************************************************************************/
4107
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004108#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004109
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004110static void
4111xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output); /* forward */
4112
Daniel Veillard4255d502002-04-16 15:50:10 +00004113/**
4114 * xmlSchemaElementDump:
4115 * @elem: an element
4116 * @output: the file output
4117 *
4118 * Dump the element
4119 */
4120static void
4121xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004122 const xmlChar * name ATTRIBUTE_UNUSED,
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004123 const xmlChar * namespace ATTRIBUTE_UNUSED,
4124 const xmlChar * context ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00004125{
4126 if (elem == NULL)
4127 return;
4128
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004129
4130 fprintf(output, "Element");
4131 if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL)
4132 fprintf(output, " (global)");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004133 fprintf(output, ": '%s' ", elem->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004134 if (namespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004135 fprintf(output, "ns '%s'", namespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004136 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004137#if 0
Daniel Veillard4255d502002-04-16 15:50:10 +00004138 if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004139 fprintf(output, " min %d ", elem->minOccurs);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004140 if (elem->maxOccurs >= UNBOUNDED)
4141 fprintf(output, "max: unbounded\n");
4142 else if (elem->maxOccurs != 1)
4143 fprintf(output, "max: %d\n", elem->maxOccurs);
4144 else
4145 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004146 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004147#endif
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004148 /*
4149 * Misc other properties.
4150 */
4151 if ((elem->flags & XML_SCHEMAS_ELEM_NILLABLE) ||
4152 (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) ||
4153 (elem->flags & XML_SCHEMAS_ELEM_FIXED) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004154 (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004155 fprintf(output, " props: ");
4156 if (elem->flags & XML_SCHEMAS_ELEM_FIXED)
4157 fprintf(output, "[fixed] ");
4158 if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)
4159 fprintf(output, "[default] ");
4160 if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT)
4161 fprintf(output, "[abstract] ");
4162 if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE)
4163 fprintf(output, "[nillable] ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004164 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004165 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004166 /*
4167 * Default/fixed value.
4168 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004169 if (elem->value != NULL)
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004170 fprintf(output, " value: '%s'\n", elem->value);
4171 /*
4172 * Type.
4173 */
4174 if (elem->namedType != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004175 fprintf(output, " type: '%s' ", elem->namedType);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004176 if (elem->namedTypeNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004177 fprintf(output, "ns '%s'\n", elem->namedTypeNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004178 else
4179 fprintf(output, "\n");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004180 } else if (elem->subtypes != NULL) {
4181 /*
4182 * Dump local types.
4183 */
4184 xmlSchemaTypeDump(elem->subtypes, output);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004185 }
4186 /*
4187 * Substitution group.
4188 */
4189 if (elem->substGroup != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004190 fprintf(output, " substitutionGroup: '%s' ", elem->substGroup);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004191 if (elem->substGroupNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004192 fprintf(output, "ns '%s'\n", elem->substGroupNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004193 else
4194 fprintf(output, "\n");
4195 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004196}
4197
4198/**
4199 * xmlSchemaAnnotDump:
4200 * @output: the file output
4201 * @annot: a annotation
4202 *
4203 * Dump the annotation
4204 */
4205static void
4206xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot)
4207{
4208 xmlChar *content;
4209
4210 if (annot == NULL)
4211 return;
4212
4213 content = xmlNodeGetContent(annot->content);
4214 if (content != NULL) {
4215 fprintf(output, " Annot: %s\n", content);
4216 xmlFree(content);
4217 } else
4218 fprintf(output, " Annot: empty\n");
4219}
4220
4221/**
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004222 * xmlSchemaContentModelDump:
4223 * @particle: the schema particle
4224 * @output: the file output
4225 * @depth: the depth used for intentation
Daniel Veillard4255d502002-04-16 15:50:10 +00004226 *
4227 * Dump a SchemaType structure
4228 */
4229static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004230xmlSchemaContentModelDump(xmlSchemaParticlePtr particle, FILE * output, int depth)
4231{
4232 xmlChar *str = NULL;
4233 xmlSchemaTreeItemPtr term;
4234 char shift[100];
4235 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004236
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004237 if (particle == NULL)
4238 return;
4239 for (i = 0;((i < depth) && (i < 25));i++)
4240 shift[2 * i] = shift[2 * i + 1] = ' ';
4241 shift[2 * i] = shift[2 * i + 1] = 0;
4242 fprintf(output, shift);
4243 if (particle->children == NULL) {
4244 fprintf(output, "MISSING particle term\n");
4245 return;
4246 }
4247 term = particle->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004248 if (term == NULL) {
4249 fprintf(output, "(NULL)");
4250 } else {
4251 switch (term->type) {
4252 case XML_SCHEMA_TYPE_ELEMENT:
4253 fprintf(output, "ELEM '%s'", xmlSchemaFormatQName(&str,
4254 ((xmlSchemaElementPtr)term)->targetNamespace,
4255 ((xmlSchemaElementPtr)term)->name));
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004256 FREE_AND_NULL(str);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004257 break;
4258 case XML_SCHEMA_TYPE_SEQUENCE:
4259 fprintf(output, "SEQUENCE");
4260 break;
4261 case XML_SCHEMA_TYPE_CHOICE:
4262 fprintf(output, "CHOICE");
4263 break;
4264 case XML_SCHEMA_TYPE_ALL:
4265 fprintf(output, "ALL");
4266 break;
4267 case XML_SCHEMA_TYPE_ANY:
4268 fprintf(output, "ANY");
4269 break;
4270 default:
4271 fprintf(output, "UNKNOWN\n");
4272 return;
4273 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004274 }
4275 if (particle->minOccurs != 1)
4276 fprintf(output, " min: %d", particle->minOccurs);
4277 if (particle->maxOccurs >= UNBOUNDED)
4278 fprintf(output, " max: unbounded");
4279 else if (particle->maxOccurs != 1)
4280 fprintf(output, " max: %d", particle->maxOccurs);
4281 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004282 if (term &&
4283 ((term->type == XML_SCHEMA_TYPE_SEQUENCE) ||
4284 (term->type == XML_SCHEMA_TYPE_CHOICE) ||
4285 (term->type == XML_SCHEMA_TYPE_ALL)) &&
4286 (term->children != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004287 xmlSchemaContentModelDump((xmlSchemaParticlePtr) term->children,
4288 output, depth +1);
4289 }
4290 if (particle->next != NULL)
4291 xmlSchemaContentModelDump((xmlSchemaParticlePtr) particle->next,
4292 output, depth);
4293}
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004294
4295/**
4296 * xmlSchemaAttrUsesDump:
4297 * @uses: attribute uses list
4298 * @output: the file output
4299 *
4300 * Dumps a list of attribute use components.
4301 */
4302static void
4303xmlSchemaAttrUsesDump(xmlSchemaItemListPtr uses, FILE * output)
4304{
4305 xmlSchemaAttributeUsePtr use;
4306 xmlSchemaAttributeUseProhibPtr prohib;
4307 xmlSchemaQNameRefPtr ref;
4308 const xmlChar *name, *tns;
4309 xmlChar *str = NULL;
4310 int i;
4311
4312 if ((uses == NULL) || (uses->nbItems == 0))
4313 return;
4314
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004315 fprintf(output, " attributes:\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004316 for (i = 0; i < uses->nbItems; i++) {
4317 use = uses->items[i];
4318 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
4319 fprintf(output, " [prohibition] ");
4320 prohib = (xmlSchemaAttributeUseProhibPtr) use;
4321 name = prohib->name;
4322 tns = prohib->targetNamespace;
4323 } else if (use->type == XML_SCHEMA_EXTRA_QNAMEREF) {
4324 fprintf(output, " [reference] ");
4325 ref = (xmlSchemaQNameRefPtr) use;
4326 name = ref->name;
4327 tns = ref->targetNamespace;
4328 } else {
4329 fprintf(output, " [use] ");
4330 name = WXS_ATTRUSE_DECL_NAME(use);
4331 tns = WXS_ATTRUSE_DECL_TNS(use);
4332 }
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004333 fprintf(output, "'%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004334 (const char *) xmlSchemaFormatQName(&str, tns, name));
4335 FREE_AND_NULL(str);
4336 }
4337}
4338
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004339/**
4340 * xmlSchemaTypeDump:
4341 * @output: the file output
4342 * @type: a type structure
4343 *
4344 * Dump a SchemaType structure
4345 */
4346static void
Daniel Veillard4255d502002-04-16 15:50:10 +00004347xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output)
4348{
4349 if (type == NULL) {
4350 fprintf(output, "Type: NULL\n");
4351 return;
4352 }
4353 fprintf(output, "Type: ");
4354 if (type->name != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004355 fprintf(output, "'%s' ", type->name);
Daniel Veillard4255d502002-04-16 15:50:10 +00004356 else
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004357 fprintf(output, "(no name) ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004358 if (type->targetNamespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004359 fprintf(output, "ns '%s' ", type->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004360 switch (type->type) {
4361 case XML_SCHEMA_TYPE_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004362 fprintf(output, "[basic] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004363 break;
4364 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004365 fprintf(output, "[simple] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004366 break;
4367 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004368 fprintf(output, "[complex] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004369 break;
4370 case XML_SCHEMA_TYPE_SEQUENCE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004371 fprintf(output, "[sequence] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004372 break;
4373 case XML_SCHEMA_TYPE_CHOICE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004374 fprintf(output, "[choice] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004375 break;
4376 case XML_SCHEMA_TYPE_ALL:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004377 fprintf(output, "[all] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004378 break;
4379 case XML_SCHEMA_TYPE_UR:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004380 fprintf(output, "[ur] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004381 break;
4382 case XML_SCHEMA_TYPE_RESTRICTION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004383 fprintf(output, "[restriction] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004384 break;
4385 case XML_SCHEMA_TYPE_EXTENSION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004386 fprintf(output, "[extension] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004387 break;
4388 default:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004389 fprintf(output, "[unknown type %d] ", type->type);
Daniel Veillard4255d502002-04-16 15:50:10 +00004390 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004391 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004392 fprintf(output, "content: ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004393 switch (type->contentType) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004394 case XML_SCHEMA_CONTENT_UNKNOWN:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004395 fprintf(output, "[unknown] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004396 break;
4397 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004398 fprintf(output, "[empty] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004399 break;
4400 case XML_SCHEMA_CONTENT_ELEMENTS:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004401 fprintf(output, "[element] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004402 break;
4403 case XML_SCHEMA_CONTENT_MIXED:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004404 fprintf(output, "[mixed] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004405 break;
4406 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004407 /* not used. */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004408 break;
4409 case XML_SCHEMA_CONTENT_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004410 fprintf(output, "[basic] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004411 break;
4412 case XML_SCHEMA_CONTENT_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004413 fprintf(output, "[simple] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004414 break;
4415 case XML_SCHEMA_CONTENT_ANY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004416 fprintf(output, "[any] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004417 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004418 }
4419 fprintf(output, "\n");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004420 if (type->base != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004421 fprintf(output, " base type: '%s'", type->base);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004422 if (type->baseNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004423 fprintf(output, " ns '%s'\n", type->baseNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004424 else
4425 fprintf(output, "\n");
4426 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004427 if (type->attrUses != NULL)
4428 xmlSchemaAttrUsesDump(type->attrUses, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004429 if (type->annot != NULL)
4430 xmlSchemaAnnotDump(output, type->annot);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004431#ifdef DUMP_CONTENT_MODEL
4432 if ((type->type == XML_SCHEMA_TYPE_COMPLEX) &&
4433 (type->subtypes != NULL)) {
4434 xmlSchemaContentModelDump((xmlSchemaParticlePtr) type->subtypes,
4435 output, 1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004436 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004437#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004438}
4439
4440/**
4441 * xmlSchemaDump:
4442 * @output: the file output
4443 * @schema: a schema structure
4444 *
4445 * Dump a Schema structure.
4446 */
4447void
4448xmlSchemaDump(FILE * output, xmlSchemaPtr schema)
4449{
Daniel Veillardce682bc2004-11-05 17:22:25 +00004450 if (output == NULL)
4451 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004452 if (schema == NULL) {
4453 fprintf(output, "Schemas: NULL\n");
4454 return;
4455 }
4456 fprintf(output, "Schemas: ");
4457 if (schema->name != NULL)
4458 fprintf(output, "%s, ", schema->name);
4459 else
4460 fprintf(output, "no name, ");
4461 if (schema->targetNamespace != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00004462 fprintf(output, "%s", (const char *) schema->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004463 else
4464 fprintf(output, "no target namespace");
4465 fprintf(output, "\n");
4466 if (schema->annot != NULL)
4467 xmlSchemaAnnotDump(output, schema->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00004468 xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump,
4469 output);
4470 xmlHashScanFull(schema->elemDecl,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004471 (xmlHashScannerFull) xmlSchemaElementDump, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004472}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004473
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004474#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004475/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004476 * xmlSchemaDebugDumpIDCTable:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004477 * @vctxt: the WXS validation context
4478 *
4479 * Displays the current IDC table for debug purposes.
4480 */
4481static void
4482xmlSchemaDebugDumpIDCTable(FILE * output,
4483 const xmlChar *namespaceName,
4484 const xmlChar *localName,
4485 xmlSchemaPSVIIDCBindingPtr bind)
4486{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004487 xmlChar *str = NULL;
4488 const xmlChar *value;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004489 xmlSchemaPSVIIDCNodePtr tab;
4490 xmlSchemaPSVIIDCKeyPtr key;
4491 int i, j, res;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004492
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004493 fprintf(output, "IDC: TABLES on '%s'\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004494 xmlSchemaFormatQName(&str, namespaceName, localName));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004495 FREE_AND_NULL(str)
4496
4497 if (bind == NULL)
4498 return;
4499 do {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004500 fprintf(output, "IDC: BINDING '%s' (%d)\n",
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004501 xmlSchemaGetComponentQName(&str,
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004502 bind->definition), bind->nbNodes);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004503 FREE_AND_NULL(str)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004504 for (i = 0; i < bind->nbNodes; i++) {
4505 tab = bind->nodeTable[i];
4506 fprintf(output, " ( ");
4507 for (j = 0; j < bind->definition->nbFields; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004508 key = tab->keys[j];
4509 if ((key != NULL) && (key->val != NULL)) {
4510 res = xmlSchemaGetCanonValue(key->val, &value);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004511 if (res >= 0)
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004512 fprintf(output, "'%s' ", value);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004513 else
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004514 fprintf(output, "CANON-VALUE-FAILED ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004515 if (res == 0)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004516 FREE_AND_NULL(value)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004517 } else if (key != NULL)
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004518 fprintf(output, "(no val), ");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004519 else
4520 fprintf(output, "(key missing), ");
4521 }
4522 fprintf(output, ")\n");
4523 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004524 if (bind->dupls && bind->dupls->nbItems) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004525 fprintf(output, "IDC: dupls (%d):\n", bind->dupls->nbItems);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004526 for (i = 0; i < bind->dupls->nbItems; i++) {
4527 tab = bind->dupls->items[i];
4528 fprintf(output, " ( ");
4529 for (j = 0; j < bind->definition->nbFields; j++) {
4530 key = tab->keys[j];
4531 if ((key != NULL) && (key->val != NULL)) {
4532 res = xmlSchemaGetCanonValue(key->val, &value);
4533 if (res >= 0)
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +00004534 fprintf(output, "'%s' ", value);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004535 else
4536 fprintf(output, "CANON-VALUE-FAILED ");
4537 if (res == 0)
4538 FREE_AND_NULL(value)
4539 } else if (key != NULL)
4540 fprintf(output, "(no val), ");
4541 else
4542 fprintf(output, "(key missing), ");
4543 }
4544 fprintf(output, ")\n");
4545 }
4546 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004547 bind = bind->next;
4548 } while (bind != NULL);
4549}
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00004550#endif /* DEBUG_IDC */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004551#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4255d502002-04-16 15:50:10 +00004552
4553/************************************************************************
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004554 * *
4555 * Utilities *
4556 * *
4557 ************************************************************************/
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004558
Daniel Veillardc0826a72004-08-10 14:17:33 +00004559/**
4560 * xmlSchemaGetPropNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004561 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004562 * @name: the name of the attribute
4563 *
4564 * Seeks an attribute with a name of @name in
4565 * no namespace.
4566 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004567 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004568 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004569static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004570xmlSchemaGetPropNode(xmlNodePtr node, const char *name)
Daniel Veillard01fa6152004-06-29 17:04:39 +00004571{
4572 xmlAttrPtr prop;
4573
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004574 if ((node == NULL) || (name == NULL))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004575 return(NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004576 prop = node->properties;
4577 while (prop != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004578 if ((prop->ns == NULL) && xmlStrEqual(prop->name, BAD_CAST name))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004579 return(prop);
4580 prop = prop->next;
4581 }
4582 return (NULL);
4583}
4584
4585/**
4586 * xmlSchemaGetPropNodeNs:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004587 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004588 * @uri: the uri
4589 * @name: the name of the attribute
4590 *
4591 * Seeks an attribute with a local name of @name and
4592 * a namespace URI of @uri.
4593 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004594 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004595 */
4596static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004597xmlSchemaGetPropNodeNs(xmlNodePtr node, const char *uri, const char *name)
Daniel Veillardc0826a72004-08-10 14:17:33 +00004598{
4599 xmlAttrPtr prop;
4600
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004601 if ((node == NULL) || (name == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004602 return(NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004603 prop = node->properties;
4604 while (prop != NULL) {
4605 if ((prop->ns != NULL) &&
4606 xmlStrEqual(prop->name, BAD_CAST name) &&
4607 xmlStrEqual(prop->ns->href, BAD_CAST uri))
Daniel Veillard01fa6152004-06-29 17:04:39 +00004608 return(prop);
4609 prop = prop->next;
4610 }
4611 return (NULL);
4612}
4613
4614static const xmlChar *
4615xmlSchemaGetNodeContent(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
4616{
4617 xmlChar *val;
4618 const xmlChar *ret;
4619
4620 val = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004621 if (val == NULL)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00004622 val = xmlStrdup((xmlChar *)"");
Daniel Veillard01fa6152004-06-29 17:04:39 +00004623 ret = xmlDictLookup(ctxt->dict, val, -1);
4624 xmlFree(val);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004625 return(ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004626}
4627
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004628static const xmlChar *
4629xmlSchemaGetNodeContentNoDict(xmlNodePtr node)
4630{
4631 return((const xmlChar*) xmlNodeGetContent(node));
4632}
4633
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004634/**
4635 * xmlSchemaGetProp:
4636 * @ctxt: the parser context
4637 * @node: the node
4638 * @name: the property name
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004639 *
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004640 * Read a attribute value and internalize the string
4641 *
4642 * Returns the string or NULL if not present.
4643 */
4644static const xmlChar *
4645xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
4646 const char *name)
4647{
4648 xmlChar *val;
4649 const xmlChar *ret;
4650
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004651 val = xmlGetNoNsProp(node, BAD_CAST name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004652 if (val == NULL)
4653 return(NULL);
4654 ret = xmlDictLookup(ctxt->dict, val, -1);
4655 xmlFree(val);
4656 return(ret);
4657}
4658
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004659/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +00004660 * *
4661 * Parsing functions *
4662 * *
4663 ************************************************************************/
4664
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004665#define WXS_FIND_GLOBAL_ITEM(slot) \
4666 if (xmlStrEqual(nsName, schema->targetNamespace)) { \
4667 ret = xmlHashLookup(schema->slot, name); \
4668 if (ret != NULL) goto exit; \
4669 } \
4670 if (xmlHashSize(schema->schemasImports) > 1) { \
4671 xmlSchemaImportPtr import; \
4672 if (nsName == NULL) \
4673 import = xmlHashLookup(schema->schemasImports, \
4674 XML_SCHEMAS_NO_NAMESPACE); \
4675 else \
4676 import = xmlHashLookup(schema->schemasImports, nsName); \
4677 if (import == NULL) \
4678 goto exit; \
4679 ret = xmlHashLookup(import->schema->slot, name); \
4680 }
4681
Daniel Veillard4255d502002-04-16 15:50:10 +00004682/**
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004683 * xmlSchemaGetElem:
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004684 * @schema: the schema context
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004685 * @name: the element name
4686 * @ns: the element namespace
4687 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004688 * Lookup a global element declaration in the schema.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004689 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004690 * Returns the element declaration or NULL if not found.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004691 */
4692static xmlSchemaElementPtr
4693xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004694 const xmlChar * nsName)
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004695{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004696 xmlSchemaElementPtr ret = NULL;
4697
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004698 if ((name == NULL) || (schema == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004699 return(NULL);
4700 if (schema != NULL) {
4701 WXS_FIND_GLOBAL_ITEM(elemDecl)
4702 }
4703exit:
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004704#ifdef DEBUG
4705 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004706 if (nsName == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004707 fprintf(stderr, "Unable to lookup element decl. %s", name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004708 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004709 fprintf(stderr, "Unable to lookup element decl. %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004710 nsName);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004711 }
4712#endif
4713 return (ret);
4714}
4715
4716/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004717 * xmlSchemaGetType:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004718 * @schema: the main schema
4719 * @name: the type's name
4720 * nsName: the type's namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00004721 *
4722 * Lookup a type in the schemas or the predefined types
4723 *
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004724 * Returns the group definition or NULL if not found.
Daniel Veillard4255d502002-04-16 15:50:10 +00004725 */
4726static xmlSchemaTypePtr
4727xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004728 const xmlChar * nsName)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004729{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004730 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00004731
4732 if (name == NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004733 return (NULL);
4734 /* First try the built-in types. */
4735 if ((nsName != NULL) && xmlStrEqual(nsName, xmlSchemaNs)) {
4736 ret = xmlSchemaGetPredefinedType(name, nsName);
4737 if (ret != NULL)
4738 goto exit;
4739 /*
4740 * Note that we try the parsed schemas as well here
4741 * since one might have parsed the S4S, which contain more
4742 * than the built-in types.
4743 * TODO: Can we optimize this?
4744 */
4745 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004746 if (schema != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004747 WXS_FIND_GLOBAL_ITEM(typeDecl)
4748 }
4749exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004750
Daniel Veillard4255d502002-04-16 15:50:10 +00004751#ifdef DEBUG
4752 if (ret == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004753 if (nsName == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004754 fprintf(stderr, "Unable to lookup type %s", name);
4755 else
4756 fprintf(stderr, "Unable to lookup type %s:%s", name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004757 nsName);
Daniel Veillard4255d502002-04-16 15:50:10 +00004758 }
4759#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004760 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004761}
4762
Daniel Veillard3646d642004-06-02 19:19:14 +00004763/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004764 * xmlSchemaGetAttributeDecl:
4765 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004766 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004767 * @ns: the target namespace of the attribute
Daniel Veillard3646d642004-06-02 19:19:14 +00004768 *
4769 * Lookup a an attribute in the schema or imported schemas
4770 *
4771 * Returns the attribute declaration or NULL if not found.
4772 */
4773static xmlSchemaAttributePtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004774xmlSchemaGetAttributeDecl(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004775 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004776{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004777 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004778
4779 if ((name == NULL) || (schema == NULL))
4780 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004781 if (schema != NULL) {
4782 WXS_FIND_GLOBAL_ITEM(attrDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004783 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004784exit:
Daniel Veillard3646d642004-06-02 19:19:14 +00004785#ifdef DEBUG
4786 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004787 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004788 fprintf(stderr, "Unable to lookup attribute %s", name);
4789 else
4790 fprintf(stderr, "Unable to lookup attribute %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004791 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004792 }
4793#endif
4794 return (ret);
4795}
4796
4797/**
4798 * xmlSchemaGetAttributeGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004799 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004800 * @name: the name of the attribute group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004801 * @ns: the target namespace of the attribute group
Daniel Veillard3646d642004-06-02 19:19:14 +00004802 *
4803 * Lookup a an attribute group in the schema or imported schemas
4804 *
4805 * Returns the attribute group definition or NULL if not found.
4806 */
4807static xmlSchemaAttributeGroupPtr
4808xmlSchemaGetAttributeGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004809 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004810{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004811 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004812
4813 if ((name == NULL) || (schema == NULL))
4814 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004815 if (schema != NULL) {
4816 WXS_FIND_GLOBAL_ITEM(attrgrpDecl)
4817 }
4818exit:
4819 /* TODO:
4820 if ((ret != NULL) && (ret->redef != NULL)) {
4821 * Return the last redefinition. *
4822 ret = ret->redef;
Daniel Veillard3646d642004-06-02 19:19:14 +00004823 }
William M. Brack2f2a6632004-08-20 23:09:47 +00004824 */
Daniel Veillard3646d642004-06-02 19:19:14 +00004825#ifdef DEBUG
4826 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004827 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004828 fprintf(stderr, "Unable to lookup attribute group %s", name);
4829 else
4830 fprintf(stderr, "Unable to lookup attribute group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004831 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004832 }
4833#endif
4834 return (ret);
4835}
4836
4837/**
4838 * xmlSchemaGetGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004839 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004840 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004841 * @ns: the target namespace of the group
Daniel Veillard3646d642004-06-02 19:19:14 +00004842 *
4843 * Lookup a group in the schema or imported schemas
4844 *
4845 * Returns the group definition or NULL if not found.
4846 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004847static xmlSchemaModelGroupDefPtr
Daniel Veillard3646d642004-06-02 19:19:14 +00004848xmlSchemaGetGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004849 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004850{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004851 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004852
4853 if ((name == NULL) || (schema == NULL))
4854 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004855 if (schema != NULL) {
4856 WXS_FIND_GLOBAL_ITEM(groupDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004857 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004858exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004859
Daniel Veillard3646d642004-06-02 19:19:14 +00004860#ifdef DEBUG
4861 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004862 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004863 fprintf(stderr, "Unable to lookup group %s", name);
4864 else
4865 fprintf(stderr, "Unable to lookup group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004866 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004867 }
4868#endif
4869 return (ret);
4870}
4871
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004872static xmlSchemaNotationPtr
4873xmlSchemaGetNotation(xmlSchemaPtr schema,
4874 const xmlChar *name,
4875 const xmlChar *nsName)
4876{
4877 xmlSchemaNotationPtr ret = NULL;
4878
4879 if ((name == NULL) || (schema == NULL))
4880 return (NULL);
4881 if (schema != NULL) {
4882 WXS_FIND_GLOBAL_ITEM(notaDecl)
4883 }
4884exit:
4885 return (ret);
4886}
4887
4888static xmlSchemaIDCPtr
4889xmlSchemaGetIDC(xmlSchemaPtr schema,
4890 const xmlChar *name,
4891 const xmlChar *nsName)
4892{
4893 xmlSchemaIDCPtr ret = NULL;
4894
4895 if ((name == NULL) || (schema == NULL))
4896 return (NULL);
4897 if (schema != NULL) {
4898 WXS_FIND_GLOBAL_ITEM(idcDef)
4899 }
4900exit:
4901 return (ret);
4902}
4903
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004904/**
4905 * xmlSchemaGetNamedComponent:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004906 * @schema: the schema
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004907 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004908 * @ns: the target namespace of the group
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004909 *
4910 * Lookup a group in the schema or imported schemas
4911 *
4912 * Returns the group definition or NULL if not found.
4913 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004914static xmlSchemaBasicItemPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004915xmlSchemaGetNamedComponent(xmlSchemaPtr schema,
4916 xmlSchemaTypeType itemType,
4917 const xmlChar *name,
4918 const xmlChar *targetNs)
4919{
4920 switch (itemType) {
4921 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004922 return ((xmlSchemaBasicItemPtr) xmlSchemaGetGroup(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004923 name, targetNs));
4924 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004925 return ((xmlSchemaBasicItemPtr) xmlSchemaGetElem(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004926 name, targetNs));
4927 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004928 TODO
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004929 return (NULL);
4930 }
4931}
4932
Daniel Veillard4255d502002-04-16 15:50:10 +00004933/************************************************************************
4934 * *
4935 * Parsing functions *
4936 * *
4937 ************************************************************************/
4938
4939#define IS_BLANK_NODE(n) \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004940 (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content, -1)))
Daniel Veillard4255d502002-04-16 15:50:10 +00004941
4942/**
4943 * xmlSchemaIsBlank:
4944 * @str: a string
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004945 * @len: the length of the string or -1
Daniel Veillard4255d502002-04-16 15:50:10 +00004946 *
4947 * Check if a string is ignorable
4948 *
4949 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
4950 */
4951static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004952xmlSchemaIsBlank(xmlChar * str, int len)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004953{
Daniel Veillard4255d502002-04-16 15:50:10 +00004954 if (str == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004955 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004956 if (len < 0) {
4957 while (*str != 0) {
4958 if (!(IS_BLANK_CH(*str)))
4959 return (0);
4960 str++;
4961 }
4962 } else while ((*str != 0) && (len != 0)) {
4963 if (!(IS_BLANK_CH(*str)))
4964 return (0);
4965 str++;
4966 len--;
Daniel Veillard4255d502002-04-16 15:50:10 +00004967 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004968
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004969 return (1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004970}
4971
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004972#define WXS_COMP_NAME(c, t) ((t) (c))->name
4973#define WXS_COMP_TNS(c, t) ((t) (c))->targetNamespace
4974/*
4975* xmlSchemaFindRedefCompInGraph:
4976* ATTENTION TODO: This uses pointer comp. for strings.
4977*/
4978static xmlSchemaBasicItemPtr
4979xmlSchemaFindRedefCompInGraph(xmlSchemaBucketPtr bucket,
4980 xmlSchemaTypeType type,
4981 const xmlChar *name,
4982 const xmlChar *nsName)
4983{
4984 xmlSchemaBasicItemPtr ret;
4985 int i;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004986
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004987 if ((bucket == NULL) || (name == NULL))
4988 return(NULL);
4989 if ((bucket->globals == NULL) ||
4990 (bucket->globals->nbItems == 0))
4991 goto subschemas;
4992 /*
4993 * Search in global components.
4994 */
4995 for (i = 0; i < bucket->globals->nbItems; i++) {
4996 ret = bucket->globals->items[i];
4997 if (ret->type == type) {
4998 switch (type) {
4999 case XML_SCHEMA_TYPE_COMPLEX:
5000 case XML_SCHEMA_TYPE_SIMPLE:
5001 if ((WXS_COMP_NAME(ret, xmlSchemaTypePtr) == name) &&
5002 (WXS_COMP_TNS(ret, xmlSchemaTypePtr) ==
5003 nsName))
5004 {
5005 return(ret);
5006 }
5007 break;
5008 case XML_SCHEMA_TYPE_GROUP:
5009 if ((WXS_COMP_NAME(ret,
5010 xmlSchemaModelGroupDefPtr) == name) &&
5011 (WXS_COMP_TNS(ret,
5012 xmlSchemaModelGroupDefPtr) == nsName))
5013 {
5014 return(ret);
5015 }
5016 break;
5017 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
5018 if ((WXS_COMP_NAME(ret,
5019 xmlSchemaAttributeGroupPtr) == name) &&
5020 (WXS_COMP_TNS(ret,
5021 xmlSchemaAttributeGroupPtr) == nsName))
5022 {
5023 return(ret);
5024 }
Kasimier T. Buchcik5d2998b2005-11-22 17:36:01 +00005025 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005026 default:
5027 /* Should not be hit. */
5028 return(NULL);
5029 }
5030 }
5031 }
5032subschemas:
5033 /*
5034 * Process imported/included schemas.
5035 */
5036 if (bucket->relations != NULL) {
5037 xmlSchemaSchemaRelationPtr rel = bucket->relations;
5038
5039 /*
5040 * TODO: Marking the bucket will not avoid multiple searches
5041 * in the same schema, but avoids at least circularity.
5042 */
5043 bucket->flags |= XML_SCHEMA_BUCKET_MARKED;
5044 do {
5045 if ((rel->bucket != NULL) &&
5046 ((rel->bucket->flags & XML_SCHEMA_BUCKET_MARKED) == 0)) {
5047 ret = xmlSchemaFindRedefCompInGraph(rel->bucket,
5048 type, name, nsName);
5049 if (ret != NULL)
5050 return(ret);
5051 }
5052 rel = rel->next;
5053 } while (rel != NULL);
5054 bucket->flags ^= XML_SCHEMA_BUCKET_MARKED;
5055 }
5056 return(NULL);
5057}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005058
5059/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005060 * xmlSchemaAddNotation:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005061 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005062 * @schema: the schema being built
5063 * @name: the item name
5064 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00005065 * Add an XML schema annotation declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00005066 * *WARNING* this interface is highly subject to change
5067 *
5068 * Returns the new struture or NULL in case of error
5069 */
5070static xmlSchemaNotationPtr
5071xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005072 const xmlChar *name, const xmlChar *nsName,
5073 xmlNodePtr node ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00005074{
5075 xmlSchemaNotationPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005076
5077 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5078 return (NULL);
5079
Daniel Veillard4255d502002-04-16 15:50:10 +00005080 ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation));
5081 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005082 xmlSchemaPErrMemory(ctxt, "add annotation", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005083 return (NULL);
5084 }
5085 memset(ret, 0, sizeof(xmlSchemaNotation));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005086 ret->type = XML_SCHEMA_TYPE_NOTATION;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005087 ret->name = name;
5088 ret->targetNamespace = nsName;
5089 /* TODO: do we need the node to be set?
5090 * ret->node = node;*/
5091 WXS_ADD_GLOBAL(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005092 return (ret);
5093}
5094
Daniel Veillard4255d502002-04-16 15:50:10 +00005095/**
5096 * xmlSchemaAddAttribute:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005097 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005098 * @schema: the schema being built
5099 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005100 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005101 *
5102 * Add an XML schema Attrribute declaration
5103 * *WARNING* this interface is highly subject to change
5104 *
5105 * Returns the new struture or NULL in case of error
5106 */
5107static xmlSchemaAttributePtr
5108xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005109 const xmlChar * name, const xmlChar * nsName,
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00005110 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005111{
5112 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005113
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005114 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005115 return (NULL);
5116
5117 ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute));
5118 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005119 xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005120 return (NULL);
5121 }
5122 memset(ret, 0, sizeof(xmlSchemaAttribute));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005123 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE;
5124 ret->node = node;
5125 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005126 ret->targetNamespace = nsName;
5127
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005128 if (topLevel)
5129 WXS_ADD_GLOBAL(ctxt, ret);
5130 else
5131 WXS_ADD_LOCAL(ctxt, ret);
5132 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005133 return (ret);
5134}
5135
5136/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005137 * xmlSchemaAddAttributeUse:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005138 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005139 * @schema: the schema being built
5140 * @name: the item name
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005141 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005142 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005143 * Add an XML schema Attrribute declaration
5144 * *WARNING* this interface is highly subject to change
5145 *
5146 * Returns the new struture or NULL in case of error
5147 */
5148static xmlSchemaAttributeUsePtr
5149xmlSchemaAddAttributeUse(xmlSchemaParserCtxtPtr pctxt,
5150 xmlNodePtr node)
5151{
5152 xmlSchemaAttributeUsePtr ret = NULL;
5153
5154 if (pctxt == NULL)
5155 return (NULL);
5156
5157 ret = (xmlSchemaAttributeUsePtr) xmlMalloc(sizeof(xmlSchemaAttributeUse));
5158 if (ret == NULL) {
5159 xmlSchemaPErrMemory(pctxt, "allocating attribute", NULL);
5160 return (NULL);
5161 }
5162 memset(ret, 0, sizeof(xmlSchemaAttributeUse));
5163 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE_USE;
5164 ret->node = node;
5165
5166 WXS_ADD_LOCAL(pctxt, ret);
5167 return (ret);
5168}
5169
5170/*
5171* xmlSchemaAddRedef:
5172*
5173* Adds a redefinition information. This is used at a later stage to:
5174* resolve references to the redefined components and to check constraints.
5175*/
5176static xmlSchemaRedefPtr
5177xmlSchemaAddRedef(xmlSchemaParserCtxtPtr pctxt,
5178 xmlSchemaBucketPtr targetBucket,
5179 void *item,
5180 const xmlChar *refName,
5181 const xmlChar *refTargetNs)
5182{
5183 xmlSchemaRedefPtr ret;
5184
5185 ret = (xmlSchemaRedefPtr)
5186 xmlMalloc(sizeof(xmlSchemaRedef));
5187 if (ret == NULL) {
5188 xmlSchemaPErrMemory(pctxt,
5189 "allocating redefinition info", NULL);
5190 return (NULL);
5191 }
5192 memset(ret, 0, sizeof(xmlSchemaRedef));
5193 ret->item = item;
5194 ret->targetBucket = targetBucket;
5195 ret->refName = refName;
5196 ret->refTargetNs = refTargetNs;
5197 if (WXS_CONSTRUCTOR(pctxt)->redefs == NULL)
5198 WXS_CONSTRUCTOR(pctxt)->redefs = ret;
5199 else
5200 WXS_CONSTRUCTOR(pctxt)->lastRedef->next = ret;
5201 WXS_CONSTRUCTOR(pctxt)->lastRedef = ret;
5202
5203 return (ret);
5204}
5205
5206/**
5207 * xmlSchemaAddAttributeGroupDefinition:
5208 * @ctxt: a schema parser context
5209 * @schema: the schema being built
5210 * @name: the item name
5211 * @nsName: the target namespace
5212 * @node: the corresponding node
5213 *
5214 * Add an XML schema Attrribute Group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00005215 *
5216 * Returns the new struture or NULL in case of error
5217 */
5218static xmlSchemaAttributeGroupPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005219xmlSchemaAddAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
5220 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
5221 const xmlChar *name,
5222 const xmlChar *nsName,
5223 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00005224{
5225 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005226
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005227 if ((pctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005228 return (NULL);
5229
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005230 ret = (xmlSchemaAttributeGroupPtr)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005231 xmlMalloc(sizeof(xmlSchemaAttributeGroup));
Daniel Veillard4255d502002-04-16 15:50:10 +00005232 if (ret == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005233 xmlSchemaPErrMemory(pctxt, "allocating attribute group", NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005234 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005235 }
5236 memset(ret, 0, sizeof(xmlSchemaAttributeGroup));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005237 ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005238 ret->name = name;
5239 ret->targetNamespace = nsName;
5240 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005241
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005242 /* TODO: Remove the flag. */
5243 ret->flags |= XML_SCHEMAS_ATTRGROUP_GLOBAL;
5244 if (pctxt->isRedefine) {
5245 pctxt->redef = xmlSchemaAddRedef(pctxt, pctxt->redefined,
5246 ret, name, nsName);
5247 if (pctxt->redef == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005248 xmlFree(ret);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005249 return(NULL);
5250 }
5251 pctxt->redefCounter = 0;
5252 }
5253 WXS_ADD_GLOBAL(pctxt, ret);
5254 WXS_ADD_PENDING(pctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005255 return (ret);
5256}
5257
5258/**
5259 * xmlSchemaAddElement:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005260 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005261 * @schema: the schema being built
5262 * @name: the type name
5263 * @namespace: the type namespace
5264 *
5265 * Add an XML schema Element declaration
5266 * *WARNING* this interface is highly subject to change
5267 *
5268 * Returns the new struture or NULL in case of error
5269 */
5270static xmlSchemaElementPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005271xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005272 const xmlChar * name, const xmlChar * nsName,
William M. Brack2f2a6632004-08-20 23:09:47 +00005273 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005274{
5275 xmlSchemaElementPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005276
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005277 if ((ctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005278 return (NULL);
5279
Daniel Veillard4255d502002-04-16 15:50:10 +00005280 ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement));
5281 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005282 xmlSchemaPErrMemory(ctxt, "allocating element", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005283 return (NULL);
5284 }
5285 memset(ret, 0, sizeof(xmlSchemaElement));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005286 ret->type = XML_SCHEMA_TYPE_ELEMENT;
5287 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005288 ret->targetNamespace = nsName;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005289 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005290
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005291 if (topLevel)
5292 WXS_ADD_GLOBAL(ctxt, ret);
5293 else
5294 WXS_ADD_LOCAL(ctxt, ret);
5295 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005296 return (ret);
5297}
5298
5299/**
5300 * xmlSchemaAddType:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005301 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005302 * @schema: the schema being built
5303 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005304 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005305 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00005306 * Add an XML schema item
Daniel Veillard4255d502002-04-16 15:50:10 +00005307 * *WARNING* this interface is highly subject to change
5308 *
5309 * Returns the new struture or NULL in case of error
5310 */
5311static xmlSchemaTypePtr
5312xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005313 xmlSchemaTypeType type,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00005314 const xmlChar * name, const xmlChar * nsName,
5315 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005316{
5317 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005318
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005319 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005320 return (NULL);
5321
5322 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
5323 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005324 xmlSchemaPErrMemory(ctxt, "allocating type", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005325 return (NULL);
5326 }
5327 memset(ret, 0, sizeof(xmlSchemaType));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005328 ret->type = type;
5329 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005330 ret->targetNamespace = nsName;
5331 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005332 if (topLevel) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005333 if (ctxt->isRedefine) {
5334 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5335 ret, name, nsName);
5336 if (ctxt->redef == NULL) {
5337 xmlFree(ret);
5338 return(NULL);
5339 }
5340 ctxt->redefCounter = 0;
5341 }
5342 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005343 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005344 WXS_ADD_LOCAL(ctxt, ret);
5345 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005346 return (ret);
5347}
5348
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005349static xmlSchemaQNameRefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005350xmlSchemaNewQNameRef(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005351 xmlSchemaTypeType refType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005352 const xmlChar *refName,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005353 const xmlChar *refNs)
5354{
5355 xmlSchemaQNameRefPtr ret;
5356
5357 ret = (xmlSchemaQNameRefPtr)
5358 xmlMalloc(sizeof(xmlSchemaQNameRef));
5359 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005360 xmlSchemaPErrMemory(pctxt,
5361 "allocating QName reference item", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005362 return (NULL);
5363 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005364 ret->node = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005365 ret->type = XML_SCHEMA_EXTRA_QNAMEREF;
5366 ret->name = refName;
5367 ret->targetNamespace = refNs;
5368 ret->item = NULL;
5369 ret->itemType = refType;
5370 /*
5371 * Store the reference item in the schema.
5372 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005373 WXS_ADD_LOCAL(pctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005374 return (ret);
5375}
5376
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005377static xmlSchemaAttributeUseProhibPtr
5378xmlSchemaAddAttributeUseProhib(xmlSchemaParserCtxtPtr pctxt)
5379{
5380 xmlSchemaAttributeUseProhibPtr ret;
5381
5382 ret = (xmlSchemaAttributeUseProhibPtr)
5383 xmlMalloc(sizeof(xmlSchemaAttributeUseProhib));
5384 if (ret == NULL) {
5385 xmlSchemaPErrMemory(pctxt,
5386 "allocating attribute use prohibition", NULL);
5387 return (NULL);
5388 }
5389 memset(ret, 0, sizeof(xmlSchemaAttributeUseProhib));
5390 ret->type = XML_SCHEMA_EXTRA_ATTR_USE_PROHIB;
5391 WXS_ADD_LOCAL(pctxt, ret);
5392 return (ret);
5393}
5394
5395
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005396/**
5397 * xmlSchemaAddModelGroup:
5398 * @ctxt: a schema parser context
5399 * @schema: the schema being built
5400 * @type: the "compositor" type of the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005401 * @node: the node in the schema doc
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005402 *
5403 * Adds a schema model group
5404 * *WARNING* this interface is highly subject to change
5405 *
5406 * Returns the new struture or NULL in case of error
5407 */
5408static xmlSchemaModelGroupPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005409xmlSchemaAddModelGroup(xmlSchemaParserCtxtPtr ctxt,
5410 xmlSchemaPtr schema,
5411 xmlSchemaTypeType type,
5412 xmlNodePtr node)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005413{
5414 xmlSchemaModelGroupPtr ret = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005415
5416 if ((ctxt == NULL) || (schema == NULL))
5417 return (NULL);
5418
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005419 ret = (xmlSchemaModelGroupPtr)
5420 xmlMalloc(sizeof(xmlSchemaModelGroup));
5421 if (ret == NULL) {
5422 xmlSchemaPErrMemory(ctxt, "allocating model group component",
5423 NULL);
5424 return (NULL);
5425 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005426 memset(ret, 0, sizeof(xmlSchemaModelGroup));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005427 ret->type = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005428 ret->node = node;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005429 WXS_ADD_LOCAL(ctxt, ret);
5430 if ((type == XML_SCHEMA_TYPE_SEQUENCE) ||
5431 (type == XML_SCHEMA_TYPE_CHOICE))
5432 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005433 return (ret);
5434}
5435
5436
5437/**
5438 * xmlSchemaAddParticle:
5439 * @ctxt: a schema parser context
5440 * @schema: the schema being built
5441 * @node: the corresponding node in the schema doc
5442 * @min: the minOccurs
5443 * @max: the maxOccurs
5444 *
5445 * Adds an XML schema particle component.
5446 * *WARNING* this interface is highly subject to change
5447 *
5448 * Returns the new struture or NULL in case of error
5449 */
5450static xmlSchemaParticlePtr
5451xmlSchemaAddParticle(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5452 xmlNodePtr node, int min, int max)
5453{
5454 xmlSchemaParticlePtr ret = NULL;
5455 if ((ctxt == NULL) || (schema == NULL))
5456 return (NULL);
5457
5458#ifdef DEBUG
5459 fprintf(stderr, "Adding particle component\n");
5460#endif
5461 ret = (xmlSchemaParticlePtr)
5462 xmlMalloc(sizeof(xmlSchemaParticle));
5463 if (ret == NULL) {
5464 xmlSchemaPErrMemory(ctxt, "allocating particle component",
5465 NULL);
5466 return (NULL);
5467 }
5468 ret->type = XML_SCHEMA_TYPE_PARTICLE;
5469 ret->annot = NULL;
5470 ret->node = node;
5471 ret->minOccurs = min;
5472 ret->maxOccurs = max;
5473 ret->next = NULL;
5474 ret->children = NULL;
5475
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005476 WXS_ADD_LOCAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005477 /*
5478 * Note that addition to pending components will be done locally
5479 * to the specific parsing function, since the most particles
5480 * need not to be fixed up (i.e. the reference to be resolved).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005481 * REMOVED: WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005482 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005483 return (ret);
5484}
5485
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005486/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005487 * xmlSchemaAddModelGroupDefinition:
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005488 * @ctxt: a schema validation context
5489 * @schema: the schema being built
5490 * @name: the group name
5491 *
5492 * Add an XML schema Group definition
5493 *
5494 * Returns the new struture or NULL in case of error
5495 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005496static xmlSchemaModelGroupDefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005497xmlSchemaAddModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
5498 xmlSchemaPtr schema,
5499 const xmlChar *name,
5500 const xmlChar *nsName,
5501 xmlNodePtr node)
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005502{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005503 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005504
5505 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5506 return (NULL);
5507
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005508 ret = (xmlSchemaModelGroupDefPtr)
5509 xmlMalloc(sizeof(xmlSchemaModelGroupDef));
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005510 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005511 xmlSchemaPErrMemory(ctxt, "adding group", NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005512 return (NULL);
5513 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005514 memset(ret, 0, sizeof(xmlSchemaModelGroupDef));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005515 ret->name = name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005516 ret->type = XML_SCHEMA_TYPE_GROUP;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005517 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005518 ret->targetNamespace = nsName;
5519
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005520 if (ctxt->isRedefine) {
5521 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5522 ret, name, nsName);
5523 if (ctxt->redef == NULL) {
5524 xmlFree(ret);
5525 return(NULL);
5526 }
5527 ctxt->redefCounter = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005528 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005529 WXS_ADD_GLOBAL(ctxt, ret);
5530 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005531 return (ret);
5532}
5533
Daniel Veillard3646d642004-06-02 19:19:14 +00005534/**
5535 * xmlSchemaNewWildcardNs:
5536 * @ctxt: a schema validation context
5537 *
5538 * Creates a new wildcard namespace constraint.
5539 *
5540 * Returns the new struture or NULL in case of error
5541 */
5542static xmlSchemaWildcardNsPtr
5543xmlSchemaNewWildcardNsConstraint(xmlSchemaParserCtxtPtr ctxt)
5544{
5545 xmlSchemaWildcardNsPtr ret;
5546
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005547 ret = (xmlSchemaWildcardNsPtr)
Daniel Veillard3646d642004-06-02 19:19:14 +00005548 xmlMalloc(sizeof(xmlSchemaWildcardNs));
5549 if (ret == NULL) {
5550 xmlSchemaPErrMemory(ctxt, "creating wildcard namespace constraint", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005551 return (NULL);
Daniel Veillard3646d642004-06-02 19:19:14 +00005552 }
5553 ret->value = NULL;
5554 ret->next = NULL;
5555 return (ret);
5556}
5557
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005558static xmlSchemaIDCPtr
5559xmlSchemaAddIDC(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5560 const xmlChar *name, const xmlChar *nsName,
5561 int category, xmlNodePtr node)
5562{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005563 xmlSchemaIDCPtr ret = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005564
5565 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5566 return (NULL);
5567
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005568 ret = (xmlSchemaIDCPtr) xmlMalloc(sizeof(xmlSchemaIDC));
5569 if (ret == NULL) {
5570 xmlSchemaPErrMemory(ctxt,
5571 "allocating an identity-constraint definition", NULL);
5572 return (NULL);
5573 }
5574 memset(ret, 0, sizeof(xmlSchemaIDC));
5575 /* The target namespace of the parent element declaration. */
5576 ret->targetNamespace = nsName;
5577 ret->name = name;
5578 ret->type = category;
5579 ret->node = node;
5580
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005581 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005582 /*
5583 * Only keyrefs need to be fixup up.
5584 */
5585 if (category == XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005586 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005587 return (ret);
5588}
5589
Daniel Veillard3646d642004-06-02 19:19:14 +00005590/**
5591 * xmlSchemaAddWildcard:
5592 * @ctxt: a schema validation context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005593 * @schema: a schema
5594 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005595 * Adds a wildcard.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005596 * It corresponds to a xsd:anyAttribute and xsd:any.
Daniel Veillard3646d642004-06-02 19:19:14 +00005597 *
5598 * Returns the new struture or NULL in case of error
5599 */
5600static xmlSchemaWildcardPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005601xmlSchemaAddWildcard(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5602 xmlSchemaTypeType type, xmlNodePtr node)
Daniel Veillard3646d642004-06-02 19:19:14 +00005603{
5604 xmlSchemaWildcardPtr ret = NULL;
5605
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005606 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard3646d642004-06-02 19:19:14 +00005607 return (NULL);
5608
5609 ret = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
5610 if (ret == NULL) {
5611 xmlSchemaPErrMemory(ctxt, "adding wildcard", NULL);
5612 return (NULL);
5613 }
5614 memset(ret, 0, sizeof(xmlSchemaWildcard));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005615 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005616 ret->node = node;
5617 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillard3646d642004-06-02 19:19:14 +00005618 return (ret);
5619}
5620
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005621static void
5622xmlSchemaSubstGroupFree(xmlSchemaSubstGroupPtr group)
5623{
5624 if (group == NULL)
5625 return;
5626 if (group->members != NULL)
5627 xmlSchemaItemListFree(group->members);
5628 xmlFree(group);
5629}
5630
5631static xmlSchemaSubstGroupPtr
5632xmlSchemaSubstGroupAdd(xmlSchemaParserCtxtPtr pctxt,
5633 xmlSchemaElementPtr head)
5634{
5635 xmlSchemaSubstGroupPtr ret;
5636
5637 /* Init subst group hash. */
5638 if (WXS_SUBST_GROUPS(pctxt) == NULL) {
5639 WXS_SUBST_GROUPS(pctxt) = xmlHashCreateDict(10, pctxt->dict);
5640 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5641 return(NULL);
5642 }
5643 /* Create a new substitution group. */
5644 ret = (xmlSchemaSubstGroupPtr) xmlMalloc(sizeof(xmlSchemaSubstGroup));
5645 if (ret == NULL) {
5646 xmlSchemaPErrMemory(NULL,
5647 "allocating a substitution group container", NULL);
5648 return(NULL);
5649 }
5650 memset(ret, 0, sizeof(xmlSchemaSubstGroup));
5651 ret->head = head;
5652 /* Create list of members. */
5653 ret->members = xmlSchemaItemListCreate();
5654 if (ret->members == NULL) {
5655 xmlSchemaSubstGroupFree(ret);
5656 return(NULL);
5657 }
5658 /* Add subst group to hash. */
5659 if (xmlHashAddEntry2(WXS_SUBST_GROUPS(pctxt),
5660 head->name, head->targetNamespace, ret) != 0) {
5661 PERROR_INT("xmlSchemaSubstGroupAdd",
5662 "failed to add a new substitution container");
5663 xmlSchemaSubstGroupFree(ret);
5664 return(NULL);
5665 }
5666 return(ret);
5667}
5668
5669static xmlSchemaSubstGroupPtr
5670xmlSchemaSubstGroupGet(xmlSchemaParserCtxtPtr pctxt,
5671 xmlSchemaElementPtr head)
5672{
5673 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5674 return(NULL);
5675 return(xmlHashLookup2(WXS_SUBST_GROUPS(pctxt),
5676 head->name, head->targetNamespace));
5677
5678}
5679
5680/**
5681 * xmlSchemaAddElementSubstitutionMember:
5682 * @pctxt: a schema parser context
5683 * @head: the head of the substitution group
5684 * @member: the new member of the substitution group
5685 *
5686 * Allocate a new annotation structure.
5687 *
5688 * Returns the newly allocated structure or NULL in case or error
5689 */
5690static int
5691xmlSchemaAddElementSubstitutionMember(xmlSchemaParserCtxtPtr pctxt,
5692 xmlSchemaElementPtr head,
5693 xmlSchemaElementPtr member)
5694{
5695 xmlSchemaSubstGroupPtr substGroup = NULL;
5696
5697 if ((pctxt == NULL) || (head == NULL) || (member == NULL))
5698 return (-1);
5699
5700 substGroup = xmlSchemaSubstGroupGet(pctxt, head);
5701 if (substGroup == NULL)
5702 substGroup = xmlSchemaSubstGroupAdd(pctxt, head);
5703 if (substGroup == NULL)
5704 return(-1);
5705 if (xmlSchemaItemListAdd(substGroup->members, member) == -1)
5706 return(-1);
5707 return(0);
5708}
5709
Daniel Veillard4255d502002-04-16 15:50:10 +00005710/************************************************************************
5711 * *
5712 * Utilities for parsing *
5713 * *
5714 ************************************************************************/
5715
Daniel Veillard4255d502002-04-16 15:50:10 +00005716/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00005717 * xmlSchemaPValAttrNodeQNameValue:
5718 * @ctxt: a schema parser context
5719 * @schema: the schema context
5720 * @ownerDes: the designation of the parent element
5721 * @ownerItem: the parent as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005722 * @value: the QName value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005723 * @local: the resulting local part if found, the attribute value otherwise
5724 * @uri: the resulting namespace URI if found
5725 *
5726 * Extracts the local name and the URI of a QName value and validates it.
5727 * This one is intended to be used on attribute values that
5728 * should resolve to schema components.
5729 *
5730 * Returns 0, in case the QName is valid, a positive error code
5731 * if not valid and -1 if an internal error occurs.
5732 */
5733static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005734xmlSchemaPValAttrNodeQNameValue(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005735 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005736 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005737 xmlAttrPtr attr,
5738 const xmlChar *value,
5739 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005740 const xmlChar **local)
5741{
5742 const xmlChar *pref;
5743 xmlNsPtr ns;
5744 int len, ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005745
Daniel Veillardc0826a72004-08-10 14:17:33 +00005746 *uri = NULL;
5747 *local = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005748 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005749 if (ret > 0) {
5750 xmlSchemaPSimpleTypeErr(ctxt,
5751 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5752 ownerItem, (xmlNodePtr) attr,
5753 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
5754 NULL, value, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005755 *local = value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005756 return (ctxt->err);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005757 } else if (ret < 0)
5758 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005759
5760 if (!strchr((char *) value, ':')) {
Daniel Veillard24505b02005-07-28 23:49:35 +00005761 ns = xmlSearchNs(attr->doc, attr->parent, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005762 if (ns)
5763 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
5764 else if (schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005765 /* TODO: move XML_SCHEMAS_INCLUDING_CONVERT_NS to the
5766 * parser context. */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005767 /*
5768 * This one takes care of included schemas with no
5769 * target namespace.
5770 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005771 *uri = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005772 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005773 *local = xmlDictLookup(ctxt->dict, value, -1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005774 return (0);
5775 }
5776 /*
5777 * At this point xmlSplitQName3 has to return a local name.
5778 */
5779 *local = xmlSplitQName3(value, &len);
5780 *local = xmlDictLookup(ctxt->dict, *local, -1);
5781 pref = xmlDictLookup(ctxt->dict, value, len);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005782 ns = xmlSearchNs(attr->doc, attr->parent, pref);
5783 if (ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005784 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005785 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005786 ownerItem, (xmlNodePtr) attr,
5787 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), NULL, value,
5788 "The value '%s' of simple type 'xs:QName' has no "
5789 "corresponding namespace declaration in scope", value, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005790 return (ctxt->err);
5791 } else {
5792 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005793 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00005794 return (0);
5795}
5796
5797/**
5798 * xmlSchemaPValAttrNodeQName:
5799 * @ctxt: a schema parser context
5800 * @schema: the schema context
5801 * @ownerDes: the designation of the owner element
5802 * @ownerItem: the owner as a schema object
5803 * @attr: the attribute node
5804 * @local: the resulting local part if found, the attribute value otherwise
5805 * @uri: the resulting namespace URI if found
5806 *
5807 * Extracts and validates the QName of an attribute value.
5808 * This one is intended to be used on attribute values that
5809 * should resolve to schema components.
5810 *
5811 * Returns 0, in case the QName is valid, a positive error code
5812 * if not valid and -1 if an internal error occurs.
5813 */
5814static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005815xmlSchemaPValAttrNodeQName(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005816 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005817 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005818 xmlAttrPtr attr,
5819 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005820 const xmlChar **local)
5821{
5822 const xmlChar *value;
5823
5824 value = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005825 return (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005826 ownerItem, attr, value, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005827}
5828
5829/**
5830 * xmlSchemaPValAttrQName:
5831 * @ctxt: a schema parser context
5832 * @schema: the schema context
5833 * @ownerDes: the designation of the parent element
5834 * @ownerItem: the owner as a schema object
5835 * @ownerElem: the parent node of the attribute
5836 * @name: the name of the attribute
5837 * @local: the resulting local part if found, the attribute value otherwise
5838 * @uri: the resulting namespace URI if found
5839 *
5840 * Extracts and validates the QName of an attribute value.
5841 *
5842 * Returns 0, in case the QName is valid, a positive error code
5843 * if not valid and -1 if an internal error occurs.
5844 */
5845static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005846xmlSchemaPValAttrQName(xmlSchemaParserCtxtPtr ctxt,
5847 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005848 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005849 xmlNodePtr ownerElem,
5850 const char *name,
5851 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005852 const xmlChar **local)
5853{
5854 xmlAttrPtr attr;
5855
5856 attr = xmlSchemaGetPropNode(ownerElem, name);
5857 if (attr == NULL) {
5858 *local = NULL;
5859 *uri = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005860 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005861 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005862 return (xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005863 ownerItem, attr, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005864}
5865
5866/**
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005867 * xmlSchemaPValAttrID:
5868 * @ctxt: a schema parser context
5869 * @schema: the schema context
5870 * @ownerDes: the designation of the parent element
5871 * @ownerItem: the owner as a schema object
5872 * @ownerElem: the parent node of the attribute
5873 * @name: the name of the attribute
5874 *
5875 * Extracts and validates the ID of an attribute value.
5876 *
5877 * Returns 0, in case the ID is valid, a positive error code
5878 * if not valid and -1 if an internal error occurs.
5879 */
5880static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005881xmlSchemaPValAttrNodeID(xmlSchemaParserCtxtPtr ctxt, xmlAttrPtr attr)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005882{
5883 int ret;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005884 const xmlChar *value;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005885
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005886 if (attr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005887 return(0);
5888 value = xmlSchemaGetNodeContentNoDict((xmlNodePtr) attr);
5889 ret = xmlValidateNCName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005890 if (ret == 0) {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005891 /*
5892 * NOTE: the IDness might have already be declared in the DTD
5893 */
5894 if (attr->atype != XML_ATTRIBUTE_ID) {
5895 xmlIDPtr res;
5896 xmlChar *strip;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005897
5898 /*
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005899 * TODO: Use xmlSchemaStrip here; it's not exported at this
5900 * moment.
5901 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005902 strip = xmlSchemaCollapseString(value);
5903 if (strip != NULL) {
5904 xmlFree((xmlChar *) value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005905 value = strip;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005906 }
5907 res = xmlAddID(NULL, attr->doc, value, attr);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005908 if (res == NULL) {
5909 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005910 xmlSchemaPSimpleTypeErr(ctxt,
5911 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005912 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005913 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5914 NULL, NULL, "Duplicate value '%s' of simple "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005915 "type 'xs:ID'", value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005916 } else
5917 attr->atype = XML_ATTRIBUTE_ID;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005918 }
5919 } else if (ret > 0) {
5920 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005921 xmlSchemaPSimpleTypeErr(ctxt,
5922 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005923 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005924 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5925 NULL, NULL, "The value '%s' of simple type 'xs:ID' is "
5926 "not a valid 'xs:NCName'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005927 value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005928 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005929 if (value != NULL)
5930 xmlFree((xmlChar *)value);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005931
5932 return (ret);
5933}
5934
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005935static int
5936xmlSchemaPValAttrID(xmlSchemaParserCtxtPtr ctxt,
5937 xmlNodePtr ownerElem,
5938 const xmlChar *name)
5939{
5940 xmlAttrPtr attr;
5941
5942 attr = xmlSchemaGetPropNode(ownerElem, (const char *) name);
5943 if (attr == NULL)
5944 return(0);
5945 return(xmlSchemaPValAttrNodeID(ctxt, attr));
5946
5947}
5948
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005949/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005950 * xmlGetMaxOccurs:
5951 * @ctxt: a schema validation context
5952 * @node: a subtree containing XML Schema informations
5953 *
5954 * Get the maxOccurs property
5955 *
5956 * Returns the default if not found, or the value
5957 */
5958static int
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005959xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
5960 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005961{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005962 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00005963 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005964 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00005965
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005966 attr = xmlSchemaGetPropNode(node, "maxOccurs");
5967 if (attr == NULL)
5968 return (def);
5969 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00005970
5971 if (xmlStrEqual(val, (const xmlChar *) "unbounded")) {
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005972 if (max != UNBOUNDED) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005973 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005974 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5975 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005976 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005977 val, NULL, NULL, NULL);
5978 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005979 } else
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005980 return (UNBOUNDED); /* encoding it with -1 might be another option */
Daniel Veillard4255d502002-04-16 15:50:10 +00005981 }
5982
5983 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00005984 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005985 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005986 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005987 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005988 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5989 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005990 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005991 val, NULL, NULL, NULL);
5992 return (def);
5993 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005994 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005995 ret = ret * 10 + (*cur - '0');
5996 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00005997 }
William M. Brack76e95df2003-10-18 16:20:14 +00005998 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005999 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006000 /*
6001 * TODO: Restrict the maximal value to Integer.
6002 */
6003 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006004 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006005 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6006 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006007 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006008 val, NULL, NULL, NULL);
6009 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006010 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006011 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006012}
6013
6014/**
6015 * xmlGetMinOccurs:
6016 * @ctxt: a schema validation context
6017 * @node: a subtree containing XML Schema informations
6018 *
6019 * Get the minOccurs property
6020 *
6021 * Returns the default if not found, or the value
6022 */
6023static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006024xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006025 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006026{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006027 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00006028 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006029 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006030
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006031 attr = xmlSchemaGetPropNode(node, "minOccurs");
6032 if (attr == NULL)
6033 return (def);
6034 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00006035 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006036 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006037 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006038 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006039 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006040 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6041 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006042 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006043 val, NULL, NULL, NULL);
6044 return (def);
6045 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006046 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006047 ret = ret * 10 + (*cur - '0');
6048 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006049 }
William M. Brack76e95df2003-10-18 16:20:14 +00006050 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006051 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006052 /*
6053 * TODO: Restrict the maximal value to Integer.
6054 */
6055 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
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);
Daniel Veillard4255d502002-04-16 15:50:10 +00006062 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006063 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006064}
6065
6066/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006067 * xmlSchemaPGetBoolNodeValue:
6068 * @ctxt: a schema validation context
6069 * @ownerDes: owner designation
6070 * @ownerItem: the owner as a schema item
6071 * @node: the node holding the value
6072 *
6073 * Converts a boolean string value into 1 or 0.
6074 *
6075 * Returns 0 or 1.
6076 */
6077static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006078xmlSchemaPGetBoolNodeValue(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006079 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006080 xmlNodePtr node)
6081{
6082 xmlChar *value = NULL;
6083 int res = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006084
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006085 value = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006086 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006087 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006088 * An instance of a datatype that is defined as ·boolean·
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006089 * can have the following legal literals {true, false, 1, 0}.
6090 */
6091 if (xmlStrEqual(BAD_CAST value, BAD_CAST "true"))
6092 res = 1;
6093 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "false"))
6094 res = 0;
6095 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "1"))
6096 res = 1;
6097 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006098 res = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006099 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006100 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006101 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006102 ownerItem, node,
6103 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6104 NULL, BAD_CAST value,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006105 NULL, NULL, NULL);
6106 }
6107 if (value != NULL)
6108 xmlFree(value);
6109 return (res);
6110}
6111
6112/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006113 * xmlGetBooleanProp:
6114 * @ctxt: a schema validation context
6115 * @node: a subtree containing XML Schema informations
6116 * @name: the attribute name
6117 * @def: the default value
6118 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006119 * Evaluate if a boolean property is set
Daniel Veillard4255d502002-04-16 15:50:10 +00006120 *
6121 * Returns the default if not found, 0 if found to be false,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006122 * 1 if found to be true
Daniel Veillard4255d502002-04-16 15:50:10 +00006123 */
6124static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006125xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006126 xmlNodePtr node,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006127 const char *name, int def)
6128{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006129 const xmlChar *val;
Daniel Veillard4255d502002-04-16 15:50:10 +00006130
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006131 val = xmlSchemaGetProp(ctxt, node, name);
Daniel Veillard4255d502002-04-16 15:50:10 +00006132 if (val == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006133 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006134 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006135 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006136 * An instance of a datatype that is defined as ·boolean·
Daniel Veillardc0826a72004-08-10 14:17:33 +00006137 * can have the following legal literals {true, false, 1, 0}.
6138 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006139 if (xmlStrEqual(val, BAD_CAST "true"))
6140 def = 1;
6141 else if (xmlStrEqual(val, BAD_CAST "false"))
6142 def = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006143 else if (xmlStrEqual(val, BAD_CAST "1"))
6144 def = 1;
6145 else if (xmlStrEqual(val, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006146 def = 0;
6147 else {
6148 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006149 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006150 NULL,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00006151 (xmlNodePtr) xmlSchemaGetPropNode(node, name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006152 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6153 NULL, val, NULL, NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006154 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006155 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006156}
6157
6158/************************************************************************
6159 * *
6160 * Shema extraction from an Infoset *
6161 * *
6162 ************************************************************************/
6163static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr
6164 ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00006165 xmlNodePtr node,
6166 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006167static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr
6168 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006169 xmlSchemaPtr schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006170 xmlNodePtr node,
Daniel Veillard3646d642004-06-02 19:19:14 +00006171 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006172static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr
6173 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006174 xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006175 xmlNodePtr node,
6176 xmlSchemaTypeType parentType);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006177static xmlSchemaBasicItemPtr
6178xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
6179 xmlSchemaPtr schema,
6180 xmlNodePtr node,
6181 xmlSchemaItemListPtr uses,
6182 int parentType);
Daniel Veillard4255d502002-04-16 15:50:10 +00006183static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt,
6184 xmlSchemaPtr schema,
6185 xmlNodePtr node);
Daniel Veillard3646d642004-06-02 19:19:14 +00006186static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006187xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6188 xmlSchemaPtr schema, xmlNodePtr node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006189
6190/**
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006191 * xmlSchemaPValAttrNodeValue:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006192 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00006193 * @ctxt: a schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00006194 * @ownerDes: the designation of the parent element
6195 * @ownerItem: the schema object owner if existent
6196 * @attr: the schema attribute node being validated
6197 * @value: the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006198 * @type: the built-in type to be validated against
Daniel Veillard01fa6152004-06-29 17:04:39 +00006199 *
6200 * Validates a value against the given built-in type.
6201 * This one is intended to be used internally for validation
6202 * of schema attribute values during parsing of the schema.
6203 *
6204 * Returns 0 if the value is valid, a positive error code
6205 * number otherwise and -1 in case of an internal or API error.
6206 */
6207static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006208xmlSchemaPValAttrNodeValue(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006209 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006210 xmlAttrPtr attr,
6211 const xmlChar *value,
6212 xmlSchemaTypePtr type)
Daniel Veillard01fa6152004-06-29 17:04:39 +00006213{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006214
6215 int ret = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006216
6217 /*
6218 * NOTE: Should we move this to xmlschematypes.c? Hmm, but this
6219 * one is really meant to be used internally, so better not.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006220 */
6221 if ((pctxt == NULL) || (type == NULL) || (attr == NULL))
Daniel Veillard01fa6152004-06-29 17:04:39 +00006222 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006223 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6224 PERROR_INT("xmlSchemaPValAttrNodeValue",
6225 "the given type is not a built-in type");
6226 return (-1);
6227 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006228 switch (type->builtInType) {
6229 case XML_SCHEMAS_NCNAME:
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006230 case XML_SCHEMAS_QNAME:
6231 case XML_SCHEMAS_ANYURI:
6232 case XML_SCHEMAS_TOKEN:
6233 case XML_SCHEMAS_LANGUAGE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006234 ret = xmlSchemaValPredefTypeNode(type, value, NULL,
6235 (xmlNodePtr) attr);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006236 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006237 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006238 PERROR_INT("xmlSchemaPValAttrNodeValue",
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00006239 "validation using the given type is not supported while "
6240 "parsing a schema");
Daniel Veillard01fa6152004-06-29 17:04:39 +00006241 return (-1);
6242 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006243 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006244 /*
6245 * TODO: Should we use the S4S error codes instead?
6246 */
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006247 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006248 PERROR_INT("xmlSchemaPValAttrNodeValue",
6249 "failed to validate a schema attribute value");
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006250 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006251 } else if (ret > 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006252 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006253 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
6254 else
6255 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
6256 xmlSchemaPSimpleTypeErr(pctxt,
6257 ret, ownerItem, (xmlNodePtr) attr,
6258 type, NULL, value, NULL, NULL, NULL);
6259 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006260 return (ret);
6261}
6262
6263/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006264 * xmlSchemaPValAttrNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006265 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006266 * @ctxt: a schema parser context
6267 * @ownerDes: the designation of the parent element
6268 * @ownerItem: the schema object owner if existent
6269 * @attr: the schema attribute node being validated
6270 * @type: the built-in type to be validated against
6271 * @value: the resulting value if any
6272 *
6273 * Extracts and validates a value against the given built-in type.
6274 * This one is intended to be used internally for validation
6275 * of schema attribute values during parsing of the schema.
6276 *
6277 * Returns 0 if the value is valid, a positive error code
6278 * number otherwise and -1 in case of an internal or API error.
6279 */
6280static int
6281xmlSchemaPValAttrNode(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006282 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006283 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006284 xmlSchemaTypePtr type,
6285 const xmlChar **value)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006286{
Daniel Veillardc0826a72004-08-10 14:17:33 +00006287 const xmlChar *val;
6288
6289 if ((ctxt == NULL) || (type == NULL) || (attr == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006290 return (-1);
6291
Daniel Veillardc0826a72004-08-10 14:17:33 +00006292 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
6293 if (value != NULL)
6294 *value = val;
6295
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006296 return (xmlSchemaPValAttrNodeValue(ctxt, ownerItem, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006297 val, type));
Daniel Veillardc0826a72004-08-10 14:17:33 +00006298}
6299
6300/**
6301 * xmlSchemaPValAttr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006302 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006303 * @ctxt: a schema parser context
6304 * @node: the element node of the attribute
6305 * @ownerDes: the designation of the parent element
6306 * @ownerItem: the schema object owner if existent
6307 * @ownerElem: the owner element node
6308 * @name: the name of the schema attribute node
6309 * @type: the built-in type to be validated against
6310 * @value: the resulting value if any
6311 *
6312 * Extracts and validates a value against the given built-in type.
6313 * This one is intended to be used internally for validation
6314 * of schema attribute values during parsing of the schema.
6315 *
6316 * Returns 0 if the value is valid, a positive error code
6317 * number otherwise and -1 in case of an internal or API error.
6318 */
6319static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006320xmlSchemaPValAttr(xmlSchemaParserCtxtPtr ctxt,
6321 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006322 xmlNodePtr ownerElem,
6323 const char *name,
6324 xmlSchemaTypePtr type,
6325 const xmlChar **value)
6326{
6327 xmlAttrPtr attr;
6328
6329 if ((ctxt == NULL) || (type == NULL)) {
6330 if (value != NULL)
6331 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006332 return (-1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006333 }
6334 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6335 if (value != NULL)
6336 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006337 xmlSchemaPErr(ctxt, ownerElem,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00006338 XML_SCHEMAP_INTERNAL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006339 "Internal error: xmlSchemaPValAttr, the given "
6340 "type '%s' is not a built-in type.\n",
6341 type->name, NULL);
6342 return (-1);
6343 }
6344 attr = xmlSchemaGetPropNode(ownerElem, name);
6345 if (attr == NULL) {
6346 if (value != NULL)
6347 *value = NULL;
6348 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006349 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006350 return (xmlSchemaPValAttrNode(ctxt, ownerItem, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006351 type, value));
6352}
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006353
6354static int
6355xmlSchemaCheckReference(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006356 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006357 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006358 xmlAttrPtr attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006359 const xmlChar *namespaceName)
6360{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006361 /* TODO: Pointer comparison instead? */
6362 if (xmlStrEqual(pctxt->targetNamespace, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006363 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006364 if (xmlStrEqual(xmlSchemaNs, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006365 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006366 /*
6367 * Check if the referenced namespace was <import>ed.
6368 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006369 if (WXS_BUCKET(pctxt)->relations != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006370 xmlSchemaSchemaRelationPtr rel;
6371
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006372 rel = WXS_BUCKET(pctxt)->relations;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006373 do {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006374 if (WXS_IS_BUCKET_IMPMAIN(rel->type) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006375 xmlStrEqual(namespaceName, rel->importNamespace))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006376 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006377 rel = rel->next;
6378 } while (rel != NULL);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006379 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006380 /*
6381 * No matching <import>ed namespace found.
6382 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006383 {
6384 xmlNodePtr n = (attr != NULL) ? (xmlNodePtr) attr : node;
6385
6386 if (namespaceName == NULL)
6387 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6388 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6389 "References from this schema to components in no "
6390 "namespace are not allowed, since not indicated by an "
6391 "import statement", NULL, NULL);
6392 else
6393 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6394 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6395 "References from this schema to components in the "
6396 "namespace '%s' are not allowed, since not indicated by an "
6397 "import statement", namespaceName, NULL);
6398 }
6399 return (XML_SCHEMAP_SRC_RESOLVE);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006400}
6401
Daniel Veillardc0826a72004-08-10 14:17:33 +00006402/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006403 * xmlSchemaParseLocalAttributes:
Daniel Veillard4255d502002-04-16 15:50:10 +00006404 * @ctxt: a schema validation context
6405 * @schema: the schema being built
6406 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006407 * @type: the hosting type where the attributes will be anchored
Daniel Veillard4255d502002-04-16 15:50:10 +00006408 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006409 * Parses attribute uses and attribute declarations and
6410 * attribute group references.
Daniel Veillard4255d502002-04-16 15:50:10 +00006411 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006412static int
6413xmlSchemaParseLocalAttributes(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6414 xmlNodePtr *child, xmlSchemaItemListPtr *list,
6415 int parentType, int *hasRefs)
Daniel Veillard4255d502002-04-16 15:50:10 +00006416{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006417 void *item;
Daniel Veillard4255d502002-04-16 15:50:10 +00006418
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006419 while ((IS_SCHEMA((*child), "attribute")) ||
6420 (IS_SCHEMA((*child), "attributeGroup"))) {
6421 if (IS_SCHEMA((*child), "attribute")) {
6422 item = xmlSchemaParseLocalAttribute(ctxt, schema, *child,
6423 *list, parentType);
6424 } else {
6425 item = xmlSchemaParseAttributeGroupRef(ctxt, schema, *child);
6426 if ((item != NULL) && (hasRefs != NULL))
6427 *hasRefs = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006428 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006429 if (item != NULL) {
6430 if (*list == NULL) {
6431 /* TODO: Customize grow factor. */
6432 *list = xmlSchemaItemListCreate();
6433 if (*list == NULL)
6434 return(-1);
6435 }
6436 if (xmlSchemaItemListAddSize(*list, 2, item) == -1)
6437 return(-1);
6438 }
6439 *child = (*child)->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006440 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006441 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00006442}
6443
6444/**
6445 * xmlSchemaParseAnnotation:
6446 * @ctxt: a schema validation context
6447 * @schema: the schema being built
6448 * @node: a subtree containing XML Schema informations
6449 *
6450 * parse a XML schema Attrribute declaration
6451 * *WARNING* this interface is highly subject to change
6452 *
William M. Bracke7091952004-05-11 15:09:58 +00006453 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00006454 * 1 in case of success.
6455 */
6456static xmlSchemaAnnotPtr
6457xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006458 xmlNodePtr node, int needed)
Daniel Veillard4255d502002-04-16 15:50:10 +00006459{
6460 xmlSchemaAnnotPtr ret;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006461 xmlNodePtr child = NULL;
6462 xmlAttrPtr attr;
6463 int barked = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00006464
Daniel Veillardc0826a72004-08-10 14:17:33 +00006465 /*
6466 * INFO: S4S completed.
6467 */
6468 /*
6469 * id = ID
6470 * {any attributes with non-schema namespace . . .}>
6471 * Content: (appinfo | documentation)*
6472 */
Daniel Veillard4255d502002-04-16 15:50:10 +00006473 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6474 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006475 if (needed)
6476 ret = xmlSchemaNewAnnot(ctxt, node);
6477 else
6478 ret = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006479 attr = node->properties;
6480 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006481 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006482 (!xmlStrEqual(attr->name, BAD_CAST "id"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006483 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006484 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006485
6486 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006487 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006488 }
6489 attr = attr->next;
6490 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006491 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006492 /*
6493 * And now for the children...
6494 */
6495 child = node->children;
6496 while (child != NULL) {
6497 if (IS_SCHEMA(child, "appinfo")) {
6498 /* TODO: make available the content of "appinfo". */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006499 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006500 * source = anyURI
6501 * {any attributes with non-schema namespace . . .}>
6502 * Content: ({any})*
6503 */
6504 attr = child->properties;
6505 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006506 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006507 (!xmlStrEqual(attr->name, BAD_CAST "source"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006508 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006509 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006510
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006511 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006512 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006513 }
6514 attr = attr->next;
6515 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006516 xmlSchemaPValAttr(ctxt, NULL, child, "source",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006517 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006518 child = child->next;
6519 } else if (IS_SCHEMA(child, "documentation")) {
6520 /* TODO: make available the content of "documentation". */
6521 /*
6522 * source = anyURI
6523 * {any attributes with non-schema namespace . . .}>
6524 * Content: ({any})*
6525 */
6526 attr = child->properties;
6527 while (attr != NULL) {
6528 if (attr->ns == NULL) {
6529 if (!xmlStrEqual(attr->name, BAD_CAST "source")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006530 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006531 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006532 }
6533 } else {
6534 if (xmlStrEqual(attr->ns->href, xmlSchemaNs) ||
6535 (xmlStrEqual(attr->name, BAD_CAST "lang") &&
6536 (!xmlStrEqual(attr->ns->href, XML_XML_NAMESPACE)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006537
6538 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006539 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006540 }
6541 }
6542 attr = attr->next;
6543 }
6544 /*
6545 * Attribute "xml:lang".
6546 */
6547 attr = xmlSchemaGetPropNodeNs(child, (const char *) XML_XML_NAMESPACE, "lang");
6548 if (attr != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006549 xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006550 xmlSchemaGetBuiltInType(XML_SCHEMAS_LANGUAGE), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006551 child = child->next;
6552 } else {
6553 if (!barked)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006554 xmlSchemaPContentErr(ctxt,
6555 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006556 NULL, node, child, NULL, "(appinfo | documentation)*");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006557 barked = 1;
6558 child = child->next;
6559 }
6560 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006561
Daniel Veillard4255d502002-04-16 15:50:10 +00006562 return (ret);
6563}
6564
6565/**
6566 * xmlSchemaParseFacet:
6567 * @ctxt: a schema validation context
6568 * @schema: the schema being built
6569 * @node: a subtree containing XML Schema informations
6570 *
6571 * parse a XML schema Facet declaration
6572 * *WARNING* this interface is highly subject to change
6573 *
6574 * Returns the new type structure or NULL in case of error
6575 */
6576static xmlSchemaFacetPtr
6577xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006578 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006579{
6580 xmlSchemaFacetPtr facet;
6581 xmlNodePtr child = NULL;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006582 const xmlChar *value;
Daniel Veillard4255d502002-04-16 15:50:10 +00006583
6584 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6585 return (NULL);
6586
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00006587 facet = xmlSchemaNewFacet();
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006588 if (facet == NULL) {
6589 xmlSchemaPErrMemory(ctxt, "allocating facet", node);
6590 return (NULL);
6591 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006592 facet->node = node;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006593 value = xmlSchemaGetProp(ctxt, node, "value");
Daniel Veillard4255d502002-04-16 15:50:10 +00006594 if (value == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006595 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE,
6596 "Facet %s has no value\n", node->name, NULL);
6597 xmlSchemaFreeFacet(facet);
Daniel Veillard4255d502002-04-16 15:50:10 +00006598 return (NULL);
6599 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006600 if (IS_SCHEMA(node, "minInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006601 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006602 } else if (IS_SCHEMA(node, "minExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006603 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006604 } else if (IS_SCHEMA(node, "maxInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006605 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006606 } else if (IS_SCHEMA(node, "maxExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006607 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006608 } else if (IS_SCHEMA(node, "totalDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006609 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006610 } else if (IS_SCHEMA(node, "fractionDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006611 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006612 } else if (IS_SCHEMA(node, "pattern")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006613 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006614 } else if (IS_SCHEMA(node, "enumeration")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006615 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006616 } else if (IS_SCHEMA(node, "whiteSpace")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006617 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006618 } else if (IS_SCHEMA(node, "length")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006619 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006620 } else if (IS_SCHEMA(node, "maxLength")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006621 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
6622 } else if (IS_SCHEMA(node, "minLength")) {
6623 facet->type = XML_SCHEMA_FACET_MINLENGTH;
6624 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006625 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE,
6626 "Unknown facet type %s\n", node->name, NULL);
6627 xmlSchemaFreeFacet(facet);
6628 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006629 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006630 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +00006631 facet->value = value;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006632 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
6633 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
6634 const xmlChar *fixed;
6635
6636 fixed = xmlSchemaGetProp(ctxt, node, "fixed");
6637 if (fixed != NULL) {
6638 if (xmlStrEqual(fixed, BAD_CAST "true"))
6639 facet->fixed = 1;
6640 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006641 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006642 child = node->children;
6643
6644 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006645 facet->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006646 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006647 }
6648 if (child != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006649 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD,
6650 "Facet %s has unexpected child content\n",
6651 node->name, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006652 }
6653 return (facet);
6654}
6655
6656/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006657 * xmlSchemaParseWildcardNs:
6658 * @ctxt: a schema parser context
6659 * @wildc: the wildcard, already created
6660 * @node: a subtree containing XML Schema informations
6661 *
6662 * Parses the attribute "processContents" and "namespace"
6663 * of a xsd:anyAttribute and xsd:any.
6664 * *WARNING* this interface is highly subject to change
6665 *
6666 * Returns 0 if everything goes fine, a positive error code
6667 * if something is not valid and -1 if an internal error occurs.
6668 */
6669static int
6670xmlSchemaParseWildcardNs(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006671 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006672 xmlSchemaWildcardPtr wildc,
6673 xmlNodePtr node)
6674{
6675 const xmlChar *pc, *ns, *dictnsItem;
6676 int ret = 0;
6677 xmlChar *nsItem;
6678 xmlSchemaWildcardNsPtr tmp, lastNs = NULL;
6679 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006680
Daniel Veillardc0826a72004-08-10 14:17:33 +00006681 pc = xmlSchemaGetProp(ctxt, node, "processContents");
6682 if ((pc == NULL)
6683 || (xmlStrEqual(pc, (const xmlChar *) "strict"))) {
6684 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
6685 } else if (xmlStrEqual(pc, (const xmlChar *) "skip")) {
6686 wildc->processContents = XML_SCHEMAS_ANY_SKIP;
6687 } else if (xmlStrEqual(pc, (const xmlChar *) "lax")) {
6688 wildc->processContents = XML_SCHEMAS_ANY_LAX;
6689 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006690 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006691 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006692 NULL, node,
6693 NULL, "(strict | skip | lax)", pc,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006694 NULL, NULL, NULL);
6695 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006696 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006697 }
6698 /*
6699 * Build the namespace constraints.
6700 */
6701 attr = xmlSchemaGetPropNode(node, "namespace");
6702 ns = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00006703 if ((attr == NULL) || (xmlStrEqual(ns, BAD_CAST "##any")))
Daniel Veillardc0826a72004-08-10 14:17:33 +00006704 wildc->any = 1;
6705 else if (xmlStrEqual(ns, BAD_CAST "##other")) {
6706 wildc->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006707 if (wildc->negNsSet == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006708 return (-1);
6709 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006710 wildc->negNsSet->value = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006711 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006712 const xmlChar *end, *cur;
6713
6714 cur = ns;
6715 do {
6716 while (IS_BLANK_CH(*cur))
6717 cur++;
6718 end = cur;
6719 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
6720 end++;
6721 if (end == cur)
6722 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006723 nsItem = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006724 if ((xmlStrEqual(nsItem, BAD_CAST "##other")) ||
6725 (xmlStrEqual(nsItem, BAD_CAST "##any"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006726 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006727 XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006728 NULL, (xmlNodePtr) attr,
6729 NULL,
6730 "((##any | ##other) | List of (xs:anyURI | "
6731 "(##targetNamespace | ##local)))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00006732 nsItem, NULL, NULL, NULL);
6733 ret = XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER;
6734 } else {
6735 if (xmlStrEqual(nsItem, BAD_CAST "##targetNamespace")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006736 dictnsItem = ctxt->targetNamespace;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006737 } else if (xmlStrEqual(nsItem, BAD_CAST "##local")) {
6738 dictnsItem = NULL;
6739 } else {
6740 /*
6741 * Validate the item (anyURI).
6742 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006743 xmlSchemaPValAttrNodeValue(ctxt, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006744 nsItem, xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI));
6745 dictnsItem = xmlDictLookup(ctxt->dict, nsItem, -1);
6746 }
6747 /*
6748 * Avoid dublicate namespaces.
6749 */
6750 tmp = wildc->nsSet;
6751 while (tmp != NULL) {
6752 if (dictnsItem == tmp->value)
6753 break;
6754 tmp = tmp->next;
6755 }
6756 if (tmp == NULL) {
6757 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
6758 if (tmp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006759 xmlFree(nsItem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006760 return (-1);
6761 }
6762 tmp->value = dictnsItem;
6763 tmp->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006764 if (wildc->nsSet == NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006765 wildc->nsSet = tmp;
6766 else
6767 lastNs->next = tmp;
6768 lastNs = tmp;
6769 }
6770
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006771 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006772 xmlFree(nsItem);
6773 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006774 } while (*cur != 0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006775 }
6776 return (ret);
6777}
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006778
6779static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006780xmlSchemaPCheckParticleCorrect_2(xmlSchemaParserCtxtPtr ctxt,
6781 xmlSchemaParticlePtr item ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006782 xmlNodePtr node,
6783 int minOccurs,
6784 int maxOccurs) {
6785
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006786 if ((maxOccurs == 0) && ( minOccurs == 0))
6787 return (0);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006788 if (maxOccurs != UNBOUNDED) {
6789 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006790 * TODO: Maybe we should better not create the particle,
6791 * if min/max is invalid, since it could confuse the build of the
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006792 * content model.
6793 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006794 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006795 * 3.9.6 Schema Component Constraint: Particle Correct
6796 *
6797 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006798 if (maxOccurs < 1) {
6799 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006800 * 2.2 {max occurs} must be greater than or equal to 1.
6801 */
6802 xmlSchemaPCustomAttrErr(ctxt,
6803 XML_SCHEMAP_P_PROPS_CORRECT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006804 NULL, NULL,
6805 xmlSchemaGetPropNode(node, "maxOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006806 "The value must be greater than or equal to 1");
6807 return (XML_SCHEMAP_P_PROPS_CORRECT_2_2);
6808 } else if (minOccurs > maxOccurs) {
6809 /*
6810 * 2.1 {min occurs} must not be greater than {max occurs}.
6811 */
6812 xmlSchemaPCustomAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006813 XML_SCHEMAP_P_PROPS_CORRECT_2_1,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006814 NULL, NULL,
6815 xmlSchemaGetPropNode(node, "minOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006816 "The value must not be greater than the value of 'maxOccurs'");
6817 return (XML_SCHEMAP_P_PROPS_CORRECT_2_1);
6818 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006819 }
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006820 return (0);
6821}
6822
Daniel Veillardc0826a72004-08-10 14:17:33 +00006823/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006824 * xmlSchemaParseAny:
6825 * @ctxt: a schema validation context
6826 * @schema: the schema being built
6827 * @node: a subtree containing XML Schema informations
6828 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006829 * Parsea a XML schema <any> element. A particle and wildcard
6830 * will be created (except if minOccurs==maxOccurs==0, in this case
6831 * nothing will be created).
Daniel Veillard4255d502002-04-16 15:50:10 +00006832 * *WARNING* this interface is highly subject to change
6833 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006834 * Returns the particle or NULL in case of error or if minOccurs==maxOccurs==0
Daniel Veillard4255d502002-04-16 15:50:10 +00006835 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006836static xmlSchemaParticlePtr
Daniel Veillard4255d502002-04-16 15:50:10 +00006837xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6838 xmlNodePtr node)
6839{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006840 xmlSchemaParticlePtr particle;
Daniel Veillard4255d502002-04-16 15:50:10 +00006841 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006842 xmlSchemaWildcardPtr wild;
6843 int min, max;
6844 xmlAttrPtr attr;
6845 xmlSchemaAnnotPtr annot = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00006846
6847 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6848 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006849 /*
6850 * Check for illegal attributes.
6851 */
6852 attr = node->properties;
6853 while (attr != NULL) {
6854 if (attr->ns == NULL) {
6855 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6856 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
6857 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
6858 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6859 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006860 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006861 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006862 }
6863 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006864 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006865 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006866 }
6867 attr = attr->next;
6868 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006869 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006870 /*
6871 * minOccurs/maxOccurs.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006872 */
6873 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
6874 "(xs:nonNegativeInteger | unbounded)");
6875 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1,
6876 "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006877 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
6878 /*
6879 * Create & parse the wildcard.
6880 */
6881 wild = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY, node);
6882 if (wild == NULL)
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006883 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006884 xmlSchemaParseWildcardNs(ctxt, schema, wild, node);
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006885 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006886 * And now for the children...
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006887 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006888 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00006889 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006890 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006891 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006892 }
6893 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006894 xmlSchemaPContentErr(ctxt,
6895 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006896 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006897 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006898 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006899 /*
6900 * No component if minOccurs==maxOccurs==0.
6901 */
6902 if ((min == 0) && (max == 0)) {
6903 /* Don't free the wildcard, since it's already on the list. */
6904 return (NULL);
6905 }
6906 /*
6907 * Create the particle.
6908 */
6909 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
6910 if (particle == NULL)
6911 return (NULL);
6912 particle->annot = annot;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006913 particle->children = (xmlSchemaTreeItemPtr) wild;
Daniel Veillard4255d502002-04-16 15:50:10 +00006914
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006915 return (particle);
Daniel Veillard4255d502002-04-16 15:50:10 +00006916}
6917
6918/**
6919 * xmlSchemaParseNotation:
6920 * @ctxt: a schema validation context
6921 * @schema: the schema being built
6922 * @node: a subtree containing XML Schema informations
6923 *
6924 * parse a XML schema Notation declaration
6925 *
6926 * Returns the new structure or NULL in case of error
6927 */
6928static xmlSchemaNotationPtr
6929xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006930 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006931{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006932 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00006933 xmlSchemaNotationPtr ret;
6934 xmlNodePtr child = NULL;
6935
6936 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6937 return (NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006938 name = xmlSchemaGetProp(ctxt, node, "name");
Daniel Veillard4255d502002-04-16 15:50:10 +00006939 if (name == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006940 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME,
6941 "Notation has no name\n", NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006942 return (NULL);
6943 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006944 ret = xmlSchemaAddNotation(ctxt, schema, name,
6945 ctxt->targetNamespace, node);
6946 if (ret == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00006947 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006948 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006949
6950 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006951 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006952 child = child->next;
6953 }
6954
Daniel Veillard4255d502002-04-16 15:50:10 +00006955 child = node->children;
6956 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006957 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006958 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006959 }
6960 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006961 xmlSchemaPContentErr(ctxt,
6962 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006963 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006964 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006965 }
6966
6967 return (ret);
6968}
6969
6970/**
6971 * xmlSchemaParseAnyAttribute:
6972 * @ctxt: a schema validation context
6973 * @schema: the schema being built
6974 * @node: a subtree containing XML Schema informations
6975 *
6976 * parse a XML schema AnyAttrribute declaration
6977 * *WARNING* this interface is highly subject to change
6978 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006979 * Returns a wildcard or NULL.
Daniel Veillard4255d502002-04-16 15:50:10 +00006980 */
Daniel Veillard3646d642004-06-02 19:19:14 +00006981static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006982xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6983 xmlSchemaPtr schema, xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006984{
Daniel Veillard3646d642004-06-02 19:19:14 +00006985 xmlSchemaWildcardPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00006986 xmlNodePtr child = NULL;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006987 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006988
6989 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6990 return (NULL);
6991
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006992 ret = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
6993 node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006994 if (ret == NULL) {
6995 return (NULL);
6996 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006997 /*
6998 * Check for illegal attributes.
6999 */
7000 attr = node->properties;
7001 while (attr != NULL) {
7002 if (attr->ns == NULL) {
7003 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7004 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
7005 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007006 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007007 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007008 }
7009 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007010 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007011 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007012 }
7013 attr = attr->next;
7014 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007015 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007016 /*
7017 * Parse the namespace list.
7018 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007019 if (xmlSchemaParseWildcardNs(ctxt, schema, ret, node) != 0)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007020 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007021 /*
7022 * And now for the children...
7023 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007024 child = node->children;
7025 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007026 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007027 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007028 }
7029 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00007030 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007031 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007032 NULL, node, child,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007033 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00007034 }
7035
7036 return (ret);
7037}
7038
7039
7040/**
7041 * xmlSchemaParseAttribute:
7042 * @ctxt: a schema validation context
7043 * @schema: the schema being built
7044 * @node: a subtree containing XML Schema informations
7045 *
7046 * parse a XML schema Attrribute declaration
7047 * *WARNING* this interface is highly subject to change
7048 *
William M. Bracke7091952004-05-11 15:09:58 +00007049 * Returns the attribute declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00007050 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007051static xmlSchemaBasicItemPtr
7052xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
7053 xmlSchemaPtr schema,
7054 xmlNodePtr node,
7055 xmlSchemaItemListPtr uses,
7056 int parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +00007057{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007058 const xmlChar *attrValue, *name = NULL, *ns = NULL;
7059 xmlSchemaAttributeUsePtr use = NULL;
7060 xmlNodePtr child = NULL;
7061 xmlAttrPtr attr;
7062 const xmlChar *tmpNs = NULL, *tmpName = NULL, *defValue = NULL;
7063 int isRef = 0, occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7064 int nberrors, hasForm = 0, defValueType = 0;
7065
7066#define WXS_ATTR_DEF_VAL_DEFAULT 1
7067#define WXS_ATTR_DEF_VAL_FIXED 2
7068
7069 /*
7070 * 3.2.3 Constraints on XML Representations of Attribute Declarations
7071 */
7072
7073 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7074 return (NULL);
7075 attr = xmlSchemaGetPropNode(node, "ref");
7076 if (attr != NULL) {
7077 if (xmlSchemaPValAttrNodeQName(pctxt, schema,
7078 NULL, attr, &tmpNs, &tmpName) != 0) {
7079 return (NULL);
7080 }
7081 if (xmlSchemaCheckReference(pctxt, schema, node, attr, tmpNs) != 0)
7082 return(NULL);
7083 isRef = 1;
7084 }
7085 nberrors = pctxt->nberrors;
7086 /*
7087 * Check for illegal attributes.
7088 */
7089 attr = node->properties;
7090 while (attr != NULL) {
7091 if (attr->ns == NULL) {
7092 if (isRef) {
7093 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7094 xmlSchemaPValAttrNodeID(pctxt, attr);
7095 goto attr_next;
7096 } else if (xmlStrEqual(attr->name, BAD_CAST "ref")) {
7097 goto attr_next;
7098 }
7099 } else {
7100 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
7101 goto attr_next;
7102 } else if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7103 xmlSchemaPValAttrNodeID(pctxt, attr);
7104 goto attr_next;
7105 } else if (xmlStrEqual(attr->name, BAD_CAST "type")) {
7106 xmlSchemaPValAttrNodeQName(pctxt, schema, NULL,
7107 attr, &tmpNs, &tmpName);
7108 goto attr_next;
7109 } else if (xmlStrEqual(attr->name, BAD_CAST "form")) {
7110 /*
7111 * Evaluate the target namespace
7112 */
7113 hasForm = 1;
7114 attrValue = xmlSchemaGetNodeContent(pctxt,
7115 (xmlNodePtr) attr);
7116 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
7117 ns = pctxt->targetNamespace;
7118 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified"))
7119 {
7120 xmlSchemaPSimpleTypeErr(pctxt,
7121 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
7122 NULL, (xmlNodePtr) attr,
7123 NULL, "(qualified | unqualified)",
7124 attrValue, NULL, NULL, NULL);
7125 }
7126 goto attr_next;
7127 }
7128 }
7129 if (xmlStrEqual(attr->name, BAD_CAST "use")) {
7130
7131 attrValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7132 /* TODO: Maybe we need to normalize the value beforehand. */
7133 if (xmlStrEqual(attrValue, BAD_CAST "optional"))
7134 occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7135 else if (xmlStrEqual(attrValue, BAD_CAST "prohibited"))
7136 occurs = XML_SCHEMAS_ATTR_USE_PROHIBITED;
7137 else if (xmlStrEqual(attrValue, BAD_CAST "required"))
7138 occurs = XML_SCHEMAS_ATTR_USE_REQUIRED;
7139 else {
7140 xmlSchemaPSimpleTypeErr(pctxt,
7141 XML_SCHEMAP_INVALID_ATTR_USE,
7142 NULL, (xmlNodePtr) attr,
7143 NULL, "(optional | prohibited | required)",
7144 attrValue, NULL, NULL, NULL);
7145 }
7146 goto attr_next;
7147 } else if (xmlStrEqual(attr->name, BAD_CAST "default")) {
7148 /*
7149 * 3.2.3 : 1
7150 * default and fixed must not both be present.
7151 */
7152 if (defValue) {
7153 xmlSchemaPMutualExclAttrErr(pctxt,
7154 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7155 NULL, attr, "default", "fixed");
7156 } else {
7157 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7158 defValueType = WXS_ATTR_DEF_VAL_DEFAULT;
7159 }
7160 goto attr_next;
7161 } else if (xmlStrEqual(attr->name, BAD_CAST "fixed")) {
7162 /*
7163 * 3.2.3 : 1
7164 * default and fixed must not both be present.
7165 */
7166 if (defValue) {
7167 xmlSchemaPMutualExclAttrErr(pctxt,
7168 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7169 NULL, attr, "default", "fixed");
7170 } else {
7171 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7172 defValueType = WXS_ATTR_DEF_VAL_FIXED;
7173 }
7174 goto attr_next;
7175 }
7176 } else if (! xmlStrEqual(attr->ns->href, xmlSchemaNs))
7177 goto attr_next;
7178
7179 xmlSchemaPIllegalAttrErr(pctxt,
7180 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7181
7182attr_next:
7183 attr = attr->next;
7184 }
7185 /*
7186 * 3.2.3 : 2
7187 * If default and use are both present, use must have
7188 * the actual value optional.
7189 */
7190 if ((defValueType == WXS_ATTR_DEF_VAL_DEFAULT) &&
7191 (occurs != XML_SCHEMAS_ATTR_USE_OPTIONAL)) {
7192 xmlSchemaPSimpleTypeErr(pctxt,
7193 XML_SCHEMAP_SRC_ATTRIBUTE_2,
7194 NULL, node, NULL,
7195 "(optional | prohibited | required)", NULL,
7196 "The value of the attribute 'use' must be 'optional' "
7197 "if the attribute 'default' is present",
7198 NULL, NULL);
7199 }
7200 /*
7201 * We want correct attributes.
7202 */
7203 if (nberrors != pctxt->nberrors)
7204 return(NULL);
7205 if (! isRef) {
7206 xmlSchemaAttributePtr attrDecl;
7207
7208 /* TODO: move XML_SCHEMAS_QUALIF_ATTR to the parser. */
7209 if ((! hasForm) && (schema->flags & XML_SCHEMAS_QUALIF_ATTR))
7210 ns = pctxt->targetNamespace;
7211 /*
7212 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7213 * TODO: Move this to the component layer.
7214 */
7215 if (xmlStrEqual(ns, xmlSchemaInstanceNs)) {
7216 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7217 XML_SCHEMAP_NO_XSI,
7218 node, NULL,
7219 "The target namespace must not match '%s'",
7220 xmlSchemaInstanceNs, NULL);
7221 }
7222 attr = xmlSchemaGetPropNode(node, "name");
7223 if (attr == NULL) {
7224 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7225 NULL, node, "name", NULL);
7226 return (NULL);
7227 }
7228 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7229 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7230 return (NULL);
7231 }
7232 /*
7233 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7234 * TODO: Move this to the component layer.
7235 */
7236 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
7237 xmlSchemaPSimpleTypeErr(pctxt,
7238 XML_SCHEMAP_NO_XMLNS,
7239 NULL, (xmlNodePtr) attr,
7240 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7241 "The value of the attribute must not match 'xmlns'",
7242 NULL, NULL);
7243 return (NULL);
7244 }
7245 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED)
7246 goto check_children;
7247 /*
7248 * Create the attribute use component.
7249 */
7250 use = xmlSchemaAddAttributeUse(pctxt, node);
7251 if (use == NULL)
7252 return(NULL);
7253 use->occurs = occurs;
7254 /*
7255 * Create the attribute declaration.
7256 */
7257 attrDecl = xmlSchemaAddAttribute(pctxt, schema, name, ns, node, 0);
7258 if (attrDecl == NULL)
7259 return (NULL);
7260 if (tmpName != NULL) {
7261 attrDecl->typeName = tmpName;
7262 attrDecl->typeNs = tmpNs;
7263 }
7264 use->attrDecl = attrDecl;
7265 /*
7266 * Value constraint.
7267 */
7268 if (defValue != NULL) {
7269 attrDecl->defValue = defValue;
7270 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7271 attrDecl->flags |= XML_SCHEMAS_ATTR_FIXED;
7272 }
7273 } else if (occurs != XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7274 xmlSchemaQNameRefPtr ref;
7275
7276 /*
7277 * Create the attribute use component.
7278 */
7279 use = xmlSchemaAddAttributeUse(pctxt, node);
7280 if (use == NULL)
7281 return(NULL);
7282 /*
7283 * We need to resolve the reference at later stage.
7284 */
7285 WXS_ADD_PENDING(pctxt, use);
7286 use->occurs = occurs;
7287 /*
7288 * Create a QName reference to the attribute declaration.
7289 */
7290 ref = xmlSchemaNewQNameRef(pctxt, XML_SCHEMA_TYPE_ATTRIBUTE,
7291 tmpName, tmpNs);
7292 if (ref == NULL)
7293 return(NULL);
7294 /*
7295 * Assign the reference. This will be substituted for the
7296 * referenced attribute declaration when the QName is resolved.
7297 */
7298 use->attrDecl = WXS_ATTR_CAST ref;
7299 /*
7300 * Value constraint.
7301 */
7302 if (defValue != NULL)
7303 use->defValue = defValue;
7304 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7305 use->flags |= XML_SCHEMA_ATTR_USE_FIXED;
7306 }
7307
7308check_children:
7309 /*
7310 * And now for the children...
7311 */
7312 child = node->children;
7313 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7314 xmlSchemaAttributeUseProhibPtr prohib;
7315
7316 if (IS_SCHEMA(child, "annotation")) {
7317 xmlSchemaParseAnnotation(pctxt, schema, child, 0);
7318 child = child->next;
7319 }
7320 if (child != NULL) {
7321 xmlSchemaPContentErr(pctxt,
7322 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7323 NULL, node, child, NULL,
7324 "(annotation?)");
7325 }
7326 /*
7327 * Check for pointlessness of attribute prohibitions.
7328 */
7329 if (parentType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) {
7330 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7331 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7332 node, NULL,
7333 "Skipping attribute use prohibition, since it is "
7334 "pointless inside an <attributeGroup>",
7335 NULL, NULL, NULL);
7336 return(NULL);
7337 } else if (parentType == XML_SCHEMA_TYPE_EXTENSION) {
7338 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7339 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7340 node, NULL,
7341 "Skipping attribute use prohibition, since it is "
7342 "pointless when extending a type",
7343 NULL, NULL, NULL);
7344 return(NULL);
7345 }
7346 if (! isRef) {
7347 tmpName = name;
7348 tmpNs = ns;
7349 }
7350 /*
7351 * Check for duplicate attribute prohibitions.
7352 */
7353 if (uses) {
7354 int i;
7355
7356 for (i = 0; i < uses->nbItems; i++) {
7357 use = uses->items[i];
7358 if ((use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) &&
7359 (tmpName == (WXS_ATTR_PROHIB_CAST use)->name) &&
7360 (tmpNs == (WXS_ATTR_PROHIB_CAST use)->targetNamespace))
7361 {
7362 xmlChar *str = NULL;
7363
7364 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7365 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7366 node, NULL,
7367 "Skipping duplicate attribute use prohibition '%s'",
7368 xmlSchemaFormatQName(&str, tmpNs, tmpName),
7369 NULL, NULL);
7370 FREE_AND_NULL(str)
7371 return(NULL);
7372 }
7373 }
7374 }
7375 /*
7376 * Create the attribute prohibition helper component.
7377 */
7378 prohib = xmlSchemaAddAttributeUseProhib(pctxt);
7379 if (prohib == NULL)
7380 return(NULL);
7381 prohib->node = node;
7382 prohib->name = tmpName;
7383 prohib->targetNamespace = tmpNs;
7384 if (isRef) {
7385 /*
7386 * We need at least to resolve to the attribute declaration.
7387 */
7388 WXS_ADD_PENDING(pctxt, prohib);
7389 }
7390 return(WXS_BASIC_CAST prohib);
7391 } else {
7392 if (IS_SCHEMA(child, "annotation")) {
7393 /*
7394 * TODO: Should this go into the attr decl?
7395 */
7396 use->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
7397 child = child->next;
7398 }
7399 if (isRef) {
7400 if (child != NULL) {
7401 if (IS_SCHEMA(child, "simpleType"))
7402 /*
7403 * 3.2.3 : 3.2
7404 * If ref is present, then all of <simpleType>,
7405 * form and type must be absent.
7406 */
7407 xmlSchemaPContentErr(pctxt,
7408 XML_SCHEMAP_SRC_ATTRIBUTE_3_2,
7409 NULL, node, child, NULL,
7410 "(annotation?)");
7411 else
7412 xmlSchemaPContentErr(pctxt,
7413 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7414 NULL, node, child, NULL,
7415 "(annotation?)");
7416 }
7417 } else {
7418 if (IS_SCHEMA(child, "simpleType")) {
7419 if (WXS_ATTRUSE_DECL(use)->typeName != NULL) {
7420 /*
7421 * 3.2.3 : 4
7422 * type and <simpleType> must not both be present.
7423 */
7424 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7425 NULL, node, child,
7426 "The attribute 'type' and the <simpleType> child "
7427 "are mutually exclusive", NULL);
7428 } else
7429 WXS_ATTRUSE_TYPEDEF(use) =
7430 xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7431 child = child->next;
7432 }
7433 if (child != NULL)
7434 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7435 NULL, node, child, NULL,
7436 "(annotation?, simpleType?)");
7437 }
7438 }
7439 return (WXS_BASIC_CAST use);
7440}
7441
7442
7443static xmlSchemaAttributePtr
7444xmlSchemaParseGlobalAttribute(xmlSchemaParserCtxtPtr pctxt,
7445 xmlSchemaPtr schema,
7446 xmlNodePtr node)
7447{
7448 const xmlChar *attrValue;
Daniel Veillard4255d502002-04-16 15:50:10 +00007449 xmlSchemaAttributePtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007450 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007451 xmlAttrPtr attr;
William M. Bracke7091952004-05-11 15:09:58 +00007452
7453 /*
7454 * Note that the w3c spec assumes the schema to be validated with schema
7455 * for schemas beforehand.
7456 *
7457 * 3.2.3 Constraints on XML Representations of Attribute Declarations
William M. Bracke7091952004-05-11 15:09:58 +00007458 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007459 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7460 return (NULL);
7461 /*
7462 * 3.2.3 : 3.1
7463 * One of ref or name must be present, but not both
7464 */
7465 attr = xmlSchemaGetPropNode(node, "name");
7466 if (attr == NULL) {
7467 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7468 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007469 return (NULL);
7470 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007471 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7472 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0) {
7473 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007474 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007475 /*
7476 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7477 * TODO: Move this to the component layer.
7478 */
7479 if (xmlStrEqual(attrValue, BAD_CAST "xmlns")) {
7480 xmlSchemaPSimpleTypeErr(pctxt,
7481 XML_SCHEMAP_NO_XMLNS,
7482 NULL, (xmlNodePtr) attr,
7483 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7484 "The value of the attribute must not match 'xmlns'",
7485 NULL, NULL);
7486 return (NULL);
7487 }
7488 /*
7489 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7490 * TODO: Move this to the component layer.
7491 * Or better leave it here and add it to the component layer
7492 * if we have a schema construction API.
7493 */
7494 if (xmlStrEqual(pctxt->targetNamespace, xmlSchemaInstanceNs)) {
7495 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7496 XML_SCHEMAP_NO_XSI, node, NULL,
7497 "The target namespace must not match '%s'",
7498 xmlSchemaInstanceNs, NULL);
7499 }
7500
7501 ret = xmlSchemaAddAttribute(pctxt, schema, attrValue,
7502 pctxt->targetNamespace, node, 1);
7503 if (ret == NULL)
7504 return (NULL);
7505 ret->flags |= XML_SCHEMAS_ATTR_GLOBAL;
7506
7507 /*
7508 * Check for illegal attributes.
7509 */
7510 attr = node->properties;
7511 while (attr != NULL) {
7512 if (attr->ns == NULL) {
7513 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7514 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
7515 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
7516 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7517 (!xmlStrEqual(attr->name, BAD_CAST "type")))
7518 {
7519 xmlSchemaPIllegalAttrErr(pctxt,
7520 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7521 }
7522 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7523 xmlSchemaPIllegalAttrErr(pctxt,
7524 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7525 }
7526 attr = attr->next;
7527 }
7528 xmlSchemaPValAttrQName(pctxt, schema, NULL,
7529 node, "type", &ret->typeNs, &ret->typeName);
7530
7531 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
William M. Bracke7091952004-05-11 15:09:58 +00007532 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007533 * Attribute "fixed".
William M. Bracke7091952004-05-11 15:09:58 +00007534 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007535 ret->defValue = xmlSchemaGetProp(pctxt, node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007536 if (ret->defValue != NULL)
7537 ret->flags |= XML_SCHEMAS_ATTR_FIXED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007538 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007539 * Attribute "default".
7540 */
7541 attr = xmlSchemaGetPropNode(node, "default");
7542 if (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007543 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007544 * 3.2.3 : 1
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007545 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00007546 */
7547 if (ret->flags & XML_SCHEMAS_ATTR_FIXED) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007548 xmlSchemaPMutualExclAttrErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_1,
7549 WXS_BASIC_CAST ret, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007550 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007551 ret->defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007552 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00007553 /*
7554 * And now for the children...
7555 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007556 child = node->children;
7557 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007558 ret->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007559 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007560 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007561 if (IS_SCHEMA(child, "simpleType")) {
7562 if (ret->typeName != NULL) {
7563 /*
7564 * 3.2.3 : 4
7565 * type and <simpleType> must not both be present.
7566 */
7567 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7568 NULL, node, child,
7569 "The attribute 'type' and the <simpleType> child "
7570 "are mutually exclusive", NULL);
7571 } else
7572 ret->subtypes = xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7573 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007574 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007575 if (child != NULL)
7576 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7577 NULL, node, child, NULL,
7578 "(annotation?, simpleType?)");
7579
Daniel Veillard4255d502002-04-16 15:50:10 +00007580 return (ret);
7581}
7582
7583/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007584 * xmlSchemaParseAttributeGroupRef:
Daniel Veillard4255d502002-04-16 15:50:10 +00007585 * @ctxt: a schema validation context
7586 * @schema: the schema being built
7587 * @node: a subtree containing XML Schema informations
7588 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007589 * Parse an attribute group definition reference.
7590 * Note that a reference to an attribute group does not
7591 * correspond to any component at all.
Daniel Veillard4255d502002-04-16 15:50:10 +00007592 * *WARNING* this interface is highly subject to change
7593 *
7594 * Returns the attribute group or NULL in case of error.
7595 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007596static xmlSchemaQNameRefPtr
7597xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
7598 xmlSchemaPtr schema,
7599 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007600{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007601 xmlSchemaQNameRefPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007602 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007603 xmlAttrPtr attr;
7604 const xmlChar *refNs = NULL, *ref = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00007605
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007606 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00007607 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007608
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007609 attr = xmlSchemaGetPropNode(node, "ref");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007610 if (attr == NULL) {
7611 xmlSchemaPMissingAttrErr(pctxt,
7612 XML_SCHEMAP_S4S_ATTR_MISSING,
7613 NULL, node, "ref", NULL);
7614 return (NULL);
7615 }
7616 xmlSchemaPValAttrNodeQName(pctxt, schema,
7617 NULL, attr, &refNs, &ref);
7618 if (xmlSchemaCheckReference(pctxt, schema, node, attr, refNs) != 0)
7619 return(NULL);
7620
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007621 /*
7622 * Check for illegal attributes.
7623 */
7624 attr = node->properties;
7625 while (attr != NULL) {
7626 if (attr->ns == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007627 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007628 (!xmlStrEqual(attr->name, BAD_CAST "id")))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007629 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007630 xmlSchemaPIllegalAttrErr(pctxt,
7631 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007632 }
7633 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007634 xmlSchemaPIllegalAttrErr(pctxt,
7635 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007636 }
7637 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007638 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00007639 /* Attribute ID */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007640 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7641
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007642 /*
7643 * And now for the children...
7644 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007645 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00007646 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007647 /*
7648 * TODO: We do not have a place to store the annotation, do we?
7649 */
7650 xmlSchemaParseAnnotation(pctxt, schema, child, 0);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007651 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007652 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007653 if (child != NULL) {
7654 xmlSchemaPContentErr(pctxt,
7655 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7656 NULL, node, child, NULL,
7657 "(annotation?)");
7658 }
7659
7660 /*
7661 * Handle attribute group redefinitions.
7662 */
7663 if (pctxt->isRedefine && pctxt->redef &&
7664 (pctxt->redef->item->type ==
7665 XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
7666 (ref == pctxt->redef->refName) &&
7667 (refNs == pctxt->redef->refTargetNs))
7668 {
7669 /*
7670 * SPEC src-redefine:
7671 * (7.1) "If it has an <attributeGroup> among its contents
7672 * the ·actual value· of whose ref [attribute] is the same
7673 * as the ·actual value· of its own name attribute plus
7674 * target namespace, then it must have exactly one such group."
7675 */
7676 if (pctxt->redefCounter != 0) {
7677 xmlChar *str = NULL;
7678
7679 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7680 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
7681 "The redefining attribute group definition "
7682 "'%s' must not contain more than one "
7683 "reference to the redefined definition",
7684 xmlSchemaFormatQName(&str, refNs, ref), NULL);
7685 FREE_AND_NULL(str);
7686 return(NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007687 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007688 pctxt->redefCounter++;
7689 /*
7690 * URGENT TODO: How to ensure that the reference will not be
7691 * handled by the normal component resolution mechanism?
7692 */
7693 ret = xmlSchemaNewQNameRef(pctxt,
7694 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7695 if (ret == NULL)
7696 return(NULL);
7697 ret->node = node;
7698 pctxt->redef->reference = WXS_BASIC_CAST ret;
7699 } else {
7700 /*
7701 * Create a QName-reference helper component. We will substitute this
7702 * component for the attribute uses of the referenced attribute group
7703 * definition.
7704 */
7705 ret = xmlSchemaNewQNameRef(pctxt,
7706 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7707 if (ret == NULL)
7708 return(NULL);
7709 ret->node = node;
7710 /* Add to pending items, to be able to resolve the reference. */
7711 WXS_ADD_PENDING(pctxt, ret);
7712 }
7713 return (ret);
7714}
7715
7716/**
7717 * xmlSchemaParseAttributeGroupDefinition:
7718 * @pctxt: a schema validation context
7719 * @schema: the schema being built
7720 * @node: a subtree containing XML Schema informations
7721 *
7722 * parse a XML schema Attribute Group declaration
7723 * *WARNING* this interface is highly subject to change
7724 *
7725 * Returns the attribute group definition or NULL in case of error.
7726 */
7727static xmlSchemaAttributeGroupPtr
7728xmlSchemaParseAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
7729 xmlSchemaPtr schema,
7730 xmlNodePtr node)
7731{
7732 const xmlChar *name;
7733 xmlSchemaAttributeGroupPtr ret;
7734 xmlNodePtr child = NULL;
7735 xmlAttrPtr attr;
7736 int hasRefs = 0;
7737
7738 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7739 return (NULL);
7740
7741 attr = xmlSchemaGetPropNode(node, "name");
7742 if (attr == NULL) {
7743 xmlSchemaPMissingAttrErr(pctxt,
7744 XML_SCHEMAP_S4S_ATTR_MISSING,
7745 NULL, node, "name", NULL);
7746 return (NULL);
7747 }
7748 /*
7749 * The name is crucial, exit if invalid.
7750 */
7751 if (xmlSchemaPValAttrNode(pctxt,
7752 NULL, attr,
7753 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7754 return (NULL);
7755 }
7756 ret = xmlSchemaAddAttributeGroupDefinition(pctxt, schema,
7757 name, pctxt->targetNamespace, node);
7758 if (ret == NULL)
7759 return (NULL);
7760 /*
7761 * Check for illegal attributes.
7762 */
7763 attr = node->properties;
7764 while (attr != NULL) {
7765 if (attr->ns == NULL) {
7766 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7767 (!xmlStrEqual(attr->name, BAD_CAST "id")))
7768 {
7769 xmlSchemaPIllegalAttrErr(pctxt,
7770 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7771 }
7772 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7773 xmlSchemaPIllegalAttrErr(pctxt,
7774 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7775 }
7776 attr = attr->next;
7777 }
7778 /* Attribute ID */
7779 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7780 /*
7781 * And now for the children...
7782 */
7783 child = node->children;
7784 if (IS_SCHEMA(child, "annotation")) {
7785 ret->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
7786 child = child->next;
7787 }
7788 /*
7789 * Parse contained attribute decls/refs.
7790 */
7791 if (xmlSchemaParseLocalAttributes(pctxt, schema, &child,
7792 (xmlSchemaItemListPtr *) &(ret->attrUses),
7793 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, &hasRefs) == -1)
7794 return(NULL);
7795 if (hasRefs)
7796 ret->flags |= XML_SCHEMAS_ATTRGROUP_HAS_REFS;
7797 /*
7798 * Parse the attribute wildcard.
7799 */
7800 if (IS_SCHEMA(child, "anyAttribute")) {
7801 ret->attributeWildcard = xmlSchemaParseAnyAttribute(pctxt,
7802 schema, child);
7803 child = child->next;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007804 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007805 if (child != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007806 xmlSchemaPContentErr(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007807 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007808 NULL, node, child, NULL,
7809 "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))");
Daniel Veillard4255d502002-04-16 15:50:10 +00007810 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007811 return (ret);
7812}
7813
7814/**
William M. Brack2f2a6632004-08-20 23:09:47 +00007815 * xmlSchemaPValAttrFormDefault:
7816 * @value: the value
7817 * @flags: the flags to be modified
7818 * @flagQualified: the specific flag for "qualified"
7819 *
7820 * Returns 0 if the value is valid, 1 otherwise.
7821 */
7822static int
7823xmlSchemaPValAttrFormDefault(const xmlChar *value,
7824 int *flags,
7825 int flagQualified)
7826{
7827 if (xmlStrEqual(value, BAD_CAST "qualified")) {
7828 if ((*flags & flagQualified) == 0)
7829 *flags |= flagQualified;
7830 } else if (!xmlStrEqual(value, BAD_CAST "unqualified"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007831 return (1);
7832
William M. Brack2f2a6632004-08-20 23:09:47 +00007833 return (0);
7834}
7835
7836/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00007837 * xmlSchemaPValAttrBlockFinal:
7838 * @value: the value
7839 * @flags: the flags to be modified
7840 * @flagAll: the specific flag for "#all"
7841 * @flagExtension: the specific flag for "extension"
7842 * @flagRestriction: the specific flag for "restriction"
7843 * @flagSubstitution: the specific flag for "substitution"
7844 * @flagList: the specific flag for "list"
7845 * @flagUnion: the specific flag for "union"
7846 *
7847 * Validates the value of the attribute "final" and "block". The value
7848 * is converted into the specified flag values and returned in @flags.
7849 *
7850 * Returns 0 if the value is valid, 1 otherwise.
7851 */
7852
7853static int
7854xmlSchemaPValAttrBlockFinal(const xmlChar *value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007855 int *flags,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007856 int flagAll,
7857 int flagExtension,
7858 int flagRestriction,
7859 int flagSubstitution,
7860 int flagList,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007861 int flagUnion)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007862{
7863 int ret = 0;
7864
7865 /*
7866 * TODO: This does not check for dublicate entries.
7867 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00007868 if ((flags == NULL) || (value == NULL))
7869 return (-1);
7870 if (value[0] == 0)
7871 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007872 if (xmlStrEqual(value, BAD_CAST "#all")) {
7873 if (flagAll != -1)
7874 *flags |= flagAll;
7875 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007876 if (flagExtension != -1)
7877 *flags |= flagExtension;
7878 if (flagRestriction != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007879 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007880 if (flagSubstitution != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007881 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007882 if (flagList != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007883 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007884 if (flagUnion != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007885 *flags |= flagUnion;
7886 }
7887 } else {
7888 const xmlChar *end, *cur = value;
7889 xmlChar *item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007890
Daniel Veillardc0826a72004-08-10 14:17:33 +00007891 do {
7892 while (IS_BLANK_CH(*cur))
7893 cur++;
7894 end = cur;
7895 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
7896 end++;
7897 if (end == cur)
7898 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007899 item = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007900 if (xmlStrEqual(item, BAD_CAST "extension")) {
7901 if (flagExtension != -1) {
7902 if ((*flags & flagExtension) == 0)
7903 *flags |= flagExtension;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007904 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007905 ret = 1;
7906 } else if (xmlStrEqual(item, BAD_CAST "restriction")) {
7907 if (flagRestriction != -1) {
7908 if ((*flags & flagRestriction) == 0)
7909 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007910 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007911 ret = 1;
7912 } else if (xmlStrEqual(item, BAD_CAST "substitution")) {
7913 if (flagSubstitution != -1) {
7914 if ((*flags & flagSubstitution) == 0)
7915 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007916 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007917 ret = 1;
7918 } else if (xmlStrEqual(item, BAD_CAST "list")) {
7919 if (flagList != -1) {
7920 if ((*flags & flagList) == 0)
7921 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007922 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007923 ret = 1;
7924 } else if (xmlStrEqual(item, BAD_CAST "union")) {
7925 if (flagUnion != -1) {
7926 if ((*flags & flagUnion) == 0)
7927 *flags |= flagUnion;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007928 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007929 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007930 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007931 ret = 1;
7932 if (item != NULL)
7933 xmlFree(item);
7934 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007935 } while ((ret == 0) && (*cur != 0));
7936 }
7937
Daniel Veillardc0826a72004-08-10 14:17:33 +00007938 return (ret);
7939}
7940
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007941static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007942xmlSchemaCheckCSelectorXPath(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007943 xmlSchemaIDCPtr idc,
7944 xmlSchemaIDCSelectPtr selector,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007945 xmlAttrPtr attr,
7946 int isField)
7947{
7948 xmlNodePtr node;
7949
7950 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007951 * c-selector-xpath:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007952 * Schema Component Constraint: Selector Value OK
7953 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007954 * TODO: 1 The {selector} must be a valid XPath expression, as defined
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007955 * in [XPath].
7956 */
7957 if (selector == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007958 xmlSchemaPErr(ctxt, idc->node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007959 XML_SCHEMAP_INTERNAL,
7960 "Internal error: xmlSchemaCheckCSelectorXPath, "
7961 "the selector is not specified.\n", NULL, NULL);
7962 return (-1);
7963 }
7964 if (attr == NULL)
7965 node = idc->node;
7966 else
7967 node = (xmlNodePtr) attr;
7968 if (selector->xpath == NULL) {
7969 xmlSchemaPCustomErr(ctxt,
7970 /* TODO: Adjust error code. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007971 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007972 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007973 "The XPath expression of the selector is not valid", NULL);
7974 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
7975 } else {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007976 const xmlChar **nsArray = NULL;
7977 xmlNsPtr *nsList = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007978 /*
7979 * Compile the XPath expression.
7980 */
7981 /*
7982 * TODO: We need the array of in-scope namespaces for compilation.
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007983 * TODO: Call xmlPatterncompile with different options for selector/
7984 * field.
7985 */
7986 nsList = xmlGetNsList(attr->doc, attr->parent);
7987 /*
7988 * Build an array of prefixes and namespaces.
7989 */
7990 if (nsList != NULL) {
7991 int i, count = 0;
7992 xmlNsPtr ns;
7993
7994 for (i = 0; nsList[i] != NULL; i++)
7995 count++;
7996
7997 nsArray = (const xmlChar **) xmlMalloc(
7998 (count * 2 + 1) * sizeof(const xmlChar *));
7999 if (nsArray == NULL) {
8000 xmlSchemaPErrMemory(ctxt, "allocating a namespace array",
8001 NULL);
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00008002 xmlFree(nsList);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008003 return (-1);
8004 }
8005 for (i = 0; i < count; i++) {
8006 ns = nsList[i];
8007 nsArray[2 * i] = nsList[i]->href;
8008 nsArray[2 * i + 1] = nsList[i]->prefix;
8009 }
8010 nsArray[count * 2] = NULL;
8011 xmlFree(nsList);
8012 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008013 /*
8014 * TODO: Differentiate between "selector" and "field".
8015 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008016 if (isField)
8017 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008018 NULL, XML_PATTERN_XSFIELD, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008019 else
8020 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008021 NULL, XML_PATTERN_XSSEL, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008022 if (nsArray != NULL)
8023 xmlFree((xmlChar **) nsArray);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008024
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00008025 if (selector->xpathComp == NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008026 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00008027 /* TODO: Adjust error code? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008028 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008029 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008030 "The XPath expression '%s' could not be "
8031 "compiled", selector->xpath);
8032 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008033 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008034 }
8035 return (0);
8036}
8037
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008038#define ADD_ANNOTATION(annot) \
8039 xmlSchemaAnnotPtr cur = item->annot; \
8040 if (item->annot == NULL) { \
8041 item->annot = annot; \
8042 return (annot); \
8043 } \
8044 cur = item->annot; \
8045 if (cur->next != NULL) { \
8046 cur = cur->next; \
8047 } \
8048 cur->next = annot;
8049
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008050/**
8051 * xmlSchemaAssignAnnotation:
8052 * @item: the schema component
8053 * @annot: the annotation
8054 *
8055 * Adds the annotation to the given schema component.
8056 *
8057 * Returns the given annotaion.
8058 */
8059static xmlSchemaAnnotPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008060xmlSchemaAddAnnotation(xmlSchemaAnnotItemPtr annItem,
8061 xmlSchemaAnnotPtr annot)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008062{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008063 if ((annItem == NULL) || (annot == NULL))
8064 return (NULL);
8065 switch (annItem->type) {
8066 case XML_SCHEMA_TYPE_ELEMENT: {
8067 xmlSchemaElementPtr item = (xmlSchemaElementPtr) annItem;
8068 ADD_ANNOTATION(annot)
8069 }
8070 break;
8071 case XML_SCHEMA_TYPE_ATTRIBUTE: {
8072 xmlSchemaAttributePtr item = (xmlSchemaAttributePtr) annItem;
8073 ADD_ANNOTATION(annot)
8074 }
8075 break;
8076 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
8077 case XML_SCHEMA_TYPE_ANY: {
8078 xmlSchemaWildcardPtr item = (xmlSchemaWildcardPtr) annItem;
8079 ADD_ANNOTATION(annot)
8080 }
8081 break;
8082 case XML_SCHEMA_TYPE_PARTICLE:
8083 case XML_SCHEMA_TYPE_IDC_KEY:
8084 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008085 case XML_SCHEMA_TYPE_IDC_UNIQUE: {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008086 xmlSchemaAnnotItemPtr item = (xmlSchemaAnnotItemPtr) annItem;
8087 ADD_ANNOTATION(annot)
8088 }
8089 break;
8090 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: {
8091 xmlSchemaAttributeGroupPtr item =
8092 (xmlSchemaAttributeGroupPtr) annItem;
8093 ADD_ANNOTATION(annot)
8094 }
8095 break;
8096 case XML_SCHEMA_TYPE_NOTATION: {
8097 xmlSchemaNotationPtr item = (xmlSchemaNotationPtr) annItem;
8098 ADD_ANNOTATION(annot)
8099 }
8100 break;
8101 case XML_SCHEMA_FACET_MININCLUSIVE:
8102 case XML_SCHEMA_FACET_MINEXCLUSIVE:
8103 case XML_SCHEMA_FACET_MAXINCLUSIVE:
8104 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
8105 case XML_SCHEMA_FACET_TOTALDIGITS:
8106 case XML_SCHEMA_FACET_FRACTIONDIGITS:
8107 case XML_SCHEMA_FACET_PATTERN:
8108 case XML_SCHEMA_FACET_ENUMERATION:
8109 case XML_SCHEMA_FACET_WHITESPACE:
8110 case XML_SCHEMA_FACET_LENGTH:
8111 case XML_SCHEMA_FACET_MAXLENGTH:
8112 case XML_SCHEMA_FACET_MINLENGTH: {
8113 xmlSchemaFacetPtr item = (xmlSchemaFacetPtr) annItem;
8114 ADD_ANNOTATION(annot)
8115 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008116 break;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008117 case XML_SCHEMA_TYPE_SIMPLE:
8118 case XML_SCHEMA_TYPE_COMPLEX: {
8119 xmlSchemaTypePtr item = (xmlSchemaTypePtr) annItem;
8120 ADD_ANNOTATION(annot)
8121 }
8122 break;
8123 case XML_SCHEMA_TYPE_GROUP: {
8124 xmlSchemaModelGroupDefPtr item = (xmlSchemaModelGroupDefPtr) annItem;
8125 ADD_ANNOTATION(annot)
8126 }
8127 break;
8128 case XML_SCHEMA_TYPE_SEQUENCE:
8129 case XML_SCHEMA_TYPE_CHOICE:
8130 case XML_SCHEMA_TYPE_ALL: {
8131 xmlSchemaModelGroupPtr item = (xmlSchemaModelGroupPtr) annItem;
8132 ADD_ANNOTATION(annot)
8133 }
8134 break;
8135 default:
8136 xmlSchemaPCustomErr(NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008137 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008138 NULL, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008139 "Internal error: xmlSchemaAddAnnotation, "
8140 "The item is not a annotated schema component", NULL);
8141 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008142 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008143 return (annot);
8144}
8145
8146/**
8147 * xmlSchemaParseIDCSelectorAndField:
8148 * @ctxt: a schema validation context
8149 * @schema: the schema being built
8150 * @node: a subtree containing XML Schema informations
8151 *
8152 * Parses a XML Schema identity-contraint definition's
8153 * <selector> and <field> elements.
8154 *
8155 * Returns the parsed identity-constraint definition.
8156 */
8157static xmlSchemaIDCSelectPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008158xmlSchemaParseIDCSelectorAndField(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008159 xmlSchemaPtr schema,
8160 xmlSchemaIDCPtr idc,
8161 xmlNodePtr node,
8162 int isField)
8163{
8164 xmlSchemaIDCSelectPtr item;
8165 xmlNodePtr child = NULL;
8166 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008167
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008168 /*
8169 * Check for illegal attributes.
8170 */
8171 attr = node->properties;
8172 while (attr != NULL) {
8173 if (attr->ns == NULL) {
8174 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8175 (!xmlStrEqual(attr->name, BAD_CAST "xpath"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008176 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008177 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008178 }
8179 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008180 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008181 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008182 }
8183 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008184 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008185 /*
8186 * Create the item.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008187 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008188 item = (xmlSchemaIDCSelectPtr) xmlMalloc(sizeof(xmlSchemaIDCSelect));
8189 if (item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008190 xmlSchemaPErrMemory(ctxt,
8191 "allocating a 'selector' of an identity-constraint definition",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008192 NULL);
8193 return (NULL);
8194 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008195 memset(item, 0, sizeof(xmlSchemaIDCSelect));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008196 /*
8197 * Attribute "xpath" (mandatory).
8198 */
8199 attr = xmlSchemaGetPropNode(node, "xpath");
8200 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008201 xmlSchemaPMissingAttrErr(ctxt,
8202 XML_SCHEMAP_S4S_ATTR_MISSING,
8203 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008204 "name", NULL);
8205 } else {
8206 item->xpath = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8207 /*
8208 * URGENT TODO: "field"s have an other syntax than "selector"s.
8209 */
8210
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008211 if (xmlSchemaCheckCSelectorXPath(ctxt, idc, item, attr,
8212 isField) == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008213 xmlSchemaPErr(ctxt,
8214 (xmlNodePtr) attr,
8215 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008216 "Internal error: xmlSchemaParseIDCSelectorAndField, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008217 "validating the XPath expression of a IDC selector.\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008218 NULL, NULL);
8219 }
8220
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008221 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008222 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008223 /*
8224 * And now for the children...
8225 */
8226 child = node->children;
8227 if (IS_SCHEMA(child, "annotation")) {
8228 /*
8229 * Add the annotation to the parent IDC.
8230 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008231 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008232 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008233 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008234 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008235 if (child != NULL) {
8236 xmlSchemaPContentErr(ctxt,
8237 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008238 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008239 NULL, "(annotation?)");
8240 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008241
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008242 return (item);
8243}
8244
8245/**
8246 * xmlSchemaParseIDC:
8247 * @ctxt: a schema validation context
8248 * @schema: the schema being built
8249 * @node: a subtree containing XML Schema informations
8250 *
8251 * Parses a XML Schema identity-contraint definition.
8252 *
8253 * Returns the parsed identity-constraint definition.
8254 */
8255static xmlSchemaIDCPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008256xmlSchemaParseIDC(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008257 xmlSchemaPtr schema,
8258 xmlNodePtr node,
8259 xmlSchemaTypeType idcCategory,
8260 const xmlChar *targetNamespace)
8261{
8262 xmlSchemaIDCPtr item = NULL;
8263 xmlNodePtr child = NULL;
8264 xmlAttrPtr attr;
8265 const xmlChar *name = NULL;
8266 xmlSchemaIDCSelectPtr field = NULL, lastField = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008267
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008268 /*
8269 * Check for illegal attributes.
8270 */
8271 attr = node->properties;
8272 while (attr != NULL) {
8273 if (attr->ns == NULL) {
8274 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8275 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8276 ((idcCategory != XML_SCHEMA_TYPE_IDC_KEYREF) ||
8277 (!xmlStrEqual(attr->name, BAD_CAST "refer")))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008278 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008279 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008280 }
8281 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008282 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008283 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008284 }
8285 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008286 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008287 /*
8288 * Attribute "name" (mandatory).
8289 */
8290 attr = xmlSchemaGetPropNode(node, "name");
8291 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008292 xmlSchemaPMissingAttrErr(ctxt,
8293 XML_SCHEMAP_S4S_ATTR_MISSING,
8294 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008295 "name", NULL);
8296 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008297 } else if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008298 NULL, attr,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008299 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
8300 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008301 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008302 /* Create the component. */
8303 item = xmlSchemaAddIDC(ctxt, schema, name, targetNamespace,
8304 idcCategory, node);
8305 if (item == NULL)
8306 return(NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008307
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008308 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008309 if (idcCategory == XML_SCHEMA_TYPE_IDC_KEYREF) {
8310 /*
8311 * Attribute "refer" (mandatory).
8312 */
8313 attr = xmlSchemaGetPropNode(node, "refer");
8314 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008315 xmlSchemaPMissingAttrErr(ctxt,
8316 XML_SCHEMAP_S4S_ATTR_MISSING,
8317 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008318 "refer", NULL);
8319 } else {
8320 /*
8321 * Create a reference item.
8322 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008323 item->ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_IDC_KEY,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008324 NULL, NULL);
8325 if (item->ref == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008326 return (NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008327 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008328 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008329 &(item->ref->targetNamespace),
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008330 &(item->ref->name));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008331 xmlSchemaCheckReference(ctxt, schema, node, attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008332 item->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008333 }
8334 }
8335 /*
8336 * And now for the children...
8337 */
8338 child = node->children;
8339 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008340 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008341 child = child->next;
8342 }
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008343 if (child == NULL) {
8344 xmlSchemaPContentErr(ctxt,
8345 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008346 NULL, node, child,
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008347 "A child element is missing",
8348 "(annotation?, (selector, field+))");
8349 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008350 /*
8351 * Child element <selector>.
8352 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008353 if (IS_SCHEMA(child, "selector")) {
8354 item->selector = xmlSchemaParseIDCSelectorAndField(ctxt, schema,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008355 item, child, 0);
8356 child = child->next;
8357 /*
8358 * Child elements <field>.
8359 */
8360 if (IS_SCHEMA(child, "field")) {
8361 do {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008362 field = xmlSchemaParseIDCSelectorAndField(ctxt, schema,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008363 item, child, 1);
8364 if (field != NULL) {
8365 field->index = item->nbFields;
8366 item->nbFields++;
8367 if (lastField != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008368 lastField->next = field;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008369 else
8370 item->fields = field;
8371 lastField = field;
8372 }
8373 child = child->next;
8374 } while (IS_SCHEMA(child, "field"));
8375 } else {
8376 xmlSchemaPContentErr(ctxt,
8377 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008378 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008379 NULL, "(annotation?, (selector, field+))");
8380 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008381 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008382 if (child != NULL) {
8383 xmlSchemaPContentErr(ctxt,
8384 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008385 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008386 NULL, "(annotation?, (selector, field+))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008387 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008388
8389 return (item);
8390}
8391
Daniel Veillardc0826a72004-08-10 14:17:33 +00008392/**
Daniel Veillard4255d502002-04-16 15:50:10 +00008393 * xmlSchemaParseElement:
8394 * @ctxt: a schema validation context
8395 * @schema: the schema being built
8396 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008397 * @topLevel: indicates if this is global declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00008398 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008399 * Parses a XML schema element declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00008400 * *WARNING* this interface is highly subject to change
8401 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008402 * Returns the element declaration or a particle; NULL in case
8403 * of an error or if the particle has minOccurs==maxOccurs==0.
Daniel Veillard4255d502002-04-16 15:50:10 +00008404 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008405static xmlSchemaBasicItemPtr
Daniel Veillard4255d502002-04-16 15:50:10 +00008406xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008407 xmlNodePtr node, int *isElemRef, int topLevel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008408{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008409 xmlSchemaElementPtr decl = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008410 xmlSchemaParticlePtr particle = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008411 xmlSchemaAnnotPtr annot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008412 xmlNodePtr child = NULL;
8413 xmlAttrPtr attr, nameAttr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008414 int min, max, isRef = 0;
8415 xmlChar *des = NULL;
William M. Bracke7091952004-05-11 15:09:58 +00008416
8417 /* 3.3.3 Constraints on XML Representations of Element Declarations */
8418 /* TODO: Complete implementation of 3.3.6 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008419
Daniel Veillard4255d502002-04-16 15:50:10 +00008420 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008421 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008422
8423 if (isElemRef != NULL)
8424 *isElemRef = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008425 /*
8426 * If we get a "ref" attribute on a local <element> we will assume it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008427 * a reference - even if there's a "name" attribute; this seems to be more
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008428 * robust.
8429 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008430 nameAttr = xmlSchemaGetPropNode(node, "name");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008431 attr = xmlSchemaGetPropNode(node, "ref");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008432 if ((topLevel) || (attr == NULL)) {
8433 if (nameAttr == NULL) {
8434 xmlSchemaPMissingAttrErr(ctxt,
8435 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008436 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008437 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008438 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008439 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00008440 isRef = 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008441
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008442 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008443 child = node->children;
8444 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008445 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008446 child = child->next;
8447 }
8448 /*
8449 * Skip particle part if a global declaration.
8450 */
8451 if (topLevel)
8452 goto declaration_part;
8453 /*
8454 * The particle part ==================================================
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008455 */
8456 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
8457 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1, "(xs:nonNegativeInteger | unbounded)");
8458 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008459 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
8460 if (particle == NULL)
8461 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008462
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008463 /* ret->flags |= XML_SCHEMAS_ELEM_REF; */
8464
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008465 if (isRef) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008466 const xmlChar *refNs = NULL, *ref = NULL;
8467 xmlSchemaQNameRefPtr refer = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008468 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008469 * The reference part =============================================
Daniel Veillardc0826a72004-08-10 14:17:33 +00008470 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008471 if (isElemRef != NULL)
8472 *isElemRef = 1;
8473
Daniel Veillardc0826a72004-08-10 14:17:33 +00008474 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008475 NULL, attr, &refNs, &ref);
8476 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008477 /*
8478 * SPEC (3.3.3 : 2.1) "One of ref or name must be present, but not both"
Daniel Veillardc0826a72004-08-10 14:17:33 +00008479 */
8480 if (nameAttr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008481 xmlSchemaPMutualExclAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008482 XML_SCHEMAP_SRC_ELEMENT_2_1, NULL, nameAttr, "ref", "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008483 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008484 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008485 * Check for illegal attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008486 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008487 attr = node->properties;
8488 while (attr != NULL) {
8489 if (attr->ns == NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008490 if (xmlStrEqual(attr->name, BAD_CAST "ref") ||
8491 xmlStrEqual(attr->name, BAD_CAST "name") ||
8492 xmlStrEqual(attr->name, BAD_CAST "id") ||
8493 xmlStrEqual(attr->name, BAD_CAST "maxOccurs") ||
8494 xmlStrEqual(attr->name, BAD_CAST "minOccurs"))
8495 {
8496 attr = attr->next;
8497 continue;
8498 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008499 /* SPEC (3.3.3 : 2.2) */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008500 xmlSchemaPCustomAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008501 XML_SCHEMAP_SRC_ELEMENT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008502 NULL, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008503 "Only the attributes 'minOccurs', 'maxOccurs' and "
8504 "'id' are allowed in addition to 'ref'");
8505 break;
8506 }
8507 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
8508 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008509 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008510 }
8511 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008512 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008513 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008514 * No children except <annotation> expected.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008515 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008516 if (child != NULL) {
8517 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008518 NULL, node, child, NULL, "(annotation?)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008519 }
8520 if ((min == 0) && (max == 0))
8521 goto return_null;
8522 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008523 * Create the reference item and attach it to the particle.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008524 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008525 refer = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_ELEMENT,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008526 ref, refNs);
8527 if (refer == NULL)
8528 goto return_null;
8529 particle->children = (xmlSchemaTreeItemPtr) refer;
8530 particle->annot = annot;
8531 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008532 * Add the particle to pending components, since the reference
8533 * need to be resolved.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008534 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008535 WXS_ADD_PENDING(ctxt, particle);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008536 return ((xmlSchemaBasicItemPtr) particle);
8537 }
8538 /*
8539 * The declaration part ===============================================
8540 */
8541declaration_part:
8542 {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008543 const xmlChar *ns = NULL, *fixed, *name, *attrValue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008544 xmlSchemaIDCPtr curIDC = NULL, lastIDC = NULL;
8545
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008546 if (xmlSchemaPValAttrNode(ctxt, NULL, nameAttr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008547 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008548 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008549 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008550 * Evaluate the target namespace.
8551 */
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008552 if (topLevel) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008553 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008554 } else {
8555 attr = xmlSchemaGetPropNode(node, "form");
8556 if (attr != NULL) {
8557 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8558 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008559 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008560 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008561 xmlSchemaPSimpleTypeErr(ctxt,
8562 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
8563 NULL, (xmlNodePtr) attr,
8564 NULL, "(qualified | unqualified)",
8565 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008566 }
8567 } else if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008568 ns = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008569 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008570 decl = xmlSchemaAddElement(ctxt, name, ns, node, topLevel);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008571 if (decl == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008572 goto return_null;
William M. Bracke7091952004-05-11 15:09:58 +00008573 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008574 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008575 * Check for illegal attributes.
8576 */
William M. Bracke7091952004-05-11 15:09:58 +00008577 attr = node->properties;
8578 while (attr != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008579 if (attr->ns == NULL) {
8580 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8581 (!xmlStrEqual(attr->name, BAD_CAST "type")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008582 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008583 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008584 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008585 (!xmlStrEqual(attr->name, BAD_CAST "block")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008586 (!xmlStrEqual(attr->name, BAD_CAST "nillable")))
8587 {
8588 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008589 if ((!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008590 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008591 (!xmlStrEqual(attr->name, BAD_CAST "form")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008592 {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008593 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008594 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008595 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008596 } else if ((!xmlStrEqual(attr->name, BAD_CAST "final")) &&
8597 (!xmlStrEqual(attr->name, BAD_CAST "abstract")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008598 (!xmlStrEqual(attr->name, BAD_CAST "substitutionGroup"))) {
8599
8600 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008601 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008602 }
8603 }
8604 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008605
Daniel Veillardc0826a72004-08-10 14:17:33 +00008606 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008607 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Bracke7091952004-05-11 15:09:58 +00008608 }
8609 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008610 }
William M. Bracke7091952004-05-11 15:09:58 +00008611 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008612 * Extract/validate attributes.
8613 */
8614 if (topLevel) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008615 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008616 * Process top attributes of global element declarations here.
8617 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008618 decl->flags |= XML_SCHEMAS_ELEM_GLOBAL;
8619 decl->flags |= XML_SCHEMAS_ELEM_TOPLEVEL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008620 xmlSchemaPValAttrQName(ctxt, schema,
8621 NULL, node, "substitutionGroup",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008622 &(decl->substGroupNs), &(decl->substGroup));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008623 if (xmlGetBooleanProp(ctxt, node, "abstract", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008624 decl->flags |= XML_SCHEMAS_ELEM_ABSTRACT;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008625 /*
8626 * Attribute "final".
8627 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008628 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008629 if (attr == NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008630 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
8631 decl->flags |= XML_SCHEMAS_ELEM_FINAL_EXTENSION;
8632 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
8633 decl->flags |= XML_SCHEMAS_ELEM_FINAL_RESTRICTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008634 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008635 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8636 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008637 -1,
8638 XML_SCHEMAS_ELEM_FINAL_EXTENSION,
8639 XML_SCHEMAS_ELEM_FINAL_RESTRICTION, -1, -1, -1) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008640 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008641 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008642 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008643 NULL, "(#all | List of (extension | restriction))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00008644 attrValue, NULL, NULL, NULL);
8645 }
8646 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008647 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008648 /*
8649 * Attribute "block".
8650 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008651 attr = xmlSchemaGetPropNode(node, "block");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008652 if (attr == NULL) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00008653 /*
8654 * Apply default "block" values.
8655 */
8656 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
8657 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_RESTRICTION;
8658 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
8659 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_EXTENSION;
8660 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
8661 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008662 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008663 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8664 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008665 -1,
8666 XML_SCHEMAS_ELEM_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008667 XML_SCHEMAS_ELEM_BLOCK_RESTRICTION,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008668 XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION, -1, -1) != 0) {
8669 xmlSchemaPSimpleTypeErr(ctxt,
8670 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008671 NULL, (xmlNodePtr) attr,
William M. Brack2f2a6632004-08-20 23:09:47 +00008672 NULL, "(#all | List of (extension | "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008673 "restriction | substitution))", attrValue,
8674 NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008675 }
8676 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008677 if (xmlGetBooleanProp(ctxt, node, "nillable", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008678 decl->flags |= XML_SCHEMAS_ELEM_NILLABLE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008679
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008680 attr = xmlSchemaGetPropNode(node, "type");
8681 if (attr != NULL) {
8682 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008683 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008684 &(decl->namedTypeNs), &(decl->namedType));
8685 xmlSchemaCheckReference(ctxt, schema, node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008686 attr, decl->namedTypeNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008687 }
8688 decl->value = xmlSchemaGetProp(ctxt, node, "default");
8689 attr = xmlSchemaGetPropNode(node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008690 if (attr != NULL) {
8691 fixed = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008692 if (decl->value != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008693 /*
8694 * 3.3.3 : 1
8695 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008696 */
8697 xmlSchemaPMutualExclAttrErr(ctxt,
8698 XML_SCHEMAP_SRC_ELEMENT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008699 NULL, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008700 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008701 decl->flags |= XML_SCHEMAS_ELEM_FIXED;
8702 decl->value = fixed;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008703 }
William M. Bracke7091952004-05-11 15:09:58 +00008704 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008705 /*
8706 * And now for the children...
8707 */
William M. Bracke7091952004-05-11 15:09:58 +00008708 if (IS_SCHEMA(child, "complexType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008709 /*
8710 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008711 * "type" and either <simpleType> or <complexType> are mutually
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008712 * exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008713 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008714 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008715 xmlSchemaPContentErr(ctxt,
8716 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008717 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008718 "The attribute 'type' and the <complexType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008719 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008720 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008721 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseComplexType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008722 child = child->next;
8723 } else if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008724 /*
8725 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008726 * "type" and either <simpleType> or <complexType> are
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008727 * mutually exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008728 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008729 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008730 xmlSchemaPContentErr(ctxt,
8731 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008732 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008733 "The attribute 'type' and the <simpleType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008734 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008735 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008736 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008737 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008738 }
William M. Bracke7091952004-05-11 15:09:58 +00008739 while ((IS_SCHEMA(child, "unique")) ||
Daniel Veillardc0826a72004-08-10 14:17:33 +00008740 (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008741 if (IS_SCHEMA(child, "unique")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008742 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008743 XML_SCHEMA_TYPE_IDC_UNIQUE, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008744 } else if (IS_SCHEMA(child, "key")) {
8745 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008746 XML_SCHEMA_TYPE_IDC_KEY, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008747 } else if (IS_SCHEMA(child, "keyref")) {
8748 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008749 XML_SCHEMA_TYPE_IDC_KEYREF, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008750 }
8751 if (lastIDC != NULL)
8752 lastIDC->next = curIDC;
8753 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008754 decl->idcs = (void *) curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008755 lastIDC = curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008756 child = child->next;
William M. Bracke7091952004-05-11 15:09:58 +00008757 }
8758 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008759 xmlSchemaPContentErr(ctxt,
8760 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008761 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008762 NULL, "(annotation?, ((simpleType | complexType)?, "
8763 "(unique | key | keyref)*))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008764 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008765 decl->annot = annot;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008766 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008767 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008768 * NOTE: Element Declaration Representation OK 4. will be checked at a
Daniel Veillardc0826a72004-08-10 14:17:33 +00008769 * different layer.
8770 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008771 FREE_AND_NULL(des)
8772 if (topLevel)
8773 return ((xmlSchemaBasicItemPtr) decl);
8774 else {
8775 particle->children = (xmlSchemaTreeItemPtr) decl;
8776 return ((xmlSchemaBasicItemPtr) particle);
8777 }
8778
8779return_null:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008780 FREE_AND_NULL(des);
8781 if (annot != NULL) {
8782 if (particle != NULL)
8783 particle->annot = NULL;
8784 if (decl != NULL)
8785 decl->annot = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008786 xmlSchemaFreeAnnot(annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008787 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008788 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00008789}
8790
8791/**
8792 * xmlSchemaParseUnion:
8793 * @ctxt: a schema validation context
8794 * @schema: the schema being built
8795 * @node: a subtree containing XML Schema informations
8796 *
8797 * parse a XML schema Union definition
8798 * *WARNING* this interface is highly subject to change
8799 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008800 * Returns -1 in case of internal error, 0 in case of success and a positive
8801 * error code otherwise.
Daniel Veillard4255d502002-04-16 15:50:10 +00008802 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008803static int
Daniel Veillard4255d502002-04-16 15:50:10 +00008804xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008805 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008806{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008807 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008808 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008809 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008810 const xmlChar *cur = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00008811
8812 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008813 return (-1);
8814 /* Not a component, don't create it. */
8815 type = ctxt->ctxtType;
8816 /*
8817 * Mark the simple type as being of variety "union".
8818 */
8819 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008820 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008821 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
8822 * then the ·simple ur-type definition·."
8823 */
8824 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008825 /*
8826 * Check for illegal attributes.
8827 */
8828 attr = node->properties;
8829 while (attr != NULL) {
8830 if (attr->ns == NULL) {
8831 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8832 (!xmlStrEqual(attr->name, BAD_CAST "memberTypes"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008833 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008834 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008835 }
8836 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008837 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008838 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008839 }
8840 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008841 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008842 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +00008843 /*
8844 * Attribute "memberTypes". This is a list of QNames.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008845 * TODO: Check the value to contain anything.
William M. Brack2f2a6632004-08-20 23:09:47 +00008846 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008847 attr = xmlSchemaGetPropNode(node, "memberTypes");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008848 if (attr != NULL) {
8849 const xmlChar *end;
8850 xmlChar *tmp;
8851 const xmlChar *localName, *nsName;
8852 xmlSchemaTypeLinkPtr link, lastLink = NULL;
8853 xmlSchemaQNameRefPtr ref;
8854
8855 cur = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008856 type->base = cur;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008857 do {
8858 while (IS_BLANK_CH(*cur))
8859 cur++;
8860 end = cur;
8861 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
8862 end++;
8863 if (end == cur)
8864 break;
8865 tmp = xmlStrndup(cur, end - cur);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008866 if (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008867 NULL, attr, BAD_CAST tmp, &nsName, &localName) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008868 /*
8869 * Create the member type link.
8870 */
8871 link = (xmlSchemaTypeLinkPtr)
8872 xmlMalloc(sizeof(xmlSchemaTypeLink));
8873 if (link == NULL) {
8874 xmlSchemaPErrMemory(ctxt, "xmlSchemaParseUnion, "
8875 "allocating a type link", NULL);
8876 return (-1);
8877 }
8878 link->type = NULL;
8879 link->next = NULL;
8880 if (lastLink == NULL)
8881 type->memberTypes = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008882 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008883 lastLink->next = link;
8884 lastLink = link;
8885 /*
8886 * Create a reference item.
8887 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008888 ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_SIMPLE,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008889 localName, nsName);
8890 if (ref == NULL) {
8891 FREE_AND_NULL(tmp)
8892 return (-1);
8893 }
8894 /*
8895 * Assign the reference to the link, it will be resolved
8896 * later during fixup of the union simple type.
8897 */
8898 link->type = (xmlSchemaTypePtr) ref;
8899 }
8900 FREE_AND_NULL(tmp)
8901 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008902 } while (*cur != 0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008903
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008904 }
William M. Brack2f2a6632004-08-20 23:09:47 +00008905 /*
8906 * And now for the children...
8907 */
Daniel Veillard4255d502002-04-16 15:50:10 +00008908 child = node->children;
8909 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008910 /*
8911 * Add the annotation to the simple type ancestor.
8912 */
8913 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008914 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008915 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00008916 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008917 if (IS_SCHEMA(child, "simpleType")) {
8918 xmlSchemaTypePtr subtype, last = NULL;
8919
8920 /*
8921 * Anchor the member types in the "subtypes" field of the
8922 * simple type.
8923 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008924 while (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008925 subtype = (xmlSchemaTypePtr)
8926 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
8927 if (subtype != NULL) {
8928 if (last == NULL) {
8929 type->subtypes = subtype;
8930 last = subtype;
8931 } else {
8932 last->next = subtype;
8933 last = subtype;
8934 }
8935 last->next = NULL;
8936 }
8937 child = child->next;
8938 }
Daniel Veillard4255d502002-04-16 15:50:10 +00008939 }
8940 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00008941 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008942 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008943 NULL, node, child, NULL, "(annotation?, simpleType*)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008944 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008945 if ((attr == NULL) && (type->subtypes == NULL)) {
8946 /*
8947 * src-union-memberTypes-or-simpleTypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008948 * Either the memberTypes [attribute] of the <union> element must
8949 * be non-empty or there must be at least one simpleType [child].
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008950 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008951 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008952 XML_SCHEMAP_SRC_UNION_MEMBERTYPES_OR_SIMPLETYPES,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008953 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008954 "Either the attribute 'memberTypes' or "
8955 "at least one <simpleType> child must be present", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008956 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008957 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00008958}
8959
8960/**
8961 * xmlSchemaParseList:
8962 * @ctxt: a schema validation context
8963 * @schema: the schema being built
8964 * @node: a subtree containing XML Schema informations
8965 *
8966 * parse a XML schema List definition
8967 * *WARNING* this interface is highly subject to change
8968 *
William M. Bracke7091952004-05-11 15:09:58 +00008969 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00008970 * 1 in case of success.
8971 */
8972static xmlSchemaTypePtr
8973xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008974 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008975{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008976 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008977 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008978 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00008979
8980 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
8981 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008982 /* Not a component, don't create it. */
8983 type = ctxt->ctxtType;
8984 /*
8985 * Mark the type as being of variety "list".
8986 */
8987 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008988 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008989 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
8990 * then the ·simple ur-type definition·."
8991 */
8992 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008993 /*
8994 * Check for illegal attributes.
8995 */
8996 attr = node->properties;
8997 while (attr != NULL) {
8998 if (attr->ns == NULL) {
8999 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9000 (!xmlStrEqual(attr->name, BAD_CAST "itemType"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009001 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009002 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009003 }
9004 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009005 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009006 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009007 }
9008 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009009 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009010
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009011 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009012
William M. Brack2f2a6632004-08-20 23:09:47 +00009013 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009014 * Attribute "itemType". NOTE that we will use the "ref" and "refNs"
9015 * fields for holding the reference to the itemType.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009016 *
9017 * REVAMP TODO: Use the "base" and "baseNs" fields, since we will remove
9018 * the "ref" fields.
William M. Brack2f2a6632004-08-20 23:09:47 +00009019 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009020 xmlSchemaPValAttrQName(ctxt, schema, NULL,
9021 node, "itemType", &(type->baseNs), &(type->base));
William M. Brack2f2a6632004-08-20 23:09:47 +00009022 /*
9023 * And now for the children...
9024 */
Daniel Veillard4255d502002-04-16 15:50:10 +00009025 child = node->children;
9026 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009027 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009028 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009029 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009030 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009031 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009032 /*
9033 * src-list-itemType-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009034 * Either the itemType [attribute] or the <simpleType> [child] of
9035 * the <list> element must be present, but not both.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009036 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009037 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009038 xmlSchemaPCustomErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009039 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009040 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +00009041 "The attribute 'itemType' and the <simpleType> child "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009042 "are mutually exclusive", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009043 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009044 type->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Brack2f2a6632004-08-20 23:09:47 +00009045 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009046 child = child->next;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009047 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009048 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009049 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009050 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009051 "Either the attribute 'itemType' or the <simpleType> child "
9052 "must be present", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009053 }
9054 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009055 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009056 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009057 NULL, node, child, NULL, "(annotation?, simpleType?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00009058 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009059 if ((type->base == NULL) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009060 (type->subtypes == NULL) &&
9061 (xmlSchemaGetPropNode(node, "itemType") == NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009062 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009063 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009064 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009065 "Either the attribute 'itemType' or the <simpleType> child "
9066 "must be present", NULL);
9067 }
9068 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009069}
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009070
Daniel Veillard4255d502002-04-16 15:50:10 +00009071/**
9072 * xmlSchemaParseSimpleType:
9073 * @ctxt: a schema validation context
9074 * @schema: the schema being built
9075 * @node: a subtree containing XML Schema informations
9076 *
9077 * parse a XML schema Simple Type definition
9078 * *WARNING* this interface is highly subject to change
9079 *
William M. Bracke7091952004-05-11 15:09:58 +00009080 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillardc0826a72004-08-10 14:17:33 +00009081 * 1 in case of success.
Daniel Veillard4255d502002-04-16 15:50:10 +00009082 */
9083static xmlSchemaTypePtr
9084xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00009085 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00009086{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009087 xmlSchemaTypePtr type, oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009088 xmlNodePtr child = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009089 const xmlChar *attrValue = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009090 xmlAttrPtr attr;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009091 int hasRestriction = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00009092
9093 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9094 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009095
Daniel Veillardc0826a72004-08-10 14:17:33 +00009096 if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009097 attr = xmlSchemaGetPropNode(node, "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009098 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009099 xmlSchemaPMissingAttrErr(ctxt,
9100 XML_SCHEMAP_S4S_ATTR_MISSING,
9101 NULL, node,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009102 "name", NULL);
9103 return (NULL);
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009104 } else {
9105 if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009106 NULL, attr,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009107 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0)
9108 return (NULL);
9109 /*
9110 * Skip built-in types.
9111 */
9112 if (ctxt->isS4S) {
9113 xmlSchemaTypePtr biType;
9114
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009115 if (ctxt->isRedefine) {
9116 /*
9117 * REDEFINE: Disallow redefinition of built-in-types.
9118 * TODO: It seems that the spec does not say anything
9119 * about this case.
9120 */
9121 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009122 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009123 "Redefinition of built-in simple types is not "
9124 "supported", NULL);
9125 return(NULL);
9126 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009127 biType = xmlSchemaGetPredefinedType(attrValue, xmlSchemaNs);
9128 if (biType != NULL)
9129 return (biType);
9130 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00009131 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009132 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009133 /*
9134 * TargetNamespace:
9135 * SPEC "The ·actual value· of the targetNamespace [attribute]
9136 * of the <schema> ancestor element information item if present,
9137 * otherwise ·absent·.
9138 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00009139 if (topLevel == 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009140#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009141 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009142#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +00009143 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00009144 * Parse as local simple type definition.
Daniel Veillard01fa6152004-06-29 17:04:39 +00009145 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009146#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00009147 snprintf(buf, 39, "#ST%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009148 type = xmlSchemaAddType(ctxt, schema,
9149 XML_SCHEMA_TYPE_SIMPLE,
9150 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009151 ctxt->targetNamespace, node, 0);
9152#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009153 type = xmlSchemaAddType(ctxt, schema,
9154 XML_SCHEMA_TYPE_SIMPLE,
9155 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009156#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +00009157 if (type == NULL)
9158 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009159 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009160 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009161 /*
9162 * Check for illegal attributes.
9163 */
9164 attr = node->properties;
9165 while (attr != NULL) {
9166 if (attr->ns == NULL) {
9167 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009168 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009169 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillard01fa6152004-06-29 17:04:39 +00009170 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009171 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009172 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009173 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009174 }
9175 attr = attr->next;
Daniel Veillard01fa6152004-06-29 17:04:39 +00009176 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009177 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00009178 /*
9179 * Parse as global simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009180 *
9181 * Note that attrValue is the value of the attribute "name" here.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009182 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009183 type = xmlSchemaAddType(ctxt, schema, XML_SCHEMA_TYPE_SIMPLE,
9184 attrValue, ctxt->targetNamespace, node, 1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009185 if (type == NULL)
9186 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009187 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009188 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009189 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
9190 /*
9191 * Check for illegal attributes.
9192 */
9193 attr = node->properties;
9194 while (attr != NULL) {
9195 if (attr->ns == NULL) {
9196 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9197 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009198 (!xmlStrEqual(attr->name, BAD_CAST "final"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009199 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009200 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009201 }
9202 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009203 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009204 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009205 }
9206 attr = attr->next;
9207 }
9208 /*
9209 * Attribute "final".
9210 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009211 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009212 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009213 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9214 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
9215 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9216 type->flags |= XML_SCHEMAS_TYPE_FINAL_LIST;
9217 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9218 type->flags |= XML_SCHEMAS_TYPE_FINAL_UNION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009219 } else {
9220 attrValue = xmlSchemaGetProp(ctxt, node, "final");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009221 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
9222 -1, -1, XML_SCHEMAS_TYPE_FINAL_RESTRICTION, -1,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009223 XML_SCHEMAS_TYPE_FINAL_LIST,
9224 XML_SCHEMAS_TYPE_FINAL_UNION) != 0) {
9225
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009226 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009227 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009228 WXS_BASIC_CAST type, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009229 NULL, "(#all | List of (list | union | restriction)",
Daniel Veillardc0826a72004-08-10 14:17:33 +00009230 attrValue, NULL, NULL, NULL);
9231 }
9232 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009233 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009234 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009235 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009236 /*
9237 * And now for the children...
9238 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009239 oldCtxtType = ctxt->ctxtType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009240
Daniel Veillardc0826a72004-08-10 14:17:33 +00009241 ctxt->ctxtType = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009242
Daniel Veillard4255d502002-04-16 15:50:10 +00009243 child = node->children;
9244 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009245 type->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009246 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009247 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009248 if (child == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009249 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009250 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009251 "(annotation?, (restriction | list | union))");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009252 } else if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009253 xmlSchemaParseRestriction(ctxt, schema, child,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009254 XML_SCHEMA_TYPE_SIMPLE);
9255 hasRestriction = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009256 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009257 } else if (IS_SCHEMA(child, "list")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009258 xmlSchemaParseList(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009259 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009260 } else if (IS_SCHEMA(child, "union")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009261 xmlSchemaParseUnion(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009262 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009263 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009264 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009265 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009266 NULL, node, child, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009267 "(annotation?, (restriction | list | union))");
Daniel Veillard4255d502002-04-16 15:50:10 +00009268 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009269 /*
9270 * REDEFINE: SPEC src-redefine (5)
9271 * "Within the [children], each <simpleType> must have a
9272 * <restriction> among its [children] ... the ·actual value· of whose
9273 * base [attribute] must be the same as the ·actual value· of its own
9274 * name attribute plus target namespace;"
9275 */
9276 if (topLevel && ctxt->isRedefine && (! hasRestriction)) {
9277 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009278 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009279 "<simpleType> must have a <restriction> child", NULL);
9280 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009281
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009282 ctxt->ctxtType = oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009283 return (type);
9284}
9285
Daniel Veillard4255d502002-04-16 15:50:10 +00009286/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009287 * xmlSchemaParseModelGroupDefRef:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009288 * @ctxt: the parser context
9289 * @schema: the schema being built
9290 * @node: the node
Daniel Veillard4255d502002-04-16 15:50:10 +00009291 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009292 * Parses a reference to a model group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00009293 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009294 * We will return a particle component with a qname-component or
9295 * NULL in case of an error.
Daniel Veillard4255d502002-04-16 15:50:10 +00009296 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009297static xmlSchemaTreeItemPtr
9298xmlSchemaParseModelGroupDefRef(xmlSchemaParserCtxtPtr ctxt,
9299 xmlSchemaPtr schema,
9300 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00009301{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009302 xmlSchemaParticlePtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009303 xmlNodePtr child = NULL;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009304 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009305 const xmlChar *ref = NULL, *refNs = NULL;
9306 int min, max;
9307
9308 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009309 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009310
9311 attr = xmlSchemaGetPropNode(node, "ref");
9312 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009313 xmlSchemaPMissingAttrErr(ctxt,
9314 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009315 NULL, node, "ref", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009316 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009317 } else if (xmlSchemaPValAttrNodeQName(ctxt, schema, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009318 attr, &refNs, &ref) != 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009319 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009320 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009321 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009322 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009323 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009324 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009325 /*
9326 * Check for illegal attributes.
9327 */
9328 attr = node->properties;
9329 while (attr != NULL) {
9330 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009331 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009332 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9333 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
9334 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009335 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009336 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009337 }
9338 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009339 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009340 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009341 }
9342 attr = attr->next;
9343 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009344 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009345 item = xmlSchemaAddParticle(ctxt, schema, node, min, max);
9346 if (item == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009347 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009348 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009349 * Create a qname-reference and set as the term; it will be substituted
9350 * for the model group after the reference has been resolved.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009351 */
9352 item->children = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009353 xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_GROUP, ref, refNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009354 xmlSchemaPCheckParticleCorrect_2(ctxt, item, node, min, max);
9355 /*
9356 * And now for the children...
9357 */
9358 child = node->children;
9359 /* TODO: Is annotation even allowed for a model group reference? */
9360 if (IS_SCHEMA(child, "annotation")) {
9361 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009362 * TODO: What to do exactly with the annotation?
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009363 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009364 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009365 child = child->next;
9366 }
9367 if (child != NULL) {
9368 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009369 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009370 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009371 "(annotation?)");
9372 }
9373 /*
9374 * Corresponds to no component at all if minOccurs==maxOccurs==0.
9375 */
9376 if ((min == 0) && (max == 0))
9377 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009378
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009379 return ((xmlSchemaTreeItemPtr) item);
9380}
9381
9382/**
9383 * xmlSchemaParseModelGroupDefinition:
9384 * @ctxt: a schema validation context
9385 * @schema: the schema being built
9386 * @node: a subtree containing XML Schema informations
9387 *
9388 * Parses a XML schema model group definition.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009389 *
9390 * Note that the contraint src-redefine (6.2) can't be applied until
9391 * references have been resolved. So we will do this at the
9392 * component fixup level.
9393 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009394 * *WARNING* this interface is highly subject to change
9395 *
9396 * Returns -1 in case of error, 0 if the declaration is improper and
9397 * 1 in case of success.
9398 */
9399static xmlSchemaModelGroupDefPtr
9400xmlSchemaParseModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
9401 xmlSchemaPtr schema,
9402 xmlNodePtr node)
9403{
9404 xmlSchemaModelGroupDefPtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009405 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009406 xmlAttrPtr attr;
9407 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00009408
9409 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009410 return (NULL);
9411
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009412 attr = xmlSchemaGetPropNode(node, "name");
9413 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009414 xmlSchemaPMissingAttrErr(ctxt,
9415 XML_SCHEMAP_S4S_ATTR_MISSING,
9416 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009417 "name", NULL);
9418 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009419 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009420 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
9421 return (NULL);
9422 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009423 item = xmlSchemaAddModelGroupDefinition(ctxt, schema, name,
9424 ctxt->targetNamespace, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009425 if (item == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009426 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009427 /*
9428 * Check for illegal attributes.
9429 */
9430 attr = node->properties;
9431 while (attr != NULL) {
9432 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009433 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009434 (!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009435 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009436 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009437 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009438 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009439 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009440 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009441 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009442 attr = attr->next;
9443 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009444 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009445 /*
9446 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009447 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009448 child = node->children;
9449 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009450 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009451 child = child->next;
9452 }
9453 if (IS_SCHEMA(child, "all")) {
9454 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9455 XML_SCHEMA_TYPE_ALL, 0);
9456 child = child->next;
9457 } else if (IS_SCHEMA(child, "choice")) {
9458 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9459 XML_SCHEMA_TYPE_CHOICE, 0);
9460 child = child->next;
9461 } else if (IS_SCHEMA(child, "sequence")) {
9462 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9463 XML_SCHEMA_TYPE_SEQUENCE, 0);
9464 child = child->next;
9465 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009466
9467
9468
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009469 if (child != NULL) {
9470 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009471 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009472 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009473 "(annotation?, (all | choice | sequence)?)");
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009474 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009475 return (item);
Daniel Veillard4255d502002-04-16 15:50:10 +00009476}
9477
9478/**
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009479 * xmlSchemaCleanupDoc:
9480 * @ctxt: a schema validation context
9481 * @node: the root of the document.
9482 *
9483 * removes unwanted nodes in a schemas document tree
9484 */
9485static void
9486xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root)
9487{
9488 xmlNodePtr delete, cur;
9489
9490 if ((ctxt == NULL) || (root == NULL)) return;
9491
9492 /*
9493 * Remove all the blank text nodes
9494 */
9495 delete = NULL;
9496 cur = root;
9497 while (cur != NULL) {
9498 if (delete != NULL) {
9499 xmlUnlinkNode(delete);
9500 xmlFreeNode(delete);
9501 delete = NULL;
9502 }
9503 if (cur->type == XML_TEXT_NODE) {
9504 if (IS_BLANK_NODE(cur)) {
9505 if (xmlNodeGetSpacePreserve(cur) != 1) {
9506 delete = cur;
9507 }
9508 }
9509 } else if ((cur->type != XML_ELEMENT_NODE) &&
9510 (cur->type != XML_CDATA_SECTION_NODE)) {
9511 delete = cur;
9512 goto skip_children;
9513 }
9514
9515 /*
9516 * Skip to next node
9517 */
9518 if (cur->children != NULL) {
9519 if ((cur->children->type != XML_ENTITY_DECL) &&
9520 (cur->children->type != XML_ENTITY_REF_NODE) &&
9521 (cur->children->type != XML_ENTITY_NODE)) {
9522 cur = cur->children;
9523 continue;
9524 }
9525 }
9526 skip_children:
9527 if (cur->next != NULL) {
9528 cur = cur->next;
9529 continue;
9530 }
9531
9532 do {
9533 cur = cur->parent;
9534 if (cur == NULL)
9535 break;
9536 if (cur == root) {
9537 cur = NULL;
9538 break;
9539 }
9540 if (cur->next != NULL) {
9541 cur = cur->next;
9542 break;
9543 }
9544 } while (cur != NULL);
9545 }
9546 if (delete != NULL) {
9547 xmlUnlinkNode(delete);
9548 xmlFreeNode(delete);
9549 delete = NULL;
9550 }
9551}
9552
William M. Brack2f2a6632004-08-20 23:09:47 +00009553
William M. Brack2f2a6632004-08-20 23:09:47 +00009554static void
9555xmlSchemaClearSchemaDefaults(xmlSchemaPtr schema)
9556{
9557 if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
9558 schema->flags ^= XML_SCHEMAS_QUALIF_ELEM;
9559
9560 if (schema->flags & XML_SCHEMAS_QUALIF_ATTR)
9561 schema->flags ^= XML_SCHEMAS_QUALIF_ATTR;
9562
9563 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
9564 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_EXTENSION;
9565 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9566 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION;
9567 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9568 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_LIST;
9569 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9570 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_UNION;
9571
9572 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
9573 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION;
9574 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
9575 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION;
9576 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
9577 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION;
9578}
9579
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009580static int
9581xmlSchemaParseSchemaElement(xmlSchemaParserCtxtPtr ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009582 xmlSchemaPtr schema,
9583 xmlNodePtr node)
9584{
9585 xmlAttrPtr attr;
9586 const xmlChar *val;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009587 int res = 0, oldErrs = ctxt->nberrors;
William M. Brack2f2a6632004-08-20 23:09:47 +00009588
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009589 /*
9590 * Those flags should be moved to the parser context flags,
9591 * since they are not visible at the component level. I.e.
9592 * they are used if processing schema *documents* only.
9593 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009594 res = xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009595 HFAILURE;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009596
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009597 /*
9598 * Since the version is of type xs:token, we won't bother to
9599 * check it.
9600 */
9601 /* REMOVED:
9602 attr = xmlSchemaGetPropNode(node, "version");
9603 if (attr != NULL) {
9604 res = xmlSchemaPValAttrNode(ctxt, NULL, NULL, attr,
9605 xmlSchemaGetBuiltInType(XML_SCHEMAS_TOKEN), &val);
9606 HFAILURE;
9607 }
9608 */
9609 attr = xmlSchemaGetPropNode(node, "targetNamespace");
9610 if (attr != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009611 res = xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009612 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
9613 HFAILURE;
9614 if (res != 0) {
9615 ctxt->stop = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
9616 goto exit;
9617 }
9618 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009619 attr = xmlSchemaGetPropNode(node, "elementFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009620 if (attr != NULL) {
9621 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009622 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9623 XML_SCHEMAS_QUALIF_ELEM);
9624 HFAILURE;
9625 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009626 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009627 XML_SCHEMAP_ELEMFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009628 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009629 "(qualified | unqualified)", val, NULL, NULL, NULL);
9630 }
9631 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009632 attr = xmlSchemaGetPropNode(node, "attributeFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009633 if (attr != NULL) {
9634 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009635 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9636 XML_SCHEMAS_QUALIF_ATTR);
9637 HFAILURE;
9638 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009639 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009640 XML_SCHEMAP_ATTRFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009641 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009642 "(qualified | unqualified)", val, NULL, NULL, NULL);
9643 }
9644 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009645 attr = xmlSchemaGetPropNode(node, "finalDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009646 if (attr != NULL) {
9647 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009648 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009649 XML_SCHEMAS_FINAL_DEFAULT_EXTENSION,
9650 XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION,
9651 -1,
9652 XML_SCHEMAS_FINAL_DEFAULT_LIST,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009653 XML_SCHEMAS_FINAL_DEFAULT_UNION);
9654 HFAILURE;
9655 if (res != 0) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009656 xmlSchemaPSimpleTypeErr(ctxt,
9657 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009658 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009659 "(#all | List of (extension | restriction | list | union))",
9660 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009661 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009662 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009663 attr = xmlSchemaGetPropNode(node, "blockDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009664 if (attr != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009665 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
9666 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009667 XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION,
9668 XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009669 XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION, -1, -1);
9670 HFAILURE;
9671 if (res != 0) {
9672 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009673 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009674 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009675 "(#all | List of (extension | restriction | substitution))",
9676 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009677 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009678 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009679
9680exit:
9681 if (oldErrs != ctxt->nberrors)
9682 res = ctxt->err;
9683 return(res);
9684exit_failure:
9685 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +00009686}
9687
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009688/**
9689 * xmlSchemaParseSchemaTopLevel:
9690 * @ctxt: a schema validation context
9691 * @schema: the schemas
9692 * @nodes: the list of top level nodes
9693 *
9694 * Returns the internal XML Schema structure built from the resource or
9695 * NULL in case of error
9696 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009697static int
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009698xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt,
9699 xmlSchemaPtr schema, xmlNodePtr nodes)
9700{
9701 xmlNodePtr child;
9702 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009703 int res = 0, oldErrs, tmpOldErrs;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009704
9705 if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009706 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009707
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009708 oldErrs = ctxt->nberrors;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009709 child = nodes;
9710 while ((IS_SCHEMA(child, "include")) ||
9711 (IS_SCHEMA(child, "import")) ||
9712 (IS_SCHEMA(child, "redefine")) ||
9713 (IS_SCHEMA(child, "annotation"))) {
9714 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009715 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009716 if (schema->annot == NULL)
9717 schema->annot = annot;
9718 else
9719 xmlSchemaFreeAnnot(annot);
9720 } else if (IS_SCHEMA(child, "import")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009721 tmpOldErrs = ctxt->nberrors;
9722 res = xmlSchemaParseImport(ctxt, schema, child);
9723 HFAILURE;
9724 HSTOP(ctxt);
9725 if (tmpOldErrs != ctxt->nberrors)
9726 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009727 } else if (IS_SCHEMA(child, "include")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009728 tmpOldErrs = ctxt->nberrors;
9729 res = xmlSchemaParseInclude(ctxt, schema, child);
9730 HFAILURE;
9731 HSTOP(ctxt);
9732 if (tmpOldErrs != ctxt->nberrors)
9733 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009734 } else if (IS_SCHEMA(child, "redefine")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009735 tmpOldErrs = ctxt->nberrors;
9736 res = xmlSchemaParseRedefine(ctxt, schema, child);
9737 HFAILURE;
9738 HSTOP(ctxt);
9739 if (tmpOldErrs != ctxt->nberrors)
9740 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009741 }
9742 child = child->next;
9743 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009744 /*
9745 * URGENT TODO: Change the functions to return int results.
9746 * We need especially to catch internal errors.
9747 */
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009748 while (child != NULL) {
9749 if (IS_SCHEMA(child, "complexType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009750 xmlSchemaParseComplexType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009751 child = child->next;
9752 } else if (IS_SCHEMA(child, "simpleType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009753 xmlSchemaParseSimpleType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009754 child = child->next;
9755 } else if (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009756 xmlSchemaParseElement(ctxt, schema, child, NULL, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009757 child = child->next;
9758 } else if (IS_SCHEMA(child, "attribute")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009759 xmlSchemaParseGlobalAttribute(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009760 child = child->next;
9761 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009762 xmlSchemaParseAttributeGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009763 child = child->next;
9764 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009765 xmlSchemaParseModelGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009766 child = child->next;
9767 } else if (IS_SCHEMA(child, "notation")) {
9768 xmlSchemaParseNotation(ctxt, schema, child);
9769 child = child->next;
9770 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009771 xmlSchemaPContentErr(ctxt,
9772 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009773 NULL, child->parent, child,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009774 NULL, "((include | import | redefine | annotation)*, "
9775 "(((simpleType | complexType | group | attributeGroup) "
9776 "| element | attribute | notation), annotation*)*)");
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009777 child = child->next;
9778 }
9779 while (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009780 /*
9781 * TODO: We should add all annotations.
9782 */
9783 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009784 if (schema->annot == NULL)
9785 schema->annot = annot;
9786 else
9787 xmlSchemaFreeAnnot(annot);
9788 child = child->next;
9789 }
9790 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009791exit:
Daniel Veillard01fa6152004-06-29 17:04:39 +00009792 ctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009793 if (oldErrs != ctxt->nberrors)
9794 res = ctxt->err;
9795 return(res);
9796exit_failure:
9797 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009798}
9799
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009800static xmlSchemaSchemaRelationPtr
9801xmlSchemaSchemaRelationCreate(void)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009802{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009803 xmlSchemaSchemaRelationPtr ret;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009804
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009805 ret = (xmlSchemaSchemaRelationPtr)
9806 xmlMalloc(sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009807 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009808 xmlSchemaPErrMemory(NULL, "allocating schema relation", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009809 return(NULL);
9810 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009811 memset(ret, 0, sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009812 return(ret);
9813}
9814
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009815#if 0
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009816static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009817xmlSchemaSchemaRelationFree(xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009818{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009819 xmlFree(rel);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009820}
9821#endif
9822
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009823static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009824xmlSchemaRedefListFree(xmlSchemaRedefPtr redef)
9825{
9826 xmlSchemaRedefPtr prev;
9827
9828 while (redef != NULL) {
9829 prev = redef;
9830 redef = redef->next;
9831 xmlFree(prev);
9832 }
9833}
9834
9835static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009836xmlSchemaConstructionCtxtFree(xmlSchemaConstructionCtxtPtr con)
9837{
9838 /*
9839 * After the construction context has been freed, there will be
9840 * no schema graph available any more. Only the schema buckets
9841 * will stay alive, which are put into the "schemasImports" and
9842 * "includes" slots of the xmlSchema.
9843 */
9844 if (con->buckets != NULL)
9845 xmlSchemaItemListFree(con->buckets);
9846 if (con->pending != NULL)
9847 xmlSchemaItemListFree(con->pending);
9848 if (con->substGroups != NULL)
9849 xmlHashFree(con->substGroups,
9850 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009851 if (con->redefs != NULL)
9852 xmlSchemaRedefListFree(con->redefs);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009853 if (con->dict != NULL)
9854 xmlDictFree(con->dict);
9855 xmlFree(con);
9856}
9857
9858static xmlSchemaConstructionCtxtPtr
9859xmlSchemaConstructionCtxtCreate(xmlDictPtr dict)
9860{
9861 xmlSchemaConstructionCtxtPtr ret;
9862
9863 ret = (xmlSchemaConstructionCtxtPtr)
9864 xmlMalloc(sizeof(xmlSchemaConstructionCtxt));
9865 if (ret == NULL) {
9866 xmlSchemaPErrMemory(NULL,
9867 "allocating schema construction context", NULL);
9868 return (NULL);
9869 }
9870 memset(ret, 0, sizeof(xmlSchemaConstructionCtxt));
9871
9872 ret->buckets = xmlSchemaItemListCreate();
9873 if (ret->buckets == NULL) {
9874 xmlSchemaPErrMemory(NULL,
9875 "allocating list of schema buckets", NULL);
9876 xmlFree(ret);
9877 return (NULL);
9878 }
9879 ret->pending = xmlSchemaItemListCreate();
9880 if (ret->pending == NULL) {
9881 xmlSchemaPErrMemory(NULL,
9882 "allocating list of pending global components", NULL);
9883 xmlSchemaConstructionCtxtFree(ret);
9884 return (NULL);
9885 }
9886 ret->dict = dict;
9887 xmlDictReference(dict);
9888 return(ret);
9889}
9890
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009891static xmlSchemaParserCtxtPtr
9892xmlSchemaParserCtxtCreate(void)
9893{
9894 xmlSchemaParserCtxtPtr ret;
9895
9896 ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
9897 if (ret == NULL) {
9898 xmlSchemaPErrMemory(NULL, "allocating schema parser context",
9899 NULL);
9900 return (NULL);
9901 }
9902 memset(ret, 0, sizeof(xmlSchemaParserCtxt));
9903 ret->type = XML_SCHEMA_CTXT_PARSER;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009904 ret->attrProhibs = xmlSchemaItemListCreate();
9905 if (ret->attrProhibs == NULL) {
9906 xmlFree(ret);
9907 return(NULL);
9908 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009909 return(ret);
9910}
9911
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009912/**
9913 * xmlSchemaNewParserCtxtUseDict:
9914 * @URL: the location of the schema
9915 * @dict: the dictionary to be used
9916 *
9917 * Create an XML Schemas parse context for that file/resource expected
9918 * to contain an XML Schemas file.
9919 *
9920 * Returns the parser context or NULL in case of error
9921 */
9922static xmlSchemaParserCtxtPtr
9923xmlSchemaNewParserCtxtUseDict(const char *URL, xmlDictPtr dict)
9924{
9925 xmlSchemaParserCtxtPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009926
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009927 ret = xmlSchemaParserCtxtCreate();
9928 if (ret == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009929 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009930 ret->dict = dict;
9931 xmlDictReference(dict);
9932 if (URL != NULL)
9933 ret->URL = xmlDictLookup(dict, (const xmlChar *) URL, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009934 return (ret);
9935}
9936
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009937static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009938xmlSchemaCreatePCtxtOnVCtxt(xmlSchemaValidCtxtPtr vctxt)
9939{
9940 if (vctxt->pctxt == NULL) {
9941 if (vctxt->schema != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009942 vctxt->pctxt =
9943 xmlSchemaNewParserCtxtUseDict("*", vctxt->schema->dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009944 else
9945 vctxt->pctxt = xmlSchemaNewParserCtxt("*");
9946 if (vctxt->pctxt == NULL) {
9947 VERROR_INT("xmlSchemaCreatePCtxtOnVCtxt",
9948 "failed to create a temp. parser context");
9949 return (-1);
9950 }
9951 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009952 xmlSchemaSetParserErrors(vctxt->pctxt, vctxt->error,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00009953 vctxt->warning, vctxt->errCtxt);
9954 xmlSchemaSetParserStructuredErrors(vctxt->pctxt, vctxt->serror,
9955 vctxt->errCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009956 }
9957 return (0);
9958}
9959
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009960/**
9961 * xmlSchemaGetSchemaBucket:
9962 * @pctxt: the schema parser context
9963 * @schemaLocation: the URI of the schema document
9964 *
9965 * Returns a schema bucket if it was already parsed.
9966 *
9967 * Returns a schema bucket if it was already parsed from
9968 * @schemaLocation, NULL otherwise.
9969 */
9970static xmlSchemaBucketPtr
9971xmlSchemaGetSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
9972 const xmlChar *schemaLocation)
9973{
9974 xmlSchemaBucketPtr cur;
9975 xmlSchemaItemListPtr list;
9976
9977 list = pctxt->constructor->buckets;
9978 if (list->nbItems == 0)
9979 return(NULL);
9980 else {
9981 int i;
9982 for (i = 0; i < list->nbItems; i++) {
9983 cur = (xmlSchemaBucketPtr) list->items[i];
9984 /* Pointer comparison! */
9985 if (cur->schemaLocation == schemaLocation)
9986 return(cur);
9987 }
9988 }
9989 return(NULL);
9990}
9991
9992static xmlSchemaBucketPtr
9993xmlSchemaGetChameleonSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
9994 const xmlChar *schemaLocation,
9995 const xmlChar *targetNamespace)
9996{
9997 xmlSchemaBucketPtr cur;
9998 xmlSchemaItemListPtr list;
9999
10000 list = pctxt->constructor->buckets;
10001 if (list->nbItems == 0)
10002 return(NULL);
10003 else {
10004 int i;
10005 for (i = 0; i < list->nbItems; i++) {
10006 cur = (xmlSchemaBucketPtr) list->items[i];
10007 /* Pointer comparison! */
10008 if ((cur->origTargetNamespace == NULL) &&
10009 (cur->schemaLocation == schemaLocation) &&
10010 (cur->targetNamespace == targetNamespace))
10011 return(cur);
10012 }
10013 }
10014 return(NULL);
10015}
10016
10017
10018#define IS_BAD_SCHEMA_DOC(b) \
10019 (((b)->doc == NULL) && ((b)->schemaLocation != NULL))
10020
10021static xmlSchemaBucketPtr
10022xmlSchemaGetSchemaBucketByTNS(xmlSchemaParserCtxtPtr pctxt,
10023 const xmlChar *targetNamespace,
10024 int imported)
10025{
10026 xmlSchemaBucketPtr cur;
10027 xmlSchemaItemListPtr list;
10028
10029 list = pctxt->constructor->buckets;
10030 if (list->nbItems == 0)
10031 return(NULL);
10032 else {
10033 int i;
10034 for (i = 0; i < list->nbItems; i++) {
10035 cur = (xmlSchemaBucketPtr) list->items[i];
10036 if ((! IS_BAD_SCHEMA_DOC(cur)) &&
10037 (cur->origTargetNamespace == targetNamespace) &&
10038 ((imported && cur->imported) ||
10039 ((!imported) && (!cur->imported))))
10040 return(cur);
10041 }
10042 }
10043 return(NULL);
10044}
10045
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010046static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010047xmlSchemaParseNewDocWithContext(xmlSchemaParserCtxtPtr pctxt,
10048 xmlSchemaPtr schema,
10049 xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010050{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010051 int oldFlags;
10052 xmlDocPtr oldDoc;
10053 xmlNodePtr node;
10054 int ret, oldErrs;
10055 xmlSchemaBucketPtr oldbucket = pctxt->constructor->bucket;
10056
10057 /*
10058 * Save old values; reset the *main* schema.
10059 * URGENT TODO: This is not good; move the per-document information
10060 * to the parser.
10061 */
10062 oldFlags = schema->flags;
10063 oldDoc = schema->doc;
10064 if (schema->flags != 0)
10065 xmlSchemaClearSchemaDefaults(schema);
10066 schema->doc = bucket->doc;
10067 /* !! REMOVED: schema->targetNamespace = bucket->targetNamespace; */
10068 pctxt->schema = schema;
10069 /*
10070 * Keep the current target namespace on the parser *not* on the
10071 * main schema.
10072 */
10073 pctxt->targetNamespace = bucket->targetNamespace;
10074 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010075
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010076 if ((bucket->targetNamespace != NULL) &&
10077 xmlStrEqual(bucket->targetNamespace, xmlSchemaNs)) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010078 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010079 * We are parsing the schema for schemas!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010080 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010081 pctxt->isS4S = 1;
10082 }
10083 /* Mark it as parsed, even if parsing fails. */
10084 bucket->parsed++;
10085 /* Compile the schema doc. */
10086 node = xmlDocGetRootElement(bucket->doc);
10087 ret = xmlSchemaParseSchemaElement(pctxt, schema, node);
10088 if (ret != 0)
10089 goto exit;
10090 /* An empty schema; just get out. */
10091 if (node->children == NULL)
10092 goto exit;
10093 oldErrs = pctxt->nberrors;
10094 ret = xmlSchemaParseSchemaTopLevel(pctxt, schema, node->children);
10095 if (ret != 0)
10096 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010097 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010098 * TODO: Not nice, but I'm not 100% sure we will get always an error
10099 * as a result of the obove functions; so better rely on pctxt->err
10100 * as well.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010101 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010102 if ((ret == 0) && (oldErrs != pctxt->nberrors)) {
10103 ret = pctxt->err;
10104 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010105 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010106
10107exit:
10108 WXS_CONSTRUCTOR(pctxt)->bucket = oldbucket;
10109 /* Restore schema values. */
10110 schema->doc = oldDoc;
10111 schema->flags = oldFlags;
10112 return(ret);
10113}
10114
10115static int
10116xmlSchemaParseNewDoc(xmlSchemaParserCtxtPtr pctxt,
10117 xmlSchemaPtr schema,
10118 xmlSchemaBucketPtr bucket)
10119{
10120 xmlSchemaParserCtxtPtr newpctxt;
10121 int res = 0;
10122
10123 if (bucket == NULL)
10124 return(0);
10125 if (bucket->parsed) {
10126 PERROR_INT("xmlSchemaParseNewDoc",
10127 "reparsing a schema doc");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010128 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010129 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010130 if (bucket->doc == NULL) {
10131 PERROR_INT("xmlSchemaParseNewDoc",
10132 "parsing a schema doc, but there's no doc");
10133 return(-1);
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +000010134 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010135 if (pctxt->constructor == NULL) {
10136 PERROR_INT("xmlSchemaParseNewDoc",
10137 "no constructor");
10138 return(-1);
10139 }
10140 /* Create and init the temporary parser context. */
10141 newpctxt = xmlSchemaNewParserCtxtUseDict(
10142 (const char *) bucket->schemaLocation, pctxt->dict);
10143 if (newpctxt == NULL)
10144 return(-1);
10145 newpctxt->constructor = pctxt->constructor;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010146 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010147 * TODO: Can we avoid that the parser knows about the main schema?
10148 * It would be better if he knows about the current schema bucket
10149 * only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010150 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010151 newpctxt->schema = schema;
10152 xmlSchemaSetParserErrors(newpctxt, pctxt->error, pctxt->warning,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000010153 pctxt->errCtxt);
10154 xmlSchemaSetParserStructuredErrors(newpctxt, pctxt->serror,
10155 pctxt->errCtxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010156 newpctxt->counter = pctxt->counter;
10157
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010158
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010159 res = xmlSchemaParseNewDocWithContext(newpctxt, schema, bucket);
10160
10161 /* Channel back errors and cleanup the temporary parser context. */
10162 if (res != 0)
10163 pctxt->err = res;
10164 pctxt->nberrors += newpctxt->nberrors;
10165 pctxt->counter = newpctxt->counter;
10166 newpctxt->constructor = NULL;
10167 /* Free the parser context. */
10168 xmlSchemaFreeParserCtxt(newpctxt);
10169 return(res);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010170}
William M. Brack2f2a6632004-08-20 23:09:47 +000010171
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010172static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010173xmlSchemaSchemaRelationAddChild(xmlSchemaBucketPtr bucket,
10174 xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010175{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010176 xmlSchemaSchemaRelationPtr cur = bucket->relations;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010177
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010178 if (cur == NULL) {
10179 bucket->relations = rel;
10180 return;
10181 }
10182 while (cur->next != NULL)
10183 cur = cur->next;
10184 cur->next = rel;
10185}
10186
10187
10188static const xmlChar *
10189xmlSchemaBuildAbsoluteURI(xmlDictPtr dict, const xmlChar* location,
10190 xmlNodePtr ctxtNode)
10191{
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010192 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010193 * Build an absolue location URI.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010194 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010195 if (location != NULL) {
10196 if (ctxtNode == NULL)
10197 return(location);
10198 else {
10199 xmlChar *base, *URI;
10200 const xmlChar *ret = NULL;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010201
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010202 base = xmlNodeGetBase(ctxtNode->doc, ctxtNode);
10203 if (base == NULL) {
10204 URI = xmlBuildURI(location, ctxtNode->doc->URL);
10205 } else {
10206 URI = xmlBuildURI(location, base);
10207 xmlFree(base);
10208 }
10209 if (URI != NULL) {
10210 ret = xmlDictLookup(dict, URI, -1);
10211 xmlFree(URI);
10212 return(ret);
10213 }
10214 }
10215 }
10216 return(NULL);
10217}
10218
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010219
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010220
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010221/**
10222 * xmlSchemaAddSchemaDoc:
10223 * @pctxt: a schema validation context
10224 * @schema: the schema being built
10225 * @node: a subtree containing XML Schema informations
10226 *
10227 * Parse an included (and to-be-redefined) XML schema document.
10228 *
10229 * Returns 0 on success, a positive error code on errors and
10230 * -1 in case of an internal or API error.
10231 */
10232
10233static int
10234xmlSchemaAddSchemaDoc(xmlSchemaParserCtxtPtr pctxt,
10235 int type, /* import or include or redefine */
10236 const xmlChar *schemaLocation,
10237 xmlDocPtr schemaDoc,
10238 const char *schemaBuffer,
10239 int schemaBufferLen,
10240 xmlNodePtr invokingNode,
10241 const xmlChar *sourceTargetNamespace,
10242 const xmlChar *importNamespace,
10243 xmlSchemaBucketPtr *bucket)
10244{
10245 const xmlChar *targetNamespace = NULL;
10246 xmlSchemaSchemaRelationPtr relation = NULL;
10247 xmlDocPtr doc = NULL;
10248 int res = 0, err = 0, located = 0, preserveDoc = 0;
10249 xmlSchemaBucketPtr bkt = NULL;
10250
10251 if (bucket != NULL)
10252 *bucket = NULL;
10253
10254 switch (type) {
10255 case XML_SCHEMA_SCHEMA_IMPORT:
10256 case XML_SCHEMA_SCHEMA_MAIN:
10257 err = XML_SCHEMAP_SRC_IMPORT;
10258 break;
10259 case XML_SCHEMA_SCHEMA_INCLUDE:
10260 err = XML_SCHEMAP_SRC_INCLUDE;
10261 break;
10262 case XML_SCHEMA_SCHEMA_REDEFINE:
10263 err = XML_SCHEMAP_SRC_REDEFINE;
10264 break;
10265 }
10266
10267
10268 /* Special handling for the main schema:
10269 * skip the location and relation logic and just parse the doc.
10270 * We need just a bucket to be returned in this case.
10271 */
10272 if ((type == XML_SCHEMA_SCHEMA_MAIN) || (! WXS_HAS_BUCKETS(pctxt)))
10273 goto doc_load;
10274
10275 /* Note that we expect the location to be an absulute URI. */
10276 if (schemaLocation != NULL) {
10277 bkt = xmlSchemaGetSchemaBucket(pctxt, schemaLocation);
10278 if ((bkt != NULL) &&
10279 (pctxt->constructor->bucket == bkt)) {
10280 /* Report self-imports/inclusions/redefinitions. */
10281
10282 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10283 invokingNode, NULL,
10284 "The schema must not import/include/redefine itself",
10285 NULL, NULL);
10286 goto exit;
10287 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000010288 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010289 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010290 * Create a relation for the graph of schemas.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010291 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010292 relation = xmlSchemaSchemaRelationCreate();
10293 if (relation == NULL)
10294 return(-1);
10295 xmlSchemaSchemaRelationAddChild(pctxt->constructor->bucket,
10296 relation);
10297 relation->type = type;
10298
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010299 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010300 * Save the namespace import information.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010301 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010302 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010303 relation->importNamespace = importNamespace;
10304 if (schemaLocation == NULL) {
10305 /*
10306 * No location; this is just an import of the namespace.
10307 * Note that we don't assign a bucket to the relation
10308 * in this case.
10309 */
10310 goto exit;
10311 }
10312 targetNamespace = importNamespace;
10313 }
10314
10315 /* Did we already fetch the doc? */
10316 if (bkt != NULL) {
10317 /* TODO: The following nasty cases will produce an error. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010318 if ((WXS_IS_BUCKET_IMPMAIN(type)) && (! bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010319 /* We included/redefined and then try to import a schema. */
10320 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10321 invokingNode, NULL,
10322 "The schema document '%s' cannot be imported, since "
10323 "it was already included or redefined",
10324 schemaLocation, NULL);
10325 goto exit;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010326 } else if ((! WXS_IS_BUCKET_IMPMAIN(type)) && (bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010327 /* We imported and then try to include/redefine a schema. */
10328 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10329 invokingNode, NULL,
10330 "The schema document '%s' cannot be included or "
10331 "redefined, since it was already imported",
10332 schemaLocation, NULL);
10333 goto exit;
10334 }
10335 }
10336
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010337 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010338 /*
10339 * Given that the schemaLocation [attribute] is only a hint, it is open
10340 * to applications to ignore all but the first <import> for a given
10341 * namespace, regardless of the ·actual value· of schemaLocation, but
10342 * such a strategy risks missing useful information when new
10343 * schemaLocations are offered.
10344 *
10345 * We will use the first <import> that comes with a location.
10346 * Further <import>s *with* a location, will result in an error.
10347 * TODO: Better would be to just report a warning here, but
10348 * we'll try it this way until someone complains.
10349 *
10350 * Schema Document Location Strategy:
10351 * 3 Based on the namespace name, identify an existing schema document,
10352 * either as a resource which is an XML document or a <schema> element
10353 * information item, in some local schema repository;
10354 * 5 Attempt to resolve the namespace name to locate such a resource.
10355 *
10356 * NOTE: (3) and (5) are not supported.
10357 */
10358 if (bkt != NULL) {
10359 relation->bucket = bkt;
10360 goto exit;
10361 }
10362 bkt = xmlSchemaGetSchemaBucketByTNS(pctxt,
10363 importNamespace, 1);
10364
10365 if (bkt != NULL) {
10366 relation->bucket = bkt;
10367 if (bkt->schemaLocation == NULL) {
10368 /* First given location of the schema; load the doc. */
10369 bkt->schemaLocation = schemaLocation;
10370 } else {
10371 if (!xmlStrEqual(schemaLocation,
10372 bkt->schemaLocation)) {
10373 /*
10374 * Additional location given; just skip it.
10375 * URGENT TODO: We should report a warning here.
10376 * res = XML_SCHEMAP_SRC_IMPORT;
10377 */
10378 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10379 XML_SCHEMAP_WARN_SKIP_SCHEMA,
10380 invokingNode, NULL,
10381 "Skipping import of schema located at '%s' for the "
10382 "namespace '%s', since this namespace was already "
10383 "imported with the schema located at '%s'",
10384 schemaLocation, importNamespace, bkt->schemaLocation);
10385 }
10386 goto exit;
10387 }
10388 }
10389 /*
10390 * No bucket + first location: load the doc and create a
10391 * bucket.
10392 */
10393 } else {
10394 /* <include> and <redefine> */
10395 if (bkt != NULL) {
10396
10397 if ((bkt->origTargetNamespace == NULL) &&
10398 (bkt->targetNamespace != sourceTargetNamespace)) {
10399 xmlSchemaBucketPtr chamel;
10400
10401 /*
10402 * Chameleon include/redefine: skip loading only if it was
10403 * aleady build for the targetNamespace of the including
10404 * schema.
10405 */
10406 /*
10407 * URGENT TODO: If the schema is a chameleon-include then copy
10408 * the components into the including schema and modify the
10409 * targetNamespace of those components, do nothing otherwise.
10410 * NOTE: This is currently worked-around by compiling the
10411 * chameleon for every destinct including targetNamespace; thus
10412 * not performant at the moment.
10413 * TODO: Check when the namespace in wildcards for chameleons
10414 * needs to be converted: before we built wildcard intersections
10415 * or after.
10416 * Answer: after!
10417 */
10418 chamel = xmlSchemaGetChameleonSchemaBucket(pctxt,
10419 schemaLocation, sourceTargetNamespace);
10420 if (chamel != NULL) {
10421 /* A fitting chameleon was already parsed; NOP. */
10422 relation->bucket = chamel;
10423 goto exit;
10424 }
10425 /*
10426 * We need to parse the chameleon again for a different
10427 * targetNamespace.
10428 * CHAMELEON TODO: Optimize this by only parsing the
10429 * chameleon once, and then copying the components to
10430 * the new targetNamespace.
10431 */
10432 bkt = NULL;
10433 } else {
10434 relation->bucket = bkt;
10435 goto exit;
10436 }
10437 }
10438 }
10439 if ((bkt != NULL) && (bkt->doc != NULL)) {
10440 PERROR_INT("xmlSchemaAddSchemaDoc",
10441 "trying to load a schema doc, but a doc is already "
10442 "assigned to the schema bucket");
10443 goto exit_failure;
10444 }
10445
10446doc_load:
10447 /*
10448 * Load the document.
10449 */
10450 if (schemaDoc != NULL) {
10451 doc = schemaDoc;
10452 /* Don' free this one, since it was provided by the caller. */
10453 preserveDoc = 1;
10454 /* TODO: Does the context or the doc hold the location? */
10455 if (schemaDoc->URL != NULL)
10456 schemaLocation = xmlDictLookup(pctxt->dict,
10457 schemaDoc->URL, -1);
10458
10459 } else if ((schemaLocation != NULL) || (schemaBuffer != NULL)) {
10460 xmlParserCtxtPtr parserCtxt;
10461
10462 parserCtxt = xmlNewParserCtxt();
10463 if (parserCtxt == NULL) {
10464 xmlSchemaPErrMemory(NULL, "xmlSchemaGetDoc, "
10465 "allocating a parser context", NULL);
10466 goto exit_failure;
10467 }
10468 if ((pctxt->dict != NULL) && (parserCtxt->dict != NULL)) {
10469 /*
10470 * TODO: Do we have to burden the schema parser dict with all
10471 * the content of the schema doc?
10472 */
10473 xmlDictFree(parserCtxt->dict);
10474 parserCtxt->dict = pctxt->dict;
10475 xmlDictReference(parserCtxt->dict);
10476 }
10477 if (schemaLocation != NULL) {
10478 /* Parse from file. */
10479 doc = xmlCtxtReadFile(parserCtxt, (const char *) schemaLocation,
10480 NULL, SCHEMAS_PARSE_OPTIONS);
10481 } else if (schemaBuffer != NULL) {
10482 /* Parse from memory buffer. */
10483 doc = xmlCtxtReadMemory(parserCtxt, schemaBuffer, schemaBufferLen,
10484 NULL, NULL, SCHEMAS_PARSE_OPTIONS);
10485 schemaLocation = xmlStrdup(BAD_CAST "in_memory_buffer");
10486 if (doc != NULL)
10487 doc->URL = schemaLocation;
10488 }
10489 /*
10490 * For <import>:
10491 * 2.1 The referent is (a fragment of) a resource which is an
10492 * XML document (see clause 1.1), which in turn corresponds to
10493 * a <schema> element information item in a well-formed information
10494 * set, which in turn corresponds to a valid schema.
10495 * TODO: (2.1) fragments of XML documents are not supported.
10496 *
10497 * 2.2 The referent is a <schema> element information item in
10498 * a well-formed information set, which in turn corresponds
10499 * to a valid schema.
10500 * TODO: (2.2) is not supported.
10501 */
10502 if (doc == NULL) {
10503 xmlErrorPtr lerr;
10504 lerr = xmlGetLastError();
10505 /*
10506 * Check if this a parser error, or if the document could
10507 * just not be located.
10508 * TODO: Try to find specific error codes to react only on
10509 * localisation failures.
10510 */
10511 if ((lerr == NULL) || (lerr->domain != XML_FROM_IO)) {
10512 /*
10513 * We assume a parser error here.
10514 */
10515 located = 1;
10516 /* TODO: Error code ?? */
10517 res = XML_SCHEMAP_SRC_IMPORT_2_1;
10518 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10519 invokingNode, NULL,
10520 "Failed to parse the XML resource '%s'",
10521 schemaLocation, NULL);
10522 }
10523 }
10524 xmlFreeParserCtxt(parserCtxt);
10525 if ((doc == NULL) && located)
10526 goto exit_error;
10527 } else {
10528 xmlSchemaPErr(pctxt, NULL,
10529 XML_SCHEMAP_NOTHING_TO_PARSE,
10530 "No information for parsing was provided with the "
10531 "given schema parser context.\n",
10532 NULL, NULL);
10533 goto exit_failure;
10534 }
10535 /*
10536 * Preprocess the document.
10537 */
10538 if (doc != NULL) {
10539 xmlNodePtr docElem = NULL;
10540
10541 located = 1;
10542 docElem = xmlDocGetRootElement(doc);
10543 if (docElem == NULL) {
10544 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOROOT,
10545 invokingNode, NULL,
10546 "The document '%s' has no document element",
10547 schemaLocation, NULL);
10548 xmlFreeDoc(doc);
10549 doc = NULL;
10550 goto exit_error;
10551 }
10552 /*
10553 * Remove all the blank text nodes.
10554 */
10555 xmlSchemaCleanupDoc(pctxt, docElem);
10556 /*
10557 * Check the schema's top level element.
10558 */
10559 if (!IS_SCHEMA(docElem, "schema")) {
10560 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOT_SCHEMA,
10561 invokingNode, NULL,
10562 "The XML document '%s' is not a schema document",
10563 schemaLocation, NULL);
10564 xmlFreeDoc(doc);
10565 doc = NULL;
10566 goto exit_error;
10567 }
10568 /*
10569 * Note that we don't apply a type check for the
10570 * targetNamespace value here.
10571 */
10572 targetNamespace = xmlSchemaGetProp(pctxt, docElem,
10573 "targetNamespace");
10574 }
10575
10576/* after_doc_loading: */
10577 if ((bkt == NULL) && located) {
10578 /* Only create a bucket if the schema was located. */
10579 bkt = xmlSchemaBucketCreate(pctxt, type,
10580 targetNamespace);
10581 if (bkt == NULL)
10582 goto exit_failure;
10583 }
10584 if (bkt != NULL) {
10585 bkt->schemaLocation = schemaLocation;
10586 bkt->located = located;
10587 if (doc != NULL) {
10588 bkt->doc = doc;
10589 bkt->targetNamespace = targetNamespace;
10590 bkt->origTargetNamespace = targetNamespace;
10591 if (preserveDoc)
10592 bkt->preserveDoc = 1;
10593 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010594 if (WXS_IS_BUCKET_IMPMAIN(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010595 bkt->imported++;
10596 /*
10597 * Add it to the graph of schemas.
10598 */
10599 if (relation != NULL)
10600 relation->bucket = bkt;
10601 }
10602
10603exit:
10604 /*
10605 * Return the bucket explicitely; this is needed for the
10606 * main schema.
10607 */
10608 if (bucket != NULL)
10609 *bucket = bkt;
10610 return (0);
10611
10612exit_error:
10613 if ((doc != NULL) && (! preserveDoc)) {
10614 xmlFreeDoc(doc);
10615 if (bkt != NULL)
10616 bkt->doc = NULL;
10617 }
10618 return(pctxt->err);
10619
10620exit_failure:
10621 if ((doc != NULL) && (! preserveDoc)) {
10622 xmlFreeDoc(doc);
10623 if (bkt != NULL)
10624 bkt->doc = NULL;
10625 }
10626 return (-1);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010627}
10628
William M. Brack2f2a6632004-08-20 23:09:47 +000010629/**
10630 * xmlSchemaParseImport:
10631 * @ctxt: a schema validation context
10632 * @schema: the schema being built
10633 * @node: a subtree containing XML Schema informations
10634 *
10635 * parse a XML schema Import definition
10636 * *WARNING* this interface is highly subject to change
10637 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010638 * Returns 0 in case of success, a positive error code if
10639 * not valid and -1 in case of an internal error.
William M. Brack2f2a6632004-08-20 23:09:47 +000010640 */
10641static int
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010642xmlSchemaParseImport(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
William M. Brack2f2a6632004-08-20 23:09:47 +000010643 xmlNodePtr node)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010644{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010645 xmlNodePtr child;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010646 const xmlChar *namespaceName = NULL, *schemaLocation = NULL;
10647 const xmlChar *thisTargetNamespace;
William M. Brack2f2a6632004-08-20 23:09:47 +000010648 xmlAttrPtr attr;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010649 int ret = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010650 xmlSchemaBucketPtr bucket = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000010651
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010652 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
William M. Brack2f2a6632004-08-20 23:09:47 +000010653 return (-1);
10654
10655 /*
10656 * Check for illegal attributes.
10657 */
10658 attr = node->properties;
10659 while (attr != NULL) {
10660 if (attr->ns == NULL) {
10661 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10662 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
10663 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010664 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010665 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010666 }
10667 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010668 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010669 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010670 }
10671 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010672 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010673 /*
10674 * Extract and validate attributes.
10675 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010676 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010677 "namespace", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010678 &namespaceName) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010679 xmlSchemaPSimpleTypeErr(pctxt,
10680 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010681 NULL, node,
10682 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010683 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010684 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010685 }
10686
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010687 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010688 "schemaLocation", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
William M. Brack2f2a6632004-08-20 23:09:47 +000010689 &schemaLocation) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010690 xmlSchemaPSimpleTypeErr(pctxt,
10691 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010692 NULL, node,
10693 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010694 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010695 return (pctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010696 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010697 /*
10698 * And now for the children...
10699 */
10700 child = node->children;
10701 if (IS_SCHEMA(child, "annotation")) {
10702 /*
10703 * the annotation here is simply discarded ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010704 * TODO: really?
William M. Brack2f2a6632004-08-20 23:09:47 +000010705 */
10706 child = child->next;
10707 }
10708 if (child != NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010709 xmlSchemaPContentErr(pctxt,
10710 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010711 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000010712 "(annotation?)");
10713 }
10714 /*
10715 * Apply additional constraints.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010716 *
10717 * Note that it is important to use the original @targetNamespace
10718 * (or none at all), to rule out imports of schemas _with_ a
10719 * @targetNamespace if the importing schema is a chameleon schema
10720 * (with no @targetNamespace).
William M. Brack2f2a6632004-08-20 23:09:47 +000010721 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010722 thisTargetNamespace = WXS_BUCKET(pctxt)->origTargetNamespace;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010723 if (namespaceName != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000010724 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010725 * 1.1 If the namespace [attribute] is present, then its ·actual value·
10726 * must not match the ·actual value· of the enclosing <schema>'s
William M. Brack2f2a6632004-08-20 23:09:47 +000010727 * targetNamespace [attribute].
10728 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010729 if (xmlStrEqual(thisTargetNamespace, namespaceName)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010730 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010731 XML_SCHEMAP_SRC_IMPORT_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010732 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010733 "The value of the attribute 'namespace' must not match "
10734 "the target namespace '%s' of the importing schema",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010735 thisTargetNamespace);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010736 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010737 }
10738 } else {
10739 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010740 * 1.2 If the namespace [attribute] is not present, then the enclosing
William M. Brack2f2a6632004-08-20 23:09:47 +000010741 * <schema> must have a targetNamespace [attribute].
10742 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010743 if (thisTargetNamespace == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010744 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010745 XML_SCHEMAP_SRC_IMPORT_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010746 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010747 "The attribute 'namespace' must be existent if "
10748 "the importing schema has no target namespace",
10749 NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010750 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010751 }
10752 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010753 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010754 * Locate and acquire the schema document.
William M. Brack2f2a6632004-08-20 23:09:47 +000010755 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010756 if (schemaLocation != NULL)
10757 schemaLocation = xmlSchemaBuildAbsoluteURI(pctxt->dict,
10758 schemaLocation, node);
10759 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010760 schemaLocation, NULL, NULL, 0, node, thisTargetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010761 namespaceName, &bucket);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010762
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010763 if (ret != 0)
10764 return(ret);
10765
10766 /*
10767 * For <import>: "It is *not* an error for the application
10768 * schema reference strategy to fail."
10769 * So just don't parse if no schema document was found.
10770 * Note that we will get no bucket if the schema could not be
10771 * located or if there was no schemaLocation.
10772 */
10773 if ((bucket == NULL) && (schemaLocation != NULL)) {
10774 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10775 XML_SCHEMAP_WARN_UNLOCATED_SCHEMA,
10776 node, NULL,
10777 "Failed to locate a schema at location '%s'. "
10778 "Skipping the import", schemaLocation, NULL, NULL);
10779 }
10780
10781 if ((bucket != NULL) && CAN_PARSE_SCHEMA(bucket)) {
10782 ret = xmlSchemaParseNewDoc(pctxt, schema, bucket);
10783 }
10784
10785 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000010786}
10787
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010788static int
10789xmlSchemaParseIncludeOrRedefineAttrs(xmlSchemaParserCtxtPtr pctxt,
10790 xmlSchemaPtr schema,
10791 xmlNodePtr node,
10792 xmlChar **schemaLocation,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010793 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010794{
10795 xmlAttrPtr attr;
10796
10797 if ((pctxt == NULL) || (schema == NULL) || (node == NULL) ||
10798 (schemaLocation == NULL))
10799 return (-1);
10800
10801 *schemaLocation = NULL;
10802 /*
10803 * Check for illegal attributes.
10804 * Applies for both <include> and <redefine>.
10805 */
10806 attr = node->properties;
10807 while (attr != NULL) {
10808 if (attr->ns == NULL) {
10809 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10810 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
10811 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010812 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010813 }
10814 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
10815 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010816 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010817 }
10818 attr = attr->next;
10819 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010820 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010821 /*
10822 * Preliminary step, extract the URI-Reference and make an URI
10823 * from the base.
10824 */
10825 /*
10826 * Attribute "schemaLocation" is mandatory.
10827 */
10828 attr = xmlSchemaGetPropNode(node, "schemaLocation");
10829 if (attr != NULL) {
10830 xmlChar *base = NULL;
10831 xmlChar *uri = NULL;
10832
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010833 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010834 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
10835 (const xmlChar **) schemaLocation) != 0)
10836 goto exit_error;
10837 base = xmlNodeGetBase(node->doc, node);
10838 if (base == NULL) {
10839 uri = xmlBuildURI(*schemaLocation, node->doc->URL);
10840 } else {
10841 uri = xmlBuildURI(*schemaLocation, base);
10842 xmlFree(base);
10843 }
10844 if (uri == NULL) {
10845 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
10846 "could not build an URI from the schemaLocation")
10847 goto exit_failure;
10848 }
10849 (*schemaLocation) = (xmlChar *) xmlDictLookup(pctxt->dict, uri, -1);
10850 xmlFree(uri);
10851 } else {
10852 xmlSchemaPMissingAttrErr(pctxt,
10853 XML_SCHEMAP_S4S_ATTR_MISSING,
10854 NULL, node, "schemaLocation", NULL);
10855 goto exit_error;
10856 }
10857 /*
10858 * Report self-inclusion and self-redefinition.
10859 */
10860 if (xmlStrEqual(*schemaLocation, pctxt->URL)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010861 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010862 xmlSchemaPCustomErr(pctxt,
10863 XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010864 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010865 "The schema document '%s' cannot redefine itself.",
10866 *schemaLocation);
10867 } else {
10868 xmlSchemaPCustomErr(pctxt,
10869 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010870 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010871 "The schema document '%s' cannot include itself.",
10872 *schemaLocation);
10873 }
10874 goto exit_error;
10875 }
10876
10877 return(0);
10878exit_error:
10879 return(pctxt->err);
10880exit_failure:
10881 return(-1);
10882}
10883
10884static int
10885xmlSchemaParseIncludeOrRedefine(xmlSchemaParserCtxtPtr pctxt,
10886 xmlSchemaPtr schema,
10887 xmlNodePtr node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010888 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010889{
10890 xmlNodePtr child = NULL;
10891 const xmlChar *schemaLocation = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010892 int res = 0; /* hasRedefinitions = 0 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010893 int isChameleon = 0, wasChameleon = 0;
10894 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010895
10896 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
10897 return (-1);
10898
10899 /*
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010900 * Parse attributes. Note that the returned schemaLocation will
10901 * be already converted to an absolute URI.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010902 */
10903 res = xmlSchemaParseIncludeOrRedefineAttrs(pctxt, schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010904 node, (xmlChar **) (&schemaLocation), type);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010905 if (res != 0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010906 return(res);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010907 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010908 * Load and add the schema document.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010909 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010910 res = xmlSchemaAddSchemaDoc(pctxt, type, schemaLocation, NULL,
10911 NULL, 0, node, pctxt->targetNamespace, NULL, &bucket);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010912 if (res != 0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010913 return(res);
10914 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010915 * If we get no schema bucket back, then this means that the schema
10916 * document could not be located or was broken XML or was not
10917 * a schema document.
10918 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010919 if ((bucket == NULL) || (bucket->doc == NULL)) {
10920 if (type == XML_SCHEMA_SCHEMA_INCLUDE) {
10921 /*
10922 * WARNING for <include>:
10923 * We will raise an error if the schema cannot be located
10924 * for inclusions, since the that was the feedback from the
10925 * schema people. I.e. the following spec piece will *not* be
10926 * satisfied:
10927 * SPEC src-include: "It is not an error for the ·actual value· of the
10928 * schemaLocation [attribute] to fail to resolve it all, in which
10929 * case no corresponding inclusion is performed.
10930 * So do we need a warning report here?"
10931 */
10932 res = XML_SCHEMAP_SRC_INCLUDE;
10933 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10934 node, NULL,
10935 "Failed to load the document '%s' for inclusion",
10936 schemaLocation, NULL);
10937 } else {
10938 /*
10939 * NOTE: This was changed to raise an error even if no redefinitions
10940 * are specified.
10941 *
10942 * SPEC src-redefine (1)
10943 * "If there are any element information items among the [children]
10944 * other than <annotation> then the ·actual value· of the
10945 * schemaLocation [attribute] must successfully resolve."
10946 * TODO: Ask the WG if a the location has always to resolve
10947 * here as well!
10948 */
10949 res = XML_SCHEMAP_SRC_REDEFINE;
10950 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10951 node, NULL,
10952 "Failed to load the document '%s' for redefinition",
10953 schemaLocation, NULL);
10954 }
10955 } else {
10956 /*
10957 * Check targetNamespace sanity before parsing the new schema.
10958 * TODO: Note that we won't check further content if the
10959 * targetNamespace was bad.
10960 */
10961 if (bucket->origTargetNamespace != NULL) {
10962 /*
10963 * SPEC src-include (2.1)
10964 * "SII has a targetNamespace [attribute], and its ·actual
10965 * value· is identical to the ·actual value· of the targetNamespace
10966 * [attribute] of SIIÂ’ (which must have such an [attribute])."
10967 */
10968 if (pctxt->targetNamespace == NULL) {
10969 xmlSchemaCustomErr(ACTXT_CAST pctxt,
10970 XML_SCHEMAP_SRC_INCLUDE,
10971 node, NULL,
10972 "The target namespace of the included/redefined schema "
10973 "'%s' has to be absent, since the including/redefining "
10974 "schema has no target namespace",
10975 schemaLocation, NULL);
10976 goto exit_error;
10977 } else if (!xmlStrEqual(bucket->origTargetNamespace,
10978 pctxt->targetNamespace)) {
10979 /* TODO: Change error function. */
10980 xmlSchemaPCustomErrExt(pctxt,
10981 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010982 NULL, node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010983 "The target namespace '%s' of the included/redefined "
10984 "schema '%s' differs from '%s' of the "
10985 "including/redefining schema",
10986 bucket->origTargetNamespace, schemaLocation,
10987 pctxt->targetNamespace);
10988 goto exit_error;
10989 }
10990 } else if (pctxt->targetNamespace != NULL) {
10991 /*
10992 * Chameleons: the original target namespace will
10993 * differ from the resulting namespace.
10994 */
10995 isChameleon = 1;
10996 if (bucket->parsed &&
10997 (bucket->targetNamespace != pctxt->targetNamespace)) {
10998 /*
10999 * This is a sanity check, I dunno yet if this can happen.
11000 */
11001 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
11002 "trying to use an already parsed schema for a "
11003 "different targetNamespace");
11004 return(-1);
11005 }
11006 bucket->targetNamespace = pctxt->targetNamespace;
11007 }
11008 }
11009 /*
11010 * Parse the schema.
11011 */
11012 if (bucket && (!bucket->parsed) && (bucket->doc != NULL)) {
11013 if (isChameleon) {
11014 /* TODO: Get rid of this flag on the schema itself. */
11015 if ((schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) == 0) {
11016 schema->flags |= XML_SCHEMAS_INCLUDING_CONVERT_NS;
11017 } else
11018 wasChameleon = 1;
11019 }
11020 xmlSchemaParseNewDoc(pctxt, schema, bucket);
11021 /* Restore chameleon flag. */
11022 if (isChameleon && (!wasChameleon))
11023 schema->flags ^= XML_SCHEMAS_INCLUDING_CONVERT_NS;
11024 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011025 /*
11026 * And now for the children...
11027 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011028 child = node->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011029 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011030 /*
11031 * Parse (simpleType | complexType | group | attributeGroup))*
11032 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011033 pctxt->redefined = bucket;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011034 /*
11035 * How to proceed if the redefined schema was not located?
11036 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011037 pctxt->isRedefine = 1;
11038 while (IS_SCHEMA(child, "annotation") ||
11039 IS_SCHEMA(child, "simpleType") ||
11040 IS_SCHEMA(child, "complexType") ||
11041 IS_SCHEMA(child, "group") ||
11042 IS_SCHEMA(child, "attributeGroup")) {
11043 if (IS_SCHEMA(child, "annotation")) {
11044 /*
11045 * TODO: discard or not?
11046 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011047 } else if (IS_SCHEMA(child, "simpleType")) {
11048 xmlSchemaParseSimpleType(pctxt, schema, child, 1);
11049 } else if (IS_SCHEMA(child, "complexType")) {
11050 xmlSchemaParseComplexType(pctxt, schema, child, 1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011051 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011052 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011053 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011054 xmlSchemaParseModelGroupDefinition(pctxt,
11055 schema, child);
11056 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011057 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011058 xmlSchemaParseAttributeGroupDefinition(pctxt, schema,
11059 child);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011060 }
11061 child = child->next;
11062 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011063 pctxt->redefined = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011064 pctxt->isRedefine = 0;
11065 } else {
11066 if (IS_SCHEMA(child, "annotation")) {
11067 /*
11068 * TODO: discard or not?
11069 */
11070 child = child->next;
11071 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011072 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011073 if (child != NULL) {
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011074 res = XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011075 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
11076 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011077 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011078 "(annotation | (simpleType | complexType | group | attributeGroup))*");
11079 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011080 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011081 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011082 "(annotation?)");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011083 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011084 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011085 return(res);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011086
11087exit_error:
11088 return(pctxt->err);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011089}
11090
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011091static int
11092xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11093 xmlNodePtr node)
11094{
11095 int res;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011096#ifndef ENABLE_REDEFINE
11097 TODO
11098 return(0);
11099#endif
11100 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11101 XML_SCHEMA_SCHEMA_REDEFINE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011102 if (res != 0)
11103 return(res);
11104 return(0);
11105}
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011106
11107static int
11108xmlSchemaParseInclude(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11109 xmlNodePtr node)
11110{
11111 int res;
11112
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011113 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11114 XML_SCHEMA_SCHEMA_INCLUDE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011115 if (res != 0)
11116 return(res);
11117 return(0);
11118}
11119
Daniel Veillardbd2904b2003-11-25 15:38:59 +000011120/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011121 * xmlSchemaParseModelGroup:
Daniel Veillard4255d502002-04-16 15:50:10 +000011122 * @ctxt: a schema validation context
11123 * @schema: the schema being built
11124 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011125 * @type: the "compositor" type
11126 * @particleNeeded: if a a model group with a particle
Daniel Veillard4255d502002-04-16 15:50:10 +000011127 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011128 * parse a XML schema Sequence definition.
11129 * Applies parts of:
11130 * Schema Representation Constraint:
11131 * Redefinition Constraints and Semantics (src-redefine)
11132 * (6.1), (6.1.1), (6.1.2)
11133 *
11134 * Schema Component Constraint:
11135 * All Group Limited (cos-all-limited) (2)
11136 * TODO: Actually this should go to component-level checks,
11137 * but is done here due to performance. Move it to an other layer
11138 * is schema construction via an API is implemented.
11139 *
Daniel Veillard4255d502002-04-16 15:50:10 +000011140 * *WARNING* this interface is highly subject to change
11141 *
William M. Bracke7091952004-05-11 15:09:58 +000011142 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +000011143 * 1 in case of success.
11144 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011145static xmlSchemaTreeItemPtr
11146xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
11147 xmlNodePtr node, xmlSchemaTypeType type,
11148 int withParticle)
Daniel Veillard4255d502002-04-16 15:50:10 +000011149{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011150 xmlSchemaModelGroupPtr item;
11151 xmlSchemaParticlePtr particle = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000011152 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011153 xmlAttrPtr attr;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011154 int min = 1, max = 1, isElemRef, hasRefs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000011155
11156 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011157 return (NULL);
11158 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011159 * Create a model group with the given compositor.
William M. Brack2f2a6632004-08-20 23:09:47 +000011160 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011161 item = xmlSchemaAddModelGroup(ctxt, schema, type, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011162 if (item == NULL)
11163 return (NULL);
11164
11165 if (withParticle) {
11166 if (type == XML_SCHEMA_TYPE_ALL) {
11167 min = xmlGetMinOccurs(ctxt, node, 0, 1, 1, "(0 | 1)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011168 max = xmlGetMaxOccurs(ctxt, node, 1, 1, 1, "1");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011169 } else {
11170 /* choice + sequence */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011171 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
11172 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
11173 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011174 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011175 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
11176 /*
11177 * Create a particle
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011178 */
11179 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
11180 if (particle == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011181 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011182 particle->children = (xmlSchemaTreeItemPtr) item;
11183 /*
11184 * Check for illegal attributes.
11185 */
11186 attr = node->properties;
11187 while (attr != NULL) {
11188 if (attr->ns == NULL) {
11189 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11190 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
11191 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011192 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011193 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011194 }
11195 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011196 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011197 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011198 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011199 attr = attr->next;
William M. Brack2f2a6632004-08-20 23:09:47 +000011200 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011201 } else {
11202 /*
11203 * Check for illegal attributes.
11204 */
11205 attr = node->properties;
11206 while (attr != NULL) {
11207 if (attr->ns == NULL) {
11208 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011209 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011210 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011211 }
11212 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011213 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011214 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011215 }
11216 attr = attr->next;
11217 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011218 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011219
William M. Brack2f2a6632004-08-20 23:09:47 +000011220 /*
11221 * Extract and validate attributes.
11222 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011223 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011224 /*
11225 * And now for the children...
11226 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011227 child = node->children;
11228 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011229 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011230 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011231 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011232 if (type == XML_SCHEMA_TYPE_ALL) {
11233 xmlSchemaParticlePtr part, last = NULL;
11234
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011235 while (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011236 part = (xmlSchemaParticlePtr) xmlSchemaParseElement(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011237 schema, child, &isElemRef, 0);
11238 /*
11239 * SPEC cos-all-limited (2)
11240 * "The {max occurs} of all the particles in the {particles}
11241 * of the ('all') group must be 0 or 1.
11242 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011243 if (part != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011244 if (isElemRef)
11245 hasRefs++;
11246 if (part->minOccurs > 1) {
11247 xmlSchemaPCustomErr(ctxt,
11248 XML_SCHEMAP_COS_ALL_LIMITED,
11249 NULL, child,
11250 "Invalid value for minOccurs (must be 0 or 1)",
11251 NULL);
11252 /* Reset to 1. */
11253 part->minOccurs = 1;
11254 }
11255 if (part->maxOccurs > 1) {
11256 xmlSchemaPCustomErr(ctxt,
11257 XML_SCHEMAP_COS_ALL_LIMITED,
11258 NULL, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011259 "Invalid value for maxOccurs (must be 0 or 1)",
11260 NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011261 /* Reset to 1. */
11262 part->maxOccurs = 1;
11263 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011264 if (last == NULL)
11265 item->children = (xmlSchemaTreeItemPtr) part;
11266 else
11267 last->next = (xmlSchemaTreeItemPtr) part;
11268 last = part;
11269 }
11270 child = child->next;
11271 }
11272 if (child != NULL) {
11273 xmlSchemaPContentErr(ctxt,
11274 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011275 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011276 "(annotation?, (annotation?, element*)");
11277 }
11278 } else {
11279 /* choice + sequence */
11280 xmlSchemaTreeItemPtr part = NULL, last = NULL;
11281
11282 while ((IS_SCHEMA(child, "element")) ||
11283 (IS_SCHEMA(child, "group")) ||
11284 (IS_SCHEMA(child, "any")) ||
11285 (IS_SCHEMA(child, "choice")) ||
11286 (IS_SCHEMA(child, "sequence"))) {
11287
11288 if (IS_SCHEMA(child, "element")) {
11289 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011290 xmlSchemaParseElement(ctxt, schema, child, &isElemRef, 0);
11291 if (part && isElemRef)
11292 hasRefs++;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011293 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011294 part =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011295 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011296 if (part != NULL)
11297 hasRefs++;
11298 /*
11299 * Handle redefinitions.
11300 */
11301 if (ctxt->isRedefine && ctxt->redef &&
11302 (ctxt->redef->item->type == XML_SCHEMA_TYPE_GROUP) &&
11303 part && part->children)
11304 {
11305 if ((xmlSchemaGetQNameRefName(part->children) ==
11306 ctxt->redef->refName) &&
11307 (xmlSchemaGetQNameRefTargetNs(part->children) ==
11308 ctxt->redef->refTargetNs))
11309 {
11310 /*
11311 * SPEC src-redefine:
11312 * (6.1) "If it has a <group> among its contents at
11313 * some level the ·actual value· of whose ref
11314 * [attribute] is the same as the ·actual value· of
11315 * its own name attribute plus target namespace, then
11316 * all of the following must be true:"
11317 * (6.1.1) "It must have exactly one such group."
11318 */
11319 if (ctxt->redefCounter != 0) {
11320 xmlChar *str = NULL;
11321
11322 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11323 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11324 "The redefining model group definition "
11325 "'%s' must not contain more than one "
11326 "reference to the redefined definition",
11327 xmlSchemaFormatQName(&str,
11328 ctxt->redef->refTargetNs,
11329 ctxt->redef->refName),
11330 NULL);
11331 FREE_AND_NULL(str)
11332 part = NULL;
11333 } else if (((WXS_PARTICLE(part))->minOccurs != 1) ||
11334 ((WXS_PARTICLE(part))->maxOccurs != 1))
11335 {
11336 xmlChar *str = NULL;
11337 /*
11338 * SPEC src-redefine:
11339 * (6.1.2) "The ·actual value· of both that
11340 * group's minOccurs and maxOccurs [attribute]
11341 * must be 1 (or ·absent·).
11342 */
11343 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11344 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11345 "The redefining model group definition "
11346 "'%s' must not contain a reference to the "
11347 "redefined definition with a "
11348 "maxOccurs/minOccurs other than 1",
11349 xmlSchemaFormatQName(&str,
11350 ctxt->redef->refTargetNs,
11351 ctxt->redef->refName),
11352 NULL);
11353 FREE_AND_NULL(str)
11354 part = NULL;
11355 }
11356 ctxt->redef->reference = WXS_BASIC_CAST part;
11357 ctxt->redefCounter++;
11358 }
11359 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011360 } else if (IS_SCHEMA(child, "any")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011361 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011362 xmlSchemaParseAny(ctxt, schema, child);
11363 } else if (IS_SCHEMA(child, "choice")) {
11364 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11365 XML_SCHEMA_TYPE_CHOICE, 1);
11366 } else if (IS_SCHEMA(child, "sequence")) {
11367 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11368 XML_SCHEMA_TYPE_SEQUENCE, 1);
11369 }
11370 if (part != NULL) {
11371 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011372 item->children = part;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011373 else
11374 last->next = part;
11375 last = part;
11376 }
11377 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011378 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011379 if (child != NULL) {
11380 xmlSchemaPContentErr(ctxt,
11381 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011382 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011383 "(annotation?, (element | group | choice | sequence | any)*)");
11384 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011385 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011386 if ((max == 0) && (min == 0))
11387 return (NULL);
11388 if (hasRefs) {
11389 /*
11390 * We need to resolve references.
11391 */
11392 WXS_ADD_PENDING(ctxt, item);
11393 }
11394 if (withParticle)
11395 return ((xmlSchemaTreeItemPtr) particle);
11396 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011397 return ((xmlSchemaTreeItemPtr) item);
Daniel Veillard4255d502002-04-16 15:50:10 +000011398}
11399
11400/**
11401 * xmlSchemaParseRestriction:
11402 * @ctxt: a schema validation context
11403 * @schema: the schema being built
11404 * @node: a subtree containing XML Schema informations
Daniel Veillard4255d502002-04-16 15:50:10 +000011405 *
11406 * parse a XML schema Restriction definition
11407 * *WARNING* this interface is highly subject to change
11408 *
11409 * Returns the type definition or NULL in case of error
11410 */
11411static xmlSchemaTypePtr
11412xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011413 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011414{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011415 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011416 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011417 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011418
11419 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11420 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011421 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011422 type = ctxt->ctxtType;
11423 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011424
11425 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011426 * Check for illegal attributes.
11427 */
11428 attr = node->properties;
11429 while (attr != NULL) {
11430 if (attr->ns == NULL) {
11431 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11432 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011433 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011434 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011435 }
11436 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011437 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011438 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011439 }
11440 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011441 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011442 /*
11443 * Extract and validate attributes.
11444 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011445 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011446 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011447 * Attribute
William M. Brack2f2a6632004-08-20 23:09:47 +000011448 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011449 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011450 * Extract the base type. The "base" attribute is mandatory if inside
11451 * a complex type or if redefining.
11452 *
11453 * SPEC (1.2) "...otherwise (<restriction> has no <simpleType> "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011454 * among its [children]), the simple type definition which is
11455 * the {content type} of the type definition ·resolved· to by
11456 * the ·actual value· of the base [attribute]"
11457 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011458 if (xmlSchemaPValAttrQName(ctxt, schema, NULL, node, "base",
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011459 &(type->baseNs), &(type->base)) == 0)
11460 {
11461 if ((type->base == NULL) && (type->type == XML_SCHEMA_TYPE_COMPLEX)) {
11462 xmlSchemaPMissingAttrErr(ctxt,
11463 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011464 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011465 } else if ((ctxt->isRedefine) &&
11466 (type->flags & XML_SCHEMAS_TYPE_GLOBAL))
11467 {
11468 if (type->base == NULL) {
11469 xmlSchemaPMissingAttrErr(ctxt,
11470 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011471 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011472 } else if ((! xmlStrEqual(type->base, type->name)) ||
11473 (! xmlStrEqual(type->baseNs, type->targetNamespace)))
11474 {
11475 xmlChar *str1 = NULL, *str2 = NULL;
11476 /*
11477 * REDEFINE: SPEC src-redefine (5)
11478 * "Within the [children], each <simpleType> must have a
11479 * <restriction> among its [children] ... the ·actual value· of
11480 * whose base [attribute] must be the same as the ·actual value·
11481 * of its own name attribute plus target namespace;"
11482 */
11483 xmlSchemaPCustomErrExt(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011484 NULL, node, "This is a redefinition, but the QName "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011485 "value '%s' of the 'base' attribute does not match the "
11486 "type's designation '%s'",
11487 xmlSchemaFormatQName(&str1, type->baseNs, type->base),
11488 xmlSchemaFormatQName(&str1, type->targetNamespace,
11489 type->name), NULL);
11490 FREE_AND_NULL(str1);
11491 FREE_AND_NULL(str2);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011492 /* Avoid confusion and erase the values. */
11493 type->base = NULL;
11494 type->baseNs = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011495 }
11496 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011497 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011498 /*
11499 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011500 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011501 child = node->children;
11502 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011503 /*
11504 * Add the annotation to the simple type ancestor.
11505 */
11506 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011507 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011508 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011509 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011510 if (parentType == XML_SCHEMA_TYPE_SIMPLE) {
11511 /*
11512 * Corresponds to <simpleType><restriction><simpleType>.
11513 */
William M. Brack2f2a6632004-08-20 23:09:47 +000011514 if (IS_SCHEMA(child, "simpleType")) {
11515 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011516 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011517 * src-restriction-base-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011518 * Either the base [attribute] or the simpleType [child] of the
11519 * <restriction> element must be present, but not both.
William M. Brack2f2a6632004-08-20 23:09:47 +000011520 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011521 xmlSchemaPContentErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000011522 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011523 NULL, node, child,
William M. Brack2f2a6632004-08-20 23:09:47 +000011524 "The attribute 'base' and the <simpleType> child are "
11525 "mutually exclusive", NULL);
11526 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011527 type->baseType = (xmlSchemaTypePtr)
William M. Brack2f2a6632004-08-20 23:09:47 +000011528 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011529 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011530 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011531 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011532 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011533 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011534 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011535 "Either the attribute 'base' or a <simpleType> child "
11536 "must be present", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000011537 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011538 } else if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11539 /*
11540 * Corresponds to <complexType><complexContent><restriction>...
11541 * followed by:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011542 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011543 * Model groups <all>, <choice> and <sequence>.
11544 */
11545 if (IS_SCHEMA(child, "all")) {
11546 type->subtypes = (xmlSchemaTypePtr)
11547 xmlSchemaParseModelGroup(ctxt, schema, child,
11548 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011549 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011550 } else if (IS_SCHEMA(child, "choice")) {
11551 type->subtypes = (xmlSchemaTypePtr)
11552 xmlSchemaParseModelGroup(ctxt,
11553 schema, child, XML_SCHEMA_TYPE_CHOICE, 1);
11554 child = child->next;
11555 } else if (IS_SCHEMA(child, "sequence")) {
11556 type->subtypes = (xmlSchemaTypePtr)
11557 xmlSchemaParseModelGroup(ctxt, schema, child,
11558 XML_SCHEMA_TYPE_SEQUENCE, 1);
11559 child = child->next;
11560 /*
11561 * Model group reference <group>.
11562 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011563 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011564 type->subtypes = (xmlSchemaTypePtr)
11565 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011566 /*
11567 * Note that the reference will be resolved in
11568 * xmlSchemaResolveTypeReferences();
11569 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011570 child = child->next;
11571 }
11572 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011573 /*
11574 * Corresponds to <complexType><simpleContent><restriction>...
11575 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011576 * "1.1 the simple type definition corresponding to the <simpleType>
11577 * among the [children] of <restriction> if there is one;"
11578 */
11579 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011580 /*
11581 * We will store the to-be-restricted simple type in
11582 * type->contentTypeDef *temporarily*.
11583 */
11584 type->contentTypeDef = (xmlSchemaTypePtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011585 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011586 if ( type->contentTypeDef == NULL)
11587 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011588 child = child->next;
11589 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011590 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011591
11592 if ((parentType == XML_SCHEMA_TYPE_SIMPLE) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011593 (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011594 xmlSchemaFacetPtr facet, lastfacet = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011595 /*
11596 * Corresponds to <complexType><simpleContent><restriction>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011597 * <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011598 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011599
Daniel Veillard01fa6152004-06-29 17:04:39 +000011600 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011601 * Add the facets to the simple type ancestor.
Daniel Veillard01fa6152004-06-29 17:04:39 +000011602 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000011603 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011604 * TODO: Datatypes: 4.1.3 Constraints on XML Representation of
11605 * Simple Type Definition Schema Representation Constraint:
Daniel Veillardc0826a72004-08-10 14:17:33 +000011606 * *Single Facet Value*
11607 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000011608 while ((IS_SCHEMA(child, "minInclusive")) ||
11609 (IS_SCHEMA(child, "minExclusive")) ||
11610 (IS_SCHEMA(child, "maxInclusive")) ||
11611 (IS_SCHEMA(child, "maxExclusive")) ||
11612 (IS_SCHEMA(child, "totalDigits")) ||
11613 (IS_SCHEMA(child, "fractionDigits")) ||
11614 (IS_SCHEMA(child, "pattern")) ||
11615 (IS_SCHEMA(child, "enumeration")) ||
11616 (IS_SCHEMA(child, "whiteSpace")) ||
11617 (IS_SCHEMA(child, "length")) ||
11618 (IS_SCHEMA(child, "maxLength")) ||
11619 (IS_SCHEMA(child, "minLength"))) {
11620 facet = xmlSchemaParseFacet(ctxt, schema, child);
11621 if (facet != NULL) {
11622 if (lastfacet == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011623 type->facets = facet;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011624 else
11625 lastfacet->next = facet;
11626 lastfacet = facet;
11627 lastfacet->next = NULL;
11628 }
11629 child = child->next;
11630 }
11631 /*
11632 * Create links for derivation and validation.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011633 */
11634 if (type->facets != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000011635 xmlSchemaFacetLinkPtr facetLink, lastFacetLink = NULL;
11636
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011637 facet = type->facets;
11638 do {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011639 facetLink = (xmlSchemaFacetLinkPtr)
11640 xmlMalloc(sizeof(xmlSchemaFacetLink));
Daniel Veillard01fa6152004-06-29 17:04:39 +000011641 if (facetLink == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000011642 xmlSchemaPErrMemory(ctxt, "allocating a facet link", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011643 xmlFree(facetLink);
11644 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011645 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000011646 facetLink->facet = facet;
11647 facetLink->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011648 if (lastFacetLink == NULL)
11649 type->facetSet = facetLink;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011650 else
11651 lastFacetLink->next = facetLink;
11652 lastFacetLink = facetLink;
11653 facet = facet->next;
11654 } while (facet != NULL);
11655 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011656 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011657 if (type->type == XML_SCHEMA_TYPE_COMPLEX) {
11658 /*
11659 * Attribute uses/declarations.
11660 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011661 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11662 (xmlSchemaItemListPtr *) &(type->attrUses),
11663 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
11664 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011665 /*
11666 * Attribute wildcard.
11667 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011668 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011669 type->attributeWildcard =
11670 xmlSchemaParseAnyAttribute(ctxt, schema, child);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011671 child = child->next;
11672 }
11673 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011674 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011675 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11676 xmlSchemaPContentErr(ctxt,
11677 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011678 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011679 "annotation?, (group | all | choice | sequence)?, "
11680 "((attribute | attributeGroup)*, anyAttribute?))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011681 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011682 xmlSchemaPContentErr(ctxt,
11683 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011684 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011685 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11686 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11687 "length | minLength | maxLength | enumeration | whiteSpace | "
11688 "pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))");
11689 } else {
11690 /* Simple type */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011691 xmlSchemaPContentErr(ctxt,
11692 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011693 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011694 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11695 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11696 "length | minLength | maxLength | enumeration | whiteSpace | "
11697 "pattern)*))");
11698 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011699 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011700 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011701}
11702
11703/**
11704 * xmlSchemaParseExtension:
11705 * @ctxt: a schema validation context
11706 * @schema: the schema being built
11707 * @node: a subtree containing XML Schema informations
11708 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011709 * Parses an <extension>, which is found inside a
11710 * <simpleContent> or <complexContent>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011711 * *WARNING* this interface is highly subject to change.
Daniel Veillard4255d502002-04-16 15:50:10 +000011712 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011713 * TODO: Returns the type definition or NULL in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +000011714 */
11715static xmlSchemaTypePtr
11716xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011717 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011718{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011719 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011720 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011721 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011722
11723 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11724 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011725 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011726 type = ctxt->ctxtType;
11727 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION;
Daniel Veillard4255d502002-04-16 15:50:10 +000011728
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011729 /*
11730 * Check for illegal attributes.
11731 */
11732 attr = node->properties;
11733 while (attr != NULL) {
11734 if (attr->ns == NULL) {
11735 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11736 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011737 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011738 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011739 }
11740 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011741 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011742 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011743 }
11744 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011745 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011746
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011747 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011748
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011749 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011750 * Attribute "base" - mandatory.
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011751 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011752 if ((xmlSchemaPValAttrQName(ctxt, schema, NULL, node,
11753 "base", &(type->baseNs), &(type->base)) == 0) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011754 (type->base == NULL)) {
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011755 xmlSchemaPMissingAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011756 XML_SCHEMAP_S4S_ATTR_MISSING,
11757 NULL, node, "base", NULL);
11758 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011759 /*
11760 * And now for the children...
11761 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011762 child = node->children;
11763 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011764 /*
11765 * Add the annotation to the type ancestor.
11766 */
11767 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011768 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011769 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011770 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011771 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11772 /*
11773 * Corresponds to <complexType><complexContent><extension>... and:
11774 *
11775 * Model groups <all>, <choice>, <sequence> and <group>.
11776 */
11777 if (IS_SCHEMA(child, "all")) {
11778 type->subtypes = (xmlSchemaTypePtr)
11779 xmlSchemaParseModelGroup(ctxt, schema,
11780 child, XML_SCHEMA_TYPE_ALL, 1);
11781 child = child->next;
11782 } else if (IS_SCHEMA(child, "choice")) {
11783 type->subtypes = (xmlSchemaTypePtr)
11784 xmlSchemaParseModelGroup(ctxt, schema,
11785 child, XML_SCHEMA_TYPE_CHOICE, 1);
11786 child = child->next;
11787 } else if (IS_SCHEMA(child, "sequence")) {
11788 type->subtypes = (xmlSchemaTypePtr)
11789 xmlSchemaParseModelGroup(ctxt, schema,
11790 child, XML_SCHEMA_TYPE_SEQUENCE, 1);
11791 child = child->next;
11792 } else if (IS_SCHEMA(child, "group")) {
11793 type->subtypes = (xmlSchemaTypePtr)
11794 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011795 /*
11796 * Note that the reference will be resolved in
11797 * xmlSchemaResolveTypeReferences();
11798 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011799 child = child->next;
11800 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011801 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011802 if (child != NULL) {
11803 /*
11804 * Attribute uses/declarations.
11805 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011806 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11807 (xmlSchemaItemListPtr *) &(type->attrUses),
11808 XML_SCHEMA_TYPE_EXTENSION, NULL) == -1)
11809 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011810 /*
11811 * Attribute wildcard.
11812 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011813 if (IS_SCHEMA(child, "anyAttribute")) {
11814 ctxt->ctxtType->attributeWildcard =
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011815 xmlSchemaParseAnyAttribute(ctxt, schema, child);
11816 child = child->next;
11817 }
11818 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011819 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011820 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11821 /* Complex content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011822 xmlSchemaPContentErr(ctxt,
11823 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011824 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011825 "(annotation?, ((group | all | choice | sequence)?, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011826 "((attribute | attributeGroup)*, anyAttribute?)))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011827 } else {
11828 /* Simple content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011829 xmlSchemaPContentErr(ctxt,
11830 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011831 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011832 "(annotation?, ((attribute | attributeGroup)*, "
11833 "anyAttribute?))");
11834 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011835 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011836 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011837}
11838
11839/**
11840 * xmlSchemaParseSimpleContent:
11841 * @ctxt: a schema validation context
11842 * @schema: the schema being built
11843 * @node: a subtree containing XML Schema informations
11844 *
11845 * parse a XML schema SimpleContent definition
11846 * *WARNING* this interface is highly subject to change
11847 *
11848 * Returns the type definition or NULL in case of error
11849 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011850static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011851xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011852 xmlSchemaPtr schema, xmlNodePtr node,
11853 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011854{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011855 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011856 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011857 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011858
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011859 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11860 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011861 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011862 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011863 /* Not a component, don't create it. */
11864 type = ctxt->ctxtType;
11865 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
11866 /*
11867 * Check for illegal attributes.
11868 */
11869 attr = node->properties;
11870 while (attr != NULL) {
11871 if (attr->ns == NULL) {
11872 if ((!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011873 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011874 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011875 }
11876 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011877 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011878 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011879 }
11880 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011881 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011882
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011883 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +000011884
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011885 /*
11886 * And now for the children...
11887 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011888 child = node->children;
11889 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011890 /*
11891 * Add the annotation to the complex type ancestor.
11892 */
11893 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011894 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011895 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011896 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011897 if (child == NULL) {
11898 xmlSchemaPContentErr(ctxt,
11899 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011900 NULL, node, NULL, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011901 "(annotation?, (restriction | extension))");
11902 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011903 if (child == NULL) {
11904 xmlSchemaPContentErr(ctxt,
11905 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011906 NULL, node, NULL, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011907 "(annotation?, (restriction | extension))");
11908 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011909 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011910 xmlSchemaParseRestriction(ctxt, schema, child,
11911 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011912 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011913 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011914 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011915 xmlSchemaParseExtension(ctxt, schema, child,
11916 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011917 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011918 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011919 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011920 if (child != NULL) {
11921 xmlSchemaPContentErr(ctxt,
11922 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011923 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011924 "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000011925 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011926 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000011927}
11928
11929/**
11930 * xmlSchemaParseComplexContent:
11931 * @ctxt: a schema validation context
11932 * @schema: the schema being built
11933 * @node: a subtree containing XML Schema informations
11934 *
11935 * parse a XML schema ComplexContent definition
11936 * *WARNING* this interface is highly subject to change
11937 *
11938 * Returns the type definition or NULL in case of error
11939 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011940static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011941xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011942 xmlSchemaPtr schema, xmlNodePtr node,
11943 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011944{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011945 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011946 xmlNodePtr child = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011947 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011948
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011949 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11950 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011951 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011952 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011953 /* Not a component, don't create it. */
11954 type = ctxt->ctxtType;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011955 /*
11956 * Check for illegal attributes.
11957 */
11958 attr = node->properties;
11959 while (attr != NULL) {
11960 if (attr->ns == NULL) {
11961 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011962 (!xmlStrEqual(attr->name, BAD_CAST "mixed")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011963 {
11964 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011965 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011966 }
11967 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
11968 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011969 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011970 }
11971 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011972 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011973
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011974 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011975
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011976 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011977 * Set the 'mixed' on the complex type ancestor.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011978 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011979 if (xmlGetBooleanProp(ctxt, node, "mixed", 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011980 if ((type->flags & XML_SCHEMAS_TYPE_MIXED) == 0)
11981 type->flags |= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011982 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011983 child = node->children;
11984 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011985 /*
11986 * Add the annotation to the complex type ancestor.
11987 */
11988 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011989 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011990 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011991 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011992 if (child == NULL) {
11993 xmlSchemaPContentErr(ctxt,
11994 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011995 NULL, node, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011996 NULL, "(annotation?, (restriction | extension))");
11997 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011998 if (child == NULL) {
11999 xmlSchemaPContentErr(ctxt,
12000 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012001 NULL, node, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012002 NULL, "(annotation?, (restriction | extension))");
12003 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012004 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012005 xmlSchemaParseRestriction(ctxt, schema, child,
12006 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012007 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012008 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012009 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012010 xmlSchemaParseExtension(ctxt, schema, child,
12011 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012012 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012013 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012014 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012015 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012016 xmlSchemaPContentErr(ctxt,
12017 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012018 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012019 NULL, "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012020 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012021 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000012022}
12023
12024/**
12025 * xmlSchemaParseComplexType:
12026 * @ctxt: a schema validation context
12027 * @schema: the schema being built
12028 * @node: a subtree containing XML Schema informations
12029 *
12030 * parse a XML schema Complex Type definition
12031 * *WARNING* this interface is highly subject to change
12032 *
12033 * Returns the type definition or NULL in case of error
12034 */
12035static xmlSchemaTypePtr
12036xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +000012037 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +000012038{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012039 xmlSchemaTypePtr type, ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012040 xmlNodePtr child = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012041 const xmlChar *name = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012042 xmlAttrPtr attr;
12043 const xmlChar *attrValue;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012044#ifdef ENABLE_NAMED_LOCALS
Daniel Veillard1a380b82004-10-21 16:00:06 +000012045 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012046#endif
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012047 int final = 0, block = 0, hasRestrictionOrExtension = 0;
Daniel Veillard1a380b82004-10-21 16:00:06 +000012048
Daniel Veillard4255d502002-04-16 15:50:10 +000012049
12050 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
12051 return (NULL);
12052
Daniel Veillard01fa6152004-06-29 17:04:39 +000012053 ctxtType = ctxt->ctxtType;
12054
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012055 if (topLevel) {
12056 attr = xmlSchemaGetPropNode(node, "name");
12057 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012058 xmlSchemaPMissingAttrErr(ctxt,
12059 XML_SCHEMAP_S4S_ATTR_MISSING, NULL, node, "name", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012060 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012061 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012062 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
12063 return (NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012064 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012065 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012066
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012067 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012068 /*
12069 * Parse as local complex type definition.
12070 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012071#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +000012072 snprintf(buf, 39, "#CT%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012073 type = xmlSchemaAddType(ctxt, schema,
12074 XML_SCHEMA_TYPE_COMPLEX,
12075 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012076 ctxt->targetNamespace, node, 0);
12077#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012078 type = xmlSchemaAddType(ctxt, schema,
12079 XML_SCHEMA_TYPE_COMPLEX,
12080 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012081#endif
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012082 if (type == NULL)
12083 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012084 name = type->name;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012085 type->node = node;
12086 type->type = XML_SCHEMA_TYPE_COMPLEX;
12087 /*
12088 * TODO: We need the target namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012089 */
12090 } else {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012091 /*
12092 * Parse as global complex type definition.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012093 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012094 type = xmlSchemaAddType(ctxt, schema,
12095 XML_SCHEMA_TYPE_COMPLEX,
12096 name, ctxt->targetNamespace, node, 1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012097 if (type == NULL)
12098 return (NULL);
12099 type->node = node;
12100 type->type = XML_SCHEMA_TYPE_COMPLEX;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012101 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +000012102 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012103 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012104 /*
12105 * Handle attributes.
12106 */
12107 attr = node->properties;
12108 while (attr != NULL) {
12109 if (attr->ns == NULL) {
12110 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
12111 /*
12112 * Attribute "id".
12113 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012114 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012115 } else if (xmlStrEqual(attr->name, BAD_CAST "mixed")) {
12116 /*
12117 * Attribute "mixed".
12118 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012119 if (xmlSchemaPGetBoolNodeValue(ctxt,
12120 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012121 type->flags |= XML_SCHEMAS_TYPE_MIXED;
12122 } else if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012123 /*
12124 * Attributes of global complex type definitions.
12125 */
12126 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
12127 /* Pass. */
12128 } else if (xmlStrEqual(attr->name, BAD_CAST "abstract")) {
12129 /*
12130 * Attribute "abstract".
12131 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012132 if (xmlSchemaPGetBoolNodeValue(ctxt,
12133 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012134 type->flags |= XML_SCHEMAS_TYPE_ABSTRACT;
12135 } else if (xmlStrEqual(attr->name, BAD_CAST "final")) {
12136 /*
12137 * Attribute "final".
12138 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012139 attrValue = xmlSchemaGetNodeContent(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012140 (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012141 if (xmlSchemaPValAttrBlockFinal(attrValue,
12142 &(type->flags),
12143 -1,
12144 XML_SCHEMAS_TYPE_FINAL_EXTENSION,
12145 XML_SCHEMAS_TYPE_FINAL_RESTRICTION,
12146 -1, -1, -1) != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012147 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012148 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012149 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012150 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012151 "(#all | List of (extension | restriction))",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012152 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012153 } else
12154 final = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012155 } else if (xmlStrEqual(attr->name, BAD_CAST "block")) {
12156 /*
12157 * Attribute "block".
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012158 */
12159 attrValue = xmlSchemaGetNodeContent(ctxt,
12160 (xmlNodePtr) attr);
12161 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012162 -1,
12163 XML_SCHEMAS_TYPE_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012164 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012165 -1, -1, -1) != 0) {
12166 xmlSchemaPSimpleTypeErr(ctxt,
12167 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012168 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012169 "(#all | List of (extension | restriction)) ",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012170 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012171 } else
12172 block = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012173 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012174 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012175 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012176 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012177 } else {
12178 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012179 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012180 }
12181 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012182 xmlSchemaPIllegalAttrErr(ctxt,
12183 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012184 }
12185 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012186 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012187 if (! block) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000012188 /*
12189 * Apply default "block" values.
12190 */
12191 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
12192 type->flags |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
12193 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
12194 type->flags |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
12195 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012196 if (! final) {
12197 /*
12198 * Apply default "block" values.
12199 */
12200 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
12201 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
12202 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
12203 type->flags |= XML_SCHEMAS_TYPE_FINAL_EXTENSION;
12204 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012205 /*
12206 * And now for the children...
12207 */
Daniel Veillard4255d502002-04-16 15:50:10 +000012208 child = node->children;
12209 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012210 type->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012211 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012212 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012213 ctxt->ctxtType = type;
Daniel Veillard4255d502002-04-16 15:50:10 +000012214 if (IS_SCHEMA(child, "simpleContent")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012215 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012216 * <complexType><simpleContent>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012217 * 3.4.3 : 2.2
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012218 * Specifying mixed='true' when the <simpleContent>
12219 * alternative is chosen has no effect
12220 */
William M. Bracke7091952004-05-11 15:09:58 +000012221 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
12222 type->flags ^= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012223 xmlSchemaParseSimpleContent(ctxt, schema, child,
12224 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012225 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012226 } else if (IS_SCHEMA(child, "complexContent")) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012227 /*
12228 * <complexType><complexContent>...
12229 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012230 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012231 xmlSchemaParseComplexContent(ctxt, schema, child,
12232 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012233 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012234 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012235 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012236 * E.g <complexType><sequence>... or <complexType><attribute>... etc.
12237 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012238 * SPEC
12239 * "...the third alternative (neither <simpleContent> nor
12240 * <complexContent>) is chosen. This case is understood as shorthand
12241 * for complex content restricting the ·ur-type definition·, and the
12242 * details of the mappings should be modified as necessary.
12243 */
12244 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
12245 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012246 /*
12247 * Parse model groups.
12248 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012249 if (IS_SCHEMA(child, "all")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012250 type->subtypes = (xmlSchemaTypePtr)
12251 xmlSchemaParseModelGroup(ctxt, schema, child,
12252 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012253 child = child->next;
12254 } else if (IS_SCHEMA(child, "choice")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012255 type->subtypes = (xmlSchemaTypePtr)
12256 xmlSchemaParseModelGroup(ctxt, schema, child,
12257 XML_SCHEMA_TYPE_CHOICE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012258 child = child->next;
12259 } else if (IS_SCHEMA(child, "sequence")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012260 type->subtypes = (xmlSchemaTypePtr)
12261 xmlSchemaParseModelGroup(ctxt, schema, child,
12262 XML_SCHEMA_TYPE_SEQUENCE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012263 child = child->next;
12264 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012265 type->subtypes = (xmlSchemaTypePtr)
12266 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012267 /*
12268 * Note that the reference will be resolved in
12269 * xmlSchemaResolveTypeReferences();
12270 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012271 child = child->next;
12272 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012273 /*
12274 * Parse attribute decls/refs.
12275 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012276 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
12277 (xmlSchemaItemListPtr *) &(type->attrUses),
12278 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
12279 return(NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012280 /*
12281 * Parse attribute wildcard.
12282 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012283 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012284 type->attributeWildcard = xmlSchemaParseAnyAttribute(ctxt, schema, child);
12285 child = child->next;
12286 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012287 }
12288 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012289 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012290 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012291 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012292 NULL, "(annotation?, (simpleContent | complexContent | "
12293 "((group | all | choice | sequence)?, ((attribute | "
12294 "attributeGroup)*, anyAttribute?))))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012295 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012296 /*
12297 * REDEFINE: SPEC src-redefine (5)
12298 */
12299 if (topLevel && ctxt->isRedefine && (! hasRestrictionOrExtension)) {
12300 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012301 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012302 "<complexType> must have a <restriction> or <extension> "
12303 "grand-child", NULL);
12304 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012305 ctxt->ctxtType = ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012306 return (type);
12307}
12308
Daniel Veillard4255d502002-04-16 15:50:10 +000012309/************************************************************************
12310 * *
12311 * Validating using Schemas *
12312 * *
12313 ************************************************************************/
12314
12315/************************************************************************
12316 * *
12317 * Reading/Writing Schemas *
12318 * *
12319 ************************************************************************/
12320
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012321#if 0 /* Will be enabled if it is clear what options are needed. */
12322/**
12323 * xmlSchemaParserCtxtSetOptions:
12324 * @ctxt: a schema parser context
12325 * @options: a combination of xmlSchemaParserOption
12326 *
12327 * Sets the options to be used during the parse.
12328 *
12329 * Returns 0 in case of success, -1 in case of an
12330 * API error.
12331 */
12332static int
12333xmlSchemaParserCtxtSetOptions(xmlSchemaParserCtxtPtr ctxt,
12334 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012335
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012336{
12337 int i;
12338
12339 if (ctxt == NULL)
12340 return (-1);
12341 /*
12342 * WARNING: Change the start value if adding to the
12343 * xmlSchemaParseOption.
12344 */
12345 for (i = 1; i < (int) sizeof(int) * 8; i++) {
12346 if (options & 1<<i) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012347 return (-1);
12348 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012349 }
12350 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012351 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012352}
12353
12354/**
12355 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012356 * @ctxt: a schema parser context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012357 *
12358 * Returns the option combination of the parser context.
12359 */
12360static int
12361xmlSchemaParserCtxtGetOptions(xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012362
12363{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012364 if (ctxt == NULL)
12365 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012366 else
12367 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012368}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012369#endif
12370
Daniel Veillard4255d502002-04-16 15:50:10 +000012371/**
12372 * xmlSchemaNewParserCtxt:
12373 * @URL: the location of the schema
12374 *
12375 * Create an XML Schemas parse context for that file/resource expected
12376 * to contain an XML Schemas file.
12377 *
12378 * Returns the parser context or NULL in case of error
12379 */
12380xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012381xmlSchemaNewParserCtxt(const char *URL)
12382{
Daniel Veillard4255d502002-04-16 15:50:10 +000012383 xmlSchemaParserCtxtPtr ret;
12384
12385 if (URL == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012386 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000012387
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012388 ret = xmlSchemaParserCtxtCreate();
12389 if (ret == NULL)
12390 return(NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012391 ret->dict = xmlDictCreate();
12392 ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012393 return (ret);
12394}
12395
12396/**
Daniel Veillard6045c902002-10-09 21:13:59 +000012397 * xmlSchemaNewMemParserCtxt:
12398 * @buffer: a pointer to a char array containing the schemas
12399 * @size: the size of the array
12400 *
12401 * Create an XML Schemas parse context for that memory buffer expected
12402 * to contain an XML Schemas file.
12403 *
12404 * Returns the parser context or NULL in case of error
12405 */
12406xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012407xmlSchemaNewMemParserCtxt(const char *buffer, int size)
12408{
Daniel Veillard6045c902002-10-09 21:13:59 +000012409 xmlSchemaParserCtxtPtr ret;
12410
12411 if ((buffer == NULL) || (size <= 0))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012412 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012413 ret = xmlSchemaParserCtxtCreate();
12414 if (ret == NULL)
12415 return(NULL);
Daniel Veillard6045c902002-10-09 21:13:59 +000012416 ret->buffer = buffer;
12417 ret->size = size;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012418 ret->dict = xmlDictCreate();
Daniel Veillard6045c902002-10-09 21:13:59 +000012419 return (ret);
12420}
12421
12422/**
Daniel Veillard9d751502003-10-29 13:21:47 +000012423 * xmlSchemaNewDocParserCtxt:
12424 * @doc: a preparsed document tree
12425 *
12426 * Create an XML Schemas parse context for that document.
12427 * NB. The document may be modified during the parsing process.
12428 *
12429 * Returns the parser context or NULL in case of error
12430 */
12431xmlSchemaParserCtxtPtr
12432xmlSchemaNewDocParserCtxt(xmlDocPtr doc)
12433{
12434 xmlSchemaParserCtxtPtr ret;
12435
12436 if (doc == NULL)
12437 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012438 ret = xmlSchemaParserCtxtCreate();
12439 if (ret == NULL)
12440 return(NULL);
Daniel Veillard9d751502003-10-29 13:21:47 +000012441 ret->doc = doc;
William M. Brackcf9eadf2003-12-25 13:24:05 +000012442 ret->dict = xmlDictCreate();
Daniel Veillarddda22c12004-01-24 08:31:30 +000012443 /* The application has responsibility for the document */
12444 ret->preserve = 1;
Daniel Veillard9d751502003-10-29 13:21:47 +000012445
12446 return (ret);
12447}
12448
12449/**
Daniel Veillard4255d502002-04-16 15:50:10 +000012450 * xmlSchemaFreeParserCtxt:
12451 * @ctxt: the schema parser context
12452 *
12453 * Free the resources associated to the schema parser context
12454 */
12455void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012456xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt)
12457{
Daniel Veillard4255d502002-04-16 15:50:10 +000012458 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012459 return;
Daniel Veillarddda22c12004-01-24 08:31:30 +000012460 if (ctxt->doc != NULL && !ctxt->preserve)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012461 xmlFreeDoc(ctxt->doc);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012462 if (ctxt->vctxt != NULL) {
12463 xmlSchemaFreeValidCtxt(ctxt->vctxt);
12464 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012465 if (ctxt->ownsConstructor && (ctxt->constructor != NULL)) {
12466 xmlSchemaConstructionCtxtFree(ctxt->constructor);
12467 ctxt->constructor = NULL;
12468 ctxt->ownsConstructor = 0;
12469 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012470 if (ctxt->attrProhibs != NULL)
12471 xmlSchemaItemListFree(ctxt->attrProhibs);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012472 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000012473 xmlFree(ctxt);
12474}
12475
12476/************************************************************************
12477 * *
12478 * Building the content models *
12479 * *
12480 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012481
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012482static void
12483xmlSchemaBuildContentModelForSubstGroup(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012484 xmlSchemaParticlePtr particle, int counter, xmlAutomataStatePtr end)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012485{
Daniel Veillarda980bef2005-07-18 21:34:03 +000012486 xmlAutomataStatePtr start, tmp;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012487 xmlSchemaElementPtr elemDecl, member;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012488 xmlSchemaSubstGroupPtr substGroup;
12489 int i;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012490
12491 elemDecl = (xmlSchemaElementPtr) particle->children;
12492 /*
12493 * Wrap the substitution group with a CHOICE.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012494 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012495 start = pctxt->state;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012496 if (end == NULL)
12497 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012498 substGroup = xmlSchemaSubstGroupGet(pctxt, elemDecl);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012499 if (substGroup == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012500 xmlSchemaPErr(pctxt, WXS_ITEM_NODE(particle),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012501 XML_SCHEMAP_INTERNAL,
12502 "Internal error: xmlSchemaBuildContentModelForSubstGroup, "
12503 "declaration is marked having a subst. group but none "
12504 "available.\n", elemDecl->name, NULL);
12505 return;
12506 }
Daniel Veillarda980bef2005-07-18 21:34:03 +000012507 if (counter >= 0) {
12508 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012509 * NOTE that we put the declaration in, even if it's abstract.
12510 * However, an error will be raised during *validation* if an element
12511 * information item shall be validated against an abstract element
12512 * declaration.
Daniel Veillarda980bef2005-07-18 21:34:03 +000012513 */
12514 tmp = xmlAutomataNewCountedTrans(pctxt->am, start, NULL, counter);
12515 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12516 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12517 /*
12518 * Add subst. group members.
12519 */
12520 for (i = 0; i < substGroup->members->nbItems; i++) {
12521 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12522 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12523 member->name, member->targetNamespace, member);
12524 }
12525 } else if (particle->maxOccurs == 1) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012526 /*
12527 * NOTE that we put the declaration in, even if it's abstract,
12528 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012529 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012530 xmlAutomataNewTransition2(pctxt->am,
12531 start, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012532 elemDecl->name, elemDecl->targetNamespace, elemDecl), end);
12533 /*
12534 * Add subst. group members.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012535 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012536 for (i = 0; i < substGroup->members->nbItems; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012537 member = (xmlSchemaElementPtr) substGroup->members->items[i];
Daniel Veillarda980bef2005-07-18 21:34:03 +000012538 tmp = xmlAutomataNewOnceTrans2(pctxt->am, start, NULL,
12539 member->name, member->targetNamespace,
12540 1, 1, member);
12541 xmlAutomataNewEpsilon(pctxt->am, tmp, end);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012542 }
12543 } else {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012544 xmlAutomataStatePtr hop;
12545 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12546 UNBOUNDED : particle->maxOccurs - 1;
12547 int minOccurs = particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
12548
12549 counter =
12550 xmlAutomataNewCounter(pctxt->am, minOccurs,
12551 maxOccurs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012552 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012553
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012554 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012555 xmlAutomataNewTransition2(pctxt->am,
12556 start, NULL,
12557 elemDecl->name, elemDecl->targetNamespace, elemDecl),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012558 hop);
12559 /*
Daniel Veillarda980bef2005-07-18 21:34:03 +000012560 * Add subst. group members.
12561 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012562 for (i = 0; i < substGroup->members->nbItems; i++) {
12563 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12564 xmlAutomataNewEpsilon(pctxt->am,
12565 xmlAutomataNewTransition2(pctxt->am,
12566 start, NULL,
12567 member->name, member->targetNamespace, member),
12568 hop);
12569 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012570 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12571 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
12572 }
12573 if (particle->minOccurs == 0)
12574 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012575 pctxt->state = end;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012576}
12577
12578static void
12579xmlSchemaBuildContentModelForElement(xmlSchemaParserCtxtPtr ctxt,
12580 xmlSchemaParticlePtr particle)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012581{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012582 if (((xmlSchemaElementPtr) particle->children)->flags &
12583 XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012584 /*
12585 * Substitution groups.
12586 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012587 xmlSchemaBuildContentModelForSubstGroup(ctxt, particle, -1, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012588 } else {
12589 xmlSchemaElementPtr elemDecl;
12590 xmlAutomataStatePtr start;
12591
12592 elemDecl = (xmlSchemaElementPtr) particle->children;
12593
12594 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012595 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012596 if (particle->maxOccurs == 1) {
12597 start = ctxt->state;
12598 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012599 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12600 } else if ((particle->maxOccurs >= UNBOUNDED) &&
12601 (particle->minOccurs < 2)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012602 /* Special case. */
12603 start = ctxt->state;
12604 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12605 elemDecl->name, elemDecl->targetNamespace, elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012606 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, start);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012607 } else {
12608 int counter;
12609 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12610 UNBOUNDED : particle->maxOccurs - 1;
12611 int minOccurs = particle->minOccurs < 1 ?
12612 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012613
12614 start = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012615 counter = xmlAutomataNewCounter(ctxt->am, minOccurs, maxOccurs);
12616 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12617 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12618 xmlAutomataNewCountedTrans(ctxt->am, ctxt->state, start, counter);
12619 ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, ctxt->state,
12620 NULL, counter);
12621 }
12622 if (particle->minOccurs == 0)
12623 xmlAutomataNewEpsilon(ctxt->am, start, ctxt->state);
12624 }
12625}
12626
Daniel Veillard4255d502002-04-16 15:50:10 +000012627/**
12628 * xmlSchemaBuildAContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012629 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012630 * @particle: the particle component
12631 * @name: the complex type's name whose content is being built
Daniel Veillard4255d502002-04-16 15:50:10 +000012632 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012633 * Create the automaton for the {content type} of a complex type.
12634 *
Daniel Veillard4255d502002-04-16 15:50:10 +000012635 */
12636static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012637xmlSchemaBuildAContentModel(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012638 xmlSchemaParticlePtr particle)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012639{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012640 if (particle == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012641 PERROR_INT("xmlSchemaBuildAContentModel", "particle is NULL");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012642 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012643 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012644 if (particle->children == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012645 /*
12646 * Just return in this case. A missing "term" of the particle
12647 * might arise due to an invalid "term" component.
12648 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012649 return;
12650 }
12651
12652 switch (particle->children->type) {
12653 case XML_SCHEMA_TYPE_ANY: {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012654 xmlAutomataStatePtr start, end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012655 xmlSchemaWildcardPtr wild;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012656 xmlSchemaWildcardNsPtr ns;
Daniel Veillard32370232002-10-16 14:08:14 +000012657
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012658 wild = (xmlSchemaWildcardPtr) particle->children;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012659
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012660 start = pctxt->state;
12661 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012662
12663 if (particle->maxOccurs == 1) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012664 if (wild->any == 1) {
12665 /*
12666 * We need to add both transitions:
12667 *
12668 * 1. the {"*", "*"} for elements in a namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012669 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012670 pctxt->state =
12671 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012672 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012673 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012674 /*
12675 * 2. the {"*"} for elements in no namespace.
12676 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012677 pctxt->state =
12678 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012679 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012680 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012681
12682 } else if (wild->nsSet != NULL) {
12683 ns = wild->nsSet;
12684 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012685 pctxt->state = start;
12686 pctxt->state = xmlAutomataNewTransition2(pctxt->am,
12687 pctxt->state, NULL, BAD_CAST "*", ns->value, wild);
12688 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012689 ns = ns->next;
12690 } while (ns != NULL);
12691
12692 } else if (wild->negNsSet != NULL) {
Daniel Veillard6e65e152005-08-09 11:09:52 +000012693 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
12694 start, end, BAD_CAST "*", wild->negNsSet->value,
12695 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012696 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000012697 } else {
12698 int counter;
12699 xmlAutomataStatePtr hop;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012700 int maxOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012701 particle->maxOccurs == UNBOUNDED ? UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012702 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012703 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012704
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012705 counter = xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12706 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012707 if (wild->any == 1) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012708 pctxt->state =
12709 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012710 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012711 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
12712 pctxt->state =
12713 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012714 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012715 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012716 } else if (wild->nsSet != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012717 ns = wild->nsSet;
12718 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012719 pctxt->state =
12720 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012721 start, NULL, BAD_CAST "*", ns->value, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012722 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012723 ns = ns->next;
12724 } while (ns != NULL);
12725
12726 } else if (wild->negNsSet != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012727 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
Daniel Veillard9efc4762005-07-19 14:33:55 +000012728 start, hop, BAD_CAST "*", wild->negNsSet->value,
12729 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012730 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012731 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12732 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012733 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012734 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012735 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012736 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012737 pctxt->state = end;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012738 break;
12739 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012740 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012741 xmlSchemaBuildContentModelForElement(pctxt, particle);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012742 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012743 case XML_SCHEMA_TYPE_SEQUENCE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012744 xmlSchemaTreeItemPtr sub;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012745
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012746 /*
12747 * If max and min occurances are default (1) then
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012748 * simply iterate over the particles of the <sequence>.
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012749 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012750 if ((particle->minOccurs == 1) && (particle->maxOccurs == 1)) {
12751 sub = particle->children->children;
12752 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012753 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012754 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012755 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012756 }
12757 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012758 xmlAutomataStatePtr oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012759
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012760 if (particle->maxOccurs >= UNBOUNDED) {
12761 if (particle->minOccurs > 1) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012762 xmlAutomataStatePtr tmp;
12763 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012764
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012765 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012766 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012767 oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012768
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012769 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012770 particle->minOccurs - 1, UNBOUNDED);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012771
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012772 sub = particle->children->children;
12773 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012774 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012775 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012776 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012777 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012778 tmp = pctxt->state;
12779 xmlAutomataNewCountedTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012780 oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012781 pctxt->state =
12782 xmlAutomataNewCounterTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012783 NULL, counter);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012784
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012785 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012786 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Daniel Veillardafc05b62005-07-17 06:11:19 +000012787 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012788 oldstate = pctxt->state;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012789
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012790 sub = particle->children->children;
12791 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012792 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012793 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012794 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012795 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012796 xmlAutomataNewEpsilon(pctxt->am, pctxt->state,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012797 oldstate);
Daniel Veillardafc05b62005-07-17 06:11:19 +000012798 /*
12799 * epsilon needed to block previous trans from
12800 * being allowed to enter back from another
12801 * construct
12802 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012803 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
12804 pctxt->state, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012805 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012806 xmlAutomataNewEpsilon(pctxt->am,
12807 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012808 }
12809 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012810 } else if ((particle->maxOccurs > 1)
12811 || (particle->minOccurs > 1)) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012812 xmlAutomataStatePtr tmp;
12813 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012814
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012815 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012816 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012817 oldstate = pctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +000012818
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012819 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012820 particle->minOccurs - 1,
12821 particle->maxOccurs - 1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012822
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012823 sub = particle->children->children;
12824 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012825 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012826 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012827 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012828 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012829 tmp = pctxt->state;
12830 xmlAutomataNewCountedTrans(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012831 tmp, oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012832 pctxt->state =
12833 xmlAutomataNewCounterTrans(pctxt->am, tmp, NULL,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012834 counter);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012835 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012836 xmlAutomataNewEpsilon(pctxt->am,
12837 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012838 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012839 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012840 sub = particle->children->children;
12841 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012842 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012843 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012844 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012845 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012846 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012847 xmlAutomataNewEpsilon(pctxt->am, oldstate,
12848 pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012849 }
12850 }
12851 }
12852 break;
12853 }
12854 case XML_SCHEMA_TYPE_CHOICE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012855 xmlSchemaTreeItemPtr sub;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012856 xmlAutomataStatePtr start, end;
Daniel Veillardb509f152002-04-17 16:28:10 +000012857
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012858 start = pctxt->state;
12859 end = xmlAutomataNewState(pctxt->am);
Daniel Veillard7646b182002-04-20 06:41:40 +000012860
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012861 /*
12862 * iterate over the subtypes and remerge the end with an
12863 * epsilon transition
12864 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012865 if (particle->maxOccurs == 1) {
12866 sub = particle->children->children;
12867 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012868 pctxt->state = start;
12869 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012870 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012871 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012872 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012873 }
12874 } else {
12875 int counter;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012876 xmlAutomataStatePtr hop, base;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012877 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12878 UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012879 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012880 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Daniel Veillard7646b182002-04-20 06:41:40 +000012881
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012882 /*
12883 * use a counter to keep track of the number of transtions
12884 * which went through the choice.
12885 */
12886 counter =
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012887 xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12888 hop = xmlAutomataNewState(pctxt->am);
12889 base = xmlAutomataNewState(pctxt->am);
Daniel Veillard6231e842002-04-18 11:54:04 +000012890
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012891 sub = particle->children->children;
12892 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012893 pctxt->state = base;
12894 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012895 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012896 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012897 sub = sub->next;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012898 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012899 xmlAutomataNewEpsilon(pctxt->am, start, base);
12900 xmlAutomataNewCountedTrans(pctxt->am, hop, base, counter);
12901 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012902 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012903 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012904 xmlAutomataNewEpsilon(pctxt->am, start, end);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012905 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012906 pctxt->state = end;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012907 break;
12908 }
12909 case XML_SCHEMA_TYPE_ALL:{
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +000012910 xmlAutomataStatePtr start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012911 xmlSchemaParticlePtr sub;
12912 xmlSchemaElementPtr elemDecl;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012913 int lax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012914
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012915 sub = (xmlSchemaParticlePtr) particle->children->children;
12916 if (sub == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012917 break;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012918 start = pctxt->state;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012919 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012920 pctxt->state = start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012921
12922 elemDecl = (xmlSchemaElementPtr) sub->children;
12923 if (elemDecl == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012924 PERROR_INT("xmlSchemaBuildAContentModel",
12925 "<element> particle has no term");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012926 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012927 };
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012928 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012929 * NOTE: The {max occurs} of all the particles in the
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +000012930 * {particles} of the group must be 0 or 1; this is
12931 * already ensured during the parse of the content of
12932 * <all>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012933 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012934 if (elemDecl->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
12935 int counter;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012936
Daniel Veillarda980bef2005-07-18 21:34:03 +000012937 /*
12938 * This is an abstract group, we need to share
12939 * the same counter for all the element transitions
12940 * derived from the group
12941 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012942 counter = xmlAutomataNewCounter(pctxt->am,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012943 sub->minOccurs, sub->maxOccurs);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012944 xmlSchemaBuildContentModelForSubstGroup(pctxt,
12945 sub, counter, pctxt->state);
Daniel Veillarda980bef2005-07-18 21:34:03 +000012946 } else {
12947 if ((sub->minOccurs == 1) &&
12948 (sub->maxOccurs == 1)) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012949 xmlAutomataNewOnceTrans2(pctxt->am, pctxt->state,
12950 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012951 elemDecl->name,
12952 elemDecl->targetNamespace,
12953 1, 1, elemDecl);
12954 } else if ((sub->minOccurs == 0) &&
12955 (sub->maxOccurs == 1)) {
12956
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012957 xmlAutomataNewCountTrans2(pctxt->am, pctxt->state,
12958 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012959 elemDecl->name,
12960 elemDecl->targetNamespace,
12961 0,
12962 1,
12963 elemDecl);
12964 }
12965 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012966 sub = (xmlSchemaParticlePtr) sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012967 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012968 lax = particle->minOccurs == 0;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012969 pctxt->state =
12970 xmlAutomataNewAllTrans(pctxt->am, pctxt->state, NULL, lax);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012971 break;
12972 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012973 case XML_SCHEMA_TYPE_GROUP:
12974 /*
12975 * If we hit a model group definition, then this means that
12976 * it was empty, thus was not substituted for the containing
12977 * model group. Just do nothing in this case.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012978 * TODO: But the group should be substituted and not occur at
12979 * all in the content model at this point. Fix this.
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012980 */
12981 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012982 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012983 xmlSchemaInternalErr2(ACTXT_CAST pctxt,
12984 "xmlSchemaBuildAContentModel",
12985 "found unexpected term of type '%s' in content model",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012986 WXS_ITEM_TYPE_NAME(particle->children), NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012987 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012988 }
12989}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012990
Daniel Veillard4255d502002-04-16 15:50:10 +000012991/**
12992 * xmlSchemaBuildContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012993 * @ctxt: the schema parser context
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012994 * @type: the complex type definition
Daniel Veillarda84c0b32003-06-02 16:58:46 +000012995 * @name: the element name
Daniel Veillard4255d502002-04-16 15:50:10 +000012996 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012997 * Builds the content model of the complex type.
Daniel Veillard4255d502002-04-16 15:50:10 +000012998 */
12999static void
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013000xmlSchemaBuildContentModel(xmlSchemaTypePtr type,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013001 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013002{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013003 if ((type->type != XML_SCHEMA_TYPE_COMPLEX) ||
13004 (type->contModel != NULL) ||
13005 ((type->contentType != XML_SCHEMA_CONTENT_ELEMENTS) &&
13006 (type->contentType != XML_SCHEMA_CONTENT_MIXED)))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013007 return;
Daniel Veillarddecd64d2002-04-18 14:41:51 +000013008
13009#ifdef DEBUG_CONTENT
13010 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013011 "Building content model for %s\n", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000013012#endif
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013013 ctxt->am = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013014 ctxt->am = xmlNewAutomata();
13015 if (ctxt->am == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013016 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013017 "Cannot create automata for complex type %s\n", type->name);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013018 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000013019 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013020 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013021 /*
13022 * Build the automaton.
13023 */
13024 xmlSchemaBuildAContentModel(ctxt, WXS_TYPE_PARTICLE(type));
Daniel Veillard4255d502002-04-16 15:50:10 +000013025 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013026 type->contModel = xmlAutomataCompile(ctxt->am);
13027 if (type->contModel == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013028 xmlSchemaPCustomErr(ctxt,
13029 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013030 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013031 "Failed to compile the content model", NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013032 } else if (xmlRegexpIsDeterminist(type->contModel) != 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013033 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000013034 XML_SCHEMAP_NOT_DETERMINISTIC,
13035 /* XML_SCHEMAS_ERR_NOTDETERMINIST, */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013036 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013037 "The content model is not determinist", NULL);
Daniel Veillarde19fc232002-04-22 16:01:24 +000013038 } else {
Daniel Veillard118aed72002-09-24 14:13:13 +000013039#ifdef DEBUG_CONTENT_REGEXP
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013040 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013041 "Content model of %s:\n", type->name);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013042 xmlRegexpPrint(stderr, type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +000013043#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +000013044 }
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013045 ctxt->state = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013046 xmlFreeAutomata(ctxt->am);
13047 ctxt->am = NULL;
13048}
13049
13050/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013051 * xmlSchemaResolveElementReferences:
Daniel Veillard4255d502002-04-16 15:50:10 +000013052 * @elem: the schema element context
13053 * @ctxt: the schema parser context
13054 *
Daniel Veillardc0826a72004-08-10 14:17:33 +000013055 * Resolves the references of an element declaration
13056 * or particle, which has an element declaration as it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013057 * term.
Daniel Veillard4255d502002-04-16 15:50:10 +000013058 */
13059static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013060xmlSchemaResolveElementReferences(xmlSchemaElementPtr elemDecl,
13061 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000013062{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013063 if ((ctxt == NULL) || (elemDecl == NULL) ||
13064 ((elemDecl != NULL) &&
13065 (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_RESOLVED)))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013066 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013067 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_RESOLVED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013068
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013069 if ((elemDecl->subtypes == NULL) && (elemDecl->namedType != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013070 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013071
13072 /* (type definition) ... otherwise the type definition ·resolved·
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013073 * to by the ·actual value· of the type [attribute] ...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013074 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013075 type = xmlSchemaGetType(ctxt->schema, elemDecl->namedType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013076 elemDecl->namedTypeNs);
13077 if (type == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013078 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013079 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013080 WXS_BASIC_CAST elemDecl, elemDecl->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013081 "type", elemDecl->namedType, elemDecl->namedTypeNs,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013082 XML_SCHEMA_TYPE_BASIC, "type definition");
13083 } else
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013084 elemDecl->subtypes = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013085 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013086 if (elemDecl->substGroup != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013087 xmlSchemaElementPtr substHead;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013088
Daniel Veillardc0826a72004-08-10 14:17:33 +000013089 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013090 * FIXME TODO: Do we need a new field in _xmlSchemaElement for
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013091 * substitutionGroup?
Daniel Veillard01fa6152004-06-29 17:04:39 +000013092 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013093 substHead = xmlSchemaGetElem(ctxt->schema, elemDecl->substGroup,
13094 elemDecl->substGroupNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013095 if (substHead == NULL) {
13096 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013097 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013098 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013099 "substitutionGroup", elemDecl->substGroup,
13100 elemDecl->substGroupNs, XML_SCHEMA_TYPE_ELEMENT, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013101 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013102 xmlSchemaResolveElementReferences(substHead, ctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013103 /*
13104 * Set the "substitution group affiliation".
13105 * NOTE that now we use the "refDecl" field for this.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013106 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013107 WXS_SUBST_HEAD(elemDecl) = substHead;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013108 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013109 * The type definitions is set to:
13110 * SPEC "...the {type definition} of the element
13111 * declaration ·resolved· to by the ·actual value·
13112 * of the substitutionGroup [attribute], if present"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013113 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013114 if (elemDecl->subtypes == NULL)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013115 elemDecl->subtypes = substHead->subtypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013116 }
13117 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013118 /*
13119 * SPEC "The definition of anyType serves as the default type definition
13120 * for element declarations whose XML representation does not specify one."
13121 */
13122 if ((elemDecl->subtypes == NULL) &&
13123 (elemDecl->namedType == NULL) &&
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013124 (elemDecl->substGroup == NULL))
13125 elemDecl->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
William M. Bracke7091952004-05-11 15:09:58 +000013126}
13127
13128/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013129 * xmlSchemaResolveUnionMemberTypes:
William M. Bracke7091952004-05-11 15:09:58 +000013130 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013131 * @type: the schema simple type definition
William M. Bracke7091952004-05-11 15:09:58 +000013132 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013133 * Checks and builds the "member type definitions" property of the union
13134 * simple type. This handles part (1), part (2) is done in
13135 * xmlSchemaFinishMemberTypeDefinitionsProperty()
13136 *
Daniel Veillard01fa6152004-06-29 17:04:39 +000013137 * Returns -1 in case of an internal error, 0 otherwise.
William M. Bracke7091952004-05-11 15:09:58 +000013138 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013139static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013140xmlSchemaResolveUnionMemberTypes(xmlSchemaParserCtxtPtr ctxt,
13141 xmlSchemaTypePtr type)
Daniel Veillard377e1a92004-04-16 16:30:05 +000013142{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013143
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013144 xmlSchemaTypeLinkPtr link, lastLink, newLink;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013145 xmlSchemaTypePtr memberType;
Daniel Veillard377e1a92004-04-16 16:30:05 +000013146
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013147 /*
13148 * SPEC (1) "If the <union> alternative is chosen, then [Definition:]
13149 * define the explicit members as the type definitions ·resolved·
13150 * to by the items in the ·actual value· of the memberTypes [attribute],
13151 * if any, followed by the type definitions corresponding to the
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013152 * <simpleType>s among the [children] of <union>, if any."
Daniel Veillard01fa6152004-06-29 17:04:39 +000013153 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013154 /*
13155 * Resolve references.
13156 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013157 link = type->memberTypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013158 lastLink = NULL;
13159 while (link != NULL) {
13160 const xmlChar *name, *nsName;
Daniel Veillardc0826a72004-08-10 14:17:33 +000013161
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013162 name = ((xmlSchemaQNameRefPtr) link->type)->name;
13163 nsName = ((xmlSchemaQNameRefPtr) link->type)->targetNamespace;
13164
13165 memberType = xmlSchemaGetType(ctxt->schema, name, nsName);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013166 if ((memberType == NULL) || (! WXS_IS_SIMPLE(memberType))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013167 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013168 WXS_BASIC_CAST type, type->node, "memberTypes",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013169 name, nsName, XML_SCHEMA_TYPE_SIMPLE, NULL);
13170 /*
13171 * Remove the member type link.
13172 */
13173 if (lastLink == NULL)
13174 type->memberTypes = link->next;
13175 else
13176 lastLink->next = link->next;
13177 newLink = link;
13178 link = link->next;
13179 xmlFree(newLink);
13180 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013181 link->type = memberType;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013182 lastLink = link;
13183 link = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013184 }
13185 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013186 /*
13187 * Add local simple types,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013188 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013189 memberType = type->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013190 while (memberType != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013191 link = (xmlSchemaTypeLinkPtr) xmlMalloc(sizeof(xmlSchemaTypeLink));
13192 if (link == NULL) {
13193 xmlSchemaPErrMemory(ctxt, "allocating a type link", NULL);
13194 return (-1);
13195 }
13196 link->type = memberType;
13197 link->next = NULL;
13198 if (lastLink == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013199 type->memberTypes = link;
13200 else
Daniel Veillard01fa6152004-06-29 17:04:39 +000013201 lastLink->next = link;
13202 lastLink = link;
13203 memberType = memberType->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013204 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013205 return (0);
Daniel Veillard377e1a92004-04-16 16:30:05 +000013206}
13207
Daniel Veillard4255d502002-04-16 15:50:10 +000013208/**
Daniel Veillard3646d642004-06-02 19:19:14 +000013209 * xmlSchemaIsDerivedFromBuiltInType:
13210 * @ctxt: the schema parser context
13211 * @type: the type definition
13212 * @valType: the value type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013213 *
Daniel Veillard3646d642004-06-02 19:19:14 +000013214 *
13215 * Returns 1 if the type has the given value type, or
13216 * is derived from such a type.
13217 */
William M. Brack803812b2004-06-03 02:11:24 +000013218static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013219xmlSchemaIsDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
Daniel Veillard3646d642004-06-02 19:19:14 +000013220{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013221 if (type == NULL)
13222 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013223 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013224 return (0);
13225 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13226 if (type->builtInType == valType)
13227 return(1);
13228 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13229 (type->builtInType == XML_SCHEMAS_ANYTYPE))
13230 return (0);
13231 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13232 } else
13233 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard3646d642004-06-02 19:19:14 +000013234
13235 return (0);
13236}
13237
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013238#if 0
13239/**
13240 * xmlSchemaIsDerivedFromBuiltInType:
13241 * @ctxt: the schema parser context
13242 * @type: the type definition
13243 * @valType: the value type
13244 *
13245 *
13246 * Returns 1 if the type has the given value type, or
13247 * is derived from such a type.
13248 */
13249static int
13250xmlSchemaIsUserDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
13251{
13252 if (type == NULL)
13253 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013254 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013255 return (0);
13256 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13257 if (type->builtInType == valType)
13258 return(1);
13259 return (0);
13260 } else
13261 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13262
13263 return (0);
13264}
13265#endif
13266
13267static xmlSchemaTypePtr
13268xmlSchemaQueryBuiltInType(xmlSchemaTypePtr type)
13269{
13270 if (type == NULL)
13271 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013272 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013273 return (NULL);
13274 if (type->type == XML_SCHEMA_TYPE_BASIC)
13275 return(type);
13276 else
13277 return(xmlSchemaQueryBuiltInType(type->subtypes));
13278
13279 return (NULL);
13280}
13281
Daniel Veillard3646d642004-06-02 19:19:14 +000013282/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013283 * xmlSchemaGetPrimitiveType:
Daniel Veillard01fa6152004-06-29 17:04:39 +000013284 * @type: the simpleType definition
13285 *
13286 * Returns the primitive type of the given type or
13287 * NULL in case of error.
13288 */
13289static xmlSchemaTypePtr
13290xmlSchemaGetPrimitiveType(xmlSchemaTypePtr type)
13291{
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013292
Daniel Veillard01fa6152004-06-29 17:04:39 +000013293 while (type != NULL) {
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013294 /*
13295 * Note that anySimpleType is actually not a primitive type
13296 * but we need that here.
13297 */
13298 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13299 (type->flags & XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE))
Daniel Veillard01fa6152004-06-29 17:04:39 +000013300 return (type);
13301 type = type->baseType;
13302 }
13303
13304 return (NULL);
13305}
13306
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013307#if 0
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013308/**
13309 * xmlSchemaGetBuiltInTypeAncestor:
13310 * @type: the simpleType definition
13311 *
13312 * Returns the primitive type of the given type or
13313 * NULL in case of error.
13314 */
13315static xmlSchemaTypePtr
13316xmlSchemaGetBuiltInTypeAncestor(xmlSchemaTypePtr type)
13317{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013318 if (WXS_IS_LIST(type) || WXS_IS_UNION(type))
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000013319 return (0);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013320 while (type != NULL) {
13321 if (type->type == XML_SCHEMA_TYPE_BASIC)
13322 return (type);
13323 type = type->baseType;
13324 }
13325
13326 return (NULL);
13327}
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013328#endif
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013329
Daniel Veillard01fa6152004-06-29 17:04:39 +000013330/**
Daniel Veillard50355f02004-06-08 17:52:16 +000013331 * xmlSchemaCloneWildcardNsConstraints:
13332 * @ctxt: the schema parser context
13333 * @dest: the destination wildcard
13334 * @source: the source wildcard
13335 *
13336 * Clones the namespace constraints of source
13337 * and assignes them to dest.
13338 * Returns -1 on internal error, 0 otherwise.
13339 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013340static int
13341xmlSchemaCloneWildcardNsConstraints(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013342 xmlSchemaWildcardPtr dest,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013343 xmlSchemaWildcardPtr source)
Daniel Veillard3646d642004-06-02 19:19:14 +000013344{
13345 xmlSchemaWildcardNsPtr cur, tmp, last;
13346
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013347 if ((source == NULL) || (dest == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013348 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013349 dest->any = source->any;
Daniel Veillard3646d642004-06-02 19:19:14 +000013350 cur = source->nsSet;
13351 last = NULL;
13352 while (cur != NULL) {
13353 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
13354 if (tmp == NULL)
13355 return(-1);
13356 tmp->value = cur->value;
13357 if (last == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013358 dest->nsSet = tmp;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013359 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013360 last->next = tmp;
13361 last = tmp;
13362 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013363 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013364 if (dest->negNsSet != NULL)
13365 xmlSchemaFreeWildcardNsSet(dest->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +000013366 if (source->negNsSet != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013367 dest->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13368 if (dest->negNsSet == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013369 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013370 dest->negNsSet->value = source->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013371 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013372 dest->negNsSet = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000013373 return(0);
13374}
13375
Daniel Veillard50355f02004-06-08 17:52:16 +000013376/**
13377 * xmlSchemaUnionWildcards:
13378 * @ctxt: the schema parser context
13379 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013380 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013381 *
13382 * Unions the namespace constraints of the given wildcards.
13383 * @completeWild will hold the resulting union.
13384 * Returns a positive error code on failure, -1 in case of an
13385 * internal error, 0 otherwise.
13386 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013387static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013388xmlSchemaUnionWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013389 xmlSchemaWildcardPtr completeWild,
13390 xmlSchemaWildcardPtr curWild)
13391{
13392 xmlSchemaWildcardNsPtr cur, curB, tmp;
13393
13394 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013395 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013396 * value.
13397 */
13398 if ((completeWild->any == curWild->any) &&
13399 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13400 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013401
Daniel Veillard3646d642004-06-02 19:19:14 +000013402 if ((completeWild->negNsSet == NULL) ||
13403 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013404
Daniel Veillard3646d642004-06-02 19:19:14 +000013405 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013406 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013407
13408 /*
13409 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013410 */
13411 cur = completeWild->nsSet;
13412 while (cur != NULL) {
13413 found = 0;
13414 curB = curWild->nsSet;
13415 while (curB != NULL) {
13416 if (cur->value == curB->value) {
13417 found = 1;
13418 break;
13419 }
13420 curB = curB->next;
13421 }
13422 if (!found)
13423 break;
13424 cur = cur->next;
13425 }
13426 if (found)
13427 return(0);
13428 } else
13429 return(0);
13430 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013431 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013432 /*
13433 * 2 If either O1 or O2 is any, then any must be the value
13434 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013435 if (completeWild->any != curWild->any) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013436 if (completeWild->any == 0) {
13437 completeWild->any = 1;
13438 if (completeWild->nsSet != NULL) {
13439 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13440 completeWild->nsSet = NULL;
13441 }
13442 if (completeWild->negNsSet != NULL) {
13443 xmlFree(completeWild->negNsSet);
13444 completeWild->negNsSet = NULL;
13445 }
13446 }
Daniel Veillard50355f02004-06-08 17:52:16 +000013447 return (0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013448 }
13449 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013450 * 3 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013451 * then the union of those sets must be the value.
13452 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013453 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013454 int found;
13455 xmlSchemaWildcardNsPtr start;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013456
Daniel Veillard3646d642004-06-02 19:19:14 +000013457 cur = curWild->nsSet;
13458 start = completeWild->nsSet;
13459 while (cur != NULL) {
13460 found = 0;
13461 curB = start;
13462 while (curB != NULL) {
13463 if (cur->value == curB->value) {
13464 found = 1;
13465 break;
13466 }
13467 curB = curB->next;
13468 }
13469 if (!found) {
13470 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013471 if (tmp == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013472 return (-1);
13473 tmp->value = cur->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013474 tmp->next = completeWild->nsSet;
Daniel Veillard3646d642004-06-02 19:19:14 +000013475 completeWild->nsSet = tmp;
13476 }
13477 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013478 }
13479
Daniel Veillard3646d642004-06-02 19:19:14 +000013480 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013481 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013482 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013483 * 4 If the two are negations of different values (namespace names
Daniel Veillard3646d642004-06-02 19:19:14 +000013484 * or ·absent·), then a pair of not and ·absent· must be the value.
13485 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013486 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013487 (curWild->negNsSet != NULL) &&
13488 (completeWild->negNsSet->value != curWild->negNsSet->value)) {
13489 completeWild->negNsSet->value = NULL;
Daniel Veillard50355f02004-06-08 17:52:16 +000013490
13491 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013492 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013493 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013494 * 5.
13495 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013496 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013497 (completeWild->negNsSet->value != NULL) &&
13498 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013499 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013500 (curWild->negNsSet->value != NULL) &&
13501 (completeWild->nsSet != NULL))) {
13502
13503 int nsFound, absentFound = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013504
Daniel Veillard3646d642004-06-02 19:19:14 +000013505 if (completeWild->nsSet != NULL) {
13506 cur = completeWild->nsSet;
13507 curB = curWild->negNsSet;
13508 } else {
13509 cur = curWild->nsSet;
13510 curB = completeWild->negNsSet;
13511 }
13512 nsFound = 0;
13513 while (cur != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013514 if (cur->value == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013515 absentFound = 1;
13516 else if (cur->value == curB->value)
13517 nsFound = 1;
13518 if (nsFound && absentFound)
13519 break;
13520 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013521 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013522
13523 if (nsFound && absentFound) {
13524 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013525 * 5.1 If the set S includes both the negated namespace
Daniel Veillard3646d642004-06-02 19:19:14 +000013526 * name and ·absent·, then any must be the value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013527 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013528 completeWild->any = 1;
13529 if (completeWild->nsSet != NULL) {
13530 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13531 completeWild->nsSet = NULL;
13532 }
13533 if (completeWild->negNsSet != NULL) {
13534 xmlFree(completeWild->negNsSet);
13535 completeWild->negNsSet = NULL;
13536 }
13537 } else if (nsFound && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013538 /*
13539 * 5.2 If the set S includes the negated namespace name
13540 * but not ·absent·, then a pair of not and ·absent· must
Daniel Veillard3646d642004-06-02 19:19:14 +000013541 * be the value.
13542 */
13543 if (completeWild->nsSet != NULL) {
13544 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13545 completeWild->nsSet = NULL;
13546 }
13547 if (completeWild->negNsSet == NULL) {
13548 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13549 if (completeWild->negNsSet == NULL)
13550 return (-1);
13551 }
13552 completeWild->negNsSet->value = NULL;
13553 } else if ((!nsFound) && absentFound) {
13554 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013555 * 5.3 If the set S includes ·absent· but not the negated
Daniel Veillard3646d642004-06-02 19:19:14 +000013556 * namespace name, then the union is not expressible.
13557 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013558 xmlSchemaPErr(ctxt, completeWild->node,
Daniel Veillard3646d642004-06-02 19:19:14 +000013559 XML_SCHEMAP_UNION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013560 "The union of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013561 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013562 return(XML_SCHEMAP_UNION_NOT_EXPRESSIBLE);
Daniel Veillard3646d642004-06-02 19:19:14 +000013563 } else if ((!nsFound) && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013564 /*
13565 * 5.4 If the set S does not include either the negated namespace
13566 * name or ·absent·, then whichever of O1 or O2 is a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013567 * and a namespace name must be the value.
13568 */
13569 if (completeWild->negNsSet == NULL) {
13570 if (completeWild->nsSet != NULL) {
13571 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13572 completeWild->nsSet = NULL;
13573 }
13574 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13575 if (completeWild->negNsSet == NULL)
13576 return (-1);
13577 completeWild->negNsSet->value = curWild->negNsSet->value;
13578 }
13579 }
13580 return (0);
13581 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013582 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013583 * 6.
13584 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013585 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013586 (completeWild->negNsSet->value == NULL) &&
13587 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013588 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013589 (curWild->negNsSet->value == NULL) &&
13590 (completeWild->nsSet != NULL))) {
13591
13592 if (completeWild->nsSet != NULL) {
13593 cur = completeWild->nsSet;
13594 } else {
13595 cur = curWild->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013596 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013597 while (cur != NULL) {
13598 if (cur->value == NULL) {
13599 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013600 * 6.1 If the set S includes ·absent·, then any must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013601 * value.
13602 */
13603 completeWild->any = 1;
13604 if (completeWild->nsSet != NULL) {
13605 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13606 completeWild->nsSet = NULL;
13607 }
13608 if (completeWild->negNsSet != NULL) {
13609 xmlFree(completeWild->negNsSet);
13610 completeWild->negNsSet = NULL;
13611 }
13612 return (0);
13613 }
13614 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013615 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013616 if (completeWild->negNsSet == NULL) {
13617 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013618 * 6.2 If the set S does not include ·absent·, then a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013619 * and ·absent· must be the value.
13620 */
13621 if (completeWild->nsSet != NULL) {
13622 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13623 completeWild->nsSet = NULL;
13624 }
13625 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13626 if (completeWild->negNsSet == NULL)
13627 return (-1);
13628 completeWild->negNsSet->value = NULL;
13629 }
13630 return (0);
13631 }
13632 return (0);
13633
13634}
13635
Daniel Veillard50355f02004-06-08 17:52:16 +000013636/**
13637 * xmlSchemaIntersectWildcards:
13638 * @ctxt: the schema parser context
13639 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013640 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013641 *
13642 * Intersects the namespace constraints of the given wildcards.
13643 * @completeWild will hold the resulting intersection.
13644 * Returns a positive error code on failure, -1 in case of an
13645 * internal error, 0 otherwise.
13646 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013647static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013648xmlSchemaIntersectWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013649 xmlSchemaWildcardPtr completeWild,
13650 xmlSchemaWildcardPtr curWild)
13651{
William M. Brack803812b2004-06-03 02:11:24 +000013652 xmlSchemaWildcardNsPtr cur, curB, prev, tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013653
13654 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013655 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013656 * value.
13657 */
13658 if ((completeWild->any == curWild->any) &&
13659 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13660 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013661
Daniel Veillard3646d642004-06-02 19:19:14 +000013662 if ((completeWild->negNsSet == NULL) ||
13663 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013664
Daniel Veillard3646d642004-06-02 19:19:14 +000013665 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013666 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013667
13668 /*
13669 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013670 */
13671 cur = completeWild->nsSet;
13672 while (cur != NULL) {
13673 found = 0;
13674 curB = curWild->nsSet;
13675 while (curB != NULL) {
13676 if (cur->value == curB->value) {
13677 found = 1;
13678 break;
13679 }
13680 curB = curB->next;
13681 }
13682 if (!found)
13683 break;
13684 cur = cur->next;
13685 }
13686 if (found)
13687 return(0);
13688 } else
13689 return(0);
13690 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013691 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013692 /*
13693 * 2 If either O1 or O2 is any, then the other must be the value.
13694 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013695 if ((completeWild->any != curWild->any) && (completeWild->any)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013696 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013697 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013698 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013699 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013700 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013701 * 3 If either O1 or O2 is a pair of not and a value (a namespace
13702 * name or ·absent·) and the other is a set of (namespace names or
13703 * ·absent·), then that set, minus the negated value if it was in
Daniel Veillard3646d642004-06-02 19:19:14 +000013704 * the set, minus ·absent· if it was in the set, must be the value.
13705 */
13706 if (((completeWild->negNsSet != NULL) && (curWild->nsSet != NULL)) ||
13707 ((curWild->negNsSet != NULL) && (completeWild->nsSet != NULL))) {
13708 const xmlChar *neg;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013709
Daniel Veillard3646d642004-06-02 19:19:14 +000013710 if (completeWild->nsSet == NULL) {
13711 neg = completeWild->negNsSet->value;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013712 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Daniel Veillard3646d642004-06-02 19:19:14 +000013713 return(-1);
13714 } else
13715 neg = curWild->negNsSet->value;
13716 /*
13717 * Remove absent and negated.
13718 */
13719 prev = NULL;
13720 cur = completeWild->nsSet;
13721 while (cur != NULL) {
13722 if (cur->value == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013723 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013724 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013725 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013726 prev->next = cur->next;
13727 xmlFree(cur);
13728 break;
13729 }
13730 prev = cur;
13731 cur = cur->next;
13732 }
13733 if (neg != NULL) {
13734 prev = NULL;
13735 cur = completeWild->nsSet;
13736 while (cur != NULL) {
13737 if (cur->value == neg) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013738 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013739 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013740 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013741 prev->next = cur->next;
13742 xmlFree(cur);
13743 break;
13744 }
13745 prev = cur;
13746 cur = cur->next;
13747 }
13748 }
13749
13750 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013751 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013752 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013753 * 4 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013754 * then the intersection of those sets must be the value.
13755 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013756 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013757 int found;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013758
Daniel Veillard3646d642004-06-02 19:19:14 +000013759 cur = completeWild->nsSet;
13760 prev = NULL;
13761 while (cur != NULL) {
13762 found = 0;
13763 curB = curWild->nsSet;
13764 while (curB != NULL) {
13765 if (cur->value == curB->value) {
13766 found = 1;
13767 break;
13768 }
13769 curB = curB->next;
13770 }
13771 if (!found) {
13772 if (prev == NULL)
13773 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013774 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013775 prev->next = cur->next;
13776 tmp = cur->next;
13777 xmlFree(cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013778 cur = tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013779 continue;
13780 }
13781 prev = cur;
13782 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013783 }
13784
Daniel Veillard3646d642004-06-02 19:19:14 +000013785 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013786 }
13787 /* 5 If the two are negations of different namespace names,
Daniel Veillard3646d642004-06-02 19:19:14 +000013788 * then the intersection is not expressible
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013789 */
13790 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013791 (curWild->negNsSet != NULL) &&
13792 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013793 (completeWild->negNsSet->value != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013794 (curWild->negNsSet->value != NULL)) {
13795
13796 xmlSchemaPErr(ctxt, completeWild->node, XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013797 "The intersection of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013798 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013799 return(XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013800 }
13801 /*
13802 * 6 If the one is a negation of a namespace name and the other
13803 * is a negation of ·absent·, then the one which is the negation
Daniel Veillard3646d642004-06-02 19:19:14 +000013804 * of a namespace name must be the value.
13805 */
13806 if ((completeWild->negNsSet != NULL) && (curWild->negNsSet != NULL) &&
13807 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013808 (completeWild->negNsSet->value == NULL)) {
13809 completeWild->negNsSet->value = curWild->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013810 }
13811 return(0);
13812}
13813
Daniel Veillard50355f02004-06-08 17:52:16 +000013814/**
13815 * xmlSchemaIsWildcardNsConstraintSubset:
13816 * @ctxt: the schema parser context
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013817 * @sub: the first wildcard
13818 * @super: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013819 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013820 * Schema Component Constraint: Wildcard Subset (cos-ns-subset)
13821 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013822 * Returns 0 if the namespace constraint of @sub is an intensional
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013823 * subset of @super, 1 otherwise.
Daniel Veillard50355f02004-06-08 17:52:16 +000013824 */
13825static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013826xmlSchemaCheckCOSNSSubset(xmlSchemaWildcardPtr sub,
13827 xmlSchemaWildcardPtr super)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013828{
Daniel Veillard50355f02004-06-08 17:52:16 +000013829 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013830 * 1 super must be any.
Daniel Veillard50355f02004-06-08 17:52:16 +000013831 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013832 if (super->any)
13833 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013834 /*
13835 * 2.1 sub must be a pair of not and a namespace name or ·absent·.
13836 * 2.2 super must be a pair of not and the same value.
13837 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013838 if ((sub->negNsSet != NULL) &&
13839 (super->negNsSet != NULL) &&
13840 (sub->negNsSet->value == sub->negNsSet->value))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013841 return (0);
13842 /*
13843 * 3.1 sub must be a set whose members are either namespace names or ·absent·.
Daniel Veillard50355f02004-06-08 17:52:16 +000013844 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013845 if (sub->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013846 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013847 * 3.2.1 super must be the same set or a superset thereof.
Daniel Veillard50355f02004-06-08 17:52:16 +000013848 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013849 if (super->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013850 xmlSchemaWildcardNsPtr cur, curB;
13851 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013852
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013853 cur = sub->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013854 while (cur != NULL) {
13855 found = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013856 curB = super->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013857 while (curB != NULL) {
13858 if (cur->value == curB->value) {
13859 found = 1;
13860 break;
13861 }
13862 curB = curB->next;
13863 }
13864 if (!found)
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013865 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013866 cur = cur->next;
13867 }
13868 if (found)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013869 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013870 } else if (super->negNsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013871 xmlSchemaWildcardNsPtr cur;
13872 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013873 * 3.2.2 super must be a pair of not and a namespace name or
13874 * ·absent· and that value must not be in sub's set.
Daniel Veillard50355f02004-06-08 17:52:16 +000013875 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013876 cur = sub->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013877 while (cur != NULL) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013878 if (cur->value == super->negNsSet->value)
13879 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013880 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013881 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013882 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013883 }
13884 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013885 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013886}
13887
Daniel Veillard50355f02004-06-08 17:52:16 +000013888static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013889xmlSchemaGetEffectiveValueConstraint(xmlSchemaAttributeUsePtr attruse,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013890 int *fixed,
13891 const xmlChar **value,
13892 xmlSchemaValPtr *val)
13893{
13894 *fixed = 0;
13895 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013896 if (val != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013897 *val = NULL;
13898
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013899 if (attruse->defValue != NULL) {
13900 *value = attruse->defValue;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013901 if (val != NULL)
13902 *val = attruse->defVal;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013903 if (attruse->flags & XML_SCHEMA_ATTR_USE_FIXED)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013904 *fixed = 1;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013905 return(1);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013906 } else if ((attruse->attrDecl != NULL) &&
13907 (attruse->attrDecl->defValue != NULL)) {
13908 *value = attruse->attrDecl->defValue;
13909 if (val != NULL)
13910 *val = attruse->attrDecl->defVal;
13911 if (attruse->attrDecl->flags & XML_SCHEMAS_ATTR_FIXED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013912 *fixed = 1;
13913 return(1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013914 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013915 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013916}
Daniel Veillard3646d642004-06-02 19:19:14 +000013917/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013918 * xmlSchemaCheckCVCWildcardNamespace:
Daniel Veillard3646d642004-06-02 19:19:14 +000013919 * @wild: the wildcard
13920 * @ns: the namespace
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013921 *
13922 * Validation Rule: Wildcard allows Namespace Name
13923 * (cvc-wildcard-namespace)
13924 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013925 * Returns 0 if the given namespace matches the wildcard,
13926 * 1 otherwise and -1 on API errors.
Daniel Veillard3646d642004-06-02 19:19:14 +000013927 */
13928static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013929xmlSchemaCheckCVCWildcardNamespace(xmlSchemaWildcardPtr wild,
13930 const xmlChar* ns)
Daniel Veillard3646d642004-06-02 19:19:14 +000013931{
13932 if (wild == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013933 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013934
13935 if (wild->any)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013936 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013937 else if (wild->nsSet != NULL) {
13938 xmlSchemaWildcardNsPtr cur;
13939
13940 cur = wild->nsSet;
13941 while (cur != NULL) {
13942 if (xmlStrEqual(cur->value, ns))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013943 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013944 cur = cur->next;
13945 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013946 } else if ((wild->negNsSet != NULL) && (ns != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013947 (!xmlStrEqual(wild->negNsSet->value, ns)))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013948 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013949
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013950 return(1);
13951}
13952
13953#define XML_SCHEMA_ACTION_DERIVE 0
13954#define XML_SCHEMA_ACTION_REDEFINE 1
13955
13956#define WXS_ACTION_STR(a) \
13957((a) == XML_SCHEMA_ACTION_DERIVE) ? (const xmlChar *) "base" : (const xmlChar *) "redefined"
13958
13959/*
13960* Schema Component Constraint:
13961* Derivation Valid (Restriction, Complex)
13962* derivation-ok-restriction (2) - (4)
13963*
13964* ATTENTION:
13965* In XML Schema 1.1 this will be:
13966* Validation Rule:
13967* Checking complex type subsumption (practicalSubsumption) (1, 2 and 3)
13968*
13969*/
13970static int
13971xmlSchemaCheckDerivationOKRestriction2to4(xmlSchemaParserCtxtPtr pctxt,
13972 int action,
13973 xmlSchemaBasicItemPtr item,
13974 xmlSchemaBasicItemPtr baseItem,
13975 xmlSchemaItemListPtr uses,
13976 xmlSchemaItemListPtr baseUses,
13977 xmlSchemaWildcardPtr wild,
13978 xmlSchemaWildcardPtr baseWild)
13979{
13980 xmlSchemaAttributeUsePtr cur = NULL, bcur;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013981 int i, j, found; /* err = 0; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013982 const xmlChar *bEffValue;
13983 int effFixed;
13984
13985 if (uses != NULL) {
13986 for (i = 0; i < uses->nbItems; i++) {
13987 cur = uses->items[i];
13988 found = 0;
13989 if (baseUses == NULL)
13990 goto not_found;
13991 for (j = 0; j < baseUses->nbItems; j++) {
13992 bcur = baseUses->items[j];
13993 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
13994 WXS_ATTRUSE_DECL_NAME(bcur)) &&
13995 (WXS_ATTRUSE_DECL_TNS(cur) ==
13996 WXS_ATTRUSE_DECL_TNS(bcur)))
13997 {
13998 /*
13999 * (2.1) "If there is an attribute use in the {attribute
14000 * uses} of the {base type definition} (call this B) whose
14001 * {attribute declaration} has the same {name} and {target
14002 * namespace}, then all of the following must be true:"
14003 */
14004 found = 1;
14005
14006 if ((cur->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
14007 (bcur->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED))
14008 {
14009 xmlChar *str = NULL;
14010 /*
14011 * (2.1.1) "one of the following must be true:"
14012 * (2.1.1.1) "B's {required} is false."
14013 * (2.1.1.2) "R's {required} is true."
14014 */
14015 xmlSchemaPAttrUseErr4(pctxt,
14016 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_1,
14017 WXS_ITEM_NODE(item), item, cur,
14018 "The 'optional' attribute use is inconsistent "
14019 "with the corresponding 'required' attribute use of "
14020 "the %s %s",
14021 WXS_ACTION_STR(action),
14022 xmlSchemaGetComponentDesignation(&str, baseItem),
14023 NULL, NULL);
14024 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014025 /* err = pctxt->err; */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014026 } else if (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014027 WXS_ATTRUSE_TYPEDEF(cur),
14028 WXS_ATTRUSE_TYPEDEF(bcur), 0) != 0)
14029 {
14030 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
14031
14032 /*
14033 * SPEC (2.1.2) "R's {attribute declaration}'s
14034 * {type definition} must be validly derived from
14035 * B's {type definition} given the empty set as
14036 * defined in Type Derivation OK (Simple) (§3.14.6)."
14037 */
14038 xmlSchemaPAttrUseErr4(pctxt,
14039 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_2,
14040 WXS_ITEM_NODE(item), item, cur,
14041 "The attribute declaration's %s "
14042 "is not validly derived from "
14043 "the corresponding %s of the "
14044 "attribute declaration in the %s %s",
14045 xmlSchemaGetComponentDesignation(&strA,
14046 WXS_ATTRUSE_TYPEDEF(cur)),
14047 xmlSchemaGetComponentDesignation(&strB,
14048 WXS_ATTRUSE_TYPEDEF(bcur)),
14049 WXS_ACTION_STR(action),
14050 xmlSchemaGetComponentDesignation(&strC, baseItem));
14051 /* xmlSchemaGetComponentDesignation(&str, baseItem), */
14052 FREE_AND_NULL(strA);
14053 FREE_AND_NULL(strB);
14054 FREE_AND_NULL(strC);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014055 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014056 } else {
14057 /*
14058 * 2.1.3 [Definition:] Let the effective value
14059 * constraint of an attribute use be its {value
14060 * constraint}, if present, otherwise its {attribute
14061 * declaration}'s {value constraint} .
14062 */
14063 xmlSchemaGetEffectiveValueConstraint(bcur,
14064 &effFixed, &bEffValue, NULL);
14065 /*
14066 * 2.1.3 ... one of the following must be true
14067 *
14068 * 2.1.3.1 B's ·effective value constraint· is
14069 * ·absent· or default.
14070 */
14071 if ((bEffValue != NULL) &&
14072 (effFixed == 1)) {
14073 const xmlChar *rEffValue = NULL;
14074
14075 xmlSchemaGetEffectiveValueConstraint(bcur,
14076 &effFixed, &rEffValue, NULL);
14077 /*
14078 * 2.1.3.2 R's ·effective value constraint· is
14079 * fixed with the same string as B's.
14080 * MAYBE TODO: Compare the computed values.
14081 * Hmm, it says "same string" so
14082 * string-equality might really be sufficient.
14083 */
14084 if ((effFixed == 0) ||
14085 (! WXS_ARE_DEFAULT_STR_EQUAL(rEffValue, bEffValue)))
14086 {
14087 xmlChar *str = NULL;
14088
14089 xmlSchemaPAttrUseErr4(pctxt,
14090 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_3,
14091 WXS_ITEM_NODE(item), item, cur,
14092 "The effective value constraint of the "
14093 "attribute use is inconsistent with "
14094 "its correspondent in the %s %s",
14095 WXS_ACTION_STR(action),
14096 xmlSchemaGetComponentDesignation(&str,
14097 baseItem),
14098 NULL, NULL);
14099 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014100 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014101 }
14102 }
14103 }
14104 break;
14105 }
14106 }
14107not_found:
14108 if (!found) {
14109 /*
14110 * (2.2) "otherwise the {base type definition} must have an
14111 * {attribute wildcard} and the {target namespace} of the
14112 * R's {attribute declaration} must be ·valid· with respect
14113 * to that wildcard, as defined in Wildcard allows Namespace
14114 * Name (§3.10.4)."
14115 */
14116 if ((baseWild == NULL) ||
14117 (xmlSchemaCheckCVCWildcardNamespace(baseWild,
14118 (WXS_ATTRUSE_DECL(cur))->targetNamespace) != 0))
14119 {
14120 xmlChar *str = NULL;
14121
14122 xmlSchemaPAttrUseErr4(pctxt,
14123 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_2,
14124 WXS_ITEM_NODE(item), item, cur,
14125 "Neither a matching attribute use, "
14126 "nor a matching wildcard exists in the %s %s",
14127 WXS_ACTION_STR(action),
14128 xmlSchemaGetComponentDesignation(&str, baseItem),
14129 NULL, NULL);
14130 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014131 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014132 }
14133 }
14134 }
14135 }
14136 /*
14137 * SPEC derivation-ok-restriction (3):
14138 * (3) "For each attribute use in the {attribute uses} of the {base type
14139 * definition} whose {required} is true, there must be an attribute
14140 * use with an {attribute declaration} with the same {name} and
14141 * {target namespace} as its {attribute declaration} in the {attribute
14142 * uses} of the complex type definition itself whose {required} is true.
14143 */
14144 if (baseUses != NULL) {
14145 for (j = 0; j < baseUses->nbItems; j++) {
14146 bcur = baseUses->items[j];
14147 if (bcur->occurs != XML_SCHEMAS_ATTR_USE_REQUIRED)
14148 continue;
14149 found = 0;
14150 if (uses != NULL) {
14151 for (i = 0; i < uses->nbItems; i++) {
14152 cur = uses->items[i];
14153 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14154 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14155 (WXS_ATTRUSE_DECL_TNS(cur) ==
14156 WXS_ATTRUSE_DECL_TNS(bcur))) {
14157 found = 1;
14158 break;
14159 }
14160 }
14161 }
14162 if (!found) {
14163 xmlChar *strA = NULL, *strB = NULL;
14164
14165 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14166 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_3,
14167 NULL, item,
14168 "A matching attribute use for the "
14169 "'required' %s of the %s %s is missing",
14170 xmlSchemaGetComponentDesignation(&strA, bcur),
14171 WXS_ACTION_STR(action),
14172 xmlSchemaGetComponentDesignation(&strB, baseItem),
14173 NULL);
14174 FREE_AND_NULL(strA);
14175 FREE_AND_NULL(strB);
14176 }
14177 }
14178 }
14179 /*
14180 * derivation-ok-restriction (4)
14181 */
14182 if (wild != NULL) {
14183 /*
14184 * (4) "If there is an {attribute wildcard}, all of the
14185 * following must be true:"
14186 */
14187 if (baseWild == NULL) {
14188 xmlChar *str = NULL;
14189
14190 /*
14191 * (4.1) "The {base type definition} must also have one."
14192 */
14193 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14194 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_1,
14195 NULL, item,
14196 "The %s has an attribute wildcard, "
14197 "but the %s %s '%s' does not have one",
14198 WXS_ITEM_TYPE_NAME(item),
14199 WXS_ACTION_STR(action),
14200 WXS_ITEM_TYPE_NAME(baseItem),
14201 xmlSchemaGetComponentQName(&str, baseItem));
14202 FREE_AND_NULL(str);
14203 return(pctxt->err);
14204 } else if ((baseWild->any == 0) &&
14205 xmlSchemaCheckCOSNSSubset(wild, baseWild))
14206 {
14207 xmlChar *str = NULL;
14208 /*
14209 * (4.2) "The complex type definition's {attribute wildcard}'s
14210 * {namespace constraint} must be a subset of the {base type
14211 * definition}'s {attribute wildcard}'s {namespace constraint},
14212 * as defined by Wildcard Subset (§3.10.6)."
14213 */
14214 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14215 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_2,
14216 NULL, item,
14217 "The attribute wildcard is not a valid "
14218 "subset of the wildcard in the %s %s '%s'",
14219 WXS_ACTION_STR(action),
14220 WXS_ITEM_TYPE_NAME(baseItem),
14221 xmlSchemaGetComponentQName(&str, baseItem),
14222 NULL);
14223 FREE_AND_NULL(str);
14224 return(pctxt->err);
14225 }
14226 /* 4.3 Unless the {base type definition} is the ·ur-type
14227 * definition·, the complex type definition's {attribute
14228 * wildcard}'s {process contents} must be identical to or
14229 * stronger than the {base type definition}'s {attribute
14230 * wildcard}'s {process contents}, where strict is stronger
14231 * than lax is stronger than skip.
14232 */
14233 if ((! WXS_IS_ANYTYPE(baseItem)) &&
14234 (wild->processContents < baseWild->processContents)) {
14235 xmlChar *str = NULL;
14236 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14237 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_3,
14238 NULL, baseItem,
14239 "The {process contents} of the attribute wildcard is "
14240 "weaker than the one in the %s %s '%s'",
14241 WXS_ACTION_STR(action),
14242 WXS_ITEM_TYPE_NAME(baseItem),
14243 xmlSchemaGetComponentQName(&str, baseItem),
14244 NULL);
14245 FREE_AND_NULL(str)
14246 return(pctxt->err);
14247 }
14248 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014249 return(0);
14250}
14251
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014252
14253static int
14254xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
14255 xmlSchemaBasicItemPtr item,
14256 xmlSchemaWildcardPtr *completeWild,
14257 xmlSchemaItemListPtr list,
14258 xmlSchemaItemListPtr prohibs);
Daniel Veillard3646d642004-06-02 19:19:14 +000014259/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014260 * xmlSchemaFixupTypeAttributeUses:
Daniel Veillard3646d642004-06-02 19:19:14 +000014261 * @ctxt: the schema parser context
14262 * @type: the complex type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014263 *
Daniel Veillard3646d642004-06-02 19:19:14 +000014264 *
14265 * Builds the wildcard and the attribute uses on the given complex type.
14266 * Returns -1 if an internal error occurs, 0 otherwise.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014267 *
14268 * ATTENTION TODO: Experimantally this uses pointer comparisons for
14269 * strings, so recheck this if we start to hardcode some schemata, since
14270 * they might not be in the same dict.
14271 * NOTE: It is allowed to "extend" the xs:anyType type.
Daniel Veillard3646d642004-06-02 19:19:14 +000014272 */
14273static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014274xmlSchemaFixupTypeAttributeUses(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014275 xmlSchemaTypePtr type)
Daniel Veillard3646d642004-06-02 19:19:14 +000014276{
14277 xmlSchemaTypePtr baseType = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014278 xmlSchemaAttributeUsePtr use;
14279 xmlSchemaItemListPtr uses, baseUses, prohibs = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000014280
Daniel Veillard01fa6152004-06-29 17:04:39 +000014281 if (type->baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014282 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014283 "no base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000014284 return (-1);
14285 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014286 baseType = type->baseType;
14287 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014288 if (xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014289 return(-1);
14290
14291 uses = type->attrUses;
14292 baseUses = baseType->attrUses;
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014293 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014294 * Expand attribute group references. And build the 'complete'
14295 * wildcard, i.e. intersect multiple wildcards.
14296 * Move attribute prohibitions into a separate list.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014297 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014298 if (uses != NULL) {
14299 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014300 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014301 * This one will transfer all attr. prohibitions
14302 * into pctxt->attrProhibs.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014303 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014304 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14305 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14306 pctxt->attrProhibs) == -1)
14307 {
14308 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14309 "failed to expand attributes");
14310 }
14311 if (pctxt->attrProhibs->nbItems != 0)
14312 prohibs = pctxt->attrProhibs;
14313 } else {
14314 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14315 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14316 NULL) == -1)
14317 {
14318 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14319 "failed to expand attributes");
14320 }
14321 }
14322 }
14323 /*
14324 * Inherit the attribute uses of the base type.
14325 */
14326 if (baseUses != NULL) {
14327 int i, j;
14328 xmlSchemaAttributeUseProhibPtr pro;
14329
14330 if (WXS_IS_RESTRICTION(type)) {
14331 int usesCount;
14332 xmlSchemaAttributeUsePtr tmp;
14333
14334 if (uses != NULL)
14335 usesCount = uses->nbItems;
14336 else
14337 usesCount = 0;
14338
14339 /* Restriction. */
14340 for (i = 0; i < baseUses->nbItems; i++) {
14341 use = baseUses->items[i];
14342 if (prohibs) {
14343 /*
14344 * Filter out prohibited uses.
14345 */
14346 for (j = 0; j < prohibs->nbItems; j++) {
14347 pro = prohibs->items[j];
14348 if ((WXS_ATTRUSE_DECL_NAME(use) == pro->name) &&
14349 (WXS_ATTRUSE_DECL_TNS(use) ==
14350 pro->targetNamespace))
14351 {
14352 goto inherit_next;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014353 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014354 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014355 }
14356 if (usesCount) {
14357 /*
14358 * Filter out existing uses.
14359 */
14360 for (j = 0; j < usesCount; j++) {
14361 tmp = uses->items[j];
14362 if ((WXS_ATTRUSE_DECL_NAME(use) ==
14363 WXS_ATTRUSE_DECL_NAME(tmp)) &&
14364 (WXS_ATTRUSE_DECL_TNS(use) ==
14365 WXS_ATTRUSE_DECL_TNS(tmp)))
14366 {
14367 goto inherit_next;
14368 }
14369 }
14370 }
14371 if (uses == NULL) {
14372 type->attrUses = xmlSchemaItemListCreate();
14373 if (type->attrUses == NULL)
14374 goto exit_failure;
14375 uses = type->attrUses;
14376 }
14377 xmlSchemaItemListAddSize(uses, 2, use);
14378inherit_next: {}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014379 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014380 } else {
14381 /* Extension. */
14382 for (i = 0; i < baseUses->nbItems; i++) {
14383 use = baseUses->items[i];
14384 if (uses == NULL) {
14385 type->attrUses = xmlSchemaItemListCreate();
14386 if (type->attrUses == NULL)
14387 goto exit_failure;
14388 uses = type->attrUses;
14389 }
14390 xmlSchemaItemListAddSize(uses, baseUses->nbItems, use);
14391 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014392 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014393 }
14394 /*
14395 * Shrink attr. uses.
14396 */
14397 if (uses) {
14398 if (uses->nbItems == 0) {
14399 xmlSchemaItemListFree(uses);
14400 type->attrUses = NULL;
14401 }
14402 /*
14403 * TODO: We could shrink the size of the array
14404 * to fit the actual number of items.
14405 */
14406 }
14407 /*
14408 * Compute the complete wildcard.
14409 */
14410 if (WXS_IS_EXTENSION(type)) {
14411 if (baseType->attributeWildcard != NULL) {
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014412 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014413 * (3.2.2.1) "If the ·base wildcard· is non-·absent·, then
14414 * the appropriate case among the following:"
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014415 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014416 if (type->attributeWildcard != NULL) {
14417 /*
14418 * Union the complete wildcard with the base wildcard.
14419 * SPEC {attribute wildcard}
14420 * (3.2.2.1.2) "otherwise a wildcard whose {process contents}
14421 * and {annotation} are those of the ·complete wildcard·,
14422 * and whose {namespace constraint} is the intensional union
14423 * of the {namespace constraint} of the ·complete wildcard·
14424 * and of the ·base wildcard·, as defined in Attribute
14425 * Wildcard Union (§3.10.6)."
14426 */
14427 if (xmlSchemaUnionWildcards(pctxt, type->attributeWildcard,
14428 baseType->attributeWildcard) == -1)
14429 goto exit_failure;
14430 } else {
14431 /*
14432 * (3.2.2.1.1) "If the ·complete wildcard· is ·absent·,
14433 * then the ·base wildcard·."
14434 */
14435 type->attributeWildcard = baseType->attributeWildcard;
14436 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014437 } else {
14438 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014439 * (3.2.2.2) "otherwise (the ·base wildcard· is ·absent·) the
14440 * ·complete wildcard"
14441 * NOOP
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014442 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014443 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014444 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014445 /*
14446 * SPEC {attribute wildcard}
14447 * (3.1) "If the <restriction> alternative is chosen, then the
14448 * ·complete wildcard·;"
14449 * NOOP
14450 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014451 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014452
Daniel Veillard3646d642004-06-02 19:19:14 +000014453 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014454
14455exit_failure:
14456 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000014457}
14458
14459/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014460 * xmlSchemaTypeFinalContains:
14461 * @schema: the schema
14462 * @type: the type definition
14463 * @final: the final
14464 *
14465 * Evaluates if a type definition contains the given "final".
14466 * This does take "finalDefault" into account as well.
14467 *
14468 * Returns 1 if the type does containt the given "final",
14469 * 0 otherwise.
14470 */
14471static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014472xmlSchemaTypeFinalContains(xmlSchemaTypePtr type, int final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014473{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014474 if (type == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014475 return (0);
14476 if (type->flags & final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014477 return (1);
14478 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014479 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014480}
14481
14482/**
14483 * xmlSchemaGetUnionSimpleTypeMemberTypes:
14484 * @type: the Union Simple Type
14485 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014486 * Returns a list of member types of @type if existing,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014487 * returns NULL otherwise.
14488 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014489static xmlSchemaTypeLinkPtr
Daniel Veillard01fa6152004-06-29 17:04:39 +000014490xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type)
14491{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014492 while ((type != NULL) && (type->type == XML_SCHEMA_TYPE_SIMPLE)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014493 if (type->memberTypes != NULL)
14494 return (type->memberTypes);
14495 else
14496 type = type->baseType;
14497 }
14498 return (NULL);
14499}
14500
14501/**
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014502 * xmlSchemaGetParticleTotalRangeMin:
14503 * @particle: the particle
14504 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014505 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014506 * (all and sequence) + (choice)
14507 *
14508 * Returns the minimun Effective Total Range.
14509 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014510static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014511xmlSchemaGetParticleTotalRangeMin(xmlSchemaParticlePtr particle)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014512{
14513 if ((particle->children == NULL) ||
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014514 (particle->minOccurs == 0))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014515 return (0);
14516 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014517 int min = -1, cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014518 xmlSchemaParticlePtr part =
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014519 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014520
14521 if (part == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014522 return (0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014523 while (part != NULL) {
14524 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14525 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014526 cur = part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014527 else
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014528 cur = xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014529 if (cur == 0)
14530 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014531 if ((min > cur) || (min == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014532 min = cur;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014533 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014534 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014535 return (particle->minOccurs * min);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014536 } else {
14537 /* <all> and <sequence> */
14538 int sum = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014539 xmlSchemaParticlePtr part =
14540 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014541
14542 if (part == NULL)
14543 return (0);
14544 do {
14545 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14546 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014547 sum += part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014548 else
14549 sum += xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014550 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014551 } while (part != NULL);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014552 return (particle->minOccurs * sum);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014553 }
14554}
14555
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014556/**
14557 * xmlSchemaGetParticleTotalRangeMax:
14558 * @particle: the particle
14559 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014560 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014561 * (all and sequence) + (choice)
14562 *
14563 * Returns the maximum Effective Total Range.
14564 */
14565static int
14566xmlSchemaGetParticleTotalRangeMax(xmlSchemaParticlePtr particle)
14567{
14568 if ((particle->children == NULL) ||
14569 (particle->children->children == NULL))
14570 return (0);
14571 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
14572 int max = -1, cur;
14573 xmlSchemaParticlePtr part =
14574 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014575
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014576 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14577 if (part->children == NULL)
14578 continue;
14579 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14580 (part->children->type == XML_SCHEMA_TYPE_ANY))
14581 cur = part->maxOccurs;
14582 else
14583 cur = xmlSchemaGetParticleTotalRangeMax(part);
14584 if (cur == UNBOUNDED)
14585 return (UNBOUNDED);
14586 if ((max < cur) || (max == -1))
14587 max = cur;
14588 }
14589 /* TODO: Handle overflows? */
14590 return (particle->maxOccurs * max);
14591 } else {
14592 /* <all> and <sequence> */
14593 int sum = 0, cur;
14594 xmlSchemaParticlePtr part =
14595 (xmlSchemaParticlePtr) particle->children->children;
14596
14597 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14598 if (part->children == NULL)
14599 continue;
14600 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14601 (part->children->type == XML_SCHEMA_TYPE_ANY))
14602 cur = part->maxOccurs;
14603 else
14604 cur = xmlSchemaGetParticleTotalRangeMax(part);
14605 if (cur == UNBOUNDED)
14606 return (UNBOUNDED);
14607 if ((cur > 0) && (particle->maxOccurs == UNBOUNDED))
14608 return (UNBOUNDED);
14609 sum += cur;
14610 }
14611 /* TODO: Handle overflows? */
14612 return (particle->maxOccurs * sum);
14613 }
14614}
14615
14616/**
14617 * xmlSchemaIsParticleEmptiable:
14618 * @particle: the particle
14619 *
14620 * Schema Component Constraint: Particle Emptiable
14621 * Checks whether the given particle is emptiable.
14622 *
14623 * Returns 1 if emptiable, 0 otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014624 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014625static int
14626xmlSchemaIsParticleEmptiable(xmlSchemaParticlePtr particle)
14627{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014628 /*
14629 * SPEC (1) "Its {min occurs} is 0."
14630 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014631 if ((particle == NULL) || (particle->minOccurs == 0) ||
14632 (particle->children == NULL))
14633 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014634 /*
14635 * SPEC (2) "Its {term} is a group and the minimum part of the
14636 * effective total range of that group, [...] is 0."
14637 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014638 if (WXS_IS_MODEL_GROUP(particle->children)) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014639 if (xmlSchemaGetParticleTotalRangeMin(particle) == 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014640 return (1);
14641 }
14642 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014643}
14644
14645/**
14646 * xmlSchemaCheckCOSSTDerivedOK:
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014647 * @actxt: a context
Daniel Veillard01fa6152004-06-29 17:04:39 +000014648 * @type: the derived simple type definition
14649 * @baseType: the base type definition
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014650 * @subset: the subset of ('restriction', ect.)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014651 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014652 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014653 * Type Derivation OK (Simple) (cos-st-derived-OK)
14654 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014655 * Checks wheter @type can be validly
Daniel Veillard01fa6152004-06-29 17:04:39 +000014656 * derived from @baseType.
14657 *
14658 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014659 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014660static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014661xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014662 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014663 xmlSchemaTypePtr baseType,
14664 int subset)
14665{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014666 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014667 * 1 They are the same type definition.
14668 * TODO: The identy check might have to be more complex than this.
14669 */
14670 if (type == baseType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014671 return (0);
14672 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014673 * 2.1 restriction is not in the subset, or in the {final}
14674 * of its own {base type definition};
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014675 *
14676 * NOTE that this will be used also via "xsi:type".
14677 *
14678 * TODO: Revise this, it looks strange. How can the "type"
14679 * not be fixed or *in* fixing?
Daniel Veillard01fa6152004-06-29 17:04:39 +000014680 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014681 if (WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014682 if (xmlSchemaTypeFixup(type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014683 return(-1);
14684 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014685 if (xmlSchemaTypeFixup(baseType, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014686 return(-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014687 if ((subset & SUBSET_RESTRICTION) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014688 (xmlSchemaTypeFinalContains(type->baseType,
14689 XML_SCHEMAS_TYPE_FINAL_RESTRICTION))) {
14690 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014691 }
14692 /* 2.2 */
14693 if (type->baseType == baseType) {
14694 /*
14695 * 2.2.1 D's ·base type definition· is B.
14696 */
14697 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014698 }
14699 /*
14700 * 2.2.2 D's ·base type definition· is not the ·ur-type definition·
14701 * and is validly derived from B given the subset, as defined by this
14702 * constraint.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014703 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014704 if ((! WXS_IS_ANYTYPE(type->baseType)) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014705 (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014706 baseType, subset) == 0)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014707 return (0);
14708 }
14709 /*
14710 * 2.2.3 D's {variety} is list or union and B is the ·simple ur-type
Daniel Veillard01fa6152004-06-29 17:04:39 +000014711 * definition·.
14712 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014713 if (WXS_IS_ANY_SIMPLE_TYPE(baseType) &&
14714 (WXS_IS_LIST(type) || WXS_IS_UNION(type))) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014715 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014716 }
14717 /*
14718 * 2.2.4 B's {variety} is union and D is validly derived from a type
14719 * definition in B's {member type definitions} given the subset, as
Daniel Veillard01fa6152004-06-29 17:04:39 +000014720 * defined by this constraint.
14721 *
14722 * NOTE: This seems not to involve built-in types, since there is no
14723 * built-in Union Simple Type.
14724 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014725 if (WXS_IS_UNION(baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014726 xmlSchemaTypeLinkPtr cur;
14727
14728 cur = baseType->memberTypes;
14729 while (cur != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014730 if (WXS_IS_TYPE_NOT_FIXED(cur->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014731 if (xmlSchemaTypeFixup(cur->type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014732 return(-1);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014733 if (xmlSchemaCheckCOSSTDerivedOK(actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014734 type, cur->type, subset) == 0)
14735 {
14736 /*
14737 * It just has to be validly derived from at least one
14738 * member-type.
14739 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014740 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014741 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014742 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014743 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014744 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014745 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_2);
14746}
14747
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014748/**
14749 * xmlSchemaCheckTypeDefCircularInternal:
14750 * @pctxt: the schema parser context
14751 * @ctxtType: the type definition
14752 * @ancestor: an ancestor of @ctxtType
14753 *
14754 * Checks st-props-correct (2) + ct-props-correct (3).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014755 * Circular type definitions are not allowed.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014756 *
14757 * Returns XML_SCHEMAP_ST_PROPS_CORRECT_2 if the given type is
14758 * circular, 0 otherwise.
14759 */
14760static int
14761xmlSchemaCheckTypeDefCircularInternal(xmlSchemaParserCtxtPtr pctxt,
14762 xmlSchemaTypePtr ctxtType,
14763 xmlSchemaTypePtr ancestor)
14764{
14765 int ret;
14766
14767 if ((ancestor == NULL) || (ancestor->type == XML_SCHEMA_TYPE_BASIC))
14768 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014769
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014770 if (ctxtType == ancestor) {
14771 xmlSchemaPCustomErr(pctxt,
14772 XML_SCHEMAP_ST_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014773 WXS_BASIC_CAST ctxtType, WXS_ITEM_NODE(ctxtType),
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014774 "The definition is circular", NULL);
14775 return (XML_SCHEMAP_ST_PROPS_CORRECT_2);
14776 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014777 if (ancestor->flags & XML_SCHEMAS_TYPE_MARKED) {
14778 /*
14779 * Avoid inifinite recursion on circular types not yet checked.
14780 */
14781 return (0);
14782 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014783 ancestor->flags |= XML_SCHEMAS_TYPE_MARKED;
14784 ret = xmlSchemaCheckTypeDefCircularInternal(pctxt, ctxtType,
14785 ancestor->baseType);
14786 ancestor->flags ^= XML_SCHEMAS_TYPE_MARKED;
14787 return (ret);
14788}
14789
14790/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014791 * xmlSchemaCheckTypeDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014792 * @item: the complex/simple type definition
14793 * @ctxt: the parser context
14794 * @name: the name
14795 *
14796 * Checks for circular type definitions.
14797 */
14798static void
14799xmlSchemaCheckTypeDefCircular(xmlSchemaTypePtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014800 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014801{
14802 if ((item == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014803 (item->type == XML_SCHEMA_TYPE_BASIC) ||
14804 (item->baseType == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014805 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014806 xmlSchemaCheckTypeDefCircularInternal(ctxt, item,
14807 item->baseType);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014808}
Daniel Veillard01fa6152004-06-29 17:04:39 +000014809
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014810/*
14811* Simple Type Definition Representation OK (src-simple-type) 4
14812*
14813* "4 Circular union type definition is disallowed. That is, if the
14814* <union> alternative is chosen, there must not be any entries in the
14815* memberTypes [attribute] at any depth which resolve to the component
14816* corresponding to the <simpleType>."
14817*
14818* Note that this should work on the *representation* of a component,
14819* thus assumes any union types in the member types not being yet
14820* substituted. At this stage we need the variety of the types
14821* to be already computed.
14822*/
14823static int
14824xmlSchemaCheckUnionTypeDefCircularRecur(xmlSchemaParserCtxtPtr pctxt,
14825 xmlSchemaTypePtr ctxType,
14826 xmlSchemaTypeLinkPtr members)
14827{
14828 xmlSchemaTypeLinkPtr member;
14829 xmlSchemaTypePtr memberType;
14830
14831 member = members;
14832 while (member != NULL) {
14833 memberType = member->type;
14834 while ((memberType != NULL) &&
14835 (memberType->type != XML_SCHEMA_TYPE_BASIC)) {
14836 if (memberType == ctxType) {
14837 xmlSchemaPCustomErr(pctxt,
14838 XML_SCHEMAP_SRC_SIMPLE_TYPE_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014839 WXS_BASIC_CAST ctxType, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014840 "The union type definition is circular", NULL);
14841 return (XML_SCHEMAP_SRC_SIMPLE_TYPE_4);
14842 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014843 if ((WXS_IS_UNION(memberType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014844 ((memberType->flags & XML_SCHEMAS_TYPE_MARKED) == 0))
14845 {
14846 int res;
14847 memberType->flags |= XML_SCHEMAS_TYPE_MARKED;
14848 res = xmlSchemaCheckUnionTypeDefCircularRecur(pctxt,
14849 ctxType,
14850 xmlSchemaGetUnionSimpleTypeMemberTypes(memberType));
14851 memberType->flags ^= XML_SCHEMAS_TYPE_MARKED;
14852 if (res != 0)
14853 return(res);
14854 }
14855 memberType = memberType->baseType;
14856 }
14857 member = member->next;
14858 }
14859 return(0);
14860}
14861
14862static int
14863xmlSchemaCheckUnionTypeDefCircular(xmlSchemaParserCtxtPtr pctxt,
14864 xmlSchemaTypePtr type)
14865{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014866 if (! WXS_IS_UNION(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014867 return(0);
14868 return(xmlSchemaCheckUnionTypeDefCircularRecur(pctxt, type,
14869 type->memberTypes));
14870}
14871
Daniel Veillard01fa6152004-06-29 17:04:39 +000014872/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014873 * xmlSchemaResolveTypeReferences:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014874 * @item: the complex/simple type definition
14875 * @ctxt: the parser context
14876 * @name: the name
14877 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014878 * Resolvese type definition references
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014879 */
14880static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014881xmlSchemaResolveTypeReferences(xmlSchemaTypePtr typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014882 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014883{
14884 if (typeDef == NULL)
14885 return;
14886
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014887 /*
14888 * Resolve the base type.
14889 */
14890 if (typeDef->baseType == NULL) {
14891 typeDef->baseType = xmlSchemaGetType(ctxt->schema,
14892 typeDef->base, typeDef->baseNs);
14893 if (typeDef->baseType == NULL) {
14894 xmlSchemaPResCompAttrErr(ctxt,
14895 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014896 WXS_BASIC_CAST typeDef, typeDef->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014897 "base", typeDef->base, typeDef->baseNs,
14898 XML_SCHEMA_TYPE_SIMPLE, NULL);
14899 return;
14900 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014901 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014902 if (WXS_IS_SIMPLE(typeDef)) {
14903 if (WXS_IS_UNION(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014904 /*
14905 * Resolve the memberTypes.
14906 */
14907 xmlSchemaResolveUnionMemberTypes(ctxt, typeDef);
14908 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014909 } else if (WXS_IS_LIST(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014910 /*
14911 * Resolve the itemType.
14912 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014913 if ((typeDef->subtypes == NULL) && (typeDef->base != NULL)) {
14914
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014915 typeDef->subtypes = xmlSchemaGetType(ctxt->schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014916 typeDef->base, typeDef->baseNs);
14917
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014918 if ((typeDef->subtypes == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014919 (! WXS_IS_SIMPLE(typeDef->subtypes)))
14920 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014921 typeDef->subtypes = NULL;
14922 xmlSchemaPResCompAttrErr(ctxt,
14923 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014924 WXS_BASIC_CAST typeDef, typeDef->node,
14925 "itemType", typeDef->base, typeDef->baseNs,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014926 XML_SCHEMA_TYPE_SIMPLE, NULL);
14927 }
14928 }
14929 return;
14930 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014931 }
14932 /*
14933 * The ball of letters below means, that if we have a particle
14934 * which has a QName-helper component as its {term}, we want
14935 * to resolve it...
14936 */
14937 else if ((WXS_TYPE_CONTENTTYPE(typeDef) != NULL) &&
14938 ((WXS_TYPE_CONTENTTYPE(typeDef))->type ==
14939 XML_SCHEMA_TYPE_PARTICLE) &&
14940 (WXS_TYPE_PARTICLE_TERM(typeDef) != NULL) &&
14941 ((WXS_TYPE_PARTICLE_TERM(typeDef))->type ==
14942 XML_SCHEMA_EXTRA_QNAMEREF))
14943 {
14944 xmlSchemaQNameRefPtr ref =
14945 WXS_QNAME_CAST WXS_TYPE_PARTICLE_TERM(typeDef);
14946 xmlSchemaModelGroupDefPtr groupDef;
14947
14948 /*
14949 * URGENT TODO: Test this.
14950 */
14951 WXS_TYPE_PARTICLE_TERM(typeDef) = NULL;
14952 /*
14953 * Resolve the MG definition reference.
14954 */
14955 groupDef =
14956 WXS_MODEL_GROUPDEF_CAST xmlSchemaGetNamedComponent(ctxt->schema,
14957 ref->itemType, ref->name, ref->targetNamespace);
14958 if (groupDef == NULL) {
14959 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
14960 NULL, WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)),
14961 "ref", ref->name, ref->targetNamespace, ref->itemType,
14962 NULL);
14963 /* Remove the particle. */
14964 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14965 } else if (WXS_MODELGROUPDEF_MODEL(groupDef) == NULL)
14966 /* Remove the particle. */
14967 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14968 else {
14969 /*
14970 * Assign the MG definition's {model group} to the
14971 * particle's {term}.
14972 */
14973 WXS_TYPE_PARTICLE_TERM(typeDef) = WXS_MODELGROUPDEF_MODEL(groupDef);
14974
14975 if (WXS_MODELGROUPDEF_MODEL(groupDef)->type == XML_SCHEMA_TYPE_ALL) {
14976 /*
14977 * SPEC cos-all-limited (1.2)
14978 * "1.2 the {term} property of a particle with
14979 * {max occurs}=1 which is part of a pair which constitutes
14980 * the {content type} of a complex type definition."
14981 */
14982 if ((WXS_TYPE_PARTICLE(typeDef))->maxOccurs != 1) {
14983 xmlSchemaCustomErr(ACTXT_CAST ctxt,
14984 /* TODO: error code */
14985 XML_SCHEMAP_COS_ALL_LIMITED,
14986 WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)), NULL,
14987 "The particle's {max occurs} must be 1, since the "
14988 "reference resolves to an 'all' model group",
14989 NULL, NULL);
14990 }
14991 }
14992 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014993 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014994}
14995
14996
14997
14998/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014999 * xmlSchemaCheckSTPropsCorrect:
15000 * @ctxt: the schema parser context
15001 * @type: the simple type definition
15002 *
15003 * Checks st-props-correct.
15004 *
15005 * Returns 0 if the properties are correct,
15006 * if not, a positive error code and -1 on internal
15007 * errors.
15008 */
15009static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015010xmlSchemaCheckSTPropsCorrect(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015011 xmlSchemaTypePtr type)
15012{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015013 xmlSchemaTypePtr baseType = type->baseType;
Daniel Veillardc0826a72004-08-10 14:17:33 +000015014 xmlChar *str = NULL;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015015
Daniel Veillardc0826a72004-08-10 14:17:33 +000015016 /* STATE: error funcs converted. */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015017 /*
15018 * Schema Component Constraint: Simple Type Definition Properties Correct
15019 *
15020 * NOTE: This is somehow redundant, since we actually built a simple type
15021 * to have all the needed information; this acts as an self test.
15022 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015023 /* Base type: If the datatype has been ·derived· by ·restriction·
15024 * then the Simple Type Definition component from which it is ·derived·,
15025 * otherwise the Simple Type Definition for anySimpleType (§4.1.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000015026 */
15027 if (baseType == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015028 /*
15029 * TODO: Think about: "modulo the impact of Missing
15030 * Sub-components (§5.3)."
15031 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015032 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015033 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015034 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015035 "No base type existent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015036 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015037
Daniel Veillard01fa6152004-06-29 17:04:39 +000015038 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015039 if (! WXS_IS_SIMPLE(baseType)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015040 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015041 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015042 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015043 "The base type '%s' is not a simple type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015044 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015045 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015046 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15047 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015048 if ( (WXS_IS_LIST(type) || WXS_IS_UNION(type)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015049 (WXS_IS_RESTRICTION(type) == 0) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015050 (! WXS_IS_ANY_SIMPLE_TYPE(baseType))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015051 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015052 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015053 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015054 "A type, derived by list or union, must have"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015055 "the simple ur-type definition as base type, not '%s'",
15056 xmlSchemaGetComponentQName(&str, baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015057 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015058 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15059 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015060 /*
15061 * Variety: One of {atomic, list, union}.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015062 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015063 if ((! WXS_IS_ATOMIC(type)) && (! WXS_IS_UNION(type)) &&
15064 (! WXS_IS_LIST(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015065 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015066 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015067 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015068 "The variety is absent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015069 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15070 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000015071 /* TODO: Finish this. Hmm, is this finished? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015072
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015073 /*
15074 * 3 The {final} of the {base type definition} must not contain restriction.
15075 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015076 if (xmlSchemaTypeFinalContains(baseType,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015077 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
15078 xmlSchemaPCustomErr(ctxt,
15079 XML_SCHEMAP_ST_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015080 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015081 "The 'final' of its base type '%s' must not contain "
15082 "'restriction'",
15083 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015084 FREE_AND_NULL(str)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015085 return (XML_SCHEMAP_ST_PROPS_CORRECT_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015086 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015087
15088 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015089 * 2 All simple type definitions must be derived ultimately from the ·simple
15090 * ur-type definition (so· circular definitions are disallowed). That is, it
15091 * must be possible to reach a built-in primitive datatype or the ·simple
15092 * ur-type definition· by repeatedly following the {base type definition}.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015093 *
15094 * NOTE: this is done in xmlSchemaCheckTypeDefCircular().
Daniel Veillard01fa6152004-06-29 17:04:39 +000015095 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015096 return (0);
15097}
15098
15099/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015100 * xmlSchemaCheckCOSSTRestricts:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015101 * @ctxt: the schema parser context
15102 * @type: the simple type definition
15103 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015104 * Schema Component Constraint:
15105 * Derivation Valid (Restriction, Simple) (cos-st-restricts)
15106
15107 * Checks if the given @type (simpleType) is derived validly by restriction.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015108 * STATUS:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015109 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015110 * Returns -1 on internal errors, 0 if the type is validly derived,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015111 * a positive error code otherwise.
15112 */
15113static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015114xmlSchemaCheckCOSSTRestricts(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015115 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015116{
Daniel Veillardc0826a72004-08-10 14:17:33 +000015117 xmlChar *str = NULL;
15118
Daniel Veillard01fa6152004-06-29 17:04:39 +000015119 if (type->type != XML_SCHEMA_TYPE_SIMPLE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015120 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15121 "given type is not a user-derived simpleType");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015122 return (-1);
15123 }
15124
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015125 if (WXS_IS_ATOMIC(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015126 xmlSchemaTypePtr primitive;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015127 /*
15128 * 1.1 The {base type definition} must be an atomic simple
Daniel Veillard01fa6152004-06-29 17:04:39 +000015129 * type definition or a built-in primitive datatype.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015130 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015131 if (! WXS_IS_ATOMIC(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015132 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015133 XML_SCHEMAP_COS_ST_RESTRICTS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015134 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015135 "The base type '%s' is not an atomic simple type",
15136 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015137 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015138 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_1);
15139 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015140 /* 1.2 The {final} of the {base type definition} must not contain
Daniel Veillard01fa6152004-06-29 17:04:39 +000015141 * restriction.
15142 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015143 /* OPTIMIZE TODO : This is already done in xmlSchemaCheckStPropsCorrect */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015144 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015145 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015146 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015147 XML_SCHEMAP_COS_ST_RESTRICTS_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015148 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015149 "The final of its base type '%s' must not contain 'restriction'",
15150 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015151 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015152 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_2);
15153 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015154
15155 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015156 * 1.3.1 DF must be an allowed constraining facet for the {primitive
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015157 * type definition}, as specified in the appropriate subsection of 3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015158 * Primitive datatypes.
15159 */
15160 if (type->facets != NULL) {
15161 xmlSchemaFacetPtr facet;
15162 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015163
Daniel Veillard01fa6152004-06-29 17:04:39 +000015164 primitive = xmlSchemaGetPrimitiveType(type);
15165 if (primitive == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015166 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15167 "failed to get primitive type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015168 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015169 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015170 facet = type->facets;
15171 do {
15172 if (xmlSchemaIsBuiltInTypeFacet(primitive, facet->type) == 0) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015173 ok = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015174 xmlSchemaPIllegalFacetAtomicErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015175 XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015176 type, primitive, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015177 }
15178 facet = facet->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015179 } while (facet != NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015180 if (ok == 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015181 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015182 }
15183 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015184 * SPEC (1.3.2) "If there is a facet of the same kind in the {facets}
15185 * of the {base type definition} (call this BF),then the DF's {value}
15186 * must be a valid restriction of BF's {value} as defined in
15187 * [XML Schemas: Datatypes]."
15188 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015189 * NOTE (1.3.2) Facet derivation constraints are currently handled in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015190 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015191 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015192 } else if (WXS_IS_LIST(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015193 xmlSchemaTypePtr itemType = NULL;
15194
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015195 itemType = type->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015196 if ((itemType == NULL) || (! WXS_IS_SIMPLE(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015197 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15198 "failed to evaluate the item type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015199 return (-1);
15200 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015201 if (WXS_IS_TYPE_NOT_FIXED(itemType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015202 xmlSchemaTypeFixup(itemType, ACTXT_CAST pctxt);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015203 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015204 * 2.1 The {item type definition} must have a {variety} of atomic or
15205 * union (in which case all the {member type definitions}
Daniel Veillard01fa6152004-06-29 17:04:39 +000015206 * must be atomic).
15207 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015208 if ((! WXS_IS_ATOMIC(itemType)) &&
15209 (! WXS_IS_UNION(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015210 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015211 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015212 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015213 "The item type '%s' does not have a variety of atomic or union",
15214 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015215 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015216 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015217 } else if (WXS_IS_UNION(itemType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015218 xmlSchemaTypeLinkPtr member;
15219
15220 member = itemType->memberTypes;
15221 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015222 if (! WXS_IS_ATOMIC(member->type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015223 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015224 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015225 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015226 "The item type is a union type, but the "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015227 "member type '%s' of this item type is not atomic",
15228 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015229 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015230 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
15231 }
15232 member = member->next;
15233 }
15234 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015235
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015236 if (WXS_IS_ANY_SIMPLE_TYPE(type->baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015237 xmlSchemaFacetPtr facet;
15238 /*
15239 * This is the case if we have: <simpleType><list ..
15240 */
15241 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015242 * 2.3.1
15243 * 2.3.1.1 The {final} of the {item type definition} must not
Daniel Veillard01fa6152004-06-29 17:04:39 +000015244 * contain list.
15245 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015246 if (xmlSchemaTypeFinalContains(itemType,
15247 XML_SCHEMAS_TYPE_FINAL_LIST)) {
15248 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015249 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015250 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015251 "The final of its item type '%s' must not contain 'list'",
15252 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015253 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015254 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1);
15255 }
15256 /*
15257 * 2.3.1.2 The {facets} must only contain the whiteSpace
15258 * facet component.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015259 * OPTIMIZE TODO: the S4S already disallows any facet
15260 * to be specified.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015261 */
15262 if (type->facets != NULL) {
15263 facet = type->facets;
15264 do {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015265 if (facet->type != XML_SCHEMA_FACET_WHITESPACE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015266 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015267 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015268 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015269 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2);
15270 }
15271 facet = facet->next;
15272 } while (facet != NULL);
15273 }
15274 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015275 * MAYBE TODO: (Hmm, not really) Datatypes states:
15276 * A ·list· datatype can be ·derived· from an ·atomic· datatype
15277 * whose ·lexical space· allows space (such as string or anyURI)or
15278 * a ·union· datatype any of whose {member type definitions}'s
Daniel Veillard01fa6152004-06-29 17:04:39 +000015279 * ·lexical space· allows space.
15280 */
15281 } else {
15282 /*
15283 * This is the case if we have: <simpleType><restriction ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015284 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015285 */
15286 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015287 * 2.3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015288 * 2.3.2.1 The {base type definition} must have a {variety} of list.
15289 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015290 if (! WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015291 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015292 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015293 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015294 "The base type '%s' must be a list type",
15295 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015296 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015297 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1);
15298 }
15299 /*
15300 * 2.3.2.2 The {final} of the {base type definition} must not
15301 * contain restriction.
15302 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015303 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015304 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015305 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015306 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015307 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015308 "The 'final' of the base type '%s' must not contain 'restriction'",
15309 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015310 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015311 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2);
15312 }
15313 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015314 * 2.3.2.3 The {item type definition} must be validly derived
Daniel Veillard01fa6152004-06-29 17:04:39 +000015315 * from the {base type definition}'s {item type definition} given
15316 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6).
15317 */
15318 {
15319 xmlSchemaTypePtr baseItemType;
15320
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015321 baseItemType = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015322 if ((baseItemType == NULL) || (! WXS_IS_SIMPLE(baseItemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015323 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15324 "failed to eval the item type of a base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015325 return (-1);
15326 }
15327 if ((itemType != baseItemType) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015328 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt, itemType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015329 baseItemType, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015330 xmlChar *strBIT = NULL, *strBT = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015331 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015332 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015333 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015334 "The item type '%s' is not validly derived from "
15335 "the item type '%s' of the base type '%s'",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015336 xmlSchemaGetComponentQName(&str, itemType),
15337 xmlSchemaGetComponentQName(&strBIT, baseItemType),
15338 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015339
15340 FREE_AND_NULL(str)
15341 FREE_AND_NULL(strBIT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015342 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015343 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3);
15344 }
15345 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015346
Daniel Veillard01fa6152004-06-29 17:04:39 +000015347 if (type->facets != NULL) {
15348 xmlSchemaFacetPtr facet;
15349 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015350 /*
15351 * 2.3.2.4 Only length, minLength, maxLength, whiteSpace, pattern
Daniel Veillard01fa6152004-06-29 17:04:39 +000015352 * and enumeration facet components are allowed among the {facets}.
15353 */
15354 facet = type->facets;
15355 do {
15356 switch (facet->type) {
15357 case XML_SCHEMA_FACET_LENGTH:
15358 case XML_SCHEMA_FACET_MINLENGTH:
15359 case XML_SCHEMA_FACET_MAXLENGTH:
15360 case XML_SCHEMA_FACET_WHITESPACE:
15361 /*
15362 * TODO: 2.5.1.2 List datatypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015363 * The value of ·whiteSpace· is fixed to the value collapse.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015364 */
15365 case XML_SCHEMA_FACET_PATTERN:
15366 case XML_SCHEMA_FACET_ENUMERATION:
15367 break;
15368 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015369 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015370 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015371 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015372 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015373 * We could return, but it's nicer to report all
Daniel Veillard01fa6152004-06-29 17:04:39 +000015374 * invalid facets.
15375 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015376 ok = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015377 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015378 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015379 facet = facet->next;
15380 } while (facet != NULL);
15381 if (ok == 0)
15382 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4);
15383 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015384 * SPEC (2.3.2.5) (same as 1.3.2)
15385 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015386 * NOTE (2.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015387 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015388 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015389 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015390 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015391 } else if (WXS_IS_UNION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015392 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015393 * 3.1 The {member type definitions} must all have {variety} of
Daniel Veillard01fa6152004-06-29 17:04:39 +000015394 * atomic or list.
15395 */
15396 xmlSchemaTypeLinkPtr member;
15397
15398 member = type->memberTypes;
15399 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015400 if (WXS_IS_TYPE_NOT_FIXED(member->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015401 xmlSchemaTypeFixup(member->type, ACTXT_CAST pctxt);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015402
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015403 if ((! WXS_IS_ATOMIC(member->type)) &&
15404 (! WXS_IS_LIST(member->type))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015405 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015406 XML_SCHEMAP_COS_ST_RESTRICTS_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015407 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015408 "The member type '%s' is neither an atomic, nor a list type",
15409 xmlSchemaGetComponentQName(&str, member->type));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015410 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015411 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_1);
15412 }
15413 member = member->next;
15414 }
15415 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015416 * 3.3.1 If the {base type definition} is the ·simple ur-type
15417 * definition·
Daniel Veillard01fa6152004-06-29 17:04:39 +000015418 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015419 if (type->baseType->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015420 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015421 * 3.3.1.1 All of the {member type definitions} must have a
Daniel Veillard01fa6152004-06-29 17:04:39 +000015422 * {final} which does not contain union.
15423 */
15424 member = type->memberTypes;
15425 while (member != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015426 if (xmlSchemaTypeFinalContains(member->type,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015427 XML_SCHEMAS_TYPE_FINAL_UNION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015428 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015429 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015430 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015431 "The 'final' of member type '%s' contains 'union'",
15432 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015433 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015434 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1);
15435 }
15436 member = member->next;
15437 }
15438 /*
15439 * 3.3.1.2 The {facets} must be empty.
15440 */
15441 if (type->facetSet != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015442 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015443 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015444 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015445 "No facets allowed", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015446 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2);
15447 }
15448 } else {
15449 /*
15450 * 3.3.2.1 The {base type definition} must have a {variety} of union.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015451 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015452 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015453 if (! WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015454 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015455 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015456 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015457 "The base type '%s' is not a union type",
15458 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015459 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015460 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1);
15461 }
15462 /*
15463 * 3.3.2.2 The {final} of the {base type definition} must not contain restriction.
15464 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015465 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015466 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015467 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015468 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015469 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015470 "The 'final' of its base type '%s' must not contain 'restriction'",
15471 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015472 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015473 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2);
15474 }
15475 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015476 * 3.3.2.3 The {member type definitions}, in order, must be validly
15477 * derived from the corresponding type definitions in the {base
15478 * type definition}'s {member type definitions} given the empty set,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015479 * as defined in Type Derivation OK (Simple) (§3.14.6).
15480 */
15481 {
15482 xmlSchemaTypeLinkPtr baseMember;
15483
15484 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015485 * OPTIMIZE: if the type is restricting, it has no local defined
15486 * member types and inherits the member types of the base type;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015487 * thus a check for equality can be skipped.
15488 */
15489 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015490 * Even worse: I cannot see a scenario where a restricting
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015491 * union simple type can have other member types as the member
Daniel Veillard01fa6152004-06-29 17:04:39 +000015492 * types of it's base type. This check seems not necessary with
15493 * respect to the derivation process in libxml2.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015494 * But necessary if constructing types with an API.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015495 */
15496 if (type->memberTypes != NULL) {
15497 member = type->memberTypes;
15498 baseMember = xmlSchemaGetUnionSimpleTypeMemberTypes(type->baseType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015499 if ((member == NULL) && (baseMember != NULL)) {
15500 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15501 "different number of member types in base");
15502 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015503 while (member != NULL) {
15504 if (baseMember == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015505 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15506 "different number of member types in base");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015507 }
15508 if ((member->type != baseMember->type) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015509 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015510 member->type, baseMember->type, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015511 xmlChar *strBMT = NULL, *strBT = NULL;
15512
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015513 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015514 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015515 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015516 "The member type %s is not validly "
15517 "derived from its corresponding member "
15518 "type %s of the base type %s",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015519 xmlSchemaGetComponentQName(&str, member->type),
15520 xmlSchemaGetComponentQName(&strBMT, baseMember->type),
15521 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015522 FREE_AND_NULL(str)
15523 FREE_AND_NULL(strBMT)
15524 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015525 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015526 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015527 member = member->next;
15528 baseMember = baseMember->next;
15529 }
15530 }
15531 }
15532 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015533 * 3.3.2.4 Only pattern and enumeration facet components are
Daniel Veillard01fa6152004-06-29 17:04:39 +000015534 * allowed among the {facets}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015535 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015536 if (type->facets != NULL) {
15537 xmlSchemaFacetPtr facet;
15538 int ok = 1;
15539
15540 facet = type->facets;
15541 do {
15542 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
15543 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015544 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015545 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015546 type, facet);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015547 ok = 0;
15548 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015549 facet = facet->next;
15550 } while (facet != NULL);
15551 if (ok == 0)
15552 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015553
Daniel Veillard01fa6152004-06-29 17:04:39 +000015554 }
15555 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015556 * SPEC (3.3.2.5) (same as 1.3.2)
15557 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015558 * NOTE (3.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015559 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015560 */
15561 }
15562 }
15563
15564 return (0);
15565}
15566
15567/**
15568 * xmlSchemaCheckSRCSimpleType:
15569 * @ctxt: the schema parser context
15570 * @type: the simple type definition
15571 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015572 * Checks crc-simple-type constraints.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015573 *
15574 * Returns 0 if the constraints are satisfied,
15575 * if not a positive error code and -1 on internal
15576 * errors.
15577 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015578#if 0
Daniel Veillard01fa6152004-06-29 17:04:39 +000015579static int
15580xmlSchemaCheckSRCSimpleType(xmlSchemaParserCtxtPtr ctxt,
15581 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015582{
15583 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015584 * src-simple-type.1 The corresponding simple type definition, if any,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015585 * must satisfy the conditions set out in Constraints on Simple Type
15586 * Definition Schema Components (§3.14.6).
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015587 */
15588 if (WXS_IS_RESTRICTION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015589 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015590 * src-simple-type.2 "If the <restriction> alternative is chosen,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015591 * either it must have a base [attribute] or a <simpleType> among its
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015592 * [children], but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015593 * NOTE: This is checked in the parse function of <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015594 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015595 /*
15596 *
15597 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015598 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015599 /* src-simple-type.3 "If the <list> alternative is chosen, either it must have
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015600 * an itemType [attribute] or a <simpleType> among its [children],
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015601 * but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015602 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015603 * NOTE: This is checked in the parse function of <list>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015604 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015605 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015606 /*
15607 * src-simple-type.4 is checked in xmlSchemaCheckUnionTypeDefCircular().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015608 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015609 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015610 return (0);
15611}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015612#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +000015613
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015614static int
15615xmlSchemaCreateVCtxtOnPCtxt(xmlSchemaParserCtxtPtr ctxt)
15616{
15617 if (ctxt->vctxt == NULL) {
15618 ctxt->vctxt = xmlSchemaNewValidCtxt(NULL);
15619 if (ctxt->vctxt == NULL) {
15620 xmlSchemaPErr(ctxt, NULL,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000015621 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015622 "Internal error: xmlSchemaCreateVCtxtOnPCtxt, "
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015623 "failed to create a temp. validation context.\n",
15624 NULL, NULL);
15625 return (-1);
15626 }
15627 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015628 xmlSchemaSetValidErrors(ctxt->vctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000015629 ctxt->error, ctxt->warning, ctxt->errCtxt);
15630 xmlSchemaSetValidStructuredErrors(ctxt->vctxt,
15631 ctxt->serror, ctxt->errCtxt);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015632 }
15633 return (0);
15634}
15635
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015636static int
15637xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
15638 xmlNodePtr node,
15639 xmlSchemaTypePtr type,
15640 const xmlChar *value,
15641 xmlSchemaValPtr *retVal,
15642 int fireErrors,
15643 int normalize,
15644 int isNormalized);
15645
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015646/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015647 * xmlSchemaParseCheckCOSValidDefault:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015648 * @pctxt: the schema parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015649 * @type: the simple type definition
15650 * @value: the default value
15651 * @node: an optional node (the holder of the value)
15652 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015653 * Schema Component Constraint: Element Default Valid (Immediate)
15654 * (cos-valid-default)
15655 * This will be used by the parser only. For the validator there's
15656 * an other version.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015657 *
15658 * Returns 0 if the constraints are satisfied,
15659 * if not, a positive error code and -1 on internal
15660 * errors.
15661 */
15662static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015663xmlSchemaParseCheckCOSValidDefault(xmlSchemaParserCtxtPtr pctxt,
15664 xmlNodePtr node,
15665 xmlSchemaTypePtr type,
15666 const xmlChar *value,
15667 xmlSchemaValPtr *val)
15668{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015669 int ret = 0;
15670
15671 /*
15672 * cos-valid-default:
15673 * Schema Component Constraint: Element Default Valid (Immediate)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015674 * For a string to be a valid default with respect to a type
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015675 * definition the appropriate case among the following must be true:
15676 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015677 if WXS_IS_COMPLEX(type) {
William M. Brack2f2a6632004-08-20 23:09:47 +000015678 /*
15679 * Complex type.
15680 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015681 * SPEC (2.1) "its {content type} must be a simple type definition
15682 * or mixed."
15683 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015684 * type}'s particle must be ·emptiable· as defined by
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015685 * Particle Emptiable (§3.9.6)."
William M. Brack2f2a6632004-08-20 23:09:47 +000015686 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015687 if ((! WXS_HAS_SIMPLE_CONTENT(type)) &&
15688 ((! WXS_HAS_MIXED_CONTENT(type)) || (! WXS_EMPTIABLE(type)))) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015689 /* NOTE that this covers (2.2.2) as well. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015690 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015691 XML_SCHEMAP_COS_VALID_DEFAULT_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015692 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015693 "For a string to be a valid default, the type definition "
15694 "must be a simple type or a complex type with mixed content "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015695 "and a particle emptiable", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015696 return(XML_SCHEMAP_COS_VALID_DEFAULT_2_1);
15697 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015698 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015699 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015700 * 1 If the type definition is a simple type definition, then the string
15701 * must be ·valid· with respect to that definition as defined by String
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015702 * Valid (§3.14.4).
15703 *
15704 * AND
15705 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015706 * 2.2.1 If the {content type} is a simple type definition, then the
15707 * string must be ·valid· with respect to that simple type definition
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015708 * as defined by String Valid (§3.14.4).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015709 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015710 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015711 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015712 type, value, val, 1, 1, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015713 else if (WXS_HAS_SIMPLE_CONTENT(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015714 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015715 type->contentTypeDef, value, val, 1, 1, 0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015716 else
15717 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015718
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015719 if (ret < 0) {
15720 PERROR_INT("xmlSchemaParseCheckCOSValidDefault",
15721 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015722 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015723
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015724 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000015725}
15726
15727/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015728 * xmlSchemaCheckCTPropsCorrect:
William M. Brack2f2a6632004-08-20 23:09:47 +000015729 * @ctxt: the schema parser context
15730 * @type: the complex type definition
15731 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015732 *.(4.6) Constraints on Complex Type Definition Schema Components
15733 * Schema Component Constraint:
15734 * Complex Type Definition Properties Correct (ct-props-correct)
15735 * STATUS: (seems) complete
William M. Brack2f2a6632004-08-20 23:09:47 +000015736 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015737 * Returns 0 if the constraints are satisfied, a positive
15738 * error code if not and -1 if an internal error occured.
William M. Brack2f2a6632004-08-20 23:09:47 +000015739 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015740static int
15741xmlSchemaCheckCTPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
15742 xmlSchemaTypePtr type)
William M. Brack2f2a6632004-08-20 23:09:47 +000015743{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015744 /*
15745 * TODO: Correct the error code; XML_SCHEMAP_SRC_CT_1 is used temporarily.
15746 *
15747 * SPEC (1) "The values of the properties of a complex type definition must
15748 * be as described in the property tableau in The Complex Type Definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015749 * Schema Component (§3.4.1), modulo the impact of Missing
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015750 * Sub-components (§5.3)."
15751 */
15752 if ((type->baseType != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015753 (WXS_IS_SIMPLE(type->baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015754 (WXS_IS_EXTENSION(type) == 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015755 /*
15756 * SPEC (2) "If the {base type definition} is a simple type definition,
15757 * the {derivation method} must be extension."
15758 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015759 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015760 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015761 NULL, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015762 "If the base type is a simple type, the derivation method must be "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015763 "'extension'", NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015764 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015765 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015766 /*
15767 * SPEC (3) "Circular definitions are disallowed, except for the ·ur-type
15768 * definition·. That is, it must be possible to reach the ·ur-type
15769 * definition by repeatedly following the {base type definition}."
15770 *
15771 * NOTE (3) is done in xmlSchemaCheckTypeDefCircular().
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015772 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015773 /*
15774 * NOTE that (4) and (5) need the following:
15775 * - attribute uses need to be already inherited (apply attr. prohibitions)
15776 * - attribute group references need to be expanded already
15777 * - simple types need to be typefixed already
15778 */
15779 if (type->attrUses &&
15780 (((xmlSchemaItemListPtr) type->attrUses)->nbItems > 1))
15781 {
15782 xmlSchemaItemListPtr uses = (xmlSchemaItemListPtr) type->attrUses;
15783 xmlSchemaAttributeUsePtr use, tmp;
15784 int i, j, hasId = 0;
15785
15786 for (i = uses->nbItems -1; i >= 0; i--) {
15787 use = uses->items[i];
15788
15789 /*
15790 * SPEC ct-props-correct
15791 * (4) "Two distinct attribute declarations in the
15792 * {attribute uses} must not have identical {name}s and
15793 * {target namespace}s."
15794 */
15795 if (i > 0) {
15796 for (j = i -1; j >= 0; j--) {
15797 tmp = uses->items[j];
15798 if ((WXS_ATTRUSE_DECL_NAME(use) ==
15799 WXS_ATTRUSE_DECL_NAME(tmp)) &&
15800 (WXS_ATTRUSE_DECL_TNS(use) ==
15801 WXS_ATTRUSE_DECL_TNS(tmp)))
15802 {
15803 xmlChar *str = NULL;
15804
15805 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15806 XML_SCHEMAP_AG_PROPS_CORRECT,
15807 NULL, WXS_BASIC_CAST type,
15808 "Duplicate %s",
15809 xmlSchemaGetComponentDesignation(&str, use),
15810 NULL);
15811 FREE_AND_NULL(str);
15812 /*
15813 * Remove the duplicate.
15814 */
15815 if (xmlSchemaItemListRemove(uses, i) == -1)
15816 goto exit_failure;
15817 goto next_use;
15818 }
15819 }
15820 }
15821 /*
15822 * SPEC ct-props-correct
15823 * (5) "Two distinct attribute declarations in the
15824 * {attribute uses} must not have {type definition}s which
15825 * are or are derived from ID."
15826 */
15827 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
15828 if (xmlSchemaIsDerivedFromBuiltInType(
15829 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
15830 {
15831 if (hasId) {
15832 xmlChar *str = NULL;
15833
15834 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15835 XML_SCHEMAP_AG_PROPS_CORRECT,
15836 NULL, WXS_BASIC_CAST type,
15837 "There must not exist more than one attribute "
15838 "declaration of type 'xs:ID' "
15839 "(or derived from 'xs:ID'). The %s violates this "
15840 "constraint",
15841 xmlSchemaGetComponentDesignation(&str, use),
15842 NULL);
15843 FREE_AND_NULL(str);
15844 if (xmlSchemaItemListRemove(uses, i) == -1)
15845 goto exit_failure;
15846 }
15847
15848 hasId = 1;
15849 }
15850 }
15851next_use: {}
15852 }
15853 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015854 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015855exit_failure:
15856 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +000015857}
15858
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015859static int
15860xmlSchemaAreEqualTypes(xmlSchemaTypePtr typeA,
15861 xmlSchemaTypePtr typeB)
15862{
15863 /*
15864 * TODO: This should implement component-identity
15865 * in the future.
15866 */
15867 if ((typeA == NULL) || (typeB == NULL))
15868 return (0);
15869 return (typeA == typeB);
15870}
15871
15872/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015873 * xmlSchemaCheckCOSCTDerivedOK:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015874 * @ctxt: the schema parser context
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015875 * @type: the to-be derived complex type definition
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015876 * @baseType: the base complex type definition
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015877 * @set: the given set
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015878 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015879 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015880 * Type Derivation OK (Complex) (cos-ct-derived-ok)
15881 *
15882 * STATUS: completed
15883 *
15884 * Returns 0 if the constraints are satisfied, or 1
15885 * if not.
15886 */
15887static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015888xmlSchemaCheckCOSCTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015889 xmlSchemaTypePtr type,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015890 xmlSchemaTypePtr baseType,
15891 int set)
15892{
15893 int equal = xmlSchemaAreEqualTypes(type, baseType);
15894 /* TODO: Error codes. */
15895 /*
15896 * SPEC "For a complex type definition (call it D, for derived)
15897 * to be validly derived from a type definition (call this
15898 * B, for base) given a subset of {extension, restriction}
15899 * all of the following must be true:"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015900 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015901 if (! equal) {
15902 /*
15903 * SPEC (1) "If B and D are not the same type definition, then the
15904 * {derivation method} of D must not be in the subset."
15905 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015906 if (((set & SUBSET_EXTENSION) && (WXS_IS_EXTENSION(type))) ||
15907 ((set & SUBSET_RESTRICTION) && (WXS_IS_RESTRICTION(type))))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015908 return (1);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015909 } else {
15910 /*
15911 * SPEC (2.1) "B and D must be the same type definition."
15912 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015913 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015914 }
15915 /*
15916 * SPEC (2.2) "B must be D's {base type definition}."
15917 */
15918 if (type->baseType == baseType)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015919 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015920 /*
15921 * SPEC (2.3.1) "D's {base type definition} must not be the ·ur-type
15922 * definition·."
15923 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015924 if (WXS_IS_ANYTYPE(type->baseType))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015925 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015926
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015927 if (WXS_IS_COMPLEX(type->baseType)) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015928 /*
15929 * SPEC (2.3.2.1) "If D's {base type definition} is complex, then it
15930 * must be validly derived from B given the subset as defined by this
15931 * constraint."
15932 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015933 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015934 baseType, set));
15935 } else {
15936 /*
15937 * SPEC (2.3.2.2) "If D's {base type definition} is simple, then it
15938 * must be validly derived from B given the subset as defined in Type
15939 * Derivation OK (Simple) (§3.14.6).
15940 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015941 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015942 baseType, set));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015943 }
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015944}
15945
15946/**
15947 * xmlSchemaCheckCOSDerivedOK:
15948 * @type: the derived simple type definition
15949 * @baseType: the base type definition
15950 *
15951 * Calls:
15952 * Type Derivation OK (Simple) AND Type Derivation OK (Complex)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015953 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015954 * Checks wheter @type can be validly derived from @baseType.
15955 *
15956 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015957 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015958static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015959xmlSchemaCheckCOSDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015960 xmlSchemaTypePtr type,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015961 xmlSchemaTypePtr baseType,
15962 int set)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015963{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015964 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015965 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015966 else
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015967 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015968}
15969
William M. Brack2f2a6632004-08-20 23:09:47 +000015970/**
15971 * xmlSchemaCheckCOSCTExtends:
15972 * @ctxt: the schema parser context
15973 * @type: the complex type definition
15974 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015975 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015976 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015977 * Derivation Valid (Extension) (cos-ct-extends)
15978 *
15979 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015980 * missing:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015981 * (1.5)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015982 * (1.4.3.2.2.2) "Particle Valid (Extension)"
William M. Brack2f2a6632004-08-20 23:09:47 +000015983 *
15984 * Returns 0 if the constraints are satisfied, a positive
15985 * error code if not and -1 if an internal error occured.
15986 */
15987static int
15988xmlSchemaCheckCOSCTExtends(xmlSchemaParserCtxtPtr ctxt,
15989 xmlSchemaTypePtr type)
15990{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015991 xmlSchemaTypePtr base = type->baseType;
15992 /*
15993 * TODO: Correct the error code; XML_SCHEMAP_COS_CT_EXTENDS_1_1 is used
15994 * temporarily only.
William M. Brack2f2a6632004-08-20 23:09:47 +000015995 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015996 /*
15997 * SPEC (1) "If the {base type definition} is a complex type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015998 * then all of the following must be true:"
15999 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016000 if (WXS_IS_COMPLEX(base)) {
William M. Brack2f2a6632004-08-20 23:09:47 +000016001 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016002 * SPEC (1.1) "The {final} of the {base type definition} must not
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016003 * contain extension."
William M. Brack2f2a6632004-08-20 23:09:47 +000016004 */
16005 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16006 xmlSchemaPCustomErr(ctxt,
16007 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016008 WXS_BASIC_CAST type, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000016009 "The 'final' of the base type definition "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016010 "contains 'extension'", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000016011 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16012 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016013
16014 /*
16015 * ATTENTION: The constrains (1.2) and (1.3) are not applied,
16016 * since they are automatically satisfied through the
16017 * inheriting mechanism.
16018 * Note that even if redefining components, the inheriting mechanism
16019 * is used.
16020 */
16021#if 0
William M. Brack2f2a6632004-08-20 23:09:47 +000016022 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016023 * SPEC (1.2) "Its {attribute uses} must be a subset of the {attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016024 * uses}
16025 * of the complex type definition itself, that is, for every attribute
16026 * use in the {attribute uses} of the {base type definition}, there
16027 * must be an attribute use in the {attribute uses} of the complex
16028 * type definition itself whose {attribute declaration} has the same
16029 * {name}, {target namespace} and {type definition} as its attribute
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016030 * declaration"
William M. Brack2f2a6632004-08-20 23:09:47 +000016031 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016032 if (base->attrUses != NULL) {
16033 int i, j, found;
16034 xmlSchemaAttributeUsePtr use, buse;
William M. Brack2f2a6632004-08-20 23:09:47 +000016035
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016036 for (i = 0; i < (WXS_LIST_CAST base->attrUses)->nbItems; i ++) {
16037 buse = (WXS_LIST_CAST base->attrUses)->items[i];
16038 found = 0;
16039 if (type->attrUses != NULL) {
16040 use = (WXS_LIST_CAST type->attrUses)->items[j];
16041 for (j = 0; j < (WXS_LIST_CAST type->attrUses)->nbItems; j ++)
16042 {
16043 if ((WXS_ATTRUSE_DECL_NAME(use) ==
16044 WXS_ATTRUSE_DECL_NAME(buse)) &&
16045 (WXS_ATTRUSE_DECL_TNS(use) ==
16046 WXS_ATTRUSE_DECL_TNS(buse)) &&
16047 (WXS_ATTRUSE_TYPEDEF(use) ==
16048 WXS_ATTRUSE_TYPEDEF(buse))
16049 {
16050 found = 1;
16051 break;
16052 }
16053 }
16054 }
16055 if (! found) {
16056 xmlChar *str = NULL;
16057
16058 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16059 XML_SCHEMAP_COS_CT_EXTENDS_1_2,
16060 NULL, WXS_BASIC_CAST type,
16061 /*
16062 * TODO: The report does not indicate that also the
16063 * type needs to be the same.
16064 */
16065 "This type is missing a matching correspondent "
16066 "for its {base type}'s %s in its {attribute uses}",
16067 xmlSchemaGetComponentDesignation(&str,
16068 buse->children),
16069 NULL);
16070 FREE_AND_NULL(str)
16071 }
16072 }
16073 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016074 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016075 * SPEC (1.3) "If it has an {attribute wildcard}, the complex type
16076 * definition must also have one, and the base type definition's
16077 * {attribute wildcard}'s {namespace constraint} must be a subset
16078 * of the complex type definition's {attribute wildcard}'s {namespace
16079 * constraint}, as defined by Wildcard Subset (§3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016080 */
16081
16082 /*
16083 * MAYBE TODO: Enable if ever needed. But this will be needed only
16084 * if created the type via a schema construction API.
16085 */
16086 if (base->attributeWildcard != NULL) {
16087 if (type->attributeWilcard == NULL) {
16088 xmlChar *str = NULL;
16089
16090 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16091 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16092 NULL, type,
16093 "The base %s has an attribute wildcard, "
16094 "but this type is missing an attribute wildcard",
16095 xmlSchemaGetComponentDesignation(&str, base));
16096 FREE_AND_NULL(str)
16097
16098 } else if (xmlSchemaCheckCOSNSSubset(
16099 base->attributeWildcard, type->attributeWildcard))
16100 {
16101 xmlChar *str = NULL;
16102
16103 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16104 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16105 NULL, type,
16106 "The attribute wildcard is not a valid "
16107 "superset of the one in the base %s",
16108 xmlSchemaGetComponentDesignation(&str, base));
16109 FREE_AND_NULL(str)
16110 }
16111 }
16112#endif
16113 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016114 * SPEC (1.4) "One of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016115 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016116 if ((type->contentTypeDef != NULL) &&
16117 (type->contentTypeDef == base->contentTypeDef)) {
16118 /*
16119 * SPEC (1.4.1) "The {content type} of the {base type definition}
16120 * and the {content type} of the complex type definition itself
16121 * must be the same simple type definition"
16122 * PASS
16123 */
16124 } else if ((type->contentType == XML_SCHEMA_CONTENT_EMPTY) &&
16125 (base->contentType == XML_SCHEMA_CONTENT_EMPTY) ) {
16126 /*
16127 * SPEC (1.4.2) "The {content type} of both the {base type
16128 * definition} and the complex type definition itself must
16129 * be empty."
16130 * PASS
16131 */
16132 } else {
16133 /*
16134 * SPEC (1.4.3) "All of the following must be true:"
16135 */
16136 if (type->subtypes == NULL) {
16137 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016138 * SPEC 1.4.3.1 The {content type} of the complex type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016139 * definition itself must specify a particle.
16140 */
16141 xmlSchemaPCustomErr(ctxt,
16142 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016143 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016144 "The content type must specify a particle", NULL);
16145 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16146 }
16147 /*
16148 * SPEC (1.4.3.2) "One of the following must be true:"
16149 */
16150 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16151 /*
16152 * SPEC (1.4.3.2.1) "The {content type} of the {base type
16153 * definition} must be empty.
16154 * PASS
16155 */
16156 } else {
16157 /*
16158 * SPEC (1.4.3.2.2) "All of the following must be true:"
16159 */
16160 if ((type->contentType != base->contentType) ||
16161 ((type->contentType != XML_SCHEMA_CONTENT_MIXED) &&
16162 (type->contentType != XML_SCHEMA_CONTENT_ELEMENTS))) {
16163 /*
16164 * SPEC (1.4.3.2.2.1) "Both {content type}s must be mixed
16165 * or both must be element-only."
16166 */
16167 xmlSchemaPCustomErr(ctxt,
16168 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016169 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016170 "The content type of both, the type and its base "
16171 "type, must either 'mixed' or 'element-only'", NULL);
16172 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016173 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016174 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016175 * URGENT TODO SPEC (1.4.3.2.2.2) "The particle of the
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016176 * complex type definition must be a ·valid extension·
16177 * of the {base type definition}'s particle, as defined
16178 * in Particle Valid (Extension) (§3.9.6)."
16179 *
16180 * NOTE that we won't check "Particle Valid (Extension)",
16181 * since it is ensured by the derivation process in
16182 * xmlSchemaTypeFixup(). We need to implement this when heading
16183 * for a construction API
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016184 * TODO: !! This is needed to be checked if redefining a type !!
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016185 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016186 }
16187 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016188 * URGENT TODO (1.5)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016189 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016190 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016191 } else {
16192 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016193 * SPEC (2) "If the {base type definition} is a simple type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016194 * then all of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016195 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016196 if (type->contentTypeDef != base) {
16197 /*
16198 * SPEC (2.1) "The {content type} must be the same simple type
16199 * definition."
16200 */
16201 xmlSchemaPCustomErr(ctxt,
16202 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016203 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016204 "The content type must be the simple base type", NULL);
16205 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16206 }
16207 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16208 /*
16209 * SPEC (2.2) "The {final} of the {base type definition} must not
16210 * contain extension"
16211 * NOTE that this is the same as (1.1).
16212 */
16213 xmlSchemaPCustomErr(ctxt,
16214 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016215 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016216 "The 'final' of the base type definition "
16217 "contains 'extension'", NULL);
16218 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016219 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016220 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016221 return (0);
William M. Brack2f2a6632004-08-20 23:09:47 +000016222}
16223
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016224/**
16225 * xmlSchemaCheckDerivationOKRestriction:
16226 * @ctxt: the schema parser context
16227 * @type: the complex type definition
16228 *
16229 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016230 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016231 * Derivation Valid (Restriction, Complex) (derivation-ok-restriction)
16232 *
16233 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016234 * missing:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016235 * (5.4.2) ???
16236 *
16237 * ATTENTION:
16238 * In XML Schema 1.1 this will be:
16239 * Validation Rule: Checking complex type subsumption
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016240 *
16241 * Returns 0 if the constraints are satisfied, a positive
16242 * error code if not and -1 if an internal error occured.
16243 */
16244static int
16245xmlSchemaCheckDerivationOKRestriction(xmlSchemaParserCtxtPtr ctxt,
16246 xmlSchemaTypePtr type)
16247{
16248 xmlSchemaTypePtr base;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016249
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016250 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016251 * TODO: Correct the error code; XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1 is used
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016252 * temporarily only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016253 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016254 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016255 if (! WXS_IS_COMPLEX(base)) {
16256 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16257 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16258 type->node, WXS_BASIC_CAST type,
16259 "The base type must be a complex type", NULL, NULL);
16260 return(ctxt->err);
16261 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016262 if (base->flags & XML_SCHEMAS_TYPE_FINAL_RESTRICTION) {
16263 /*
16264 * SPEC (1) "The {base type definition} must be a complex type
16265 * definition whose {final} does not contain restriction."
16266 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016267 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16268 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16269 type->node, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016270 "The 'final' of the base type definition "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016271 "contains 'restriction'", NULL, NULL);
16272 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016273 }
16274 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016275 * SPEC (2), (3) and (4)
16276 * Those are handled in a separate function, since the
16277 * same constraints are needed for redefinition of
16278 * attribute groups as well.
16279 */
16280 if (xmlSchemaCheckDerivationOKRestriction2to4(ctxt,
16281 XML_SCHEMA_ACTION_DERIVE,
16282 WXS_BASIC_CAST type, WXS_BASIC_CAST base,
16283 type->attrUses, base->attrUses,
16284 type->attributeWildcard,
16285 base->attributeWildcard) == -1)
16286 {
16287 return(-1);
16288 }
16289 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016290 * SPEC (5) "One of the following must be true:"
16291 */
16292 if (base->builtInType == XML_SCHEMAS_ANYTYPE) {
16293 /*
16294 * SPEC (5.1) "The {base type definition} must be the
16295 * ·ur-type definition·."
16296 * PASS
16297 */
16298 } else if ((type->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16299 (type->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16300 /*
16301 * SPEC (5.2.1) "The {content type} of the complex type definition
16302 * must be a simple type definition"
16303 *
16304 * SPEC (5.2.2) "One of the following must be true:"
16305 */
16306 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016307 (base->contentType == XML_SCHEMA_CONTENT_BASIC))
16308 {
16309 int err;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016310 /*
16311 * SPEC (5.2.2.1) "The {content type} of the {base type
16312 * definition} must be a simple type definition from which
16313 * the {content type} is validly derived given the empty
16314 * set as defined in Type Derivation OK (Simple) (§3.14.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016315 *
16316 * ATTENTION TODO: This seems not needed if the type implicitely
16317 * derived from the base type.
16318 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016319 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016320 err = xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST ctxt,
16321 type->contentTypeDef, base->contentTypeDef, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016322 if (err != 0) {
16323 xmlChar *strA = NULL, *strB = NULL;
16324
16325 if (err == -1)
16326 return(-1);
16327 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16328 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16329 NULL, WXS_BASIC_CAST type,
16330 "The {content type} %s is not validly derived from the "
16331 "base type's {content type} %s",
16332 xmlSchemaGetComponentDesignation(&strA,
16333 type->contentTypeDef),
16334 xmlSchemaGetComponentDesignation(&strB,
16335 base->contentTypeDef));
16336 FREE_AND_NULL(strA);
16337 FREE_AND_NULL(strB);
16338 return(ctxt->err);
16339 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016340 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
16341 (xmlSchemaIsParticleEmptiable(
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016342 (xmlSchemaParticlePtr) base->subtypes))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016343 /*
16344 * SPEC (5.2.2.2) "The {base type definition} must be mixed
16345 * and have a particle which is ·emptiable· as defined in
16346 * Particle Emptiable (§3.9.6)."
16347 * PASS
16348 */
16349 } else {
16350 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016351 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16352 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016353 "The content type of the base type must be either "
16354 "a simple type or 'mixed' and an emptiable particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016355 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016356 }
16357 } else if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16358 /*
16359 * SPEC (5.3.1) "The {content type} of the complex type itself must
16360 * be empty"
16361 */
16362 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16363 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016364 * SPEC (5.3.2.1) "The {content type} of the {base type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016365 * definition} must also be empty."
16366 * PASS
16367 */
16368 } else if (((base->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
16369 (base->contentType == XML_SCHEMA_CONTENT_MIXED)) &&
16370 xmlSchemaIsParticleEmptiable(
16371 (xmlSchemaParticlePtr) base->subtypes)) {
16372 /*
16373 * SPEC (5.3.2.2) "The {content type} of the {base type
16374 * definition} must be elementOnly or mixed and have a particle
16375 * which is ·emptiable· as defined in Particle Emptiable (§3.9.6)."
16376 * PASS
16377 */
16378 } else {
16379 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016380 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16381 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016382 "The content type of the base type must be either "
16383 "empty or 'mixed' (or 'elements-only') and an emptiable "
16384 "particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016385 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016386 }
16387 } else if ((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016388 WXS_HAS_MIXED_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016389 /*
16390 * SPEC (5.4.1.1) "The {content type} of the complex type definition
16391 * itself must be element-only"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016392 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016393 if (WXS_HAS_MIXED_CONTENT(type) && (! WXS_HAS_MIXED_CONTENT(base))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016394 /*
16395 * SPEC (5.4.1.2) "The {content type} of the complex type
16396 * definition itself and of the {base type definition} must be
16397 * mixed"
16398 */
16399 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016400 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16401 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016402 "If the content type is 'mixed', then the content type of the "
16403 "base type must also be 'mixed'", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016404 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016405 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016406 /*
16407 * SPEC (5.4.2) "The particle of the complex type definition itself
16408 * must be a ·valid restriction· of the particle of the {content
16409 * type} of the {base type definition} as defined in Particle Valid
16410 * (Restriction) (§3.9.6).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016411 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016412 * URGENT TODO: (5.4.2)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016413 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016414 } else {
16415 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016416 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16417 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016418 "The type is not a valid restriction of its base type", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016419 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016420 }
16421 return (0);
16422}
16423
16424/**
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016425 * xmlSchemaCheckCTComponent:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016426 * @ctxt: the schema parser context
16427 * @type: the complex type definition
16428 *
16429 * (3.4.6) Constraints on Complex Type Definition Schema Components
16430 *
16431 * Returns 0 if the constraints are satisfied, a positive
16432 * error code if not and -1 if an internal error occured.
16433 */
16434static int
16435xmlSchemaCheckCTComponent(xmlSchemaParserCtxtPtr ctxt,
16436 xmlSchemaTypePtr type)
16437{
16438 int ret;
16439 /*
16440 * Complex Type Definition Properties Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016441 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016442 ret = xmlSchemaCheckCTPropsCorrect(ctxt, type);
16443 if (ret != 0)
16444 return (ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016445 if (WXS_IS_EXTENSION(type))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016446 ret = xmlSchemaCheckCOSCTExtends(ctxt, type);
16447 else
16448 ret = xmlSchemaCheckDerivationOKRestriction(ctxt, type);
16449 return (ret);
16450}
16451
16452/**
16453 * xmlSchemaCheckSRCCT:
16454 * @ctxt: the schema parser context
16455 * @type: the complex type definition
16456 *
16457 * (3.4.3) Constraints on XML Representations of Complex Type Definitions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016458 * Schema Representation Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016459 * Complex Type Definition Representation OK (src-ct)
16460 *
16461 * Returns 0 if the constraints are satisfied, a positive
16462 * error code if not and -1 if an internal error occured.
16463 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016464static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016465xmlSchemaCheckSRCCT(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016466 xmlSchemaTypePtr type)
16467{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016468 xmlSchemaTypePtr base;
16469 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016470
16471 /*
16472 * TODO: Adjust the error codes here, as I used
16473 * XML_SCHEMAP_SRC_CT_1 only yet.
16474 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016475 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016476 if (! WXS_HAS_SIMPLE_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016477 /*
16478 * 1 If the <complexContent> alternative is chosen, the type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016479 * ·resolved· to by the ·actual value· of the base [attribute]
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016480 * must be a complex type definition;
16481 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016482 if (! WXS_IS_COMPLEX(base)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016483 xmlChar *str = NULL;
16484 xmlSchemaPCustomErr(ctxt,
16485 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016486 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016487 "If using <complexContent>, the base type is expected to be "
16488 "a complex type. The base type '%s' is a simple type",
16489 xmlSchemaFormatQName(&str, base->targetNamespace,
16490 base->name));
16491 FREE_AND_NULL(str)
16492 return (XML_SCHEMAP_SRC_CT_1);
16493 }
16494 } else {
16495 /*
16496 * SPEC
16497 * 2 If the <simpleContent> alternative is chosen, all of the
16498 * following must be true:
16499 * 2.1 The type definition ·resolved· to by the ·actual value· of the
16500 * base [attribute] must be one of the following:
16501 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016502 if (WXS_IS_SIMPLE(base)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016503 if (WXS_IS_EXTENSION(type) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016504 xmlChar *str = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016505 /*
16506 * 2.1.3 only if the <extension> alternative is also
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016507 * chosen, a simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016508 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016509 /* TODO: Change error code to ..._SRC_CT_2_1_3. */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016510 xmlSchemaPCustomErr(ctxt,
16511 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016512 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016513 "If using <simpleContent> and <restriction>, the base "
16514 "type must be a complex type. The base type '%s' is "
16515 "a simple type",
16516 xmlSchemaFormatQName(&str, base->targetNamespace,
16517 base->name));
16518 FREE_AND_NULL(str)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016519 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016520 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016521 } else {
16522 /* Base type is a complex type. */
16523 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16524 (base->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16525 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016526 * 2.1.1 a complex type definition whose {content type} is a
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016527 * simple type definition;
16528 * PASS
16529 */
16530 if (base->contentTypeDef == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016531 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016532 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016533 "Internal error: xmlSchemaCheckSRCCT, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016534 "'%s', base type has no content type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016535 type->name);
16536 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016537 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016538 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016539 (WXS_IS_RESTRICTION(type))) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016540
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016541 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016542 * 2.1.2 only if the <restriction> alternative is also
16543 * chosen, a complex type definition whose {content type}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016544 * is mixed and a particle emptiable.
16545 */
16546 if (! xmlSchemaIsParticleEmptiable(
16547 (xmlSchemaParticlePtr) base->subtypes)) {
16548 ret = XML_SCHEMAP_SRC_CT_1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016549 } else
16550 /*
16551 * Attention: at this point the <simpleType> child is in
16552 * ->contentTypeDef (put there during parsing).
16553 */
16554 if (type->contentTypeDef == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016555 xmlChar *str = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016556 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016557 * 2.2 If clause 2.1.2 above is satisfied, then there
16558 * must be a <simpleType> among the [children] of
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016559 * <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016560 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016561 /* TODO: Change error code to ..._SRC_CT_2_2. */
16562 xmlSchemaPCustomErr(ctxt,
16563 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016564 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016565 "A <simpleType> is expected among the children "
16566 "of <restriction>, if <simpleContent> is used and "
16567 "the base type '%s' is a complex type",
16568 xmlSchemaFormatQName(&str, base->targetNamespace,
16569 base->name));
16570 FREE_AND_NULL(str)
16571 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016572 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016573 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016574 ret = XML_SCHEMAP_SRC_CT_1;
16575 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016576 }
16577 if (ret > 0) {
16578 xmlChar *str = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016579 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016580 xmlSchemaPCustomErr(ctxt,
16581 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016582 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016583 "If <simpleContent> and <restriction> is used, the "
16584 "base type must be a simple type or a complex type with "
16585 "mixed content and particle emptiable. The base type "
16586 "'%s' is none of those",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016587 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016588 base->name));
16589 } else {
16590 xmlSchemaPCustomErr(ctxt,
16591 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016592 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016593 "If <simpleContent> and <extension> is used, the "
16594 "base type must be a simple type. The base type '%s' "
16595 "is a complex type",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016596 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016597 base->name));
16598 }
16599 FREE_AND_NULL(str)
16600 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016601 }
16602 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016603 * SPEC (3) "The corresponding complex type definition component must
16604 * satisfy the conditions set out in Constraints on Complex Type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016605 * Definition Schema Components (§3.4.6);"
16606 * NOTE (3) will be done in xmlSchemaTypeFixup().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016607 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016608 /*
16609 * SPEC (4) If clause 2.2.1 or clause 2.2.2 in the correspondence specification
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016610 * above for {attribute wildcard} is satisfied, the intensional
16611 * intersection must be expressible, as defined in Attribute Wildcard
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016612 * Intersection (§3.10.6).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016613 * NOTE (4) is done in xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016614 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016615 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016616}
William M. Brack2f2a6632004-08-20 23:09:47 +000016617
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016618#ifdef ENABLE_PARTICLE_RESTRICTION
16619/**
16620 * xmlSchemaCheckParticleRangeOK:
16621 * @ctxt: the schema parser context
16622 * @type: the complex type definition
16623 *
16624 * (3.9.6) Constraints on Particle Schema Components
16625 * Schema Component Constraint:
16626 * Occurrence Range OK (range-ok)
16627 *
16628 * STATUS: complete
16629 *
16630 * Returns 0 if the constraints are satisfied, a positive
16631 * error code if not and -1 if an internal error occured.
16632 */
16633static int
16634xmlSchemaCheckParticleRangeOK(int rmin, int rmax,
16635 int bmin, int bmax)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016636{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016637 if (rmin < bmin)
16638 return (1);
16639 if ((bmax != UNBOUNDED) &&
16640 (rmax > bmax))
16641 return (1);
16642 return (0);
16643}
16644
16645/**
16646 * xmlSchemaCheckRCaseNameAndTypeOK:
16647 * @ctxt: the schema parser context
16648 * @r: the restricting element declaration particle
16649 * @b: the base element declaration particle
16650 *
16651 * (3.9.6) Constraints on Particle Schema Components
16652 * Schema Component Constraint:
16653 * Particle Restriction OK (Elt:Elt -- NameAndTypeOK)
16654 * (rcase-NameAndTypeOK)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016655 *
16656 * STATUS:
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016657 * MISSING (3.2.3)
16658 * CLARIFY: (3.2.2)
16659 *
16660 * Returns 0 if the constraints are satisfied, a positive
16661 * error code if not and -1 if an internal error occured.
16662 */
16663static int
16664xmlSchemaCheckRCaseNameAndTypeOK(xmlSchemaParserCtxtPtr ctxt,
16665 xmlSchemaParticlePtr r,
16666 xmlSchemaParticlePtr b)
16667{
16668 xmlSchemaElementPtr elemR, elemB;
16669
16670 /* TODO: Error codes (rcase-NameAndTypeOK). */
16671 elemR = (xmlSchemaElementPtr) r->children;
16672 elemB = (xmlSchemaElementPtr) b->children;
16673 /*
16674 * SPEC (1) "The declarations' {name}s and {target namespace}s are
16675 * the same."
16676 */
16677 if ((elemR != elemB) &&
16678 ((! xmlStrEqual(elemR->name, elemB->name)) ||
16679 (! xmlStrEqual(elemR->targetNamespace, elemB->targetNamespace))))
16680 return (1);
16681 /*
16682 * SPEC (2) "R's occurrence range is a valid restriction of B's
16683 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16684 */
16685 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16686 b->minOccurs, b->maxOccurs) != 0)
16687 return (1);
16688 /*
16689 * SPEC (3.1) "Both B's declaration's {scope} and R's declaration's
16690 * {scope} are global."
16691 */
16692 if (elemR == elemB)
16693 return (0);
16694 /*
16695 * SPEC (3.2.1) "Either B's {nillable} is true or R's {nillable} is false."
16696 */
16697 if (((elemB->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) &&
16698 (elemR->flags & XML_SCHEMAS_ELEM_NILLABLE))
16699 return (1);
16700 /*
16701 * SPEC (3.2.2) "either B's declaration's {value constraint} is absent,
16702 * or is not fixed, or R's declaration's {value constraint} is fixed
16703 * with the same value."
16704 */
16705 if ((elemB->value != NULL) && (elemB->flags & XML_SCHEMAS_ELEM_FIXED) &&
16706 ((elemR->value == NULL) ||
16707 ((elemR->flags & XML_SCHEMAS_ELEM_FIXED) == 0) ||
16708 /* TODO: Equality of the initial value or normalized or canonical? */
16709 (! xmlStrEqual(elemR->value, elemB->value))))
16710 return (1);
16711 /*
16712 * TODO: SPEC (3.2.3) "R's declaration's {identity-constraint
16713 * definitions} is a subset of B's declaration's {identity-constraint
16714 * definitions}, if any."
16715 */
16716 if (elemB->idcs != NULL) {
16717 /* TODO */
16718 }
16719 /*
16720 * SPEC (3.2.4) "R's declaration's {disallowed substitutions} is a
16721 * superset of B's declaration's {disallowed substitutions}."
16722 */
16723 if (((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) &&
16724 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) == 0)) ||
16725 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) &&
16726 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) == 0)) ||
16727 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) &&
16728 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) == 0)))
16729 return (1);
16730 /*
16731 * SPEC (3.2.5) "R's {type definition} is validly derived given
16732 * {extension, list, union} from B's {type definition}"
16733 *
16734 * BADSPEC TODO: What's the point of adding "list" and "union" to the
16735 * set, if the corresponding constraints handle "restriction" and
16736 * "extension" only?
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016737 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016738 */
16739 {
16740 int set = 0;
16741
16742 set |= SUBSET_EXTENSION;
16743 set |= SUBSET_LIST;
16744 set |= SUBSET_UNION;
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016745 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST ctxt, elemR->subtypes,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016746 elemB->subtypes, set) != 0)
16747 return (1);
16748 }
16749 return (0);
16750}
16751
16752/**
16753 * xmlSchemaCheckRCaseNSCompat:
16754 * @ctxt: the schema parser context
16755 * @r: the restricting element declaration particle
16756 * @b: the base wildcard particle
16757 *
16758 * (3.9.6) Constraints on Particle Schema Components
16759 * Schema Component Constraint:
16760 * Particle Derivation OK (Elt:Any -- NSCompat)
16761 * (rcase-NSCompat)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016762 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016763 * STATUS: complete
16764 *
16765 * Returns 0 if the constraints are satisfied, a positive
16766 * error code if not and -1 if an internal error occured.
16767 */
16768static int
16769xmlSchemaCheckRCaseNSCompat(xmlSchemaParserCtxtPtr ctxt,
16770 xmlSchemaParticlePtr r,
16771 xmlSchemaParticlePtr b)
16772{
16773 /* TODO:Error codes (rcase-NSCompat). */
16774 /*
16775 * SPEC "For an element declaration particle to be a ·valid restriction·
16776 * of a wildcard particle all of the following must be true:"
16777 *
16778 * SPEC (1) "The element declaration's {target namespace} is ·valid·
16779 * with respect to the wildcard's {namespace constraint} as defined by
16780 * Wildcard allows Namespace Name (§3.10.4)."
16781 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016782 if (xmlSchemaCheckCVCWildcardNamespace((xmlSchemaWildcardPtr) b->children,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016783 ((xmlSchemaElementPtr) r->children)->targetNamespace) != 0)
16784 return (1);
16785 /*
16786 * SPEC (2) "R's occurrence range is a valid restriction of B's
16787 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16788 */
16789 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16790 b->minOccurs, b->maxOccurs) != 0)
16791 return (1);
16792
16793 return (0);
16794}
16795
16796/**
16797 * xmlSchemaCheckRCaseRecurseAsIfGroup:
16798 * @ctxt: the schema parser context
16799 * @r: the restricting element declaration particle
16800 * @b: the base model group particle
16801 *
16802 * (3.9.6) Constraints on Particle Schema Components
16803 * Schema Component Constraint:
16804 * Particle Derivation OK (Elt:All/Choice/Sequence -- RecurseAsIfGroup)
16805 * (rcase-RecurseAsIfGroup)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016806 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016807 * STATUS: TODO
16808 *
16809 * Returns 0 if the constraints are satisfied, a positive
16810 * error code if not and -1 if an internal error occured.
16811 */
16812static int
16813xmlSchemaCheckRCaseRecurseAsIfGroup(xmlSchemaParserCtxtPtr ctxt,
16814 xmlSchemaParticlePtr r,
16815 xmlSchemaParticlePtr b)
16816{
16817 /* TODO: Error codes (rcase-RecurseAsIfGroup). */
16818 TODO
16819 return (0);
16820}
16821
16822/**
16823 * xmlSchemaCheckRCaseNSSubset:
16824 * @ctxt: the schema parser context
16825 * @r: the restricting wildcard particle
16826 * @b: the base wildcard particle
16827 *
16828 * (3.9.6) Constraints on Particle Schema Components
16829 * Schema Component Constraint:
16830 * Particle Derivation OK (Any:Any -- NSSubset)
16831 * (rcase-NSSubset)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016832 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016833 * STATUS: complete
16834 *
16835 * Returns 0 if the constraints are satisfied, a positive
16836 * error code if not and -1 if an internal error occured.
16837 */
16838static int
16839xmlSchemaCheckRCaseNSSubset(xmlSchemaParserCtxtPtr ctxt,
16840 xmlSchemaParticlePtr r,
16841 xmlSchemaParticlePtr b,
16842 int isAnyTypeBase)
16843{
16844 /* TODO: Error codes (rcase-NSSubset). */
16845 /*
16846 * SPEC (1) "R's occurrence range is a valid restriction of B's
16847 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16848 */
16849 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16850 b->minOccurs, b->maxOccurs))
16851 return (1);
16852 /*
16853 * SPEC (2) "R's {namespace constraint} must be an intensional subset
16854 * of B's {namespace constraint} as defined by Wildcard Subset (§3.10.6)."
16855 */
16856 if (xmlSchemaCheckCOSNSSubset((xmlSchemaWildcardPtr) r->children,
16857 (xmlSchemaWildcardPtr) b->children))
16858 return (1);
16859 /*
16860 * SPEC (3) "Unless B is the content model wildcard of the ·ur-type
16861 * definition·, R's {process contents} must be identical to or stronger
16862 * than B's {process contents}, where strict is stronger than lax is
16863 * stronger than skip."
16864 */
16865 if (! isAnyTypeBase) {
16866 if ( ((xmlSchemaWildcardPtr) r->children)->processContents <
16867 ((xmlSchemaWildcardPtr) b->children)->processContents)
16868 return (1);
16869 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016870
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016871 return (0);
16872}
16873
16874/**
16875 * xmlSchemaCheckCOSParticleRestrict:
16876 * @ctxt: the schema parser context
16877 * @type: the complex type definition
16878 *
16879 * (3.9.6) Constraints on Particle Schema Components
16880 * Schema Component Constraint:
16881 * Particle Valid (Restriction) (cos-particle-restrict)
16882 *
16883 * STATUS: TODO
16884 *
16885 * Returns 0 if the constraints are satisfied, a positive
16886 * error code if not and -1 if an internal error occured.
16887 */
16888static int
16889xmlSchemaCheckCOSParticleRestrict(xmlSchemaParserCtxtPtr ctxt,
16890 xmlSchemaParticlePtr r,
16891 xmlSchemaParticlePtr b)
16892{
16893 int ret = 0;
16894
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016895 /*part = WXS_TYPE_PARTICLE(type);
16896 basePart = WXS_TYPE_PARTICLE(base);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016897 */
16898
16899 TODO
16900
16901 /*
16902 * SPEC (1) "They are the same particle."
16903 */
16904 if (r == b)
16905 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016906
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016907
16908 return (0);
16909}
16910
16911/**
16912 * xmlSchemaCheckRCaseNSRecurseCheckCardinality:
16913 * @ctxt: the schema parser context
16914 * @r: the model group particle
16915 * @b: the base wildcard particle
16916 *
16917 * (3.9.6) Constraints on Particle Schema Components
16918 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016919 * Particle Derivation OK (All/Choice/Sequence:Any --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016920 * NSRecurseCheckCardinality)
16921 * (rcase-NSRecurseCheckCardinality)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016922 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016923 * STATUS: TODO: subst-groups
16924 *
16925 * Returns 0 if the constraints are satisfied, a positive
16926 * error code if not and -1 if an internal error occured.
16927 */
16928static int
16929xmlSchemaCheckRCaseNSRecurseCheckCardinality(xmlSchemaParserCtxtPtr ctxt,
16930 xmlSchemaParticlePtr r,
16931 xmlSchemaParticlePtr b)
16932{
16933 xmlSchemaParticlePtr part;
16934 /* TODO: Error codes (rcase-NSRecurseCheckCardinality). */
16935 if ((r->children == NULL) || (r->children->children == NULL))
16936 return (-1);
16937 /*
16938 * SPEC "For a group particle to be a ·valid restriction· of a
16939 * wildcard particle..."
16940 *
16941 * SPEC (1) "Every member of the {particles} of the group is a ·valid
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016942 * restriction· of the wildcard as defined by
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016943 * Particle Valid (Restriction) (§3.9.6)."
16944 */
16945 part = (xmlSchemaParticlePtr) r->children->children;
16946 do {
16947 if (xmlSchemaCheckCOSParticleRestrict(ctxt, part, b))
16948 return (1);
16949 part = (xmlSchemaParticlePtr) part->next;
16950 } while (part != NULL);
16951 /*
16952 * SPEC (2) "The effective total range of the group [...] is a
16953 * valid restriction of B's occurrence range as defined by
16954 * Occurrence Range OK (§3.9.6)."
16955 */
16956 if (xmlSchemaCheckParticleRangeOK(
16957 xmlSchemaGetParticleTotalRangeMin(r),
16958 xmlSchemaGetParticleTotalRangeMax(r),
16959 b->minOccurs, b->maxOccurs) != 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016960 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016961 return (0);
16962}
16963
16964/**
16965 * xmlSchemaCheckRCaseRecurse:
16966 * @ctxt: the schema parser context
16967 * @r: the <all> or <sequence> model group particle
16968 * @b: the base <all> or <sequence> model group particle
16969 *
16970 * (3.9.6) Constraints on Particle Schema Components
16971 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016972 * Particle Derivation OK (All:All,Sequence:Sequence --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016973 Recurse)
16974 * (rcase-Recurse)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016975 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016976 * STATUS: ?
16977 * TODO: subst-groups
16978 *
16979 * Returns 0 if the constraints are satisfied, a positive
16980 * error code if not and -1 if an internal error occured.
16981 */
16982static int
16983xmlSchemaCheckRCaseRecurse(xmlSchemaParserCtxtPtr ctxt,
16984 xmlSchemaParticlePtr r,
16985 xmlSchemaParticlePtr b)
16986{
16987 /* xmlSchemaParticlePtr part; */
16988 /* TODO: Error codes (rcase-Recurse). */
16989 if ((r->children == NULL) || (b->children == NULL) ||
16990 (r->children->type != b->children->type))
16991 return (-1);
16992 /*
16993 * SPEC "For an all or sequence group particle to be a ·valid
16994 * restriction· of another group particle with the same {compositor}..."
16995 *
16996 * SPEC (1) "R's occurrence range is a valid restriction of B's
16997 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16998 */
16999 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
17000 b->minOccurs, b->maxOccurs))
17001 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017002
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017003
17004 return (0);
17005}
17006
17007#endif
17008
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017009#define FACET_RESTR_MUTUAL_ERR(fac1, fac2) \
17010 xmlSchemaPCustomErrExt(pctxt, \
17011 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017012 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017013 "It is an error for both '%s' and '%s' to be specified on the "\
17014 "same type definition", \
17015 BAD_CAST xmlSchemaFacetTypeToString(fac1->type), \
17016 BAD_CAST xmlSchemaFacetTypeToString(fac2->type), NULL);
17017
17018#define FACET_RESTR_ERR(fac1, msg) \
17019 xmlSchemaPCustomErr(pctxt, \
17020 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017021 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017022 msg, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017023
17024#define FACET_RESTR_FIXED_ERR(fac) \
17025 xmlSchemaPCustomErr(pctxt, \
17026 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017027 WXS_BASIC_CAST fac, fac->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017028 "The base type's facet is 'fixed', thus the value must not " \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017029 "differ", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017030
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017031static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017032xmlSchemaDeriveFacetErr(xmlSchemaParserCtxtPtr pctxt,
17033 xmlSchemaFacetPtr facet1,
17034 xmlSchemaFacetPtr facet2,
17035 int lessGreater,
17036 int orEqual,
17037 int ofBase)
17038{
17039 xmlChar *msg = NULL;
17040
17041 msg = xmlStrdup(BAD_CAST "'");
17042 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet1->type));
17043 msg = xmlStrcat(msg, BAD_CAST "' has to be");
17044 if (lessGreater == 0)
17045 msg = xmlStrcat(msg, BAD_CAST " equal to");
17046 if (lessGreater == 1)
17047 msg = xmlStrcat(msg, BAD_CAST " greater than");
17048 else
17049 msg = xmlStrcat(msg, BAD_CAST " less than");
17050
17051 if (orEqual)
17052 msg = xmlStrcat(msg, BAD_CAST " or equal to");
17053 msg = xmlStrcat(msg, BAD_CAST " '");
17054 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet2->type));
17055 if (ofBase)
17056 msg = xmlStrcat(msg, BAD_CAST "' of the base type");
17057 else
17058 msg = xmlStrcat(msg, BAD_CAST "'");
17059
17060 xmlSchemaPCustomErr(pctxt,
17061 XML_SCHEMAP_INVALID_FACET_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017062 WXS_BASIC_CAST facet1, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017063 (const char *) msg, NULL);
17064
17065 if (msg != NULL)
17066 xmlFree(msg);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017067}
17068
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017069/*
17070* xmlSchemaDeriveAndValidateFacets:
17071*
17072* Schema Component Constraint: Simple Type Restriction (Facets)
17073* (st-restrict-facets)
17074*/
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017075static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017076xmlSchemaDeriveAndValidateFacets(xmlSchemaParserCtxtPtr pctxt,
17077 xmlSchemaTypePtr type)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017078{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017079 xmlSchemaTypePtr base = type->baseType;
17080 xmlSchemaFacetLinkPtr link, cur, last = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017081 xmlSchemaFacetPtr facet, bfacet,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017082 flength = NULL, ftotdig = NULL, ffracdig = NULL,
17083 fmaxlen = NULL, fminlen = NULL, /* facets of the current type */
17084 fmininc = NULL, fmaxinc = NULL,
17085 fminexc = NULL, fmaxexc = NULL,
17086 bflength = NULL, bftotdig = NULL, bffracdig = NULL,
17087 bfmaxlen = NULL, bfminlen = NULL, /* facets of the base type */
17088 bfmininc = NULL, bfmaxinc = NULL,
17089 bfminexc = NULL, bfmaxexc = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017090 int res; /* err = 0, fixedErr; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017091
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017092 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017093 * SPEC st-restrict-facets 1:
17094 * "The {variety} of R is the same as that of B."
17095 */
17096 /*
17097 * SPEC st-restrict-facets 2:
17098 * "If {variety} is atomic, the {primitive type definition}
17099 * of R is the same as that of B."
17100 *
17101 * NOTE: we leave 1 & 2 out for now, since this will be
17102 * satisfied by the derivation process.
17103 * CONSTRUCTION TODO: Maybe needed if using a construction API.
17104 */
17105 /*
17106 * SPEC st-restrict-facets 3:
17107 * "The {facets} of R are the union of S and the {facets}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017108 * of B, eliminating duplicates. To eliminate duplicates,
17109 * when a facet of the same kind occurs in both S and the
17110 * {facets} of B, the one in the {facets} of B is not
17111 * included, with the exception of enumeration and pattern
17112 * facets, for which multiple occurrences with distinct values
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017113 * are allowed."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017114 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017115
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017116 if ((type->facetSet == NULL) && (base->facetSet == NULL))
17117 return (0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017118
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017119 last = type->facetSet;
17120 if (last != NULL)
17121 while (last->next != NULL)
17122 last = last->next;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017123
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017124 for (cur = type->facetSet; cur != NULL; cur = cur->next) {
17125 facet = cur->facet;
17126 switch (facet->type) {
17127 case XML_SCHEMA_FACET_LENGTH:
17128 flength = facet; break;
17129 case XML_SCHEMA_FACET_MINLENGTH:
17130 fminlen = facet; break;
17131 case XML_SCHEMA_FACET_MININCLUSIVE:
17132 fmininc = facet; break;
17133 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17134 fminexc = facet; break;
17135 case XML_SCHEMA_FACET_MAXLENGTH:
17136 fmaxlen = facet; break;
17137 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17138 fmaxinc = facet; break;
17139 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17140 fmaxexc = facet; break;
17141 case XML_SCHEMA_FACET_TOTALDIGITS:
17142 ftotdig = facet; break;
17143 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17144 ffracdig = facet; break;
17145 default:
17146 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017147 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017148 }
17149 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17150 facet = cur->facet;
17151 switch (facet->type) {
17152 case XML_SCHEMA_FACET_LENGTH:
17153 bflength = facet; break;
17154 case XML_SCHEMA_FACET_MINLENGTH:
17155 bfminlen = facet; break;
17156 case XML_SCHEMA_FACET_MININCLUSIVE:
17157 bfmininc = facet; break;
17158 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17159 bfminexc = facet; break;
17160 case XML_SCHEMA_FACET_MAXLENGTH:
17161 bfmaxlen = facet; break;
17162 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17163 bfmaxinc = facet; break;
17164 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17165 bfmaxexc = facet; break;
17166 case XML_SCHEMA_FACET_TOTALDIGITS:
17167 bftotdig = facet; break;
17168 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17169 bffracdig = facet; break;
17170 default:
17171 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017172 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017173 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017174 /*
17175 * length and minLength or maxLength (2.2) + (3.2)
17176 */
17177 if (flength && (fminlen || fmaxlen)) {
17178 FACET_RESTR_ERR(flength, "It is an error for both 'length' and "
17179 "either of 'minLength' or 'maxLength' to be specified on "
17180 "the same type definition")
17181 }
17182 /*
17183 * Mutual exclusions in the same derivation step.
17184 */
17185 if ((fmaxinc) && (fmaxexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017186 /*
17187 * SCC "maxInclusive and maxExclusive"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017188 */
17189 FACET_RESTR_MUTUAL_ERR(fmaxinc, fmaxexc)
17190 }
17191 if ((fmininc) && (fminexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017192 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017193 * SCC "minInclusive and minExclusive"
17194 */
17195 FACET_RESTR_MUTUAL_ERR(fmininc, fminexc)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017196 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017197
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017198 if (flength && bflength) {
17199 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017200 * SCC "length valid restriction"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017201 * The values have to be equal.
17202 */
17203 res = xmlSchemaCompareValues(flength->val, bflength->val);
17204 if (res == -2)
17205 goto internal_error;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017206 if (res != 0)
17207 xmlSchemaDeriveFacetErr(pctxt, flength, bflength, 0, 0, 1);
17208 if ((res != 0) && (bflength->fixed)) {
17209 FACET_RESTR_FIXED_ERR(flength)
17210 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017211
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017212 }
17213 if (fminlen && bfminlen) {
17214 /*
17215 * SCC "minLength valid restriction"
17216 * minLength >= BASE minLength
17217 */
17218 res = xmlSchemaCompareValues(fminlen->val, bfminlen->val);
17219 if (res == -2)
17220 goto internal_error;
17221 if (res == -1)
17222 xmlSchemaDeriveFacetErr(pctxt, fminlen, bfminlen, 1, 1, 1);
17223 if ((res != 0) && (bfminlen->fixed)) {
17224 FACET_RESTR_FIXED_ERR(fminlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017225 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017226 }
17227 if (fmaxlen && bfmaxlen) {
17228 /*
17229 * SCC "maxLength valid restriction"
17230 * maxLength <= BASE minLength
17231 */
17232 res = xmlSchemaCompareValues(fmaxlen->val, bfmaxlen->val);
17233 if (res == -2)
17234 goto internal_error;
17235 if (res == 1)
17236 xmlSchemaDeriveFacetErr(pctxt, fmaxlen, bfmaxlen, -1, 1, 1);
17237 if ((res != 0) && (bfmaxlen->fixed)) {
17238 FACET_RESTR_FIXED_ERR(fmaxlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017239 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017240 }
17241 /*
17242 * SCC "length and minLength or maxLength"
17243 */
17244 if (! flength)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017245 flength = bflength;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017246 if (flength) {
17247 if (! fminlen)
17248 flength = bflength;
17249 if (fminlen) {
17250 /* (1.1) length >= minLength */
17251 res = xmlSchemaCompareValues(flength->val, fminlen->val);
17252 if (res == -2)
17253 goto internal_error;
17254 if (res == -1)
17255 xmlSchemaDeriveFacetErr(pctxt, flength, fminlen, 1, 1, 0);
17256 }
17257 if (! fmaxlen)
17258 fmaxlen = bfmaxlen;
17259 if (fmaxlen) {
17260 /* (2.1) length <= maxLength */
17261 res = xmlSchemaCompareValues(flength->val, fmaxlen->val);
17262 if (res == -2)
17263 goto internal_error;
17264 if (res == 1)
17265 xmlSchemaDeriveFacetErr(pctxt, flength, fmaxlen, -1, 1, 0);
17266 }
17267 }
17268 if (fmaxinc) {
17269 /*
17270 * "maxInclusive"
17271 */
17272 if (fmininc) {
17273 /* SCC "maxInclusive >= minInclusive" */
17274 res = xmlSchemaCompareValues(fmaxinc->val, fmininc->val);
17275 if (res == -2)
17276 goto internal_error;
17277 if (res == -1) {
17278 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, fmininc, 1, 1, 0);
17279 }
17280 }
17281 /*
17282 * SCC "maxInclusive valid restriction"
17283 */
17284 if (bfmaxinc) {
17285 /* maxInclusive <= BASE maxInclusive */
17286 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxinc->val);
17287 if (res == -2)
17288 goto internal_error;
17289 if (res == 1)
17290 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxinc, -1, 1, 1);
17291 if ((res != 0) && (bfmaxinc->fixed)) {
17292 FACET_RESTR_FIXED_ERR(fmaxinc)
17293 }
17294 }
17295 if (bfmaxexc) {
17296 /* maxInclusive < BASE maxExclusive */
17297 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxexc->val);
17298 if (res == -2)
17299 goto internal_error;
17300 if (res != -1) {
17301 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxexc, -1, 0, 1);
17302 }
17303 }
17304 if (bfmininc) {
17305 /* maxInclusive >= BASE minInclusive */
17306 res = xmlSchemaCompareValues(fmaxinc->val, bfmininc->val);
17307 if (res == -2)
17308 goto internal_error;
17309 if (res == -1) {
17310 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmininc, 1, 1, 1);
17311 }
17312 }
17313 if (bfminexc) {
17314 /* maxInclusive > BASE minExclusive */
17315 res = xmlSchemaCompareValues(fmaxinc->val, bfminexc->val);
17316 if (res == -2)
17317 goto internal_error;
17318 if (res != 1) {
17319 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfminexc, 1, 0, 1);
17320 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017321 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017322 }
17323 if (fmaxexc) {
17324 /*
17325 * "maxExclusive >= minExclusive"
17326 */
17327 if (fminexc) {
17328 res = xmlSchemaCompareValues(fmaxexc->val, fminexc->val);
17329 if (res == -2)
17330 goto internal_error;
17331 if (res == -1) {
17332 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, fminexc, 1, 1, 0);
17333 }
17334 }
17335 /*
17336 * "maxExclusive valid restriction"
17337 */
17338 if (bfmaxexc) {
17339 /* maxExclusive <= BASE maxExclusive */
17340 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxexc->val);
17341 if (res == -2)
17342 goto internal_error;
17343 if (res == 1) {
17344 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxexc, -1, 1, 1);
17345 }
17346 if ((res != 0) && (bfmaxexc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017347 FACET_RESTR_FIXED_ERR(fmaxexc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017348 }
17349 }
17350 if (bfmaxinc) {
17351 /* maxExclusive <= BASE maxInclusive */
17352 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxinc->val);
17353 if (res == -2)
17354 goto internal_error;
17355 if (res == 1) {
17356 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxinc, -1, 1, 1);
17357 }
17358 }
17359 if (bfmininc) {
17360 /* maxExclusive > BASE minInclusive */
17361 res = xmlSchemaCompareValues(fmaxexc->val, bfmininc->val);
17362 if (res == -2)
17363 goto internal_error;
17364 if (res != 1) {
17365 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmininc, 1, 0, 1);
17366 }
17367 }
17368 if (bfminexc) {
17369 /* maxExclusive > BASE minExclusive */
17370 res = xmlSchemaCompareValues(fmaxexc->val, bfminexc->val);
17371 if (res == -2)
17372 goto internal_error;
17373 if (res != 1) {
17374 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfminexc, 1, 0, 1);
17375 }
17376 }
17377 }
17378 if (fminexc) {
17379 /*
17380 * "minExclusive < maxInclusive"
17381 */
17382 if (fmaxinc) {
17383 res = xmlSchemaCompareValues(fminexc->val, fmaxinc->val);
17384 if (res == -2)
17385 goto internal_error;
17386 if (res != -1) {
17387 xmlSchemaDeriveFacetErr(pctxt, fminexc, fmaxinc, -1, 0, 0);
17388 }
17389 }
17390 /*
17391 * "minExclusive valid restriction"
17392 */
17393 if (bfminexc) {
17394 /* minExclusive >= BASE minExclusive */
17395 res = xmlSchemaCompareValues(fminexc->val, bfminexc->val);
17396 if (res == -2)
17397 goto internal_error;
17398 if (res == -1) {
17399 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfminexc, 1, 1, 1);
17400 }
17401 if ((res != 0) && (bfminexc->fixed)) {
17402 FACET_RESTR_FIXED_ERR(fminexc)
17403 }
17404 }
17405 if (bfmaxinc) {
17406 /* minExclusive <= BASE maxInclusive */
17407 res = xmlSchemaCompareValues(fminexc->val, bfmaxinc->val);
17408 if (res == -2)
17409 goto internal_error;
17410 if (res == 1) {
17411 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxinc, -1, 1, 1);
17412 }
17413 }
17414 if (bfmininc) {
17415 /* minExclusive >= BASE minInclusive */
17416 res = xmlSchemaCompareValues(fminexc->val, bfmininc->val);
17417 if (res == -2)
17418 goto internal_error;
17419 if (res == -1) {
17420 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmininc, 1, 1, 1);
17421 }
17422 }
17423 if (bfmaxexc) {
17424 /* minExclusive < BASE maxExclusive */
17425 res = xmlSchemaCompareValues(fminexc->val, bfmaxexc->val);
17426 if (res == -2)
17427 goto internal_error;
17428 if (res != -1) {
17429 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxexc, -1, 0, 1);
17430 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017431 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017432 }
17433 if (fmininc) {
17434 /*
17435 * "minInclusive < maxExclusive"
17436 */
17437 if (fmaxexc) {
17438 res = xmlSchemaCompareValues(fmininc->val, fmaxexc->val);
17439 if (res == -2)
17440 goto internal_error;
17441 if (res != -1) {
17442 xmlSchemaDeriveFacetErr(pctxt, fmininc, fmaxexc, -1, 0, 0);
17443 }
17444 }
17445 /*
17446 * "minExclusive valid restriction"
17447 */
17448 if (bfmininc) {
17449 /* minInclusive >= BASE minInclusive */
17450 res = xmlSchemaCompareValues(fmininc->val, bfmininc->val);
17451 if (res == -2)
17452 goto internal_error;
17453 if (res == -1) {
17454 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmininc, 1, 1, 1);
17455 }
17456 if ((res != 0) && (bfmininc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017457 FACET_RESTR_FIXED_ERR(fmininc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017458 }
17459 }
17460 if (bfmaxinc) {
17461 /* minInclusive <= BASE maxInclusive */
17462 res = xmlSchemaCompareValues(fmininc->val, bfmaxinc->val);
17463 if (res == -2)
17464 goto internal_error;
Daniel Veillard0a119eb2005-07-20 13:46:00 +000017465 if (res == 1) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017466 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxinc, -1, 1, 1);
17467 }
17468 }
17469 if (bfminexc) {
17470 /* minInclusive > BASE minExclusive */
17471 res = xmlSchemaCompareValues(fmininc->val, bfminexc->val);
17472 if (res == -2)
17473 goto internal_error;
17474 if (res != 1)
17475 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfminexc, 1, 0, 1);
17476 }
17477 if (bfmaxexc) {
17478 /* minInclusive < BASE maxExclusive */
17479 res = xmlSchemaCompareValues(fmininc->val, bfmaxexc->val);
17480 if (res == -2)
17481 goto internal_error;
17482 if (res != -1)
17483 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxexc, -1, 0, 1);
17484 }
17485 }
17486 if (ftotdig && bftotdig) {
17487 /*
17488 * SCC " totalDigits valid restriction"
17489 * totalDigits <= BASE totalDigits
17490 */
17491 res = xmlSchemaCompareValues(ftotdig->val, bftotdig->val);
17492 if (res == -2)
17493 goto internal_error;
17494 if (res == 1)
17495 xmlSchemaDeriveFacetErr(pctxt, ftotdig, bftotdig,
17496 -1, 1, 1);
17497 if ((res != 0) && (bftotdig->fixed)) {
17498 FACET_RESTR_FIXED_ERR(ftotdig)
17499 }
17500 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017501 if (ffracdig && bffracdig) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017502 /*
17503 * SCC "fractionDigits valid restriction"
17504 * fractionDigits <= BASE fractionDigits
17505 */
17506 res = xmlSchemaCompareValues(ffracdig->val, bffracdig->val);
17507 if (res == -2)
17508 goto internal_error;
17509 if (res == 1)
17510 xmlSchemaDeriveFacetErr(pctxt, ffracdig, bffracdig,
17511 -1, 1, 1);
17512 if ((res != 0) && (bffracdig->fixed)) {
17513 FACET_RESTR_FIXED_ERR(ffracdig)
17514 }
17515 }
17516 /*
17517 * SCC "fractionDigits less than or equal to totalDigits"
17518 */
17519 if (! ftotdig)
17520 ftotdig = bftotdig;
17521 if (! ffracdig)
17522 ffracdig = bffracdig;
17523 if (ftotdig && ffracdig) {
17524 res = xmlSchemaCompareValues(ffracdig->val, ftotdig->val);
17525 if (res == -2)
17526 goto internal_error;
17527 if (res == 1)
17528 xmlSchemaDeriveFacetErr(pctxt, ffracdig, ftotdig,
17529 -1, 1, 0);
17530 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017531 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017532 * *Enumerations* won' be added here, since only the first set
17533 * of enumerations in the ancestor-or-self axis is used
17534 * for validation, plus we need to use the base type of those
17535 * enumerations for whitespace.
17536 *
17537 * *Patterns*: won't be add here, since they are ORed at
17538 * type level and ANDed at ancestor level. This will
17539 * happed during validation by walking the base axis
17540 * of the type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017541 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017542 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17543 bfacet = cur->facet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017544 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017545 * Special handling of enumerations and patterns.
17546 * TODO: hmm, they should not appear in the set, so remove this.
17547 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017548 if ((bfacet->type == XML_SCHEMA_FACET_PATTERN) ||
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017549 (bfacet->type == XML_SCHEMA_FACET_ENUMERATION))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017550 continue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017551 /*
17552 * Search for a duplicate facet in the current type.
17553 */
17554 link = type->facetSet;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017555 /* err = 0; */
17556 /* fixedErr = 0; */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017557 while (link != NULL) {
17558 facet = link->facet;
17559 if (facet->type == bfacet->type) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017560 switch (facet->type) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017561 case XML_SCHEMA_FACET_WHITESPACE:
17562 /*
17563 * The whitespace must be stronger.
17564 */
17565 if (facet->whitespace < bfacet->whitespace) {
17566 FACET_RESTR_ERR(flength,
17567 "The 'whitespace' value has to be equal to "
17568 "or stronger than the 'whitespace' value of "
17569 "the base type")
17570 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017571 if ((bfacet->fixed) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017572 (facet->whitespace != bfacet->whitespace)) {
17573 FACET_RESTR_FIXED_ERR(facet)
17574 }
17575 break;
17576 default:
17577 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017578 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017579 /* Duplicate found. */
17580 break;
17581 }
17582 link = link->next;
17583 }
17584 /*
17585 * If no duplicate was found: add the base types's facet
17586 * to the set.
17587 */
17588 if (link == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017589 link = (xmlSchemaFacetLinkPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017590 xmlMalloc(sizeof(xmlSchemaFacetLink));
17591 if (link == NULL) {
17592 xmlSchemaPErrMemory(pctxt,
17593 "deriving facets, creating a facet link", NULL);
17594 return (-1);
17595 }
17596 link->facet = cur->facet;
17597 link->next = NULL;
17598 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017599 type->facetSet = link;
17600 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017601 last->next = link;
17602 last = link;
17603 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017604
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017605 }
17606
17607 return (0);
17608internal_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017609 PERROR_INT("xmlSchemaDeriveAndValidateFacets",
17610 "an error occured");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017611 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017612}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017613
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017614static int
17615xmlSchemaFinishMemberTypeDefinitionsProperty(xmlSchemaParserCtxtPtr pctxt,
17616 xmlSchemaTypePtr type)
17617{
17618 xmlSchemaTypeLinkPtr link, lastLink, prevLink, subLink, newLink;
17619 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017620 * The actual value is then formed by replacing any union type
17621 * definition in the ·explicit members· with the members of their
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017622 * {member type definitions}, in order.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017623 *
17624 * TODO: There's a bug entry at
17625 * "http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0287.html"
17626 * which indicates that we'll keep the union types the future.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017627 */
17628 link = type->memberTypes;
17629 while (link != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017630
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017631 if (WXS_IS_TYPE_NOT_FIXED(link->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000017632 xmlSchemaTypeFixup(link->type, ACTXT_CAST pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017633
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017634 if (WXS_IS_UNION(link->type)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017635 subLink = xmlSchemaGetUnionSimpleTypeMemberTypes(link->type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017636 if (subLink != NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017637 link->type = subLink->type;
17638 if (subLink->next != NULL) {
17639 lastLink = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017640 subLink = subLink->next;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017641 prevLink = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017642 while (subLink != NULL) {
17643 newLink = (xmlSchemaTypeLinkPtr)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017644 xmlMalloc(sizeof(xmlSchemaTypeLink));
17645 if (newLink == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017646 xmlSchemaPErrMemory(pctxt, "allocating a type link",
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017647 NULL);
17648 return (-1);
17649 }
17650 newLink->type = subLink->type;
17651 prevLink->next = newLink;
17652 prevLink = newLink;
17653 newLink->next = lastLink;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017654
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017655 subLink = subLink->next;
17656 }
17657 }
17658 }
17659 }
17660 link = link->next;
17661 }
17662 return (0);
17663}
17664
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017665static void
17666xmlSchemaTypeFixupOptimFacets(xmlSchemaTypePtr type)
17667{
17668 int has = 0, needVal = 0, normVal = 0;
17669
17670 has = (type->baseType->flags & XML_SCHEMAS_TYPE_HAS_FACETS) ? 1 : 0;
17671 if (has) {
17672 needVal = (type->baseType->flags &
17673 XML_SCHEMAS_TYPE_FACETSNEEDVALUE) ? 1 : 0;
17674 normVal = (type->baseType->flags &
17675 XML_SCHEMAS_TYPE_NORMVALUENEEDED) ? 1 : 0;
17676 }
17677 if (type->facets != NULL) {
17678 xmlSchemaFacetPtr fac;
17679
17680 for (fac = type->facets; fac != NULL; fac = fac->next) {
17681 switch (fac->type) {
17682 case XML_SCHEMA_FACET_WHITESPACE:
17683 break;
17684 case XML_SCHEMA_FACET_PATTERN:
17685 normVal = 1;
17686 has = 1;
17687 break;
17688 case XML_SCHEMA_FACET_ENUMERATION:
17689 needVal = 1;
17690 normVal = 1;
17691 has = 1;
17692 break;
17693 default:
17694 has = 1;
17695 break;
17696 }
17697 }
17698 }
17699 if (normVal)
17700 type->flags |= XML_SCHEMAS_TYPE_NORMVALUENEEDED;
17701 if (needVal)
17702 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17703 if (has)
17704 type->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
17705
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017706 if (has && (! needVal) && WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017707 xmlSchemaTypePtr prim = xmlSchemaGetPrimitiveType(type);
17708 /*
17709 * OPTIMIZE VAL TODO: Some facets need a computed value.
17710 */
17711 if ((prim->builtInType != XML_SCHEMAS_ANYSIMPLETYPE) &&
17712 (prim->builtInType != XML_SCHEMAS_STRING)) {
17713 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17714 }
17715 }
17716}
17717
17718static int
17719xmlSchemaTypeFixupWhitespace(xmlSchemaTypePtr type)
17720{
17721
17722
17723 /*
17724 * Evaluate the whitespace-facet value.
17725 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017726 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017727 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17728 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017729 } else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017730 return (0);
17731
17732 if (type->facetSet != NULL) {
17733 xmlSchemaFacetLinkPtr lin;
17734
17735 for (lin = type->facetSet; lin != NULL; lin = lin->next) {
17736 if (lin->facet->type == XML_SCHEMA_FACET_WHITESPACE) {
17737 switch (lin->facet->whitespace) {
17738 case XML_SCHEMAS_FACET_PRESERVE:
17739 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17740 break;
17741 case XML_SCHEMAS_FACET_REPLACE:
17742 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17743 break;
17744 case XML_SCHEMAS_FACET_COLLAPSE:
17745 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17746 break;
17747 default:
17748 return (-1);
17749 }
17750 return (0);
17751 }
17752 }
17753 }
17754 /*
17755 * For all ·atomic· datatypes other than string (and types ·derived·
17756 * by ·restriction· from it) the value of whiteSpace is fixed to
17757 * collapse
17758 */
17759 {
17760 xmlSchemaTypePtr anc;
17761
17762 for (anc = type->baseType; anc != NULL &&
17763 anc->builtInType != XML_SCHEMAS_ANYTYPE;
17764 anc = anc->baseType) {
17765
17766 if (anc->type == XML_SCHEMA_TYPE_BASIC) {
17767 if (anc->builtInType == XML_SCHEMAS_NORMSTRING) {
17768 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17769
17770 } else if ((anc->builtInType == XML_SCHEMAS_STRING) ||
17771 (anc->builtInType == XML_SCHEMAS_ANYSIMPLETYPE)) {
17772 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17773
17774 } else
17775 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17776 break;
17777 }
17778 }
17779 return (0);
17780 }
17781 return (0);
17782}
17783
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017784static int
17785xmlSchemaFixupSimpleTypeStageOne(xmlSchemaParserCtxtPtr pctxt,
17786 xmlSchemaTypePtr type)
Daniel Veillard4255d502002-04-16 15:50:10 +000017787{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017788 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17789 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017790 if (! WXS_IS_TYPE_NOT_FIXED_1(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017791 return(0);
17792 type->flags |= XML_SCHEMAS_TYPE_FIXUP_1;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017793
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017794 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017795 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017796 * Corresponds to <simpleType><list>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017797 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017798 if (type->subtypes == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017799 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017800 * This one is really needed, so get out.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017801 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017802 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017803 "list type has no item-type assigned");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017804 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017805 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017806 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017807 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017808 * Corresponds to <simpleType><union>...
17809 */
17810 if (type->memberTypes == NULL) {
17811 /*
17812 * This one is really needed, so get out.
17813 */
17814 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17815 "union type has no member-types assigned");
17816 return(-1);
17817 }
17818 } else {
17819 /*
17820 * Corresponds to <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017821 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017822 if (type->baseType == NULL) {
17823 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17824 "type has no base-type assigned");
17825 return(-1);
17826 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017827 if (WXS_IS_TYPE_NOT_FIXED_1(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017828 if (xmlSchemaFixupSimpleTypeStageOne(pctxt, type->baseType) == -1)
17829 return(-1);
17830 /*
17831 * Variety
17832 * If the <restriction> alternative is chosen, then the
17833 * {variety} of the {base type definition}.
17834 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017835 if (WXS_IS_ATOMIC(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017836 type->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017837 else if (WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017838 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017839 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017840 * Inherit the itemType.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017841 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017842 type->subtypes = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017843 } else if (WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017844 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017845 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017846 * NOTE that we won't assign the memberTypes of the base,
17847 * since this will make trouble when freeing them; we will
17848 * use a lookup function to access them instead.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017849 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017850 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017851 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017852 return(0);
17853}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017854
Daniel Veillard8651f532002-04-17 09:06:27 +000017855#ifdef DEBUG_TYPE
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017856xmlSchemaDebugFixedType(xmlSchemaParserCtxtPtr pctxt,
17857 xmlSchemaTypePtr type)
17858{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017859 if (type->node != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017860 xmlGenericError(xmlGenericErrorContext,
17861 "Type of %s : %s:%d :", name,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017862 type->node->doc->URL,
17863 xmlGetLineNo(type->node));
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017864 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017865 xmlGenericError(xmlGenericErrorContext, "Type of %s :", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017866 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017867 if ((WXS_IS_SIMPLE(type)) || (WXS_IS_COMPLEX(type))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017868 switch (type->contentType) {
17869 case XML_SCHEMA_CONTENT_SIMPLE:
17870 xmlGenericError(xmlGenericErrorContext, "simple\n");
17871 break;
17872 case XML_SCHEMA_CONTENT_ELEMENTS:
17873 xmlGenericError(xmlGenericErrorContext, "elements\n");
17874 break;
17875 case XML_SCHEMA_CONTENT_UNKNOWN:
17876 xmlGenericError(xmlGenericErrorContext, "unknown !!!\n");
17877 break;
17878 case XML_SCHEMA_CONTENT_EMPTY:
17879 xmlGenericError(xmlGenericErrorContext, "empty\n");
17880 break;
17881 case XML_SCHEMA_CONTENT_MIXED:
17882 if (xmlSchemaIsParticleEmptiable((xmlSchemaParticlePtr)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017883 type->subtypes))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017884 xmlGenericError(xmlGenericErrorContext,
17885 "mixed as emptiable particle\n");
17886 else
17887 xmlGenericError(xmlGenericErrorContext, "mixed\n");
17888 break;
17889 /* Removed, since not used. */
17890 /*
17891 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
17892 xmlGenericError(xmlGenericErrorContext, "mixed or elems\n");
17893 break;
17894 */
17895 case XML_SCHEMA_CONTENT_BASIC:
17896 xmlGenericError(xmlGenericErrorContext, "basic\n");
17897 break;
17898 default:
17899 xmlGenericError(xmlGenericErrorContext,
17900 "not registered !!!\n");
17901 break;
17902 }
Daniel Veillard8651f532002-04-17 09:06:27 +000017903 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017904}
Daniel Veillard8651f532002-04-17 09:06:27 +000017905#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017906
17907/*
17908* 3.14.6 Constraints on Simple Type Definition Schema Components
17909*/
17910static int
17911xmlSchemaFixupSimpleTypeStageTwo(xmlSchemaParserCtxtPtr pctxt,
17912 xmlSchemaTypePtr type)
17913{
17914 int res, olderrs = pctxt->nberrors;
17915
17916 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17917 return(-1);
17918
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017919 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017920 return(0);
17921
17922 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
17923 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
17924
17925 if (type->baseType == NULL) {
17926 PERROR_INT("xmlSchemaFixupSimpleTypeStageTwo",
17927 "missing baseType");
17928 goto exit_failure;
17929 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017930 if (WXS_IS_TYPE_NOT_FIXED(type->baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000017931 xmlSchemaTypeFixup(type->baseType, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017932 /*
17933 * If a member type of a union is a union itself, we need to substitute
17934 * that member type for its member types.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017935 * NOTE that this might change in WXS 1.1; i.e. we will keep the union
17936 * types in WXS 1.1.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017937 */
17938 if ((type->memberTypes != NULL) &&
17939 (xmlSchemaFinishMemberTypeDefinitionsProperty(pctxt, type) == -1))
17940 return(-1);
17941 /*
17942 * SPEC src-simple-type 1
17943 * "The corresponding simple type definition, if any, must satisfy
17944 * the conditions set out in Constraints on Simple Type Definition
17945 * Schema Components (§3.14.6)."
17946 */
17947 /*
17948 * Schema Component Constraint: Simple Type Definition Properties Correct
17949 * (st-props-correct)
17950 */
17951 res = xmlSchemaCheckSTPropsCorrect(pctxt, type);
17952 HFAILURE HERROR
17953 /*
17954 * Schema Component Constraint: Derivation Valid (Restriction, Simple)
17955 * (cos-st-restricts)
17956 */
17957 res = xmlSchemaCheckCOSSTRestricts(pctxt, type);
17958 HFAILURE HERROR
17959 /*
17960 * TODO: Removed the error report, since it got annoying to get an
17961 * extra error report, if anything failed until now.
17962 * Enable this if needed.
17963 *
17964 * xmlSchemaPErr(ctxt, type->node,
17965 * XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
17966 * "Simple type '%s' does not satisfy the constraints "
17967 * "on simple type definitions.\n",
17968 * type->name, NULL);
17969 */
17970 /*
17971 * Schema Component Constraint: Simple Type Restriction (Facets)
17972 * (st-restrict-facets)
17973 */
17974 res = xmlSchemaCheckFacetValues(type, pctxt);
17975 HFAILURE HERROR
17976 if ((type->facetSet != NULL) ||
17977 (type->baseType->facetSet != NULL)) {
17978 res = xmlSchemaDeriveAndValidateFacets(pctxt, type);
17979 HFAILURE HERROR
17980 }
17981 /*
17982 * Whitespace value.
17983 */
17984 res = xmlSchemaTypeFixupWhitespace(type);
17985 HFAILURE HERROR
17986 xmlSchemaTypeFixupOptimFacets(type);
17987
17988exit_error:
17989#ifdef DEBUG_TYPE
17990 xmlSchemaDebugFixedType(pctxt, type);
17991#endif
17992 if (olderrs != pctxt->nberrors)
17993 return(pctxt->err);
17994 return(0);
17995
17996exit_failure:
17997#ifdef DEBUG_TYPE
17998 xmlSchemaDebugFixedType(pctxt, type);
17999#endif
18000 return(-1);
18001}
18002
18003static int
18004xmlSchemaFixupComplexType(xmlSchemaParserCtxtPtr pctxt,
18005 xmlSchemaTypePtr type)
18006{
18007 int res = 0, olderrs = pctxt->nberrors;
18008 xmlSchemaTypePtr baseType = type->baseType;
18009
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018010 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018011 return(0);
18012 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
18013 if (baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018014 PERROR_INT("xmlSchemaFixupComplexType",
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018015 "missing baseType");
18016 goto exit_failure;
18017 }
18018 /*
18019 * Fixup the base type.
18020 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018021 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018022 xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018023 if (baseType->flags & XML_SCHEMAS_TYPE_INTERNAL_INVALID) {
18024 /*
18025 * Skip fixup if the base type is invalid.
18026 * TODO: Generate a warning!
18027 */
18028 return(0);
18029 }
18030 /*
18031 * This basically checks if the base type can be derived.
18032 */
18033 res = xmlSchemaCheckSRCCT(pctxt, type);
18034 HFAILURE HERROR
18035 /*
18036 * Fixup the content type.
18037 */
18038 if (type->contentType == XML_SCHEMA_CONTENT_SIMPLE) {
18039 /*
18040 * Corresponds to <complexType><simpleContent>...
18041 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018042 if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018043 (baseType->contentTypeDef != NULL) &&
18044 (WXS_IS_RESTRICTION(type))) {
18045 xmlSchemaTypePtr contentBase, content;
18046#ifdef ENABLE_NAMED_LOCALS
18047 char buf[30];
18048 const xmlChar *tmpname;
18049#endif
18050 /*
18051 * SPEC (1) If <restriction> + base type is <complexType>,
18052 * "whose own {content type} is a simple type..."
18053 */
18054 if (type->contentTypeDef != NULL) {
18055 /*
18056 * SPEC (1.1) "the simple type definition corresponding to the
18057 * <simpleType> among the [children] of <restriction> if there
18058 * is one;"
18059 * Note that this "<simpleType> among the [children]" was put
18060 * into ->contentTypeDef during parsing.
18061 */
18062 contentBase = type->contentTypeDef;
18063 type->contentTypeDef = NULL;
18064 } else {
18065 /*
18066 * (1.2) "...otherwise (<restriction> has no <simpleType>
18067 * among its [children]), the simple type definition which
18068 * is the {content type} of the ... base type."
18069 */
18070 contentBase = baseType->contentTypeDef;
18071 }
18072 /*
18073 * SPEC
18074 * "... a simple type definition which restricts the simple
18075 * type definition identified in clause 1.1 or clause 1.2
18076 * with a set of facet components"
18077 *
18078 * Create the anonymous simple type, which will be the content
18079 * type of the complex type.
18080 */
18081#ifdef ENABLE_NAMED_LOCALS
18082 snprintf(buf, 29, "#scST%d", ++(pctxt->counter));
18083 tmpname = xmlDictLookup(pctxt->dict, BAD_CAST buf, -1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018084 content = xmlSchemaAddType(pctxt, pctxt->schema,
18085 XML_SCHEMA_TYPE_SIMPLE, tmpname, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018086 type->node, 0);
18087#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018088 content = xmlSchemaAddType(pctxt, pctxt->schema,
18089 XML_SCHEMA_TYPE_SIMPLE, NULL, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018090 type->node, 0);
18091#endif
18092 if (content == NULL)
18093 goto exit_failure;
18094 /*
18095 * We will use the same node as for the <complexType>
18096 * to have it somehow anchored in the schema doc.
18097 */
18098 content->type = XML_SCHEMA_TYPE_SIMPLE;
18099 content->baseType = contentBase;
18100 /*
18101 * Move the facets, previously anchored on the
18102 * complexType during parsing.
18103 */
18104 content->facets = type->facets;
18105 type->facets = NULL;
18106 content->facetSet = type->facetSet;
18107 type->facetSet = NULL;
18108
18109 type->contentTypeDef = content;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018110 if (WXS_IS_TYPE_NOT_FIXED(contentBase))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018111 xmlSchemaTypeFixup(contentBase, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018112 /*
18113 * Fixup the newly created type. We don't need to check
18114 * for circularity here.
18115 */
18116 res = xmlSchemaFixupSimpleTypeStageOne(pctxt, content);
18117 HFAILURE HERROR
18118 res = xmlSchemaFixupSimpleTypeStageTwo(pctxt, content);
18119 HFAILURE HERROR
18120
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018121 } else if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018122 (baseType->contentType == XML_SCHEMA_CONTENT_MIXED) &&
18123 (WXS_IS_RESTRICTION(type))) {
18124 /*
18125 * SPEC (2) If <restriction> + base is a mixed <complexType> with
18126 * an emptiable particle, then a simple type definition which
18127 * restricts the <restriction>'s <simpleType> child.
18128 */
18129 if ((type->contentTypeDef == NULL) ||
18130 (type->contentTypeDef->baseType == NULL)) {
18131 /*
18132 * TODO: Check if this ever happens.
18133 */
18134 xmlSchemaPCustomErr(pctxt,
18135 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018136 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018137 "Internal error: xmlSchemaTypeFixup, "
18138 "complex type '%s': the <simpleContent><restriction> "
18139 "is missing a <simpleType> child, but was not catched "
18140 "by xmlSchemaCheckSRCCT()", type->name);
18141 goto exit_failure;
18142 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018143 } else if ((WXS_IS_COMPLEX(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018144 /*
18145 * SPEC (3) If <extension> + base is <complexType> with
18146 * <simpleType> content, "...then the {content type} of that
18147 * complex type definition"
18148 */
18149 if (baseType->contentTypeDef == NULL) {
18150 /*
18151 * TODO: Check if this ever happens. xmlSchemaCheckSRCCT
18152 * should have catched this already.
18153 */
18154 xmlSchemaPCustomErr(pctxt,
18155 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018156 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018157 "Internal error: xmlSchemaTypeFixup, "
18158 "complex type '%s': the <extension>ed base type is "
18159 "a complex type with no simple content type",
18160 type->name);
18161 goto exit_failure;
18162 }
18163 type->contentTypeDef = baseType->contentTypeDef;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018164 } else if ((WXS_IS_SIMPLE(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018165 /*
18166 * SPEC (4) <extension> + base is <simpleType>
18167 * "... then that simple type definition"
18168 */
18169 type->contentTypeDef = baseType;
18170 } else {
18171 /*
18172 * TODO: Check if this ever happens.
18173 */
18174 xmlSchemaPCustomErr(pctxt,
18175 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018176 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018177 "Internal error: xmlSchemaTypeFixup, "
18178 "complex type '%s' with <simpleContent>: unhandled "
18179 "derivation case", type->name);
18180 goto exit_failure;
18181 }
18182 } else {
18183 int dummySequence = 0;
18184 xmlSchemaParticlePtr particle =
18185 (xmlSchemaParticlePtr) type->subtypes;
18186 /*
18187 * Corresponds to <complexType><complexContent>...
18188 *
18189 * NOTE that the effective mixed was already set during parsing of
18190 * <complexType> and <complexContent>; its flag value is
18191 * XML_SCHEMAS_TYPE_MIXED.
18192 *
18193 * Compute the "effective content":
18194 * (2.1.1) + (2.1.2) + (2.1.3)
18195 */
18196 if ((particle == NULL) ||
18197 ((particle->type == XML_SCHEMA_TYPE_PARTICLE) &&
18198 ((particle->children->type == XML_SCHEMA_TYPE_ALL) ||
18199 (particle->children->type == XML_SCHEMA_TYPE_SEQUENCE) ||
18200 ((particle->children->type == XML_SCHEMA_TYPE_CHOICE) &&
18201 (particle->minOccurs == 0))) &&
18202 ( ((xmlSchemaTreeItemPtr) particle->children)->children == NULL))) {
18203 if (type->flags & XML_SCHEMAS_TYPE_MIXED) {
18204 /*
18205 * SPEC (2.1.4) "If the ·effective mixed· is true, then
18206 * a particle whose properties are as follows:..."
18207 *
18208 * Empty sequence model group with
18209 * minOccurs/maxOccurs = 1 (i.e. a "particle emptiable").
18210 * NOTE that we sill assign it the <complexType> node to
18211 * somehow anchor it in the doc.
18212 */
18213 if ((particle == NULL) ||
18214 (particle->children->type != XML_SCHEMA_TYPE_SEQUENCE)) {
18215 /*
18216 * Create the particle.
18217 */
18218 particle = xmlSchemaAddParticle(pctxt, pctxt->schema,
18219 type->node, 1, 1);
18220 if (particle == NULL)
18221 goto exit_failure;
18222 /*
18223 * Create the model group.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018224 */ /* URGENT TODO: avoid adding to pending items. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018225 particle->children = (xmlSchemaTreeItemPtr)
18226 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18227 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18228 if (particle->children == NULL)
18229 goto exit_failure;
18230
18231 type->subtypes = (xmlSchemaTypePtr) particle;
18232 }
18233 dummySequence = 1;
18234 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18235 } else {
18236 /*
18237 * SPEC (2.1.5) "otherwise empty"
18238 */
18239 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
18240 }
18241 } else {
18242 /*
18243 * SPEC (2.2) "otherwise the particle corresponding to the
18244 * <all>, <choice>, <group> or <sequence> among the
18245 * [children]."
18246 */
18247 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18248 }
18249 /*
18250 * Compute the "content type".
18251 */
18252 if (WXS_IS_RESTRICTION(type)) {
18253 /*
18254 * SPEC (3.1) "If <restriction>..."
18255 * (3.1.1) + (3.1.2) */
18256 if (type->contentType != XML_SCHEMA_CONTENT_EMPTY) {
18257 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18258 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18259 }
18260 } else {
18261 /*
18262 * SPEC (3.2) "If <extension>..."
18263 */
18264 if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18265 /*
18266 * SPEC (3.2.1)
18267 */
18268 type->contentType = baseType->contentType;
18269 type->subtypes = baseType->subtypes;
18270 /*
18271 * NOTE that the effective mixed is ignored here.
18272 */
18273 } else if (baseType->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18274 /*
18275 * SPEC (3.2.2)
18276 */
18277 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18278 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18279 } else {
18280 /*
18281 * SPEC (3.2.3)
18282 */
18283 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18284 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18285 /*
18286 * "A model group whose {compositor} is sequence and whose
18287 * {particles} are..."
18288 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018289 if ((WXS_TYPE_PARTICLE(type) != NULL) &&
18290 (WXS_TYPE_PARTICLE_TERM(type) != NULL) &&
18291 ((WXS_TYPE_PARTICLE_TERM(type))->type ==
18292 XML_SCHEMA_TYPE_ALL))
18293 {
18294 /*
18295 * SPEC cos-all-limited (1)
18296 */
18297 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18298 /* TODO: error code */
18299 XML_SCHEMAP_COS_ALL_LIMITED,
18300 WXS_ITEM_NODE(type), NULL,
18301 "The type has an 'all' model group in its "
18302 "{content type} and thus cannot be derived from "
18303 "a non-empty type, since this would produce a "
18304 "'sequence' model group containing the 'all' "
18305 "model group; 'all' model groups are not "
18306 "allowed to appear inside other model groups",
18307 NULL, NULL);
18308
18309 } else if ((WXS_TYPE_PARTICLE(baseType) != NULL) &&
18310 (WXS_TYPE_PARTICLE_TERM(baseType) != NULL) &&
18311 ((WXS_TYPE_PARTICLE_TERM(baseType))->type ==
18312 XML_SCHEMA_TYPE_ALL))
18313 {
18314 /*
18315 * SPEC cos-all-limited (1)
18316 */
18317 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18318 /* TODO: error code */
18319 XML_SCHEMAP_COS_ALL_LIMITED,
18320 WXS_ITEM_NODE(type), NULL,
18321 "A type cannot be derived by extension from a type "
18322 "which has an 'all' model group in its "
18323 "{content type}, since this would produce a "
18324 "'sequence' model group containing the 'all' "
18325 "model group; 'all' model groups are not "
18326 "allowed to appear inside other model groups",
18327 NULL, NULL);
18328
18329 } else if (! dummySequence) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018330 xmlSchemaTreeItemPtr effectiveContent =
18331 (xmlSchemaTreeItemPtr) type->subtypes;
18332 /*
18333 * Create the particle.
18334 */
18335 particle = xmlSchemaAddParticle(pctxt, pctxt->schema,
18336 type->node, 1, 1);
18337 if (particle == NULL)
18338 goto exit_failure;
18339 /*
18340 * Create the "sequence" model group.
18341 */
18342 particle->children = (xmlSchemaTreeItemPtr)
18343 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18344 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18345 if (particle->children == NULL)
18346 goto exit_failure;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018347 WXS_TYPE_CONTENTTYPE(type) = (xmlSchemaTypePtr) particle;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018348 /*
18349 * SPEC "the particle of the {content type} of
18350 * the ... base ..."
18351 * Create a duplicate of the base type's particle
18352 * and assign its "term" to it.
18353 */
18354 particle->children->children =
18355 (xmlSchemaTreeItemPtr) xmlSchemaAddParticle(pctxt,
18356 pctxt->schema, type->node,
18357 ((xmlSchemaParticlePtr) type->subtypes)->minOccurs,
18358 ((xmlSchemaParticlePtr) type->subtypes)->maxOccurs);
18359 if (particle->children->children == NULL)
18360 goto exit_failure;
18361 particle = (xmlSchemaParticlePtr)
18362 particle->children->children;
18363 particle->children =
18364 ((xmlSchemaParticlePtr) baseType->subtypes)->children;
18365 /*
18366 * SPEC "followed by the ·effective content·."
18367 */
18368 particle->next = effectiveContent;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018369 /*
18370 * This all will result in:
18371 * new-particle
18372 * --> new-sequence(
18373 * new-particle
18374 * --> base-model,
18375 * this-particle
18376 * --> this-model
18377 * )
18378 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018379 } else {
18380 /*
18381 * This is the case when there is already an empty
18382 * <sequence> with minOccurs==maxOccurs==1.
18383 * Just add the base types's content type.
18384 * NOTE that, although we miss to add an intermediate
18385 * <sequence>, this should produce no difference to
18386 * neither the regex compilation of the content model,
18387 * nor to the complex type contraints.
18388 */
18389 particle->children->children =
18390 (xmlSchemaTreeItemPtr) baseType->subtypes;
18391 }
18392 }
18393 }
18394 }
18395 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018396 * Now fixup attribute uses:
18397 * - expand attr. group references
18398 * - intersect attribute wildcards
18399 * - inherit attribute uses of the base type
18400 * - inherit or union attr. wildcards if extending
18401 * - apply attr. use prohibitions if restricting
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018402 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018403 res = xmlSchemaFixupTypeAttributeUses(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018404 HFAILURE HERROR
18405 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018406 * Apply the complex type component constraints; this will not
18407 * check attributes, since this is done in
18408 * xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018409 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018410 res = xmlSchemaCheckCTComponent(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018411 HFAILURE HERROR
18412
18413#ifdef DEBUG_TYPE
18414 xmlSchemaDebugFixedType(pctxt, type);
18415#endif
18416 if (olderrs != pctxt->nberrors)
18417 return(pctxt->err);
18418 else
18419 return(0);
18420
18421exit_error:
18422 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18423#ifdef DEBUG_TYPE
18424 xmlSchemaDebugFixedType(pctxt, type);
18425#endif
18426 return(pctxt->err);
18427
18428exit_failure:
18429 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18430#ifdef DEBUG_TYPE
18431 xmlSchemaDebugFixedType(pctxt, type);
18432#endif
18433 return(-1);
18434}
18435
18436
18437/**
18438 * xmlSchemaTypeFixup:
18439 * @typeDecl: the schema type definition
18440 * @ctxt: the schema parser context
18441 *
18442 * Fixes the content model of the type.
18443 * URGENT TODO: We need an int result!
18444 */
18445static int
18446xmlSchemaTypeFixup(xmlSchemaTypePtr type,
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018447 xmlSchemaAbstractCtxtPtr actxt)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018448{
18449 if (type == NULL)
18450 return(0);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018451 if (actxt->type != XML_SCHEMA_CTXT_PARSER) {
18452 AERROR_INT("xmlSchemaTypeFixup",
18453 "this function needs a parser context");
18454 return(-1);
18455 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018456 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018457 return(0);
18458 if (type->type == XML_SCHEMA_TYPE_COMPLEX)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018459 return(xmlSchemaFixupComplexType(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018460 else if (type->type == XML_SCHEMA_TYPE_SIMPLE)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018461 return(xmlSchemaFixupSimpleTypeStageTwo(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018462 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000018463}
18464
18465/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018466 * xmlSchemaCheckFacet:
18467 * @facet: the facet
18468 * @typeDecl: the schema type definition
Daniel Veillard81562d22005-06-15 13:27:56 +000018469 * @pctxt: the schema parser context or NULL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018470 * @name: the optional name of the type
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018471 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018472 * Checks and computes the values of facets.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018473 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018474 * Returns 0 if valid, a positive error code if not valid and
18475 * -1 in case of an internal or API error.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018476 */
18477int
18478xmlSchemaCheckFacet(xmlSchemaFacetPtr facet,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018479 xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018480 xmlSchemaParserCtxtPtr pctxt,
18481 const xmlChar * name ATTRIBUTE_UNUSED)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018482{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018483 int ret = 0, ctxtGiven;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018484
Daniel Veillardce682bc2004-11-05 17:22:25 +000018485 if ((facet == NULL) || (typeDecl == NULL))
18486 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018487 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018488 * TODO: will the parser context be given if used from
18489 * the relaxNG module?
18490 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018491 if (pctxt == NULL)
18492 ctxtGiven = 0;
18493 else
18494 ctxtGiven = 1;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018495
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018496 switch (facet->type) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018497 case XML_SCHEMA_FACET_MININCLUSIVE:
18498 case XML_SCHEMA_FACET_MINEXCLUSIVE:
18499 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018500 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
18501 case XML_SCHEMA_FACET_ENUMERATION: {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018502 /*
18503 * Okay we need to validate the value
18504 * at that point.
18505 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000018506 xmlSchemaTypePtr base;
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018507
18508 /* 4.3.5.5 Constraints on enumeration Schema Components
18509 * Schema Component Constraint: enumeration valid restriction
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018510 * It is an ·error· if any member of {value} is not in the
18511 * ·value space· of {base type definition}.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018512 *
18513 * minInclusive, maxInclusive, minExclusive, maxExclusive:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018514 * The value ·must· be in the
18515 * ·value space· of the ·base type·.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018516 */
18517 /*
18518 * This function is intended to deliver a compiled value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018519 * on the facet. In this implementation of XML Schemata the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018520 * type holding a facet, won't be a built-in type.
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018521 * Thus to ensure that other API
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018522 * calls (relaxng) do work, if the given type is a built-in
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018523 * type, we will assume that the given built-in type *is
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018524 * already* the base type.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018525 */
18526 if (typeDecl->type != XML_SCHEMA_TYPE_BASIC) {
18527 base = typeDecl->baseType;
18528 if (base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018529 PERROR_INT("xmlSchemaCheckFacet",
18530 "a type user derived type has no base type");
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018531 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018532 }
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018533 } else
18534 base = typeDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018535
18536 if (! ctxtGiven) {
18537 /*
18538 * A context is needed if called from RelaxNG.
18539 */
18540 pctxt = xmlSchemaNewParserCtxt("*");
18541 if (pctxt == NULL)
18542 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000018543 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018544 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018545 * NOTE: This call does not check the content nodes,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018546 * since they are not available:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018547 * facet->node is just the node holding the facet
18548 * definition, *not* the attribute holding the *value*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018549 * of the facet.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018550 */
18551 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018552 ACTXT_CAST pctxt, facet->node, base,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018553 facet->value, &(facet->val), 1, 1, 0);
18554 if (ret != 0) {
18555 if (ret < 0) {
18556 /* No error message for RelaxNG. */
18557 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018558 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018559 XML_SCHEMAP_INTERNAL, facet->node, NULL,
18560 "Internal error: xmlSchemaCheckFacet, "
18561 "failed to validate the value '%s' of the "
18562 "facet '%s' against the base type",
18563 facet->value, xmlSchemaFacetTypeToString(facet->type));
18564 }
18565 goto internal_error;
18566 }
18567 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18568 /* No error message for RelaxNG. */
18569 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018570 xmlChar *str = NULL;
18571
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018572 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018573 ret, facet->node, WXS_BASIC_CAST facet,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018574 "The value '%s' of the facet does not validate "
18575 "against the base type '%s'",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018576 facet->value,
18577 xmlSchemaFormatQName(&str,
18578 base->targetNamespace, base->name));
18579 FREE_AND_NULL(str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018580 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018581 goto exit;
18582 } else if (facet->val == NULL) {
18583 if (ctxtGiven) {
18584 PERROR_INT("xmlSchemaCheckFacet",
18585 "value was not computed");
18586 }
18587 TODO
18588 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018589 break;
18590 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018591 case XML_SCHEMA_FACET_PATTERN:
18592 facet->regexp = xmlRegexpCompile(facet->value);
18593 if (facet->regexp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018594 ret = XML_SCHEMAP_REGEXP_INVALID;
18595 /* No error message for RelaxNG. */
18596 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018597 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018598 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018599 "The value '%s' of the facet 'pattern' is not a "
18600 "valid regular expression",
18601 facet->value, NULL);
18602 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018603 }
18604 break;
18605 case XML_SCHEMA_FACET_TOTALDIGITS:
18606 case XML_SCHEMA_FACET_FRACTIONDIGITS:
18607 case XML_SCHEMA_FACET_LENGTH:
18608 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018609 case XML_SCHEMA_FACET_MINLENGTH:
18610
18611 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
18612 ret = xmlSchemaValidatePredefinedType(
18613 xmlSchemaGetBuiltInType(XML_SCHEMAS_PINTEGER),
18614 facet->value, &(facet->val));
18615 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018616 ret = xmlSchemaValidatePredefinedType(
18617 xmlSchemaGetBuiltInType(XML_SCHEMAS_NNINTEGER),
18618 facet->value, &(facet->val));
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018619 }
18620 if (ret != 0) {
18621 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018622 /* No error message for RelaxNG. */
18623 if (ctxtGiven) {
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018624 PERROR_INT("xmlSchemaCheckFacet",
18625 "validating facet value");
18626 }
18627 goto internal_error;
18628 }
18629 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18630 /* No error message for RelaxNG. */
18631 if (ctxtGiven) {
18632 /* error code */
18633 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
18634 ret, facet->node, WXS_BASIC_CAST typeDecl,
18635 "The value '%s' of the facet '%s' is not a valid '%s'",
18636 facet->value,
18637 xmlSchemaFacetTypeToString(facet->type),
18638 (facet->type != XML_SCHEMA_FACET_TOTALDIGITS) ?
18639 BAD_CAST "nonNegativeInteger" :
18640 BAD_CAST "positiveInteger",
18641 NULL);
18642 }
18643 }
18644 break;
18645
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018646 case XML_SCHEMA_FACET_WHITESPACE:{
18647 if (xmlStrEqual(facet->value, BAD_CAST "preserve")) {
18648 facet->whitespace = XML_SCHEMAS_FACET_PRESERVE;
18649 } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) {
18650 facet->whitespace = XML_SCHEMAS_FACET_REPLACE;
18651 } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) {
18652 facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE;
18653 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018654 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18655 /* No error message for RelaxNG. */
18656 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018657 /* error was previously: XML_SCHEMAP_INVALID_WHITE_SPACE */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018658 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018659 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018660 "The value '%s' of the facet 'whitespace' is not "
18661 "valid", facet->value, NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018662 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018663 }
18664 }
18665 default:
18666 break;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018667 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018668exit:
18669 if ((! ctxtGiven) && (pctxt != NULL))
18670 xmlSchemaFreeParserCtxt(pctxt);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018671 return (ret);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018672internal_error:
18673 if ((! ctxtGiven) && (pctxt != NULL))
18674 xmlSchemaFreeParserCtxt(pctxt);
18675 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018676}
18677
18678/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018679 * xmlSchemaCheckFacetValues:
Daniel Veillard4255d502002-04-16 15:50:10 +000018680 * @typeDecl: the schema type definition
18681 * @ctxt: the schema parser context
18682 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018683 * Checks the default values types, especially for facets
Daniel Veillard4255d502002-04-16 15:50:10 +000018684 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018685static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018686xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018687 xmlSchemaParserCtxtPtr pctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000018688{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018689 int res, olderrs = pctxt->nberrors;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018690 const xmlChar *name = typeDecl->name;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018691 /*
18692 * NOTE: It is intended to use the facets list, instead
18693 * of facetSet.
18694 */
18695 if (typeDecl->facets != NULL) {
18696 xmlSchemaFacetPtr facet = typeDecl->facets;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018697
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018698 /*
18699 * Temporarily assign the "schema" to the validation context
18700 * of the parser context. This is needed for NOTATION validation.
18701 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018702 if (pctxt->vctxt == NULL) {
18703 if (xmlSchemaCreateVCtxtOnPCtxt(pctxt) == -1)
18704 return(-1);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018705 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018706 pctxt->vctxt->schema = pctxt->schema;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018707 while (facet != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018708 res = xmlSchemaCheckFacet(facet, typeDecl, pctxt, name);
18709 HFAILURE
Daniel Veillard01fa6152004-06-29 17:04:39 +000018710 facet = facet->next;
18711 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018712 pctxt->vctxt->schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018713 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018714 if (olderrs != pctxt->nberrors)
18715 return(pctxt->err);
18716 return(0);
18717exit_failure:
18718 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000018719}
18720
18721/**
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018722 * xmlSchemaGetCircModelGrDefRef:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018723 * @ctxtMGroup: the searched model group
18724 * @selfMGroup: the second searched model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018725 * @particle: the first particle
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018726 *
18727 * This one is intended to be used by
18728 * xmlSchemaCheckGroupDefCircular only.
18729 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018730 * Returns the particle with the circular model group definition reference,
18731 * otherwise NULL.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018732 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018733static xmlSchemaTreeItemPtr
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018734xmlSchemaGetCircModelGrDefRef(xmlSchemaModelGroupDefPtr groupDef,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018735 xmlSchemaTreeItemPtr particle)
18736{
18737 xmlSchemaTreeItemPtr circ = NULL;
18738 xmlSchemaTreeItemPtr term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018739 xmlSchemaModelGroupDefPtr gdef;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018740
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018741 for (; particle != NULL; particle = particle->next) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018742 term = particle->children;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018743 if (term == NULL)
18744 continue;
18745 switch (term->type) {
18746 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018747 gdef = (xmlSchemaModelGroupDefPtr) term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018748 if (gdef == groupDef)
18749 return (particle);
18750 /*
18751 * Mark this model group definition to avoid infinite
18752 * recursion on circular references not yet examined.
18753 */
18754 if (gdef->flags & XML_SCHEMA_MODEL_GROUP_DEF_MARKED)
18755 continue;
18756 if (gdef->children != NULL) {
18757 gdef->flags |= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18758 circ = xmlSchemaGetCircModelGrDefRef(groupDef,
18759 gdef->children->children);
18760 gdef->flags ^= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18761 if (circ != NULL)
18762 return (circ);
18763 }
18764 break;
18765 case XML_SCHEMA_TYPE_SEQUENCE:
18766 case XML_SCHEMA_TYPE_CHOICE:
18767 case XML_SCHEMA_TYPE_ALL:
18768 circ = xmlSchemaGetCircModelGrDefRef(groupDef, term->children);
18769 if (circ != NULL)
18770 return (circ);
18771 break;
18772 default:
18773 break;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018774 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018775 }
18776 return (NULL);
18777}
18778
18779/**
18780 * xmlSchemaCheckGroupDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018781 * @item: the model group definition
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018782 * @ctxt: the parser context
18783 * @name: the name
18784 *
18785 * Checks for circular references to model group definitions.
18786 */
18787static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018788xmlSchemaCheckGroupDefCircular(xmlSchemaModelGroupDefPtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018789 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018790{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018791 /*
18792 * Schema Component Constraint: Model Group Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018793 * 2 Circular groups are disallowed. That is, within the {particles}
18794 * of a group there must not be at any depth a particle whose {term}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018795 * is the group itself.
18796 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018797 if ((item == NULL) ||
18798 (item->type != XML_SCHEMA_TYPE_GROUP) ||
18799 (item->children == NULL))
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018800 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018801 {
18802 xmlSchemaTreeItemPtr circ;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018803
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018804 circ = xmlSchemaGetCircModelGrDefRef(item, item->children->children);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018805 if (circ != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018806 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018807 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018808 * TODO: The error report is not adequate: this constraint
18809 * is defined for model groups but not definitions, but since
18810 * there cannot be any circular model groups without a model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018811 * definition (if not using a construction API), we check those
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018812 * defintions only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018813 */
18814 xmlSchemaPCustomErr(ctxt,
18815 XML_SCHEMAP_MG_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018816 NULL, WXS_ITEM_NODE(circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018817 "Circular reference to the model group definition '%s' "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018818 "defined", xmlSchemaFormatQName(&str,
18819 item->targetNamespace, item->name));
18820 FREE_AND_NULL(str)
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018821 /*
18822 * NOTE: We will cut the reference to avoid further
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018823 * confusion of the processor. This is a fatal error.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018824 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018825 circ->children = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018826 }
18827 }
18828}
18829
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018830/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018831 * xmlSchemaModelGroupToModelGroupDefFixup:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018832 * @ctxt: the parser context
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018833 * @mg: the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018834 *
18835 * Assigns the model group of model group definitions to the "term"
18836 * of the referencing particle.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018837 * In xmlSchemaResolveModelGroupParticleReferences the model group
18838 * definitions were assigned to the "term", since needed for the
18839 * circularity check.
18840 *
18841 * Schema Component Constraint:
18842 * All Group Limited (cos-all-limited) (1.2)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018843 */
18844static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018845xmlSchemaModelGroupToModelGroupDefFixup(
18846 xmlSchemaParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
18847 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018848{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018849 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
18850
18851 while (particle != NULL) {
18852 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
18853 ((WXS_PARTICLE_TERM(particle))->type !=
18854 XML_SCHEMA_TYPE_GROUP))
18855 {
18856 particle = WXS_PTC_CAST particle->next;
18857 continue;
18858 }
18859 if (WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle)) == NULL) {
18860 /*
18861 * TODO: Remove the particle.
18862 */
18863 WXS_PARTICLE_TERM(particle) = NULL;
18864 particle = WXS_PTC_CAST particle->next;
18865 continue;
18866 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018867 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018868 * Assign the model group to the {term} of the particle.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018869 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018870 WXS_PARTICLE_TERM(particle) =
18871 WXS_TREE_CAST WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle));
18872
18873 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018874 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018875}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018876
18877/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018878 * xmlSchemaCheckAttrGroupCircularRecur:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018879 * @ctxtGr: the searched attribute group
18880 * @attr: the current attribute list to be processed
18881 *
18882 * This one is intended to be used by
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018883 * xmlSchemaCheckAttrGroupCircular only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018884 *
18885 * Returns the circular attribute grou reference, otherwise NULL.
18886 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018887static xmlSchemaQNameRefPtr
18888xmlSchemaCheckAttrGroupCircularRecur(xmlSchemaAttributeGroupPtr ctxtGr,
18889 xmlSchemaItemListPtr list)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018890{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018891 xmlSchemaAttributeGroupPtr gr;
18892 xmlSchemaQNameRefPtr ref, circ;
18893 int i;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018894 /*
18895 * We will search for an attribute group reference which
18896 * references the context attribute group.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018897 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018898 for (i = 0; i < list->nbItems; i++) {
18899 ref = list->items[i];
18900 if ((ref->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
18901 (ref->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
18902 (ref->item != NULL))
18903 {
18904 gr = WXS_ATTR_GROUP_CAST ref->item;
18905 if (gr == ctxtGr)
18906 return(ref);
18907 if (gr->flags & XML_SCHEMAS_ATTRGROUP_MARKED)
18908 continue;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018909 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018910 * Mark as visited to avoid infinite recursion on
18911 * circular references not yet examined.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018912 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018913 if ((gr->attrUses) &&
18914 (gr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS))
18915 {
18916 gr->flags |= XML_SCHEMAS_ATTRGROUP_MARKED;
18917 circ = xmlSchemaCheckAttrGroupCircularRecur(ctxtGr,
18918 (xmlSchemaItemListPtr) gr->attrUses);
18919 gr->flags ^= XML_SCHEMAS_ATTRGROUP_MARKED;
18920 if (circ != NULL)
18921 return (circ);
18922 }
18923
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018924 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018925 }
18926 return (NULL);
18927}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018928
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018929/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018930 * xmlSchemaCheckAttrGroupCircular:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018931 * attrGr: the attribute group definition
18932 * @ctxt: the parser context
18933 * @name: the name
18934 *
18935 * Checks for circular references of attribute groups.
18936 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018937static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018938xmlSchemaCheckAttrGroupCircular(xmlSchemaAttributeGroupPtr attrGr,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018939 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018940{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018941 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018942 * Schema Representation Constraint:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018943 * Attribute Group Definition Representation OK
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018944 * 3 Circular group reference is disallowed outside <redefine>.
18945 * That is, unless this element information item's parent is
18946 * <redefine>, then among the [children], if any, there must
18947 * not be an <attributeGroup> with ref [attribute] which resolves
18948 * to the component corresponding to this <attributeGroup>. Indirect
18949 * circularity is also ruled out. That is, when QName resolution
18950 * (Schema Document) (§3.15.3) is applied to a ·QName· arising from
18951 * any <attributeGroup>s with a ref [attribute] among the [children],
18952 * it must not be the case that a ·QName· is encountered at any depth
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018953 * which resolves to the component corresponding to this <attributeGroup>.
18954 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018955 if (attrGr->attrUses == NULL)
18956 return(0);
18957 else if ((attrGr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS) == 0)
18958 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018959 else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018960 xmlSchemaQNameRefPtr circ;
18961
18962 circ = xmlSchemaCheckAttrGroupCircularRecur(attrGr,
18963 (xmlSchemaItemListPtr) attrGr->attrUses);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018964 if (circ != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018965 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018966 /*
18967 * TODO: Report the referenced attr group as QName.
18968 */
18969 xmlSchemaPCustomErr(ctxt,
18970 XML_SCHEMAP_SRC_ATTRIBUTE_GROUP_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018971 NULL, WXS_ITEM_NODE(WXS_BASIC_CAST circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018972 "Circular reference to the attribute group '%s' "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018973 "defined", xmlSchemaGetComponentQName(&str, attrGr));
18974 FREE_AND_NULL(str);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018975 /*
18976 * NOTE: We will cut the reference to avoid further
18977 * confusion of the processor.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018978 * BADSPEC TODO: The spec should define how to process in this case.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018979 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018980 circ->item = NULL;
18981 return(ctxt->err);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018982 }
18983 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018984 return(0);
18985}
18986
18987static int
18988xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
18989 xmlSchemaAttributeGroupPtr attrGr);
18990
18991/**
18992 * xmlSchemaExpandAttributeGroupRefs:
18993 * @pctxt: the parser context
18994 * @node: the node of the component holding the attribute uses
18995 * @completeWild: the intersected wildcard to be returned
18996 * @list: the attribute uses
18997 *
18998 * Substitutes contained attribute group references
18999 * for their attribute uses. Wilcards are intersected.
19000 * Attribute use prohibitions are removed from the list
19001 * and returned via the @prohibs list.
19002 * Pointlessness of attr. prohibs, if a matching attr. decl
19003 * is existent a well, are checked.
19004 */
19005static int
19006xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
19007 xmlSchemaBasicItemPtr item,
19008 xmlSchemaWildcardPtr *completeWild,
19009 xmlSchemaItemListPtr list,
19010 xmlSchemaItemListPtr prohibs)
19011{
19012 xmlSchemaAttributeGroupPtr gr;
19013 xmlSchemaAttributeUsePtr use;
19014 xmlSchemaItemListPtr sublist;
19015 int i, j;
19016 int created = (*completeWild == NULL) ? 0 : 1;
19017
19018 if (prohibs)
19019 prohibs->nbItems = 0;
19020
19021 for (i = 0; i < list->nbItems; i++) {
19022 use = list->items[i];
19023
19024 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
19025 if (prohibs == NULL) {
19026 PERROR_INT("xmlSchemaExpandAttributeGroupRefs",
19027 "unexpected attr prohibition found");
19028 return(-1);
19029 }
19030 /*
19031 * Remove from attribute uses.
19032 */
19033 if (xmlSchemaItemListRemove(list, i) == -1)
19034 return(-1);
19035 i--;
19036 /*
19037 * Note that duplicate prohibitions were already
19038 * handled at parsing time.
19039 */
19040 /*
19041 * Add to list of prohibitions.
19042 */
19043 xmlSchemaItemListAddSize(prohibs, 2, use);
19044 continue;
19045 }
19046 if ((use->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
19047 ((WXS_QNAME_CAST use)->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP))
19048 {
19049 if ((WXS_QNAME_CAST use)->item == NULL)
19050 return(-1);
19051 gr = WXS_ATTR_GROUP_CAST (WXS_QNAME_CAST use)->item;
19052 /*
19053 * Expand the referenced attr. group.
19054 * TODO: remove this, this is done in a previous step, so
19055 * already done here.
19056 */
19057 if ((gr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED) == 0) {
19058 if (xmlSchemaAttributeGroupExpandRefs(pctxt, gr) == -1)
19059 return(-1);
19060 }
19061 /*
19062 * Build the 'complete' wildcard; i.e. intersect multiple
19063 * wildcards.
19064 */
19065 if (gr->attributeWildcard != NULL) {
19066 if (*completeWild == NULL) {
19067 *completeWild = gr->attributeWildcard;
19068 } else {
19069 if (! created) {
19070 xmlSchemaWildcardPtr tmpWild;
19071
19072 /*
19073 * Copy the first encountered wildcard as context,
19074 * except for the annotation.
19075 *
19076 * Although the complete wildcard might not correspond
19077 * to any node in the schema, we will anchor it on
19078 * the node of the owner component.
19079 */
19080 tmpWild = xmlSchemaAddWildcard(pctxt, pctxt->schema,
19081 XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
19082 WXS_ITEM_NODE(item));
19083 if (tmpWild == NULL)
19084 return(-1);
19085 if (xmlSchemaCloneWildcardNsConstraints(pctxt,
19086 tmpWild, *completeWild) == -1)
19087 return (-1);
19088 tmpWild->processContents = (*completeWild)->processContents;
19089 *completeWild = tmpWild;
19090 created = 1;
19091 }
19092
19093 if (xmlSchemaIntersectWildcards(pctxt, *completeWild,
19094 gr->attributeWildcard) == -1)
19095 return(-1);
19096 }
19097 }
19098 /*
19099 * Just remove the reference if the referenced group does not
19100 * contain any attribute uses.
19101 */
19102 if (gr->attrUses == NULL) {
19103 if (xmlSchemaItemListRemove(list, i) == -1)
19104 return(-1);
19105 i--;
19106 continue;
19107 }
19108 /*
19109 * Add the attribute uses.
19110 */
19111 sublist = ((xmlSchemaItemListPtr) gr->attrUses);
19112 if (sublist->nbItems != 0) {
19113 list->items[i] = sublist->items[0];
19114 if (sublist->nbItems != 1) {
19115 for (j = 1; j < sublist->nbItems; j++) {
19116 i++;
19117 if (xmlSchemaItemListInsert(list,
19118 sublist->items[j], i) == -1)
19119 return(-1);
19120 }
19121 }
19122 }
19123 }
19124
19125 }
19126 /*
19127 * Handle pointless prohibitions of declared attributes.
19128 */
19129 if (prohibs && (prohibs->nbItems != 0) && (list->nbItems != 0)) {
19130 xmlSchemaAttributeUseProhibPtr prohib;
19131
19132 for (i = prohibs->nbItems -1; i >= 0; i--) {
19133 prohib = prohibs->items[i];
19134 for (j = 0; j < list->nbItems; j++) {
19135 use = list->items[j];
19136
19137 if ((prohib->name == WXS_ATTRUSE_DECL_NAME(use)) &&
19138 (prohib->targetNamespace == WXS_ATTRUSE_DECL_TNS(use)))
19139 {
19140 xmlChar *str = NULL;
19141
19142 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
19143 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
19144 prohib->node, NULL,
19145 "Skipping pointless attribute use prohibition "
19146 "'%s', since a corresponding attribute use "
19147 "exists already in the type definition",
19148 xmlSchemaFormatQName(&str,
19149 prohib->targetNamespace, prohib->name),
19150 NULL, NULL);
19151 FREE_AND_NULL(str);
19152 /*
19153 * Remove the prohibition.
19154 */
19155 if (xmlSchemaItemListRemove(prohibs, i) == -1)
19156 return(-1);
19157 break;
19158 }
19159 }
19160 }
19161 }
19162 return(0);
19163}
19164
19165/**
19166 * xmlSchemaAttributeGroupExpandRefs:
19167 * @pctxt: the parser context
19168 * @attrGr: the attribute group definition
19169 *
19170 * Computation of:
19171 * {attribute uses} property
19172 * {attribute wildcard} property
19173 *
19174 * Substitutes contained attribute group references
19175 * for their attribute uses. Wilcards are intersected.
19176 */
19177static int
19178xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19179 xmlSchemaAttributeGroupPtr attrGr)
19180{
19181 if ((attrGr->attrUses == NULL) ||
19182 (attrGr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED))
19183 return(0);
19184
19185 attrGr->flags |= XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED;
19186 if (xmlSchemaExpandAttributeGroupRefs(pctxt, WXS_BASIC_CAST attrGr,
19187 &(attrGr->attributeWildcard), attrGr->attrUses, NULL) == -1)
19188 return(-1);
19189 return(0);
19190}
19191
19192/**
19193 * xmlSchemaAttributeGroupExpandRefs:
19194 * @pctxt: the parser context
19195 * @attrGr: the attribute group definition
19196 *
19197 * Substitutes contained attribute group references
19198 * for their attribute uses. Wilcards are intersected.
19199 *
19200 * Schema Component Constraint:
19201 * Attribute Group Definition Properties Correct (ag-props-correct)
19202 */
19203static int
19204xmlSchemaCheckAGPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19205 xmlSchemaAttributeGroupPtr attrGr)
19206{
19207 /*
19208 * SPEC ag-props-correct
19209 * (1) "The values of the properties of an attribute group definition
19210 * must be as described in the property tableau in The Attribute
19211 * Group Definition Schema Component (§3.6.1), modulo the impact of
19212 * Missing Sub-components (§5.3);"
19213 */
19214
19215 if ((attrGr->attrUses != NULL) &&
19216 (WXS_LIST_CAST attrGr->attrUses)->nbItems > 1)
19217 {
19218 xmlSchemaItemListPtr uses = WXS_LIST_CAST attrGr->attrUses;
19219 xmlSchemaAttributeUsePtr use, tmp;
19220 int i, j, hasId = 0;
19221
19222 for (i = uses->nbItems -1; i >= 0; i--) {
19223 use = uses->items[i];
19224 /*
19225 * SPEC ag-props-correct
19226 * (2) "Two distinct members of the {attribute uses} must not have
19227 * {attribute declaration}s both of whose {name}s match and whose
19228 * {target namespace}s are identical."
19229 */
19230 if (i > 0) {
19231 for (j = i -1; j >= 0; j--) {
19232 tmp = uses->items[j];
19233 if ((WXS_ATTRUSE_DECL_NAME(use) ==
19234 WXS_ATTRUSE_DECL_NAME(tmp)) &&
19235 (WXS_ATTRUSE_DECL_TNS(use) ==
19236 WXS_ATTRUSE_DECL_TNS(tmp)))
19237 {
19238 xmlChar *str = NULL;
19239
19240 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19241 XML_SCHEMAP_AG_PROPS_CORRECT,
19242 attrGr->node, WXS_BASIC_CAST attrGr,
19243 "Duplicate %s",
19244 xmlSchemaGetComponentDesignation(&str, use),
19245 NULL);
19246 FREE_AND_NULL(str);
19247 /*
19248 * Remove the duplicate.
19249 */
19250 if (xmlSchemaItemListRemove(uses, i) == -1)
19251 return(-1);
19252 goto next_use;
19253 }
19254 }
19255 }
19256 /*
19257 * SPEC ag-props-correct
19258 * (3) "Two distinct members of the {attribute uses} must not have
19259 * {attribute declaration}s both of whose {type definition}s are or
19260 * are derived from ID."
19261 * TODO: Does 'derived' include member-types of unions?
19262 */
19263 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
19264 if (xmlSchemaIsDerivedFromBuiltInType(
19265 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
19266 {
19267 if (hasId) {
19268 xmlChar *str = NULL;
19269
19270 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19271 XML_SCHEMAP_AG_PROPS_CORRECT,
19272 attrGr->node, WXS_BASIC_CAST attrGr,
19273 "There must not exist more than one attribute "
19274 "declaration of type 'xs:ID' "
19275 "(or derived from 'xs:ID'). The %s violates this "
19276 "constraint",
19277 xmlSchemaGetComponentDesignation(&str, use),
19278 NULL);
19279 FREE_AND_NULL(str);
19280 if (xmlSchemaItemListRemove(uses, i) == -1)
19281 return(-1);
19282 }
19283 hasId = 1;
19284 }
19285 }
19286next_use: {}
19287 }
19288 }
19289 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019290}
19291
19292/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019293 * xmlSchemaResolveAttrGroupReferences:
Daniel Veillard13e04c62002-04-23 17:51:29 +000019294 * @attrgrpDecl: the schema attribute definition
19295 * @ctxt: the schema parser context
19296 * @name: the attribute name
19297 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019298 * Resolves references to attribute group definitions.
Daniel Veillard13e04c62002-04-23 17:51:29 +000019299 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019300static int
19301xmlSchemaResolveAttrGroupReferences(xmlSchemaQNameRefPtr ref,
19302 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard13e04c62002-04-23 17:51:29 +000019303{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019304 xmlSchemaAttributeGroupPtr group;
Daniel Veillard13e04c62002-04-23 17:51:29 +000019305
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019306 if (ref->item != NULL)
19307 return(0);
19308 group = xmlSchemaGetAttributeGroup(ctxt->schema,
19309 ref->name,
19310 ref->targetNamespace);
19311 if (group == NULL) {
19312 xmlSchemaPResCompAttrErr(ctxt,
19313 XML_SCHEMAP_SRC_RESOLVE,
19314 NULL, ref->node,
19315 "ref", ref->name, ref->targetNamespace,
19316 ref->itemType, NULL);
19317 return(ctxt->err);
Daniel Veillard3646d642004-06-02 19:19:14 +000019318 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019319 ref->item = WXS_BASIC_CAST group;
19320 return(0);
Daniel Veillard13e04c62002-04-23 17:51:29 +000019321}
19322
19323/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019324 * xmlSchemaCheckAttrPropsCorrect:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019325 * @item: an schema attribute declaration/use
19326 * @ctxt: a schema parser context
19327 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019328 *
19329 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019330 * Schema Component Constraint:
19331 * Attribute Declaration Properties Correct (a-props-correct)
Daniel Veillard4255d502002-04-16 15:50:10 +000019332 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019333 * Validates the value constraints of an attribute declaration/use.
19334 * NOTE that this needs the simle type definitions to be already
19335 * builded and checked.
Daniel Veillard4255d502002-04-16 15:50:10 +000019336 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019337static int
19338xmlSchemaCheckAttrPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19339 xmlSchemaAttributePtr attr)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019340{
19341
19342 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019343 * SPEC a-props-correct (1)
19344 * "The values of the properties of an attribute declaration must
19345 * be as described in the property tableau in The Attribute
19346 * Declaration Schema Component (§3.2.1), modulo the impact of
19347 * Missing Sub-components (§5.3)."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019348 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019349
19350 if (WXS_ATTR_TYPEDEF(attr) == NULL)
19351 return(0);
19352
19353 if (attr->defValue != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019354 int ret;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019355
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019356 /*
19357 * SPEC a-props-correct (3)
19358 * "If the {type definition} is or is derived from ID then there
19359 * must not be a {value constraint}."
19360 */
19361 if (xmlSchemaIsDerivedFromBuiltInType(
19362 WXS_ATTR_TYPEDEF(attr), XML_SCHEMAS_ID))
19363 {
19364 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19365 XML_SCHEMAP_A_PROPS_CORRECT_3,
19366 NULL, WXS_BASIC_CAST attr,
19367 "Value constraints are not allowed if the type definition "
19368 "is or is derived from xs:ID",
19369 NULL, NULL);
19370 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019371 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019372 /*
19373 * SPEC a-props-correct (2)
19374 * "if there is a {value constraint}, the canonical lexical
19375 * representation of its value must be ·valid· with respect
19376 * to the {type definition} as defined in String Valid (§3.14.4)."
19377 * TODO: Don't care about the *cononical* stuff here, this requirement
19378 * will be removed in WXS 1.1 anyway.
19379 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019380 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019381 attr->node, WXS_ATTR_TYPEDEF(attr),
19382 attr->defValue, &(attr->defVal),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019383 1, 1, 0);
19384 if (ret != 0) {
19385 if (ret < 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019386 PERROR_INT("xmlSchemaCheckAttrPropsCorrect",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019387 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019388 return(-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019389 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019390 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019391 XML_SCHEMAP_A_PROPS_CORRECT_2,
19392 NULL, WXS_BASIC_CAST attr,
19393 "The value of the value constraint is not valid",
19394 NULL, NULL);
19395 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019396 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019397 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019398
19399 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019400}
19401
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019402static xmlSchemaElementPtr
19403xmlSchemaCheckSubstGroupCircular(xmlSchemaElementPtr elemDecl,
19404 xmlSchemaElementPtr ancestor)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019405{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019406 xmlSchemaElementPtr ret;
19407
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019408 if (WXS_SUBST_HEAD(ancestor) == NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019409 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019410 if (WXS_SUBST_HEAD(ancestor) == elemDecl)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019411 return (ancestor);
19412
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019413 if (WXS_SUBST_HEAD(ancestor)->flags & XML_SCHEMAS_ELEM_CIRCULAR)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019414 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019415 WXS_SUBST_HEAD(ancestor)->flags |= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019416 ret = xmlSchemaCheckSubstGroupCircular(elemDecl,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019417 WXS_SUBST_HEAD(ancestor));
19418 WXS_SUBST_HEAD(ancestor)->flags ^= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019419
19420 return (ret);
19421}
19422
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019423/**
19424 * xmlSchemaCheckElemPropsCorrect:
19425 * @ctxt: a schema parser context
19426 * @decl: the element declaration
19427 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019428 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019429 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019430 * Element Declaration Properties Correct (e-props-correct)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019431 *
19432 * STATUS:
19433 * missing: (6)
19434 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019435static int
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019436xmlSchemaCheckElemPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19437 xmlSchemaElementPtr elemDecl)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019438{
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019439 int ret = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019440 xmlSchemaTypePtr typeDef = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019441 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019442 * SPEC (1) "The values of the properties of an element declaration
19443 * must be as described in the property tableau in The Element
19444 * Declaration Schema Component (§3.3.1), modulo the impact of Missing
19445 * Sub-components (§5.3)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019446 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019447 if (WXS_SUBST_HEAD(elemDecl) != NULL) {
19448 xmlSchemaElementPtr head = WXS_SUBST_HEAD(elemDecl), circ;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019449
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019450 xmlSchemaCheckElementDeclComponent(head, pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019451 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019452 * SPEC (3) "If there is a non-·absent· {substitution group
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019453 * affiliation}, then {scope} must be global."
19454 */
19455 if ((elemDecl->flags & XML_SCHEMAS_ELEM_GLOBAL) == 0) {
19456 xmlSchemaPCustomErr(pctxt,
19457 XML_SCHEMAP_E_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019458 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019459 "Only global element declarations can have a "
19460 "substitution group affiliation", NULL);
19461 ret = XML_SCHEMAP_E_PROPS_CORRECT_3;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019462 }
19463 /*
19464 * TODO: SPEC (6) "Circular substitution groups are disallowed.
19465 * That is, it must not be possible to return to an element declaration
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019466 * by repeatedly following the {substitution group affiliation}
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019467 * property."
19468 */
19469 if (head == elemDecl)
19470 circ = head;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019471 else if (WXS_SUBST_HEAD(head) != NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019472 circ = xmlSchemaCheckSubstGroupCircular(head, head);
19473 else
19474 circ = NULL;
19475 if (circ != NULL) {
19476 xmlChar *strA = NULL, *strB = NULL;
19477
19478 xmlSchemaPCustomErrExt(pctxt,
19479 XML_SCHEMAP_E_PROPS_CORRECT_6,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019480 WXS_BASIC_CAST circ, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019481 "The element declaration '%s' defines a circular "
19482 "substitution group to element declaration '%s'",
19483 xmlSchemaGetComponentQName(&strA, circ),
19484 xmlSchemaGetComponentQName(&strB, head),
19485 NULL);
19486 FREE_AND_NULL(strA)
19487 FREE_AND_NULL(strB)
19488 ret = XML_SCHEMAP_E_PROPS_CORRECT_6;
19489 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019490 /*
19491 * SPEC (4) "If there is a {substitution group affiliation},
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019492 * the {type definition}
19493 * of the element declaration must be validly derived from the {type
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019494 * definition} of the {substitution group affiliation}, given the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019495 * of the {substitution group exclusions} of the {substitution group
19496 * affiliation}, as defined in Type Derivation OK (Complex) (§3.4.6)
19497 * (if the {type definition} is complex) or as defined in
19498 * Type Derivation OK (Simple) (§3.14.6) (if the {type definition} is
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019499 * simple)."
19500 *
19501 * NOTE: {substitution group exclusions} means the values of the
19502 * attribute "final".
19503 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019504
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019505 if (typeDef != WXS_ELEM_TYPEDEF(WXS_SUBST_HEAD(elemDecl))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019506 int set = 0;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019507
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019508 if (head->flags & XML_SCHEMAS_ELEM_FINAL_EXTENSION)
19509 set |= SUBSET_EXTENSION;
19510 if (head->flags & XML_SCHEMAS_ELEM_FINAL_RESTRICTION)
19511 set |= SUBSET_RESTRICTION;
19512
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000019513 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST pctxt, typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019514 WXS_ELEM_TYPEDEF(head), set) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019515 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
19516
19517 ret = XML_SCHEMAP_E_PROPS_CORRECT_4;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019518 xmlSchemaPCustomErrExt(pctxt,
19519 XML_SCHEMAP_E_PROPS_CORRECT_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019520 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019521 "The type definition '%s' was "
19522 "either rejected by the substitution group "
19523 "affiliation '%s', or not validly derived from its type "
19524 "definition '%s'",
19525 xmlSchemaGetComponentQName(&strA, typeDef),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019526 xmlSchemaGetComponentQName(&strB, head),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019527 xmlSchemaGetComponentQName(&strC, WXS_ELEM_TYPEDEF(head)));
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019528 FREE_AND_NULL(strA)
19529 FREE_AND_NULL(strB)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019530 FREE_AND_NULL(strC)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019531 }
19532 }
19533 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019534 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019535 * SPEC (5) "If the {type definition} or {type definition}'s
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019536 * {content type}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019537 * is or is derived from ID then there must not be a {value constraint}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019538 * Note: The use of ID as a type definition for elements goes beyond
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019539 * XML 1.0, and should be avoided if backwards compatibility is desired"
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019540 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019541 if ((elemDecl->value != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019542 ((WXS_IS_SIMPLE(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019543 xmlSchemaIsDerivedFromBuiltInType(typeDef, XML_SCHEMAS_ID)) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019544 (WXS_IS_COMPLEX(typeDef) &&
19545 WXS_HAS_SIMPLE_CONTENT(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019546 xmlSchemaIsDerivedFromBuiltInType(typeDef->contentTypeDef,
19547 XML_SCHEMAS_ID)))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019548
19549 ret = XML_SCHEMAP_E_PROPS_CORRECT_5;
19550 xmlSchemaPCustomErr(pctxt,
19551 XML_SCHEMAP_E_PROPS_CORRECT_5,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019552 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019553 "The type definition (or type definition's content type) is or "
19554 "is derived from ID; value constraints are not allowed in "
19555 "conjunction with such a type definition", NULL);
19556 } else if (elemDecl->value != NULL) {
19557 int vcret;
19558 xmlNodePtr node = NULL;
19559
19560 /*
19561 * SPEC (2) "If there is a {value constraint}, the canonical lexical
19562 * representation of its value must be ·valid· with respect to the
19563 * {type definition} as defined in Element Default Valid (Immediate)
19564 * (§3.3.6)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019565 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019566 if (typeDef == NULL) {
19567 xmlSchemaPErr(pctxt, elemDecl->node,
19568 XML_SCHEMAP_INTERNAL,
19569 "Internal error: xmlSchemaCheckElemPropsCorrect, "
19570 "type is missing... skipping validation of "
19571 "the value constraint", NULL, NULL);
19572 return (-1);
19573 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019574 if (elemDecl->node != NULL) {
19575 if (elemDecl->flags & XML_SCHEMAS_ELEM_FIXED)
19576 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19577 BAD_CAST "fixed");
19578 else
19579 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19580 BAD_CAST "default");
19581 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019582 vcret = xmlSchemaParseCheckCOSValidDefault(pctxt, node,
19583 typeDef, elemDecl->value, &(elemDecl->defVal));
19584 if (vcret != 0) {
19585 if (vcret < 0) {
19586 PERROR_INT("xmlSchemaElemCheckValConstr",
19587 "failed to validate the value constraint of an "
19588 "element declaration");
19589 return (-1);
19590 }
19591 return (vcret);
19592 }
19593 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019594
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019595 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019596}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019597
19598/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019599 * xmlSchemaCheckElemSubstGroup:
19600 * @ctxt: a schema parser context
19601 * @decl: the element declaration
19602 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019603 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019604 * Schema Component Constraint:
19605 * Substitution Group (cos-equiv-class)
19606 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019607 * In Libxml2 the subst. groups will be precomputed, in terms of that
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019608 * a list will be built for each subst. group head, holding all direct
19609 * referents to this head.
19610 * NOTE that this function needs:
19611 * 1. circular subst. groups to be checked beforehand
19612 * 2. the declaration's type to be derived from the head's type
19613 *
19614 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019615 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019616 */
19617static void
19618xmlSchemaCheckElemSubstGroup(xmlSchemaParserCtxtPtr ctxt,
19619 xmlSchemaElementPtr elemDecl)
19620{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019621 if ((WXS_SUBST_HEAD(elemDecl) == NULL) ||
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019622 /* SPEC (1) "Its {abstract} is false." */
19623 (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT))
19624 return;
19625 {
19626 xmlSchemaElementPtr head;
19627 xmlSchemaTypePtr headType, type;
19628 int set, methSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019629 /*
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019630 * SPEC (2) "It is validly substitutable for HEAD subject to HEAD's
19631 * {disallowed substitutions} as the blocking constraint, as defined in
19632 * Substitution Group OK (Transitive) (§3.3.6)."
19633 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019634 for (head = WXS_SUBST_HEAD(elemDecl); head != NULL;
19635 head = WXS_SUBST_HEAD(head)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019636 set = 0;
19637 methSet = 0;
19638 /*
19639 * The blocking constraints.
19640 */
19641 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION)
19642 continue;
19643 headType = head->subtypes;
19644 type = elemDecl->subtypes;
19645 if (headType == type)
19646 goto add_member;
19647 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION)
19648 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19649 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION)
19650 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19651 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019652 * SPEC: Substitution Group OK (Transitive) (2.3)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019653 * "The set of all {derivation method}s involved in the
19654 * derivation of D's {type definition} from C's {type definition}
19655 * does not intersect with the union of the blocking constraint,
19656 * C's {prohibited substitutions} (if C is complex, otherwise the
19657 * empty set) and the {prohibited substitutions} (respectively the
19658 * empty set) of any intermediate {type definition}s in the
19659 * derivation of D's {type definition} from C's {type definition}."
19660 */
19661 /*
19662 * OPTIMIZE TODO: Optimize this a bit, since, if traversing the
19663 * subst.head axis, the methSet does not need to be computed for
19664 * the full depth over and over.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019665 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019666 /*
19667 * The set of all {derivation method}s involved in the derivation
19668 */
19669 while ((type != NULL) && (type != headType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019670 if ((WXS_IS_EXTENSION(type)) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019671 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19672 methSet |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019673
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019674 if (WXS_IS_RESTRICTION(type) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019675 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19676 methSet |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19677
19678 type = type->baseType;
19679 }
19680 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019681 * The {prohibited substitutions} of all intermediate types +
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019682 * the head's type.
19683 */
19684 type = elemDecl->subtypes->baseType;
19685 while (type != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019686 if (WXS_IS_COMPLEX(type)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019687 if ((type->flags &
19688 XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19689 ((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) == 0))
19690 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19691 if ((type->flags &
19692 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19693 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19694 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19695 } else
19696 break;
19697 if (type == headType)
19698 break;
19699 type = type->baseType;
19700 }
19701 if ((set != 0) &&
19702 (((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19703 (methSet & XML_SCHEMAS_TYPE_BLOCK_EXTENSION)) ||
19704 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19705 (methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION)))) {
19706 continue;
19707 }
19708add_member:
19709 xmlSchemaAddElementSubstitutionMember(ctxt, head, elemDecl);
19710 if ((head->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) == 0)
19711 head->flags |= XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD;
19712 }
19713 }
19714}
19715
19716/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019717 * xmlSchemaCheckElementDeclComponent
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019718 * @item: an schema element declaration/particle
19719 * @ctxt: a schema parser context
19720 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019721 *
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019722 * Validates the value constraints of an element declaration.
19723 *
19724 * Fixes finish doing the computations on the element declarations.
19725 */
19726static void
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019727xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019728 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019729{
19730 if (elemDecl == NULL)
19731 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019732 if (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_CHECKED)
19733 return;
19734 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_CHECKED;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019735 if (xmlSchemaCheckElemPropsCorrect(ctxt, elemDecl) == 0)
19736 xmlSchemaCheckElemSubstGroup(ctxt, elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019737}
19738
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019739/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019740 * xmlSchemaResolveModelGroupParticleReferences:
19741 * @particle: a particle component
19742 * @ctxt: a parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019743 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019744 * Resolves references of a model group's {particles} to
19745 * model group definitions and to element declarations.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019746 */
19747static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019748xmlSchemaResolveModelGroupParticleReferences(
19749 xmlSchemaParserCtxtPtr ctxt,
19750 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019751{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019752 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
19753 xmlSchemaQNameRefPtr ref;
19754 xmlSchemaBasicItemPtr refItem;
19755
19756 /*
19757 * URGENT TODO: Test this.
19758 */
19759 while (particle != NULL) {
19760 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
19761 ((WXS_PARTICLE_TERM(particle))->type !=
19762 XML_SCHEMA_EXTRA_QNAMEREF))
19763 {
19764 goto next_particle;
19765 }
19766 ref = WXS_QNAME_CAST WXS_PARTICLE_TERM(particle);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019767 /*
19768 * Resolve the reference.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019769 * NULL the {term} by default.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019770 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019771 particle->children = NULL;
19772
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019773 refItem = xmlSchemaGetNamedComponent(ctxt->schema,
19774 ref->itemType, ref->name, ref->targetNamespace);
19775 if (refItem == NULL) {
19776 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019777 NULL, WXS_ITEM_NODE(particle), "ref", ref->name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019778 ref->targetNamespace, ref->itemType, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019779 /* TODO: remove the particle. */
19780 goto next_particle;
19781 }
19782 if (refItem->type == XML_SCHEMA_TYPE_GROUP) {
19783 if (WXS_MODELGROUPDEF_MODEL(refItem) == NULL)
19784 /* TODO: remove the particle. */
19785 goto next_particle;
19786 /*
19787 * NOTE that we will assign the model group definition
19788 * itself to the "term" of the particle. This will ease
19789 * the check for circular model group definitions. After
19790 * that the "term" will be assigned the model group of the
19791 * model group definition.
19792 */
19793 if ((WXS_MODELGROUPDEF_MODEL(refItem))->type ==
19794 XML_SCHEMA_TYPE_ALL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019795 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019796 * SPEC cos-all-limited (1)
19797 * SPEC cos-all-limited (1.2)
19798 * "It appears only as the value of one or both of the
19799 * following properties:"
19800 * (1.1) "the {model group} property of a model group
19801 * definition."
19802 * (1.2) "the {term} property of a particle [... of] the "
19803 * {content type} of a complex type definition."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019804 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019805 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19806 /* TODO: error code */
19807 XML_SCHEMAP_COS_ALL_LIMITED,
19808 WXS_ITEM_NODE(particle), NULL,
19809 "A model group definition is referenced, but "
19810 "it contains an 'all' model group, which "
19811 "cannot be contained by model groups",
19812 NULL, NULL);
19813 /* TODO: remove the particle. */
19814 goto next_particle;
19815 }
19816 particle->children = (xmlSchemaTreeItemPtr) refItem;
19817 } else {
19818 /*
19819 * TODO: Are referenced element declarations the only
19820 * other components we expect here?
19821 */
19822 particle->children = (xmlSchemaTreeItemPtr) refItem;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019823 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019824next_particle:
19825 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019826 }
19827}
19828
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019829static int
19830xmlSchemaAreValuesEqual(xmlSchemaValPtr x,
19831 xmlSchemaValPtr y)
19832{
19833 xmlSchemaTypePtr tx, ty, ptx, pty;
19834 int ret;
19835
19836 while (x != NULL) {
19837 /* Same types. */
19838 tx = xmlSchemaGetBuiltInType(xmlSchemaGetValType(x));
19839 ty = xmlSchemaGetBuiltInType(xmlSchemaGetValType(y));
19840 ptx = xmlSchemaGetPrimitiveType(tx);
19841 pty = xmlSchemaGetPrimitiveType(ty);
19842 /*
19843 * (1) if a datatype T' is ·derived· by ·restriction· from an
19844 * atomic datatype T then the ·value space· of T' is a subset of
19845 * the ·value space· of T. */
19846 /*
19847 * (2) if datatypes T' and T'' are ·derived· by ·restriction·
19848 * from a common atomic ancestor T then the ·value space·s of T'
19849 * and T'' may overlap.
19850 */
19851 if (ptx != pty)
19852 return(0);
19853 /*
19854 * We assume computed values to be normalized, so do a fast
19855 * string comparison for string based types.
19856 */
19857 if ((ptx->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019858 WXS_IS_ANY_SIMPLE_TYPE(ptx)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019859 if (! xmlStrEqual(
19860 xmlSchemaValueGetAsString(x),
19861 xmlSchemaValueGetAsString(y)))
19862 return (0);
19863 } else {
19864 ret = xmlSchemaCompareValuesWhtsp(
19865 x, XML_SCHEMA_WHITESPACE_PRESERVE,
19866 y, XML_SCHEMA_WHITESPACE_PRESERVE);
19867 if (ret == -2)
19868 return(-1);
19869 if (ret != 0)
19870 return(0);
19871 }
19872 /*
19873 * Lists.
19874 */
19875 x = xmlSchemaValueGetNext(x);
19876 if (x != NULL) {
19877 y = xmlSchemaValueGetNext(y);
19878 if (y == NULL)
19879 return (0);
19880 } else if (xmlSchemaValueGetNext(y) != NULL)
19881 return (0);
19882 else
19883 return (1);
19884 }
19885 return (0);
19886}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019887
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019888/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019889 * xmlSchemaResolveAttrUseReferences:
19890 * @item: an attribute use
19891 * @ctxt: a parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019892 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019893 * Resolves the referenced attribute declaration.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019894 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019895static int
19896xmlSchemaResolveAttrUseReferences(xmlSchemaAttributeUsePtr ause,
19897 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000019898{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019899 if ((ctxt == NULL) || (ause == NULL))
19900 return(-1);
19901 if ((ause->attrDecl == NULL) ||
19902 (ause->attrDecl->type != XML_SCHEMA_EXTRA_QNAMEREF))
19903 return(0);
19904
19905 {
19906 xmlSchemaQNameRefPtr ref = WXS_QNAME_CAST ause->attrDecl;
19907
19908 /*
19909 * TODO: Evaluate, what errors could occur if the declaration is not
19910 * found.
19911 */
19912 ause->attrDecl = xmlSchemaGetAttributeDecl(ctxt->schema,
19913 ref->name, ref->targetNamespace);
19914 if (ause->attrDecl == NULL) {
19915 xmlSchemaPResCompAttrErr(ctxt,
19916 XML_SCHEMAP_SRC_RESOLVE,
19917 WXS_BASIC_CAST ause, ause->node,
19918 "ref", ref->name, ref->targetNamespace,
19919 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
19920 return(ctxt->err);;
19921 }
19922 }
19923 return(0);
19924}
19925
19926/**
19927 * xmlSchemaCheckAttrUsePropsCorrect:
19928 * @ctxt: a parser context
19929 * @use: an attribute use
19930 *
19931 * Schema Component Constraint:
19932 * Attribute Use Correct (au-props-correct)
19933 *
19934 */
19935static int
19936xmlSchemaCheckAttrUsePropsCorrect(xmlSchemaParserCtxtPtr ctxt,
19937 xmlSchemaAttributeUsePtr use)
19938{
19939 if ((ctxt == NULL) || (use == NULL))
19940 return(-1);
19941 if ((use->defValue == NULL) || (WXS_ATTRUSE_DECL(use) == NULL) ||
19942 ((WXS_ATTRUSE_DECL(use))->type != XML_SCHEMA_TYPE_ATTRIBUTE))
19943 return(0);
19944
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019945 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019946 * SPEC au-props-correct (1)
19947 * "The values of the properties of an attribute use must be as
19948 * described in the property tableau in The Attribute Use Schema
19949 * Component (§3.5.1), modulo the impact of Missing
19950 * Sub-components (§5.3)."
Daniel Veillardc0826a72004-08-10 14:17:33 +000019951 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019952
19953 if (((WXS_ATTRUSE_DECL(use))->defValue != NULL) &&
19954 ((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMAS_ATTR_FIXED) &&
19955 ((use->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
19956 {
19957 xmlSchemaPCustomErr(ctxt,
19958 XML_SCHEMAP_AU_PROPS_CORRECT_2,
19959 WXS_BASIC_CAST use, NULL,
19960 "The attribute declaration has a 'fixed' value constraint "
19961 ", thus the attribute use must also have a 'fixed' value "
19962 "constraint",
19963 NULL);
19964 return(ctxt->err);
19965 }
19966 /*
19967 * Compute and check the value constraint's value.
19968 */
19969 if ((use->defVal != NULL) && (WXS_ATTRUSE_TYPEDEF(use) != NULL)) {
19970 int ret;
19971 /*
19972 * TODO: The spec seems to be missing a check of the
19973 * value constraint of the attribute use. We will do it here.
19974 */
19975 /*
19976 * SPEC a-props-correct (3)
19977 */
19978 if (xmlSchemaIsDerivedFromBuiltInType(
19979 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
19980 {
19981 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19982 XML_SCHEMAP_AU_PROPS_CORRECT,
19983 NULL, WXS_BASIC_CAST use,
19984 "Value constraints are not allowed if the type definition "
19985 "is or is derived from xs:ID",
19986 NULL, NULL);
19987 return(ctxt->err);
19988 }
19989
19990 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST ctxt,
19991 use->node, WXS_ATTRUSE_TYPEDEF(use),
19992 use->defValue, &(use->defVal),
19993 1, 1, 0);
19994 if (ret != 0) {
19995 if (ret < 0) {
19996 PERROR_INT2("xmlSchemaCheckAttrUsePropsCorrect",
19997 "calling xmlSchemaVCheckCVCSimpleType()");
19998 return(-1);
19999 }
20000 xmlSchemaCustomErr(ACTXT_CAST ctxt,
20001 XML_SCHEMAP_AU_PROPS_CORRECT,
20002 NULL, WXS_BASIC_CAST use,
20003 "The value of the value constraint is not valid",
20004 NULL, NULL);
20005 return(ctxt->err);
20006 }
20007 }
20008 /*
20009 * SPEC au-props-correct (2)
20010 * "If the {attribute declaration} has a fixed
20011 * {value constraint}, then if the attribute use itself has a
20012 * {value constraint}, it must also be fixed and its value must match
20013 * that of the {attribute declaration}'s {value constraint}."
20014 */
20015 if (((WXS_ATTRUSE_DECL(use))->defVal != NULL) &&
20016 (((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
20017 {
20018 if (! xmlSchemaAreValuesEqual(use->defVal,
20019 (WXS_ATTRUSE_DECL(use))->defVal))
20020 {
20021 xmlSchemaPCustomErr(ctxt,
20022 XML_SCHEMAP_AU_PROPS_CORRECT_2,
20023 WXS_BASIC_CAST use, NULL,
20024 "The 'fixed' value constraint of the attribute use "
20025 "must match the attribute declaration's value "
20026 "constraint '%s'",
20027 (WXS_ATTRUSE_DECL(use))->defValue);
20028 }
20029 return(ctxt->err);
20030 }
20031 return(0);
20032}
20033
20034
20035
20036
20037/**
20038 * xmlSchemaResolveAttrTypeReferences:
20039 * @item: an attribute declaration
20040 * @ctxt: a parser context
20041 *
20042 * Resolves the referenced type definition component.
20043 */
20044static int
20045xmlSchemaResolveAttrTypeReferences(xmlSchemaAttributePtr item,
20046 xmlSchemaParserCtxtPtr ctxt)
20047{
Daniel Veillard01fa6152004-06-29 17:04:39 +000020048 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020049 * The simple type definition corresponding to the <simpleType> element
20050 * information item in the [children], if present, otherwise the simple
20051 * type definition ·resolved· to by the ·actual value· of the type
Daniel Veillard01fa6152004-06-29 17:04:39 +000020052 * [attribute], if present, otherwise the ·simple ur-type definition·.
20053 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020054 if (item->flags & XML_SCHEMAS_ATTR_INTERNAL_RESOLVED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020055 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020056 item->flags |= XML_SCHEMAS_ATTR_INTERNAL_RESOLVED;
20057 if (item->subtypes != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020058 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020059 if (item->typeName != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000020060 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000020061
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020062 type = xmlSchemaGetType(ctxt->schema, item->typeName,
20063 item->typeNs);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020064 if ((type == NULL) || (! WXS_IS_SIMPLE(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000020065 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000020066 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020067 WXS_BASIC_CAST item, item->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020068 "type", item->typeName, item->typeNs,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020069 XML_SCHEMA_TYPE_SIMPLE, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020070 return(ctxt->err);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020071 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020072 item->subtypes = type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020073
Daniel Veillard3646d642004-06-02 19:19:14 +000020074 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020075 /*
20076 * The type defaults to the xs:anySimpleType.
20077 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020078 item->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
20079 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020080 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000020081}
20082
20083/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020084 * xmlSchemaResolveIDCKeyReferences:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020085 * @idc: the identity-constraint definition
20086 * @ctxt: the schema parser context
20087 * @name: the attribute name
20088 *
20089 * Resolve keyRef references to key/unique IDCs.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020090 * Schema Component Constraint:
20091 * Identity-constraint Definition Properties Correct (c-props-correct)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020092 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020093static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020094xmlSchemaResolveIDCKeyReferences(xmlSchemaIDCPtr idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020095 xmlSchemaParserCtxtPtr pctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020096{
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020097 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020098 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020099 if (idc->ref->name != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020100 idc->ref->item = (xmlSchemaBasicItemPtr)
20101 xmlSchemaGetIDC(pctxt->schema, idc->ref->name,
20102 idc->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020103 if (idc->ref->item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020104 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020105 * TODO: It is actually not an error to fail to resolve
20106 * at this stage. BUT we need to be that strict!
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020107 */
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020108 xmlSchemaPResCompAttrErr(pctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020109 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020110 WXS_BASIC_CAST idc, idc->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020111 "refer", idc->ref->name,
20112 idc->ref->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020113 XML_SCHEMA_TYPE_IDC_KEY, NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020114 return(pctxt->err);
20115 } else if (idc->ref->item->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
20116 /*
20117 * SPEC c-props-correct (1)
20118 */
20119 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20120 XML_SCHEMAP_C_PROPS_CORRECT,
20121 NULL, WXS_BASIC_CAST idc,
20122 "The keyref references a keyref",
20123 NULL, NULL);
20124 idc->ref->item = NULL;
20125 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020126 } else {
20127 if (idc->nbFields !=
20128 ((xmlSchemaIDCPtr) idc->ref->item)->nbFields) {
20129 xmlChar *str = NULL;
20130 xmlSchemaIDCPtr refer;
20131
20132 refer = (xmlSchemaIDCPtr) idc->ref->item;
20133 /*
20134 * SPEC c-props-correct(2)
20135 * "If the {identity-constraint category} is keyref,
20136 * the cardinality of the {fields} must equal that of
20137 * the {fields} of the {referenced key}.
20138 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020139 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020140 XML_SCHEMAP_C_PROPS_CORRECT,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020141 NULL, WXS_BASIC_CAST idc,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020142 "The cardinality of the keyref differs from the "
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020143 "cardinality of the referenced key/unique '%s'",
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020144 xmlSchemaFormatQName(&str, refer->targetNamespace,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020145 refer->name),
20146 NULL);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020147 FREE_AND_NULL(str)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020148 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020149 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020150 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020151 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020152 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020153}
20154
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020155static int
20156xmlSchemaResolveAttrUseProhibReferences(xmlSchemaAttributeUseProhibPtr prohib,
20157 xmlSchemaParserCtxtPtr pctxt)
20158{
20159 if (xmlSchemaGetAttributeDecl(pctxt->schema, prohib->name,
20160 prohib->targetNamespace) == NULL) {
20161
20162 xmlSchemaPResCompAttrErr(pctxt,
20163 XML_SCHEMAP_SRC_RESOLVE,
20164 NULL, prohib->node,
20165 "ref", prohib->name, prohib->targetNamespace,
20166 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
20167 return(XML_SCHEMAP_SRC_RESOLVE);
20168 }
20169 return(0);
20170}
20171
20172#define WXS_REDEFINED_TYPE(c) \
20173(((xmlSchemaTypePtr) item)->flags & XML_SCHEMAS_TYPE_REDEFINED)
20174
20175#define WXS_REDEFINED_MODEL_GROUP_DEF(c) \
20176(((xmlSchemaModelGroupDefPtr) item)->flags & XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20177
20178#define WXS_REDEFINED_ATTR_GROUP(c) \
20179(((xmlSchemaAttributeGroupPtr) item)->flags & XML_SCHEMAS_ATTRGROUP_REDEFINED)
20180
20181static int
20182xmlSchemaCheckSRCRedefineFirst(xmlSchemaParserCtxtPtr pctxt)
20183{
20184 int err = 0;
20185 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20186 xmlSchemaBasicItemPtr prev, item;
20187 int wasRedefined;
20188
20189 if (redef == NULL)
20190 return(0);
20191
20192 do {
20193 item = redef->item;
20194 /*
20195 * First try to locate the redefined component in the
20196 * schema graph starting with the redefined schema.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020197 * NOTE: According to this schema bug entry:
20198 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005OctDec/0019.html
20199 * it's not clear if the referenced component needs to originate
20200 * from the <redefine>d schema _document_ or the schema; the latter
20201 * would include all imported and included sub-schemas of the
20202 * <redefine>d schema. Currenlty we latter approach is used.
20203 * SUPPLEMENT: It seems that the WG moves towards the latter
20204 * approach, so we are doing it right.
20205 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020206 */
20207 prev = xmlSchemaFindRedefCompInGraph(
20208 redef->targetBucket, item->type,
20209 redef->refName, redef->refTargetNs);
20210 if (prev == NULL) {
20211 xmlChar *str = NULL;
20212 xmlNodePtr node;
20213
20214 /*
20215 * SPEC src-redefine:
20216 * (6.2.1) "The ·actual value· of its own name attribute plus
20217 * target namespace must successfully ·resolve· to a model
20218 * group definition in I."
20219 * (7.2.1) "The ·actual value· of its own name attribute plus
20220 * target namespace must successfully ·resolve· to an attribute
20221 * group definition in I."
20222
20223 *
20224 * Note that, if we are redefining with the use of references
20225 * to components, the spec assumes the src-resolve to be used;
20226 * but this won't assure that we search only *inside* the
20227 * redefined schema.
20228 */
20229 if (redef->reference)
20230 node = WXS_ITEM_NODE(redef->reference);
20231 else
20232 node = WXS_ITEM_NODE(item);
20233 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20234 /*
20235 * TODO: error code.
20236 * Probably XML_SCHEMAP_SRC_RESOLVE, if this is using the
20237 * reference kind.
20238 */
20239 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
Kasimier T. Buchcik5d2998b2005-11-22 17:36:01 +000020240 "The %s '%s' to be redefined could not be found in "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020241 "the redefined schema",
20242 WXS_ITEM_TYPE_NAME(item),
20243 xmlSchemaFormatQName(&str, redef->refTargetNs,
20244 redef->refName));
20245 FREE_AND_NULL(str);
20246 err = pctxt->err;
20247 redef = redef->next;
20248 continue;
20249 }
20250 /*
20251 * TODO: Obtaining and setting the redefinition state is really
20252 * clumsy.
20253 */
20254 wasRedefined = 0;
20255 switch (item->type) {
20256 case XML_SCHEMA_TYPE_COMPLEX:
20257 case XML_SCHEMA_TYPE_SIMPLE:
20258 if ((WXS_TYPE_CAST prev)->flags &
20259 XML_SCHEMAS_TYPE_REDEFINED)
20260 {
20261 wasRedefined = 1;
20262 break;
20263 }
20264 /* Mark it as redefined. */
20265 (WXS_TYPE_CAST prev)->flags |= XML_SCHEMAS_TYPE_REDEFINED;
20266 /*
20267 * Assign the redefined type to the
20268 * base type of the redefining type.
20269 * TODO: How
20270 */
20271 ((xmlSchemaTypePtr) item)->baseType =
20272 (xmlSchemaTypePtr) prev;
20273 break;
20274 case XML_SCHEMA_TYPE_GROUP:
20275 if ((WXS_MODEL_GROUPDEF_CAST prev)->flags &
20276 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20277 {
20278 wasRedefined = 1;
20279 break;
20280 }
20281 /* Mark it as redefined. */
20282 (WXS_MODEL_GROUPDEF_CAST prev)->flags |=
20283 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED;
20284 if (redef->reference != NULL) {
20285 /*
20286 * Overwrite the QName-reference with the
20287 * referenced model group def.
20288 */
20289 (WXS_PTC_CAST redef->reference)->children =
20290 WXS_TREE_CAST prev;
20291 }
20292 redef->target = prev;
20293 break;
20294 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20295 if ((WXS_ATTR_GROUP_CAST prev)->flags &
20296 XML_SCHEMAS_ATTRGROUP_REDEFINED)
20297 {
20298 wasRedefined = 1;
20299 break;
20300 }
20301 (WXS_ATTR_GROUP_CAST prev)->flags |=
20302 XML_SCHEMAS_ATTRGROUP_REDEFINED;
20303 if (redef->reference != NULL) {
20304 /*
20305 * Assign the redefined attribute group to the
20306 * QName-reference component.
20307 * This is the easy case, since we will just
20308 * expand the redefined group.
20309 */
20310 (WXS_QNAME_CAST redef->reference)->item = prev;
20311 redef->target = NULL;
20312 } else {
20313 /*
20314 * This is the complicated case: we need
20315 * to apply src-redefine (7.2.2) at a later
20316 * stage, i.e. when attribute group references
20317 * have beed expanded and simple types have
20318 * beed fixed.
20319 */
20320 redef->target = prev;
20321 }
20322 break;
20323 default:
20324 PERROR_INT("xmlSchemaResolveRedefReferences",
20325 "Unexpected redefined component type");
20326 return(-1);
20327 }
20328 if (wasRedefined) {
20329 xmlChar *str = NULL;
20330 xmlNodePtr node;
20331
20332 if (redef->reference)
20333 node = WXS_ITEM_NODE(redef->reference);
20334 else
20335 node = WXS_ITEM_NODE(redef->item);
20336
20337 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20338 /* TODO: error code. */
20339 XML_SCHEMAP_SRC_REDEFINE,
20340 node, NULL,
20341 "The referenced %s was already redefined. Multiple "
20342 "redefinition of the same component is not supported",
20343 xmlSchemaGetComponentDesignation(&str, prev),
20344 NULL);
20345 FREE_AND_NULL(str)
20346 err = pctxt->err;
20347 redef = redef->next;
20348 continue;
20349 }
20350 redef = redef->next;
20351 } while (redef != NULL);
20352
20353 return(err);
20354}
20355
20356static int
20357xmlSchemaCheckSRCRedefineSecond(xmlSchemaParserCtxtPtr pctxt)
20358{
20359 int err = 0;
20360 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20361 xmlSchemaBasicItemPtr item;
20362
20363 if (redef == NULL)
20364 return(0);
20365
20366 do {
20367 if (redef->target == NULL) {
20368 redef = redef->next;
20369 continue;
20370 }
20371 item = redef->item;
20372
20373 switch (item->type) {
20374 case XML_SCHEMA_TYPE_SIMPLE:
20375 case XML_SCHEMA_TYPE_COMPLEX:
20376 /*
20377 * Since the spec wants the {name} of the redefined
20378 * type to be 'absent', we'll NULL it.
20379 */
20380 (WXS_TYPE_CAST redef->target)->name = NULL;
20381
20382 /*
20383 * TODO: Seems like there's nothing more to do. The normal
20384 * inheritance mechanism is used. But not 100% sure.
20385 */
20386 break;
20387 case XML_SCHEMA_TYPE_GROUP:
20388 /*
20389 * URGENT TODO:
20390 * SPEC src-redefine:
20391 * (6.2.2) "The {model group} of the model group definition
20392 * which corresponds to it per XML Representation of Model
20393 * Group Definition Schema Components (§3.7.2) must be a
20394 * ·valid restriction· of the {model group} of that model
20395 * group definition in I, as defined in Particle Valid
20396 * (Restriction) (§3.9.6)."
20397 */
20398 break;
20399 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20400 /*
20401 * SPEC src-redefine:
20402 * (7.2.2) "The {attribute uses} and {attribute wildcard} of
20403 * the attribute group definition which corresponds to it
20404 * per XML Representation of Attribute Group Definition Schema
20405 * Components (§3.6.2) must be ·valid restrictions· of the
20406 * {attribute uses} and {attribute wildcard} of that attribute
20407 * group definition in I, as defined in clause 2, clause 3 and
20408 * clause 4 of Derivation Valid (Restriction, Complex)
20409 * (§3.4.6) (where references to the base type definition are
20410 * understood as references to the attribute group definition
20411 * in I)."
20412 */
20413 err = xmlSchemaCheckDerivationOKRestriction2to4(pctxt,
20414 XML_SCHEMA_ACTION_REDEFINE,
20415 item, redef->target,
20416 (WXS_ATTR_GROUP_CAST item)->attrUses,
20417 (WXS_ATTR_GROUP_CAST redef->target)->attrUses,
20418 (WXS_ATTR_GROUP_CAST item)->attributeWildcard,
20419 (WXS_ATTR_GROUP_CAST redef->target)->attributeWildcard);
20420 if (err == -1)
20421 return(-1);
20422 break;
20423 default:
20424 break;
20425 }
20426 redef = redef->next;
20427 } while (redef != NULL);
20428 return(0);
20429}
20430
20431
20432static int
20433xmlSchemaAddComponents(xmlSchemaParserCtxtPtr pctxt,
20434 xmlSchemaBucketPtr bucket)
20435{
20436 xmlSchemaBasicItemPtr item;
20437 int err;
20438 xmlHashTablePtr *table;
20439 const xmlChar *name;
20440 int i;
20441
20442#define WXS_GET_GLOBAL_HASH(c, s, slot) { \
20443 if (WXS_IS_BUCKET_IMPMAIN((c)->type)) \
20444 table = &(WXS_IMPBUCKET((c))->schema->slot); \
20445 else \
20446 table = &(WXS_INCBUCKET((c))->ownerImport->schema->slot); }
20447
20448 /*
20449 * Add global components to the schema's hash tables.
20450 * This is the place where duplicate components will be
20451 * detected.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020452 * TODO: I think normally we should support imports of the
20453 * same namespace from multiple locations. We don't do currently,
20454 * but if we do then according to:
20455 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2224
20456 * we would need, if imported directly, to import redefined
20457 * components as well to be able to catch clashing components.
20458 * (I hope I'll still know what this means after some months :-()
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020459 */
20460 if (bucket == NULL)
20461 return(-1);
20462 if (bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED)
20463 return(0);
20464 bucket->flags |= XML_SCHEMA_BUCKET_COMPS_ADDED;
20465
20466 for (i = 0; i < bucket->globals->nbItems; i++) {
20467 item = bucket->globals->items[i];
20468 table = NULL;
20469 switch (item->type) {
20470 case XML_SCHEMA_TYPE_COMPLEX:
20471 case XML_SCHEMA_TYPE_SIMPLE:
20472 if (WXS_REDEFINED_TYPE(item))
20473 continue;
20474 name = (WXS_TYPE_CAST item)->name;
20475 WXS_GET_GLOBAL_HASH(bucket, schema, typeDecl)
20476 break;
20477 case XML_SCHEMA_TYPE_ELEMENT:
20478 name = (WXS_ELEM_CAST item)->name;
20479 WXS_GET_GLOBAL_HASH(bucket, schema, elemDecl)
20480 break;
20481 case XML_SCHEMA_TYPE_ATTRIBUTE:
20482 name = (WXS_ATTR_CAST item)->name;
20483 WXS_GET_GLOBAL_HASH(bucket, schema, attrDecl)
20484 break;
20485 case XML_SCHEMA_TYPE_GROUP:
20486 if (WXS_REDEFINED_MODEL_GROUP_DEF(item))
20487 continue;
20488 name = (WXS_MODEL_GROUPDEF_CAST item)->name;
20489 WXS_GET_GLOBAL_HASH(bucket, schema, groupDecl)
20490 break;
20491 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20492 if (WXS_REDEFINED_ATTR_GROUP(item))
20493 continue;
20494 name = (WXS_ATTR_GROUP_CAST item)->name;
20495 WXS_GET_GLOBAL_HASH(bucket, schema, attrgrpDecl)
20496 break;
20497 case XML_SCHEMA_TYPE_IDC_KEY:
20498 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20499 case XML_SCHEMA_TYPE_IDC_KEYREF:
20500 name = (WXS_IDC_CAST item)->name;
20501 WXS_GET_GLOBAL_HASH(bucket, schema, idcDef)
20502 break;
20503 case XML_SCHEMA_TYPE_NOTATION:
20504 name = ((xmlSchemaNotationPtr) item)->name;
20505 WXS_GET_GLOBAL_HASH(bucket, schema, notaDecl)
20506 break;
20507 default:
20508 PERROR_INT("xmlSchemaAddComponents",
20509 "Unexpected global component type");
20510 continue;
20511 }
20512 if (*table == NULL) {
20513 *table = xmlHashCreateDict(10, pctxt->dict);
20514 if (*table == NULL) {
20515 PERROR_INT("xmlSchemaAddComponents",
20516 "failed to create a component hash table");
20517 return(-1);
20518 }
20519 }
20520 err = xmlHashAddEntry(*table, name, item);
20521 if (err != 0) {
20522 xmlChar *str = NULL;
20523
20524 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20525 XML_SCHEMAP_REDEFINED_TYPE,
20526 WXS_ITEM_NODE(item),
20527 WXS_BASIC_CAST item,
20528 "A global %s '%s' does already exist",
20529 WXS_ITEM_TYPE_NAME(item),
20530 xmlSchemaGetComponentQName(&str, item));
20531 FREE_AND_NULL(str);
20532 }
20533 }
20534 /*
20535 * Process imported/included schemas.
20536 */
20537 if (bucket->relations != NULL) {
20538 xmlSchemaSchemaRelationPtr rel = bucket->relations;
20539 do {
20540 if ((rel->bucket != NULL) &&
20541 ((rel->bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED) == 0)) {
20542 if (xmlSchemaAddComponents(pctxt, rel->bucket) == -1)
20543 return(-1);
20544 }
20545 rel = rel->next;
20546 } while (rel != NULL);
20547 }
20548 return(0);
20549}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020550
20551static int
20552xmlSchemaFixupComponents(xmlSchemaParserCtxtPtr pctxt)
20553{
20554 xmlSchemaConstructionCtxtPtr con = pctxt->constructor;
20555 xmlSchemaTreeItemPtr item, *items;
20556 int nbItems, i;
20557
20558#define FIXHFAILURE if (pctxt->err == XML_SCHEMAP_INTERNAL) goto exit_failure;
20559
20560 if ((con->pending == NULL) ||
20561 (con->pending->nbItems == 0))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020562 return(0);
20563
20564 /* TODO:
20565 * SPEC (src-redefine):
20566 * (6.2) "If it has no such self-reference, then all of the
20567 * following must be true:"
20568
20569 * (6.2.2) The {model group} of the model group definition which
20570 * corresponds to it per XML Representation of Model Group
20571 * Definition Schema Components (§3.7.2) must be a ·valid
20572 * restriction· of the {model group} of that model group definition
20573 * in I, as defined in Particle Valid (Restriction) (§3.9.6)."
20574 */
20575 xmlSchemaCheckSRCRedefineFirst(pctxt);
20576
20577 /*
20578 * Add global components to the schemata's hash tables.
20579 */
20580 xmlSchemaAddComponents(pctxt, WXS_CONSTRUCTOR(pctxt)->mainBucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020581
20582 pctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020583 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20584 nbItems = con->pending->nbItems;
20585 /*
20586 * Now that we have parsed *all* the schema document(s) and converted
20587 * them to schema components, we can resolve references, apply component
20588 * constraints, create the FSA from the content model, etc.
20589 */
20590 /*
20591 * Resolve references of..
20592 *
20593 * 1. element declarations:
20594 * - the type definition
20595 * - the substitution group affiliation
20596 * 2. simple/complex types:
20597 * - the base type definition
20598 * - the memberTypes of union types
20599 * - the itemType of list types
20600 * 3. attributes declarations and attribute uses:
20601 * - the type definition
20602 * - if an attribute use, then the attribute declaration
20603 * 4. attribute group references:
20604 * - the attribute group definition
20605 * 5. particles:
20606 * - the term of the particle (e.g. a model group)
20607 * 6. IDC key-references:
20608 * - the referenced IDC 'key' or 'unique' definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020609 * 7. Attribute prohibitions which had a "ref" attribute.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020610 */
20611 for (i = 0; i < nbItems; i++) {
20612 item = items[i];
20613 switch (item->type) {
20614 case XML_SCHEMA_TYPE_ELEMENT:
20615 xmlSchemaResolveElementReferences(
20616 (xmlSchemaElementPtr) item, pctxt);
20617 FIXHFAILURE;
20618 break;
20619 case XML_SCHEMA_TYPE_COMPLEX:
20620 case XML_SCHEMA_TYPE_SIMPLE:
20621 xmlSchemaResolveTypeReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020622 (xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020623 FIXHFAILURE;
20624 break;
20625 case XML_SCHEMA_TYPE_ATTRIBUTE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020626 xmlSchemaResolveAttrTypeReferences(
20627 (xmlSchemaAttributePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020628 FIXHFAILURE;
20629 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020630 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20631 xmlSchemaResolveAttrUseReferences(
20632 (xmlSchemaAttributeUsePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020633 FIXHFAILURE;
20634 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020635 case XML_SCHEMA_EXTRA_QNAMEREF:
20636 if ((WXS_QNAME_CAST item)->itemType ==
20637 XML_SCHEMA_TYPE_ATTRIBUTEGROUP)
20638 {
20639 xmlSchemaResolveAttrGroupReferences(
20640 WXS_QNAME_CAST item, pctxt);
20641 }
20642 FIXHFAILURE;
20643 break;
20644 case XML_SCHEMA_TYPE_SEQUENCE:
20645 case XML_SCHEMA_TYPE_CHOICE:
20646 case XML_SCHEMA_TYPE_ALL:
20647 xmlSchemaResolveModelGroupParticleReferences(pctxt,
20648 WXS_MODEL_GROUP_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020649 FIXHFAILURE;
20650 break;
20651 case XML_SCHEMA_TYPE_IDC_KEY:
20652 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20653 case XML_SCHEMA_TYPE_IDC_KEYREF:
20654 xmlSchemaResolveIDCKeyReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020655 (xmlSchemaIDCPtr) item, pctxt);
20656 FIXHFAILURE;
20657 break;
20658 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
20659 /*
20660 * Handle attribue prohibition which had a
20661 * "ref" attribute.
20662 */
20663 xmlSchemaResolveAttrUseProhibReferences(
20664 WXS_ATTR_PROHIB_CAST item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020665 FIXHFAILURE;
20666 break;
20667 default:
20668 break;
20669 }
20670 }
20671 if (pctxt->nberrors != 0)
20672 goto exit_error;
20673
20674 /*
20675 * Now that all references are resolved we
20676 * can check for circularity of...
20677 * 1. the base axis of type definitions
20678 * 2. nested model group definitions
20679 * 3. nested attribute group definitions
20680 * TODO: check for circual substitution groups.
20681 */
20682 for (i = 0; i < nbItems; i++) {
20683 item = items[i];
20684 /*
20685 * Let's better stop on the first error here.
20686 */
20687 switch (item->type) {
20688 case XML_SCHEMA_TYPE_COMPLEX:
20689 case XML_SCHEMA_TYPE_SIMPLE:
20690 xmlSchemaCheckTypeDefCircular(
20691 (xmlSchemaTypePtr) item, pctxt);
20692 FIXHFAILURE;
20693 if (pctxt->nberrors != 0)
20694 goto exit_error;
20695 break;
20696 case XML_SCHEMA_TYPE_GROUP:
20697 xmlSchemaCheckGroupDefCircular(
20698 (xmlSchemaModelGroupDefPtr) item, pctxt);
20699 FIXHFAILURE;
20700 if (pctxt->nberrors != 0)
20701 goto exit_error;
20702 break;
20703 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20704 xmlSchemaCheckAttrGroupCircular(
20705 (xmlSchemaAttributeGroupPtr) item, pctxt);
20706 FIXHFAILURE;
20707 if (pctxt->nberrors != 0)
20708 goto exit_error;
20709 break;
20710 default:
20711 break;
20712 }
20713 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020714 if (pctxt->nberrors != 0)
20715 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020716 /*
20717 * Model group definition references:
20718 * Such a reference is reflected by a particle at the component
20719 * level. Until now the 'term' of such particles pointed
20720 * to the model group definition; this was done, in order to
20721 * ease circularity checks. Now we need to set the 'term' of
20722 * such particles to the model group of the model group definition.
20723 */
20724 for (i = 0; i < nbItems; i++) {
20725 item = items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020726 switch (item->type) {
20727 case XML_SCHEMA_TYPE_SEQUENCE:
20728 case XML_SCHEMA_TYPE_CHOICE:
20729 xmlSchemaModelGroupToModelGroupDefFixup(pctxt,
20730 WXS_MODEL_GROUP_CAST item);
20731 break;
20732 default:
20733 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020734 }
20735 }
20736 if (pctxt->nberrors != 0)
20737 goto exit_error;
20738 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020739 * Expand attribute group references of attribute group definitions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020740 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020741 for (i = 0; i < nbItems; i++) {
20742 item = items[i];
20743 switch (item->type) {
20744 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20745 if ((! WXS_ATTR_GROUP_EXPANDED(item)) &&
20746 WXS_ATTR_GROUP_HAS_REFS(item))
20747 {
20748 xmlSchemaAttributeGroupExpandRefs(pctxt,
20749 WXS_ATTR_GROUP_CAST item);
20750 FIXHFAILURE;
20751 }
20752 break;
20753 default:
20754 break;
20755 }
20756 }
20757 if (pctxt->nberrors != 0)
20758 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020759 /*
20760 * First compute the variety of simple types. This is needed as
20761 * a seperate step, since otherwise we won't be able to detect
20762 * circular union types in all cases.
20763 */
20764 for (i = 0; i < nbItems; i++) {
20765 item = items[i];
20766 switch (item->type) {
20767 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020768 if (WXS_IS_TYPE_NOT_FIXED_1((xmlSchemaTypePtr) item)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020769 xmlSchemaFixupSimpleTypeStageOne(pctxt,
20770 (xmlSchemaTypePtr) item);
20771 FIXHFAILURE;
20772 }
20773 break;
20774 default:
20775 break;
20776 }
20777 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020778 if (pctxt->nberrors != 0)
20779 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020780 /*
20781 * Detect circular union types. Note that this needs the variety to
20782 * be already computed.
20783 */
20784 for (i = 0; i < nbItems; i++) {
20785 item = items[i];
20786 switch (item->type) {
20787 case XML_SCHEMA_TYPE_SIMPLE:
20788 if (((xmlSchemaTypePtr) item)->memberTypes != NULL) {
20789 xmlSchemaCheckUnionTypeDefCircular(pctxt,
20790 (xmlSchemaTypePtr) item);
20791 FIXHFAILURE;
20792 }
20793 break;
20794 default:
20795 break;
20796 }
20797 }
20798 if (pctxt->nberrors != 0)
20799 goto exit_error;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020800
20801 /*
20802 * Do the complete type fixup for simple types.
20803 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020804 for (i = 0; i < nbItems; i++) {
20805 item = items[i];
20806 switch (item->type) {
20807 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020808 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20809 xmlSchemaFixupSimpleTypeStageTwo(pctxt, WXS_TYPE_CAST item);
20810 FIXHFAILURE;
20811 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020812 break;
20813 default:
20814 break;
20815 }
20816 }
20817 if (pctxt->nberrors != 0)
20818 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020819 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020820 * At this point we need all simple types to be builded and checked.
20821 */
20822 /*
20823 * Apply contraints for attribute declarations.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020824 */
20825 for (i = 0; i < nbItems; i++) {
20826 item = items[i];
20827 switch (item->type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020828 case XML_SCHEMA_TYPE_ATTRIBUTE:
20829 xmlSchemaCheckAttrPropsCorrect(pctxt, WXS_ATTR_CAST item);
20830 FIXHFAILURE;
20831 break;
20832 default:
20833 break;
20834 }
20835 }
20836 if (pctxt->nberrors != 0)
20837 goto exit_error;
20838 /*
20839 * Apply constraints for attribute uses.
20840 */
20841 for (i = 0; i < nbItems; i++) {
20842 item = items[i];
20843 switch (item->type) {
20844 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20845 if (((xmlSchemaAttributeUsePtr)item)->defValue != NULL) {
20846 xmlSchemaCheckAttrUsePropsCorrect(pctxt,
20847 WXS_ATTR_USE_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020848 FIXHFAILURE;
20849 }
20850 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020851 default:
20852 break;
20853 }
20854 }
20855 if (pctxt->nberrors != 0)
20856 goto exit_error;
20857
20858 /*
20859 * Apply constraints for attribute group definitions.
20860 */
20861 for (i = 0; i < nbItems; i++) {
20862 item = items[i];
20863 switch (item->type) {
20864 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20865 if (( (WXS_ATTR_GROUP_CAST item)->attrUses != NULL) &&
20866 ( (WXS_LIST_CAST (WXS_ATTR_GROUP_CAST item)->attrUses)->nbItems > 1))
20867 {
20868 xmlSchemaCheckAGPropsCorrect(pctxt, WXS_ATTR_GROUP_CAST item);
20869 FIXHFAILURE;
20870 }
20871 break;
20872 default:
20873 break;
20874 }
20875 }
20876 if (pctxt->nberrors != 0)
20877 goto exit_error;
20878
20879 /*
20880 * Apply constraints for redefinitions.
20881 */
20882 if (WXS_CONSTRUCTOR(pctxt)->redefs != NULL)
20883 xmlSchemaCheckSRCRedefineSecond(pctxt);
20884 if (pctxt->nberrors != 0)
20885 goto exit_error;
20886
20887 /*
20888 * Fixup complex types.
20889 */
20890 for (i = 0; i < nbItems; i++) {
20891 item = con->pending->items[i];
20892 switch (item->type) {
20893 case XML_SCHEMA_TYPE_COMPLEX:
20894 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20895 xmlSchemaFixupComplexType(pctxt, WXS_TYPE_CAST item);
20896 FIXHFAILURE;
20897 }
20898 break;
20899 default:
20900 break;
20901 }
20902 }
20903 if (pctxt->nberrors != 0)
20904 goto exit_error;
20905
20906 /*
20907 * The list could have changed, since xmlSchemaFixupComplexType()
20908 * will create particles and model groups in some cases.
20909 */
20910 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20911 nbItems = con->pending->nbItems;
20912
20913 /*
20914 * At this point all complex types need to be builded and checked.
20915 */
20916 /*
20917 * Apply some constraints for element declarations.
20918 */
20919 for (i = 0; i < nbItems; i++) {
20920 item = items[i];
20921 switch (item->type) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020922 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020923
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020924 if ((((xmlSchemaElementPtr) item)->flags &
20925 XML_SCHEMAS_ELEM_INTERNAL_CHECKED) == 0) {
20926 xmlSchemaCheckElementDeclComponent(
20927 (xmlSchemaElementPtr) item, pctxt);
20928 FIXHFAILURE;
20929 }
20930 break;
20931 default:
20932 break;
20933 }
20934 }
20935 if (pctxt->nberrors != 0)
20936 goto exit_error;
20937 /*
20938 * Finally we can build the automaton from the content model of
20939 * complex types.
20940 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020941
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020942 for (i = 0; i < nbItems; i++) {
20943 item = items[i];
20944 switch (item->type) {
20945 case XML_SCHEMA_TYPE_COMPLEX:
20946 xmlSchemaBuildContentModel((xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020947 /* FIXHFAILURE; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020948 break;
20949 default:
20950 break;
20951 }
20952 }
20953 if (pctxt->nberrors != 0)
20954 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020955 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020956 * URGENT TODO: cos-element-consistent
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020957 */
20958 con->pending->nbItems = 0;
20959 return(0);
20960exit_error:
20961 con->pending->nbItems = 0;
20962 return(pctxt->err);
20963exit_failure:
20964 con->pending->nbItems = 0;
20965 return(-1);
20966}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020967/**
Daniel Veillard4255d502002-04-16 15:50:10 +000020968 * xmlSchemaParse:
20969 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000020970 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000020971 * parse a schema definition resource and build an internal
Daniel Veillard4255d502002-04-16 15:50:10 +000020972 * XML Shema struture which can be used to validate instances.
Daniel Veillard4255d502002-04-16 15:50:10 +000020973 *
20974 * Returns the internal XML Schema structure built from the resource or
20975 * NULL in case of error
20976 */
20977xmlSchemaPtr
20978xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt)
20979{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020980 xmlSchemaPtr schema = NULL;
20981 xmlSchemaBucketPtr bucket = NULL;
20982 int res;
Daniel Veillard4255d502002-04-16 15:50:10 +000020983
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020984 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020985 * This one is used if the schema to be parsed was specified via
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020986 * the API; i.e. not automatically by the validated instance document.
20987 */
20988
Daniel Veillard4255d502002-04-16 15:50:10 +000020989 xmlSchemaInitTypes();
20990
Daniel Veillard6045c902002-10-09 21:13:59 +000020991 if (ctxt == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +000020992 return (NULL);
20993
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020994 /* TODO: Init the context. Is this all we need?*/
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000020995 ctxt->nberrors = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020996 ctxt->err = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000020997 ctxt->counter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000020998
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020999 /* Create the *main* schema. */
21000 schema = xmlSchemaNewSchema(ctxt);
21001 if (schema == NULL)
21002 goto exit_failure;
Daniel Veillard4255d502002-04-16 15:50:10 +000021003 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021004 * Create the schema constructor.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021005 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021006 if (ctxt->constructor == NULL) {
21007 ctxt->constructor = xmlSchemaConstructionCtxtCreate(ctxt->dict);
21008 if (ctxt->constructor == NULL)
21009 return(NULL);
21010 /* Take ownership of the constructor to be able to free it. */
21011 ctxt->ownsConstructor = 1;
21012 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021013 ctxt->constructor->mainSchema = schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021014 /*
21015 * Locate and add the schema document.
21016 */
21017 res = xmlSchemaAddSchemaDoc(ctxt, XML_SCHEMA_SCHEMA_MAIN,
21018 ctxt->URL, ctxt->doc, ctxt->buffer, ctxt->size, NULL,
21019 NULL, NULL, &bucket);
21020 if (res == -1)
21021 goto exit_failure;
21022 if (res != 0)
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000021023 goto exit;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021024
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021025 if (bucket == NULL) {
21026 /* TODO: Error code, actually we failed to *locate* the schema. */
21027 if (ctxt->URL)
21028 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21029 NULL, NULL,
21030 "Failed to locate the main schema resource at '%s'",
21031 ctxt->URL, NULL);
21032 else
21033 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21034 NULL, NULL,
21035 "Failed to locate the main schema resource",
21036 NULL, NULL);
21037 goto exit;
21038 }
21039 /* Set the main schema bucket. */
21040 ctxt->constructor->bucket = bucket;
21041 ctxt->targetNamespace = bucket->targetNamespace;
21042 schema->targetNamespace = bucket->targetNamespace;
21043
21044 /* Then do the parsing for good. */
21045 if (xmlSchemaParseNewDocWithContext(ctxt, schema, bucket) == -1)
21046 goto exit_failure;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021047 if (ctxt->nberrors != 0)
21048 goto exit;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021049
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021050 schema->doc = bucket->doc;
21051 schema->preserve = ctxt->preserve;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021052
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021053 ctxt->schema = schema;
21054
21055 if (xmlSchemaFixupComponents(ctxt) == -1)
21056 goto exit_failure;
21057
21058 /*
21059 * TODO: This is not nice, since we cannot distinguish from the
21060 * result if there was an internal error or not.
21061 */
21062exit:
21063 if (ctxt->nberrors != 0) {
21064 if (schema) {
21065 xmlSchemaFree(schema);
21066 schema = NULL;
21067 }
21068 if (ctxt->constructor) {
21069 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21070 ctxt->constructor = NULL;
21071 ctxt->ownsConstructor = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021072 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021073 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021074 ctxt->schema = NULL;
21075 return(schema);
21076exit_failure:
21077 /*
21078 * Quite verbose, but should catch internal errors, which were
21079 * not communitated.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021080 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021081 if (schema) {
21082 xmlSchemaFree(schema);
21083 schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021084 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021085 if (ctxt->constructor) {
21086 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21087 ctxt->constructor = NULL;
21088 ctxt->ownsConstructor = 0;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021089 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021090 PERROR_INT2("xmlSchemaParse",
21091 "An internal error occured");
21092 ctxt->schema = NULL;
21093 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000021094}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021095
Daniel Veillard4255d502002-04-16 15:50:10 +000021096/**
Daniel Veillard01c13b52002-12-10 15:19:08 +000021097 * xmlSchemaSetParserErrors:
Daniel Veillard4255d502002-04-16 15:50:10 +000021098 * @ctxt: a schema validation context
Daniel Veillard01c13b52002-12-10 15:19:08 +000021099 * @err: the error callback
21100 * @warn: the warning callback
21101 * @ctx: contextual data for the callbacks
Daniel Veillard4255d502002-04-16 15:50:10 +000021102 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021103 * Set the callback functions used to handle errors for a validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021104 */
21105void
21106xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021107 xmlSchemaValidityErrorFunc err,
21108 xmlSchemaValidityWarningFunc warn, void *ctx)
21109{
Daniel Veillard4255d502002-04-16 15:50:10 +000021110 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021111 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000021112 ctxt->error = err;
21113 ctxt->warning = warn;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021114 ctxt->errCtxt = ctx;
21115 if (ctxt->vctxt != NULL)
21116 xmlSchemaSetValidErrors(ctxt->vctxt, err, warn, ctx);
21117}
21118
21119/**
21120 * xmlSchemaSetParserStructuredErrors:
21121 * @ctxt: a schema parser context
21122 * @serror: the structured error function
21123 * @ctx: the functions context
21124 *
21125 * Set the structured error callback
21126 */
21127void
21128xmlSchemaSetParserStructuredErrors(xmlSchemaParserCtxtPtr ctxt,
21129 xmlStructuredErrorFunc serror,
21130 void *ctx)
21131{
21132 if (ctxt == NULL)
21133 return;
21134 ctxt->serror = serror;
21135 ctxt->errCtxt = ctx;
21136 if (ctxt->vctxt != NULL)
21137 xmlSchemaSetValidStructuredErrors(ctxt->vctxt, serror, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000021138}
21139
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021140/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000021141 * xmlSchemaGetParserErrors:
21142 * @ctxt: a XMl-Schema parser context
21143 * @err: the error callback result
21144 * @warn: the warning callback result
21145 * @ctx: contextual data for the callbacks result
21146 *
21147 * Get the callback information used to handle errors for a parser context
21148 *
21149 * Returns -1 in case of failure, 0 otherwise
21150 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021151int
Daniel Veillard259f0df2004-08-18 09:13:18 +000021152xmlSchemaGetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021153 xmlSchemaValidityErrorFunc * err,
21154 xmlSchemaValidityWarningFunc * warn, void **ctx)
Daniel Veillard259f0df2004-08-18 09:13:18 +000021155{
21156 if (ctxt == NULL)
21157 return(-1);
21158 if (err != NULL)
21159 *err = ctxt->error;
21160 if (warn != NULL)
21161 *warn = ctxt->warning;
21162 if (ctx != NULL)
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021163 *ctx = ctxt->errCtxt;
Daniel Veillard259f0df2004-08-18 09:13:18 +000021164 return(0);
21165}
21166
21167/**
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021168 * xmlSchemaFacetTypeToString:
21169 * @type: the facet type
21170 *
21171 * Convert the xmlSchemaTypeType to a char string.
21172 *
21173 * Returns the char string representation of the facet type if the
21174 * type is a facet and an "Internal Error" string otherwise.
21175 */
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021176static const xmlChar *
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021177xmlSchemaFacetTypeToString(xmlSchemaTypeType type)
21178{
21179 switch (type) {
21180 case XML_SCHEMA_FACET_PATTERN:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021181 return (BAD_CAST "pattern");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021182 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021183 return (BAD_CAST "maxExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021184 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021185 return (BAD_CAST "maxInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021186 case XML_SCHEMA_FACET_MINEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021187 return (BAD_CAST "minExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021188 case XML_SCHEMA_FACET_MININCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021189 return (BAD_CAST "minInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021190 case XML_SCHEMA_FACET_WHITESPACE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021191 return (BAD_CAST "whiteSpace");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021192 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021193 return (BAD_CAST "enumeration");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021194 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021195 return (BAD_CAST "length");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021196 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021197 return (BAD_CAST "maxLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021198 case XML_SCHEMA_FACET_MINLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021199 return (BAD_CAST "minLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021200 case XML_SCHEMA_FACET_TOTALDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021201 return (BAD_CAST "totalDigits");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021202 case XML_SCHEMA_FACET_FRACTIONDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021203 return (BAD_CAST "fractionDigits");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021204 default:
21205 break;
21206 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021207 return (BAD_CAST "Internal Error");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021208}
21209
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021210static xmlSchemaWhitespaceValueType
Daniel Veillardc0826a72004-08-10 14:17:33 +000021211xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type)
21212{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021213 /*
21214 * The normalization type can be changed only for types which are derived
Daniel Veillardc0826a72004-08-10 14:17:33 +000021215 * from xsd:string.
21216 */
21217 if (type->type == XML_SCHEMA_TYPE_BASIC) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021218 /*
21219 * Note that we assume a whitespace of preserve for anySimpleType.
21220 */
21221 if ((type->builtInType == XML_SCHEMAS_STRING) ||
21222 (type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
21223 return(XML_SCHEMA_WHITESPACE_PRESERVE);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +000021224 else if (type->builtInType == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021225 return(XML_SCHEMA_WHITESPACE_REPLACE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021226 else {
21227 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021228 * For all ·atomic· datatypes other than string (and types ·derived·
21229 * by ·restriction· from it) the value of whiteSpace is fixed to
Daniel Veillardc0826a72004-08-10 14:17:33 +000021230 * collapse
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021231 * Note that this includes built-in list datatypes.
Daniel Veillardc0826a72004-08-10 14:17:33 +000021232 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021233 return(XML_SCHEMA_WHITESPACE_COLLAPSE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021234 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021235 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021236 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021237 * For list types the facet "whiteSpace" is fixed to "collapse".
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021238 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021239 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021240 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021241 return (XML_SCHEMA_WHITESPACE_UNKNOWN);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021242 } else if (WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021243 if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE)
21244 return (XML_SCHEMA_WHITESPACE_PRESERVE);
21245 else if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_REPLACE)
21246 return (XML_SCHEMA_WHITESPACE_REPLACE);
21247 else
21248 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021249 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021250 return (-1);
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021251}
21252
Daniel Veillard4255d502002-04-16 15:50:10 +000021253/************************************************************************
21254 * *
21255 * Simple type validation *
21256 * *
21257 ************************************************************************/
Daniel Veillard377e1a92004-04-16 16:30:05 +000021258
Daniel Veillard4255d502002-04-16 15:50:10 +000021259
21260/************************************************************************
21261 * *
21262 * DOM Validation code *
21263 * *
21264 ************************************************************************/
21265
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021266/**
21267 * xmlSchemaAssembleByLocation:
21268 * @pctxt: a schema parser context
21269 * @vctxt: a schema validation context
21270 * @schema: the existing schema
21271 * @node: the node that fired the assembling
21272 * @nsName: the namespace name of the new schema
21273 * @location: the location of the schema
21274 *
21275 * Expands an existing schema by an additional schema.
21276 *
21277 * Returns 0 if the new schema is correct, a positive error code
21278 * number otherwise and -1 in case of an internal or API error.
21279 */
21280static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021281xmlSchemaAssembleByLocation(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021282 xmlSchemaPtr schema,
21283 xmlNodePtr node,
21284 const xmlChar *nsName,
21285 const xmlChar *location)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021286{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021287 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021288 xmlSchemaParserCtxtPtr pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021289 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021290
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021291 if ((vctxt == NULL) || (schema == NULL))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021292 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021293
21294 if (vctxt->pctxt == NULL) {
21295 VERROR_INT("xmlSchemaAssembleByLocation",
21296 "no parser context available");
21297 return(-1);
21298 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021299 pctxt = vctxt->pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021300 if (pctxt->constructor == NULL) {
21301 PERROR_INT("xmlSchemaAssembleByLocation",
21302 "no constructor");
21303 return(-1);
21304 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021305 /*
21306 * Acquire the schema document.
21307 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021308 location = xmlSchemaBuildAbsoluteURI(pctxt->dict,
21309 location, node);
21310 /*
21311 * Note that we pass XML_SCHEMA_SCHEMA_IMPORT here;
21312 * the process will automatically change this to
21313 * XML_SCHEMA_SCHEMA_MAIN if it is the first schema document.
21314 */
21315 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
21316 location, NULL, NULL, 0, node, NULL, nsName,
21317 &bucket);
21318 if (ret != 0)
21319 return(ret);
21320 if (bucket == NULL) {
21321 PERROR_INT("xmlSchemaAssembleByLocation",
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021322 "no schema bucket acquired");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021323 return(-1);
21324 }
21325 /*
21326 * The first located schema will be handled as if all other
21327 * schemas imported by XSI were imported by this first schema.
21328 */
21329 if ((bucket != NULL) &&
21330 (WXS_CONSTRUCTOR(pctxt)->bucket == NULL))
21331 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
21332 /*
21333 * TODO: Is this handled like an import? I.e. is it not an error
21334 * if the schema cannot be located?
21335 */
21336 if ((bucket == NULL) || (! CAN_PARSE_SCHEMA(bucket)))
21337 return(0);
21338 /*
21339 * We will reuse the parser context for every schema imported
21340 * directly via XSI. So reset the context.
21341 */
21342 pctxt->nberrors = 0;
21343 pctxt->err = 0;
21344 pctxt->doc = bucket->doc;
21345
21346 ret = xmlSchemaParseNewDocWithContext(pctxt, schema, bucket);
21347 if (ret == -1) {
21348 pctxt->doc = NULL;
21349 goto exit_failure;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021350 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021351 /* Paranoid error channelling. */
21352 if ((ret == 0) && (pctxt->nberrors != 0))
21353 ret = pctxt->err;
21354 if (pctxt->nberrors == 0) {
21355 /*
21356 * Only bother to fixup pending components, if there was
21357 * no error yet.
21358 */
21359 xmlSchemaFixupComponents(pctxt);
21360 vctxt->nberrors += pctxt->nberrors;
21361 } else {
21362 /* Add to validation error sum. */
21363 vctxt->nberrors += pctxt->nberrors;
21364 }
21365 pctxt->doc = NULL;
21366 return(ret);
21367exit_failure:
21368 pctxt->doc = NULL;
21369 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021370}
21371
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021372static xmlSchemaAttrInfoPtr
21373xmlSchemaGetMetaAttrInfo(xmlSchemaValidCtxtPtr vctxt,
21374 int metaType)
21375{
21376 if (vctxt->nbAttrInfos == 0)
21377 return (NULL);
21378 {
21379 int i;
21380 xmlSchemaAttrInfoPtr iattr;
21381
21382 for (i = 0; i < vctxt->nbAttrInfos; i++) {
21383 iattr = vctxt->attrInfos[i];
21384 if (iattr->metaType == metaType)
21385 return (iattr);
21386 }
21387
21388 }
21389 return (NULL);
21390}
21391
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021392/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021393 * xmlSchemaAssembleByXSI:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021394 * @vctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021395 *
21396 * Expands an existing schema by an additional schema using
21397 * the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute
21398 * of an instance. If xsi:noNamespaceSchemaLocation is used, @noNamespace
21399 * must be set to 1.
21400 *
21401 * Returns 0 if the new schema is correct, a positive error code
21402 * number otherwise and -1 in case of an internal or API error.
21403 */
21404static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021405xmlSchemaAssembleByXSI(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021406{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021407 const xmlChar *cur, *end;
21408 const xmlChar *nsname = NULL, *location;
21409 int count = 0;
21410 int ret = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021411 xmlSchemaAttrInfoPtr iattr;
21412
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021413 /*
21414 * Parse the value; we will assume an even number of values
21415 * to be given (this is how Xerces and XSV work).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021416 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021417 * URGENT TODO: !! This needs to work for both
21418 * @noNamespaceSchemaLocation AND @schemaLocation on the same
21419 * element !!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021420 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021421 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
21422 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC);
21423 if (iattr == NULL)
Kasimier T. Buchcik81755ea2005-11-23 16:39:27 +000021424 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021425 XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC);
21426 if (iattr == NULL)
21427 return (0);
21428 cur = iattr->value;
21429 do {
21430 if (iattr->metaType == XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021431 /*
21432 * Get the namespace name.
21433 */
21434 while (IS_BLANK_CH(*cur))
21435 cur++;
21436 end = cur;
21437 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21438 end++;
21439 if (end == cur)
21440 break;
21441 count++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021442 nsname = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021443 cur = end;
21444 }
21445 /*
21446 * Get the URI.
21447 */
21448 while (IS_BLANK_CH(*cur))
21449 cur++;
21450 end = cur;
21451 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21452 end++;
21453 if (end == cur)
21454 break;
21455 count++;
21456 location = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021457 cur = end;
21458 ret = xmlSchemaAssembleByLocation(vctxt, vctxt->schema,
21459 iattr->node, nsname, location);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021460 if (ret == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021461 VERROR_INT("xmlSchemaAssembleByXSI",
21462 "assembling schemata");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021463 return (-1);
21464 }
21465 } while (*cur != 0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021466 return (ret);
21467}
21468
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021469static const xmlChar *
21470xmlSchemaLookupNamespace(xmlSchemaValidCtxtPtr vctxt,
21471 const xmlChar *prefix)
21472{
21473 if (vctxt->sax != NULL) {
21474 int i, j;
21475 xmlSchemaNodeInfoPtr inode;
21476
21477 for (i = vctxt->depth; i >= 0; i--) {
21478 if (vctxt->elemInfos[i]->nbNsBindings != 0) {
21479 inode = vctxt->elemInfos[i];
21480 for (j = 0; j < inode->nbNsBindings * 2; j += 2) {
21481 if (((prefix == NULL) &&
21482 (inode->nsBindings[j] == NULL)) ||
21483 ((prefix != NULL) && xmlStrEqual(prefix,
21484 inode->nsBindings[j]))) {
21485
21486 /*
21487 * Note that the namespace bindings are already
21488 * in a string dict.
21489 */
21490 return (inode->nsBindings[j+1]);
21491 }
21492 }
21493 }
21494 }
21495 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021496#ifdef LIBXML_WRITER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021497 } else if (vctxt->reader != NULL) {
21498 xmlChar *nsName;
21499
21500 nsName = xmlTextReaderLookupNamespace(vctxt->reader, prefix);
21501 if (nsName != NULL) {
21502 const xmlChar *ret;
21503
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021504 ret = xmlDictLookup(vctxt->dict, nsName, -1);
21505 xmlFree(nsName);
21506 return (ret);
21507 } else
21508 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021509#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021510 } else {
21511 xmlNsPtr ns;
21512
21513 if ((vctxt->inode->node == NULL) ||
21514 (vctxt->inode->node->doc == NULL)) {
21515 VERROR_INT("xmlSchemaLookupNamespace",
21516 "no node or node's doc avaliable");
21517 return (NULL);
21518 }
21519 ns = xmlSearchNs(vctxt->inode->node->doc,
21520 vctxt->inode->node, prefix);
21521 if (ns != NULL)
21522 return (ns->href);
21523 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021524 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021525}
21526
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021527/*
21528* This one works on the schema of the validation context.
21529*/
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021530static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021531xmlSchemaValidateNotation(xmlSchemaValidCtxtPtr vctxt,
21532 xmlSchemaPtr schema,
21533 xmlNodePtr node,
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021534 const xmlChar *value,
21535 xmlSchemaValPtr *val,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021536 int valNeeded)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021537{
21538 int ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021539
21540 if (vctxt && (vctxt->schema == NULL)) {
21541 VERROR_INT("xmlSchemaValidateNotation",
21542 "a schema is needed on the validation context");
21543 return (-1);
21544 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021545 ret = xmlValidateQName(value, 1);
21546 if (ret != 0)
21547 return (ret);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021548 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021549 xmlChar *localName = NULL;
21550 xmlChar *prefix = NULL;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021551
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021552 localName = xmlSplitQName2(value, &prefix);
21553 if (prefix != NULL) {
21554 const xmlChar *nsName = NULL;
21555
21556 if (vctxt != NULL)
21557 nsName = xmlSchemaLookupNamespace(vctxt, BAD_CAST prefix);
21558 else if (node != NULL) {
21559 xmlNsPtr ns = xmlSearchNs(node->doc, node, prefix);
21560 if (ns != NULL)
21561 nsName = ns->href;
21562 } else {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021563 xmlFree(prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021564 xmlFree(localName);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021565 return (1);
21566 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021567 if (nsName == NULL) {
21568 xmlFree(prefix);
21569 xmlFree(localName);
21570 return (1);
21571 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021572 if (xmlSchemaGetNotation(schema, localName, nsName) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021573 if (valNeeded && (val != NULL)) {
21574 (*val) = xmlSchemaNewNOTATIONValue(BAD_CAST localName,
21575 BAD_CAST xmlStrdup(nsName));
21576 if (*val == NULL)
21577 ret = -1;
21578 }
21579 } else
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021580 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021581 xmlFree(prefix);
21582 xmlFree(localName);
21583 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021584 if (xmlSchemaGetNotation(schema, value, NULL) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021585 if (valNeeded && (val != NULL)) {
21586 (*val) = xmlSchemaNewNOTATIONValue(
21587 BAD_CAST xmlStrdup(value), NULL);
21588 if (*val == NULL)
21589 ret = -1;
21590 }
21591 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021592 return (1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021593 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021594 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021595 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021596}
21597
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000021598static int
21599xmlSchemaVAddNodeQName(xmlSchemaValidCtxtPtr vctxt,
21600 const xmlChar* lname,
21601 const xmlChar* nsname)
21602{
21603 int i;
21604
21605 lname = xmlDictLookup(vctxt->dict, lname, -1);
21606 if (lname == NULL)
21607 return(-1);
21608 if (nsname != NULL) {
21609 nsname = xmlDictLookup(vctxt->dict, nsname, -1);
21610 if (nsname == NULL)
21611 return(-1);
21612 }
21613 for (i = 0; i < vctxt->nodeQNames->nbItems; i += 2) {
21614 if ((vctxt->nodeQNames->items [i] == lname) &&
21615 (vctxt->nodeQNames->items[i +1] == nsname))
21616 /* Already there */
21617 return(i);
21618 }
21619 /* Add new entry. */
21620 i = vctxt->nodeQNames->nbItems;
21621 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) lname);
21622 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) nsname);
21623 return(i);
21624}
21625
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021626/************************************************************************
21627 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021628 * Validation of identity-constraints (IDC) *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021629 * *
21630 ************************************************************************/
21631
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021632/**
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021633 * xmlSchemaAugmentIDC:
21634 * @idcDef: the IDC definition
21635 *
21636 * Creates an augmented IDC definition item.
21637 *
21638 * Returns the item, or NULL on internal errors.
21639 */
21640static void
21641xmlSchemaAugmentIDC(xmlSchemaIDCPtr idcDef,
21642 xmlSchemaValidCtxtPtr vctxt)
21643{
21644 xmlSchemaIDCAugPtr aidc;
21645
21646 aidc = (xmlSchemaIDCAugPtr) xmlMalloc(sizeof(xmlSchemaIDCAug));
21647 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021648 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021649 "xmlSchemaAugmentIDC: allocating an augmented IDC definition",
21650 NULL);
21651 return;
21652 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021653 aidc->keyrefDepth = -1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021654 aidc->def = idcDef;
21655 aidc->next = NULL;
21656 if (vctxt->aidcs == NULL)
21657 vctxt->aidcs = aidc;
21658 else {
21659 aidc->next = vctxt->aidcs;
21660 vctxt->aidcs = aidc;
21661 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021662 /*
21663 * Save if we have keyrefs at all.
21664 */
21665 if ((vctxt->hasKeyrefs == 0) &&
21666 (idcDef->type == XML_SCHEMA_TYPE_IDC_KEYREF))
21667 vctxt->hasKeyrefs = 1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021668}
21669
21670/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021671 * xmlSchemaIDCNewBinding:
21672 * @idcDef: the IDC definition of this binding
21673 *
21674 * Creates a new IDC binding.
21675 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021676 * Returns the new IDC binding, NULL on internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021677 */
21678static xmlSchemaPSVIIDCBindingPtr
21679xmlSchemaIDCNewBinding(xmlSchemaIDCPtr idcDef)
21680{
21681 xmlSchemaPSVIIDCBindingPtr ret;
21682
21683 ret = (xmlSchemaPSVIIDCBindingPtr) xmlMalloc(
21684 sizeof(xmlSchemaPSVIIDCBinding));
21685 if (ret == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021686 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021687 "allocating a PSVI IDC binding item", NULL);
21688 return (NULL);
21689 }
21690 memset(ret, 0, sizeof(xmlSchemaPSVIIDCBinding));
21691 ret->definition = idcDef;
21692 return (ret);
21693}
21694
21695/**
21696 * xmlSchemaIDCStoreNodeTableItem:
21697 * @vctxt: the WXS validation context
21698 * @item: the IDC node table item
21699 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021700 * The validation context is used to store IDC node table items.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021701 * They are stored to avoid copying them if IDC node-tables are merged
21702 * with corresponding parent IDC node-tables (bubbling).
21703 *
21704 * Returns 0 if succeeded, -1 on internal errors.
21705 */
21706static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021707xmlSchemaIDCStoreNodeTableItem(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021708 xmlSchemaPSVIIDCNodePtr item)
21709{
21710 /*
21711 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021712 */
21713 if (vctxt->idcNodes == NULL) {
21714 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021715 xmlMalloc(20 * sizeof(xmlSchemaPSVIIDCNodePtr));
21716 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021717 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021718 "allocating the IDC node table item list", NULL);
21719 return (-1);
21720 }
21721 vctxt->sizeIdcNodes = 20;
21722 } else if (vctxt->sizeIdcNodes <= vctxt->nbIdcNodes) {
21723 vctxt->sizeIdcNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021724 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
21725 xmlRealloc(vctxt->idcNodes, vctxt->sizeIdcNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021726 sizeof(xmlSchemaPSVIIDCNodePtr));
21727 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021728 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021729 "re-allocating the IDC node table item list", NULL);
21730 return (-1);
21731 }
21732 }
21733 vctxt->idcNodes[vctxt->nbIdcNodes++] = item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021734
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021735 return (0);
21736}
21737
21738/**
21739 * xmlSchemaIDCStoreKey:
21740 * @vctxt: the WXS validation context
21741 * @item: the IDC key
21742 *
21743 * The validation context is used to store an IDC key.
21744 *
21745 * Returns 0 if succeeded, -1 on internal errors.
21746 */
21747static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021748xmlSchemaIDCStoreKey(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021749 xmlSchemaPSVIIDCKeyPtr key)
21750{
21751 /*
21752 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021753 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021754 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021755 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021756 xmlMalloc(40 * sizeof(xmlSchemaPSVIIDCKeyPtr));
21757 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021758 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021759 "allocating the IDC key storage list", NULL);
21760 return (-1);
21761 }
21762 vctxt->sizeIdcKeys = 40;
21763 } else if (vctxt->sizeIdcKeys <= vctxt->nbIdcKeys) {
21764 vctxt->sizeIdcKeys *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021765 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
21766 xmlRealloc(vctxt->idcKeys, vctxt->sizeIdcKeys *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021767 sizeof(xmlSchemaPSVIIDCKeyPtr));
21768 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021769 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021770 "re-allocating the IDC key storage list", NULL);
21771 return (-1);
21772 }
21773 }
21774 vctxt->idcKeys[vctxt->nbIdcKeys++] = key;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021775
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021776 return (0);
21777}
21778
21779/**
21780 * xmlSchemaIDCAppendNodeTableItem:
21781 * @bind: the IDC binding
21782 * @ntItem: the node-table item
21783 *
21784 * Appends the IDC node-table item to the binding.
21785 *
21786 * Returns 0 on success and -1 on internal errors.
21787 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021788static int
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021789xmlSchemaIDCAppendNodeTableItem(xmlSchemaPSVIIDCBindingPtr bind,
21790 xmlSchemaPSVIIDCNodePtr ntItem)
21791{
21792 if (bind->nodeTable == NULL) {
21793 bind->sizeNodes = 10;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021794 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021795 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
21796 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021797 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021798 "allocating an array of IDC node-table items", NULL);
21799 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021800 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021801 } else if (bind->sizeNodes <= bind->nbNodes) {
21802 bind->sizeNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021803 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
21804 xmlRealloc(bind->nodeTable, bind->sizeNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021805 sizeof(xmlSchemaPSVIIDCNodePtr));
21806 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021807 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021808 "re-allocating an array of IDC node-table items", NULL);
21809 return(-1);
21810 }
21811 }
21812 bind->nodeTable[bind->nbNodes++] = ntItem;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021813 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021814}
21815
21816/**
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021817 * xmlSchemaIDCAcquireBinding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021818 * @vctxt: the WXS validation context
21819 * @matcher: the IDC matcher
21820 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021821 * Looks up an PSVI IDC binding, for the IDC definition and
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021822 * of the given matcher. If none found, a new one is created
21823 * and added to the IDC table.
21824 *
21825 * Returns an IDC binding or NULL on internal errors.
21826 */
21827static xmlSchemaPSVIIDCBindingPtr
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021828xmlSchemaIDCAcquireBinding(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021829 xmlSchemaIDCMatcherPtr matcher)
21830{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021831 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021832
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021833 ielem = vctxt->elemInfos[matcher->depth];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021834
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021835 if (ielem->idcTable == NULL) {
21836 ielem->idcTable = xmlSchemaIDCNewBinding(matcher->aidc->def);
21837 if (ielem->idcTable == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021838 return (NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021839 return(ielem->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021840 } else {
21841 xmlSchemaPSVIIDCBindingPtr bind = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021842
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021843 bind = ielem->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021844 do {
21845 if (bind->definition == matcher->aidc->def)
21846 return(bind);
21847 if (bind->next == NULL) {
21848 bind->next = xmlSchemaIDCNewBinding(matcher->aidc->def);
21849 if (bind->next == NULL)
21850 return (NULL);
21851 return(bind->next);
21852 }
21853 bind = bind->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021854 } while (bind != NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021855 }
21856 return (NULL);
21857}
21858
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021859static xmlSchemaItemListPtr
21860xmlSchemaIDCAcquireTargetList(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
21861 xmlSchemaIDCMatcherPtr matcher)
21862{
21863 if (matcher->targets == NULL)
21864 matcher->targets = xmlSchemaItemListCreate();
21865 return(matcher->targets);
21866}
21867
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021868/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021869 * xmlSchemaIDCFreeKey:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021870 * @key: the IDC key
21871 *
21872 * Frees an IDC key together with its compiled value.
21873 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021874static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021875xmlSchemaIDCFreeKey(xmlSchemaPSVIIDCKeyPtr key)
21876{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021877 if (key->val != NULL)
21878 xmlSchemaFreeValue(key->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021879 xmlFree(key);
21880}
21881
21882/**
21883 * xmlSchemaIDCFreeBinding:
21884 *
21885 * Frees an IDC binding. Note that the node table-items
21886 * are not freed.
21887 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000021888static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021889xmlSchemaIDCFreeBinding(xmlSchemaPSVIIDCBindingPtr bind)
21890{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021891 if (bind->nodeTable != NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021892 xmlFree(bind->nodeTable);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021893 if (bind->dupls != NULL)
21894 xmlSchemaItemListFree(bind->dupls);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021895 xmlFree(bind);
21896}
21897
21898/**
21899 * xmlSchemaIDCFreeIDCTable:
21900 * @bind: the first IDC binding in the list
21901 *
21902 * Frees an IDC table, i.e. all the IDC bindings in the list.
21903 */
21904static void
21905xmlSchemaIDCFreeIDCTable(xmlSchemaPSVIIDCBindingPtr bind)
21906{
21907 xmlSchemaPSVIIDCBindingPtr prev;
21908
21909 while (bind != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021910 prev = bind;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021911 bind = bind->next;
21912 xmlSchemaIDCFreeBinding(prev);
21913 }
21914}
21915
21916/**
21917 * xmlSchemaIDCFreeMatcherList:
21918 * @matcher: the first IDC matcher in the list
21919 *
21920 * Frees a list of IDC matchers.
21921 */
21922static void
21923xmlSchemaIDCFreeMatcherList(xmlSchemaIDCMatcherPtr matcher)
21924{
21925 xmlSchemaIDCMatcherPtr next;
21926
21927 while (matcher != NULL) {
21928 next = matcher->next;
21929 if (matcher->keySeqs != NULL) {
21930 int i;
21931 for (i = 0; i < matcher->sizeKeySeqs; i++)
21932 if (matcher->keySeqs[i] != NULL)
21933 xmlFree(matcher->keySeqs[i]);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021934 xmlFree(matcher->keySeqs);
21935 }
21936 if (matcher->targets != NULL) {
21937 if (matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) {
21938 int i;
21939 xmlSchemaPSVIIDCNodePtr idcNode;
21940 /*
21941 * Node-table items for keyrefs are not stored globally
21942 * to the validation context, since they are not bubbled.
21943 * We need to free them here.
21944 */
21945 for (i = 0; i < matcher->targets->nbItems; i++) {
21946 idcNode =
21947 (xmlSchemaPSVIIDCNodePtr) matcher->targets->items[i];
21948 xmlFree(idcNode->keys);
21949 xmlFree(idcNode);
21950 }
21951 }
21952 xmlSchemaItemListFree(matcher->targets);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021953 }
21954 xmlFree(matcher);
21955 matcher = next;
21956 }
21957}
21958
21959/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021960 * xmlSchemaIDCAddStateObject:
21961 * @vctxt: the WXS validation context
21962 * @matcher: the IDC matcher
21963 * @sel: the XPath information
21964 * @parent: the parent "selector" state object if any
21965 * @type: "selector" or "field"
21966 *
21967 * Creates/reuses and activates state objects for the given
21968 * XPath information; if the XPath expression consists of unions,
21969 * multiple state objects are created for every unioned expression.
21970 *
21971 * Returns 0 on success and -1 on internal errors.
21972 */
21973static int
21974xmlSchemaIDCAddStateObject(xmlSchemaValidCtxtPtr vctxt,
21975 xmlSchemaIDCMatcherPtr matcher,
21976 xmlSchemaIDCSelectPtr sel,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021977 int type)
21978{
21979 xmlSchemaIDCStateObjPtr sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021980
21981 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021982 * Reuse the state objects from the pool.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021983 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021984 if (vctxt->xpathStatePool != NULL) {
21985 sto = vctxt->xpathStatePool;
21986 vctxt->xpathStatePool = sto->next;
21987 sto->next = NULL;
21988 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021989 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021990 * Create a new state object.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021991 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021992 sto = (xmlSchemaIDCStateObjPtr) xmlMalloc(sizeof(xmlSchemaIDCStateObj));
21993 if (sto == NULL) {
21994 xmlSchemaVErrMemory(NULL,
21995 "allocating an IDC state object", NULL);
21996 return (-1);
21997 }
21998 memset(sto, 0, sizeof(xmlSchemaIDCStateObj));
21999 }
22000 /*
22001 * Add to global list.
22002 */
22003 if (vctxt->xpathStates != NULL)
22004 sto->next = vctxt->xpathStates;
22005 vctxt->xpathStates = sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022006
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022007 /*
22008 * Free the old xpath validation context.
22009 */
22010 if (sto->xpathCtxt != NULL)
22011 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
22012
22013 /*
22014 * Create a new XPath (pattern) validation context.
22015 */
22016 sto->xpathCtxt = (void *) xmlPatternGetStreamCtxt(
22017 (xmlPatternPtr) sel->xpathComp);
22018 if (sto->xpathCtxt == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022019 VERROR_INT("xmlSchemaIDCAddStateObject",
22020 "failed to create an XPath validation context");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022021 return (-1);
22022 }
22023 sto->type = type;
22024 sto->depth = vctxt->depth;
22025 sto->matcher = matcher;
22026 sto->sel = sel;
22027 sto->nbHistory = 0;
22028
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022029#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022030 xmlGenericError(xmlGenericErrorContext, "IDC: STO push '%s'\n",
22031 sto->sel->xpath);
22032#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022033 return (0);
22034}
22035
22036/**
22037 * xmlSchemaXPathEvaluate:
22038 * @vctxt: the WXS validation context
22039 * @nodeType: the nodeType of the current node
22040 *
22041 * Evaluates all active XPath state objects.
22042 *
22043 * Returns the number of IC "field" state objects which resolved to
22044 * this node, 0 if none resolved and -1 on internal errors.
22045 */
22046static int
22047xmlSchemaXPathEvaluate(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022048 xmlElementType nodeType)
22049{
22050 xmlSchemaIDCStateObjPtr sto, head = NULL, first;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022051 int res, resolved = 0, depth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022052
22053 if (vctxt->xpathStates == NULL)
22054 return (0);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022055
22056 if (nodeType == XML_ATTRIBUTE_NODE)
22057 depth++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022058#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022059 {
22060 xmlChar *str = NULL;
22061 xmlGenericError(xmlGenericErrorContext,
22062 "IDC: EVAL on %s, depth %d, type %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022063 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22064 vctxt->inode->localName), depth, nodeType);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022065 FREE_AND_NULL(str)
22066 }
22067#endif
22068 /*
22069 * Process all active XPath state objects.
22070 */
22071 first = vctxt->xpathStates;
22072 sto = first;
22073 while (sto != head) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022074#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022075 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022076 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] selector '%s'\n",
22077 sto->matcher->aidc->def->name, sto->sel->xpath);
22078 else
22079 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] field '%s'\n",
22080 sto->matcher->aidc->def->name, sto->sel->xpath);
22081#endif
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022082 if (nodeType == XML_ELEMENT_NODE)
22083 res = xmlStreamPush((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022084 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022085 else
22086 res = xmlStreamPushAttr((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022087 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022088
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022089 if (res == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022090 VERROR_INT("xmlSchemaXPathEvaluate",
22091 "calling xmlStreamPush()");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022092 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022093 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022094 if (res == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022095 goto next_sto;
22096 /*
22097 * Full match.
22098 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022099#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022100 xmlGenericError(xmlGenericErrorContext, "IDC: "
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022101 "MATCH\n");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022102#endif
22103 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022104 * Register a match in the state object history.
22105 */
22106 if (sto->history == NULL) {
22107 sto->history = (int *) xmlMalloc(5 * sizeof(int));
22108 if (sto->history == NULL) {
22109 xmlSchemaVErrMemory(NULL,
22110 "allocating the state object history", NULL);
22111 return(-1);
22112 }
22113 sto->sizeHistory = 10;
22114 } else if (sto->sizeHistory <= sto->nbHistory) {
22115 sto->sizeHistory *= 2;
22116 sto->history = (int *) xmlRealloc(sto->history,
22117 sto->sizeHistory * sizeof(int));
22118 if (sto->history == NULL) {
22119 xmlSchemaVErrMemory(NULL,
22120 "re-allocating the state object history", NULL);
22121 return(-1);
22122 }
22123 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022124 sto->history[sto->nbHistory++] = depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022125
22126#ifdef DEBUG_IDC
22127 xmlGenericError(xmlGenericErrorContext, "IDC: push match '%d'\n",
22128 vctxt->depth);
22129#endif
22130
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022131 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
22132 xmlSchemaIDCSelectPtr sel;
22133 /*
22134 * Activate state objects for the IDC fields of
22135 * the IDC selector.
22136 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022137#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022138 xmlGenericError(xmlGenericErrorContext, "IDC: "
22139 "activating field states\n");
22140#endif
22141 sel = sto->matcher->aidc->def->fields;
22142 while (sel != NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022143 if (xmlSchemaIDCAddStateObject(vctxt, sto->matcher,
22144 sel, XPATH_STATE_OBJ_TYPE_IDC_FIELD) == -1)
22145 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022146 sel = sel->next;
22147 }
22148 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
22149 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000022150 * An IDC key node was found by the IDC field.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022151 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022152#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022153 xmlGenericError(xmlGenericErrorContext,
22154 "IDC: key found\n");
22155#endif
22156 /*
22157 * Notify that the character value of this node is
22158 * needed.
22159 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022160 if (resolved == 0) {
22161 if ((vctxt->inode->flags &
22162 XML_SCHEMA_NODE_INFO_VALUE_NEEDED) == 0)
22163 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
22164 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022165 resolved++;
22166 }
22167next_sto:
22168 if (sto->next == NULL) {
22169 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022170 * Evaluate field state objects created on this node as well.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022171 */
22172 head = first;
22173 sto = vctxt->xpathStates;
22174 } else
22175 sto = sto->next;
22176 }
22177 return (resolved);
22178}
22179
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022180static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022181xmlSchemaFormatIDCKeySequence(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022182 xmlChar **buf,
22183 xmlSchemaPSVIIDCKeyPtr *seq,
22184 int count)
22185{
22186 int i, res;
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022187 xmlChar *value = NULL;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022188
22189 *buf = xmlStrdup(BAD_CAST "[");
22190 for (i = 0; i < count; i++) {
22191 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022192 res = xmlSchemaGetCanonValueWhtspExt(seq[i]->val,
22193 xmlSchemaGetWhiteSpaceFacetValue(seq[i]->type),
22194 &value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022195 if (res == 0)
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022196 *buf = xmlStrcat(*buf, BAD_CAST value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022197 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022198 VERROR_INT("xmlSchemaFormatIDCKeySequence",
22199 "failed to compute a canonical value");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022200 *buf = xmlStrcat(*buf, BAD_CAST "???");
22201 }
22202 if (i < count -1)
22203 *buf = xmlStrcat(*buf, BAD_CAST "', ");
22204 else
22205 *buf = xmlStrcat(*buf, BAD_CAST "'");
22206 if (value != NULL) {
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022207 xmlFree(value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022208 value = NULL;
22209 }
22210 }
22211 *buf = xmlStrcat(*buf, BAD_CAST "]");
22212
22213 return (BAD_CAST *buf);
22214}
22215
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022216/**
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000022217 * xmlSchemaXPathPop:
22218 * @vctxt: the WXS validation context
22219 *
22220 * Pops all XPath states.
22221 *
22222 * Returns 0 on success and -1 on internal errors.
22223 */
22224static int
22225xmlSchemaXPathPop(xmlSchemaValidCtxtPtr vctxt)
22226{
22227 xmlSchemaIDCStateObjPtr sto;
22228 int res;
22229
22230 if (vctxt->xpathStates == NULL)
22231 return(0);
22232 sto = vctxt->xpathStates;
22233 do {
22234 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22235 if (res == -1)
22236 return (-1);
22237 sto = sto->next;
22238 } while (sto != NULL);
22239 return(0);
22240}
22241
22242/**
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022243 * xmlSchemaXPathProcessHistory:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022244 * @vctxt: the WXS validation context
22245 * @type: the simple/complex type of the current node if any at all
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022246 * @val: the precompiled value
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022247 *
22248 * Processes and pops the history items of the IDC state objects.
22249 * IDC key-sequences are validated/created on IDC bindings.
22250 *
22251 * Returns 0 on success and -1 on internal errors.
22252 */
22253static int
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022254xmlSchemaXPathProcessHistory(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022255 int depth)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022256{
22257 xmlSchemaIDCStateObjPtr sto, nextsto;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022258 int res, matchDepth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022259 xmlSchemaPSVIIDCKeyPtr key = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022260 xmlSchemaTypePtr type = vctxt->inode->typeDef, simpleType = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022261
22262 if (vctxt->xpathStates == NULL)
22263 return (0);
22264 sto = vctxt->xpathStates;
22265
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022266#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022267 {
22268 xmlChar *str = NULL;
22269 xmlGenericError(xmlGenericErrorContext,
22270 "IDC: BACK on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022271 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22272 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022273 FREE_AND_NULL(str)
22274 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022275#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022276 /*
22277 * Evaluate the state objects.
22278 */
22279 while (sto != NULL) {
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000022280 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22281 if (res == -1) {
22282 VERROR_INT("xmlSchemaXPathProcessHistory",
22283 "calling xmlStreamPop()");
22284 return (-1);
22285 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022286#ifdef DEBUG_IDC
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000022287 xmlGenericError(xmlGenericErrorContext, "IDC: stream pop '%s'\n",
22288 sto->sel->xpath);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022289#endif
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022290 if (sto->nbHistory == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022291 goto deregister_check;
22292
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022293 matchDepth = sto->history[sto->nbHistory -1];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022294
22295 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022296 * Only matches at the current depth are of interest.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022297 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022298 if (matchDepth != depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022299 sto = sto->next;
22300 continue;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022301 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022302 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022303 /*
22304 * NOTE: According to
22305 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2198
22306 * ... the simple-content of complex types is also allowed.
22307 */
22308
22309 if (WXS_IS_COMPLEX(type)) {
22310 if (WXS_HAS_SIMPLE_CONTENT(type)) {
22311 /*
22312 * Sanity check for complex types with simple content.
22313 */
22314 simpleType = type->contentTypeDef;
22315 if (simpleType == NULL) {
22316 VERROR_INT("xmlSchemaXPathProcessHistory",
22317 "field resolves to a CT with simple content "
22318 "but the CT is missing the ST definition");
22319 return (-1);
22320 }
22321 } else
22322 simpleType = NULL;
22323 } else
22324 simpleType = type;
22325 if (simpleType == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022326 xmlChar *str = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022327
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022328 /*
22329 * Not qualified if the field resolves to a node of non
22330 * simple type.
22331 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022332 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022333 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022334 WXS_BASIC_CAST sto->matcher->aidc->def,
22335 "The XPath '%s' of a field of %s does evaluate to a node of "
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022336 "non-simple type",
22337 sto->sel->xpath,
22338 xmlSchemaGetIDCDesignation(&str, sto->matcher->aidc->def));
22339 FREE_AND_NULL(str);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022340 sto->nbHistory--;
22341 goto deregister_check;
22342 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022343
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022344 if ((key == NULL) && (vctxt->inode->val == NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022345 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022346 * Failed to provide the normalized value; maybe
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022347 * the value was invalid.
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022348 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022349 VERROR(XML_SCHEMAV_CVC_IDC,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022350 WXS_BASIC_CAST sto->matcher->aidc->def,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000022351 "Warning: No precomputed value available, the value "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022352 "was either invalid or something strange happend");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022353 sto->nbHistory--;
22354 goto deregister_check;
22355 } else {
22356 xmlSchemaIDCMatcherPtr matcher = sto->matcher;
22357 xmlSchemaPSVIIDCKeyPtr *keySeq;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022358 int pos, idx;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022359
22360 /*
22361 * The key will be anchored on the matcher's list of
22362 * key-sequences. The position in this list is determined
22363 * by the target node's depth relative to the matcher's
22364 * depth of creation (i.e. the depth of the scope element).
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022365 *
22366 * Element Depth Pos List-entries
22367 * <scope> 0 NULL
22368 * <bar> 1 NULL
22369 * <target/> 2 2 target
22370 * <bar>
22371 * </scope>
22372 *
22373 * The size of the list is only dependant on the depth of
22374 * the tree.
22375 * An entry will be NULLed in selector_leave, i.e. when
22376 * we hit the target's
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022377 */
22378 pos = sto->depth - matcher->depth;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022379 idx = sto->sel->index;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022380
22381 /*
22382 * Create/grow the array of key-sequences.
22383 */
22384 if (matcher->keySeqs == NULL) {
22385 if (pos > 9)
22386 matcher->sizeKeySeqs = pos * 2;
22387 else
22388 matcher->sizeKeySeqs = 10;
22389 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22390 xmlMalloc(matcher->sizeKeySeqs *
22391 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22392 if (matcher->keySeqs == NULL) {
22393 xmlSchemaVErrMemory(NULL,
22394 "allocating an array of key-sequences",
22395 NULL);
22396 return(-1);
22397 }
22398 memset(matcher->keySeqs, 0,
22399 matcher->sizeKeySeqs *
22400 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22401 } else if (pos >= matcher->sizeKeySeqs) {
22402 int i = matcher->sizeKeySeqs;
22403
22404 matcher->sizeKeySeqs *= 2;
22405 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22406 xmlRealloc(matcher->keySeqs,
22407 matcher->sizeKeySeqs *
22408 sizeof(xmlSchemaPSVIIDCKeyPtr *));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022409 if (matcher->keySeqs == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022410 xmlSchemaVErrMemory(NULL,
22411 "reallocating an array of key-sequences",
22412 NULL);
22413 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022414 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022415 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022416 * The array needs to be NULLed.
22417 * TODO: Use memset?
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022418 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022419 for (; i < matcher->sizeKeySeqs; i++)
22420 matcher->keySeqs[i] = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022421 }
22422
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022423 /*
22424 * Get/create the key-sequence.
22425 */
22426 keySeq = matcher->keySeqs[pos];
22427 if (keySeq == NULL) {
22428 goto create_sequence;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022429 } else if (keySeq[idx] != NULL) {
22430 xmlChar *str = NULL;
22431 /*
22432 * cvc-identity-constraint:
22433 * 3 For each node in the ·target node set· all
22434 * of the {fields}, with that node as the context
22435 * node, evaluate to either an empty node-set or
22436 * a node-set with exactly one member, which must
22437 * have a simple type.
22438 *
22439 * The key was already set; report an error.
22440 */
22441 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22442 XML_SCHEMAV_CVC_IDC, NULL,
22443 WXS_BASIC_CAST matcher->aidc->def,
22444 "The XPath '%s' of a field of %s evaluates to a "
22445 "node-set with more than one member",
22446 sto->sel->xpath,
22447 xmlSchemaGetIDCDesignation(&str, matcher->aidc->def));
22448 FREE_AND_NULL(str);
22449 sto->nbHistory--;
22450 goto deregister_check;
22451 } else
22452 goto create_key;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022453
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022454create_sequence:
22455 /*
22456 * Create a key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022457 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022458 keySeq = (xmlSchemaPSVIIDCKeyPtr *) xmlMalloc(
22459 matcher->aidc->def->nbFields *
22460 sizeof(xmlSchemaPSVIIDCKeyPtr));
22461 if (keySeq == NULL) {
22462 xmlSchemaVErrMemory(NULL,
22463 "allocating an IDC key-sequence", NULL);
22464 return(-1);
22465 }
22466 memset(keySeq, 0, matcher->aidc->def->nbFields *
22467 sizeof(xmlSchemaPSVIIDCKeyPtr));
22468 matcher->keySeqs[pos] = keySeq;
22469create_key:
22470 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022471 * Create a key once per node only.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022472 */
22473 if (key == NULL) {
22474 key = (xmlSchemaPSVIIDCKeyPtr) xmlMalloc(
22475 sizeof(xmlSchemaPSVIIDCKey));
22476 if (key == NULL) {
22477 xmlSchemaVErrMemory(NULL,
22478 "allocating a IDC key", NULL);
22479 xmlFree(keySeq);
22480 matcher->keySeqs[pos] = NULL;
22481 return(-1);
22482 }
22483 /*
22484 * Consume the compiled value.
22485 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022486 key->type = simpleType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022487 key->val = vctxt->inode->val;
22488 vctxt->inode->val = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022489 /*
22490 * Store the key in a global list.
22491 */
22492 if (xmlSchemaIDCStoreKey(vctxt, key) == -1) {
22493 xmlSchemaIDCFreeKey(key);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022494 return (-1);
22495 }
22496 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022497 keySeq[idx] = key;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022498 }
22499 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022500
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022501 xmlSchemaPSVIIDCKeyPtr **keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022502 /* xmlSchemaPSVIIDCBindingPtr bind; */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022503 xmlSchemaPSVIIDCNodePtr ntItem;
22504 xmlSchemaIDCMatcherPtr matcher;
22505 xmlSchemaIDCPtr idc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022506 xmlSchemaItemListPtr targets;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022507 int pos, i, j, nbKeys;
22508 /*
22509 * Here we have the following scenario:
22510 * An IDC 'selector' state object resolved to a target node,
22511 * during the time this target node was in the
22512 * ancestor-or-self axis, the 'field' state object(s) looked
22513 * out for matching nodes to create a key-sequence for this
22514 * target node. Now we are back to this target node and need
22515 * to put the key-sequence, together with the target node
22516 * itself, into the node-table of the corresponding IDC
22517 * binding.
22518 */
22519 matcher = sto->matcher;
22520 idc = matcher->aidc->def;
22521 nbKeys = idc->nbFields;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022522 pos = depth - matcher->depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022523 /*
22524 * Check if the matcher has any key-sequences at all, plus
22525 * if it has a key-sequence for the current target node.
22526 */
22527 if ((matcher->keySeqs == NULL) ||
22528 (matcher->sizeKeySeqs <= pos)) {
22529 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22530 goto selector_key_error;
22531 else
22532 goto selector_leave;
22533 }
22534
22535 keySeq = &(matcher->keySeqs[pos]);
22536 if (*keySeq == NULL) {
22537 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22538 goto selector_key_error;
22539 else
22540 goto selector_leave;
22541 }
22542
22543 for (i = 0; i < nbKeys; i++) {
22544 if ((*keySeq)[i] == NULL) {
22545 /*
22546 * Not qualified, if not all fields did resolve.
22547 */
22548 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY) {
22549 /*
22550 * All fields of a "key" IDC must resolve.
22551 */
22552 goto selector_key_error;
22553 }
22554 goto selector_leave;
22555 }
22556 }
22557 /*
22558 * All fields did resolve.
22559 */
22560
22561 /*
22562 * 4.1 If the {identity-constraint category} is unique(/key),
22563 * then no two members of the ·qualified node set· have
22564 * ·key-sequences· whose members are pairwise equal, as
22565 * defined by Equal in [XML Schemas: Datatypes].
22566 *
22567 * Get the IDC binding from the matcher and check for
22568 * duplicate key-sequences.
22569 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022570#if 0
22571 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22572#endif
22573 targets = xmlSchemaIDCAcquireTargetList(vctxt, matcher);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022574 if ((idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) &&
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022575 (targets->nbItems != 0)) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022576 xmlSchemaPSVIIDCKeyPtr ckey, bkey, *bkeySeq;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022577
22578 i = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022579 res = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022580 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022581 * Compare the key-sequences, key by key.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022582 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022583 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022584 bkeySeq =
22585 ((xmlSchemaPSVIIDCNodePtr) targets->items[i])->keys;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022586 for (j = 0; j < nbKeys; j++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022587 ckey = (*keySeq)[j];
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022588 bkey = bkeySeq[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022589 res = xmlSchemaAreValuesEqual(ckey->val, bkey->val);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022590 if (res == -1) {
22591 return (-1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022592 } else if (res == 0) {
22593 /*
22594 * One of the keys differs, so the key-sequence
22595 * won't be equal; get out.
22596 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022597 break;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022598 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022599 }
22600 if (res == 1) {
22601 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022602 * Duplicate key-sequence found.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022603 */
22604 break;
22605 }
22606 i++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022607 } while (i < targets->nbItems);
22608 if (i != targets->nbItems) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022609 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022610 /*
22611 * TODO: Try to report the key-sequence.
22612 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022613 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022614 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022615 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022616 "Duplicate key-sequence %s in %s",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022617 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022618 (*keySeq), nbKeys),
22619 xmlSchemaGetIDCDesignation(&strB, idc));
22620 FREE_AND_NULL(str);
22621 FREE_AND_NULL(strB);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022622 goto selector_leave;
22623 }
22624 }
22625 /*
22626 * Add a node-table item to the IDC binding.
22627 */
22628 ntItem = (xmlSchemaPSVIIDCNodePtr) xmlMalloc(
22629 sizeof(xmlSchemaPSVIIDCNode));
22630 if (ntItem == NULL) {
22631 xmlSchemaVErrMemory(NULL,
22632 "allocating an IDC node-table item", NULL);
22633 xmlFree(*keySeq);
22634 *keySeq = NULL;
22635 return(-1);
22636 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022637 memset(ntItem, 0, sizeof(xmlSchemaPSVIIDCNode));
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022638
22639 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022640 * Store the node-table item in a global list.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022641 */
22642 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) {
22643 if (xmlSchemaIDCStoreNodeTableItem(vctxt, ntItem) == -1) {
22644 xmlFree(ntItem);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022645 xmlFree(*keySeq);
22646 *keySeq = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022647 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022648 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022649 ntItem->nodeQNameID = -1;
22650 } else {
22651 /*
22652 * Save a cached QName for this node on the IDC node, to be
22653 * able to report it, even if the node is not saved.
22654 */
22655 ntItem->nodeQNameID = xmlSchemaVAddNodeQName(vctxt,
22656 vctxt->inode->localName, vctxt->inode->nsName);
22657 if (ntItem->nodeQNameID == -1) {
22658 xmlFree(ntItem);
22659 xmlFree(*keySeq);
22660 *keySeq = NULL;
22661 return (-1);
22662 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022663 }
22664 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022665 * Init the node-table item: Save the node, position and
22666 * consume the key-sequence.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022667 */
22668 ntItem->node = vctxt->node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022669 ntItem->nodeLine = vctxt->inode->nodeLine;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022670 ntItem->keys = *keySeq;
22671 *keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022672#if 0
22673 if (xmlSchemaIDCAppendNodeTableItem(bind, ntItem) == -1) {
22674#endif
22675 if (xmlSchemaItemListAdd(targets, ntItem) == -1) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022676 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22677 /*
22678 * Free the item, since keyref items won't be
22679 * put on a global list.
22680 */
22681 xmlFree(ntItem->keys);
22682 xmlFree(ntItem);
22683 }
22684 return (-1);
22685 }
22686
22687 goto selector_leave;
22688selector_key_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022689 {
22690 xmlChar *str = NULL;
22691 /*
22692 * 4.2.1 (KEY) The ·target node set· and the
22693 * ·qualified node set· are equal, that is, every
22694 * member of the ·target node set· is also a member
22695 * of the ·qualified node set· and vice versa.
22696 */
22697 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22698 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022699 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022700 "Not all fields of %s evaluate to a node",
22701 xmlSchemaGetIDCDesignation(&str, idc), NULL);
22702 FREE_AND_NULL(str);
22703 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022704selector_leave:
22705 /*
22706 * Free the key-sequence if not added to the IDC table.
22707 */
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022708 if ((keySeq != NULL) && (*keySeq != NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022709 xmlFree(*keySeq);
22710 *keySeq = NULL;
22711 }
22712 } /* if selector */
22713
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022714 sto->nbHistory--;
22715
22716deregister_check:
22717 /*
22718 * Deregister state objects if they reach the depth of creation.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022719 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022720 if ((sto->nbHistory == 0) && (sto->depth == depth)) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022721#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022722 xmlGenericError(xmlGenericErrorContext, "IDC: STO pop '%s'\n",
22723 sto->sel->xpath);
22724#endif
22725 if (vctxt->xpathStates != sto) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022726 VERROR_INT("xmlSchemaXPathProcessHistory",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022727 "The state object to be removed is not the first "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022728 "in the list");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022729 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022730 nextsto = sto->next;
22731 /*
22732 * Unlink from the list of active XPath state objects.
22733 */
22734 vctxt->xpathStates = sto->next;
22735 sto->next = vctxt->xpathStatePool;
22736 /*
22737 * Link it to the pool of reusable state objects.
22738 */
22739 vctxt->xpathStatePool = sto;
22740 sto = nextsto;
22741 } else
22742 sto = sto->next;
22743 } /* while (sto != NULL) */
22744 return (0);
22745}
22746
22747/**
22748 * xmlSchemaIDCRegisterMatchers:
22749 * @vctxt: the WXS validation context
22750 * @elemDecl: the element declaration
22751 *
22752 * Creates helper objects to evaluate IDC selectors/fields
22753 * successively.
22754 *
22755 * Returns 0 if OK and -1 on internal errors.
22756 */
22757static int
22758xmlSchemaIDCRegisterMatchers(xmlSchemaValidCtxtPtr vctxt,
22759 xmlSchemaElementPtr elemDecl)
22760{
22761 xmlSchemaIDCMatcherPtr matcher, last = NULL;
22762 xmlSchemaIDCPtr idc, refIdc;
22763 xmlSchemaIDCAugPtr aidc;
22764
22765 idc = (xmlSchemaIDCPtr) elemDecl->idcs;
22766 if (idc == NULL)
22767 return (0);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022768
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022769#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022770 {
22771 xmlChar *str = NULL;
22772 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022773 "IDC: REGISTER on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022774 (char *) xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22775 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022776 FREE_AND_NULL(str)
22777 }
22778#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022779 if (vctxt->inode->idcMatchers != NULL) {
22780 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22781 "The chain of IDC matchers is expected to be empty");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022782 return (-1);
22783 }
22784 do {
22785 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22786 /*
22787 * Since IDCs bubbles are expensive we need to know the
22788 * depth at which the bubbles should stop; this will be
22789 * the depth of the top-most keyref IDC. If no keyref
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022790 * references a key/unique IDC, the keyrefDepth will
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022791 * be -1, indicating that no bubbles are needed.
22792 */
22793 refIdc = (xmlSchemaIDCPtr) idc->ref->item;
22794 if (refIdc != NULL) {
22795 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022796 * Remember that we have keyrefs on this node.
22797 */
22798 vctxt->inode->hasKeyrefs = 1;
22799 /*
22800 * Lookup the referenced augmented IDC info.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022801 */
22802 aidc = vctxt->aidcs;
22803 while (aidc != NULL) {
22804 if (aidc->def == refIdc)
22805 break;
22806 aidc = aidc->next;
22807 }
22808 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022809 VERROR_INT("xmlSchemaIDCRegisterMatchers",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022810 "Could not find an augmented IDC item for an IDC "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022811 "definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022812 return (-1);
22813 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022814 if ((aidc->keyrefDepth == -1) ||
22815 (vctxt->depth < aidc->keyrefDepth))
22816 aidc->keyrefDepth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022817 }
22818 }
22819 /*
22820 * Lookup the augmented IDC item for the IDC definition.
22821 */
22822 aidc = vctxt->aidcs;
22823 while (aidc != NULL) {
22824 if (aidc->def == idc)
22825 break;
22826 aidc = aidc->next;
22827 }
22828 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022829 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22830 "Could not find an augmented IDC item for an IDC definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022831 return (-1);
22832 }
22833 /*
22834 * Create an IDC matcher for every IDC definition.
22835 */
22836 matcher = (xmlSchemaIDCMatcherPtr)
22837 xmlMalloc(sizeof(xmlSchemaIDCMatcher));
22838 if (matcher == NULL) {
22839 xmlSchemaVErrMemory(vctxt,
22840 "allocating an IDC matcher", NULL);
22841 return (-1);
22842 }
22843 memset(matcher, 0, sizeof(xmlSchemaIDCMatcher));
22844 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022845 vctxt->inode->idcMatchers = matcher;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022846 else
22847 last->next = matcher;
22848 last = matcher;
22849
22850 matcher->type = IDC_MATCHER;
22851 matcher->depth = vctxt->depth;
22852 matcher->aidc = aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022853 matcher->idcType = aidc->def->type;
22854#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022855 xmlGenericError(xmlGenericErrorContext, "IDC: register matcher\n");
22856#endif
22857 /*
22858 * Init the automaton state object.
22859 */
22860 if (xmlSchemaIDCAddStateObject(vctxt, matcher,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022861 idc->selector, XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) == -1)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022862 return (-1);
22863
22864 idc = idc->next;
22865 } while (idc != NULL);
22866 return (0);
22867}
22868
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022869static int
22870xmlSchemaIDCFillNodeTables(xmlSchemaValidCtxtPtr vctxt,
22871 xmlSchemaNodeInfoPtr ielem)
22872{
22873 xmlSchemaPSVIIDCBindingPtr bind;
22874 int res, i, j, k, nbTargets, nbFields, nbDupls, nbNodeTable;
22875 xmlSchemaPSVIIDCKeyPtr *keys, *ntkeys;
22876 xmlSchemaPSVIIDCNodePtr *targets, *dupls;
22877
22878 xmlSchemaIDCMatcherPtr matcher = ielem->idcMatchers;
22879 /* vctxt->createIDCNodeTables */
22880 while (matcher != NULL) {
22881 /*
22882 * Skip keyref IDCs and empty IDC target-lists.
22883 */
22884 if ((matcher->aidc->def->type == XML_SCHEMA_TYPE_IDC_KEYREF) ||
22885 WXS_ILIST_IS_EMPTY(matcher->targets))
22886 {
22887 matcher = matcher->next;
22888 continue;
22889 }
22890 /*
22891 * If we _want_ the IDC node-table to be created in any case
22892 * then do so. Otherwise create them only if keyrefs need them.
22893 */
22894 if ((! vctxt->createIDCNodeTables) &&
22895 ((matcher->aidc->keyrefDepth == -1) ||
22896 (matcher->aidc->keyrefDepth > vctxt->depth)))
22897 {
22898 matcher = matcher->next;
22899 continue;
22900 }
22901 /*
22902 * Get/create the IDC binding on this element for the IDC definition.
22903 */
22904 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22905
22906 if (! WXS_ILIST_IS_EMPTY(bind->dupls)) {
22907 dupls = (xmlSchemaPSVIIDCNodePtr *) bind->dupls->items;
22908 nbDupls = bind->dupls->nbItems;
22909 } else {
22910 dupls = NULL;
22911 nbDupls = 0;
22912 }
22913 if (bind->nodeTable != NULL) {
22914 nbNodeTable = bind->nbNodes;
22915 } else {
22916 nbNodeTable = 0;
22917 }
22918
22919 if ((nbNodeTable == 0) && (nbDupls == 0)) {
22920 /*
22921 * Transfer all IDC target-nodes to the IDC node-table.
22922 */
22923 bind->nodeTable =
22924 (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
22925 bind->sizeNodes = matcher->targets->sizeItems;
22926 bind->nbNodes = matcher->targets->nbItems;
22927
22928 matcher->targets->items = NULL;
22929 matcher->targets->sizeItems = 0;
22930 matcher->targets->nbItems = 0;
22931 } else {
22932 /*
22933 * Compare the key-sequences and add to the IDC node-table.
22934 */
22935 nbTargets = matcher->targets->nbItems;
22936 targets = (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
22937 nbFields = matcher->aidc->def->nbFields;
22938 i = 0;
22939 do {
22940 keys = targets[i]->keys;
22941 if (nbDupls) {
22942 /*
22943 * Search in already found duplicates first.
22944 */
22945 j = 0;
22946 do {
22947 if (nbFields == 1) {
22948 res = xmlSchemaAreValuesEqual(keys[0]->val,
22949 dupls[j]->keys[0]->val);
22950 if (res == -1)
22951 goto internal_error;
22952 if (res == 1) {
22953 /*
22954 * Equal key-sequence.
22955 */
22956 goto next_target;
22957 }
22958 } else {
22959 res = 0;
22960 ntkeys = dupls[j]->keys;
22961 for (k = 0; k < nbFields; k++) {
22962 res = xmlSchemaAreValuesEqual(keys[k]->val,
22963 ntkeys[k]->val);
22964 if (res == -1)
22965 goto internal_error;
22966 if (res == 0) {
22967 /*
22968 * One of the keys differs.
22969 */
22970 break;
22971 }
22972 }
22973 if (res == 1) {
22974 /*
22975 * Equal key-sequence found.
22976 */
22977 goto next_target;
22978 }
22979 }
22980 j++;
22981 } while (j < nbDupls);
22982 }
22983 if (nbNodeTable) {
22984 j = 0;
22985 do {
22986 if (nbFields == 1) {
22987 res = xmlSchemaAreValuesEqual(keys[0]->val,
22988 bind->nodeTable[j]->keys[0]->val);
22989 if (res == -1)
22990 goto internal_error;
22991 if (res == 0) {
22992 /*
22993 * The key-sequence differs.
22994 */
22995 goto next_node_table_entry;
22996 }
22997 } else {
22998 res = 0;
22999 ntkeys = bind->nodeTable[j]->keys;
23000 for (k = 0; k < nbFields; k++) {
23001 res = xmlSchemaAreValuesEqual(keys[k]->val,
23002 ntkeys[k]->val);
23003 if (res == -1)
23004 goto internal_error;
23005 if (res == 0) {
23006 /*
23007 * One of the keys differs.
23008 */
23009 goto next_node_table_entry;
23010 }
23011 }
23012 }
23013 /*
23014 * Add the duplicate to the list of duplicates.
23015 */
23016 if (bind->dupls == NULL) {
23017 bind->dupls = xmlSchemaItemListCreate();
23018 if (bind->dupls == NULL)
23019 goto internal_error;
23020 }
23021 if (xmlSchemaItemListAdd(bind->dupls, bind->nodeTable[j]) == -1)
23022 goto internal_error;
23023 /*
23024 * Remove the duplicate entry from the IDC node-table.
23025 */
23026 bind->nodeTable[j] = bind->nodeTable[bind->nbNodes -1];
23027 bind->nbNodes--;
23028
23029 goto next_target;
23030
23031next_node_table_entry:
23032 j++;
23033 } while (j < nbNodeTable);
23034 }
23035 /*
23036 * If everything is fine, then add the IDC target-node to
23037 * the IDC node-table.
23038 */
23039 if (xmlSchemaIDCAppendNodeTableItem(bind, targets[i]) == -1)
23040 goto internal_error;
23041
23042next_target:
23043 i++;
23044 } while (i < nbTargets);
23045 }
23046 matcher = matcher->next;
23047 }
23048 return(0);
23049
23050internal_error:
23051 return(-1);
23052}
23053
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023054/**
23055 * xmlSchemaBubbleIDCNodeTables:
23056 * @depth: the current tree depth
23057 *
23058 * Merges IDC bindings of an element at @depth into the corresponding IDC
23059 * bindings of its parent element. If a duplicate note-table entry is found,
23060 * both, the parent node-table entry and child entry are discarded from the
23061 * node-table of the parent.
23062 *
23063 * Returns 0 if OK and -1 on internal errors.
23064 */
23065static int
23066xmlSchemaBubbleIDCNodeTables(xmlSchemaValidCtxtPtr vctxt)
23067{
23068 xmlSchemaPSVIIDCBindingPtr bind; /* IDC bindings of the current node. */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023069 xmlSchemaPSVIIDCBindingPtr *parTable, parBind = NULL; /* parent IDC bindings. */
23070 xmlSchemaPSVIIDCNodePtr node, parNode = NULL, *dupls, *parNodes; /* node-table entries. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023071 xmlSchemaIDCAugPtr aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023072 int i, j, k, ret = 0, nbFields, oldNum, oldDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023073
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023074 bind = vctxt->inode->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023075 if (bind == NULL) {
23076 /* Fine, no table, no bubbles. */
23077 return (0);
23078 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000023079
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023080 parTable = &(vctxt->elemInfos[vctxt->depth -1]->idcTable);
23081 /*
23082 * Walk all bindings; create new or add to existing bindings.
23083 * Remove duplicate key-sequences.
23084 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023085 while (bind != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023086
23087 if ((bind->nbNodes == 0) && WXS_ILIST_IS_EMPTY(bind->dupls))
23088 goto next_binding;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023089 /*
23090 * Check if the key/unique IDC table needs to be bubbled.
23091 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023092 if (! vctxt->createIDCNodeTables) {
23093 aidc = vctxt->aidcs;
23094 do {
23095 if (aidc->def == bind->definition) {
23096 if ((aidc->keyrefDepth == -1) ||
23097 (aidc->keyrefDepth >= vctxt->depth)) {
23098 goto next_binding;
23099 }
23100 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023101 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023102 aidc = aidc->next;
23103 } while (aidc != NULL);
23104 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023105
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023106 if (parTable != NULL)
23107 parBind = *parTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023108 /*
23109 * Search a matching parent binding for the
23110 * IDC definition.
23111 */
23112 while (parBind != NULL) {
23113 if (parBind->definition == bind->definition)
23114 break;
23115 parBind = parBind->next;
23116 }
23117
23118 if (parBind != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023119 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023120 * Compare every node-table entry of the child node,
23121 * i.e. the key-sequence within, ...
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023122 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023123 oldNum = parBind->nbNodes; /* Skip newly added items. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023124
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023125 if (! WXS_ILIST_IS_EMPTY(parBind->dupls)) {
23126 oldDupls = parBind->dupls->nbItems;
23127 dupls = (xmlSchemaPSVIIDCNodePtr *) parBind->dupls->items;
23128 } else {
23129 dupls = NULL;
23130 oldDupls = 0;
23131 }
23132
23133 parNodes = parBind->nodeTable;
23134 nbFields = bind->definition->nbFields;
23135
23136 for (i = 0; i < bind->nbNodes; i++) {
23137 node = bind->nodeTable[i];
23138 if (node == NULL)
23139 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023140 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023141 * ...with every key-sequence of the parent node, already
23142 * evaluated to be a duplicate key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023143 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023144 if (oldDupls) {
23145 j = 0;
23146 while (j < oldDupls) {
23147 if (nbFields == 1) {
23148 ret = xmlSchemaAreValuesEqual(
23149 node->keys[0]->val,
23150 dupls[j]->keys[0]->val);
23151 if (ret == -1)
23152 goto internal_error;
23153 if (ret == 0) {
23154 j++;
23155 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023156 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023157 } else {
23158 parNode = dupls[j];
23159 for (k = 0; k < nbFields; k++) {
23160 ret = xmlSchemaAreValuesEqual(
23161 node->keys[k]->val,
23162 parNode->keys[k]->val);
23163 if (ret == -1)
23164 goto internal_error;
23165 if (ret == 0)
23166 break;
23167 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023168 }
23169 if (ret == 1)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023170 /* Duplicate found. */
23171 break;
23172 j++;
23173 }
23174 if (j != oldDupls) {
23175 /* Duplicate found. Skip this entry. */
23176 continue;
23177 }
23178 }
23179 /*
23180 * ... and with every key-sequence of the parent node.
23181 */
23182 if (oldNum) {
23183 j = 0;
23184 while (j < oldNum) {
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +000023185 parNode = parNodes[j];
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023186 if (nbFields == 1) {
23187 ret = xmlSchemaAreValuesEqual(
23188 node->keys[0]->val,
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +000023189 parNode->keys[0]->val);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023190 if (ret == -1)
23191 goto internal_error;
23192 if (ret == 0) {
23193 j++;
23194 continue;
23195 }
Kasimier T. Buchcikf39e8d12005-11-30 11:03:24 +000023196 } else {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023197 for (k = 0; k < nbFields; k++) {
23198 ret = xmlSchemaAreValuesEqual(
23199 node->keys[k]->val,
23200 parNode->keys[k]->val);
23201 if (ret == -1)
23202 goto internal_error;
23203 if (ret == 0)
23204 break;
23205 }
23206 }
23207 if (ret == 1)
23208 /* Duplicate found. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023209 break;
23210 j++;
23211 }
23212 if (j != oldNum) {
23213 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023214 * Handle duplicates. Move the duplicate in
23215 * the parent's node-table to the list of
23216 * duplicates.
23217 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023218 oldNum--;
23219 parBind->nbNodes--;
23220 /*
23221 * Move last old item to pos of duplicate.
23222 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023223 parNodes[j] = parNodes[oldNum];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023224
23225 if (parBind->nbNodes != oldNum) {
23226 /*
23227 * If new items exist, move last new item to
23228 * last of old items.
23229 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023230 parNodes[oldNum] =
23231 parNodes[parBind->nbNodes];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023232 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023233 if (parBind->dupls == NULL) {
23234 parBind->dupls = xmlSchemaItemListCreate();
23235 if (parBind->dupls == NULL)
23236 goto internal_error;
23237 }
23238 xmlSchemaItemListAdd(parBind->dupls, parNode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023239 } else {
23240 /*
23241 * Add the node-table entry (node and key-sequence) of
23242 * the child node to the node table of the parent node.
23243 */
23244 if (parBind->nodeTable == NULL) {
23245 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023246 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023247 if (parBind->nodeTable == NULL) {
23248 xmlSchemaVErrMemory(NULL,
23249 "allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023250 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023251 }
23252 parBind->sizeNodes = 1;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023253 } else if (parBind->nbNodes >= parBind->sizeNodes) {
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023254 parBind->sizeNodes *= 2;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023255 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23256 xmlRealloc(parBind->nodeTable, parBind->sizeNodes *
23257 sizeof(xmlSchemaPSVIIDCNodePtr));
23258 if (parBind->nodeTable == NULL) {
23259 xmlSchemaVErrMemory(NULL,
23260 "re-allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023261 goto internal_error;
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000023262 }
23263 }
23264 parNodes = parBind->nodeTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023265 /*
23266 * Append the new node-table entry to the 'new node-table
23267 * entries' section.
23268 */
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000023269 parNodes[parBind->nbNodes++] = node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023270 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023271
23272 }
23273
23274 }
23275 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023276 /*
23277 * No binding for the IDC was found: create a new one and
23278 * copy all node-tables.
23279 */
23280 parBind = xmlSchemaIDCNewBinding(bind->definition);
23281 if (parBind == NULL)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023282 goto internal_error;
23283
23284 /*
23285 * TODO: Hmm, how to optimize the initial number of
23286 * allocated entries?
23287 */
23288 if (bind->nbNodes != 0) {
23289 /*
23290 * Add all IDC node-table entries.
23291 */
23292 if (! vctxt->psviExposeIDCNodeTables) {
23293 /*
23294 * Just move the entries.
23295 * NOTE: this is quite save here, since
23296 * all the keyref lookups have already been
23297 * performed.
23298 */
23299 parBind->nodeTable = bind->nodeTable;
23300 bind->nodeTable = NULL;
23301 parBind->sizeNodes = bind->sizeNodes;
23302 bind->sizeNodes = 0;
23303 parBind->nbNodes = bind->nbNodes;
23304 bind->nbNodes = 0;
23305 } else {
23306 /*
23307 * Copy the entries.
23308 */
23309 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23310 xmlMalloc(bind->nbNodes *
23311 sizeof(xmlSchemaPSVIIDCNodePtr));
23312 if (parBind->nodeTable == NULL) {
23313 xmlSchemaVErrMemory(NULL,
23314 "allocating an array of IDC node-table "
23315 "items", NULL);
23316 xmlSchemaIDCFreeBinding(parBind);
23317 goto internal_error;
23318 }
23319 parBind->sizeNodes = bind->nbNodes;
23320 parBind->nbNodes = bind->nbNodes;
23321 memcpy(parBind->nodeTable, bind->nodeTable,
23322 bind->nbNodes * sizeof(xmlSchemaPSVIIDCNodePtr));
23323 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023324 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023325 if (bind->dupls) {
23326 /*
23327 * Move the duplicates.
23328 */
23329 if (parBind->dupls != NULL)
23330 xmlSchemaItemListFree(parBind->dupls);
23331 parBind->dupls = bind->dupls;
23332 bind->dupls = NULL;
23333 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023334 if (*parTable == NULL)
23335 *parTable = parBind;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023336 else {
23337 parBind->next = *parTable;
23338 *parTable = parBind;
23339 }
23340 }
23341
23342next_binding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023343 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023344 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023345 return (0);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023346
23347internal_error:
23348 return(-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023349}
23350
23351/**
23352 * xmlSchemaCheckCVCIDCKeyRef:
23353 * @vctxt: the WXS validation context
23354 * @elemDecl: the element declaration
23355 *
23356 * Check the cvc-idc-keyref constraints.
23357 */
23358static int
23359xmlSchemaCheckCVCIDCKeyRef(xmlSchemaValidCtxtPtr vctxt)
23360{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023361 xmlSchemaIDCMatcherPtr matcher;
23362 xmlSchemaPSVIIDCBindingPtr bind;
23363
23364 matcher = vctxt->inode->idcMatchers;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023365 /*
23366 * Find a keyref.
23367 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023368 while (matcher != NULL) {
23369 if ((matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) &&
23370 matcher->targets &&
23371 matcher->targets->nbItems)
23372 {
23373 int i, j, k, res, nbFields, hasDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023374 xmlSchemaPSVIIDCKeyPtr *refKeys, *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023375 xmlSchemaPSVIIDCNodePtr refNode = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023376
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023377 nbFields = matcher->aidc->def->nbFields;
23378
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023379 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023380 * Find the IDC node-table for the referenced IDC key/unique.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023381 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023382 bind = vctxt->inode->idcTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023383 while (bind != NULL) {
23384 if ((xmlSchemaIDCPtr) matcher->aidc->def->ref->item ==
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023385 bind->definition)
23386 break;
23387 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023388 }
23389 hasDupls = (bind && bind->dupls && bind->dupls->nbItems) ? 1 : 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023390 /*
23391 * Search for a matching key-sequences.
23392 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023393 for (i = 0; i < matcher->targets->nbItems; i++) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023394 res = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023395 refNode = matcher->targets->items[i];
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023396 if (bind != NULL) {
23397 refKeys = refNode->keys;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023398 for (j = 0; j < bind->nbNodes; j++) {
23399 keys = bind->nodeTable[j]->keys;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023400 for (k = 0; k < nbFields; k++) {
23401 res = xmlSchemaAreValuesEqual(keys[k]->val,
23402 refKeys[k]->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023403 if (res == 0)
23404 break;
23405 else if (res == -1) {
23406 return (-1);
23407 }
23408 }
23409 if (res == 1) {
23410 /*
23411 * Match found.
23412 */
23413 break;
23414 }
23415 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023416 if ((res == 0) && hasDupls) {
23417 /*
23418 * Search in duplicates
23419 */
23420 for (j = 0; j < bind->dupls->nbItems; j++) {
23421 keys = ((xmlSchemaPSVIIDCNodePtr)
23422 bind->dupls->items[j])->keys;
23423 for (k = 0; k < nbFields; k++) {
23424 res = xmlSchemaAreValuesEqual(keys[k]->val,
23425 refKeys[k]->val);
23426 if (res == 0)
23427 break;
23428 else if (res == -1) {
23429 return (-1);
23430 }
23431 }
23432 if (res == 1) {
23433 /*
23434 * Match in duplicates found.
23435 */
23436 xmlChar *str = NULL, *strB = NULL;
23437 xmlSchemaKeyrefErr(vctxt,
23438 XML_SCHEMAV_CVC_IDC, refNode,
23439 (xmlSchemaTypePtr) matcher->aidc->def,
23440 "More than one match found for "
23441 "key-sequence %s of keyref '%s'",
23442 xmlSchemaFormatIDCKeySequence(vctxt, &str,
23443 refNode->keys, nbFields),
23444 xmlSchemaGetComponentQName(&strB,
23445 matcher->aidc->def));
23446 FREE_AND_NULL(str);
23447 FREE_AND_NULL(strB);
23448 break;
23449 }
23450 }
23451 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023452 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023453
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023454 if (res == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023455 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023456 xmlSchemaKeyrefErr(vctxt,
23457 XML_SCHEMAV_CVC_IDC, refNode,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023458 (xmlSchemaTypePtr) matcher->aidc->def,
23459 "No match found for key-sequence %s of keyref '%s'",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000023460 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023461 refNode->keys, nbFields),
23462 xmlSchemaGetComponentQName(&strB, matcher->aidc->def));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023463 FREE_AND_NULL(str);
23464 FREE_AND_NULL(strB);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023465 }
23466 }
23467 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023468 matcher = matcher->next;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023469 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023470 /* TODO: Return an error if any error encountered. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023471 return (0);
23472}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023473
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023474/************************************************************************
23475 * *
23476 * XML Reader validation code *
23477 * *
23478 ************************************************************************/
23479
23480static xmlSchemaAttrInfoPtr
23481xmlSchemaGetFreshAttrInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023482{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023483 xmlSchemaAttrInfoPtr iattr;
23484 /*
23485 * Grow/create list of attribute infos.
23486 */
23487 if (vctxt->attrInfos == NULL) {
23488 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23489 xmlMalloc(sizeof(xmlSchemaAttrInfoPtr));
23490 vctxt->sizeAttrInfos = 1;
23491 if (vctxt->attrInfos == NULL) {
23492 xmlSchemaVErrMemory(vctxt,
23493 "allocating attribute info list", NULL);
23494 return (NULL);
23495 }
23496 } else if (vctxt->sizeAttrInfos <= vctxt->nbAttrInfos) {
23497 vctxt->sizeAttrInfos++;
23498 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23499 xmlRealloc(vctxt->attrInfos,
23500 vctxt->sizeAttrInfos * sizeof(xmlSchemaAttrInfoPtr));
23501 if (vctxt->attrInfos == NULL) {
23502 xmlSchemaVErrMemory(vctxt,
23503 "re-allocating attribute info list", NULL);
23504 return (NULL);
23505 }
23506 } else {
23507 iattr = vctxt->attrInfos[vctxt->nbAttrInfos++];
23508 if (iattr->localName != NULL) {
23509 VERROR_INT("xmlSchemaGetFreshAttrInfo",
23510 "attr info not cleared");
23511 return (NULL);
23512 }
23513 iattr->nodeType = XML_ATTRIBUTE_NODE;
23514 return (iattr);
23515 }
23516 /*
23517 * Create an attribute info.
23518 */
23519 iattr = (xmlSchemaAttrInfoPtr)
23520 xmlMalloc(sizeof(xmlSchemaAttrInfo));
23521 if (iattr == NULL) {
23522 xmlSchemaVErrMemory(vctxt, "creating new attribute info", NULL);
23523 return (NULL);
23524 }
23525 memset(iattr, 0, sizeof(xmlSchemaAttrInfo));
23526 iattr->nodeType = XML_ATTRIBUTE_NODE;
23527 vctxt->attrInfos[vctxt->nbAttrInfos++] = iattr;
23528
23529 return (iattr);
23530}
23531
23532static int
23533xmlSchemaValidatorPushAttribute(xmlSchemaValidCtxtPtr vctxt,
23534 xmlNodePtr attrNode,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023535 int nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023536 const xmlChar *localName,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023537 const xmlChar *nsName,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023538 int ownedNames,
23539 xmlChar *value,
23540 int ownedValue)
23541{
23542 xmlSchemaAttrInfoPtr attr;
23543
23544 attr = xmlSchemaGetFreshAttrInfo(vctxt);
23545 if (attr == NULL) {
23546 VERROR_INT("xmlSchemaPushAttribute",
23547 "calling xmlSchemaGetFreshAttrInfo()");
23548 return (-1);
23549 }
23550 attr->node = attrNode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023551 attr->nodeLine = nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023552 attr->state = XML_SCHEMAS_ATTR_UNKNOWN;
23553 attr->localName = localName;
23554 attr->nsName = nsName;
23555 if (ownedNames)
23556 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
23557 /*
23558 * Evaluate if it's an XSI attribute.
23559 */
23560 if (nsName != NULL) {
23561 if (xmlStrEqual(localName, BAD_CAST "nil")) {
23562 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23563 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NIL;
23564 }
23565 } else if (xmlStrEqual(localName, BAD_CAST "type")) {
23566 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23567 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_TYPE;
23568 }
23569 } else if (xmlStrEqual(localName, BAD_CAST "schemaLocation")) {
23570 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23571 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC;
23572 }
23573 } else if (xmlStrEqual(localName, BAD_CAST "noNamespaceSchemaLocation")) {
23574 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23575 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC;
23576 }
23577 } else if (xmlStrEqual(attr->nsName, xmlNamespaceNs)) {
23578 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XMLNS;
23579 }
23580 }
23581 attr->value = value;
23582 if (ownedValue)
23583 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
23584 if (attr->metaType != 0)
23585 attr->state = XML_SCHEMAS_ATTR_META;
23586 return (0);
23587}
23588
23589static void
23590xmlSchemaClearElemInfo(xmlSchemaNodeInfoPtr ielem)
23591{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023592 ielem->hasKeyrefs = 0;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000023593 ielem->appliedXPath = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023594 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
23595 FREE_AND_NULL(ielem->localName);
23596 FREE_AND_NULL(ielem->nsName);
23597 } else {
23598 ielem->localName = NULL;
23599 ielem->nsName = NULL;
23600 }
23601 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
23602 FREE_AND_NULL(ielem->value);
23603 } else {
23604 ielem->value = NULL;
23605 }
23606 if (ielem->val != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023607 /*
23608 * PSVI TODO: Be careful not to free it when the value is
23609 * exposed via PSVI.
23610 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023611 xmlSchemaFreeValue(ielem->val);
23612 ielem->val = NULL;
23613 }
23614 if (ielem->idcMatchers != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023615 /*
23616 * URGENT OPTIMIZE TODO: Use a pool of IDC matchers.
23617 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023618 xmlSchemaIDCFreeMatcherList(ielem->idcMatchers);
23619 ielem->idcMatchers = NULL;
23620 }
23621 if (ielem->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023622 /*
23623 * OPTIMIZE TODO: Use a pool of IDC tables??.
23624 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023625 xmlSchemaIDCFreeIDCTable(ielem->idcTable);
23626 ielem->idcTable = NULL;
23627 }
23628 if (ielem->regexCtxt != NULL) {
23629 xmlRegFreeExecCtxt(ielem->regexCtxt);
23630 ielem->regexCtxt = NULL;
23631 }
23632 if (ielem->nsBindings != NULL) {
23633 xmlFree((xmlChar **)ielem->nsBindings);
23634 ielem->nsBindings = NULL;
23635 ielem->nbNsBindings = 0;
23636 ielem->sizeNsBindings = 0;
23637 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023638}
23639
23640/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023641 * xmlSchemaGetFreshElemInfo:
23642 * @vctxt: the schema validation context
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023643 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023644 * Creates/reuses and initializes the element info item for
23645 * the currect tree depth.
23646 *
23647 * Returns the element info item or NULL on API or internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023648 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023649static xmlSchemaNodeInfoPtr
23650xmlSchemaGetFreshElemInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023651{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023652 xmlSchemaNodeInfoPtr info = NULL;
23653
23654 if (vctxt->depth > vctxt->sizeElemInfos) {
23655 VERROR_INT("xmlSchemaGetFreshElemInfo",
23656 "inconsistent depth encountered");
23657 return (NULL);
23658 }
23659 if (vctxt->elemInfos == NULL) {
23660 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23661 xmlMalloc(10 * sizeof(xmlSchemaNodeInfoPtr));
23662 if (vctxt->elemInfos == NULL) {
23663 xmlSchemaVErrMemory(vctxt,
23664 "allocating the element info array", NULL);
23665 return (NULL);
23666 }
23667 memset(vctxt->elemInfos, 0, 10 * sizeof(xmlSchemaNodeInfoPtr));
23668 vctxt->sizeElemInfos = 10;
23669 } else if (vctxt->sizeElemInfos <= vctxt->depth) {
23670 int i = vctxt->sizeElemInfos;
23671
23672 vctxt->sizeElemInfos *= 2;
23673 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23674 xmlRealloc(vctxt->elemInfos, vctxt->sizeElemInfos *
23675 sizeof(xmlSchemaNodeInfoPtr));
23676 if (vctxt->elemInfos == NULL) {
23677 xmlSchemaVErrMemory(vctxt,
23678 "re-allocating the element info array", NULL);
23679 return (NULL);
23680 }
23681 /*
23682 * We need the new memory to be NULLed.
23683 * TODO: Use memset instead?
23684 */
23685 for (; i < vctxt->sizeElemInfos; i++)
23686 vctxt->elemInfos[i] = NULL;
23687 } else
23688 info = vctxt->elemInfos[vctxt->depth];
23689
23690 if (info == NULL) {
23691 info = (xmlSchemaNodeInfoPtr)
23692 xmlMalloc(sizeof(xmlSchemaNodeInfo));
23693 if (info == NULL) {
23694 xmlSchemaVErrMemory(vctxt,
23695 "allocating an element info", NULL);
23696 return (NULL);
23697 }
23698 vctxt->elemInfos[vctxt->depth] = info;
23699 } else {
23700 if (info->localName != NULL) {
23701 VERROR_INT("xmlSchemaGetFreshElemInfo",
23702 "elem info has not been cleared");
23703 return (NULL);
23704 }
23705 }
23706 memset(info, 0, sizeof(xmlSchemaNodeInfo));
23707 info->nodeType = XML_ELEMENT_NODE;
23708 info->depth = vctxt->depth;
23709
23710 return (info);
23711}
23712
23713#define ACTIVATE_ATTRIBUTE(item) vctxt->inode = (xmlSchemaNodeInfoPtr) item;
23714#define ACTIVATE_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth];
23715#define ACTIVATE_PARENT_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth -1];
23716
23717static int
23718xmlSchemaValidateFacets(xmlSchemaAbstractCtxtPtr actxt,
23719 xmlNodePtr node,
23720 xmlSchemaTypePtr type,
23721 xmlSchemaValType valType,
23722 const xmlChar * value,
23723 xmlSchemaValPtr val,
23724 unsigned long length,
23725 int fireErrors)
23726{
23727 int ret, error = 0;
23728
23729 xmlSchemaTypePtr tmpType;
23730 xmlSchemaFacetLinkPtr facetLink;
23731 xmlSchemaFacetPtr facet;
23732 unsigned long len = 0;
23733 xmlSchemaWhitespaceValueType ws;
23734
23735 /*
23736 * In Libxml2, derived built-in types have currently no explicit facets.
23737 */
23738 if (type->type == XML_SCHEMA_TYPE_BASIC)
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000023739 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023740
23741 /*
23742 * NOTE: Do not jump away, if the facetSet of the given type is
23743 * empty: until now, "pattern" and "enumeration" facets of the
23744 * *base types* need to be checked as well.
23745 */
23746 if (type->facetSet == NULL)
23747 goto pattern_and_enum;
23748
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023749 if (! WXS_IS_ATOMIC(type)) {
23750 if (WXS_IS_LIST(type))
23751 goto WXS_IS_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023752 else
23753 goto pattern_and_enum;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023754 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023755 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023756 * Whitespace handling is only of importance for string-based
23757 * types.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023758 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023759 tmpType = xmlSchemaGetPrimitiveType(type);
23760 if ((tmpType->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023761 WXS_IS_ANY_SIMPLE_TYPE(tmpType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023762 ws = xmlSchemaGetWhiteSpaceFacetValue(type);
23763 } else
23764 ws = XML_SCHEMA_WHITESPACE_COLLAPSE;
23765 /*
23766 * If the value was not computed (for string or
23767 * anySimpleType based types), then use the provided
23768 * type.
23769 */
23770 if (val == NULL)
23771 valType = valType;
23772 else
23773 valType = xmlSchemaGetValType(val);
23774
23775 ret = 0;
23776 for (facetLink = type->facetSet; facetLink != NULL;
23777 facetLink = facetLink->next) {
23778 /*
23779 * Skip the pattern "whiteSpace": it is used to
23780 * format the character content beforehand.
23781 */
23782 switch (facetLink->facet->type) {
23783 case XML_SCHEMA_FACET_WHITESPACE:
23784 case XML_SCHEMA_FACET_PATTERN:
23785 case XML_SCHEMA_FACET_ENUMERATION:
23786 continue;
23787 case XML_SCHEMA_FACET_LENGTH:
23788 case XML_SCHEMA_FACET_MINLENGTH:
23789 case XML_SCHEMA_FACET_MAXLENGTH:
23790 ret = xmlSchemaValidateLengthFacetWhtsp(facetLink->facet,
23791 valType, value, val, &len, ws);
23792 break;
23793 default:
23794 ret = xmlSchemaValidateFacetWhtsp(facetLink->facet, ws,
23795 valType, value, val, ws);
23796 break;
23797 }
23798 if (ret < 0) {
23799 AERROR_INT("xmlSchemaValidateFacets",
23800 "validating against a atomic type facet");
23801 return (-1);
23802 } else if (ret > 0) {
23803 if (fireErrors)
23804 xmlSchemaFacetErr(actxt, ret, node,
23805 value, len, type, facetLink->facet, NULL, NULL, NULL);
23806 else
23807 return (ret);
23808 if (error == 0)
23809 error = ret;
23810 }
23811 ret = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023812 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023813
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023814WXS_IS_LIST:
23815 if (! WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023816 goto pattern_and_enum;
23817 /*
23818 * "length", "minLength" and "maxLength" of list types.
23819 */
23820 ret = 0;
23821 for (facetLink = type->facetSet; facetLink != NULL;
23822 facetLink = facetLink->next) {
23823
23824 switch (facetLink->facet->type) {
23825 case XML_SCHEMA_FACET_LENGTH:
23826 case XML_SCHEMA_FACET_MINLENGTH:
23827 case XML_SCHEMA_FACET_MAXLENGTH:
23828 ret = xmlSchemaValidateListSimpleTypeFacet(facetLink->facet,
23829 value, length, NULL);
23830 break;
23831 default:
23832 continue;
23833 }
23834 if (ret < 0) {
23835 AERROR_INT("xmlSchemaValidateFacets",
23836 "validating against a list type facet");
23837 return (-1);
23838 } else if (ret > 0) {
23839 if (fireErrors)
23840 xmlSchemaFacetErr(actxt, ret, node,
23841 value, length, type, facetLink->facet, NULL, NULL, NULL);
23842 else
23843 return (ret);
23844 if (error == 0)
23845 error = ret;
23846 }
23847 ret = 0;
23848 }
23849
23850pattern_and_enum:
23851 if (error >= 0) {
23852 int found = 0;
23853 /*
23854 * Process enumerations. Facet values are in the value space
23855 * of the defining type's base type. This seems to be a bug in the
23856 * XML Schema 1.0 spec. Use the whitespace type of the base type.
23857 * Only the first set of enumerations in the ancestor-or-self axis
23858 * is used for validation.
23859 */
23860 ret = 0;
23861 tmpType = type;
23862 do {
23863 for (facet = tmpType->facets; facet != NULL; facet = facet->next) {
23864 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
23865 continue;
23866 found = 1;
23867 ret = xmlSchemaAreValuesEqual(facet->val, val);
23868 if (ret == 1)
23869 break;
23870 else if (ret < 0) {
23871 AERROR_INT("xmlSchemaValidateFacets",
23872 "validating against an enumeration facet");
23873 return (-1);
23874 }
23875 }
23876 if (ret != 0)
23877 break;
23878 tmpType = tmpType->baseType;
23879 } while ((tmpType != NULL) &&
23880 (tmpType->type != XML_SCHEMA_TYPE_BASIC));
23881 if (found && (ret == 0)) {
23882 ret = XML_SCHEMAV_CVC_ENUMERATION_VALID;
23883 if (fireErrors) {
23884 xmlSchemaFacetErr(actxt, ret, node,
23885 value, 0, type, NULL, NULL, NULL, NULL);
23886 } else
23887 return (ret);
23888 if (error == 0)
23889 error = ret;
23890 }
23891 }
23892
23893 if (error >= 0) {
23894 int found;
23895 /*
23896 * Process patters. Pattern facets are ORed at type level
23897 * and ANDed if derived. Walk the base type axis.
23898 */
23899 tmpType = type;
23900 facet = NULL;
23901 do {
23902 found = 0;
23903 for (facetLink = tmpType->facetSet; facetLink != NULL;
23904 facetLink = facetLink->next) {
23905 if (facetLink->facet->type != XML_SCHEMA_FACET_PATTERN)
23906 continue;
23907 found = 1;
23908 /*
23909 * NOTE that for patterns, @value needs to be the
23910 * normalized vaule.
23911 */
23912 ret = xmlRegexpExec(facetLink->facet->regexp, value);
23913 if (ret == 1)
23914 break;
23915 else if (ret < 0) {
23916 AERROR_INT("xmlSchemaValidateFacets",
23917 "validating against a pattern facet");
23918 return (-1);
23919 } else {
23920 /*
23921 * Save the last non-validating facet.
23922 */
23923 facet = facetLink->facet;
23924 }
23925 }
23926 if (found && (ret != 1)) {
23927 ret = XML_SCHEMAV_CVC_PATTERN_VALID;
23928 if (fireErrors) {
23929 xmlSchemaFacetErr(actxt, ret, node,
23930 value, 0, type, facet, NULL, NULL, NULL);
23931 } else
23932 return (ret);
23933 if (error == 0)
23934 error = ret;
23935 break;
23936 }
23937 tmpType = tmpType->baseType;
23938 } while ((tmpType != NULL) && (tmpType->type != XML_SCHEMA_TYPE_BASIC));
23939 }
23940
23941 return (error);
23942}
23943
23944static xmlChar *
23945xmlSchemaNormalizeValue(xmlSchemaTypePtr type,
23946 const xmlChar *value)
23947{
23948 switch (xmlSchemaGetWhiteSpaceFacetValue(type)) {
23949 case XML_SCHEMA_WHITESPACE_COLLAPSE:
23950 return (xmlSchemaCollapseString(value));
23951 case XML_SCHEMA_WHITESPACE_REPLACE:
23952 return (xmlSchemaWhiteSpaceReplace(value));
23953 default:
23954 return (NULL);
23955 }
23956}
23957
23958static int
23959xmlSchemaValidateQName(xmlSchemaValidCtxtPtr vctxt,
23960 const xmlChar *value,
23961 xmlSchemaValPtr *val,
23962 int valNeeded)
23963{
23964 int ret;
23965 const xmlChar *nsName;
23966 xmlChar *local, *prefix = NULL;
23967
23968 ret = xmlValidateQName(value, 1);
23969 if (ret != 0) {
23970 if (ret == -1) {
23971 VERROR_INT("xmlSchemaValidateQName",
23972 "calling xmlValidateQName()");
23973 return (-1);
23974 }
23975 return( XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1);
23976 }
23977 /*
23978 * NOTE: xmlSplitQName2 will always return a duplicated
23979 * strings.
23980 */
23981 local = xmlSplitQName2(value, &prefix);
23982 if (local == NULL)
23983 local = xmlStrdup(value);
23984 /*
23985 * OPTIMIZE TODO: Use flags for:
23986 * - is there any namespace binding?
23987 * - is there a default namespace?
23988 */
23989 nsName = xmlSchemaLookupNamespace(vctxt, prefix);
23990
23991 if (prefix != NULL) {
23992 xmlFree(prefix);
23993 /*
23994 * A namespace must be found if the prefix is
23995 * NOT NULL.
23996 */
23997 if (nsName == NULL) {
23998 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023999 xmlSchemaCustomErr(ACTXT_CAST vctxt, ret, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024000 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024001 "The QName value '%s' has no "
24002 "corresponding namespace declaration in "
24003 "scope", value, NULL);
24004 if (local != NULL)
24005 xmlFree(local);
24006 return (ret);
24007 }
24008 }
24009 if (valNeeded && val) {
24010 if (nsName != NULL)
24011 *val = xmlSchemaNewQNameValue(
24012 BAD_CAST xmlStrdup(nsName), BAD_CAST local);
24013 else
24014 *val = xmlSchemaNewQNameValue(NULL,
24015 BAD_CAST local);
24016 } else
24017 xmlFree(local);
24018 return (0);
24019}
24020
24021/*
24022* cvc-simple-type
24023*/
24024static int
24025xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
24026 xmlNodePtr node,
24027 xmlSchemaTypePtr type,
24028 const xmlChar *value,
24029 xmlSchemaValPtr *retVal,
24030 int fireErrors,
24031 int normalize,
24032 int isNormalized)
24033{
24034 int ret = 0, valNeeded = (retVal) ? 1 : 0;
24035 xmlSchemaValPtr val = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024036 /* xmlSchemaWhitespaceValueType ws; */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024037 xmlChar *normValue = NULL;
24038
24039#define NORMALIZE(atype) \
24040 if ((! isNormalized) && \
24041 (normalize || (type->flags & XML_SCHEMAS_TYPE_NORMVALUENEEDED))) { \
24042 normValue = xmlSchemaNormalizeValue(atype, value); \
24043 if (normValue != NULL) \
24044 value = normValue; \
24045 isNormalized = 1; \
24046 }
24047
24048 if ((retVal != NULL) && (*retVal != NULL)) {
24049 xmlSchemaFreeValue(*retVal);
24050 *retVal = NULL;
24051 }
24052 /*
24053 * 3.14.4 Simple Type Definition Validation Rules
24054 * Validation Rule: String Valid
24055 */
24056 /*
24057 * 1 It is schema-valid with respect to that definition as defined
24058 * by Datatype Valid in [XML Schemas: Datatypes].
24059 */
24060 /*
24061 * 2.1 If The definition is ENTITY or is validly derived from ENTITY given
24062 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6), then
24063 * the string must be a ·declared entity name·.
24064 */
24065 /*
24066 * 2.2 If The definition is ENTITIES or is validly derived from ENTITIES
24067 * given the empty set, as defined in Type Derivation OK (Simple) (§3.14.6),
24068 * then every whitespace-delimited substring of the string must be a ·declared
24069 * entity name·.
24070 */
24071 /*
24072 * 2.3 otherwise no further condition applies.
24073 */
24074 if ((! valNeeded) && (type->flags & XML_SCHEMAS_TYPE_FACETSNEEDVALUE))
24075 valNeeded = 1;
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000024076 if (value == NULL)
24077 value = BAD_CAST "";
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024078 if (WXS_IS_ANY_SIMPLE_TYPE(type) || WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024079 xmlSchemaTypePtr biType; /* The built-in type. */
24080 /*
24081 * SPEC (1.2.1) "if {variety} is ·atomic· then the string must ·match·
24082 * a literal in the ·lexical space· of {base type definition}"
24083 */
24084 /*
24085 * Whitespace-normalize.
24086 */
24087 NORMALIZE(type);
24088 if (type->type != XML_SCHEMA_TYPE_BASIC) {
24089 /*
24090 * Get the built-in type.
24091 */
24092 biType = type->baseType;
24093 while ((biType != NULL) &&
24094 (biType->type != XML_SCHEMA_TYPE_BASIC))
24095 biType = biType->baseType;
24096
24097 if (biType == NULL) {
24098 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24099 "could not get the built-in type");
24100 goto internal_error;
24101 }
24102 } else
24103 biType = type;
24104 /*
24105 * NOTATIONs need to be processed here, since they need
24106 * to lookup in the hashtable of NOTATION declarations of the schema.
24107 */
24108 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
24109 switch (biType->builtInType) {
24110 case XML_SCHEMAS_NOTATION:
24111 ret = xmlSchemaValidateNotation(
24112 (xmlSchemaValidCtxtPtr) actxt,
24113 ((xmlSchemaValidCtxtPtr) actxt)->schema,
24114 NULL, value, &val, valNeeded);
24115 break;
24116 case XML_SCHEMAS_QNAME:
24117 ret = xmlSchemaValidateQName((xmlSchemaValidCtxtPtr) actxt,
24118 value, &val, valNeeded);
24119 break;
24120 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024121 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024122 if (valNeeded)
24123 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24124 value, &val, NULL);
24125 else
24126 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24127 value, NULL, NULL);
24128 break;
24129 }
24130 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
24131 switch (biType->builtInType) {
24132 case XML_SCHEMAS_NOTATION:
24133 ret = xmlSchemaValidateNotation(NULL,
24134 ((xmlSchemaParserCtxtPtr) actxt)->schema, node,
24135 value, &val, valNeeded);
24136 break;
24137 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024138 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024139 if (valNeeded)
24140 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24141 value, &val, node);
24142 else
24143 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24144 value, NULL, node);
24145 break;
24146 }
24147 } else {
24148 /*
24149 * Validation via a public API is not implemented yet.
24150 */
24151 TODO
24152 goto internal_error;
24153 }
24154 if (ret != 0) {
24155 if (ret < 0) {
24156 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24157 "validating against a built-in type");
24158 goto internal_error;
24159 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024160 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024161 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24162 else
24163 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24164 }
24165 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24166 /*
24167 * Check facets.
24168 */
24169 ret = xmlSchemaValidateFacets(actxt, node, type,
24170 (xmlSchemaValType) biType->builtInType, value, val,
24171 0, fireErrors);
24172 if (ret != 0) {
24173 if (ret < 0) {
24174 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24175 "validating facets of atomic simple type");
24176 goto internal_error;
24177 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024178 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024179 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24180 else
24181 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24182 }
24183 }
24184 if (fireErrors && (ret > 0))
24185 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024186 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024187
24188 xmlSchemaTypePtr itemType;
24189 const xmlChar *cur, *end;
24190 xmlChar *tmpValue = NULL;
24191 unsigned long len = 0;
24192 xmlSchemaValPtr prevVal = NULL, curVal = NULL;
24193 /* 1.2.2 if {variety} is ·list· then the string must be a sequence
24194 * of white space separated tokens, each of which ·match·es a literal
24195 * in the ·lexical space· of {item type definition}
24196 */
24197 /*
24198 * Note that XML_SCHEMAS_TYPE_NORMVALUENEEDED will be set if
24199 * the list type has an enum or pattern facet.
24200 */
24201 NORMALIZE(type);
24202 /*
24203 * VAL TODO: Optimize validation of empty values.
24204 * VAL TODO: We do not have computed values for lists.
24205 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024206 itemType = WXS_LIST_ITEMTYPE(type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024207 cur = value;
24208 do {
24209 while (IS_BLANK_CH(*cur))
24210 cur++;
24211 end = cur;
24212 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
24213 end++;
24214 if (end == cur)
24215 break;
24216 tmpValue = xmlStrndup(cur, end - cur);
24217 len++;
24218
24219 if (valNeeded)
24220 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24221 tmpValue, &curVal, fireErrors, 0, 1);
24222 else
24223 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24224 tmpValue, NULL, fireErrors, 0, 1);
24225 FREE_AND_NULL(tmpValue);
24226 if (curVal != NULL) {
24227 /*
24228 * Add to list of computed values.
24229 */
24230 if (val == NULL)
24231 val = curVal;
24232 else
24233 xmlSchemaValueAppend(prevVal, curVal);
24234 prevVal = curVal;
24235 curVal = NULL;
24236 }
24237 if (ret != 0) {
24238 if (ret < 0) {
24239 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24240 "validating an item of list simple type");
24241 goto internal_error;
24242 }
24243 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24244 break;
24245 }
24246 cur = end;
24247 } while (*cur != 0);
24248 FREE_AND_NULL(tmpValue);
24249 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24250 /*
24251 * Apply facets (pattern, enumeration).
24252 */
24253 ret = xmlSchemaValidateFacets(actxt, node, type,
24254 XML_SCHEMAS_UNKNOWN, value, val,
24255 len, fireErrors);
24256 if (ret != 0) {
24257 if (ret < 0) {
24258 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24259 "validating facets of list simple type");
24260 goto internal_error;
24261 }
24262 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24263 }
24264 }
24265 if (fireErrors && (ret > 0)) {
24266 /*
24267 * Report the normalized value.
24268 */
24269 normalize = 1;
24270 NORMALIZE(type);
24271 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24272 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024273 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024274 xmlSchemaTypeLinkPtr memberLink;
24275 /*
24276 * TODO: For all datatypes ·derived· by ·union· whiteSpace does
24277 * not apply directly; however, the normalization behavior of ·union·
24278 * types is controlled by the value of whiteSpace on that one of the
24279 * ·memberTypes· against which the ·union· is successfully validated.
24280 *
24281 * This means that the value is normalized by the first validating
24282 * member type, then the facets of the union type are applied. This
24283 * needs changing of the value!
24284 */
24285
24286 /*
24287 * 1.2.3 if {variety} is ·union· then the string must ·match· a
24288 * literal in the ·lexical space· of at least one member of
24289 * {member type definitions}
24290 */
24291 memberLink = xmlSchemaGetUnionSimpleTypeMemberTypes(type);
24292 if (memberLink == NULL) {
24293 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24294 "union simple type has no member types");
24295 goto internal_error;
24296 }
24297 /*
24298 * Always normalize union type values, since we currently
24299 * cannot store the whitespace information with the value
24300 * itself; otherwise a later value-comparison would be
24301 * not possible.
24302 */
24303 while (memberLink != NULL) {
24304 if (valNeeded)
24305 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24306 memberLink->type, value, &val, 0, 1, 0);
24307 else
24308 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24309 memberLink->type, value, NULL, 0, 1, 0);
24310 if (ret <= 0)
24311 break;
24312 memberLink = memberLink->next;
24313 }
24314 if (ret != 0) {
24315 if (ret < 0) {
24316 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24317 "validating members of union simple type");
24318 goto internal_error;
24319 }
24320 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24321 }
24322 /*
24323 * Apply facets (pattern, enumeration).
24324 */
24325 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24326 /*
24327 * The normalization behavior of ·union· types is controlled by
24328 * the value of whiteSpace on that one of the ·memberTypes·
24329 * against which the ·union· is successfully validated.
24330 */
24331 NORMALIZE(memberLink->type);
24332 ret = xmlSchemaValidateFacets(actxt, node, type,
24333 XML_SCHEMAS_UNKNOWN, value, val,
24334 0, fireErrors);
24335 if (ret != 0) {
24336 if (ret < 0) {
24337 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24338 "validating facets of union simple type");
24339 goto internal_error;
24340 }
24341 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24342 }
24343 }
24344 if (fireErrors && (ret > 0))
24345 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24346 }
24347
24348 if (normValue != NULL)
24349 xmlFree(normValue);
24350 if (ret == 0) {
24351 if (retVal != NULL)
24352 *retVal = val;
24353 else if (val != NULL)
24354 xmlSchemaFreeValue(val);
24355 } else if (val != NULL)
24356 xmlSchemaFreeValue(val);
24357 return (ret);
24358internal_error:
24359 if (normValue != NULL)
24360 xmlFree(normValue);
24361 if (val != NULL)
24362 xmlSchemaFreeValue(val);
24363 return (-1);
24364}
24365
24366static int
24367xmlSchemaVExpandQName(xmlSchemaValidCtxtPtr vctxt,
24368 const xmlChar *value,
24369 const xmlChar **nsName,
24370 const xmlChar **localName)
24371{
24372 int ret = 0;
24373
24374 if ((nsName == NULL) || (localName == NULL))
24375 return (-1);
24376 *nsName = NULL;
24377 *localName = NULL;
24378
24379 ret = xmlValidateQName(value, 1);
24380 if (ret == -1)
24381 return (-1);
24382 if (ret > 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024383 xmlSchemaSimpleTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024384 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
24385 value, xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), 1);
24386 return (1);
24387 }
24388 {
24389 xmlChar *local = NULL;
24390 xmlChar *prefix;
24391
24392 /*
24393 * NOTE: xmlSplitQName2 will return a duplicated
24394 * string.
24395 */
24396 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024397 if (local == NULL)
24398 *localName = xmlDictLookup(vctxt->dict, value, -1);
24399 else {
24400 *localName = xmlDictLookup(vctxt->dict, local, -1);
24401 xmlFree(local);
24402 }
24403
24404 *nsName = xmlSchemaLookupNamespace(vctxt, prefix);
24405
24406 if (prefix != NULL) {
24407 xmlFree(prefix);
24408 /*
24409 * A namespace must be found if the prefix is NOT NULL.
24410 */
24411 if (*nsName == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024412 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024413 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024414 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024415 "The QName value '%s' has no "
24416 "corresponding namespace declaration in scope",
24417 value, NULL);
24418 return (2);
24419 }
24420 }
24421 }
24422 return (0);
24423}
24424
24425static int
24426xmlSchemaProcessXSIType(xmlSchemaValidCtxtPtr vctxt,
24427 xmlSchemaAttrInfoPtr iattr,
24428 xmlSchemaTypePtr *localType,
24429 xmlSchemaElementPtr elemDecl)
24430{
24431 int ret = 0;
24432 /*
24433 * cvc-elt (3.3.4) : (4)
24434 * AND
24435 * Schema-Validity Assessment (Element) (cvc-assess-elt)
24436 * (1.2.1.2.1) - (1.2.1.2.4)
24437 * Handle 'xsi:type'.
24438 */
24439 if (localType == NULL)
24440 return (-1);
24441 *localType = NULL;
24442 if (iattr == NULL)
24443 return (0);
24444 else {
24445 const xmlChar *nsName = NULL, *local = NULL;
24446 /*
24447 * TODO: We should report a *warning* that the type was overriden
24448 * by the instance.
24449 */
24450 ACTIVATE_ATTRIBUTE(iattr);
24451 /*
24452 * (cvc-elt) (3.3.4) : (4.1)
24453 * (cvc-assess-elt) (1.2.1.2.2)
24454 */
24455 ret = xmlSchemaVExpandQName(vctxt, iattr->value,
24456 &nsName, &local);
24457 if (ret != 0) {
24458 if (ret < 0) {
24459 VERROR_INT("xmlSchemaValidateElementByDeclaration",
24460 "calling xmlSchemaQNameExpand() to validate the "
24461 "attribute 'xsi:type'");
24462 goto internal_error;
24463 }
24464 goto exit;
24465 }
24466 /*
24467 * (cvc-elt) (3.3.4) : (4.2)
24468 * (cvc-assess-elt) (1.2.1.2.3)
24469 */
24470 *localType = xmlSchemaGetType(vctxt->schema, local, nsName);
24471 if (*localType == NULL) {
24472 xmlChar *str = NULL;
24473
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024474 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024475 XML_SCHEMAV_CVC_ELT_4_2, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024476 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024477 "The QName value '%s' of the xsi:type attribute does not "
24478 "resolve to a type definition",
24479 xmlSchemaFormatQName(&str, nsName, local), NULL);
24480 FREE_AND_NULL(str);
24481 ret = vctxt->err;
24482 goto exit;
24483 }
24484 if (elemDecl != NULL) {
24485 int set = 0;
24486
24487 /*
24488 * SPEC cvc-elt (3.3.4) : (4.3) (Type Derivation OK)
24489 * "The ·local type definition· must be validly
24490 * derived from the {type definition} given the union of
24491 * the {disallowed substitutions} and the {type definition}'s
24492 * {prohibited substitutions}, as defined in
24493 * Type Derivation OK (Complex) (§3.4.6)
24494 * (if it is a complex type definition),
24495 * or given {disallowed substitutions} as defined in Type
24496 * Derivation OK (Simple) (§3.14.6) (if it is a simple type
24497 * definition)."
24498 *
24499 * {disallowed substitutions}: the "block" on the element decl.
24500 * {prohibited substitutions}: the "block" on the type def.
24501 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024502 /*
24503 * OPTIMIZE TODO: We could map types already evaluated
24504 * to be validly derived from other types to avoid checking
24505 * this over and over for the same types.
24506 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024507 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) ||
24508 (elemDecl->subtypes->flags &
24509 XML_SCHEMAS_TYPE_BLOCK_EXTENSION))
24510 set |= SUBSET_EXTENSION;
24511
24512 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) ||
24513 (elemDecl->subtypes->flags &
24514 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION))
24515 set |= SUBSET_RESTRICTION;
24516
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024517 /*
24518 * REMOVED and CHANGED since this produced a parser context
24519 * which adds to the string dict of the schema. So this would
24520 * change the schema and we don't want this. We don't need
24521 * the parser context anymore.
24522 *
24523 * if ((vctxt->pctxt == NULL) &&
24524 * (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
24525 * return (-1);
24526 */
24527
24528 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST vctxt, *localType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024529 elemDecl->subtypes, set) != 0) {
24530 xmlChar *str = NULL;
24531
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024532 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024533 XML_SCHEMAV_CVC_ELT_4_3, NULL, NULL,
24534 "The type definition '%s', specified by xsi:type, is "
24535 "blocked or not validly derived from the type definition "
24536 "of the element declaration",
24537 xmlSchemaFormatQName(&str,
24538 (*localType)->targetNamespace,
24539 (*localType)->name),
24540 NULL);
24541 FREE_AND_NULL(str);
24542 ret = vctxt->err;
24543 *localType = NULL;
24544 }
24545 }
24546 }
24547exit:
24548 ACTIVATE_ELEM;
24549 return (ret);
24550internal_error:
24551 ACTIVATE_ELEM;
24552 return (-1);
24553}
24554
24555static int
24556xmlSchemaValidateElemDecl(xmlSchemaValidCtxtPtr vctxt)
24557{
24558 xmlSchemaElementPtr elemDecl = vctxt->inode->decl;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024559 xmlSchemaTypePtr actualType = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024560
24561 /*
24562 * cvc-elt (3.3.4) : 1
24563 */
24564 if (elemDecl == NULL) {
24565 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL,
24566 "No matching declaration available");
24567 return (vctxt->err);
24568 }
24569 /*
24570 * cvc-elt (3.3.4) : 2
24571 */
24572 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) {
24573 VERROR(XML_SCHEMAV_CVC_ELT_2, NULL,
24574 "The element declaration is abstract");
24575 return (vctxt->err);
24576 }
24577 if (actualType == NULL) {
24578 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24579 "The type definition is absent");
24580 return (XML_SCHEMAV_CVC_TYPE_1);
24581 }
24582 if (vctxt->nbAttrInfos != 0) {
24583 int ret;
24584 xmlSchemaAttrInfoPtr iattr;
24585 /*
24586 * cvc-elt (3.3.4) : 3
24587 * Handle 'xsi:nil'.
24588 */
24589 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24590 XML_SCHEMA_ATTR_INFO_META_XSI_NIL);
24591 if (iattr) {
24592 ACTIVATE_ATTRIBUTE(iattr);
24593 /*
24594 * Validate the value.
24595 */
24596 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024597 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024598 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
24599 iattr->value, &(iattr->val), 1, 0, 0);
24600 ACTIVATE_ELEM;
24601 if (ret < 0) {
24602 VERROR_INT("xmlSchemaValidateElemDecl",
24603 "calling xmlSchemaVCheckCVCSimpleType() to "
24604 "validate the attribute 'xsi:nil'");
24605 return (-1);
24606 }
24607 if (ret == 0) {
24608 if ((elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) {
24609 /*
24610 * cvc-elt (3.3.4) : 3.1
24611 */
24612 VERROR(XML_SCHEMAV_CVC_ELT_3_1, NULL,
24613 "The element is not 'nillable'");
24614 /* Does not return an error on purpose. */
24615 } else {
24616 if (xmlSchemaValueGetAsBoolean(iattr->val)) {
24617 /*
24618 * cvc-elt (3.3.4) : 3.2.2
24619 */
24620 if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) &&
24621 (elemDecl->value != NULL)) {
24622 VERROR(XML_SCHEMAV_CVC_ELT_3_2_2, NULL,
24623 "The element cannot be 'nilled' because "
24624 "there is a fixed value constraint defined "
24625 "for it");
24626 /* Does not return an error on purpose. */
24627 } else
24628 vctxt->inode->flags |=
24629 XML_SCHEMA_ELEM_INFO_NILLED;
24630 }
24631 }
24632 }
24633 }
24634 /*
24635 * cvc-elt (3.3.4) : 4
24636 * Handle 'xsi:type'.
24637 */
24638 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24639 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
24640 if (iattr) {
24641 xmlSchemaTypePtr localType = NULL;
24642
24643 ret = xmlSchemaProcessXSIType(vctxt, iattr, &localType,
24644 elemDecl);
24645 if (ret != 0) {
24646 if (ret == -1) {
24647 VERROR_INT("xmlSchemaValidateElemDecl",
24648 "calling xmlSchemaProcessXSIType() to "
24649 "process the attribute 'xsi:type'");
24650 return (-1);
24651 }
24652 /* Does not return an error on purpose. */
24653 }
24654 if (localType != NULL) {
24655 vctxt->inode->flags |= XML_SCHEMA_ELEM_INFO_LOCAL_TYPE;
24656 actualType = localType;
24657 }
24658 }
24659 }
24660 /*
24661 * IDC: Register identity-constraint XPath matchers.
24662 */
24663 if ((elemDecl->idcs != NULL) &&
24664 (xmlSchemaIDCRegisterMatchers(vctxt, elemDecl) == -1))
24665 return (-1);
24666 /*
24667 * No actual type definition.
24668 */
24669 if (actualType == NULL) {
24670 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24671 "The type definition is absent");
24672 return (XML_SCHEMAV_CVC_TYPE_1);
24673 }
24674 /*
24675 * Remember the actual type definition.
24676 */
24677 vctxt->inode->typeDef = actualType;
24678
24679 return (0);
24680}
24681
24682static int
24683xmlSchemaVAttributesSimple(xmlSchemaValidCtxtPtr vctxt)
24684{
24685 xmlSchemaAttrInfoPtr iattr;
24686 int ret = 0, i;
24687
24688 /*
24689 * SPEC cvc-type (3.1.1)
24690 * "The attributes of must be empty, excepting those whose namespace
24691 * name is identical to http://www.w3.org/2001/XMLSchema-instance and
24692 * whose local name is one of type, nil, schemaLocation or
24693 * noNamespaceSchemaLocation."
24694 */
24695 if (vctxt->nbAttrInfos == 0)
24696 return (0);
24697 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24698 iattr = vctxt->attrInfos[i];
24699 if (! iattr->metaType) {
24700 ACTIVATE_ATTRIBUTE(iattr)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024701 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024702 XML_SCHEMAV_CVC_TYPE_3_1_1, iattr, NULL);
24703 ret = XML_SCHEMAV_CVC_TYPE_3_1_1;
24704 }
24705 }
24706 ACTIVATE_ELEM
24707 return (ret);
24708}
24709
24710/*
24711* Cleanup currently used attribute infos.
24712*/
24713static void
24714xmlSchemaClearAttrInfos(xmlSchemaValidCtxtPtr vctxt)
24715{
24716 int i;
24717 xmlSchemaAttrInfoPtr attr;
24718
24719 if (vctxt->nbAttrInfos == 0)
24720 return;
24721 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24722 attr = vctxt->attrInfos[i];
24723 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
24724 if (attr->localName != NULL)
24725 xmlFree((xmlChar *) attr->localName);
24726 if (attr->nsName != NULL)
24727 xmlFree((xmlChar *) attr->nsName);
24728 }
24729 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
24730 if (attr->value != NULL)
24731 xmlFree((xmlChar *) attr->value);
24732 }
24733 if (attr->val != NULL) {
24734 xmlSchemaFreeValue(attr->val);
24735 attr->val = NULL;
24736 }
24737 memset(attr, 0, sizeof(xmlSchemaAttrInfo));
24738 }
24739 vctxt->nbAttrInfos = 0;
24740}
24741
24742/*
24743* 3.4.4 Complex Type Definition Validation Rules
24744* Element Locally Valid (Complex Type) (cvc-complex-type)
24745* 3.2.4 Attribute Declaration Validation Rules
24746* Validation Rule: Attribute Locally Valid (cvc-attribute)
24747* Attribute Locally Valid (Use) (cvc-au)
24748*
24749* Only "assessed" attribute information items will be visible to
24750* IDCs. I.e. not "lax" (without declaration) and "skip" wild attributes.
24751*/
24752static int
24753xmlSchemaVAttributesComplex(xmlSchemaValidCtxtPtr vctxt)
24754{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024755 xmlSchemaTypePtr type = vctxt->inode->typeDef;
24756 xmlSchemaItemListPtr attrUseList;
24757 xmlSchemaAttributeUsePtr attrUse = NULL;
24758 xmlSchemaAttributePtr attrDecl = NULL;
24759 xmlSchemaAttrInfoPtr iattr, tmpiattr;
24760 int i, j, found, nbAttrs, nbUses;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024761 int xpathRes = 0, res, wildIDs = 0, fixed;
24762
24763 /*
24764 * SPEC (cvc-attribute)
24765 * (1) "The declaration must not be ·absent· (see Missing
24766 * Sub-components (§5.3) for how this can fail to be
24767 * the case)."
24768 * (2) "Its {type definition} must not be absent."
24769 *
24770 * NOTE (1) + (2): This is not handled here, since we currently do not
24771 * allow validation against schemas which have missing sub-components.
24772 *
24773 * SPEC (cvc-complex-type)
24774 * (3) "For each attribute information item in the element information
24775 * item's [attributes] excepting those whose [namespace name] is
24776 * identical to http://www.w3.org/2001/XMLSchema-instance and whose
24777 * [local name] is one of type, nil, schemaLocation or
24778 * noNamespaceSchemaLocation, the appropriate case among the following
24779 * must be true:
24780 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024781 */
24782 attrUseList = (xmlSchemaItemListPtr) type->attrUses;
24783 /*
24784 * @nbAttrs is the number of attributes present in the instance.
24785 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024786 nbAttrs = vctxt->nbAttrInfos;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024787 if (attrUseList != NULL)
24788 nbUses = attrUseList->nbItems;
24789 else
24790 nbUses = 0;
24791 for (i = 0; i < nbUses; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024792 found = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024793 attrUse = attrUseList->items[i];
24794 attrDecl = WXS_ATTRUSE_DECL(attrUse);
24795 for (j = 0; j < nbAttrs; j++) {
24796 iattr = vctxt->attrInfos[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024797 /*
24798 * SPEC (cvc-complex-type) (3)
24799 * Skip meta attributes.
24800 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024801 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024802 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024803 if (iattr->localName[0] != attrDecl->name[0])
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024804 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024805 if (!xmlStrEqual(iattr->localName, attrDecl->name))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024806 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024807 if (!xmlStrEqual(iattr->nsName, attrDecl->targetNamespace))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024808 continue;
24809 found = 1;
24810 /*
24811 * SPEC (cvc-complex-type)
24812 * (3.1) "If there is among the {attribute uses} an attribute
24813 * use with an {attribute declaration} whose {name} matches
24814 * the attribute information item's [local name] and whose
24815 * {target namespace} is identical to the attribute information
24816 * item's [namespace name] (where an ·absent· {target namespace}
24817 * is taken to be identical to a [namespace name] with no value),
24818 * then the attribute information must be ·valid· with respect
24819 * to that attribute use as per Attribute Locally Valid (Use)
24820 * (§3.5.4). In this case the {attribute declaration} of that
24821 * attribute use is the ·context-determined declaration· for the
24822 * attribute information item with respect to Schema-Validity
24823 * Assessment (Attribute) (§3.2.4) and
24824 * Assessment Outcome (Attribute) (§3.2.5).
24825 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024826 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
24827 iattr->use = attrUse;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024828 /*
24829 * Context-determined declaration.
24830 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024831 iattr->decl = attrDecl;
24832 iattr->typeDef = attrDecl->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024833 break;
24834 }
24835
24836 if (found)
24837 continue;
24838
24839 if (attrUse->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED) {
24840 /*
24841 * Handle non-existent, required attributes.
24842 *
24843 * SPEC (cvc-complex-type)
24844 * (4) "The {attribute declaration} of each attribute use in
24845 * the {attribute uses} whose {required} is true matches one
24846 * of the attribute information items in the element information
24847 * item's [attributes] as per clause 3.1 above."
24848 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024849 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24850 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024851 VERROR_INT(
24852 "xmlSchemaVAttributesComplex",
24853 "calling xmlSchemaGetFreshAttrInfo()");
24854 return (-1);
24855 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024856 tmpiattr->state = XML_SCHEMAS_ATTR_ERR_MISSING;
24857 tmpiattr->use = attrUse;
24858 tmpiattr->decl = attrDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024859 } else if ((attrUse->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
24860 ((attrUse->defValue != NULL) ||
24861 (attrDecl->defValue != NULL))) {
24862 /*
24863 * Handle non-existent, optional, default/fixed attributes.
24864 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024865 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24866 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024867 VERROR_INT(
24868 "xmlSchemaVAttributesComplex",
24869 "calling xmlSchemaGetFreshAttrInfo()");
24870 return (-1);
24871 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024872 tmpiattr->state = XML_SCHEMAS_ATTR_DEFAULT;
24873 tmpiattr->use = attrUse;
24874 tmpiattr->decl = attrDecl;
24875 tmpiattr->typeDef = attrDecl->subtypes;
24876 tmpiattr->localName = attrDecl->name;
24877 tmpiattr->nsName = attrDecl->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024878 }
24879 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024880
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024881 if (vctxt->nbAttrInfos == 0)
24882 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024883 nbUses = vctxt->nbAttrInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024884 /*
24885 * Validate against the wildcard.
24886 */
24887 if (type->attributeWildcard != NULL) {
24888 /*
24889 * SPEC (cvc-complex-type)
24890 * (3.2.1) "There must be an {attribute wildcard}."
24891 */
24892 for (i = 0; i < nbAttrs; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024893 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024894 /*
24895 * SPEC (cvc-complex-type) (3)
24896 * Skip meta attributes.
24897 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024898 if (iattr->state != XML_SCHEMAS_ATTR_UNKNOWN)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024899 continue;
24900 /*
24901 * SPEC (cvc-complex-type)
24902 * (3.2.2) "The attribute information item must be ·valid· with
24903 * respect to it as defined in Item Valid (Wildcard) (§3.10.4)."
24904 *
24905 * SPEC Item Valid (Wildcard) (cvc-wildcard)
24906 * "... its [namespace name] must be ·valid· with respect to
24907 * the wildcard constraint, as defined in Wildcard allows
24908 * Namespace Name (§3.10.4)."
24909 */
24910 if (xmlSchemaCheckCVCWildcardNamespace(type->attributeWildcard,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024911 iattr->nsName) == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024912 /*
24913 * Handle processContents.
24914 *
24915 * SPEC (cvc-wildcard):
24916 * processContents | context-determined declaration:
24917 * "strict" "mustFind"
24918 * "lax" "none"
24919 * "skip" "skip"
24920 */
24921 if (type->attributeWildcard->processContents ==
24922 XML_SCHEMAS_ANY_SKIP) {
24923 /*
24924 * context-determined declaration = "skip"
24925 *
24926 * SPEC PSVI Assessment Outcome (Attribute)
24927 * [validity] = "notKnown"
24928 * [validation attempted] = "none"
24929 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024930 iattr->state = XML_SCHEMAS_ATTR_WILD_SKIP;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024931 continue;
24932 }
24933 /*
24934 * Find an attribute declaration.
24935 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024936 iattr->decl = xmlSchemaGetAttributeDecl(vctxt->schema,
24937 iattr->localName, iattr->nsName);
24938 if (iattr->decl != NULL) {
24939 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024940 /*
24941 * SPEC (cvc-complex-type)
24942 * (5) "Let [Definition:] the wild IDs be the set of
24943 * all attribute information item to which clause 3.2
24944 * applied and whose ·validation· resulted in a
24945 * ·context-determined declaration· of mustFind or no
24946 * ·context-determined declaration· at all, and whose
24947 * [local name] and [namespace name] resolve (as
24948 * defined by QName resolution (Instance) (§3.15.4)) to
24949 * an attribute declaration whose {type definition} is
24950 * or is derived from ID. Then all of the following
24951 * must be true:"
24952 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024953 iattr->typeDef = WXS_ATTR_TYPEDEF(iattr->decl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024954 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024955 iattr->typeDef, XML_SCHEMAS_ID)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024956 /*
24957 * SPEC (5.1) "There must be no more than one
24958 * item in ·wild IDs·."
24959 */
24960 if (wildIDs != 0) {
24961 /* VAL TODO */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024962 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024963 TODO
24964 continue;
24965 }
24966 wildIDs++;
24967 /*
24968 * SPEC (cvc-complex-type)
24969 * (5.2) "If ·wild IDs· is non-empty, there must not
24970 * be any attribute uses among the {attribute uses}
24971 * whose {attribute declaration}'s {type definition}
24972 * is or is derived from ID."
24973 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024974 for (j = 0; j < attrUseList->nbItems; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024975 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024976 WXS_ATTRUSE_TYPEDEF(attrUseList->items[j]),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024977 XML_SCHEMAS_ID)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024978 /* URGENT VAL TODO: implement */
24979 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024980 TODO
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024981 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024982 }
24983 }
24984 }
24985 } else if (type->attributeWildcard->processContents ==
24986 XML_SCHEMAS_ANY_LAX) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024987 iattr->state = XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024988 /*
24989 * SPEC PSVI Assessment Outcome (Attribute)
24990 * [validity] = "notKnown"
24991 * [validation attempted] = "none"
24992 */
24993 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024994 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024995 }
24996 }
24997 }
24998 }
24999
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025000 if (vctxt->nbAttrInfos == 0)
25001 return (0);
25002
25003 /*
25004 * Validate values, create default attributes, evaluate IDCs.
25005 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025006 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025007 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025008 /*
25009 * VAL TODO: Note that we won't try to resolve IDCs to
25010 * "lax" and "skip" validated attributes. Check what to
25011 * do in this case.
25012 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025013 if ((iattr->state != XML_SCHEMAS_ATTR_ASSESSED) &&
25014 (iattr->state != XML_SCHEMAS_ATTR_DEFAULT))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025015 continue;
25016 /*
25017 * VAL TODO: What to do if the type definition is missing?
25018 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025019 if (iattr->typeDef == NULL) {
25020 iattr->state = XML_SCHEMAS_ATTR_ERR_NO_TYPE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025021 continue;
25022 }
25023
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025024 ACTIVATE_ATTRIBUTE(iattr);
Kasimier T. Buchcik828f6542005-06-09 11:23:39 +000025025 fixed = 0;
25026 xpathRes = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025027
25028 if (vctxt->xpathStates != NULL) {
25029 /*
25030 * Evaluate IDCs.
25031 */
25032 xpathRes = xmlSchemaXPathEvaluate(vctxt,
25033 XML_ATTRIBUTE_NODE);
25034 if (xpathRes == -1) {
25035 VERROR_INT("xmlSchemaVAttributesComplex",
25036 "calling xmlSchemaXPathEvaluate()");
25037 goto internal_error;
25038 }
25039 }
25040
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025041 if (iattr->state == XML_SCHEMAS_ATTR_DEFAULT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025042 /*
25043 * Default/fixed attributes.
25044 */
25045 if (xpathRes) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025046 if (iattr->use->defValue != NULL) {
25047 iattr->value = (xmlChar *) iattr->use->defValue;
25048 iattr->val = iattr->use->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025049 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025050 iattr->value = (xmlChar *) iattr->decl->defValue;
25051 iattr->val = iattr->decl->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025052 }
25053 /*
25054 * IDCs will consume the precomputed default value,
25055 * so we need to clone it.
25056 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025057 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025058 VERROR_INT("xmlSchemaVAttributesComplex",
25059 "default/fixed value on an attribute use was "
25060 "not precomputed");
25061 goto internal_error;
25062 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025063 iattr->val = xmlSchemaCopyValue(iattr->val);
25064 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025065 VERROR_INT("xmlSchemaVAttributesComplex",
25066 "calling xmlSchemaCopyValue()");
25067 goto internal_error;
25068 }
25069 }
25070 /*
25071 * PSVI: Add the default attribute to the current element.
25072 * VAL TODO: Should we use the *normalized* value? This currently
25073 * uses the *initial* value.
25074 */
25075 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025076 (iattr->node != NULL) && (iattr->node->doc != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025077 xmlChar *normValue;
25078 const xmlChar *value;
25079
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025080 value = iattr->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025081 /*
25082 * Normalize the value.
25083 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025084 normValue = xmlSchemaNormalizeValue(iattr->typeDef,
25085 iattr->value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025086 if (normValue != NULL)
25087 value = BAD_CAST normValue;
25088
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025089 if (iattr->nsName == NULL) {
25090 if (xmlNewProp(iattr->node->parent,
25091 iattr->localName, value) == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025092 VERROR_INT("xmlSchemaVAttributesComplex",
25093 "callling xmlNewProp()");
25094 if (normValue != NULL)
25095 xmlFree(normValue);
25096 goto internal_error;
25097 }
25098 } else {
25099 xmlNsPtr ns;
25100
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025101 ns = xmlSearchNsByHref(iattr->node->doc,
25102 iattr->node->parent, iattr->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025103 if (ns == NULL) {
25104 xmlChar prefix[12];
25105 int counter = 0;
25106
25107 /*
25108 * Create a namespace declaration on the validation
25109 * root node if no namespace declaration is in scope.
25110 */
25111 do {
25112 snprintf((char *) prefix, 12, "p%d", counter++);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025113 ns = xmlSearchNs(iattr->node->doc,
25114 iattr->node->parent, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025115 if (counter > 1000) {
25116 VERROR_INT(
25117 "xmlSchemaVAttributesComplex",
25118 "could not compute a ns prefix for a "
25119 "default/fixed attribute");
25120 if (normValue != NULL)
25121 xmlFree(normValue);
25122 goto internal_error;
25123 }
25124 } while (ns != NULL);
25125 ns = xmlNewNs(vctxt->validationRoot,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025126 iattr->nsName, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025127 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025128 /*
25129 * TODO:
25130 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0406.html
25131 * If we have QNames: do we need to ensure there's a
25132 * prefix defined for the QName?
25133 */
25134 xmlNewNsProp(iattr->node->parent, ns,
25135 iattr->localName, value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025136 }
25137 if (normValue != NULL)
25138 xmlFree(normValue);
25139 }
25140 /*
25141 * Go directly to IDC evaluation.
25142 */
25143 goto eval_idcs;
25144 }
25145 /*
25146 * Validate the value.
25147 */
25148 if (vctxt->value != NULL) {
25149 /*
25150 * Free last computed value; just for safety reasons.
25151 */
25152 xmlSchemaFreeValue(vctxt->value);
25153 vctxt->value = NULL;
25154 }
25155 /*
25156 * Note that the attribute *use* can be unavailable, if
25157 * the attribute was a wild attribute.
25158 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025159 if ((iattr->decl->flags & XML_SCHEMAS_ATTR_FIXED) ||
25160 ((iattr->use != NULL) &&
25161 (iattr->use->flags & XML_SCHEMAS_ATTR_FIXED)))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025162 fixed = 1;
25163 else
25164 fixed = 0;
25165 /*
25166 * SPEC (cvc-attribute)
25167 * (3) "The item's ·normalized value· must be locally ·valid·
25168 * with respect to that {type definition} as per
25169 * String Valid (§3.14.4)."
25170 *
25171 * VAL TODO: Do we already have the
25172 * "normalized attribute value" here?
25173 */
25174 if (xpathRes || fixed) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025175 iattr->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025176 /*
25177 * Request a computed value.
25178 */
25179 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025180 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025181 iattr->node, iattr->typeDef, iattr->value, &(iattr->val),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025182 1, 1, 0);
25183 } else {
25184 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025185 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025186 iattr->node, iattr->typeDef, iattr->value, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025187 1, 0, 0);
25188 }
25189
25190 if (res != 0) {
25191 if (res == -1) {
25192 VERROR_INT("xmlSchemaVAttributesComplex",
25193 "calling xmlSchemaStreamValidateSimpleTypeValue()");
25194 goto internal_error;
25195 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025196 iattr->state = XML_SCHEMAS_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025197 /*
25198 * SPEC PSVI Assessment Outcome (Attribute)
25199 * [validity] = "invalid"
25200 */
25201 goto eval_idcs;
25202 }
25203
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025204 if (fixed) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025205 /*
25206 * SPEC Attribute Locally Valid (Use) (cvc-au)
25207 * "For an attribute information item to be·valid·
25208 * with respect to an attribute use its *normalized*
25209 * value· must match the *canonical* lexical
25210 * representation of the attribute use's {value
25211 * constraint}value, if it is present and fixed."
25212 *
25213 * VAL TODO: The requirement for the *canonical* value
25214 * will be removed in XML Schema 1.1.
25215 */
25216 /*
25217 * SPEC Attribute Locally Valid (cvc-attribute)
25218 * (4) "The item's *actual* value· must match the *value* of
25219 * the {value constraint}, if it is present and fixed."
25220 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025221 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025222 /* VAL TODO: A value was not precomputed. */
25223 TODO
25224 goto eval_idcs;
25225 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025226 if ((iattr->use != NULL) &&
25227 (iattr->use->defValue != NULL)) {
25228 if (iattr->use->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025229 /* VAL TODO: A default value was not precomputed. */
25230 TODO
25231 goto eval_idcs;
25232 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025233 iattr->vcValue = iattr->use->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025234 /*
25235 if (xmlSchemaCompareValuesWhtsp(attr->val,
25236 (xmlSchemaWhitespaceValueType) ws,
25237 attr->use->defVal,
25238 (xmlSchemaWhitespaceValueType) ws) != 0) {
25239 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025240 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->use->defVal))
25241 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025242 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025243 if (iattr->decl->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025244 /* VAL TODO: A default value was not precomputed. */
25245 TODO
25246 goto eval_idcs;
25247 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025248 iattr->vcValue = iattr->decl->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025249 /*
25250 if (xmlSchemaCompareValuesWhtsp(attr->val,
25251 (xmlSchemaWhitespaceValueType) ws,
25252 attrDecl->defVal,
25253 (xmlSchemaWhitespaceValueType) ws) != 0) {
25254 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025255 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->decl->defVal))
25256 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025257 }
25258 /*
25259 * [validity] = "valid"
25260 */
25261 }
25262eval_idcs:
25263 /*
25264 * Evaluate IDCs.
25265 */
25266 if (xpathRes) {
25267 if (xmlSchemaXPathProcessHistory(vctxt,
25268 vctxt->depth +1) == -1) {
25269 VERROR_INT("xmlSchemaVAttributesComplex",
25270 "calling xmlSchemaXPathEvaluate()");
25271 goto internal_error;
25272 }
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000025273 } else if (vctxt->xpathStates != NULL)
25274 xmlSchemaXPathPop(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025275 }
25276
25277 /*
25278 * Report errors.
25279 */
25280 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025281 iattr = vctxt->attrInfos[i];
25282 if ((iattr->state == XML_SCHEMAS_ATTR_META) ||
25283 (iattr->state == XML_SCHEMAS_ATTR_ASSESSED) ||
25284 (iattr->state == XML_SCHEMAS_ATTR_WILD_SKIP) ||
25285 (iattr->state == XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025286 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025287 ACTIVATE_ATTRIBUTE(iattr);
25288 switch (iattr->state) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025289 case XML_SCHEMAS_ATTR_ERR_MISSING: {
25290 xmlChar *str = NULL;
25291 ACTIVATE_ELEM;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025292 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025293 XML_SCHEMAV_CVC_COMPLEX_TYPE_4, NULL, NULL,
25294 "The attribute '%s' is required but missing",
25295 xmlSchemaFormatQName(&str,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025296 iattr->decl->targetNamespace,
25297 iattr->decl->name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025298 NULL);
25299 FREE_AND_NULL(str)
25300 break;
25301 }
25302 case XML_SCHEMAS_ATTR_ERR_NO_TYPE:
25303 VERROR(XML_SCHEMAV_CVC_ATTRIBUTE_2, NULL,
25304 "The type definition is absent");
25305 break;
25306 case XML_SCHEMAS_ATTR_ERR_FIXED_VALUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025307 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025308 XML_SCHEMAV_CVC_AU, NULL, NULL,
25309 "The value '%s' does not match the fixed "
25310 "value constraint '%s'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025311 iattr->value, iattr->vcValue);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025312 break;
25313 case XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL:
25314 VERROR(XML_SCHEMAV_CVC_WILDCARD, NULL,
25315 "No matching global attribute declaration available, but "
25316 "demanded by the strict wildcard");
25317 break;
25318 case XML_SCHEMAS_ATTR_UNKNOWN:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025319 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025320 break;
25321 /*
25322 * MAYBE VAL TODO: One might report different error messages
25323 * for the following errors.
25324 */
25325 if (type->attributeWildcard == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025326 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025327 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_1, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025328 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025329 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025330 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_2, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025331 }
25332 break;
25333 default:
25334 break;
25335 }
25336 }
25337
25338 ACTIVATE_ELEM;
25339 return (0);
25340internal_error:
25341 ACTIVATE_ELEM;
25342 return (-1);
25343}
25344
25345static int
25346xmlSchemaValidateElemWildcard(xmlSchemaValidCtxtPtr vctxt,
25347 int *skip)
25348{
25349 xmlSchemaWildcardPtr wild = (xmlSchemaWildcardPtr) vctxt->inode->decl;
25350 /*
25351 * The namespace of the element was already identified to be
25352 * matching the wildcard.
25353 */
25354 if ((skip == NULL) || (wild == NULL) ||
25355 (wild->type != XML_SCHEMA_TYPE_ANY)) {
25356 VERROR_INT("xmlSchemaValidateElemWildcard",
25357 "bad arguments");
25358 return (-1);
25359 }
25360 *skip = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025361 if (wild->processContents == XML_SCHEMAS_ANY_SKIP) {
25362 /*
25363 * URGENT VAL TODO: Either we need to position the stream to the
25364 * next sibling, or walk the whole subtree.
25365 */
25366 *skip = 1;
25367 return (0);
25368 }
25369 {
25370 xmlSchemaElementPtr decl = NULL;
25371
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025372 decl = xmlSchemaGetElem(vctxt->schema,
25373 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025374 if (decl != NULL) {
25375 vctxt->inode->decl = decl;
25376 return (0);
25377 }
25378 }
25379 if (wild->processContents == XML_SCHEMAS_ANY_STRICT) {
25380 /* VAL TODO: Change to proper error code. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025381 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL, /* WXS_BASIC_CAST wild */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025382 "No matching global element declaration available, but "
25383 "demanded by the strict wildcard");
25384 return (vctxt->err);
25385 }
25386 if (vctxt->nbAttrInfos != 0) {
25387 xmlSchemaAttrInfoPtr iattr;
25388 /*
25389 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25390 * (1.2.1.2.1) - (1.2.1.2.3 )
25391 *
25392 * Use the xsi:type attribute for the type definition.
25393 */
25394 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25395 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
25396 if (iattr != NULL) {
25397 if (xmlSchemaProcessXSIType(vctxt, iattr,
25398 &(vctxt->inode->typeDef), NULL) == -1) {
25399 VERROR_INT("xmlSchemaValidateElemWildcard",
25400 "calling xmlSchemaProcessXSIType() to "
25401 "process the attribute 'xsi:nil'");
25402 return (-1);
25403 }
25404 /*
25405 * Don't return an error on purpose.
25406 */
25407 return (0);
25408 }
25409 }
25410 /*
25411 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25412 *
25413 * Fallback to "anyType".
25414 */
25415 vctxt->inode->typeDef =
25416 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
25417 return (0);
25418}
25419
25420/*
25421* xmlSchemaCheckCOSValidDefault:
25422*
25423* This will be called if: not nilled, no content and a default/fixed
25424* value is provided.
25425*/
25426
25427static int
25428xmlSchemaCheckCOSValidDefault(xmlSchemaValidCtxtPtr vctxt,
25429 const xmlChar *value,
25430 xmlSchemaValPtr *val)
25431{
25432 int ret = 0;
25433 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25434
25435 /*
25436 * cos-valid-default:
25437 * Schema Component Constraint: Element Default Valid (Immediate)
25438 * For a string to be a valid default with respect to a type
25439 * definition the appropriate case among the following must be true:
25440 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025441 if WXS_IS_COMPLEX(inode->typeDef) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025442 /*
25443 * Complex type.
25444 *
25445 * SPEC (2.1) "its {content type} must be a simple type definition
25446 * or mixed."
25447 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
25448 * type}'s particle must be ·emptiable· as defined by
25449 * Particle Emptiable (§3.9.6)."
25450 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025451 if ((! WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) &&
25452 ((! WXS_HAS_MIXED_CONTENT(inode->typeDef)) ||
25453 (! WXS_EMPTIABLE(inode->typeDef)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025454 ret = XML_SCHEMAP_COS_VALID_DEFAULT_2_1;
25455 /* NOTE that this covers (2.2.2) as well. */
25456 VERROR(ret, NULL,
25457 "For a string to be a valid default, the type definition "
25458 "must be a simple type or a complex type with simple content "
25459 "or mixed content and a particle emptiable");
25460 return(ret);
25461 }
25462 }
25463 /*
25464 * 1 If the type definition is a simple type definition, then the string
25465 * must be ·valid· with respect to that definition as defined by String
25466 * Valid (§3.14.4).
25467 *
25468 * AND
25469 *
25470 * 2.2.1 If the {content type} is a simple type definition, then the
25471 * string must be ·valid· with respect to that simple type definition
25472 * as defined by String Valid (§3.14.4).
25473 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025474 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025475
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025476 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025477 NULL, inode->typeDef, value, val, 1, 1, 0);
25478
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025479 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025480
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025481 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025482 NULL, inode->typeDef->contentTypeDef, value, val, 1, 1, 0);
25483 }
25484 if (ret < 0) {
25485 VERROR_INT("xmlSchemaCheckCOSValidDefault",
25486 "calling xmlSchemaVCheckCVCSimpleType()");
25487 }
25488 return (ret);
25489}
25490
25491static void
25492xmlSchemaVContentModelCallback(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
25493 const xmlChar * name ATTRIBUTE_UNUSED,
25494 xmlSchemaElementPtr item,
25495 xmlSchemaNodeInfoPtr inode)
25496{
25497 inode->decl = item;
25498#ifdef DEBUG_CONTENT
25499 {
25500 xmlChar *str = NULL;
25501
25502 if (item->type == XML_SCHEMA_TYPE_ELEMENT) {
25503 xmlGenericError(xmlGenericErrorContext,
25504 "AUTOMATON callback for '%s' [declaration]\n",
25505 xmlSchemaFormatQName(&str,
25506 inode->localName, inode->nsName));
25507 } else {
25508 xmlGenericError(xmlGenericErrorContext,
25509 "AUTOMATON callback for '%s' [wildcard]\n",
25510 xmlSchemaFormatQName(&str,
25511 inode->localName, inode->nsName));
25512
25513 }
25514 FREE_AND_NULL(str)
25515 }
25516#endif
25517}
25518
25519static int
25520xmlSchemaValidatorPushElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025521{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025522 vctxt->inode = xmlSchemaGetFreshElemInfo(vctxt);
25523 if (vctxt->inode == NULL) {
25524 VERROR_INT("xmlSchemaValidatorPushElem",
25525 "calling xmlSchemaGetFreshElemInfo()");
25526 return (-1);
25527 }
25528 vctxt->nbAttrInfos = 0;
25529 return (0);
25530}
25531
25532static int
25533xmlSchemaVCheckINodeDataType(xmlSchemaValidCtxtPtr vctxt,
25534 xmlSchemaNodeInfoPtr inode,
25535 xmlSchemaTypePtr type,
25536 const xmlChar *value)
25537{
25538 if (inode->flags & XML_SCHEMA_NODE_INFO_VALUE_NEEDED)
25539 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025540 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025541 type, value, &(inode->val), 1, 1, 0));
25542 else
25543 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025544 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025545 type, value, NULL, 1, 0, 0));
25546}
25547
25548
25549
25550/*
25551* Process END of element.
25552*/
25553static int
25554xmlSchemaValidatorPopElem(xmlSchemaValidCtxtPtr vctxt)
25555{
25556 int ret = 0;
25557 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25558
25559 if (vctxt->nbAttrInfos != 0)
25560 xmlSchemaClearAttrInfos(vctxt);
25561 if (inode->flags & XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED) {
25562 /*
25563 * This element was not expected;
25564 * we will not validate child elements of broken parents.
25565 * Skip validation of all content of the parent.
25566 */
25567 vctxt->skipDepth = vctxt->depth -1;
25568 goto end_elem;
25569 }
25570 if ((inode->typeDef == NULL) ||
25571 (inode->flags & XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE)) {
25572 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025573 * 1. the type definition might be missing if the element was
25574 * error prone
25575 * 2. it might be abstract.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025576 */
25577 goto end_elem;
25578 }
25579 /*
25580 * Check the content model.
25581 */
25582 if ((inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) ||
25583 (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)) {
25584
25585 /*
25586 * Workaround for "anyType".
25587 */
25588 if (inode->typeDef->builtInType == XML_SCHEMAS_ANYTYPE)
25589 goto character_content;
25590
25591 if ((inode->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) == 0) {
25592 xmlChar *values[10];
25593 int terminal, nbval = 10, nbneg;
25594
25595 if (inode->regexCtxt == NULL) {
25596 /*
25597 * Create the regex context.
25598 */
25599 inode->regexCtxt =
25600 xmlRegNewExecCtxt(inode->typeDef->contModel,
25601 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
25602 vctxt);
25603 if (inode->regexCtxt == NULL) {
25604 VERROR_INT("xmlSchemaValidatorPopElem",
25605 "failed to create a regex context");
25606 goto internal_error;
25607 }
25608#ifdef DEBUG_AUTOMATA
25609 xmlGenericError(xmlGenericErrorContext,
25610 "AUTOMATON create on '%s'\n", inode->localName);
25611#endif
25612 }
25613 /*
25614 * Get hold of the still expected content, since a further
25615 * call to xmlRegExecPushString() will loose this information.
25616 */
25617 xmlRegExecNextValues(inode->regexCtxt,
25618 &nbval, &nbneg, &values[0], &terminal);
25619 ret = xmlRegExecPushString(inode->regexCtxt, NULL, NULL);
25620 if (ret <= 0) {
25621 /*
25622 * Still missing something.
25623 */
25624 ret = 1;
25625 inode->flags |=
25626 XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025627 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025628 XML_SCHEMAV_ELEMENT_CONTENT, NULL, NULL,
25629 "Missing child element(s)",
25630 nbval, nbneg, values);
25631#ifdef DEBUG_AUTOMATA
25632 xmlGenericError(xmlGenericErrorContext,
25633 "AUTOMATON missing ERROR on '%s'\n",
25634 inode->localName);
25635#endif
25636 } else {
25637 /*
25638 * Content model is satisfied.
25639 */
25640 ret = 0;
25641#ifdef DEBUG_AUTOMATA
25642 xmlGenericError(xmlGenericErrorContext,
25643 "AUTOMATON succeeded on '%s'\n",
25644 inode->localName);
25645#endif
25646 }
25647
25648 }
25649 }
25650 if (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)
25651 goto end_elem;
25652
25653character_content:
25654
25655 if (vctxt->value != NULL) {
25656 xmlSchemaFreeValue(vctxt->value);
25657 vctxt->value = NULL;
25658 }
25659 /*
25660 * Check character content.
25661 */
25662 if (inode->decl == NULL) {
25663 /*
25664 * Speedup if no declaration exists.
25665 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025666 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025667 ret = xmlSchemaVCheckINodeDataType(vctxt,
25668 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025669 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025670 ret = xmlSchemaVCheckINodeDataType(vctxt,
25671 inode, inode->typeDef->contentTypeDef,
25672 inode->value);
25673 }
25674 if (ret < 0) {
25675 VERROR_INT("xmlSchemaValidatorPopElem",
25676 "calling xmlSchemaVCheckCVCSimpleType()");
25677 goto internal_error;
25678 }
25679 goto end_elem;
25680 }
25681 /*
25682 * cvc-elt (3.3.4) : 5
25683 * The appropriate case among the following must be true:
25684 */
25685 /*
25686 * cvc-elt (3.3.4) : 5.1
25687 * If the declaration has a {value constraint},
25688 * the item has neither element nor character [children] and
25689 * clause 3.2 has not applied, then all of the following must be true:
25690 */
25691 if ((inode->decl->value != NULL) &&
25692 (inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY) &&
25693 (! INODE_NILLED(inode))) {
25694 /*
25695 * cvc-elt (3.3.4) : 5.1.1
25696 * If the ·actual type definition· is a ·local type definition·
25697 * then the canonical lexical representation of the {value constraint}
25698 * value must be a valid default for the ·actual type definition· as
25699 * defined in Element Default Valid (Immediate) (§3.3.6).
25700 */
25701 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025702 * NOTE: 'local' above means types acquired by xsi:type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025703 * NOTE: Although the *canonical* value is stated, it is not
25704 * relevant if canonical or not. Additionally XML Schema 1.1
25705 * will removed this requirement as well.
25706 */
25707 if (inode->flags & XML_SCHEMA_ELEM_INFO_LOCAL_TYPE) {
25708
25709 ret = xmlSchemaCheckCOSValidDefault(vctxt,
25710 inode->decl->value, &(inode->val));
25711 if (ret != 0) {
25712 if (ret < 0) {
25713 VERROR_INT("xmlSchemaValidatorPopElem",
25714 "calling xmlSchemaCheckCOSValidDefault()");
25715 goto internal_error;
25716 }
25717 goto end_elem;
25718 }
25719 /*
25720 * Stop here, to avoid redundant validation of the value
25721 * (see following).
25722 */
25723 goto default_psvi;
25724 }
25725 /*
25726 * cvc-elt (3.3.4) : 5.1.2
25727 * The element information item with the canonical lexical
25728 * representation of the {value constraint} value used as its
25729 * ·normalized value· must be ·valid· with respect to the
25730 * ·actual type definition· as defined by Element Locally Valid (Type)
25731 * (§3.3.4).
25732 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025733 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025734 ret = xmlSchemaVCheckINodeDataType(vctxt,
25735 inode, inode->typeDef, inode->decl->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025736 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025737 ret = xmlSchemaVCheckINodeDataType(vctxt,
25738 inode, inode->typeDef->contentTypeDef,
25739 inode->decl->value);
25740 }
25741 if (ret != 0) {
25742 if (ret < 0) {
25743 VERROR_INT("xmlSchemaValidatorPopElem",
25744 "calling xmlSchemaVCheckCVCSimpleType()");
25745 goto internal_error;
25746 }
25747 goto end_elem;
25748 }
25749
25750default_psvi:
25751 /*
25752 * PSVI: Create a text node on the instance element.
25753 */
25754 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
25755 (inode->node != NULL)) {
25756 xmlNodePtr textChild;
25757 xmlChar *normValue;
25758 /*
25759 * VAL TODO: Normalize the value.
25760 */
25761 normValue = xmlSchemaNormalizeValue(inode->typeDef,
25762 inode->decl->value);
25763 if (normValue != NULL) {
25764 textChild = xmlNewText(BAD_CAST normValue);
25765 xmlFree(normValue);
25766 } else
25767 textChild = xmlNewText(inode->decl->value);
25768 if (textChild == NULL) {
25769 VERROR_INT("xmlSchemaValidatorPopElem",
25770 "calling xmlNewText()");
25771 goto internal_error;
25772 } else
25773 xmlAddChild(inode->node, textChild);
25774 }
25775
25776 } else if (! INODE_NILLED(inode)) {
25777 /*
25778 * 5.2.1 The element information item must be ·valid· with respect
25779 * to the ·actual type definition· as defined by Element Locally
25780 * Valid (Type) (§3.3.4).
25781 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025782 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025783 /*
25784 * SPEC (cvc-type) (3.1)
25785 * "If the type definition is a simple type definition, ..."
25786 * (3.1.3) "If clause 3.2 of Element Locally Valid
25787 * (Element) (§3.3.4) did not apply, then the ·normalized value·
25788 * must be ·valid· with respect to the type definition as defined
25789 * by String Valid (§3.14.4).
25790 */
25791 ret = xmlSchemaVCheckINodeDataType(vctxt,
25792 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025793 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025794 /*
25795 * SPEC (cvc-type) (3.2) "If the type definition is a complex type
25796 * definition, then the element information item must be
25797 * ·valid· with respect to the type definition as per
25798 * Element Locally Valid (Complex Type) (§3.4.4);"
25799 *
25800 * SPEC (cvc-complex-type) (2.2)
25801 * "If the {content type} is a simple type definition, ...
25802 * the ·normalized value· of the element information item is
25803 * ·valid· with respect to that simple type definition as
25804 * defined by String Valid (§3.14.4)."
25805 */
25806 ret = xmlSchemaVCheckINodeDataType(vctxt,
25807 inode, inode->typeDef->contentTypeDef, inode->value);
25808 }
25809 if (ret != 0) {
25810 if (ret < 0) {
25811 VERROR_INT("xmlSchemaValidatorPopElem",
25812 "calling xmlSchemaVCheckCVCSimpleType()");
25813 goto internal_error;
25814 }
25815 goto end_elem;
25816 }
25817 /*
25818 * 5.2.2 If there is a fixed {value constraint} and clause 3.2 has
25819 * not applied, all of the following must be true:
25820 */
25821 if ((inode->decl->value != NULL) &&
25822 (inode->decl->flags & XML_SCHEMAS_ELEM_FIXED)) {
25823
25824 /*
25825 * TODO: We will need a computed value, when comparison is
25826 * done on computed values.
25827 */
25828 /*
25829 * 5.2.2.1 The element information item must have no element
25830 * information item [children].
25831 */
25832 if (inode->flags &
25833 XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT) {
25834 ret = XML_SCHEMAV_CVC_ELT_5_2_2_1;
25835 VERROR(ret, NULL,
25836 "The content must not containt element nodes since "
25837 "there is a fixed value constraint");
25838 goto end_elem;
25839 } else {
25840 /*
25841 * 5.2.2.2 The appropriate case among the following must
25842 * be true:
25843 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025844 if (WXS_HAS_MIXED_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025845 /*
25846 * 5.2.2.2.1 If the {content type} of the ·actual type
25847 * definition· is mixed, then the *initial value* of the
25848 * item must match the canonical lexical representation
25849 * of the {value constraint} value.
25850 *
25851 * ... the *initial value* of an element information
25852 * item is the string composed of, in order, the
25853 * [character code] of each character information item in
25854 * the [children] of that element information item.
25855 */
25856 if (! xmlStrEqual(inode->value, inode->decl->value)){
25857 /*
25858 * VAL TODO: Report invalid & expected values as well.
25859 * VAL TODO: Implement the canonical stuff.
25860 */
25861 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025862 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025863 ret, NULL, NULL,
25864 "The initial value '%s' does not match the fixed "
25865 "value constraint '%s'",
25866 inode->value, inode->decl->value);
25867 goto end_elem;
25868 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025869 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025870 /*
25871 * 5.2.2.2.2 If the {content type} of the ·actual type
25872 * definition· is a simple type definition, then the
25873 * *actual value* of the item must match the canonical
25874 * lexical representation of the {value constraint} value.
25875 */
25876 /*
25877 * VAL TODO: *actual value* is the normalized value, impl.
25878 * this.
25879 * VAL TODO: Report invalid & expected values as well.
25880 * VAL TODO: Implement a comparison with the computed values.
25881 */
25882 if (! xmlStrEqual(inode->value,
25883 inode->decl->value)) {
25884 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_2;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025885 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025886 ret, NULL, NULL,
25887 "The actual value '%s' does not match the fixed "
25888 "value constraint '%s'",
25889 inode->value,
25890 inode->decl->value);
25891 goto end_elem;
25892 }
25893 }
25894 }
25895 }
25896 }
25897
25898end_elem:
25899 if (vctxt->depth < 0) {
25900 /* TODO: raise error? */
25901 return (0);
25902 }
25903 if (vctxt->depth == vctxt->skipDepth)
25904 vctxt->skipDepth = -1;
25905 /*
25906 * Evaluate the history of XPath state objects.
25907 */
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000025908 if (inode->appliedXPath &&
25909 (xmlSchemaXPathProcessHistory(vctxt, vctxt->depth) == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025910 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025911 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025912 * MAYBE TODO:
25913 * SPEC (6) "The element information item must be ·valid· with
25914 * respect to each of the {identity-constraint definitions} as per
25915 * Identity-constraint Satisfied (§3.11.4)."
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025916 */
25917 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025918 * PSVI TODO: If we expose IDC node-tables via PSVI then the tables
25919 * need to be built in any case.
25920 * We will currently build IDC node-tables and bubble them only if
25921 * keyrefs do exist.
25922 */
25923
25924 /*
25925 * Add the current IDC target-nodes to the IDC node-tables.
25926 */
25927 if ((inode->idcMatchers != NULL) &&
25928 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
25929 {
25930 if (xmlSchemaIDCFillNodeTables(vctxt, inode) == -1)
25931 goto internal_error;
25932 }
25933 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025934 * Validate IDC keyrefs.
25935 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025936 if (vctxt->inode->hasKeyrefs)
25937 if (xmlSchemaCheckCVCIDCKeyRef(vctxt) == -1)
25938 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025939 /*
25940 * Merge/free the IDC table.
25941 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025942 if (inode->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025943#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025944 xmlSchemaDebugDumpIDCTable(stdout,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025945 inode->nsName,
25946 inode->localName,
25947 inode->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025948#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025949 if ((vctxt->depth > 0) &&
25950 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
25951 {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025952 /*
25953 * Merge the IDC node table with the table of the parent node.
25954 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025955 if (xmlSchemaBubbleIDCNodeTables(vctxt) == -1)
25956 goto internal_error;
25957 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025958 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025959 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025960 * Clear the current ielem.
25961 * VAL TODO: Don't free the PSVI IDC tables if they are
25962 * requested for the PSVI.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025963 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025964 xmlSchemaClearElemInfo(inode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025965 /*
25966 * Skip further processing if we are on the validation root.
25967 */
25968 if (vctxt->depth == 0) {
25969 vctxt->depth--;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025970 vctxt->inode = NULL;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000025971 return (0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025972 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025973 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025974 * Reset the keyrefDepth if needed.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025975 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025976 if (vctxt->aidcs != NULL) {
25977 xmlSchemaIDCAugPtr aidc = vctxt->aidcs;
25978 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025979 if (aidc->keyrefDepth == vctxt->depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025980 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025981 * A 'keyrefDepth' of a key/unique IDC matches the current
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025982 * depth, this means that we are leaving the scope of the
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025983 * top-most keyref IDC which refers to this IDC.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025984 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025985 aidc->keyrefDepth = -1;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025986 }
25987 aidc = aidc->next;
25988 } while (aidc != NULL);
25989 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025990 vctxt->depth--;
25991 vctxt->inode = vctxt->elemInfos[vctxt->depth];
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000025992 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025993 * VAL TODO: 7 If the element information item is the ·validation root·, it must be
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000025994 * ·valid· per Validation Root Valid (ID/IDREF) (§3.3.4).
25995 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025996 return (ret);
25997
25998internal_error:
25999 vctxt->err = -1;
26000 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026001}
26002
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026003/*
26004* 3.4.4 Complex Type Definition Validation Rules
26005* Validation Rule: Element Locally Valid (Complex Type) (cvc-complex-type)
26006*/
Daniel Veillardc0826a72004-08-10 14:17:33 +000026007static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026008xmlSchemaValidateChildElem(xmlSchemaValidCtxtPtr vctxt)
William M. Brack2f2a6632004-08-20 23:09:47 +000026009{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026010 xmlSchemaNodeInfoPtr pielem;
26011 xmlSchemaTypePtr ptype;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026012 int ret = 0;
Daniel Veillard3646d642004-06-02 19:19:14 +000026013
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026014 if (vctxt->depth <= 0) {
26015 VERROR_INT("xmlSchemaValidateChildElem",
26016 "not intended for the validation root");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026017 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026018 }
26019 pielem = vctxt->elemInfos[vctxt->depth -1];
26020 if (pielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26021 pielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026022 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026023 * Handle 'nilled' elements.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026024 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026025 if (INODE_NILLED(pielem)) {
26026 /*
26027 * SPEC (cvc-elt) (3.3.4) : (3.2.1)
26028 */
26029 ACTIVATE_PARENT_ELEM;
26030 ret = XML_SCHEMAV_CVC_ELT_3_2_1;
26031 VERROR(ret, NULL,
26032 "Neither character nor element content is allowed, "
26033 "because the element was 'nilled'");
26034 ACTIVATE_ELEM;
26035 goto unexpected_elem;
26036 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026037
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026038 ptype = pielem->typeDef;
26039
26040 if (ptype->builtInType == XML_SCHEMAS_ANYTYPE) {
26041 /*
26042 * Workaround for "anyType": we have currently no content model
26043 * assigned for "anyType", so handle it explicitely.
26044 * "anyType" has an unbounded, lax "any" wildcard.
26045 */
26046 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26047 vctxt->inode->localName,
26048 vctxt->inode->nsName);
26049
26050 if (vctxt->inode->decl == NULL) {
26051 xmlSchemaAttrInfoPtr iattr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026052 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026053 * Process "xsi:type".
26054 * SPEC (cvc-assess-elt) (1.2.1.2.1) - (1.2.1.2.3)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026055 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026056 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
26057 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
26058 if (iattr != NULL) {
26059 ret = xmlSchemaProcessXSIType(vctxt, iattr,
26060 &(vctxt->inode->typeDef), NULL);
26061 if (ret != 0) {
26062 if (ret == -1) {
26063 VERROR_INT("xmlSchemaValidateChildElem",
26064 "calling xmlSchemaProcessXSIType() to "
26065 "process the attribute 'xsi:nil'");
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000026066 return (-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000026067 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026068 return (ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +000026069 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026070 } else {
26071 /*
26072 * Fallback to "anyType".
26073 *
26074 * SPEC (cvc-assess-elt)
26075 * "If the item cannot be ·strictly assessed·, [...]
26076 * an element information item's schema validity may be laxly
26077 * assessed if its ·context-determined declaration· is not
26078 * skip by ·validating· with respect to the ·ur-type
26079 * definition· as per Element Locally Valid (Type) (§3.3.4)."
26080 */
26081 vctxt->inode->typeDef =
26082 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026083 }
26084 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026085 return (0);
26086 }
26087
26088 switch (ptype->contentType) {
26089 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026090 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026091 * SPEC (2.1) "If the {content type} is empty, then the
26092 * element information item has no character or element
26093 * information item [children]."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026094 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026095 ACTIVATE_PARENT_ELEM
26096 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1;
26097 VERROR(ret, NULL,
26098 "Element content is not allowed, "
26099 "because the content type is empty");
26100 ACTIVATE_ELEM
26101 goto unexpected_elem;
26102 break;
26103
26104 case XML_SCHEMA_CONTENT_MIXED:
26105 case XML_SCHEMA_CONTENT_ELEMENTS: {
26106 xmlRegExecCtxtPtr regexCtxt;
26107 xmlChar *values[10];
26108 int terminal, nbval = 10, nbneg;
26109
26110 /* VAL TODO: Optimized "anyType" validation.*/
26111
26112 if (ptype->contModel == NULL) {
26113 VERROR_INT("xmlSchemaValidateChildElem",
26114 "type has elem content but no content model");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026115 return (-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000026116 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026117 /*
26118 * Safety belf for evaluation if the cont. model was already
26119 * examined to be invalid.
26120 */
26121 if (pielem->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) {
26122 VERROR_INT("xmlSchemaValidateChildElem",
26123 "validating elem, but elem content is already invalid");
26124 return (-1);
26125 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +000026126
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026127 regexCtxt = pielem->regexCtxt;
26128 if (regexCtxt == NULL) {
26129 /*
26130 * Create the regex context.
26131 */
26132 regexCtxt = xmlRegNewExecCtxt(ptype->contModel,
26133 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
26134 vctxt);
26135 if (regexCtxt == NULL) {
26136 VERROR_INT("xmlSchemaValidateChildElem",
26137 "failed to create a regex context");
26138 return (-1);
26139 }
26140 pielem->regexCtxt = regexCtxt;
26141#ifdef DEBUG_AUTOMATA
26142 xmlGenericError(xmlGenericErrorContext, "AUTOMATA create on '%s'\n",
26143 pielem->localName);
26144#endif
26145 }
26146
26147 /*
26148 * SPEC (2.4) "If the {content type} is element-only or mixed,
26149 * then the sequence of the element information item's
26150 * element information item [children], if any, taken in
26151 * order, is ·valid· with respect to the {content type}'s
26152 * particle, as defined in Element Sequence Locally Valid
26153 * (Particle) (§3.9.4)."
26154 */
26155 ret = xmlRegExecPushString2(regexCtxt,
26156 vctxt->inode->localName,
26157 vctxt->inode->nsName,
26158 vctxt->inode);
26159#ifdef DEBUG_AUTOMATA
26160 if (ret < 0)
26161 xmlGenericError(xmlGenericErrorContext,
26162 "AUTOMATON push ERROR for '%s' on '%s'\n",
26163 vctxt->inode->localName, pielem->localName);
26164 else
26165 xmlGenericError(xmlGenericErrorContext,
26166 "AUTOMATON push OK for '%s' on '%s'\n",
26167 vctxt->inode->localName, pielem->localName);
26168#endif
26169 if (vctxt->err == XML_SCHEMAV_INTERNAL) {
26170 VERROR_INT("xmlSchemaValidateChildElem",
26171 "calling xmlRegExecPushString2()");
26172 return (-1);
26173 }
26174 if (ret < 0) {
26175 xmlRegExecErrInfo(regexCtxt, NULL, &nbval, &nbneg,
26176 &values[0], &terminal);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026177 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026178 XML_SCHEMAV_ELEMENT_CONTENT, NULL,NULL,
26179 "This element is not expected",
26180 nbval, nbneg, values);
26181 ret = vctxt->err;
26182 goto unexpected_elem;
26183 } else
26184 ret = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026185 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026186 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026187 case XML_SCHEMA_CONTENT_SIMPLE:
26188 case XML_SCHEMA_CONTENT_BASIC:
26189 ACTIVATE_PARENT_ELEM
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026190 if (WXS_IS_COMPLEX(ptype)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026191 /*
26192 * SPEC (cvc-complex-type) (2.2)
26193 * "If the {content type} is a simple type definition, then
26194 * the element information item has no element information
26195 * item [children], ..."
26196 */
26197 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_2;
26198 VERROR(ret, NULL, "Element content is not allowed, "
26199 "because the content type is a simple type definition");
26200 } else {
26201 /*
26202 * SPEC (cvc-type) (3.1.2) "The element information item must
26203 * have no element information item [children]."
26204 */
26205 ret = XML_SCHEMAV_CVC_TYPE_3_1_2;
26206 VERROR(ret, NULL, "Element content is not allowed, "
26207 "because the type definition is simple");
26208 }
26209 ACTIVATE_ELEM
26210 ret = vctxt->err;
26211 goto unexpected_elem;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026212 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026213
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026214 default:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026215 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026216 }
26217 return (ret);
26218unexpected_elem:
26219 /*
26220 * Pop this element and set the skipDepth to skip
26221 * all further content of the parent element.
26222 */
26223 vctxt->skipDepth = vctxt->depth;
26224 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED;
26225 pielem->flags |= XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
26226 return (ret);
26227}
26228
26229#define XML_SCHEMA_PUSH_TEXT_PERSIST 1
26230#define XML_SCHEMA_PUSH_TEXT_CREATED 2
26231#define XML_SCHEMA_PUSH_TEXT_VOLATILE 3
26232
26233static int
26234xmlSchemaVPushText(xmlSchemaValidCtxtPtr vctxt,
26235 int nodeType, const xmlChar *value, int len,
26236 int mode, int *consumed)
26237{
26238 /*
26239 * Unfortunately we have to duplicate the text sometimes.
26240 * OPTIMIZE: Maybe we could skip it, if:
26241 * 1. content type is simple
26242 * 2. whitespace is "collapse"
26243 * 3. it consists of whitespace only
26244 *
26245 * Process character content.
26246 */
26247 if (consumed != NULL)
26248 *consumed = 0;
26249 if (INODE_NILLED(vctxt->inode)) {
26250 /*
26251 * SPEC cvc-elt (3.3.4 - 3.2.1)
26252 * "The element information item must have no character or
26253 * element information item [children]."
26254 */
26255 VERROR(XML_SCHEMAV_CVC_ELT_3_2_1, NULL,
26256 "Neither character nor element content is allowed "
26257 "because the element is 'nilled'");
26258 return (vctxt->err);
26259 }
26260 /*
26261 * SPEC (2.1) "If the {content type} is empty, then the
26262 * element information item has no character or element
26263 * information item [children]."
26264 */
26265 if (vctxt->inode->typeDef->contentType ==
26266 XML_SCHEMA_CONTENT_EMPTY) {
26267 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1, NULL,
26268 "Character content is not allowed, "
26269 "because the content type is empty");
26270 return (vctxt->err);
26271 }
26272
26273 if (vctxt->inode->typeDef->contentType ==
26274 XML_SCHEMA_CONTENT_ELEMENTS) {
26275 if ((nodeType != XML_TEXT_NODE) ||
26276 (! xmlSchemaIsBlank((xmlChar *) value, len))) {
26277 /*
26278 * SPEC cvc-complex-type (2.3)
26279 * "If the {content type} is element-only, then the
26280 * element information item has no character information
26281 * item [children] other than those whose [character
26282 * code] is defined as a white space in [XML 1.0 (Second
26283 * Edition)]."
26284 */
26285 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_3, NULL,
26286 "Character content other than whitespace is not allowed "
26287 "because the content type is 'element-only'");
26288 return (vctxt->err);
26289 }
26290 return (0);
26291 }
26292
26293 if ((value == NULL) || (value[0] == 0))
26294 return (0);
26295 /*
26296 * Save the value.
26297 * NOTE that even if the content type is *mixed*, we need the
26298 * *initial value* for default/fixed value constraints.
26299 */
26300 if ((vctxt->inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) &&
26301 ((vctxt->inode->decl == NULL) ||
26302 (vctxt->inode->decl->value == NULL)))
26303 return (0);
26304
26305 if (vctxt->inode->value == NULL) {
26306 /*
26307 * Set the value.
26308 */
26309 switch (mode) {
26310 case XML_SCHEMA_PUSH_TEXT_PERSIST:
26311 /*
26312 * When working on a tree.
26313 */
26314 vctxt->inode->value = value;
26315 break;
26316 case XML_SCHEMA_PUSH_TEXT_CREATED:
26317 /*
26318 * When working with the reader.
26319 * The value will be freed by the element info.
26320 */
26321 vctxt->inode->value = value;
26322 if (consumed != NULL)
26323 *consumed = 1;
26324 vctxt->inode->flags |=
26325 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26326 break;
26327 case XML_SCHEMA_PUSH_TEXT_VOLATILE:
26328 /*
26329 * When working with SAX.
26330 * The value will be freed by the element info.
26331 */
26332 if (len != -1)
26333 vctxt->inode->value = BAD_CAST xmlStrndup(value, len);
26334 else
26335 vctxt->inode->value = BAD_CAST xmlStrdup(value);
26336 vctxt->inode->flags |=
26337 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26338 break;
26339 default:
26340 break;
26341 }
26342 } else {
26343 /*
26344 * Concat the value.
26345 */
26346 if (vctxt->inode->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000026347 vctxt->inode->value = BAD_CAST xmlStrncat(
26348 (xmlChar *) vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026349 } else {
26350 vctxt->inode->value =
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026351 BAD_CAST xmlStrncatNew(vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026352 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26353 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026354 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026355
26356 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000026357}
26358
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026359static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026360xmlSchemaValidateElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000026361{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026362 int ret = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000026363
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026364 if ((vctxt->skipDepth != -1) &&
26365 (vctxt->depth >= vctxt->skipDepth)) {
26366 VERROR_INT("xmlSchemaValidateElem",
26367 "in skip-state");
26368 goto internal_error;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026369 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026370 if (vctxt->xsiAssemble) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026371 /*
26372 * URGENT TODO: Better to fully stop validation
26373 * if there was an error during dynamic schema construction.
26374 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026375 if (xmlSchemaAssembleByXSI(vctxt) == -1)
26376 goto internal_error;
26377 }
26378 if (vctxt->depth > 0) {
26379 /*
26380 * Validate this element against the content model
26381 * of the parent.
26382 */
26383 ret = xmlSchemaValidateChildElem(vctxt);
26384 if (ret != 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026385 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026386 VERROR_INT("xmlSchemaValidateElem",
26387 "calling xmlSchemaStreamValidateChildElement()");
26388 goto internal_error;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026389 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026390 goto exit;
26391 }
26392 if (vctxt->depth == vctxt->skipDepth)
26393 goto exit;
26394 if ((vctxt->inode->decl == NULL) &&
26395 (vctxt->inode->typeDef == NULL)) {
26396 VERROR_INT("xmlSchemaValidateElem",
26397 "the child element was valid but neither the "
26398 "declaration nor the type was set");
26399 goto internal_error;
26400 }
26401 } else {
26402 /*
26403 * Get the declaration of the validation root.
26404 */
26405 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26406 vctxt->inode->localName,
26407 vctxt->inode->nsName);
26408 if (vctxt->inode->decl == NULL) {
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026409 ret = XML_SCHEMAV_CVC_ELT_1;
26410 VERROR(ret, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026411 "No matching global declaration available "
26412 "for the validation root");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026413 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026414 }
26415 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026416
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026417 if (vctxt->inode->decl == NULL)
26418 goto type_validation;
26419
26420 if (vctxt->inode->decl->type == XML_SCHEMA_TYPE_ANY) {
26421 int skip;
26422 /*
26423 * Wildcards.
26424 */
26425 ret = xmlSchemaValidateElemWildcard(vctxt, &skip);
26426 if (ret != 0) {
26427 if (ret < 0) {
26428 VERROR_INT("xmlSchemaValidateElem",
26429 "calling xmlSchemaValidateElemWildcard()");
26430 goto internal_error;
26431 }
26432 goto exit;
26433 }
26434 if (skip) {
26435 vctxt->skipDepth = vctxt->depth;
26436 goto exit;
26437 }
26438 /*
26439 * The declaration might be set by the wildcard validation,
26440 * when the processContents is "lax" or "strict".
26441 */
26442 if (vctxt->inode->decl->type != XML_SCHEMA_TYPE_ELEMENT) {
26443 /*
26444 * Clear the "decl" field to not confuse further processing.
26445 */
26446 vctxt->inode->decl = NULL;
26447 goto type_validation;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026448 }
Daniel Veillard4255d502002-04-16 15:50:10 +000026449 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026450 /*
26451 * Validate against the declaration.
26452 */
26453 ret = xmlSchemaValidateElemDecl(vctxt);
26454 if (ret != 0) {
26455 if (ret < 0) {
26456 VERROR_INT("xmlSchemaValidateElem",
26457 "calling xmlSchemaValidateElemDecl()");
26458 goto internal_error;
26459 }
26460 goto exit;
26461 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026462 /*
26463 * Validate against the type definition.
26464 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026465type_validation:
26466
26467 if (vctxt->inode->typeDef == NULL) {
26468 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26469 ret = XML_SCHEMAV_CVC_TYPE_1;
26470 VERROR(ret, NULL,
26471 "The type definition is absent");
26472 goto exit;
26473 }
26474 if (vctxt->inode->typeDef->flags & XML_SCHEMAS_TYPE_ABSTRACT) {
26475 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26476 ret = XML_SCHEMAV_CVC_TYPE_2;
26477 VERROR(ret, NULL,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026478 "The type definition is abstract");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026479 goto exit;
26480 }
26481 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026482 * Evaluate IDCs. Do it here, since new IDC matchers are registered
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026483 * during validation against the declaration. This must be done
26484 * _before_ attribute validation.
26485 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026486 if (vctxt->xpathStates != NULL) {
26487 ret = xmlSchemaXPathEvaluate(vctxt, XML_ELEMENT_NODE);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000026488 vctxt->inode->appliedXPath = 1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026489 if (ret == -1) {
26490 VERROR_INT("xmlSchemaValidateElem",
26491 "calling xmlSchemaXPathEvaluate()");
26492 goto internal_error;
26493 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026494 }
26495 /*
26496 * Validate attributes.
26497 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026498 if (WXS_IS_COMPLEX(vctxt->inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026499 if ((vctxt->nbAttrInfos != 0) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026500 (vctxt->inode->typeDef->attrUses != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026501
26502 ret = xmlSchemaVAttributesComplex(vctxt);
26503 }
26504 } else if (vctxt->nbAttrInfos != 0) {
26505
26506 ret = xmlSchemaVAttributesSimple(vctxt);
26507 }
26508 /*
26509 * Clear registered attributes.
26510 */
26511 if (vctxt->nbAttrInfos != 0)
26512 xmlSchemaClearAttrInfos(vctxt);
26513 if (ret == -1) {
26514 VERROR_INT("xmlSchemaValidateElem",
26515 "calling attributes validation");
26516 goto internal_error;
26517 }
26518 /*
26519 * Don't return an error if attributes are invalid on purpose.
26520 */
26521 ret = 0;
26522
26523exit:
26524 if (ret != 0)
26525 vctxt->skipDepth = vctxt->depth;
26526 return (ret);
26527internal_error:
26528 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026529}
26530
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026531#ifdef XML_SCHEMA_READER_ENABLED
26532static int
26533xmlSchemaVReaderWalk(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026534{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026535 const int WHTSP = 13, SIGN_WHTSP = 14, END_ELEM = 15;
26536 int depth, nodeType, ret = 0, consumed;
26537 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026538
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026539 vctxt->depth = -1;
26540 ret = xmlTextReaderRead(vctxt->reader);
26541 /*
26542 * Move to the document element.
26543 */
26544 while (ret == 1) {
26545 nodeType = xmlTextReaderNodeType(vctxt->reader);
26546 if (nodeType == XML_ELEMENT_NODE)
26547 goto root_found;
26548 ret = xmlTextReaderRead(vctxt->reader);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026549 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026550 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026551
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026552root_found:
26553
26554 do {
26555 depth = xmlTextReaderDepth(vctxt->reader);
26556 nodeType = xmlTextReaderNodeType(vctxt->reader);
26557
26558 if (nodeType == XML_ELEMENT_NODE) {
26559
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026560 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026561 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26562 VERROR_INT("xmlSchemaVReaderWalk",
26563 "calling xmlSchemaValidatorPushElem()");
26564 goto internal_error;
26565 }
26566 ielem = vctxt->inode;
26567 ielem->localName = xmlTextReaderLocalName(vctxt->reader);
26568 ielem->nsName = xmlTextReaderNamespaceUri(vctxt->reader);
26569 ielem->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
26570 /*
26571 * Is the element empty?
26572 */
26573 ret = xmlTextReaderIsEmptyElement(vctxt->reader);
26574 if (ret == -1) {
26575 VERROR_INT("xmlSchemaVReaderWalk",
26576 "calling xmlTextReaderIsEmptyElement()");
26577 goto internal_error;
26578 }
26579 if (ret) {
26580 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26581 }
26582 /*
26583 * Register attributes.
26584 */
26585 vctxt->nbAttrInfos = 0;
26586 ret = xmlTextReaderMoveToFirstAttribute(vctxt->reader);
26587 if (ret == -1) {
26588 VERROR_INT("xmlSchemaVReaderWalk",
26589 "calling xmlTextReaderMoveToFirstAttribute()");
26590 goto internal_error;
26591 }
26592 if (ret == 1) {
26593 do {
26594 /*
26595 * VAL TODO: How do we know that the reader works on a
26596 * node tree, to be able to pass a node here?
26597 */
26598 if (xmlSchemaValidatorPushAttribute(vctxt, NULL,
26599 (const xmlChar *) xmlTextReaderLocalName(vctxt->reader),
26600 xmlTextReaderNamespaceUri(vctxt->reader), 1,
26601 xmlTextReaderValue(vctxt->reader), 1) == -1) {
26602
26603 VERROR_INT("xmlSchemaVReaderWalk",
26604 "calling xmlSchemaValidatorPushAttribute()");
26605 goto internal_error;
26606 }
26607 ret = xmlTextReaderMoveToNextAttribute(vctxt->reader);
26608 if (ret == -1) {
26609 VERROR_INT("xmlSchemaVReaderWalk",
26610 "calling xmlTextReaderMoveToFirstAttribute()");
26611 goto internal_error;
26612 }
26613 } while (ret == 1);
26614 /*
26615 * Back to element position.
26616 */
26617 ret = xmlTextReaderMoveToElement(vctxt->reader);
26618 if (ret == -1) {
26619 VERROR_INT("xmlSchemaVReaderWalk",
26620 "calling xmlTextReaderMoveToElement()");
26621 goto internal_error;
26622 }
26623 }
26624 /*
26625 * Validate the element.
26626 */
26627 ret= xmlSchemaValidateElem(vctxt);
26628 if (ret != 0) {
26629 if (ret == -1) {
26630 VERROR_INT("xmlSchemaVReaderWalk",
26631 "calling xmlSchemaValidateElem()");
26632 goto internal_error;
26633 }
26634 goto exit;
26635 }
26636 if (vctxt->depth == vctxt->skipDepth) {
26637 int curDepth;
26638 /*
26639 * Skip all content.
26640 */
26641 if ((ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY) == 0) {
26642 ret = xmlTextReaderRead(vctxt->reader);
26643 curDepth = xmlTextReaderDepth(vctxt->reader);
26644 while ((ret == 1) && (curDepth != depth)) {
26645 ret = xmlTextReaderRead(vctxt->reader);
26646 curDepth = xmlTextReaderDepth(vctxt->reader);
26647 }
26648 if (ret < 0) {
26649 /*
26650 * VAL TODO: A reader error occured; what to do here?
26651 */
26652 ret = 1;
26653 goto exit;
26654 }
26655 }
26656 goto leave_elem;
26657 }
26658 /*
26659 * READER VAL TODO: Is an END_ELEM really never called
26660 * if the elem is empty?
26661 */
26662 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26663 goto leave_elem;
26664 } else if (nodeType == END_ELEM) {
26665 /*
26666 * Process END of element.
26667 */
26668leave_elem:
26669 ret = xmlSchemaValidatorPopElem(vctxt);
26670 if (ret != 0) {
26671 if (ret < 0) {
26672 VERROR_INT("xmlSchemaVReaderWalk",
26673 "calling xmlSchemaValidatorPopElem()");
26674 goto internal_error;
26675 }
26676 goto exit;
26677 }
26678 if (vctxt->depth >= 0)
26679 ielem = vctxt->inode;
26680 else
26681 ielem = NULL;
26682 } else if ((nodeType == XML_TEXT_NODE) ||
26683 (nodeType == XML_CDATA_SECTION_NODE) ||
26684 (nodeType == WHTSP) ||
26685 (nodeType == SIGN_WHTSP)) {
26686 /*
26687 * Process character content.
26688 */
26689 xmlChar *value;
26690
26691 if ((nodeType == WHTSP) || (nodeType == SIGN_WHTSP))
26692 nodeType = XML_TEXT_NODE;
26693
26694 value = xmlTextReaderValue(vctxt->reader);
26695 ret = xmlSchemaVPushText(vctxt, nodeType, BAD_CAST value,
26696 -1, XML_SCHEMA_PUSH_TEXT_CREATED, &consumed);
26697 if (! consumed)
26698 xmlFree(value);
26699 if (ret == -1) {
26700 VERROR_INT("xmlSchemaVReaderWalk",
26701 "calling xmlSchemaVPushText()");
26702 goto internal_error;
26703 }
26704 } else if ((nodeType == XML_ENTITY_NODE) ||
26705 (nodeType == XML_ENTITY_REF_NODE)) {
26706 /*
26707 * VAL TODO: What to do with entities?
26708 */
26709 TODO
26710 }
26711 /*
26712 * Read next node.
26713 */
26714 ret = xmlTextReaderRead(vctxt->reader);
26715 } while (ret == 1);
26716
26717exit:
26718 return (ret);
26719internal_error:
26720 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026721}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026722#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000026723
26724/************************************************************************
26725 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026726 * SAX validation handlers *
Daniel Veillard4255d502002-04-16 15:50:10 +000026727 * *
26728 ************************************************************************/
26729
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026730/*
26731* Process text content.
26732*/
26733static void
26734xmlSchemaSAXHandleText(void *ctx,
26735 const xmlChar * ch,
26736 int len)
26737{
26738 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26739
26740 if (vctxt->depth < 0)
26741 return;
26742 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26743 return;
26744 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26745 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26746 if (xmlSchemaVPushText(vctxt, XML_TEXT_NODE, ch, len,
26747 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26748 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26749 "calling xmlSchemaVPushText()");
26750 vctxt->err = -1;
26751 xmlStopParser(vctxt->parserCtxt);
26752 }
26753}
26754
26755/*
26756* Process CDATA content.
26757*/
26758static void
26759xmlSchemaSAXHandleCDataSection(void *ctx,
26760 const xmlChar * ch,
26761 int len)
26762{
26763 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26764
26765 if (vctxt->depth < 0)
26766 return;
26767 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26768 return;
26769 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26770 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26771 if (xmlSchemaVPushText(vctxt, XML_CDATA_SECTION_NODE, ch, len,
26772 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26773 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26774 "calling xmlSchemaVPushText()");
26775 vctxt->err = -1;
26776 xmlStopParser(vctxt->parserCtxt);
26777 }
26778}
26779
26780static void
26781xmlSchemaSAXHandleReference(void *ctx ATTRIBUTE_UNUSED,
26782 const xmlChar * name ATTRIBUTE_UNUSED)
26783{
26784 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26785
26786 if (vctxt->depth < 0)
26787 return;
26788 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26789 return;
26790 /* SAX VAL TODO: What to do here? */
26791 TODO
26792}
26793
26794static void
26795xmlSchemaSAXHandleStartElementNs(void *ctx,
26796 const xmlChar * localname,
26797 const xmlChar * prefix ATTRIBUTE_UNUSED,
26798 const xmlChar * URI,
26799 int nb_namespaces,
26800 const xmlChar ** namespaces,
26801 int nb_attributes,
26802 int nb_defaulted ATTRIBUTE_UNUSED,
26803 const xmlChar ** attributes)
26804{
26805 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26806 int ret;
26807 xmlSchemaNodeInfoPtr ielem;
26808 int i, j;
26809
26810 /*
26811 * SAX VAL TODO: What to do with nb_defaulted?
26812 */
26813 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026814 * Skip elements if inside a "skip" wildcard or invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026815 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026816 vctxt->depth++;
26817 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026818 return;
26819 /*
26820 * Push the element.
26821 */
26822 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26823 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26824 "calling xmlSchemaValidatorPushElem()");
26825 goto internal_error;
26826 }
26827 ielem = vctxt->inode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026828 /*
26829 * TODO: Is this OK?
26830 */
26831 ielem->nodeLine = xmlSAX2GetLineNumber(vctxt->parserCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026832 ielem->localName = localname;
26833 ielem->nsName = URI;
26834 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26835 /*
26836 * Register namespaces on the elem info.
26837 */
26838 if (nb_namespaces != 0) {
26839 /*
26840 * Although the parser builds its own namespace list,
26841 * we have no access to it, so we'll use an own one.
26842 */
26843 for (i = 0, j = 0; i < nb_namespaces; i++, j += 2) {
26844 /*
26845 * Store prefix and namespace name.
26846 */
26847 if (ielem->nsBindings == NULL) {
26848 ielem->nsBindings =
26849 (const xmlChar **) xmlMalloc(10 *
26850 sizeof(const xmlChar *));
26851 if (ielem->nsBindings == NULL) {
26852 xmlSchemaVErrMemory(vctxt,
26853 "allocating namespace bindings for SAX validation",
26854 NULL);
26855 goto internal_error;
26856 }
26857 ielem->nbNsBindings = 0;
26858 ielem->sizeNsBindings = 5;
26859 } else if (ielem->sizeNsBindings <= ielem->nbNsBindings) {
26860 ielem->sizeNsBindings *= 2;
26861 ielem->nsBindings =
26862 (const xmlChar **) xmlRealloc(
26863 (void *) ielem->nsBindings,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026864 ielem->sizeNsBindings * 2 * sizeof(const xmlChar *));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026865 if (ielem->nsBindings == NULL) {
26866 xmlSchemaVErrMemory(vctxt,
26867 "re-allocating namespace bindings for SAX validation",
26868 NULL);
26869 goto internal_error;
26870 }
26871 }
26872
26873 ielem->nsBindings[ielem->nbNsBindings * 2] = namespaces[j];
26874 if (namespaces[j+1][0] == 0) {
26875 /*
26876 * Handle xmlns="".
26877 */
26878 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] = NULL;
26879 } else
26880 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] =
26881 namespaces[j+1];
26882 ielem->nbNsBindings++;
26883 }
26884 }
26885 /*
26886 * Register attributes.
26887 * SAX VAL TODO: We are not adding namespace declaration
26888 * attributes yet.
26889 */
26890 if (nb_attributes != 0) {
26891 xmlChar *value;
26892
26893 for (j = 0, i = 0; i < nb_attributes; i++, j += 5) {
26894 /*
26895 * Duplicate the value.
26896 */
26897 value = xmlStrndup(attributes[j+3],
26898 attributes[j+4] - attributes[j+3]);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026899 /*
26900 * TODO: Set the node line.
26901 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026902 ret = xmlSchemaValidatorPushAttribute(vctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026903 NULL, ielem->nodeLine, attributes[j], attributes[j+2], 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026904 value, 1);
26905 if (ret == -1) {
26906 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26907 "calling xmlSchemaValidatorPushAttribute()");
26908 goto internal_error;
26909 }
26910 }
26911 }
26912 /*
26913 * Validate the element.
26914 */
26915 ret = xmlSchemaValidateElem(vctxt);
26916 if (ret != 0) {
26917 if (ret == -1) {
26918 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26919 "calling xmlSchemaValidateElem()");
26920 goto internal_error;
26921 }
26922 goto exit;
26923 }
26924
26925exit:
26926 return;
26927internal_error:
26928 vctxt->err = -1;
26929 xmlStopParser(vctxt->parserCtxt);
26930 return;
26931}
26932
26933static void
26934xmlSchemaSAXHandleEndElementNs(void *ctx,
26935 const xmlChar * localname ATTRIBUTE_UNUSED,
26936 const xmlChar * prefix ATTRIBUTE_UNUSED,
26937 const xmlChar * URI ATTRIBUTE_UNUSED)
26938{
26939 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26940 int res;
26941
26942 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026943 * Skip elements if inside a "skip" wildcard or if invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026944 */
26945 if (vctxt->skipDepth != -1) {
26946 if (vctxt->depth > vctxt->skipDepth) {
26947 vctxt->depth--;
26948 return;
26949 } else
26950 vctxt->skipDepth = -1;
26951 }
26952 /*
26953 * SAX VAL TODO: Just a temporary check.
26954 */
26955 if ((!xmlStrEqual(vctxt->inode->localName, localname)) ||
26956 (!xmlStrEqual(vctxt->inode->nsName, URI))) {
26957 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
26958 "elem pop mismatch");
26959 }
26960 res = xmlSchemaValidatorPopElem(vctxt);
26961 if (res != 0) {
26962 if (res < 0) {
26963 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
26964 "calling xmlSchemaValidatorPopElem()");
26965 goto internal_error;
26966 }
26967 goto exit;
26968 }
26969exit:
26970 return;
26971internal_error:
26972 vctxt->err = -1;
26973 xmlStopParser(vctxt->parserCtxt);
26974 return;
26975}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026976
Daniel Veillard4255d502002-04-16 15:50:10 +000026977/************************************************************************
26978 * *
26979 * Validation interfaces *
26980 * *
26981 ************************************************************************/
26982
26983/**
26984 * xmlSchemaNewValidCtxt:
26985 * @schema: a precompiled XML Schemas
26986 *
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026987 * Create an XML Schemas validation context based on the given schema.
Daniel Veillard4255d502002-04-16 15:50:10 +000026988 *
26989 * Returns the validation context or NULL in case of error
26990 */
26991xmlSchemaValidCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000026992xmlSchemaNewValidCtxt(xmlSchemaPtr schema)
26993{
Daniel Veillard4255d502002-04-16 15:50:10 +000026994 xmlSchemaValidCtxtPtr ret;
26995
26996 ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt));
26997 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000026998 xmlSchemaVErrMemory(NULL, "allocating validation context", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000026999 return (NULL);
27000 }
27001 memset(ret, 0, sizeof(xmlSchemaValidCtxt));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027002 ret->type = XML_SCHEMA_CTXT_VALIDATOR;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027003 ret->dict = xmlDictCreate();
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027004 ret->nodeQNames = xmlSchemaItemListCreate();
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027005 ret->schema = schema;
Daniel Veillard4255d502002-04-16 15:50:10 +000027006 return (ret);
27007}
27008
27009/**
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027010 * xmlSchemaClearValidCtxt:
27011 * @ctxt: the schema validation context
27012 *
27013 * Free the resources associated to the schema validation context;
27014 * leaves some fields alive intended for reuse of the context.
27015 */
27016static void
27017xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt)
27018{
27019 if (vctxt == NULL)
27020 return;
27021
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027022 /*
27023 * TODO: Should we clear the flags?
27024 * Might be problematic if one reuses the context
27025 * and assumes that the options remain the same.
27026 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000027027 vctxt->flags = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027028 vctxt->validationRoot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027029 vctxt->doc = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000027030#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027031 vctxt->reader = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000027032#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027033 vctxt->hasKeyrefs = 0;
27034
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027035 if (vctxt->value != NULL) {
27036 xmlSchemaFreeValue(vctxt->value);
27037 vctxt->value = NULL;
27038 }
27039 /*
27040 * Augmented IDC information.
27041 */
27042 if (vctxt->aidcs != NULL) {
27043 xmlSchemaIDCAugPtr cur = vctxt->aidcs, next;
27044 do {
27045 next = cur->next;
27046 xmlFree(cur);
27047 cur = next;
27048 } while (cur != NULL);
27049 vctxt->aidcs = NULL;
27050 }
27051 if (vctxt->idcNodes != NULL) {
27052 int i;
27053 xmlSchemaPSVIIDCNodePtr item;
27054
27055 for (i = 0; i < vctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027056 item = vctxt->idcNodes[i];
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027057 xmlFree(item->keys);
27058 xmlFree(item);
27059 }
27060 xmlFree(vctxt->idcNodes);
27061 vctxt->idcNodes = NULL;
27062 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027063 /*
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027064 * Note that we won't delete the XPath state pool here.
27065 */
27066 if (vctxt->xpathStates != NULL) {
27067 xmlSchemaFreeIDCStateObjList(vctxt->xpathStates);
27068 vctxt->xpathStates = NULL;
27069 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027070 /*
27071 * Attribute info.
27072 */
27073 if (vctxt->nbAttrInfos != 0) {
27074 xmlSchemaClearAttrInfos(vctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027075 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027076 /*
27077 * Element info.
27078 */
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027079 if (vctxt->elemInfos != NULL) {
27080 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027081 xmlSchemaNodeInfoPtr ei;
27082
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027083 for (i = 0; i < vctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027084 ei = vctxt->elemInfos[i];
27085 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027086 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027087 xmlSchemaClearElemInfo(ei);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027088 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027089 }
27090 xmlSchemaItemListClear(vctxt->nodeQNames);
27091 /* Recreate the dict. */
27092 xmlDictFree(vctxt->dict);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027093 /*
27094 * TODO: Is is save to recreate it? Do we have a scenario
27095 * where the user provides the dict?
27096 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027097 vctxt->dict = xmlDictCreate();
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027098}
27099
27100/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027101 * xmlSchemaFreeValidCtxt:
27102 * @ctxt: the schema validation context
27103 *
27104 * Free the resources associated to the schema validation context
27105 */
27106void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027107xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt)
27108{
Daniel Veillard4255d502002-04-16 15:50:10 +000027109 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027110 return;
Daniel Veillard88c58912002-04-23 07:12:20 +000027111 if (ctxt->value != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027112 xmlSchemaFreeValue(ctxt->value);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027113 if (ctxt->pctxt != NULL)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027114 xmlSchemaFreeParserCtxt(ctxt->pctxt);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027115 if (ctxt->idcNodes != NULL) {
27116 int i;
27117 xmlSchemaPSVIIDCNodePtr item;
27118
27119 for (i = 0; i < ctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027120 item = ctxt->idcNodes[i];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027121 xmlFree(item->keys);
27122 xmlFree(item);
27123 }
27124 xmlFree(ctxt->idcNodes);
27125 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027126 if (ctxt->idcKeys != NULL) {
27127 int i;
27128 for (i = 0; i < ctxt->nbIdcKeys; i++)
27129 xmlSchemaIDCFreeKey(ctxt->idcKeys[i]);
27130 xmlFree(ctxt->idcKeys);
27131 }
27132
27133 if (ctxt->xpathStates != NULL)
27134 xmlSchemaFreeIDCStateObjList(ctxt->xpathStates);
27135 if (ctxt->xpathStatePool != NULL)
27136 xmlSchemaFreeIDCStateObjList(ctxt->xpathStatePool);
27137
27138 /*
27139 * Augmented IDC information.
27140 */
27141 if (ctxt->aidcs != NULL) {
27142 xmlSchemaIDCAugPtr cur = ctxt->aidcs, next;
27143 do {
27144 next = cur->next;
27145 xmlFree(cur);
27146 cur = next;
27147 } while (cur != NULL);
27148 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027149 if (ctxt->attrInfos != NULL) {
27150 int i;
27151 xmlSchemaAttrInfoPtr attr;
27152
27153 /* Just a paranoid call to the cleanup. */
27154 if (ctxt->nbAttrInfos != 0)
27155 xmlSchemaClearAttrInfos(ctxt);
27156 for (i = 0; i < ctxt->sizeAttrInfos; i++) {
27157 attr = ctxt->attrInfos[i];
27158 xmlFree(attr);
27159 }
27160 xmlFree(ctxt->attrInfos);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000027161 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027162 if (ctxt->elemInfos != NULL) {
27163 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027164 xmlSchemaNodeInfoPtr ei;
27165
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027166 for (i = 0; i < ctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027167 ei = ctxt->elemInfos[i];
27168 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027169 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027170 xmlSchemaClearElemInfo(ei);
27171 xmlFree(ei);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027172 }
27173 xmlFree(ctxt->elemInfos);
27174 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027175 if (ctxt->nodeQNames != NULL)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027176 xmlSchemaItemListFree(ctxt->nodeQNames);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027177 if (ctxt->dict != NULL)
27178 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000027179 xmlFree(ctxt);
27180}
27181
27182/**
Daniel Veillardf10ae122005-07-10 19:03:16 +000027183 * xmlSchemaIsValid:
27184 * @ctxt: the schema validation context
27185 *
27186 * Check if any error was detected during validation.
27187 *
27188 * Returns 1 if valid so far, 0 if errors were detected, and -1 in case
27189 * of internal error.
27190 */
27191int
27192xmlSchemaIsValid(xmlSchemaValidCtxtPtr ctxt)
27193{
27194 if (ctxt == NULL)
27195 return(-1);
27196 return(ctxt->err == 0);
27197}
27198
27199/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027200 * xmlSchemaSetValidErrors:
27201 * @ctxt: a schema validation context
27202 * @err: the error function
27203 * @warn: the warning function
Daniel Veillarda9b66d02002-12-11 14:23:49 +000027204 * @ctx: the functions context
Daniel Veillard4255d502002-04-16 15:50:10 +000027205 *
William M. Brack2f2a6632004-08-20 23:09:47 +000027206 * Set the error and warning callback informations
Daniel Veillard4255d502002-04-16 15:50:10 +000027207 */
27208void
27209xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027210 xmlSchemaValidityErrorFunc err,
27211 xmlSchemaValidityWarningFunc warn, void *ctx)
27212{
Daniel Veillard4255d502002-04-16 15:50:10 +000027213 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027214 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000027215 ctxt->error = err;
27216 ctxt->warning = warn;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027217 ctxt->errCtxt = ctx;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027218 if (ctxt->pctxt != NULL)
27219 xmlSchemaSetParserErrors(ctxt->pctxt, err, warn, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000027220}
27221
27222/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027223 * xmlSchemaSetValidStructuredErrors:
27224 * @ctxt: a schema validation context
27225 * @serror: the structured error function
27226 * @ctx: the functions context
27227 *
27228 * Set the structured error callback
27229 */
27230void
27231xmlSchemaSetValidStructuredErrors(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027232 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027233{
27234 if (ctxt == NULL)
27235 return;
27236 ctxt->serror = serror;
27237 ctxt->error = NULL;
27238 ctxt->warning = NULL;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027239 ctxt->errCtxt = ctx;
27240 if (ctxt->pctxt != NULL)
27241 xmlSchemaSetParserStructuredErrors(ctxt->pctxt, serror, ctx);
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027242}
27243
27244/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000027245 * xmlSchemaGetValidErrors:
27246 * @ctxt: a XML-Schema validation context
27247 * @err: the error function result
27248 * @warn: the warning function result
27249 * @ctx: the functions context result
27250 *
27251 * Get the error and warning callback informations
27252 *
27253 * Returns -1 in case of error and 0 otherwise
27254 */
27255int
27256xmlSchemaGetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027257 xmlSchemaValidityErrorFunc * err,
27258 xmlSchemaValidityWarningFunc * warn, void **ctx)
Daniel Veillard259f0df2004-08-18 09:13:18 +000027259{
27260 if (ctxt == NULL)
27261 return (-1);
27262 if (err != NULL)
27263 *err = ctxt->error;
27264 if (warn != NULL)
27265 *warn = ctxt->warning;
27266 if (ctx != NULL)
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027267 *ctx = ctxt->errCtxt;
Daniel Veillard259f0df2004-08-18 09:13:18 +000027268 return (0);
27269}
27270
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027271
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027272/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027273 * xmlSchemaSetValidOptions:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027274 * @ctxt: a schema validation context
27275 * @options: a combination of xmlSchemaValidOption
27276 *
27277 * Sets the options to be used during the validation.
27278 *
27279 * Returns 0 in case of success, -1 in case of an
27280 * API error.
27281 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027282int
27283xmlSchemaSetValidOptions(xmlSchemaValidCtxtPtr ctxt,
27284 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027285
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027286{
27287 int i;
27288
27289 if (ctxt == NULL)
27290 return (-1);
27291 /*
27292 * WARNING: Change the start value if adding to the
27293 * xmlSchemaValidOption.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027294 * TODO: Is there an other, more easy to maintain,
27295 * way?
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027296 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027297 for (i = 1; i < (int) sizeof(int) * 8; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027298 if (options & 1<<i)
27299 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027300 }
27301 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027302 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027303}
27304
27305/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027306 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027307 * @ctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027308 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027309 * Get the validation context options.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027310 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027311 * Returns the option combination or -1 on error.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027312 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027313int
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027314xmlSchemaValidCtxtGetOptions(xmlSchemaValidCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027315
27316{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027317 if (ctxt == NULL)
27318 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027319 else
27320 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027321}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027322
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027323static int
27324xmlSchemaVDocWalk(xmlSchemaValidCtxtPtr vctxt)
27325{
27326 xmlAttrPtr attr;
27327 int ret = 0;
27328 xmlSchemaNodeInfoPtr ielem = NULL;
27329 xmlNodePtr node, valRoot;
27330 const xmlChar *nsName;
27331
27332 /* DOC VAL TODO: Move this to the start function. */
27333 valRoot = xmlDocGetRootElement(vctxt->doc);
27334 if (valRoot == NULL) {
27335 /* VAL TODO: Error code? */
27336 VERROR(1, NULL, "The document has no document element");
27337 return (1);
27338 }
27339 vctxt->depth = -1;
27340 vctxt->validationRoot = valRoot;
27341 node = valRoot;
27342 while (node != NULL) {
27343 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27344 goto next_sibling;
27345 if (node->type == XML_ELEMENT_NODE) {
27346
27347 /*
27348 * Init the node-info.
27349 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027350 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027351 if (xmlSchemaValidatorPushElem(vctxt) == -1)
27352 goto internal_error;
27353 ielem = vctxt->inode;
27354 ielem->node = node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027355 ielem->nodeLine = node->line;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027356 ielem->localName = node->name;
27357 if (node->ns != NULL)
27358 ielem->nsName = node->ns->href;
27359 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
27360 /*
27361 * Register attributes.
27362 * DOC VAL TODO: We do not register namespace declaration
27363 * attributes yet.
27364 */
27365 vctxt->nbAttrInfos = 0;
27366 if (node->properties != NULL) {
27367 attr = node->properties;
27368 do {
27369 if (attr->ns != NULL)
27370 nsName = attr->ns->href;
27371 else
27372 nsName = NULL;
27373 ret = xmlSchemaValidatorPushAttribute(vctxt,
27374 (xmlNodePtr) attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027375 /*
27376 * Note that we give it the line number of the
27377 * parent element.
27378 */
27379 ielem->nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027380 attr->name, nsName, 0,
27381 xmlNodeListGetString(attr->doc, attr->children, 1), 1);
27382 if (ret == -1) {
27383 VERROR_INT("xmlSchemaDocWalk",
27384 "calling xmlSchemaValidatorPushAttribute()");
27385 goto internal_error;
27386 }
27387 attr = attr->next;
27388 } while (attr);
27389 }
27390 /*
27391 * Validate the element.
27392 */
27393 ret = xmlSchemaValidateElem(vctxt);
27394 if (ret != 0) {
27395 if (ret == -1) {
27396 VERROR_INT("xmlSchemaDocWalk",
27397 "calling xmlSchemaValidateElem()");
27398 goto internal_error;
27399 }
27400 /*
27401 * Don't stop validation; just skip the content
27402 * of this element.
27403 */
27404 goto leave_node;
27405 }
27406 if ((vctxt->skipDepth != -1) &&
27407 (vctxt->depth >= vctxt->skipDepth))
27408 goto leave_node;
27409 } else if ((node->type == XML_TEXT_NODE) ||
27410 (node->type == XML_CDATA_SECTION_NODE)) {
27411 /*
27412 * Process character content.
27413 */
27414 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
27415 ielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27416 ret = xmlSchemaVPushText(vctxt, node->type, node->content,
27417 -1, XML_SCHEMA_PUSH_TEXT_PERSIST, NULL);
27418 if (ret < 0) {
27419 VERROR_INT("xmlSchemaVDocWalk",
27420 "calling xmlSchemaVPushText()");
27421 goto internal_error;
27422 }
27423 /*
27424 * DOC VAL TODO: Should we skip further validation of the
27425 * element content here?
27426 */
27427 } else if ((node->type == XML_ENTITY_NODE) ||
27428 (node->type == XML_ENTITY_REF_NODE)) {
27429 /*
27430 * DOC VAL TODO: What to do with entities?
27431 */
27432 TODO
27433 } else {
27434 goto leave_node;
27435 /*
27436 * DOC VAL TODO: XInclude nodes, etc.
27437 */
27438 }
27439 /*
27440 * Walk the doc.
27441 */
27442 if (node->children != NULL) {
27443 node = node->children;
27444 continue;
27445 }
27446leave_node:
27447 if (node->type == XML_ELEMENT_NODE) {
27448 /*
27449 * Leaving the scope of an element.
27450 */
27451 if (node != vctxt->inode->node) {
27452 VERROR_INT("xmlSchemaVDocWalk",
27453 "element position mismatch");
27454 goto internal_error;
27455 }
27456 ret = xmlSchemaValidatorPopElem(vctxt);
27457 if (ret != 0) {
27458 if (ret < 0) {
27459 VERROR_INT("xmlSchemaVDocWalk",
27460 "calling xmlSchemaValidatorPopElem()");
27461 goto internal_error;
27462 }
27463 }
27464 if (node == valRoot)
27465 goto exit;
27466 }
27467next_sibling:
27468 if (node->next != NULL)
27469 node = node->next;
27470 else {
27471 node = node->parent;
27472 goto leave_node;
27473 }
27474 }
27475
27476exit:
27477 return (ret);
27478internal_error:
27479 return (-1);
27480}
27481
27482static int
Daniel Veillardf10ae122005-07-10 19:03:16 +000027483xmlSchemaPreRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027484 /*
27485 * Some initialization.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027486 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027487 vctxt->err = 0;
27488 vctxt->nberrors = 0;
27489 vctxt->depth = -1;
27490 vctxt->skipDepth = -1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027491 vctxt->xsiAssemble = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027492 vctxt->hasKeyrefs = 0;
Kasimier T. Buchcik132ba5f2005-11-28 12:32:24 +000027493#ifdef ENABLE_IDC_NODE_TABLES_TEST
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027494 vctxt->createIDCNodeTables = 1;
27495#else
27496 vctxt->createIDCNodeTables = 0;
27497#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027498 /*
27499 * Create a schema + parser if necessary.
27500 */
27501 if (vctxt->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027502 xmlSchemaParserCtxtPtr pctxt;
27503
27504 vctxt->xsiAssemble = 1;
27505 /*
27506 * If not schema was given then we will create a schema
27507 * dynamically using XSI schema locations.
27508 *
27509 * Create the schema parser context.
27510 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027511 if ((vctxt->pctxt == NULL) &&
27512 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
27513 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027514 pctxt = vctxt->pctxt;
27515 pctxt->xsiAssemble = 1;
27516 /*
27517 * Create the schema.
27518 */
27519 vctxt->schema = xmlSchemaNewSchema(pctxt);
27520 if (vctxt->schema == NULL)
27521 return (-1);
27522 /*
27523 * Create the schema construction context.
27524 */
27525 pctxt->constructor = xmlSchemaConstructionCtxtCreate(pctxt->dict);
27526 if (pctxt->constructor == NULL)
27527 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000027528 pctxt->constructor->mainSchema = vctxt->schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027529 /*
27530 * Take ownership of the constructor to be able to free it.
27531 */
27532 pctxt->ownsConstructor = 1;
27533 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027534 /*
27535 * Augment the IDC definitions.
27536 */
27537 if (vctxt->schema->idcDef != NULL) {
27538 xmlHashScan(vctxt->schema->idcDef,
27539 (xmlHashScanner) xmlSchemaAugmentIDC, vctxt);
27540 }
Daniel Veillardf10ae122005-07-10 19:03:16 +000027541 return(0);
27542}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027543
Daniel Veillardf10ae122005-07-10 19:03:16 +000027544static void
27545xmlSchemaPostRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027546 if (vctxt->xsiAssemble) {
27547 if (vctxt->schema != NULL) {
27548 xmlSchemaFree(vctxt->schema);
27549 vctxt->schema = NULL;
27550 }
27551 }
27552 xmlSchemaClearValidCtxt(vctxt);
Daniel Veillardf10ae122005-07-10 19:03:16 +000027553}
27554
27555static int
27556xmlSchemaVStart(xmlSchemaValidCtxtPtr vctxt)
27557{
27558 int ret = 0;
27559
27560 if (xmlSchemaPreRun(vctxt) < 0)
27561 return(-1);
27562
27563 if (vctxt->doc != NULL) {
27564 /*
27565 * Tree validation.
27566 */
27567 ret = xmlSchemaVDocWalk(vctxt);
27568#ifdef LIBXML_READER_ENABLED
27569 } else if (vctxt->reader != NULL) {
27570 /*
27571 * XML Reader validation.
27572 */
27573#ifdef XML_SCHEMA_READER_ENABLED
27574 ret = xmlSchemaVReaderWalk(vctxt);
27575#endif
27576#endif
27577 } else if ((vctxt->sax != NULL) && (vctxt->parserCtxt != NULL)) {
27578 /*
27579 * SAX validation.
27580 */
27581 ret = xmlParseDocument(vctxt->parserCtxt);
27582 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027583 VERROR_INT("xmlSchemaVStart",
Daniel Veillardf10ae122005-07-10 19:03:16 +000027584 "no instance to validate");
27585 ret = -1;
27586 }
27587
27588 xmlSchemaPostRun(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027589 if (ret == 0)
27590 ret = vctxt->err;
27591 return (ret);
27592}
27593
27594/**
27595 * xmlSchemaValidateOneElement:
27596 * @ctxt: a schema validation context
27597 * @elem: an element node
27598 *
27599 * Validate a branch of a tree, starting with the given @elem.
27600 *
27601 * Returns 0 if the element and its subtree is valid, a positive error
27602 * code number otherwise and -1 in case of an internal or API error.
27603 */
27604int
27605xmlSchemaValidateOneElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem)
27606{
27607 if ((ctxt == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
27608 return (-1);
27609
27610 if (ctxt->schema == NULL)
27611 return (-1);
27612
27613 ctxt->doc = elem->doc;
27614 ctxt->node = elem;
27615 ctxt->validationRoot = elem;
27616 return(xmlSchemaVStart(ctxt));
27617}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027618
Daniel Veillard259f0df2004-08-18 09:13:18 +000027619/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027620 * xmlSchemaValidateDoc:
27621 * @ctxt: a schema validation context
27622 * @doc: a parsed document tree
27623 *
27624 * Validate a document tree in memory.
27625 *
27626 * Returns 0 if the document is schemas valid, a positive error code
27627 * number otherwise and -1 in case of internal or API error.
27628 */
27629int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027630xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc)
27631{
Daniel Veillard4255d502002-04-16 15:50:10 +000027632 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027633 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000027634
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027635 ctxt->doc = doc;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027636 ctxt->node = xmlDocGetRootElement(doc);
27637 if (ctxt->node == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027638 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027639 XML_SCHEMAV_DOCUMENT_ELEMENT_MISSING,
27640 (xmlNodePtr) doc, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027641 "The document has no document element", NULL, NULL);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027642 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027643 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027644 ctxt->validationRoot = ctxt->node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027645 return (xmlSchemaVStart(ctxt));
Daniel Veillard4255d502002-04-16 15:50:10 +000027646}
27647
Daniel Veillardcdc82732005-07-08 15:04:06 +000027648
27649/************************************************************************
27650 * *
27651 * Function and data for SAX streaming API *
27652 * *
27653 ************************************************************************/
27654typedef struct _xmlSchemaSplitSAXData xmlSchemaSplitSAXData;
27655typedef xmlSchemaSplitSAXData *xmlSchemaSplitSAXDataPtr;
27656
27657struct _xmlSchemaSplitSAXData {
27658 xmlSAXHandlerPtr user_sax;
27659 void *user_data;
27660 xmlSchemaValidCtxtPtr ctxt;
27661 xmlSAXHandlerPtr schemas_sax;
27662};
27663
Daniel Veillard971771e2005-07-09 17:32:57 +000027664#define XML_SAX_PLUG_MAGIC 0xdc43ba21
27665
27666struct _xmlSchemaSAXPlug {
27667 unsigned int magic;
27668
27669 /* the original callbacks informations */
27670 xmlSAXHandlerPtr *user_sax_ptr;
27671 xmlSAXHandlerPtr user_sax;
27672 void **user_data_ptr;
27673 void *user_data;
27674
27675 /* the block plugged back and validation informations */
27676 xmlSAXHandler schemas_sax;
27677 xmlSchemaValidCtxtPtr ctxt;
27678};
27679
Daniel Veillardcdc82732005-07-08 15:04:06 +000027680/* All those functions just bounces to the user provided SAX handlers */
27681static void
27682internalSubsetSplit(void *ctx, const xmlChar *name,
27683 const xmlChar *ExternalID, const xmlChar *SystemID)
27684{
Daniel Veillard971771e2005-07-09 17:32:57 +000027685 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027686 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27687 (ctxt->user_sax->internalSubset != NULL))
27688 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27689 SystemID);
27690}
27691
27692static int
27693isStandaloneSplit(void *ctx)
27694{
Daniel Veillard971771e2005-07-09 17:32:57 +000027695 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027696 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27697 (ctxt->user_sax->isStandalone != NULL))
27698 return(ctxt->user_sax->isStandalone(ctxt->user_data));
27699 return(0);
27700}
27701
27702static int
27703hasInternalSubsetSplit(void *ctx)
27704{
Daniel Veillard971771e2005-07-09 17:32:57 +000027705 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027706 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27707 (ctxt->user_sax->hasInternalSubset != NULL))
27708 return(ctxt->user_sax->hasInternalSubset(ctxt->user_data));
27709 return(0);
27710}
27711
27712static int
27713hasExternalSubsetSplit(void *ctx)
27714{
Daniel Veillard971771e2005-07-09 17:32:57 +000027715 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027716 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27717 (ctxt->user_sax->hasExternalSubset != NULL))
27718 return(ctxt->user_sax->hasExternalSubset(ctxt->user_data));
27719 return(0);
27720}
27721
27722static void
27723externalSubsetSplit(void *ctx, const xmlChar *name,
27724 const xmlChar *ExternalID, const xmlChar *SystemID)
27725{
Daniel Veillard971771e2005-07-09 17:32:57 +000027726 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027727 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27728 (ctxt->user_sax->internalSubset != NULL))
27729 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27730 SystemID);
27731}
27732
27733static xmlParserInputPtr
27734resolveEntitySplit(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
27735{
Daniel Veillard971771e2005-07-09 17:32:57 +000027736 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027737 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27738 (ctxt->user_sax->resolveEntity != NULL))
27739 return(ctxt->user_sax->resolveEntity(ctxt->user_data, publicId,
27740 systemId));
27741 return(NULL);
27742}
27743
27744static xmlEntityPtr
27745getEntitySplit(void *ctx, const xmlChar *name)
27746{
Daniel Veillard971771e2005-07-09 17:32:57 +000027747 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027748 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27749 (ctxt->user_sax->getEntity != NULL))
27750 return(ctxt->user_sax->getEntity(ctxt->user_data, name));
27751 return(NULL);
27752}
27753
27754static xmlEntityPtr
27755getParameterEntitySplit(void *ctx, const xmlChar *name)
27756{
Daniel Veillard971771e2005-07-09 17:32:57 +000027757 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027758 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27759 (ctxt->user_sax->getParameterEntity != NULL))
27760 return(ctxt->user_sax->getParameterEntity(ctxt->user_data, name));
27761 return(NULL);
27762}
27763
27764
27765static void
27766entityDeclSplit(void *ctx, const xmlChar *name, int type,
27767 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
27768{
Daniel Veillard971771e2005-07-09 17:32:57 +000027769 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027770 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27771 (ctxt->user_sax->entityDecl != NULL))
27772 ctxt->user_sax->entityDecl(ctxt->user_data, name, type, publicId,
27773 systemId, content);
27774}
27775
27776static void
27777attributeDeclSplit(void *ctx, const xmlChar * elem,
27778 const xmlChar * name, int type, int def,
27779 const xmlChar * defaultValue, xmlEnumerationPtr tree)
27780{
Daniel Veillard971771e2005-07-09 17:32:57 +000027781 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027782 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27783 (ctxt->user_sax->attributeDecl != NULL)) {
27784 ctxt->user_sax->attributeDecl(ctxt->user_data, elem, name, type,
27785 def, defaultValue, tree);
27786 } else {
27787 xmlFreeEnumeration(tree);
27788 }
27789}
27790
27791static void
27792elementDeclSplit(void *ctx, const xmlChar *name, int type,
27793 xmlElementContentPtr content)
27794{
Daniel Veillard971771e2005-07-09 17:32:57 +000027795 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027796 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27797 (ctxt->user_sax->elementDecl != NULL))
27798 ctxt->user_sax->elementDecl(ctxt->user_data, name, type, content);
27799}
27800
27801static void
27802notationDeclSplit(void *ctx, const xmlChar *name,
27803 const xmlChar *publicId, const xmlChar *systemId)
27804{
Daniel Veillard971771e2005-07-09 17:32:57 +000027805 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027806 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27807 (ctxt->user_sax->notationDecl != NULL))
27808 ctxt->user_sax->notationDecl(ctxt->user_data, name, publicId,
27809 systemId);
27810}
27811
27812static void
27813unparsedEntityDeclSplit(void *ctx, const xmlChar *name,
27814 const xmlChar *publicId, const xmlChar *systemId,
27815 const xmlChar *notationName)
27816{
Daniel Veillard971771e2005-07-09 17:32:57 +000027817 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027818 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27819 (ctxt->user_sax->unparsedEntityDecl != NULL))
27820 ctxt->user_sax->unparsedEntityDecl(ctxt->user_data, name, publicId,
27821 systemId, notationName);
27822}
27823
27824static void
27825setDocumentLocatorSplit(void *ctx, xmlSAXLocatorPtr loc)
27826{
Daniel Veillard971771e2005-07-09 17:32:57 +000027827 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027828 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27829 (ctxt->user_sax->setDocumentLocator != NULL))
27830 ctxt->user_sax->setDocumentLocator(ctxt->user_data, loc);
27831}
27832
27833static void
27834startDocumentSplit(void *ctx)
27835{
Daniel Veillard971771e2005-07-09 17:32:57 +000027836 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027837 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27838 (ctxt->user_sax->startDocument != NULL))
27839 ctxt->user_sax->startDocument(ctxt->user_data);
27840}
27841
27842static void
27843endDocumentSplit(void *ctx)
27844{
Daniel Veillard971771e2005-07-09 17:32:57 +000027845 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027846 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27847 (ctxt->user_sax->endDocument != NULL))
27848 ctxt->user_sax->endDocument(ctxt->user_data);
27849}
27850
27851static void
27852processingInstructionSplit(void *ctx, const xmlChar *target,
27853 const xmlChar *data)
27854{
Daniel Veillard971771e2005-07-09 17:32:57 +000027855 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027856 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27857 (ctxt->user_sax->processingInstruction != NULL))
27858 ctxt->user_sax->processingInstruction(ctxt->user_data, target, data);
27859}
27860
27861static void
27862commentSplit(void *ctx, const xmlChar *value)
27863{
Daniel Veillard971771e2005-07-09 17:32:57 +000027864 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027865 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27866 (ctxt->user_sax->comment != NULL))
27867 ctxt->user_sax->comment(ctxt->user_data, value);
27868}
27869
27870/*
27871 * Varargs error callbacks to the user application, harder ...
27872 */
27873
Daniel Veillardffa3c742005-07-21 13:24:09 +000027874static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027875warningSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027876 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027877 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27878 (ctxt->user_sax->warning != NULL)) {
27879 TODO
27880 }
27881}
Daniel Veillardffa3c742005-07-21 13:24:09 +000027882static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027883errorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027884 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027885 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27886 (ctxt->user_sax->error != NULL)) {
27887 TODO
27888 }
27889}
Daniel Veillardffa3c742005-07-21 13:24:09 +000027890static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027891fatalErrorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027892 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027893 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27894 (ctxt->user_sax->fatalError != NULL)) {
27895 TODO
27896 }
27897}
27898
27899/*
27900 * Those are function where both the user handler and the schemas handler
27901 * need to be called.
27902 */
27903static void
27904charactersSplit(void *ctx, const xmlChar *ch, int len)
27905{
Daniel Veillard971771e2005-07-09 17:32:57 +000027906 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027907 if (ctxt == NULL)
27908 return;
27909 if ((ctxt->user_sax != NULL) && (ctxt->user_sax->characters != NULL))
27910 ctxt->user_sax->characters(ctxt->user_data, ch, len);
27911 if (ctxt->ctxt != NULL)
27912 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
27913}
27914
27915static void
27916ignorableWhitespaceSplit(void *ctx, const xmlChar *ch, int len)
27917{
Daniel Veillard971771e2005-07-09 17:32:57 +000027918 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027919 if (ctxt == NULL)
27920 return;
27921 if ((ctxt->user_sax != NULL) &&
27922 (ctxt->user_sax->ignorableWhitespace != NULL))
27923 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, ch, len);
27924 if (ctxt->ctxt != NULL)
27925 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
27926}
27927
27928static void
27929cdataBlockSplit(void *ctx, const xmlChar *value, int len)
27930{
Daniel Veillard971771e2005-07-09 17:32:57 +000027931 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027932 if (ctxt == NULL)
27933 return;
27934 if ((ctxt->user_sax != NULL) &&
27935 (ctxt->user_sax->ignorableWhitespace != NULL))
27936 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, value, len);
27937 if (ctxt->ctxt != NULL)
27938 xmlSchemaSAXHandleCDataSection(ctxt->ctxt, value, len);
27939}
27940
27941static void
27942referenceSplit(void *ctx, const xmlChar *name)
27943{
Daniel Veillard971771e2005-07-09 17:32:57 +000027944 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027945 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27946 (ctxt->user_sax->reference != NULL))
27947 ctxt->user_sax->reference(ctxt->user_data, name);
27948 if (ctxt->ctxt != NULL)
27949 xmlSchemaSAXHandleReference(ctxt->user_data, name);
27950}
27951
27952static void
27953startElementNsSplit(void *ctx, const xmlChar * localname,
27954 const xmlChar * prefix, const xmlChar * URI,
27955 int nb_namespaces, const xmlChar ** namespaces,
27956 int nb_attributes, int nb_defaulted,
27957 const xmlChar ** attributes) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027958 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027959 if (ctxt == NULL)
27960 return;
27961 if ((ctxt->user_sax != NULL) &&
27962 (ctxt->user_sax->startElementNs != NULL))
27963 ctxt->user_sax->startElementNs(ctxt->user_data, localname, prefix,
27964 URI, nb_namespaces, namespaces,
27965 nb_attributes, nb_defaulted,
27966 attributes);
27967 if (ctxt->ctxt != NULL)
27968 xmlSchemaSAXHandleStartElementNs(ctxt->ctxt, localname, prefix,
27969 URI, nb_namespaces, namespaces,
27970 nb_attributes, nb_defaulted,
27971 attributes);
27972}
27973
27974static void
27975endElementNsSplit(void *ctx, const xmlChar * localname,
27976 const xmlChar * prefix, const xmlChar * URI) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027977 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027978 if (ctxt == NULL)
27979 return;
27980 if ((ctxt->user_sax != NULL) &&
27981 (ctxt->user_sax->endElementNs != NULL))
27982 ctxt->user_sax->endElementNs(ctxt->user_data, localname, prefix, URI);
27983 if (ctxt->ctxt != NULL)
27984 xmlSchemaSAXHandleEndElementNs(ctxt->ctxt, localname, prefix, URI);
27985}
27986
Daniel Veillard4255d502002-04-16 15:50:10 +000027987/**
Daniel Veillard971771e2005-07-09 17:32:57 +000027988 * xmlSchemaSAXPlug:
27989 * @ctxt: a schema validation context
Daniel Veillard1f33c4d2005-07-10 21:38:31 +000027990 * @sax: a pointer to the original xmlSAXHandlerPtr
27991 * @user_data: a pointer to the original SAX user data pointer
Daniel Veillard971771e2005-07-09 17:32:57 +000027992 *
27993 * Plug a SAX based validation layer in a SAX parsing event flow.
27994 * The original @saxptr and @dataptr data are replaced by new pointers
27995 * but the calls to the original will be maintained.
27996 *
27997 * Returns a pointer to a data structure needed to unplug the validation layer
27998 * or NULL in case of errors.
27999 */
28000xmlSchemaSAXPlugPtr
28001xmlSchemaSAXPlug(xmlSchemaValidCtxtPtr ctxt,
28002 xmlSAXHandlerPtr *sax, void **user_data)
28003{
28004 xmlSchemaSAXPlugPtr ret;
28005 xmlSAXHandlerPtr old_sax;
28006
28007 if ((ctxt == NULL) || (sax == NULL) || (user_data == NULL))
28008 return(NULL);
28009
28010 /*
28011 * We only allow to plug into SAX2 event streams
28012 */
28013 old_sax = *sax;
28014 if ((old_sax != NULL) && (old_sax->initialized != XML_SAX2_MAGIC))
28015 return(NULL);
28016 if ((old_sax != NULL) &&
28017 (old_sax->startElementNs == NULL) && (old_sax->endElementNs == NULL) &&
28018 ((old_sax->startElement != NULL) || (old_sax->endElement != NULL)))
28019 return(NULL);
28020
28021 /*
28022 * everything seems right allocate the local data needed for that layer
28023 */
28024 ret = (xmlSchemaSAXPlugPtr) xmlMalloc(sizeof(xmlSchemaSAXPlugStruct));
28025 if (ret == NULL) {
28026 return(NULL);
28027 }
28028 memset(ret, 0, sizeof(xmlSchemaSAXPlugStruct));
28029 ret->magic = XML_SAX_PLUG_MAGIC;
28030 ret->schemas_sax.initialized = XML_SAX2_MAGIC;
28031 ret->ctxt = ctxt;
28032 ret->user_sax_ptr = sax;
28033 ret->user_sax = old_sax;
28034 if (old_sax == NULL) {
28035 /*
28036 * go direct, no need for the split block and functions.
28037 */
28038 ret->schemas_sax.startElementNs = xmlSchemaSAXHandleStartElementNs;
28039 ret->schemas_sax.endElementNs = xmlSchemaSAXHandleEndElementNs;
28040 /*
28041 * Note that we use the same text-function for both, to prevent
28042 * the parser from testing for ignorable whitespace.
28043 */
28044 ret->schemas_sax.ignorableWhitespace = xmlSchemaSAXHandleText;
28045 ret->schemas_sax.characters = xmlSchemaSAXHandleText;
28046
28047 ret->schemas_sax.cdataBlock = xmlSchemaSAXHandleCDataSection;
28048 ret->schemas_sax.reference = xmlSchemaSAXHandleReference;
28049
28050 ret->user_data = ctxt;
28051 *user_data = ctxt;
28052 } else {
28053 /*
28054 * for each callback unused by Schemas initialize it to the Split
28055 * routine only if non NULL in the user block, this can speed up
28056 * things at the SAX level.
28057 */
28058 if (old_sax->internalSubset != NULL)
28059 ret->schemas_sax.internalSubset = internalSubsetSplit;
28060 if (old_sax->isStandalone != NULL)
28061 ret->schemas_sax.isStandalone = isStandaloneSplit;
28062 if (old_sax->hasInternalSubset != NULL)
28063 ret->schemas_sax.hasInternalSubset = hasInternalSubsetSplit;
28064 if (old_sax->hasExternalSubset != NULL)
28065 ret->schemas_sax.hasExternalSubset = hasExternalSubsetSplit;
28066 if (old_sax->resolveEntity != NULL)
28067 ret->schemas_sax.resolveEntity = resolveEntitySplit;
28068 if (old_sax->getEntity != NULL)
28069 ret->schemas_sax.getEntity = getEntitySplit;
28070 if (old_sax->entityDecl != NULL)
28071 ret->schemas_sax.entityDecl = entityDeclSplit;
28072 if (old_sax->notationDecl != NULL)
28073 ret->schemas_sax.notationDecl = notationDeclSplit;
28074 if (old_sax->attributeDecl != NULL)
28075 ret->schemas_sax.attributeDecl = attributeDeclSplit;
28076 if (old_sax->elementDecl != NULL)
28077 ret->schemas_sax.elementDecl = elementDeclSplit;
28078 if (old_sax->unparsedEntityDecl != NULL)
28079 ret->schemas_sax.unparsedEntityDecl = unparsedEntityDeclSplit;
28080 if (old_sax->setDocumentLocator != NULL)
28081 ret->schemas_sax.setDocumentLocator = setDocumentLocatorSplit;
28082 if (old_sax->startDocument != NULL)
28083 ret->schemas_sax.startDocument = startDocumentSplit;
28084 if (old_sax->endDocument != NULL)
28085 ret->schemas_sax.endDocument = endDocumentSplit;
28086 if (old_sax->processingInstruction != NULL)
28087 ret->schemas_sax.processingInstruction = processingInstructionSplit;
28088 if (old_sax->comment != NULL)
28089 ret->schemas_sax.comment = commentSplit;
28090 if (old_sax->warning != NULL)
28091 ret->schemas_sax.warning = warningSplit;
28092 if (old_sax->error != NULL)
28093 ret->schemas_sax.error = errorSplit;
28094 if (old_sax->fatalError != NULL)
28095 ret->schemas_sax.fatalError = fatalErrorSplit;
28096 if (old_sax->getParameterEntity != NULL)
28097 ret->schemas_sax.getParameterEntity = getParameterEntitySplit;
28098 if (old_sax->externalSubset != NULL)
28099 ret->schemas_sax.externalSubset = externalSubsetSplit;
28100
28101 /*
28102 * the 6 schemas callback have to go to the splitter functions
28103 * Note that we use the same text-function for ignorableWhitespace
28104 * if possible, to prevent the parser from testing for ignorable
28105 * whitespace.
28106 */
28107 ret->schemas_sax.characters = charactersSplit;
28108 if ((old_sax->ignorableWhitespace != NULL) &&
28109 (old_sax->ignorableWhitespace != old_sax->characters))
28110 ret->schemas_sax.ignorableWhitespace = ignorableWhitespaceSplit;
28111 else
28112 ret->schemas_sax.ignorableWhitespace = charactersSplit;
28113 ret->schemas_sax.cdataBlock = cdataBlockSplit;
28114 ret->schemas_sax.reference = referenceSplit;
28115 ret->schemas_sax.startElementNs = startElementNsSplit;
28116 ret->schemas_sax.endElementNs = endElementNsSplit;
28117
28118 ret->user_data_ptr = user_data;
28119 ret->user_data = *user_data;
28120 *user_data = ret;
28121 }
28122
28123 /*
28124 * plug the pointers back.
28125 */
28126 *sax = &(ret->schemas_sax);
Daniel Veillardf10ae122005-07-10 19:03:16 +000028127 ctxt->sax = *sax;
28128 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28129 xmlSchemaPreRun(ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028130 return(ret);
28131}
28132
28133/**
28134 * xmlSchemaSAXUnplug:
28135 * @plug: a data structure returned by xmlSchemaSAXPlug
28136 *
28137 * Unplug a SAX based validation layer in a SAX parsing event flow.
28138 * The original pointers used in the call are restored.
28139 *
28140 * Returns 0 in case of success and -1 in case of failure.
28141 */
28142int
28143xmlSchemaSAXUnplug(xmlSchemaSAXPlugPtr plug)
28144{
28145 xmlSAXHandlerPtr *sax;
28146 void **user_data;
28147
28148 if ((plug == NULL) || (plug->magic != XML_SAX_PLUG_MAGIC))
28149 return(-1);
28150 plug->magic = 0;
28151
Daniel Veillardf10ae122005-07-10 19:03:16 +000028152 xmlSchemaPostRun(plug->ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028153 /* restore the data */
28154 sax = plug->user_sax_ptr;
28155 *sax = plug->user_sax;
28156 if (plug->user_sax != NULL) {
28157 user_data = plug->user_data_ptr;
28158 *user_data = plug->user_data;
28159 }
28160
28161 /* free and return */
28162 xmlFree(plug);
28163 return(0);
28164}
28165
28166/**
Daniel Veillard4255d502002-04-16 15:50:10 +000028167 * xmlSchemaValidateStream:
28168 * @ctxt: a schema validation context
28169 * @input: the input to use for reading the data
28170 * @enc: an optional encoding information
28171 * @sax: a SAX handler for the resulting events
28172 * @user_data: the context to provide to the SAX handler.
28173 *
Daniel Veillardcdc82732005-07-08 15:04:06 +000028174 * Validate an input based on a flow of SAX event from the parser
28175 * and forward the events to the @sax handler with the provided @user_data
28176 * the user provided @sax handler must be a SAX2 one.
Daniel Veillard4255d502002-04-16 15:50:10 +000028177 *
28178 * Returns 0 if the document is schemas valid, a positive error code
28179 * number otherwise and -1 in case of internal or API error.
28180 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028181int
Daniel Veillard4255d502002-04-16 15:50:10 +000028182xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028183 xmlParserInputBufferPtr input, xmlCharEncoding enc,
28184 xmlSAXHandlerPtr sax, void *user_data)
28185{
Daniel Veillard971771e2005-07-09 17:32:57 +000028186 xmlSchemaSAXPlugPtr plug = NULL;
28187 xmlSAXHandlerPtr old_sax = NULL;
28188 xmlParserCtxtPtr pctxt = NULL;
28189 xmlParserInputPtr inputStream = NULL;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028190 int ret;
28191
Daniel Veillard4255d502002-04-16 15:50:10 +000028192 if ((ctxt == NULL) || (input == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028193 return (-1);
Daniel Veillardcdc82732005-07-08 15:04:06 +000028194
Daniel Veillardcdc82732005-07-08 15:04:06 +000028195 /*
28196 * prepare the parser
28197 */
28198 pctxt = xmlNewParserCtxt();
28199 if (pctxt == NULL)
28200 return (-1);
28201 old_sax = pctxt->sax;
Daniel Veillard971771e2005-07-09 17:32:57 +000028202 pctxt->sax = sax;
28203 pctxt->userData = user_data;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028204#if 0
28205 if (options)
28206 xmlCtxtUseOptions(pctxt, options);
28207#endif
Kasimier T. Buchcik85aed6d2005-07-08 18:06:18 +000028208 pctxt->linenumbers = 1;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028209
Daniel Veillardf0af8ec2005-07-08 17:27:33 +000028210 inputStream = xmlNewIOInputStream(pctxt, input, enc);;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028211 if (inputStream == NULL) {
28212 ret = -1;
28213 goto done;
28214 }
28215 inputPush(pctxt, inputStream);
28216 ctxt->parserCtxt = pctxt;
28217 ctxt->input = input;
28218
28219 /*
Daniel Veillard971771e2005-07-09 17:32:57 +000028220 * Plug the validation and launch the parsing
Daniel Veillardcdc82732005-07-08 15:04:06 +000028221 */
Daniel Veillard971771e2005-07-09 17:32:57 +000028222 plug = xmlSchemaSAXPlug(ctxt, &(pctxt->sax), &(pctxt->userData));
28223 if (plug == NULL) {
28224 ret = -1;
28225 goto done;
28226 }
28227 ctxt->input = input;
28228 ctxt->enc = enc;
28229 ctxt->sax = pctxt->sax;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028230 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28231 ret = xmlSchemaVStart(ctxt);
28232
28233 if ((ret == 0) && (! ctxt->parserCtxt->wellFormed)) {
28234 ret = ctxt->parserCtxt->errNo;
28235 if (ret == 0)
28236 ret = 1;
28237 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028238
28239done:
Daniel Veillard971771e2005-07-09 17:32:57 +000028240 ctxt->parserCtxt = NULL;
28241 ctxt->sax = NULL;
28242 ctxt->input = NULL;
28243 if (plug != NULL) {
28244 xmlSchemaSAXUnplug(plug);
28245 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028246 /* cleanup */
Daniel Veillard971771e2005-07-09 17:32:57 +000028247 if (pctxt != NULL) {
28248 pctxt->sax = old_sax;
28249 xmlFreeParserCtxt(pctxt);
28250 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028251 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +000028252}
28253
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028254/**
28255 * xmlSchemaValidateFile:
28256 * @ctxt: a schema validation context
Daniel Veillard81562d22005-06-15 13:27:56 +000028257 * @filename: the URI of the instance
28258 * @options: a future set of options, currently unused
28259 *
28260 * Do a schemas validation of the given resource, it will use the
28261 * SAX streamable validation internally.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028262 *
28263 * Returns 0 if the document is valid, a positive error code
28264 * number otherwise and -1 in case of an internal or API error.
28265 */
28266int
28267xmlSchemaValidateFile(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000028268 const char * filename,
28269 int options ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028270{
28271 int ret;
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028272 xmlParserInputBufferPtr input;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028273
28274 if ((ctxt == NULL) || (filename == NULL))
28275 return (-1);
28276
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028277 input = xmlParserInputBufferCreateFilename(filename,
28278 XML_CHAR_ENCODING_NONE);
28279 if (input == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028280 return (-1);
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028281 ret = xmlSchemaValidateStream(ctxt, input, XML_CHAR_ENCODING_NONE,
28282 NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028283 return (ret);
Daniel Veillard81562d22005-06-15 13:27:56 +000028284}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028285
Daniel Veillard5d4644e2005-04-01 13:11:58 +000028286#define bottom_xmlschemas
28287#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +000028288#endif /* LIBXML_SCHEMAS_ENABLED */