blob: 858a479ed7c494913c730b732964015879f84bb8 [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. Buchcik4efd90d2005-06-09 10:32:53 +000083#define DEBUG_ATTR_VALIDATION 0
Daniel Veillardc0826a72004-08-10 14:17:33 +000084
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000085/* #define DEBUG_IDC */
86
87/* #define DEBUG_IDC_NODE_TABLE */
88
89#ifdef DEBUG_IDC
90 #ifndef DEBUG_IDC_NODE_TABLE
91 #define DEBUG_IDC_NODE_TABLE
92 #endif
93#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000094
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000095/* #define ENABLE_PARTICLE_RESTRICTION 1 */
96
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000097#define ENABLE_REDEFINE
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000098
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000099/* #define ENABLE_NAMED_LOCALS */
100
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000101#define ENABLE_IDC_NODE_TABLES
102
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000103#define DUMP_CONTENT_MODEL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000104
105#ifdef LIBXML_READER_ENABLED
106/* #define XML_SCHEMA_READER_ENABLED */
107#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +0000108
Daniel Veillard4255d502002-04-16 15:50:10 +0000109#define UNBOUNDED (1 << 30)
110#define TODO \
111 xmlGenericError(xmlGenericErrorContext, \
112 "Unimplemented block at %s:%d\n", \
113 __FILE__, __LINE__);
114
William M. Brack2f2a6632004-08-20 23:09:47 +0000115#define XML_SCHEMAS_NO_NAMESPACE (const xmlChar *) "##"
Daniel Veillardc0826a72004-08-10 14:17:33 +0000116
Daniel Veillard4255d502002-04-16 15:50:10 +0000117/*
118 * The XML Schemas namespaces
119 */
120static const xmlChar *xmlSchemaNs = (const xmlChar *)
121 "http://www.w3.org/2001/XMLSchema";
122
123static const xmlChar *xmlSchemaInstanceNs = (const xmlChar *)
124 "http://www.w3.org/2001/XMLSchema-instance";
125
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000126static const xmlChar *xmlNamespaceNs = (const xmlChar *)
127 "http://www.w3.org/2000/xmlns/";
128
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000129/*
130* Come casting macros.
131*/
132#define ACTXT_CAST (xmlSchemaAbstractCtxtPtr)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +0000133#define PCTXT_CAST (xmlSchemaParserCtxtPtr)
134#define VCTXT_CAST (xmlSchemaValidCtxtPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000135#define WXS_BASIC_CAST (xmlSchemaBasicItemPtr)
136#define WXS_TREE_CAST (xmlSchemaTreeItemPtr)
137#define WXS_PTC_CAST (xmlSchemaParticlePtr)
138#define WXS_TYPE_CAST (xmlSchemaTypePtr)
139#define WXS_ELEM_CAST (xmlSchemaElementPtr)
140#define WXS_ATTR_GROUP_CAST (xmlSchemaAttributeGroupPtr)
141#define WXS_ATTR_CAST (xmlSchemaAttributePtr)
142#define WXS_ATTR_USE_CAST (xmlSchemaAttributeUsePtr)
143#define WXS_ATTR_PROHIB_CAST (xmlSchemaAttributeUseProhibPtr)
144#define WXS_MODEL_GROUPDEF_CAST (xmlSchemaModelGroupDefPtr)
145#define WXS_MODEL_GROUP_CAST (xmlSchemaModelGroupPtr)
146#define WXS_IDC_CAST (xmlSchemaIDCPtr)
147#define WXS_QNAME_CAST (xmlSchemaQNameRefPtr)
148#define WXS_LIST_CAST (xmlSchemaItemListPtr)
Daniel Veillardc0826a72004-08-10 14:17:33 +0000149
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000150/*
151* Macros to query common properties of components.
152*/
153#define WXS_ITEM_NODE(i) xmlSchemaGetComponentNode(WXS_BASIC_CAST (i))
Daniel Veillard4255d502002-04-16 15:50:10 +0000154
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000155#define WXS_ITEM_TYPE_NAME(i) xmlSchemaGetComponentTypeStr(WXS_BASIC_CAST (i))
156/*
157* Macros for element declarations.
158*/
159#define WXS_ELEM_TYPEDEF(e) (e)->subtypes
Daniel Veillardc0826a72004-08-10 14:17:33 +0000160
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000161#define WXS_SUBST_HEAD(item) (item)->refDecl
162/*
163* Macros for attribute declarations.
164*/
165#define WXS_ATTR_TYPEDEF(a) (a)->subtypes
166/*
167* Macros for attribute uses.
168*/
169#define WXS_ATTRUSE_DECL(au) WXS_ATTR_CAST (WXS_ATTR_USE_CAST (au))->attrDecl
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000170
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000171#define WXS_ATTRUSE_TYPEDEF(au) WXS_ATTR_TYPEDEF(WXS_ATTRUSE_DECL( WXS_ATTR_USE_CAST au))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000172
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000173#define WXS_ATTRUSE_DECL_NAME(au) (WXS_ATTRUSE_DECL(au))->name
174
175#define WXS_ATTRUSE_DECL_TNS(au) (WXS_ATTRUSE_DECL(au))->targetNamespace
176/*
177* Macros for attribute groups.
178*/
179#define WXS_ATTR_GROUP_HAS_REFS(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS)
180#define WXS_ATTR_GROUP_EXPANDED(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED)
181/*
182* Macros for particles.
183*/
184#define WXS_PARTICLE(p) WXS_PTC_CAST (p)
185
186#define WXS_PARTICLE_TERM(p) (WXS_PARTICLE(p))->children
187
188#define WXS_PARTICLE_MODEL(p) WXS_MODEL_GROUP_CAST WXS_PARTICLE(p)->children
189/*
190* Macros for model groups definitions.
191*/
192#define WXS_MODELGROUPDEF_MODEL(mgd) (WXS_MODEL_GROUP_CAST (mgd))->children
193/*
194* Macros for model groups.
195*/
196#define WXS_IS_MODEL_GROUP(i) \
197 (((i)->type == XML_SCHEMA_TYPE_SEQUENCE) || \
198 ((i)->type == XML_SCHEMA_TYPE_CHOICE) || \
199 ((i)->type == XML_SCHEMA_TYPE_ALL))
200
201#define WXS_MODELGROUP_PARTICLE(mg) WXS_PTC_CAST (mg)->children
202/*
203* Macros for schema buckets.
204*/
205#define WXS_IS_BUCKET_INCREDEF(t) (((t) == XML_SCHEMA_SCHEMA_INCLUDE) || \
206 ((t) == XML_SCHEMA_SCHEMA_REDEFINE))
207
208#define WXS_IS_BUCKET_IMPMAIN(t) (((t) == XML_SCHEMA_SCHEMA_MAIN) || \
209 ((t) == XML_SCHEMA_SCHEMA_IMPORT))
210
211#define WXS_IMPBUCKET(b) ((xmlSchemaImportPtr) (b))
212
213#define WXS_INCBUCKET(b) ((xmlSchemaIncludePtr) (b))
214/*
215* Macros for complex/simple types.
216*/
217#define WXS_IS_ANYTYPE(i) \
218 (( (i)->type == XML_SCHEMA_TYPE_BASIC) && \
219 ( (WXS_TYPE_CAST (i))->builtInType == XML_SCHEMAS_ANYTYPE))
220
221#define WXS_IS_COMPLEX(i) \
222 (((i)->type == XML_SCHEMA_TYPE_COMPLEX) || \
223 ((i)->builtInType == XML_SCHEMAS_ANYTYPE))
224
225#define WXS_IS_SIMPLE(item) \
226 ((item->type == XML_SCHEMA_TYPE_SIMPLE) || \
227 ((item->type == XML_SCHEMA_TYPE_BASIC) && \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000228 (item->builtInType != XML_SCHEMAS_ANYTYPE)))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000229
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000230#define WXS_IS_ANY_SIMPLE_TYPE(i) \
231 (((i)->type == XML_SCHEMA_TYPE_BASIC) && \
232 ((i)->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000233
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000234#define WXS_IS_RESTRICTION(t) \
235 ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000236
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000237#define WXS_IS_EXTENSION(t) \
238 ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION)
239
240#define WXS_IS_TYPE_NOT_FIXED(i) \
241 (((i)->type != XML_SCHEMA_TYPE_BASIC) && \
242 (((i)->flags & XML_SCHEMAS_TYPE_INTERNAL_RESOLVED) == 0))
243
244#define WXS_IS_TYPE_NOT_FIXED_1(item) \
245 (((item)->type != XML_SCHEMA_TYPE_BASIC) && \
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000246 (((item)->flags & XML_SCHEMAS_TYPE_FIXUP_1) == 0))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000247/*
248* Macros for exclusively for complex types.
249*/
250#define WXS_HAS_COMPLEX_CONTENT(item) \
251 ((item->contentType == XML_SCHEMA_CONTENT_MIXED) || \
252 (item->contentType == XML_SCHEMA_CONTENT_EMPTY) || \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000253 (item->contentType == XML_SCHEMA_CONTENT_ELEMENTS))
254
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000255#define WXS_HAS_SIMPLE_CONTENT(item) \
256 ((item->contentType == XML_SCHEMA_CONTENT_SIMPLE) || \
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000257 (item->contentType == XML_SCHEMA_CONTENT_BASIC))
258
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000259#define WXS_HAS_MIXED_CONTENT(item) \
260 (item->contentType == XML_SCHEMA_CONTENT_MIXED)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000261
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000262#define WXS_EMPTIABLE(t) \
263 (xmlSchemaIsParticleEmptiable(WXS_PTC_CAST (t)->subtypes))
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +0000264
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000265#define WXS_TYPE_CONTENTTYPE(t) (t)->subtypes
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000266
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000267#define WXS_TYPE_PARTICLE(t) WXS_PTC_CAST (t)->subtypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000268
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000269#define WXS_TYPE_PARTICLE_TERM(t) WXS_PARTICLE_TERM(WXS_TYPE_PARTICLE(t))
270/*
271* Macros for exclusively for simple types.
272*/
273#define WXS_LIST_ITEMTYPE(t) (t)->subtypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000274
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000275#define WXS_IS_ATOMIC(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_ATOMIC)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000276
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000277#define WXS_IS_LIST(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_LIST)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000278
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000279#define WXS_IS_UNION(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_UNION)
280/*
281* Misc parser context macros.
282*/
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000283#define WXS_CONSTRUCTOR(ctx) (ctx)->constructor
284
285#define WXS_HAS_BUCKETS(ctx) \
286( (WXS_CONSTRUCTOR((ctx))->buckets != NULL) && \
287(WXS_CONSTRUCTOR((ctx))->buckets->nbItems > 0) )
288
289#define WXS_SUBST_GROUPS(ctx) WXS_CONSTRUCTOR((ctx))->substGroups
290
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000291#define WXS_BUCKET(ctx) WXS_CONSTRUCTOR((ctx))->bucket
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000292
293#define WXS_SCHEMA(ctx) (ctx)->schema
294
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000295#define WXS_ADD_LOCAL(ctx, item) \
296 xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->locals), 10, item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000297
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000298#define WXS_ADD_GLOBAL(ctx, item) \
299 xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->globals), 5, item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000300
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000301#define WXS_ADD_PENDING(ctx, item) \
302 xmlSchemaAddItemSize(&((ctx)->constructor->pending), 10, item)
303/*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000304* xmlSchemaItemList macros.
305*/
306#define WXS_ILIST_IS_EMPTY(l) ((l == NULL) || ((l)->nbItems == 0))
307/*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000308* Misc macros.
309*/
310#define IS_SCHEMA(node, type) \
311 ((node != NULL) && (node->ns != NULL) && \
312 (xmlStrEqual(node->name, (const xmlChar *) type)) && \
313 (xmlStrEqual(node->ns->href, xmlSchemaNs)))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000314
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000315#define FREE_AND_NULL(str) if ((str) != NULL) { xmlFree((xmlChar *) (str)); str = NULL; }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000316
317/*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000318* Since we put the default/fixed values into the dict, we can
319* use pointer comparison for those values.
320* REMOVED: (xmlStrEqual((v1), (v2)))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000321*/
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000322#define WXS_ARE_DEFAULT_STR_EQUAL(v1, v2) ((v1) == (v2))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000323
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000324#define INODE_NILLED(item) (item->flags & XML_SCHEMA_ELEM_INFO_NILLED)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000325
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000326#define CAN_PARSE_SCHEMA(b) (((b)->doc != NULL) && ((b)->parsed == 0))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000327
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000328#define HFAILURE if (res == -1) goto exit_failure;
329
330#define HERROR if (res != 0) goto exit_error;
331
332#define HSTOP(ctx) if ((ctx)->stop) goto exit;
333/*
334* Some flags used for various schema constraints.
335*/
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +0000336#define SUBSET_RESTRICTION 1<<0
337#define SUBSET_EXTENSION 1<<1
338#define SUBSET_SUBSTITUTION 1<<2
339#define SUBSET_LIST 1<<3
340#define SUBSET_UNION 1<<4
341
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000342typedef struct _xmlSchemaNodeInfo xmlSchemaNodeInfo;
343typedef xmlSchemaNodeInfo *xmlSchemaNodeInfoPtr;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000344
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000345typedef struct _xmlSchemaItemList xmlSchemaItemList;
346typedef xmlSchemaItemList *xmlSchemaItemListPtr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000347struct _xmlSchemaItemList {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000348 void **items; /* used for dynamic addition of schemata */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000349 int nbItems; /* used for dynamic addition of schemata */
350 int sizeItems; /* used for dynamic addition of schemata */
351};
352
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000353#define XML_SCHEMA_CTXT_PARSER 1
354#define XML_SCHEMA_CTXT_VALIDATOR 2
355
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000356typedef struct _xmlSchemaAbstractCtxt xmlSchemaAbstractCtxt;
357typedef xmlSchemaAbstractCtxt *xmlSchemaAbstractCtxtPtr;
358struct _xmlSchemaAbstractCtxt {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000359 int type; /* E.g. XML_SCHEMA_CTXT_VALIDATOR */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000360};
361
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000362typedef struct _xmlSchemaBucket xmlSchemaBucket;
363typedef xmlSchemaBucket *xmlSchemaBucketPtr;
364
365#define XML_SCHEMA_SCHEMA_MAIN 0
366#define XML_SCHEMA_SCHEMA_IMPORT 1
367#define XML_SCHEMA_SCHEMA_INCLUDE 2
368#define XML_SCHEMA_SCHEMA_REDEFINE 3
369
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000370/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000371 * xmlSchemaSchemaRelation:
372 *
373 * Used to create a graph of schema relationships.
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000374 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000375typedef struct _xmlSchemaSchemaRelation xmlSchemaSchemaRelation;
376typedef xmlSchemaSchemaRelation *xmlSchemaSchemaRelationPtr;
377struct _xmlSchemaSchemaRelation {
378 xmlSchemaSchemaRelationPtr next;
379 int type; /* E.g. XML_SCHEMA_SCHEMA_IMPORT */
380 const xmlChar *importNamespace;
381 xmlSchemaBucketPtr bucket;
382};
383
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000384#define XML_SCHEMA_BUCKET_MARKED 1<<0
385#define XML_SCHEMA_BUCKET_COMPS_ADDED 1<<1
386
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000387struct _xmlSchemaBucket {
388 int type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000389 int flags;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000390 const xmlChar *schemaLocation;
391 const xmlChar *origTargetNamespace;
392 const xmlChar *targetNamespace;
393 xmlDocPtr doc;
394 xmlSchemaSchemaRelationPtr relations;
395 int located;
396 int parsed;
397 int imported;
398 int preserveDoc;
399 xmlSchemaItemListPtr globals; /* Global components. */
400 xmlSchemaItemListPtr locals; /* Local components. */
401};
402
403/**
404 * xmlSchemaImport:
405 * (extends xmlSchemaBucket)
406 *
407 * Reflects a schema. Holds some information
408 * about the schema and its toplevel components. Duplicate
409 * toplevel components are not checked at this level.
410 */
411typedef struct _xmlSchemaImport xmlSchemaImport;
412typedef xmlSchemaImport *xmlSchemaImportPtr;
413struct _xmlSchemaImport {
414 int type; /* Main OR import OR include. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000415 int flags;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000416 const xmlChar *schemaLocation; /* The URI of the schema document. */
417 /* For chameleon includes, @origTargetNamespace will be NULL */
418 const xmlChar *origTargetNamespace;
419 /*
420 * For chameleon includes, @targetNamespace will be the
421 * targetNamespace of the including schema.
422 */
423 const xmlChar *targetNamespace;
424 xmlDocPtr doc; /* The schema node-tree. */
425 /* @relations will hold any included/imported/redefined schemas. */
426 xmlSchemaSchemaRelationPtr relations;
427 int located;
428 int parsed;
429 int imported;
430 int preserveDoc;
431 xmlSchemaItemListPtr globals;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000432 xmlSchemaItemListPtr locals;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000433 /* The imported schema. */
434 xmlSchemaPtr schema;
435};
436
437/*
438* (extends xmlSchemaBucket)
439*/
440typedef struct _xmlSchemaInclude xmlSchemaInclude;
441typedef xmlSchemaInclude *xmlSchemaIncludePtr;
442struct _xmlSchemaInclude {
443 int type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000444 int flags;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +0000445 const xmlChar *schemaLocation;
446 const xmlChar *origTargetNamespace;
447 const xmlChar *targetNamespace;
448 xmlDocPtr doc;
449 xmlSchemaSchemaRelationPtr relations;
450 int located;
451 int parsed;
452 int imported;
453 int preserveDoc;
454 xmlSchemaItemListPtr globals; /* Global components. */
455 xmlSchemaItemListPtr locals; /* Local components. */
456
457 /* The owning main or import schema bucket. */
458 xmlSchemaImportPtr ownerImport;
459};
460
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000461/**
462 * xmlSchemaBasicItem:
463 *
464 * The abstract base type for schema components.
465 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000466typedef struct _xmlSchemaBasicItem xmlSchemaBasicItem;
467typedef xmlSchemaBasicItem *xmlSchemaBasicItemPtr;
468struct _xmlSchemaBasicItem {
469 xmlSchemaTypeType type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000470};
471
472/**
473 * xmlSchemaAnnotItem:
474 *
475 * The abstract base type for annotated schema components.
476 * (Extends xmlSchemaBasicItem)
477 */
478typedef struct _xmlSchemaAnnotItem xmlSchemaAnnotItem;
479typedef xmlSchemaAnnotItem *xmlSchemaAnnotItemPtr;
480struct _xmlSchemaAnnotItem {
481 xmlSchemaTypeType type;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000482 xmlSchemaAnnotPtr annot;
483};
484
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000485/**
486 * xmlSchemaTreeItem:
487 *
488 * The abstract base type for tree-like structured schema components.
489 * (Extends xmlSchemaAnnotItem)
490 */
491typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem;
492typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr;
493struct _xmlSchemaTreeItem {
494 xmlSchemaTypeType type;
495 xmlSchemaAnnotPtr annot;
496 xmlSchemaTreeItemPtr next;
497 xmlSchemaTreeItemPtr children;
498};
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000499
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000500
501#define XML_SCHEMA_ATTR_USE_FIXED 1<<0
502/**
503 * xmlSchemaAttributeUsePtr:
504 *
505 * The abstract base type for tree-like structured schema components.
506 * (Extends xmlSchemaTreeItem)
507 */
508typedef struct _xmlSchemaAttributeUse xmlSchemaAttributeUse;
509typedef xmlSchemaAttributeUse *xmlSchemaAttributeUsePtr;
510struct _xmlSchemaAttributeUse {
511 xmlSchemaTypeType type;
512 xmlSchemaAnnotPtr annot;
513 xmlSchemaAttributeUsePtr next; /* The next attr. use. */
514 /*
515 * The attr. decl. OR a QName-ref. to an attr. decl. OR
516 * a QName-ref. to an attribute group definition.
517 */
518 xmlSchemaAttributePtr attrDecl;
519
520 int flags;
521 xmlNodePtr node;
522 int occurs; /* required, optional */
523 const xmlChar * defValue;
524 xmlSchemaValPtr defVal;
525};
526
527/**
528 * xmlSchemaAttributeUseProhibPtr:
529 *
530 * A helper component to reflect attribute prohibitions.
531 * (Extends xmlSchemaBasicItem)
532 */
533typedef struct _xmlSchemaAttributeUseProhib xmlSchemaAttributeUseProhib;
534typedef xmlSchemaAttributeUseProhib *xmlSchemaAttributeUseProhibPtr;
535struct _xmlSchemaAttributeUseProhib {
536 xmlSchemaTypeType type; /* == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB */
537 xmlNodePtr node;
538 const xmlChar *name;
539 const xmlChar *targetNamespace;
540 int isRef;
541};
542
543/**
544 * xmlSchemaRedef:
545 */
546typedef struct _xmlSchemaRedef xmlSchemaRedef;
547typedef xmlSchemaRedef *xmlSchemaRedefPtr;
548struct _xmlSchemaRedef {
549 xmlSchemaRedefPtr next;
550 xmlSchemaBasicItemPtr item; /* The redefining component. */
551 xmlSchemaBasicItemPtr reference; /* The referencing component. */
552 xmlSchemaBasicItemPtr target; /* The to-be-redefined component. */
553 const xmlChar *refName; /* The name of the to-be-redefined component. */
554 const xmlChar *refTargetNs; /* The target namespace of the
555 to-be-redefined comp. */
556 xmlSchemaBucketPtr targetBucket; /* The redefined schema. */
557};
558
559/**
560 * xmlSchemaConstructionCtxt:
561 */
562typedef struct _xmlSchemaConstructionCtxt xmlSchemaConstructionCtxt;
563typedef xmlSchemaConstructionCtxt *xmlSchemaConstructionCtxtPtr;
564struct _xmlSchemaConstructionCtxt {
565 xmlSchemaPtr mainSchema; /* The main schema. */
566 xmlSchemaBucketPtr mainBucket; /* The main schema bucket */
567 xmlDictPtr dict;
568 xmlSchemaItemListPtr buckets; /* List of schema buckets. */
569 /* xmlSchemaItemListPtr relations; */ /* List of schema relations. */
570 xmlSchemaBucketPtr bucket; /* The current schema bucket */
571 xmlSchemaItemListPtr pending; /* All Components of all schemas that
572 need to be fixed. */
573 xmlHashTablePtr substGroups;
574 xmlSchemaRedefPtr redefs;
575 xmlSchemaRedefPtr lastRedef;
576};
577
578#define XML_SCHEMAS_PARSE_ERROR 1
579#define SCHEMAS_PARSE_OPTIONS XML_PARSE_NOENT
580
581struct _xmlSchemaParserCtxt {
582 int type;
583 void *userData; /* user specific data block */
584 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
585 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
586 xmlSchemaValidError err;
587 int nberrors;
588 xmlStructuredErrorFunc serror;
589
590 xmlSchemaConstructionCtxtPtr constructor;
591 int ownsConstructor; /* TODO: Move this to parser *flags*. */
592
593 /* xmlSchemaPtr topschema; */
594 /* xmlHashTablePtr namespaces; */
595
596 xmlSchemaPtr schema; /* The main schema in use */
597 int counter;
598
599 const xmlChar *URL;
600 xmlDocPtr doc;
601 int preserve; /* Whether the doc should be freed */
602
603 const char *buffer;
604 int size;
605
606 /*
607 * Used to build complex element content models
608 */
609 xmlAutomataPtr am;
610 xmlAutomataStatePtr start;
611 xmlAutomataStatePtr end;
612 xmlAutomataStatePtr state;
613
614 xmlDictPtr dict; /* dictionnary for interned string names */
615 xmlSchemaTypePtr ctxtType; /* The current context simple/complex type */
616 int options;
617 xmlSchemaValidCtxtPtr vctxt;
618 int isS4S;
619 int isRedefine;
620 int xsiAssemble;
621 int stop; /* If the parser should stop; i.e. a critical error. */
622 const xmlChar *targetNamespace;
623 xmlSchemaBucketPtr redefined; /* The schema to be redefined. */
624
625 xmlSchemaRedefPtr redef; /* Used for redefinitions. */
626 int redefCounter; /* Used for redefinitions. */
627 xmlSchemaItemListPtr attrProhibs;
628};
629
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000630/**
631 * xmlSchemaQNameRef:
632 *
633 * A component reference item (not a schema component)
634 * (Extends xmlSchemaBasicItem)
635 */
636typedef struct _xmlSchemaQNameRef xmlSchemaQNameRef;
637typedef xmlSchemaQNameRef *xmlSchemaQNameRefPtr;
638struct _xmlSchemaQNameRef {
639 xmlSchemaTypeType type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000640 xmlSchemaBasicItemPtr item; /* The resolved referenced item. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000641 xmlSchemaTypeType itemType;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000642 const xmlChar *name;
643 const xmlChar *targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000644 xmlNodePtr node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000645};
646
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000647/**
648 * xmlSchemaParticle:
649 *
650 * A particle component.
651 * (Extends xmlSchemaTreeItem)
652 */
653typedef struct _xmlSchemaParticle xmlSchemaParticle;
654typedef xmlSchemaParticle *xmlSchemaParticlePtr;
655struct _xmlSchemaParticle {
656 xmlSchemaTypeType type;
657 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000658 xmlSchemaTreeItemPtr next; /* next particle */
659 xmlSchemaTreeItemPtr children; /* the "term" (e.g. a model group,
660 a group definition, a XML_SCHEMA_EXTRA_QNAMEREF (if a reference),
661 etc.) */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000662 int minOccurs;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000663 int maxOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000664 xmlNodePtr node;
665};
666
667/**
668 * xmlSchemaModelGroup:
669 *
670 * A model group component.
671 * (Extends xmlSchemaTreeItem)
672 */
673typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup;
674typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr;
675struct _xmlSchemaModelGroup {
676 xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_SEQUENCE, XML_SCHEMA_TYPE_CHOICE, XML_SCHEMA_TYPE_ALL */
677 xmlSchemaAnnotPtr annot;
678 xmlSchemaTreeItemPtr next; /* not used */
679 xmlSchemaTreeItemPtr children; /* first particle (OR "element decl" OR "wildcard") */
680 xmlNodePtr node;
681};
682
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000683#define XML_SCHEMA_MODEL_GROUP_DEF_MARKED 1<<0
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000684#define XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED 1<<1
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000685/**
686 * xmlSchemaModelGroupDef:
687 *
688 * A model group definition component.
689 * (Extends xmlSchemaTreeItem)
690 */
691typedef struct _xmlSchemaModelGroupDef xmlSchemaModelGroupDef;
692typedef xmlSchemaModelGroupDef *xmlSchemaModelGroupDefPtr;
693struct _xmlSchemaModelGroupDef {
694 xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_GROUP */
695 xmlSchemaAnnotPtr annot;
696 xmlSchemaTreeItemPtr next; /* not used */
697 xmlSchemaTreeItemPtr children; /* the "model group" */
698 const xmlChar *name;
699 const xmlChar *targetNamespace;
700 xmlNodePtr node;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +0000701 int flags;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000702};
703
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000704typedef struct _xmlSchemaIDC xmlSchemaIDC;
705typedef xmlSchemaIDC *xmlSchemaIDCPtr;
706
707/**
708 * xmlSchemaIDCSelect:
709 *
710 * The identity-constraint "field" and "selector" item, holding the
711 * XPath expression.
712 */
713typedef struct _xmlSchemaIDCSelect xmlSchemaIDCSelect;
714typedef xmlSchemaIDCSelect *xmlSchemaIDCSelectPtr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000715struct _xmlSchemaIDCSelect {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000716 xmlSchemaIDCSelectPtr next;
717 xmlSchemaIDCPtr idc;
718 int index; /* an index position if significant for IDC key-sequences */
719 const xmlChar *xpath; /* the XPath expression */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000720 void *xpathComp; /* the compiled XPath expression */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000721};
722
723/**
724 * xmlSchemaIDC:
725 *
726 * The identity-constraint definition component.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000727 * (Extends xmlSchemaAnnotItem)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000728 */
729
730struct _xmlSchemaIDC {
731 xmlSchemaTypeType type;
732 xmlSchemaAnnotPtr annot;
733 xmlSchemaIDCPtr next;
734 xmlNodePtr node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000735 const xmlChar *name;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000736 const xmlChar *targetNamespace;
737 xmlSchemaIDCSelectPtr selector;
738 xmlSchemaIDCSelectPtr fields;
739 int nbFields;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000740 xmlSchemaQNameRefPtr ref;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000741};
742
743/**
744 * xmlSchemaIDCAug:
745 *
746 * The augmented IDC information used for validation.
747 */
748typedef struct _xmlSchemaIDCAug xmlSchemaIDCAug;
749typedef xmlSchemaIDCAug *xmlSchemaIDCAugPtr;
750struct _xmlSchemaIDCAug {
751 xmlSchemaIDCAugPtr next; /* next in a list */
752 xmlSchemaIDCPtr def; /* the IDC definition */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000753 int keyrefDepth; /* the lowest tree level to which IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000754 tables need to be bubbled upwards */
755};
756
757/**
758 * xmlSchemaPSVIIDCKeySequence:
759 *
760 * The key sequence of a node table item.
761 */
762typedef struct _xmlSchemaPSVIIDCKey xmlSchemaPSVIIDCKey;
763typedef xmlSchemaPSVIIDCKey *xmlSchemaPSVIIDCKeyPtr;
764struct _xmlSchemaPSVIIDCKey {
765 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000766 xmlSchemaValPtr val;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000767};
768
769/**
770 * xmlSchemaPSVIIDCNode:
771 *
772 * The node table item of a node table.
773 */
774typedef struct _xmlSchemaPSVIIDCNode xmlSchemaPSVIIDCNode;
775typedef xmlSchemaPSVIIDCNode *xmlSchemaPSVIIDCNodePtr;
776struct _xmlSchemaPSVIIDCNode {
777 xmlNodePtr node;
778 xmlSchemaPSVIIDCKeyPtr *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000779 int nodeLine;
780 int nodeQNameID;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000781
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000782};
783
784/**
785 * xmlSchemaPSVIIDCBinding:
786 *
787 * The identity-constraint binding item of the [identity-constraint table].
788 */
789typedef struct _xmlSchemaPSVIIDCBinding xmlSchemaPSVIIDCBinding;
790typedef xmlSchemaPSVIIDCBinding *xmlSchemaPSVIIDCBindingPtr;
791struct _xmlSchemaPSVIIDCBinding {
792 xmlSchemaPSVIIDCBindingPtr next; /* next binding of a specific node */
793 xmlSchemaIDCPtr definition; /* the IDC definition */
794 xmlSchemaPSVIIDCNodePtr *nodeTable; /* array of key-sequences */
795 int nbNodes; /* number of entries in the node table */
796 int sizeNodes; /* size of the node table */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000797 xmlSchemaItemListPtr dupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000798};
799
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000800
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000801#define XPATH_STATE_OBJ_TYPE_IDC_SELECTOR 1
802#define XPATH_STATE_OBJ_TYPE_IDC_FIELD 2
803
804#define XPATH_STATE_OBJ_MATCHES -2
805#define XPATH_STATE_OBJ_BLOCKED -3
806
807typedef struct _xmlSchemaIDCMatcher xmlSchemaIDCMatcher;
808typedef xmlSchemaIDCMatcher *xmlSchemaIDCMatcherPtr;
809
810/**
811 * xmlSchemaIDCStateObj:
812 *
813 * The state object used to evaluate XPath expressions.
814 */
815typedef struct _xmlSchemaIDCStateObj xmlSchemaIDCStateObj;
816typedef xmlSchemaIDCStateObj *xmlSchemaIDCStateObjPtr;
817struct _xmlSchemaIDCStateObj {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000818 int type;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000819 xmlSchemaIDCStateObjPtr next; /* next if in a list */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000820 int depth; /* depth of creation */
821 int *history; /* list of (depth, state-id) tuples */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000822 int nbHistory;
823 int sizeHistory;
824 xmlSchemaIDCMatcherPtr matcher; /* the correspondent field/selector
825 matcher */
826 xmlSchemaIDCSelectPtr sel;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +0000827 void *xpathCtxt;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000828};
829
830#define IDC_MATCHER 0
831
832/**
833 * xmlSchemaIDCMatcher:
834 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000835 * Used to evaluate IDC selectors (and fields).
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000836 */
837struct _xmlSchemaIDCMatcher {
838 int type;
839 int depth; /* the tree depth at creation time */
840 xmlSchemaIDCMatcherPtr next; /* next in the list */
841 xmlSchemaIDCAugPtr aidc; /* the augmented IDC item */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000842 int idcType;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000843 xmlSchemaPSVIIDCKeyPtr **keySeqs; /* the key-sequences of the target
844 elements */
845 int sizeKeySeqs;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000846 int targetDepth;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000847 xmlSchemaItemListPtr targets; /* list of target-node
848 (xmlSchemaPSVIIDCNodePtr) entries */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000849};
850
851/*
852* Element info flags.
853*/
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000854#define XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES 1<<0
855#define XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES 1<<1
856#define XML_SCHEMA_ELEM_INFO_NILLED 1<<2
857#define XML_SCHEMA_ELEM_INFO_LOCAL_TYPE 1<<3
858
859#define XML_SCHEMA_NODE_INFO_VALUE_NEEDED 1<<4
860#define XML_SCHEMA_ELEM_INFO_EMPTY 1<<5
861#define XML_SCHEMA_ELEM_INFO_HAS_CONTENT 1<<6
862
863#define XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT 1<<7
864#define XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT 1<<8
865#define XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED 1<<9
866#define XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE 1<<10
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000867
868/**
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000869 * xmlSchemaNodeInfo:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000870 *
871 * Holds information of an element node.
872 */
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000873struct _xmlSchemaNodeInfo {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000874 int nodeType;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000875 xmlNodePtr node;
876 int nodeLine;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000877 const xmlChar *localName;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000878 const xmlChar *nsName;
879 const xmlChar *value;
880 xmlSchemaValPtr val; /* the pre-computed value if any */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000881 xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000882
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +0000883 int flags; /* combination of node info flags */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000884
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000885 int valNeeded;
886 int normVal;
887
888 xmlSchemaElementPtr decl; /* the element/attribute declaration */
889 int depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000890 xmlSchemaPSVIIDCBindingPtr idcTable; /* the table of PSVI IDC bindings
891 for the scope element*/
892 xmlSchemaIDCMatcherPtr idcMatchers; /* the IDC matchers for the scope
893 element */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000894 xmlRegExecCtxtPtr regexCtxt;
895
896 const xmlChar **nsBindings; /* Namespace bindings on this element */
897 int nbNsBindings;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000898 int sizeNsBindings;
899
900 int hasKeyrefs;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +0000901 int appliedXPath; /* Indicates that an XPath has been applied. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000902};
903
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000904#define XML_SCHEMAS_ATTR_UNKNOWN 1
905#define XML_SCHEMAS_ATTR_ASSESSED 2
906#define XML_SCHEMAS_ATTR_PROHIBITED 3
907#define XML_SCHEMAS_ATTR_ERR_MISSING 4
908#define XML_SCHEMAS_ATTR_INVALID_VALUE 5
909#define XML_SCHEMAS_ATTR_ERR_NO_TYPE 6
910#define XML_SCHEMAS_ATTR_ERR_FIXED_VALUE 7
911#define XML_SCHEMAS_ATTR_DEFAULT 8
912#define XML_SCHEMAS_ATTR_VALIDATE_VALUE 9
913#define XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL 10
914#define XML_SCHEMAS_ATTR_HAS_ATTR_USE 11
915#define XML_SCHEMAS_ATTR_HAS_ATTR_DECL 12
916#define XML_SCHEMAS_ATTR_WILD_SKIP 13
917#define XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL 14
918#define XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID 15
919#define XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID 16
920#define XML_SCHEMAS_ATTR_META 17
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000921/*
922* @metaType values of xmlSchemaAttrInfo.
923*/
924#define XML_SCHEMA_ATTR_INFO_META_XSI_TYPE 1
925#define XML_SCHEMA_ATTR_INFO_META_XSI_NIL 2
926#define XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC 3
927#define XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC 4
928#define XML_SCHEMA_ATTR_INFO_META_XMLNS 5
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000929
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000930typedef struct _xmlSchemaAttrInfo xmlSchemaAttrInfo;
931typedef xmlSchemaAttrInfo *xmlSchemaAttrInfoPtr;
932struct _xmlSchemaAttrInfo {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000933 int nodeType;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +0000934 xmlNodePtr node;
935 int nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000936 const xmlChar *localName;
937 const xmlChar *nsName;
938 const xmlChar *value;
939 xmlSchemaValPtr val; /* the pre-computed value if any */
940 xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
941 int flags; /* combination of node info flags */
942
943 xmlSchemaAttributePtr decl; /* the attribute declaration */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000944 xmlSchemaAttributeUsePtr use; /* the attribute use */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000945 int state;
946 int metaType;
947 const xmlChar *vcValue; /* the value constraint value */
948 xmlSchemaNodeInfoPtr parent;
949};
950
951
952#define XML_SCHEMA_VALID_CTXT_FLAG_STREAM 1
Daniel Veillard4255d502002-04-16 15:50:10 +0000953/**
954 * xmlSchemaValidCtxt:
955 *
956 * A Schemas validation context
957 */
Daniel Veillard4255d502002-04-16 15:50:10 +0000958struct _xmlSchemaValidCtxt {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000959 int type;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000960 void *userData; /* user specific data block */
961 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000962 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000963 xmlStructuredErrorFunc serror;
Daniel Veillard4255d502002-04-16 15:50:10 +0000964
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000965 xmlSchemaPtr schema; /* The schema in use */
966 xmlDocPtr doc;
Daniel Veillard4255d502002-04-16 15:50:10 +0000967 xmlParserInputBufferPtr input;
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000968 xmlCharEncoding enc;
969 xmlSAXHandlerPtr sax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000970 xmlParserCtxtPtr parserCtxt;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +0000971 void *user_data; /* TODO: What is this for? */
Daniel Veillard4255d502002-04-16 15:50:10 +0000972
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000973 int err;
974 int nberrors;
Daniel Veillard4255d502002-04-16 15:50:10 +0000975
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000976 xmlNodePtr node;
977 xmlNodePtr cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000978 /* xmlSchemaTypePtr type; */
Daniel Veillard4255d502002-04-16 15:50:10 +0000979
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000980 xmlRegExecCtxtPtr regexp;
981 xmlSchemaValPtr value;
Daniel Veillard4255d502002-04-16 15:50:10 +0000982
Daniel Veillardc0826a72004-08-10 14:17:33 +0000983 int valueWS;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +0000984 int options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000985 xmlNodePtr validationRoot;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +0000986 xmlSchemaParserCtxtPtr pctxt;
987 int xsiAssemble;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000988
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000989 int depth;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +0000990 xmlSchemaNodeInfoPtr *elemInfos; /* array of element informations */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000991 int sizeElemInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000992 xmlSchemaNodeInfoPtr inode; /* the current element information */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +0000993
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000994 xmlSchemaIDCAugPtr aidcs; /* a list of augmented IDC informations */
995
996 xmlSchemaIDCStateObjPtr xpathStates; /* first active state object. */
997 xmlSchemaIDCStateObjPtr xpathStatePool; /* first stored state object. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +0000998
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +0000999 xmlSchemaPSVIIDCNodePtr *idcNodes; /* list of all IDC node-table entries*/
1000 int nbIdcNodes;
1001 int sizeIdcNodes;
1002
1003 xmlSchemaPSVIIDCKeyPtr *idcKeys; /* list of all IDC node-table entries */
1004 int nbIdcKeys;
1005 int sizeIdcKeys;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001006
1007 int flags;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001008
1009 xmlDictPtr dict;
1010
Daniel Veillard39e5c892005-07-03 22:48:50 +00001011#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001012 xmlTextReaderPtr reader;
Daniel Veillard39e5c892005-07-03 22:48:50 +00001013#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001014
1015 xmlSchemaAttrInfoPtr *attrInfos;
1016 int nbAttrInfos;
1017 int sizeAttrInfos;
1018
1019 int skipDepth;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00001020 xmlSchemaItemListPtr nodeQNames;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00001021 int hasKeyrefs;
1022 int createIDCNodeTables;
1023 int psviExposeIDCNodeTables;
Daniel Veillard4255d502002-04-16 15:50:10 +00001024};
1025
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00001026/**
1027 * xmlSchemaSubstGroup:
1028 *
1029 *
1030 */
1031typedef struct _xmlSchemaSubstGroup xmlSchemaSubstGroup;
1032typedef xmlSchemaSubstGroup *xmlSchemaSubstGroupPtr;
1033struct _xmlSchemaSubstGroup {
1034 xmlSchemaElementPtr head;
1035 xmlSchemaItemListPtr members;
1036};
1037
Daniel Veillard4255d502002-04-16 15:50:10 +00001038/************************************************************************
1039 * *
1040 * Some predeclarations *
1041 * *
1042 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001043
Daniel Veillardbd2904b2003-11-25 15:38:59 +00001044static int xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt,
1045 xmlSchemaPtr schema,
1046 xmlNodePtr node);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00001047static int xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr ctxt,
1048 xmlSchemaPtr schema,
1049 xmlNodePtr node);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001050static int
1051xmlSchemaTypeFixup(xmlSchemaTypePtr type,
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +00001052 xmlSchemaAbstractCtxtPtr ctxt);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00001053static const xmlChar *
Daniel Veillard01fa6152004-06-29 17:04:39 +00001054xmlSchemaFacetTypeToString(xmlSchemaTypeType type);
1055static int
William M. Brack2f2a6632004-08-20 23:09:47 +00001056xmlSchemaParseImport(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1057 xmlNodePtr node);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001058static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001059xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
1060 xmlSchemaParserCtxtPtr ctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +00001061static void
1062xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001063static xmlSchemaWhitespaceValueType
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00001064xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001065static xmlSchemaTreeItemPtr
1066xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1067 xmlNodePtr node, xmlSchemaTypeType type,
1068 int withParticle);
1069static const xmlChar *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001070xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00001071static xmlSchemaTypeLinkPtr
1072xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001073static void
1074xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
1075 const char *funcName,
1076 const char *message);
1077static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +00001078xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001079 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001080 xmlSchemaTypePtr baseType,
1081 int subset);
1082static void
1083xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001084 xmlSchemaParserCtxtPtr ctxt);
1085static void
1086xmlSchemaComponentListFree(xmlSchemaItemListPtr list);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001087static xmlSchemaQNameRefPtr
1088xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
1089 xmlSchemaPtr schema,
1090 xmlNodePtr node);
William M. Brack87640d52004-04-17 14:58:15 +00001091
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001092/************************************************************************
1093 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001094 * Helper functions *
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001095 * *
1096 ************************************************************************/
1097
1098/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001099 * xmlSchemaItemTypeToStr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001100 * @type: the type of the schema item
1101 *
1102 * Returns the component name of a schema item.
1103 */
1104static const xmlChar *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001105xmlSchemaItemTypeToStr(xmlSchemaTypeType type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001106{
1107 switch (type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001108 case XML_SCHEMA_TYPE_BASIC:
1109 return(BAD_CAST "simple type definition");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001110 case XML_SCHEMA_TYPE_SIMPLE:
1111 return(BAD_CAST "simple type definition");
1112 case XML_SCHEMA_TYPE_COMPLEX:
1113 return(BAD_CAST "complex type definition");
1114 case XML_SCHEMA_TYPE_ELEMENT:
1115 return(BAD_CAST "element declaration");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001116 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1117 return(BAD_CAST "attribute use");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001118 case XML_SCHEMA_TYPE_ATTRIBUTE:
1119 return(BAD_CAST "attribute declaration");
1120 case XML_SCHEMA_TYPE_GROUP:
1121 return(BAD_CAST "model group definition");
1122 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1123 return(BAD_CAST "attribute group definition");
1124 case XML_SCHEMA_TYPE_NOTATION:
1125 return(BAD_CAST "notation declaration");
1126 case XML_SCHEMA_TYPE_SEQUENCE:
1127 return(BAD_CAST "model group (sequence)");
1128 case XML_SCHEMA_TYPE_CHOICE:
1129 return(BAD_CAST "model group (choice)");
1130 case XML_SCHEMA_TYPE_ALL:
1131 return(BAD_CAST "model group (all)");
1132 case XML_SCHEMA_TYPE_PARTICLE:
1133 return(BAD_CAST "particle");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001134 case XML_SCHEMA_TYPE_IDC_UNIQUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001135 return(BAD_CAST "unique identity-constraint");
1136 /* return(BAD_CAST "IDC (unique)"); */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001137 case XML_SCHEMA_TYPE_IDC_KEY:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001138 return(BAD_CAST "key identity-constraint");
1139 /* return(BAD_CAST "IDC (key)"); */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001140 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001141 return(BAD_CAST "keyref identity-constraint");
1142 /* return(BAD_CAST "IDC (keyref)"); */
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00001143 case XML_SCHEMA_TYPE_ANY:
1144 return(BAD_CAST "wildcard (any)");
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00001145 case XML_SCHEMA_EXTRA_QNAMEREF:
1146 return(BAD_CAST "[helper component] QName reference");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001147 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
1148 return(BAD_CAST "[helper component] attribute use prohibition");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001149 default:
1150 return(BAD_CAST "Not a schema component");
1151 }
1152}
1153
1154/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001155 * xmlSchemaGetComponentTypeStr:
1156 * @type: the type of the schema item
1157 *
1158 * Returns the component name of a schema item.
1159 */
1160static const xmlChar *
1161xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item)
1162{
1163 switch (item->type) {
1164 case XML_SCHEMA_TYPE_BASIC:
1165 if (WXS_IS_COMPLEX(WXS_TYPE_CAST item))
1166 return(BAD_CAST "complex type definition");
1167 else
1168 return(BAD_CAST "simple type definition");
1169 default:
1170 return(xmlSchemaItemTypeToStr(item->type));
1171 }
1172}
1173
1174/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001175 * xmlSchemaGetComponentNode:
1176 * @item: a schema component
1177 *
1178 * Returns node associated with the schema component.
1179 * NOTE that such a node need not be available; plus, a component's
1180 * node need not to reflect the component directly, since there is no
1181 * one-to-one relationship between the XML Schema representation and
1182 * the component representation.
1183 */
1184static xmlNodePtr
1185xmlSchemaGetComponentNode(xmlSchemaBasicItemPtr item)
1186{
1187 switch (item->type) {
1188 case XML_SCHEMA_TYPE_ELEMENT:
1189 return (((xmlSchemaElementPtr) item)->node);
1190 case XML_SCHEMA_TYPE_ATTRIBUTE:
1191 return (((xmlSchemaAttributePtr) item)->node);
1192 case XML_SCHEMA_TYPE_COMPLEX:
1193 case XML_SCHEMA_TYPE_SIMPLE:
1194 return (((xmlSchemaTypePtr) item)->node);
1195 case XML_SCHEMA_TYPE_ANY:
1196 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1197 return (((xmlSchemaWildcardPtr) item)->node);
1198 case XML_SCHEMA_TYPE_PARTICLE:
1199 return (((xmlSchemaParticlePtr) item)->node);
1200 case XML_SCHEMA_TYPE_SEQUENCE:
1201 case XML_SCHEMA_TYPE_CHOICE:
1202 case XML_SCHEMA_TYPE_ALL:
1203 return (((xmlSchemaModelGroupPtr) item)->node);
1204 case XML_SCHEMA_TYPE_GROUP:
1205 return (((xmlSchemaModelGroupDefPtr) item)->node);
1206 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1207 return (((xmlSchemaAttributeGroupPtr) item)->node);
1208 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1209 case XML_SCHEMA_TYPE_IDC_KEY:
1210 case XML_SCHEMA_TYPE_IDC_KEYREF:
1211 return (((xmlSchemaIDCPtr) item)->node);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001212 case XML_SCHEMA_EXTRA_QNAMEREF:
1213 return(((xmlSchemaQNameRefPtr) item)->node);
1214 /* TODO: What to do with NOTATIONs?
1215 case XML_SCHEMA_TYPE_NOTATION:
1216 return (((xmlSchemaNotationPtr) item)->node);
1217 */
1218 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1219 return (((xmlSchemaAttributeUsePtr) item)->node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001220 default:
1221 return (NULL);
1222 }
1223}
1224
1225#if 0
1226/**
1227 * xmlSchemaGetNextComponent:
1228 * @item: a schema component
1229 *
1230 * Returns the next sibling of the schema component.
1231 */
1232static xmlSchemaBasicItemPtr
1233xmlSchemaGetNextComponent(xmlSchemaBasicItemPtr item)
1234{
1235 switch (item->type) {
1236 case XML_SCHEMA_TYPE_ELEMENT:
1237 return ((xmlSchemaBasicItemPtr) ((xmlSchemaElementPtr) item)->next);
1238 case XML_SCHEMA_TYPE_ATTRIBUTE:
1239 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributePtr) item)->next);
1240 case XML_SCHEMA_TYPE_COMPLEX:
1241 case XML_SCHEMA_TYPE_SIMPLE:
1242 return ((xmlSchemaBasicItemPtr) ((xmlSchemaTypePtr) item)->next);
1243 case XML_SCHEMA_TYPE_ANY:
1244 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1245 return (NULL);
1246 case XML_SCHEMA_TYPE_PARTICLE:
1247 return ((xmlSchemaBasicItemPtr) ((xmlSchemaParticlePtr) item)->next);
1248 case XML_SCHEMA_TYPE_SEQUENCE:
1249 case XML_SCHEMA_TYPE_CHOICE:
1250 case XML_SCHEMA_TYPE_ALL:
1251 return (NULL);
1252 case XML_SCHEMA_TYPE_GROUP:
1253 return (NULL);
1254 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1255 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributeGroupPtr) item)->next);
1256 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1257 case XML_SCHEMA_TYPE_IDC_KEY:
1258 case XML_SCHEMA_TYPE_IDC_KEYREF:
1259 return ((xmlSchemaBasicItemPtr) ((xmlSchemaIDCPtr) item)->next);
1260 default:
1261 return (NULL);
1262 }
1263}
1264#endif
1265
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001266
1267/**
1268 * xmlSchemaFormatQName:
1269 * @buf: the string buffer
1270 * @namespaceName: the namespace name
1271 * @localName: the local name
1272 *
1273 * Returns the given QName in the format "{namespaceName}localName" or
1274 * just "localName" if @namespaceName is NULL.
1275 *
1276 * Returns the localName if @namespaceName is NULL, a formatted
1277 * string otherwise.
1278 */
1279static const xmlChar*
1280xmlSchemaFormatQName(xmlChar **buf,
1281 const xmlChar *namespaceName,
1282 const xmlChar *localName)
1283{
1284 FREE_AND_NULL(*buf)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001285 if (namespaceName != NULL) {
1286 *buf = xmlStrdup(BAD_CAST "{");
1287 *buf = xmlStrcat(*buf, namespaceName);
1288 *buf = xmlStrcat(*buf, BAD_CAST "}");
1289 }
1290 if (localName != NULL) {
1291 if (namespaceName == NULL)
1292 return(localName);
1293 *buf = xmlStrcat(*buf, localName);
1294 } else {
1295 *buf = xmlStrcat(*buf, BAD_CAST "(NULL)");
1296 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001297 return ((const xmlChar *) *buf);
1298}
1299
1300static const xmlChar*
1301xmlSchemaFormatQNameNs(xmlChar **buf, xmlNsPtr ns, const xmlChar *localName)
1302{
1303 if (ns != NULL)
1304 return (xmlSchemaFormatQName(buf, ns->href, localName));
1305 else
1306 return (xmlSchemaFormatQName(buf, NULL, localName));
1307}
1308
1309static const xmlChar *
1310xmlSchemaGetComponentName(xmlSchemaBasicItemPtr item)
1311{
1312 switch (item->type) {
1313 case XML_SCHEMA_TYPE_ELEMENT:
1314 return (((xmlSchemaElementPtr) item)->name);
1315 case XML_SCHEMA_TYPE_ATTRIBUTE:
1316 return (((xmlSchemaAttributePtr) item)->name);
1317 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1318 return (((xmlSchemaAttributeGroupPtr) item)->name);
1319 case XML_SCHEMA_TYPE_BASIC:
1320 case XML_SCHEMA_TYPE_SIMPLE:
1321 case XML_SCHEMA_TYPE_COMPLEX:
1322 return (((xmlSchemaTypePtr) item)->name);
1323 case XML_SCHEMA_TYPE_GROUP:
1324 return (((xmlSchemaModelGroupDefPtr) item)->name);
1325 case XML_SCHEMA_TYPE_IDC_KEY:
1326 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1327 case XML_SCHEMA_TYPE_IDC_KEYREF:
1328 return (((xmlSchemaIDCPtr) item)->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001329 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1330 if (WXS_ATTRUSE_DECL(item) != NULL) {
1331 return(xmlSchemaGetComponentName(
1332 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1333 } else
1334 return(NULL);
1335 case XML_SCHEMA_EXTRA_QNAMEREF:
1336 return (((xmlSchemaQNameRefPtr) item)->name);
1337 case XML_SCHEMA_TYPE_NOTATION:
1338 return (((xmlSchemaNotationPtr) item)->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001339 default:
1340 /*
1341 * Other components cannot have names.
1342 */
1343 break;
1344 }
1345 return (NULL);
1346}
1347
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001348#define xmlSchemaGetQNameRefName(r) (WXS_QNAME_CAST (r))->name
1349#define xmlSchemaGetQNameRefTargetNs(r) (WXS_QNAME_CAST (r))->targetNamespace
1350/*
1351static const xmlChar *
1352xmlSchemaGetQNameRefName(void *ref)
1353{
1354 return(((xmlSchemaQNameRefPtr) ref)->name);
1355}
1356
1357static const xmlChar *
1358xmlSchemaGetQNameRefTargetNs(void *ref)
1359{
1360 return(((xmlSchemaQNameRefPtr) ref)->targetNamespace);
1361}
1362*/
1363
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001364static const xmlChar *
1365xmlSchemaGetComponentTargetNs(xmlSchemaBasicItemPtr item)
1366{
1367 switch (item->type) {
1368 case XML_SCHEMA_TYPE_ELEMENT:
1369 return (((xmlSchemaElementPtr) item)->targetNamespace);
1370 case XML_SCHEMA_TYPE_ATTRIBUTE:
1371 return (((xmlSchemaAttributePtr) item)->targetNamespace);
1372 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1373 return (((xmlSchemaAttributeGroupPtr) item)->targetNamespace);
1374 case XML_SCHEMA_TYPE_BASIC:
1375 return (BAD_CAST "http://www.w3.org/2001/XMLSchema");
1376 case XML_SCHEMA_TYPE_SIMPLE:
1377 case XML_SCHEMA_TYPE_COMPLEX:
1378 return (((xmlSchemaTypePtr) item)->targetNamespace);
1379 case XML_SCHEMA_TYPE_GROUP:
1380 return (((xmlSchemaModelGroupDefPtr) item)->targetNamespace);
1381 case XML_SCHEMA_TYPE_IDC_KEY:
1382 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1383 case XML_SCHEMA_TYPE_IDC_KEYREF:
1384 return (((xmlSchemaIDCPtr) item)->targetNamespace);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001385 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1386 if (WXS_ATTRUSE_DECL(item) != NULL) {
1387 return(xmlSchemaGetComponentTargetNs(
1388 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1389 }
1390 /* TODO: Will returning NULL break something? */
1391 break;
1392 case XML_SCHEMA_EXTRA_QNAMEREF:
1393 return (((xmlSchemaQNameRefPtr) item)->targetNamespace);
1394 case XML_SCHEMA_TYPE_NOTATION:
1395 return (((xmlSchemaNotationPtr) item)->targetNamespace);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001396 default:
1397 /*
1398 * Other components cannot have names.
1399 */
1400 break;
1401 }
1402 return (NULL);
1403}
1404
1405static const xmlChar*
1406xmlSchemaGetComponentQName(xmlChar **buf,
1407 void *item)
1408{
1409 return (xmlSchemaFormatQName(buf,
1410 xmlSchemaGetComponentTargetNs((xmlSchemaBasicItemPtr) item),
1411 xmlSchemaGetComponentName((xmlSchemaBasicItemPtr) item)));
1412}
1413
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001414static const xmlChar*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001415xmlSchemaGetComponentDesignation(xmlChar **buf, void *item)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001416{
1417 xmlChar *str = NULL;
1418
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001419 *buf = xmlStrcat(*buf, WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001420 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001421 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str,
1422 (xmlSchemaBasicItemPtr) item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001423 *buf = xmlStrcat(*buf, BAD_CAST "'");
1424 FREE_AND_NULL(str);
1425 return(*buf);
1426}
1427
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001428static const xmlChar*
1429xmlSchemaGetIDCDesignation(xmlChar **buf, xmlSchemaIDCPtr idc)
1430{
1431 return(xmlSchemaGetComponentDesignation(buf, idc));
1432}
1433
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001434/**
1435 * xmlSchemaWildcardPCToString:
1436 * @pc: the type of processContents
1437 *
1438 * Returns a string representation of the type of
1439 * processContents.
1440 */
1441static const xmlChar *
1442xmlSchemaWildcardPCToString(int pc)
1443{
1444 switch (pc) {
1445 case XML_SCHEMAS_ANY_SKIP:
1446 return (BAD_CAST "skip");
1447 case XML_SCHEMAS_ANY_LAX:
1448 return (BAD_CAST "lax");
1449 case XML_SCHEMAS_ANY_STRICT:
1450 return (BAD_CAST "strict");
1451 default:
1452 return (BAD_CAST "invalid process contents");
1453 }
1454}
1455
1456/**
1457 * xmlSchemaGetCanonValueWhtspExt:
1458 * @val: the precomputed value
1459 * @retValue: the returned value
1460 * @ws: the whitespace type of the value
1461 *
1462 * Get a the cononical representation of the value.
1463 * The caller has to free the returned retValue.
1464 *
1465 * Returns 0 if the value could be built and -1 in case of
1466 * API errors or if the value type is not supported yet.
1467 */
1468static int
1469xmlSchemaGetCanonValueWhtspExt(xmlSchemaValPtr val,
1470 xmlSchemaWhitespaceValueType ws,
1471 xmlChar **retValue)
1472{
1473 int list;
1474 xmlSchemaValType valType;
1475 const xmlChar *value, *value2 = NULL;
1476
1477
1478 if ((retValue == NULL) || (val == NULL))
1479 return (-1);
1480 list = xmlSchemaValueGetNext(val) ? 1 : 0;
1481 *retValue = NULL;
1482 do {
1483 value = NULL;
1484 valType = xmlSchemaGetValType(val);
1485 switch (valType) {
1486 case XML_SCHEMAS_STRING:
1487 case XML_SCHEMAS_NORMSTRING:
1488 case XML_SCHEMAS_ANYSIMPLETYPE:
1489 value = xmlSchemaValueGetAsString(val);
1490 if (value != NULL) {
1491 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
1492 value2 = xmlSchemaCollapseString(value);
1493 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
1494 value2 = xmlSchemaWhiteSpaceReplace(value);
1495 if (value2 != NULL)
1496 value = value2;
1497 }
1498 break;
1499 default:
1500 if (xmlSchemaGetCanonValue(val, &value2) == -1) {
1501 if (value2 != NULL)
1502 xmlFree((xmlChar *) value2);
1503 goto internal_error;
1504 }
1505 value = value2;
1506 }
1507 if (*retValue == NULL)
1508 if (value == NULL) {
1509 if (! list)
1510 *retValue = xmlStrdup(BAD_CAST "");
1511 } else
1512 *retValue = xmlStrdup(value);
1513 else if (value != NULL) {
1514 /* List. */
1515 *retValue = xmlStrcat((xmlChar *) *retValue, BAD_CAST " ");
1516 *retValue = xmlStrcat((xmlChar *) *retValue, value);
1517 }
1518 FREE_AND_NULL(value2)
1519 val = xmlSchemaValueGetNext(val);
1520 } while (val != NULL);
1521
1522 return (0);
1523internal_error:
1524 if (*retValue != NULL)
1525 xmlFree((xmlChar *) (*retValue));
1526 if (value2 != NULL)
1527 xmlFree((xmlChar *) value2);
1528 return (-1);
1529}
1530
1531/**
1532 * xmlSchemaFormatItemForReport:
1533 * @buf: the string buffer
1534 * @itemDes: the designation of the item
1535 * @itemName: the name of the item
1536 * @item: the item as an object
1537 * @itemNode: the node of the item
1538 * @local: the local name
1539 * @parsing: if the function is used during the parse
1540 *
1541 * Returns a representation of the given item used
1542 * for error reports.
1543 *
1544 * The following order is used to build the resulting
1545 * designation if the arguments are not NULL:
1546 * 1a. If itemDes not NULL -> itemDes
1547 * 1b. If (itemDes not NULL) and (itemName not NULL)
1548 * -> itemDes + itemName
1549 * 2. If the preceding was NULL and (item not NULL) -> item
1550 * 3. If the preceding was NULL and (itemNode not NULL) -> itemNode
1551 *
1552 * If the itemNode is an attribute node, the name of the attribute
1553 * will be appended to the result.
1554 *
1555 * Returns the formatted string and sets @buf to the resulting value.
1556 */
1557static xmlChar*
1558xmlSchemaFormatItemForReport(xmlChar **buf,
1559 const xmlChar *itemDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001560 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001561 xmlNodePtr itemNode)
1562{
1563 xmlChar *str = NULL;
1564 int named = 1;
1565
1566 if (*buf != NULL) {
1567 xmlFree(*buf);
1568 *buf = NULL;
1569 }
1570
1571 if (itemDes != NULL) {
1572 *buf = xmlStrdup(itemDes);
1573 } else if (item != NULL) {
1574 switch (item->type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001575 case XML_SCHEMA_TYPE_BASIC: {
1576 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1577
1578 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001579 *buf = xmlStrdup(BAD_CAST "atomic type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001580 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001581 *buf = xmlStrdup(BAD_CAST "list type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001582 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001583 *buf = xmlStrdup(BAD_CAST "union type 'xs:");
1584 else
1585 *buf = xmlStrdup(BAD_CAST "simple type 'xs:");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001586 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001587 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001588 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001589 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001590 case XML_SCHEMA_TYPE_SIMPLE: {
1591 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1592
1593 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001594 *buf = xmlStrdup(BAD_CAST"");
1595 } else {
1596 *buf = xmlStrdup(BAD_CAST "local ");
1597 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001598 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001599 *buf = xmlStrcat(*buf, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001600 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001601 *buf = xmlStrcat(*buf, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001602 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001603 *buf = xmlStrcat(*buf, BAD_CAST "union type");
1604 else
1605 *buf = xmlStrcat(*buf, BAD_CAST "simple type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001606 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001607 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001608 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001609 *buf = xmlStrcat(*buf, BAD_CAST "'");
1610 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001611 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001612 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001613 case XML_SCHEMA_TYPE_COMPLEX: {
1614 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1615
1616 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001617 *buf = xmlStrdup(BAD_CAST "");
1618 else
1619 *buf = xmlStrdup(BAD_CAST "local ");
1620 *buf = xmlStrcat(*buf, BAD_CAST "complex type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001621 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001622 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001623 *buf = xmlStrcat(*buf, type->name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001624 *buf = xmlStrcat(*buf, BAD_CAST "'");
1625 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001626 }
1627 break;
1628 case XML_SCHEMA_TYPE_ATTRIBUTE_USE: {
1629 xmlSchemaAttributeUsePtr ause;
1630
1631 ause = WXS_ATTR_USE_CAST item;
1632 *buf = xmlStrdup(BAD_CAST "attribute use ");
1633 if (WXS_ATTRUSE_DECL(ause) != NULL) {
1634 *buf = xmlStrcat(*buf, BAD_CAST "'");
1635 *buf = xmlStrcat(*buf,
1636 xmlSchemaGetComponentQName(&str, WXS_ATTRUSE_DECL(ause)));
1637 FREE_AND_NULL(str)
1638 *buf = xmlStrcat(*buf, BAD_CAST "'");
1639 } else {
1640 *buf = xmlStrcat(*buf, BAD_CAST "(unknown)");
1641 }
1642 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001643 break;
1644 case XML_SCHEMA_TYPE_ATTRIBUTE: {
1645 xmlSchemaAttributePtr attr;
1646
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001647 attr = (xmlSchemaAttributePtr) item;
1648 *buf = xmlStrdup(BAD_CAST "attribute decl.");
1649 *buf = xmlStrcat(*buf, BAD_CAST " '");
1650 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1651 attr->targetNamespace, attr->name));
1652 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001653 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001654 }
1655 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001656 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1657 xmlSchemaGetComponentDesignation(buf, item);
1658 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001659 case XML_SCHEMA_TYPE_ELEMENT: {
1660 xmlSchemaElementPtr elem;
1661
1662 elem = (xmlSchemaElementPtr) item;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001663 *buf = xmlStrdup(BAD_CAST "element decl.");
1664 *buf = xmlStrcat(*buf, BAD_CAST " '");
1665 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1666 elem->targetNamespace, elem->name));
1667 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001668 }
1669 break;
1670 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1671 case XML_SCHEMA_TYPE_IDC_KEY:
1672 case XML_SCHEMA_TYPE_IDC_KEYREF:
1673 if (item->type == XML_SCHEMA_TYPE_IDC_UNIQUE)
1674 *buf = xmlStrdup(BAD_CAST "unique '");
1675 else if (item->type == XML_SCHEMA_TYPE_IDC_KEY)
1676 *buf = xmlStrdup(BAD_CAST "key '");
1677 else
1678 *buf = xmlStrdup(BAD_CAST "keyRef '");
1679 *buf = xmlStrcat(*buf, ((xmlSchemaIDCPtr) item)->name);
1680 *buf = xmlStrcat(*buf, BAD_CAST "'");
1681 break;
1682 case XML_SCHEMA_TYPE_ANY:
1683 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1684 *buf = xmlStrdup(xmlSchemaWildcardPCToString(
1685 ((xmlSchemaWildcardPtr) item)->processContents));
1686 *buf = xmlStrcat(*buf, BAD_CAST " wildcard");
1687 break;
1688 case XML_SCHEMA_FACET_MININCLUSIVE:
1689 case XML_SCHEMA_FACET_MINEXCLUSIVE:
1690 case XML_SCHEMA_FACET_MAXINCLUSIVE:
1691 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
1692 case XML_SCHEMA_FACET_TOTALDIGITS:
1693 case XML_SCHEMA_FACET_FRACTIONDIGITS:
1694 case XML_SCHEMA_FACET_PATTERN:
1695 case XML_SCHEMA_FACET_ENUMERATION:
1696 case XML_SCHEMA_FACET_WHITESPACE:
1697 case XML_SCHEMA_FACET_LENGTH:
1698 case XML_SCHEMA_FACET_MAXLENGTH:
1699 case XML_SCHEMA_FACET_MINLENGTH:
1700 *buf = xmlStrdup(BAD_CAST "facet '");
1701 *buf = xmlStrcat(*buf, xmlSchemaFacetTypeToString(item->type));
1702 *buf = xmlStrcat(*buf, BAD_CAST "'");
1703 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001704 case XML_SCHEMA_TYPE_GROUP: {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001705 *buf = xmlStrdup(BAD_CAST "model group def.");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001706 *buf = xmlStrcat(*buf, BAD_CAST " '");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001707 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001708 *buf = xmlStrcat(*buf, BAD_CAST "'");
1709 FREE_AND_NULL(str)
1710 }
1711 break;
1712 case XML_SCHEMA_TYPE_SEQUENCE:
1713 case XML_SCHEMA_TYPE_CHOICE:
1714 case XML_SCHEMA_TYPE_ALL:
1715 case XML_SCHEMA_TYPE_PARTICLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001716 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1717 break;
1718 case XML_SCHEMA_TYPE_NOTATION: {
1719 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1720 *buf = xmlStrcat(*buf, BAD_CAST " '");
1721 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
1722 *buf = xmlStrcat(*buf, BAD_CAST "'");
1723 FREE_AND_NULL(str);
1724 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001725 default:
1726 named = 0;
1727 }
1728 } else
1729 named = 0;
1730
1731 if ((named == 0) && (itemNode != NULL)) {
1732 xmlNodePtr elem;
1733
1734 if (itemNode->type == XML_ATTRIBUTE_NODE)
1735 elem = itemNode->parent;
1736 else
1737 elem = itemNode;
1738 *buf = xmlStrdup(BAD_CAST "Element '");
1739 if (elem->ns != NULL) {
1740 *buf = xmlStrcat(*buf,
1741 xmlSchemaFormatQName(&str, elem->ns->href, elem->name));
1742 FREE_AND_NULL(str)
1743 } else
1744 *buf = xmlStrcat(*buf, elem->name);
1745 *buf = xmlStrcat(*buf, BAD_CAST "'");
1746
1747 }
1748 if ((itemNode != NULL) && (itemNode->type == XML_ATTRIBUTE_NODE)) {
1749 *buf = xmlStrcat(*buf, BAD_CAST ", attribute '");
1750 if (itemNode->ns != NULL) {
1751 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1752 itemNode->ns->href, itemNode->name));
1753 FREE_AND_NULL(str)
1754 } else
1755 *buf = xmlStrcat(*buf, itemNode->name);
1756 *buf = xmlStrcat(*buf, BAD_CAST "'");
1757 }
1758 FREE_AND_NULL(str)
1759
1760 return (*buf);
1761}
1762
1763/**
1764 * xmlSchemaFormatFacetEnumSet:
1765 * @buf: the string buffer
1766 * @type: the type holding the enumeration facets
1767 *
1768 * Builds a string consisting of all enumeration elements.
1769 *
1770 * Returns a string of all enumeration elements.
1771 */
1772static const xmlChar *
1773xmlSchemaFormatFacetEnumSet(xmlSchemaAbstractCtxtPtr actxt,
1774 xmlChar **buf, xmlSchemaTypePtr type)
1775{
1776 xmlSchemaFacetPtr facet;
1777 xmlSchemaWhitespaceValueType ws;
1778 xmlChar *value = NULL;
1779 int res;
1780
1781 if (*buf != NULL)
1782 xmlFree(*buf);
1783 *buf = NULL;
1784
1785 do {
1786 /*
1787 * Use the whitespace type of the base type.
1788 */
1789 ws = xmlSchemaGetWhiteSpaceFacetValue(type->baseType);
1790 for (facet = type->facets; facet != NULL; facet = facet->next) {
1791 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
1792 continue;
1793 res = xmlSchemaGetCanonValueWhtspExt(facet->val,
1794 ws, &value);
1795 if (res == -1) {
1796 xmlSchemaInternalErr(actxt,
1797 "xmlSchemaFormatFacetEnumSet",
1798 "compute the canonical lexical representation");
1799 if (*buf != NULL)
1800 xmlFree(*buf);
1801 *buf = NULL;
1802 return (NULL);
1803 }
1804 if (*buf == NULL)
1805 *buf = xmlStrdup(BAD_CAST "'");
1806 else
1807 *buf = xmlStrcat(*buf, BAD_CAST ", '");
1808 *buf = xmlStrcat(*buf, BAD_CAST value);
1809 *buf = xmlStrcat(*buf, BAD_CAST "'");
1810 if (value != NULL) {
1811 xmlFree((xmlChar *)value);
1812 value = NULL;
1813 }
1814 }
1815 type = type->baseType;
1816 } while ((type != NULL) && (type->type != XML_SCHEMA_TYPE_BASIC));
1817
1818 return ((const xmlChar *) *buf);
1819}
1820
1821/************************************************************************
1822 * *
1823 * Error functions *
1824 * *
1825 ************************************************************************/
1826
1827#if 0
1828static void
1829xmlSchemaErrMemory(const char *msg)
1830{
1831 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1832 msg);
1833}
1834#endif
1835
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00001836static void
1837xmlSchemaPSimpleErr(const char *msg)
1838{
1839 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1840 msg);
1841}
1842
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001843/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001844 * xmlSchemaPErrMemory:
1845 * @node: a context node
1846 * @extra: extra informations
1847 *
1848 * Handle an out of memory condition
1849 */
1850static void
1851xmlSchemaPErrMemory(xmlSchemaParserCtxtPtr ctxt,
1852 const char *extra, xmlNodePtr node)
1853{
1854 if (ctxt != NULL)
1855 ctxt->nberrors++;
1856 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL,
1857 extra);
1858}
1859
1860/**
1861 * xmlSchemaPErr:
1862 * @ctxt: the parsing context
1863 * @node: the context node
1864 * @error: the error code
1865 * @msg: the error message
1866 * @str1: extra data
1867 * @str2: extra data
1868 *
1869 * Handle a parser error
1870 */
1871static void
1872xmlSchemaPErr(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1873 const char *msg, const xmlChar * str1, const xmlChar * str2)
1874{
1875 xmlGenericErrorFunc channel = NULL;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001876 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001877 void *data = NULL;
1878
1879 if (ctxt != NULL) {
1880 ctxt->nberrors++;
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00001881 ctxt->err = error;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001882 channel = ctxt->error;
1883 data = ctxt->userData;
Daniel Veillard659e71e2003-10-10 14:10:40 +00001884 schannel = ctxt->serror;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001885 }
Daniel Veillard659e71e2003-10-10 14:10:40 +00001886 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001887 error, XML_ERR_ERROR, NULL, 0,
1888 (const char *) str1, (const char *) str2, NULL, 0, 0,
1889 msg, str1, str2);
1890}
1891
1892/**
1893 * xmlSchemaPErr2:
1894 * @ctxt: the parsing context
1895 * @node: the context node
1896 * @node: the current child
1897 * @error: the error code
1898 * @msg: the error message
1899 * @str1: extra data
1900 * @str2: extra data
1901 *
1902 * Handle a parser error
1903 */
1904static void
1905xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
1906 xmlNodePtr child, int error,
1907 const char *msg, const xmlChar * str1, const xmlChar * str2)
1908{
1909 if (child != NULL)
1910 xmlSchemaPErr(ctxt, child, error, msg, str1, str2);
1911 else
1912 xmlSchemaPErr(ctxt, node, error, msg, str1, str2);
1913}
1914
Daniel Veillard01fa6152004-06-29 17:04:39 +00001915
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001916/**
Daniel Veillard3646d642004-06-02 19:19:14 +00001917 * xmlSchemaPErrExt:
1918 * @ctxt: the parsing context
1919 * @node: the context node
1920 * @error: the error code
1921 * @strData1: extra data
1922 * @strData2: extra data
1923 * @strData3: extra data
1924 * @msg: the message
1925 * @str1: extra parameter for the message display
1926 * @str2: extra parameter for the message display
1927 * @str3: extra parameter for the message display
1928 * @str4: extra parameter for the message display
1929 * @str5: extra parameter for the message display
1930 *
1931 * Handle a parser error
1932 */
1933static void
1934xmlSchemaPErrExt(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1935 const xmlChar * strData1, const xmlChar * strData2,
1936 const xmlChar * strData3, const char *msg, const xmlChar * str1,
1937 const xmlChar * str2, const xmlChar * str3, const xmlChar * str4,
1938 const xmlChar * str5)
1939{
1940
1941 xmlGenericErrorFunc channel = NULL;
1942 xmlStructuredErrorFunc schannel = NULL;
1943 void *data = NULL;
1944
1945 if (ctxt != NULL) {
1946 ctxt->nberrors++;
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00001947 ctxt->err = error;
Daniel Veillard3646d642004-06-02 19:19:14 +00001948 channel = ctxt->error;
1949 data = ctxt->userData;
1950 schannel = ctxt->serror;
1951 }
1952 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
1953 error, XML_ERR_ERROR, NULL, 0,
1954 (const char *) strData1, (const char *) strData2,
William M. Brack803812b2004-06-03 02:11:24 +00001955 (const char *) strData3, 0, 0, msg, str1, str2,
1956 str3, str4, str5);
Daniel Veillard3646d642004-06-02 19:19:14 +00001957}
Daniel Veillard01fa6152004-06-29 17:04:39 +00001958
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001959/************************************************************************
1960 * *
1961 * Allround error functions *
1962 * *
1963 ************************************************************************/
Daniel Veillard3646d642004-06-02 19:19:14 +00001964
1965/**
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001966 * xmlSchemaVTypeErrMemory:
1967 * @node: a context node
1968 * @extra: extra informations
1969 *
1970 * Handle an out of memory condition
1971 */
1972static void
1973xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt,
1974 const char *extra, xmlNodePtr node)
1975{
1976 if (ctxt != NULL) {
1977 ctxt->nberrors++;
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00001978 ctxt->err = XML_SCHEMAV_INTERNAL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001979 }
1980 __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL,
1981 extra);
1982}
1983
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00001984static void
1985xmlSchemaPSimpleInternalErr(xmlNodePtr node,
1986 const char *msg, const xmlChar *str)
1987{
1988 __xmlSimpleError(XML_FROM_SCHEMASP, XML_SCHEMAP_INTERNAL, node,
1989 msg, (const char *) str);
1990}
1991
1992#define WXS_ERROR_TYPE_ERROR 1
1993#define WXS_ERROR_TYPE_WARNING 2
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001994/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00001995 * xmlSchemaErr3:
Daniel Veillardd0c9c322003-10-10 00:49:42 +00001996 * @ctxt: the validation context
1997 * @node: the context node
1998 * @error: the error code
1999 * @msg: the error message
2000 * @str1: extra data
2001 * @str2: extra data
2002 * @str3: extra data
2003 *
2004 * Handle a validation error
2005 */
2006static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002007xmlSchemaErr4Line(xmlSchemaAbstractCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002008 xmlErrorLevel errorLevel,
2009 int error, xmlNodePtr node, int line, const char *msg,
2010 const xmlChar *str1, const xmlChar *str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002011 const xmlChar *str3, const xmlChar *str4)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002012{
Daniel Veillard659e71e2003-10-10 14:10:40 +00002013 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00002014 xmlGenericErrorFunc channel = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002015 void *data = NULL;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002016
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002017 if (ctxt != NULL) {
2018 if (ctxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2019 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctxt;
Daniel Veillard971771e2005-07-09 17:32:57 +00002020 const char *file = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002021 if (errorLevel != XML_ERR_WARNING) {
2022 vctxt->nberrors++;
2023 vctxt->err = error;
2024 channel = vctxt->error;
2025 } else {
2026 channel = vctxt->warning;
2027 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002028 schannel = vctxt->serror;
2029 data = vctxt->userData;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002030
2031 /*
2032 * Error node. If we specify a line number, then
2033 * do not channel any node to the error function.
2034 */
2035 if (line == 0) {
2036 if ((node == NULL) &&
2037 (vctxt->depth >= 0) &&
2038 (vctxt->inode != NULL)) {
2039 node = vctxt->inode->node;
2040 }
2041 /*
2042 * Get filename and line if no node-tree.
2043 */
2044 if ((node == NULL) &&
2045 (vctxt->parserCtxt != NULL) &&
2046 (vctxt->parserCtxt->input != NULL)) {
2047 file = vctxt->parserCtxt->input->filename;
2048 line = vctxt->parserCtxt->input->line;
2049 }
2050 } else {
2051 /*
2052 * Override the given node's (if any) position
2053 * and channel only the given line number.
2054 */
2055 node = NULL;
2056 /*
2057 * Get filename.
2058 */
2059 if (vctxt->doc != NULL)
2060 file = (const char *) vctxt->doc->URL;
2061 else if ((vctxt->parserCtxt != NULL) &&
2062 (vctxt->parserCtxt->input != NULL))
2063 file = vctxt->parserCtxt->input->filename;
2064 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002065 __xmlRaiseError(schannel, channel, data, ctxt,
2066 node, XML_FROM_SCHEMASV,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002067 error, errorLevel, file, line,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002068 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002069 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002070
2071 } else if (ctxt->type == XML_SCHEMA_CTXT_PARSER) {
2072 xmlSchemaParserCtxtPtr pctxt = (xmlSchemaParserCtxtPtr) ctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002073 if (errorLevel != XML_ERR_WARNING) {
2074 pctxt->nberrors++;
2075 pctxt->err = error;
2076 channel = pctxt->error;
2077 } else {
2078 channel = pctxt->warning;
2079 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002080 schannel = pctxt->serror;
2081 data = pctxt->userData;
2082 __xmlRaiseError(schannel, channel, data, ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002083 node, XML_FROM_SCHEMASP, error,
2084 errorLevel, NULL, 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002085 (const char *) str1, (const char *) str2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002086 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002087 } else {
2088 TODO
2089 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002090 }
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00002091}
2092
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002093/**
2094 * xmlSchemaErr3:
2095 * @ctxt: the validation context
2096 * @node: the context node
2097 * @error: the error code
2098 * @msg: the error message
2099 * @str1: extra data
2100 * @str2: extra data
2101 * @str3: extra data
2102 *
2103 * Handle a validation error
2104 */
2105static void
2106xmlSchemaErr3(xmlSchemaAbstractCtxtPtr actxt,
2107 int error, xmlNodePtr node, const char *msg,
2108 const xmlChar *str1, const xmlChar *str2, const xmlChar *str3)
2109{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002110 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2111 msg, str1, str2, str3, NULL);
2112}
2113
2114static void
2115xmlSchemaErr4(xmlSchemaAbstractCtxtPtr actxt,
2116 int error, xmlNodePtr node, const char *msg,
2117 const xmlChar *str1, const xmlChar *str2,
2118 const xmlChar *str3, const xmlChar *str4)
2119{
2120 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2121 msg, str1, str2, str3, str4);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002122}
2123
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002124static void
2125xmlSchemaErr(xmlSchemaAbstractCtxtPtr actxt,
2126 int error, xmlNodePtr node, const char *msg,
2127 const xmlChar *str1, const xmlChar *str2)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002128{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002129 xmlSchemaErr4(actxt, error, node, msg, str1, str2, NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00002130}
2131
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002132static xmlChar *
2133xmlSchemaFormatNodeForError(xmlChar ** msg,
2134 xmlSchemaAbstractCtxtPtr actxt,
2135 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002136{
2137 xmlChar *str = NULL;
2138
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002139 *msg = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002140 if (node != NULL) {
2141 /*
2142 * Work on tree nodes.
2143 */
2144 if (node->type == XML_ATTRIBUTE_NODE) {
2145 xmlNodePtr elem = node->parent;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002146
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002147 *msg = xmlStrdup(BAD_CAST "Element '");
2148 if (elem->ns != NULL)
2149 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2150 elem->ns->href, elem->name));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002151 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002152 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2153 NULL, elem->name));
2154 FREE_AND_NULL(str);
2155 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2156 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2157 } else {
2158 *msg = xmlStrdup(BAD_CAST "Element '");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002159 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002160 if (node->ns != NULL)
2161 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2162 node->ns->href, node->name));
Daniel Veillardc0826a72004-08-10 14:17:33 +00002163 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002164 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2165 NULL, node->name));
2166 FREE_AND_NULL(str);
2167 *msg = xmlStrcat(*msg, BAD_CAST "': ");
2168 } else if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2169 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) actxt;
2170 /*
2171 * Work on node infos.
2172 */
2173 if (vctxt->inode->nodeType == XML_ATTRIBUTE_NODE) {
2174 xmlSchemaNodeInfoPtr ielem =
2175 vctxt->elemInfos[vctxt->depth];
2176
2177 *msg = xmlStrdup(BAD_CAST "Element '");
2178 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2179 ielem->nsName, ielem->localName));
2180 FREE_AND_NULL(str);
2181 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2182 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2183 } else {
2184 *msg = xmlStrdup(BAD_CAST "Element '");
2185 }
2186 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2187 vctxt->inode->nsName, vctxt->inode->localName));
2188 FREE_AND_NULL(str);
2189 *msg = xmlStrcat(*msg, BAD_CAST "': ");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002190 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
2191 /*
2192 * Hmm, no node while parsing?
2193 * Return an empty string, in case NULL will break something.
2194 */
2195 *msg = xmlStrdup(BAD_CAST "");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002196 } else {
2197 TODO
2198 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002199 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002200 /*
2201 * VAL TODO: The output of the given schema component is currently
2202 * disabled.
2203 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002204#if 0
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002205 if ((type != NULL) && (xmlSchemaIsGlobalItem(type))) {
2206 *msg = xmlStrcat(*msg, BAD_CAST " [");
2207 *msg = xmlStrcat(*msg, xmlSchemaFormatItemForReport(&str,
2208 NULL, type, NULL, 0));
2209 FREE_AND_NULL(str)
2210 *msg = xmlStrcat(*msg, BAD_CAST "]");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002211 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002212#endif
2213 return (*msg);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002214}
2215
Daniel Veillardc0826a72004-08-10 14:17:33 +00002216static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002217xmlSchemaInternalErr2(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002218 const char *funcName,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002219 const char *message,
2220 const xmlChar *str1,
2221 const xmlChar *str2)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002222{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002223 xmlChar *msg = NULL;
2224
2225 msg = xmlStrdup(BAD_CAST "Internal error: ");
2226 msg = xmlStrcat(msg, BAD_CAST funcName);
2227 msg = xmlStrcat(msg, BAD_CAST ", ");
2228 msg = xmlStrcat(msg, BAD_CAST message);
2229 msg = xmlStrcat(msg, BAD_CAST ".\n");
2230
2231 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR)
2232 xmlSchemaErr(actxt, XML_SCHEMAV_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002233 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002234
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002235 else if (actxt->type == XML_SCHEMA_CTXT_PARSER)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002236 xmlSchemaErr(actxt, XML_SCHEMAP_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002237 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002238
2239 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002240}
2241
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002242static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002243xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
2244 const char *funcName,
2245 const char *message)
2246{
2247 xmlSchemaInternalErr2(actxt, funcName, message, NULL, NULL);
2248}
2249
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002250#if 0
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002251static void
2252xmlSchemaPInternalErr(xmlSchemaParserCtxtPtr pctxt,
2253 const char *funcName,
2254 const char *message,
2255 const xmlChar *str1,
2256 const xmlChar *str2)
2257{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002258 xmlSchemaInternalErr2(ACTXT_CAST pctxt, funcName, message,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002259 str1, str2);
2260}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002261#endif
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002262
2263static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002264xmlSchemaCustomErr4(xmlSchemaAbstractCtxtPtr actxt,
2265 xmlParserErrors error,
2266 xmlNodePtr node,
2267 xmlSchemaBasicItemPtr item,
2268 const char *message,
2269 const xmlChar *str1, const xmlChar *str2,
2270 const xmlChar *str3, const xmlChar *str4)
2271{
2272 xmlChar *msg = NULL;
2273
2274 if ((node == NULL) && (item != NULL) &&
2275 (actxt->type == XML_SCHEMA_CTXT_PARSER)) {
2276 node = WXS_ITEM_NODE(item);
2277 xmlSchemaFormatItemForReport(&msg, NULL, item, NULL);
2278 msg = xmlStrcat(msg, BAD_CAST ": ");
2279 } else
2280 xmlSchemaFormatNodeForError(&msg, actxt, node);
2281 msg = xmlStrcat(msg, (const xmlChar *) message);
2282 msg = xmlStrcat(msg, BAD_CAST ".\n");
2283 xmlSchemaErr4(actxt, error, node,
2284 (const char *) msg, str1, str2, str3, str4);
2285 FREE_AND_NULL(msg)
2286}
2287
2288static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002289xmlSchemaCustomErr(xmlSchemaAbstractCtxtPtr actxt,
2290 xmlParserErrors error,
2291 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002292 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002293 const char *message,
2294 const xmlChar *str1,
2295 const xmlChar *str2)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002296{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002297 xmlSchemaCustomErr4(actxt, error, node, item,
2298 message, str1, str2, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002299}
2300
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002301
2302
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002303static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002304xmlSchemaCustomWarning(xmlSchemaAbstractCtxtPtr actxt,
2305 xmlParserErrors error,
2306 xmlNodePtr node,
2307 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2308 const char *message,
2309 const xmlChar *str1,
2310 const xmlChar *str2,
2311 const xmlChar *str3)
2312{
2313 xmlChar *msg = NULL;
2314
2315 xmlSchemaFormatNodeForError(&msg, actxt, node);
2316 msg = xmlStrcat(msg, (const xmlChar *) message);
2317 msg = xmlStrcat(msg, BAD_CAST ".\n");
2318
2319 /* URGENT TODO: Set the error code to something sane. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002320 xmlSchemaErr4Line(actxt, XML_ERR_WARNING, error, node, 0,
2321 (const char *) msg, str1, str2, str3, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002322
2323 FREE_AND_NULL(msg)
2324}
2325
2326
2327
2328static void
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002329xmlSchemaKeyrefErr(xmlSchemaValidCtxtPtr vctxt,
2330 xmlParserErrors error,
2331 xmlSchemaPSVIIDCNodePtr idcNode,
2332 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2333 const char *message,
2334 const xmlChar *str1,
2335 const xmlChar *str2)
2336{
2337 xmlChar *msg = NULL, *qname = NULL;
2338
2339 msg = xmlStrdup(BAD_CAST "Element '%s': ");
2340 msg = xmlStrcat(msg, (const xmlChar *) message);
2341 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002342 xmlSchemaErr4Line(ACTXT_CAST vctxt, XML_ERR_ERROR,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002343 error, NULL, idcNode->nodeLine, (const char *) msg,
2344 xmlSchemaFormatQName(&qname,
2345 vctxt->nodeQNames->items[idcNode->nodeQNameID +1],
2346 vctxt->nodeQNames->items[idcNode->nodeQNameID]),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002347 str1, str2, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002348 FREE_AND_NULL(qname);
2349 FREE_AND_NULL(msg);
2350}
2351
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002352static int
2353xmlSchemaEvalErrorNodeType(xmlSchemaAbstractCtxtPtr actxt,
2354 xmlNodePtr node)
2355{
2356 if (node != NULL)
2357 return (node->type);
2358 if ((actxt->type == XML_SCHEMA_CTXT_VALIDATOR) &&
2359 (((xmlSchemaValidCtxtPtr) actxt)->inode != NULL))
2360 return ( ((xmlSchemaValidCtxtPtr) actxt)->inode->nodeType);
2361 return (-1);
2362}
2363
2364static int
2365xmlSchemaIsGlobalItem(xmlSchemaTypePtr item)
2366{
2367 switch (item->type) {
2368 case XML_SCHEMA_TYPE_COMPLEX:
2369 case XML_SCHEMA_TYPE_SIMPLE:
2370 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL)
2371 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002372 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002373 case XML_SCHEMA_TYPE_GROUP:
2374 return (1);
2375 case XML_SCHEMA_TYPE_ELEMENT:
2376 if ( ((xmlSchemaElementPtr) item)->flags &
2377 XML_SCHEMAS_ELEM_GLOBAL)
2378 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002379 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002380 case XML_SCHEMA_TYPE_ATTRIBUTE:
2381 if ( ((xmlSchemaAttributePtr) item)->flags &
2382 XML_SCHEMAS_ATTR_GLOBAL)
2383 return(1);
2384 break;
2385 /* Note that attribute groups are always global. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002386 default:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002387 return(1);
2388 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002389 return (0);
2390}
2391
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002392static void
2393xmlSchemaSimpleTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2394 xmlParserErrors error,
2395 xmlNodePtr node,
2396 const xmlChar *value,
2397 xmlSchemaTypePtr type,
2398 int displayValue)
2399{
2400 xmlChar *msg = NULL;
2401
2402 xmlSchemaFormatNodeForError(&msg, actxt, node);
2403
2404 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2405 XML_ATTRIBUTE_NODE))
2406 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
2407 else
2408 msg = xmlStrcat(msg, BAD_CAST "The character content is not a valid "
2409 "value of ");
2410
2411 if (! xmlSchemaIsGlobalItem(type))
2412 msg = xmlStrcat(msg, BAD_CAST "the local ");
2413 else
2414 msg = xmlStrcat(msg, BAD_CAST "the ");
2415
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002416 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002417 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002418 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002419 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002420 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002421 msg = xmlStrcat(msg, BAD_CAST "union type");
2422
2423 if (xmlSchemaIsGlobalItem(type)) {
2424 xmlChar *str = NULL;
2425 msg = xmlStrcat(msg, BAD_CAST " '");
2426 if (type->builtInType != 0) {
2427 msg = xmlStrcat(msg, BAD_CAST "xs:");
2428 msg = xmlStrcat(msg, type->name);
2429 } else
2430 msg = xmlStrcat(msg,
2431 xmlSchemaFormatQName(&str,
2432 type->targetNamespace, type->name));
2433 msg = xmlStrcat(msg, BAD_CAST "'");
2434 FREE_AND_NULL(str);
2435 }
2436 msg = xmlStrcat(msg, BAD_CAST ".\n");
2437 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2438 XML_ATTRIBUTE_NODE))
2439 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2440 else
2441 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2442 FREE_AND_NULL(msg)
2443}
2444
Daniel Veillardc0826a72004-08-10 14:17:33 +00002445static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002446xmlSchemaFormatErrorNodeQName(xmlChar ** str,
2447 xmlSchemaNodeInfoPtr ni,
2448 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002449{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002450 if (node != NULL) {
2451 if (node->ns != NULL)
2452 return (xmlSchemaFormatQName(str, node->ns->href, node->name));
2453 else
2454 return (xmlSchemaFormatQName(str, NULL, node->name));
2455 } else if (ni != NULL)
2456 return (xmlSchemaFormatQName(str, ni->nsName, ni->localName));
2457 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002458}
2459
Daniel Veillardc0826a72004-08-10 14:17:33 +00002460static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002461xmlSchemaIllegalAttrErr(xmlSchemaAbstractCtxtPtr actxt,
2462 xmlParserErrors error,
2463 xmlSchemaAttrInfoPtr ni,
2464 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002465{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002466 xmlChar *msg = NULL, *str = NULL;
2467
2468 xmlSchemaFormatNodeForError(&msg, actxt, node);
2469 msg = xmlStrcat(msg, BAD_CAST "The attribute '%s' is not allowed.\n");
2470 xmlSchemaErr(actxt, error, node, (const char *) msg,
2471 xmlSchemaFormatErrorNodeQName(&str, (xmlSchemaNodeInfoPtr) ni, node),
2472 NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002473 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002474 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002475}
2476
Daniel Veillardc0826a72004-08-10 14:17:33 +00002477static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002478xmlSchemaComplexTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2479 xmlParserErrors error,
2480 xmlNodePtr node,
2481 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002482 const char *message,
2483 int nbval,
2484 int nbneg,
2485 xmlChar **values)
2486{
2487 xmlChar *str = NULL, *msg = NULL;
2488 xmlChar *localName, *nsName;
2489 const xmlChar *cur, *end;
Daniel Veillard77005e62005-07-19 16:26:18 +00002490 int i, is_not;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002491
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002492 xmlSchemaFormatNodeForError(&msg, actxt, node);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002493 msg = xmlStrcat(msg, (const xmlChar *) message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002494 msg = xmlStrcat(msg, BAD_CAST ".");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002495 /*
2496 * Note that is does not make sense to report that we have a
2497 * wildcard here, since the wildcard might be unfolded into
2498 * multiple transitions.
2499 */
2500 if (nbval + nbneg > 0) {
2501 if (nbval + nbneg > 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002502 str = xmlStrdup(BAD_CAST " Expected is one of ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002503 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002504 str = xmlStrdup(BAD_CAST " Expected is ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002505 nsName = NULL;
2506
2507 for (i = 0; i < nbval + nbneg; i++) {
2508 cur = values[i];
Daniel Veillard77005e62005-07-19 16:26:18 +00002509 if (cur == NULL)
2510 continue;
2511 if ((cur[0] == 'n') && (cur[1] == 'o') && (cur[2] == 't') &&
2512 (cur[3] == ' ')) {
2513 is_not = 1;
2514 cur += 4;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002515 str = xmlStrcat(str, BAD_CAST "##other");
Daniel Veillard77005e62005-07-19 16:26:18 +00002516 } else {
2517 is_not = 0;
2518 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002519 /*
2520 * Get the local name.
2521 */
2522 localName = NULL;
2523
2524 end = cur;
2525 if (*end == '*') {
2526 localName = xmlStrdup(BAD_CAST "*");
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002527 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002528 } else {
2529 while ((*end != 0) && (*end != '|'))
2530 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002531 localName = xmlStrncat(localName, BAD_CAST cur, end - cur);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002532 }
2533 if (*end != 0) {
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002534 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002535 /*
2536 * Skip "*|*" if they come with negated expressions, since
2537 * they represent the same negated wildcard.
2538 */
2539 if ((nbneg == 0) || (*end != '*') || (*localName != '*')) {
2540 /*
2541 * Get the namespace name.
2542 */
2543 cur = end;
2544 if (*end == '*') {
2545 nsName = xmlStrdup(BAD_CAST "{*}");
2546 } else {
2547 while (*end != 0)
2548 end++;
2549
2550 if (i >= nbval)
2551 nsName = xmlStrdup(BAD_CAST "{##other:");
2552 else
2553 nsName = xmlStrdup(BAD_CAST "{");
2554
2555 nsName = xmlStrncat(nsName, BAD_CAST cur, end - cur);
2556 nsName = xmlStrcat(nsName, BAD_CAST "}");
2557 }
2558 str = xmlStrcat(str, BAD_CAST nsName);
2559 FREE_AND_NULL(nsName)
2560 } else {
2561 FREE_AND_NULL(localName);
2562 continue;
2563 }
2564 }
2565 str = xmlStrcat(str, BAD_CAST localName);
2566 FREE_AND_NULL(localName);
2567
2568 if (i < nbval + nbneg -1)
2569 str = xmlStrcat(str, BAD_CAST ", ");
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00002570 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002571 str = xmlStrcat(str, BAD_CAST " ).\n");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002572 msg = xmlStrcat(msg, BAD_CAST str);
2573 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002574 } else
2575 msg = xmlStrcat(msg, BAD_CAST "\n");
2576 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002577 xmlFree(msg);
2578}
2579
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002580static void
2581xmlSchemaFacetErr(xmlSchemaAbstractCtxtPtr actxt,
2582 xmlParserErrors error,
2583 xmlNodePtr node,
2584 const xmlChar *value,
2585 unsigned long length,
2586 xmlSchemaTypePtr type,
2587 xmlSchemaFacetPtr facet,
2588 const char *message,
2589 const xmlChar *str1,
2590 const xmlChar *str2)
2591{
2592 xmlChar *str = NULL, *msg = NULL;
2593 xmlSchemaTypeType facetType;
2594 int nodeType = xmlSchemaEvalErrorNodeType(actxt, node);
2595
2596 xmlSchemaFormatNodeForError(&msg, actxt, node);
2597 if (error == XML_SCHEMAV_CVC_ENUMERATION_VALID) {
2598 facetType = XML_SCHEMA_FACET_ENUMERATION;
2599 /*
2600 * If enumerations are validated, one must not expect the
2601 * facet to be given.
2602 */
2603 } else
2604 facetType = facet->type;
2605 msg = xmlStrcat(msg, BAD_CAST "[");
2606 msg = xmlStrcat(msg, BAD_CAST "facet '");
2607 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facetType));
2608 msg = xmlStrcat(msg, BAD_CAST "'] ");
2609 if (message == NULL) {
2610 /*
2611 * Use a default message.
2612 */
2613 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
2614 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
2615 (facetType == XML_SCHEMA_FACET_MAXLENGTH)) {
2616
2617 char len[25], actLen[25];
2618
2619 /* FIXME, TODO: What is the max expected string length of the
2620 * this value?
2621 */
2622 if (nodeType == XML_ATTRIBUTE_NODE)
2623 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has a length of '%s'; ");
2624 else
2625 msg = xmlStrcat(msg, BAD_CAST "The value has a length of '%s'; ");
2626
2627 snprintf(len, 24, "%lu", xmlSchemaGetFacetValueAsULong(facet));
2628 snprintf(actLen, 24, "%lu", length);
2629
2630 if (facetType == XML_SCHEMA_FACET_LENGTH)
2631 msg = xmlStrcat(msg,
2632 BAD_CAST "this differs from the allowed length of '%s'.\n");
2633 else if (facetType == XML_SCHEMA_FACET_MAXLENGTH)
2634 msg = xmlStrcat(msg,
2635 BAD_CAST "this exceeds the allowed maximum length of '%s'.\n");
2636 else if (facetType == XML_SCHEMA_FACET_MINLENGTH)
2637 msg = xmlStrcat(msg,
2638 BAD_CAST "this underruns the allowed minimum length of '%s'.\n");
2639
2640 if (nodeType == XML_ATTRIBUTE_NODE)
2641 xmlSchemaErr3(actxt, error, node, (const char *) msg,
2642 value, (const xmlChar *) actLen, (const xmlChar *) len);
2643 else
2644 xmlSchemaErr(actxt, error, node, (const char *) msg,
2645 (const xmlChar *) actLen, (const xmlChar *) len);
2646
2647 } else if (facetType == XML_SCHEMA_FACET_ENUMERATION) {
2648 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not an element "
2649 "of the set {%s}.\n");
2650 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2651 xmlSchemaFormatFacetEnumSet(actxt, &str, type));
2652 } else if (facetType == XML_SCHEMA_FACET_PATTERN) {
2653 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not accepted "
2654 "by the pattern '%s'.\n");
2655 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2656 facet->value);
2657 } else if (facetType == XML_SCHEMA_FACET_MININCLUSIVE) {
2658 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is less than the "
2659 "minimum value allowed ('%s').\n");
2660 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2661 facet->value);
2662 } else if (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) {
2663 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is greater than the "
2664 "maximum value allowed ('%s').\n");
2665 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2666 facet->value);
2667 } else if (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE) {
2668 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be less than "
2669 "'%s'.\n");
2670 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2671 facet->value);
2672 } else if (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) {
2673 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be more than "
2674 "'%s'.\n");
2675 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2676 facet->value);
2677 } else if (facetType == XML_SCHEMA_FACET_TOTALDIGITS) {
2678 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more "
2679 "digits than are allowed ('%s').\n");
2680 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2681 facet->value);
2682 } else if (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) {
2683 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more fractional "
2684 "digits than are allowed ('%s').\n");
2685 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2686 facet->value);
2687 } else if (nodeType == XML_ATTRIBUTE_NODE) {
2688 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not facet-valid.\n");
2689 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2690 } else {
2691 msg = xmlStrcat(msg, BAD_CAST "The value is not facet-valid.\n");
2692 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2693 }
2694 } else {
2695 msg = xmlStrcat(msg, (const xmlChar *) message);
2696 msg = xmlStrcat(msg, BAD_CAST ".\n");
2697 xmlSchemaErr(actxt, error, node, (const char *) msg, str1, str2);
2698 }
2699 FREE_AND_NULL(str)
2700 xmlFree(msg);
2701}
2702
2703#define VERROR(err, type, msg) \
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002704 xmlSchemaCustomErr(ACTXT_CAST vctxt, err, NULL, type, msg, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002705
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002706#define VERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST vctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002707
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002708#define PERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST pctxt, func, msg);
2709#define PERROR_INT2(func, msg) xmlSchemaInternalErr(ACTXT_CAST ctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002710
2711#define AERROR_INT(func, msg) xmlSchemaInternalErr(actxt, func, msg);
2712
2713
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002714/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00002715 * xmlSchemaPMissingAttrErr:
2716 * @ctxt: the schema validation context
2717 * @ownerDes: the designation of the owner
2718 * @ownerName: the name of the owner
2719 * @ownerItem: the owner as a schema object
2720 * @ownerElem: the owner as an element node
2721 * @node: the parent element node of the missing attribute node
2722 * @type: the corresponding type of the attribute node
2723 *
2724 * Reports an illegal attribute.
2725 */
2726static void
2727xmlSchemaPMissingAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002728 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002729 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002730 xmlNodePtr ownerElem,
2731 const char *name,
2732 const char *message)
2733{
2734 xmlChar *des = NULL;
2735
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002736 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
2737
Daniel Veillardc0826a72004-08-10 14:17:33 +00002738 if (message != NULL)
2739 xmlSchemaPErr(ctxt, ownerElem, error, "%s: %s.\n", BAD_CAST des, BAD_CAST message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002740 else
2741 xmlSchemaPErr(ctxt, ownerElem, error,
2742 "%s: The attribute '%s' is required but missing.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002743 BAD_CAST des, BAD_CAST name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002744 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002745}
2746
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002747
Daniel Veillardc0826a72004-08-10 14:17:33 +00002748/**
2749 * xmlSchemaPResCompAttrErr:
2750 * @ctxt: the schema validation context
2751 * @error: the error code
2752 * @ownerDes: the designation of the owner
2753 * @ownerItem: the owner as a schema object
2754 * @ownerElem: the owner as an element node
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002755 * @name: the name of the attribute holding the QName
Daniel Veillardc0826a72004-08-10 14:17:33 +00002756 * @refName: the referenced local name
2757 * @refURI: the referenced namespace URI
2758 * @message: optional message
2759 *
2760 * Used to report QName attribute values that failed to resolve
2761 * to schema components.
2762 */
2763static void
2764xmlSchemaPResCompAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002765 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002766 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002767 xmlNodePtr ownerElem,
2768 const char *name,
2769 const xmlChar *refName,
2770 const xmlChar *refURI,
2771 xmlSchemaTypeType refType,
2772 const char *refTypeStr)
2773{
2774 xmlChar *des = NULL, *strA = NULL;
2775
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002776 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002777 if (refTypeStr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002778 refTypeStr = (const char *) xmlSchemaItemTypeToStr(refType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002779 xmlSchemaPErrExt(ctxt, ownerElem, error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002780 NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002781 "%s, attribute '%s': The QName value '%s' does not resolve to a(n) "
2782 "%s.\n", BAD_CAST des, BAD_CAST name,
2783 xmlSchemaFormatQName(&strA, refURI, refName),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002784 BAD_CAST refTypeStr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002785 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002786 FREE_AND_NULL(strA)
2787}
2788
William M. Brack2f2a6632004-08-20 23:09:47 +00002789/**
2790 * xmlSchemaPCustomAttrErr:
2791 * @ctxt: the schema parser context
2792 * @error: the error code
2793 * @ownerDes: the designation of the owner
2794 * @ownerItem: the owner as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002795 * @attr: the illegal attribute node
William M. Brack2f2a6632004-08-20 23:09:47 +00002796 *
2797 * Reports an illegal attribute during the parse.
2798 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002799static void
2800xmlSchemaPCustomAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002801 xmlParserErrors error,
William M. Brack2f2a6632004-08-20 23:09:47 +00002802 xmlChar **ownerDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002803 xmlSchemaBasicItemPtr ownerItem,
William M. Brack2f2a6632004-08-20 23:09:47 +00002804 xmlAttrPtr attr,
2805 const char *msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002806{
2807 xmlChar *des = NULL;
2808
2809 if (ownerDes == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002810 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002811 else if (*ownerDes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002812 xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002813 des = *ownerDes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002814 } else
2815 des = *ownerDes;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002816 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002817 "%s, attribute '%s': %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002818 BAD_CAST des, attr->name, (const xmlChar *) msg, NULL, NULL);
2819 if (ownerDes == NULL)
2820 FREE_AND_NULL(des);
2821}
2822
2823/**
2824 * xmlSchemaPIllegalAttrErr:
William M. Brack2f2a6632004-08-20 23:09:47 +00002825 * @ctxt: the schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00002826 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00002827 * @ownerDes: the designation of the attribute's owner
2828 * @ownerItem: the attribute's owner item
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002829 * @attr: the illegal attribute node
Daniel Veillardc0826a72004-08-10 14:17:33 +00002830 *
William M. Brack2f2a6632004-08-20 23:09:47 +00002831 * Reports an illegal attribute during the parse.
Daniel Veillardc0826a72004-08-10 14:17:33 +00002832 */
2833static void
2834xmlSchemaPIllegalAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002835 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002836 xmlSchemaBasicItemPtr ownerComp ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002837 xmlAttrPtr attr)
2838{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002839 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002840
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002841 xmlSchemaFormatNodeForError(&strA, ACTXT_CAST ctxt, attr->parent);
2842 xmlSchemaErr4(ACTXT_CAST ctxt, error, (xmlNodePtr) attr,
2843 "%sThe attribute '%s' is not allowed.\n", BAD_CAST strA,
2844 xmlSchemaFormatQNameNs(&strB, attr->ns, attr->name),
2845 NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002846 FREE_AND_NULL(strA);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002847 FREE_AND_NULL(strB);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002848}
2849
William M. Brack2f2a6632004-08-20 23:09:47 +00002850/**
2851 * xmlSchemaPCustomErr:
2852 * @ctxt: the schema parser context
2853 * @error: the error code
2854 * @itemDes: the designation of the schema item
2855 * @item: the schema item
2856 * @itemElem: the node of the schema item
2857 * @message: the error message
2858 * @str1: an optional param for the error message
2859 * @str2: an optional param for the error message
2860 * @str3: an optional param for the error message
2861 *
2862 * Reports an error during parsing.
2863 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002864static void
2865xmlSchemaPCustomErrExt(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002866 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002867 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002868 xmlNodePtr itemElem,
2869 const char *message,
2870 const xmlChar *str1,
2871 const xmlChar *str2,
2872 const xmlChar *str3)
2873{
2874 xmlChar *des = NULL, *msg = NULL;
2875
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002876 xmlSchemaFormatItemForReport(&des, NULL, item, itemElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002877 msg = xmlStrdup(BAD_CAST "%s: ");
2878 msg = xmlStrcat(msg, (const xmlChar *) message);
2879 msg = xmlStrcat(msg, BAD_CAST ".\n");
2880 if ((itemElem == NULL) && (item != NULL))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002881 itemElem = WXS_ITEM_NODE(item);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002882 xmlSchemaPErrExt(ctxt, itemElem, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002883 (const char *) msg, BAD_CAST des, str1, str2, str3, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002884 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002885 FREE_AND_NULL(msg);
2886}
2887
William M. Brack2f2a6632004-08-20 23:09:47 +00002888/**
2889 * xmlSchemaPCustomErr:
2890 * @ctxt: the schema parser context
2891 * @error: the error code
2892 * @itemDes: the designation of the schema item
2893 * @item: the schema item
2894 * @itemElem: the node of the schema item
2895 * @message: the error message
2896 * @str1: the optional param for the error message
2897 *
2898 * Reports an error during parsing.
2899 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002900static void
2901xmlSchemaPCustomErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002902 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002903 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002904 xmlNodePtr itemElem,
2905 const char *message,
2906 const xmlChar *str1)
2907{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002908 xmlSchemaPCustomErrExt(ctxt, error, item, itemElem, message,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002909 str1, NULL, NULL);
2910}
2911
William M. Brack2f2a6632004-08-20 23:09:47 +00002912/**
2913 * xmlSchemaPAttrUseErr:
2914 * @ctxt: the schema parser context
2915 * @error: the error code
2916 * @itemDes: the designation of the schema type
2917 * @item: the schema type
2918 * @itemElem: the node of the schema type
2919 * @attr: the invalid schema attribute
2920 * @message: the error message
2921 * @str1: the optional param for the error message
2922 *
2923 * Reports an attribute use error during parsing.
2924 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002925static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002926xmlSchemaPAttrUseErr4(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002927 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002928 xmlNodePtr node,
2929 xmlSchemaBasicItemPtr ownerItem,
2930 const xmlSchemaAttributeUsePtr attruse,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002931 const char *message,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002932 const xmlChar *str1, const xmlChar *str2,
2933 const xmlChar *str3,const xmlChar *str4)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002934{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002935 xmlChar *str = NULL, *msg = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002936
2937 xmlSchemaFormatItemForReport(&msg, NULL, ownerItem, NULL);
2938 msg = xmlStrcat(msg, BAD_CAST ", ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002939 msg = xmlStrcat(msg,
2940 BAD_CAST xmlSchemaFormatItemForReport(&str, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002941 WXS_BASIC_CAST attruse, NULL));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002942 FREE_AND_NULL(str);
2943 msg = xmlStrcat(msg, BAD_CAST ": ");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002944 msg = xmlStrcat(msg, (const xmlChar *) message);
2945 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002946 xmlSchemaErr4(ACTXT_CAST ctxt, error, node,
2947 (const char *) msg, str1, str2, str3, str4);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002948 xmlFree(msg);
2949}
2950
William M. Brack2f2a6632004-08-20 23:09:47 +00002951/**
2952 * xmlSchemaPIllegalFacetAtomicErr:
2953 * @ctxt: the schema parser context
2954 * @error: the error code
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002955 * @type: the schema type
2956 * @baseType: the base type of type
William M. Brack2f2a6632004-08-20 23:09:47 +00002957 * @facet: the illegal facet
2958 *
2959 * Reports an illegal facet for atomic simple types.
2960 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002961static void
2962xmlSchemaPIllegalFacetAtomicErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002963 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002964 xmlSchemaTypePtr type,
2965 xmlSchemaTypePtr baseType,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002966 xmlSchemaFacetPtr facet)
2967{
2968 xmlChar *des = NULL, *strT = NULL;
2969
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002970 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type, type->node);
2971 xmlSchemaPErrExt(ctxt, type->node, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002972 "%s: The facet '%s' is not allowed on types derived from the "
2973 "type %s.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00002974 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002975 xmlSchemaFormatItemForReport(&strT, NULL, WXS_BASIC_CAST baseType, NULL),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002976 NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002977 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002978 FREE_AND_NULL(strT);
2979}
2980
William M. Brack2f2a6632004-08-20 23:09:47 +00002981/**
2982 * xmlSchemaPIllegalFacetListUnionErr:
2983 * @ctxt: the schema parser context
2984 * @error: the error code
2985 * @itemDes: the designation of the schema item involved
2986 * @item: the schema item involved
2987 * @facet: the illegal facet
2988 *
2989 * Reports an illegal facet for <list> and <union>.
2990 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002991static void
2992xmlSchemaPIllegalFacetListUnionErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002993 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002994 xmlSchemaTypePtr type,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002995 xmlSchemaFacetPtr facet)
2996{
2997 xmlChar *des = NULL, *strT = NULL;
2998
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002999 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type,
3000 type->node);
3001 xmlSchemaPErr(ctxt, type->node, error,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003002 "%s: The facet '%s' is not allowed.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00003003 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003004 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003005 FREE_AND_NULL(strT);
3006}
3007
3008/**
3009 * xmlSchemaPMutualExclAttrErr:
3010 * @ctxt: the schema validation context
3011 * @error: the error code
3012 * @elemDes: the designation of the parent element node
3013 * @attr: the bad attribute node
3014 * @type: the corresponding type of the attribute node
3015 *
3016 * Reports an illegal attribute.
3017 */
3018static void
3019xmlSchemaPMutualExclAttrErr(xmlSchemaParserCtxtPtr ctxt,
3020 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003021 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003022 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003023 const char *name1,
3024 const char *name2)
3025{
3026 xmlChar *des = NULL;
3027
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003028 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003029 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003030 "%s: The attributes '%s' and '%s' are mutually exclusive.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003031 BAD_CAST des, BAD_CAST name1, BAD_CAST name2, NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003032 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003033}
3034
3035/**
3036 * xmlSchemaPSimpleTypeErr:
3037 * @ctxt: the schema validation context
3038 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00003039 * @type: the type specifier
Daniel Veillardc0826a72004-08-10 14:17:33 +00003040 * @ownerDes: the designation of the owner
3041 * @ownerItem: the schema object if existent
3042 * @node: the validated node
3043 * @value: the validated value
3044 *
3045 * Reports a simple type validation error.
3046 * TODO: Should this report the value of an element as well?
3047 */
3048static void
3049xmlSchemaPSimpleTypeErr(xmlSchemaParserCtxtPtr ctxt,
3050 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003051 xmlSchemaBasicItemPtr ownerItem ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003052 xmlNodePtr node,
William M. Brack2f2a6632004-08-20 23:09:47 +00003053 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003054 const char *expected,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003055 const xmlChar *value,
3056 const char *message,
3057 const xmlChar *str1,
3058 const xmlChar *str2)
3059{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003060 xmlChar *msg = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003061
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003062 xmlSchemaFormatNodeForError(&msg, ACTXT_CAST ctxt, node);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003063 if (message == NULL) {
3064 /*
3065 * Use default messages.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003066 */
3067 if (type != NULL) {
3068 if (node->type == XML_ATTRIBUTE_NODE)
3069 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
3070 else
3071 msg = xmlStrcat(msg, BAD_CAST "The character content is not a "
3072 "valid value of ");
3073 if (! xmlSchemaIsGlobalItem(type))
3074 msg = xmlStrcat(msg, BAD_CAST "the local ");
3075 else
3076 msg = xmlStrcat(msg, BAD_CAST "the ");
3077
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003078 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003079 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003080 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003081 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003082 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003083 msg = xmlStrcat(msg, BAD_CAST "union type");
3084
3085 if (xmlSchemaIsGlobalItem(type)) {
3086 xmlChar *str = NULL;
3087 msg = xmlStrcat(msg, BAD_CAST " '");
3088 if (type->builtInType != 0) {
3089 msg = xmlStrcat(msg, BAD_CAST "xs:");
3090 msg = xmlStrcat(msg, type->name);
3091 } else
3092 msg = xmlStrcat(msg,
3093 xmlSchemaFormatQName(&str,
3094 type->targetNamespace, type->name));
3095 msg = xmlStrcat(msg, BAD_CAST "'.");
3096 FREE_AND_NULL(str);
3097 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003098 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003099 if (node->type == XML_ATTRIBUTE_NODE)
3100 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not valid.");
3101 else
3102 msg = xmlStrcat(msg, BAD_CAST "The character content is not "
3103 "valid.");
3104 }
3105 if (expected) {
3106 msg = xmlStrcat(msg, BAD_CAST " Expected is '");
3107 msg = xmlStrcat(msg, BAD_CAST expected);
3108 msg = xmlStrcat(msg, BAD_CAST "'.\n");
3109 } else
3110 msg = xmlStrcat(msg, BAD_CAST "\n");
Daniel Veillardc0826a72004-08-10 14:17:33 +00003111 if (node->type == XML_ATTRIBUTE_NODE)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003112 xmlSchemaPErr(ctxt, node, error, (const char *) msg, value, NULL);
3113 else
3114 xmlSchemaPErr(ctxt, node, error, (const char *) msg, NULL, NULL);
3115 } else {
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003116 msg = xmlStrcat(msg, BAD_CAST message);
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00003117 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003118 xmlSchemaPErrExt(ctxt, node, error, NULL, NULL, NULL,
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003119 (const char*) msg, str1, str2, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003120 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003121 /* Cleanup. */
3122 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003123}
3124
William M. Brack2f2a6632004-08-20 23:09:47 +00003125/**
3126 * xmlSchemaPContentErr:
3127 * @ctxt: the schema parser context
3128 * @error: the error code
3129 * @onwerDes: the designation of the holder of the content
3130 * @ownerItem: the owner item of the holder of the content
3131 * @ownerElem: the node of the holder of the content
3132 * @child: the invalid child node
3133 * @message: the optional error message
3134 * @content: the optional string describing the correct content
3135 *
3136 * Reports an error concerning the content of a schema element.
3137 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003138static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003139xmlSchemaPContentErr(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003140 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003141 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003142 xmlNodePtr ownerElem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003143 xmlNodePtr child,
3144 const char *message,
3145 const char *content)
3146{
3147 xmlChar *des = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003148
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003149 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003150 if (message != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003151 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3152 "%s: %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003153 BAD_CAST des, BAD_CAST message);
3154 else {
3155 if (content != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003156 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3157 "%s: The content is not valid. Expected is %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003158 BAD_CAST des, BAD_CAST content);
3159 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003160 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3161 "%s: The content is not valid.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003162 BAD_CAST des, NULL);
3163 }
3164 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003165 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003166}
3167
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003168/************************************************************************
3169 * *
3170 * Streamable error functions *
3171 * *
3172 ************************************************************************/
Kasimier T. Buchcik8b418172004-11-17 13:14:27 +00003173
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003174
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003175
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003176
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003177/************************************************************************
3178 * *
3179 * Validation helper functions *
3180 * *
3181 ************************************************************************/
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003182
Daniel Veillardc0826a72004-08-10 14:17:33 +00003183
Daniel Veillard4255d502002-04-16 15:50:10 +00003184/************************************************************************
3185 * *
3186 * Allocation functions *
3187 * *
3188 ************************************************************************/
3189
3190/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003191 * xmlSchemaNewSchemaForParserCtxt:
William M. Brack08171912003-12-29 02:52:11 +00003192 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003193 *
3194 * Allocate a new Schema structure.
3195 *
3196 * Returns the newly allocated structure or NULL in case or error
3197 */
3198static xmlSchemaPtr
3199xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt)
3200{
3201 xmlSchemaPtr ret;
3202
3203 ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema));
3204 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003205 xmlSchemaPErrMemory(ctxt, "allocating schema", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003206 return (NULL);
3207 }
3208 memset(ret, 0, sizeof(xmlSchema));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003209 ret->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +00003210 xmlDictReference(ret->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00003211
3212 return (ret);
3213}
3214
3215/**
3216 * xmlSchemaNewFacet:
Daniel Veillard4255d502002-04-16 15:50:10 +00003217 *
3218 * Allocate a new Facet structure.
3219 *
3220 * Returns the newly allocated structure or NULL in case or error
3221 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003222xmlSchemaFacetPtr
3223xmlSchemaNewFacet(void)
Daniel Veillard4255d502002-04-16 15:50:10 +00003224{
3225 xmlSchemaFacetPtr ret;
3226
3227 ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet));
3228 if (ret == NULL) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003229 return (NULL);
3230 }
3231 memset(ret, 0, sizeof(xmlSchemaFacet));
3232
3233 return (ret);
3234}
3235
3236/**
3237 * xmlSchemaNewAnnot:
William M. Brack08171912003-12-29 02:52:11 +00003238 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003239 * @node: a node
3240 *
3241 * Allocate a new annotation structure.
3242 *
3243 * Returns the newly allocated structure or NULL in case or error
3244 */
3245static xmlSchemaAnnotPtr
3246xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
3247{
3248 xmlSchemaAnnotPtr ret;
3249
3250 ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot));
3251 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003252 xmlSchemaPErrMemory(ctxt, "allocating annotation", node);
Daniel Veillard4255d502002-04-16 15:50:10 +00003253 return (NULL);
3254 }
3255 memset(ret, 0, sizeof(xmlSchemaAnnot));
3256 ret->content = node;
3257 return (ret);
3258}
3259
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003260static xmlSchemaItemListPtr
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003261xmlSchemaItemListCreate(void)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003262{
3263 xmlSchemaItemListPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003264
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003265 ret = xmlMalloc(sizeof(xmlSchemaItemList));
3266 if (ret == NULL) {
3267 xmlSchemaPErrMemory(NULL,
3268 "allocating an item list structure", NULL);
3269 return (NULL);
3270 }
3271 memset(ret, 0, sizeof(xmlSchemaItemList));
3272 return (ret);
3273}
3274
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003275static void
3276xmlSchemaItemListClear(xmlSchemaItemListPtr list)
3277{
3278 if (list->items != NULL) {
3279 xmlFree(list->items);
3280 list->items = NULL;
3281 }
3282 list->nbItems = 0;
3283 list->sizeItems = 0;
3284}
3285
3286static int
3287xmlSchemaItemListAdd(xmlSchemaItemListPtr list, void *item)
3288{
3289 if (list->items == NULL) {
3290 list->items = (void **) xmlMalloc(
3291 20 * sizeof(void *));
3292 if (list->items == NULL) {
3293 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3294 return(-1);
3295 }
3296 list->sizeItems = 20;
3297 } else if (list->sizeItems <= list->nbItems) {
3298 list->sizeItems *= 2;
3299 list->items = (void **) xmlRealloc(list->items,
3300 list->sizeItems * sizeof(void *));
3301 if (list->items == NULL) {
3302 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3303 list->sizeItems = 0;
3304 return(-1);
3305 }
3306 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003307 list->items[list->nbItems++] = item;
3308 return(0);
3309}
3310
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003311static int
3312xmlSchemaItemListAddSize(xmlSchemaItemListPtr list,
3313 int initialSize,
3314 void *item)
3315{
3316 if (list->items == NULL) {
3317 if (initialSize <= 0)
3318 initialSize = 1;
3319 list->items = (void **) xmlMalloc(
3320 initialSize * sizeof(void *));
3321 if (list->items == NULL) {
3322 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3323 return(-1);
3324 }
3325 list->sizeItems = initialSize;
3326 } else if (list->sizeItems <= list->nbItems) {
3327 list->sizeItems *= 2;
3328 list->items = (void **) xmlRealloc(list->items,
3329 list->sizeItems * sizeof(void *));
3330 if (list->items == NULL) {
3331 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3332 list->sizeItems = 0;
3333 return(-1);
3334 }
3335 }
3336 list->items[list->nbItems++] = item;
3337 return(0);
3338}
3339
3340static int
3341xmlSchemaItemListInsert(xmlSchemaItemListPtr list, void *item, int idx)
3342{
3343 if (list->items == NULL) {
3344 list->items = (void **) xmlMalloc(
3345 20 * sizeof(void *));
3346 if (list->items == NULL) {
3347 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3348 return(-1);
3349 }
3350 list->sizeItems = 20;
3351 } else if (list->sizeItems <= list->nbItems) {
3352 list->sizeItems *= 2;
3353 list->items = (void **) xmlRealloc(list->items,
3354 list->sizeItems * sizeof(void *));
3355 if (list->items == NULL) {
3356 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3357 list->sizeItems = 0;
3358 return(-1);
3359 }
3360 }
3361 /*
3362 * Just append if the index is greater/equal than the item count.
3363 */
3364 if (idx >= list->nbItems) {
3365 list->items[list->nbItems++] = item;
3366 } else {
3367 int i;
3368 for (i = list->nbItems; i > idx; i--)
3369 list->items[i] = list->items[i-1];
3370 list->items[idx] = item;
3371 list->nbItems++;
3372 }
3373 return(0);
3374}
3375
3376#if 0 /* enable if ever needed */
3377static int
3378xmlSchemaItemListInsertSize(xmlSchemaItemListPtr list,
3379 int initialSize,
3380 void *item,
3381 int idx)
3382{
3383 if (list->items == NULL) {
3384 if (initialSize <= 0)
3385 initialSize = 1;
3386 list->items = (void **) xmlMalloc(
3387 initialSize * sizeof(void *));
3388 if (list->items == NULL) {
3389 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3390 return(-1);
3391 }
3392 list->sizeItems = initialSize;
3393 } else if (list->sizeItems <= list->nbItems) {
3394 list->sizeItems *= 2;
3395 list->items = (void **) xmlRealloc(list->items,
3396 list->sizeItems * sizeof(void *));
3397 if (list->items == NULL) {
3398 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3399 list->sizeItems = 0;
3400 return(-1);
3401 }
3402 }
3403 /*
3404 * Just append if the index is greater/equal than the item count.
3405 */
3406 if (idx >= list->nbItems) {
3407 list->items[list->nbItems++] = item;
3408 } else {
3409 int i;
3410 for (i = list->nbItems; i > idx; i--)
3411 list->items[i] = list->items[i-1];
3412 list->items[idx] = item;
3413 list->nbItems++;
3414 }
3415 return(0);
3416}
3417#endif
3418
3419static int
3420xmlSchemaItemListRemove(xmlSchemaItemListPtr list, int idx)
3421{
3422 int i;
3423 if ((list->items == NULL) || (idx >= list->nbItems)) {
3424 xmlSchemaPSimpleErr("Internal error: xmlSchemaItemListRemove, "
3425 "index error.\n");
3426 return(-1);
3427 }
3428
3429 if (list->nbItems == 1) {
3430 /* TODO: Really free the list? */
3431 xmlFree(list->items);
3432 list->items = NULL;
3433 list->nbItems = 0;
3434 list->sizeItems = 0;
3435 } else if (list->nbItems -1 == idx) {
3436 list->nbItems--;
3437 } else {
3438 for (i = idx; i < list->nbItems -1; i++)
3439 list->items[i] = list->items[i+1];
3440 list->nbItems--;
3441 }
3442 return(0);
3443}
3444
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003445/**
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003446 * xmlSchemaItemListFree:
3447 * @annot: a schema type structure
3448 *
3449 * Deallocate a annotation structure
3450 */
3451static void
3452xmlSchemaItemListFree(xmlSchemaItemListPtr list)
3453{
3454 if (list == NULL)
3455 return;
3456 if (list->items != NULL)
3457 xmlFree(list->items);
3458 xmlFree(list);
3459}
3460
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003461static void
3462xmlSchemaBucketFree(xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003463{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003464 if (bucket == NULL)
3465 return;
3466 if (bucket->globals != NULL) {
3467 xmlSchemaComponentListFree(bucket->globals);
3468 xmlSchemaItemListFree(bucket->globals);
3469 }
3470 if (bucket->locals != NULL) {
3471 xmlSchemaComponentListFree(bucket->locals);
3472 xmlSchemaItemListFree(bucket->locals);
3473 }
3474 if (bucket->relations != NULL) {
3475 xmlSchemaSchemaRelationPtr prev, cur = bucket->relations;
3476 do {
3477 prev = cur;
3478 cur = cur->next;
3479 xmlFree(prev);
3480 } while (cur != NULL);
3481 }
3482 if ((! bucket->preserveDoc) && (bucket->doc != NULL)) {
3483 xmlFreeDoc(bucket->doc);
3484 }
3485 if (bucket->type == XML_SCHEMA_SCHEMA_IMPORT) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003486 if (WXS_IMPBUCKET(bucket)->schema != NULL)
3487 xmlSchemaFree(WXS_IMPBUCKET(bucket)->schema);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003488 }
3489 xmlFree(bucket);
3490}
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003491
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003492static xmlSchemaBucketPtr
3493xmlSchemaBucketCreate(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003494 int type, const xmlChar *targetNamespace)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003495{
3496 xmlSchemaBucketPtr ret;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003497 int size;
3498 xmlSchemaPtr mainSchema;
3499
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003500 if (WXS_CONSTRUCTOR(pctxt)->mainSchema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003501 PERROR_INT("xmlSchemaBucketCreate",
3502 "no main schema on constructor");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003503 return(NULL);
3504 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003505 mainSchema = WXS_CONSTRUCTOR(pctxt)->mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003506 /* Create the schema bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003507 if (WXS_IS_BUCKET_INCREDEF(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003508 size = sizeof(xmlSchemaInclude);
3509 else
3510 size = sizeof(xmlSchemaImport);
3511 ret = (xmlSchemaBucketPtr) xmlMalloc(size);
3512 if (ret == NULL) {
3513 xmlSchemaPErrMemory(NULL, "allocating schema bucket", NULL);
3514 return(NULL);
3515 }
3516 memset(ret, 0, size);
3517 ret->targetNamespace = targetNamespace;
3518 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003519 ret->globals = xmlSchemaItemListCreate();
3520 if (ret->globals == NULL) {
3521 xmlFree(ret);
3522 return(NULL);
3523 }
3524 ret->locals = xmlSchemaItemListCreate();
3525 if (ret->locals == NULL) {
3526 xmlFree(ret);
3527 return(NULL);
3528 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003529 /*
3530 * The following will assure that only the first bucket is marked as
3531 * XML_SCHEMA_SCHEMA_MAIN and it points to the *main* schema.
3532 * For each following import buckets an xmlSchema will be created.
3533 */
3534 if (! WXS_HAS_BUCKETS(pctxt)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003535 if (WXS_IS_BUCKET_INCREDEF(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003536 PERROR_INT("xmlSchemaBucketCreate",
3537 "first bucket but it's an include or redefine");
3538 xmlSchemaBucketFree(ret);
3539 return(NULL);
3540 }
3541 /* Force the type to be XML_SCHEMA_SCHEMA_MAIN. */
3542 ret->type = XML_SCHEMA_SCHEMA_MAIN;
3543 /* Point to the *main* schema. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003544 WXS_CONSTRUCTOR(pctxt)->mainBucket = ret;
3545 WXS_IMPBUCKET(ret)->schema = mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003546 } else {
3547 if (type == XML_SCHEMA_SCHEMA_MAIN) {
3548 PERROR_INT("xmlSchemaBucketCreate",
3549 "main bucket but it's not the first one");
3550 xmlSchemaBucketFree(ret);
3551 return(NULL);
3552 } else if (type == XML_SCHEMA_SCHEMA_IMPORT) {
3553 /*
3554 * Create a schema for imports.
3555 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003556 WXS_IMPBUCKET(ret)->schema = xmlSchemaNewSchema(pctxt);
3557 if (WXS_IMPBUCKET(ret)->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003558 xmlSchemaBucketFree(ret);
3559 return(NULL);
3560 }
3561 }
3562 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003563 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003564 int res;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003565 /* Imports go into the "schemasImports" slot of the main *schema*. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003566 if (mainSchema->schemasImports == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003567 mainSchema->schemasImports = xmlHashCreateDict(5,
3568 WXS_CONSTRUCTOR(pctxt)->dict);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003569 if (mainSchema->schemasImports == NULL) {
3570 xmlSchemaBucketFree(ret);
3571 return(NULL);
3572 }
3573 }
3574 if (targetNamespace == NULL)
3575 res = xmlHashAddEntry(mainSchema->schemasImports,
3576 XML_SCHEMAS_NO_NAMESPACE, ret);
3577 else
3578 res = xmlHashAddEntry(mainSchema->schemasImports,
3579 targetNamespace, ret);
3580 if (res != 0) {
3581 PERROR_INT("xmlSchemaBucketCreate",
3582 "failed to add the schema bucket to the hash");
3583 xmlSchemaBucketFree(ret);
3584 return(NULL);
3585 }
3586 } else {
3587 /* Set the @ownerImport of an include bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003588 if (WXS_IS_BUCKET_IMPMAIN(WXS_CONSTRUCTOR(pctxt)->bucket->type))
3589 WXS_INCBUCKET(ret)->ownerImport =
3590 WXS_IMPBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003591 else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003592 WXS_INCBUCKET(ret)->ownerImport =
3593 WXS_INCBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket)->ownerImport;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003594
3595 /* Includes got into the "includes" slot of the *main* schema. */
3596 if (mainSchema->includes == NULL) {
3597 mainSchema->includes = xmlSchemaItemListCreate();
3598 if (mainSchema->includes == NULL) {
3599 xmlSchemaBucketFree(ret);
3600 return(NULL);
3601 }
3602 }
3603 xmlSchemaItemListAdd(mainSchema->includes, ret);
3604 }
3605 /*
3606 * Add to list of all buckets; this is used for lookup
3607 * during schema construction time only.
3608 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003609 if (xmlSchemaItemListAdd(WXS_CONSTRUCTOR(pctxt)->buckets, ret) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003610 return(NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003611 return(ret);
3612}
3613
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003614static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003615xmlSchemaAddItemSize(xmlSchemaItemListPtr *list, int initialSize, void *item)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003616{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003617 if (*list == NULL) {
3618 *list = xmlSchemaItemListCreate();
3619 if (*list == NULL)
3620 return(-1);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003621 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003622 xmlSchemaItemListAddSize(*list, initialSize, item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003623 return(0);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003624}
3625
3626/**
Daniel Veillardfdc91562002-07-01 21:52:03 +00003627 * xmlSchemaFreeAnnot:
3628 * @annot: a schema type structure
3629 *
3630 * Deallocate a annotation structure
3631 */
3632static void
3633xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot)
3634{
3635 if (annot == NULL)
3636 return;
Kasimier T. Buchcik004b5462005-08-08 12:43:09 +00003637 if (annot->next == NULL) {
3638 xmlFree(annot);
3639 } else {
3640 xmlSchemaAnnotPtr prev;
3641
3642 do {
3643 prev = annot;
3644 annot = annot->next;
3645 xmlFree(prev);
3646 } while (annot != NULL);
3647 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00003648}
3649
3650/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003651 * xmlSchemaFreeNotation:
3652 * @schema: a schema notation structure
3653 *
3654 * Deallocate a Schema Notation structure.
3655 */
3656static void
3657xmlSchemaFreeNotation(xmlSchemaNotationPtr nota)
3658{
3659 if (nota == NULL)
3660 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003661 xmlFree(nota);
3662}
3663
3664/**
3665 * xmlSchemaFreeAttribute:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003666 * @attr: an attribute declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00003667 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003668 * Deallocates an attribute declaration structure.
Daniel Veillard4255d502002-04-16 15:50:10 +00003669 */
3670static void
3671xmlSchemaFreeAttribute(xmlSchemaAttributePtr attr)
3672{
3673 if (attr == NULL)
3674 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003675 if (attr->annot != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003676 xmlSchemaFreeAnnot(attr->annot);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003677 if (attr->defVal != NULL)
3678 xmlSchemaFreeValue(attr->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003679 xmlFree(attr);
3680}
3681
3682/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003683 * xmlSchemaFreeAttributeUse:
3684 * @use: an attribute use
3685 *
3686 * Deallocates an attribute use structure.
3687 */
3688static void
3689xmlSchemaFreeAttributeUse(xmlSchemaAttributeUsePtr use)
3690{
3691 if (use == NULL)
3692 return;
3693 if (use->annot != NULL)
3694 xmlSchemaFreeAnnot(use->annot);
3695 if (use->defVal != NULL)
3696 xmlSchemaFreeValue(use->defVal);
3697 xmlFree(use);
3698}
3699
3700/**
3701 * xmlSchemaFreeAttributeUseProhib:
3702 * @prohib: an attribute use prohibition
3703 *
3704 * Deallocates an attribute use structure.
3705 */
3706static void
3707xmlSchemaFreeAttributeUseProhib(xmlSchemaAttributeUseProhibPtr prohib)
3708{
3709 if (prohib == NULL)
3710 return;
3711 xmlFree(prohib);
3712}
3713
3714/**
Daniel Veillard3646d642004-06-02 19:19:14 +00003715 * xmlSchemaFreeWildcardNsSet:
3716 * set: a schema wildcard namespace
3717 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003718 * Deallocates a list of wildcard constraint structures.
Daniel Veillard3646d642004-06-02 19:19:14 +00003719 */
3720static void
3721xmlSchemaFreeWildcardNsSet(xmlSchemaWildcardNsPtr set)
3722{
3723 xmlSchemaWildcardNsPtr next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003724
Daniel Veillard3646d642004-06-02 19:19:14 +00003725 while (set != NULL) {
3726 next = set->next;
3727 xmlFree(set);
3728 set = next;
3729 }
3730}
3731
3732/**
3733 * xmlSchemaFreeWildcard:
Daniel Veillard01fa6152004-06-29 17:04:39 +00003734 * @wildcard: a wildcard structure
Daniel Veillard3646d642004-06-02 19:19:14 +00003735 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003736 * Deallocates a wildcard structure.
Daniel Veillard3646d642004-06-02 19:19:14 +00003737 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00003738void
Daniel Veillard3646d642004-06-02 19:19:14 +00003739xmlSchemaFreeWildcard(xmlSchemaWildcardPtr wildcard)
3740{
3741 if (wildcard == NULL)
3742 return;
3743 if (wildcard->annot != NULL)
3744 xmlSchemaFreeAnnot(wildcard->annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003745 if (wildcard->nsSet != NULL)
3746 xmlSchemaFreeWildcardNsSet(wildcard->nsSet);
3747 if (wildcard->negNsSet != NULL)
3748 xmlFree(wildcard->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +00003749 xmlFree(wildcard);
3750}
3751
3752/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003753 * xmlSchemaFreeAttributeGroup:
3754 * @schema: a schema attribute group structure
3755 *
3756 * Deallocate a Schema Attribute Group structure.
3757 */
3758static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003759xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillard4255d502002-04-16 15:50:10 +00003760{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003761 if (attrGr == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00003762 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003763 if (attrGr->annot != NULL)
3764 xmlSchemaFreeAnnot(attrGr->annot);
3765 if (attrGr->attrUses != NULL)
3766 xmlSchemaItemListFree(WXS_LIST_CAST attrGr->attrUses);
3767 xmlFree(attrGr);
Daniel Veillard3646d642004-06-02 19:19:14 +00003768}
3769
3770/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003771 * xmlSchemaFreeQNameRef:
3772 * @item: a QName reference structure
3773 *
3774 * Deallocatea a QName reference structure.
3775 */
3776static void
3777xmlSchemaFreeQNameRef(xmlSchemaQNameRefPtr item)
3778{
3779 xmlFree(item);
3780}
3781
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003782/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00003783 * xmlSchemaFreeTypeLinkList:
3784 * @alink: a type link
3785 *
3786 * Deallocate a list of types.
3787 */
3788static void
3789xmlSchemaFreeTypeLinkList(xmlSchemaTypeLinkPtr link)
3790{
3791 xmlSchemaTypeLinkPtr next;
3792
3793 while (link != NULL) {
3794 next = link->next;
3795 xmlFree(link);
3796 link = next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003797 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003798}
3799
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003800static void
3801xmlSchemaFreeIDCStateObjList(xmlSchemaIDCStateObjPtr sto)
3802{
3803 xmlSchemaIDCStateObjPtr next;
3804 while (sto != NULL) {
3805 next = sto->next;
3806 if (sto->history != NULL)
3807 xmlFree(sto->history);
3808 if (sto->xpathCtxt != NULL)
3809 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
3810 xmlFree(sto);
3811 sto = next;
3812 }
3813}
3814
3815/**
3816 * xmlSchemaFreeIDC:
3817 * @idc: a identity-constraint definition
3818 *
3819 * Deallocates an identity-constraint definition.
3820 */
3821static void
3822xmlSchemaFreeIDC(xmlSchemaIDCPtr idcDef)
3823{
3824 xmlSchemaIDCSelectPtr cur, prev;
3825
3826 if (idcDef == NULL)
3827 return;
3828 if (idcDef->annot != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003829 xmlSchemaFreeAnnot(idcDef->annot);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003830 /* Selector */
3831 if (idcDef->selector != NULL) {
3832 if (idcDef->selector->xpathComp != NULL)
3833 xmlFreePattern((xmlPatternPtr) idcDef->selector->xpathComp);
3834 xmlFree(idcDef->selector);
3835 }
3836 /* Fields */
3837 if (idcDef->fields != NULL) {
3838 cur = idcDef->fields;
3839 do {
3840 prev = cur;
3841 cur = cur->next;
3842 if (prev->xpathComp != NULL)
3843 xmlFreePattern((xmlPatternPtr) prev->xpathComp);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003844 xmlFree(prev);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003845 } while (cur != NULL);
3846 }
3847 xmlFree(idcDef);
3848}
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003849
Daniel Veillard01fa6152004-06-29 17:04:39 +00003850/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003851 * xmlSchemaFreeElement:
3852 * @schema: a schema element structure
3853 *
3854 * Deallocate a Schema Element structure.
3855 */
3856static void
3857xmlSchemaFreeElement(xmlSchemaElementPtr elem)
3858{
3859 if (elem == NULL)
3860 return;
Daniel Veillard32370232002-10-16 14:08:14 +00003861 if (elem->annot != NULL)
3862 xmlSchemaFreeAnnot(elem->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003863 if (elem->contModel != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003864 xmlRegFreeRegexp(elem->contModel);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003865 if (elem->defVal != NULL)
3866 xmlSchemaFreeValue(elem->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003867 xmlFree(elem);
3868}
3869
3870/**
3871 * xmlSchemaFreeFacet:
3872 * @facet: a schema facet structure
3873 *
3874 * Deallocate a Schema Facet structure.
3875 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003876void
Daniel Veillard4255d502002-04-16 15:50:10 +00003877xmlSchemaFreeFacet(xmlSchemaFacetPtr facet)
3878{
3879 if (facet == NULL)
3880 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003881 if (facet->val != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003882 xmlSchemaFreeValue(facet->val);
Daniel Veillard4255d502002-04-16 15:50:10 +00003883 if (facet->regexp != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003884 xmlRegFreeRegexp(facet->regexp);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003885 if (facet->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003886 xmlSchemaFreeAnnot(facet->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003887 xmlFree(facet);
3888}
3889
3890/**
3891 * xmlSchemaFreeType:
3892 * @type: a schema type structure
3893 *
3894 * Deallocate a Schema Type structure.
3895 */
3896void
3897xmlSchemaFreeType(xmlSchemaTypePtr type)
3898{
3899 if (type == NULL)
3900 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003901 if (type->annot != NULL)
Daniel Veillard32370232002-10-16 14:08:14 +00003902 xmlSchemaFreeAnnot(type->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003903 if (type->facets != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003904 xmlSchemaFacetPtr facet, next;
Daniel Veillard4255d502002-04-16 15:50:10 +00003905
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003906 facet = type->facets;
3907 while (facet != NULL) {
3908 next = facet->next;
3909 xmlSchemaFreeFacet(facet);
3910 facet = next;
3911 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003912 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003913 if (type->attrUses != NULL)
3914 xmlSchemaItemListFree((xmlSchemaItemListPtr) type->attrUses);
Daniel Veillard01fa6152004-06-29 17:04:39 +00003915 if (type->memberTypes != NULL)
3916 xmlSchemaFreeTypeLinkList(type->memberTypes);
3917 if (type->facetSet != NULL) {
3918 xmlSchemaFacetLinkPtr next, link;
3919
3920 link = type->facetSet;
3921 do {
3922 next = link->next;
3923 xmlFree(link);
3924 link = next;
3925 } while (link != NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003926 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00003927 if (type->contModel != NULL)
3928 xmlRegFreeRegexp(type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +00003929 xmlFree(type);
3930}
3931
3932/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003933 * xmlSchemaFreeModelGroupDef:
3934 * @item: a schema model group definition
3935 *
3936 * Deallocates a schema model group definition.
3937 */
3938static void
3939xmlSchemaFreeModelGroupDef(xmlSchemaModelGroupDefPtr item)
3940{
3941 if (item->annot != NULL)
3942 xmlSchemaFreeAnnot(item->annot);
3943 xmlFree(item);
3944}
3945
3946/**
3947 * xmlSchemaFreeModelGroup:
3948 * @item: a schema model group
3949 *
3950 * Deallocates a schema model group structure.
3951 */
3952static void
3953xmlSchemaFreeModelGroup(xmlSchemaModelGroupPtr item)
3954{
3955 if (item->annot != NULL)
3956 xmlSchemaFreeAnnot(item->annot);
3957 xmlFree(item);
3958}
3959
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003960static void
3961xmlSchemaComponentListFree(xmlSchemaItemListPtr list)
3962{
3963 if ((list == NULL) || (list->nbItems == 0))
3964 return;
3965 {
3966 xmlSchemaTreeItemPtr item;
3967 xmlSchemaTreeItemPtr *items = (xmlSchemaTreeItemPtr *) list->items;
3968 int i;
3969
3970 for (i = 0; i < list->nbItems; i++) {
3971 item = items[i];
3972 if (item == NULL)
3973 continue;
3974 switch (item->type) {
3975 case XML_SCHEMA_TYPE_SIMPLE:
3976 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003977 xmlSchemaFreeType((xmlSchemaTypePtr) item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003978 break;
3979 case XML_SCHEMA_TYPE_ATTRIBUTE:
3980 xmlSchemaFreeAttribute((xmlSchemaAttributePtr) item);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003981 break;
3982 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
3983 xmlSchemaFreeAttributeUse((xmlSchemaAttributeUsePtr) item);
3984 break;
3985 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
3986 xmlSchemaFreeAttributeUseProhib(
3987 (xmlSchemaAttributeUseProhibPtr) item);
3988 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003989 case XML_SCHEMA_TYPE_ELEMENT:
3990 xmlSchemaFreeElement((xmlSchemaElementPtr) item);
3991 break;
3992 case XML_SCHEMA_TYPE_PARTICLE:
3993 if (item->annot != NULL)
3994 xmlSchemaFreeAnnot(item->annot);
3995 xmlFree(item);
3996 break;
3997 case XML_SCHEMA_TYPE_SEQUENCE:
3998 case XML_SCHEMA_TYPE_CHOICE:
3999 case XML_SCHEMA_TYPE_ALL:
4000 xmlSchemaFreeModelGroup((xmlSchemaModelGroupPtr) item);
4001 break;
4002 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
4003 xmlSchemaFreeAttributeGroup(
4004 (xmlSchemaAttributeGroupPtr) item);
4005 break;
4006 case XML_SCHEMA_TYPE_GROUP:
4007 xmlSchemaFreeModelGroupDef(
4008 (xmlSchemaModelGroupDefPtr) item);
4009 break;
4010 case XML_SCHEMA_TYPE_ANY:
4011 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
4012 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr) item);
4013 break;
4014 case XML_SCHEMA_TYPE_IDC_KEY:
4015 case XML_SCHEMA_TYPE_IDC_UNIQUE:
4016 case XML_SCHEMA_TYPE_IDC_KEYREF:
4017 xmlSchemaFreeIDC((xmlSchemaIDCPtr) item);
4018 break;
4019 case XML_SCHEMA_TYPE_NOTATION:
4020 xmlSchemaFreeNotation((xmlSchemaNotationPtr) item);
4021 break;
4022 case XML_SCHEMA_EXTRA_QNAMEREF:
4023 xmlSchemaFreeQNameRef((xmlSchemaQNameRefPtr) item);
4024 break;
4025 default: {
4026 /* TODO: This should never be hit. */
4027 xmlSchemaPSimpleInternalErr(NULL,
4028 "Internal error: xmlSchemaComponentListFree, "
4029 "unexpected component type '%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004030 (const xmlChar *) WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004031 }
4032 break;
4033 }
4034 }
4035 list->nbItems = 0;
4036 }
4037}
4038
Daniel Veillardb0f397e2003-12-23 23:30:53 +00004039/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004040 * xmlSchemaFree:
4041 * @schema: a schema structure
4042 *
4043 * Deallocate a Schema structure.
4044 */
4045void
4046xmlSchemaFree(xmlSchemaPtr schema)
4047{
4048 if (schema == NULL)
4049 return;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004050 /* @volatiles is not used anymore :-/ */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004051 if (schema->volatiles != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004052 TODO
4053 /*
4054 * Note that those slots are not responsible for freeing
4055 * schema components anymore; this will now be done by
4056 * the schema buckets.
4057 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004058 if (schema->notaDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004059 xmlHashFree(schema->notaDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004060 if (schema->attrDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004061 xmlHashFree(schema->attrDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004062 if (schema->attrgrpDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004063 xmlHashFree(schema->attrgrpDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004064 if (schema->elemDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004065 xmlHashFree(schema->elemDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004066 if (schema->typeDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004067 xmlHashFree(schema->typeDecl, NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004068 if (schema->groupDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004069 xmlHashFree(schema->groupDecl, NULL);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004070 if (schema->idcDef != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004071 xmlHashFree(schema->idcDef, NULL);
4072
Daniel Veillard1d913862003-11-21 00:28:39 +00004073 if (schema->schemasImports != NULL)
4074 xmlHashFree(schema->schemasImports,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004075 (xmlHashDeallocator) xmlSchemaBucketFree);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004076 if (schema->includes != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004077 xmlSchemaItemListPtr list = (xmlSchemaItemListPtr) schema->includes;
4078 int i;
4079 for (i = 0; i < list->nbItems; i++) {
4080 xmlSchemaBucketFree((xmlSchemaBucketPtr) list->items[i]);
4081 }
4082 xmlSchemaItemListFree(list);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004083 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004084 if (schema->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004085 xmlSchemaFreeAnnot(schema->annot);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004086 /* Never free the doc here, since this will be done by the buckets. */
4087
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004088 xmlDictFree(schema->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00004089 xmlFree(schema);
4090}
4091
4092/************************************************************************
4093 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00004094 * Debug functions *
4095 * *
4096 ************************************************************************/
4097
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004098#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004099
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004100static void
4101xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output); /* forward */
4102
Daniel Veillard4255d502002-04-16 15:50:10 +00004103/**
4104 * xmlSchemaElementDump:
4105 * @elem: an element
4106 * @output: the file output
4107 *
4108 * Dump the element
4109 */
4110static void
4111xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004112 const xmlChar * name ATTRIBUTE_UNUSED,
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004113 const xmlChar * namespace ATTRIBUTE_UNUSED,
4114 const xmlChar * context ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00004115{
4116 if (elem == NULL)
4117 return;
4118
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004119
4120 fprintf(output, "Element");
4121 if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL)
4122 fprintf(output, " (global)");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004123 fprintf(output, ": '%s' ", elem->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004124 if (namespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004125 fprintf(output, "ns '%s'", namespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004126 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004127#if 0
Daniel Veillard4255d502002-04-16 15:50:10 +00004128 if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004129 fprintf(output, " min %d ", elem->minOccurs);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004130 if (elem->maxOccurs >= UNBOUNDED)
4131 fprintf(output, "max: unbounded\n");
4132 else if (elem->maxOccurs != 1)
4133 fprintf(output, "max: %d\n", elem->maxOccurs);
4134 else
4135 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004136 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004137#endif
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004138 /*
4139 * Misc other properties.
4140 */
4141 if ((elem->flags & XML_SCHEMAS_ELEM_NILLABLE) ||
4142 (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) ||
4143 (elem->flags & XML_SCHEMAS_ELEM_FIXED) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004144 (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004145 fprintf(output, " props: ");
4146 if (elem->flags & XML_SCHEMAS_ELEM_FIXED)
4147 fprintf(output, "[fixed] ");
4148 if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)
4149 fprintf(output, "[default] ");
4150 if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT)
4151 fprintf(output, "[abstract] ");
4152 if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE)
4153 fprintf(output, "[nillable] ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004154 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004155 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004156 /*
4157 * Default/fixed value.
4158 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004159 if (elem->value != NULL)
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004160 fprintf(output, " value: '%s'\n", elem->value);
4161 /*
4162 * Type.
4163 */
4164 if (elem->namedType != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004165 fprintf(output, " type: '%s' ", elem->namedType);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004166 if (elem->namedTypeNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004167 fprintf(output, "ns '%s'\n", elem->namedTypeNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004168 else
4169 fprintf(output, "\n");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004170 } else if (elem->subtypes != NULL) {
4171 /*
4172 * Dump local types.
4173 */
4174 xmlSchemaTypeDump(elem->subtypes, output);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004175 }
4176 /*
4177 * Substitution group.
4178 */
4179 if (elem->substGroup != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004180 fprintf(output, " substitutionGroup: '%s' ", elem->substGroup);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004181 if (elem->substGroupNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004182 fprintf(output, "ns '%s'\n", elem->substGroupNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004183 else
4184 fprintf(output, "\n");
4185 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004186}
4187
4188/**
4189 * xmlSchemaAnnotDump:
4190 * @output: the file output
4191 * @annot: a annotation
4192 *
4193 * Dump the annotation
4194 */
4195static void
4196xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot)
4197{
4198 xmlChar *content;
4199
4200 if (annot == NULL)
4201 return;
4202
4203 content = xmlNodeGetContent(annot->content);
4204 if (content != NULL) {
4205 fprintf(output, " Annot: %s\n", content);
4206 xmlFree(content);
4207 } else
4208 fprintf(output, " Annot: empty\n");
4209}
4210
4211/**
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004212 * xmlSchemaContentModelDump:
4213 * @particle: the schema particle
4214 * @output: the file output
4215 * @depth: the depth used for intentation
Daniel Veillard4255d502002-04-16 15:50:10 +00004216 *
4217 * Dump a SchemaType structure
4218 */
4219static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004220xmlSchemaContentModelDump(xmlSchemaParticlePtr particle, FILE * output, int depth)
4221{
4222 xmlChar *str = NULL;
4223 xmlSchemaTreeItemPtr term;
4224 char shift[100];
4225 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004226
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004227 if (particle == NULL)
4228 return;
4229 for (i = 0;((i < depth) && (i < 25));i++)
4230 shift[2 * i] = shift[2 * i + 1] = ' ';
4231 shift[2 * i] = shift[2 * i + 1] = 0;
4232 fprintf(output, shift);
4233 if (particle->children == NULL) {
4234 fprintf(output, "MISSING particle term\n");
4235 return;
4236 }
4237 term = particle->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004238 if (term == NULL) {
4239 fprintf(output, "(NULL)");
4240 } else {
4241 switch (term->type) {
4242 case XML_SCHEMA_TYPE_ELEMENT:
4243 fprintf(output, "ELEM '%s'", xmlSchemaFormatQName(&str,
4244 ((xmlSchemaElementPtr)term)->targetNamespace,
4245 ((xmlSchemaElementPtr)term)->name));
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004246 FREE_AND_NULL(str);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004247 break;
4248 case XML_SCHEMA_TYPE_SEQUENCE:
4249 fprintf(output, "SEQUENCE");
4250 break;
4251 case XML_SCHEMA_TYPE_CHOICE:
4252 fprintf(output, "CHOICE");
4253 break;
4254 case XML_SCHEMA_TYPE_ALL:
4255 fprintf(output, "ALL");
4256 break;
4257 case XML_SCHEMA_TYPE_ANY:
4258 fprintf(output, "ANY");
4259 break;
4260 default:
4261 fprintf(output, "UNKNOWN\n");
4262 return;
4263 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004264 }
4265 if (particle->minOccurs != 1)
4266 fprintf(output, " min: %d", particle->minOccurs);
4267 if (particle->maxOccurs >= UNBOUNDED)
4268 fprintf(output, " max: unbounded");
4269 else if (particle->maxOccurs != 1)
4270 fprintf(output, " max: %d", particle->maxOccurs);
4271 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004272 if (term &&
4273 ((term->type == XML_SCHEMA_TYPE_SEQUENCE) ||
4274 (term->type == XML_SCHEMA_TYPE_CHOICE) ||
4275 (term->type == XML_SCHEMA_TYPE_ALL)) &&
4276 (term->children != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004277 xmlSchemaContentModelDump((xmlSchemaParticlePtr) term->children,
4278 output, depth +1);
4279 }
4280 if (particle->next != NULL)
4281 xmlSchemaContentModelDump((xmlSchemaParticlePtr) particle->next,
4282 output, depth);
4283}
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004284
4285/**
4286 * xmlSchemaAttrUsesDump:
4287 * @uses: attribute uses list
4288 * @output: the file output
4289 *
4290 * Dumps a list of attribute use components.
4291 */
4292static void
4293xmlSchemaAttrUsesDump(xmlSchemaItemListPtr uses, FILE * output)
4294{
4295 xmlSchemaAttributeUsePtr use;
4296 xmlSchemaAttributeUseProhibPtr prohib;
4297 xmlSchemaQNameRefPtr ref;
4298 const xmlChar *name, *tns;
4299 xmlChar *str = NULL;
4300 int i;
4301
4302 if ((uses == NULL) || (uses->nbItems == 0))
4303 return;
4304
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004305 fprintf(output, " attributes:\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004306 for (i = 0; i < uses->nbItems; i++) {
4307 use = uses->items[i];
4308 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
4309 fprintf(output, " [prohibition] ");
4310 prohib = (xmlSchemaAttributeUseProhibPtr) use;
4311 name = prohib->name;
4312 tns = prohib->targetNamespace;
4313 } else if (use->type == XML_SCHEMA_EXTRA_QNAMEREF) {
4314 fprintf(output, " [reference] ");
4315 ref = (xmlSchemaQNameRefPtr) use;
4316 name = ref->name;
4317 tns = ref->targetNamespace;
4318 } else {
4319 fprintf(output, " [use] ");
4320 name = WXS_ATTRUSE_DECL_NAME(use);
4321 tns = WXS_ATTRUSE_DECL_TNS(use);
4322 }
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004323 fprintf(output, "'%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004324 (const char *) xmlSchemaFormatQName(&str, tns, name));
4325 FREE_AND_NULL(str);
4326 }
4327}
4328
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004329/**
4330 * xmlSchemaTypeDump:
4331 * @output: the file output
4332 * @type: a type structure
4333 *
4334 * Dump a SchemaType structure
4335 */
4336static void
Daniel Veillard4255d502002-04-16 15:50:10 +00004337xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output)
4338{
4339 if (type == NULL) {
4340 fprintf(output, "Type: NULL\n");
4341 return;
4342 }
4343 fprintf(output, "Type: ");
4344 if (type->name != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004345 fprintf(output, "'%s' ", type->name);
Daniel Veillard4255d502002-04-16 15:50:10 +00004346 else
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004347 fprintf(output, "(no name) ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004348 if (type->targetNamespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004349 fprintf(output, "ns '%s' ", type->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004350 switch (type->type) {
4351 case XML_SCHEMA_TYPE_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004352 fprintf(output, "[basic] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004353 break;
4354 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004355 fprintf(output, "[simple] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004356 break;
4357 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004358 fprintf(output, "[complex] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004359 break;
4360 case XML_SCHEMA_TYPE_SEQUENCE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004361 fprintf(output, "[sequence] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004362 break;
4363 case XML_SCHEMA_TYPE_CHOICE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004364 fprintf(output, "[choice] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004365 break;
4366 case XML_SCHEMA_TYPE_ALL:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004367 fprintf(output, "[all] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004368 break;
4369 case XML_SCHEMA_TYPE_UR:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004370 fprintf(output, "[ur] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004371 break;
4372 case XML_SCHEMA_TYPE_RESTRICTION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004373 fprintf(output, "[restriction] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004374 break;
4375 case XML_SCHEMA_TYPE_EXTENSION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004376 fprintf(output, "[extension] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004377 break;
4378 default:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004379 fprintf(output, "[unknown type %d] ", type->type);
Daniel Veillard4255d502002-04-16 15:50:10 +00004380 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004381 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004382 fprintf(output, "content: ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004383 switch (type->contentType) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004384 case XML_SCHEMA_CONTENT_UNKNOWN:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004385 fprintf(output, "[unknown] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004386 break;
4387 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004388 fprintf(output, "[empty] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004389 break;
4390 case XML_SCHEMA_CONTENT_ELEMENTS:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004391 fprintf(output, "[element] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004392 break;
4393 case XML_SCHEMA_CONTENT_MIXED:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004394 fprintf(output, "[mixed] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004395 break;
4396 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004397 /* not used. */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004398 break;
4399 case XML_SCHEMA_CONTENT_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004400 fprintf(output, "[basic] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004401 break;
4402 case XML_SCHEMA_CONTENT_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004403 fprintf(output, "[simple] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004404 break;
4405 case XML_SCHEMA_CONTENT_ANY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004406 fprintf(output, "[any] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004407 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004408 }
4409 fprintf(output, "\n");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004410 if (type->base != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004411 fprintf(output, " base type: '%s'", type->base);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004412 if (type->baseNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004413 fprintf(output, " ns '%s'\n", type->baseNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004414 else
4415 fprintf(output, "\n");
4416 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004417 if (type->attrUses != NULL)
4418 xmlSchemaAttrUsesDump(type->attrUses, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004419 if (type->annot != NULL)
4420 xmlSchemaAnnotDump(output, type->annot);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004421#ifdef DUMP_CONTENT_MODEL
4422 if ((type->type == XML_SCHEMA_TYPE_COMPLEX) &&
4423 (type->subtypes != NULL)) {
4424 xmlSchemaContentModelDump((xmlSchemaParticlePtr) type->subtypes,
4425 output, 1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004426 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004427#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004428}
4429
4430/**
4431 * xmlSchemaDump:
4432 * @output: the file output
4433 * @schema: a schema structure
4434 *
4435 * Dump a Schema structure.
4436 */
4437void
4438xmlSchemaDump(FILE * output, xmlSchemaPtr schema)
4439{
Daniel Veillardce682bc2004-11-05 17:22:25 +00004440 if (output == NULL)
4441 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004442 if (schema == NULL) {
4443 fprintf(output, "Schemas: NULL\n");
4444 return;
4445 }
4446 fprintf(output, "Schemas: ");
4447 if (schema->name != NULL)
4448 fprintf(output, "%s, ", schema->name);
4449 else
4450 fprintf(output, "no name, ");
4451 if (schema->targetNamespace != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00004452 fprintf(output, "%s", (const char *) schema->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004453 else
4454 fprintf(output, "no target namespace");
4455 fprintf(output, "\n");
4456 if (schema->annot != NULL)
4457 xmlSchemaAnnotDump(output, schema->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00004458 xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump,
4459 output);
4460 xmlHashScanFull(schema->elemDecl,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004461 (xmlHashScannerFull) xmlSchemaElementDump, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004462}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004463
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004464#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004465/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004466 * xmlSchemaDebugDumpIDCTable:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004467 * @vctxt: the WXS validation context
4468 *
4469 * Displays the current IDC table for debug purposes.
4470 */
4471static void
4472xmlSchemaDebugDumpIDCTable(FILE * output,
4473 const xmlChar *namespaceName,
4474 const xmlChar *localName,
4475 xmlSchemaPSVIIDCBindingPtr bind)
4476{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004477 xmlChar *str = NULL;
4478 const xmlChar *value;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004479 xmlSchemaPSVIIDCNodePtr tab;
4480 xmlSchemaPSVIIDCKeyPtr key;
4481 int i, j, res;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004482
4483 fprintf(output, "IDC: TABLES on %s\n",
4484 xmlSchemaFormatQName(&str, namespaceName, localName));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004485 FREE_AND_NULL(str)
4486
4487 if (bind == NULL)
4488 return;
4489 do {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004490 fprintf(output, "IDC: BINDING %s\n",
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004491 xmlSchemaGetComponentQName(&str,
4492 bind->definition));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004493 FREE_AND_NULL(str)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004494 for (i = 0; i < bind->nbNodes; i++) {
4495 tab = bind->nodeTable[i];
4496 fprintf(output, " ( ");
4497 for (j = 0; j < bind->definition->nbFields; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004498 key = tab->keys[j];
4499 if ((key != NULL) && (key->val != NULL)) {
4500 res = xmlSchemaGetCanonValue(key->val, &value);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004501 if (res >= 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004502 fprintf(output, "\"%s\" ", value);
4503 else
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004504 fprintf(output, "CANON-VALUE-FAILED ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004505 if (res == 0)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004506 FREE_AND_NULL(value)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004507 } else if (key != NULL)
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004508 fprintf(output, "(no val), ");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004509 else
4510 fprintf(output, "(key missing), ");
4511 }
4512 fprintf(output, ")\n");
4513 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004514 if (bind->dupls && bind->dupls->nbItems) {
4515 fprintf(output, "IDC: dupls:\n");
4516 for (i = 0; i < bind->dupls->nbItems; i++) {
4517 tab = bind->dupls->items[i];
4518 fprintf(output, " ( ");
4519 for (j = 0; j < bind->definition->nbFields; j++) {
4520 key = tab->keys[j];
4521 if ((key != NULL) && (key->val != NULL)) {
4522 res = xmlSchemaGetCanonValue(key->val, &value);
4523 if (res >= 0)
4524 fprintf(output, "\"%s\" ", value);
4525 else
4526 fprintf(output, "CANON-VALUE-FAILED ");
4527 if (res == 0)
4528 FREE_AND_NULL(value)
4529 } else if (key != NULL)
4530 fprintf(output, "(no val), ");
4531 else
4532 fprintf(output, "(key missing), ");
4533 }
4534 fprintf(output, ")\n");
4535 }
4536 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004537 bind = bind->next;
4538 } while (bind != NULL);
4539}
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00004540#endif /* DEBUG_IDC */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004541#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4255d502002-04-16 15:50:10 +00004542
4543/************************************************************************
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004544 * *
4545 * Utilities *
4546 * *
4547 ************************************************************************/
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004548
Daniel Veillardc0826a72004-08-10 14:17:33 +00004549/**
4550 * xmlSchemaGetPropNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004551 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004552 * @name: the name of the attribute
4553 *
4554 * Seeks an attribute with a name of @name in
4555 * no namespace.
4556 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004557 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004558 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004559static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004560xmlSchemaGetPropNode(xmlNodePtr node, const char *name)
Daniel Veillard01fa6152004-06-29 17:04:39 +00004561{
4562 xmlAttrPtr prop;
4563
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004564 if ((node == NULL) || (name == NULL))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004565 return(NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004566 prop = node->properties;
4567 while (prop != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004568 if ((prop->ns == NULL) && xmlStrEqual(prop->name, BAD_CAST name))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004569 return(prop);
4570 prop = prop->next;
4571 }
4572 return (NULL);
4573}
4574
4575/**
4576 * xmlSchemaGetPropNodeNs:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004577 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004578 * @uri: the uri
4579 * @name: the name of the attribute
4580 *
4581 * Seeks an attribute with a local name of @name and
4582 * a namespace URI of @uri.
4583 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004584 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004585 */
4586static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004587xmlSchemaGetPropNodeNs(xmlNodePtr node, const char *uri, const char *name)
Daniel Veillardc0826a72004-08-10 14:17:33 +00004588{
4589 xmlAttrPtr prop;
4590
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004591 if ((node == NULL) || (name == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004592 return(NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004593 prop = node->properties;
4594 while (prop != NULL) {
4595 if ((prop->ns != NULL) &&
4596 xmlStrEqual(prop->name, BAD_CAST name) &&
4597 xmlStrEqual(prop->ns->href, BAD_CAST uri))
Daniel Veillard01fa6152004-06-29 17:04:39 +00004598 return(prop);
4599 prop = prop->next;
4600 }
4601 return (NULL);
4602}
4603
4604static const xmlChar *
4605xmlSchemaGetNodeContent(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
4606{
4607 xmlChar *val;
4608 const xmlChar *ret;
4609
4610 val = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004611 if (val == NULL)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00004612 val = xmlStrdup((xmlChar *)"");
Daniel Veillard01fa6152004-06-29 17:04:39 +00004613 ret = xmlDictLookup(ctxt->dict, val, -1);
4614 xmlFree(val);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004615 return(ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004616}
4617
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004618static const xmlChar *
4619xmlSchemaGetNodeContentNoDict(xmlNodePtr node)
4620{
4621 return((const xmlChar*) xmlNodeGetContent(node));
4622}
4623
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004624/**
4625 * xmlSchemaGetProp:
4626 * @ctxt: the parser context
4627 * @node: the node
4628 * @name: the property name
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004629 *
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004630 * Read a attribute value and internalize the string
4631 *
4632 * Returns the string or NULL if not present.
4633 */
4634static const xmlChar *
4635xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
4636 const char *name)
4637{
4638 xmlChar *val;
4639 const xmlChar *ret;
4640
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004641 val = xmlGetNoNsProp(node, BAD_CAST name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004642 if (val == NULL)
4643 return(NULL);
4644 ret = xmlDictLookup(ctxt->dict, val, -1);
4645 xmlFree(val);
4646 return(ret);
4647}
4648
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004649/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +00004650 * *
4651 * Parsing functions *
4652 * *
4653 ************************************************************************/
4654
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004655#define WXS_FIND_GLOBAL_ITEM(slot) \
4656 if (xmlStrEqual(nsName, schema->targetNamespace)) { \
4657 ret = xmlHashLookup(schema->slot, name); \
4658 if (ret != NULL) goto exit; \
4659 } \
4660 if (xmlHashSize(schema->schemasImports) > 1) { \
4661 xmlSchemaImportPtr import; \
4662 if (nsName == NULL) \
4663 import = xmlHashLookup(schema->schemasImports, \
4664 XML_SCHEMAS_NO_NAMESPACE); \
4665 else \
4666 import = xmlHashLookup(schema->schemasImports, nsName); \
4667 if (import == NULL) \
4668 goto exit; \
4669 ret = xmlHashLookup(import->schema->slot, name); \
4670 }
4671
Daniel Veillard4255d502002-04-16 15:50:10 +00004672/**
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004673 * xmlSchemaGetElem:
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004674 * @schema: the schema context
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004675 * @name: the element name
4676 * @ns: the element namespace
4677 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004678 * Lookup a global element declaration in the schema.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004679 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004680 * Returns the element declaration or NULL if not found.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004681 */
4682static xmlSchemaElementPtr
4683xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004684 const xmlChar * nsName)
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004685{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004686 xmlSchemaElementPtr ret = NULL;
4687
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004688 if ((name == NULL) || (schema == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004689 return(NULL);
4690 if (schema != NULL) {
4691 WXS_FIND_GLOBAL_ITEM(elemDecl)
4692 }
4693exit:
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004694#ifdef DEBUG
4695 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004696 if (nsName == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004697 fprintf(stderr, "Unable to lookup element decl. %s", name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004698 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004699 fprintf(stderr, "Unable to lookup element decl. %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004700 nsName);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004701 }
4702#endif
4703 return (ret);
4704}
4705
4706/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004707 * xmlSchemaGetType:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004708 * @schema: the main schema
4709 * @name: the type's name
4710 * nsName: the type's namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00004711 *
4712 * Lookup a type in the schemas or the predefined types
4713 *
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004714 * Returns the group definition or NULL if not found.
Daniel Veillard4255d502002-04-16 15:50:10 +00004715 */
4716static xmlSchemaTypePtr
4717xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004718 const xmlChar * nsName)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004719{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004720 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00004721
4722 if (name == NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004723 return (NULL);
4724 /* First try the built-in types. */
4725 if ((nsName != NULL) && xmlStrEqual(nsName, xmlSchemaNs)) {
4726 ret = xmlSchemaGetPredefinedType(name, nsName);
4727 if (ret != NULL)
4728 goto exit;
4729 /*
4730 * Note that we try the parsed schemas as well here
4731 * since one might have parsed the S4S, which contain more
4732 * than the built-in types.
4733 * TODO: Can we optimize this?
4734 */
4735 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004736 if (schema != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004737 WXS_FIND_GLOBAL_ITEM(typeDecl)
4738 }
4739exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004740
Daniel Veillard4255d502002-04-16 15:50:10 +00004741#ifdef DEBUG
4742 if (ret == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004743 if (nsName == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004744 fprintf(stderr, "Unable to lookup type %s", name);
4745 else
4746 fprintf(stderr, "Unable to lookup type %s:%s", name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004747 nsName);
Daniel Veillard4255d502002-04-16 15:50:10 +00004748 }
4749#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004750 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004751}
4752
Daniel Veillard3646d642004-06-02 19:19:14 +00004753/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004754 * xmlSchemaGetAttributeDecl:
4755 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004756 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004757 * @ns: the target namespace of the attribute
Daniel Veillard3646d642004-06-02 19:19:14 +00004758 *
4759 * Lookup a an attribute in the schema or imported schemas
4760 *
4761 * Returns the attribute declaration or NULL if not found.
4762 */
4763static xmlSchemaAttributePtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004764xmlSchemaGetAttributeDecl(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004765 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004766{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004767 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004768
4769 if ((name == NULL) || (schema == NULL))
4770 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004771 if (schema != NULL) {
4772 WXS_FIND_GLOBAL_ITEM(attrDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004773 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004774exit:
Daniel Veillard3646d642004-06-02 19:19:14 +00004775#ifdef DEBUG
4776 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004777 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004778 fprintf(stderr, "Unable to lookup attribute %s", name);
4779 else
4780 fprintf(stderr, "Unable to lookup attribute %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004781 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004782 }
4783#endif
4784 return (ret);
4785}
4786
4787/**
4788 * xmlSchemaGetAttributeGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004789 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004790 * @name: the name of the attribute group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004791 * @ns: the target namespace of the attribute group
Daniel Veillard3646d642004-06-02 19:19:14 +00004792 *
4793 * Lookup a an attribute group in the schema or imported schemas
4794 *
4795 * Returns the attribute group definition or NULL if not found.
4796 */
4797static xmlSchemaAttributeGroupPtr
4798xmlSchemaGetAttributeGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004799 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004800{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004801 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004802
4803 if ((name == NULL) || (schema == NULL))
4804 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004805 if (schema != NULL) {
4806 WXS_FIND_GLOBAL_ITEM(attrgrpDecl)
4807 }
4808exit:
4809 /* TODO:
4810 if ((ret != NULL) && (ret->redef != NULL)) {
4811 * Return the last redefinition. *
4812 ret = ret->redef;
Daniel Veillard3646d642004-06-02 19:19:14 +00004813 }
William M. Brack2f2a6632004-08-20 23:09:47 +00004814 */
Daniel Veillard3646d642004-06-02 19:19:14 +00004815#ifdef DEBUG
4816 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004817 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004818 fprintf(stderr, "Unable to lookup attribute group %s", name);
4819 else
4820 fprintf(stderr, "Unable to lookup attribute group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004821 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004822 }
4823#endif
4824 return (ret);
4825}
4826
4827/**
4828 * xmlSchemaGetGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004829 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004830 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004831 * @ns: the target namespace of the group
Daniel Veillard3646d642004-06-02 19:19:14 +00004832 *
4833 * Lookup a group in the schema or imported schemas
4834 *
4835 * Returns the group definition or NULL if not found.
4836 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004837static xmlSchemaModelGroupDefPtr
Daniel Veillard3646d642004-06-02 19:19:14 +00004838xmlSchemaGetGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004839 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004840{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004841 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004842
4843 if ((name == NULL) || (schema == NULL))
4844 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004845 if (schema != NULL) {
4846 WXS_FIND_GLOBAL_ITEM(groupDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004847 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004848exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004849
Daniel Veillard3646d642004-06-02 19:19:14 +00004850#ifdef DEBUG
4851 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004852 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004853 fprintf(stderr, "Unable to lookup group %s", name);
4854 else
4855 fprintf(stderr, "Unable to lookup group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004856 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004857 }
4858#endif
4859 return (ret);
4860}
4861
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004862static xmlSchemaNotationPtr
4863xmlSchemaGetNotation(xmlSchemaPtr schema,
4864 const xmlChar *name,
4865 const xmlChar *nsName)
4866{
4867 xmlSchemaNotationPtr ret = NULL;
4868
4869 if ((name == NULL) || (schema == NULL))
4870 return (NULL);
4871 if (schema != NULL) {
4872 WXS_FIND_GLOBAL_ITEM(notaDecl)
4873 }
4874exit:
4875 return (ret);
4876}
4877
4878static xmlSchemaIDCPtr
4879xmlSchemaGetIDC(xmlSchemaPtr schema,
4880 const xmlChar *name,
4881 const xmlChar *nsName)
4882{
4883 xmlSchemaIDCPtr ret = NULL;
4884
4885 if ((name == NULL) || (schema == NULL))
4886 return (NULL);
4887 if (schema != NULL) {
4888 WXS_FIND_GLOBAL_ITEM(idcDef)
4889 }
4890exit:
4891 return (ret);
4892}
4893
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004894/**
4895 * xmlSchemaGetNamedComponent:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004896 * @schema: the schema
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004897 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004898 * @ns: the target namespace of the group
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004899 *
4900 * Lookup a group in the schema or imported schemas
4901 *
4902 * Returns the group definition or NULL if not found.
4903 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004904static xmlSchemaBasicItemPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004905xmlSchemaGetNamedComponent(xmlSchemaPtr schema,
4906 xmlSchemaTypeType itemType,
4907 const xmlChar *name,
4908 const xmlChar *targetNs)
4909{
4910 switch (itemType) {
4911 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004912 return ((xmlSchemaBasicItemPtr) xmlSchemaGetGroup(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004913 name, targetNs));
4914 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004915 return ((xmlSchemaBasicItemPtr) xmlSchemaGetElem(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004916 name, targetNs));
4917 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004918 TODO
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004919 return (NULL);
4920 }
4921}
4922
Daniel Veillard4255d502002-04-16 15:50:10 +00004923/************************************************************************
4924 * *
4925 * Parsing functions *
4926 * *
4927 ************************************************************************/
4928
4929#define IS_BLANK_NODE(n) \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004930 (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content, -1)))
Daniel Veillard4255d502002-04-16 15:50:10 +00004931
4932/**
4933 * xmlSchemaIsBlank:
4934 * @str: a string
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004935 * @len: the length of the string or -1
Daniel Veillard4255d502002-04-16 15:50:10 +00004936 *
4937 * Check if a string is ignorable
4938 *
4939 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
4940 */
4941static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004942xmlSchemaIsBlank(xmlChar * str, int len)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004943{
Daniel Veillard4255d502002-04-16 15:50:10 +00004944 if (str == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004945 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004946 if (len < 0) {
4947 while (*str != 0) {
4948 if (!(IS_BLANK_CH(*str)))
4949 return (0);
4950 str++;
4951 }
4952 } else while ((*str != 0) && (len != 0)) {
4953 if (!(IS_BLANK_CH(*str)))
4954 return (0);
4955 str++;
4956 len--;
Daniel Veillard4255d502002-04-16 15:50:10 +00004957 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004958
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004959 return (1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004960}
4961
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004962#define WXS_COMP_NAME(c, t) ((t) (c))->name
4963#define WXS_COMP_TNS(c, t) ((t) (c))->targetNamespace
4964/*
4965* xmlSchemaFindRedefCompInGraph:
4966* ATTENTION TODO: This uses pointer comp. for strings.
4967*/
4968static xmlSchemaBasicItemPtr
4969xmlSchemaFindRedefCompInGraph(xmlSchemaBucketPtr bucket,
4970 xmlSchemaTypeType type,
4971 const xmlChar *name,
4972 const xmlChar *nsName)
4973{
4974 xmlSchemaBasicItemPtr ret;
4975 int i;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004976
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004977 if ((bucket == NULL) || (name == NULL))
4978 return(NULL);
4979 if ((bucket->globals == NULL) ||
4980 (bucket->globals->nbItems == 0))
4981 goto subschemas;
4982 /*
4983 * Search in global components.
4984 */
4985 for (i = 0; i < bucket->globals->nbItems; i++) {
4986 ret = bucket->globals->items[i];
4987 if (ret->type == type) {
4988 switch (type) {
4989 case XML_SCHEMA_TYPE_COMPLEX:
4990 case XML_SCHEMA_TYPE_SIMPLE:
4991 if ((WXS_COMP_NAME(ret, xmlSchemaTypePtr) == name) &&
4992 (WXS_COMP_TNS(ret, xmlSchemaTypePtr) ==
4993 nsName))
4994 {
4995 return(ret);
4996 }
4997 break;
4998 case XML_SCHEMA_TYPE_GROUP:
4999 if ((WXS_COMP_NAME(ret,
5000 xmlSchemaModelGroupDefPtr) == name) &&
5001 (WXS_COMP_TNS(ret,
5002 xmlSchemaModelGroupDefPtr) == nsName))
5003 {
5004 return(ret);
5005 }
5006 break;
5007 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
5008 if ((WXS_COMP_NAME(ret,
5009 xmlSchemaAttributeGroupPtr) == name) &&
5010 (WXS_COMP_TNS(ret,
5011 xmlSchemaAttributeGroupPtr) == nsName))
5012 {
5013 return(ret);
5014 }
5015 default:
5016 /* Should not be hit. */
5017 return(NULL);
5018 }
5019 }
5020 }
5021subschemas:
5022 /*
5023 * Process imported/included schemas.
5024 */
5025 if (bucket->relations != NULL) {
5026 xmlSchemaSchemaRelationPtr rel = bucket->relations;
5027
5028 /*
5029 * TODO: Marking the bucket will not avoid multiple searches
5030 * in the same schema, but avoids at least circularity.
5031 */
5032 bucket->flags |= XML_SCHEMA_BUCKET_MARKED;
5033 do {
5034 if ((rel->bucket != NULL) &&
5035 ((rel->bucket->flags & XML_SCHEMA_BUCKET_MARKED) == 0)) {
5036 ret = xmlSchemaFindRedefCompInGraph(rel->bucket,
5037 type, name, nsName);
5038 if (ret != NULL)
5039 return(ret);
5040 }
5041 rel = rel->next;
5042 } while (rel != NULL);
5043 bucket->flags ^= XML_SCHEMA_BUCKET_MARKED;
5044 }
5045 return(NULL);
5046}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005047
5048/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005049 * xmlSchemaAddNotation:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005050 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005051 * @schema: the schema being built
5052 * @name: the item name
5053 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00005054 * Add an XML schema annotation declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00005055 * *WARNING* this interface is highly subject to change
5056 *
5057 * Returns the new struture or NULL in case of error
5058 */
5059static xmlSchemaNotationPtr
5060xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005061 const xmlChar *name, const xmlChar *nsName,
5062 xmlNodePtr node ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00005063{
5064 xmlSchemaNotationPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005065
5066 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5067 return (NULL);
5068
Daniel Veillard4255d502002-04-16 15:50:10 +00005069 ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation));
5070 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005071 xmlSchemaPErrMemory(ctxt, "add annotation", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005072 return (NULL);
5073 }
5074 memset(ret, 0, sizeof(xmlSchemaNotation));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005075 ret->type = XML_SCHEMA_TYPE_NOTATION;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005076 ret->name = name;
5077 ret->targetNamespace = nsName;
5078 /* TODO: do we need the node to be set?
5079 * ret->node = node;*/
5080 WXS_ADD_GLOBAL(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005081 return (ret);
5082}
5083
Daniel Veillard4255d502002-04-16 15:50:10 +00005084/**
5085 * xmlSchemaAddAttribute:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005086 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005087 * @schema: the schema being built
5088 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005089 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005090 *
5091 * Add an XML schema Attrribute declaration
5092 * *WARNING* this interface is highly subject to change
5093 *
5094 * Returns the new struture or NULL in case of error
5095 */
5096static xmlSchemaAttributePtr
5097xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005098 const xmlChar * name, const xmlChar * nsName,
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00005099 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005100{
5101 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005102
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005103 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005104 return (NULL);
5105
5106 ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute));
5107 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005108 xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005109 return (NULL);
5110 }
5111 memset(ret, 0, sizeof(xmlSchemaAttribute));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005112 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE;
5113 ret->node = node;
5114 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005115 ret->targetNamespace = nsName;
5116
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005117 if (topLevel)
5118 WXS_ADD_GLOBAL(ctxt, ret);
5119 else
5120 WXS_ADD_LOCAL(ctxt, ret);
5121 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005122 return (ret);
5123}
5124
5125/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005126 * xmlSchemaAddAttributeUse:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005127 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005128 * @schema: the schema being built
5129 * @name: the item name
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005130 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005131 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005132 * Add an XML schema Attrribute declaration
5133 * *WARNING* this interface is highly subject to change
5134 *
5135 * Returns the new struture or NULL in case of error
5136 */
5137static xmlSchemaAttributeUsePtr
5138xmlSchemaAddAttributeUse(xmlSchemaParserCtxtPtr pctxt,
5139 xmlNodePtr node)
5140{
5141 xmlSchemaAttributeUsePtr ret = NULL;
5142
5143 if (pctxt == NULL)
5144 return (NULL);
5145
5146 ret = (xmlSchemaAttributeUsePtr) xmlMalloc(sizeof(xmlSchemaAttributeUse));
5147 if (ret == NULL) {
5148 xmlSchemaPErrMemory(pctxt, "allocating attribute", NULL);
5149 return (NULL);
5150 }
5151 memset(ret, 0, sizeof(xmlSchemaAttributeUse));
5152 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE_USE;
5153 ret->node = node;
5154
5155 WXS_ADD_LOCAL(pctxt, ret);
5156 return (ret);
5157}
5158
5159/*
5160* xmlSchemaAddRedef:
5161*
5162* Adds a redefinition information. This is used at a later stage to:
5163* resolve references to the redefined components and to check constraints.
5164*/
5165static xmlSchemaRedefPtr
5166xmlSchemaAddRedef(xmlSchemaParserCtxtPtr pctxt,
5167 xmlSchemaBucketPtr targetBucket,
5168 void *item,
5169 const xmlChar *refName,
5170 const xmlChar *refTargetNs)
5171{
5172 xmlSchemaRedefPtr ret;
5173
5174 ret = (xmlSchemaRedefPtr)
5175 xmlMalloc(sizeof(xmlSchemaRedef));
5176 if (ret == NULL) {
5177 xmlSchemaPErrMemory(pctxt,
5178 "allocating redefinition info", NULL);
5179 return (NULL);
5180 }
5181 memset(ret, 0, sizeof(xmlSchemaRedef));
5182 ret->item = item;
5183 ret->targetBucket = targetBucket;
5184 ret->refName = refName;
5185 ret->refTargetNs = refTargetNs;
5186 if (WXS_CONSTRUCTOR(pctxt)->redefs == NULL)
5187 WXS_CONSTRUCTOR(pctxt)->redefs = ret;
5188 else
5189 WXS_CONSTRUCTOR(pctxt)->lastRedef->next = ret;
5190 WXS_CONSTRUCTOR(pctxt)->lastRedef = ret;
5191
5192 return (ret);
5193}
5194
5195/**
5196 * xmlSchemaAddAttributeGroupDefinition:
5197 * @ctxt: a schema parser context
5198 * @schema: the schema being built
5199 * @name: the item name
5200 * @nsName: the target namespace
5201 * @node: the corresponding node
5202 *
5203 * Add an XML schema Attrribute Group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00005204 *
5205 * Returns the new struture or NULL in case of error
5206 */
5207static xmlSchemaAttributeGroupPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005208xmlSchemaAddAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
5209 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
5210 const xmlChar *name,
5211 const xmlChar *nsName,
5212 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00005213{
5214 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005215
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005216 if ((pctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005217 return (NULL);
5218
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005219 ret = (xmlSchemaAttributeGroupPtr)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005220 xmlMalloc(sizeof(xmlSchemaAttributeGroup));
Daniel Veillard4255d502002-04-16 15:50:10 +00005221 if (ret == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005222 xmlSchemaPErrMemory(pctxt, "allocating attribute group", NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005223 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005224 }
5225 memset(ret, 0, sizeof(xmlSchemaAttributeGroup));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005226 ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005227 ret->name = name;
5228 ret->targetNamespace = nsName;
5229 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005230
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005231 /* TODO: Remove the flag. */
5232 ret->flags |= XML_SCHEMAS_ATTRGROUP_GLOBAL;
5233 if (pctxt->isRedefine) {
5234 pctxt->redef = xmlSchemaAddRedef(pctxt, pctxt->redefined,
5235 ret, name, nsName);
5236 if (pctxt->redef == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005237 xmlFree(ret);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005238 return(NULL);
5239 }
5240 pctxt->redefCounter = 0;
5241 }
5242 WXS_ADD_GLOBAL(pctxt, ret);
5243 WXS_ADD_PENDING(pctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005244 return (ret);
5245}
5246
5247/**
5248 * xmlSchemaAddElement:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005249 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005250 * @schema: the schema being built
5251 * @name: the type name
5252 * @namespace: the type namespace
5253 *
5254 * Add an XML schema Element declaration
5255 * *WARNING* this interface is highly subject to change
5256 *
5257 * Returns the new struture or NULL in case of error
5258 */
5259static xmlSchemaElementPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005260xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005261 const xmlChar * name, const xmlChar * nsName,
William M. Brack2f2a6632004-08-20 23:09:47 +00005262 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005263{
5264 xmlSchemaElementPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005265
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005266 if ((ctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005267 return (NULL);
5268
Daniel Veillard4255d502002-04-16 15:50:10 +00005269 ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement));
5270 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005271 xmlSchemaPErrMemory(ctxt, "allocating element", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005272 return (NULL);
5273 }
5274 memset(ret, 0, sizeof(xmlSchemaElement));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005275 ret->type = XML_SCHEMA_TYPE_ELEMENT;
5276 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005277 ret->targetNamespace = nsName;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005278 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005279
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005280 if (topLevel)
5281 WXS_ADD_GLOBAL(ctxt, ret);
5282 else
5283 WXS_ADD_LOCAL(ctxt, ret);
5284 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005285 return (ret);
5286}
5287
5288/**
5289 * xmlSchemaAddType:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005290 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005291 * @schema: the schema being built
5292 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005293 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005294 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00005295 * Add an XML schema item
Daniel Veillard4255d502002-04-16 15:50:10 +00005296 * *WARNING* this interface is highly subject to change
5297 *
5298 * Returns the new struture or NULL in case of error
5299 */
5300static xmlSchemaTypePtr
5301xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005302 xmlSchemaTypeType type,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00005303 const xmlChar * name, const xmlChar * nsName,
5304 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005305{
5306 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005307
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005308 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005309 return (NULL);
5310
5311 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
5312 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005313 xmlSchemaPErrMemory(ctxt, "allocating type", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005314 return (NULL);
5315 }
5316 memset(ret, 0, sizeof(xmlSchemaType));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005317 ret->type = type;
5318 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005319 ret->targetNamespace = nsName;
5320 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005321 if (topLevel) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005322 if (ctxt->isRedefine) {
5323 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5324 ret, name, nsName);
5325 if (ctxt->redef == NULL) {
5326 xmlFree(ret);
5327 return(NULL);
5328 }
5329 ctxt->redefCounter = 0;
5330 }
5331 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005332 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005333 WXS_ADD_LOCAL(ctxt, ret);
5334 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005335 return (ret);
5336}
5337
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005338static xmlSchemaQNameRefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005339xmlSchemaNewQNameRef(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005340 xmlSchemaTypeType refType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005341 const xmlChar *refName,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005342 const xmlChar *refNs)
5343{
5344 xmlSchemaQNameRefPtr ret;
5345
5346 ret = (xmlSchemaQNameRefPtr)
5347 xmlMalloc(sizeof(xmlSchemaQNameRef));
5348 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005349 xmlSchemaPErrMemory(pctxt,
5350 "allocating QName reference item", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005351 return (NULL);
5352 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005353 ret->node = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005354 ret->type = XML_SCHEMA_EXTRA_QNAMEREF;
5355 ret->name = refName;
5356 ret->targetNamespace = refNs;
5357 ret->item = NULL;
5358 ret->itemType = refType;
5359 /*
5360 * Store the reference item in the schema.
5361 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005362 WXS_ADD_LOCAL(pctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005363 return (ret);
5364}
5365
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005366static xmlSchemaAttributeUseProhibPtr
5367xmlSchemaAddAttributeUseProhib(xmlSchemaParserCtxtPtr pctxt)
5368{
5369 xmlSchemaAttributeUseProhibPtr ret;
5370
5371 ret = (xmlSchemaAttributeUseProhibPtr)
5372 xmlMalloc(sizeof(xmlSchemaAttributeUseProhib));
5373 if (ret == NULL) {
5374 xmlSchemaPErrMemory(pctxt,
5375 "allocating attribute use prohibition", NULL);
5376 return (NULL);
5377 }
5378 memset(ret, 0, sizeof(xmlSchemaAttributeUseProhib));
5379 ret->type = XML_SCHEMA_EXTRA_ATTR_USE_PROHIB;
5380 WXS_ADD_LOCAL(pctxt, ret);
5381 return (ret);
5382}
5383
5384
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005385/**
5386 * xmlSchemaAddModelGroup:
5387 * @ctxt: a schema parser context
5388 * @schema: the schema being built
5389 * @type: the "compositor" type of the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005390 * @node: the node in the schema doc
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005391 *
5392 * Adds a schema model group
5393 * *WARNING* this interface is highly subject to change
5394 *
5395 * Returns the new struture or NULL in case of error
5396 */
5397static xmlSchemaModelGroupPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005398xmlSchemaAddModelGroup(xmlSchemaParserCtxtPtr ctxt,
5399 xmlSchemaPtr schema,
5400 xmlSchemaTypeType type,
5401 xmlNodePtr node)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005402{
5403 xmlSchemaModelGroupPtr ret = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005404
5405 if ((ctxt == NULL) || (schema == NULL))
5406 return (NULL);
5407
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005408 ret = (xmlSchemaModelGroupPtr)
5409 xmlMalloc(sizeof(xmlSchemaModelGroup));
5410 if (ret == NULL) {
5411 xmlSchemaPErrMemory(ctxt, "allocating model group component",
5412 NULL);
5413 return (NULL);
5414 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005415 memset(ret, 0, sizeof(xmlSchemaModelGroup));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005416 ret->type = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005417 ret->node = node;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005418 WXS_ADD_LOCAL(ctxt, ret);
5419 if ((type == XML_SCHEMA_TYPE_SEQUENCE) ||
5420 (type == XML_SCHEMA_TYPE_CHOICE))
5421 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005422 return (ret);
5423}
5424
5425
5426/**
5427 * xmlSchemaAddParticle:
5428 * @ctxt: a schema parser context
5429 * @schema: the schema being built
5430 * @node: the corresponding node in the schema doc
5431 * @min: the minOccurs
5432 * @max: the maxOccurs
5433 *
5434 * Adds an XML schema particle component.
5435 * *WARNING* this interface is highly subject to change
5436 *
5437 * Returns the new struture or NULL in case of error
5438 */
5439static xmlSchemaParticlePtr
5440xmlSchemaAddParticle(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5441 xmlNodePtr node, int min, int max)
5442{
5443 xmlSchemaParticlePtr ret = NULL;
5444 if ((ctxt == NULL) || (schema == NULL))
5445 return (NULL);
5446
5447#ifdef DEBUG
5448 fprintf(stderr, "Adding particle component\n");
5449#endif
5450 ret = (xmlSchemaParticlePtr)
5451 xmlMalloc(sizeof(xmlSchemaParticle));
5452 if (ret == NULL) {
5453 xmlSchemaPErrMemory(ctxt, "allocating particle component",
5454 NULL);
5455 return (NULL);
5456 }
5457 ret->type = XML_SCHEMA_TYPE_PARTICLE;
5458 ret->annot = NULL;
5459 ret->node = node;
5460 ret->minOccurs = min;
5461 ret->maxOccurs = max;
5462 ret->next = NULL;
5463 ret->children = NULL;
5464
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005465 WXS_ADD_LOCAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005466 /*
5467 * Note that addition to pending components will be done locally
5468 * to the specific parsing function, since the most particles
5469 * need not to be fixed up (i.e. the reference to be resolved).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005470 * REMOVED: WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005471 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005472 return (ret);
5473}
5474
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005475/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005476 * xmlSchemaAddModelGroupDefinition:
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005477 * @ctxt: a schema validation context
5478 * @schema: the schema being built
5479 * @name: the group name
5480 *
5481 * Add an XML schema Group definition
5482 *
5483 * Returns the new struture or NULL in case of error
5484 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005485static xmlSchemaModelGroupDefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005486xmlSchemaAddModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
5487 xmlSchemaPtr schema,
5488 const xmlChar *name,
5489 const xmlChar *nsName,
5490 xmlNodePtr node)
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005491{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005492 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005493
5494 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5495 return (NULL);
5496
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005497 ret = (xmlSchemaModelGroupDefPtr)
5498 xmlMalloc(sizeof(xmlSchemaModelGroupDef));
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005499 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005500 xmlSchemaPErrMemory(ctxt, "adding group", NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005501 return (NULL);
5502 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005503 memset(ret, 0, sizeof(xmlSchemaModelGroupDef));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005504 ret->name = name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005505 ret->type = XML_SCHEMA_TYPE_GROUP;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005506 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005507 ret->targetNamespace = nsName;
5508
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005509 if (ctxt->isRedefine) {
5510 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5511 ret, name, nsName);
5512 if (ctxt->redef == NULL) {
5513 xmlFree(ret);
5514 return(NULL);
5515 }
5516 ctxt->redefCounter = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005517 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005518 WXS_ADD_GLOBAL(ctxt, ret);
5519 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005520 return (ret);
5521}
5522
Daniel Veillard3646d642004-06-02 19:19:14 +00005523/**
5524 * xmlSchemaNewWildcardNs:
5525 * @ctxt: a schema validation context
5526 *
5527 * Creates a new wildcard namespace constraint.
5528 *
5529 * Returns the new struture or NULL in case of error
5530 */
5531static xmlSchemaWildcardNsPtr
5532xmlSchemaNewWildcardNsConstraint(xmlSchemaParserCtxtPtr ctxt)
5533{
5534 xmlSchemaWildcardNsPtr ret;
5535
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005536 ret = (xmlSchemaWildcardNsPtr)
Daniel Veillard3646d642004-06-02 19:19:14 +00005537 xmlMalloc(sizeof(xmlSchemaWildcardNs));
5538 if (ret == NULL) {
5539 xmlSchemaPErrMemory(ctxt, "creating wildcard namespace constraint", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005540 return (NULL);
Daniel Veillard3646d642004-06-02 19:19:14 +00005541 }
5542 ret->value = NULL;
5543 ret->next = NULL;
5544 return (ret);
5545}
5546
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005547static xmlSchemaIDCPtr
5548xmlSchemaAddIDC(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5549 const xmlChar *name, const xmlChar *nsName,
5550 int category, xmlNodePtr node)
5551{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005552 xmlSchemaIDCPtr ret = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005553
5554 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5555 return (NULL);
5556
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005557 ret = (xmlSchemaIDCPtr) xmlMalloc(sizeof(xmlSchemaIDC));
5558 if (ret == NULL) {
5559 xmlSchemaPErrMemory(ctxt,
5560 "allocating an identity-constraint definition", NULL);
5561 return (NULL);
5562 }
5563 memset(ret, 0, sizeof(xmlSchemaIDC));
5564 /* The target namespace of the parent element declaration. */
5565 ret->targetNamespace = nsName;
5566 ret->name = name;
5567 ret->type = category;
5568 ret->node = node;
5569
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005570 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005571 /*
5572 * Only keyrefs need to be fixup up.
5573 */
5574 if (category == XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005575 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005576 return (ret);
5577}
5578
Daniel Veillard3646d642004-06-02 19:19:14 +00005579/**
5580 * xmlSchemaAddWildcard:
5581 * @ctxt: a schema validation context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005582 * @schema: a schema
5583 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005584 * Adds a wildcard.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005585 * It corresponds to a xsd:anyAttribute and xsd:any.
Daniel Veillard3646d642004-06-02 19:19:14 +00005586 *
5587 * Returns the new struture or NULL in case of error
5588 */
5589static xmlSchemaWildcardPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005590xmlSchemaAddWildcard(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5591 xmlSchemaTypeType type, xmlNodePtr node)
Daniel Veillard3646d642004-06-02 19:19:14 +00005592{
5593 xmlSchemaWildcardPtr ret = NULL;
5594
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005595 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard3646d642004-06-02 19:19:14 +00005596 return (NULL);
5597
5598 ret = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
5599 if (ret == NULL) {
5600 xmlSchemaPErrMemory(ctxt, "adding wildcard", NULL);
5601 return (NULL);
5602 }
5603 memset(ret, 0, sizeof(xmlSchemaWildcard));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005604 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005605 ret->node = node;
5606 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillard3646d642004-06-02 19:19:14 +00005607 return (ret);
5608}
5609
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005610static void
5611xmlSchemaSubstGroupFree(xmlSchemaSubstGroupPtr group)
5612{
5613 if (group == NULL)
5614 return;
5615 if (group->members != NULL)
5616 xmlSchemaItemListFree(group->members);
5617 xmlFree(group);
5618}
5619
5620static xmlSchemaSubstGroupPtr
5621xmlSchemaSubstGroupAdd(xmlSchemaParserCtxtPtr pctxt,
5622 xmlSchemaElementPtr head)
5623{
5624 xmlSchemaSubstGroupPtr ret;
5625
5626 /* Init subst group hash. */
5627 if (WXS_SUBST_GROUPS(pctxt) == NULL) {
5628 WXS_SUBST_GROUPS(pctxt) = xmlHashCreateDict(10, pctxt->dict);
5629 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5630 return(NULL);
5631 }
5632 /* Create a new substitution group. */
5633 ret = (xmlSchemaSubstGroupPtr) xmlMalloc(sizeof(xmlSchemaSubstGroup));
5634 if (ret == NULL) {
5635 xmlSchemaPErrMemory(NULL,
5636 "allocating a substitution group container", NULL);
5637 return(NULL);
5638 }
5639 memset(ret, 0, sizeof(xmlSchemaSubstGroup));
5640 ret->head = head;
5641 /* Create list of members. */
5642 ret->members = xmlSchemaItemListCreate();
5643 if (ret->members == NULL) {
5644 xmlSchemaSubstGroupFree(ret);
5645 return(NULL);
5646 }
5647 /* Add subst group to hash. */
5648 if (xmlHashAddEntry2(WXS_SUBST_GROUPS(pctxt),
5649 head->name, head->targetNamespace, ret) != 0) {
5650 PERROR_INT("xmlSchemaSubstGroupAdd",
5651 "failed to add a new substitution container");
5652 xmlSchemaSubstGroupFree(ret);
5653 return(NULL);
5654 }
5655 return(ret);
5656}
5657
5658static xmlSchemaSubstGroupPtr
5659xmlSchemaSubstGroupGet(xmlSchemaParserCtxtPtr pctxt,
5660 xmlSchemaElementPtr head)
5661{
5662 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5663 return(NULL);
5664 return(xmlHashLookup2(WXS_SUBST_GROUPS(pctxt),
5665 head->name, head->targetNamespace));
5666
5667}
5668
5669/**
5670 * xmlSchemaAddElementSubstitutionMember:
5671 * @pctxt: a schema parser context
5672 * @head: the head of the substitution group
5673 * @member: the new member of the substitution group
5674 *
5675 * Allocate a new annotation structure.
5676 *
5677 * Returns the newly allocated structure or NULL in case or error
5678 */
5679static int
5680xmlSchemaAddElementSubstitutionMember(xmlSchemaParserCtxtPtr pctxt,
5681 xmlSchemaElementPtr head,
5682 xmlSchemaElementPtr member)
5683{
5684 xmlSchemaSubstGroupPtr substGroup = NULL;
5685
5686 if ((pctxt == NULL) || (head == NULL) || (member == NULL))
5687 return (-1);
5688
5689 substGroup = xmlSchemaSubstGroupGet(pctxt, head);
5690 if (substGroup == NULL)
5691 substGroup = xmlSchemaSubstGroupAdd(pctxt, head);
5692 if (substGroup == NULL)
5693 return(-1);
5694 if (xmlSchemaItemListAdd(substGroup->members, member) == -1)
5695 return(-1);
5696 return(0);
5697}
5698
Daniel Veillard4255d502002-04-16 15:50:10 +00005699/************************************************************************
5700 * *
5701 * Utilities for parsing *
5702 * *
5703 ************************************************************************/
5704
Daniel Veillard4255d502002-04-16 15:50:10 +00005705/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00005706 * xmlSchemaPValAttrNodeQNameValue:
5707 * @ctxt: a schema parser context
5708 * @schema: the schema context
5709 * @ownerDes: the designation of the parent element
5710 * @ownerItem: the parent as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005711 * @value: the QName value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005712 * @local: the resulting local part if found, the attribute value otherwise
5713 * @uri: the resulting namespace URI if found
5714 *
5715 * Extracts the local name and the URI of a QName value and validates it.
5716 * This one is intended to be used on attribute values that
5717 * should resolve to schema components.
5718 *
5719 * Returns 0, in case the QName is valid, a positive error code
5720 * if not valid and -1 if an internal error occurs.
5721 */
5722static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005723xmlSchemaPValAttrNodeQNameValue(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005724 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005725 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005726 xmlAttrPtr attr,
5727 const xmlChar *value,
5728 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005729 const xmlChar **local)
5730{
5731 const xmlChar *pref;
5732 xmlNsPtr ns;
5733 int len, ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005734
Daniel Veillardc0826a72004-08-10 14:17:33 +00005735 *uri = NULL;
5736 *local = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005737 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005738 if (ret > 0) {
5739 xmlSchemaPSimpleTypeErr(ctxt,
5740 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5741 ownerItem, (xmlNodePtr) attr,
5742 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
5743 NULL, value, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005744 *local = value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005745 return (ctxt->err);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005746 } else if (ret < 0)
5747 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005748
5749 if (!strchr((char *) value, ':')) {
Daniel Veillard24505b02005-07-28 23:49:35 +00005750 ns = xmlSearchNs(attr->doc, attr->parent, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005751 if (ns)
5752 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
5753 else if (schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005754 /* TODO: move XML_SCHEMAS_INCLUDING_CONVERT_NS to the
5755 * parser context. */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005756 /*
5757 * This one takes care of included schemas with no
5758 * target namespace.
5759 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005760 *uri = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005761 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005762 *local = xmlDictLookup(ctxt->dict, value, -1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005763 return (0);
5764 }
5765 /*
5766 * At this point xmlSplitQName3 has to return a local name.
5767 */
5768 *local = xmlSplitQName3(value, &len);
5769 *local = xmlDictLookup(ctxt->dict, *local, -1);
5770 pref = xmlDictLookup(ctxt->dict, value, len);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005771 ns = xmlSearchNs(attr->doc, attr->parent, pref);
5772 if (ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005773 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005774 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005775 ownerItem, (xmlNodePtr) attr,
5776 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), NULL, value,
5777 "The value '%s' of simple type 'xs:QName' has no "
5778 "corresponding namespace declaration in scope", value, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005779 return (ctxt->err);
5780 } else {
5781 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005782 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00005783 return (0);
5784}
5785
5786/**
5787 * xmlSchemaPValAttrNodeQName:
5788 * @ctxt: a schema parser context
5789 * @schema: the schema context
5790 * @ownerDes: the designation of the owner element
5791 * @ownerItem: the owner as a schema object
5792 * @attr: the attribute node
5793 * @local: the resulting local part if found, the attribute value otherwise
5794 * @uri: the resulting namespace URI if found
5795 *
5796 * Extracts and validates the QName of an attribute value.
5797 * This one is intended to be used on attribute values that
5798 * should resolve to schema components.
5799 *
5800 * Returns 0, in case the QName is valid, a positive error code
5801 * if not valid and -1 if an internal error occurs.
5802 */
5803static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005804xmlSchemaPValAttrNodeQName(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005805 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005806 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005807 xmlAttrPtr attr,
5808 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005809 const xmlChar **local)
5810{
5811 const xmlChar *value;
5812
5813 value = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005814 return (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005815 ownerItem, attr, value, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005816}
5817
5818/**
5819 * xmlSchemaPValAttrQName:
5820 * @ctxt: a schema parser context
5821 * @schema: the schema context
5822 * @ownerDes: the designation of the parent element
5823 * @ownerItem: the owner as a schema object
5824 * @ownerElem: the parent node of the attribute
5825 * @name: the name of the attribute
5826 * @local: the resulting local part if found, the attribute value otherwise
5827 * @uri: the resulting namespace URI if found
5828 *
5829 * Extracts and validates the QName of an attribute value.
5830 *
5831 * Returns 0, in case the QName is valid, a positive error code
5832 * if not valid and -1 if an internal error occurs.
5833 */
5834static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005835xmlSchemaPValAttrQName(xmlSchemaParserCtxtPtr ctxt,
5836 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005837 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005838 xmlNodePtr ownerElem,
5839 const char *name,
5840 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005841 const xmlChar **local)
5842{
5843 xmlAttrPtr attr;
5844
5845 attr = xmlSchemaGetPropNode(ownerElem, name);
5846 if (attr == NULL) {
5847 *local = NULL;
5848 *uri = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005849 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005850 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005851 return (xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005852 ownerItem, attr, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005853}
5854
5855/**
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005856 * xmlSchemaPValAttrID:
5857 * @ctxt: a schema parser context
5858 * @schema: the schema context
5859 * @ownerDes: the designation of the parent element
5860 * @ownerItem: the owner as a schema object
5861 * @ownerElem: the parent node of the attribute
5862 * @name: the name of the attribute
5863 *
5864 * Extracts and validates the ID of an attribute value.
5865 *
5866 * Returns 0, in case the ID is valid, a positive error code
5867 * if not valid and -1 if an internal error occurs.
5868 */
5869static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005870xmlSchemaPValAttrNodeID(xmlSchemaParserCtxtPtr ctxt, xmlAttrPtr attr)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005871{
5872 int ret;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005873 const xmlChar *value;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005874
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005875 if (attr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005876 return(0);
5877 value = xmlSchemaGetNodeContentNoDict((xmlNodePtr) attr);
5878 ret = xmlValidateNCName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005879 if (ret == 0) {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005880 /*
5881 * NOTE: the IDness might have already be declared in the DTD
5882 */
5883 if (attr->atype != XML_ATTRIBUTE_ID) {
5884 xmlIDPtr res;
5885 xmlChar *strip;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005886
5887 /*
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005888 * TODO: Use xmlSchemaStrip here; it's not exported at this
5889 * moment.
5890 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005891 strip = xmlSchemaCollapseString(value);
5892 if (strip != NULL) {
5893 xmlFree((xmlChar *) value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005894 value = strip;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005895 }
5896 res = xmlAddID(NULL, attr->doc, value, attr);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005897 if (res == NULL) {
5898 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005899 xmlSchemaPSimpleTypeErr(ctxt,
5900 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005901 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005902 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5903 NULL, NULL, "Duplicate value '%s' of simple "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005904 "type 'xs:ID'", value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005905 } else
5906 attr->atype = XML_ATTRIBUTE_ID;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005907 }
5908 } else if (ret > 0) {
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, "The value '%s' of simple type 'xs:ID' is "
5915 "not a valid 'xs:NCName'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005916 value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005917 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005918 if (value != NULL)
5919 xmlFree((xmlChar *)value);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005920
5921 return (ret);
5922}
5923
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005924static int
5925xmlSchemaPValAttrID(xmlSchemaParserCtxtPtr ctxt,
5926 xmlNodePtr ownerElem,
5927 const xmlChar *name)
5928{
5929 xmlAttrPtr attr;
5930
5931 attr = xmlSchemaGetPropNode(ownerElem, (const char *) name);
5932 if (attr == NULL)
5933 return(0);
5934 return(xmlSchemaPValAttrNodeID(ctxt, attr));
5935
5936}
5937
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005938/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005939 * xmlGetMaxOccurs:
5940 * @ctxt: a schema validation context
5941 * @node: a subtree containing XML Schema informations
5942 *
5943 * Get the maxOccurs property
5944 *
5945 * Returns the default if not found, or the value
5946 */
5947static int
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005948xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
5949 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005950{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005951 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00005952 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005953 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00005954
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005955 attr = xmlSchemaGetPropNode(node, "maxOccurs");
5956 if (attr == NULL)
5957 return (def);
5958 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00005959
5960 if (xmlStrEqual(val, (const xmlChar *) "unbounded")) {
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005961 if (max != UNBOUNDED) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005962 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005963 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5964 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005965 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005966 val, NULL, NULL, NULL);
5967 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005968 } else
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005969 return (UNBOUNDED); /* encoding it with -1 might be another option */
Daniel Veillard4255d502002-04-16 15:50:10 +00005970 }
5971
5972 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00005973 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005974 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005975 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005976 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005977 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5978 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005979 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005980 val, NULL, NULL, NULL);
5981 return (def);
5982 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005983 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005984 ret = ret * 10 + (*cur - '0');
5985 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00005986 }
William M. Brack76e95df2003-10-18 16:20:14 +00005987 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005988 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005989 /*
5990 * TODO: Restrict the maximal value to Integer.
5991 */
5992 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005993 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005994 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5995 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005996 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005997 val, NULL, NULL, NULL);
5998 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00005999 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006000 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006001}
6002
6003/**
6004 * xmlGetMinOccurs:
6005 * @ctxt: a schema validation context
6006 * @node: a subtree containing XML Schema informations
6007 *
6008 * Get the minOccurs property
6009 *
6010 * Returns the default if not found, or the value
6011 */
6012static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006013xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006014 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006015{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006016 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00006017 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006018 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006019
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006020 attr = xmlSchemaGetPropNode(node, "minOccurs");
6021 if (attr == NULL)
6022 return (def);
6023 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00006024 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006025 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006026 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006027 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006028 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006029 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6030 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006031 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006032 val, NULL, NULL, NULL);
6033 return (def);
6034 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006035 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006036 ret = ret * 10 + (*cur - '0');
6037 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006038 }
William M. Brack76e95df2003-10-18 16:20:14 +00006039 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006040 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006041 /*
6042 * TODO: Restrict the maximal value to Integer.
6043 */
6044 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006045 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006046 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6047 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006048 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006049 val, NULL, NULL, NULL);
6050 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006051 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006052 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006053}
6054
6055/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006056 * xmlSchemaPGetBoolNodeValue:
6057 * @ctxt: a schema validation context
6058 * @ownerDes: owner designation
6059 * @ownerItem: the owner as a schema item
6060 * @node: the node holding the value
6061 *
6062 * Converts a boolean string value into 1 or 0.
6063 *
6064 * Returns 0 or 1.
6065 */
6066static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006067xmlSchemaPGetBoolNodeValue(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006068 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006069 xmlNodePtr node)
6070{
6071 xmlChar *value = NULL;
6072 int res = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006073
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006074 value = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006075 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006076 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006077 * An instance of a datatype that is defined as ·boolean·
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006078 * can have the following legal literals {true, false, 1, 0}.
6079 */
6080 if (xmlStrEqual(BAD_CAST value, BAD_CAST "true"))
6081 res = 1;
6082 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "false"))
6083 res = 0;
6084 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "1"))
6085 res = 1;
6086 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006087 res = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006088 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006089 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006090 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006091 ownerItem, node,
6092 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6093 NULL, BAD_CAST value,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006094 NULL, NULL, NULL);
6095 }
6096 if (value != NULL)
6097 xmlFree(value);
6098 return (res);
6099}
6100
6101/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006102 * xmlGetBooleanProp:
6103 * @ctxt: a schema validation context
6104 * @node: a subtree containing XML Schema informations
6105 * @name: the attribute name
6106 * @def: the default value
6107 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006108 * Evaluate if a boolean property is set
Daniel Veillard4255d502002-04-16 15:50:10 +00006109 *
6110 * Returns the default if not found, 0 if found to be false,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006111 * 1 if found to be true
Daniel Veillard4255d502002-04-16 15:50:10 +00006112 */
6113static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006114xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006115 xmlNodePtr node,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006116 const char *name, int def)
6117{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006118 const xmlChar *val;
Daniel Veillard4255d502002-04-16 15:50:10 +00006119
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006120 val = xmlSchemaGetProp(ctxt, node, name);
Daniel Veillard4255d502002-04-16 15:50:10 +00006121 if (val == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006122 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006123 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006124 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006125 * An instance of a datatype that is defined as ·boolean·
Daniel Veillardc0826a72004-08-10 14:17:33 +00006126 * can have the following legal literals {true, false, 1, 0}.
6127 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006128 if (xmlStrEqual(val, BAD_CAST "true"))
6129 def = 1;
6130 else if (xmlStrEqual(val, BAD_CAST "false"))
6131 def = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006132 else if (xmlStrEqual(val, BAD_CAST "1"))
6133 def = 1;
6134 else if (xmlStrEqual(val, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006135 def = 0;
6136 else {
6137 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006138 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006139 NULL,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00006140 (xmlNodePtr) xmlSchemaGetPropNode(node, name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006141 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6142 NULL, val, NULL, NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006143 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006144 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006145}
6146
6147/************************************************************************
6148 * *
6149 * Shema extraction from an Infoset *
6150 * *
6151 ************************************************************************/
6152static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr
6153 ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00006154 xmlNodePtr node,
6155 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006156static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr
6157 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006158 xmlSchemaPtr schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006159 xmlNodePtr node,
Daniel Veillard3646d642004-06-02 19:19:14 +00006160 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006161static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr
6162 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006163 xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006164 xmlNodePtr node,
6165 xmlSchemaTypeType parentType);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006166static xmlSchemaBasicItemPtr
6167xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
6168 xmlSchemaPtr schema,
6169 xmlNodePtr node,
6170 xmlSchemaItemListPtr uses,
6171 int parentType);
Daniel Veillard4255d502002-04-16 15:50:10 +00006172static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt,
6173 xmlSchemaPtr schema,
6174 xmlNodePtr node);
Daniel Veillard3646d642004-06-02 19:19:14 +00006175static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006176xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6177 xmlSchemaPtr schema, xmlNodePtr node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006178
6179/**
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006180 * xmlSchemaPValAttrNodeValue:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006181 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00006182 * @ctxt: a schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00006183 * @ownerDes: the designation of the parent element
6184 * @ownerItem: the schema object owner if existent
6185 * @attr: the schema attribute node being validated
6186 * @value: the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006187 * @type: the built-in type to be validated against
Daniel Veillard01fa6152004-06-29 17:04:39 +00006188 *
6189 * Validates a value against the given built-in type.
6190 * This one is intended to be used internally for validation
6191 * of schema attribute values during parsing of the schema.
6192 *
6193 * Returns 0 if the value is valid, a positive error code
6194 * number otherwise and -1 in case of an internal or API error.
6195 */
6196static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006197xmlSchemaPValAttrNodeValue(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006198 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006199 xmlAttrPtr attr,
6200 const xmlChar *value,
6201 xmlSchemaTypePtr type)
Daniel Veillard01fa6152004-06-29 17:04:39 +00006202{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006203
6204 int ret = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006205
6206 /*
6207 * NOTE: Should we move this to xmlschematypes.c? Hmm, but this
6208 * one is really meant to be used internally, so better not.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006209 */
6210 if ((pctxt == NULL) || (type == NULL) || (attr == NULL))
Daniel Veillard01fa6152004-06-29 17:04:39 +00006211 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006212 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6213 PERROR_INT("xmlSchemaPValAttrNodeValue",
6214 "the given type is not a built-in type");
6215 return (-1);
6216 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006217 switch (type->builtInType) {
6218 case XML_SCHEMAS_NCNAME:
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006219 case XML_SCHEMAS_QNAME:
6220 case XML_SCHEMAS_ANYURI:
6221 case XML_SCHEMAS_TOKEN:
6222 case XML_SCHEMAS_LANGUAGE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006223 ret = xmlSchemaValPredefTypeNode(type, value, NULL,
6224 (xmlNodePtr) attr);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006225 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006226 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006227 PERROR_INT("xmlSchemaPValAttrNodeValue",
6228 "validation using the given type is not supported");
Daniel Veillard01fa6152004-06-29 17:04:39 +00006229 return (-1);
6230 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006231 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006232 /*
6233 * TODO: Should we use the S4S error codes instead?
6234 */
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006235 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006236 PERROR_INT("xmlSchemaPValAttrNodeValue",
6237 "failed to validate a schema attribute value");
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006238 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006239 } else if (ret > 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006240 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006241 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
6242 else
6243 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
6244 xmlSchemaPSimpleTypeErr(pctxt,
6245 ret, ownerItem, (xmlNodePtr) attr,
6246 type, NULL, value, NULL, NULL, NULL);
6247 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006248 return (ret);
6249}
6250
6251/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006252 * xmlSchemaPValAttrNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006253 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006254 * @ctxt: a schema parser context
6255 * @ownerDes: the designation of the parent element
6256 * @ownerItem: the schema object owner if existent
6257 * @attr: the schema attribute node being validated
6258 * @type: the built-in type to be validated against
6259 * @value: the resulting value if any
6260 *
6261 * Extracts and validates a value against the given built-in type.
6262 * This one is intended to be used internally for validation
6263 * of schema attribute values during parsing of the schema.
6264 *
6265 * Returns 0 if the value is valid, a positive error code
6266 * number otherwise and -1 in case of an internal or API error.
6267 */
6268static int
6269xmlSchemaPValAttrNode(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006270 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006271 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006272 xmlSchemaTypePtr type,
6273 const xmlChar **value)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006274{
Daniel Veillardc0826a72004-08-10 14:17:33 +00006275 const xmlChar *val;
6276
6277 if ((ctxt == NULL) || (type == NULL) || (attr == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006278 return (-1);
6279
Daniel Veillardc0826a72004-08-10 14:17:33 +00006280 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
6281 if (value != NULL)
6282 *value = val;
6283
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006284 return (xmlSchemaPValAttrNodeValue(ctxt, ownerItem, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006285 val, type));
Daniel Veillardc0826a72004-08-10 14:17:33 +00006286}
6287
6288/**
6289 * xmlSchemaPValAttr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006290 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006291 * @ctxt: a schema parser context
6292 * @node: the element node of the attribute
6293 * @ownerDes: the designation of the parent element
6294 * @ownerItem: the schema object owner if existent
6295 * @ownerElem: the owner element node
6296 * @name: the name of the schema attribute node
6297 * @type: the built-in type to be validated against
6298 * @value: the resulting value if any
6299 *
6300 * Extracts and validates a value against the given built-in type.
6301 * This one is intended to be used internally for validation
6302 * of schema attribute values during parsing of the schema.
6303 *
6304 * Returns 0 if the value is valid, a positive error code
6305 * number otherwise and -1 in case of an internal or API error.
6306 */
6307static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006308xmlSchemaPValAttr(xmlSchemaParserCtxtPtr ctxt,
6309 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006310 xmlNodePtr ownerElem,
6311 const char *name,
6312 xmlSchemaTypePtr type,
6313 const xmlChar **value)
6314{
6315 xmlAttrPtr attr;
6316
6317 if ((ctxt == NULL) || (type == NULL)) {
6318 if (value != NULL)
6319 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006320 return (-1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006321 }
6322 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6323 if (value != NULL)
6324 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006325 xmlSchemaPErr(ctxt, ownerElem,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00006326 XML_SCHEMAP_INTERNAL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006327 "Internal error: xmlSchemaPValAttr, the given "
6328 "type '%s' is not a built-in type.\n",
6329 type->name, NULL);
6330 return (-1);
6331 }
6332 attr = xmlSchemaGetPropNode(ownerElem, name);
6333 if (attr == NULL) {
6334 if (value != NULL)
6335 *value = NULL;
6336 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006337 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006338 return (xmlSchemaPValAttrNode(ctxt, ownerItem, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006339 type, value));
6340}
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006341
6342static int
6343xmlSchemaCheckReference(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006344 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006345 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006346 xmlAttrPtr attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006347 const xmlChar *namespaceName)
6348{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006349 /* TODO: Pointer comparison instead? */
6350 if (xmlStrEqual(pctxt->targetNamespace, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006351 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006352 if (xmlStrEqual(xmlSchemaNs, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006353 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006354 /*
6355 * Check if the referenced namespace was <import>ed.
6356 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006357 if (WXS_BUCKET(pctxt)->relations != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006358 xmlSchemaSchemaRelationPtr rel;
6359
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006360 rel = WXS_BUCKET(pctxt)->relations;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006361 do {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006362 if (WXS_IS_BUCKET_IMPMAIN(rel->type) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006363 xmlStrEqual(namespaceName, rel->importNamespace))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006364 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006365 rel = rel->next;
6366 } while (rel != NULL);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006367 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006368 /*
6369 * No matching <import>ed namespace found.
6370 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006371 {
6372 xmlNodePtr n = (attr != NULL) ? (xmlNodePtr) attr : node;
6373
6374 if (namespaceName == NULL)
6375 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6376 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6377 "References from this schema to components in no "
6378 "namespace are not allowed, since not indicated by an "
6379 "import statement", NULL, NULL);
6380 else
6381 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6382 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6383 "References from this schema to components in the "
6384 "namespace '%s' are not allowed, since not indicated by an "
6385 "import statement", namespaceName, NULL);
6386 }
6387 return (XML_SCHEMAP_SRC_RESOLVE);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006388}
6389
Daniel Veillardc0826a72004-08-10 14:17:33 +00006390/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006391 * xmlSchemaParseLocalAttributes:
Daniel Veillard4255d502002-04-16 15:50:10 +00006392 * @ctxt: a schema validation context
6393 * @schema: the schema being built
6394 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006395 * @type: the hosting type where the attributes will be anchored
Daniel Veillard4255d502002-04-16 15:50:10 +00006396 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006397 * Parses attribute uses and attribute declarations and
6398 * attribute group references.
Daniel Veillard4255d502002-04-16 15:50:10 +00006399 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006400static int
6401xmlSchemaParseLocalAttributes(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6402 xmlNodePtr *child, xmlSchemaItemListPtr *list,
6403 int parentType, int *hasRefs)
Daniel Veillard4255d502002-04-16 15:50:10 +00006404{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006405 void *item;
Daniel Veillard4255d502002-04-16 15:50:10 +00006406
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006407 while ((IS_SCHEMA((*child), "attribute")) ||
6408 (IS_SCHEMA((*child), "attributeGroup"))) {
6409 if (IS_SCHEMA((*child), "attribute")) {
6410 item = xmlSchemaParseLocalAttribute(ctxt, schema, *child,
6411 *list, parentType);
6412 } else {
6413 item = xmlSchemaParseAttributeGroupRef(ctxt, schema, *child);
6414 if ((item != NULL) && (hasRefs != NULL))
6415 *hasRefs = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006416 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006417 if (item != NULL) {
6418 if (*list == NULL) {
6419 /* TODO: Customize grow factor. */
6420 *list = xmlSchemaItemListCreate();
6421 if (*list == NULL)
6422 return(-1);
6423 }
6424 if (xmlSchemaItemListAddSize(*list, 2, item) == -1)
6425 return(-1);
6426 }
6427 *child = (*child)->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006428 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006429 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00006430}
6431
6432/**
6433 * xmlSchemaParseAnnotation:
6434 * @ctxt: a schema validation context
6435 * @schema: the schema being built
6436 * @node: a subtree containing XML Schema informations
6437 *
6438 * parse a XML schema Attrribute declaration
6439 * *WARNING* this interface is highly subject to change
6440 *
William M. Bracke7091952004-05-11 15:09:58 +00006441 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00006442 * 1 in case of success.
6443 */
6444static xmlSchemaAnnotPtr
6445xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006446 xmlNodePtr node, int needed)
Daniel Veillard4255d502002-04-16 15:50:10 +00006447{
6448 xmlSchemaAnnotPtr ret;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006449 xmlNodePtr child = NULL;
6450 xmlAttrPtr attr;
6451 int barked = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00006452
Daniel Veillardc0826a72004-08-10 14:17:33 +00006453 /*
6454 * INFO: S4S completed.
6455 */
6456 /*
6457 * id = ID
6458 * {any attributes with non-schema namespace . . .}>
6459 * Content: (appinfo | documentation)*
6460 */
Daniel Veillard4255d502002-04-16 15:50:10 +00006461 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6462 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006463 if (needed)
6464 ret = xmlSchemaNewAnnot(ctxt, node);
6465 else
6466 ret = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006467 attr = node->properties;
6468 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006469 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006470 (!xmlStrEqual(attr->name, BAD_CAST "id"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006471 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006472 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006473
6474 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006475 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006476 }
6477 attr = attr->next;
6478 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006479 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006480 /*
6481 * And now for the children...
6482 */
6483 child = node->children;
6484 while (child != NULL) {
6485 if (IS_SCHEMA(child, "appinfo")) {
6486 /* TODO: make available the content of "appinfo". */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006487 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006488 * source = anyURI
6489 * {any attributes with non-schema namespace . . .}>
6490 * Content: ({any})*
6491 */
6492 attr = child->properties;
6493 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006494 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006495 (!xmlStrEqual(attr->name, BAD_CAST "source"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006496 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006497 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006498
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006499 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006500 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006501 }
6502 attr = attr->next;
6503 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006504 xmlSchemaPValAttr(ctxt, NULL, child, "source",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006505 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006506 child = child->next;
6507 } else if (IS_SCHEMA(child, "documentation")) {
6508 /* TODO: make available the content of "documentation". */
6509 /*
6510 * source = anyURI
6511 * {any attributes with non-schema namespace . . .}>
6512 * Content: ({any})*
6513 */
6514 attr = child->properties;
6515 while (attr != NULL) {
6516 if (attr->ns == NULL) {
6517 if (!xmlStrEqual(attr->name, BAD_CAST "source")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006518 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006519 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006520 }
6521 } else {
6522 if (xmlStrEqual(attr->ns->href, xmlSchemaNs) ||
6523 (xmlStrEqual(attr->name, BAD_CAST "lang") &&
6524 (!xmlStrEqual(attr->ns->href, XML_XML_NAMESPACE)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006525
6526 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006527 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006528 }
6529 }
6530 attr = attr->next;
6531 }
6532 /*
6533 * Attribute "xml:lang".
6534 */
6535 attr = xmlSchemaGetPropNodeNs(child, (const char *) XML_XML_NAMESPACE, "lang");
6536 if (attr != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006537 xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006538 xmlSchemaGetBuiltInType(XML_SCHEMAS_LANGUAGE), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006539 child = child->next;
6540 } else {
6541 if (!barked)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006542 xmlSchemaPContentErr(ctxt,
6543 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006544 NULL, node, child, NULL, "(appinfo | documentation)*");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006545 barked = 1;
6546 child = child->next;
6547 }
6548 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006549
Daniel Veillard4255d502002-04-16 15:50:10 +00006550 return (ret);
6551}
6552
6553/**
6554 * xmlSchemaParseFacet:
6555 * @ctxt: a schema validation context
6556 * @schema: the schema being built
6557 * @node: a subtree containing XML Schema informations
6558 *
6559 * parse a XML schema Facet declaration
6560 * *WARNING* this interface is highly subject to change
6561 *
6562 * Returns the new type structure or NULL in case of error
6563 */
6564static xmlSchemaFacetPtr
6565xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006566 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006567{
6568 xmlSchemaFacetPtr facet;
6569 xmlNodePtr child = NULL;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006570 const xmlChar *value;
Daniel Veillard4255d502002-04-16 15:50:10 +00006571
6572 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6573 return (NULL);
6574
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00006575 facet = xmlSchemaNewFacet();
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006576 if (facet == NULL) {
6577 xmlSchemaPErrMemory(ctxt, "allocating facet", node);
6578 return (NULL);
6579 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006580 facet->node = node;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006581 value = xmlSchemaGetProp(ctxt, node, "value");
Daniel Veillard4255d502002-04-16 15:50:10 +00006582 if (value == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006583 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE,
6584 "Facet %s has no value\n", node->name, NULL);
6585 xmlSchemaFreeFacet(facet);
Daniel Veillard4255d502002-04-16 15:50:10 +00006586 return (NULL);
6587 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006588 if (IS_SCHEMA(node, "minInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006589 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006590 } else if (IS_SCHEMA(node, "minExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006591 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006592 } else if (IS_SCHEMA(node, "maxInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006593 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006594 } else if (IS_SCHEMA(node, "maxExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006595 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006596 } else if (IS_SCHEMA(node, "totalDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006597 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006598 } else if (IS_SCHEMA(node, "fractionDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006599 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006600 } else if (IS_SCHEMA(node, "pattern")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006601 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006602 } else if (IS_SCHEMA(node, "enumeration")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006603 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006604 } else if (IS_SCHEMA(node, "whiteSpace")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006605 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006606 } else if (IS_SCHEMA(node, "length")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006607 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006608 } else if (IS_SCHEMA(node, "maxLength")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006609 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
6610 } else if (IS_SCHEMA(node, "minLength")) {
6611 facet->type = XML_SCHEMA_FACET_MINLENGTH;
6612 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006613 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE,
6614 "Unknown facet type %s\n", node->name, NULL);
6615 xmlSchemaFreeFacet(facet);
6616 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006617 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006618 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +00006619 facet->value = value;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006620 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
6621 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
6622 const xmlChar *fixed;
6623
6624 fixed = xmlSchemaGetProp(ctxt, node, "fixed");
6625 if (fixed != NULL) {
6626 if (xmlStrEqual(fixed, BAD_CAST "true"))
6627 facet->fixed = 1;
6628 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006629 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006630 child = node->children;
6631
6632 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006633 facet->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006634 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006635 }
6636 if (child != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006637 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD,
6638 "Facet %s has unexpected child content\n",
6639 node->name, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006640 }
6641 return (facet);
6642}
6643
6644/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006645 * xmlSchemaParseWildcardNs:
6646 * @ctxt: a schema parser context
6647 * @wildc: the wildcard, already created
6648 * @node: a subtree containing XML Schema informations
6649 *
6650 * Parses the attribute "processContents" and "namespace"
6651 * of a xsd:anyAttribute and xsd:any.
6652 * *WARNING* this interface is highly subject to change
6653 *
6654 * Returns 0 if everything goes fine, a positive error code
6655 * if something is not valid and -1 if an internal error occurs.
6656 */
6657static int
6658xmlSchemaParseWildcardNs(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006659 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006660 xmlSchemaWildcardPtr wildc,
6661 xmlNodePtr node)
6662{
6663 const xmlChar *pc, *ns, *dictnsItem;
6664 int ret = 0;
6665 xmlChar *nsItem;
6666 xmlSchemaWildcardNsPtr tmp, lastNs = NULL;
6667 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006668
Daniel Veillardc0826a72004-08-10 14:17:33 +00006669 pc = xmlSchemaGetProp(ctxt, node, "processContents");
6670 if ((pc == NULL)
6671 || (xmlStrEqual(pc, (const xmlChar *) "strict"))) {
6672 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
6673 } else if (xmlStrEqual(pc, (const xmlChar *) "skip")) {
6674 wildc->processContents = XML_SCHEMAS_ANY_SKIP;
6675 } else if (xmlStrEqual(pc, (const xmlChar *) "lax")) {
6676 wildc->processContents = XML_SCHEMAS_ANY_LAX;
6677 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006678 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006679 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006680 NULL, node,
6681 NULL, "(strict | skip | lax)", pc,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006682 NULL, NULL, NULL);
6683 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006684 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006685 }
6686 /*
6687 * Build the namespace constraints.
6688 */
6689 attr = xmlSchemaGetPropNode(node, "namespace");
6690 ns = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00006691 if ((attr == NULL) || (xmlStrEqual(ns, BAD_CAST "##any")))
Daniel Veillardc0826a72004-08-10 14:17:33 +00006692 wildc->any = 1;
6693 else if (xmlStrEqual(ns, BAD_CAST "##other")) {
6694 wildc->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006695 if (wildc->negNsSet == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006696 return (-1);
6697 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006698 wildc->negNsSet->value = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006699 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006700 const xmlChar *end, *cur;
6701
6702 cur = ns;
6703 do {
6704 while (IS_BLANK_CH(*cur))
6705 cur++;
6706 end = cur;
6707 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
6708 end++;
6709 if (end == cur)
6710 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006711 nsItem = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006712 if ((xmlStrEqual(nsItem, BAD_CAST "##other")) ||
6713 (xmlStrEqual(nsItem, BAD_CAST "##any"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006714 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006715 XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006716 NULL, (xmlNodePtr) attr,
6717 NULL,
6718 "((##any | ##other) | List of (xs:anyURI | "
6719 "(##targetNamespace | ##local)))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00006720 nsItem, NULL, NULL, NULL);
6721 ret = XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER;
6722 } else {
6723 if (xmlStrEqual(nsItem, BAD_CAST "##targetNamespace")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006724 dictnsItem = ctxt->targetNamespace;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006725 } else if (xmlStrEqual(nsItem, BAD_CAST "##local")) {
6726 dictnsItem = NULL;
6727 } else {
6728 /*
6729 * Validate the item (anyURI).
6730 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006731 xmlSchemaPValAttrNodeValue(ctxt, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006732 nsItem, xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI));
6733 dictnsItem = xmlDictLookup(ctxt->dict, nsItem, -1);
6734 }
6735 /*
6736 * Avoid dublicate namespaces.
6737 */
6738 tmp = wildc->nsSet;
6739 while (tmp != NULL) {
6740 if (dictnsItem == tmp->value)
6741 break;
6742 tmp = tmp->next;
6743 }
6744 if (tmp == NULL) {
6745 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
6746 if (tmp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006747 xmlFree(nsItem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006748 return (-1);
6749 }
6750 tmp->value = dictnsItem;
6751 tmp->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006752 if (wildc->nsSet == NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006753 wildc->nsSet = tmp;
6754 else
6755 lastNs->next = tmp;
6756 lastNs = tmp;
6757 }
6758
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006759 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006760 xmlFree(nsItem);
6761 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006762 } while (*cur != 0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006763 }
6764 return (ret);
6765}
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006766
6767static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006768xmlSchemaPCheckParticleCorrect_2(xmlSchemaParserCtxtPtr ctxt,
6769 xmlSchemaParticlePtr item ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006770 xmlNodePtr node,
6771 int minOccurs,
6772 int maxOccurs) {
6773
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006774 if ((maxOccurs == 0) && ( minOccurs == 0))
6775 return (0);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006776 if (maxOccurs != UNBOUNDED) {
6777 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006778 * TODO: Maybe we should better not create the particle,
6779 * if min/max is invalid, since it could confuse the build of the
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006780 * content model.
6781 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006782 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006783 * 3.9.6 Schema Component Constraint: Particle Correct
6784 *
6785 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006786 if (maxOccurs < 1) {
6787 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006788 * 2.2 {max occurs} must be greater than or equal to 1.
6789 */
6790 xmlSchemaPCustomAttrErr(ctxt,
6791 XML_SCHEMAP_P_PROPS_CORRECT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006792 NULL, NULL,
6793 xmlSchemaGetPropNode(node, "maxOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006794 "The value must be greater than or equal to 1");
6795 return (XML_SCHEMAP_P_PROPS_CORRECT_2_2);
6796 } else if (minOccurs > maxOccurs) {
6797 /*
6798 * 2.1 {min occurs} must not be greater than {max occurs}.
6799 */
6800 xmlSchemaPCustomAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006801 XML_SCHEMAP_P_PROPS_CORRECT_2_1,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006802 NULL, NULL,
6803 xmlSchemaGetPropNode(node, "minOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006804 "The value must not be greater than the value of 'maxOccurs'");
6805 return (XML_SCHEMAP_P_PROPS_CORRECT_2_1);
6806 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006807 }
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006808 return (0);
6809}
6810
Daniel Veillardc0826a72004-08-10 14:17:33 +00006811/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006812 * xmlSchemaParseAny:
6813 * @ctxt: a schema validation context
6814 * @schema: the schema being built
6815 * @node: a subtree containing XML Schema informations
6816 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006817 * Parsea a XML schema <any> element. A particle and wildcard
6818 * will be created (except if minOccurs==maxOccurs==0, in this case
6819 * nothing will be created).
Daniel Veillard4255d502002-04-16 15:50:10 +00006820 * *WARNING* this interface is highly subject to change
6821 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006822 * Returns the particle or NULL in case of error or if minOccurs==maxOccurs==0
Daniel Veillard4255d502002-04-16 15:50:10 +00006823 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006824static xmlSchemaParticlePtr
Daniel Veillard4255d502002-04-16 15:50:10 +00006825xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6826 xmlNodePtr node)
6827{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006828 xmlSchemaParticlePtr particle;
Daniel Veillard4255d502002-04-16 15:50:10 +00006829 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006830 xmlSchemaWildcardPtr wild;
6831 int min, max;
6832 xmlAttrPtr attr;
6833 xmlSchemaAnnotPtr annot = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00006834
6835 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6836 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006837 /*
6838 * Check for illegal attributes.
6839 */
6840 attr = node->properties;
6841 while (attr != NULL) {
6842 if (attr->ns == NULL) {
6843 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6844 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
6845 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
6846 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6847 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006848 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006849 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006850 }
6851 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006852 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006853 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006854 }
6855 attr = attr->next;
6856 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006857 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006858 /*
6859 * minOccurs/maxOccurs.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006860 */
6861 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
6862 "(xs:nonNegativeInteger | unbounded)");
6863 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1,
6864 "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006865 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
6866 /*
6867 * Create & parse the wildcard.
6868 */
6869 wild = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY, node);
6870 if (wild == NULL)
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006871 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006872 xmlSchemaParseWildcardNs(ctxt, schema, wild, node);
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006873 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006874 * And now for the children...
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006875 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006876 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00006877 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006878 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006879 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006880 }
6881 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006882 xmlSchemaPContentErr(ctxt,
6883 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006884 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006885 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006886 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006887 /*
6888 * No component if minOccurs==maxOccurs==0.
6889 */
6890 if ((min == 0) && (max == 0)) {
6891 /* Don't free the wildcard, since it's already on the list. */
6892 return (NULL);
6893 }
6894 /*
6895 * Create the particle.
6896 */
6897 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
6898 if (particle == NULL)
6899 return (NULL);
6900 particle->annot = annot;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006901 particle->children = (xmlSchemaTreeItemPtr) wild;
Daniel Veillard4255d502002-04-16 15:50:10 +00006902
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006903 return (particle);
Daniel Veillard4255d502002-04-16 15:50:10 +00006904}
6905
6906/**
6907 * xmlSchemaParseNotation:
6908 * @ctxt: a schema validation context
6909 * @schema: the schema being built
6910 * @node: a subtree containing XML Schema informations
6911 *
6912 * parse a XML schema Notation declaration
6913 *
6914 * Returns the new structure or NULL in case of error
6915 */
6916static xmlSchemaNotationPtr
6917xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006918 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006919{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006920 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00006921 xmlSchemaNotationPtr ret;
6922 xmlNodePtr child = NULL;
6923
6924 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6925 return (NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006926 name = xmlSchemaGetProp(ctxt, node, "name");
Daniel Veillard4255d502002-04-16 15:50:10 +00006927 if (name == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006928 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME,
6929 "Notation has no name\n", NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006930 return (NULL);
6931 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006932 ret = xmlSchemaAddNotation(ctxt, schema, name,
6933 ctxt->targetNamespace, node);
6934 if (ret == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00006935 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006936 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006937
6938 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006939 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006940 child = child->next;
6941 }
6942
Daniel Veillard4255d502002-04-16 15:50:10 +00006943 child = node->children;
6944 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006945 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006946 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006947 }
6948 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006949 xmlSchemaPContentErr(ctxt,
6950 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006951 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006952 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006953 }
6954
6955 return (ret);
6956}
6957
6958/**
6959 * xmlSchemaParseAnyAttribute:
6960 * @ctxt: a schema validation context
6961 * @schema: the schema being built
6962 * @node: a subtree containing XML Schema informations
6963 *
6964 * parse a XML schema AnyAttrribute declaration
6965 * *WARNING* this interface is highly subject to change
6966 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006967 * Returns a wildcard or NULL.
Daniel Veillard4255d502002-04-16 15:50:10 +00006968 */
Daniel Veillard3646d642004-06-02 19:19:14 +00006969static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006970xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6971 xmlSchemaPtr schema, xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006972{
Daniel Veillard3646d642004-06-02 19:19:14 +00006973 xmlSchemaWildcardPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00006974 xmlNodePtr child = NULL;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006975 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006976
6977 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6978 return (NULL);
6979
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006980 ret = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
6981 node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006982 if (ret == NULL) {
6983 return (NULL);
6984 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006985 /*
6986 * Check for illegal attributes.
6987 */
6988 attr = node->properties;
6989 while (attr != NULL) {
6990 if (attr->ns == NULL) {
6991 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6992 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6993 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006994 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006995 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006996 }
6997 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006998 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006999 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007000 }
7001 attr = attr->next;
7002 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007003 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007004 /*
7005 * Parse the namespace list.
7006 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007007 if (xmlSchemaParseWildcardNs(ctxt, schema, ret, node) != 0)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007008 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007009 /*
7010 * And now for the children...
7011 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007012 child = node->children;
7013 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007014 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007015 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007016 }
7017 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00007018 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007019 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007020 NULL, node, child,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007021 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00007022 }
7023
7024 return (ret);
7025}
7026
7027
7028/**
7029 * xmlSchemaParseAttribute:
7030 * @ctxt: a schema validation context
7031 * @schema: the schema being built
7032 * @node: a subtree containing XML Schema informations
7033 *
7034 * parse a XML schema Attrribute declaration
7035 * *WARNING* this interface is highly subject to change
7036 *
William M. Bracke7091952004-05-11 15:09:58 +00007037 * Returns the attribute declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00007038 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007039static xmlSchemaBasicItemPtr
7040xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
7041 xmlSchemaPtr schema,
7042 xmlNodePtr node,
7043 xmlSchemaItemListPtr uses,
7044 int parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +00007045{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007046 const xmlChar *attrValue, *name = NULL, *ns = NULL;
7047 xmlSchemaAttributeUsePtr use = NULL;
7048 xmlNodePtr child = NULL;
7049 xmlAttrPtr attr;
7050 const xmlChar *tmpNs = NULL, *tmpName = NULL, *defValue = NULL;
7051 int isRef = 0, occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7052 int nberrors, hasForm = 0, defValueType = 0;
7053
7054#define WXS_ATTR_DEF_VAL_DEFAULT 1
7055#define WXS_ATTR_DEF_VAL_FIXED 2
7056
7057 /*
7058 * 3.2.3 Constraints on XML Representations of Attribute Declarations
7059 */
7060
7061 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7062 return (NULL);
7063 attr = xmlSchemaGetPropNode(node, "ref");
7064 if (attr != NULL) {
7065 if (xmlSchemaPValAttrNodeQName(pctxt, schema,
7066 NULL, attr, &tmpNs, &tmpName) != 0) {
7067 return (NULL);
7068 }
7069 if (xmlSchemaCheckReference(pctxt, schema, node, attr, tmpNs) != 0)
7070 return(NULL);
7071 isRef = 1;
7072 }
7073 nberrors = pctxt->nberrors;
7074 /*
7075 * Check for illegal attributes.
7076 */
7077 attr = node->properties;
7078 while (attr != NULL) {
7079 if (attr->ns == NULL) {
7080 if (isRef) {
7081 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7082 xmlSchemaPValAttrNodeID(pctxt, attr);
7083 goto attr_next;
7084 } else if (xmlStrEqual(attr->name, BAD_CAST "ref")) {
7085 goto attr_next;
7086 }
7087 } else {
7088 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
7089 goto attr_next;
7090 } else if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7091 xmlSchemaPValAttrNodeID(pctxt, attr);
7092 goto attr_next;
7093 } else if (xmlStrEqual(attr->name, BAD_CAST "type")) {
7094 xmlSchemaPValAttrNodeQName(pctxt, schema, NULL,
7095 attr, &tmpNs, &tmpName);
7096 goto attr_next;
7097 } else if (xmlStrEqual(attr->name, BAD_CAST "form")) {
7098 /*
7099 * Evaluate the target namespace
7100 */
7101 hasForm = 1;
7102 attrValue = xmlSchemaGetNodeContent(pctxt,
7103 (xmlNodePtr) attr);
7104 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
7105 ns = pctxt->targetNamespace;
7106 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified"))
7107 {
7108 xmlSchemaPSimpleTypeErr(pctxt,
7109 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
7110 NULL, (xmlNodePtr) attr,
7111 NULL, "(qualified | unqualified)",
7112 attrValue, NULL, NULL, NULL);
7113 }
7114 goto attr_next;
7115 }
7116 }
7117 if (xmlStrEqual(attr->name, BAD_CAST "use")) {
7118
7119 attrValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7120 /* TODO: Maybe we need to normalize the value beforehand. */
7121 if (xmlStrEqual(attrValue, BAD_CAST "optional"))
7122 occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7123 else if (xmlStrEqual(attrValue, BAD_CAST "prohibited"))
7124 occurs = XML_SCHEMAS_ATTR_USE_PROHIBITED;
7125 else if (xmlStrEqual(attrValue, BAD_CAST "required"))
7126 occurs = XML_SCHEMAS_ATTR_USE_REQUIRED;
7127 else {
7128 xmlSchemaPSimpleTypeErr(pctxt,
7129 XML_SCHEMAP_INVALID_ATTR_USE,
7130 NULL, (xmlNodePtr) attr,
7131 NULL, "(optional | prohibited | required)",
7132 attrValue, NULL, NULL, NULL);
7133 }
7134 goto attr_next;
7135 } else if (xmlStrEqual(attr->name, BAD_CAST "default")) {
7136 /*
7137 * 3.2.3 : 1
7138 * default and fixed must not both be present.
7139 */
7140 if (defValue) {
7141 xmlSchemaPMutualExclAttrErr(pctxt,
7142 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7143 NULL, attr, "default", "fixed");
7144 } else {
7145 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7146 defValueType = WXS_ATTR_DEF_VAL_DEFAULT;
7147 }
7148 goto attr_next;
7149 } else if (xmlStrEqual(attr->name, BAD_CAST "fixed")) {
7150 /*
7151 * 3.2.3 : 1
7152 * default and fixed must not both be present.
7153 */
7154 if (defValue) {
7155 xmlSchemaPMutualExclAttrErr(pctxt,
7156 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7157 NULL, attr, "default", "fixed");
7158 } else {
7159 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7160 defValueType = WXS_ATTR_DEF_VAL_FIXED;
7161 }
7162 goto attr_next;
7163 }
7164 } else if (! xmlStrEqual(attr->ns->href, xmlSchemaNs))
7165 goto attr_next;
7166
7167 xmlSchemaPIllegalAttrErr(pctxt,
7168 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7169
7170attr_next:
7171 attr = attr->next;
7172 }
7173 /*
7174 * 3.2.3 : 2
7175 * If default and use are both present, use must have
7176 * the actual value optional.
7177 */
7178 if ((defValueType == WXS_ATTR_DEF_VAL_DEFAULT) &&
7179 (occurs != XML_SCHEMAS_ATTR_USE_OPTIONAL)) {
7180 xmlSchemaPSimpleTypeErr(pctxt,
7181 XML_SCHEMAP_SRC_ATTRIBUTE_2,
7182 NULL, node, NULL,
7183 "(optional | prohibited | required)", NULL,
7184 "The value of the attribute 'use' must be 'optional' "
7185 "if the attribute 'default' is present",
7186 NULL, NULL);
7187 }
7188 /*
7189 * We want correct attributes.
7190 */
7191 if (nberrors != pctxt->nberrors)
7192 return(NULL);
7193 if (! isRef) {
7194 xmlSchemaAttributePtr attrDecl;
7195
7196 /* TODO: move XML_SCHEMAS_QUALIF_ATTR to the parser. */
7197 if ((! hasForm) && (schema->flags & XML_SCHEMAS_QUALIF_ATTR))
7198 ns = pctxt->targetNamespace;
7199 /*
7200 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7201 * TODO: Move this to the component layer.
7202 */
7203 if (xmlStrEqual(ns, xmlSchemaInstanceNs)) {
7204 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7205 XML_SCHEMAP_NO_XSI,
7206 node, NULL,
7207 "The target namespace must not match '%s'",
7208 xmlSchemaInstanceNs, NULL);
7209 }
7210 attr = xmlSchemaGetPropNode(node, "name");
7211 if (attr == NULL) {
7212 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7213 NULL, node, "name", NULL);
7214 return (NULL);
7215 }
7216 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7217 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7218 return (NULL);
7219 }
7220 /*
7221 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7222 * TODO: Move this to the component layer.
7223 */
7224 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
7225 xmlSchemaPSimpleTypeErr(pctxt,
7226 XML_SCHEMAP_NO_XMLNS,
7227 NULL, (xmlNodePtr) attr,
7228 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7229 "The value of the attribute must not match 'xmlns'",
7230 NULL, NULL);
7231 return (NULL);
7232 }
7233 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED)
7234 goto check_children;
7235 /*
7236 * Create the attribute use component.
7237 */
7238 use = xmlSchemaAddAttributeUse(pctxt, node);
7239 if (use == NULL)
7240 return(NULL);
7241 use->occurs = occurs;
7242 /*
7243 * Create the attribute declaration.
7244 */
7245 attrDecl = xmlSchemaAddAttribute(pctxt, schema, name, ns, node, 0);
7246 if (attrDecl == NULL)
7247 return (NULL);
7248 if (tmpName != NULL) {
7249 attrDecl->typeName = tmpName;
7250 attrDecl->typeNs = tmpNs;
7251 }
7252 use->attrDecl = attrDecl;
7253 /*
7254 * Value constraint.
7255 */
7256 if (defValue != NULL) {
7257 attrDecl->defValue = defValue;
7258 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7259 attrDecl->flags |= XML_SCHEMAS_ATTR_FIXED;
7260 }
7261 } else if (occurs != XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7262 xmlSchemaQNameRefPtr ref;
7263
7264 /*
7265 * Create the attribute use component.
7266 */
7267 use = xmlSchemaAddAttributeUse(pctxt, node);
7268 if (use == NULL)
7269 return(NULL);
7270 /*
7271 * We need to resolve the reference at later stage.
7272 */
7273 WXS_ADD_PENDING(pctxt, use);
7274 use->occurs = occurs;
7275 /*
7276 * Create a QName reference to the attribute declaration.
7277 */
7278 ref = xmlSchemaNewQNameRef(pctxt, XML_SCHEMA_TYPE_ATTRIBUTE,
7279 tmpName, tmpNs);
7280 if (ref == NULL)
7281 return(NULL);
7282 /*
7283 * Assign the reference. This will be substituted for the
7284 * referenced attribute declaration when the QName is resolved.
7285 */
7286 use->attrDecl = WXS_ATTR_CAST ref;
7287 /*
7288 * Value constraint.
7289 */
7290 if (defValue != NULL)
7291 use->defValue = defValue;
7292 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7293 use->flags |= XML_SCHEMA_ATTR_USE_FIXED;
7294 }
7295
7296check_children:
7297 /*
7298 * And now for the children...
7299 */
7300 child = node->children;
7301 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7302 xmlSchemaAttributeUseProhibPtr prohib;
7303
7304 if (IS_SCHEMA(child, "annotation")) {
7305 xmlSchemaParseAnnotation(pctxt, schema, child, 0);
7306 child = child->next;
7307 }
7308 if (child != NULL) {
7309 xmlSchemaPContentErr(pctxt,
7310 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7311 NULL, node, child, NULL,
7312 "(annotation?)");
7313 }
7314 /*
7315 * Check for pointlessness of attribute prohibitions.
7316 */
7317 if (parentType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) {
7318 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7319 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7320 node, NULL,
7321 "Skipping attribute use prohibition, since it is "
7322 "pointless inside an <attributeGroup>",
7323 NULL, NULL, NULL);
7324 return(NULL);
7325 } else if (parentType == XML_SCHEMA_TYPE_EXTENSION) {
7326 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7327 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7328 node, NULL,
7329 "Skipping attribute use prohibition, since it is "
7330 "pointless when extending a type",
7331 NULL, NULL, NULL);
7332 return(NULL);
7333 }
7334 if (! isRef) {
7335 tmpName = name;
7336 tmpNs = ns;
7337 }
7338 /*
7339 * Check for duplicate attribute prohibitions.
7340 */
7341 if (uses) {
7342 int i;
7343
7344 for (i = 0; i < uses->nbItems; i++) {
7345 use = uses->items[i];
7346 if ((use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) &&
7347 (tmpName == (WXS_ATTR_PROHIB_CAST use)->name) &&
7348 (tmpNs == (WXS_ATTR_PROHIB_CAST use)->targetNamespace))
7349 {
7350 xmlChar *str = NULL;
7351
7352 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7353 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7354 node, NULL,
7355 "Skipping duplicate attribute use prohibition '%s'",
7356 xmlSchemaFormatQName(&str, tmpNs, tmpName),
7357 NULL, NULL);
7358 FREE_AND_NULL(str)
7359 return(NULL);
7360 }
7361 }
7362 }
7363 /*
7364 * Create the attribute prohibition helper component.
7365 */
7366 prohib = xmlSchemaAddAttributeUseProhib(pctxt);
7367 if (prohib == NULL)
7368 return(NULL);
7369 prohib->node = node;
7370 prohib->name = tmpName;
7371 prohib->targetNamespace = tmpNs;
7372 if (isRef) {
7373 /*
7374 * We need at least to resolve to the attribute declaration.
7375 */
7376 WXS_ADD_PENDING(pctxt, prohib);
7377 }
7378 return(WXS_BASIC_CAST prohib);
7379 } else {
7380 if (IS_SCHEMA(child, "annotation")) {
7381 /*
7382 * TODO: Should this go into the attr decl?
7383 */
7384 use->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
7385 child = child->next;
7386 }
7387 if (isRef) {
7388 if (child != NULL) {
7389 if (IS_SCHEMA(child, "simpleType"))
7390 /*
7391 * 3.2.3 : 3.2
7392 * If ref is present, then all of <simpleType>,
7393 * form and type must be absent.
7394 */
7395 xmlSchemaPContentErr(pctxt,
7396 XML_SCHEMAP_SRC_ATTRIBUTE_3_2,
7397 NULL, node, child, NULL,
7398 "(annotation?)");
7399 else
7400 xmlSchemaPContentErr(pctxt,
7401 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7402 NULL, node, child, NULL,
7403 "(annotation?)");
7404 }
7405 } else {
7406 if (IS_SCHEMA(child, "simpleType")) {
7407 if (WXS_ATTRUSE_DECL(use)->typeName != NULL) {
7408 /*
7409 * 3.2.3 : 4
7410 * type and <simpleType> must not both be present.
7411 */
7412 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7413 NULL, node, child,
7414 "The attribute 'type' and the <simpleType> child "
7415 "are mutually exclusive", NULL);
7416 } else
7417 WXS_ATTRUSE_TYPEDEF(use) =
7418 xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7419 child = child->next;
7420 }
7421 if (child != NULL)
7422 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7423 NULL, node, child, NULL,
7424 "(annotation?, simpleType?)");
7425 }
7426 }
7427 return (WXS_BASIC_CAST use);
7428}
7429
7430
7431static xmlSchemaAttributePtr
7432xmlSchemaParseGlobalAttribute(xmlSchemaParserCtxtPtr pctxt,
7433 xmlSchemaPtr schema,
7434 xmlNodePtr node)
7435{
7436 const xmlChar *attrValue;
Daniel Veillard4255d502002-04-16 15:50:10 +00007437 xmlSchemaAttributePtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007438 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007439 xmlAttrPtr attr;
William M. Bracke7091952004-05-11 15:09:58 +00007440
7441 /*
7442 * Note that the w3c spec assumes the schema to be validated with schema
7443 * for schemas beforehand.
7444 *
7445 * 3.2.3 Constraints on XML Representations of Attribute Declarations
William M. Bracke7091952004-05-11 15:09:58 +00007446 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007447 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7448 return (NULL);
7449 /*
7450 * 3.2.3 : 3.1
7451 * One of ref or name must be present, but not both
7452 */
7453 attr = xmlSchemaGetPropNode(node, "name");
7454 if (attr == NULL) {
7455 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7456 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007457 return (NULL);
7458 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007459 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7460 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0) {
7461 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007462 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007463 /*
7464 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7465 * TODO: Move this to the component layer.
7466 */
7467 if (xmlStrEqual(attrValue, BAD_CAST "xmlns")) {
7468 xmlSchemaPSimpleTypeErr(pctxt,
7469 XML_SCHEMAP_NO_XMLNS,
7470 NULL, (xmlNodePtr) attr,
7471 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7472 "The value of the attribute must not match 'xmlns'",
7473 NULL, NULL);
7474 return (NULL);
7475 }
7476 /*
7477 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7478 * TODO: Move this to the component layer.
7479 * Or better leave it here and add it to the component layer
7480 * if we have a schema construction API.
7481 */
7482 if (xmlStrEqual(pctxt->targetNamespace, xmlSchemaInstanceNs)) {
7483 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7484 XML_SCHEMAP_NO_XSI, node, NULL,
7485 "The target namespace must not match '%s'",
7486 xmlSchemaInstanceNs, NULL);
7487 }
7488
7489 ret = xmlSchemaAddAttribute(pctxt, schema, attrValue,
7490 pctxt->targetNamespace, node, 1);
7491 if (ret == NULL)
7492 return (NULL);
7493 ret->flags |= XML_SCHEMAS_ATTR_GLOBAL;
7494
7495 /*
7496 * Check for illegal attributes.
7497 */
7498 attr = node->properties;
7499 while (attr != NULL) {
7500 if (attr->ns == NULL) {
7501 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7502 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
7503 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
7504 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7505 (!xmlStrEqual(attr->name, BAD_CAST "type")))
7506 {
7507 xmlSchemaPIllegalAttrErr(pctxt,
7508 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7509 }
7510 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7511 xmlSchemaPIllegalAttrErr(pctxt,
7512 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7513 }
7514 attr = attr->next;
7515 }
7516 xmlSchemaPValAttrQName(pctxt, schema, NULL,
7517 node, "type", &ret->typeNs, &ret->typeName);
7518
7519 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
William M. Bracke7091952004-05-11 15:09:58 +00007520 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007521 * Attribute "fixed".
William M. Bracke7091952004-05-11 15:09:58 +00007522 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007523 ret->defValue = xmlSchemaGetProp(pctxt, node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007524 if (ret->defValue != NULL)
7525 ret->flags |= XML_SCHEMAS_ATTR_FIXED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007526 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007527 * Attribute "default".
7528 */
7529 attr = xmlSchemaGetPropNode(node, "default");
7530 if (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007531 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007532 * 3.2.3 : 1
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007533 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00007534 */
7535 if (ret->flags & XML_SCHEMAS_ATTR_FIXED) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007536 xmlSchemaPMutualExclAttrErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_1,
7537 WXS_BASIC_CAST ret, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007538 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007539 ret->defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007540 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00007541 /*
7542 * And now for the children...
7543 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007544 child = node->children;
7545 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007546 ret->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007547 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007548 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007549 if (IS_SCHEMA(child, "simpleType")) {
7550 if (ret->typeName != NULL) {
7551 /*
7552 * 3.2.3 : 4
7553 * type and <simpleType> must not both be present.
7554 */
7555 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7556 NULL, node, child,
7557 "The attribute 'type' and the <simpleType> child "
7558 "are mutually exclusive", NULL);
7559 } else
7560 ret->subtypes = xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7561 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007562 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007563 if (child != NULL)
7564 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7565 NULL, node, child, NULL,
7566 "(annotation?, simpleType?)");
7567
Daniel Veillard4255d502002-04-16 15:50:10 +00007568 return (ret);
7569}
7570
7571/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007572 * xmlSchemaParseAttributeGroupRef:
Daniel Veillard4255d502002-04-16 15:50:10 +00007573 * @ctxt: a schema validation context
7574 * @schema: the schema being built
7575 * @node: a subtree containing XML Schema informations
7576 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007577 * Parse an attribute group definition reference.
7578 * Note that a reference to an attribute group does not
7579 * correspond to any component at all.
Daniel Veillard4255d502002-04-16 15:50:10 +00007580 * *WARNING* this interface is highly subject to change
7581 *
7582 * Returns the attribute group or NULL in case of error.
7583 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007584static xmlSchemaQNameRefPtr
7585xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
7586 xmlSchemaPtr schema,
7587 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007588{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007589 xmlSchemaQNameRefPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007590 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007591 xmlAttrPtr attr;
7592 const xmlChar *refNs = NULL, *ref = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00007593
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007594 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00007595 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007596
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007597 attr = xmlSchemaGetPropNode(node, "ref");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007598 if (attr == NULL) {
7599 xmlSchemaPMissingAttrErr(pctxt,
7600 XML_SCHEMAP_S4S_ATTR_MISSING,
7601 NULL, node, "ref", NULL);
7602 return (NULL);
7603 }
7604 xmlSchemaPValAttrNodeQName(pctxt, schema,
7605 NULL, attr, &refNs, &ref);
7606 if (xmlSchemaCheckReference(pctxt, schema, node, attr, refNs) != 0)
7607 return(NULL);
7608
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007609 /*
7610 * Check for illegal attributes.
7611 */
7612 attr = node->properties;
7613 while (attr != NULL) {
7614 if (attr->ns == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007615 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007616 (!xmlStrEqual(attr->name, BAD_CAST "id")))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007617 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007618 xmlSchemaPIllegalAttrErr(pctxt,
7619 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007620 }
7621 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007622 xmlSchemaPIllegalAttrErr(pctxt,
7623 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007624 }
7625 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007626 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00007627 /* Attribute ID */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007628 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7629
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007630 /*
7631 * And now for the children...
7632 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007633 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00007634 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007635 /*
7636 * TODO: We do not have a place to store the annotation, do we?
7637 */
7638 xmlSchemaParseAnnotation(pctxt, schema, child, 0);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007639 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007640 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007641 if (child != NULL) {
7642 xmlSchemaPContentErr(pctxt,
7643 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7644 NULL, node, child, NULL,
7645 "(annotation?)");
7646 }
7647
7648 /*
7649 * Handle attribute group redefinitions.
7650 */
7651 if (pctxt->isRedefine && pctxt->redef &&
7652 (pctxt->redef->item->type ==
7653 XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
7654 (ref == pctxt->redef->refName) &&
7655 (refNs == pctxt->redef->refTargetNs))
7656 {
7657 /*
7658 * SPEC src-redefine:
7659 * (7.1) "If it has an <attributeGroup> among its contents
7660 * the ·actual value· of whose ref [attribute] is the same
7661 * as the ·actual value· of its own name attribute plus
7662 * target namespace, then it must have exactly one such group."
7663 */
7664 if (pctxt->redefCounter != 0) {
7665 xmlChar *str = NULL;
7666
7667 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7668 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
7669 "The redefining attribute group definition "
7670 "'%s' must not contain more than one "
7671 "reference to the redefined definition",
7672 xmlSchemaFormatQName(&str, refNs, ref), NULL);
7673 FREE_AND_NULL(str);
7674 return(NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007675 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007676 pctxt->redefCounter++;
7677 /*
7678 * URGENT TODO: How to ensure that the reference will not be
7679 * handled by the normal component resolution mechanism?
7680 */
7681 ret = xmlSchemaNewQNameRef(pctxt,
7682 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7683 if (ret == NULL)
7684 return(NULL);
7685 ret->node = node;
7686 pctxt->redef->reference = WXS_BASIC_CAST ret;
7687 } else {
7688 /*
7689 * Create a QName-reference helper component. We will substitute this
7690 * component for the attribute uses of the referenced attribute group
7691 * definition.
7692 */
7693 ret = xmlSchemaNewQNameRef(pctxt,
7694 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7695 if (ret == NULL)
7696 return(NULL);
7697 ret->node = node;
7698 /* Add to pending items, to be able to resolve the reference. */
7699 WXS_ADD_PENDING(pctxt, ret);
7700 }
7701 return (ret);
7702}
7703
7704/**
7705 * xmlSchemaParseAttributeGroupDefinition:
7706 * @pctxt: a schema validation context
7707 * @schema: the schema being built
7708 * @node: a subtree containing XML Schema informations
7709 *
7710 * parse a XML schema Attribute Group declaration
7711 * *WARNING* this interface is highly subject to change
7712 *
7713 * Returns the attribute group definition or NULL in case of error.
7714 */
7715static xmlSchemaAttributeGroupPtr
7716xmlSchemaParseAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
7717 xmlSchemaPtr schema,
7718 xmlNodePtr node)
7719{
7720 const xmlChar *name;
7721 xmlSchemaAttributeGroupPtr ret;
7722 xmlNodePtr child = NULL;
7723 xmlAttrPtr attr;
7724 int hasRefs = 0;
7725
7726 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7727 return (NULL);
7728
7729 attr = xmlSchemaGetPropNode(node, "name");
7730 if (attr == NULL) {
7731 xmlSchemaPMissingAttrErr(pctxt,
7732 XML_SCHEMAP_S4S_ATTR_MISSING,
7733 NULL, node, "name", NULL);
7734 return (NULL);
7735 }
7736 /*
7737 * The name is crucial, exit if invalid.
7738 */
7739 if (xmlSchemaPValAttrNode(pctxt,
7740 NULL, attr,
7741 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7742 return (NULL);
7743 }
7744 ret = xmlSchemaAddAttributeGroupDefinition(pctxt, schema,
7745 name, pctxt->targetNamespace, node);
7746 if (ret == NULL)
7747 return (NULL);
7748 /*
7749 * Check for illegal attributes.
7750 */
7751 attr = node->properties;
7752 while (attr != NULL) {
7753 if (attr->ns == NULL) {
7754 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7755 (!xmlStrEqual(attr->name, BAD_CAST "id")))
7756 {
7757 xmlSchemaPIllegalAttrErr(pctxt,
7758 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7759 }
7760 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7761 xmlSchemaPIllegalAttrErr(pctxt,
7762 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7763 }
7764 attr = attr->next;
7765 }
7766 /* Attribute ID */
7767 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7768 /*
7769 * And now for the children...
7770 */
7771 child = node->children;
7772 if (IS_SCHEMA(child, "annotation")) {
7773 ret->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
7774 child = child->next;
7775 }
7776 /*
7777 * Parse contained attribute decls/refs.
7778 */
7779 if (xmlSchemaParseLocalAttributes(pctxt, schema, &child,
7780 (xmlSchemaItemListPtr *) &(ret->attrUses),
7781 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, &hasRefs) == -1)
7782 return(NULL);
7783 if (hasRefs)
7784 ret->flags |= XML_SCHEMAS_ATTRGROUP_HAS_REFS;
7785 /*
7786 * Parse the attribute wildcard.
7787 */
7788 if (IS_SCHEMA(child, "anyAttribute")) {
7789 ret->attributeWildcard = xmlSchemaParseAnyAttribute(pctxt,
7790 schema, child);
7791 child = child->next;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007792 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007793 if (child != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007794 xmlSchemaPContentErr(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007795 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007796 NULL, node, child, NULL,
7797 "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))");
Daniel Veillard4255d502002-04-16 15:50:10 +00007798 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007799 return (ret);
7800}
7801
7802/**
William M. Brack2f2a6632004-08-20 23:09:47 +00007803 * xmlSchemaPValAttrFormDefault:
7804 * @value: the value
7805 * @flags: the flags to be modified
7806 * @flagQualified: the specific flag for "qualified"
7807 *
7808 * Returns 0 if the value is valid, 1 otherwise.
7809 */
7810static int
7811xmlSchemaPValAttrFormDefault(const xmlChar *value,
7812 int *flags,
7813 int flagQualified)
7814{
7815 if (xmlStrEqual(value, BAD_CAST "qualified")) {
7816 if ((*flags & flagQualified) == 0)
7817 *flags |= flagQualified;
7818 } else if (!xmlStrEqual(value, BAD_CAST "unqualified"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007819 return (1);
7820
William M. Brack2f2a6632004-08-20 23:09:47 +00007821 return (0);
7822}
7823
7824/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00007825 * xmlSchemaPValAttrBlockFinal:
7826 * @value: the value
7827 * @flags: the flags to be modified
7828 * @flagAll: the specific flag for "#all"
7829 * @flagExtension: the specific flag for "extension"
7830 * @flagRestriction: the specific flag for "restriction"
7831 * @flagSubstitution: the specific flag for "substitution"
7832 * @flagList: the specific flag for "list"
7833 * @flagUnion: the specific flag for "union"
7834 *
7835 * Validates the value of the attribute "final" and "block". The value
7836 * is converted into the specified flag values and returned in @flags.
7837 *
7838 * Returns 0 if the value is valid, 1 otherwise.
7839 */
7840
7841static int
7842xmlSchemaPValAttrBlockFinal(const xmlChar *value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007843 int *flags,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007844 int flagAll,
7845 int flagExtension,
7846 int flagRestriction,
7847 int flagSubstitution,
7848 int flagList,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007849 int flagUnion)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007850{
7851 int ret = 0;
7852
7853 /*
7854 * TODO: This does not check for dublicate entries.
7855 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00007856 if ((flags == NULL) || (value == NULL))
7857 return (-1);
7858 if (value[0] == 0)
7859 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007860 if (xmlStrEqual(value, BAD_CAST "#all")) {
7861 if (flagAll != -1)
7862 *flags |= flagAll;
7863 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007864 if (flagExtension != -1)
7865 *flags |= flagExtension;
7866 if (flagRestriction != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007867 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007868 if (flagSubstitution != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007869 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007870 if (flagList != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007871 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007872 if (flagUnion != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007873 *flags |= flagUnion;
7874 }
7875 } else {
7876 const xmlChar *end, *cur = value;
7877 xmlChar *item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007878
Daniel Veillardc0826a72004-08-10 14:17:33 +00007879 do {
7880 while (IS_BLANK_CH(*cur))
7881 cur++;
7882 end = cur;
7883 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
7884 end++;
7885 if (end == cur)
7886 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007887 item = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007888 if (xmlStrEqual(item, BAD_CAST "extension")) {
7889 if (flagExtension != -1) {
7890 if ((*flags & flagExtension) == 0)
7891 *flags |= flagExtension;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007892 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007893 ret = 1;
7894 } else if (xmlStrEqual(item, BAD_CAST "restriction")) {
7895 if (flagRestriction != -1) {
7896 if ((*flags & flagRestriction) == 0)
7897 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007898 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007899 ret = 1;
7900 } else if (xmlStrEqual(item, BAD_CAST "substitution")) {
7901 if (flagSubstitution != -1) {
7902 if ((*flags & flagSubstitution) == 0)
7903 *flags |= flagSubstitution;
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 "list")) {
7907 if (flagList != -1) {
7908 if ((*flags & flagList) == 0)
7909 *flags |= flagList;
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 "union")) {
7913 if (flagUnion != -1) {
7914 if ((*flags & flagUnion) == 0)
7915 *flags |= flagUnion;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007916 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007917 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007918 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007919 ret = 1;
7920 if (item != NULL)
7921 xmlFree(item);
7922 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007923 } while ((ret == 0) && (*cur != 0));
7924 }
7925
Daniel Veillardc0826a72004-08-10 14:17:33 +00007926 return (ret);
7927}
7928
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007929static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007930xmlSchemaCheckCSelectorXPath(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007931 xmlSchemaIDCPtr idc,
7932 xmlSchemaIDCSelectPtr selector,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007933 xmlAttrPtr attr,
7934 int isField)
7935{
7936 xmlNodePtr node;
7937
7938 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007939 * c-selector-xpath:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007940 * Schema Component Constraint: Selector Value OK
7941 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007942 * TODO: 1 The {selector} must be a valid XPath expression, as defined
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007943 * in [XPath].
7944 */
7945 if (selector == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007946 xmlSchemaPErr(ctxt, idc->node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007947 XML_SCHEMAP_INTERNAL,
7948 "Internal error: xmlSchemaCheckCSelectorXPath, "
7949 "the selector is not specified.\n", NULL, NULL);
7950 return (-1);
7951 }
7952 if (attr == NULL)
7953 node = idc->node;
7954 else
7955 node = (xmlNodePtr) attr;
7956 if (selector->xpath == NULL) {
7957 xmlSchemaPCustomErr(ctxt,
7958 /* TODO: Adjust error code. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007959 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007960 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007961 "The XPath expression of the selector is not valid", NULL);
7962 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
7963 } else {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007964 const xmlChar **nsArray = NULL;
7965 xmlNsPtr *nsList = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007966 /*
7967 * Compile the XPath expression.
7968 */
7969 /*
7970 * TODO: We need the array of in-scope namespaces for compilation.
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007971 * TODO: Call xmlPatterncompile with different options for selector/
7972 * field.
7973 */
7974 nsList = xmlGetNsList(attr->doc, attr->parent);
7975 /*
7976 * Build an array of prefixes and namespaces.
7977 */
7978 if (nsList != NULL) {
7979 int i, count = 0;
7980 xmlNsPtr ns;
7981
7982 for (i = 0; nsList[i] != NULL; i++)
7983 count++;
7984
7985 nsArray = (const xmlChar **) xmlMalloc(
7986 (count * 2 + 1) * sizeof(const xmlChar *));
7987 if (nsArray == NULL) {
7988 xmlSchemaPErrMemory(ctxt, "allocating a namespace array",
7989 NULL);
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00007990 xmlFree(nsList);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007991 return (-1);
7992 }
7993 for (i = 0; i < count; i++) {
7994 ns = nsList[i];
7995 nsArray[2 * i] = nsList[i]->href;
7996 nsArray[2 * i + 1] = nsList[i]->prefix;
7997 }
7998 nsArray[count * 2] = NULL;
7999 xmlFree(nsList);
8000 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008001 /*
8002 * TODO: Differentiate between "selector" and "field".
8003 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008004 if (isField)
8005 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008006 NULL, XML_PATTERN_XSFIELD, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008007 else
8008 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008009 NULL, XML_PATTERN_XSSEL, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008010 if (nsArray != NULL)
8011 xmlFree((xmlChar **) nsArray);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008012
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00008013 if (selector->xpathComp == NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008014 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00008015 /* TODO: Adjust error code? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008016 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008017 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008018 "The XPath expression '%s' could not be "
8019 "compiled", selector->xpath);
8020 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008021 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008022 }
8023 return (0);
8024}
8025
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008026#define ADD_ANNOTATION(annot) \
8027 xmlSchemaAnnotPtr cur = item->annot; \
8028 if (item->annot == NULL) { \
8029 item->annot = annot; \
8030 return (annot); \
8031 } \
8032 cur = item->annot; \
8033 if (cur->next != NULL) { \
8034 cur = cur->next; \
8035 } \
8036 cur->next = annot;
8037
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008038/**
8039 * xmlSchemaAssignAnnotation:
8040 * @item: the schema component
8041 * @annot: the annotation
8042 *
8043 * Adds the annotation to the given schema component.
8044 *
8045 * Returns the given annotaion.
8046 */
8047static xmlSchemaAnnotPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008048xmlSchemaAddAnnotation(xmlSchemaAnnotItemPtr annItem,
8049 xmlSchemaAnnotPtr annot)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008050{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008051 if ((annItem == NULL) || (annot == NULL))
8052 return (NULL);
8053 switch (annItem->type) {
8054 case XML_SCHEMA_TYPE_ELEMENT: {
8055 xmlSchemaElementPtr item = (xmlSchemaElementPtr) annItem;
8056 ADD_ANNOTATION(annot)
8057 }
8058 break;
8059 case XML_SCHEMA_TYPE_ATTRIBUTE: {
8060 xmlSchemaAttributePtr item = (xmlSchemaAttributePtr) annItem;
8061 ADD_ANNOTATION(annot)
8062 }
8063 break;
8064 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
8065 case XML_SCHEMA_TYPE_ANY: {
8066 xmlSchemaWildcardPtr item = (xmlSchemaWildcardPtr) annItem;
8067 ADD_ANNOTATION(annot)
8068 }
8069 break;
8070 case XML_SCHEMA_TYPE_PARTICLE:
8071 case XML_SCHEMA_TYPE_IDC_KEY:
8072 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008073 case XML_SCHEMA_TYPE_IDC_UNIQUE: {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008074 xmlSchemaAnnotItemPtr item = (xmlSchemaAnnotItemPtr) annItem;
8075 ADD_ANNOTATION(annot)
8076 }
8077 break;
8078 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: {
8079 xmlSchemaAttributeGroupPtr item =
8080 (xmlSchemaAttributeGroupPtr) annItem;
8081 ADD_ANNOTATION(annot)
8082 }
8083 break;
8084 case XML_SCHEMA_TYPE_NOTATION: {
8085 xmlSchemaNotationPtr item = (xmlSchemaNotationPtr) annItem;
8086 ADD_ANNOTATION(annot)
8087 }
8088 break;
8089 case XML_SCHEMA_FACET_MININCLUSIVE:
8090 case XML_SCHEMA_FACET_MINEXCLUSIVE:
8091 case XML_SCHEMA_FACET_MAXINCLUSIVE:
8092 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
8093 case XML_SCHEMA_FACET_TOTALDIGITS:
8094 case XML_SCHEMA_FACET_FRACTIONDIGITS:
8095 case XML_SCHEMA_FACET_PATTERN:
8096 case XML_SCHEMA_FACET_ENUMERATION:
8097 case XML_SCHEMA_FACET_WHITESPACE:
8098 case XML_SCHEMA_FACET_LENGTH:
8099 case XML_SCHEMA_FACET_MAXLENGTH:
8100 case XML_SCHEMA_FACET_MINLENGTH: {
8101 xmlSchemaFacetPtr item = (xmlSchemaFacetPtr) annItem;
8102 ADD_ANNOTATION(annot)
8103 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008104 break;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008105 case XML_SCHEMA_TYPE_SIMPLE:
8106 case XML_SCHEMA_TYPE_COMPLEX: {
8107 xmlSchemaTypePtr item = (xmlSchemaTypePtr) annItem;
8108 ADD_ANNOTATION(annot)
8109 }
8110 break;
8111 case XML_SCHEMA_TYPE_GROUP: {
8112 xmlSchemaModelGroupDefPtr item = (xmlSchemaModelGroupDefPtr) annItem;
8113 ADD_ANNOTATION(annot)
8114 }
8115 break;
8116 case XML_SCHEMA_TYPE_SEQUENCE:
8117 case XML_SCHEMA_TYPE_CHOICE:
8118 case XML_SCHEMA_TYPE_ALL: {
8119 xmlSchemaModelGroupPtr item = (xmlSchemaModelGroupPtr) annItem;
8120 ADD_ANNOTATION(annot)
8121 }
8122 break;
8123 default:
8124 xmlSchemaPCustomErr(NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008125 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008126 NULL, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008127 "Internal error: xmlSchemaAddAnnotation, "
8128 "The item is not a annotated schema component", NULL);
8129 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008130 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008131 return (annot);
8132}
8133
8134/**
8135 * xmlSchemaParseIDCSelectorAndField:
8136 * @ctxt: a schema validation context
8137 * @schema: the schema being built
8138 * @node: a subtree containing XML Schema informations
8139 *
8140 * Parses a XML Schema identity-contraint definition's
8141 * <selector> and <field> elements.
8142 *
8143 * Returns the parsed identity-constraint definition.
8144 */
8145static xmlSchemaIDCSelectPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008146xmlSchemaParseIDCSelectorAndField(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008147 xmlSchemaPtr schema,
8148 xmlSchemaIDCPtr idc,
8149 xmlNodePtr node,
8150 int isField)
8151{
8152 xmlSchemaIDCSelectPtr item;
8153 xmlNodePtr child = NULL;
8154 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008155
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008156 /*
8157 * Check for illegal attributes.
8158 */
8159 attr = node->properties;
8160 while (attr != NULL) {
8161 if (attr->ns == NULL) {
8162 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8163 (!xmlStrEqual(attr->name, BAD_CAST "xpath"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008164 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008165 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008166 }
8167 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008168 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008169 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008170 }
8171 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008172 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008173 /*
8174 * Create the item.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008175 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008176 item = (xmlSchemaIDCSelectPtr) xmlMalloc(sizeof(xmlSchemaIDCSelect));
8177 if (item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008178 xmlSchemaPErrMemory(ctxt,
8179 "allocating a 'selector' of an identity-constraint definition",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008180 NULL);
8181 return (NULL);
8182 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008183 memset(item, 0, sizeof(xmlSchemaIDCSelect));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008184 /*
8185 * Attribute "xpath" (mandatory).
8186 */
8187 attr = xmlSchemaGetPropNode(node, "xpath");
8188 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008189 xmlSchemaPMissingAttrErr(ctxt,
8190 XML_SCHEMAP_S4S_ATTR_MISSING,
8191 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008192 "name", NULL);
8193 } else {
8194 item->xpath = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8195 /*
8196 * URGENT TODO: "field"s have an other syntax than "selector"s.
8197 */
8198
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008199 if (xmlSchemaCheckCSelectorXPath(ctxt, idc, item, attr,
8200 isField) == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008201 xmlSchemaPErr(ctxt,
8202 (xmlNodePtr) attr,
8203 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008204 "Internal error: xmlSchemaParseIDCSelectorAndField, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008205 "validating the XPath expression of a IDC selector.\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008206 NULL, NULL);
8207 }
8208
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008209 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008210 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008211 /*
8212 * And now for the children...
8213 */
8214 child = node->children;
8215 if (IS_SCHEMA(child, "annotation")) {
8216 /*
8217 * Add the annotation to the parent IDC.
8218 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008219 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008220 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008221 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008222 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008223 if (child != NULL) {
8224 xmlSchemaPContentErr(ctxt,
8225 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008226 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008227 NULL, "(annotation?)");
8228 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008229
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008230 return (item);
8231}
8232
8233/**
8234 * xmlSchemaParseIDC:
8235 * @ctxt: a schema validation context
8236 * @schema: the schema being built
8237 * @node: a subtree containing XML Schema informations
8238 *
8239 * Parses a XML Schema identity-contraint definition.
8240 *
8241 * Returns the parsed identity-constraint definition.
8242 */
8243static xmlSchemaIDCPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008244xmlSchemaParseIDC(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008245 xmlSchemaPtr schema,
8246 xmlNodePtr node,
8247 xmlSchemaTypeType idcCategory,
8248 const xmlChar *targetNamespace)
8249{
8250 xmlSchemaIDCPtr item = NULL;
8251 xmlNodePtr child = NULL;
8252 xmlAttrPtr attr;
8253 const xmlChar *name = NULL;
8254 xmlSchemaIDCSelectPtr field = NULL, lastField = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008255
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008256 /*
8257 * Check for illegal attributes.
8258 */
8259 attr = node->properties;
8260 while (attr != NULL) {
8261 if (attr->ns == NULL) {
8262 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8263 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8264 ((idcCategory != XML_SCHEMA_TYPE_IDC_KEYREF) ||
8265 (!xmlStrEqual(attr->name, BAD_CAST "refer")))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008266 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008267 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008268 }
8269 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008270 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008271 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008272 }
8273 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008274 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008275 /*
8276 * Attribute "name" (mandatory).
8277 */
8278 attr = xmlSchemaGetPropNode(node, "name");
8279 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008280 xmlSchemaPMissingAttrErr(ctxt,
8281 XML_SCHEMAP_S4S_ATTR_MISSING,
8282 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008283 "name", NULL);
8284 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008285 } else if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008286 NULL, attr,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008287 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
8288 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008289 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008290 /* Create the component. */
8291 item = xmlSchemaAddIDC(ctxt, schema, name, targetNamespace,
8292 idcCategory, node);
8293 if (item == NULL)
8294 return(NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008295
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008296 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008297 if (idcCategory == XML_SCHEMA_TYPE_IDC_KEYREF) {
8298 /*
8299 * Attribute "refer" (mandatory).
8300 */
8301 attr = xmlSchemaGetPropNode(node, "refer");
8302 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008303 xmlSchemaPMissingAttrErr(ctxt,
8304 XML_SCHEMAP_S4S_ATTR_MISSING,
8305 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008306 "refer", NULL);
8307 } else {
8308 /*
8309 * Create a reference item.
8310 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008311 item->ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_IDC_KEY,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008312 NULL, NULL);
8313 if (item->ref == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008314 return (NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008315 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008316 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008317 &(item->ref->targetNamespace),
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008318 &(item->ref->name));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008319 xmlSchemaCheckReference(ctxt, schema, node, attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008320 item->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008321 }
8322 }
8323 /*
8324 * And now for the children...
8325 */
8326 child = node->children;
8327 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008328 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008329 child = child->next;
8330 }
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008331 if (child == NULL) {
8332 xmlSchemaPContentErr(ctxt,
8333 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008334 NULL, node, child,
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008335 "A child element is missing",
8336 "(annotation?, (selector, field+))");
8337 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008338 /*
8339 * Child element <selector>.
8340 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008341 if (IS_SCHEMA(child, "selector")) {
8342 item->selector = xmlSchemaParseIDCSelectorAndField(ctxt, schema,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008343 item, child, 0);
8344 child = child->next;
8345 /*
8346 * Child elements <field>.
8347 */
8348 if (IS_SCHEMA(child, "field")) {
8349 do {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008350 field = xmlSchemaParseIDCSelectorAndField(ctxt, schema,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008351 item, child, 1);
8352 if (field != NULL) {
8353 field->index = item->nbFields;
8354 item->nbFields++;
8355 if (lastField != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008356 lastField->next = field;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008357 else
8358 item->fields = field;
8359 lastField = field;
8360 }
8361 child = child->next;
8362 } while (IS_SCHEMA(child, "field"));
8363 } else {
8364 xmlSchemaPContentErr(ctxt,
8365 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008366 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008367 NULL, "(annotation?, (selector, field+))");
8368 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008369 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008370 if (child != NULL) {
8371 xmlSchemaPContentErr(ctxt,
8372 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008373 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008374 NULL, "(annotation?, (selector, field+))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008375 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008376
8377 return (item);
8378}
8379
Daniel Veillardc0826a72004-08-10 14:17:33 +00008380/**
Daniel Veillard4255d502002-04-16 15:50:10 +00008381 * xmlSchemaParseElement:
8382 * @ctxt: a schema validation context
8383 * @schema: the schema being built
8384 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008385 * @topLevel: indicates if this is global declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00008386 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008387 * Parses a XML schema element declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00008388 * *WARNING* this interface is highly subject to change
8389 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008390 * Returns the element declaration or a particle; NULL in case
8391 * of an error or if the particle has minOccurs==maxOccurs==0.
Daniel Veillard4255d502002-04-16 15:50:10 +00008392 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008393static xmlSchemaBasicItemPtr
Daniel Veillard4255d502002-04-16 15:50:10 +00008394xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008395 xmlNodePtr node, int *isElemRef, int topLevel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008396{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008397 xmlSchemaElementPtr decl = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008398 xmlSchemaParticlePtr particle = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008399 xmlSchemaAnnotPtr annot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008400 xmlNodePtr child = NULL;
8401 xmlAttrPtr attr, nameAttr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008402 int min, max, isRef = 0;
8403 xmlChar *des = NULL;
William M. Bracke7091952004-05-11 15:09:58 +00008404
8405 /* 3.3.3 Constraints on XML Representations of Element Declarations */
8406 /* TODO: Complete implementation of 3.3.6 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008407
Daniel Veillard4255d502002-04-16 15:50:10 +00008408 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008409 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008410
8411 if (isElemRef != NULL)
8412 *isElemRef = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008413 /*
8414 * If we get a "ref" attribute on a local <element> we will assume it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008415 * a reference - even if there's a "name" attribute; this seems to be more
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008416 * robust.
8417 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008418 nameAttr = xmlSchemaGetPropNode(node, "name");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008419 attr = xmlSchemaGetPropNode(node, "ref");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008420 if ((topLevel) || (attr == NULL)) {
8421 if (nameAttr == NULL) {
8422 xmlSchemaPMissingAttrErr(ctxt,
8423 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008424 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008425 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008426 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008427 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00008428 isRef = 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008429
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008430 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008431 child = node->children;
8432 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008433 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008434 child = child->next;
8435 }
8436 /*
8437 * Skip particle part if a global declaration.
8438 */
8439 if (topLevel)
8440 goto declaration_part;
8441 /*
8442 * The particle part ==================================================
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008443 */
8444 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
8445 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1, "(xs:nonNegativeInteger | unbounded)");
8446 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008447 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
8448 if (particle == NULL)
8449 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008450
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008451 /* ret->flags |= XML_SCHEMAS_ELEM_REF; */
8452
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008453 if (isRef) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008454 const xmlChar *refNs = NULL, *ref = NULL;
8455 xmlSchemaQNameRefPtr refer = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008456 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008457 * The reference part =============================================
Daniel Veillardc0826a72004-08-10 14:17:33 +00008458 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008459 if (isElemRef != NULL)
8460 *isElemRef = 1;
8461
Daniel Veillardc0826a72004-08-10 14:17:33 +00008462 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008463 NULL, attr, &refNs, &ref);
8464 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008465 /*
8466 * SPEC (3.3.3 : 2.1) "One of ref or name must be present, but not both"
Daniel Veillardc0826a72004-08-10 14:17:33 +00008467 */
8468 if (nameAttr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008469 xmlSchemaPMutualExclAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008470 XML_SCHEMAP_SRC_ELEMENT_2_1, NULL, nameAttr, "ref", "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008471 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008472 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008473 * Check for illegal attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008474 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008475 attr = node->properties;
8476 while (attr != NULL) {
8477 if (attr->ns == NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008478 if (xmlStrEqual(attr->name, BAD_CAST "ref") ||
8479 xmlStrEqual(attr->name, BAD_CAST "name") ||
8480 xmlStrEqual(attr->name, BAD_CAST "id") ||
8481 xmlStrEqual(attr->name, BAD_CAST "maxOccurs") ||
8482 xmlStrEqual(attr->name, BAD_CAST "minOccurs"))
8483 {
8484 attr = attr->next;
8485 continue;
8486 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008487 /* SPEC (3.3.3 : 2.2) */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008488 xmlSchemaPCustomAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008489 XML_SCHEMAP_SRC_ELEMENT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008490 NULL, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008491 "Only the attributes 'minOccurs', 'maxOccurs' and "
8492 "'id' are allowed in addition to 'ref'");
8493 break;
8494 }
8495 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
8496 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008497 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008498 }
8499 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008500 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008501 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008502 * No children except <annotation> expected.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008503 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008504 if (child != NULL) {
8505 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008506 NULL, node, child, NULL, "(annotation?)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008507 }
8508 if ((min == 0) && (max == 0))
8509 goto return_null;
8510 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008511 * Create the reference item and attach it to the particle.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008512 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008513 refer = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_ELEMENT,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008514 ref, refNs);
8515 if (refer == NULL)
8516 goto return_null;
8517 particle->children = (xmlSchemaTreeItemPtr) refer;
8518 particle->annot = annot;
8519 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008520 * Add the particle to pending components, since the reference
8521 * need to be resolved.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008522 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008523 WXS_ADD_PENDING(ctxt, particle);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008524 return ((xmlSchemaBasicItemPtr) particle);
8525 }
8526 /*
8527 * The declaration part ===============================================
8528 */
8529declaration_part:
8530 {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008531 const xmlChar *ns = NULL, *fixed, *name, *attrValue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008532 xmlSchemaIDCPtr curIDC = NULL, lastIDC = NULL;
8533
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008534 if (xmlSchemaPValAttrNode(ctxt, NULL, nameAttr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008535 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008536 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008537 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008538 * Evaluate the target namespace.
8539 */
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008540 if (topLevel) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008541 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008542 } else {
8543 attr = xmlSchemaGetPropNode(node, "form");
8544 if (attr != NULL) {
8545 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8546 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008547 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008548 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008549 xmlSchemaPSimpleTypeErr(ctxt,
8550 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
8551 NULL, (xmlNodePtr) attr,
8552 NULL, "(qualified | unqualified)",
8553 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008554 }
8555 } else if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008556 ns = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008557 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008558 decl = xmlSchemaAddElement(ctxt, name, ns, node, topLevel);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008559 if (decl == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008560 goto return_null;
William M. Bracke7091952004-05-11 15:09:58 +00008561 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008562 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008563 * Check for illegal attributes.
8564 */
William M. Bracke7091952004-05-11 15:09:58 +00008565 attr = node->properties;
8566 while (attr != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008567 if (attr->ns == NULL) {
8568 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8569 (!xmlStrEqual(attr->name, BAD_CAST "type")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008570 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008571 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008572 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008573 (!xmlStrEqual(attr->name, BAD_CAST "block")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008574 (!xmlStrEqual(attr->name, BAD_CAST "nillable")))
8575 {
8576 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008577 if ((!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008578 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008579 (!xmlStrEqual(attr->name, BAD_CAST "form")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008580 {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008581 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008582 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008583 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008584 } else if ((!xmlStrEqual(attr->name, BAD_CAST "final")) &&
8585 (!xmlStrEqual(attr->name, BAD_CAST "abstract")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008586 (!xmlStrEqual(attr->name, BAD_CAST "substitutionGroup"))) {
8587
8588 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008589 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008590 }
8591 }
8592 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008593
Daniel Veillardc0826a72004-08-10 14:17:33 +00008594 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008595 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Bracke7091952004-05-11 15:09:58 +00008596 }
8597 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008598 }
William M. Bracke7091952004-05-11 15:09:58 +00008599 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008600 * Extract/validate attributes.
8601 */
8602 if (topLevel) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008603 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008604 * Process top attributes of global element declarations here.
8605 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008606 decl->flags |= XML_SCHEMAS_ELEM_GLOBAL;
8607 decl->flags |= XML_SCHEMAS_ELEM_TOPLEVEL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008608 xmlSchemaPValAttrQName(ctxt, schema,
8609 NULL, node, "substitutionGroup",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008610 &(decl->substGroupNs), &(decl->substGroup));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008611 if (xmlGetBooleanProp(ctxt, node, "abstract", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008612 decl->flags |= XML_SCHEMAS_ELEM_ABSTRACT;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008613 /*
8614 * Attribute "final".
8615 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008616 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008617 if (attr == NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008618 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
8619 decl->flags |= XML_SCHEMAS_ELEM_FINAL_EXTENSION;
8620 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
8621 decl->flags |= XML_SCHEMAS_ELEM_FINAL_RESTRICTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008622 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008623 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8624 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008625 -1,
8626 XML_SCHEMAS_ELEM_FINAL_EXTENSION,
8627 XML_SCHEMAS_ELEM_FINAL_RESTRICTION, -1, -1, -1) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008628 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008629 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008630 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008631 NULL, "(#all | List of (extension | restriction))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00008632 attrValue, NULL, NULL, NULL);
8633 }
8634 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008635 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008636 /*
8637 * Attribute "block".
8638 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008639 attr = xmlSchemaGetPropNode(node, "block");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008640 if (attr == NULL) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00008641 /*
8642 * Apply default "block" values.
8643 */
8644 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
8645 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_RESTRICTION;
8646 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
8647 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_EXTENSION;
8648 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
8649 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008650 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008651 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8652 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008653 -1,
8654 XML_SCHEMAS_ELEM_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008655 XML_SCHEMAS_ELEM_BLOCK_RESTRICTION,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008656 XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION, -1, -1) != 0) {
8657 xmlSchemaPSimpleTypeErr(ctxt,
8658 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008659 NULL, (xmlNodePtr) attr,
William M. Brack2f2a6632004-08-20 23:09:47 +00008660 NULL, "(#all | List of (extension | "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008661 "restriction | substitution))", attrValue,
8662 NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008663 }
8664 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008665 if (xmlGetBooleanProp(ctxt, node, "nillable", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008666 decl->flags |= XML_SCHEMAS_ELEM_NILLABLE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008667
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008668 attr = xmlSchemaGetPropNode(node, "type");
8669 if (attr != NULL) {
8670 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008671 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008672 &(decl->namedTypeNs), &(decl->namedType));
8673 xmlSchemaCheckReference(ctxt, schema, node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008674 attr, decl->namedTypeNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008675 }
8676 decl->value = xmlSchemaGetProp(ctxt, node, "default");
8677 attr = xmlSchemaGetPropNode(node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008678 if (attr != NULL) {
8679 fixed = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008680 if (decl->value != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008681 /*
8682 * 3.3.3 : 1
8683 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008684 */
8685 xmlSchemaPMutualExclAttrErr(ctxt,
8686 XML_SCHEMAP_SRC_ELEMENT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008687 NULL, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008688 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008689 decl->flags |= XML_SCHEMAS_ELEM_FIXED;
8690 decl->value = fixed;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008691 }
William M. Bracke7091952004-05-11 15:09:58 +00008692 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008693 /*
8694 * And now for the children...
8695 */
William M. Bracke7091952004-05-11 15:09:58 +00008696 if (IS_SCHEMA(child, "complexType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008697 /*
8698 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008699 * "type" and either <simpleType> or <complexType> are mutually
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008700 * exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008701 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008702 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008703 xmlSchemaPContentErr(ctxt,
8704 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008705 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008706 "The attribute 'type' and the <complexType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008707 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008708 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008709 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseComplexType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008710 child = child->next;
8711 } else if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008712 /*
8713 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008714 * "type" and either <simpleType> or <complexType> are
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008715 * mutually exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008716 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008717 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008718 xmlSchemaPContentErr(ctxt,
8719 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008720 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008721 "The attribute 'type' and the <simpleType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008722 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008723 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008724 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008725 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008726 }
William M. Bracke7091952004-05-11 15:09:58 +00008727 while ((IS_SCHEMA(child, "unique")) ||
Daniel Veillardc0826a72004-08-10 14:17:33 +00008728 (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008729 if (IS_SCHEMA(child, "unique")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008730 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008731 XML_SCHEMA_TYPE_IDC_UNIQUE, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008732 } else if (IS_SCHEMA(child, "key")) {
8733 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008734 XML_SCHEMA_TYPE_IDC_KEY, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008735 } else if (IS_SCHEMA(child, "keyref")) {
8736 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008737 XML_SCHEMA_TYPE_IDC_KEYREF, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008738 }
8739 if (lastIDC != NULL)
8740 lastIDC->next = curIDC;
8741 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008742 decl->idcs = (void *) curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008743 lastIDC = curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008744 child = child->next;
William M. Bracke7091952004-05-11 15:09:58 +00008745 }
8746 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008747 xmlSchemaPContentErr(ctxt,
8748 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008749 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008750 NULL, "(annotation?, ((simpleType | complexType)?, "
8751 "(unique | key | keyref)*))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008752 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008753 decl->annot = annot;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008754 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008755 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008756 * NOTE: Element Declaration Representation OK 4. will be checked at a
Daniel Veillardc0826a72004-08-10 14:17:33 +00008757 * different layer.
8758 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008759 FREE_AND_NULL(des)
8760 if (topLevel)
8761 return ((xmlSchemaBasicItemPtr) decl);
8762 else {
8763 particle->children = (xmlSchemaTreeItemPtr) decl;
8764 return ((xmlSchemaBasicItemPtr) particle);
8765 }
8766
8767return_null:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008768 FREE_AND_NULL(des);
8769 if (annot != NULL) {
8770 if (particle != NULL)
8771 particle->annot = NULL;
8772 if (decl != NULL)
8773 decl->annot = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008774 xmlSchemaFreeAnnot(annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008775 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008776 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00008777}
8778
8779/**
8780 * xmlSchemaParseUnion:
8781 * @ctxt: a schema validation context
8782 * @schema: the schema being built
8783 * @node: a subtree containing XML Schema informations
8784 *
8785 * parse a XML schema Union definition
8786 * *WARNING* this interface is highly subject to change
8787 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008788 * Returns -1 in case of internal error, 0 in case of success and a positive
8789 * error code otherwise.
Daniel Veillard4255d502002-04-16 15:50:10 +00008790 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008791static int
Daniel Veillard4255d502002-04-16 15:50:10 +00008792xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008793 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008794{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008795 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008796 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008797 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008798 const xmlChar *cur = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00008799
8800 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008801 return (-1);
8802 /* Not a component, don't create it. */
8803 type = ctxt->ctxtType;
8804 /*
8805 * Mark the simple type as being of variety "union".
8806 */
8807 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008808 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008809 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
8810 * then the ·simple ur-type definition·."
8811 */
8812 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008813 /*
8814 * Check for illegal attributes.
8815 */
8816 attr = node->properties;
8817 while (attr != NULL) {
8818 if (attr->ns == NULL) {
8819 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8820 (!xmlStrEqual(attr->name, BAD_CAST "memberTypes"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008821 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008822 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008823 }
8824 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008825 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008826 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008827 }
8828 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008829 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008830 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +00008831 /*
8832 * Attribute "memberTypes". This is a list of QNames.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008833 * TODO: Check the value to contain anything.
William M. Brack2f2a6632004-08-20 23:09:47 +00008834 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008835 attr = xmlSchemaGetPropNode(node, "memberTypes");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008836 if (attr != NULL) {
8837 const xmlChar *end;
8838 xmlChar *tmp;
8839 const xmlChar *localName, *nsName;
8840 xmlSchemaTypeLinkPtr link, lastLink = NULL;
8841 xmlSchemaQNameRefPtr ref;
8842
8843 cur = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008844 type->base = cur;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008845 do {
8846 while (IS_BLANK_CH(*cur))
8847 cur++;
8848 end = cur;
8849 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
8850 end++;
8851 if (end == cur)
8852 break;
8853 tmp = xmlStrndup(cur, end - cur);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008854 if (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008855 NULL, attr, BAD_CAST tmp, &nsName, &localName) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008856 /*
8857 * Create the member type link.
8858 */
8859 link = (xmlSchemaTypeLinkPtr)
8860 xmlMalloc(sizeof(xmlSchemaTypeLink));
8861 if (link == NULL) {
8862 xmlSchemaPErrMemory(ctxt, "xmlSchemaParseUnion, "
8863 "allocating a type link", NULL);
8864 return (-1);
8865 }
8866 link->type = NULL;
8867 link->next = NULL;
8868 if (lastLink == NULL)
8869 type->memberTypes = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008870 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008871 lastLink->next = link;
8872 lastLink = link;
8873 /*
8874 * Create a reference item.
8875 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008876 ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_SIMPLE,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008877 localName, nsName);
8878 if (ref == NULL) {
8879 FREE_AND_NULL(tmp)
8880 return (-1);
8881 }
8882 /*
8883 * Assign the reference to the link, it will be resolved
8884 * later during fixup of the union simple type.
8885 */
8886 link->type = (xmlSchemaTypePtr) ref;
8887 }
8888 FREE_AND_NULL(tmp)
8889 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008890 } while (*cur != 0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008891
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008892 }
William M. Brack2f2a6632004-08-20 23:09:47 +00008893 /*
8894 * And now for the children...
8895 */
Daniel Veillard4255d502002-04-16 15:50:10 +00008896 child = node->children;
8897 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008898 /*
8899 * Add the annotation to the simple type ancestor.
8900 */
8901 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008902 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008903 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00008904 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008905 if (IS_SCHEMA(child, "simpleType")) {
8906 xmlSchemaTypePtr subtype, last = NULL;
8907
8908 /*
8909 * Anchor the member types in the "subtypes" field of the
8910 * simple type.
8911 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008912 while (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008913 subtype = (xmlSchemaTypePtr)
8914 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
8915 if (subtype != NULL) {
8916 if (last == NULL) {
8917 type->subtypes = subtype;
8918 last = subtype;
8919 } else {
8920 last->next = subtype;
8921 last = subtype;
8922 }
8923 last->next = NULL;
8924 }
8925 child = child->next;
8926 }
Daniel Veillard4255d502002-04-16 15:50:10 +00008927 }
8928 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00008929 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008930 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008931 NULL, node, child, NULL, "(annotation?, simpleType*)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008932 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008933 if ((attr == NULL) && (type->subtypes == NULL)) {
8934 /*
8935 * src-union-memberTypes-or-simpleTypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008936 * Either the memberTypes [attribute] of the <union> element must
8937 * be non-empty or there must be at least one simpleType [child].
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008938 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008939 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008940 XML_SCHEMAP_SRC_UNION_MEMBERTYPES_OR_SIMPLETYPES,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008941 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008942 "Either the attribute 'memberTypes' or "
8943 "at least one <simpleType> child must be present", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008944 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008945 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00008946}
8947
8948/**
8949 * xmlSchemaParseList:
8950 * @ctxt: a schema validation context
8951 * @schema: the schema being built
8952 * @node: a subtree containing XML Schema informations
8953 *
8954 * parse a XML schema List definition
8955 * *WARNING* this interface is highly subject to change
8956 *
William M. Bracke7091952004-05-11 15:09:58 +00008957 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00008958 * 1 in case of success.
8959 */
8960static xmlSchemaTypePtr
8961xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008962 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008963{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008964 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008965 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008966 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00008967
8968 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
8969 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008970 /* Not a component, don't create it. */
8971 type = ctxt->ctxtType;
8972 /*
8973 * Mark the type as being of variety "list".
8974 */
8975 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008976 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008977 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
8978 * then the ·simple ur-type definition·."
8979 */
8980 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008981 /*
8982 * Check for illegal attributes.
8983 */
8984 attr = node->properties;
8985 while (attr != NULL) {
8986 if (attr->ns == NULL) {
8987 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8988 (!xmlStrEqual(attr->name, BAD_CAST "itemType"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008989 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008990 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008991 }
8992 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008993 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008994 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008995 }
8996 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008997 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008998
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008999 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009000
William M. Brack2f2a6632004-08-20 23:09:47 +00009001 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009002 * Attribute "itemType". NOTE that we will use the "ref" and "refNs"
9003 * fields for holding the reference to the itemType.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009004 *
9005 * REVAMP TODO: Use the "base" and "baseNs" fields, since we will remove
9006 * the "ref" fields.
William M. Brack2f2a6632004-08-20 23:09:47 +00009007 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009008 xmlSchemaPValAttrQName(ctxt, schema, NULL,
9009 node, "itemType", &(type->baseNs), &(type->base));
William M. Brack2f2a6632004-08-20 23:09:47 +00009010 /*
9011 * And now for the children...
9012 */
Daniel Veillard4255d502002-04-16 15:50:10 +00009013 child = node->children;
9014 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009015 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009016 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009017 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009018 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009019 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009020 /*
9021 * src-list-itemType-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009022 * Either the itemType [attribute] or the <simpleType> [child] of
9023 * the <list> element must be present, but not both.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009024 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009025 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009026 xmlSchemaPCustomErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009027 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009028 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +00009029 "The attribute 'itemType' and the <simpleType> child "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009030 "are mutually exclusive", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009031 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009032 type->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Brack2f2a6632004-08-20 23:09:47 +00009033 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009034 child = child->next;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009035 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009036 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009037 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009038 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009039 "Either the attribute 'itemType' or the <simpleType> child "
9040 "must be present", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009041 }
9042 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009043 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009044 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009045 NULL, node, child, NULL, "(annotation?, simpleType?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00009046 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009047 if ((type->base == NULL) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009048 (type->subtypes == NULL) &&
9049 (xmlSchemaGetPropNode(node, "itemType") == NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009050 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009051 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009052 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009053 "Either the attribute 'itemType' or the <simpleType> child "
9054 "must be present", NULL);
9055 }
9056 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009057}
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009058
Daniel Veillard4255d502002-04-16 15:50:10 +00009059/**
9060 * xmlSchemaParseSimpleType:
9061 * @ctxt: a schema validation context
9062 * @schema: the schema being built
9063 * @node: a subtree containing XML Schema informations
9064 *
9065 * parse a XML schema Simple Type definition
9066 * *WARNING* this interface is highly subject to change
9067 *
William M. Bracke7091952004-05-11 15:09:58 +00009068 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillardc0826a72004-08-10 14:17:33 +00009069 * 1 in case of success.
Daniel Veillard4255d502002-04-16 15:50:10 +00009070 */
9071static xmlSchemaTypePtr
9072xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00009073 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00009074{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009075 xmlSchemaTypePtr type, oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009076 xmlNodePtr child = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009077 const xmlChar *attrValue = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009078 xmlAttrPtr attr;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009079 int hasRestriction = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00009080
9081 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9082 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009083
Daniel Veillardc0826a72004-08-10 14:17:33 +00009084 if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009085 attr = xmlSchemaGetPropNode(node, "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009086 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009087 xmlSchemaPMissingAttrErr(ctxt,
9088 XML_SCHEMAP_S4S_ATTR_MISSING,
9089 NULL, node,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009090 "name", NULL);
9091 return (NULL);
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009092 } else {
9093 if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009094 NULL, attr,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009095 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0)
9096 return (NULL);
9097 /*
9098 * Skip built-in types.
9099 */
9100 if (ctxt->isS4S) {
9101 xmlSchemaTypePtr biType;
9102
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009103 if (ctxt->isRedefine) {
9104 /*
9105 * REDEFINE: Disallow redefinition of built-in-types.
9106 * TODO: It seems that the spec does not say anything
9107 * about this case.
9108 */
9109 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009110 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009111 "Redefinition of built-in simple types is not "
9112 "supported", NULL);
9113 return(NULL);
9114 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009115 biType = xmlSchemaGetPredefinedType(attrValue, xmlSchemaNs);
9116 if (biType != NULL)
9117 return (biType);
9118 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00009119 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009120 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009121 /*
9122 * TargetNamespace:
9123 * SPEC "The ·actual value· of the targetNamespace [attribute]
9124 * of the <schema> ancestor element information item if present,
9125 * otherwise ·absent·.
9126 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00009127 if (topLevel == 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009128#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009129 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009130#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +00009131 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00009132 * Parse as local simple type definition.
Daniel Veillard01fa6152004-06-29 17:04:39 +00009133 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009134#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00009135 snprintf(buf, 39, "#ST%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009136 type = xmlSchemaAddType(ctxt, schema,
9137 XML_SCHEMA_TYPE_SIMPLE,
9138 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009139 ctxt->targetNamespace, node, 0);
9140#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009141 type = xmlSchemaAddType(ctxt, schema,
9142 XML_SCHEMA_TYPE_SIMPLE,
9143 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009144#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +00009145 if (type == NULL)
9146 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009147 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009148 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009149 /*
9150 * Check for illegal attributes.
9151 */
9152 attr = node->properties;
9153 while (attr != NULL) {
9154 if (attr->ns == NULL) {
9155 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009156 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009157 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillard01fa6152004-06-29 17:04:39 +00009158 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009159 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009160 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009161 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009162 }
9163 attr = attr->next;
Daniel Veillard01fa6152004-06-29 17:04:39 +00009164 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009165 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00009166 /*
9167 * Parse as global simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009168 *
9169 * Note that attrValue is the value of the attribute "name" here.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009170 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009171 type = xmlSchemaAddType(ctxt, schema, XML_SCHEMA_TYPE_SIMPLE,
9172 attrValue, ctxt->targetNamespace, node, 1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009173 if (type == NULL)
9174 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009175 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009176 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009177 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
9178 /*
9179 * Check for illegal attributes.
9180 */
9181 attr = node->properties;
9182 while (attr != NULL) {
9183 if (attr->ns == NULL) {
9184 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9185 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009186 (!xmlStrEqual(attr->name, BAD_CAST "final"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009187 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009188 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009189 }
9190 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009191 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009192 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009193 }
9194 attr = attr->next;
9195 }
9196 /*
9197 * Attribute "final".
9198 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009199 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009200 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009201 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9202 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
9203 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9204 type->flags |= XML_SCHEMAS_TYPE_FINAL_LIST;
9205 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9206 type->flags |= XML_SCHEMAS_TYPE_FINAL_UNION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009207 } else {
9208 attrValue = xmlSchemaGetProp(ctxt, node, "final");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009209 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
9210 -1, -1, XML_SCHEMAS_TYPE_FINAL_RESTRICTION, -1,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009211 XML_SCHEMAS_TYPE_FINAL_LIST,
9212 XML_SCHEMAS_TYPE_FINAL_UNION) != 0) {
9213
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009214 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009215 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009216 WXS_BASIC_CAST type, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009217 NULL, "(#all | List of (list | union | restriction)",
Daniel Veillardc0826a72004-08-10 14:17:33 +00009218 attrValue, NULL, NULL, NULL);
9219 }
9220 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009221 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009222 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009223 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009224 /*
9225 * And now for the children...
9226 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009227 oldCtxtType = ctxt->ctxtType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009228
Daniel Veillardc0826a72004-08-10 14:17:33 +00009229 ctxt->ctxtType = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009230
Daniel Veillard4255d502002-04-16 15:50:10 +00009231 child = node->children;
9232 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009233 type->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009234 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009235 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009236 if (child == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009237 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009238 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009239 "(annotation?, (restriction | list | union))");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009240 } else if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009241 xmlSchemaParseRestriction(ctxt, schema, child,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009242 XML_SCHEMA_TYPE_SIMPLE);
9243 hasRestriction = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009244 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009245 } else if (IS_SCHEMA(child, "list")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009246 xmlSchemaParseList(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009247 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009248 } else if (IS_SCHEMA(child, "union")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009249 xmlSchemaParseUnion(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009250 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009251 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009252 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009253 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009254 NULL, node, child, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009255 "(annotation?, (restriction | list | union))");
Daniel Veillard4255d502002-04-16 15:50:10 +00009256 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009257 /*
9258 * REDEFINE: SPEC src-redefine (5)
9259 * "Within the [children], each <simpleType> must have a
9260 * <restriction> among its [children] ... the ·actual value· of whose
9261 * base [attribute] must be the same as the ·actual value· of its own
9262 * name attribute plus target namespace;"
9263 */
9264 if (topLevel && ctxt->isRedefine && (! hasRestriction)) {
9265 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009266 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009267 "<simpleType> must have a <restriction> child", NULL);
9268 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009269
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009270 ctxt->ctxtType = oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009271 return (type);
9272}
9273
Daniel Veillard4255d502002-04-16 15:50:10 +00009274/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009275 * xmlSchemaParseModelGroupDefRef:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009276 * @ctxt: the parser context
9277 * @schema: the schema being built
9278 * @node: the node
Daniel Veillard4255d502002-04-16 15:50:10 +00009279 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009280 * Parses a reference to a model group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00009281 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009282 * We will return a particle component with a qname-component or
9283 * NULL in case of an error.
Daniel Veillard4255d502002-04-16 15:50:10 +00009284 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009285static xmlSchemaTreeItemPtr
9286xmlSchemaParseModelGroupDefRef(xmlSchemaParserCtxtPtr ctxt,
9287 xmlSchemaPtr schema,
9288 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00009289{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009290 xmlSchemaParticlePtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009291 xmlNodePtr child = NULL;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009292 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009293 const xmlChar *ref = NULL, *refNs = NULL;
9294 int min, max;
9295
9296 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009297 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009298
9299 attr = xmlSchemaGetPropNode(node, "ref");
9300 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009301 xmlSchemaPMissingAttrErr(ctxt,
9302 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009303 NULL, node, "ref", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009304 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009305 } else if (xmlSchemaPValAttrNodeQName(ctxt, schema, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009306 attr, &refNs, &ref) != 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009307 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009308 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009309 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009310 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009311 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009312 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009313 /*
9314 * Check for illegal attributes.
9315 */
9316 attr = node->properties;
9317 while (attr != NULL) {
9318 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009319 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009320 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9321 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
9322 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009323 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009324 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009325 }
9326 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009327 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009328 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009329 }
9330 attr = attr->next;
9331 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009332 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009333 item = xmlSchemaAddParticle(ctxt, schema, node, min, max);
9334 if (item == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009335 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009336 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009337 * Create a qname-reference and set as the term; it will be substituted
9338 * for the model group after the reference has been resolved.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009339 */
9340 item->children = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009341 xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_GROUP, ref, refNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009342 xmlSchemaPCheckParticleCorrect_2(ctxt, item, node, min, max);
9343 /*
9344 * And now for the children...
9345 */
9346 child = node->children;
9347 /* TODO: Is annotation even allowed for a model group reference? */
9348 if (IS_SCHEMA(child, "annotation")) {
9349 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009350 * TODO: What to do exactly with the annotation?
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009351 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009352 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009353 child = child->next;
9354 }
9355 if (child != NULL) {
9356 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009357 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009358 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009359 "(annotation?)");
9360 }
9361 /*
9362 * Corresponds to no component at all if minOccurs==maxOccurs==0.
9363 */
9364 if ((min == 0) && (max == 0))
9365 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009366
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009367 return ((xmlSchemaTreeItemPtr) item);
9368}
9369
9370/**
9371 * xmlSchemaParseModelGroupDefinition:
9372 * @ctxt: a schema validation context
9373 * @schema: the schema being built
9374 * @node: a subtree containing XML Schema informations
9375 *
9376 * Parses a XML schema model group definition.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009377 *
9378 * Note that the contraint src-redefine (6.2) can't be applied until
9379 * references have been resolved. So we will do this at the
9380 * component fixup level.
9381 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009382 * *WARNING* this interface is highly subject to change
9383 *
9384 * Returns -1 in case of error, 0 if the declaration is improper and
9385 * 1 in case of success.
9386 */
9387static xmlSchemaModelGroupDefPtr
9388xmlSchemaParseModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
9389 xmlSchemaPtr schema,
9390 xmlNodePtr node)
9391{
9392 xmlSchemaModelGroupDefPtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009393 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009394 xmlAttrPtr attr;
9395 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00009396
9397 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009398 return (NULL);
9399
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009400 attr = xmlSchemaGetPropNode(node, "name");
9401 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009402 xmlSchemaPMissingAttrErr(ctxt,
9403 XML_SCHEMAP_S4S_ATTR_MISSING,
9404 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009405 "name", NULL);
9406 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009407 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009408 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
9409 return (NULL);
9410 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009411 item = xmlSchemaAddModelGroupDefinition(ctxt, schema, name,
9412 ctxt->targetNamespace, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009413 if (item == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009414 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009415 /*
9416 * Check for illegal attributes.
9417 */
9418 attr = node->properties;
9419 while (attr != NULL) {
9420 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009421 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009422 (!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009423 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009424 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009425 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009426 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009427 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009428 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009429 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009430 attr = attr->next;
9431 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009432 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009433 /*
9434 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009435 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009436 child = node->children;
9437 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009438 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009439 child = child->next;
9440 }
9441 if (IS_SCHEMA(child, "all")) {
9442 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9443 XML_SCHEMA_TYPE_ALL, 0);
9444 child = child->next;
9445 } else if (IS_SCHEMA(child, "choice")) {
9446 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9447 XML_SCHEMA_TYPE_CHOICE, 0);
9448 child = child->next;
9449 } else if (IS_SCHEMA(child, "sequence")) {
9450 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9451 XML_SCHEMA_TYPE_SEQUENCE, 0);
9452 child = child->next;
9453 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009454
9455
9456
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009457 if (child != NULL) {
9458 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009459 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009460 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009461 "(annotation?, (all | choice | sequence)?)");
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009462 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009463 return (item);
Daniel Veillard4255d502002-04-16 15:50:10 +00009464}
9465
9466/**
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009467 * xmlSchemaCleanupDoc:
9468 * @ctxt: a schema validation context
9469 * @node: the root of the document.
9470 *
9471 * removes unwanted nodes in a schemas document tree
9472 */
9473static void
9474xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root)
9475{
9476 xmlNodePtr delete, cur;
9477
9478 if ((ctxt == NULL) || (root == NULL)) return;
9479
9480 /*
9481 * Remove all the blank text nodes
9482 */
9483 delete = NULL;
9484 cur = root;
9485 while (cur != NULL) {
9486 if (delete != NULL) {
9487 xmlUnlinkNode(delete);
9488 xmlFreeNode(delete);
9489 delete = NULL;
9490 }
9491 if (cur->type == XML_TEXT_NODE) {
9492 if (IS_BLANK_NODE(cur)) {
9493 if (xmlNodeGetSpacePreserve(cur) != 1) {
9494 delete = cur;
9495 }
9496 }
9497 } else if ((cur->type != XML_ELEMENT_NODE) &&
9498 (cur->type != XML_CDATA_SECTION_NODE)) {
9499 delete = cur;
9500 goto skip_children;
9501 }
9502
9503 /*
9504 * Skip to next node
9505 */
9506 if (cur->children != NULL) {
9507 if ((cur->children->type != XML_ENTITY_DECL) &&
9508 (cur->children->type != XML_ENTITY_REF_NODE) &&
9509 (cur->children->type != XML_ENTITY_NODE)) {
9510 cur = cur->children;
9511 continue;
9512 }
9513 }
9514 skip_children:
9515 if (cur->next != NULL) {
9516 cur = cur->next;
9517 continue;
9518 }
9519
9520 do {
9521 cur = cur->parent;
9522 if (cur == NULL)
9523 break;
9524 if (cur == root) {
9525 cur = NULL;
9526 break;
9527 }
9528 if (cur->next != NULL) {
9529 cur = cur->next;
9530 break;
9531 }
9532 } while (cur != NULL);
9533 }
9534 if (delete != NULL) {
9535 xmlUnlinkNode(delete);
9536 xmlFreeNode(delete);
9537 delete = NULL;
9538 }
9539}
9540
William M. Brack2f2a6632004-08-20 23:09:47 +00009541
William M. Brack2f2a6632004-08-20 23:09:47 +00009542static void
9543xmlSchemaClearSchemaDefaults(xmlSchemaPtr schema)
9544{
9545 if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
9546 schema->flags ^= XML_SCHEMAS_QUALIF_ELEM;
9547
9548 if (schema->flags & XML_SCHEMAS_QUALIF_ATTR)
9549 schema->flags ^= XML_SCHEMAS_QUALIF_ATTR;
9550
9551 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
9552 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_EXTENSION;
9553 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9554 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION;
9555 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9556 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_LIST;
9557 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9558 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_UNION;
9559
9560 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
9561 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION;
9562 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
9563 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION;
9564 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
9565 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION;
9566}
9567
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009568static int
9569xmlSchemaParseSchemaElement(xmlSchemaParserCtxtPtr ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009570 xmlSchemaPtr schema,
9571 xmlNodePtr node)
9572{
9573 xmlAttrPtr attr;
9574 const xmlChar *val;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009575 int res = 0, oldErrs = ctxt->nberrors;
William M. Brack2f2a6632004-08-20 23:09:47 +00009576
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009577 /*
9578 * Those flags should be moved to the parser context flags,
9579 * since they are not visible at the component level. I.e.
9580 * they are used if processing schema *documents* only.
9581 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009582 res = xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009583 HFAILURE;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009584
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009585 /*
9586 * Since the version is of type xs:token, we won't bother to
9587 * check it.
9588 */
9589 /* REMOVED:
9590 attr = xmlSchemaGetPropNode(node, "version");
9591 if (attr != NULL) {
9592 res = xmlSchemaPValAttrNode(ctxt, NULL, NULL, attr,
9593 xmlSchemaGetBuiltInType(XML_SCHEMAS_TOKEN), &val);
9594 HFAILURE;
9595 }
9596 */
9597 attr = xmlSchemaGetPropNode(node, "targetNamespace");
9598 if (attr != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009599 res = xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009600 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
9601 HFAILURE;
9602 if (res != 0) {
9603 ctxt->stop = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
9604 goto exit;
9605 }
9606 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009607 attr = xmlSchemaGetPropNode(node, "elementFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009608 if (attr != NULL) {
9609 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009610 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9611 XML_SCHEMAS_QUALIF_ELEM);
9612 HFAILURE;
9613 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009614 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009615 XML_SCHEMAP_ELEMFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009616 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009617 "(qualified | unqualified)", val, NULL, NULL, NULL);
9618 }
9619 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009620 attr = xmlSchemaGetPropNode(node, "attributeFormDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009621 if (attr != NULL) {
9622 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009623 res = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9624 XML_SCHEMAS_QUALIF_ATTR);
9625 HFAILURE;
9626 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009627 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009628 XML_SCHEMAP_ATTRFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009629 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009630 "(qualified | unqualified)", val, NULL, NULL, NULL);
9631 }
9632 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009633 attr = xmlSchemaGetPropNode(node, "finalDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009634 if (attr != NULL) {
9635 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009636 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009637 XML_SCHEMAS_FINAL_DEFAULT_EXTENSION,
9638 XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION,
9639 -1,
9640 XML_SCHEMAS_FINAL_DEFAULT_LIST,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009641 XML_SCHEMAS_FINAL_DEFAULT_UNION);
9642 HFAILURE;
9643 if (res != 0) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009644 xmlSchemaPSimpleTypeErr(ctxt,
9645 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009646 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009647 "(#all | List of (extension | restriction | list | union))",
9648 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009649 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009650 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009651 attr = xmlSchemaGetPropNode(node, "blockDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009652 if (attr != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009653 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
9654 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009655 XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION,
9656 XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009657 XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION, -1, -1);
9658 HFAILURE;
9659 if (res != 0) {
9660 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009661 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009662 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009663 "(#all | List of (extension | restriction | substitution))",
9664 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009665 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009666 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009667
9668exit:
9669 if (oldErrs != ctxt->nberrors)
9670 res = ctxt->err;
9671 return(res);
9672exit_failure:
9673 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +00009674}
9675
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009676/**
9677 * xmlSchemaParseSchemaTopLevel:
9678 * @ctxt: a schema validation context
9679 * @schema: the schemas
9680 * @nodes: the list of top level nodes
9681 *
9682 * Returns the internal XML Schema structure built from the resource or
9683 * NULL in case of error
9684 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009685static int
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009686xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt,
9687 xmlSchemaPtr schema, xmlNodePtr nodes)
9688{
9689 xmlNodePtr child;
9690 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009691 int res = 0, oldErrs, tmpOldErrs;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009692
9693 if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009694 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009695
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009696 oldErrs = ctxt->nberrors;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009697 child = nodes;
9698 while ((IS_SCHEMA(child, "include")) ||
9699 (IS_SCHEMA(child, "import")) ||
9700 (IS_SCHEMA(child, "redefine")) ||
9701 (IS_SCHEMA(child, "annotation"))) {
9702 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009703 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009704 if (schema->annot == NULL)
9705 schema->annot = annot;
9706 else
9707 xmlSchemaFreeAnnot(annot);
9708 } else if (IS_SCHEMA(child, "import")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009709 tmpOldErrs = ctxt->nberrors;
9710 res = xmlSchemaParseImport(ctxt, schema, child);
9711 HFAILURE;
9712 HSTOP(ctxt);
9713 if (tmpOldErrs != ctxt->nberrors)
9714 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009715 } else if (IS_SCHEMA(child, "include")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009716 tmpOldErrs = ctxt->nberrors;
9717 res = xmlSchemaParseInclude(ctxt, schema, child);
9718 HFAILURE;
9719 HSTOP(ctxt);
9720 if (tmpOldErrs != ctxt->nberrors)
9721 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009722 } else if (IS_SCHEMA(child, "redefine")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009723 tmpOldErrs = ctxt->nberrors;
9724 res = xmlSchemaParseRedefine(ctxt, schema, child);
9725 HFAILURE;
9726 HSTOP(ctxt);
9727 if (tmpOldErrs != ctxt->nberrors)
9728 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009729 }
9730 child = child->next;
9731 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009732 /*
9733 * URGENT TODO: Change the functions to return int results.
9734 * We need especially to catch internal errors.
9735 */
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009736 while (child != NULL) {
9737 if (IS_SCHEMA(child, "complexType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009738 xmlSchemaParseComplexType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009739 child = child->next;
9740 } else if (IS_SCHEMA(child, "simpleType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009741 xmlSchemaParseSimpleType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009742 child = child->next;
9743 } else if (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009744 xmlSchemaParseElement(ctxt, schema, child, NULL, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009745 child = child->next;
9746 } else if (IS_SCHEMA(child, "attribute")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009747 xmlSchemaParseGlobalAttribute(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009748 child = child->next;
9749 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009750 xmlSchemaParseAttributeGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009751 child = child->next;
9752 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009753 xmlSchemaParseModelGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009754 child = child->next;
9755 } else if (IS_SCHEMA(child, "notation")) {
9756 xmlSchemaParseNotation(ctxt, schema, child);
9757 child = child->next;
9758 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009759 xmlSchemaPContentErr(ctxt,
9760 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009761 NULL, child->parent, child,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009762 NULL, "((include | import | redefine | annotation)*, "
9763 "(((simpleType | complexType | group | attributeGroup) "
9764 "| element | attribute | notation), annotation*)*)");
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009765 child = child->next;
9766 }
9767 while (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009768 /*
9769 * TODO: We should add all annotations.
9770 */
9771 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009772 if (schema->annot == NULL)
9773 schema->annot = annot;
9774 else
9775 xmlSchemaFreeAnnot(annot);
9776 child = child->next;
9777 }
9778 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009779exit:
Daniel Veillard01fa6152004-06-29 17:04:39 +00009780 ctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009781 if (oldErrs != ctxt->nberrors)
9782 res = ctxt->err;
9783 return(res);
9784exit_failure:
9785 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009786}
9787
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009788static xmlSchemaSchemaRelationPtr
9789xmlSchemaSchemaRelationCreate(void)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009790{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009791 xmlSchemaSchemaRelationPtr ret;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009792
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009793 ret = (xmlSchemaSchemaRelationPtr)
9794 xmlMalloc(sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009795 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009796 xmlSchemaPErrMemory(NULL, "allocating schema relation", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009797 return(NULL);
9798 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009799 memset(ret, 0, sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009800 return(ret);
9801}
9802
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009803#if 0
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009804static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009805xmlSchemaSchemaRelationFree(xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009806{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009807 xmlFree(rel);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009808}
9809#endif
9810
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009811static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009812xmlSchemaRedefListFree(xmlSchemaRedefPtr redef)
9813{
9814 xmlSchemaRedefPtr prev;
9815
9816 while (redef != NULL) {
9817 prev = redef;
9818 redef = redef->next;
9819 xmlFree(prev);
9820 }
9821}
9822
9823static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009824xmlSchemaConstructionCtxtFree(xmlSchemaConstructionCtxtPtr con)
9825{
9826 /*
9827 * After the construction context has been freed, there will be
9828 * no schema graph available any more. Only the schema buckets
9829 * will stay alive, which are put into the "schemasImports" and
9830 * "includes" slots of the xmlSchema.
9831 */
9832 if (con->buckets != NULL)
9833 xmlSchemaItemListFree(con->buckets);
9834 if (con->pending != NULL)
9835 xmlSchemaItemListFree(con->pending);
9836 if (con->substGroups != NULL)
9837 xmlHashFree(con->substGroups,
9838 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009839 if (con->redefs != NULL)
9840 xmlSchemaRedefListFree(con->redefs);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009841 if (con->dict != NULL)
9842 xmlDictFree(con->dict);
9843 xmlFree(con);
9844}
9845
9846static xmlSchemaConstructionCtxtPtr
9847xmlSchemaConstructionCtxtCreate(xmlDictPtr dict)
9848{
9849 xmlSchemaConstructionCtxtPtr ret;
9850
9851 ret = (xmlSchemaConstructionCtxtPtr)
9852 xmlMalloc(sizeof(xmlSchemaConstructionCtxt));
9853 if (ret == NULL) {
9854 xmlSchemaPErrMemory(NULL,
9855 "allocating schema construction context", NULL);
9856 return (NULL);
9857 }
9858 memset(ret, 0, sizeof(xmlSchemaConstructionCtxt));
9859
9860 ret->buckets = xmlSchemaItemListCreate();
9861 if (ret->buckets == NULL) {
9862 xmlSchemaPErrMemory(NULL,
9863 "allocating list of schema buckets", NULL);
9864 xmlFree(ret);
9865 return (NULL);
9866 }
9867 ret->pending = xmlSchemaItemListCreate();
9868 if (ret->pending == NULL) {
9869 xmlSchemaPErrMemory(NULL,
9870 "allocating list of pending global components", NULL);
9871 xmlSchemaConstructionCtxtFree(ret);
9872 return (NULL);
9873 }
9874 ret->dict = dict;
9875 xmlDictReference(dict);
9876 return(ret);
9877}
9878
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009879static xmlSchemaParserCtxtPtr
9880xmlSchemaParserCtxtCreate(void)
9881{
9882 xmlSchemaParserCtxtPtr ret;
9883
9884 ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
9885 if (ret == NULL) {
9886 xmlSchemaPErrMemory(NULL, "allocating schema parser context",
9887 NULL);
9888 return (NULL);
9889 }
9890 memset(ret, 0, sizeof(xmlSchemaParserCtxt));
9891 ret->type = XML_SCHEMA_CTXT_PARSER;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009892 ret->attrProhibs = xmlSchemaItemListCreate();
9893 if (ret->attrProhibs == NULL) {
9894 xmlFree(ret);
9895 return(NULL);
9896 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009897 return(ret);
9898}
9899
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009900/**
9901 * xmlSchemaNewParserCtxtUseDict:
9902 * @URL: the location of the schema
9903 * @dict: the dictionary to be used
9904 *
9905 * Create an XML Schemas parse context for that file/resource expected
9906 * to contain an XML Schemas file.
9907 *
9908 * Returns the parser context or NULL in case of error
9909 */
9910static xmlSchemaParserCtxtPtr
9911xmlSchemaNewParserCtxtUseDict(const char *URL, xmlDictPtr dict)
9912{
9913 xmlSchemaParserCtxtPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009914
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009915 ret = xmlSchemaParserCtxtCreate();
9916 if (ret == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009917 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009918 ret->dict = dict;
9919 xmlDictReference(dict);
9920 if (URL != NULL)
9921 ret->URL = xmlDictLookup(dict, (const xmlChar *) URL, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009922 return (ret);
9923}
9924
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009925static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009926xmlSchemaCreatePCtxtOnVCtxt(xmlSchemaValidCtxtPtr vctxt)
9927{
9928 if (vctxt->pctxt == NULL) {
9929 if (vctxt->schema != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009930 vctxt->pctxt =
9931 xmlSchemaNewParserCtxtUseDict("*", vctxt->schema->dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009932 else
9933 vctxt->pctxt = xmlSchemaNewParserCtxt("*");
9934 if (vctxt->pctxt == NULL) {
9935 VERROR_INT("xmlSchemaCreatePCtxtOnVCtxt",
9936 "failed to create a temp. parser context");
9937 return (-1);
9938 }
9939 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009940 xmlSchemaSetParserErrors(vctxt->pctxt, vctxt->error,
9941 vctxt->warning, vctxt->userData);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009942 }
9943 return (0);
9944}
9945
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009946/**
9947 * xmlSchemaGetSchemaBucket:
9948 * @pctxt: the schema parser context
9949 * @schemaLocation: the URI of the schema document
9950 *
9951 * Returns a schema bucket if it was already parsed.
9952 *
9953 * Returns a schema bucket if it was already parsed from
9954 * @schemaLocation, NULL otherwise.
9955 */
9956static xmlSchemaBucketPtr
9957xmlSchemaGetSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
9958 const xmlChar *schemaLocation)
9959{
9960 xmlSchemaBucketPtr cur;
9961 xmlSchemaItemListPtr list;
9962
9963 list = pctxt->constructor->buckets;
9964 if (list->nbItems == 0)
9965 return(NULL);
9966 else {
9967 int i;
9968 for (i = 0; i < list->nbItems; i++) {
9969 cur = (xmlSchemaBucketPtr) list->items[i];
9970 /* Pointer comparison! */
9971 if (cur->schemaLocation == schemaLocation)
9972 return(cur);
9973 }
9974 }
9975 return(NULL);
9976}
9977
9978static xmlSchemaBucketPtr
9979xmlSchemaGetChameleonSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
9980 const xmlChar *schemaLocation,
9981 const xmlChar *targetNamespace)
9982{
9983 xmlSchemaBucketPtr cur;
9984 xmlSchemaItemListPtr list;
9985
9986 list = pctxt->constructor->buckets;
9987 if (list->nbItems == 0)
9988 return(NULL);
9989 else {
9990 int i;
9991 for (i = 0; i < list->nbItems; i++) {
9992 cur = (xmlSchemaBucketPtr) list->items[i];
9993 /* Pointer comparison! */
9994 if ((cur->origTargetNamespace == NULL) &&
9995 (cur->schemaLocation == schemaLocation) &&
9996 (cur->targetNamespace == targetNamespace))
9997 return(cur);
9998 }
9999 }
10000 return(NULL);
10001}
10002
10003
10004#define IS_BAD_SCHEMA_DOC(b) \
10005 (((b)->doc == NULL) && ((b)->schemaLocation != NULL))
10006
10007static xmlSchemaBucketPtr
10008xmlSchemaGetSchemaBucketByTNS(xmlSchemaParserCtxtPtr pctxt,
10009 const xmlChar *targetNamespace,
10010 int imported)
10011{
10012 xmlSchemaBucketPtr cur;
10013 xmlSchemaItemListPtr list;
10014
10015 list = pctxt->constructor->buckets;
10016 if (list->nbItems == 0)
10017 return(NULL);
10018 else {
10019 int i;
10020 for (i = 0; i < list->nbItems; i++) {
10021 cur = (xmlSchemaBucketPtr) list->items[i];
10022 if ((! IS_BAD_SCHEMA_DOC(cur)) &&
10023 (cur->origTargetNamespace == targetNamespace) &&
10024 ((imported && cur->imported) ||
10025 ((!imported) && (!cur->imported))))
10026 return(cur);
10027 }
10028 }
10029 return(NULL);
10030}
10031
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010032static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010033xmlSchemaParseNewDocWithContext(xmlSchemaParserCtxtPtr pctxt,
10034 xmlSchemaPtr schema,
10035 xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010036{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010037 int oldFlags;
10038 xmlDocPtr oldDoc;
10039 xmlNodePtr node;
10040 int ret, oldErrs;
10041 xmlSchemaBucketPtr oldbucket = pctxt->constructor->bucket;
10042
10043 /*
10044 * Save old values; reset the *main* schema.
10045 * URGENT TODO: This is not good; move the per-document information
10046 * to the parser.
10047 */
10048 oldFlags = schema->flags;
10049 oldDoc = schema->doc;
10050 if (schema->flags != 0)
10051 xmlSchemaClearSchemaDefaults(schema);
10052 schema->doc = bucket->doc;
10053 /* !! REMOVED: schema->targetNamespace = bucket->targetNamespace; */
10054 pctxt->schema = schema;
10055 /*
10056 * Keep the current target namespace on the parser *not* on the
10057 * main schema.
10058 */
10059 pctxt->targetNamespace = bucket->targetNamespace;
10060 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010061
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010062 if ((bucket->targetNamespace != NULL) &&
10063 xmlStrEqual(bucket->targetNamespace, xmlSchemaNs)) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010064 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010065 * We are parsing the schema for schemas!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010066 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010067 pctxt->isS4S = 1;
10068 }
10069 /* Mark it as parsed, even if parsing fails. */
10070 bucket->parsed++;
10071 /* Compile the schema doc. */
10072 node = xmlDocGetRootElement(bucket->doc);
10073 ret = xmlSchemaParseSchemaElement(pctxt, schema, node);
10074 if (ret != 0)
10075 goto exit;
10076 /* An empty schema; just get out. */
10077 if (node->children == NULL)
10078 goto exit;
10079 oldErrs = pctxt->nberrors;
10080 ret = xmlSchemaParseSchemaTopLevel(pctxt, schema, node->children);
10081 if (ret != 0)
10082 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010083 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010084 * TODO: Not nice, but I'm not 100% sure we will get always an error
10085 * as a result of the obove functions; so better rely on pctxt->err
10086 * as well.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010087 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010088 if ((ret == 0) && (oldErrs != pctxt->nberrors)) {
10089 ret = pctxt->err;
10090 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010091 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010092
10093exit:
10094 WXS_CONSTRUCTOR(pctxt)->bucket = oldbucket;
10095 /* Restore schema values. */
10096 schema->doc = oldDoc;
10097 schema->flags = oldFlags;
10098 return(ret);
10099}
10100
10101static int
10102xmlSchemaParseNewDoc(xmlSchemaParserCtxtPtr pctxt,
10103 xmlSchemaPtr schema,
10104 xmlSchemaBucketPtr bucket)
10105{
10106 xmlSchemaParserCtxtPtr newpctxt;
10107 int res = 0;
10108
10109 if (bucket == NULL)
10110 return(0);
10111 if (bucket->parsed) {
10112 PERROR_INT("xmlSchemaParseNewDoc",
10113 "reparsing a schema doc");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010114 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010115 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010116 if (bucket->doc == NULL) {
10117 PERROR_INT("xmlSchemaParseNewDoc",
10118 "parsing a schema doc, but there's no doc");
10119 return(-1);
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +000010120 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010121 if (pctxt->constructor == NULL) {
10122 PERROR_INT("xmlSchemaParseNewDoc",
10123 "no constructor");
10124 return(-1);
10125 }
10126 /* Create and init the temporary parser context. */
10127 newpctxt = xmlSchemaNewParserCtxtUseDict(
10128 (const char *) bucket->schemaLocation, pctxt->dict);
10129 if (newpctxt == NULL)
10130 return(-1);
10131 newpctxt->constructor = pctxt->constructor;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010132 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010133 * TODO: Can we avoid that the parser knows about the main schema?
10134 * It would be better if he knows about the current schema bucket
10135 * only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010136 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010137 newpctxt->schema = schema;
10138 xmlSchemaSetParserErrors(newpctxt, pctxt->error, pctxt->warning,
10139 pctxt->userData);
10140 newpctxt->counter = pctxt->counter;
10141
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010142
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010143 res = xmlSchemaParseNewDocWithContext(newpctxt, schema, bucket);
10144
10145 /* Channel back errors and cleanup the temporary parser context. */
10146 if (res != 0)
10147 pctxt->err = res;
10148 pctxt->nberrors += newpctxt->nberrors;
10149 pctxt->counter = newpctxt->counter;
10150 newpctxt->constructor = NULL;
10151 /* Free the parser context. */
10152 xmlSchemaFreeParserCtxt(newpctxt);
10153 return(res);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010154}
William M. Brack2f2a6632004-08-20 23:09:47 +000010155
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010156static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010157xmlSchemaSchemaRelationAddChild(xmlSchemaBucketPtr bucket,
10158 xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010159{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010160 xmlSchemaSchemaRelationPtr cur = bucket->relations;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010161
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010162 if (cur == NULL) {
10163 bucket->relations = rel;
10164 return;
10165 }
10166 while (cur->next != NULL)
10167 cur = cur->next;
10168 cur->next = rel;
10169}
10170
10171
10172static const xmlChar *
10173xmlSchemaBuildAbsoluteURI(xmlDictPtr dict, const xmlChar* location,
10174 xmlNodePtr ctxtNode)
10175{
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010176 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010177 * Build an absolue location URI.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010178 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010179 if (location != NULL) {
10180 if (ctxtNode == NULL)
10181 return(location);
10182 else {
10183 xmlChar *base, *URI;
10184 const xmlChar *ret = NULL;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010185
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010186 base = xmlNodeGetBase(ctxtNode->doc, ctxtNode);
10187 if (base == NULL) {
10188 URI = xmlBuildURI(location, ctxtNode->doc->URL);
10189 } else {
10190 URI = xmlBuildURI(location, base);
10191 xmlFree(base);
10192 }
10193 if (URI != NULL) {
10194 ret = xmlDictLookup(dict, URI, -1);
10195 xmlFree(URI);
10196 return(ret);
10197 }
10198 }
10199 }
10200 return(NULL);
10201}
10202
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010203
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010204
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010205/**
10206 * xmlSchemaAddSchemaDoc:
10207 * @pctxt: a schema validation context
10208 * @schema: the schema being built
10209 * @node: a subtree containing XML Schema informations
10210 *
10211 * Parse an included (and to-be-redefined) XML schema document.
10212 *
10213 * Returns 0 on success, a positive error code on errors and
10214 * -1 in case of an internal or API error.
10215 */
10216
10217static int
10218xmlSchemaAddSchemaDoc(xmlSchemaParserCtxtPtr pctxt,
10219 int type, /* import or include or redefine */
10220 const xmlChar *schemaLocation,
10221 xmlDocPtr schemaDoc,
10222 const char *schemaBuffer,
10223 int schemaBufferLen,
10224 xmlNodePtr invokingNode,
10225 const xmlChar *sourceTargetNamespace,
10226 const xmlChar *importNamespace,
10227 xmlSchemaBucketPtr *bucket)
10228{
10229 const xmlChar *targetNamespace = NULL;
10230 xmlSchemaSchemaRelationPtr relation = NULL;
10231 xmlDocPtr doc = NULL;
10232 int res = 0, err = 0, located = 0, preserveDoc = 0;
10233 xmlSchemaBucketPtr bkt = NULL;
10234
10235 if (bucket != NULL)
10236 *bucket = NULL;
10237
10238 switch (type) {
10239 case XML_SCHEMA_SCHEMA_IMPORT:
10240 case XML_SCHEMA_SCHEMA_MAIN:
10241 err = XML_SCHEMAP_SRC_IMPORT;
10242 break;
10243 case XML_SCHEMA_SCHEMA_INCLUDE:
10244 err = XML_SCHEMAP_SRC_INCLUDE;
10245 break;
10246 case XML_SCHEMA_SCHEMA_REDEFINE:
10247 err = XML_SCHEMAP_SRC_REDEFINE;
10248 break;
10249 }
10250
10251
10252 /* Special handling for the main schema:
10253 * skip the location and relation logic and just parse the doc.
10254 * We need just a bucket to be returned in this case.
10255 */
10256 if ((type == XML_SCHEMA_SCHEMA_MAIN) || (! WXS_HAS_BUCKETS(pctxt)))
10257 goto doc_load;
10258
10259 /* Note that we expect the location to be an absulute URI. */
10260 if (schemaLocation != NULL) {
10261 bkt = xmlSchemaGetSchemaBucket(pctxt, schemaLocation);
10262 if ((bkt != NULL) &&
10263 (pctxt->constructor->bucket == bkt)) {
10264 /* Report self-imports/inclusions/redefinitions. */
10265
10266 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10267 invokingNode, NULL,
10268 "The schema must not import/include/redefine itself",
10269 NULL, NULL);
10270 goto exit;
10271 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000010272 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010273 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010274 * Create a relation for the graph of schemas.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010275 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010276 relation = xmlSchemaSchemaRelationCreate();
10277 if (relation == NULL)
10278 return(-1);
10279 xmlSchemaSchemaRelationAddChild(pctxt->constructor->bucket,
10280 relation);
10281 relation->type = type;
10282
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010283 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010284 * Save the namespace import information.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010285 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010286 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010287 relation->importNamespace = importNamespace;
10288 if (schemaLocation == NULL) {
10289 /*
10290 * No location; this is just an import of the namespace.
10291 * Note that we don't assign a bucket to the relation
10292 * in this case.
10293 */
10294 goto exit;
10295 }
10296 targetNamespace = importNamespace;
10297 }
10298
10299 /* Did we already fetch the doc? */
10300 if (bkt != NULL) {
10301 /* TODO: The following nasty cases will produce an error. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010302 if ((WXS_IS_BUCKET_IMPMAIN(type)) && (! bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010303 /* We included/redefined and then try to import a schema. */
10304 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10305 invokingNode, NULL,
10306 "The schema document '%s' cannot be imported, since "
10307 "it was already included or redefined",
10308 schemaLocation, NULL);
10309 goto exit;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010310 } else if ((! WXS_IS_BUCKET_IMPMAIN(type)) && (bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010311 /* We imported and then try to include/redefine a schema. */
10312 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10313 invokingNode, NULL,
10314 "The schema document '%s' cannot be included or "
10315 "redefined, since it was already imported",
10316 schemaLocation, NULL);
10317 goto exit;
10318 }
10319 }
10320
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010321 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010322 /*
10323 * Given that the schemaLocation [attribute] is only a hint, it is open
10324 * to applications to ignore all but the first <import> for a given
10325 * namespace, regardless of the ·actual value· of schemaLocation, but
10326 * such a strategy risks missing useful information when new
10327 * schemaLocations are offered.
10328 *
10329 * We will use the first <import> that comes with a location.
10330 * Further <import>s *with* a location, will result in an error.
10331 * TODO: Better would be to just report a warning here, but
10332 * we'll try it this way until someone complains.
10333 *
10334 * Schema Document Location Strategy:
10335 * 3 Based on the namespace name, identify an existing schema document,
10336 * either as a resource which is an XML document or a <schema> element
10337 * information item, in some local schema repository;
10338 * 5 Attempt to resolve the namespace name to locate such a resource.
10339 *
10340 * NOTE: (3) and (5) are not supported.
10341 */
10342 if (bkt != NULL) {
10343 relation->bucket = bkt;
10344 goto exit;
10345 }
10346 bkt = xmlSchemaGetSchemaBucketByTNS(pctxt,
10347 importNamespace, 1);
10348
10349 if (bkt != NULL) {
10350 relation->bucket = bkt;
10351 if (bkt->schemaLocation == NULL) {
10352 /* First given location of the schema; load the doc. */
10353 bkt->schemaLocation = schemaLocation;
10354 } else {
10355 if (!xmlStrEqual(schemaLocation,
10356 bkt->schemaLocation)) {
10357 /*
10358 * Additional location given; just skip it.
10359 * URGENT TODO: We should report a warning here.
10360 * res = XML_SCHEMAP_SRC_IMPORT;
10361 */
10362 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10363 XML_SCHEMAP_WARN_SKIP_SCHEMA,
10364 invokingNode, NULL,
10365 "Skipping import of schema located at '%s' for the "
10366 "namespace '%s', since this namespace was already "
10367 "imported with the schema located at '%s'",
10368 schemaLocation, importNamespace, bkt->schemaLocation);
10369 }
10370 goto exit;
10371 }
10372 }
10373 /*
10374 * No bucket + first location: load the doc and create a
10375 * bucket.
10376 */
10377 } else {
10378 /* <include> and <redefine> */
10379 if (bkt != NULL) {
10380
10381 if ((bkt->origTargetNamespace == NULL) &&
10382 (bkt->targetNamespace != sourceTargetNamespace)) {
10383 xmlSchemaBucketPtr chamel;
10384
10385 /*
10386 * Chameleon include/redefine: skip loading only if it was
10387 * aleady build for the targetNamespace of the including
10388 * schema.
10389 */
10390 /*
10391 * URGENT TODO: If the schema is a chameleon-include then copy
10392 * the components into the including schema and modify the
10393 * targetNamespace of those components, do nothing otherwise.
10394 * NOTE: This is currently worked-around by compiling the
10395 * chameleon for every destinct including targetNamespace; thus
10396 * not performant at the moment.
10397 * TODO: Check when the namespace in wildcards for chameleons
10398 * needs to be converted: before we built wildcard intersections
10399 * or after.
10400 * Answer: after!
10401 */
10402 chamel = xmlSchemaGetChameleonSchemaBucket(pctxt,
10403 schemaLocation, sourceTargetNamespace);
10404 if (chamel != NULL) {
10405 /* A fitting chameleon was already parsed; NOP. */
10406 relation->bucket = chamel;
10407 goto exit;
10408 }
10409 /*
10410 * We need to parse the chameleon again for a different
10411 * targetNamespace.
10412 * CHAMELEON TODO: Optimize this by only parsing the
10413 * chameleon once, and then copying the components to
10414 * the new targetNamespace.
10415 */
10416 bkt = NULL;
10417 } else {
10418 relation->bucket = bkt;
10419 goto exit;
10420 }
10421 }
10422 }
10423 if ((bkt != NULL) && (bkt->doc != NULL)) {
10424 PERROR_INT("xmlSchemaAddSchemaDoc",
10425 "trying to load a schema doc, but a doc is already "
10426 "assigned to the schema bucket");
10427 goto exit_failure;
10428 }
10429
10430doc_load:
10431 /*
10432 * Load the document.
10433 */
10434 if (schemaDoc != NULL) {
10435 doc = schemaDoc;
10436 /* Don' free this one, since it was provided by the caller. */
10437 preserveDoc = 1;
10438 /* TODO: Does the context or the doc hold the location? */
10439 if (schemaDoc->URL != NULL)
10440 schemaLocation = xmlDictLookup(pctxt->dict,
10441 schemaDoc->URL, -1);
10442
10443 } else if ((schemaLocation != NULL) || (schemaBuffer != NULL)) {
10444 xmlParserCtxtPtr parserCtxt;
10445
10446 parserCtxt = xmlNewParserCtxt();
10447 if (parserCtxt == NULL) {
10448 xmlSchemaPErrMemory(NULL, "xmlSchemaGetDoc, "
10449 "allocating a parser context", NULL);
10450 goto exit_failure;
10451 }
10452 if ((pctxt->dict != NULL) && (parserCtxt->dict != NULL)) {
10453 /*
10454 * TODO: Do we have to burden the schema parser dict with all
10455 * the content of the schema doc?
10456 */
10457 xmlDictFree(parserCtxt->dict);
10458 parserCtxt->dict = pctxt->dict;
10459 xmlDictReference(parserCtxt->dict);
10460 }
10461 if (schemaLocation != NULL) {
10462 /* Parse from file. */
10463 doc = xmlCtxtReadFile(parserCtxt, (const char *) schemaLocation,
10464 NULL, SCHEMAS_PARSE_OPTIONS);
10465 } else if (schemaBuffer != NULL) {
10466 /* Parse from memory buffer. */
10467 doc = xmlCtxtReadMemory(parserCtxt, schemaBuffer, schemaBufferLen,
10468 NULL, NULL, SCHEMAS_PARSE_OPTIONS);
10469 schemaLocation = xmlStrdup(BAD_CAST "in_memory_buffer");
10470 if (doc != NULL)
10471 doc->URL = schemaLocation;
10472 }
10473 /*
10474 * For <import>:
10475 * 2.1 The referent is (a fragment of) a resource which is an
10476 * XML document (see clause 1.1), which in turn corresponds to
10477 * a <schema> element information item in a well-formed information
10478 * set, which in turn corresponds to a valid schema.
10479 * TODO: (2.1) fragments of XML documents are not supported.
10480 *
10481 * 2.2 The referent is a <schema> element information item in
10482 * a well-formed information set, which in turn corresponds
10483 * to a valid schema.
10484 * TODO: (2.2) is not supported.
10485 */
10486 if (doc == NULL) {
10487 xmlErrorPtr lerr;
10488 lerr = xmlGetLastError();
10489 /*
10490 * Check if this a parser error, or if the document could
10491 * just not be located.
10492 * TODO: Try to find specific error codes to react only on
10493 * localisation failures.
10494 */
10495 if ((lerr == NULL) || (lerr->domain != XML_FROM_IO)) {
10496 /*
10497 * We assume a parser error here.
10498 */
10499 located = 1;
10500 /* TODO: Error code ?? */
10501 res = XML_SCHEMAP_SRC_IMPORT_2_1;
10502 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10503 invokingNode, NULL,
10504 "Failed to parse the XML resource '%s'",
10505 schemaLocation, NULL);
10506 }
10507 }
10508 xmlFreeParserCtxt(parserCtxt);
10509 if ((doc == NULL) && located)
10510 goto exit_error;
10511 } else {
10512 xmlSchemaPErr(pctxt, NULL,
10513 XML_SCHEMAP_NOTHING_TO_PARSE,
10514 "No information for parsing was provided with the "
10515 "given schema parser context.\n",
10516 NULL, NULL);
10517 goto exit_failure;
10518 }
10519 /*
10520 * Preprocess the document.
10521 */
10522 if (doc != NULL) {
10523 xmlNodePtr docElem = NULL;
10524
10525 located = 1;
10526 docElem = xmlDocGetRootElement(doc);
10527 if (docElem == NULL) {
10528 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOROOT,
10529 invokingNode, NULL,
10530 "The document '%s' has no document element",
10531 schemaLocation, NULL);
10532 xmlFreeDoc(doc);
10533 doc = NULL;
10534 goto exit_error;
10535 }
10536 /*
10537 * Remove all the blank text nodes.
10538 */
10539 xmlSchemaCleanupDoc(pctxt, docElem);
10540 /*
10541 * Check the schema's top level element.
10542 */
10543 if (!IS_SCHEMA(docElem, "schema")) {
10544 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOT_SCHEMA,
10545 invokingNode, NULL,
10546 "The XML document '%s' is not a schema document",
10547 schemaLocation, NULL);
10548 xmlFreeDoc(doc);
10549 doc = NULL;
10550 goto exit_error;
10551 }
10552 /*
10553 * Note that we don't apply a type check for the
10554 * targetNamespace value here.
10555 */
10556 targetNamespace = xmlSchemaGetProp(pctxt, docElem,
10557 "targetNamespace");
10558 }
10559
10560/* after_doc_loading: */
10561 if ((bkt == NULL) && located) {
10562 /* Only create a bucket if the schema was located. */
10563 bkt = xmlSchemaBucketCreate(pctxt, type,
10564 targetNamespace);
10565 if (bkt == NULL)
10566 goto exit_failure;
10567 }
10568 if (bkt != NULL) {
10569 bkt->schemaLocation = schemaLocation;
10570 bkt->located = located;
10571 if (doc != NULL) {
10572 bkt->doc = doc;
10573 bkt->targetNamespace = targetNamespace;
10574 bkt->origTargetNamespace = targetNamespace;
10575 if (preserveDoc)
10576 bkt->preserveDoc = 1;
10577 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010578 if (WXS_IS_BUCKET_IMPMAIN(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010579 bkt->imported++;
10580 /*
10581 * Add it to the graph of schemas.
10582 */
10583 if (relation != NULL)
10584 relation->bucket = bkt;
10585 }
10586
10587exit:
10588 /*
10589 * Return the bucket explicitely; this is needed for the
10590 * main schema.
10591 */
10592 if (bucket != NULL)
10593 *bucket = bkt;
10594 return (0);
10595
10596exit_error:
10597 if ((doc != NULL) && (! preserveDoc)) {
10598 xmlFreeDoc(doc);
10599 if (bkt != NULL)
10600 bkt->doc = NULL;
10601 }
10602 return(pctxt->err);
10603
10604exit_failure:
10605 if ((doc != NULL) && (! preserveDoc)) {
10606 xmlFreeDoc(doc);
10607 if (bkt != NULL)
10608 bkt->doc = NULL;
10609 }
10610 return (-1);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010611}
10612
William M. Brack2f2a6632004-08-20 23:09:47 +000010613/**
10614 * xmlSchemaParseImport:
10615 * @ctxt: a schema validation context
10616 * @schema: the schema being built
10617 * @node: a subtree containing XML Schema informations
10618 *
10619 * parse a XML schema Import definition
10620 * *WARNING* this interface is highly subject to change
10621 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010622 * Returns 0 in case of success, a positive error code if
10623 * not valid and -1 in case of an internal error.
William M. Brack2f2a6632004-08-20 23:09:47 +000010624 */
10625static int
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010626xmlSchemaParseImport(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
William M. Brack2f2a6632004-08-20 23:09:47 +000010627 xmlNodePtr node)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010628{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010629 xmlNodePtr child;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010630 const xmlChar *namespaceName = NULL, *schemaLocation = NULL;
10631 const xmlChar *thisTargetNamespace;
William M. Brack2f2a6632004-08-20 23:09:47 +000010632 xmlAttrPtr attr;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010633 int ret = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010634 xmlSchemaBucketPtr bucket = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000010635
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010636 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
William M. Brack2f2a6632004-08-20 23:09:47 +000010637 return (-1);
10638
10639 /*
10640 * Check for illegal attributes.
10641 */
10642 attr = node->properties;
10643 while (attr != NULL) {
10644 if (attr->ns == NULL) {
10645 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10646 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
10647 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010648 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010649 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010650 }
10651 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010652 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010653 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010654 }
10655 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010656 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010657 /*
10658 * Extract and validate attributes.
10659 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010660 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010661 "namespace", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010662 &namespaceName) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010663 xmlSchemaPSimpleTypeErr(pctxt,
10664 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010665 NULL, node,
10666 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010667 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010668 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010669 }
10670
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010671 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010672 "schemaLocation", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
William M. Brack2f2a6632004-08-20 23:09:47 +000010673 &schemaLocation) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010674 xmlSchemaPSimpleTypeErr(pctxt,
10675 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010676 NULL, node,
10677 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010678 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010679 return (pctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010680 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010681 /*
10682 * And now for the children...
10683 */
10684 child = node->children;
10685 if (IS_SCHEMA(child, "annotation")) {
10686 /*
10687 * the annotation here is simply discarded ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010688 * TODO: really?
William M. Brack2f2a6632004-08-20 23:09:47 +000010689 */
10690 child = child->next;
10691 }
10692 if (child != NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010693 xmlSchemaPContentErr(pctxt,
10694 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010695 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000010696 "(annotation?)");
10697 }
10698 /*
10699 * Apply additional constraints.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010700 *
10701 * Note that it is important to use the original @targetNamespace
10702 * (or none at all), to rule out imports of schemas _with_ a
10703 * @targetNamespace if the importing schema is a chameleon schema
10704 * (with no @targetNamespace).
William M. Brack2f2a6632004-08-20 23:09:47 +000010705 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010706 thisTargetNamespace = WXS_BUCKET(pctxt)->origTargetNamespace;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010707 if (namespaceName != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000010708 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010709 * 1.1 If the namespace [attribute] is present, then its ·actual value·
10710 * must not match the ·actual value· of the enclosing <schema>'s
William M. Brack2f2a6632004-08-20 23:09:47 +000010711 * targetNamespace [attribute].
10712 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010713 if (xmlStrEqual(thisTargetNamespace, namespaceName)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010714 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010715 XML_SCHEMAP_SRC_IMPORT_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010716 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010717 "The value of the attribute 'namespace' must not match "
10718 "the target namespace '%s' of the importing schema",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010719 thisTargetNamespace);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010720 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010721 }
10722 } else {
10723 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010724 * 1.2 If the namespace [attribute] is not present, then the enclosing
William M. Brack2f2a6632004-08-20 23:09:47 +000010725 * <schema> must have a targetNamespace [attribute].
10726 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010727 if (thisTargetNamespace == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010728 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010729 XML_SCHEMAP_SRC_IMPORT_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010730 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010731 "The attribute 'namespace' must be existent if "
10732 "the importing schema has no target namespace",
10733 NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010734 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010735 }
10736 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010737 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010738 * Locate and acquire the schema document.
William M. Brack2f2a6632004-08-20 23:09:47 +000010739 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010740 if (schemaLocation != NULL)
10741 schemaLocation = xmlSchemaBuildAbsoluteURI(pctxt->dict,
10742 schemaLocation, node);
10743 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010744 schemaLocation, NULL, NULL, 0, node, thisTargetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010745 namespaceName, &bucket);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010746
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010747 if (ret != 0)
10748 return(ret);
10749
10750 /*
10751 * For <import>: "It is *not* an error for the application
10752 * schema reference strategy to fail."
10753 * So just don't parse if no schema document was found.
10754 * Note that we will get no bucket if the schema could not be
10755 * located or if there was no schemaLocation.
10756 */
10757 if ((bucket == NULL) && (schemaLocation != NULL)) {
10758 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10759 XML_SCHEMAP_WARN_UNLOCATED_SCHEMA,
10760 node, NULL,
10761 "Failed to locate a schema at location '%s'. "
10762 "Skipping the import", schemaLocation, NULL, NULL);
10763 }
10764
10765 if ((bucket != NULL) && CAN_PARSE_SCHEMA(bucket)) {
10766 ret = xmlSchemaParseNewDoc(pctxt, schema, bucket);
10767 }
10768
10769 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000010770}
10771
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010772static int
10773xmlSchemaParseIncludeOrRedefineAttrs(xmlSchemaParserCtxtPtr pctxt,
10774 xmlSchemaPtr schema,
10775 xmlNodePtr node,
10776 xmlChar **schemaLocation,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010777 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010778{
10779 xmlAttrPtr attr;
10780
10781 if ((pctxt == NULL) || (schema == NULL) || (node == NULL) ||
10782 (schemaLocation == NULL))
10783 return (-1);
10784
10785 *schemaLocation = NULL;
10786 /*
10787 * Check for illegal attributes.
10788 * Applies for both <include> and <redefine>.
10789 */
10790 attr = node->properties;
10791 while (attr != NULL) {
10792 if (attr->ns == NULL) {
10793 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10794 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
10795 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010796 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010797 }
10798 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
10799 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010800 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010801 }
10802 attr = attr->next;
10803 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010804 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010805 /*
10806 * Preliminary step, extract the URI-Reference and make an URI
10807 * from the base.
10808 */
10809 /*
10810 * Attribute "schemaLocation" is mandatory.
10811 */
10812 attr = xmlSchemaGetPropNode(node, "schemaLocation");
10813 if (attr != NULL) {
10814 xmlChar *base = NULL;
10815 xmlChar *uri = NULL;
10816
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010817 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010818 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
10819 (const xmlChar **) schemaLocation) != 0)
10820 goto exit_error;
10821 base = xmlNodeGetBase(node->doc, node);
10822 if (base == NULL) {
10823 uri = xmlBuildURI(*schemaLocation, node->doc->URL);
10824 } else {
10825 uri = xmlBuildURI(*schemaLocation, base);
10826 xmlFree(base);
10827 }
10828 if (uri == NULL) {
10829 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
10830 "could not build an URI from the schemaLocation")
10831 goto exit_failure;
10832 }
10833 (*schemaLocation) = (xmlChar *) xmlDictLookup(pctxt->dict, uri, -1);
10834 xmlFree(uri);
10835 } else {
10836 xmlSchemaPMissingAttrErr(pctxt,
10837 XML_SCHEMAP_S4S_ATTR_MISSING,
10838 NULL, node, "schemaLocation", NULL);
10839 goto exit_error;
10840 }
10841 /*
10842 * Report self-inclusion and self-redefinition.
10843 */
10844 if (xmlStrEqual(*schemaLocation, pctxt->URL)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010845 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010846 xmlSchemaPCustomErr(pctxt,
10847 XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010848 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010849 "The schema document '%s' cannot redefine itself.",
10850 *schemaLocation);
10851 } else {
10852 xmlSchemaPCustomErr(pctxt,
10853 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010854 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010855 "The schema document '%s' cannot include itself.",
10856 *schemaLocation);
10857 }
10858 goto exit_error;
10859 }
10860
10861 return(0);
10862exit_error:
10863 return(pctxt->err);
10864exit_failure:
10865 return(-1);
10866}
10867
10868static int
10869xmlSchemaParseIncludeOrRedefine(xmlSchemaParserCtxtPtr pctxt,
10870 xmlSchemaPtr schema,
10871 xmlNodePtr node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010872 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010873{
10874 xmlNodePtr child = NULL;
10875 const xmlChar *schemaLocation = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010876 int res = 0; /* hasRedefinitions = 0 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010877 int isChameleon = 0, wasChameleon = 0;
10878 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010879
10880 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
10881 return (-1);
10882
10883 /*
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010884 * Parse attributes. Note that the returned schemaLocation will
10885 * be already converted to an absolute URI.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010886 */
10887 res = xmlSchemaParseIncludeOrRedefineAttrs(pctxt, schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010888 node, (xmlChar **) (&schemaLocation), type);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010889 if (res != 0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010890 return(res);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010891 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010892 * Load and add the schema document.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010893 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010894 res = xmlSchemaAddSchemaDoc(pctxt, type, schemaLocation, NULL,
10895 NULL, 0, node, pctxt->targetNamespace, NULL, &bucket);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010896 if (res != 0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010897 return(res);
10898 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010899 * If we get no schema bucket back, then this means that the schema
10900 * document could not be located or was broken XML or was not
10901 * a schema document.
10902 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010903 if ((bucket == NULL) || (bucket->doc == NULL)) {
10904 if (type == XML_SCHEMA_SCHEMA_INCLUDE) {
10905 /*
10906 * WARNING for <include>:
10907 * We will raise an error if the schema cannot be located
10908 * for inclusions, since the that was the feedback from the
10909 * schema people. I.e. the following spec piece will *not* be
10910 * satisfied:
10911 * SPEC src-include: "It is not an error for the ·actual value· of the
10912 * schemaLocation [attribute] to fail to resolve it all, in which
10913 * case no corresponding inclusion is performed.
10914 * So do we need a warning report here?"
10915 */
10916 res = XML_SCHEMAP_SRC_INCLUDE;
10917 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10918 node, NULL,
10919 "Failed to load the document '%s' for inclusion",
10920 schemaLocation, NULL);
10921 } else {
10922 /*
10923 * NOTE: This was changed to raise an error even if no redefinitions
10924 * are specified.
10925 *
10926 * SPEC src-redefine (1)
10927 * "If there are any element information items among the [children]
10928 * other than <annotation> then the ·actual value· of the
10929 * schemaLocation [attribute] must successfully resolve."
10930 * TODO: Ask the WG if a the location has always to resolve
10931 * here as well!
10932 */
10933 res = XML_SCHEMAP_SRC_REDEFINE;
10934 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10935 node, NULL,
10936 "Failed to load the document '%s' for redefinition",
10937 schemaLocation, NULL);
10938 }
10939 } else {
10940 /*
10941 * Check targetNamespace sanity before parsing the new schema.
10942 * TODO: Note that we won't check further content if the
10943 * targetNamespace was bad.
10944 */
10945 if (bucket->origTargetNamespace != NULL) {
10946 /*
10947 * SPEC src-include (2.1)
10948 * "SII has a targetNamespace [attribute], and its ·actual
10949 * value· is identical to the ·actual value· of the targetNamespace
10950 * [attribute] of SIIÂ’ (which must have such an [attribute])."
10951 */
10952 if (pctxt->targetNamespace == NULL) {
10953 xmlSchemaCustomErr(ACTXT_CAST pctxt,
10954 XML_SCHEMAP_SRC_INCLUDE,
10955 node, NULL,
10956 "The target namespace of the included/redefined schema "
10957 "'%s' has to be absent, since the including/redefining "
10958 "schema has no target namespace",
10959 schemaLocation, NULL);
10960 goto exit_error;
10961 } else if (!xmlStrEqual(bucket->origTargetNamespace,
10962 pctxt->targetNamespace)) {
10963 /* TODO: Change error function. */
10964 xmlSchemaPCustomErrExt(pctxt,
10965 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010966 NULL, node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010967 "The target namespace '%s' of the included/redefined "
10968 "schema '%s' differs from '%s' of the "
10969 "including/redefining schema",
10970 bucket->origTargetNamespace, schemaLocation,
10971 pctxt->targetNamespace);
10972 goto exit_error;
10973 }
10974 } else if (pctxt->targetNamespace != NULL) {
10975 /*
10976 * Chameleons: the original target namespace will
10977 * differ from the resulting namespace.
10978 */
10979 isChameleon = 1;
10980 if (bucket->parsed &&
10981 (bucket->targetNamespace != pctxt->targetNamespace)) {
10982 /*
10983 * This is a sanity check, I dunno yet if this can happen.
10984 */
10985 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
10986 "trying to use an already parsed schema for a "
10987 "different targetNamespace");
10988 return(-1);
10989 }
10990 bucket->targetNamespace = pctxt->targetNamespace;
10991 }
10992 }
10993 /*
10994 * Parse the schema.
10995 */
10996 if (bucket && (!bucket->parsed) && (bucket->doc != NULL)) {
10997 if (isChameleon) {
10998 /* TODO: Get rid of this flag on the schema itself. */
10999 if ((schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) == 0) {
11000 schema->flags |= XML_SCHEMAS_INCLUDING_CONVERT_NS;
11001 } else
11002 wasChameleon = 1;
11003 }
11004 xmlSchemaParseNewDoc(pctxt, schema, bucket);
11005 /* Restore chameleon flag. */
11006 if (isChameleon && (!wasChameleon))
11007 schema->flags ^= XML_SCHEMAS_INCLUDING_CONVERT_NS;
11008 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011009 /*
11010 * And now for the children...
11011 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011012 child = node->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011013 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011014 /*
11015 * Parse (simpleType | complexType | group | attributeGroup))*
11016 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011017 pctxt->redefined = bucket;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011018 /*
11019 * How to proceed if the redefined schema was not located?
11020 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011021 pctxt->isRedefine = 1;
11022 while (IS_SCHEMA(child, "annotation") ||
11023 IS_SCHEMA(child, "simpleType") ||
11024 IS_SCHEMA(child, "complexType") ||
11025 IS_SCHEMA(child, "group") ||
11026 IS_SCHEMA(child, "attributeGroup")) {
11027 if (IS_SCHEMA(child, "annotation")) {
11028 /*
11029 * TODO: discard or not?
11030 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011031 } else if (IS_SCHEMA(child, "simpleType")) {
11032 xmlSchemaParseSimpleType(pctxt, schema, child, 1);
11033 } else if (IS_SCHEMA(child, "complexType")) {
11034 xmlSchemaParseComplexType(pctxt, schema, child, 1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011035 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011036 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011037 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011038 xmlSchemaParseModelGroupDefinition(pctxt,
11039 schema, child);
11040 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011041 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011042 xmlSchemaParseAttributeGroupDefinition(pctxt, schema,
11043 child);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011044 }
11045 child = child->next;
11046 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011047 pctxt->redefined = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011048 pctxt->isRedefine = 0;
11049 } else {
11050 if (IS_SCHEMA(child, "annotation")) {
11051 /*
11052 * TODO: discard or not?
11053 */
11054 child = child->next;
11055 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011056 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011057 if (child != NULL) {
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011058 res = XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011059 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
11060 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011061 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011062 "(annotation | (simpleType | complexType | group | attributeGroup))*");
11063 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011064 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011065 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011066 "(annotation?)");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011067 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011068 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011069 return(res);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011070
11071exit_error:
11072 return(pctxt->err);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011073}
11074
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011075static int
11076xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11077 xmlNodePtr node)
11078{
11079 int res;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011080#ifndef ENABLE_REDEFINE
11081 TODO
11082 return(0);
11083#endif
11084 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11085 XML_SCHEMA_SCHEMA_REDEFINE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011086 if (res != 0)
11087 return(res);
11088 return(0);
11089}
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011090
11091static int
11092xmlSchemaParseInclude(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11093 xmlNodePtr node)
11094{
11095 int res;
11096
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011097 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11098 XML_SCHEMA_SCHEMA_INCLUDE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011099 if (res != 0)
11100 return(res);
11101 return(0);
11102}
11103
Daniel Veillardbd2904b2003-11-25 15:38:59 +000011104/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011105 * xmlSchemaParseModelGroup:
Daniel Veillard4255d502002-04-16 15:50:10 +000011106 * @ctxt: a schema validation context
11107 * @schema: the schema being built
11108 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011109 * @type: the "compositor" type
11110 * @particleNeeded: if a a model group with a particle
Daniel Veillard4255d502002-04-16 15:50:10 +000011111 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011112 * parse a XML schema Sequence definition.
11113 * Applies parts of:
11114 * Schema Representation Constraint:
11115 * Redefinition Constraints and Semantics (src-redefine)
11116 * (6.1), (6.1.1), (6.1.2)
11117 *
11118 * Schema Component Constraint:
11119 * All Group Limited (cos-all-limited) (2)
11120 * TODO: Actually this should go to component-level checks,
11121 * but is done here due to performance. Move it to an other layer
11122 * is schema construction via an API is implemented.
11123 *
Daniel Veillard4255d502002-04-16 15:50:10 +000011124 * *WARNING* this interface is highly subject to change
11125 *
William M. Bracke7091952004-05-11 15:09:58 +000011126 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +000011127 * 1 in case of success.
11128 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011129static xmlSchemaTreeItemPtr
11130xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
11131 xmlNodePtr node, xmlSchemaTypeType type,
11132 int withParticle)
Daniel Veillard4255d502002-04-16 15:50:10 +000011133{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011134 xmlSchemaModelGroupPtr item;
11135 xmlSchemaParticlePtr particle = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000011136 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011137 xmlAttrPtr attr;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011138 int min = 1, max = 1, isElemRef, hasRefs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000011139
11140 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011141 return (NULL);
11142 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011143 * Create a model group with the given compositor.
William M. Brack2f2a6632004-08-20 23:09:47 +000011144 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011145 item = xmlSchemaAddModelGroup(ctxt, schema, type, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011146 if (item == NULL)
11147 return (NULL);
11148
11149 if (withParticle) {
11150 if (type == XML_SCHEMA_TYPE_ALL) {
11151 min = xmlGetMinOccurs(ctxt, node, 0, 1, 1, "(0 | 1)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011152 max = xmlGetMaxOccurs(ctxt, node, 1, 1, 1, "1");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011153 } else {
11154 /* choice + sequence */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011155 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
11156 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
11157 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011158 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011159 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
11160 /*
11161 * Create a particle
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011162 */
11163 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
11164 if (particle == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011165 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011166 particle->children = (xmlSchemaTreeItemPtr) item;
11167 /*
11168 * Check for illegal attributes.
11169 */
11170 attr = node->properties;
11171 while (attr != NULL) {
11172 if (attr->ns == NULL) {
11173 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11174 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
11175 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011176 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011177 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011178 }
11179 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011180 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011181 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011182 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011183 attr = attr->next;
William M. Brack2f2a6632004-08-20 23:09:47 +000011184 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011185 } else {
11186 /*
11187 * Check for illegal attributes.
11188 */
11189 attr = node->properties;
11190 while (attr != NULL) {
11191 if (attr->ns == NULL) {
11192 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011193 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011194 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011195 }
11196 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011197 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011198 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011199 }
11200 attr = attr->next;
11201 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011202 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011203
William M. Brack2f2a6632004-08-20 23:09:47 +000011204 /*
11205 * Extract and validate attributes.
11206 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011207 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011208 /*
11209 * And now for the children...
11210 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011211 child = node->children;
11212 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011213 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011214 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011215 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011216 if (type == XML_SCHEMA_TYPE_ALL) {
11217 xmlSchemaParticlePtr part, last = NULL;
11218
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011219 while (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011220 part = (xmlSchemaParticlePtr) xmlSchemaParseElement(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011221 schema, child, &isElemRef, 0);
11222 /*
11223 * SPEC cos-all-limited (2)
11224 * "The {max occurs} of all the particles in the {particles}
11225 * of the ('all') group must be 0 or 1.
11226 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011227 if (part != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011228 if (isElemRef)
11229 hasRefs++;
11230 if (part->minOccurs > 1) {
11231 xmlSchemaPCustomErr(ctxt,
11232 XML_SCHEMAP_COS_ALL_LIMITED,
11233 NULL, child,
11234 "Invalid value for minOccurs (must be 0 or 1)",
11235 NULL);
11236 /* Reset to 1. */
11237 part->minOccurs = 1;
11238 }
11239 if (part->maxOccurs > 1) {
11240 xmlSchemaPCustomErr(ctxt,
11241 XML_SCHEMAP_COS_ALL_LIMITED,
11242 NULL, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011243 "Invalid value for maxOccurs (must be 0 or 1)",
11244 NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011245 /* Reset to 1. */
11246 part->maxOccurs = 1;
11247 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011248 if (last == NULL)
11249 item->children = (xmlSchemaTreeItemPtr) part;
11250 else
11251 last->next = (xmlSchemaTreeItemPtr) part;
11252 last = part;
11253 }
11254 child = child->next;
11255 }
11256 if (child != NULL) {
11257 xmlSchemaPContentErr(ctxt,
11258 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011259 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011260 "(annotation?, (annotation?, element*)");
11261 }
11262 } else {
11263 /* choice + sequence */
11264 xmlSchemaTreeItemPtr part = NULL, last = NULL;
11265
11266 while ((IS_SCHEMA(child, "element")) ||
11267 (IS_SCHEMA(child, "group")) ||
11268 (IS_SCHEMA(child, "any")) ||
11269 (IS_SCHEMA(child, "choice")) ||
11270 (IS_SCHEMA(child, "sequence"))) {
11271
11272 if (IS_SCHEMA(child, "element")) {
11273 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011274 xmlSchemaParseElement(ctxt, schema, child, &isElemRef, 0);
11275 if (part && isElemRef)
11276 hasRefs++;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011277 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011278 part =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011279 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011280 if (part != NULL)
11281 hasRefs++;
11282 /*
11283 * Handle redefinitions.
11284 */
11285 if (ctxt->isRedefine && ctxt->redef &&
11286 (ctxt->redef->item->type == XML_SCHEMA_TYPE_GROUP) &&
11287 part && part->children)
11288 {
11289 if ((xmlSchemaGetQNameRefName(part->children) ==
11290 ctxt->redef->refName) &&
11291 (xmlSchemaGetQNameRefTargetNs(part->children) ==
11292 ctxt->redef->refTargetNs))
11293 {
11294 /*
11295 * SPEC src-redefine:
11296 * (6.1) "If it has a <group> among its contents at
11297 * some level the ·actual value· of whose ref
11298 * [attribute] is the same as the ·actual value· of
11299 * its own name attribute plus target namespace, then
11300 * all of the following must be true:"
11301 * (6.1.1) "It must have exactly one such group."
11302 */
11303 if (ctxt->redefCounter != 0) {
11304 xmlChar *str = NULL;
11305
11306 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11307 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11308 "The redefining model group definition "
11309 "'%s' must not contain more than one "
11310 "reference to the redefined definition",
11311 xmlSchemaFormatQName(&str,
11312 ctxt->redef->refTargetNs,
11313 ctxt->redef->refName),
11314 NULL);
11315 FREE_AND_NULL(str)
11316 part = NULL;
11317 } else if (((WXS_PARTICLE(part))->minOccurs != 1) ||
11318 ((WXS_PARTICLE(part))->maxOccurs != 1))
11319 {
11320 xmlChar *str = NULL;
11321 /*
11322 * SPEC src-redefine:
11323 * (6.1.2) "The ·actual value· of both that
11324 * group's minOccurs and maxOccurs [attribute]
11325 * must be 1 (or ·absent·).
11326 */
11327 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11328 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11329 "The redefining model group definition "
11330 "'%s' must not contain a reference to the "
11331 "redefined definition with a "
11332 "maxOccurs/minOccurs other than 1",
11333 xmlSchemaFormatQName(&str,
11334 ctxt->redef->refTargetNs,
11335 ctxt->redef->refName),
11336 NULL);
11337 FREE_AND_NULL(str)
11338 part = NULL;
11339 }
11340 ctxt->redef->reference = WXS_BASIC_CAST part;
11341 ctxt->redefCounter++;
11342 }
11343 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011344 } else if (IS_SCHEMA(child, "any")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011345 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011346 xmlSchemaParseAny(ctxt, schema, child);
11347 } else if (IS_SCHEMA(child, "choice")) {
11348 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11349 XML_SCHEMA_TYPE_CHOICE, 1);
11350 } else if (IS_SCHEMA(child, "sequence")) {
11351 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11352 XML_SCHEMA_TYPE_SEQUENCE, 1);
11353 }
11354 if (part != NULL) {
11355 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011356 item->children = part;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011357 else
11358 last->next = part;
11359 last = part;
11360 }
11361 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011362 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011363 if (child != NULL) {
11364 xmlSchemaPContentErr(ctxt,
11365 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011366 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011367 "(annotation?, (element | group | choice | sequence | any)*)");
11368 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011369 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011370 if ((max == 0) && (min == 0))
11371 return (NULL);
11372 if (hasRefs) {
11373 /*
11374 * We need to resolve references.
11375 */
11376 WXS_ADD_PENDING(ctxt, item);
11377 }
11378 if (withParticle)
11379 return ((xmlSchemaTreeItemPtr) particle);
11380 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011381 return ((xmlSchemaTreeItemPtr) item);
Daniel Veillard4255d502002-04-16 15:50:10 +000011382}
11383
11384/**
11385 * xmlSchemaParseRestriction:
11386 * @ctxt: a schema validation context
11387 * @schema: the schema being built
11388 * @node: a subtree containing XML Schema informations
Daniel Veillard4255d502002-04-16 15:50:10 +000011389 *
11390 * parse a XML schema Restriction definition
11391 * *WARNING* this interface is highly subject to change
11392 *
11393 * Returns the type definition or NULL in case of error
11394 */
11395static xmlSchemaTypePtr
11396xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011397 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011398{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011399 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011400 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011401 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011402
11403 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11404 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011405 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011406 type = ctxt->ctxtType;
11407 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011408
11409 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011410 * Check for illegal attributes.
11411 */
11412 attr = node->properties;
11413 while (attr != NULL) {
11414 if (attr->ns == NULL) {
11415 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11416 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011417 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011418 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011419 }
11420 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011421 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011422 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011423 }
11424 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011425 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011426 /*
11427 * Extract and validate attributes.
11428 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011429 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011430 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011431 * Attribute
William M. Brack2f2a6632004-08-20 23:09:47 +000011432 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011433 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011434 * Extract the base type. The "base" attribute is mandatory if inside
11435 * a complex type or if redefining.
11436 *
11437 * SPEC (1.2) "...otherwise (<restriction> has no <simpleType> "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011438 * among its [children]), the simple type definition which is
11439 * the {content type} of the type definition ·resolved· to by
11440 * the ·actual value· of the base [attribute]"
11441 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011442 if (xmlSchemaPValAttrQName(ctxt, schema, NULL, node, "base",
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011443 &(type->baseNs), &(type->base)) == 0)
11444 {
11445 if ((type->base == NULL) && (type->type == XML_SCHEMA_TYPE_COMPLEX)) {
11446 xmlSchemaPMissingAttrErr(ctxt,
11447 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011448 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011449 } else if ((ctxt->isRedefine) &&
11450 (type->flags & XML_SCHEMAS_TYPE_GLOBAL))
11451 {
11452 if (type->base == NULL) {
11453 xmlSchemaPMissingAttrErr(ctxt,
11454 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011455 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011456 } else if ((! xmlStrEqual(type->base, type->name)) ||
11457 (! xmlStrEqual(type->baseNs, type->targetNamespace)))
11458 {
11459 xmlChar *str1 = NULL, *str2 = NULL;
11460 /*
11461 * REDEFINE: SPEC src-redefine (5)
11462 * "Within the [children], each <simpleType> must have a
11463 * <restriction> among its [children] ... the ·actual value· of
11464 * whose base [attribute] must be the same as the ·actual value·
11465 * of its own name attribute plus target namespace;"
11466 */
11467 xmlSchemaPCustomErrExt(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011468 NULL, node, "This is a redefinition, but the QName "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011469 "value '%s' of the 'base' attribute does not match the "
11470 "type's designation '%s'",
11471 xmlSchemaFormatQName(&str1, type->baseNs, type->base),
11472 xmlSchemaFormatQName(&str1, type->targetNamespace,
11473 type->name), NULL);
11474 FREE_AND_NULL(str1);
11475 FREE_AND_NULL(str2);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011476 /* Avoid confusion and erase the values. */
11477 type->base = NULL;
11478 type->baseNs = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011479 }
11480 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011481 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011482 /*
11483 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011484 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011485 child = node->children;
11486 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011487 /*
11488 * Add the annotation to the simple type ancestor.
11489 */
11490 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011491 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011492 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011493 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011494 if (parentType == XML_SCHEMA_TYPE_SIMPLE) {
11495 /*
11496 * Corresponds to <simpleType><restriction><simpleType>.
11497 */
William M. Brack2f2a6632004-08-20 23:09:47 +000011498 if (IS_SCHEMA(child, "simpleType")) {
11499 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011500 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011501 * src-restriction-base-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011502 * Either the base [attribute] or the simpleType [child] of the
11503 * <restriction> element must be present, but not both.
William M. Brack2f2a6632004-08-20 23:09:47 +000011504 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011505 xmlSchemaPContentErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000011506 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011507 NULL, node, child,
William M. Brack2f2a6632004-08-20 23:09:47 +000011508 "The attribute 'base' and the <simpleType> child are "
11509 "mutually exclusive", NULL);
11510 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011511 type->baseType = (xmlSchemaTypePtr)
William M. Brack2f2a6632004-08-20 23:09:47 +000011512 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011513 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011514 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011515 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011516 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011517 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011518 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011519 "Either the attribute 'base' or a <simpleType> child "
11520 "must be present", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000011521 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011522 } else if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11523 /*
11524 * Corresponds to <complexType><complexContent><restriction>...
11525 * followed by:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011526 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011527 * Model groups <all>, <choice> and <sequence>.
11528 */
11529 if (IS_SCHEMA(child, "all")) {
11530 type->subtypes = (xmlSchemaTypePtr)
11531 xmlSchemaParseModelGroup(ctxt, schema, child,
11532 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011533 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011534 } else if (IS_SCHEMA(child, "choice")) {
11535 type->subtypes = (xmlSchemaTypePtr)
11536 xmlSchemaParseModelGroup(ctxt,
11537 schema, child, XML_SCHEMA_TYPE_CHOICE, 1);
11538 child = child->next;
11539 } else if (IS_SCHEMA(child, "sequence")) {
11540 type->subtypes = (xmlSchemaTypePtr)
11541 xmlSchemaParseModelGroup(ctxt, schema, child,
11542 XML_SCHEMA_TYPE_SEQUENCE, 1);
11543 child = child->next;
11544 /*
11545 * Model group reference <group>.
11546 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011547 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011548 type->subtypes = (xmlSchemaTypePtr)
11549 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011550 /*
11551 * Note that the reference will be resolved in
11552 * xmlSchemaResolveTypeReferences();
11553 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011554 child = child->next;
11555 }
11556 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011557 /*
11558 * Corresponds to <complexType><simpleContent><restriction>...
11559 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011560 * "1.1 the simple type definition corresponding to the <simpleType>
11561 * among the [children] of <restriction> if there is one;"
11562 */
11563 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011564 /*
11565 * We will store the to-be-restricted simple type in
11566 * type->contentTypeDef *temporarily*.
11567 */
11568 type->contentTypeDef = (xmlSchemaTypePtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011569 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011570 if ( type->contentTypeDef == NULL)
11571 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011572 child = child->next;
11573 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011574 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011575
11576 if ((parentType == XML_SCHEMA_TYPE_SIMPLE) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011577 (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011578 xmlSchemaFacetPtr facet, lastfacet = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011579 /*
11580 * Corresponds to <complexType><simpleContent><restriction>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011581 * <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011582 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011583
Daniel Veillard01fa6152004-06-29 17:04:39 +000011584 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011585 * Add the facets to the simple type ancestor.
Daniel Veillard01fa6152004-06-29 17:04:39 +000011586 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000011587 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011588 * TODO: Datatypes: 4.1.3 Constraints on XML Representation of
11589 * Simple Type Definition Schema Representation Constraint:
Daniel Veillardc0826a72004-08-10 14:17:33 +000011590 * *Single Facet Value*
11591 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000011592 while ((IS_SCHEMA(child, "minInclusive")) ||
11593 (IS_SCHEMA(child, "minExclusive")) ||
11594 (IS_SCHEMA(child, "maxInclusive")) ||
11595 (IS_SCHEMA(child, "maxExclusive")) ||
11596 (IS_SCHEMA(child, "totalDigits")) ||
11597 (IS_SCHEMA(child, "fractionDigits")) ||
11598 (IS_SCHEMA(child, "pattern")) ||
11599 (IS_SCHEMA(child, "enumeration")) ||
11600 (IS_SCHEMA(child, "whiteSpace")) ||
11601 (IS_SCHEMA(child, "length")) ||
11602 (IS_SCHEMA(child, "maxLength")) ||
11603 (IS_SCHEMA(child, "minLength"))) {
11604 facet = xmlSchemaParseFacet(ctxt, schema, child);
11605 if (facet != NULL) {
11606 if (lastfacet == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011607 type->facets = facet;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011608 else
11609 lastfacet->next = facet;
11610 lastfacet = facet;
11611 lastfacet->next = NULL;
11612 }
11613 child = child->next;
11614 }
11615 /*
11616 * Create links for derivation and validation.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011617 */
11618 if (type->facets != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000011619 xmlSchemaFacetLinkPtr facetLink, lastFacetLink = NULL;
11620
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011621 facet = type->facets;
11622 do {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011623 facetLink = (xmlSchemaFacetLinkPtr)
11624 xmlMalloc(sizeof(xmlSchemaFacetLink));
Daniel Veillard01fa6152004-06-29 17:04:39 +000011625 if (facetLink == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000011626 xmlSchemaPErrMemory(ctxt, "allocating a facet link", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011627 xmlFree(facetLink);
11628 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011629 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000011630 facetLink->facet = facet;
11631 facetLink->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011632 if (lastFacetLink == NULL)
11633 type->facetSet = facetLink;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011634 else
11635 lastFacetLink->next = facetLink;
11636 lastFacetLink = facetLink;
11637 facet = facet->next;
11638 } while (facet != NULL);
11639 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011640 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011641 if (type->type == XML_SCHEMA_TYPE_COMPLEX) {
11642 /*
11643 * Attribute uses/declarations.
11644 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011645 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11646 (xmlSchemaItemListPtr *) &(type->attrUses),
11647 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
11648 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011649 /*
11650 * Attribute wildcard.
11651 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011652 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011653 type->attributeWildcard =
11654 xmlSchemaParseAnyAttribute(ctxt, schema, child);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011655 child = child->next;
11656 }
11657 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011658 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011659 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11660 xmlSchemaPContentErr(ctxt,
11661 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011662 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011663 "annotation?, (group | all | choice | sequence)?, "
11664 "((attribute | attributeGroup)*, anyAttribute?))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011665 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011666 xmlSchemaPContentErr(ctxt,
11667 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011668 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011669 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11670 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11671 "length | minLength | maxLength | enumeration | whiteSpace | "
11672 "pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))");
11673 } else {
11674 /* Simple type */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011675 xmlSchemaPContentErr(ctxt,
11676 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011677 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011678 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11679 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11680 "length | minLength | maxLength | enumeration | whiteSpace | "
11681 "pattern)*))");
11682 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011683 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011684 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011685}
11686
11687/**
11688 * xmlSchemaParseExtension:
11689 * @ctxt: a schema validation context
11690 * @schema: the schema being built
11691 * @node: a subtree containing XML Schema informations
11692 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011693 * Parses an <extension>, which is found inside a
11694 * <simpleContent> or <complexContent>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011695 * *WARNING* this interface is highly subject to change.
Daniel Veillard4255d502002-04-16 15:50:10 +000011696 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011697 * TODO: Returns the type definition or NULL in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +000011698 */
11699static xmlSchemaTypePtr
11700xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011701 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011702{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011703 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011704 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011705 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011706
11707 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11708 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011709 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011710 type = ctxt->ctxtType;
11711 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION;
Daniel Veillard4255d502002-04-16 15:50:10 +000011712
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011713 /*
11714 * Check for illegal attributes.
11715 */
11716 attr = node->properties;
11717 while (attr != NULL) {
11718 if (attr->ns == NULL) {
11719 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11720 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011721 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011722 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011723 }
11724 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011725 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011726 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011727 }
11728 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011729 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011730
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011731 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011732
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011733 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011734 * Attribute "base" - mandatory.
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011735 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011736 if ((xmlSchemaPValAttrQName(ctxt, schema, NULL, node,
11737 "base", &(type->baseNs), &(type->base)) == 0) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011738 (type->base == NULL)) {
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011739 xmlSchemaPMissingAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011740 XML_SCHEMAP_S4S_ATTR_MISSING,
11741 NULL, node, "base", NULL);
11742 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011743 /*
11744 * And now for the children...
11745 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011746 child = node->children;
11747 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011748 /*
11749 * Add the annotation to the type ancestor.
11750 */
11751 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011752 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011753 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011754 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011755 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11756 /*
11757 * Corresponds to <complexType><complexContent><extension>... and:
11758 *
11759 * Model groups <all>, <choice>, <sequence> and <group>.
11760 */
11761 if (IS_SCHEMA(child, "all")) {
11762 type->subtypes = (xmlSchemaTypePtr)
11763 xmlSchemaParseModelGroup(ctxt, schema,
11764 child, XML_SCHEMA_TYPE_ALL, 1);
11765 child = child->next;
11766 } else if (IS_SCHEMA(child, "choice")) {
11767 type->subtypes = (xmlSchemaTypePtr)
11768 xmlSchemaParseModelGroup(ctxt, schema,
11769 child, XML_SCHEMA_TYPE_CHOICE, 1);
11770 child = child->next;
11771 } else if (IS_SCHEMA(child, "sequence")) {
11772 type->subtypes = (xmlSchemaTypePtr)
11773 xmlSchemaParseModelGroup(ctxt, schema,
11774 child, XML_SCHEMA_TYPE_SEQUENCE, 1);
11775 child = child->next;
11776 } else if (IS_SCHEMA(child, "group")) {
11777 type->subtypes = (xmlSchemaTypePtr)
11778 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011779 /*
11780 * Note that the reference will be resolved in
11781 * xmlSchemaResolveTypeReferences();
11782 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011783 child = child->next;
11784 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011785 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011786 if (child != NULL) {
11787 /*
11788 * Attribute uses/declarations.
11789 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011790 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11791 (xmlSchemaItemListPtr *) &(type->attrUses),
11792 XML_SCHEMA_TYPE_EXTENSION, NULL) == -1)
11793 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011794 /*
11795 * Attribute wildcard.
11796 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011797 if (IS_SCHEMA(child, "anyAttribute")) {
11798 ctxt->ctxtType->attributeWildcard =
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011799 xmlSchemaParseAnyAttribute(ctxt, schema, child);
11800 child = child->next;
11801 }
11802 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011803 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011804 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11805 /* Complex content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011806 xmlSchemaPContentErr(ctxt,
11807 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011808 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011809 "(annotation?, ((group | all | choice | sequence)?, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011810 "((attribute | attributeGroup)*, anyAttribute?)))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011811 } else {
11812 /* Simple content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011813 xmlSchemaPContentErr(ctxt,
11814 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011815 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011816 "(annotation?, ((attribute | attributeGroup)*, "
11817 "anyAttribute?))");
11818 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011819 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011820 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011821}
11822
11823/**
11824 * xmlSchemaParseSimpleContent:
11825 * @ctxt: a schema validation context
11826 * @schema: the schema being built
11827 * @node: a subtree containing XML Schema informations
11828 *
11829 * parse a XML schema SimpleContent definition
11830 * *WARNING* this interface is highly subject to change
11831 *
11832 * Returns the type definition or NULL in case of error
11833 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011834static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011835xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011836 xmlSchemaPtr schema, xmlNodePtr node,
11837 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011838{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011839 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011840 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011841 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011842
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011843 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11844 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011845 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011846 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011847 /* Not a component, don't create it. */
11848 type = ctxt->ctxtType;
11849 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
11850 /*
11851 * Check for illegal attributes.
11852 */
11853 attr = node->properties;
11854 while (attr != NULL) {
11855 if (attr->ns == NULL) {
11856 if ((!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011857 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011858 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011859 }
11860 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011861 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011862 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011863 }
11864 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011865 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011866
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011867 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +000011868
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011869 /*
11870 * And now for the children...
11871 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011872 child = node->children;
11873 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011874 /*
11875 * Add the annotation to the complex type ancestor.
11876 */
11877 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011878 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011879 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011880 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011881 if (child == NULL) {
11882 xmlSchemaPContentErr(ctxt,
11883 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011884 NULL, node, NULL, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011885 "(annotation?, (restriction | extension))");
11886 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011887 if (child == NULL) {
11888 xmlSchemaPContentErr(ctxt,
11889 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011890 NULL, node, NULL, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011891 "(annotation?, (restriction | extension))");
11892 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011893 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011894 xmlSchemaParseRestriction(ctxt, schema, child,
11895 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011896 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011897 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011898 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011899 xmlSchemaParseExtension(ctxt, schema, child,
11900 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011901 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011902 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011903 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011904 if (child != NULL) {
11905 xmlSchemaPContentErr(ctxt,
11906 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011907 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011908 "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000011909 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011910 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000011911}
11912
11913/**
11914 * xmlSchemaParseComplexContent:
11915 * @ctxt: a schema validation context
11916 * @schema: the schema being built
11917 * @node: a subtree containing XML Schema informations
11918 *
11919 * parse a XML schema ComplexContent definition
11920 * *WARNING* this interface is highly subject to change
11921 *
11922 * Returns the type definition or NULL in case of error
11923 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011924static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011925xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011926 xmlSchemaPtr schema, xmlNodePtr node,
11927 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011928{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011929 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011930 xmlNodePtr child = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011931 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011932
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011933 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11934 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011935 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011936 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011937 /* Not a component, don't create it. */
11938 type = ctxt->ctxtType;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011939 /*
11940 * Check for illegal attributes.
11941 */
11942 attr = node->properties;
11943 while (attr != NULL) {
11944 if (attr->ns == NULL) {
11945 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011946 (!xmlStrEqual(attr->name, BAD_CAST "mixed")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011947 {
11948 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011949 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011950 }
11951 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
11952 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011953 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011954 }
11955 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011956 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011957
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011958 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011959
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011960 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011961 * Set the 'mixed' on the complex type ancestor.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011962 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011963 if (xmlGetBooleanProp(ctxt, node, "mixed", 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011964 if ((type->flags & XML_SCHEMAS_TYPE_MIXED) == 0)
11965 type->flags |= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011966 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011967 child = node->children;
11968 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011969 /*
11970 * Add the annotation to the complex type ancestor.
11971 */
11972 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011973 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011974 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011975 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011976 if (child == NULL) {
11977 xmlSchemaPContentErr(ctxt,
11978 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011979 NULL, node, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011980 NULL, "(annotation?, (restriction | extension))");
11981 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011982 if (child == NULL) {
11983 xmlSchemaPContentErr(ctxt,
11984 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011985 NULL, node, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011986 NULL, "(annotation?, (restriction | extension))");
11987 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011988 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011989 xmlSchemaParseRestriction(ctxt, schema, child,
11990 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011991 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011992 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011993 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011994 xmlSchemaParseExtension(ctxt, schema, child,
11995 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011996 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011997 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011998 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011999 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012000 xmlSchemaPContentErr(ctxt,
12001 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012002 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012003 NULL, "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012004 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012005 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000012006}
12007
12008/**
12009 * xmlSchemaParseComplexType:
12010 * @ctxt: a schema validation context
12011 * @schema: the schema being built
12012 * @node: a subtree containing XML Schema informations
12013 *
12014 * parse a XML schema Complex Type definition
12015 * *WARNING* this interface is highly subject to change
12016 *
12017 * Returns the type definition or NULL in case of error
12018 */
12019static xmlSchemaTypePtr
12020xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +000012021 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +000012022{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012023 xmlSchemaTypePtr type, ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012024 xmlNodePtr child = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012025 const xmlChar *name = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012026 xmlAttrPtr attr;
12027 const xmlChar *attrValue;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012028#ifdef ENABLE_NAMED_LOCALS
Daniel Veillard1a380b82004-10-21 16:00:06 +000012029 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012030#endif
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012031 int final = 0, block = 0, hasRestrictionOrExtension = 0;
Daniel Veillard1a380b82004-10-21 16:00:06 +000012032
Daniel Veillard4255d502002-04-16 15:50:10 +000012033
12034 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
12035 return (NULL);
12036
Daniel Veillard01fa6152004-06-29 17:04:39 +000012037 ctxtType = ctxt->ctxtType;
12038
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012039 if (topLevel) {
12040 attr = xmlSchemaGetPropNode(node, "name");
12041 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012042 xmlSchemaPMissingAttrErr(ctxt,
12043 XML_SCHEMAP_S4S_ATTR_MISSING, NULL, node, "name", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012044 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012045 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012046 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
12047 return (NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012048 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012049 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012050
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012051 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012052 /*
12053 * Parse as local complex type definition.
12054 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012055#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +000012056 snprintf(buf, 39, "#CT%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012057 type = xmlSchemaAddType(ctxt, schema,
12058 XML_SCHEMA_TYPE_COMPLEX,
12059 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012060 ctxt->targetNamespace, node, 0);
12061#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012062 type = xmlSchemaAddType(ctxt, schema,
12063 XML_SCHEMA_TYPE_COMPLEX,
12064 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012065#endif
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012066 if (type == NULL)
12067 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012068 name = type->name;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012069 type->node = node;
12070 type->type = XML_SCHEMA_TYPE_COMPLEX;
12071 /*
12072 * TODO: We need the target namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012073 */
12074 } else {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012075 /*
12076 * Parse as global complex type definition.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012077 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012078 type = xmlSchemaAddType(ctxt, schema,
12079 XML_SCHEMA_TYPE_COMPLEX,
12080 name, ctxt->targetNamespace, node, 1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012081 if (type == NULL)
12082 return (NULL);
12083 type->node = node;
12084 type->type = XML_SCHEMA_TYPE_COMPLEX;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012085 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +000012086 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012087 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012088 /*
12089 * Handle attributes.
12090 */
12091 attr = node->properties;
12092 while (attr != NULL) {
12093 if (attr->ns == NULL) {
12094 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
12095 /*
12096 * Attribute "id".
12097 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012098 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012099 } else if (xmlStrEqual(attr->name, BAD_CAST "mixed")) {
12100 /*
12101 * Attribute "mixed".
12102 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012103 if (xmlSchemaPGetBoolNodeValue(ctxt,
12104 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012105 type->flags |= XML_SCHEMAS_TYPE_MIXED;
12106 } else if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012107 /*
12108 * Attributes of global complex type definitions.
12109 */
12110 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
12111 /* Pass. */
12112 } else if (xmlStrEqual(attr->name, BAD_CAST "abstract")) {
12113 /*
12114 * Attribute "abstract".
12115 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012116 if (xmlSchemaPGetBoolNodeValue(ctxt,
12117 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012118 type->flags |= XML_SCHEMAS_TYPE_ABSTRACT;
12119 } else if (xmlStrEqual(attr->name, BAD_CAST "final")) {
12120 /*
12121 * Attribute "final".
12122 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012123 attrValue = xmlSchemaGetNodeContent(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012124 (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012125 if (xmlSchemaPValAttrBlockFinal(attrValue,
12126 &(type->flags),
12127 -1,
12128 XML_SCHEMAS_TYPE_FINAL_EXTENSION,
12129 XML_SCHEMAS_TYPE_FINAL_RESTRICTION,
12130 -1, -1, -1) != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012131 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012132 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012133 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012134 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012135 "(#all | List of (extension | restriction))",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012136 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012137 } else
12138 final = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012139 } else if (xmlStrEqual(attr->name, BAD_CAST "block")) {
12140 /*
12141 * Attribute "block".
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012142 */
12143 attrValue = xmlSchemaGetNodeContent(ctxt,
12144 (xmlNodePtr) attr);
12145 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012146 -1,
12147 XML_SCHEMAS_TYPE_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012148 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012149 -1, -1, -1) != 0) {
12150 xmlSchemaPSimpleTypeErr(ctxt,
12151 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012152 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012153 "(#all | List of (extension | restriction)) ",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012154 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012155 } else
12156 block = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012157 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012158 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012159 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012160 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012161 } else {
12162 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012163 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012164 }
12165 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012166 xmlSchemaPIllegalAttrErr(ctxt,
12167 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012168 }
12169 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012170 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012171 if (! block) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000012172 /*
12173 * Apply default "block" values.
12174 */
12175 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
12176 type->flags |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
12177 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
12178 type->flags |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
12179 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012180 if (! final) {
12181 /*
12182 * Apply default "block" values.
12183 */
12184 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
12185 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
12186 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
12187 type->flags |= XML_SCHEMAS_TYPE_FINAL_EXTENSION;
12188 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012189 /*
12190 * And now for the children...
12191 */
Daniel Veillard4255d502002-04-16 15:50:10 +000012192 child = node->children;
12193 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012194 type->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012195 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012196 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012197 ctxt->ctxtType = type;
Daniel Veillard4255d502002-04-16 15:50:10 +000012198 if (IS_SCHEMA(child, "simpleContent")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012199 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012200 * <complexType><simpleContent>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012201 * 3.4.3 : 2.2
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012202 * Specifying mixed='true' when the <simpleContent>
12203 * alternative is chosen has no effect
12204 */
William M. Bracke7091952004-05-11 15:09:58 +000012205 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
12206 type->flags ^= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012207 xmlSchemaParseSimpleContent(ctxt, schema, child,
12208 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012209 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012210 } else if (IS_SCHEMA(child, "complexContent")) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012211 /*
12212 * <complexType><complexContent>...
12213 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012214 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012215 xmlSchemaParseComplexContent(ctxt, schema, child,
12216 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012217 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012218 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012219 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012220 * E.g <complexType><sequence>... or <complexType><attribute>... etc.
12221 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012222 * SPEC
12223 * "...the third alternative (neither <simpleContent> nor
12224 * <complexContent>) is chosen. This case is understood as shorthand
12225 * for complex content restricting the ·ur-type definition·, and the
12226 * details of the mappings should be modified as necessary.
12227 */
12228 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
12229 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012230 /*
12231 * Parse model groups.
12232 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012233 if (IS_SCHEMA(child, "all")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012234 type->subtypes = (xmlSchemaTypePtr)
12235 xmlSchemaParseModelGroup(ctxt, schema, child,
12236 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012237 child = child->next;
12238 } else if (IS_SCHEMA(child, "choice")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012239 type->subtypes = (xmlSchemaTypePtr)
12240 xmlSchemaParseModelGroup(ctxt, schema, child,
12241 XML_SCHEMA_TYPE_CHOICE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012242 child = child->next;
12243 } else if (IS_SCHEMA(child, "sequence")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012244 type->subtypes = (xmlSchemaTypePtr)
12245 xmlSchemaParseModelGroup(ctxt, schema, child,
12246 XML_SCHEMA_TYPE_SEQUENCE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012247 child = child->next;
12248 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012249 type->subtypes = (xmlSchemaTypePtr)
12250 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012251 /*
12252 * Note that the reference will be resolved in
12253 * xmlSchemaResolveTypeReferences();
12254 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012255 child = child->next;
12256 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012257 /*
12258 * Parse attribute decls/refs.
12259 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012260 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
12261 (xmlSchemaItemListPtr *) &(type->attrUses),
12262 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
12263 return(NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012264 /*
12265 * Parse attribute wildcard.
12266 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012267 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012268 type->attributeWildcard = xmlSchemaParseAnyAttribute(ctxt, schema, child);
12269 child = child->next;
12270 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012271 }
12272 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012273 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012274 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012275 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012276 NULL, "(annotation?, (simpleContent | complexContent | "
12277 "((group | all | choice | sequence)?, ((attribute | "
12278 "attributeGroup)*, anyAttribute?))))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012279 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012280 /*
12281 * REDEFINE: SPEC src-redefine (5)
12282 */
12283 if (topLevel && ctxt->isRedefine && (! hasRestrictionOrExtension)) {
12284 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012285 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012286 "<complexType> must have a <restriction> or <extension> "
12287 "grand-child", NULL);
12288 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012289 ctxt->ctxtType = ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012290 return (type);
12291}
12292
Daniel Veillard4255d502002-04-16 15:50:10 +000012293/************************************************************************
12294 * *
12295 * Validating using Schemas *
12296 * *
12297 ************************************************************************/
12298
12299/************************************************************************
12300 * *
12301 * Reading/Writing Schemas *
12302 * *
12303 ************************************************************************/
12304
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012305#if 0 /* Will be enabled if it is clear what options are needed. */
12306/**
12307 * xmlSchemaParserCtxtSetOptions:
12308 * @ctxt: a schema parser context
12309 * @options: a combination of xmlSchemaParserOption
12310 *
12311 * Sets the options to be used during the parse.
12312 *
12313 * Returns 0 in case of success, -1 in case of an
12314 * API error.
12315 */
12316static int
12317xmlSchemaParserCtxtSetOptions(xmlSchemaParserCtxtPtr ctxt,
12318 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012319
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012320{
12321 int i;
12322
12323 if (ctxt == NULL)
12324 return (-1);
12325 /*
12326 * WARNING: Change the start value if adding to the
12327 * xmlSchemaParseOption.
12328 */
12329 for (i = 1; i < (int) sizeof(int) * 8; i++) {
12330 if (options & 1<<i) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012331 return (-1);
12332 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012333 }
12334 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012335 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012336}
12337
12338/**
12339 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012340 * @ctxt: a schema parser context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012341 *
12342 * Returns the option combination of the parser context.
12343 */
12344static int
12345xmlSchemaParserCtxtGetOptions(xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012346
12347{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012348 if (ctxt == NULL)
12349 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012350 else
12351 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012352}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012353#endif
12354
Daniel Veillard4255d502002-04-16 15:50:10 +000012355/**
12356 * xmlSchemaNewParserCtxt:
12357 * @URL: the location of the schema
12358 *
12359 * Create an XML Schemas parse context for that file/resource expected
12360 * to contain an XML Schemas file.
12361 *
12362 * Returns the parser context or NULL in case of error
12363 */
12364xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012365xmlSchemaNewParserCtxt(const char *URL)
12366{
Daniel Veillard4255d502002-04-16 15:50:10 +000012367 xmlSchemaParserCtxtPtr ret;
12368
12369 if (URL == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012370 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000012371
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012372 ret = xmlSchemaParserCtxtCreate();
12373 if (ret == NULL)
12374 return(NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012375 ret->dict = xmlDictCreate();
12376 ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012377 return (ret);
12378}
12379
12380/**
Daniel Veillard6045c902002-10-09 21:13:59 +000012381 * xmlSchemaNewMemParserCtxt:
12382 * @buffer: a pointer to a char array containing the schemas
12383 * @size: the size of the array
12384 *
12385 * Create an XML Schemas parse context for that memory buffer expected
12386 * to contain an XML Schemas file.
12387 *
12388 * Returns the parser context or NULL in case of error
12389 */
12390xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012391xmlSchemaNewMemParserCtxt(const char *buffer, int size)
12392{
Daniel Veillard6045c902002-10-09 21:13:59 +000012393 xmlSchemaParserCtxtPtr ret;
12394
12395 if ((buffer == NULL) || (size <= 0))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012396 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012397 ret = xmlSchemaParserCtxtCreate();
12398 if (ret == NULL)
12399 return(NULL);
Daniel Veillard6045c902002-10-09 21:13:59 +000012400 ret->buffer = buffer;
12401 ret->size = size;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012402 ret->dict = xmlDictCreate();
Daniel Veillard6045c902002-10-09 21:13:59 +000012403 return (ret);
12404}
12405
12406/**
Daniel Veillard9d751502003-10-29 13:21:47 +000012407 * xmlSchemaNewDocParserCtxt:
12408 * @doc: a preparsed document tree
12409 *
12410 * Create an XML Schemas parse context for that document.
12411 * NB. The document may be modified during the parsing process.
12412 *
12413 * Returns the parser context or NULL in case of error
12414 */
12415xmlSchemaParserCtxtPtr
12416xmlSchemaNewDocParserCtxt(xmlDocPtr doc)
12417{
12418 xmlSchemaParserCtxtPtr ret;
12419
12420 if (doc == NULL)
12421 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012422 ret = xmlSchemaParserCtxtCreate();
12423 if (ret == NULL)
12424 return(NULL);
Daniel Veillard9d751502003-10-29 13:21:47 +000012425 ret->doc = doc;
William M. Brackcf9eadf2003-12-25 13:24:05 +000012426 ret->dict = xmlDictCreate();
Daniel Veillarddda22c12004-01-24 08:31:30 +000012427 /* The application has responsibility for the document */
12428 ret->preserve = 1;
Daniel Veillard9d751502003-10-29 13:21:47 +000012429
12430 return (ret);
12431}
12432
12433/**
Daniel Veillard4255d502002-04-16 15:50:10 +000012434 * xmlSchemaFreeParserCtxt:
12435 * @ctxt: the schema parser context
12436 *
12437 * Free the resources associated to the schema parser context
12438 */
12439void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012440xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt)
12441{
Daniel Veillard4255d502002-04-16 15:50:10 +000012442 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012443 return;
Daniel Veillarddda22c12004-01-24 08:31:30 +000012444 if (ctxt->doc != NULL && !ctxt->preserve)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012445 xmlFreeDoc(ctxt->doc);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012446 if (ctxt->vctxt != NULL) {
12447 xmlSchemaFreeValidCtxt(ctxt->vctxt);
12448 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012449 if (ctxt->ownsConstructor && (ctxt->constructor != NULL)) {
12450 xmlSchemaConstructionCtxtFree(ctxt->constructor);
12451 ctxt->constructor = NULL;
12452 ctxt->ownsConstructor = 0;
12453 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012454 if (ctxt->attrProhibs != NULL)
12455 xmlSchemaItemListFree(ctxt->attrProhibs);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012456 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000012457 xmlFree(ctxt);
12458}
12459
12460/************************************************************************
12461 * *
12462 * Building the content models *
12463 * *
12464 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012465
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012466static void
12467xmlSchemaBuildContentModelForSubstGroup(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012468 xmlSchemaParticlePtr particle, int counter, xmlAutomataStatePtr end)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012469{
Daniel Veillarda980bef2005-07-18 21:34:03 +000012470 xmlAutomataStatePtr start, tmp;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012471 xmlSchemaElementPtr elemDecl, member;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012472 xmlSchemaSubstGroupPtr substGroup;
12473 int i;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012474
12475 elemDecl = (xmlSchemaElementPtr) particle->children;
12476 /*
12477 * Wrap the substitution group with a CHOICE.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012478 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012479 start = pctxt->state;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012480 if (end == NULL)
12481 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012482 substGroup = xmlSchemaSubstGroupGet(pctxt, elemDecl);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012483 if (substGroup == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012484 xmlSchemaPErr(pctxt, WXS_ITEM_NODE(particle),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012485 XML_SCHEMAP_INTERNAL,
12486 "Internal error: xmlSchemaBuildContentModelForSubstGroup, "
12487 "declaration is marked having a subst. group but none "
12488 "available.\n", elemDecl->name, NULL);
12489 return;
12490 }
Daniel Veillarda980bef2005-07-18 21:34:03 +000012491 if (counter >= 0) {
12492 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012493 * NOTE that we put the declaration in, even if it's abstract.
12494 * However, an error will be raised during *validation* if an element
12495 * information item shall be validated against an abstract element
12496 * declaration.
Daniel Veillarda980bef2005-07-18 21:34:03 +000012497 */
12498 tmp = xmlAutomataNewCountedTrans(pctxt->am, start, NULL, counter);
12499 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12500 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12501 /*
12502 * Add subst. group members.
12503 */
12504 for (i = 0; i < substGroup->members->nbItems; i++) {
12505 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12506 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12507 member->name, member->targetNamespace, member);
12508 }
12509 } else if (particle->maxOccurs == 1) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012510 /*
12511 * NOTE that we put the declaration in, even if it's abstract,
12512 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012513 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012514 xmlAutomataNewTransition2(pctxt->am,
12515 start, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012516 elemDecl->name, elemDecl->targetNamespace, elemDecl), end);
12517 /*
12518 * Add subst. group members.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012519 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012520 for (i = 0; i < substGroup->members->nbItems; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012521 member = (xmlSchemaElementPtr) substGroup->members->items[i];
Daniel Veillarda980bef2005-07-18 21:34:03 +000012522 tmp = xmlAutomataNewOnceTrans2(pctxt->am, start, NULL,
12523 member->name, member->targetNamespace,
12524 1, 1, member);
12525 xmlAutomataNewEpsilon(pctxt->am, tmp, end);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012526 }
12527 } else {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012528 xmlAutomataStatePtr hop;
12529 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12530 UNBOUNDED : particle->maxOccurs - 1;
12531 int minOccurs = particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
12532
12533 counter =
12534 xmlAutomataNewCounter(pctxt->am, minOccurs,
12535 maxOccurs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012536 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012537
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012538 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012539 xmlAutomataNewTransition2(pctxt->am,
12540 start, NULL,
12541 elemDecl->name, elemDecl->targetNamespace, elemDecl),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012542 hop);
12543 /*
Daniel Veillarda980bef2005-07-18 21:34:03 +000012544 * Add subst. group members.
12545 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012546 for (i = 0; i < substGroup->members->nbItems; i++) {
12547 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12548 xmlAutomataNewEpsilon(pctxt->am,
12549 xmlAutomataNewTransition2(pctxt->am,
12550 start, NULL,
12551 member->name, member->targetNamespace, member),
12552 hop);
12553 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012554 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12555 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
12556 }
12557 if (particle->minOccurs == 0)
12558 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012559 pctxt->state = end;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012560}
12561
12562static void
12563xmlSchemaBuildContentModelForElement(xmlSchemaParserCtxtPtr ctxt,
12564 xmlSchemaParticlePtr particle)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012565{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012566 if (((xmlSchemaElementPtr) particle->children)->flags &
12567 XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012568 /*
12569 * Substitution groups.
12570 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012571 xmlSchemaBuildContentModelForSubstGroup(ctxt, particle, -1, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012572 } else {
12573 xmlSchemaElementPtr elemDecl;
12574 xmlAutomataStatePtr start;
12575
12576 elemDecl = (xmlSchemaElementPtr) particle->children;
12577
12578 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012579 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012580 if (particle->maxOccurs == 1) {
12581 start = ctxt->state;
12582 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012583 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12584 } else if ((particle->maxOccurs >= UNBOUNDED) &&
12585 (particle->minOccurs < 2)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012586 /* Special case. */
12587 start = ctxt->state;
12588 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12589 elemDecl->name, elemDecl->targetNamespace, elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012590 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, start);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012591 } else {
12592 int counter;
12593 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12594 UNBOUNDED : particle->maxOccurs - 1;
12595 int minOccurs = particle->minOccurs < 1 ?
12596 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012597
12598 start = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012599 counter = xmlAutomataNewCounter(ctxt->am, minOccurs, maxOccurs);
12600 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12601 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12602 xmlAutomataNewCountedTrans(ctxt->am, ctxt->state, start, counter);
12603 ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, ctxt->state,
12604 NULL, counter);
12605 }
12606 if (particle->minOccurs == 0)
12607 xmlAutomataNewEpsilon(ctxt->am, start, ctxt->state);
12608 }
12609}
12610
Daniel Veillard4255d502002-04-16 15:50:10 +000012611/**
12612 * xmlSchemaBuildAContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012613 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012614 * @particle: the particle component
12615 * @name: the complex type's name whose content is being built
Daniel Veillard4255d502002-04-16 15:50:10 +000012616 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012617 * Create the automaton for the {content type} of a complex type.
12618 *
Daniel Veillard4255d502002-04-16 15:50:10 +000012619 */
12620static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012621xmlSchemaBuildAContentModel(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012622 xmlSchemaParticlePtr particle)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012623{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012624 if (particle == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012625 PERROR_INT("xmlSchemaBuildAContentModel", "particle is NULL");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012626 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012627 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012628 if (particle->children == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012629 /*
12630 * Just return in this case. A missing "term" of the particle
12631 * might arise due to an invalid "term" component.
12632 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012633 return;
12634 }
12635
12636 switch (particle->children->type) {
12637 case XML_SCHEMA_TYPE_ANY: {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012638 xmlAutomataStatePtr start, end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012639 xmlSchemaWildcardPtr wild;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012640 xmlSchemaWildcardNsPtr ns;
Daniel Veillard32370232002-10-16 14:08:14 +000012641
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012642 wild = (xmlSchemaWildcardPtr) particle->children;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012643
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012644 start = pctxt->state;
12645 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012646
12647 if (particle->maxOccurs == 1) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012648 if (wild->any == 1) {
12649 /*
12650 * We need to add both transitions:
12651 *
12652 * 1. the {"*", "*"} for elements in a namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012653 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012654 pctxt->state =
12655 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012656 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012657 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012658 /*
12659 * 2. the {"*"} for elements in no namespace.
12660 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012661 pctxt->state =
12662 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012663 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012664 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012665
12666 } else if (wild->nsSet != NULL) {
12667 ns = wild->nsSet;
12668 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012669 pctxt->state = start;
12670 pctxt->state = xmlAutomataNewTransition2(pctxt->am,
12671 pctxt->state, NULL, BAD_CAST "*", ns->value, wild);
12672 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012673 ns = ns->next;
12674 } while (ns != NULL);
12675
12676 } else if (wild->negNsSet != NULL) {
Daniel Veillard6e65e152005-08-09 11:09:52 +000012677 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
12678 start, end, BAD_CAST "*", wild->negNsSet->value,
12679 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012680 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000012681 } else {
12682 int counter;
12683 xmlAutomataStatePtr hop;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012684 int maxOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012685 particle->maxOccurs == UNBOUNDED ? UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012686 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012687 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012688
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012689 counter = xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12690 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012691 if (wild->any == 1) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012692 pctxt->state =
12693 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012694 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012695 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
12696 pctxt->state =
12697 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012698 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012699 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012700 } else if (wild->nsSet != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012701 ns = wild->nsSet;
12702 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012703 pctxt->state =
12704 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012705 start, NULL, BAD_CAST "*", ns->value, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012706 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012707 ns = ns->next;
12708 } while (ns != NULL);
12709
12710 } else if (wild->negNsSet != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012711 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
Daniel Veillard9efc4762005-07-19 14:33:55 +000012712 start, hop, BAD_CAST "*", wild->negNsSet->value,
12713 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012714 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012715 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12716 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012717 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012718 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012719 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012720 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012721 pctxt->state = end;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012722 break;
12723 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012724 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012725 xmlSchemaBuildContentModelForElement(pctxt, particle);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012726 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012727 case XML_SCHEMA_TYPE_SEQUENCE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012728 xmlSchemaTreeItemPtr sub;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012729
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012730 /*
12731 * If max and min occurances are default (1) then
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012732 * simply iterate over the particles of the <sequence>.
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012733 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012734 if ((particle->minOccurs == 1) && (particle->maxOccurs == 1)) {
12735 sub = particle->children->children;
12736 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012737 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012738 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012739 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012740 }
12741 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012742 xmlAutomataStatePtr oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012743
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012744 if (particle->maxOccurs >= UNBOUNDED) {
12745 if (particle->minOccurs > 1) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012746 xmlAutomataStatePtr tmp;
12747 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012748
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012749 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012750 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012751 oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012752
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012753 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012754 particle->minOccurs - 1, UNBOUNDED);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012755
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012756 sub = particle->children->children;
12757 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012758 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012759 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012760 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012761 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012762 tmp = pctxt->state;
12763 xmlAutomataNewCountedTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012764 oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012765 pctxt->state =
12766 xmlAutomataNewCounterTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012767 NULL, counter);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012768
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012769 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012770 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Daniel Veillardafc05b62005-07-17 06:11:19 +000012771 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012772 oldstate = pctxt->state;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012773
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012774 sub = particle->children->children;
12775 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012776 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012777 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012778 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012779 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012780 xmlAutomataNewEpsilon(pctxt->am, pctxt->state,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012781 oldstate);
Daniel Veillardafc05b62005-07-17 06:11:19 +000012782 /*
12783 * epsilon needed to block previous trans from
12784 * being allowed to enter back from another
12785 * construct
12786 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012787 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
12788 pctxt->state, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012789 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012790 xmlAutomataNewEpsilon(pctxt->am,
12791 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012792 }
12793 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012794 } else if ((particle->maxOccurs > 1)
12795 || (particle->minOccurs > 1)) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012796 xmlAutomataStatePtr tmp;
12797 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012798
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012799 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012800 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012801 oldstate = pctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +000012802
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012803 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012804 particle->minOccurs - 1,
12805 particle->maxOccurs - 1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012806
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012807 sub = particle->children->children;
12808 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012809 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012810 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012811 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012812 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012813 tmp = pctxt->state;
12814 xmlAutomataNewCountedTrans(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012815 tmp, oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012816 pctxt->state =
12817 xmlAutomataNewCounterTrans(pctxt->am, tmp, NULL,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012818 counter);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012819 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012820 xmlAutomataNewEpsilon(pctxt->am,
12821 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012822 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012823 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012824 sub = particle->children->children;
12825 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012826 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012827 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012828 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012829 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012830 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012831 xmlAutomataNewEpsilon(pctxt->am, oldstate,
12832 pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012833 }
12834 }
12835 }
12836 break;
12837 }
12838 case XML_SCHEMA_TYPE_CHOICE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012839 xmlSchemaTreeItemPtr sub;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012840 xmlAutomataStatePtr start, end;
Daniel Veillardb509f152002-04-17 16:28:10 +000012841
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012842 start = pctxt->state;
12843 end = xmlAutomataNewState(pctxt->am);
Daniel Veillard7646b182002-04-20 06:41:40 +000012844
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012845 /*
12846 * iterate over the subtypes and remerge the end with an
12847 * epsilon transition
12848 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012849 if (particle->maxOccurs == 1) {
12850 sub = particle->children->children;
12851 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012852 pctxt->state = start;
12853 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012854 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012855 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012856 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012857 }
12858 } else {
12859 int counter;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012860 xmlAutomataStatePtr hop, base;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012861 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12862 UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012863 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012864 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Daniel Veillard7646b182002-04-20 06:41:40 +000012865
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012866 /*
12867 * use a counter to keep track of the number of transtions
12868 * which went through the choice.
12869 */
12870 counter =
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012871 xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12872 hop = xmlAutomataNewState(pctxt->am);
12873 base = xmlAutomataNewState(pctxt->am);
Daniel Veillard6231e842002-04-18 11:54:04 +000012874
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012875 sub = particle->children->children;
12876 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012877 pctxt->state = base;
12878 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012879 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012880 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012881 sub = sub->next;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012882 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012883 xmlAutomataNewEpsilon(pctxt->am, start, base);
12884 xmlAutomataNewCountedTrans(pctxt->am, hop, base, counter);
12885 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012886 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012887 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012888 xmlAutomataNewEpsilon(pctxt->am, start, end);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012889 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012890 pctxt->state = end;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012891 break;
12892 }
12893 case XML_SCHEMA_TYPE_ALL:{
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +000012894 xmlAutomataStatePtr start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012895 xmlSchemaParticlePtr sub;
12896 xmlSchemaElementPtr elemDecl;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012897 int lax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012898
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012899 sub = (xmlSchemaParticlePtr) particle->children->children;
12900 if (sub == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012901 break;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012902 start = pctxt->state;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012903 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012904 pctxt->state = start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012905
12906 elemDecl = (xmlSchemaElementPtr) sub->children;
12907 if (elemDecl == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012908 PERROR_INT("xmlSchemaBuildAContentModel",
12909 "<element> particle has no term");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012910 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012911 };
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012912 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012913 * NOTE: The {max occurs} of all the particles in the
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +000012914 * {particles} of the group must be 0 or 1; this is
12915 * already ensured during the parse of the content of
12916 * <all>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012917 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012918 if (elemDecl->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
12919 int counter;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012920
Daniel Veillarda980bef2005-07-18 21:34:03 +000012921 /*
12922 * This is an abstract group, we need to share
12923 * the same counter for all the element transitions
12924 * derived from the group
12925 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012926 counter = xmlAutomataNewCounter(pctxt->am,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012927 sub->minOccurs, sub->maxOccurs);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012928 xmlSchemaBuildContentModelForSubstGroup(pctxt,
12929 sub, counter, pctxt->state);
Daniel Veillarda980bef2005-07-18 21:34:03 +000012930 } else {
12931 if ((sub->minOccurs == 1) &&
12932 (sub->maxOccurs == 1)) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012933 xmlAutomataNewOnceTrans2(pctxt->am, pctxt->state,
12934 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012935 elemDecl->name,
12936 elemDecl->targetNamespace,
12937 1, 1, elemDecl);
12938 } else if ((sub->minOccurs == 0) &&
12939 (sub->maxOccurs == 1)) {
12940
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012941 xmlAutomataNewCountTrans2(pctxt->am, pctxt->state,
12942 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012943 elemDecl->name,
12944 elemDecl->targetNamespace,
12945 0,
12946 1,
12947 elemDecl);
12948 }
12949 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012950 sub = (xmlSchemaParticlePtr) sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012951 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012952 lax = particle->minOccurs == 0;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012953 pctxt->state =
12954 xmlAutomataNewAllTrans(pctxt->am, pctxt->state, NULL, lax);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012955 break;
12956 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012957 case XML_SCHEMA_TYPE_GROUP:
12958 /*
12959 * If we hit a model group definition, then this means that
12960 * it was empty, thus was not substituted for the containing
12961 * model group. Just do nothing in this case.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012962 * TODO: But the group should be substituted and not occur at
12963 * all in the content model at this point. Fix this.
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012964 */
12965 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012966 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012967 xmlSchemaInternalErr2(ACTXT_CAST pctxt,
12968 "xmlSchemaBuildAContentModel",
12969 "found unexpected term of type '%s' in content model",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012970 WXS_ITEM_TYPE_NAME(particle->children), NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012971 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012972 }
12973}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012974
Daniel Veillard4255d502002-04-16 15:50:10 +000012975/**
12976 * xmlSchemaBuildContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012977 * @ctxt: the schema parser context
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012978 * @type: the complex type definition
Daniel Veillarda84c0b32003-06-02 16:58:46 +000012979 * @name: the element name
Daniel Veillard4255d502002-04-16 15:50:10 +000012980 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012981 * Builds the content model of the complex type.
Daniel Veillard4255d502002-04-16 15:50:10 +000012982 */
12983static void
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012984xmlSchemaBuildContentModel(xmlSchemaTypePtr type,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012985 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012986{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012987 if ((type->type != XML_SCHEMA_TYPE_COMPLEX) ||
12988 (type->contModel != NULL) ||
12989 ((type->contentType != XML_SCHEMA_CONTENT_ELEMENTS) &&
12990 (type->contentType != XML_SCHEMA_CONTENT_MIXED)))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012991 return;
Daniel Veillarddecd64d2002-04-18 14:41:51 +000012992
12993#ifdef DEBUG_CONTENT
12994 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012995 "Building content model for %s\n", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000012996#endif
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012997 ctxt->am = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000012998 ctxt->am = xmlNewAutomata();
12999 if (ctxt->am == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013000 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013001 "Cannot create automata for complex type %s\n", type->name);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013002 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000013003 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013004 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013005 /*
13006 * Build the automaton.
13007 */
13008 xmlSchemaBuildAContentModel(ctxt, WXS_TYPE_PARTICLE(type));
Daniel Veillard4255d502002-04-16 15:50:10 +000013009 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013010 type->contModel = xmlAutomataCompile(ctxt->am);
13011 if (type->contModel == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013012 xmlSchemaPCustomErr(ctxt,
13013 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013014 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013015 "Failed to compile the content model", NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013016 } else if (xmlRegexpIsDeterminist(type->contModel) != 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013017 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000013018 XML_SCHEMAP_NOT_DETERMINISTIC,
13019 /* XML_SCHEMAS_ERR_NOTDETERMINIST, */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013020 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013021 "The content model is not determinist", NULL);
Daniel Veillarde19fc232002-04-22 16:01:24 +000013022 } else {
Daniel Veillard118aed72002-09-24 14:13:13 +000013023#ifdef DEBUG_CONTENT_REGEXP
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013024 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013025 "Content model of %s:\n", type->name);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013026 xmlRegexpPrint(stderr, type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +000013027#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +000013028 }
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013029 ctxt->state = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013030 xmlFreeAutomata(ctxt->am);
13031 ctxt->am = NULL;
13032}
13033
13034/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013035 * xmlSchemaResolveElementReferences:
Daniel Veillard4255d502002-04-16 15:50:10 +000013036 * @elem: the schema element context
13037 * @ctxt: the schema parser context
13038 *
Daniel Veillardc0826a72004-08-10 14:17:33 +000013039 * Resolves the references of an element declaration
13040 * or particle, which has an element declaration as it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013041 * term.
Daniel Veillard4255d502002-04-16 15:50:10 +000013042 */
13043static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013044xmlSchemaResolveElementReferences(xmlSchemaElementPtr elemDecl,
13045 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000013046{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013047 if ((ctxt == NULL) || (elemDecl == NULL) ||
13048 ((elemDecl != NULL) &&
13049 (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_RESOLVED)))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013050 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013051 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_RESOLVED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013052
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013053 if ((elemDecl->subtypes == NULL) && (elemDecl->namedType != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013054 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013055
13056 /* (type definition) ... otherwise the type definition ·resolved·
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013057 * to by the ·actual value· of the type [attribute] ...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013058 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013059 type = xmlSchemaGetType(ctxt->schema, elemDecl->namedType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013060 elemDecl->namedTypeNs);
13061 if (type == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013062 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013063 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013064 WXS_BASIC_CAST elemDecl, elemDecl->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013065 "type", elemDecl->namedType, elemDecl->namedTypeNs,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013066 XML_SCHEMA_TYPE_BASIC, "type definition");
13067 } else
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013068 elemDecl->subtypes = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013069 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013070 if (elemDecl->substGroup != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013071 xmlSchemaElementPtr substHead;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013072
Daniel Veillardc0826a72004-08-10 14:17:33 +000013073 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013074 * FIXME TODO: Do we need a new field in _xmlSchemaElement for
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013075 * substitutionGroup?
Daniel Veillard01fa6152004-06-29 17:04:39 +000013076 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013077 substHead = xmlSchemaGetElem(ctxt->schema, elemDecl->substGroup,
13078 elemDecl->substGroupNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013079 if (substHead == NULL) {
13080 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013081 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013082 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013083 "substitutionGroup", elemDecl->substGroup,
13084 elemDecl->substGroupNs, XML_SCHEMA_TYPE_ELEMENT, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013085 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013086 xmlSchemaResolveElementReferences(substHead, ctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013087 /*
13088 * Set the "substitution group affiliation".
13089 * NOTE that now we use the "refDecl" field for this.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013090 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013091 WXS_SUBST_HEAD(elemDecl) = substHead;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013092 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013093 * The type definitions is set to:
13094 * SPEC "...the {type definition} of the element
13095 * declaration ·resolved· to by the ·actual value·
13096 * of the substitutionGroup [attribute], if present"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013097 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013098 if (elemDecl->subtypes == NULL)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013099 elemDecl->subtypes = substHead->subtypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013100 }
13101 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013102 /*
13103 * SPEC "The definition of anyType serves as the default type definition
13104 * for element declarations whose XML representation does not specify one."
13105 */
13106 if ((elemDecl->subtypes == NULL) &&
13107 (elemDecl->namedType == NULL) &&
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013108 (elemDecl->substGroup == NULL))
13109 elemDecl->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
William M. Bracke7091952004-05-11 15:09:58 +000013110}
13111
13112/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013113 * xmlSchemaResolveUnionMemberTypes:
William M. Bracke7091952004-05-11 15:09:58 +000013114 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013115 * @type: the schema simple type definition
William M. Bracke7091952004-05-11 15:09:58 +000013116 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013117 * Checks and builds the "member type definitions" property of the union
13118 * simple type. This handles part (1), part (2) is done in
13119 * xmlSchemaFinishMemberTypeDefinitionsProperty()
13120 *
Daniel Veillard01fa6152004-06-29 17:04:39 +000013121 * Returns -1 in case of an internal error, 0 otherwise.
William M. Bracke7091952004-05-11 15:09:58 +000013122 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013123static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013124xmlSchemaResolveUnionMemberTypes(xmlSchemaParserCtxtPtr ctxt,
13125 xmlSchemaTypePtr type)
Daniel Veillard377e1a92004-04-16 16:30:05 +000013126{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013127
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013128 xmlSchemaTypeLinkPtr link, lastLink, newLink;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013129 xmlSchemaTypePtr memberType;
Daniel Veillard377e1a92004-04-16 16:30:05 +000013130
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013131 /*
13132 * SPEC (1) "If the <union> alternative is chosen, then [Definition:]
13133 * define the explicit members as the type definitions ·resolved·
13134 * to by the items in the ·actual value· of the memberTypes [attribute],
13135 * if any, followed by the type definitions corresponding to the
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013136 * <simpleType>s among the [children] of <union>, if any."
Daniel Veillard01fa6152004-06-29 17:04:39 +000013137 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013138 /*
13139 * Resolve references.
13140 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013141 link = type->memberTypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013142 lastLink = NULL;
13143 while (link != NULL) {
13144 const xmlChar *name, *nsName;
Daniel Veillardc0826a72004-08-10 14:17:33 +000013145
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013146 name = ((xmlSchemaQNameRefPtr) link->type)->name;
13147 nsName = ((xmlSchemaQNameRefPtr) link->type)->targetNamespace;
13148
13149 memberType = xmlSchemaGetType(ctxt->schema, name, nsName);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013150 if ((memberType == NULL) || (! WXS_IS_SIMPLE(memberType))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013151 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013152 WXS_BASIC_CAST type, type->node, "memberTypes",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013153 name, nsName, XML_SCHEMA_TYPE_SIMPLE, NULL);
13154 /*
13155 * Remove the member type link.
13156 */
13157 if (lastLink == NULL)
13158 type->memberTypes = link->next;
13159 else
13160 lastLink->next = link->next;
13161 newLink = link;
13162 link = link->next;
13163 xmlFree(newLink);
13164 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013165 link->type = memberType;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013166 lastLink = link;
13167 link = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013168 }
13169 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013170 /*
13171 * Add local simple types,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013172 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013173 memberType = type->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013174 while (memberType != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013175 link = (xmlSchemaTypeLinkPtr) xmlMalloc(sizeof(xmlSchemaTypeLink));
13176 if (link == NULL) {
13177 xmlSchemaPErrMemory(ctxt, "allocating a type link", NULL);
13178 return (-1);
13179 }
13180 link->type = memberType;
13181 link->next = NULL;
13182 if (lastLink == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013183 type->memberTypes = link;
13184 else
Daniel Veillard01fa6152004-06-29 17:04:39 +000013185 lastLink->next = link;
13186 lastLink = link;
13187 memberType = memberType->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013188 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013189 return (0);
Daniel Veillard377e1a92004-04-16 16:30:05 +000013190}
13191
Daniel Veillard4255d502002-04-16 15:50:10 +000013192/**
Daniel Veillard3646d642004-06-02 19:19:14 +000013193 * xmlSchemaIsDerivedFromBuiltInType:
13194 * @ctxt: the schema parser context
13195 * @type: the type definition
13196 * @valType: the value type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013197 *
Daniel Veillard3646d642004-06-02 19:19:14 +000013198 *
13199 * Returns 1 if the type has the given value type, or
13200 * is derived from such a type.
13201 */
William M. Brack803812b2004-06-03 02:11:24 +000013202static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013203xmlSchemaIsDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
Daniel Veillard3646d642004-06-02 19:19:14 +000013204{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013205 if (type == NULL)
13206 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013207 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013208 return (0);
13209 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13210 if (type->builtInType == valType)
13211 return(1);
13212 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13213 (type->builtInType == XML_SCHEMAS_ANYTYPE))
13214 return (0);
13215 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13216 } else
13217 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard3646d642004-06-02 19:19:14 +000013218
13219 return (0);
13220}
13221
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013222#if 0
13223/**
13224 * xmlSchemaIsDerivedFromBuiltInType:
13225 * @ctxt: the schema parser context
13226 * @type: the type definition
13227 * @valType: the value type
13228 *
13229 *
13230 * Returns 1 if the type has the given value type, or
13231 * is derived from such a type.
13232 */
13233static int
13234xmlSchemaIsUserDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
13235{
13236 if (type == NULL)
13237 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013238 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013239 return (0);
13240 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13241 if (type->builtInType == valType)
13242 return(1);
13243 return (0);
13244 } else
13245 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13246
13247 return (0);
13248}
13249#endif
13250
13251static xmlSchemaTypePtr
13252xmlSchemaQueryBuiltInType(xmlSchemaTypePtr type)
13253{
13254 if (type == NULL)
13255 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013256 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013257 return (NULL);
13258 if (type->type == XML_SCHEMA_TYPE_BASIC)
13259 return(type);
13260 else
13261 return(xmlSchemaQueryBuiltInType(type->subtypes));
13262
13263 return (NULL);
13264}
13265
Daniel Veillard3646d642004-06-02 19:19:14 +000013266/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013267 * xmlSchemaGetPrimitiveType:
Daniel Veillard01fa6152004-06-29 17:04:39 +000013268 * @type: the simpleType definition
13269 *
13270 * Returns the primitive type of the given type or
13271 * NULL in case of error.
13272 */
13273static xmlSchemaTypePtr
13274xmlSchemaGetPrimitiveType(xmlSchemaTypePtr type)
13275{
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013276
Daniel Veillard01fa6152004-06-29 17:04:39 +000013277 while (type != NULL) {
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013278 /*
13279 * Note that anySimpleType is actually not a primitive type
13280 * but we need that here.
13281 */
13282 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13283 (type->flags & XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE))
Daniel Veillard01fa6152004-06-29 17:04:39 +000013284 return (type);
13285 type = type->baseType;
13286 }
13287
13288 return (NULL);
13289}
13290
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013291#if 0
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013292/**
13293 * xmlSchemaGetBuiltInTypeAncestor:
13294 * @type: the simpleType definition
13295 *
13296 * Returns the primitive type of the given type or
13297 * NULL in case of error.
13298 */
13299static xmlSchemaTypePtr
13300xmlSchemaGetBuiltInTypeAncestor(xmlSchemaTypePtr type)
13301{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013302 if (WXS_IS_LIST(type) || WXS_IS_UNION(type))
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000013303 return (0);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013304 while (type != NULL) {
13305 if (type->type == XML_SCHEMA_TYPE_BASIC)
13306 return (type);
13307 type = type->baseType;
13308 }
13309
13310 return (NULL);
13311}
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013312#endif
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013313
Daniel Veillard01fa6152004-06-29 17:04:39 +000013314/**
Daniel Veillard50355f02004-06-08 17:52:16 +000013315 * xmlSchemaCloneWildcardNsConstraints:
13316 * @ctxt: the schema parser context
13317 * @dest: the destination wildcard
13318 * @source: the source wildcard
13319 *
13320 * Clones the namespace constraints of source
13321 * and assignes them to dest.
13322 * Returns -1 on internal error, 0 otherwise.
13323 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013324static int
13325xmlSchemaCloneWildcardNsConstraints(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013326 xmlSchemaWildcardPtr dest,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013327 xmlSchemaWildcardPtr source)
Daniel Veillard3646d642004-06-02 19:19:14 +000013328{
13329 xmlSchemaWildcardNsPtr cur, tmp, last;
13330
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013331 if ((source == NULL) || (dest == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013332 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013333 dest->any = source->any;
Daniel Veillard3646d642004-06-02 19:19:14 +000013334 cur = source->nsSet;
13335 last = NULL;
13336 while (cur != NULL) {
13337 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
13338 if (tmp == NULL)
13339 return(-1);
13340 tmp->value = cur->value;
13341 if (last == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013342 dest->nsSet = tmp;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013343 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013344 last->next = tmp;
13345 last = tmp;
13346 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013347 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013348 if (dest->negNsSet != NULL)
13349 xmlSchemaFreeWildcardNsSet(dest->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +000013350 if (source->negNsSet != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013351 dest->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13352 if (dest->negNsSet == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013353 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013354 dest->negNsSet->value = source->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013355 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013356 dest->negNsSet = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000013357 return(0);
13358}
13359
Daniel Veillard50355f02004-06-08 17:52:16 +000013360/**
13361 * xmlSchemaUnionWildcards:
13362 * @ctxt: the schema parser context
13363 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013364 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013365 *
13366 * Unions the namespace constraints of the given wildcards.
13367 * @completeWild will hold the resulting union.
13368 * Returns a positive error code on failure, -1 in case of an
13369 * internal error, 0 otherwise.
13370 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013371static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013372xmlSchemaUnionWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013373 xmlSchemaWildcardPtr completeWild,
13374 xmlSchemaWildcardPtr curWild)
13375{
13376 xmlSchemaWildcardNsPtr cur, curB, tmp;
13377
13378 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013379 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013380 * value.
13381 */
13382 if ((completeWild->any == curWild->any) &&
13383 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13384 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013385
Daniel Veillard3646d642004-06-02 19:19:14 +000013386 if ((completeWild->negNsSet == NULL) ||
13387 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013388
Daniel Veillard3646d642004-06-02 19:19:14 +000013389 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013390 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013391
13392 /*
13393 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013394 */
13395 cur = completeWild->nsSet;
13396 while (cur != NULL) {
13397 found = 0;
13398 curB = curWild->nsSet;
13399 while (curB != NULL) {
13400 if (cur->value == curB->value) {
13401 found = 1;
13402 break;
13403 }
13404 curB = curB->next;
13405 }
13406 if (!found)
13407 break;
13408 cur = cur->next;
13409 }
13410 if (found)
13411 return(0);
13412 } else
13413 return(0);
13414 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013415 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013416 /*
13417 * 2 If either O1 or O2 is any, then any must be the value
13418 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013419 if (completeWild->any != curWild->any) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013420 if (completeWild->any == 0) {
13421 completeWild->any = 1;
13422 if (completeWild->nsSet != NULL) {
13423 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13424 completeWild->nsSet = NULL;
13425 }
13426 if (completeWild->negNsSet != NULL) {
13427 xmlFree(completeWild->negNsSet);
13428 completeWild->negNsSet = NULL;
13429 }
13430 }
Daniel Veillard50355f02004-06-08 17:52:16 +000013431 return (0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013432 }
13433 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013434 * 3 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013435 * then the union of those sets must be the value.
13436 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013437 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013438 int found;
13439 xmlSchemaWildcardNsPtr start;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013440
Daniel Veillard3646d642004-06-02 19:19:14 +000013441 cur = curWild->nsSet;
13442 start = completeWild->nsSet;
13443 while (cur != NULL) {
13444 found = 0;
13445 curB = start;
13446 while (curB != NULL) {
13447 if (cur->value == curB->value) {
13448 found = 1;
13449 break;
13450 }
13451 curB = curB->next;
13452 }
13453 if (!found) {
13454 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013455 if (tmp == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013456 return (-1);
13457 tmp->value = cur->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013458 tmp->next = completeWild->nsSet;
Daniel Veillard3646d642004-06-02 19:19:14 +000013459 completeWild->nsSet = tmp;
13460 }
13461 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013462 }
13463
Daniel Veillard3646d642004-06-02 19:19:14 +000013464 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013465 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013466 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013467 * 4 If the two are negations of different values (namespace names
Daniel Veillard3646d642004-06-02 19:19:14 +000013468 * or ·absent·), then a pair of not and ·absent· must be the value.
13469 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013470 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013471 (curWild->negNsSet != NULL) &&
13472 (completeWild->negNsSet->value != curWild->negNsSet->value)) {
13473 completeWild->negNsSet->value = NULL;
Daniel Veillard50355f02004-06-08 17:52:16 +000013474
13475 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013476 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013477 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013478 * 5.
13479 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013480 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013481 (completeWild->negNsSet->value != NULL) &&
13482 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013483 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013484 (curWild->negNsSet->value != NULL) &&
13485 (completeWild->nsSet != NULL))) {
13486
13487 int nsFound, absentFound = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013488
Daniel Veillard3646d642004-06-02 19:19:14 +000013489 if (completeWild->nsSet != NULL) {
13490 cur = completeWild->nsSet;
13491 curB = curWild->negNsSet;
13492 } else {
13493 cur = curWild->nsSet;
13494 curB = completeWild->negNsSet;
13495 }
13496 nsFound = 0;
13497 while (cur != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013498 if (cur->value == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013499 absentFound = 1;
13500 else if (cur->value == curB->value)
13501 nsFound = 1;
13502 if (nsFound && absentFound)
13503 break;
13504 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013505 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013506
13507 if (nsFound && absentFound) {
13508 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013509 * 5.1 If the set S includes both the negated namespace
Daniel Veillard3646d642004-06-02 19:19:14 +000013510 * name and ·absent·, then any must be the value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013511 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013512 completeWild->any = 1;
13513 if (completeWild->nsSet != NULL) {
13514 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13515 completeWild->nsSet = NULL;
13516 }
13517 if (completeWild->negNsSet != NULL) {
13518 xmlFree(completeWild->negNsSet);
13519 completeWild->negNsSet = NULL;
13520 }
13521 } else if (nsFound && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013522 /*
13523 * 5.2 If the set S includes the negated namespace name
13524 * but not ·absent·, then a pair of not and ·absent· must
Daniel Veillard3646d642004-06-02 19:19:14 +000013525 * be the value.
13526 */
13527 if (completeWild->nsSet != NULL) {
13528 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13529 completeWild->nsSet = NULL;
13530 }
13531 if (completeWild->negNsSet == NULL) {
13532 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13533 if (completeWild->negNsSet == NULL)
13534 return (-1);
13535 }
13536 completeWild->negNsSet->value = NULL;
13537 } else if ((!nsFound) && absentFound) {
13538 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013539 * 5.3 If the set S includes ·absent· but not the negated
Daniel Veillard3646d642004-06-02 19:19:14 +000013540 * namespace name, then the union is not expressible.
13541 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013542 xmlSchemaPErr(ctxt, completeWild->node,
Daniel Veillard3646d642004-06-02 19:19:14 +000013543 XML_SCHEMAP_UNION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013544 "The union of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013545 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013546 return(XML_SCHEMAP_UNION_NOT_EXPRESSIBLE);
Daniel Veillard3646d642004-06-02 19:19:14 +000013547 } else if ((!nsFound) && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013548 /*
13549 * 5.4 If the set S does not include either the negated namespace
13550 * name or ·absent·, then whichever of O1 or O2 is a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013551 * and a namespace name must be the value.
13552 */
13553 if (completeWild->negNsSet == NULL) {
13554 if (completeWild->nsSet != NULL) {
13555 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13556 completeWild->nsSet = NULL;
13557 }
13558 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13559 if (completeWild->negNsSet == NULL)
13560 return (-1);
13561 completeWild->negNsSet->value = curWild->negNsSet->value;
13562 }
13563 }
13564 return (0);
13565 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013566 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013567 * 6.
13568 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013569 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013570 (completeWild->negNsSet->value == NULL) &&
13571 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013572 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013573 (curWild->negNsSet->value == NULL) &&
13574 (completeWild->nsSet != NULL))) {
13575
13576 if (completeWild->nsSet != NULL) {
13577 cur = completeWild->nsSet;
13578 } else {
13579 cur = curWild->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013580 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013581 while (cur != NULL) {
13582 if (cur->value == NULL) {
13583 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013584 * 6.1 If the set S includes ·absent·, then any must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013585 * value.
13586 */
13587 completeWild->any = 1;
13588 if (completeWild->nsSet != NULL) {
13589 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13590 completeWild->nsSet = NULL;
13591 }
13592 if (completeWild->negNsSet != NULL) {
13593 xmlFree(completeWild->negNsSet);
13594 completeWild->negNsSet = NULL;
13595 }
13596 return (0);
13597 }
13598 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013599 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013600 if (completeWild->negNsSet == NULL) {
13601 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013602 * 6.2 If the set S does not include ·absent·, then a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013603 * and ·absent· must be the value.
13604 */
13605 if (completeWild->nsSet != NULL) {
13606 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13607 completeWild->nsSet = NULL;
13608 }
13609 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13610 if (completeWild->negNsSet == NULL)
13611 return (-1);
13612 completeWild->negNsSet->value = NULL;
13613 }
13614 return (0);
13615 }
13616 return (0);
13617
13618}
13619
Daniel Veillard50355f02004-06-08 17:52:16 +000013620/**
13621 * xmlSchemaIntersectWildcards:
13622 * @ctxt: the schema parser context
13623 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013624 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013625 *
13626 * Intersects the namespace constraints of the given wildcards.
13627 * @completeWild will hold the resulting intersection.
13628 * Returns a positive error code on failure, -1 in case of an
13629 * internal error, 0 otherwise.
13630 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013631static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013632xmlSchemaIntersectWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013633 xmlSchemaWildcardPtr completeWild,
13634 xmlSchemaWildcardPtr curWild)
13635{
William M. Brack803812b2004-06-03 02:11:24 +000013636 xmlSchemaWildcardNsPtr cur, curB, prev, tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013637
13638 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013639 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013640 * value.
13641 */
13642 if ((completeWild->any == curWild->any) &&
13643 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13644 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013645
Daniel Veillard3646d642004-06-02 19:19:14 +000013646 if ((completeWild->negNsSet == NULL) ||
13647 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013648
Daniel Veillard3646d642004-06-02 19:19:14 +000013649 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013650 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013651
13652 /*
13653 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013654 */
13655 cur = completeWild->nsSet;
13656 while (cur != NULL) {
13657 found = 0;
13658 curB = curWild->nsSet;
13659 while (curB != NULL) {
13660 if (cur->value == curB->value) {
13661 found = 1;
13662 break;
13663 }
13664 curB = curB->next;
13665 }
13666 if (!found)
13667 break;
13668 cur = cur->next;
13669 }
13670 if (found)
13671 return(0);
13672 } else
13673 return(0);
13674 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013675 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013676 /*
13677 * 2 If either O1 or O2 is any, then the other must be the value.
13678 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013679 if ((completeWild->any != curWild->any) && (completeWild->any)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013680 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013681 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013682 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013683 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013684 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013685 * 3 If either O1 or O2 is a pair of not and a value (a namespace
13686 * name or ·absent·) and the other is a set of (namespace names or
13687 * ·absent·), then that set, minus the negated value if it was in
Daniel Veillard3646d642004-06-02 19:19:14 +000013688 * the set, minus ·absent· if it was in the set, must be the value.
13689 */
13690 if (((completeWild->negNsSet != NULL) && (curWild->nsSet != NULL)) ||
13691 ((curWild->negNsSet != NULL) && (completeWild->nsSet != NULL))) {
13692 const xmlChar *neg;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013693
Daniel Veillard3646d642004-06-02 19:19:14 +000013694 if (completeWild->nsSet == NULL) {
13695 neg = completeWild->negNsSet->value;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013696 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Daniel Veillard3646d642004-06-02 19:19:14 +000013697 return(-1);
13698 } else
13699 neg = curWild->negNsSet->value;
13700 /*
13701 * Remove absent and negated.
13702 */
13703 prev = NULL;
13704 cur = completeWild->nsSet;
13705 while (cur != NULL) {
13706 if (cur->value == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013707 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013708 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013709 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013710 prev->next = cur->next;
13711 xmlFree(cur);
13712 break;
13713 }
13714 prev = cur;
13715 cur = cur->next;
13716 }
13717 if (neg != NULL) {
13718 prev = NULL;
13719 cur = completeWild->nsSet;
13720 while (cur != NULL) {
13721 if (cur->value == neg) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013722 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013723 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013724 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013725 prev->next = cur->next;
13726 xmlFree(cur);
13727 break;
13728 }
13729 prev = cur;
13730 cur = cur->next;
13731 }
13732 }
13733
13734 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013735 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013736 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013737 * 4 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013738 * then the intersection of those sets must be the value.
13739 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013740 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013741 int found;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013742
Daniel Veillard3646d642004-06-02 19:19:14 +000013743 cur = completeWild->nsSet;
13744 prev = NULL;
13745 while (cur != NULL) {
13746 found = 0;
13747 curB = curWild->nsSet;
13748 while (curB != NULL) {
13749 if (cur->value == curB->value) {
13750 found = 1;
13751 break;
13752 }
13753 curB = curB->next;
13754 }
13755 if (!found) {
13756 if (prev == NULL)
13757 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013758 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013759 prev->next = cur->next;
13760 tmp = cur->next;
13761 xmlFree(cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013762 cur = tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013763 continue;
13764 }
13765 prev = cur;
13766 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013767 }
13768
Daniel Veillard3646d642004-06-02 19:19:14 +000013769 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013770 }
13771 /* 5 If the two are negations of different namespace names,
Daniel Veillard3646d642004-06-02 19:19:14 +000013772 * then the intersection is not expressible
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013773 */
13774 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013775 (curWild->negNsSet != NULL) &&
13776 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013777 (completeWild->negNsSet->value != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013778 (curWild->negNsSet->value != NULL)) {
13779
13780 xmlSchemaPErr(ctxt, completeWild->node, XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013781 "The intersection of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013782 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013783 return(XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013784 }
13785 /*
13786 * 6 If the one is a negation of a namespace name and the other
13787 * is a negation of ·absent·, then the one which is the negation
Daniel Veillard3646d642004-06-02 19:19:14 +000013788 * of a namespace name must be the value.
13789 */
13790 if ((completeWild->negNsSet != NULL) && (curWild->negNsSet != NULL) &&
13791 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013792 (completeWild->negNsSet->value == NULL)) {
13793 completeWild->negNsSet->value = curWild->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013794 }
13795 return(0);
13796}
13797
Daniel Veillard50355f02004-06-08 17:52:16 +000013798/**
13799 * xmlSchemaIsWildcardNsConstraintSubset:
13800 * @ctxt: the schema parser context
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013801 * @sub: the first wildcard
13802 * @super: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013803 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013804 * Schema Component Constraint: Wildcard Subset (cos-ns-subset)
13805 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013806 * Returns 0 if the namespace constraint of @sub is an intensional
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013807 * subset of @super, 1 otherwise.
Daniel Veillard50355f02004-06-08 17:52:16 +000013808 */
13809static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013810xmlSchemaCheckCOSNSSubset(xmlSchemaWildcardPtr sub,
13811 xmlSchemaWildcardPtr super)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013812{
Daniel Veillard50355f02004-06-08 17:52:16 +000013813 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013814 * 1 super must be any.
Daniel Veillard50355f02004-06-08 17:52:16 +000013815 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013816 if (super->any)
13817 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013818 /*
13819 * 2.1 sub must be a pair of not and a namespace name or ·absent·.
13820 * 2.2 super must be a pair of not and the same value.
13821 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013822 if ((sub->negNsSet != NULL) &&
13823 (super->negNsSet != NULL) &&
13824 (sub->negNsSet->value == sub->negNsSet->value))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013825 return (0);
13826 /*
13827 * 3.1 sub must be a set whose members are either namespace names or ·absent·.
Daniel Veillard50355f02004-06-08 17:52:16 +000013828 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013829 if (sub->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013830 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013831 * 3.2.1 super must be the same set or a superset thereof.
Daniel Veillard50355f02004-06-08 17:52:16 +000013832 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013833 if (super->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013834 xmlSchemaWildcardNsPtr cur, curB;
13835 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013836
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013837 cur = sub->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013838 while (cur != NULL) {
13839 found = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013840 curB = super->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013841 while (curB != NULL) {
13842 if (cur->value == curB->value) {
13843 found = 1;
13844 break;
13845 }
13846 curB = curB->next;
13847 }
13848 if (!found)
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013849 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013850 cur = cur->next;
13851 }
13852 if (found)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013853 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013854 } else if (super->negNsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013855 xmlSchemaWildcardNsPtr cur;
13856 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013857 * 3.2.2 super must be a pair of not and a namespace name or
13858 * ·absent· and that value must not be in sub's set.
Daniel Veillard50355f02004-06-08 17:52:16 +000013859 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013860 cur = sub->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013861 while (cur != NULL) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013862 if (cur->value == super->negNsSet->value)
13863 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013864 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013865 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013866 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013867 }
13868 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013869 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013870}
13871
Daniel Veillard50355f02004-06-08 17:52:16 +000013872static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013873xmlSchemaGetEffectiveValueConstraint(xmlSchemaAttributeUsePtr attruse,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013874 int *fixed,
13875 const xmlChar **value,
13876 xmlSchemaValPtr *val)
13877{
13878 *fixed = 0;
13879 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013880 if (val != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013881 *val = NULL;
13882
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013883 if (attruse->defValue != NULL) {
13884 *value = attruse->defValue;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013885 if (val != NULL)
13886 *val = attruse->defVal;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013887 if (attruse->flags & XML_SCHEMA_ATTR_USE_FIXED)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013888 *fixed = 1;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013889 return(1);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013890 } else if ((attruse->attrDecl != NULL) &&
13891 (attruse->attrDecl->defValue != NULL)) {
13892 *value = attruse->attrDecl->defValue;
13893 if (val != NULL)
13894 *val = attruse->attrDecl->defVal;
13895 if (attruse->attrDecl->flags & XML_SCHEMAS_ATTR_FIXED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013896 *fixed = 1;
13897 return(1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013898 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013899 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013900}
Daniel Veillard3646d642004-06-02 19:19:14 +000013901/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013902 * xmlSchemaCheckCVCWildcardNamespace:
Daniel Veillard3646d642004-06-02 19:19:14 +000013903 * @wild: the wildcard
13904 * @ns: the namespace
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013905 *
13906 * Validation Rule: Wildcard allows Namespace Name
13907 * (cvc-wildcard-namespace)
13908 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013909 * Returns 0 if the given namespace matches the wildcard,
13910 * 1 otherwise and -1 on API errors.
Daniel Veillard3646d642004-06-02 19:19:14 +000013911 */
13912static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013913xmlSchemaCheckCVCWildcardNamespace(xmlSchemaWildcardPtr wild,
13914 const xmlChar* ns)
Daniel Veillard3646d642004-06-02 19:19:14 +000013915{
13916 if (wild == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013917 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013918
13919 if (wild->any)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013920 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013921 else if (wild->nsSet != NULL) {
13922 xmlSchemaWildcardNsPtr cur;
13923
13924 cur = wild->nsSet;
13925 while (cur != NULL) {
13926 if (xmlStrEqual(cur->value, ns))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013927 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013928 cur = cur->next;
13929 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013930 } else if ((wild->negNsSet != NULL) && (ns != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013931 (!xmlStrEqual(wild->negNsSet->value, ns)))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013932 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013933
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013934 return(1);
13935}
13936
13937#define XML_SCHEMA_ACTION_DERIVE 0
13938#define XML_SCHEMA_ACTION_REDEFINE 1
13939
13940#define WXS_ACTION_STR(a) \
13941((a) == XML_SCHEMA_ACTION_DERIVE) ? (const xmlChar *) "base" : (const xmlChar *) "redefined"
13942
13943/*
13944* Schema Component Constraint:
13945* Derivation Valid (Restriction, Complex)
13946* derivation-ok-restriction (2) - (4)
13947*
13948* ATTENTION:
13949* In XML Schema 1.1 this will be:
13950* Validation Rule:
13951* Checking complex type subsumption (practicalSubsumption) (1, 2 and 3)
13952*
13953*/
13954static int
13955xmlSchemaCheckDerivationOKRestriction2to4(xmlSchemaParserCtxtPtr pctxt,
13956 int action,
13957 xmlSchemaBasicItemPtr item,
13958 xmlSchemaBasicItemPtr baseItem,
13959 xmlSchemaItemListPtr uses,
13960 xmlSchemaItemListPtr baseUses,
13961 xmlSchemaWildcardPtr wild,
13962 xmlSchemaWildcardPtr baseWild)
13963{
13964 xmlSchemaAttributeUsePtr cur = NULL, bcur;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013965 int i, j, found; /* err = 0; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013966 const xmlChar *bEffValue;
13967 int effFixed;
13968
13969 if (uses != NULL) {
13970 for (i = 0; i < uses->nbItems; i++) {
13971 cur = uses->items[i];
13972 found = 0;
13973 if (baseUses == NULL)
13974 goto not_found;
13975 for (j = 0; j < baseUses->nbItems; j++) {
13976 bcur = baseUses->items[j];
13977 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
13978 WXS_ATTRUSE_DECL_NAME(bcur)) &&
13979 (WXS_ATTRUSE_DECL_TNS(cur) ==
13980 WXS_ATTRUSE_DECL_TNS(bcur)))
13981 {
13982 /*
13983 * (2.1) "If there is an attribute use in the {attribute
13984 * uses} of the {base type definition} (call this B) whose
13985 * {attribute declaration} has the same {name} and {target
13986 * namespace}, then all of the following must be true:"
13987 */
13988 found = 1;
13989
13990 if ((cur->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
13991 (bcur->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED))
13992 {
13993 xmlChar *str = NULL;
13994 /*
13995 * (2.1.1) "one of the following must be true:"
13996 * (2.1.1.1) "B's {required} is false."
13997 * (2.1.1.2) "R's {required} is true."
13998 */
13999 xmlSchemaPAttrUseErr4(pctxt,
14000 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_1,
14001 WXS_ITEM_NODE(item), item, cur,
14002 "The 'optional' attribute use is inconsistent "
14003 "with the corresponding 'required' attribute use of "
14004 "the %s %s",
14005 WXS_ACTION_STR(action),
14006 xmlSchemaGetComponentDesignation(&str, baseItem),
14007 NULL, NULL);
14008 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014009 /* err = pctxt->err; */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014010 } else if (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014011 WXS_ATTRUSE_TYPEDEF(cur),
14012 WXS_ATTRUSE_TYPEDEF(bcur), 0) != 0)
14013 {
14014 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
14015
14016 /*
14017 * SPEC (2.1.2) "R's {attribute declaration}'s
14018 * {type definition} must be validly derived from
14019 * B's {type definition} given the empty set as
14020 * defined in Type Derivation OK (Simple) (§3.14.6)."
14021 */
14022 xmlSchemaPAttrUseErr4(pctxt,
14023 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_2,
14024 WXS_ITEM_NODE(item), item, cur,
14025 "The attribute declaration's %s "
14026 "is not validly derived from "
14027 "the corresponding %s of the "
14028 "attribute declaration in the %s %s",
14029 xmlSchemaGetComponentDesignation(&strA,
14030 WXS_ATTRUSE_TYPEDEF(cur)),
14031 xmlSchemaGetComponentDesignation(&strB,
14032 WXS_ATTRUSE_TYPEDEF(bcur)),
14033 WXS_ACTION_STR(action),
14034 xmlSchemaGetComponentDesignation(&strC, baseItem));
14035 /* xmlSchemaGetComponentDesignation(&str, baseItem), */
14036 FREE_AND_NULL(strA);
14037 FREE_AND_NULL(strB);
14038 FREE_AND_NULL(strC);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014039 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014040 } else {
14041 /*
14042 * 2.1.3 [Definition:] Let the effective value
14043 * constraint of an attribute use be its {value
14044 * constraint}, if present, otherwise its {attribute
14045 * declaration}'s {value constraint} .
14046 */
14047 xmlSchemaGetEffectiveValueConstraint(bcur,
14048 &effFixed, &bEffValue, NULL);
14049 /*
14050 * 2.1.3 ... one of the following must be true
14051 *
14052 * 2.1.3.1 B's ·effective value constraint· is
14053 * ·absent· or default.
14054 */
14055 if ((bEffValue != NULL) &&
14056 (effFixed == 1)) {
14057 const xmlChar *rEffValue = NULL;
14058
14059 xmlSchemaGetEffectiveValueConstraint(bcur,
14060 &effFixed, &rEffValue, NULL);
14061 /*
14062 * 2.1.3.2 R's ·effective value constraint· is
14063 * fixed with the same string as B's.
14064 * MAYBE TODO: Compare the computed values.
14065 * Hmm, it says "same string" so
14066 * string-equality might really be sufficient.
14067 */
14068 if ((effFixed == 0) ||
14069 (! WXS_ARE_DEFAULT_STR_EQUAL(rEffValue, bEffValue)))
14070 {
14071 xmlChar *str = NULL;
14072
14073 xmlSchemaPAttrUseErr4(pctxt,
14074 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_3,
14075 WXS_ITEM_NODE(item), item, cur,
14076 "The effective value constraint of the "
14077 "attribute use is inconsistent with "
14078 "its correspondent in the %s %s",
14079 WXS_ACTION_STR(action),
14080 xmlSchemaGetComponentDesignation(&str,
14081 baseItem),
14082 NULL, NULL);
14083 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014084 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014085 }
14086 }
14087 }
14088 break;
14089 }
14090 }
14091not_found:
14092 if (!found) {
14093 /*
14094 * (2.2) "otherwise the {base type definition} must have an
14095 * {attribute wildcard} and the {target namespace} of the
14096 * R's {attribute declaration} must be ·valid· with respect
14097 * to that wildcard, as defined in Wildcard allows Namespace
14098 * Name (§3.10.4)."
14099 */
14100 if ((baseWild == NULL) ||
14101 (xmlSchemaCheckCVCWildcardNamespace(baseWild,
14102 (WXS_ATTRUSE_DECL(cur))->targetNamespace) != 0))
14103 {
14104 xmlChar *str = NULL;
14105
14106 xmlSchemaPAttrUseErr4(pctxt,
14107 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_2,
14108 WXS_ITEM_NODE(item), item, cur,
14109 "Neither a matching attribute use, "
14110 "nor a matching wildcard exists in the %s %s",
14111 WXS_ACTION_STR(action),
14112 xmlSchemaGetComponentDesignation(&str, baseItem),
14113 NULL, NULL);
14114 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014115 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014116 }
14117 }
14118 }
14119 }
14120 /*
14121 * SPEC derivation-ok-restriction (3):
14122 * (3) "For each attribute use in the {attribute uses} of the {base type
14123 * definition} whose {required} is true, there must be an attribute
14124 * use with an {attribute declaration} with the same {name} and
14125 * {target namespace} as its {attribute declaration} in the {attribute
14126 * uses} of the complex type definition itself whose {required} is true.
14127 */
14128 if (baseUses != NULL) {
14129 for (j = 0; j < baseUses->nbItems; j++) {
14130 bcur = baseUses->items[j];
14131 if (bcur->occurs != XML_SCHEMAS_ATTR_USE_REQUIRED)
14132 continue;
14133 found = 0;
14134 if (uses != NULL) {
14135 for (i = 0; i < uses->nbItems; i++) {
14136 cur = uses->items[i];
14137 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14138 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14139 (WXS_ATTRUSE_DECL_TNS(cur) ==
14140 WXS_ATTRUSE_DECL_TNS(bcur))) {
14141 found = 1;
14142 break;
14143 }
14144 }
14145 }
14146 if (!found) {
14147 xmlChar *strA = NULL, *strB = NULL;
14148
14149 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14150 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_3,
14151 NULL, item,
14152 "A matching attribute use for the "
14153 "'required' %s of the %s %s is missing",
14154 xmlSchemaGetComponentDesignation(&strA, bcur),
14155 WXS_ACTION_STR(action),
14156 xmlSchemaGetComponentDesignation(&strB, baseItem),
14157 NULL);
14158 FREE_AND_NULL(strA);
14159 FREE_AND_NULL(strB);
14160 }
14161 }
14162 }
14163 /*
14164 * derivation-ok-restriction (4)
14165 */
14166 if (wild != NULL) {
14167 /*
14168 * (4) "If there is an {attribute wildcard}, all of the
14169 * following must be true:"
14170 */
14171 if (baseWild == NULL) {
14172 xmlChar *str = NULL;
14173
14174 /*
14175 * (4.1) "The {base type definition} must also have one."
14176 */
14177 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14178 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_1,
14179 NULL, item,
14180 "The %s has an attribute wildcard, "
14181 "but the %s %s '%s' does not have one",
14182 WXS_ITEM_TYPE_NAME(item),
14183 WXS_ACTION_STR(action),
14184 WXS_ITEM_TYPE_NAME(baseItem),
14185 xmlSchemaGetComponentQName(&str, baseItem));
14186 FREE_AND_NULL(str);
14187 return(pctxt->err);
14188 } else if ((baseWild->any == 0) &&
14189 xmlSchemaCheckCOSNSSubset(wild, baseWild))
14190 {
14191 xmlChar *str = NULL;
14192 /*
14193 * (4.2) "The complex type definition's {attribute wildcard}'s
14194 * {namespace constraint} must be a subset of the {base type
14195 * definition}'s {attribute wildcard}'s {namespace constraint},
14196 * as defined by Wildcard Subset (§3.10.6)."
14197 */
14198 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14199 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_2,
14200 NULL, item,
14201 "The attribute wildcard is not a valid "
14202 "subset of the wildcard in the %s %s '%s'",
14203 WXS_ACTION_STR(action),
14204 WXS_ITEM_TYPE_NAME(baseItem),
14205 xmlSchemaGetComponentQName(&str, baseItem),
14206 NULL);
14207 FREE_AND_NULL(str);
14208 return(pctxt->err);
14209 }
14210 /* 4.3 Unless the {base type definition} is the ·ur-type
14211 * definition·, the complex type definition's {attribute
14212 * wildcard}'s {process contents} must be identical to or
14213 * stronger than the {base type definition}'s {attribute
14214 * wildcard}'s {process contents}, where strict is stronger
14215 * than lax is stronger than skip.
14216 */
14217 if ((! WXS_IS_ANYTYPE(baseItem)) &&
14218 (wild->processContents < baseWild->processContents)) {
14219 xmlChar *str = NULL;
14220 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14221 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_3,
14222 NULL, baseItem,
14223 "The {process contents} of the attribute wildcard is "
14224 "weaker than the one in the %s %s '%s'",
14225 WXS_ACTION_STR(action),
14226 WXS_ITEM_TYPE_NAME(baseItem),
14227 xmlSchemaGetComponentQName(&str, baseItem),
14228 NULL);
14229 FREE_AND_NULL(str)
14230 return(pctxt->err);
14231 }
14232 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014233 return(0);
14234}
14235
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014236
14237static int
14238xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
14239 xmlSchemaBasicItemPtr item,
14240 xmlSchemaWildcardPtr *completeWild,
14241 xmlSchemaItemListPtr list,
14242 xmlSchemaItemListPtr prohibs);
Daniel Veillard3646d642004-06-02 19:19:14 +000014243/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014244 * xmlSchemaFixupTypeAttributeUses:
Daniel Veillard3646d642004-06-02 19:19:14 +000014245 * @ctxt: the schema parser context
14246 * @type: the complex type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014247 *
Daniel Veillard3646d642004-06-02 19:19:14 +000014248 *
14249 * Builds the wildcard and the attribute uses on the given complex type.
14250 * Returns -1 if an internal error occurs, 0 otherwise.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014251 *
14252 * ATTENTION TODO: Experimantally this uses pointer comparisons for
14253 * strings, so recheck this if we start to hardcode some schemata, since
14254 * they might not be in the same dict.
14255 * NOTE: It is allowed to "extend" the xs:anyType type.
Daniel Veillard3646d642004-06-02 19:19:14 +000014256 */
14257static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014258xmlSchemaFixupTypeAttributeUses(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014259 xmlSchemaTypePtr type)
Daniel Veillard3646d642004-06-02 19:19:14 +000014260{
14261 xmlSchemaTypePtr baseType = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014262 xmlSchemaAttributeUsePtr use;
14263 xmlSchemaItemListPtr uses, baseUses, prohibs = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000014264
Daniel Veillard01fa6152004-06-29 17:04:39 +000014265 if (type->baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014266 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014267 "no base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000014268 return (-1);
14269 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014270 baseType = type->baseType;
14271 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014272 if (xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014273 return(-1);
14274
14275 uses = type->attrUses;
14276 baseUses = baseType->attrUses;
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014277 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014278 * Expand attribute group references. And build the 'complete'
14279 * wildcard, i.e. intersect multiple wildcards.
14280 * Move attribute prohibitions into a separate list.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014281 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014282 if (uses != NULL) {
14283 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014284 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014285 * This one will transfer all attr. prohibitions
14286 * into pctxt->attrProhibs.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014287 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014288 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14289 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14290 pctxt->attrProhibs) == -1)
14291 {
14292 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14293 "failed to expand attributes");
14294 }
14295 if (pctxt->attrProhibs->nbItems != 0)
14296 prohibs = pctxt->attrProhibs;
14297 } else {
14298 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14299 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14300 NULL) == -1)
14301 {
14302 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14303 "failed to expand attributes");
14304 }
14305 }
14306 }
14307 /*
14308 * Inherit the attribute uses of the base type.
14309 */
14310 if (baseUses != NULL) {
14311 int i, j;
14312 xmlSchemaAttributeUseProhibPtr pro;
14313
14314 if (WXS_IS_RESTRICTION(type)) {
14315 int usesCount;
14316 xmlSchemaAttributeUsePtr tmp;
14317
14318 if (uses != NULL)
14319 usesCount = uses->nbItems;
14320 else
14321 usesCount = 0;
14322
14323 /* Restriction. */
14324 for (i = 0; i < baseUses->nbItems; i++) {
14325 use = baseUses->items[i];
14326 if (prohibs) {
14327 /*
14328 * Filter out prohibited uses.
14329 */
14330 for (j = 0; j < prohibs->nbItems; j++) {
14331 pro = prohibs->items[j];
14332 if ((WXS_ATTRUSE_DECL_NAME(use) == pro->name) &&
14333 (WXS_ATTRUSE_DECL_TNS(use) ==
14334 pro->targetNamespace))
14335 {
14336 goto inherit_next;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014337 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014338 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014339 }
14340 if (usesCount) {
14341 /*
14342 * Filter out existing uses.
14343 */
14344 for (j = 0; j < usesCount; j++) {
14345 tmp = uses->items[j];
14346 if ((WXS_ATTRUSE_DECL_NAME(use) ==
14347 WXS_ATTRUSE_DECL_NAME(tmp)) &&
14348 (WXS_ATTRUSE_DECL_TNS(use) ==
14349 WXS_ATTRUSE_DECL_TNS(tmp)))
14350 {
14351 goto inherit_next;
14352 }
14353 }
14354 }
14355 if (uses == NULL) {
14356 type->attrUses = xmlSchemaItemListCreate();
14357 if (type->attrUses == NULL)
14358 goto exit_failure;
14359 uses = type->attrUses;
14360 }
14361 xmlSchemaItemListAddSize(uses, 2, use);
14362inherit_next: {}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014363 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014364 } else {
14365 /* Extension. */
14366 for (i = 0; i < baseUses->nbItems; i++) {
14367 use = baseUses->items[i];
14368 if (uses == NULL) {
14369 type->attrUses = xmlSchemaItemListCreate();
14370 if (type->attrUses == NULL)
14371 goto exit_failure;
14372 uses = type->attrUses;
14373 }
14374 xmlSchemaItemListAddSize(uses, baseUses->nbItems, use);
14375 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014376 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014377 }
14378 /*
14379 * Shrink attr. uses.
14380 */
14381 if (uses) {
14382 if (uses->nbItems == 0) {
14383 xmlSchemaItemListFree(uses);
14384 type->attrUses = NULL;
14385 }
14386 /*
14387 * TODO: We could shrink the size of the array
14388 * to fit the actual number of items.
14389 */
14390 }
14391 /*
14392 * Compute the complete wildcard.
14393 */
14394 if (WXS_IS_EXTENSION(type)) {
14395 if (baseType->attributeWildcard != NULL) {
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014396 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014397 * (3.2.2.1) "If the ·base wildcard· is non-·absent·, then
14398 * the appropriate case among the following:"
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014399 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014400 if (type->attributeWildcard != NULL) {
14401 /*
14402 * Union the complete wildcard with the base wildcard.
14403 * SPEC {attribute wildcard}
14404 * (3.2.2.1.2) "otherwise a wildcard whose {process contents}
14405 * and {annotation} are those of the ·complete wildcard·,
14406 * and whose {namespace constraint} is the intensional union
14407 * of the {namespace constraint} of the ·complete wildcard·
14408 * and of the ·base wildcard·, as defined in Attribute
14409 * Wildcard Union (§3.10.6)."
14410 */
14411 if (xmlSchemaUnionWildcards(pctxt, type->attributeWildcard,
14412 baseType->attributeWildcard) == -1)
14413 goto exit_failure;
14414 } else {
14415 /*
14416 * (3.2.2.1.1) "If the ·complete wildcard· is ·absent·,
14417 * then the ·base wildcard·."
14418 */
14419 type->attributeWildcard = baseType->attributeWildcard;
14420 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014421 } else {
14422 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014423 * (3.2.2.2) "otherwise (the ·base wildcard· is ·absent·) the
14424 * ·complete wildcard"
14425 * NOOP
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014426 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014427 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014428 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014429 /*
14430 * SPEC {attribute wildcard}
14431 * (3.1) "If the <restriction> alternative is chosen, then the
14432 * ·complete wildcard·;"
14433 * NOOP
14434 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014435 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014436
Daniel Veillard3646d642004-06-02 19:19:14 +000014437 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014438
14439exit_failure:
14440 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000014441}
14442
14443/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014444 * xmlSchemaTypeFinalContains:
14445 * @schema: the schema
14446 * @type: the type definition
14447 * @final: the final
14448 *
14449 * Evaluates if a type definition contains the given "final".
14450 * This does take "finalDefault" into account as well.
14451 *
14452 * Returns 1 if the type does containt the given "final",
14453 * 0 otherwise.
14454 */
14455static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014456xmlSchemaTypeFinalContains(xmlSchemaTypePtr type, int final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014457{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014458 if (type == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014459 return (0);
14460 if (type->flags & final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014461 return (1);
14462 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014463 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014464}
14465
14466/**
14467 * xmlSchemaGetUnionSimpleTypeMemberTypes:
14468 * @type: the Union Simple Type
14469 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014470 * Returns a list of member types of @type if existing,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014471 * returns NULL otherwise.
14472 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014473static xmlSchemaTypeLinkPtr
Daniel Veillard01fa6152004-06-29 17:04:39 +000014474xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type)
14475{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014476 while ((type != NULL) && (type->type == XML_SCHEMA_TYPE_SIMPLE)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014477 if (type->memberTypes != NULL)
14478 return (type->memberTypes);
14479 else
14480 type = type->baseType;
14481 }
14482 return (NULL);
14483}
14484
14485/**
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014486 * xmlSchemaGetParticleTotalRangeMin:
14487 * @particle: the particle
14488 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014489 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014490 * (all and sequence) + (choice)
14491 *
14492 * Returns the minimun Effective Total Range.
14493 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014494static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014495xmlSchemaGetParticleTotalRangeMin(xmlSchemaParticlePtr particle)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014496{
14497 if ((particle->children == NULL) ||
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014498 (particle->minOccurs == 0))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014499 return (0);
14500 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014501 int min = -1, cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014502 xmlSchemaParticlePtr part =
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014503 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014504
14505 if (part == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014506 return (0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014507 while (part != NULL) {
14508 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14509 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014510 cur = part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014511 else
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014512 cur = xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014513 if (cur == 0)
14514 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014515 if ((min > cur) || (min == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014516 min = cur;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014517 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014518 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014519 return (particle->minOccurs * min);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014520 } else {
14521 /* <all> and <sequence> */
14522 int sum = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014523 xmlSchemaParticlePtr part =
14524 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014525
14526 if (part == NULL)
14527 return (0);
14528 do {
14529 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14530 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014531 sum += part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014532 else
14533 sum += xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014534 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014535 } while (part != NULL);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014536 return (particle->minOccurs * sum);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014537 }
14538}
14539
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014540/**
14541 * xmlSchemaGetParticleTotalRangeMax:
14542 * @particle: the particle
14543 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014544 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014545 * (all and sequence) + (choice)
14546 *
14547 * Returns the maximum Effective Total Range.
14548 */
14549static int
14550xmlSchemaGetParticleTotalRangeMax(xmlSchemaParticlePtr particle)
14551{
14552 if ((particle->children == NULL) ||
14553 (particle->children->children == NULL))
14554 return (0);
14555 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
14556 int max = -1, cur;
14557 xmlSchemaParticlePtr part =
14558 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014559
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014560 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14561 if (part->children == NULL)
14562 continue;
14563 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14564 (part->children->type == XML_SCHEMA_TYPE_ANY))
14565 cur = part->maxOccurs;
14566 else
14567 cur = xmlSchemaGetParticleTotalRangeMax(part);
14568 if (cur == UNBOUNDED)
14569 return (UNBOUNDED);
14570 if ((max < cur) || (max == -1))
14571 max = cur;
14572 }
14573 /* TODO: Handle overflows? */
14574 return (particle->maxOccurs * max);
14575 } else {
14576 /* <all> and <sequence> */
14577 int sum = 0, cur;
14578 xmlSchemaParticlePtr part =
14579 (xmlSchemaParticlePtr) particle->children->children;
14580
14581 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14582 if (part->children == NULL)
14583 continue;
14584 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14585 (part->children->type == XML_SCHEMA_TYPE_ANY))
14586 cur = part->maxOccurs;
14587 else
14588 cur = xmlSchemaGetParticleTotalRangeMax(part);
14589 if (cur == UNBOUNDED)
14590 return (UNBOUNDED);
14591 if ((cur > 0) && (particle->maxOccurs == UNBOUNDED))
14592 return (UNBOUNDED);
14593 sum += cur;
14594 }
14595 /* TODO: Handle overflows? */
14596 return (particle->maxOccurs * sum);
14597 }
14598}
14599
14600/**
14601 * xmlSchemaIsParticleEmptiable:
14602 * @particle: the particle
14603 *
14604 * Schema Component Constraint: Particle Emptiable
14605 * Checks whether the given particle is emptiable.
14606 *
14607 * Returns 1 if emptiable, 0 otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014608 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014609static int
14610xmlSchemaIsParticleEmptiable(xmlSchemaParticlePtr particle)
14611{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014612 /*
14613 * SPEC (1) "Its {min occurs} is 0."
14614 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014615 if ((particle == NULL) || (particle->minOccurs == 0) ||
14616 (particle->children == NULL))
14617 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014618 /*
14619 * SPEC (2) "Its {term} is a group and the minimum part of the
14620 * effective total range of that group, [...] is 0."
14621 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014622 if (WXS_IS_MODEL_GROUP(particle->children)) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014623 if (xmlSchemaGetParticleTotalRangeMin(particle) == 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014624 return (1);
14625 }
14626 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014627}
14628
14629/**
14630 * xmlSchemaCheckCOSSTDerivedOK:
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014631 * @actxt: a context
Daniel Veillard01fa6152004-06-29 17:04:39 +000014632 * @type: the derived simple type definition
14633 * @baseType: the base type definition
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014634 * @subset: the subset of ('restriction', ect.)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014635 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014636 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014637 * Type Derivation OK (Simple) (cos-st-derived-OK)
14638 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014639 * Checks wheter @type can be validly
Daniel Veillard01fa6152004-06-29 17:04:39 +000014640 * derived from @baseType.
14641 *
14642 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014643 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014644static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014645xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014646 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014647 xmlSchemaTypePtr baseType,
14648 int subset)
14649{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014650 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014651 * 1 They are the same type definition.
14652 * TODO: The identy check might have to be more complex than this.
14653 */
14654 if (type == baseType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014655 return (0);
14656 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014657 * 2.1 restriction is not in the subset, or in the {final}
14658 * of its own {base type definition};
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014659 *
14660 * NOTE that this will be used also via "xsi:type".
14661 *
14662 * TODO: Revise this, it looks strange. How can the "type"
14663 * not be fixed or *in* fixing?
Daniel Veillard01fa6152004-06-29 17:04:39 +000014664 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014665 if (WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014666 if (xmlSchemaTypeFixup(type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014667 return(-1);
14668 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014669 if (xmlSchemaTypeFixup(baseType, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014670 return(-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014671 if ((subset & SUBSET_RESTRICTION) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014672 (xmlSchemaTypeFinalContains(type->baseType,
14673 XML_SCHEMAS_TYPE_FINAL_RESTRICTION))) {
14674 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014675 }
14676 /* 2.2 */
14677 if (type->baseType == baseType) {
14678 /*
14679 * 2.2.1 D's ·base type definition· is B.
14680 */
14681 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014682 }
14683 /*
14684 * 2.2.2 D's ·base type definition· is not the ·ur-type definition·
14685 * and is validly derived from B given the subset, as defined by this
14686 * constraint.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014687 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014688 if ((! WXS_IS_ANYTYPE(type->baseType)) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014689 (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014690 baseType, subset) == 0)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014691 return (0);
14692 }
14693 /*
14694 * 2.2.3 D's {variety} is list or union and B is the ·simple ur-type
Daniel Veillard01fa6152004-06-29 17:04:39 +000014695 * definition·.
14696 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014697 if (WXS_IS_ANY_SIMPLE_TYPE(baseType) &&
14698 (WXS_IS_LIST(type) || WXS_IS_UNION(type))) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014699 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014700 }
14701 /*
14702 * 2.2.4 B's {variety} is union and D is validly derived from a type
14703 * definition in B's {member type definitions} given the subset, as
Daniel Veillard01fa6152004-06-29 17:04:39 +000014704 * defined by this constraint.
14705 *
14706 * NOTE: This seems not to involve built-in types, since there is no
14707 * built-in Union Simple Type.
14708 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014709 if (WXS_IS_UNION(baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014710 xmlSchemaTypeLinkPtr cur;
14711
14712 cur = baseType->memberTypes;
14713 while (cur != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014714 if (WXS_IS_TYPE_NOT_FIXED(cur->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014715 if (xmlSchemaTypeFixup(cur->type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014716 return(-1);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014717 if (xmlSchemaCheckCOSSTDerivedOK(actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014718 type, cur->type, subset) == 0)
14719 {
14720 /*
14721 * It just has to be validly derived from at least one
14722 * member-type.
14723 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014724 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014725 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014726 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014727 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014728 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014729 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_2);
14730}
14731
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014732/**
14733 * xmlSchemaCheckTypeDefCircularInternal:
14734 * @pctxt: the schema parser context
14735 * @ctxtType: the type definition
14736 * @ancestor: an ancestor of @ctxtType
14737 *
14738 * Checks st-props-correct (2) + ct-props-correct (3).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014739 * Circular type definitions are not allowed.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014740 *
14741 * Returns XML_SCHEMAP_ST_PROPS_CORRECT_2 if the given type is
14742 * circular, 0 otherwise.
14743 */
14744static int
14745xmlSchemaCheckTypeDefCircularInternal(xmlSchemaParserCtxtPtr pctxt,
14746 xmlSchemaTypePtr ctxtType,
14747 xmlSchemaTypePtr ancestor)
14748{
14749 int ret;
14750
14751 if ((ancestor == NULL) || (ancestor->type == XML_SCHEMA_TYPE_BASIC))
14752 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014753
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014754 if (ctxtType == ancestor) {
14755 xmlSchemaPCustomErr(pctxt,
14756 XML_SCHEMAP_ST_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014757 WXS_BASIC_CAST ctxtType, WXS_ITEM_NODE(ctxtType),
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014758 "The definition is circular", NULL);
14759 return (XML_SCHEMAP_ST_PROPS_CORRECT_2);
14760 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014761 if (ancestor->flags & XML_SCHEMAS_TYPE_MARKED) {
14762 /*
14763 * Avoid inifinite recursion on circular types not yet checked.
14764 */
14765 return (0);
14766 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014767 ancestor->flags |= XML_SCHEMAS_TYPE_MARKED;
14768 ret = xmlSchemaCheckTypeDefCircularInternal(pctxt, ctxtType,
14769 ancestor->baseType);
14770 ancestor->flags ^= XML_SCHEMAS_TYPE_MARKED;
14771 return (ret);
14772}
14773
14774/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014775 * xmlSchemaCheckTypeDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014776 * @item: the complex/simple type definition
14777 * @ctxt: the parser context
14778 * @name: the name
14779 *
14780 * Checks for circular type definitions.
14781 */
14782static void
14783xmlSchemaCheckTypeDefCircular(xmlSchemaTypePtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014784 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014785{
14786 if ((item == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014787 (item->type == XML_SCHEMA_TYPE_BASIC) ||
14788 (item->baseType == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014789 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014790 xmlSchemaCheckTypeDefCircularInternal(ctxt, item,
14791 item->baseType);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014792}
Daniel Veillard01fa6152004-06-29 17:04:39 +000014793
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014794/*
14795* Simple Type Definition Representation OK (src-simple-type) 4
14796*
14797* "4 Circular union type definition is disallowed. That is, if the
14798* <union> alternative is chosen, there must not be any entries in the
14799* memberTypes [attribute] at any depth which resolve to the component
14800* corresponding to the <simpleType>."
14801*
14802* Note that this should work on the *representation* of a component,
14803* thus assumes any union types in the member types not being yet
14804* substituted. At this stage we need the variety of the types
14805* to be already computed.
14806*/
14807static int
14808xmlSchemaCheckUnionTypeDefCircularRecur(xmlSchemaParserCtxtPtr pctxt,
14809 xmlSchemaTypePtr ctxType,
14810 xmlSchemaTypeLinkPtr members)
14811{
14812 xmlSchemaTypeLinkPtr member;
14813 xmlSchemaTypePtr memberType;
14814
14815 member = members;
14816 while (member != NULL) {
14817 memberType = member->type;
14818 while ((memberType != NULL) &&
14819 (memberType->type != XML_SCHEMA_TYPE_BASIC)) {
14820 if (memberType == ctxType) {
14821 xmlSchemaPCustomErr(pctxt,
14822 XML_SCHEMAP_SRC_SIMPLE_TYPE_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014823 WXS_BASIC_CAST ctxType, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014824 "The union type definition is circular", NULL);
14825 return (XML_SCHEMAP_SRC_SIMPLE_TYPE_4);
14826 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014827 if ((WXS_IS_UNION(memberType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014828 ((memberType->flags & XML_SCHEMAS_TYPE_MARKED) == 0))
14829 {
14830 int res;
14831 memberType->flags |= XML_SCHEMAS_TYPE_MARKED;
14832 res = xmlSchemaCheckUnionTypeDefCircularRecur(pctxt,
14833 ctxType,
14834 xmlSchemaGetUnionSimpleTypeMemberTypes(memberType));
14835 memberType->flags ^= XML_SCHEMAS_TYPE_MARKED;
14836 if (res != 0)
14837 return(res);
14838 }
14839 memberType = memberType->baseType;
14840 }
14841 member = member->next;
14842 }
14843 return(0);
14844}
14845
14846static int
14847xmlSchemaCheckUnionTypeDefCircular(xmlSchemaParserCtxtPtr pctxt,
14848 xmlSchemaTypePtr type)
14849{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014850 if (! WXS_IS_UNION(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014851 return(0);
14852 return(xmlSchemaCheckUnionTypeDefCircularRecur(pctxt, type,
14853 type->memberTypes));
14854}
14855
Daniel Veillard01fa6152004-06-29 17:04:39 +000014856/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014857 * xmlSchemaResolveTypeReferences:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014858 * @item: the complex/simple type definition
14859 * @ctxt: the parser context
14860 * @name: the name
14861 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014862 * Resolvese type definition references
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014863 */
14864static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014865xmlSchemaResolveTypeReferences(xmlSchemaTypePtr typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014866 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014867{
14868 if (typeDef == NULL)
14869 return;
14870
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014871 /*
14872 * Resolve the base type.
14873 */
14874 if (typeDef->baseType == NULL) {
14875 typeDef->baseType = xmlSchemaGetType(ctxt->schema,
14876 typeDef->base, typeDef->baseNs);
14877 if (typeDef->baseType == NULL) {
14878 xmlSchemaPResCompAttrErr(ctxt,
14879 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014880 WXS_BASIC_CAST typeDef, typeDef->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014881 "base", typeDef->base, typeDef->baseNs,
14882 XML_SCHEMA_TYPE_SIMPLE, NULL);
14883 return;
14884 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014885 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014886 if (WXS_IS_SIMPLE(typeDef)) {
14887 if (WXS_IS_UNION(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014888 /*
14889 * Resolve the memberTypes.
14890 */
14891 xmlSchemaResolveUnionMemberTypes(ctxt, typeDef);
14892 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014893 } else if (WXS_IS_LIST(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014894 /*
14895 * Resolve the itemType.
14896 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014897 if ((typeDef->subtypes == NULL) && (typeDef->base != NULL)) {
14898
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014899 typeDef->subtypes = xmlSchemaGetType(ctxt->schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014900 typeDef->base, typeDef->baseNs);
14901
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014902 if ((typeDef->subtypes == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014903 (! WXS_IS_SIMPLE(typeDef->subtypes)))
14904 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014905 typeDef->subtypes = NULL;
14906 xmlSchemaPResCompAttrErr(ctxt,
14907 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014908 WXS_BASIC_CAST typeDef, typeDef->node,
14909 "itemType", typeDef->base, typeDef->baseNs,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014910 XML_SCHEMA_TYPE_SIMPLE, NULL);
14911 }
14912 }
14913 return;
14914 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014915 }
14916 /*
14917 * The ball of letters below means, that if we have a particle
14918 * which has a QName-helper component as its {term}, we want
14919 * to resolve it...
14920 */
14921 else if ((WXS_TYPE_CONTENTTYPE(typeDef) != NULL) &&
14922 ((WXS_TYPE_CONTENTTYPE(typeDef))->type ==
14923 XML_SCHEMA_TYPE_PARTICLE) &&
14924 (WXS_TYPE_PARTICLE_TERM(typeDef) != NULL) &&
14925 ((WXS_TYPE_PARTICLE_TERM(typeDef))->type ==
14926 XML_SCHEMA_EXTRA_QNAMEREF))
14927 {
14928 xmlSchemaQNameRefPtr ref =
14929 WXS_QNAME_CAST WXS_TYPE_PARTICLE_TERM(typeDef);
14930 xmlSchemaModelGroupDefPtr groupDef;
14931
14932 /*
14933 * URGENT TODO: Test this.
14934 */
14935 WXS_TYPE_PARTICLE_TERM(typeDef) = NULL;
14936 /*
14937 * Resolve the MG definition reference.
14938 */
14939 groupDef =
14940 WXS_MODEL_GROUPDEF_CAST xmlSchemaGetNamedComponent(ctxt->schema,
14941 ref->itemType, ref->name, ref->targetNamespace);
14942 if (groupDef == NULL) {
14943 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
14944 NULL, WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)),
14945 "ref", ref->name, ref->targetNamespace, ref->itemType,
14946 NULL);
14947 /* Remove the particle. */
14948 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14949 } else if (WXS_MODELGROUPDEF_MODEL(groupDef) == NULL)
14950 /* Remove the particle. */
14951 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14952 else {
14953 /*
14954 * Assign the MG definition's {model group} to the
14955 * particle's {term}.
14956 */
14957 WXS_TYPE_PARTICLE_TERM(typeDef) = WXS_MODELGROUPDEF_MODEL(groupDef);
14958
14959 if (WXS_MODELGROUPDEF_MODEL(groupDef)->type == XML_SCHEMA_TYPE_ALL) {
14960 /*
14961 * SPEC cos-all-limited (1.2)
14962 * "1.2 the {term} property of a particle with
14963 * {max occurs}=1 which is part of a pair which constitutes
14964 * the {content type} of a complex type definition."
14965 */
14966 if ((WXS_TYPE_PARTICLE(typeDef))->maxOccurs != 1) {
14967 xmlSchemaCustomErr(ACTXT_CAST ctxt,
14968 /* TODO: error code */
14969 XML_SCHEMAP_COS_ALL_LIMITED,
14970 WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)), NULL,
14971 "The particle's {max occurs} must be 1, since the "
14972 "reference resolves to an 'all' model group",
14973 NULL, NULL);
14974 }
14975 }
14976 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014977 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014978}
14979
14980
14981
14982/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014983 * xmlSchemaCheckSTPropsCorrect:
14984 * @ctxt: the schema parser context
14985 * @type: the simple type definition
14986 *
14987 * Checks st-props-correct.
14988 *
14989 * Returns 0 if the properties are correct,
14990 * if not, a positive error code and -1 on internal
14991 * errors.
14992 */
14993static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014994xmlSchemaCheckSTPropsCorrect(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014995 xmlSchemaTypePtr type)
14996{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014997 xmlSchemaTypePtr baseType = type->baseType;
Daniel Veillardc0826a72004-08-10 14:17:33 +000014998 xmlChar *str = NULL;
Daniel Veillard01fa6152004-06-29 17:04:39 +000014999
Daniel Veillardc0826a72004-08-10 14:17:33 +000015000 /* STATE: error funcs converted. */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015001 /*
15002 * Schema Component Constraint: Simple Type Definition Properties Correct
15003 *
15004 * NOTE: This is somehow redundant, since we actually built a simple type
15005 * to have all the needed information; this acts as an self test.
15006 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015007 /* Base type: If the datatype has been ·derived· by ·restriction·
15008 * then the Simple Type Definition component from which it is ·derived·,
15009 * otherwise the Simple Type Definition for anySimpleType (§4.1.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000015010 */
15011 if (baseType == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015012 /*
15013 * TODO: Think about: "modulo the impact of Missing
15014 * Sub-components (§5.3)."
15015 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015016 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015017 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015018 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015019 "No base type existent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015020 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015021
Daniel Veillard01fa6152004-06-29 17:04:39 +000015022 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015023 if (! WXS_IS_SIMPLE(baseType)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015024 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015025 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015026 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015027 "The base type '%s' is not a simple type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015028 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015029 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015030 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15031 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015032 if ( (WXS_IS_LIST(type) || WXS_IS_UNION(type)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015033 (WXS_IS_RESTRICTION(type) == 0) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015034 (! WXS_IS_ANY_SIMPLE_TYPE(baseType))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015035 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015036 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015037 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015038 "A type, derived by list or union, must have"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015039 "the simple ur-type definition as base type, not '%s'",
15040 xmlSchemaGetComponentQName(&str, baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015041 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015042 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15043 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015044 /*
15045 * Variety: One of {atomic, list, union}.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015046 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015047 if ((! WXS_IS_ATOMIC(type)) && (! WXS_IS_UNION(type)) &&
15048 (! WXS_IS_LIST(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015049 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015050 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015051 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015052 "The variety is absent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015053 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15054 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000015055 /* TODO: Finish this. Hmm, is this finished? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015056
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015057 /*
15058 * 3 The {final} of the {base type definition} must not contain restriction.
15059 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015060 if (xmlSchemaTypeFinalContains(baseType,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015061 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
15062 xmlSchemaPCustomErr(ctxt,
15063 XML_SCHEMAP_ST_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015064 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015065 "The 'final' of its base type '%s' must not contain "
15066 "'restriction'",
15067 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015068 FREE_AND_NULL(str)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015069 return (XML_SCHEMAP_ST_PROPS_CORRECT_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015070 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015071
15072 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015073 * 2 All simple type definitions must be derived ultimately from the ·simple
15074 * ur-type definition (so· circular definitions are disallowed). That is, it
15075 * must be possible to reach a built-in primitive datatype or the ·simple
15076 * ur-type definition· by repeatedly following the {base type definition}.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015077 *
15078 * NOTE: this is done in xmlSchemaCheckTypeDefCircular().
Daniel Veillard01fa6152004-06-29 17:04:39 +000015079 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015080 return (0);
15081}
15082
15083/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015084 * xmlSchemaCheckCOSSTRestricts:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015085 * @ctxt: the schema parser context
15086 * @type: the simple type definition
15087 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015088 * Schema Component Constraint:
15089 * Derivation Valid (Restriction, Simple) (cos-st-restricts)
15090
15091 * Checks if the given @type (simpleType) is derived validly by restriction.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015092 * STATUS:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015093 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015094 * Returns -1 on internal errors, 0 if the type is validly derived,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015095 * a positive error code otherwise.
15096 */
15097static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015098xmlSchemaCheckCOSSTRestricts(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015099 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015100{
Daniel Veillardc0826a72004-08-10 14:17:33 +000015101 xmlChar *str = NULL;
15102
Daniel Veillard01fa6152004-06-29 17:04:39 +000015103 if (type->type != XML_SCHEMA_TYPE_SIMPLE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015104 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15105 "given type is not a user-derived simpleType");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015106 return (-1);
15107 }
15108
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015109 if (WXS_IS_ATOMIC(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015110 xmlSchemaTypePtr primitive;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015111 /*
15112 * 1.1 The {base type definition} must be an atomic simple
Daniel Veillard01fa6152004-06-29 17:04:39 +000015113 * type definition or a built-in primitive datatype.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015114 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015115 if (! WXS_IS_ATOMIC(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015116 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015117 XML_SCHEMAP_COS_ST_RESTRICTS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015118 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015119 "The base type '%s' is not an atomic simple type",
15120 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015121 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015122 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_1);
15123 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015124 /* 1.2 The {final} of the {base type definition} must not contain
Daniel Veillard01fa6152004-06-29 17:04:39 +000015125 * restriction.
15126 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015127 /* OPTIMIZE TODO : This is already done in xmlSchemaCheckStPropsCorrect */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015128 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015129 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015130 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015131 XML_SCHEMAP_COS_ST_RESTRICTS_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015132 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015133 "The final of its base type '%s' must not contain 'restriction'",
15134 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015135 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015136 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_2);
15137 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015138
15139 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015140 * 1.3.1 DF must be an allowed constraining facet for the {primitive
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015141 * type definition}, as specified in the appropriate subsection of 3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015142 * Primitive datatypes.
15143 */
15144 if (type->facets != NULL) {
15145 xmlSchemaFacetPtr facet;
15146 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015147
Daniel Veillard01fa6152004-06-29 17:04:39 +000015148 primitive = xmlSchemaGetPrimitiveType(type);
15149 if (primitive == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015150 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15151 "failed to get primitive type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015152 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015153 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015154 facet = type->facets;
15155 do {
15156 if (xmlSchemaIsBuiltInTypeFacet(primitive, facet->type) == 0) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015157 ok = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015158 xmlSchemaPIllegalFacetAtomicErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015159 XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015160 type, primitive, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015161 }
15162 facet = facet->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015163 } while (facet != NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015164 if (ok == 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015165 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015166 }
15167 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015168 * SPEC (1.3.2) "If there is a facet of the same kind in the {facets}
15169 * of the {base type definition} (call this BF),then the DF's {value}
15170 * must be a valid restriction of BF's {value} as defined in
15171 * [XML Schemas: Datatypes]."
15172 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015173 * NOTE (1.3.2) Facet derivation constraints are currently handled in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015174 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015175 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015176 } else if (WXS_IS_LIST(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015177 xmlSchemaTypePtr itemType = NULL;
15178
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015179 itemType = type->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015180 if ((itemType == NULL) || (! WXS_IS_SIMPLE(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015181 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15182 "failed to evaluate the item type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015183 return (-1);
15184 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015185 if (WXS_IS_TYPE_NOT_FIXED(itemType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015186 xmlSchemaTypeFixup(itemType, ACTXT_CAST pctxt);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015187 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015188 * 2.1 The {item type definition} must have a {variety} of atomic or
15189 * union (in which case all the {member type definitions}
Daniel Veillard01fa6152004-06-29 17:04:39 +000015190 * must be atomic).
15191 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015192 if ((! WXS_IS_ATOMIC(itemType)) &&
15193 (! WXS_IS_UNION(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015194 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015195 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015196 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015197 "The item type '%s' does not have a variety of atomic or union",
15198 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015199 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015200 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015201 } else if (WXS_IS_UNION(itemType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015202 xmlSchemaTypeLinkPtr member;
15203
15204 member = itemType->memberTypes;
15205 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015206 if (! WXS_IS_ATOMIC(member->type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015207 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015208 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015209 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015210 "The item type is a union type, but the "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015211 "member type '%s' of this item type is not atomic",
15212 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015213 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015214 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
15215 }
15216 member = member->next;
15217 }
15218 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015219
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015220 if (WXS_IS_ANY_SIMPLE_TYPE(type->baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015221 xmlSchemaFacetPtr facet;
15222 /*
15223 * This is the case if we have: <simpleType><list ..
15224 */
15225 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015226 * 2.3.1
15227 * 2.3.1.1 The {final} of the {item type definition} must not
Daniel Veillard01fa6152004-06-29 17:04:39 +000015228 * contain list.
15229 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015230 if (xmlSchemaTypeFinalContains(itemType,
15231 XML_SCHEMAS_TYPE_FINAL_LIST)) {
15232 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015233 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015234 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015235 "The final of its item type '%s' must not contain 'list'",
15236 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015237 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015238 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1);
15239 }
15240 /*
15241 * 2.3.1.2 The {facets} must only contain the whiteSpace
15242 * facet component.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015243 * OPTIMIZE TODO: the S4S already disallows any facet
15244 * to be specified.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015245 */
15246 if (type->facets != NULL) {
15247 facet = type->facets;
15248 do {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015249 if (facet->type != XML_SCHEMA_FACET_WHITESPACE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015250 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015251 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015252 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015253 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2);
15254 }
15255 facet = facet->next;
15256 } while (facet != NULL);
15257 }
15258 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015259 * MAYBE TODO: (Hmm, not really) Datatypes states:
15260 * A ·list· datatype can be ·derived· from an ·atomic· datatype
15261 * whose ·lexical space· allows space (such as string or anyURI)or
15262 * a ·union· datatype any of whose {member type definitions}'s
Daniel Veillard01fa6152004-06-29 17:04:39 +000015263 * ·lexical space· allows space.
15264 */
15265 } else {
15266 /*
15267 * This is the case if we have: <simpleType><restriction ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015268 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015269 */
15270 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015271 * 2.3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015272 * 2.3.2.1 The {base type definition} must have a {variety} of list.
15273 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015274 if (! WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015275 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015276 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015277 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015278 "The base type '%s' must be a list type",
15279 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015280 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015281 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1);
15282 }
15283 /*
15284 * 2.3.2.2 The {final} of the {base type definition} must not
15285 * contain restriction.
15286 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015287 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015288 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015289 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015290 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015291 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015292 "The 'final' of the base type '%s' must not contain 'restriction'",
15293 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015294 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015295 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2);
15296 }
15297 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015298 * 2.3.2.3 The {item type definition} must be validly derived
Daniel Veillard01fa6152004-06-29 17:04:39 +000015299 * from the {base type definition}'s {item type definition} given
15300 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6).
15301 */
15302 {
15303 xmlSchemaTypePtr baseItemType;
15304
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015305 baseItemType = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015306 if ((baseItemType == NULL) || (! WXS_IS_SIMPLE(baseItemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015307 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15308 "failed to eval the item type of a base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015309 return (-1);
15310 }
15311 if ((itemType != baseItemType) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015312 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt, itemType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015313 baseItemType, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015314 xmlChar *strBIT = NULL, *strBT = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015315 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015316 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015317 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015318 "The item type '%s' is not validly derived from "
15319 "the item type '%s' of the base type '%s'",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015320 xmlSchemaGetComponentQName(&str, itemType),
15321 xmlSchemaGetComponentQName(&strBIT, baseItemType),
15322 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015323
15324 FREE_AND_NULL(str)
15325 FREE_AND_NULL(strBIT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015326 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015327 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3);
15328 }
15329 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015330
Daniel Veillard01fa6152004-06-29 17:04:39 +000015331 if (type->facets != NULL) {
15332 xmlSchemaFacetPtr facet;
15333 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015334 /*
15335 * 2.3.2.4 Only length, minLength, maxLength, whiteSpace, pattern
Daniel Veillard01fa6152004-06-29 17:04:39 +000015336 * and enumeration facet components are allowed among the {facets}.
15337 */
15338 facet = type->facets;
15339 do {
15340 switch (facet->type) {
15341 case XML_SCHEMA_FACET_LENGTH:
15342 case XML_SCHEMA_FACET_MINLENGTH:
15343 case XML_SCHEMA_FACET_MAXLENGTH:
15344 case XML_SCHEMA_FACET_WHITESPACE:
15345 /*
15346 * TODO: 2.5.1.2 List datatypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015347 * The value of ·whiteSpace· is fixed to the value collapse.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015348 */
15349 case XML_SCHEMA_FACET_PATTERN:
15350 case XML_SCHEMA_FACET_ENUMERATION:
15351 break;
15352 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015353 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015354 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015355 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015356 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015357 * We could return, but it's nicer to report all
Daniel Veillard01fa6152004-06-29 17:04:39 +000015358 * invalid facets.
15359 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015360 ok = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015361 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015362 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015363 facet = facet->next;
15364 } while (facet != NULL);
15365 if (ok == 0)
15366 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4);
15367 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015368 * SPEC (2.3.2.5) (same as 1.3.2)
15369 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015370 * NOTE (2.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015371 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015372 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015373 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015374 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015375 } else if (WXS_IS_UNION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015376 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015377 * 3.1 The {member type definitions} must all have {variety} of
Daniel Veillard01fa6152004-06-29 17:04:39 +000015378 * atomic or list.
15379 */
15380 xmlSchemaTypeLinkPtr member;
15381
15382 member = type->memberTypes;
15383 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015384 if (WXS_IS_TYPE_NOT_FIXED(member->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015385 xmlSchemaTypeFixup(member->type, ACTXT_CAST pctxt);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015386
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015387 if ((! WXS_IS_ATOMIC(member->type)) &&
15388 (! WXS_IS_LIST(member->type))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015389 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015390 XML_SCHEMAP_COS_ST_RESTRICTS_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015391 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015392 "The member type '%s' is neither an atomic, nor a list type",
15393 xmlSchemaGetComponentQName(&str, member->type));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015394 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015395 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_1);
15396 }
15397 member = member->next;
15398 }
15399 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015400 * 3.3.1 If the {base type definition} is the ·simple ur-type
15401 * definition·
Daniel Veillard01fa6152004-06-29 17:04:39 +000015402 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015403 if (type->baseType->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015404 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015405 * 3.3.1.1 All of the {member type definitions} must have a
Daniel Veillard01fa6152004-06-29 17:04:39 +000015406 * {final} which does not contain union.
15407 */
15408 member = type->memberTypes;
15409 while (member != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015410 if (xmlSchemaTypeFinalContains(member->type,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015411 XML_SCHEMAS_TYPE_FINAL_UNION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015412 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015413 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015414 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015415 "The 'final' of member type '%s' contains 'union'",
15416 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015417 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015418 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1);
15419 }
15420 member = member->next;
15421 }
15422 /*
15423 * 3.3.1.2 The {facets} must be empty.
15424 */
15425 if (type->facetSet != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015426 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015427 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015428 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015429 "No facets allowed", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015430 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2);
15431 }
15432 } else {
15433 /*
15434 * 3.3.2.1 The {base type definition} must have a {variety} of union.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015435 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015436 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015437 if (! WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015438 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015439 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015440 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015441 "The base type '%s' is not a union type",
15442 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015443 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015444 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1);
15445 }
15446 /*
15447 * 3.3.2.2 The {final} of the {base type definition} must not contain restriction.
15448 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015449 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015450 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015451 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015452 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015453 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015454 "The 'final' of its base type '%s' must not contain 'restriction'",
15455 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015456 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015457 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2);
15458 }
15459 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015460 * 3.3.2.3 The {member type definitions}, in order, must be validly
15461 * derived from the corresponding type definitions in the {base
15462 * type definition}'s {member type definitions} given the empty set,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015463 * as defined in Type Derivation OK (Simple) (§3.14.6).
15464 */
15465 {
15466 xmlSchemaTypeLinkPtr baseMember;
15467
15468 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015469 * OPTIMIZE: if the type is restricting, it has no local defined
15470 * member types and inherits the member types of the base type;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015471 * thus a check for equality can be skipped.
15472 */
15473 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015474 * Even worse: I cannot see a scenario where a restricting
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015475 * union simple type can have other member types as the member
Daniel Veillard01fa6152004-06-29 17:04:39 +000015476 * types of it's base type. This check seems not necessary with
15477 * respect to the derivation process in libxml2.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015478 * But necessary if constructing types with an API.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015479 */
15480 if (type->memberTypes != NULL) {
15481 member = type->memberTypes;
15482 baseMember = xmlSchemaGetUnionSimpleTypeMemberTypes(type->baseType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015483 if ((member == NULL) && (baseMember != NULL)) {
15484 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15485 "different number of member types in base");
15486 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015487 while (member != NULL) {
15488 if (baseMember == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015489 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15490 "different number of member types in base");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015491 }
15492 if ((member->type != baseMember->type) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015493 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015494 member->type, baseMember->type, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015495 xmlChar *strBMT = NULL, *strBT = NULL;
15496
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015497 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015498 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015499 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015500 "The member type %s is not validly "
15501 "derived from its corresponding member "
15502 "type %s of the base type %s",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015503 xmlSchemaGetComponentQName(&str, member->type),
15504 xmlSchemaGetComponentQName(&strBMT, baseMember->type),
15505 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015506 FREE_AND_NULL(str)
15507 FREE_AND_NULL(strBMT)
15508 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015509 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015510 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015511 member = member->next;
15512 baseMember = baseMember->next;
15513 }
15514 }
15515 }
15516 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015517 * 3.3.2.4 Only pattern and enumeration facet components are
Daniel Veillard01fa6152004-06-29 17:04:39 +000015518 * allowed among the {facets}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015519 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015520 if (type->facets != NULL) {
15521 xmlSchemaFacetPtr facet;
15522 int ok = 1;
15523
15524 facet = type->facets;
15525 do {
15526 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
15527 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015528 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015529 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015530 type, facet);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015531 ok = 0;
15532 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015533 facet = facet->next;
15534 } while (facet != NULL);
15535 if (ok == 0)
15536 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015537
Daniel Veillard01fa6152004-06-29 17:04:39 +000015538 }
15539 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015540 * SPEC (3.3.2.5) (same as 1.3.2)
15541 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015542 * NOTE (3.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015543 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015544 */
15545 }
15546 }
15547
15548 return (0);
15549}
15550
15551/**
15552 * xmlSchemaCheckSRCSimpleType:
15553 * @ctxt: the schema parser context
15554 * @type: the simple type definition
15555 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015556 * Checks crc-simple-type constraints.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015557 *
15558 * Returns 0 if the constraints are satisfied,
15559 * if not a positive error code and -1 on internal
15560 * errors.
15561 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015562#if 0
Daniel Veillard01fa6152004-06-29 17:04:39 +000015563static int
15564xmlSchemaCheckSRCSimpleType(xmlSchemaParserCtxtPtr ctxt,
15565 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015566{
15567 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015568 * src-simple-type.1 The corresponding simple type definition, if any,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015569 * must satisfy the conditions set out in Constraints on Simple Type
15570 * Definition Schema Components (§3.14.6).
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015571 */
15572 if (WXS_IS_RESTRICTION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015573 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015574 * src-simple-type.2 "If the <restriction> alternative is chosen,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015575 * either it must have a base [attribute] or a <simpleType> among its
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015576 * [children], but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015577 * NOTE: This is checked in the parse function of <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015578 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015579 /*
15580 *
15581 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015582 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015583 /* src-simple-type.3 "If the <list> alternative is chosen, either it must have
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015584 * an itemType [attribute] or a <simpleType> among its [children],
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015585 * but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015586 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015587 * NOTE: This is checked in the parse function of <list>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015588 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015589 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015590 /*
15591 * src-simple-type.4 is checked in xmlSchemaCheckUnionTypeDefCircular().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015592 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015593 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015594 return (0);
15595}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015596#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +000015597
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015598static int
15599xmlSchemaCreateVCtxtOnPCtxt(xmlSchemaParserCtxtPtr ctxt)
15600{
15601 if (ctxt->vctxt == NULL) {
15602 ctxt->vctxt = xmlSchemaNewValidCtxt(NULL);
15603 if (ctxt->vctxt == NULL) {
15604 xmlSchemaPErr(ctxt, NULL,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000015605 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015606 "Internal error: xmlSchemaCreateVCtxtOnPCtxt, "
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015607 "failed to create a temp. validation context.\n",
15608 NULL, NULL);
15609 return (-1);
15610 }
15611 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015612 xmlSchemaSetValidErrors(ctxt->vctxt,
15613 ctxt->error, ctxt->warning, ctxt->userData);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015614 }
15615 return (0);
15616}
15617
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015618static int
15619xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
15620 xmlNodePtr node,
15621 xmlSchemaTypePtr type,
15622 const xmlChar *value,
15623 xmlSchemaValPtr *retVal,
15624 int fireErrors,
15625 int normalize,
15626 int isNormalized);
15627
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015628/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015629 * xmlSchemaParseCheckCOSValidDefault:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015630 * @pctxt: the schema parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015631 * @type: the simple type definition
15632 * @value: the default value
15633 * @node: an optional node (the holder of the value)
15634 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015635 * Schema Component Constraint: Element Default Valid (Immediate)
15636 * (cos-valid-default)
15637 * This will be used by the parser only. For the validator there's
15638 * an other version.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015639 *
15640 * Returns 0 if the constraints are satisfied,
15641 * if not, a positive error code and -1 on internal
15642 * errors.
15643 */
15644static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015645xmlSchemaParseCheckCOSValidDefault(xmlSchemaParserCtxtPtr pctxt,
15646 xmlNodePtr node,
15647 xmlSchemaTypePtr type,
15648 const xmlChar *value,
15649 xmlSchemaValPtr *val)
15650{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015651 int ret = 0;
15652
15653 /*
15654 * cos-valid-default:
15655 * Schema Component Constraint: Element Default Valid (Immediate)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015656 * For a string to be a valid default with respect to a type
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015657 * definition the appropriate case among the following must be true:
15658 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015659 if WXS_IS_COMPLEX(type) {
William M. Brack2f2a6632004-08-20 23:09:47 +000015660 /*
15661 * Complex type.
15662 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015663 * SPEC (2.1) "its {content type} must be a simple type definition
15664 * or mixed."
15665 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015666 * type}'s particle must be ·emptiable· as defined by
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015667 * Particle Emptiable (§3.9.6)."
William M. Brack2f2a6632004-08-20 23:09:47 +000015668 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015669 if ((! WXS_HAS_SIMPLE_CONTENT(type)) &&
15670 ((! WXS_HAS_MIXED_CONTENT(type)) || (! WXS_EMPTIABLE(type)))) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015671 /* NOTE that this covers (2.2.2) as well. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015672 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015673 XML_SCHEMAP_COS_VALID_DEFAULT_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015674 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015675 "For a string to be a valid default, the type definition "
15676 "must be a simple type or a complex type with mixed content "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015677 "and a particle emptiable", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015678 return(XML_SCHEMAP_COS_VALID_DEFAULT_2_1);
15679 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015680 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015681 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015682 * 1 If the type definition is a simple type definition, then the string
15683 * must be ·valid· with respect to that definition as defined by String
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015684 * Valid (§3.14.4).
15685 *
15686 * AND
15687 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015688 * 2.2.1 If the {content type} is a simple type definition, then the
15689 * string must be ·valid· with respect to that simple type definition
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015690 * as defined by String Valid (§3.14.4).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015691 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015692 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015693 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015694 type, value, val, 1, 1, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015695 else if (WXS_HAS_SIMPLE_CONTENT(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015696 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015697 type->contentTypeDef, value, val, 1, 1, 0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015698 else
15699 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015700
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015701 if (ret < 0) {
15702 PERROR_INT("xmlSchemaParseCheckCOSValidDefault",
15703 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015704 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015705
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015706 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000015707}
15708
15709/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015710 * xmlSchemaCheckCTPropsCorrect:
William M. Brack2f2a6632004-08-20 23:09:47 +000015711 * @ctxt: the schema parser context
15712 * @type: the complex type definition
15713 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015714 *.(4.6) Constraints on Complex Type Definition Schema Components
15715 * Schema Component Constraint:
15716 * Complex Type Definition Properties Correct (ct-props-correct)
15717 * STATUS: (seems) complete
William M. Brack2f2a6632004-08-20 23:09:47 +000015718 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015719 * Returns 0 if the constraints are satisfied, a positive
15720 * error code if not and -1 if an internal error occured.
William M. Brack2f2a6632004-08-20 23:09:47 +000015721 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015722static int
15723xmlSchemaCheckCTPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
15724 xmlSchemaTypePtr type)
William M. Brack2f2a6632004-08-20 23:09:47 +000015725{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015726 /*
15727 * TODO: Correct the error code; XML_SCHEMAP_SRC_CT_1 is used temporarily.
15728 *
15729 * SPEC (1) "The values of the properties of a complex type definition must
15730 * be as described in the property tableau in The Complex Type Definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015731 * Schema Component (§3.4.1), modulo the impact of Missing
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015732 * Sub-components (§5.3)."
15733 */
15734 if ((type->baseType != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015735 (WXS_IS_SIMPLE(type->baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015736 (WXS_IS_EXTENSION(type) == 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015737 /*
15738 * SPEC (2) "If the {base type definition} is a simple type definition,
15739 * the {derivation method} must be extension."
15740 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015741 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015742 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015743 NULL, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015744 "If the base type is a simple type, the derivation method must be "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015745 "'extension'", NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015746 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015747 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015748 /*
15749 * SPEC (3) "Circular definitions are disallowed, except for the ·ur-type
15750 * definition·. That is, it must be possible to reach the ·ur-type
15751 * definition by repeatedly following the {base type definition}."
15752 *
15753 * NOTE (3) is done in xmlSchemaCheckTypeDefCircular().
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015754 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015755 /*
15756 * NOTE that (4) and (5) need the following:
15757 * - attribute uses need to be already inherited (apply attr. prohibitions)
15758 * - attribute group references need to be expanded already
15759 * - simple types need to be typefixed already
15760 */
15761 if (type->attrUses &&
15762 (((xmlSchemaItemListPtr) type->attrUses)->nbItems > 1))
15763 {
15764 xmlSchemaItemListPtr uses = (xmlSchemaItemListPtr) type->attrUses;
15765 xmlSchemaAttributeUsePtr use, tmp;
15766 int i, j, hasId = 0;
15767
15768 for (i = uses->nbItems -1; i >= 0; i--) {
15769 use = uses->items[i];
15770
15771 /*
15772 * SPEC ct-props-correct
15773 * (4) "Two distinct attribute declarations in the
15774 * {attribute uses} must not have identical {name}s and
15775 * {target namespace}s."
15776 */
15777 if (i > 0) {
15778 for (j = i -1; j >= 0; j--) {
15779 tmp = uses->items[j];
15780 if ((WXS_ATTRUSE_DECL_NAME(use) ==
15781 WXS_ATTRUSE_DECL_NAME(tmp)) &&
15782 (WXS_ATTRUSE_DECL_TNS(use) ==
15783 WXS_ATTRUSE_DECL_TNS(tmp)))
15784 {
15785 xmlChar *str = NULL;
15786
15787 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15788 XML_SCHEMAP_AG_PROPS_CORRECT,
15789 NULL, WXS_BASIC_CAST type,
15790 "Duplicate %s",
15791 xmlSchemaGetComponentDesignation(&str, use),
15792 NULL);
15793 FREE_AND_NULL(str);
15794 /*
15795 * Remove the duplicate.
15796 */
15797 if (xmlSchemaItemListRemove(uses, i) == -1)
15798 goto exit_failure;
15799 goto next_use;
15800 }
15801 }
15802 }
15803 /*
15804 * SPEC ct-props-correct
15805 * (5) "Two distinct attribute declarations in the
15806 * {attribute uses} must not have {type definition}s which
15807 * are or are derived from ID."
15808 */
15809 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
15810 if (xmlSchemaIsDerivedFromBuiltInType(
15811 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
15812 {
15813 if (hasId) {
15814 xmlChar *str = NULL;
15815
15816 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15817 XML_SCHEMAP_AG_PROPS_CORRECT,
15818 NULL, WXS_BASIC_CAST type,
15819 "There must not exist more than one attribute "
15820 "declaration of type 'xs:ID' "
15821 "(or derived from 'xs:ID'). The %s violates this "
15822 "constraint",
15823 xmlSchemaGetComponentDesignation(&str, use),
15824 NULL);
15825 FREE_AND_NULL(str);
15826 if (xmlSchemaItemListRemove(uses, i) == -1)
15827 goto exit_failure;
15828 }
15829
15830 hasId = 1;
15831 }
15832 }
15833next_use: {}
15834 }
15835 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015836 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015837exit_failure:
15838 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +000015839}
15840
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015841static int
15842xmlSchemaAreEqualTypes(xmlSchemaTypePtr typeA,
15843 xmlSchemaTypePtr typeB)
15844{
15845 /*
15846 * TODO: This should implement component-identity
15847 * in the future.
15848 */
15849 if ((typeA == NULL) || (typeB == NULL))
15850 return (0);
15851 return (typeA == typeB);
15852}
15853
15854/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015855 * xmlSchemaCheckCOSCTDerivedOK:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015856 * @ctxt: the schema parser context
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015857 * @type: the to-be derived complex type definition
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015858 * @baseType: the base complex type definition
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015859 * @set: the given set
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015860 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015861 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015862 * Type Derivation OK (Complex) (cos-ct-derived-ok)
15863 *
15864 * STATUS: completed
15865 *
15866 * Returns 0 if the constraints are satisfied, or 1
15867 * if not.
15868 */
15869static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015870xmlSchemaCheckCOSCTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015871 xmlSchemaTypePtr type,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015872 xmlSchemaTypePtr baseType,
15873 int set)
15874{
15875 int equal = xmlSchemaAreEqualTypes(type, baseType);
15876 /* TODO: Error codes. */
15877 /*
15878 * SPEC "For a complex type definition (call it D, for derived)
15879 * to be validly derived from a type definition (call this
15880 * B, for base) given a subset of {extension, restriction}
15881 * all of the following must be true:"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015882 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015883 if (! equal) {
15884 /*
15885 * SPEC (1) "If B and D are not the same type definition, then the
15886 * {derivation method} of D must not be in the subset."
15887 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015888 if (((set & SUBSET_EXTENSION) && (WXS_IS_EXTENSION(type))) ||
15889 ((set & SUBSET_RESTRICTION) && (WXS_IS_RESTRICTION(type))))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015890 return (1);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015891 } else {
15892 /*
15893 * SPEC (2.1) "B and D must be the same type definition."
15894 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015895 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015896 }
15897 /*
15898 * SPEC (2.2) "B must be D's {base type definition}."
15899 */
15900 if (type->baseType == baseType)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015901 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015902 /*
15903 * SPEC (2.3.1) "D's {base type definition} must not be the ·ur-type
15904 * definition·."
15905 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015906 if (WXS_IS_ANYTYPE(type->baseType))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015907 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015908
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015909 if (WXS_IS_COMPLEX(type->baseType)) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015910 /*
15911 * SPEC (2.3.2.1) "If D's {base type definition} is complex, then it
15912 * must be validly derived from B given the subset as defined by this
15913 * constraint."
15914 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015915 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015916 baseType, set));
15917 } else {
15918 /*
15919 * SPEC (2.3.2.2) "If D's {base type definition} is simple, then it
15920 * must be validly derived from B given the subset as defined in Type
15921 * Derivation OK (Simple) (§3.14.6).
15922 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015923 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015924 baseType, set));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015925 }
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015926}
15927
15928/**
15929 * xmlSchemaCheckCOSDerivedOK:
15930 * @type: the derived simple type definition
15931 * @baseType: the base type definition
15932 *
15933 * Calls:
15934 * Type Derivation OK (Simple) AND Type Derivation OK (Complex)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015935 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015936 * Checks wheter @type can be validly derived from @baseType.
15937 *
15938 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015939 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015940static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015941xmlSchemaCheckCOSDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015942 xmlSchemaTypePtr type,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015943 xmlSchemaTypePtr baseType,
15944 int set)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015945{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015946 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015947 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015948 else
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015949 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015950}
15951
William M. Brack2f2a6632004-08-20 23:09:47 +000015952/**
15953 * xmlSchemaCheckCOSCTExtends:
15954 * @ctxt: the schema parser context
15955 * @type: the complex type definition
15956 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015957 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015958 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015959 * Derivation Valid (Extension) (cos-ct-extends)
15960 *
15961 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015962 * missing:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015963 * (1.5)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015964 * (1.4.3.2.2.2) "Particle Valid (Extension)"
William M. Brack2f2a6632004-08-20 23:09:47 +000015965 *
15966 * Returns 0 if the constraints are satisfied, a positive
15967 * error code if not and -1 if an internal error occured.
15968 */
15969static int
15970xmlSchemaCheckCOSCTExtends(xmlSchemaParserCtxtPtr ctxt,
15971 xmlSchemaTypePtr type)
15972{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015973 xmlSchemaTypePtr base = type->baseType;
15974 /*
15975 * TODO: Correct the error code; XML_SCHEMAP_COS_CT_EXTENDS_1_1 is used
15976 * temporarily only.
William M. Brack2f2a6632004-08-20 23:09:47 +000015977 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015978 /*
15979 * SPEC (1) "If the {base type definition} is a complex type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015980 * then all of the following must be true:"
15981 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015982 if (WXS_IS_COMPLEX(base)) {
William M. Brack2f2a6632004-08-20 23:09:47 +000015983 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015984 * SPEC (1.1) "The {final} of the {base type definition} must not
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015985 * contain extension."
William M. Brack2f2a6632004-08-20 23:09:47 +000015986 */
15987 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
15988 xmlSchemaPCustomErr(ctxt,
15989 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015990 WXS_BASIC_CAST type, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000015991 "The 'final' of the base type definition "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015992 "contains 'extension'", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000015993 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
15994 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015995
15996 /*
15997 * ATTENTION: The constrains (1.2) and (1.3) are not applied,
15998 * since they are automatically satisfied through the
15999 * inheriting mechanism.
16000 * Note that even if redefining components, the inheriting mechanism
16001 * is used.
16002 */
16003#if 0
William M. Brack2f2a6632004-08-20 23:09:47 +000016004 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016005 * SPEC (1.2) "Its {attribute uses} must be a subset of the {attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016006 * uses}
16007 * of the complex type definition itself, that is, for every attribute
16008 * use in the {attribute uses} of the {base type definition}, there
16009 * must be an attribute use in the {attribute uses} of the complex
16010 * type definition itself whose {attribute declaration} has the same
16011 * {name}, {target namespace} and {type definition} as its attribute
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016012 * declaration"
William M. Brack2f2a6632004-08-20 23:09:47 +000016013 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016014 if (base->attrUses != NULL) {
16015 int i, j, found;
16016 xmlSchemaAttributeUsePtr use, buse;
William M. Brack2f2a6632004-08-20 23:09:47 +000016017
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016018 for (i = 0; i < (WXS_LIST_CAST base->attrUses)->nbItems; i ++) {
16019 buse = (WXS_LIST_CAST base->attrUses)->items[i];
16020 found = 0;
16021 if (type->attrUses != NULL) {
16022 use = (WXS_LIST_CAST type->attrUses)->items[j];
16023 for (j = 0; j < (WXS_LIST_CAST type->attrUses)->nbItems; j ++)
16024 {
16025 if ((WXS_ATTRUSE_DECL_NAME(use) ==
16026 WXS_ATTRUSE_DECL_NAME(buse)) &&
16027 (WXS_ATTRUSE_DECL_TNS(use) ==
16028 WXS_ATTRUSE_DECL_TNS(buse)) &&
16029 (WXS_ATTRUSE_TYPEDEF(use) ==
16030 WXS_ATTRUSE_TYPEDEF(buse))
16031 {
16032 found = 1;
16033 break;
16034 }
16035 }
16036 }
16037 if (! found) {
16038 xmlChar *str = NULL;
16039
16040 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16041 XML_SCHEMAP_COS_CT_EXTENDS_1_2,
16042 NULL, WXS_BASIC_CAST type,
16043 /*
16044 * TODO: The report does not indicate that also the
16045 * type needs to be the same.
16046 */
16047 "This type is missing a matching correspondent "
16048 "for its {base type}'s %s in its {attribute uses}",
16049 xmlSchemaGetComponentDesignation(&str,
16050 buse->children),
16051 NULL);
16052 FREE_AND_NULL(str)
16053 }
16054 }
16055 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016056 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016057 * SPEC (1.3) "If it has an {attribute wildcard}, the complex type
16058 * definition must also have one, and the base type definition's
16059 * {attribute wildcard}'s {namespace constraint} must be a subset
16060 * of the complex type definition's {attribute wildcard}'s {namespace
16061 * constraint}, as defined by Wildcard Subset (§3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016062 */
16063
16064 /*
16065 * MAYBE TODO: Enable if ever needed. But this will be needed only
16066 * if created the type via a schema construction API.
16067 */
16068 if (base->attributeWildcard != NULL) {
16069 if (type->attributeWilcard == NULL) {
16070 xmlChar *str = NULL;
16071
16072 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16073 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16074 NULL, type,
16075 "The base %s has an attribute wildcard, "
16076 "but this type is missing an attribute wildcard",
16077 xmlSchemaGetComponentDesignation(&str, base));
16078 FREE_AND_NULL(str)
16079
16080 } else if (xmlSchemaCheckCOSNSSubset(
16081 base->attributeWildcard, type->attributeWildcard))
16082 {
16083 xmlChar *str = NULL;
16084
16085 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16086 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16087 NULL, type,
16088 "The attribute wildcard is not a valid "
16089 "superset of the one in the base %s",
16090 xmlSchemaGetComponentDesignation(&str, base));
16091 FREE_AND_NULL(str)
16092 }
16093 }
16094#endif
16095 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016096 * SPEC (1.4) "One of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016097 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016098 if ((type->contentTypeDef != NULL) &&
16099 (type->contentTypeDef == base->contentTypeDef)) {
16100 /*
16101 * SPEC (1.4.1) "The {content type} of the {base type definition}
16102 * and the {content type} of the complex type definition itself
16103 * must be the same simple type definition"
16104 * PASS
16105 */
16106 } else if ((type->contentType == XML_SCHEMA_CONTENT_EMPTY) &&
16107 (base->contentType == XML_SCHEMA_CONTENT_EMPTY) ) {
16108 /*
16109 * SPEC (1.4.2) "The {content type} of both the {base type
16110 * definition} and the complex type definition itself must
16111 * be empty."
16112 * PASS
16113 */
16114 } else {
16115 /*
16116 * SPEC (1.4.3) "All of the following must be true:"
16117 */
16118 if (type->subtypes == NULL) {
16119 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016120 * SPEC 1.4.3.1 The {content type} of the complex type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016121 * definition itself must specify a particle.
16122 */
16123 xmlSchemaPCustomErr(ctxt,
16124 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016125 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016126 "The content type must specify a particle", NULL);
16127 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16128 }
16129 /*
16130 * SPEC (1.4.3.2) "One of the following must be true:"
16131 */
16132 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16133 /*
16134 * SPEC (1.4.3.2.1) "The {content type} of the {base type
16135 * definition} must be empty.
16136 * PASS
16137 */
16138 } else {
16139 /*
16140 * SPEC (1.4.3.2.2) "All of the following must be true:"
16141 */
16142 if ((type->contentType != base->contentType) ||
16143 ((type->contentType != XML_SCHEMA_CONTENT_MIXED) &&
16144 (type->contentType != XML_SCHEMA_CONTENT_ELEMENTS))) {
16145 /*
16146 * SPEC (1.4.3.2.2.1) "Both {content type}s must be mixed
16147 * or both must be element-only."
16148 */
16149 xmlSchemaPCustomErr(ctxt,
16150 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016151 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016152 "The content type of both, the type and its base "
16153 "type, must either 'mixed' or 'element-only'", NULL);
16154 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016155 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016156 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016157 * URGENT TODO SPEC (1.4.3.2.2.2) "The particle of the
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016158 * complex type definition must be a ·valid extension·
16159 * of the {base type definition}'s particle, as defined
16160 * in Particle Valid (Extension) (§3.9.6)."
16161 *
16162 * NOTE that we won't check "Particle Valid (Extension)",
16163 * since it is ensured by the derivation process in
16164 * xmlSchemaTypeFixup(). We need to implement this when heading
16165 * for a construction API
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016166 * TODO: !! This is needed to be checked if redefining a type !!
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016167 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016168 }
16169 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016170 * URGENT TODO (1.5)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016171 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016172 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016173 } else {
16174 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016175 * SPEC (2) "If the {base type definition} is a simple type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016176 * then all of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016177 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016178 if (type->contentTypeDef != base) {
16179 /*
16180 * SPEC (2.1) "The {content type} must be the same simple type
16181 * definition."
16182 */
16183 xmlSchemaPCustomErr(ctxt,
16184 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016185 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016186 "The content type must be the simple base type", NULL);
16187 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16188 }
16189 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16190 /*
16191 * SPEC (2.2) "The {final} of the {base type definition} must not
16192 * contain extension"
16193 * NOTE that this is the same as (1.1).
16194 */
16195 xmlSchemaPCustomErr(ctxt,
16196 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016197 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016198 "The 'final' of the base type definition "
16199 "contains 'extension'", NULL);
16200 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016201 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016202 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016203 return (0);
William M. Brack2f2a6632004-08-20 23:09:47 +000016204}
16205
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016206/**
16207 * xmlSchemaCheckDerivationOKRestriction:
16208 * @ctxt: the schema parser context
16209 * @type: the complex type definition
16210 *
16211 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016212 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016213 * Derivation Valid (Restriction, Complex) (derivation-ok-restriction)
16214 *
16215 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016216 * missing:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016217 * (5.4.2) ???
16218 *
16219 * ATTENTION:
16220 * In XML Schema 1.1 this will be:
16221 * Validation Rule: Checking complex type subsumption
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016222 *
16223 * Returns 0 if the constraints are satisfied, a positive
16224 * error code if not and -1 if an internal error occured.
16225 */
16226static int
16227xmlSchemaCheckDerivationOKRestriction(xmlSchemaParserCtxtPtr ctxt,
16228 xmlSchemaTypePtr type)
16229{
16230 xmlSchemaTypePtr base;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016231
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016232 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016233 * TODO: Correct the error code; XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1 is used
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016234 * temporarily only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016235 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016236 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016237 if (! WXS_IS_COMPLEX(base)) {
16238 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16239 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16240 type->node, WXS_BASIC_CAST type,
16241 "The base type must be a complex type", NULL, NULL);
16242 return(ctxt->err);
16243 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016244 if (base->flags & XML_SCHEMAS_TYPE_FINAL_RESTRICTION) {
16245 /*
16246 * SPEC (1) "The {base type definition} must be a complex type
16247 * definition whose {final} does not contain restriction."
16248 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016249 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16250 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16251 type->node, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016252 "The 'final' of the base type definition "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016253 "contains 'restriction'", NULL, NULL);
16254 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016255 }
16256 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016257 * SPEC (2), (3) and (4)
16258 * Those are handled in a separate function, since the
16259 * same constraints are needed for redefinition of
16260 * attribute groups as well.
16261 */
16262 if (xmlSchemaCheckDerivationOKRestriction2to4(ctxt,
16263 XML_SCHEMA_ACTION_DERIVE,
16264 WXS_BASIC_CAST type, WXS_BASIC_CAST base,
16265 type->attrUses, base->attrUses,
16266 type->attributeWildcard,
16267 base->attributeWildcard) == -1)
16268 {
16269 return(-1);
16270 }
16271 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016272 * SPEC (5) "One of the following must be true:"
16273 */
16274 if (base->builtInType == XML_SCHEMAS_ANYTYPE) {
16275 /*
16276 * SPEC (5.1) "The {base type definition} must be the
16277 * ·ur-type definition·."
16278 * PASS
16279 */
16280 } else if ((type->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16281 (type->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16282 /*
16283 * SPEC (5.2.1) "The {content type} of the complex type definition
16284 * must be a simple type definition"
16285 *
16286 * SPEC (5.2.2) "One of the following must be true:"
16287 */
16288 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016289 (base->contentType == XML_SCHEMA_CONTENT_BASIC))
16290 {
16291 int err;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016292 /*
16293 * SPEC (5.2.2.1) "The {content type} of the {base type
16294 * definition} must be a simple type definition from which
16295 * the {content type} is validly derived given the empty
16296 * set as defined in Type Derivation OK (Simple) (§3.14.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016297 *
16298 * ATTENTION TODO: This seems not needed if the type implicitely
16299 * derived from the base type.
16300 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016301 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016302 err = xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST ctxt,
16303 type->contentTypeDef, base->contentTypeDef, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016304 if (err != 0) {
16305 xmlChar *strA = NULL, *strB = NULL;
16306
16307 if (err == -1)
16308 return(-1);
16309 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16310 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16311 NULL, WXS_BASIC_CAST type,
16312 "The {content type} %s is not validly derived from the "
16313 "base type's {content type} %s",
16314 xmlSchemaGetComponentDesignation(&strA,
16315 type->contentTypeDef),
16316 xmlSchemaGetComponentDesignation(&strB,
16317 base->contentTypeDef));
16318 FREE_AND_NULL(strA);
16319 FREE_AND_NULL(strB);
16320 return(ctxt->err);
16321 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016322 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
16323 (xmlSchemaIsParticleEmptiable(
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016324 (xmlSchemaParticlePtr) base->subtypes))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016325 /*
16326 * SPEC (5.2.2.2) "The {base type definition} must be mixed
16327 * and have a particle which is ·emptiable· as defined in
16328 * Particle Emptiable (§3.9.6)."
16329 * PASS
16330 */
16331 } else {
16332 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016333 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16334 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016335 "The content type of the base type must be either "
16336 "a simple type or 'mixed' and an emptiable particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016337 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016338 }
16339 } else if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16340 /*
16341 * SPEC (5.3.1) "The {content type} of the complex type itself must
16342 * be empty"
16343 */
16344 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16345 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016346 * SPEC (5.3.2.1) "The {content type} of the {base type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016347 * definition} must also be empty."
16348 * PASS
16349 */
16350 } else if (((base->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
16351 (base->contentType == XML_SCHEMA_CONTENT_MIXED)) &&
16352 xmlSchemaIsParticleEmptiable(
16353 (xmlSchemaParticlePtr) base->subtypes)) {
16354 /*
16355 * SPEC (5.3.2.2) "The {content type} of the {base type
16356 * definition} must be elementOnly or mixed and have a particle
16357 * which is ·emptiable· as defined in Particle Emptiable (§3.9.6)."
16358 * PASS
16359 */
16360 } else {
16361 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016362 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16363 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016364 "The content type of the base type must be either "
16365 "empty or 'mixed' (or 'elements-only') and an emptiable "
16366 "particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016367 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016368 }
16369 } else if ((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016370 WXS_HAS_MIXED_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016371 /*
16372 * SPEC (5.4.1.1) "The {content type} of the complex type definition
16373 * itself must be element-only"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016374 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016375 if (WXS_HAS_MIXED_CONTENT(type) && (! WXS_HAS_MIXED_CONTENT(base))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016376 /*
16377 * SPEC (5.4.1.2) "The {content type} of the complex type
16378 * definition itself and of the {base type definition} must be
16379 * mixed"
16380 */
16381 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016382 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16383 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016384 "If the content type is 'mixed', then the content type of the "
16385 "base type must also be 'mixed'", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016386 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016387 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016388 /*
16389 * SPEC (5.4.2) "The particle of the complex type definition itself
16390 * must be a ·valid restriction· of the particle of the {content
16391 * type} of the {base type definition} as defined in Particle Valid
16392 * (Restriction) (§3.9.6).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016393 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016394 * URGENT TODO: (5.4.2)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016395 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016396 } else {
16397 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016398 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16399 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016400 "The type is not a valid restriction of its base type", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016401 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016402 }
16403 return (0);
16404}
16405
16406/**
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016407 * xmlSchemaCheckCTComponent:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016408 * @ctxt: the schema parser context
16409 * @type: the complex type definition
16410 *
16411 * (3.4.6) Constraints on Complex Type Definition Schema Components
16412 *
16413 * Returns 0 if the constraints are satisfied, a positive
16414 * error code if not and -1 if an internal error occured.
16415 */
16416static int
16417xmlSchemaCheckCTComponent(xmlSchemaParserCtxtPtr ctxt,
16418 xmlSchemaTypePtr type)
16419{
16420 int ret;
16421 /*
16422 * Complex Type Definition Properties Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016423 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016424 ret = xmlSchemaCheckCTPropsCorrect(ctxt, type);
16425 if (ret != 0)
16426 return (ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016427 if (WXS_IS_EXTENSION(type))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016428 ret = xmlSchemaCheckCOSCTExtends(ctxt, type);
16429 else
16430 ret = xmlSchemaCheckDerivationOKRestriction(ctxt, type);
16431 return (ret);
16432}
16433
16434/**
16435 * xmlSchemaCheckSRCCT:
16436 * @ctxt: the schema parser context
16437 * @type: the complex type definition
16438 *
16439 * (3.4.3) Constraints on XML Representations of Complex Type Definitions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016440 * Schema Representation Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016441 * Complex Type Definition Representation OK (src-ct)
16442 *
16443 * Returns 0 if the constraints are satisfied, a positive
16444 * error code if not and -1 if an internal error occured.
16445 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016446static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016447xmlSchemaCheckSRCCT(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016448 xmlSchemaTypePtr type)
16449{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016450 xmlSchemaTypePtr base;
16451 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016452
16453 /*
16454 * TODO: Adjust the error codes here, as I used
16455 * XML_SCHEMAP_SRC_CT_1 only yet.
16456 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016457 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016458 if (! WXS_HAS_SIMPLE_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016459 /*
16460 * 1 If the <complexContent> alternative is chosen, the type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016461 * ·resolved· to by the ·actual value· of the base [attribute]
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016462 * must be a complex type definition;
16463 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016464 if (! WXS_IS_COMPLEX(base)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016465 xmlChar *str = NULL;
16466 xmlSchemaPCustomErr(ctxt,
16467 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016468 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016469 "If using <complexContent>, the base type is expected to be "
16470 "a complex type. The base type '%s' is a simple type",
16471 xmlSchemaFormatQName(&str, base->targetNamespace,
16472 base->name));
16473 FREE_AND_NULL(str)
16474 return (XML_SCHEMAP_SRC_CT_1);
16475 }
16476 } else {
16477 /*
16478 * SPEC
16479 * 2 If the <simpleContent> alternative is chosen, all of the
16480 * following must be true:
16481 * 2.1 The type definition ·resolved· to by the ·actual value· of the
16482 * base [attribute] must be one of the following:
16483 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016484 if (WXS_IS_SIMPLE(base)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016485 if (WXS_IS_EXTENSION(type) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016486 xmlChar *str = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016487 /*
16488 * 2.1.3 only if the <extension> alternative is also
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016489 * chosen, a simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016490 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016491 /* TODO: Change error code to ..._SRC_CT_2_1_3. */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016492 xmlSchemaPCustomErr(ctxt,
16493 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016494 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016495 "If using <simpleContent> and <restriction>, the base "
16496 "type must be a complex type. The base type '%s' is "
16497 "a simple type",
16498 xmlSchemaFormatQName(&str, base->targetNamespace,
16499 base->name));
16500 FREE_AND_NULL(str)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016501 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016502 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016503 } else {
16504 /* Base type is a complex type. */
16505 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16506 (base->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16507 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016508 * 2.1.1 a complex type definition whose {content type} is a
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016509 * simple type definition;
16510 * PASS
16511 */
16512 if (base->contentTypeDef == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016513 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016514 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016515 "Internal error: xmlSchemaCheckSRCCT, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016516 "'%s', base type has no content type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016517 type->name);
16518 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016519 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016520 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016521 (WXS_IS_RESTRICTION(type))) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016522
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016523 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016524 * 2.1.2 only if the <restriction> alternative is also
16525 * chosen, a complex type definition whose {content type}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016526 * is mixed and a particle emptiable.
16527 */
16528 if (! xmlSchemaIsParticleEmptiable(
16529 (xmlSchemaParticlePtr) base->subtypes)) {
16530 ret = XML_SCHEMAP_SRC_CT_1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016531 } else
16532 /*
16533 * Attention: at this point the <simpleType> child is in
16534 * ->contentTypeDef (put there during parsing).
16535 */
16536 if (type->contentTypeDef == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016537 xmlChar *str = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016538 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016539 * 2.2 If clause 2.1.2 above is satisfied, then there
16540 * must be a <simpleType> among the [children] of
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016541 * <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016542 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016543 /* TODO: Change error code to ..._SRC_CT_2_2. */
16544 xmlSchemaPCustomErr(ctxt,
16545 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016546 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016547 "A <simpleType> is expected among the children "
16548 "of <restriction>, if <simpleContent> is used and "
16549 "the base type '%s' is a complex type",
16550 xmlSchemaFormatQName(&str, base->targetNamespace,
16551 base->name));
16552 FREE_AND_NULL(str)
16553 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016554 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016555 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016556 ret = XML_SCHEMAP_SRC_CT_1;
16557 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016558 }
16559 if (ret > 0) {
16560 xmlChar *str = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016561 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016562 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 "If <simpleContent> and <restriction> is used, the "
16566 "base type must be a simple type or a complex type with "
16567 "mixed content and particle emptiable. The base type "
16568 "'%s' is none of those",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016569 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016570 base->name));
16571 } else {
16572 xmlSchemaPCustomErr(ctxt,
16573 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016574 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016575 "If <simpleContent> and <extension> is used, the "
16576 "base type must be a simple type. The base type '%s' "
16577 "is a complex type",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016578 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016579 base->name));
16580 }
16581 FREE_AND_NULL(str)
16582 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016583 }
16584 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016585 * SPEC (3) "The corresponding complex type definition component must
16586 * satisfy the conditions set out in Constraints on Complex Type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016587 * Definition Schema Components (§3.4.6);"
16588 * NOTE (3) will be done in xmlSchemaTypeFixup().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016589 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016590 /*
16591 * SPEC (4) If clause 2.2.1 or clause 2.2.2 in the correspondence specification
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016592 * above for {attribute wildcard} is satisfied, the intensional
16593 * intersection must be expressible, as defined in Attribute Wildcard
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016594 * Intersection (§3.10.6).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016595 * NOTE (4) is done in xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016596 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016597 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016598}
William M. Brack2f2a6632004-08-20 23:09:47 +000016599
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016600#ifdef ENABLE_PARTICLE_RESTRICTION
16601/**
16602 * xmlSchemaCheckParticleRangeOK:
16603 * @ctxt: the schema parser context
16604 * @type: the complex type definition
16605 *
16606 * (3.9.6) Constraints on Particle Schema Components
16607 * Schema Component Constraint:
16608 * Occurrence Range OK (range-ok)
16609 *
16610 * STATUS: complete
16611 *
16612 * Returns 0 if the constraints are satisfied, a positive
16613 * error code if not and -1 if an internal error occured.
16614 */
16615static int
16616xmlSchemaCheckParticleRangeOK(int rmin, int rmax,
16617 int bmin, int bmax)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016618{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016619 if (rmin < bmin)
16620 return (1);
16621 if ((bmax != UNBOUNDED) &&
16622 (rmax > bmax))
16623 return (1);
16624 return (0);
16625}
16626
16627/**
16628 * xmlSchemaCheckRCaseNameAndTypeOK:
16629 * @ctxt: the schema parser context
16630 * @r: the restricting element declaration particle
16631 * @b: the base element declaration particle
16632 *
16633 * (3.9.6) Constraints on Particle Schema Components
16634 * Schema Component Constraint:
16635 * Particle Restriction OK (Elt:Elt -- NameAndTypeOK)
16636 * (rcase-NameAndTypeOK)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016637 *
16638 * STATUS:
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016639 * MISSING (3.2.3)
16640 * CLARIFY: (3.2.2)
16641 *
16642 * Returns 0 if the constraints are satisfied, a positive
16643 * error code if not and -1 if an internal error occured.
16644 */
16645static int
16646xmlSchemaCheckRCaseNameAndTypeOK(xmlSchemaParserCtxtPtr ctxt,
16647 xmlSchemaParticlePtr r,
16648 xmlSchemaParticlePtr b)
16649{
16650 xmlSchemaElementPtr elemR, elemB;
16651
16652 /* TODO: Error codes (rcase-NameAndTypeOK). */
16653 elemR = (xmlSchemaElementPtr) r->children;
16654 elemB = (xmlSchemaElementPtr) b->children;
16655 /*
16656 * SPEC (1) "The declarations' {name}s and {target namespace}s are
16657 * the same."
16658 */
16659 if ((elemR != elemB) &&
16660 ((! xmlStrEqual(elemR->name, elemB->name)) ||
16661 (! xmlStrEqual(elemR->targetNamespace, elemB->targetNamespace))))
16662 return (1);
16663 /*
16664 * SPEC (2) "R's occurrence range is a valid restriction of B's
16665 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16666 */
16667 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16668 b->minOccurs, b->maxOccurs) != 0)
16669 return (1);
16670 /*
16671 * SPEC (3.1) "Both B's declaration's {scope} and R's declaration's
16672 * {scope} are global."
16673 */
16674 if (elemR == elemB)
16675 return (0);
16676 /*
16677 * SPEC (3.2.1) "Either B's {nillable} is true or R's {nillable} is false."
16678 */
16679 if (((elemB->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) &&
16680 (elemR->flags & XML_SCHEMAS_ELEM_NILLABLE))
16681 return (1);
16682 /*
16683 * SPEC (3.2.2) "either B's declaration's {value constraint} is absent,
16684 * or is not fixed, or R's declaration's {value constraint} is fixed
16685 * with the same value."
16686 */
16687 if ((elemB->value != NULL) && (elemB->flags & XML_SCHEMAS_ELEM_FIXED) &&
16688 ((elemR->value == NULL) ||
16689 ((elemR->flags & XML_SCHEMAS_ELEM_FIXED) == 0) ||
16690 /* TODO: Equality of the initial value or normalized or canonical? */
16691 (! xmlStrEqual(elemR->value, elemB->value))))
16692 return (1);
16693 /*
16694 * TODO: SPEC (3.2.3) "R's declaration's {identity-constraint
16695 * definitions} is a subset of B's declaration's {identity-constraint
16696 * definitions}, if any."
16697 */
16698 if (elemB->idcs != NULL) {
16699 /* TODO */
16700 }
16701 /*
16702 * SPEC (3.2.4) "R's declaration's {disallowed substitutions} is a
16703 * superset of B's declaration's {disallowed substitutions}."
16704 */
16705 if (((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) &&
16706 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) == 0)) ||
16707 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) &&
16708 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) == 0)) ||
16709 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) &&
16710 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) == 0)))
16711 return (1);
16712 /*
16713 * SPEC (3.2.5) "R's {type definition} is validly derived given
16714 * {extension, list, union} from B's {type definition}"
16715 *
16716 * BADSPEC TODO: What's the point of adding "list" and "union" to the
16717 * set, if the corresponding constraints handle "restriction" and
16718 * "extension" only?
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016719 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016720 */
16721 {
16722 int set = 0;
16723
16724 set |= SUBSET_EXTENSION;
16725 set |= SUBSET_LIST;
16726 set |= SUBSET_UNION;
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016727 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST ctxt, elemR->subtypes,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016728 elemB->subtypes, set) != 0)
16729 return (1);
16730 }
16731 return (0);
16732}
16733
16734/**
16735 * xmlSchemaCheckRCaseNSCompat:
16736 * @ctxt: the schema parser context
16737 * @r: the restricting element declaration particle
16738 * @b: the base wildcard particle
16739 *
16740 * (3.9.6) Constraints on Particle Schema Components
16741 * Schema Component Constraint:
16742 * Particle Derivation OK (Elt:Any -- NSCompat)
16743 * (rcase-NSCompat)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016744 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016745 * STATUS: complete
16746 *
16747 * Returns 0 if the constraints are satisfied, a positive
16748 * error code if not and -1 if an internal error occured.
16749 */
16750static int
16751xmlSchemaCheckRCaseNSCompat(xmlSchemaParserCtxtPtr ctxt,
16752 xmlSchemaParticlePtr r,
16753 xmlSchemaParticlePtr b)
16754{
16755 /* TODO:Error codes (rcase-NSCompat). */
16756 /*
16757 * SPEC "For an element declaration particle to be a ·valid restriction·
16758 * of a wildcard particle all of the following must be true:"
16759 *
16760 * SPEC (1) "The element declaration's {target namespace} is ·valid·
16761 * with respect to the wildcard's {namespace constraint} as defined by
16762 * Wildcard allows Namespace Name (§3.10.4)."
16763 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016764 if (xmlSchemaCheckCVCWildcardNamespace((xmlSchemaWildcardPtr) b->children,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016765 ((xmlSchemaElementPtr) r->children)->targetNamespace) != 0)
16766 return (1);
16767 /*
16768 * SPEC (2) "R's occurrence range is a valid restriction of B's
16769 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16770 */
16771 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16772 b->minOccurs, b->maxOccurs) != 0)
16773 return (1);
16774
16775 return (0);
16776}
16777
16778/**
16779 * xmlSchemaCheckRCaseRecurseAsIfGroup:
16780 * @ctxt: the schema parser context
16781 * @r: the restricting element declaration particle
16782 * @b: the base model group particle
16783 *
16784 * (3.9.6) Constraints on Particle Schema Components
16785 * Schema Component Constraint:
16786 * Particle Derivation OK (Elt:All/Choice/Sequence -- RecurseAsIfGroup)
16787 * (rcase-RecurseAsIfGroup)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016788 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016789 * STATUS: TODO
16790 *
16791 * Returns 0 if the constraints are satisfied, a positive
16792 * error code if not and -1 if an internal error occured.
16793 */
16794static int
16795xmlSchemaCheckRCaseRecurseAsIfGroup(xmlSchemaParserCtxtPtr ctxt,
16796 xmlSchemaParticlePtr r,
16797 xmlSchemaParticlePtr b)
16798{
16799 /* TODO: Error codes (rcase-RecurseAsIfGroup). */
16800 TODO
16801 return (0);
16802}
16803
16804/**
16805 * xmlSchemaCheckRCaseNSSubset:
16806 * @ctxt: the schema parser context
16807 * @r: the restricting wildcard particle
16808 * @b: the base wildcard particle
16809 *
16810 * (3.9.6) Constraints on Particle Schema Components
16811 * Schema Component Constraint:
16812 * Particle Derivation OK (Any:Any -- NSSubset)
16813 * (rcase-NSSubset)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016814 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016815 * STATUS: complete
16816 *
16817 * Returns 0 if the constraints are satisfied, a positive
16818 * error code if not and -1 if an internal error occured.
16819 */
16820static int
16821xmlSchemaCheckRCaseNSSubset(xmlSchemaParserCtxtPtr ctxt,
16822 xmlSchemaParticlePtr r,
16823 xmlSchemaParticlePtr b,
16824 int isAnyTypeBase)
16825{
16826 /* TODO: Error codes (rcase-NSSubset). */
16827 /*
16828 * SPEC (1) "R's occurrence range is a valid restriction of B's
16829 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16830 */
16831 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16832 b->minOccurs, b->maxOccurs))
16833 return (1);
16834 /*
16835 * SPEC (2) "R's {namespace constraint} must be an intensional subset
16836 * of B's {namespace constraint} as defined by Wildcard Subset (§3.10.6)."
16837 */
16838 if (xmlSchemaCheckCOSNSSubset((xmlSchemaWildcardPtr) r->children,
16839 (xmlSchemaWildcardPtr) b->children))
16840 return (1);
16841 /*
16842 * SPEC (3) "Unless B is the content model wildcard of the ·ur-type
16843 * definition·, R's {process contents} must be identical to or stronger
16844 * than B's {process contents}, where strict is stronger than lax is
16845 * stronger than skip."
16846 */
16847 if (! isAnyTypeBase) {
16848 if ( ((xmlSchemaWildcardPtr) r->children)->processContents <
16849 ((xmlSchemaWildcardPtr) b->children)->processContents)
16850 return (1);
16851 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016852
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016853 return (0);
16854}
16855
16856/**
16857 * xmlSchemaCheckCOSParticleRestrict:
16858 * @ctxt: the schema parser context
16859 * @type: the complex type definition
16860 *
16861 * (3.9.6) Constraints on Particle Schema Components
16862 * Schema Component Constraint:
16863 * Particle Valid (Restriction) (cos-particle-restrict)
16864 *
16865 * STATUS: TODO
16866 *
16867 * Returns 0 if the constraints are satisfied, a positive
16868 * error code if not and -1 if an internal error occured.
16869 */
16870static int
16871xmlSchemaCheckCOSParticleRestrict(xmlSchemaParserCtxtPtr ctxt,
16872 xmlSchemaParticlePtr r,
16873 xmlSchemaParticlePtr b)
16874{
16875 int ret = 0;
16876
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016877 /*part = WXS_TYPE_PARTICLE(type);
16878 basePart = WXS_TYPE_PARTICLE(base);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016879 */
16880
16881 TODO
16882
16883 /*
16884 * SPEC (1) "They are the same particle."
16885 */
16886 if (r == b)
16887 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016888
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016889
16890 return (0);
16891}
16892
16893/**
16894 * xmlSchemaCheckRCaseNSRecurseCheckCardinality:
16895 * @ctxt: the schema parser context
16896 * @r: the model group particle
16897 * @b: the base wildcard particle
16898 *
16899 * (3.9.6) Constraints on Particle Schema Components
16900 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016901 * Particle Derivation OK (All/Choice/Sequence:Any --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016902 * NSRecurseCheckCardinality)
16903 * (rcase-NSRecurseCheckCardinality)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016904 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016905 * STATUS: TODO: subst-groups
16906 *
16907 * Returns 0 if the constraints are satisfied, a positive
16908 * error code if not and -1 if an internal error occured.
16909 */
16910static int
16911xmlSchemaCheckRCaseNSRecurseCheckCardinality(xmlSchemaParserCtxtPtr ctxt,
16912 xmlSchemaParticlePtr r,
16913 xmlSchemaParticlePtr b)
16914{
16915 xmlSchemaParticlePtr part;
16916 /* TODO: Error codes (rcase-NSRecurseCheckCardinality). */
16917 if ((r->children == NULL) || (r->children->children == NULL))
16918 return (-1);
16919 /*
16920 * SPEC "For a group particle to be a ·valid restriction· of a
16921 * wildcard particle..."
16922 *
16923 * SPEC (1) "Every member of the {particles} of the group is a ·valid
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016924 * restriction· of the wildcard as defined by
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016925 * Particle Valid (Restriction) (§3.9.6)."
16926 */
16927 part = (xmlSchemaParticlePtr) r->children->children;
16928 do {
16929 if (xmlSchemaCheckCOSParticleRestrict(ctxt, part, b))
16930 return (1);
16931 part = (xmlSchemaParticlePtr) part->next;
16932 } while (part != NULL);
16933 /*
16934 * SPEC (2) "The effective total range of the group [...] is a
16935 * valid restriction of B's occurrence range as defined by
16936 * Occurrence Range OK (§3.9.6)."
16937 */
16938 if (xmlSchemaCheckParticleRangeOK(
16939 xmlSchemaGetParticleTotalRangeMin(r),
16940 xmlSchemaGetParticleTotalRangeMax(r),
16941 b->minOccurs, b->maxOccurs) != 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016942 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016943 return (0);
16944}
16945
16946/**
16947 * xmlSchemaCheckRCaseRecurse:
16948 * @ctxt: the schema parser context
16949 * @r: the <all> or <sequence> model group particle
16950 * @b: the base <all> or <sequence> model group particle
16951 *
16952 * (3.9.6) Constraints on Particle Schema Components
16953 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016954 * Particle Derivation OK (All:All,Sequence:Sequence --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016955 Recurse)
16956 * (rcase-Recurse)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016957 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016958 * STATUS: ?
16959 * TODO: subst-groups
16960 *
16961 * Returns 0 if the constraints are satisfied, a positive
16962 * error code if not and -1 if an internal error occured.
16963 */
16964static int
16965xmlSchemaCheckRCaseRecurse(xmlSchemaParserCtxtPtr ctxt,
16966 xmlSchemaParticlePtr r,
16967 xmlSchemaParticlePtr b)
16968{
16969 /* xmlSchemaParticlePtr part; */
16970 /* TODO: Error codes (rcase-Recurse). */
16971 if ((r->children == NULL) || (b->children == NULL) ||
16972 (r->children->type != b->children->type))
16973 return (-1);
16974 /*
16975 * SPEC "For an all or sequence group particle to be a ·valid
16976 * restriction· of another group particle with the same {compositor}..."
16977 *
16978 * SPEC (1) "R's occurrence range is a valid restriction of B's
16979 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16980 */
16981 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16982 b->minOccurs, b->maxOccurs))
16983 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016984
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016985
16986 return (0);
16987}
16988
16989#endif
16990
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016991#define FACET_RESTR_MUTUAL_ERR(fac1, fac2) \
16992 xmlSchemaPCustomErrExt(pctxt, \
16993 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016994 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016995 "It is an error for both '%s' and '%s' to be specified on the "\
16996 "same type definition", \
16997 BAD_CAST xmlSchemaFacetTypeToString(fac1->type), \
16998 BAD_CAST xmlSchemaFacetTypeToString(fac2->type), NULL);
16999
17000#define FACET_RESTR_ERR(fac1, msg) \
17001 xmlSchemaPCustomErr(pctxt, \
17002 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017003 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017004 msg, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017005
17006#define FACET_RESTR_FIXED_ERR(fac) \
17007 xmlSchemaPCustomErr(pctxt, \
17008 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017009 WXS_BASIC_CAST fac, fac->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017010 "The base type's facet is 'fixed', thus the value must not " \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017011 "differ", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017012
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017013static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017014xmlSchemaDeriveFacetErr(xmlSchemaParserCtxtPtr pctxt,
17015 xmlSchemaFacetPtr facet1,
17016 xmlSchemaFacetPtr facet2,
17017 int lessGreater,
17018 int orEqual,
17019 int ofBase)
17020{
17021 xmlChar *msg = NULL;
17022
17023 msg = xmlStrdup(BAD_CAST "'");
17024 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet1->type));
17025 msg = xmlStrcat(msg, BAD_CAST "' has to be");
17026 if (lessGreater == 0)
17027 msg = xmlStrcat(msg, BAD_CAST " equal to");
17028 if (lessGreater == 1)
17029 msg = xmlStrcat(msg, BAD_CAST " greater than");
17030 else
17031 msg = xmlStrcat(msg, BAD_CAST " less than");
17032
17033 if (orEqual)
17034 msg = xmlStrcat(msg, BAD_CAST " or equal to");
17035 msg = xmlStrcat(msg, BAD_CAST " '");
17036 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet2->type));
17037 if (ofBase)
17038 msg = xmlStrcat(msg, BAD_CAST "' of the base type");
17039 else
17040 msg = xmlStrcat(msg, BAD_CAST "'");
17041
17042 xmlSchemaPCustomErr(pctxt,
17043 XML_SCHEMAP_INVALID_FACET_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017044 WXS_BASIC_CAST facet1, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017045 (const char *) msg, NULL);
17046
17047 if (msg != NULL)
17048 xmlFree(msg);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017049}
17050
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017051/*
17052* xmlSchemaDeriveAndValidateFacets:
17053*
17054* Schema Component Constraint: Simple Type Restriction (Facets)
17055* (st-restrict-facets)
17056*/
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017057static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017058xmlSchemaDeriveAndValidateFacets(xmlSchemaParserCtxtPtr pctxt,
17059 xmlSchemaTypePtr type)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017060{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017061 xmlSchemaTypePtr base = type->baseType;
17062 xmlSchemaFacetLinkPtr link, cur, last = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017063 xmlSchemaFacetPtr facet, bfacet,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017064 flength = NULL, ftotdig = NULL, ffracdig = NULL,
17065 fmaxlen = NULL, fminlen = NULL, /* facets of the current type */
17066 fmininc = NULL, fmaxinc = NULL,
17067 fminexc = NULL, fmaxexc = NULL,
17068 bflength = NULL, bftotdig = NULL, bffracdig = NULL,
17069 bfmaxlen = NULL, bfminlen = NULL, /* facets of the base type */
17070 bfmininc = NULL, bfmaxinc = NULL,
17071 bfminexc = NULL, bfmaxexc = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017072 int res; /* err = 0, fixedErr; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017073
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017074 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017075 * SPEC st-restrict-facets 1:
17076 * "The {variety} of R is the same as that of B."
17077 */
17078 /*
17079 * SPEC st-restrict-facets 2:
17080 * "If {variety} is atomic, the {primitive type definition}
17081 * of R is the same as that of B."
17082 *
17083 * NOTE: we leave 1 & 2 out for now, since this will be
17084 * satisfied by the derivation process.
17085 * CONSTRUCTION TODO: Maybe needed if using a construction API.
17086 */
17087 /*
17088 * SPEC st-restrict-facets 3:
17089 * "The {facets} of R are the union of S and the {facets}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017090 * of B, eliminating duplicates. To eliminate duplicates,
17091 * when a facet of the same kind occurs in both S and the
17092 * {facets} of B, the one in the {facets} of B is not
17093 * included, with the exception of enumeration and pattern
17094 * facets, for which multiple occurrences with distinct values
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017095 * are allowed."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017096 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017097
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017098 if ((type->facetSet == NULL) && (base->facetSet == NULL))
17099 return (0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017100
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017101 last = type->facetSet;
17102 if (last != NULL)
17103 while (last->next != NULL)
17104 last = last->next;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017105
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017106 for (cur = type->facetSet; cur != NULL; cur = cur->next) {
17107 facet = cur->facet;
17108 switch (facet->type) {
17109 case XML_SCHEMA_FACET_LENGTH:
17110 flength = facet; break;
17111 case XML_SCHEMA_FACET_MINLENGTH:
17112 fminlen = facet; break;
17113 case XML_SCHEMA_FACET_MININCLUSIVE:
17114 fmininc = facet; break;
17115 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17116 fminexc = facet; break;
17117 case XML_SCHEMA_FACET_MAXLENGTH:
17118 fmaxlen = facet; break;
17119 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17120 fmaxinc = facet; break;
17121 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17122 fmaxexc = facet; break;
17123 case XML_SCHEMA_FACET_TOTALDIGITS:
17124 ftotdig = facet; break;
17125 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17126 ffracdig = facet; break;
17127 default:
17128 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017129 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017130 }
17131 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17132 facet = cur->facet;
17133 switch (facet->type) {
17134 case XML_SCHEMA_FACET_LENGTH:
17135 bflength = facet; break;
17136 case XML_SCHEMA_FACET_MINLENGTH:
17137 bfminlen = facet; break;
17138 case XML_SCHEMA_FACET_MININCLUSIVE:
17139 bfmininc = facet; break;
17140 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17141 bfminexc = facet; break;
17142 case XML_SCHEMA_FACET_MAXLENGTH:
17143 bfmaxlen = facet; break;
17144 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17145 bfmaxinc = facet; break;
17146 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17147 bfmaxexc = facet; break;
17148 case XML_SCHEMA_FACET_TOTALDIGITS:
17149 bftotdig = facet; break;
17150 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17151 bffracdig = facet; break;
17152 default:
17153 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017154 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017155 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017156 /*
17157 * length and minLength or maxLength (2.2) + (3.2)
17158 */
17159 if (flength && (fminlen || fmaxlen)) {
17160 FACET_RESTR_ERR(flength, "It is an error for both 'length' and "
17161 "either of 'minLength' or 'maxLength' to be specified on "
17162 "the same type definition")
17163 }
17164 /*
17165 * Mutual exclusions in the same derivation step.
17166 */
17167 if ((fmaxinc) && (fmaxexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017168 /*
17169 * SCC "maxInclusive and maxExclusive"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017170 */
17171 FACET_RESTR_MUTUAL_ERR(fmaxinc, fmaxexc)
17172 }
17173 if ((fmininc) && (fminexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017174 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017175 * SCC "minInclusive and minExclusive"
17176 */
17177 FACET_RESTR_MUTUAL_ERR(fmininc, fminexc)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017178 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017179
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017180 if (flength && bflength) {
17181 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017182 * SCC "length valid restriction"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017183 * The values have to be equal.
17184 */
17185 res = xmlSchemaCompareValues(flength->val, bflength->val);
17186 if (res == -2)
17187 goto internal_error;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017188 if (res != 0)
17189 xmlSchemaDeriveFacetErr(pctxt, flength, bflength, 0, 0, 1);
17190 if ((res != 0) && (bflength->fixed)) {
17191 FACET_RESTR_FIXED_ERR(flength)
17192 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017193
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017194 }
17195 if (fminlen && bfminlen) {
17196 /*
17197 * SCC "minLength valid restriction"
17198 * minLength >= BASE minLength
17199 */
17200 res = xmlSchemaCompareValues(fminlen->val, bfminlen->val);
17201 if (res == -2)
17202 goto internal_error;
17203 if (res == -1)
17204 xmlSchemaDeriveFacetErr(pctxt, fminlen, bfminlen, 1, 1, 1);
17205 if ((res != 0) && (bfminlen->fixed)) {
17206 FACET_RESTR_FIXED_ERR(fminlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017207 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017208 }
17209 if (fmaxlen && bfmaxlen) {
17210 /*
17211 * SCC "maxLength valid restriction"
17212 * maxLength <= BASE minLength
17213 */
17214 res = xmlSchemaCompareValues(fmaxlen->val, bfmaxlen->val);
17215 if (res == -2)
17216 goto internal_error;
17217 if (res == 1)
17218 xmlSchemaDeriveFacetErr(pctxt, fmaxlen, bfmaxlen, -1, 1, 1);
17219 if ((res != 0) && (bfmaxlen->fixed)) {
17220 FACET_RESTR_FIXED_ERR(fmaxlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017221 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017222 }
17223 /*
17224 * SCC "length and minLength or maxLength"
17225 */
17226 if (! flength)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017227 flength = bflength;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017228 if (flength) {
17229 if (! fminlen)
17230 flength = bflength;
17231 if (fminlen) {
17232 /* (1.1) length >= minLength */
17233 res = xmlSchemaCompareValues(flength->val, fminlen->val);
17234 if (res == -2)
17235 goto internal_error;
17236 if (res == -1)
17237 xmlSchemaDeriveFacetErr(pctxt, flength, fminlen, 1, 1, 0);
17238 }
17239 if (! fmaxlen)
17240 fmaxlen = bfmaxlen;
17241 if (fmaxlen) {
17242 /* (2.1) length <= maxLength */
17243 res = xmlSchemaCompareValues(flength->val, fmaxlen->val);
17244 if (res == -2)
17245 goto internal_error;
17246 if (res == 1)
17247 xmlSchemaDeriveFacetErr(pctxt, flength, fmaxlen, -1, 1, 0);
17248 }
17249 }
17250 if (fmaxinc) {
17251 /*
17252 * "maxInclusive"
17253 */
17254 if (fmininc) {
17255 /* SCC "maxInclusive >= minInclusive" */
17256 res = xmlSchemaCompareValues(fmaxinc->val, fmininc->val);
17257 if (res == -2)
17258 goto internal_error;
17259 if (res == -1) {
17260 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, fmininc, 1, 1, 0);
17261 }
17262 }
17263 /*
17264 * SCC "maxInclusive valid restriction"
17265 */
17266 if (bfmaxinc) {
17267 /* maxInclusive <= BASE maxInclusive */
17268 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxinc->val);
17269 if (res == -2)
17270 goto internal_error;
17271 if (res == 1)
17272 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxinc, -1, 1, 1);
17273 if ((res != 0) && (bfmaxinc->fixed)) {
17274 FACET_RESTR_FIXED_ERR(fmaxinc)
17275 }
17276 }
17277 if (bfmaxexc) {
17278 /* maxInclusive < BASE maxExclusive */
17279 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxexc->val);
17280 if (res == -2)
17281 goto internal_error;
17282 if (res != -1) {
17283 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxexc, -1, 0, 1);
17284 }
17285 }
17286 if (bfmininc) {
17287 /* maxInclusive >= BASE minInclusive */
17288 res = xmlSchemaCompareValues(fmaxinc->val, bfmininc->val);
17289 if (res == -2)
17290 goto internal_error;
17291 if (res == -1) {
17292 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmininc, 1, 1, 1);
17293 }
17294 }
17295 if (bfminexc) {
17296 /* maxInclusive > BASE minExclusive */
17297 res = xmlSchemaCompareValues(fmaxinc->val, bfminexc->val);
17298 if (res == -2)
17299 goto internal_error;
17300 if (res != 1) {
17301 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfminexc, 1, 0, 1);
17302 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017303 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017304 }
17305 if (fmaxexc) {
17306 /*
17307 * "maxExclusive >= minExclusive"
17308 */
17309 if (fminexc) {
17310 res = xmlSchemaCompareValues(fmaxexc->val, fminexc->val);
17311 if (res == -2)
17312 goto internal_error;
17313 if (res == -1) {
17314 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, fminexc, 1, 1, 0);
17315 }
17316 }
17317 /*
17318 * "maxExclusive valid restriction"
17319 */
17320 if (bfmaxexc) {
17321 /* maxExclusive <= BASE maxExclusive */
17322 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxexc->val);
17323 if (res == -2)
17324 goto internal_error;
17325 if (res == 1) {
17326 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxexc, -1, 1, 1);
17327 }
17328 if ((res != 0) && (bfmaxexc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017329 FACET_RESTR_FIXED_ERR(fmaxexc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017330 }
17331 }
17332 if (bfmaxinc) {
17333 /* maxExclusive <= BASE maxInclusive */
17334 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxinc->val);
17335 if (res == -2)
17336 goto internal_error;
17337 if (res == 1) {
17338 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxinc, -1, 1, 1);
17339 }
17340 }
17341 if (bfmininc) {
17342 /* maxExclusive > BASE minInclusive */
17343 res = xmlSchemaCompareValues(fmaxexc->val, bfmininc->val);
17344 if (res == -2)
17345 goto internal_error;
17346 if (res != 1) {
17347 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmininc, 1, 0, 1);
17348 }
17349 }
17350 if (bfminexc) {
17351 /* maxExclusive > BASE minExclusive */
17352 res = xmlSchemaCompareValues(fmaxexc->val, bfminexc->val);
17353 if (res == -2)
17354 goto internal_error;
17355 if (res != 1) {
17356 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfminexc, 1, 0, 1);
17357 }
17358 }
17359 }
17360 if (fminexc) {
17361 /*
17362 * "minExclusive < maxInclusive"
17363 */
17364 if (fmaxinc) {
17365 res = xmlSchemaCompareValues(fminexc->val, fmaxinc->val);
17366 if (res == -2)
17367 goto internal_error;
17368 if (res != -1) {
17369 xmlSchemaDeriveFacetErr(pctxt, fminexc, fmaxinc, -1, 0, 0);
17370 }
17371 }
17372 /*
17373 * "minExclusive valid restriction"
17374 */
17375 if (bfminexc) {
17376 /* minExclusive >= BASE minExclusive */
17377 res = xmlSchemaCompareValues(fminexc->val, bfminexc->val);
17378 if (res == -2)
17379 goto internal_error;
17380 if (res == -1) {
17381 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfminexc, 1, 1, 1);
17382 }
17383 if ((res != 0) && (bfminexc->fixed)) {
17384 FACET_RESTR_FIXED_ERR(fminexc)
17385 }
17386 }
17387 if (bfmaxinc) {
17388 /* minExclusive <= BASE maxInclusive */
17389 res = xmlSchemaCompareValues(fminexc->val, bfmaxinc->val);
17390 if (res == -2)
17391 goto internal_error;
17392 if (res == 1) {
17393 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxinc, -1, 1, 1);
17394 }
17395 }
17396 if (bfmininc) {
17397 /* minExclusive >= BASE minInclusive */
17398 res = xmlSchemaCompareValues(fminexc->val, bfmininc->val);
17399 if (res == -2)
17400 goto internal_error;
17401 if (res == -1) {
17402 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmininc, 1, 1, 1);
17403 }
17404 }
17405 if (bfmaxexc) {
17406 /* minExclusive < BASE maxExclusive */
17407 res = xmlSchemaCompareValues(fminexc->val, bfmaxexc->val);
17408 if (res == -2)
17409 goto internal_error;
17410 if (res != -1) {
17411 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxexc, -1, 0, 1);
17412 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017413 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017414 }
17415 if (fmininc) {
17416 /*
17417 * "minInclusive < maxExclusive"
17418 */
17419 if (fmaxexc) {
17420 res = xmlSchemaCompareValues(fmininc->val, fmaxexc->val);
17421 if (res == -2)
17422 goto internal_error;
17423 if (res != -1) {
17424 xmlSchemaDeriveFacetErr(pctxt, fmininc, fmaxexc, -1, 0, 0);
17425 }
17426 }
17427 /*
17428 * "minExclusive valid restriction"
17429 */
17430 if (bfmininc) {
17431 /* minInclusive >= BASE minInclusive */
17432 res = xmlSchemaCompareValues(fmininc->val, bfmininc->val);
17433 if (res == -2)
17434 goto internal_error;
17435 if (res == -1) {
17436 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmininc, 1, 1, 1);
17437 }
17438 if ((res != 0) && (bfmininc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017439 FACET_RESTR_FIXED_ERR(fmininc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017440 }
17441 }
17442 if (bfmaxinc) {
17443 /* minInclusive <= BASE maxInclusive */
17444 res = xmlSchemaCompareValues(fmininc->val, bfmaxinc->val);
17445 if (res == -2)
17446 goto internal_error;
Daniel Veillard0a119eb2005-07-20 13:46:00 +000017447 if (res == 1) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017448 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxinc, -1, 1, 1);
17449 }
17450 }
17451 if (bfminexc) {
17452 /* minInclusive > BASE minExclusive */
17453 res = xmlSchemaCompareValues(fmininc->val, bfminexc->val);
17454 if (res == -2)
17455 goto internal_error;
17456 if (res != 1)
17457 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfminexc, 1, 0, 1);
17458 }
17459 if (bfmaxexc) {
17460 /* minInclusive < BASE maxExclusive */
17461 res = xmlSchemaCompareValues(fmininc->val, bfmaxexc->val);
17462 if (res == -2)
17463 goto internal_error;
17464 if (res != -1)
17465 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxexc, -1, 0, 1);
17466 }
17467 }
17468 if (ftotdig && bftotdig) {
17469 /*
17470 * SCC " totalDigits valid restriction"
17471 * totalDigits <= BASE totalDigits
17472 */
17473 res = xmlSchemaCompareValues(ftotdig->val, bftotdig->val);
17474 if (res == -2)
17475 goto internal_error;
17476 if (res == 1)
17477 xmlSchemaDeriveFacetErr(pctxt, ftotdig, bftotdig,
17478 -1, 1, 1);
17479 if ((res != 0) && (bftotdig->fixed)) {
17480 FACET_RESTR_FIXED_ERR(ftotdig)
17481 }
17482 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017483 if (ffracdig && bffracdig) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017484 /*
17485 * SCC "fractionDigits valid restriction"
17486 * fractionDigits <= BASE fractionDigits
17487 */
17488 res = xmlSchemaCompareValues(ffracdig->val, bffracdig->val);
17489 if (res == -2)
17490 goto internal_error;
17491 if (res == 1)
17492 xmlSchemaDeriveFacetErr(pctxt, ffracdig, bffracdig,
17493 -1, 1, 1);
17494 if ((res != 0) && (bffracdig->fixed)) {
17495 FACET_RESTR_FIXED_ERR(ffracdig)
17496 }
17497 }
17498 /*
17499 * SCC "fractionDigits less than or equal to totalDigits"
17500 */
17501 if (! ftotdig)
17502 ftotdig = bftotdig;
17503 if (! ffracdig)
17504 ffracdig = bffracdig;
17505 if (ftotdig && ffracdig) {
17506 res = xmlSchemaCompareValues(ffracdig->val, ftotdig->val);
17507 if (res == -2)
17508 goto internal_error;
17509 if (res == 1)
17510 xmlSchemaDeriveFacetErr(pctxt, ffracdig, ftotdig,
17511 -1, 1, 0);
17512 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017513 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017514 * *Enumerations* won' be added here, since only the first set
17515 * of enumerations in the ancestor-or-self axis is used
17516 * for validation, plus we need to use the base type of those
17517 * enumerations for whitespace.
17518 *
17519 * *Patterns*: won't be add here, since they are ORed at
17520 * type level and ANDed at ancestor level. This will
17521 * happed during validation by walking the base axis
17522 * of the type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017523 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017524 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17525 bfacet = cur->facet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017526 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017527 * Special handling of enumerations and patterns.
17528 * TODO: hmm, they should not appear in the set, so remove this.
17529 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017530 if ((bfacet->type == XML_SCHEMA_FACET_PATTERN) ||
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017531 (bfacet->type == XML_SCHEMA_FACET_ENUMERATION))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017532 continue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017533 /*
17534 * Search for a duplicate facet in the current type.
17535 */
17536 link = type->facetSet;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017537 /* err = 0; */
17538 /* fixedErr = 0; */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017539 while (link != NULL) {
17540 facet = link->facet;
17541 if (facet->type == bfacet->type) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017542 switch (facet->type) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017543 case XML_SCHEMA_FACET_WHITESPACE:
17544 /*
17545 * The whitespace must be stronger.
17546 */
17547 if (facet->whitespace < bfacet->whitespace) {
17548 FACET_RESTR_ERR(flength,
17549 "The 'whitespace' value has to be equal to "
17550 "or stronger than the 'whitespace' value of "
17551 "the base type")
17552 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017553 if ((bfacet->fixed) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017554 (facet->whitespace != bfacet->whitespace)) {
17555 FACET_RESTR_FIXED_ERR(facet)
17556 }
17557 break;
17558 default:
17559 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017560 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017561 /* Duplicate found. */
17562 break;
17563 }
17564 link = link->next;
17565 }
17566 /*
17567 * If no duplicate was found: add the base types's facet
17568 * to the set.
17569 */
17570 if (link == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017571 link = (xmlSchemaFacetLinkPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017572 xmlMalloc(sizeof(xmlSchemaFacetLink));
17573 if (link == NULL) {
17574 xmlSchemaPErrMemory(pctxt,
17575 "deriving facets, creating a facet link", NULL);
17576 return (-1);
17577 }
17578 link->facet = cur->facet;
17579 link->next = NULL;
17580 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017581 type->facetSet = link;
17582 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017583 last->next = link;
17584 last = link;
17585 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017586
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017587 }
17588
17589 return (0);
17590internal_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017591 PERROR_INT("xmlSchemaDeriveAndValidateFacets",
17592 "an error occured");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017593 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017594}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017595
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017596static int
17597xmlSchemaFinishMemberTypeDefinitionsProperty(xmlSchemaParserCtxtPtr pctxt,
17598 xmlSchemaTypePtr type)
17599{
17600 xmlSchemaTypeLinkPtr link, lastLink, prevLink, subLink, newLink;
17601 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017602 * The actual value is then formed by replacing any union type
17603 * definition in the ·explicit members· with the members of their
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017604 * {member type definitions}, in order.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017605 *
17606 * TODO: There's a bug entry at
17607 * "http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0287.html"
17608 * which indicates that we'll keep the union types the future.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017609 */
17610 link = type->memberTypes;
17611 while (link != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017612
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017613 if (WXS_IS_TYPE_NOT_FIXED(link->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000017614 xmlSchemaTypeFixup(link->type, ACTXT_CAST pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017615
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017616 if (WXS_IS_UNION(link->type)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017617 subLink = xmlSchemaGetUnionSimpleTypeMemberTypes(link->type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017618 if (subLink != NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017619 link->type = subLink->type;
17620 if (subLink->next != NULL) {
17621 lastLink = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017622 subLink = subLink->next;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017623 prevLink = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017624 while (subLink != NULL) {
17625 newLink = (xmlSchemaTypeLinkPtr)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017626 xmlMalloc(sizeof(xmlSchemaTypeLink));
17627 if (newLink == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017628 xmlSchemaPErrMemory(pctxt, "allocating a type link",
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017629 NULL);
17630 return (-1);
17631 }
17632 newLink->type = subLink->type;
17633 prevLink->next = newLink;
17634 prevLink = newLink;
17635 newLink->next = lastLink;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017636
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017637 subLink = subLink->next;
17638 }
17639 }
17640 }
17641 }
17642 link = link->next;
17643 }
17644 return (0);
17645}
17646
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017647static void
17648xmlSchemaTypeFixupOptimFacets(xmlSchemaTypePtr type)
17649{
17650 int has = 0, needVal = 0, normVal = 0;
17651
17652 has = (type->baseType->flags & XML_SCHEMAS_TYPE_HAS_FACETS) ? 1 : 0;
17653 if (has) {
17654 needVal = (type->baseType->flags &
17655 XML_SCHEMAS_TYPE_FACETSNEEDVALUE) ? 1 : 0;
17656 normVal = (type->baseType->flags &
17657 XML_SCHEMAS_TYPE_NORMVALUENEEDED) ? 1 : 0;
17658 }
17659 if (type->facets != NULL) {
17660 xmlSchemaFacetPtr fac;
17661
17662 for (fac = type->facets; fac != NULL; fac = fac->next) {
17663 switch (fac->type) {
17664 case XML_SCHEMA_FACET_WHITESPACE:
17665 break;
17666 case XML_SCHEMA_FACET_PATTERN:
17667 normVal = 1;
17668 has = 1;
17669 break;
17670 case XML_SCHEMA_FACET_ENUMERATION:
17671 needVal = 1;
17672 normVal = 1;
17673 has = 1;
17674 break;
17675 default:
17676 has = 1;
17677 break;
17678 }
17679 }
17680 }
17681 if (normVal)
17682 type->flags |= XML_SCHEMAS_TYPE_NORMVALUENEEDED;
17683 if (needVal)
17684 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17685 if (has)
17686 type->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
17687
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017688 if (has && (! needVal) && WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017689 xmlSchemaTypePtr prim = xmlSchemaGetPrimitiveType(type);
17690 /*
17691 * OPTIMIZE VAL TODO: Some facets need a computed value.
17692 */
17693 if ((prim->builtInType != XML_SCHEMAS_ANYSIMPLETYPE) &&
17694 (prim->builtInType != XML_SCHEMAS_STRING)) {
17695 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17696 }
17697 }
17698}
17699
17700static int
17701xmlSchemaTypeFixupWhitespace(xmlSchemaTypePtr type)
17702{
17703
17704
17705 /*
17706 * Evaluate the whitespace-facet value.
17707 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017708 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017709 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17710 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017711 } else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017712 return (0);
17713
17714 if (type->facetSet != NULL) {
17715 xmlSchemaFacetLinkPtr lin;
17716
17717 for (lin = type->facetSet; lin != NULL; lin = lin->next) {
17718 if (lin->facet->type == XML_SCHEMA_FACET_WHITESPACE) {
17719 switch (lin->facet->whitespace) {
17720 case XML_SCHEMAS_FACET_PRESERVE:
17721 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17722 break;
17723 case XML_SCHEMAS_FACET_REPLACE:
17724 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17725 break;
17726 case XML_SCHEMAS_FACET_COLLAPSE:
17727 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17728 break;
17729 default:
17730 return (-1);
17731 }
17732 return (0);
17733 }
17734 }
17735 }
17736 /*
17737 * For all ·atomic· datatypes other than string (and types ·derived·
17738 * by ·restriction· from it) the value of whiteSpace is fixed to
17739 * collapse
17740 */
17741 {
17742 xmlSchemaTypePtr anc;
17743
17744 for (anc = type->baseType; anc != NULL &&
17745 anc->builtInType != XML_SCHEMAS_ANYTYPE;
17746 anc = anc->baseType) {
17747
17748 if (anc->type == XML_SCHEMA_TYPE_BASIC) {
17749 if (anc->builtInType == XML_SCHEMAS_NORMSTRING) {
17750 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17751
17752 } else if ((anc->builtInType == XML_SCHEMAS_STRING) ||
17753 (anc->builtInType == XML_SCHEMAS_ANYSIMPLETYPE)) {
17754 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17755
17756 } else
17757 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17758 break;
17759 }
17760 }
17761 return (0);
17762 }
17763 return (0);
17764}
17765
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017766static int
17767xmlSchemaFixupSimpleTypeStageOne(xmlSchemaParserCtxtPtr pctxt,
17768 xmlSchemaTypePtr type)
Daniel Veillard4255d502002-04-16 15:50:10 +000017769{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017770 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17771 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017772 if (! WXS_IS_TYPE_NOT_FIXED_1(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017773 return(0);
17774 type->flags |= XML_SCHEMAS_TYPE_FIXUP_1;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017775
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017776 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017777 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017778 * Corresponds to <simpleType><list>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017779 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017780 if (type->subtypes == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017781 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017782 * This one is really needed, so get out.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017783 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017784 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017785 "list type has no item-type assigned");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017786 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017787 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017788 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017789 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017790 * Corresponds to <simpleType><union>...
17791 */
17792 if (type->memberTypes == NULL) {
17793 /*
17794 * This one is really needed, so get out.
17795 */
17796 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17797 "union type has no member-types assigned");
17798 return(-1);
17799 }
17800 } else {
17801 /*
17802 * Corresponds to <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017803 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017804 if (type->baseType == NULL) {
17805 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17806 "type has no base-type assigned");
17807 return(-1);
17808 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017809 if (WXS_IS_TYPE_NOT_FIXED_1(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017810 if (xmlSchemaFixupSimpleTypeStageOne(pctxt, type->baseType) == -1)
17811 return(-1);
17812 /*
17813 * Variety
17814 * If the <restriction> alternative is chosen, then the
17815 * {variety} of the {base type definition}.
17816 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017817 if (WXS_IS_ATOMIC(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017818 type->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017819 else if (WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017820 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017821 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017822 * Inherit the itemType.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017823 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017824 type->subtypes = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017825 } else if (WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017826 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017827 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017828 * NOTE that we won't assign the memberTypes of the base,
17829 * since this will make trouble when freeing them; we will
17830 * use a lookup function to access them instead.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017831 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017832 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017833 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017834 return(0);
17835}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017836
Daniel Veillard8651f532002-04-17 09:06:27 +000017837#ifdef DEBUG_TYPE
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017838xmlSchemaDebugFixedType(xmlSchemaParserCtxtPtr pctxt,
17839 xmlSchemaTypePtr type)
17840{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017841 if (type->node != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017842 xmlGenericError(xmlGenericErrorContext,
17843 "Type of %s : %s:%d :", name,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017844 type->node->doc->URL,
17845 xmlGetLineNo(type->node));
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017846 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017847 xmlGenericError(xmlGenericErrorContext, "Type of %s :", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017848 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017849 if ((WXS_IS_SIMPLE(type)) || (WXS_IS_COMPLEX(type))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017850 switch (type->contentType) {
17851 case XML_SCHEMA_CONTENT_SIMPLE:
17852 xmlGenericError(xmlGenericErrorContext, "simple\n");
17853 break;
17854 case XML_SCHEMA_CONTENT_ELEMENTS:
17855 xmlGenericError(xmlGenericErrorContext, "elements\n");
17856 break;
17857 case XML_SCHEMA_CONTENT_UNKNOWN:
17858 xmlGenericError(xmlGenericErrorContext, "unknown !!!\n");
17859 break;
17860 case XML_SCHEMA_CONTENT_EMPTY:
17861 xmlGenericError(xmlGenericErrorContext, "empty\n");
17862 break;
17863 case XML_SCHEMA_CONTENT_MIXED:
17864 if (xmlSchemaIsParticleEmptiable((xmlSchemaParticlePtr)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017865 type->subtypes))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017866 xmlGenericError(xmlGenericErrorContext,
17867 "mixed as emptiable particle\n");
17868 else
17869 xmlGenericError(xmlGenericErrorContext, "mixed\n");
17870 break;
17871 /* Removed, since not used. */
17872 /*
17873 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
17874 xmlGenericError(xmlGenericErrorContext, "mixed or elems\n");
17875 break;
17876 */
17877 case XML_SCHEMA_CONTENT_BASIC:
17878 xmlGenericError(xmlGenericErrorContext, "basic\n");
17879 break;
17880 default:
17881 xmlGenericError(xmlGenericErrorContext,
17882 "not registered !!!\n");
17883 break;
17884 }
Daniel Veillard8651f532002-04-17 09:06:27 +000017885 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017886}
Daniel Veillard8651f532002-04-17 09:06:27 +000017887#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017888
17889/*
17890* 3.14.6 Constraints on Simple Type Definition Schema Components
17891*/
17892static int
17893xmlSchemaFixupSimpleTypeStageTwo(xmlSchemaParserCtxtPtr pctxt,
17894 xmlSchemaTypePtr type)
17895{
17896 int res, olderrs = pctxt->nberrors;
17897
17898 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17899 return(-1);
17900
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017901 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017902 return(0);
17903
17904 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
17905 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
17906
17907 if (type->baseType == NULL) {
17908 PERROR_INT("xmlSchemaFixupSimpleTypeStageTwo",
17909 "missing baseType");
17910 goto exit_failure;
17911 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017912 if (WXS_IS_TYPE_NOT_FIXED(type->baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000017913 xmlSchemaTypeFixup(type->baseType, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017914 /*
17915 * If a member type of a union is a union itself, we need to substitute
17916 * that member type for its member types.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017917 * NOTE that this might change in WXS 1.1; i.e. we will keep the union
17918 * types in WXS 1.1.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017919 */
17920 if ((type->memberTypes != NULL) &&
17921 (xmlSchemaFinishMemberTypeDefinitionsProperty(pctxt, type) == -1))
17922 return(-1);
17923 /*
17924 * SPEC src-simple-type 1
17925 * "The corresponding simple type definition, if any, must satisfy
17926 * the conditions set out in Constraints on Simple Type Definition
17927 * Schema Components (§3.14.6)."
17928 */
17929 /*
17930 * Schema Component Constraint: Simple Type Definition Properties Correct
17931 * (st-props-correct)
17932 */
17933 res = xmlSchemaCheckSTPropsCorrect(pctxt, type);
17934 HFAILURE HERROR
17935 /*
17936 * Schema Component Constraint: Derivation Valid (Restriction, Simple)
17937 * (cos-st-restricts)
17938 */
17939 res = xmlSchemaCheckCOSSTRestricts(pctxt, type);
17940 HFAILURE HERROR
17941 /*
17942 * TODO: Removed the error report, since it got annoying to get an
17943 * extra error report, if anything failed until now.
17944 * Enable this if needed.
17945 *
17946 * xmlSchemaPErr(ctxt, type->node,
17947 * XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
17948 * "Simple type '%s' does not satisfy the constraints "
17949 * "on simple type definitions.\n",
17950 * type->name, NULL);
17951 */
17952 /*
17953 * Schema Component Constraint: Simple Type Restriction (Facets)
17954 * (st-restrict-facets)
17955 */
17956 res = xmlSchemaCheckFacetValues(type, pctxt);
17957 HFAILURE HERROR
17958 if ((type->facetSet != NULL) ||
17959 (type->baseType->facetSet != NULL)) {
17960 res = xmlSchemaDeriveAndValidateFacets(pctxt, type);
17961 HFAILURE HERROR
17962 }
17963 /*
17964 * Whitespace value.
17965 */
17966 res = xmlSchemaTypeFixupWhitespace(type);
17967 HFAILURE HERROR
17968 xmlSchemaTypeFixupOptimFacets(type);
17969
17970exit_error:
17971#ifdef DEBUG_TYPE
17972 xmlSchemaDebugFixedType(pctxt, type);
17973#endif
17974 if (olderrs != pctxt->nberrors)
17975 return(pctxt->err);
17976 return(0);
17977
17978exit_failure:
17979#ifdef DEBUG_TYPE
17980 xmlSchemaDebugFixedType(pctxt, type);
17981#endif
17982 return(-1);
17983}
17984
17985static int
17986xmlSchemaFixupComplexType(xmlSchemaParserCtxtPtr pctxt,
17987 xmlSchemaTypePtr type)
17988{
17989 int res = 0, olderrs = pctxt->nberrors;
17990 xmlSchemaTypePtr baseType = type->baseType;
17991
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017992 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017993 return(0);
17994 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
17995 if (baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017996 PERROR_INT("xmlSchemaFixupComplexType",
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017997 "missing baseType");
17998 goto exit_failure;
17999 }
18000 /*
18001 * Fixup the base type.
18002 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018003 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018004 xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018005 if (baseType->flags & XML_SCHEMAS_TYPE_INTERNAL_INVALID) {
18006 /*
18007 * Skip fixup if the base type is invalid.
18008 * TODO: Generate a warning!
18009 */
18010 return(0);
18011 }
18012 /*
18013 * This basically checks if the base type can be derived.
18014 */
18015 res = xmlSchemaCheckSRCCT(pctxt, type);
18016 HFAILURE HERROR
18017 /*
18018 * Fixup the content type.
18019 */
18020 if (type->contentType == XML_SCHEMA_CONTENT_SIMPLE) {
18021 /*
18022 * Corresponds to <complexType><simpleContent>...
18023 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018024 if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018025 (baseType->contentTypeDef != NULL) &&
18026 (WXS_IS_RESTRICTION(type))) {
18027 xmlSchemaTypePtr contentBase, content;
18028#ifdef ENABLE_NAMED_LOCALS
18029 char buf[30];
18030 const xmlChar *tmpname;
18031#endif
18032 /*
18033 * SPEC (1) If <restriction> + base type is <complexType>,
18034 * "whose own {content type} is a simple type..."
18035 */
18036 if (type->contentTypeDef != NULL) {
18037 /*
18038 * SPEC (1.1) "the simple type definition corresponding to the
18039 * <simpleType> among the [children] of <restriction> if there
18040 * is one;"
18041 * Note that this "<simpleType> among the [children]" was put
18042 * into ->contentTypeDef during parsing.
18043 */
18044 contentBase = type->contentTypeDef;
18045 type->contentTypeDef = NULL;
18046 } else {
18047 /*
18048 * (1.2) "...otherwise (<restriction> has no <simpleType>
18049 * among its [children]), the simple type definition which
18050 * is the {content type} of the ... base type."
18051 */
18052 contentBase = baseType->contentTypeDef;
18053 }
18054 /*
18055 * SPEC
18056 * "... a simple type definition which restricts the simple
18057 * type definition identified in clause 1.1 or clause 1.2
18058 * with a set of facet components"
18059 *
18060 * Create the anonymous simple type, which will be the content
18061 * type of the complex type.
18062 */
18063#ifdef ENABLE_NAMED_LOCALS
18064 snprintf(buf, 29, "#scST%d", ++(pctxt->counter));
18065 tmpname = xmlDictLookup(pctxt->dict, BAD_CAST buf, -1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018066 content = xmlSchemaAddType(pctxt, pctxt->schema,
18067 XML_SCHEMA_TYPE_SIMPLE, tmpname, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018068 type->node, 0);
18069#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018070 content = xmlSchemaAddType(pctxt, pctxt->schema,
18071 XML_SCHEMA_TYPE_SIMPLE, NULL, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018072 type->node, 0);
18073#endif
18074 if (content == NULL)
18075 goto exit_failure;
18076 /*
18077 * We will use the same node as for the <complexType>
18078 * to have it somehow anchored in the schema doc.
18079 */
18080 content->type = XML_SCHEMA_TYPE_SIMPLE;
18081 content->baseType = contentBase;
18082 /*
18083 * Move the facets, previously anchored on the
18084 * complexType during parsing.
18085 */
18086 content->facets = type->facets;
18087 type->facets = NULL;
18088 content->facetSet = type->facetSet;
18089 type->facetSet = NULL;
18090
18091 type->contentTypeDef = content;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018092 if (WXS_IS_TYPE_NOT_FIXED(contentBase))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018093 xmlSchemaTypeFixup(contentBase, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018094 /*
18095 * Fixup the newly created type. We don't need to check
18096 * for circularity here.
18097 */
18098 res = xmlSchemaFixupSimpleTypeStageOne(pctxt, content);
18099 HFAILURE HERROR
18100 res = xmlSchemaFixupSimpleTypeStageTwo(pctxt, content);
18101 HFAILURE HERROR
18102
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018103 } else if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018104 (baseType->contentType == XML_SCHEMA_CONTENT_MIXED) &&
18105 (WXS_IS_RESTRICTION(type))) {
18106 /*
18107 * SPEC (2) If <restriction> + base is a mixed <complexType> with
18108 * an emptiable particle, then a simple type definition which
18109 * restricts the <restriction>'s <simpleType> child.
18110 */
18111 if ((type->contentTypeDef == NULL) ||
18112 (type->contentTypeDef->baseType == NULL)) {
18113 /*
18114 * TODO: Check if this ever happens.
18115 */
18116 xmlSchemaPCustomErr(pctxt,
18117 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018118 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018119 "Internal error: xmlSchemaTypeFixup, "
18120 "complex type '%s': the <simpleContent><restriction> "
18121 "is missing a <simpleType> child, but was not catched "
18122 "by xmlSchemaCheckSRCCT()", type->name);
18123 goto exit_failure;
18124 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018125 } else if ((WXS_IS_COMPLEX(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018126 /*
18127 * SPEC (3) If <extension> + base is <complexType> with
18128 * <simpleType> content, "...then the {content type} of that
18129 * complex type definition"
18130 */
18131 if (baseType->contentTypeDef == NULL) {
18132 /*
18133 * TODO: Check if this ever happens. xmlSchemaCheckSRCCT
18134 * should have catched this already.
18135 */
18136 xmlSchemaPCustomErr(pctxt,
18137 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018138 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018139 "Internal error: xmlSchemaTypeFixup, "
18140 "complex type '%s': the <extension>ed base type is "
18141 "a complex type with no simple content type",
18142 type->name);
18143 goto exit_failure;
18144 }
18145 type->contentTypeDef = baseType->contentTypeDef;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018146 } else if ((WXS_IS_SIMPLE(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018147 /*
18148 * SPEC (4) <extension> + base is <simpleType>
18149 * "... then that simple type definition"
18150 */
18151 type->contentTypeDef = baseType;
18152 } else {
18153 /*
18154 * TODO: Check if this ever happens.
18155 */
18156 xmlSchemaPCustomErr(pctxt,
18157 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018158 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018159 "Internal error: xmlSchemaTypeFixup, "
18160 "complex type '%s' with <simpleContent>: unhandled "
18161 "derivation case", type->name);
18162 goto exit_failure;
18163 }
18164 } else {
18165 int dummySequence = 0;
18166 xmlSchemaParticlePtr particle =
18167 (xmlSchemaParticlePtr) type->subtypes;
18168 /*
18169 * Corresponds to <complexType><complexContent>...
18170 *
18171 * NOTE that the effective mixed was already set during parsing of
18172 * <complexType> and <complexContent>; its flag value is
18173 * XML_SCHEMAS_TYPE_MIXED.
18174 *
18175 * Compute the "effective content":
18176 * (2.1.1) + (2.1.2) + (2.1.3)
18177 */
18178 if ((particle == NULL) ||
18179 ((particle->type == XML_SCHEMA_TYPE_PARTICLE) &&
18180 ((particle->children->type == XML_SCHEMA_TYPE_ALL) ||
18181 (particle->children->type == XML_SCHEMA_TYPE_SEQUENCE) ||
18182 ((particle->children->type == XML_SCHEMA_TYPE_CHOICE) &&
18183 (particle->minOccurs == 0))) &&
18184 ( ((xmlSchemaTreeItemPtr) particle->children)->children == NULL))) {
18185 if (type->flags & XML_SCHEMAS_TYPE_MIXED) {
18186 /*
18187 * SPEC (2.1.4) "If the ·effective mixed· is true, then
18188 * a particle whose properties are as follows:..."
18189 *
18190 * Empty sequence model group with
18191 * minOccurs/maxOccurs = 1 (i.e. a "particle emptiable").
18192 * NOTE that we sill assign it the <complexType> node to
18193 * somehow anchor it in the doc.
18194 */
18195 if ((particle == NULL) ||
18196 (particle->children->type != XML_SCHEMA_TYPE_SEQUENCE)) {
18197 /*
18198 * Create the particle.
18199 */
18200 particle = xmlSchemaAddParticle(pctxt, pctxt->schema,
18201 type->node, 1, 1);
18202 if (particle == NULL)
18203 goto exit_failure;
18204 /*
18205 * Create the model group.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018206 */ /* URGENT TODO: avoid adding to pending items. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018207 particle->children = (xmlSchemaTreeItemPtr)
18208 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18209 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18210 if (particle->children == NULL)
18211 goto exit_failure;
18212
18213 type->subtypes = (xmlSchemaTypePtr) particle;
18214 }
18215 dummySequence = 1;
18216 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18217 } else {
18218 /*
18219 * SPEC (2.1.5) "otherwise empty"
18220 */
18221 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
18222 }
18223 } else {
18224 /*
18225 * SPEC (2.2) "otherwise the particle corresponding to the
18226 * <all>, <choice>, <group> or <sequence> among the
18227 * [children]."
18228 */
18229 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18230 }
18231 /*
18232 * Compute the "content type".
18233 */
18234 if (WXS_IS_RESTRICTION(type)) {
18235 /*
18236 * SPEC (3.1) "If <restriction>..."
18237 * (3.1.1) + (3.1.2) */
18238 if (type->contentType != XML_SCHEMA_CONTENT_EMPTY) {
18239 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18240 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18241 }
18242 } else {
18243 /*
18244 * SPEC (3.2) "If <extension>..."
18245 */
18246 if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18247 /*
18248 * SPEC (3.2.1)
18249 */
18250 type->contentType = baseType->contentType;
18251 type->subtypes = baseType->subtypes;
18252 /*
18253 * NOTE that the effective mixed is ignored here.
18254 */
18255 } else if (baseType->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18256 /*
18257 * SPEC (3.2.2)
18258 */
18259 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18260 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18261 } else {
18262 /*
18263 * SPEC (3.2.3)
18264 */
18265 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18266 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18267 /*
18268 * "A model group whose {compositor} is sequence and whose
18269 * {particles} are..."
18270 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018271 if ((WXS_TYPE_PARTICLE(type) != NULL) &&
18272 (WXS_TYPE_PARTICLE_TERM(type) != NULL) &&
18273 ((WXS_TYPE_PARTICLE_TERM(type))->type ==
18274 XML_SCHEMA_TYPE_ALL))
18275 {
18276 /*
18277 * SPEC cos-all-limited (1)
18278 */
18279 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18280 /* TODO: error code */
18281 XML_SCHEMAP_COS_ALL_LIMITED,
18282 WXS_ITEM_NODE(type), NULL,
18283 "The type has an 'all' model group in its "
18284 "{content type} and thus cannot be derived from "
18285 "a non-empty type, since this would produce a "
18286 "'sequence' model group containing the 'all' "
18287 "model group; 'all' model groups are not "
18288 "allowed to appear inside other model groups",
18289 NULL, NULL);
18290
18291 } else if ((WXS_TYPE_PARTICLE(baseType) != NULL) &&
18292 (WXS_TYPE_PARTICLE_TERM(baseType) != NULL) &&
18293 ((WXS_TYPE_PARTICLE_TERM(baseType))->type ==
18294 XML_SCHEMA_TYPE_ALL))
18295 {
18296 /*
18297 * SPEC cos-all-limited (1)
18298 */
18299 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18300 /* TODO: error code */
18301 XML_SCHEMAP_COS_ALL_LIMITED,
18302 WXS_ITEM_NODE(type), NULL,
18303 "A type cannot be derived by extension from a type "
18304 "which has an 'all' model group in its "
18305 "{content type}, since this would produce a "
18306 "'sequence' model group containing the 'all' "
18307 "model group; 'all' model groups are not "
18308 "allowed to appear inside other model groups",
18309 NULL, NULL);
18310
18311 } else if (! dummySequence) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018312 xmlSchemaTreeItemPtr effectiveContent =
18313 (xmlSchemaTreeItemPtr) type->subtypes;
18314 /*
18315 * Create the particle.
18316 */
18317 particle = xmlSchemaAddParticle(pctxt, pctxt->schema,
18318 type->node, 1, 1);
18319 if (particle == NULL)
18320 goto exit_failure;
18321 /*
18322 * Create the "sequence" model group.
18323 */
18324 particle->children = (xmlSchemaTreeItemPtr)
18325 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18326 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18327 if (particle->children == NULL)
18328 goto exit_failure;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018329 WXS_TYPE_CONTENTTYPE(type) = (xmlSchemaTypePtr) particle;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018330 /*
18331 * SPEC "the particle of the {content type} of
18332 * the ... base ..."
18333 * Create a duplicate of the base type's particle
18334 * and assign its "term" to it.
18335 */
18336 particle->children->children =
18337 (xmlSchemaTreeItemPtr) xmlSchemaAddParticle(pctxt,
18338 pctxt->schema, type->node,
18339 ((xmlSchemaParticlePtr) type->subtypes)->minOccurs,
18340 ((xmlSchemaParticlePtr) type->subtypes)->maxOccurs);
18341 if (particle->children->children == NULL)
18342 goto exit_failure;
18343 particle = (xmlSchemaParticlePtr)
18344 particle->children->children;
18345 particle->children =
18346 ((xmlSchemaParticlePtr) baseType->subtypes)->children;
18347 /*
18348 * SPEC "followed by the ·effective content·."
18349 */
18350 particle->next = effectiveContent;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018351 /*
18352 * This all will result in:
18353 * new-particle
18354 * --> new-sequence(
18355 * new-particle
18356 * --> base-model,
18357 * this-particle
18358 * --> this-model
18359 * )
18360 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018361 } else {
18362 /*
18363 * This is the case when there is already an empty
18364 * <sequence> with minOccurs==maxOccurs==1.
18365 * Just add the base types's content type.
18366 * NOTE that, although we miss to add an intermediate
18367 * <sequence>, this should produce no difference to
18368 * neither the regex compilation of the content model,
18369 * nor to the complex type contraints.
18370 */
18371 particle->children->children =
18372 (xmlSchemaTreeItemPtr) baseType->subtypes;
18373 }
18374 }
18375 }
18376 }
18377 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018378 * Now fixup attribute uses:
18379 * - expand attr. group references
18380 * - intersect attribute wildcards
18381 * - inherit attribute uses of the base type
18382 * - inherit or union attr. wildcards if extending
18383 * - apply attr. use prohibitions if restricting
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018384 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018385 res = xmlSchemaFixupTypeAttributeUses(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018386 HFAILURE HERROR
18387 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018388 * Apply the complex type component constraints; this will not
18389 * check attributes, since this is done in
18390 * xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018391 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018392 res = xmlSchemaCheckCTComponent(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018393 HFAILURE HERROR
18394
18395#ifdef DEBUG_TYPE
18396 xmlSchemaDebugFixedType(pctxt, type);
18397#endif
18398 if (olderrs != pctxt->nberrors)
18399 return(pctxt->err);
18400 else
18401 return(0);
18402
18403exit_error:
18404 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18405#ifdef DEBUG_TYPE
18406 xmlSchemaDebugFixedType(pctxt, type);
18407#endif
18408 return(pctxt->err);
18409
18410exit_failure:
18411 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18412#ifdef DEBUG_TYPE
18413 xmlSchemaDebugFixedType(pctxt, type);
18414#endif
18415 return(-1);
18416}
18417
18418
18419/**
18420 * xmlSchemaTypeFixup:
18421 * @typeDecl: the schema type definition
18422 * @ctxt: the schema parser context
18423 *
18424 * Fixes the content model of the type.
18425 * URGENT TODO: We need an int result!
18426 */
18427static int
18428xmlSchemaTypeFixup(xmlSchemaTypePtr type,
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018429 xmlSchemaAbstractCtxtPtr actxt)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018430{
18431 if (type == NULL)
18432 return(0);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018433 if (actxt->type != XML_SCHEMA_CTXT_PARSER) {
18434 AERROR_INT("xmlSchemaTypeFixup",
18435 "this function needs a parser context");
18436 return(-1);
18437 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018438 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018439 return(0);
18440 if (type->type == XML_SCHEMA_TYPE_COMPLEX)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018441 return(xmlSchemaFixupComplexType(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018442 else if (type->type == XML_SCHEMA_TYPE_SIMPLE)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018443 return(xmlSchemaFixupSimpleTypeStageTwo(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018444 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000018445}
18446
18447/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018448 * xmlSchemaCheckFacet:
18449 * @facet: the facet
18450 * @typeDecl: the schema type definition
Daniel Veillard81562d22005-06-15 13:27:56 +000018451 * @pctxt: the schema parser context or NULL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018452 * @name: the optional name of the type
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018453 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018454 * Checks and computes the values of facets.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018455 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018456 * Returns 0 if valid, a positive error code if not valid and
18457 * -1 in case of an internal or API error.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018458 */
18459int
18460xmlSchemaCheckFacet(xmlSchemaFacetPtr facet,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018461 xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018462 xmlSchemaParserCtxtPtr pctxt,
18463 const xmlChar * name ATTRIBUTE_UNUSED)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018464{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018465 int ret = 0, ctxtGiven;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018466
Daniel Veillardce682bc2004-11-05 17:22:25 +000018467 if ((facet == NULL) || (typeDecl == NULL))
18468 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018469 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018470 * TODO: will the parser context be given if used from
18471 * the relaxNG module?
18472 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018473 if (pctxt == NULL)
18474 ctxtGiven = 0;
18475 else
18476 ctxtGiven = 1;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018477
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018478 switch (facet->type) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018479 case XML_SCHEMA_FACET_MININCLUSIVE:
18480 case XML_SCHEMA_FACET_MINEXCLUSIVE:
18481 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018482 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
18483 case XML_SCHEMA_FACET_ENUMERATION: {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018484 /*
18485 * Okay we need to validate the value
18486 * at that point.
18487 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000018488 xmlSchemaTypePtr base;
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018489
18490 /* 4.3.5.5 Constraints on enumeration Schema Components
18491 * Schema Component Constraint: enumeration valid restriction
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018492 * It is an ·error· if any member of {value} is not in the
18493 * ·value space· of {base type definition}.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018494 *
18495 * minInclusive, maxInclusive, minExclusive, maxExclusive:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018496 * The value ·must· be in the
18497 * ·value space· of the ·base type·.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018498 */
18499 /*
18500 * This function is intended to deliver a compiled value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018501 * on the facet. In this implementation of XML Schemata the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018502 * type holding a facet, won't be a built-in type.
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018503 * Thus to ensure that other API
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018504 * calls (relaxng) do work, if the given type is a built-in
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018505 * type, we will assume that the given built-in type *is
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018506 * already* the base type.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018507 */
18508 if (typeDecl->type != XML_SCHEMA_TYPE_BASIC) {
18509 base = typeDecl->baseType;
18510 if (base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018511 PERROR_INT("xmlSchemaCheckFacet",
18512 "a type user derived type has no base type");
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018513 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018514 }
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018515 } else
18516 base = typeDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018517
18518 if (! ctxtGiven) {
18519 /*
18520 * A context is needed if called from RelaxNG.
18521 */
18522 pctxt = xmlSchemaNewParserCtxt("*");
18523 if (pctxt == NULL)
18524 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000018525 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018526 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018527 * NOTE: This call does not check the content nodes,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018528 * since they are not available:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018529 * facet->node is just the node holding the facet
18530 * definition, *not* the attribute holding the *value*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018531 * of the facet.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018532 */
18533 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018534 ACTXT_CAST pctxt, facet->node, base,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018535 facet->value, &(facet->val), 1, 1, 0);
18536 if (ret != 0) {
18537 if (ret < 0) {
18538 /* No error message for RelaxNG. */
18539 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018540 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018541 XML_SCHEMAP_INTERNAL, facet->node, NULL,
18542 "Internal error: xmlSchemaCheckFacet, "
18543 "failed to validate the value '%s' of the "
18544 "facet '%s' against the base type",
18545 facet->value, xmlSchemaFacetTypeToString(facet->type));
18546 }
18547 goto internal_error;
18548 }
18549 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18550 /* No error message for RelaxNG. */
18551 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018552 xmlChar *str = NULL;
18553
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018554 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018555 ret, facet->node, WXS_BASIC_CAST facet,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018556 "The value '%s' of the facet does not validate "
18557 "against the base type '%s'",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018558 facet->value,
18559 xmlSchemaFormatQName(&str,
18560 base->targetNamespace, base->name));
18561 FREE_AND_NULL(str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018562 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018563 goto exit;
18564 } else if (facet->val == NULL) {
18565 if (ctxtGiven) {
18566 PERROR_INT("xmlSchemaCheckFacet",
18567 "value was not computed");
18568 }
18569 TODO
18570 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018571 break;
18572 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018573 case XML_SCHEMA_FACET_PATTERN:
18574 facet->regexp = xmlRegexpCompile(facet->value);
18575 if (facet->regexp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018576 ret = XML_SCHEMAP_REGEXP_INVALID;
18577 /* No error message for RelaxNG. */
18578 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018579 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018580 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018581 "The value '%s' of the facet 'pattern' is not a "
18582 "valid regular expression",
18583 facet->value, NULL);
18584 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018585 }
18586 break;
18587 case XML_SCHEMA_FACET_TOTALDIGITS:
18588 case XML_SCHEMA_FACET_FRACTIONDIGITS:
18589 case XML_SCHEMA_FACET_LENGTH:
18590 case XML_SCHEMA_FACET_MAXLENGTH:
18591 case XML_SCHEMA_FACET_MINLENGTH:{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018592 ret = xmlSchemaValidatePredefinedType(
18593 xmlSchemaGetBuiltInType(XML_SCHEMAS_NNINTEGER),
18594 facet->value, &(facet->val));
18595 if (ret != 0) {
18596 if (ret < 0) {
18597 /* No error message for RelaxNG. */
18598 if (ctxtGiven) {
18599 PERROR_INT("xmlSchemaCheckFacet",
18600 "validating facet value");
18601 }
18602 goto internal_error;
18603 }
18604 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18605 /* No error message for RelaxNG. */
18606 if (ctxtGiven) {
18607 /* error code */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018608 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018609 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018610 "The value '%s' of the facet '%s' is not a valid "
18611 "'nonNegativeInteger'",
18612 facet->value,
18613 xmlSchemaFacetTypeToString(facet->type));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018614 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018615 }
18616 break;
18617 }
18618 case XML_SCHEMA_FACET_WHITESPACE:{
18619 if (xmlStrEqual(facet->value, BAD_CAST "preserve")) {
18620 facet->whitespace = XML_SCHEMAS_FACET_PRESERVE;
18621 } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) {
18622 facet->whitespace = XML_SCHEMAS_FACET_REPLACE;
18623 } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) {
18624 facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE;
18625 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018626 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18627 /* No error message for RelaxNG. */
18628 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018629 /* error was previously: XML_SCHEMAP_INVALID_WHITE_SPACE */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018630 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018631 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018632 "The value '%s' of the facet 'whitespace' is not "
18633 "valid", facet->value, NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018634 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018635 }
18636 }
18637 default:
18638 break;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018639 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018640exit:
18641 if ((! ctxtGiven) && (pctxt != NULL))
18642 xmlSchemaFreeParserCtxt(pctxt);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018643 return (ret);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018644internal_error:
18645 if ((! ctxtGiven) && (pctxt != NULL))
18646 xmlSchemaFreeParserCtxt(pctxt);
18647 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018648}
18649
18650/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018651 * xmlSchemaCheckFacetValues:
Daniel Veillard4255d502002-04-16 15:50:10 +000018652 * @typeDecl: the schema type definition
18653 * @ctxt: the schema parser context
18654 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018655 * Checks the default values types, especially for facets
Daniel Veillard4255d502002-04-16 15:50:10 +000018656 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018657static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018658xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018659 xmlSchemaParserCtxtPtr pctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000018660{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018661 int res, olderrs = pctxt->nberrors;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018662 const xmlChar *name = typeDecl->name;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018663 /*
18664 * NOTE: It is intended to use the facets list, instead
18665 * of facetSet.
18666 */
18667 if (typeDecl->facets != NULL) {
18668 xmlSchemaFacetPtr facet = typeDecl->facets;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018669
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018670 /*
18671 * Temporarily assign the "schema" to the validation context
18672 * of the parser context. This is needed for NOTATION validation.
18673 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018674 if (pctxt->vctxt == NULL) {
18675 if (xmlSchemaCreateVCtxtOnPCtxt(pctxt) == -1)
18676 return(-1);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018677 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018678 pctxt->vctxt->schema = pctxt->schema;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018679 while (facet != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018680 res = xmlSchemaCheckFacet(facet, typeDecl, pctxt, name);
18681 HFAILURE
Daniel Veillard01fa6152004-06-29 17:04:39 +000018682 facet = facet->next;
18683 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018684 pctxt->vctxt->schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018685 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018686 if (olderrs != pctxt->nberrors)
18687 return(pctxt->err);
18688 return(0);
18689exit_failure:
18690 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000018691}
18692
18693/**
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018694 * xmlSchemaGetCircModelGrDefRef:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018695 * @ctxtMGroup: the searched model group
18696 * @selfMGroup: the second searched model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018697 * @particle: the first particle
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018698 *
18699 * This one is intended to be used by
18700 * xmlSchemaCheckGroupDefCircular only.
18701 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018702 * Returns the particle with the circular model group definition reference,
18703 * otherwise NULL.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018704 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018705static xmlSchemaTreeItemPtr
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018706xmlSchemaGetCircModelGrDefRef(xmlSchemaModelGroupDefPtr groupDef,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018707 xmlSchemaTreeItemPtr particle)
18708{
18709 xmlSchemaTreeItemPtr circ = NULL;
18710 xmlSchemaTreeItemPtr term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018711 xmlSchemaModelGroupDefPtr gdef;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018712
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018713 for (; particle != NULL; particle = particle->next) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018714 term = particle->children;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018715 if (term == NULL)
18716 continue;
18717 switch (term->type) {
18718 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018719 gdef = (xmlSchemaModelGroupDefPtr) term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018720 if (gdef == groupDef)
18721 return (particle);
18722 /*
18723 * Mark this model group definition to avoid infinite
18724 * recursion on circular references not yet examined.
18725 */
18726 if (gdef->flags & XML_SCHEMA_MODEL_GROUP_DEF_MARKED)
18727 continue;
18728 if (gdef->children != NULL) {
18729 gdef->flags |= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18730 circ = xmlSchemaGetCircModelGrDefRef(groupDef,
18731 gdef->children->children);
18732 gdef->flags ^= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18733 if (circ != NULL)
18734 return (circ);
18735 }
18736 break;
18737 case XML_SCHEMA_TYPE_SEQUENCE:
18738 case XML_SCHEMA_TYPE_CHOICE:
18739 case XML_SCHEMA_TYPE_ALL:
18740 circ = xmlSchemaGetCircModelGrDefRef(groupDef, term->children);
18741 if (circ != NULL)
18742 return (circ);
18743 break;
18744 default:
18745 break;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018746 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018747 }
18748 return (NULL);
18749}
18750
18751/**
18752 * xmlSchemaCheckGroupDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018753 * @item: the model group definition
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018754 * @ctxt: the parser context
18755 * @name: the name
18756 *
18757 * Checks for circular references to model group definitions.
18758 */
18759static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018760xmlSchemaCheckGroupDefCircular(xmlSchemaModelGroupDefPtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018761 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018762{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018763 /*
18764 * Schema Component Constraint: Model Group Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018765 * 2 Circular groups are disallowed. That is, within the {particles}
18766 * of a group there must not be at any depth a particle whose {term}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018767 * is the group itself.
18768 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018769 if ((item == NULL) ||
18770 (item->type != XML_SCHEMA_TYPE_GROUP) ||
18771 (item->children == NULL))
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018772 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018773 {
18774 xmlSchemaTreeItemPtr circ;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018775
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018776 circ = xmlSchemaGetCircModelGrDefRef(item, item->children->children);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018777 if (circ != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018778 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018779 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018780 * TODO: The error report is not adequate: this constraint
18781 * is defined for model groups but not definitions, but since
18782 * there cannot be any circular model groups without a model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018783 * definition (if not using a construction API), we check those
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018784 * defintions only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018785 */
18786 xmlSchemaPCustomErr(ctxt,
18787 XML_SCHEMAP_MG_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018788 NULL, WXS_ITEM_NODE(circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018789 "Circular reference to the model group definition '%s' "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018790 "defined", xmlSchemaFormatQName(&str,
18791 item->targetNamespace, item->name));
18792 FREE_AND_NULL(str)
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018793 /*
18794 * NOTE: We will cut the reference to avoid further
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018795 * confusion of the processor. This is a fatal error.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018796 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018797 circ->children = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018798 }
18799 }
18800}
18801
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018802/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018803 * xmlSchemaModelGroupToModelGroupDefFixup:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018804 * @ctxt: the parser context
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018805 * @mg: the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018806 *
18807 * Assigns the model group of model group definitions to the "term"
18808 * of the referencing particle.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018809 * In xmlSchemaResolveModelGroupParticleReferences the model group
18810 * definitions were assigned to the "term", since needed for the
18811 * circularity check.
18812 *
18813 * Schema Component Constraint:
18814 * All Group Limited (cos-all-limited) (1.2)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018815 */
18816static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018817xmlSchemaModelGroupToModelGroupDefFixup(
18818 xmlSchemaParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
18819 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018820{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018821 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
18822
18823 while (particle != NULL) {
18824 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
18825 ((WXS_PARTICLE_TERM(particle))->type !=
18826 XML_SCHEMA_TYPE_GROUP))
18827 {
18828 particle = WXS_PTC_CAST particle->next;
18829 continue;
18830 }
18831 if (WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle)) == NULL) {
18832 /*
18833 * TODO: Remove the particle.
18834 */
18835 WXS_PARTICLE_TERM(particle) = NULL;
18836 particle = WXS_PTC_CAST particle->next;
18837 continue;
18838 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018839 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018840 * Assign the model group to the {term} of the particle.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018841 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018842 WXS_PARTICLE_TERM(particle) =
18843 WXS_TREE_CAST WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle));
18844
18845 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018846 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018847}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018848
18849/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018850 * xmlSchemaCheckAttrGroupCircularRecur:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018851 * @ctxtGr: the searched attribute group
18852 * @attr: the current attribute list to be processed
18853 *
18854 * This one is intended to be used by
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018855 * xmlSchemaCheckAttrGroupCircular only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018856 *
18857 * Returns the circular attribute grou reference, otherwise NULL.
18858 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018859static xmlSchemaQNameRefPtr
18860xmlSchemaCheckAttrGroupCircularRecur(xmlSchemaAttributeGroupPtr ctxtGr,
18861 xmlSchemaItemListPtr list)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018862{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018863 xmlSchemaAttributeGroupPtr gr;
18864 xmlSchemaQNameRefPtr ref, circ;
18865 int i;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018866 /*
18867 * We will search for an attribute group reference which
18868 * references the context attribute group.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018869 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018870 for (i = 0; i < list->nbItems; i++) {
18871 ref = list->items[i];
18872 if ((ref->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
18873 (ref->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
18874 (ref->item != NULL))
18875 {
18876 gr = WXS_ATTR_GROUP_CAST ref->item;
18877 if (gr == ctxtGr)
18878 return(ref);
18879 if (gr->flags & XML_SCHEMAS_ATTRGROUP_MARKED)
18880 continue;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018881 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018882 * Mark as visited to avoid infinite recursion on
18883 * circular references not yet examined.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018884 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018885 if ((gr->attrUses) &&
18886 (gr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS))
18887 {
18888 gr->flags |= XML_SCHEMAS_ATTRGROUP_MARKED;
18889 circ = xmlSchemaCheckAttrGroupCircularRecur(ctxtGr,
18890 (xmlSchemaItemListPtr) gr->attrUses);
18891 gr->flags ^= XML_SCHEMAS_ATTRGROUP_MARKED;
18892 if (circ != NULL)
18893 return (circ);
18894 }
18895
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018896 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018897 }
18898 return (NULL);
18899}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018900
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018901/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018902 * xmlSchemaCheckAttrGroupCircular:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018903 * attrGr: the attribute group definition
18904 * @ctxt: the parser context
18905 * @name: the name
18906 *
18907 * Checks for circular references of attribute groups.
18908 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018909static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018910xmlSchemaCheckAttrGroupCircular(xmlSchemaAttributeGroupPtr attrGr,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018911 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018912{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018913 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018914 * Schema Representation Constraint:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018915 * Attribute Group Definition Representation OK
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018916 * 3 Circular group reference is disallowed outside <redefine>.
18917 * That is, unless this element information item's parent is
18918 * <redefine>, then among the [children], if any, there must
18919 * not be an <attributeGroup> with ref [attribute] which resolves
18920 * to the component corresponding to this <attributeGroup>. Indirect
18921 * circularity is also ruled out. That is, when QName resolution
18922 * (Schema Document) (§3.15.3) is applied to a ·QName· arising from
18923 * any <attributeGroup>s with a ref [attribute] among the [children],
18924 * it must not be the case that a ·QName· is encountered at any depth
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018925 * which resolves to the component corresponding to this <attributeGroup>.
18926 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018927 if (attrGr->attrUses == NULL)
18928 return(0);
18929 else if ((attrGr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS) == 0)
18930 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018931 else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018932 xmlSchemaQNameRefPtr circ;
18933
18934 circ = xmlSchemaCheckAttrGroupCircularRecur(attrGr,
18935 (xmlSchemaItemListPtr) attrGr->attrUses);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018936 if (circ != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018937 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018938 /*
18939 * TODO: Report the referenced attr group as QName.
18940 */
18941 xmlSchemaPCustomErr(ctxt,
18942 XML_SCHEMAP_SRC_ATTRIBUTE_GROUP_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018943 NULL, WXS_ITEM_NODE(WXS_BASIC_CAST circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018944 "Circular reference to the attribute group '%s' "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018945 "defined", xmlSchemaGetComponentQName(&str, attrGr));
18946 FREE_AND_NULL(str);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018947 /*
18948 * NOTE: We will cut the reference to avoid further
18949 * confusion of the processor.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018950 * BADSPEC TODO: The spec should define how to process in this case.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018951 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018952 circ->item = NULL;
18953 return(ctxt->err);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018954 }
18955 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018956 return(0);
18957}
18958
18959static int
18960xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
18961 xmlSchemaAttributeGroupPtr attrGr);
18962
18963/**
18964 * xmlSchemaExpandAttributeGroupRefs:
18965 * @pctxt: the parser context
18966 * @node: the node of the component holding the attribute uses
18967 * @completeWild: the intersected wildcard to be returned
18968 * @list: the attribute uses
18969 *
18970 * Substitutes contained attribute group references
18971 * for their attribute uses. Wilcards are intersected.
18972 * Attribute use prohibitions are removed from the list
18973 * and returned via the @prohibs list.
18974 * Pointlessness of attr. prohibs, if a matching attr. decl
18975 * is existent a well, are checked.
18976 */
18977static int
18978xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
18979 xmlSchemaBasicItemPtr item,
18980 xmlSchemaWildcardPtr *completeWild,
18981 xmlSchemaItemListPtr list,
18982 xmlSchemaItemListPtr prohibs)
18983{
18984 xmlSchemaAttributeGroupPtr gr;
18985 xmlSchemaAttributeUsePtr use;
18986 xmlSchemaItemListPtr sublist;
18987 int i, j;
18988 int created = (*completeWild == NULL) ? 0 : 1;
18989
18990 if (prohibs)
18991 prohibs->nbItems = 0;
18992
18993 for (i = 0; i < list->nbItems; i++) {
18994 use = list->items[i];
18995
18996 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
18997 if (prohibs == NULL) {
18998 PERROR_INT("xmlSchemaExpandAttributeGroupRefs",
18999 "unexpected attr prohibition found");
19000 return(-1);
19001 }
19002 /*
19003 * Remove from attribute uses.
19004 */
19005 if (xmlSchemaItemListRemove(list, i) == -1)
19006 return(-1);
19007 i--;
19008 /*
19009 * Note that duplicate prohibitions were already
19010 * handled at parsing time.
19011 */
19012 /*
19013 * Add to list of prohibitions.
19014 */
19015 xmlSchemaItemListAddSize(prohibs, 2, use);
19016 continue;
19017 }
19018 if ((use->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
19019 ((WXS_QNAME_CAST use)->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP))
19020 {
19021 if ((WXS_QNAME_CAST use)->item == NULL)
19022 return(-1);
19023 gr = WXS_ATTR_GROUP_CAST (WXS_QNAME_CAST use)->item;
19024 /*
19025 * Expand the referenced attr. group.
19026 * TODO: remove this, this is done in a previous step, so
19027 * already done here.
19028 */
19029 if ((gr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED) == 0) {
19030 if (xmlSchemaAttributeGroupExpandRefs(pctxt, gr) == -1)
19031 return(-1);
19032 }
19033 /*
19034 * Build the 'complete' wildcard; i.e. intersect multiple
19035 * wildcards.
19036 */
19037 if (gr->attributeWildcard != NULL) {
19038 if (*completeWild == NULL) {
19039 *completeWild = gr->attributeWildcard;
19040 } else {
19041 if (! created) {
19042 xmlSchemaWildcardPtr tmpWild;
19043
19044 /*
19045 * Copy the first encountered wildcard as context,
19046 * except for the annotation.
19047 *
19048 * Although the complete wildcard might not correspond
19049 * to any node in the schema, we will anchor it on
19050 * the node of the owner component.
19051 */
19052 tmpWild = xmlSchemaAddWildcard(pctxt, pctxt->schema,
19053 XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
19054 WXS_ITEM_NODE(item));
19055 if (tmpWild == NULL)
19056 return(-1);
19057 if (xmlSchemaCloneWildcardNsConstraints(pctxt,
19058 tmpWild, *completeWild) == -1)
19059 return (-1);
19060 tmpWild->processContents = (*completeWild)->processContents;
19061 *completeWild = tmpWild;
19062 created = 1;
19063 }
19064
19065 if (xmlSchemaIntersectWildcards(pctxt, *completeWild,
19066 gr->attributeWildcard) == -1)
19067 return(-1);
19068 }
19069 }
19070 /*
19071 * Just remove the reference if the referenced group does not
19072 * contain any attribute uses.
19073 */
19074 if (gr->attrUses == NULL) {
19075 if (xmlSchemaItemListRemove(list, i) == -1)
19076 return(-1);
19077 i--;
19078 continue;
19079 }
19080 /*
19081 * Add the attribute uses.
19082 */
19083 sublist = ((xmlSchemaItemListPtr) gr->attrUses);
19084 if (sublist->nbItems != 0) {
19085 list->items[i] = sublist->items[0];
19086 if (sublist->nbItems != 1) {
19087 for (j = 1; j < sublist->nbItems; j++) {
19088 i++;
19089 if (xmlSchemaItemListInsert(list,
19090 sublist->items[j], i) == -1)
19091 return(-1);
19092 }
19093 }
19094 }
19095 }
19096
19097 }
19098 /*
19099 * Handle pointless prohibitions of declared attributes.
19100 */
19101 if (prohibs && (prohibs->nbItems != 0) && (list->nbItems != 0)) {
19102 xmlSchemaAttributeUseProhibPtr prohib;
19103
19104 for (i = prohibs->nbItems -1; i >= 0; i--) {
19105 prohib = prohibs->items[i];
19106 for (j = 0; j < list->nbItems; j++) {
19107 use = list->items[j];
19108
19109 if ((prohib->name == WXS_ATTRUSE_DECL_NAME(use)) &&
19110 (prohib->targetNamespace == WXS_ATTRUSE_DECL_TNS(use)))
19111 {
19112 xmlChar *str = NULL;
19113
19114 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
19115 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
19116 prohib->node, NULL,
19117 "Skipping pointless attribute use prohibition "
19118 "'%s', since a corresponding attribute use "
19119 "exists already in the type definition",
19120 xmlSchemaFormatQName(&str,
19121 prohib->targetNamespace, prohib->name),
19122 NULL, NULL);
19123 FREE_AND_NULL(str);
19124 /*
19125 * Remove the prohibition.
19126 */
19127 if (xmlSchemaItemListRemove(prohibs, i) == -1)
19128 return(-1);
19129 break;
19130 }
19131 }
19132 }
19133 }
19134 return(0);
19135}
19136
19137/**
19138 * xmlSchemaAttributeGroupExpandRefs:
19139 * @pctxt: the parser context
19140 * @attrGr: the attribute group definition
19141 *
19142 * Computation of:
19143 * {attribute uses} property
19144 * {attribute wildcard} property
19145 *
19146 * Substitutes contained attribute group references
19147 * for their attribute uses. Wilcards are intersected.
19148 */
19149static int
19150xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19151 xmlSchemaAttributeGroupPtr attrGr)
19152{
19153 if ((attrGr->attrUses == NULL) ||
19154 (attrGr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED))
19155 return(0);
19156
19157 attrGr->flags |= XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED;
19158 if (xmlSchemaExpandAttributeGroupRefs(pctxt, WXS_BASIC_CAST attrGr,
19159 &(attrGr->attributeWildcard), attrGr->attrUses, NULL) == -1)
19160 return(-1);
19161 return(0);
19162}
19163
19164/**
19165 * xmlSchemaAttributeGroupExpandRefs:
19166 * @pctxt: the parser context
19167 * @attrGr: the attribute group definition
19168 *
19169 * Substitutes contained attribute group references
19170 * for their attribute uses. Wilcards are intersected.
19171 *
19172 * Schema Component Constraint:
19173 * Attribute Group Definition Properties Correct (ag-props-correct)
19174 */
19175static int
19176xmlSchemaCheckAGPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19177 xmlSchemaAttributeGroupPtr attrGr)
19178{
19179 /*
19180 * SPEC ag-props-correct
19181 * (1) "The values of the properties of an attribute group definition
19182 * must be as described in the property tableau in The Attribute
19183 * Group Definition Schema Component (§3.6.1), modulo the impact of
19184 * Missing Sub-components (§5.3);"
19185 */
19186
19187 if ((attrGr->attrUses != NULL) &&
19188 (WXS_LIST_CAST attrGr->attrUses)->nbItems > 1)
19189 {
19190 xmlSchemaItemListPtr uses = WXS_LIST_CAST attrGr->attrUses;
19191 xmlSchemaAttributeUsePtr use, tmp;
19192 int i, j, hasId = 0;
19193
19194 for (i = uses->nbItems -1; i >= 0; i--) {
19195 use = uses->items[i];
19196 /*
19197 * SPEC ag-props-correct
19198 * (2) "Two distinct members of the {attribute uses} must not have
19199 * {attribute declaration}s both of whose {name}s match and whose
19200 * {target namespace}s are identical."
19201 */
19202 if (i > 0) {
19203 for (j = i -1; j >= 0; j--) {
19204 tmp = uses->items[j];
19205 if ((WXS_ATTRUSE_DECL_NAME(use) ==
19206 WXS_ATTRUSE_DECL_NAME(tmp)) &&
19207 (WXS_ATTRUSE_DECL_TNS(use) ==
19208 WXS_ATTRUSE_DECL_TNS(tmp)))
19209 {
19210 xmlChar *str = NULL;
19211
19212 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19213 XML_SCHEMAP_AG_PROPS_CORRECT,
19214 attrGr->node, WXS_BASIC_CAST attrGr,
19215 "Duplicate %s",
19216 xmlSchemaGetComponentDesignation(&str, use),
19217 NULL);
19218 FREE_AND_NULL(str);
19219 /*
19220 * Remove the duplicate.
19221 */
19222 if (xmlSchemaItemListRemove(uses, i) == -1)
19223 return(-1);
19224 goto next_use;
19225 }
19226 }
19227 }
19228 /*
19229 * SPEC ag-props-correct
19230 * (3) "Two distinct members of the {attribute uses} must not have
19231 * {attribute declaration}s both of whose {type definition}s are or
19232 * are derived from ID."
19233 * TODO: Does 'derived' include member-types of unions?
19234 */
19235 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
19236 if (xmlSchemaIsDerivedFromBuiltInType(
19237 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
19238 {
19239 if (hasId) {
19240 xmlChar *str = NULL;
19241
19242 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19243 XML_SCHEMAP_AG_PROPS_CORRECT,
19244 attrGr->node, WXS_BASIC_CAST attrGr,
19245 "There must not exist more than one attribute "
19246 "declaration of type 'xs:ID' "
19247 "(or derived from 'xs:ID'). The %s violates this "
19248 "constraint",
19249 xmlSchemaGetComponentDesignation(&str, use),
19250 NULL);
19251 FREE_AND_NULL(str);
19252 if (xmlSchemaItemListRemove(uses, i) == -1)
19253 return(-1);
19254 }
19255 hasId = 1;
19256 }
19257 }
19258next_use: {}
19259 }
19260 }
19261 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019262}
19263
19264/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019265 * xmlSchemaResolveAttrGroupReferences:
Daniel Veillard13e04c62002-04-23 17:51:29 +000019266 * @attrgrpDecl: the schema attribute definition
19267 * @ctxt: the schema parser context
19268 * @name: the attribute name
19269 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019270 * Resolves references to attribute group definitions.
Daniel Veillard13e04c62002-04-23 17:51:29 +000019271 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019272static int
19273xmlSchemaResolveAttrGroupReferences(xmlSchemaQNameRefPtr ref,
19274 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard13e04c62002-04-23 17:51:29 +000019275{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019276 xmlSchemaAttributeGroupPtr group;
Daniel Veillard13e04c62002-04-23 17:51:29 +000019277
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019278 if (ref->item != NULL)
19279 return(0);
19280 group = xmlSchemaGetAttributeGroup(ctxt->schema,
19281 ref->name,
19282 ref->targetNamespace);
19283 if (group == NULL) {
19284 xmlSchemaPResCompAttrErr(ctxt,
19285 XML_SCHEMAP_SRC_RESOLVE,
19286 NULL, ref->node,
19287 "ref", ref->name, ref->targetNamespace,
19288 ref->itemType, NULL);
19289 return(ctxt->err);
Daniel Veillard3646d642004-06-02 19:19:14 +000019290 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019291 ref->item = WXS_BASIC_CAST group;
19292 return(0);
Daniel Veillard13e04c62002-04-23 17:51:29 +000019293}
19294
19295/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019296 * xmlSchemaCheckAttrPropsCorrect:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019297 * @item: an schema attribute declaration/use
19298 * @ctxt: a schema parser context
19299 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019300 *
19301 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019302 * Schema Component Constraint:
19303 * Attribute Declaration Properties Correct (a-props-correct)
Daniel Veillard4255d502002-04-16 15:50:10 +000019304 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019305 * Validates the value constraints of an attribute declaration/use.
19306 * NOTE that this needs the simle type definitions to be already
19307 * builded and checked.
Daniel Veillard4255d502002-04-16 15:50:10 +000019308 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019309static int
19310xmlSchemaCheckAttrPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19311 xmlSchemaAttributePtr attr)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019312{
19313
19314 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019315 * SPEC a-props-correct (1)
19316 * "The values of the properties of an attribute declaration must
19317 * be as described in the property tableau in The Attribute
19318 * Declaration Schema Component (§3.2.1), modulo the impact of
19319 * Missing Sub-components (§5.3)."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019320 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019321
19322 if (WXS_ATTR_TYPEDEF(attr) == NULL)
19323 return(0);
19324
19325 if (attr->defValue != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019326 int ret;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019327
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019328 /*
19329 * SPEC a-props-correct (3)
19330 * "If the {type definition} is or is derived from ID then there
19331 * must not be a {value constraint}."
19332 */
19333 if (xmlSchemaIsDerivedFromBuiltInType(
19334 WXS_ATTR_TYPEDEF(attr), XML_SCHEMAS_ID))
19335 {
19336 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19337 XML_SCHEMAP_A_PROPS_CORRECT_3,
19338 NULL, WXS_BASIC_CAST attr,
19339 "Value constraints are not allowed if the type definition "
19340 "is or is derived from xs:ID",
19341 NULL, NULL);
19342 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019343 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019344 /*
19345 * SPEC a-props-correct (2)
19346 * "if there is a {value constraint}, the canonical lexical
19347 * representation of its value must be ·valid· with respect
19348 * to the {type definition} as defined in String Valid (§3.14.4)."
19349 * TODO: Don't care about the *cononical* stuff here, this requirement
19350 * will be removed in WXS 1.1 anyway.
19351 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019352 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019353 attr->node, WXS_ATTR_TYPEDEF(attr),
19354 attr->defValue, &(attr->defVal),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019355 1, 1, 0);
19356 if (ret != 0) {
19357 if (ret < 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019358 PERROR_INT("xmlSchemaCheckAttrPropsCorrect",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019359 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019360 return(-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019361 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019362 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019363 XML_SCHEMAP_A_PROPS_CORRECT_2,
19364 NULL, WXS_BASIC_CAST attr,
19365 "The value of the value constraint is not valid",
19366 NULL, NULL);
19367 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019368 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019369 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019370
19371 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019372}
19373
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019374static xmlSchemaElementPtr
19375xmlSchemaCheckSubstGroupCircular(xmlSchemaElementPtr elemDecl,
19376 xmlSchemaElementPtr ancestor)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019377{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019378 xmlSchemaElementPtr ret;
19379
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019380 if (WXS_SUBST_HEAD(ancestor) == NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019381 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019382 if (WXS_SUBST_HEAD(ancestor) == elemDecl)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019383 return (ancestor);
19384
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019385 if (WXS_SUBST_HEAD(ancestor)->flags & XML_SCHEMAS_ELEM_CIRCULAR)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019386 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019387 WXS_SUBST_HEAD(ancestor)->flags |= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019388 ret = xmlSchemaCheckSubstGroupCircular(elemDecl,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019389 WXS_SUBST_HEAD(ancestor));
19390 WXS_SUBST_HEAD(ancestor)->flags ^= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019391
19392 return (ret);
19393}
19394
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019395/**
19396 * xmlSchemaCheckElemPropsCorrect:
19397 * @ctxt: a schema parser context
19398 * @decl: the element declaration
19399 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019400 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019401 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019402 * Element Declaration Properties Correct (e-props-correct)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019403 *
19404 * STATUS:
19405 * missing: (6)
19406 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019407static int
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019408xmlSchemaCheckElemPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19409 xmlSchemaElementPtr elemDecl)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019410{
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019411 int ret = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019412 xmlSchemaTypePtr typeDef = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019413 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019414 * SPEC (1) "The values of the properties of an element declaration
19415 * must be as described in the property tableau in The Element
19416 * Declaration Schema Component (§3.3.1), modulo the impact of Missing
19417 * Sub-components (§5.3)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019418 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019419 if (WXS_SUBST_HEAD(elemDecl) != NULL) {
19420 xmlSchemaElementPtr head = WXS_SUBST_HEAD(elemDecl), circ;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019421
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019422 xmlSchemaCheckElementDeclComponent(head, pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019423 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019424 * SPEC (3) "If there is a non-·absent· {substitution group
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019425 * affiliation}, then {scope} must be global."
19426 */
19427 if ((elemDecl->flags & XML_SCHEMAS_ELEM_GLOBAL) == 0) {
19428 xmlSchemaPCustomErr(pctxt,
19429 XML_SCHEMAP_E_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019430 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019431 "Only global element declarations can have a "
19432 "substitution group affiliation", NULL);
19433 ret = XML_SCHEMAP_E_PROPS_CORRECT_3;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019434 }
19435 /*
19436 * TODO: SPEC (6) "Circular substitution groups are disallowed.
19437 * That is, it must not be possible to return to an element declaration
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019438 * by repeatedly following the {substitution group affiliation}
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019439 * property."
19440 */
19441 if (head == elemDecl)
19442 circ = head;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019443 else if (WXS_SUBST_HEAD(head) != NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019444 circ = xmlSchemaCheckSubstGroupCircular(head, head);
19445 else
19446 circ = NULL;
19447 if (circ != NULL) {
19448 xmlChar *strA = NULL, *strB = NULL;
19449
19450 xmlSchemaPCustomErrExt(pctxt,
19451 XML_SCHEMAP_E_PROPS_CORRECT_6,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019452 WXS_BASIC_CAST circ, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019453 "The element declaration '%s' defines a circular "
19454 "substitution group to element declaration '%s'",
19455 xmlSchemaGetComponentQName(&strA, circ),
19456 xmlSchemaGetComponentQName(&strB, head),
19457 NULL);
19458 FREE_AND_NULL(strA)
19459 FREE_AND_NULL(strB)
19460 ret = XML_SCHEMAP_E_PROPS_CORRECT_6;
19461 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019462 /*
19463 * SPEC (4) "If there is a {substitution group affiliation},
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019464 * the {type definition}
19465 * of the element declaration must be validly derived from the {type
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019466 * definition} of the {substitution group affiliation}, given the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019467 * of the {substitution group exclusions} of the {substitution group
19468 * affiliation}, as defined in Type Derivation OK (Complex) (§3.4.6)
19469 * (if the {type definition} is complex) or as defined in
19470 * Type Derivation OK (Simple) (§3.14.6) (if the {type definition} is
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019471 * simple)."
19472 *
19473 * NOTE: {substitution group exclusions} means the values of the
19474 * attribute "final".
19475 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019476
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019477 if (typeDef != WXS_ELEM_TYPEDEF(WXS_SUBST_HEAD(elemDecl))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019478 int set = 0;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019479
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019480 if (head->flags & XML_SCHEMAS_ELEM_FINAL_EXTENSION)
19481 set |= SUBSET_EXTENSION;
19482 if (head->flags & XML_SCHEMAS_ELEM_FINAL_RESTRICTION)
19483 set |= SUBSET_RESTRICTION;
19484
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000019485 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST pctxt, typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019486 WXS_ELEM_TYPEDEF(head), set) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019487 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
19488
19489 ret = XML_SCHEMAP_E_PROPS_CORRECT_4;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019490 xmlSchemaPCustomErrExt(pctxt,
19491 XML_SCHEMAP_E_PROPS_CORRECT_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019492 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019493 "The type definition '%s' was "
19494 "either rejected by the substitution group "
19495 "affiliation '%s', or not validly derived from its type "
19496 "definition '%s'",
19497 xmlSchemaGetComponentQName(&strA, typeDef),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019498 xmlSchemaGetComponentQName(&strB, head),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019499 xmlSchemaGetComponentQName(&strC, WXS_ELEM_TYPEDEF(head)));
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019500 FREE_AND_NULL(strA)
19501 FREE_AND_NULL(strB)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019502 FREE_AND_NULL(strC)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019503 }
19504 }
19505 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019506 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019507 * SPEC (5) "If the {type definition} or {type definition}'s
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019508 * {content type}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019509 * is or is derived from ID then there must not be a {value constraint}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019510 * Note: The use of ID as a type definition for elements goes beyond
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019511 * XML 1.0, and should be avoided if backwards compatibility is desired"
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019512 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019513 if ((elemDecl->value != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019514 ((WXS_IS_SIMPLE(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019515 xmlSchemaIsDerivedFromBuiltInType(typeDef, XML_SCHEMAS_ID)) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019516 (WXS_IS_COMPLEX(typeDef) &&
19517 WXS_HAS_SIMPLE_CONTENT(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019518 xmlSchemaIsDerivedFromBuiltInType(typeDef->contentTypeDef,
19519 XML_SCHEMAS_ID)))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019520
19521 ret = XML_SCHEMAP_E_PROPS_CORRECT_5;
19522 xmlSchemaPCustomErr(pctxt,
19523 XML_SCHEMAP_E_PROPS_CORRECT_5,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019524 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019525 "The type definition (or type definition's content type) is or "
19526 "is derived from ID; value constraints are not allowed in "
19527 "conjunction with such a type definition", NULL);
19528 } else if (elemDecl->value != NULL) {
19529 int vcret;
19530 xmlNodePtr node = NULL;
19531
19532 /*
19533 * SPEC (2) "If there is a {value constraint}, the canonical lexical
19534 * representation of its value must be ·valid· with respect to the
19535 * {type definition} as defined in Element Default Valid (Immediate)
19536 * (§3.3.6)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019537 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019538 if (typeDef == NULL) {
19539 xmlSchemaPErr(pctxt, elemDecl->node,
19540 XML_SCHEMAP_INTERNAL,
19541 "Internal error: xmlSchemaCheckElemPropsCorrect, "
19542 "type is missing... skipping validation of "
19543 "the value constraint", NULL, NULL);
19544 return (-1);
19545 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019546 if (elemDecl->node != NULL) {
19547 if (elemDecl->flags & XML_SCHEMAS_ELEM_FIXED)
19548 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19549 BAD_CAST "fixed");
19550 else
19551 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19552 BAD_CAST "default");
19553 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019554 vcret = xmlSchemaParseCheckCOSValidDefault(pctxt, node,
19555 typeDef, elemDecl->value, &(elemDecl->defVal));
19556 if (vcret != 0) {
19557 if (vcret < 0) {
19558 PERROR_INT("xmlSchemaElemCheckValConstr",
19559 "failed to validate the value constraint of an "
19560 "element declaration");
19561 return (-1);
19562 }
19563 return (vcret);
19564 }
19565 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019566
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019567 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019568}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019569
19570/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019571 * xmlSchemaCheckElemSubstGroup:
19572 * @ctxt: a schema parser context
19573 * @decl: the element declaration
19574 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019575 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019576 * Schema Component Constraint:
19577 * Substitution Group (cos-equiv-class)
19578 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019579 * In Libxml2 the subst. groups will be precomputed, in terms of that
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019580 * a list will be built for each subst. group head, holding all direct
19581 * referents to this head.
19582 * NOTE that this function needs:
19583 * 1. circular subst. groups to be checked beforehand
19584 * 2. the declaration's type to be derived from the head's type
19585 *
19586 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019587 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019588 */
19589static void
19590xmlSchemaCheckElemSubstGroup(xmlSchemaParserCtxtPtr ctxt,
19591 xmlSchemaElementPtr elemDecl)
19592{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019593 if ((WXS_SUBST_HEAD(elemDecl) == NULL) ||
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019594 /* SPEC (1) "Its {abstract} is false." */
19595 (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT))
19596 return;
19597 {
19598 xmlSchemaElementPtr head;
19599 xmlSchemaTypePtr headType, type;
19600 int set, methSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019601 /*
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019602 * SPEC (2) "It is validly substitutable for HEAD subject to HEAD's
19603 * {disallowed substitutions} as the blocking constraint, as defined in
19604 * Substitution Group OK (Transitive) (§3.3.6)."
19605 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019606 for (head = WXS_SUBST_HEAD(elemDecl); head != NULL;
19607 head = WXS_SUBST_HEAD(head)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019608 set = 0;
19609 methSet = 0;
19610 /*
19611 * The blocking constraints.
19612 */
19613 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION)
19614 continue;
19615 headType = head->subtypes;
19616 type = elemDecl->subtypes;
19617 if (headType == type)
19618 goto add_member;
19619 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION)
19620 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19621 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION)
19622 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19623 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019624 * SPEC: Substitution Group OK (Transitive) (2.3)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019625 * "The set of all {derivation method}s involved in the
19626 * derivation of D's {type definition} from C's {type definition}
19627 * does not intersect with the union of the blocking constraint,
19628 * C's {prohibited substitutions} (if C is complex, otherwise the
19629 * empty set) and the {prohibited substitutions} (respectively the
19630 * empty set) of any intermediate {type definition}s in the
19631 * derivation of D's {type definition} from C's {type definition}."
19632 */
19633 /*
19634 * OPTIMIZE TODO: Optimize this a bit, since, if traversing the
19635 * subst.head axis, the methSet does not need to be computed for
19636 * the full depth over and over.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019637 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019638 /*
19639 * The set of all {derivation method}s involved in the derivation
19640 */
19641 while ((type != NULL) && (type != headType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019642 if ((WXS_IS_EXTENSION(type)) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019643 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19644 methSet |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019645
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019646 if (WXS_IS_RESTRICTION(type) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019647 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19648 methSet |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19649
19650 type = type->baseType;
19651 }
19652 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019653 * The {prohibited substitutions} of all intermediate types +
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019654 * the head's type.
19655 */
19656 type = elemDecl->subtypes->baseType;
19657 while (type != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019658 if (WXS_IS_COMPLEX(type)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019659 if ((type->flags &
19660 XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19661 ((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) == 0))
19662 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19663 if ((type->flags &
19664 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19665 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19666 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19667 } else
19668 break;
19669 if (type == headType)
19670 break;
19671 type = type->baseType;
19672 }
19673 if ((set != 0) &&
19674 (((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19675 (methSet & XML_SCHEMAS_TYPE_BLOCK_EXTENSION)) ||
19676 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19677 (methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION)))) {
19678 continue;
19679 }
19680add_member:
19681 xmlSchemaAddElementSubstitutionMember(ctxt, head, elemDecl);
19682 if ((head->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) == 0)
19683 head->flags |= XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD;
19684 }
19685 }
19686}
19687
19688/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019689 * xmlSchemaCheckElementDeclComponent
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019690 * @item: an schema element declaration/particle
19691 * @ctxt: a schema parser context
19692 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019693 *
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019694 * Validates the value constraints of an element declaration.
19695 *
19696 * Fixes finish doing the computations on the element declarations.
19697 */
19698static void
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019699xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019700 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019701{
19702 if (elemDecl == NULL)
19703 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019704 if (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_CHECKED)
19705 return;
19706 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_CHECKED;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019707 if (xmlSchemaCheckElemPropsCorrect(ctxt, elemDecl) == 0)
19708 xmlSchemaCheckElemSubstGroup(ctxt, elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019709}
19710
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019711/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019712 * xmlSchemaResolveModelGroupParticleReferences:
19713 * @particle: a particle component
19714 * @ctxt: a parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019715 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019716 * Resolves references of a model group's {particles} to
19717 * model group definitions and to element declarations.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019718 */
19719static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019720xmlSchemaResolveModelGroupParticleReferences(
19721 xmlSchemaParserCtxtPtr ctxt,
19722 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019723{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019724 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
19725 xmlSchemaQNameRefPtr ref;
19726 xmlSchemaBasicItemPtr refItem;
19727
19728 /*
19729 * URGENT TODO: Test this.
19730 */
19731 while (particle != NULL) {
19732 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
19733 ((WXS_PARTICLE_TERM(particle))->type !=
19734 XML_SCHEMA_EXTRA_QNAMEREF))
19735 {
19736 goto next_particle;
19737 }
19738 ref = WXS_QNAME_CAST WXS_PARTICLE_TERM(particle);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019739 /*
19740 * Resolve the reference.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019741 * NULL the {term} by default.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019742 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019743 particle->children = NULL;
19744
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019745 refItem = xmlSchemaGetNamedComponent(ctxt->schema,
19746 ref->itemType, ref->name, ref->targetNamespace);
19747 if (refItem == NULL) {
19748 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019749 NULL, WXS_ITEM_NODE(particle), "ref", ref->name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019750 ref->targetNamespace, ref->itemType, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019751 /* TODO: remove the particle. */
19752 goto next_particle;
19753 }
19754 if (refItem->type == XML_SCHEMA_TYPE_GROUP) {
19755 if (WXS_MODELGROUPDEF_MODEL(refItem) == NULL)
19756 /* TODO: remove the particle. */
19757 goto next_particle;
19758 /*
19759 * NOTE that we will assign the model group definition
19760 * itself to the "term" of the particle. This will ease
19761 * the check for circular model group definitions. After
19762 * that the "term" will be assigned the model group of the
19763 * model group definition.
19764 */
19765 if ((WXS_MODELGROUPDEF_MODEL(refItem))->type ==
19766 XML_SCHEMA_TYPE_ALL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019767 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019768 * SPEC cos-all-limited (1)
19769 * SPEC cos-all-limited (1.2)
19770 * "It appears only as the value of one or both of the
19771 * following properties:"
19772 * (1.1) "the {model group} property of a model group
19773 * definition."
19774 * (1.2) "the {term} property of a particle [... of] the "
19775 * {content type} of a complex type definition."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019776 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019777 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19778 /* TODO: error code */
19779 XML_SCHEMAP_COS_ALL_LIMITED,
19780 WXS_ITEM_NODE(particle), NULL,
19781 "A model group definition is referenced, but "
19782 "it contains an 'all' model group, which "
19783 "cannot be contained by model groups",
19784 NULL, NULL);
19785 /* TODO: remove the particle. */
19786 goto next_particle;
19787 }
19788 particle->children = (xmlSchemaTreeItemPtr) refItem;
19789 } else {
19790 /*
19791 * TODO: Are referenced element declarations the only
19792 * other components we expect here?
19793 */
19794 particle->children = (xmlSchemaTreeItemPtr) refItem;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019795 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019796next_particle:
19797 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019798 }
19799}
19800
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019801static int
19802xmlSchemaAreValuesEqual(xmlSchemaValPtr x,
19803 xmlSchemaValPtr y)
19804{
19805 xmlSchemaTypePtr tx, ty, ptx, pty;
19806 int ret;
19807
19808 while (x != NULL) {
19809 /* Same types. */
19810 tx = xmlSchemaGetBuiltInType(xmlSchemaGetValType(x));
19811 ty = xmlSchemaGetBuiltInType(xmlSchemaGetValType(y));
19812 ptx = xmlSchemaGetPrimitiveType(tx);
19813 pty = xmlSchemaGetPrimitiveType(ty);
19814 /*
19815 * (1) if a datatype T' is ·derived· by ·restriction· from an
19816 * atomic datatype T then the ·value space· of T' is a subset of
19817 * the ·value space· of T. */
19818 /*
19819 * (2) if datatypes T' and T'' are ·derived· by ·restriction·
19820 * from a common atomic ancestor T then the ·value space·s of T'
19821 * and T'' may overlap.
19822 */
19823 if (ptx != pty)
19824 return(0);
19825 /*
19826 * We assume computed values to be normalized, so do a fast
19827 * string comparison for string based types.
19828 */
19829 if ((ptx->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019830 WXS_IS_ANY_SIMPLE_TYPE(ptx)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019831 if (! xmlStrEqual(
19832 xmlSchemaValueGetAsString(x),
19833 xmlSchemaValueGetAsString(y)))
19834 return (0);
19835 } else {
19836 ret = xmlSchemaCompareValuesWhtsp(
19837 x, XML_SCHEMA_WHITESPACE_PRESERVE,
19838 y, XML_SCHEMA_WHITESPACE_PRESERVE);
19839 if (ret == -2)
19840 return(-1);
19841 if (ret != 0)
19842 return(0);
19843 }
19844 /*
19845 * Lists.
19846 */
19847 x = xmlSchemaValueGetNext(x);
19848 if (x != NULL) {
19849 y = xmlSchemaValueGetNext(y);
19850 if (y == NULL)
19851 return (0);
19852 } else if (xmlSchemaValueGetNext(y) != NULL)
19853 return (0);
19854 else
19855 return (1);
19856 }
19857 return (0);
19858}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019859
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019860/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019861 * xmlSchemaResolveAttrUseReferences:
19862 * @item: an attribute use
19863 * @ctxt: a parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019864 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019865 * Resolves the referenced attribute declaration.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019866 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019867static int
19868xmlSchemaResolveAttrUseReferences(xmlSchemaAttributeUsePtr ause,
19869 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000019870{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019871 if ((ctxt == NULL) || (ause == NULL))
19872 return(-1);
19873 if ((ause->attrDecl == NULL) ||
19874 (ause->attrDecl->type != XML_SCHEMA_EXTRA_QNAMEREF))
19875 return(0);
19876
19877 {
19878 xmlSchemaQNameRefPtr ref = WXS_QNAME_CAST ause->attrDecl;
19879
19880 /*
19881 * TODO: Evaluate, what errors could occur if the declaration is not
19882 * found.
19883 */
19884 ause->attrDecl = xmlSchemaGetAttributeDecl(ctxt->schema,
19885 ref->name, ref->targetNamespace);
19886 if (ause->attrDecl == NULL) {
19887 xmlSchemaPResCompAttrErr(ctxt,
19888 XML_SCHEMAP_SRC_RESOLVE,
19889 WXS_BASIC_CAST ause, ause->node,
19890 "ref", ref->name, ref->targetNamespace,
19891 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
19892 return(ctxt->err);;
19893 }
19894 }
19895 return(0);
19896}
19897
19898/**
19899 * xmlSchemaCheckAttrUsePropsCorrect:
19900 * @ctxt: a parser context
19901 * @use: an attribute use
19902 *
19903 * Schema Component Constraint:
19904 * Attribute Use Correct (au-props-correct)
19905 *
19906 */
19907static int
19908xmlSchemaCheckAttrUsePropsCorrect(xmlSchemaParserCtxtPtr ctxt,
19909 xmlSchemaAttributeUsePtr use)
19910{
19911 if ((ctxt == NULL) || (use == NULL))
19912 return(-1);
19913 if ((use->defValue == NULL) || (WXS_ATTRUSE_DECL(use) == NULL) ||
19914 ((WXS_ATTRUSE_DECL(use))->type != XML_SCHEMA_TYPE_ATTRIBUTE))
19915 return(0);
19916
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019917 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019918 * SPEC au-props-correct (1)
19919 * "The values of the properties of an attribute use must be as
19920 * described in the property tableau in The Attribute Use Schema
19921 * Component (§3.5.1), modulo the impact of Missing
19922 * Sub-components (§5.3)."
Daniel Veillardc0826a72004-08-10 14:17:33 +000019923 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019924
19925 if (((WXS_ATTRUSE_DECL(use))->defValue != NULL) &&
19926 ((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMAS_ATTR_FIXED) &&
19927 ((use->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
19928 {
19929 xmlSchemaPCustomErr(ctxt,
19930 XML_SCHEMAP_AU_PROPS_CORRECT_2,
19931 WXS_BASIC_CAST use, NULL,
19932 "The attribute declaration has a 'fixed' value constraint "
19933 ", thus the attribute use must also have a 'fixed' value "
19934 "constraint",
19935 NULL);
19936 return(ctxt->err);
19937 }
19938 /*
19939 * Compute and check the value constraint's value.
19940 */
19941 if ((use->defVal != NULL) && (WXS_ATTRUSE_TYPEDEF(use) != NULL)) {
19942 int ret;
19943 /*
19944 * TODO: The spec seems to be missing a check of the
19945 * value constraint of the attribute use. We will do it here.
19946 */
19947 /*
19948 * SPEC a-props-correct (3)
19949 */
19950 if (xmlSchemaIsDerivedFromBuiltInType(
19951 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
19952 {
19953 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19954 XML_SCHEMAP_AU_PROPS_CORRECT,
19955 NULL, WXS_BASIC_CAST use,
19956 "Value constraints are not allowed if the type definition "
19957 "is or is derived from xs:ID",
19958 NULL, NULL);
19959 return(ctxt->err);
19960 }
19961
19962 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST ctxt,
19963 use->node, WXS_ATTRUSE_TYPEDEF(use),
19964 use->defValue, &(use->defVal),
19965 1, 1, 0);
19966 if (ret != 0) {
19967 if (ret < 0) {
19968 PERROR_INT2("xmlSchemaCheckAttrUsePropsCorrect",
19969 "calling xmlSchemaVCheckCVCSimpleType()");
19970 return(-1);
19971 }
19972 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19973 XML_SCHEMAP_AU_PROPS_CORRECT,
19974 NULL, WXS_BASIC_CAST use,
19975 "The value of the value constraint is not valid",
19976 NULL, NULL);
19977 return(ctxt->err);
19978 }
19979 }
19980 /*
19981 * SPEC au-props-correct (2)
19982 * "If the {attribute declaration} has a fixed
19983 * {value constraint}, then if the attribute use itself has a
19984 * {value constraint}, it must also be fixed and its value must match
19985 * that of the {attribute declaration}'s {value constraint}."
19986 */
19987 if (((WXS_ATTRUSE_DECL(use))->defVal != NULL) &&
19988 (((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
19989 {
19990 if (! xmlSchemaAreValuesEqual(use->defVal,
19991 (WXS_ATTRUSE_DECL(use))->defVal))
19992 {
19993 xmlSchemaPCustomErr(ctxt,
19994 XML_SCHEMAP_AU_PROPS_CORRECT_2,
19995 WXS_BASIC_CAST use, NULL,
19996 "The 'fixed' value constraint of the attribute use "
19997 "must match the attribute declaration's value "
19998 "constraint '%s'",
19999 (WXS_ATTRUSE_DECL(use))->defValue);
20000 }
20001 return(ctxt->err);
20002 }
20003 return(0);
20004}
20005
20006
20007
20008
20009/**
20010 * xmlSchemaResolveAttrTypeReferences:
20011 * @item: an attribute declaration
20012 * @ctxt: a parser context
20013 *
20014 * Resolves the referenced type definition component.
20015 */
20016static int
20017xmlSchemaResolveAttrTypeReferences(xmlSchemaAttributePtr item,
20018 xmlSchemaParserCtxtPtr ctxt)
20019{
Daniel Veillard01fa6152004-06-29 17:04:39 +000020020 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020021 * The simple type definition corresponding to the <simpleType> element
20022 * information item in the [children], if present, otherwise the simple
20023 * type definition ·resolved· to by the ·actual value· of the type
Daniel Veillard01fa6152004-06-29 17:04:39 +000020024 * [attribute], if present, otherwise the ·simple ur-type definition·.
20025 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020026 if (item->flags & XML_SCHEMAS_ATTR_INTERNAL_RESOLVED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020027 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020028 item->flags |= XML_SCHEMAS_ATTR_INTERNAL_RESOLVED;
20029 if (item->subtypes != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020030 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020031 if (item->typeName != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000020032 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000020033
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020034 type = xmlSchemaGetType(ctxt->schema, item->typeName,
20035 item->typeNs);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020036 if ((type == NULL) || (! WXS_IS_SIMPLE(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000020037 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000020038 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020039 WXS_BASIC_CAST item, item->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020040 "type", item->typeName, item->typeNs,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020041 XML_SCHEMA_TYPE_SIMPLE, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020042 return(ctxt->err);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020043 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020044 item->subtypes = type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020045
Daniel Veillard3646d642004-06-02 19:19:14 +000020046 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020047 /*
20048 * The type defaults to the xs:anySimpleType.
20049 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020050 item->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
20051 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020052 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000020053}
20054
20055/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020056 * xmlSchemaResolveIDCKeyReferences:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020057 * @idc: the identity-constraint definition
20058 * @ctxt: the schema parser context
20059 * @name: the attribute name
20060 *
20061 * Resolve keyRef references to key/unique IDCs.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020062 * Schema Component Constraint:
20063 * Identity-constraint Definition Properties Correct (c-props-correct)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020064 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020065static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020066xmlSchemaResolveIDCKeyReferences(xmlSchemaIDCPtr idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020067 xmlSchemaParserCtxtPtr pctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020068{
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020069 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020070 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020071 if (idc->ref->name != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020072 idc->ref->item = (xmlSchemaBasicItemPtr)
20073 xmlSchemaGetIDC(pctxt->schema, idc->ref->name,
20074 idc->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020075 if (idc->ref->item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020076 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020077 * TODO: It is actually not an error to fail to resolve
20078 * at this stage. BUT we need to be that strict!
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020079 */
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020080 xmlSchemaPResCompAttrErr(pctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020081 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020082 WXS_BASIC_CAST idc, idc->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020083 "refer", idc->ref->name,
20084 idc->ref->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020085 XML_SCHEMA_TYPE_IDC_KEY, NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020086 return(pctxt->err);
20087 } else if (idc->ref->item->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
20088 /*
20089 * SPEC c-props-correct (1)
20090 */
20091 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20092 XML_SCHEMAP_C_PROPS_CORRECT,
20093 NULL, WXS_BASIC_CAST idc,
20094 "The keyref references a keyref",
20095 NULL, NULL);
20096 idc->ref->item = NULL;
20097 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020098 } else {
20099 if (idc->nbFields !=
20100 ((xmlSchemaIDCPtr) idc->ref->item)->nbFields) {
20101 xmlChar *str = NULL;
20102 xmlSchemaIDCPtr refer;
20103
20104 refer = (xmlSchemaIDCPtr) idc->ref->item;
20105 /*
20106 * SPEC c-props-correct(2)
20107 * "If the {identity-constraint category} is keyref,
20108 * the cardinality of the {fields} must equal that of
20109 * the {fields} of the {referenced key}.
20110 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020111 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020112 XML_SCHEMAP_C_PROPS_CORRECT,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020113 NULL, WXS_BASIC_CAST idc,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020114 "The cardinality of the keyref differs from the "
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020115 "cardinality of the referenced key/unique '%s'",
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020116 xmlSchemaFormatQName(&str, refer->targetNamespace,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020117 refer->name),
20118 NULL);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020119 FREE_AND_NULL(str)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020120 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020121 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020122 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020123 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020124 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020125}
20126
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020127static int
20128xmlSchemaResolveAttrUseProhibReferences(xmlSchemaAttributeUseProhibPtr prohib,
20129 xmlSchemaParserCtxtPtr pctxt)
20130{
20131 if (xmlSchemaGetAttributeDecl(pctxt->schema, prohib->name,
20132 prohib->targetNamespace) == NULL) {
20133
20134 xmlSchemaPResCompAttrErr(pctxt,
20135 XML_SCHEMAP_SRC_RESOLVE,
20136 NULL, prohib->node,
20137 "ref", prohib->name, prohib->targetNamespace,
20138 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
20139 return(XML_SCHEMAP_SRC_RESOLVE);
20140 }
20141 return(0);
20142}
20143
20144#define WXS_REDEFINED_TYPE(c) \
20145(((xmlSchemaTypePtr) item)->flags & XML_SCHEMAS_TYPE_REDEFINED)
20146
20147#define WXS_REDEFINED_MODEL_GROUP_DEF(c) \
20148(((xmlSchemaModelGroupDefPtr) item)->flags & XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20149
20150#define WXS_REDEFINED_ATTR_GROUP(c) \
20151(((xmlSchemaAttributeGroupPtr) item)->flags & XML_SCHEMAS_ATTRGROUP_REDEFINED)
20152
20153static int
20154xmlSchemaCheckSRCRedefineFirst(xmlSchemaParserCtxtPtr pctxt)
20155{
20156 int err = 0;
20157 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20158 xmlSchemaBasicItemPtr prev, item;
20159 int wasRedefined;
20160
20161 if (redef == NULL)
20162 return(0);
20163
20164 do {
20165 item = redef->item;
20166 /*
20167 * First try to locate the redefined component in the
20168 * schema graph starting with the redefined schema.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020169 * NOTE: According to this schema bug entry:
20170 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005OctDec/0019.html
20171 * it's not clear if the referenced component needs to originate
20172 * from the <redefine>d schema _document_ or the schema; the latter
20173 * would include all imported and included sub-schemas of the
20174 * <redefine>d schema. Currenlty we latter approach is used.
20175 * SUPPLEMENT: It seems that the WG moves towards the latter
20176 * approach, so we are doing it right.
20177 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020178 */
20179 prev = xmlSchemaFindRedefCompInGraph(
20180 redef->targetBucket, item->type,
20181 redef->refName, redef->refTargetNs);
20182 if (prev == NULL) {
20183 xmlChar *str = NULL;
20184 xmlNodePtr node;
20185
20186 /*
20187 * SPEC src-redefine:
20188 * (6.2.1) "The ·actual value· of its own name attribute plus
20189 * target namespace must successfully ·resolve· to a model
20190 * group definition in I."
20191 * (7.2.1) "The ·actual value· of its own name attribute plus
20192 * target namespace must successfully ·resolve· to an attribute
20193 * group definition in I."
20194
20195 *
20196 * Note that, if we are redefining with the use of references
20197 * to components, the spec assumes the src-resolve to be used;
20198 * but this won't assure that we search only *inside* the
20199 * redefined schema.
20200 */
20201 if (redef->reference)
20202 node = WXS_ITEM_NODE(redef->reference);
20203 else
20204 node = WXS_ITEM_NODE(item);
20205 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20206 /*
20207 * TODO: error code.
20208 * Probably XML_SCHEMAP_SRC_RESOLVE, if this is using the
20209 * reference kind.
20210 */
20211 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
20212 "The %s to be redefined could not be found in "
20213 "the redefined schema",
20214 WXS_ITEM_TYPE_NAME(item),
20215 xmlSchemaFormatQName(&str, redef->refTargetNs,
20216 redef->refName));
20217 FREE_AND_NULL(str);
20218 err = pctxt->err;
20219 redef = redef->next;
20220 continue;
20221 }
20222 /*
20223 * TODO: Obtaining and setting the redefinition state is really
20224 * clumsy.
20225 */
20226 wasRedefined = 0;
20227 switch (item->type) {
20228 case XML_SCHEMA_TYPE_COMPLEX:
20229 case XML_SCHEMA_TYPE_SIMPLE:
20230 if ((WXS_TYPE_CAST prev)->flags &
20231 XML_SCHEMAS_TYPE_REDEFINED)
20232 {
20233 wasRedefined = 1;
20234 break;
20235 }
20236 /* Mark it as redefined. */
20237 (WXS_TYPE_CAST prev)->flags |= XML_SCHEMAS_TYPE_REDEFINED;
20238 /*
20239 * Assign the redefined type to the
20240 * base type of the redefining type.
20241 * TODO: How
20242 */
20243 ((xmlSchemaTypePtr) item)->baseType =
20244 (xmlSchemaTypePtr) prev;
20245 break;
20246 case XML_SCHEMA_TYPE_GROUP:
20247 if ((WXS_MODEL_GROUPDEF_CAST prev)->flags &
20248 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20249 {
20250 wasRedefined = 1;
20251 break;
20252 }
20253 /* Mark it as redefined. */
20254 (WXS_MODEL_GROUPDEF_CAST prev)->flags |=
20255 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED;
20256 if (redef->reference != NULL) {
20257 /*
20258 * Overwrite the QName-reference with the
20259 * referenced model group def.
20260 */
20261 (WXS_PTC_CAST redef->reference)->children =
20262 WXS_TREE_CAST prev;
20263 }
20264 redef->target = prev;
20265 break;
20266 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20267 if ((WXS_ATTR_GROUP_CAST prev)->flags &
20268 XML_SCHEMAS_ATTRGROUP_REDEFINED)
20269 {
20270 wasRedefined = 1;
20271 break;
20272 }
20273 (WXS_ATTR_GROUP_CAST prev)->flags |=
20274 XML_SCHEMAS_ATTRGROUP_REDEFINED;
20275 if (redef->reference != NULL) {
20276 /*
20277 * Assign the redefined attribute group to the
20278 * QName-reference component.
20279 * This is the easy case, since we will just
20280 * expand the redefined group.
20281 */
20282 (WXS_QNAME_CAST redef->reference)->item = prev;
20283 redef->target = NULL;
20284 } else {
20285 /*
20286 * This is the complicated case: we need
20287 * to apply src-redefine (7.2.2) at a later
20288 * stage, i.e. when attribute group references
20289 * have beed expanded and simple types have
20290 * beed fixed.
20291 */
20292 redef->target = prev;
20293 }
20294 break;
20295 default:
20296 PERROR_INT("xmlSchemaResolveRedefReferences",
20297 "Unexpected redefined component type");
20298 return(-1);
20299 }
20300 if (wasRedefined) {
20301 xmlChar *str = NULL;
20302 xmlNodePtr node;
20303
20304 if (redef->reference)
20305 node = WXS_ITEM_NODE(redef->reference);
20306 else
20307 node = WXS_ITEM_NODE(redef->item);
20308
20309 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20310 /* TODO: error code. */
20311 XML_SCHEMAP_SRC_REDEFINE,
20312 node, NULL,
20313 "The referenced %s was already redefined. Multiple "
20314 "redefinition of the same component is not supported",
20315 xmlSchemaGetComponentDesignation(&str, prev),
20316 NULL);
20317 FREE_AND_NULL(str)
20318 err = pctxt->err;
20319 redef = redef->next;
20320 continue;
20321 }
20322 redef = redef->next;
20323 } while (redef != NULL);
20324
20325 return(err);
20326}
20327
20328static int
20329xmlSchemaCheckSRCRedefineSecond(xmlSchemaParserCtxtPtr pctxt)
20330{
20331 int err = 0;
20332 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20333 xmlSchemaBasicItemPtr item;
20334
20335 if (redef == NULL)
20336 return(0);
20337
20338 do {
20339 if (redef->target == NULL) {
20340 redef = redef->next;
20341 continue;
20342 }
20343 item = redef->item;
20344
20345 switch (item->type) {
20346 case XML_SCHEMA_TYPE_SIMPLE:
20347 case XML_SCHEMA_TYPE_COMPLEX:
20348 /*
20349 * Since the spec wants the {name} of the redefined
20350 * type to be 'absent', we'll NULL it.
20351 */
20352 (WXS_TYPE_CAST redef->target)->name = NULL;
20353
20354 /*
20355 * TODO: Seems like there's nothing more to do. The normal
20356 * inheritance mechanism is used. But not 100% sure.
20357 */
20358 break;
20359 case XML_SCHEMA_TYPE_GROUP:
20360 /*
20361 * URGENT TODO:
20362 * SPEC src-redefine:
20363 * (6.2.2) "The {model group} of the model group definition
20364 * which corresponds to it per XML Representation of Model
20365 * Group Definition Schema Components (§3.7.2) must be a
20366 * ·valid restriction· of the {model group} of that model
20367 * group definition in I, as defined in Particle Valid
20368 * (Restriction) (§3.9.6)."
20369 */
20370 break;
20371 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20372 /*
20373 * SPEC src-redefine:
20374 * (7.2.2) "The {attribute uses} and {attribute wildcard} of
20375 * the attribute group definition which corresponds to it
20376 * per XML Representation of Attribute Group Definition Schema
20377 * Components (§3.6.2) must be ·valid restrictions· of the
20378 * {attribute uses} and {attribute wildcard} of that attribute
20379 * group definition in I, as defined in clause 2, clause 3 and
20380 * clause 4 of Derivation Valid (Restriction, Complex)
20381 * (§3.4.6) (where references to the base type definition are
20382 * understood as references to the attribute group definition
20383 * in I)."
20384 */
20385 err = xmlSchemaCheckDerivationOKRestriction2to4(pctxt,
20386 XML_SCHEMA_ACTION_REDEFINE,
20387 item, redef->target,
20388 (WXS_ATTR_GROUP_CAST item)->attrUses,
20389 (WXS_ATTR_GROUP_CAST redef->target)->attrUses,
20390 (WXS_ATTR_GROUP_CAST item)->attributeWildcard,
20391 (WXS_ATTR_GROUP_CAST redef->target)->attributeWildcard);
20392 if (err == -1)
20393 return(-1);
20394 break;
20395 default:
20396 break;
20397 }
20398 redef = redef->next;
20399 } while (redef != NULL);
20400 return(0);
20401}
20402
20403
20404static int
20405xmlSchemaAddComponents(xmlSchemaParserCtxtPtr pctxt,
20406 xmlSchemaBucketPtr bucket)
20407{
20408 xmlSchemaBasicItemPtr item;
20409 int err;
20410 xmlHashTablePtr *table;
20411 const xmlChar *name;
20412 int i;
20413
20414#define WXS_GET_GLOBAL_HASH(c, s, slot) { \
20415 if (WXS_IS_BUCKET_IMPMAIN((c)->type)) \
20416 table = &(WXS_IMPBUCKET((c))->schema->slot); \
20417 else \
20418 table = &(WXS_INCBUCKET((c))->ownerImport->schema->slot); }
20419
20420 /*
20421 * Add global components to the schema's hash tables.
20422 * This is the place where duplicate components will be
20423 * detected.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020424 * TODO: I think normally we should support imports of the
20425 * same namespace from multiple locations. We don't do currently,
20426 * but if we do then according to:
20427 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2224
20428 * we would need, if imported directly, to import redefined
20429 * components as well to be able to catch clashing components.
20430 * (I hope I'll still know what this means after some months :-()
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020431 */
20432 if (bucket == NULL)
20433 return(-1);
20434 if (bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED)
20435 return(0);
20436 bucket->flags |= XML_SCHEMA_BUCKET_COMPS_ADDED;
20437
20438 for (i = 0; i < bucket->globals->nbItems; i++) {
20439 item = bucket->globals->items[i];
20440 table = NULL;
20441 switch (item->type) {
20442 case XML_SCHEMA_TYPE_COMPLEX:
20443 case XML_SCHEMA_TYPE_SIMPLE:
20444 if (WXS_REDEFINED_TYPE(item))
20445 continue;
20446 name = (WXS_TYPE_CAST item)->name;
20447 WXS_GET_GLOBAL_HASH(bucket, schema, typeDecl)
20448 break;
20449 case XML_SCHEMA_TYPE_ELEMENT:
20450 name = (WXS_ELEM_CAST item)->name;
20451 WXS_GET_GLOBAL_HASH(bucket, schema, elemDecl)
20452 break;
20453 case XML_SCHEMA_TYPE_ATTRIBUTE:
20454 name = (WXS_ATTR_CAST item)->name;
20455 WXS_GET_GLOBAL_HASH(bucket, schema, attrDecl)
20456 break;
20457 case XML_SCHEMA_TYPE_GROUP:
20458 if (WXS_REDEFINED_MODEL_GROUP_DEF(item))
20459 continue;
20460 name = (WXS_MODEL_GROUPDEF_CAST item)->name;
20461 WXS_GET_GLOBAL_HASH(bucket, schema, groupDecl)
20462 break;
20463 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20464 if (WXS_REDEFINED_ATTR_GROUP(item))
20465 continue;
20466 name = (WXS_ATTR_GROUP_CAST item)->name;
20467 WXS_GET_GLOBAL_HASH(bucket, schema, attrgrpDecl)
20468 break;
20469 case XML_SCHEMA_TYPE_IDC_KEY:
20470 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20471 case XML_SCHEMA_TYPE_IDC_KEYREF:
20472 name = (WXS_IDC_CAST item)->name;
20473 WXS_GET_GLOBAL_HASH(bucket, schema, idcDef)
20474 break;
20475 case XML_SCHEMA_TYPE_NOTATION:
20476 name = ((xmlSchemaNotationPtr) item)->name;
20477 WXS_GET_GLOBAL_HASH(bucket, schema, notaDecl)
20478 break;
20479 default:
20480 PERROR_INT("xmlSchemaAddComponents",
20481 "Unexpected global component type");
20482 continue;
20483 }
20484 if (*table == NULL) {
20485 *table = xmlHashCreateDict(10, pctxt->dict);
20486 if (*table == NULL) {
20487 PERROR_INT("xmlSchemaAddComponents",
20488 "failed to create a component hash table");
20489 return(-1);
20490 }
20491 }
20492 err = xmlHashAddEntry(*table, name, item);
20493 if (err != 0) {
20494 xmlChar *str = NULL;
20495
20496 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20497 XML_SCHEMAP_REDEFINED_TYPE,
20498 WXS_ITEM_NODE(item),
20499 WXS_BASIC_CAST item,
20500 "A global %s '%s' does already exist",
20501 WXS_ITEM_TYPE_NAME(item),
20502 xmlSchemaGetComponentQName(&str, item));
20503 FREE_AND_NULL(str);
20504 }
20505 }
20506 /*
20507 * Process imported/included schemas.
20508 */
20509 if (bucket->relations != NULL) {
20510 xmlSchemaSchemaRelationPtr rel = bucket->relations;
20511 do {
20512 if ((rel->bucket != NULL) &&
20513 ((rel->bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED) == 0)) {
20514 if (xmlSchemaAddComponents(pctxt, rel->bucket) == -1)
20515 return(-1);
20516 }
20517 rel = rel->next;
20518 } while (rel != NULL);
20519 }
20520 return(0);
20521}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020522
20523static int
20524xmlSchemaFixupComponents(xmlSchemaParserCtxtPtr pctxt)
20525{
20526 xmlSchemaConstructionCtxtPtr con = pctxt->constructor;
20527 xmlSchemaTreeItemPtr item, *items;
20528 int nbItems, i;
20529
20530#define FIXHFAILURE if (pctxt->err == XML_SCHEMAP_INTERNAL) goto exit_failure;
20531
20532 if ((con->pending == NULL) ||
20533 (con->pending->nbItems == 0))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020534 return(0);
20535
20536 /* TODO:
20537 * SPEC (src-redefine):
20538 * (6.2) "If it has no such self-reference, then all of the
20539 * following must be true:"
20540
20541 * (6.2.2) The {model group} of the model group definition which
20542 * corresponds to it per XML Representation of Model Group
20543 * Definition Schema Components (§3.7.2) must be a ·valid
20544 * restriction· of the {model group} of that model group definition
20545 * in I, as defined in Particle Valid (Restriction) (§3.9.6)."
20546 */
20547 xmlSchemaCheckSRCRedefineFirst(pctxt);
20548
20549 /*
20550 * Add global components to the schemata's hash tables.
20551 */
20552 xmlSchemaAddComponents(pctxt, WXS_CONSTRUCTOR(pctxt)->mainBucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020553
20554 pctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020555 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20556 nbItems = con->pending->nbItems;
20557 /*
20558 * Now that we have parsed *all* the schema document(s) and converted
20559 * them to schema components, we can resolve references, apply component
20560 * constraints, create the FSA from the content model, etc.
20561 */
20562 /*
20563 * Resolve references of..
20564 *
20565 * 1. element declarations:
20566 * - the type definition
20567 * - the substitution group affiliation
20568 * 2. simple/complex types:
20569 * - the base type definition
20570 * - the memberTypes of union types
20571 * - the itemType of list types
20572 * 3. attributes declarations and attribute uses:
20573 * - the type definition
20574 * - if an attribute use, then the attribute declaration
20575 * 4. attribute group references:
20576 * - the attribute group definition
20577 * 5. particles:
20578 * - the term of the particle (e.g. a model group)
20579 * 6. IDC key-references:
20580 * - the referenced IDC 'key' or 'unique' definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020581 * 7. Attribute prohibitions which had a "ref" attribute.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020582 */
20583 for (i = 0; i < nbItems; i++) {
20584 item = items[i];
20585 switch (item->type) {
20586 case XML_SCHEMA_TYPE_ELEMENT:
20587 xmlSchemaResolveElementReferences(
20588 (xmlSchemaElementPtr) item, pctxt);
20589 FIXHFAILURE;
20590 break;
20591 case XML_SCHEMA_TYPE_COMPLEX:
20592 case XML_SCHEMA_TYPE_SIMPLE:
20593 xmlSchemaResolveTypeReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020594 (xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020595 FIXHFAILURE;
20596 break;
20597 case XML_SCHEMA_TYPE_ATTRIBUTE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020598 xmlSchemaResolveAttrTypeReferences(
20599 (xmlSchemaAttributePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020600 FIXHFAILURE;
20601 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020602 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20603 xmlSchemaResolveAttrUseReferences(
20604 (xmlSchemaAttributeUsePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020605 FIXHFAILURE;
20606 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020607 case XML_SCHEMA_EXTRA_QNAMEREF:
20608 if ((WXS_QNAME_CAST item)->itemType ==
20609 XML_SCHEMA_TYPE_ATTRIBUTEGROUP)
20610 {
20611 xmlSchemaResolveAttrGroupReferences(
20612 WXS_QNAME_CAST item, pctxt);
20613 }
20614 FIXHFAILURE;
20615 break;
20616 case XML_SCHEMA_TYPE_SEQUENCE:
20617 case XML_SCHEMA_TYPE_CHOICE:
20618 case XML_SCHEMA_TYPE_ALL:
20619 xmlSchemaResolveModelGroupParticleReferences(pctxt,
20620 WXS_MODEL_GROUP_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020621 FIXHFAILURE;
20622 break;
20623 case XML_SCHEMA_TYPE_IDC_KEY:
20624 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20625 case XML_SCHEMA_TYPE_IDC_KEYREF:
20626 xmlSchemaResolveIDCKeyReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020627 (xmlSchemaIDCPtr) item, pctxt);
20628 FIXHFAILURE;
20629 break;
20630 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
20631 /*
20632 * Handle attribue prohibition which had a
20633 * "ref" attribute.
20634 */
20635 xmlSchemaResolveAttrUseProhibReferences(
20636 WXS_ATTR_PROHIB_CAST item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020637 FIXHFAILURE;
20638 break;
20639 default:
20640 break;
20641 }
20642 }
20643 if (pctxt->nberrors != 0)
20644 goto exit_error;
20645
20646 /*
20647 * Now that all references are resolved we
20648 * can check for circularity of...
20649 * 1. the base axis of type definitions
20650 * 2. nested model group definitions
20651 * 3. nested attribute group definitions
20652 * TODO: check for circual substitution groups.
20653 */
20654 for (i = 0; i < nbItems; i++) {
20655 item = items[i];
20656 /*
20657 * Let's better stop on the first error here.
20658 */
20659 switch (item->type) {
20660 case XML_SCHEMA_TYPE_COMPLEX:
20661 case XML_SCHEMA_TYPE_SIMPLE:
20662 xmlSchemaCheckTypeDefCircular(
20663 (xmlSchemaTypePtr) item, pctxt);
20664 FIXHFAILURE;
20665 if (pctxt->nberrors != 0)
20666 goto exit_error;
20667 break;
20668 case XML_SCHEMA_TYPE_GROUP:
20669 xmlSchemaCheckGroupDefCircular(
20670 (xmlSchemaModelGroupDefPtr) item, pctxt);
20671 FIXHFAILURE;
20672 if (pctxt->nberrors != 0)
20673 goto exit_error;
20674 break;
20675 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20676 xmlSchemaCheckAttrGroupCircular(
20677 (xmlSchemaAttributeGroupPtr) item, pctxt);
20678 FIXHFAILURE;
20679 if (pctxt->nberrors != 0)
20680 goto exit_error;
20681 break;
20682 default:
20683 break;
20684 }
20685 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020686 if (pctxt->nberrors != 0)
20687 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020688 /*
20689 * Model group definition references:
20690 * Such a reference is reflected by a particle at the component
20691 * level. Until now the 'term' of such particles pointed
20692 * to the model group definition; this was done, in order to
20693 * ease circularity checks. Now we need to set the 'term' of
20694 * such particles to the model group of the model group definition.
20695 */
20696 for (i = 0; i < nbItems; i++) {
20697 item = items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020698 switch (item->type) {
20699 case XML_SCHEMA_TYPE_SEQUENCE:
20700 case XML_SCHEMA_TYPE_CHOICE:
20701 xmlSchemaModelGroupToModelGroupDefFixup(pctxt,
20702 WXS_MODEL_GROUP_CAST item);
20703 break;
20704 default:
20705 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020706 }
20707 }
20708 if (pctxt->nberrors != 0)
20709 goto exit_error;
20710 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020711 * Expand attribute group references of attribute group definitions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020712 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020713 for (i = 0; i < nbItems; i++) {
20714 item = items[i];
20715 switch (item->type) {
20716 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20717 if ((! WXS_ATTR_GROUP_EXPANDED(item)) &&
20718 WXS_ATTR_GROUP_HAS_REFS(item))
20719 {
20720 xmlSchemaAttributeGroupExpandRefs(pctxt,
20721 WXS_ATTR_GROUP_CAST item);
20722 FIXHFAILURE;
20723 }
20724 break;
20725 default:
20726 break;
20727 }
20728 }
20729 if (pctxt->nberrors != 0)
20730 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020731 /*
20732 * First compute the variety of simple types. This is needed as
20733 * a seperate step, since otherwise we won't be able to detect
20734 * circular union types in all cases.
20735 */
20736 for (i = 0; i < nbItems; i++) {
20737 item = items[i];
20738 switch (item->type) {
20739 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020740 if (WXS_IS_TYPE_NOT_FIXED_1((xmlSchemaTypePtr) item)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020741 xmlSchemaFixupSimpleTypeStageOne(pctxt,
20742 (xmlSchemaTypePtr) item);
20743 FIXHFAILURE;
20744 }
20745 break;
20746 default:
20747 break;
20748 }
20749 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020750 if (pctxt->nberrors != 0)
20751 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020752 /*
20753 * Detect circular union types. Note that this needs the variety to
20754 * be already computed.
20755 */
20756 for (i = 0; i < nbItems; i++) {
20757 item = items[i];
20758 switch (item->type) {
20759 case XML_SCHEMA_TYPE_SIMPLE:
20760 if (((xmlSchemaTypePtr) item)->memberTypes != NULL) {
20761 xmlSchemaCheckUnionTypeDefCircular(pctxt,
20762 (xmlSchemaTypePtr) item);
20763 FIXHFAILURE;
20764 }
20765 break;
20766 default:
20767 break;
20768 }
20769 }
20770 if (pctxt->nberrors != 0)
20771 goto exit_error;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020772
20773 /*
20774 * Do the complete type fixup for simple types.
20775 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020776 for (i = 0; i < nbItems; i++) {
20777 item = items[i];
20778 switch (item->type) {
20779 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020780 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20781 xmlSchemaFixupSimpleTypeStageTwo(pctxt, WXS_TYPE_CAST item);
20782 FIXHFAILURE;
20783 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020784 break;
20785 default:
20786 break;
20787 }
20788 }
20789 if (pctxt->nberrors != 0)
20790 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020791 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020792 * At this point we need all simple types to be builded and checked.
20793 */
20794 /*
20795 * Apply contraints for attribute declarations.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020796 */
20797 for (i = 0; i < nbItems; i++) {
20798 item = items[i];
20799 switch (item->type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020800 case XML_SCHEMA_TYPE_ATTRIBUTE:
20801 xmlSchemaCheckAttrPropsCorrect(pctxt, WXS_ATTR_CAST item);
20802 FIXHFAILURE;
20803 break;
20804 default:
20805 break;
20806 }
20807 }
20808 if (pctxt->nberrors != 0)
20809 goto exit_error;
20810 /*
20811 * Apply constraints for attribute uses.
20812 */
20813 for (i = 0; i < nbItems; i++) {
20814 item = items[i];
20815 switch (item->type) {
20816 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20817 if (((xmlSchemaAttributeUsePtr)item)->defValue != NULL) {
20818 xmlSchemaCheckAttrUsePropsCorrect(pctxt,
20819 WXS_ATTR_USE_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020820 FIXHFAILURE;
20821 }
20822 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020823 default:
20824 break;
20825 }
20826 }
20827 if (pctxt->nberrors != 0)
20828 goto exit_error;
20829
20830 /*
20831 * Apply constraints for attribute group definitions.
20832 */
20833 for (i = 0; i < nbItems; i++) {
20834 item = items[i];
20835 switch (item->type) {
20836 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20837 if (( (WXS_ATTR_GROUP_CAST item)->attrUses != NULL) &&
20838 ( (WXS_LIST_CAST (WXS_ATTR_GROUP_CAST item)->attrUses)->nbItems > 1))
20839 {
20840 xmlSchemaCheckAGPropsCorrect(pctxt, WXS_ATTR_GROUP_CAST item);
20841 FIXHFAILURE;
20842 }
20843 break;
20844 default:
20845 break;
20846 }
20847 }
20848 if (pctxt->nberrors != 0)
20849 goto exit_error;
20850
20851 /*
20852 * Apply constraints for redefinitions.
20853 */
20854 if (WXS_CONSTRUCTOR(pctxt)->redefs != NULL)
20855 xmlSchemaCheckSRCRedefineSecond(pctxt);
20856 if (pctxt->nberrors != 0)
20857 goto exit_error;
20858
20859 /*
20860 * Fixup complex types.
20861 */
20862 for (i = 0; i < nbItems; i++) {
20863 item = con->pending->items[i];
20864 switch (item->type) {
20865 case XML_SCHEMA_TYPE_COMPLEX:
20866 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20867 xmlSchemaFixupComplexType(pctxt, WXS_TYPE_CAST item);
20868 FIXHFAILURE;
20869 }
20870 break;
20871 default:
20872 break;
20873 }
20874 }
20875 if (pctxt->nberrors != 0)
20876 goto exit_error;
20877
20878 /*
20879 * The list could have changed, since xmlSchemaFixupComplexType()
20880 * will create particles and model groups in some cases.
20881 */
20882 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20883 nbItems = con->pending->nbItems;
20884
20885 /*
20886 * At this point all complex types need to be builded and checked.
20887 */
20888 /*
20889 * Apply some constraints for element declarations.
20890 */
20891 for (i = 0; i < nbItems; i++) {
20892 item = items[i];
20893 switch (item->type) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020894 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020895
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020896 if ((((xmlSchemaElementPtr) item)->flags &
20897 XML_SCHEMAS_ELEM_INTERNAL_CHECKED) == 0) {
20898 xmlSchemaCheckElementDeclComponent(
20899 (xmlSchemaElementPtr) item, pctxt);
20900 FIXHFAILURE;
20901 }
20902 break;
20903 default:
20904 break;
20905 }
20906 }
20907 if (pctxt->nberrors != 0)
20908 goto exit_error;
20909 /*
20910 * Finally we can build the automaton from the content model of
20911 * complex types.
20912 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020913
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020914 for (i = 0; i < nbItems; i++) {
20915 item = items[i];
20916 switch (item->type) {
20917 case XML_SCHEMA_TYPE_COMPLEX:
20918 xmlSchemaBuildContentModel((xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020919 /* FIXHFAILURE; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020920 break;
20921 default:
20922 break;
20923 }
20924 }
20925 if (pctxt->nberrors != 0)
20926 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020927 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020928 * URGENT TODO: cos-element-consistent
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020929 */
20930 con->pending->nbItems = 0;
20931 return(0);
20932exit_error:
20933 con->pending->nbItems = 0;
20934 return(pctxt->err);
20935exit_failure:
20936 con->pending->nbItems = 0;
20937 return(-1);
20938}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020939/**
Daniel Veillard4255d502002-04-16 15:50:10 +000020940 * xmlSchemaParse:
20941 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000020942 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000020943 * parse a schema definition resource and build an internal
Daniel Veillard4255d502002-04-16 15:50:10 +000020944 * XML Shema struture which can be used to validate instances.
Daniel Veillard4255d502002-04-16 15:50:10 +000020945 *
20946 * Returns the internal XML Schema structure built from the resource or
20947 * NULL in case of error
20948 */
20949xmlSchemaPtr
20950xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt)
20951{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020952 xmlSchemaPtr schema = NULL;
20953 xmlSchemaBucketPtr bucket = NULL;
20954 int res;
Daniel Veillard4255d502002-04-16 15:50:10 +000020955
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020956 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020957 * This one is used if the schema to be parsed was specified via
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020958 * the API; i.e. not automatically by the validated instance document.
20959 */
20960
Daniel Veillard4255d502002-04-16 15:50:10 +000020961 xmlSchemaInitTypes();
20962
Daniel Veillard6045c902002-10-09 21:13:59 +000020963 if (ctxt == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +000020964 return (NULL);
20965
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020966 /* TODO: Init the context. Is this all we need?*/
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000020967 ctxt->nberrors = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020968 ctxt->err = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000020969 ctxt->counter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000020970
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020971 /* Create the *main* schema. */
20972 schema = xmlSchemaNewSchema(ctxt);
20973 if (schema == NULL)
20974 goto exit_failure;
Daniel Veillard4255d502002-04-16 15:50:10 +000020975 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020976 * Create the schema constructor.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000020977 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020978 if (ctxt->constructor == NULL) {
20979 ctxt->constructor = xmlSchemaConstructionCtxtCreate(ctxt->dict);
20980 if (ctxt->constructor == NULL)
20981 return(NULL);
20982 /* Take ownership of the constructor to be able to free it. */
20983 ctxt->ownsConstructor = 1;
20984 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020985 ctxt->constructor->mainSchema = schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020986 /*
20987 * Locate and add the schema document.
20988 */
20989 res = xmlSchemaAddSchemaDoc(ctxt, XML_SCHEMA_SCHEMA_MAIN,
20990 ctxt->URL, ctxt->doc, ctxt->buffer, ctxt->size, NULL,
20991 NULL, NULL, &bucket);
20992 if (res == -1)
20993 goto exit_failure;
20994 if (res != 0)
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000020995 goto exit;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020996
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020997 if (bucket == NULL) {
20998 /* TODO: Error code, actually we failed to *locate* the schema. */
20999 if (ctxt->URL)
21000 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21001 NULL, NULL,
21002 "Failed to locate the main schema resource at '%s'",
21003 ctxt->URL, NULL);
21004 else
21005 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21006 NULL, NULL,
21007 "Failed to locate the main schema resource",
21008 NULL, NULL);
21009 goto exit;
21010 }
21011 /* Set the main schema bucket. */
21012 ctxt->constructor->bucket = bucket;
21013 ctxt->targetNamespace = bucket->targetNamespace;
21014 schema->targetNamespace = bucket->targetNamespace;
21015
21016 /* Then do the parsing for good. */
21017 if (xmlSchemaParseNewDocWithContext(ctxt, schema, bucket) == -1)
21018 goto exit_failure;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021019 if (ctxt->nberrors != 0)
21020 goto exit;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021021
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021022 schema->doc = bucket->doc;
21023 schema->preserve = ctxt->preserve;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021024
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021025 ctxt->schema = schema;
21026
21027 if (xmlSchemaFixupComponents(ctxt) == -1)
21028 goto exit_failure;
21029
21030 /*
21031 * TODO: This is not nice, since we cannot distinguish from the
21032 * result if there was an internal error or not.
21033 */
21034exit:
21035 if (ctxt->nberrors != 0) {
21036 if (schema) {
21037 xmlSchemaFree(schema);
21038 schema = NULL;
21039 }
21040 if (ctxt->constructor) {
21041 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21042 ctxt->constructor = NULL;
21043 ctxt->ownsConstructor = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021044 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021045 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021046 ctxt->schema = NULL;
21047 return(schema);
21048exit_failure:
21049 /*
21050 * Quite verbose, but should catch internal errors, which were
21051 * not communitated.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021052 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021053 if (schema) {
21054 xmlSchemaFree(schema);
21055 schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021056 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021057 if (ctxt->constructor) {
21058 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21059 ctxt->constructor = NULL;
21060 ctxt->ownsConstructor = 0;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021061 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021062 PERROR_INT2("xmlSchemaParse",
21063 "An internal error occured");
21064 ctxt->schema = NULL;
21065 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000021066}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021067
Daniel Veillard4255d502002-04-16 15:50:10 +000021068/**
Daniel Veillard01c13b52002-12-10 15:19:08 +000021069 * xmlSchemaSetParserErrors:
Daniel Veillard4255d502002-04-16 15:50:10 +000021070 * @ctxt: a schema validation context
Daniel Veillard01c13b52002-12-10 15:19:08 +000021071 * @err: the error callback
21072 * @warn: the warning callback
21073 * @ctx: contextual data for the callbacks
Daniel Veillard4255d502002-04-16 15:50:10 +000021074 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021075 * Set the callback functions used to handle errors for a validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021076 */
21077void
21078xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021079 xmlSchemaValidityErrorFunc err,
21080 xmlSchemaValidityWarningFunc warn, void *ctx)
21081{
Daniel Veillard4255d502002-04-16 15:50:10 +000021082 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021083 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000021084 ctxt->error = err;
21085 ctxt->warning = warn;
21086 ctxt->userData = ctx;
21087}
21088
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021089/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000021090 * xmlSchemaGetParserErrors:
21091 * @ctxt: a XMl-Schema parser context
21092 * @err: the error callback result
21093 * @warn: the warning callback result
21094 * @ctx: contextual data for the callbacks result
21095 *
21096 * Get the callback information used to handle errors for a parser context
21097 *
21098 * Returns -1 in case of failure, 0 otherwise
21099 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021100int
Daniel Veillard259f0df2004-08-18 09:13:18 +000021101xmlSchemaGetParserErrors(xmlSchemaParserCtxtPtr ctxt,
21102 xmlSchemaValidityErrorFunc * err,
21103 xmlSchemaValidityWarningFunc * warn, void **ctx)
21104{
21105 if (ctxt == NULL)
21106 return(-1);
21107 if (err != NULL)
21108 *err = ctxt->error;
21109 if (warn != NULL)
21110 *warn = ctxt->warning;
21111 if (ctx != NULL)
21112 *ctx = ctxt->userData;
21113 return(0);
21114}
21115
21116/**
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021117 * xmlSchemaFacetTypeToString:
21118 * @type: the facet type
21119 *
21120 * Convert the xmlSchemaTypeType to a char string.
21121 *
21122 * Returns the char string representation of the facet type if the
21123 * type is a facet and an "Internal Error" string otherwise.
21124 */
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021125static const xmlChar *
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021126xmlSchemaFacetTypeToString(xmlSchemaTypeType type)
21127{
21128 switch (type) {
21129 case XML_SCHEMA_FACET_PATTERN:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021130 return (BAD_CAST "pattern");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021131 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021132 return (BAD_CAST "maxExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021133 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021134 return (BAD_CAST "maxInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021135 case XML_SCHEMA_FACET_MINEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021136 return (BAD_CAST "minExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021137 case XML_SCHEMA_FACET_MININCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021138 return (BAD_CAST "minInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021139 case XML_SCHEMA_FACET_WHITESPACE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021140 return (BAD_CAST "whiteSpace");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021141 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021142 return (BAD_CAST "enumeration");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021143 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021144 return (BAD_CAST "length");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021145 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021146 return (BAD_CAST "maxLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021147 case XML_SCHEMA_FACET_MINLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021148 return (BAD_CAST "minLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021149 case XML_SCHEMA_FACET_TOTALDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021150 return (BAD_CAST "totalDigits");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021151 case XML_SCHEMA_FACET_FRACTIONDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021152 return (BAD_CAST "fractionDigits");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021153 default:
21154 break;
21155 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021156 return (BAD_CAST "Internal Error");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021157}
21158
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021159static xmlSchemaWhitespaceValueType
Daniel Veillardc0826a72004-08-10 14:17:33 +000021160xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type)
21161{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021162 /*
21163 * The normalization type can be changed only for types which are derived
Daniel Veillardc0826a72004-08-10 14:17:33 +000021164 * from xsd:string.
21165 */
21166 if (type->type == XML_SCHEMA_TYPE_BASIC) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021167 /*
21168 * Note that we assume a whitespace of preserve for anySimpleType.
21169 */
21170 if ((type->builtInType == XML_SCHEMAS_STRING) ||
21171 (type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
21172 return(XML_SCHEMA_WHITESPACE_PRESERVE);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +000021173 else if (type->builtInType == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021174 return(XML_SCHEMA_WHITESPACE_REPLACE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021175 else {
21176 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021177 * For all ·atomic· datatypes other than string (and types ·derived·
21178 * by ·restriction· from it) the value of whiteSpace is fixed to
Daniel Veillardc0826a72004-08-10 14:17:33 +000021179 * collapse
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021180 * Note that this includes built-in list datatypes.
Daniel Veillardc0826a72004-08-10 14:17:33 +000021181 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021182 return(XML_SCHEMA_WHITESPACE_COLLAPSE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021183 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021184 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021185 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021186 * For list types the facet "whiteSpace" is fixed to "collapse".
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021187 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021188 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021189 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021190 return (XML_SCHEMA_WHITESPACE_UNKNOWN);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021191 } else if (WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021192 if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE)
21193 return (XML_SCHEMA_WHITESPACE_PRESERVE);
21194 else if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_REPLACE)
21195 return (XML_SCHEMA_WHITESPACE_REPLACE);
21196 else
21197 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021198 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021199 return (-1);
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021200}
21201
Daniel Veillard4255d502002-04-16 15:50:10 +000021202/************************************************************************
21203 * *
21204 * Simple type validation *
21205 * *
21206 ************************************************************************/
Daniel Veillard377e1a92004-04-16 16:30:05 +000021207
Daniel Veillard4255d502002-04-16 15:50:10 +000021208
21209/************************************************************************
21210 * *
21211 * DOM Validation code *
21212 * *
21213 ************************************************************************/
21214
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021215/**
21216 * xmlSchemaAssembleByLocation:
21217 * @pctxt: a schema parser context
21218 * @vctxt: a schema validation context
21219 * @schema: the existing schema
21220 * @node: the node that fired the assembling
21221 * @nsName: the namespace name of the new schema
21222 * @location: the location of the schema
21223 *
21224 * Expands an existing schema by an additional schema.
21225 *
21226 * Returns 0 if the new schema is correct, a positive error code
21227 * number otherwise and -1 in case of an internal or API error.
21228 */
21229static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021230xmlSchemaAssembleByLocation(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021231 xmlSchemaPtr schema,
21232 xmlNodePtr node,
21233 const xmlChar *nsName,
21234 const xmlChar *location)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021235{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021236 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021237 xmlSchemaParserCtxtPtr pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021238 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021239
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021240 if ((vctxt == NULL) || (schema == NULL))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021241 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021242
21243 if (vctxt->pctxt == NULL) {
21244 VERROR_INT("xmlSchemaAssembleByLocation",
21245 "no parser context available");
21246 return(-1);
21247 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021248 pctxt = vctxt->pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021249 if (pctxt->constructor == NULL) {
21250 PERROR_INT("xmlSchemaAssembleByLocation",
21251 "no constructor");
21252 return(-1);
21253 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021254 /*
21255 * Acquire the schema document.
21256 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021257 location = xmlSchemaBuildAbsoluteURI(pctxt->dict,
21258 location, node);
21259 /*
21260 * Note that we pass XML_SCHEMA_SCHEMA_IMPORT here;
21261 * the process will automatically change this to
21262 * XML_SCHEMA_SCHEMA_MAIN if it is the first schema document.
21263 */
21264 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
21265 location, NULL, NULL, 0, node, NULL, nsName,
21266 &bucket);
21267 if (ret != 0)
21268 return(ret);
21269 if (bucket == NULL) {
21270 PERROR_INT("xmlSchemaAssembleByLocation",
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021271 "no schema bucket acquired");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021272 return(-1);
21273 }
21274 /*
21275 * The first located schema will be handled as if all other
21276 * schemas imported by XSI were imported by this first schema.
21277 */
21278 if ((bucket != NULL) &&
21279 (WXS_CONSTRUCTOR(pctxt)->bucket == NULL))
21280 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
21281 /*
21282 * TODO: Is this handled like an import? I.e. is it not an error
21283 * if the schema cannot be located?
21284 */
21285 if ((bucket == NULL) || (! CAN_PARSE_SCHEMA(bucket)))
21286 return(0);
21287 /*
21288 * We will reuse the parser context for every schema imported
21289 * directly via XSI. So reset the context.
21290 */
21291 pctxt->nberrors = 0;
21292 pctxt->err = 0;
21293 pctxt->doc = bucket->doc;
21294
21295 ret = xmlSchemaParseNewDocWithContext(pctxt, schema, bucket);
21296 if (ret == -1) {
21297 pctxt->doc = NULL;
21298 goto exit_failure;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021299 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021300 /* Paranoid error channelling. */
21301 if ((ret == 0) && (pctxt->nberrors != 0))
21302 ret = pctxt->err;
21303 if (pctxt->nberrors == 0) {
21304 /*
21305 * Only bother to fixup pending components, if there was
21306 * no error yet.
21307 */
21308 xmlSchemaFixupComponents(pctxt);
21309 vctxt->nberrors += pctxt->nberrors;
21310 } else {
21311 /* Add to validation error sum. */
21312 vctxt->nberrors += pctxt->nberrors;
21313 }
21314 pctxt->doc = NULL;
21315 return(ret);
21316exit_failure:
21317 pctxt->doc = NULL;
21318 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021319}
21320
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021321static xmlSchemaAttrInfoPtr
21322xmlSchemaGetMetaAttrInfo(xmlSchemaValidCtxtPtr vctxt,
21323 int metaType)
21324{
21325 if (vctxt->nbAttrInfos == 0)
21326 return (NULL);
21327 {
21328 int i;
21329 xmlSchemaAttrInfoPtr iattr;
21330
21331 for (i = 0; i < vctxt->nbAttrInfos; i++) {
21332 iattr = vctxt->attrInfos[i];
21333 if (iattr->metaType == metaType)
21334 return (iattr);
21335 }
21336
21337 }
21338 return (NULL);
21339}
21340
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021341/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021342 * xmlSchemaAssembleByXSI:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021343 * @vctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021344 *
21345 * Expands an existing schema by an additional schema using
21346 * the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute
21347 * of an instance. If xsi:noNamespaceSchemaLocation is used, @noNamespace
21348 * must be set to 1.
21349 *
21350 * Returns 0 if the new schema is correct, a positive error code
21351 * number otherwise and -1 in case of an internal or API error.
21352 */
21353static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021354xmlSchemaAssembleByXSI(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021355{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021356 const xmlChar *cur, *end;
21357 const xmlChar *nsname = NULL, *location;
21358 int count = 0;
21359 int ret = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021360 xmlSchemaAttrInfoPtr iattr;
21361
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021362 /*
21363 * Parse the value; we will assume an even number of values
21364 * to be given (this is how Xerces and XSV work).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021365 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021366 * URGENT TODO: !! This needs to work for both
21367 * @noNamespaceSchemaLocation AND @schemaLocation on the same
21368 * element !!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021369 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021370 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
21371 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC);
21372 if (iattr == NULL)
21373 xmlSchemaGetMetaAttrInfo(vctxt,
21374 XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC);
21375 if (iattr == NULL)
21376 return (0);
21377 cur = iattr->value;
21378 do {
21379 if (iattr->metaType == XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021380 /*
21381 * Get the namespace name.
21382 */
21383 while (IS_BLANK_CH(*cur))
21384 cur++;
21385 end = cur;
21386 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21387 end++;
21388 if (end == cur)
21389 break;
21390 count++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021391 nsname = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021392 cur = end;
21393 }
21394 /*
21395 * Get the URI.
21396 */
21397 while (IS_BLANK_CH(*cur))
21398 cur++;
21399 end = cur;
21400 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21401 end++;
21402 if (end == cur)
21403 break;
21404 count++;
21405 location = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021406 cur = end;
21407 ret = xmlSchemaAssembleByLocation(vctxt, vctxt->schema,
21408 iattr->node, nsname, location);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021409 if (ret == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021410 VERROR_INT("xmlSchemaAssembleByXSI",
21411 "assembling schemata");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021412 return (-1);
21413 }
21414 } while (*cur != 0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021415 return (ret);
21416}
21417
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021418static const xmlChar *
21419xmlSchemaLookupNamespace(xmlSchemaValidCtxtPtr vctxt,
21420 const xmlChar *prefix)
21421{
21422 if (vctxt->sax != NULL) {
21423 int i, j;
21424 xmlSchemaNodeInfoPtr inode;
21425
21426 for (i = vctxt->depth; i >= 0; i--) {
21427 if (vctxt->elemInfos[i]->nbNsBindings != 0) {
21428 inode = vctxt->elemInfos[i];
21429 for (j = 0; j < inode->nbNsBindings * 2; j += 2) {
21430 if (((prefix == NULL) &&
21431 (inode->nsBindings[j] == NULL)) ||
21432 ((prefix != NULL) && xmlStrEqual(prefix,
21433 inode->nsBindings[j]))) {
21434
21435 /*
21436 * Note that the namespace bindings are already
21437 * in a string dict.
21438 */
21439 return (inode->nsBindings[j+1]);
21440 }
21441 }
21442 }
21443 }
21444 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021445#ifdef LIBXML_WRITER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021446 } else if (vctxt->reader != NULL) {
21447 xmlChar *nsName;
21448
21449 nsName = xmlTextReaderLookupNamespace(vctxt->reader, prefix);
21450 if (nsName != NULL) {
21451 const xmlChar *ret;
21452
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021453 ret = xmlDictLookup(vctxt->dict, nsName, -1);
21454 xmlFree(nsName);
21455 return (ret);
21456 } else
21457 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021458#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021459 } else {
21460 xmlNsPtr ns;
21461
21462 if ((vctxt->inode->node == NULL) ||
21463 (vctxt->inode->node->doc == NULL)) {
21464 VERROR_INT("xmlSchemaLookupNamespace",
21465 "no node or node's doc avaliable");
21466 return (NULL);
21467 }
21468 ns = xmlSearchNs(vctxt->inode->node->doc,
21469 vctxt->inode->node, prefix);
21470 if (ns != NULL)
21471 return (ns->href);
21472 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021473 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021474}
21475
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021476/*
21477* This one works on the schema of the validation context.
21478*/
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021479static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021480xmlSchemaValidateNotation(xmlSchemaValidCtxtPtr vctxt,
21481 xmlSchemaPtr schema,
21482 xmlNodePtr node,
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021483 const xmlChar *value,
21484 xmlSchemaValPtr *val,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021485 int valNeeded)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021486{
21487 int ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021488
21489 if (vctxt && (vctxt->schema == NULL)) {
21490 VERROR_INT("xmlSchemaValidateNotation",
21491 "a schema is needed on the validation context");
21492 return (-1);
21493 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021494 ret = xmlValidateQName(value, 1);
21495 if (ret != 0)
21496 return (ret);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021497 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021498 xmlChar *localName = NULL;
21499 xmlChar *prefix = NULL;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021500
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021501 localName = xmlSplitQName2(value, &prefix);
21502 if (prefix != NULL) {
21503 const xmlChar *nsName = NULL;
21504
21505 if (vctxt != NULL)
21506 nsName = xmlSchemaLookupNamespace(vctxt, BAD_CAST prefix);
21507 else if (node != NULL) {
21508 xmlNsPtr ns = xmlSearchNs(node->doc, node, prefix);
21509 if (ns != NULL)
21510 nsName = ns->href;
21511 } else {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021512 xmlFree(prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021513 xmlFree(localName);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021514 return (1);
21515 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021516 if (nsName == NULL) {
21517 xmlFree(prefix);
21518 xmlFree(localName);
21519 return (1);
21520 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021521 if (xmlSchemaGetNotation(schema, localName, nsName) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021522 if (valNeeded && (val != NULL)) {
21523 (*val) = xmlSchemaNewNOTATIONValue(BAD_CAST localName,
21524 BAD_CAST xmlStrdup(nsName));
21525 if (*val == NULL)
21526 ret = -1;
21527 }
21528 } else
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021529 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021530 xmlFree(prefix);
21531 xmlFree(localName);
21532 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021533 if (xmlSchemaGetNotation(schema, value, NULL) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021534 if (valNeeded && (val != NULL)) {
21535 (*val) = xmlSchemaNewNOTATIONValue(
21536 BAD_CAST xmlStrdup(value), NULL);
21537 if (*val == NULL)
21538 ret = -1;
21539 }
21540 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021541 return (1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021542 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021543 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021544 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021545}
21546
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000021547static int
21548xmlSchemaVAddNodeQName(xmlSchemaValidCtxtPtr vctxt,
21549 const xmlChar* lname,
21550 const xmlChar* nsname)
21551{
21552 int i;
21553
21554 lname = xmlDictLookup(vctxt->dict, lname, -1);
21555 if (lname == NULL)
21556 return(-1);
21557 if (nsname != NULL) {
21558 nsname = xmlDictLookup(vctxt->dict, nsname, -1);
21559 if (nsname == NULL)
21560 return(-1);
21561 }
21562 for (i = 0; i < vctxt->nodeQNames->nbItems; i += 2) {
21563 if ((vctxt->nodeQNames->items [i] == lname) &&
21564 (vctxt->nodeQNames->items[i +1] == nsname))
21565 /* Already there */
21566 return(i);
21567 }
21568 /* Add new entry. */
21569 i = vctxt->nodeQNames->nbItems;
21570 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) lname);
21571 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) nsname);
21572 return(i);
21573}
21574
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021575/************************************************************************
21576 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021577 * Validation of identity-constraints (IDC) *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021578 * *
21579 ************************************************************************/
21580
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021581/**
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021582 * xmlSchemaAugmentIDC:
21583 * @idcDef: the IDC definition
21584 *
21585 * Creates an augmented IDC definition item.
21586 *
21587 * Returns the item, or NULL on internal errors.
21588 */
21589static void
21590xmlSchemaAugmentIDC(xmlSchemaIDCPtr idcDef,
21591 xmlSchemaValidCtxtPtr vctxt)
21592{
21593 xmlSchemaIDCAugPtr aidc;
21594
21595 aidc = (xmlSchemaIDCAugPtr) xmlMalloc(sizeof(xmlSchemaIDCAug));
21596 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021597 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021598 "xmlSchemaAugmentIDC: allocating an augmented IDC definition",
21599 NULL);
21600 return;
21601 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021602 aidc->keyrefDepth = -1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021603 aidc->def = idcDef;
21604 aidc->next = NULL;
21605 if (vctxt->aidcs == NULL)
21606 vctxt->aidcs = aidc;
21607 else {
21608 aidc->next = vctxt->aidcs;
21609 vctxt->aidcs = aidc;
21610 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021611 /*
21612 * Save if we have keyrefs at all.
21613 */
21614 if ((vctxt->hasKeyrefs == 0) &&
21615 (idcDef->type == XML_SCHEMA_TYPE_IDC_KEYREF))
21616 vctxt->hasKeyrefs = 1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021617}
21618
21619/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021620 * xmlSchemaIDCNewBinding:
21621 * @idcDef: the IDC definition of this binding
21622 *
21623 * Creates a new IDC binding.
21624 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021625 * Returns the new IDC binding, NULL on internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021626 */
21627static xmlSchemaPSVIIDCBindingPtr
21628xmlSchemaIDCNewBinding(xmlSchemaIDCPtr idcDef)
21629{
21630 xmlSchemaPSVIIDCBindingPtr ret;
21631
21632 ret = (xmlSchemaPSVIIDCBindingPtr) xmlMalloc(
21633 sizeof(xmlSchemaPSVIIDCBinding));
21634 if (ret == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021635 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021636 "allocating a PSVI IDC binding item", NULL);
21637 return (NULL);
21638 }
21639 memset(ret, 0, sizeof(xmlSchemaPSVIIDCBinding));
21640 ret->definition = idcDef;
21641 return (ret);
21642}
21643
21644/**
21645 * xmlSchemaIDCStoreNodeTableItem:
21646 * @vctxt: the WXS validation context
21647 * @item: the IDC node table item
21648 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021649 * The validation context is used to store IDC node table items.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021650 * They are stored to avoid copying them if IDC node-tables are merged
21651 * with corresponding parent IDC node-tables (bubbling).
21652 *
21653 * Returns 0 if succeeded, -1 on internal errors.
21654 */
21655static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021656xmlSchemaIDCStoreNodeTableItem(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021657 xmlSchemaPSVIIDCNodePtr item)
21658{
21659 /*
21660 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021661 */
21662 if (vctxt->idcNodes == NULL) {
21663 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021664 xmlMalloc(20 * sizeof(xmlSchemaPSVIIDCNodePtr));
21665 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021666 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021667 "allocating the IDC node table item list", NULL);
21668 return (-1);
21669 }
21670 vctxt->sizeIdcNodes = 20;
21671 } else if (vctxt->sizeIdcNodes <= vctxt->nbIdcNodes) {
21672 vctxt->sizeIdcNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021673 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
21674 xmlRealloc(vctxt->idcNodes, vctxt->sizeIdcNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021675 sizeof(xmlSchemaPSVIIDCNodePtr));
21676 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021677 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021678 "re-allocating the IDC node table item list", NULL);
21679 return (-1);
21680 }
21681 }
21682 vctxt->idcNodes[vctxt->nbIdcNodes++] = item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021683
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021684 return (0);
21685}
21686
21687/**
21688 * xmlSchemaIDCStoreKey:
21689 * @vctxt: the WXS validation context
21690 * @item: the IDC key
21691 *
21692 * The validation context is used to store an IDC key.
21693 *
21694 * Returns 0 if succeeded, -1 on internal errors.
21695 */
21696static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021697xmlSchemaIDCStoreKey(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021698 xmlSchemaPSVIIDCKeyPtr key)
21699{
21700 /*
21701 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021702 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021703 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021704 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021705 xmlMalloc(40 * sizeof(xmlSchemaPSVIIDCKeyPtr));
21706 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021707 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021708 "allocating the IDC key storage list", NULL);
21709 return (-1);
21710 }
21711 vctxt->sizeIdcKeys = 40;
21712 } else if (vctxt->sizeIdcKeys <= vctxt->nbIdcKeys) {
21713 vctxt->sizeIdcKeys *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021714 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
21715 xmlRealloc(vctxt->idcKeys, vctxt->sizeIdcKeys *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021716 sizeof(xmlSchemaPSVIIDCKeyPtr));
21717 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021718 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021719 "re-allocating the IDC key storage list", NULL);
21720 return (-1);
21721 }
21722 }
21723 vctxt->idcKeys[vctxt->nbIdcKeys++] = key;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021724
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021725 return (0);
21726}
21727
21728/**
21729 * xmlSchemaIDCAppendNodeTableItem:
21730 * @bind: the IDC binding
21731 * @ntItem: the node-table item
21732 *
21733 * Appends the IDC node-table item to the binding.
21734 *
21735 * Returns 0 on success and -1 on internal errors.
21736 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021737static int
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021738xmlSchemaIDCAppendNodeTableItem(xmlSchemaPSVIIDCBindingPtr bind,
21739 xmlSchemaPSVIIDCNodePtr ntItem)
21740{
21741 if (bind->nodeTable == NULL) {
21742 bind->sizeNodes = 10;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021743 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021744 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
21745 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021746 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021747 "allocating an array of IDC node-table items", NULL);
21748 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021749 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021750 } else if (bind->sizeNodes <= bind->nbNodes) {
21751 bind->sizeNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021752 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
21753 xmlRealloc(bind->nodeTable, bind->sizeNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021754 sizeof(xmlSchemaPSVIIDCNodePtr));
21755 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021756 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021757 "re-allocating an array of IDC node-table items", NULL);
21758 return(-1);
21759 }
21760 }
21761 bind->nodeTable[bind->nbNodes++] = ntItem;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021762 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021763}
21764
21765/**
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021766 * xmlSchemaIDCAcquireBinding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021767 * @vctxt: the WXS validation context
21768 * @matcher: the IDC matcher
21769 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021770 * Looks up an PSVI IDC binding, for the IDC definition and
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021771 * of the given matcher. If none found, a new one is created
21772 * and added to the IDC table.
21773 *
21774 * Returns an IDC binding or NULL on internal errors.
21775 */
21776static xmlSchemaPSVIIDCBindingPtr
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021777xmlSchemaIDCAcquireBinding(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021778 xmlSchemaIDCMatcherPtr matcher)
21779{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021780 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021781
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021782 ielem = vctxt->elemInfos[matcher->depth];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021783
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021784 if (ielem->idcTable == NULL) {
21785 ielem->idcTable = xmlSchemaIDCNewBinding(matcher->aidc->def);
21786 if (ielem->idcTable == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021787 return (NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021788 return(ielem->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021789 } else {
21790 xmlSchemaPSVIIDCBindingPtr bind = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021791
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021792 bind = ielem->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021793 do {
21794 if (bind->definition == matcher->aidc->def)
21795 return(bind);
21796 if (bind->next == NULL) {
21797 bind->next = xmlSchemaIDCNewBinding(matcher->aidc->def);
21798 if (bind->next == NULL)
21799 return (NULL);
21800 return(bind->next);
21801 }
21802 bind = bind->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021803 } while (bind != NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021804 }
21805 return (NULL);
21806}
21807
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021808static xmlSchemaItemListPtr
21809xmlSchemaIDCAcquireTargetList(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
21810 xmlSchemaIDCMatcherPtr matcher)
21811{
21812 if (matcher->targets == NULL)
21813 matcher->targets = xmlSchemaItemListCreate();
21814 return(matcher->targets);
21815}
21816
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021817/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021818 * xmlSchemaIDCFreeKey:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021819 * @key: the IDC key
21820 *
21821 * Frees an IDC key together with its compiled value.
21822 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021823static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021824xmlSchemaIDCFreeKey(xmlSchemaPSVIIDCKeyPtr key)
21825{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021826 if (key->val != NULL)
21827 xmlSchemaFreeValue(key->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021828 xmlFree(key);
21829}
21830
21831/**
21832 * xmlSchemaIDCFreeBinding:
21833 *
21834 * Frees an IDC binding. Note that the node table-items
21835 * are not freed.
21836 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000021837static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021838xmlSchemaIDCFreeBinding(xmlSchemaPSVIIDCBindingPtr bind)
21839{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021840 if (bind->nodeTable != NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021841 xmlFree(bind->nodeTable);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021842 if (bind->dupls != NULL)
21843 xmlSchemaItemListFree(bind->dupls);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021844 xmlFree(bind);
21845}
21846
21847/**
21848 * xmlSchemaIDCFreeIDCTable:
21849 * @bind: the first IDC binding in the list
21850 *
21851 * Frees an IDC table, i.e. all the IDC bindings in the list.
21852 */
21853static void
21854xmlSchemaIDCFreeIDCTable(xmlSchemaPSVIIDCBindingPtr bind)
21855{
21856 xmlSchemaPSVIIDCBindingPtr prev;
21857
21858 while (bind != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021859 prev = bind;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021860 bind = bind->next;
21861 xmlSchemaIDCFreeBinding(prev);
21862 }
21863}
21864
21865/**
21866 * xmlSchemaIDCFreeMatcherList:
21867 * @matcher: the first IDC matcher in the list
21868 *
21869 * Frees a list of IDC matchers.
21870 */
21871static void
21872xmlSchemaIDCFreeMatcherList(xmlSchemaIDCMatcherPtr matcher)
21873{
21874 xmlSchemaIDCMatcherPtr next;
21875
21876 while (matcher != NULL) {
21877 next = matcher->next;
21878 if (matcher->keySeqs != NULL) {
21879 int i;
21880 for (i = 0; i < matcher->sizeKeySeqs; i++)
21881 if (matcher->keySeqs[i] != NULL)
21882 xmlFree(matcher->keySeqs[i]);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021883 xmlFree(matcher->keySeqs);
21884 }
21885 if (matcher->targets != NULL) {
21886 if (matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) {
21887 int i;
21888 xmlSchemaPSVIIDCNodePtr idcNode;
21889 /*
21890 * Node-table items for keyrefs are not stored globally
21891 * to the validation context, since they are not bubbled.
21892 * We need to free them here.
21893 */
21894 for (i = 0; i < matcher->targets->nbItems; i++) {
21895 idcNode =
21896 (xmlSchemaPSVIIDCNodePtr) matcher->targets->items[i];
21897 xmlFree(idcNode->keys);
21898 xmlFree(idcNode);
21899 }
21900 }
21901 xmlSchemaItemListFree(matcher->targets);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021902 }
21903 xmlFree(matcher);
21904 matcher = next;
21905 }
21906}
21907
21908/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021909 * xmlSchemaIDCAddStateObject:
21910 * @vctxt: the WXS validation context
21911 * @matcher: the IDC matcher
21912 * @sel: the XPath information
21913 * @parent: the parent "selector" state object if any
21914 * @type: "selector" or "field"
21915 *
21916 * Creates/reuses and activates state objects for the given
21917 * XPath information; if the XPath expression consists of unions,
21918 * multiple state objects are created for every unioned expression.
21919 *
21920 * Returns 0 on success and -1 on internal errors.
21921 */
21922static int
21923xmlSchemaIDCAddStateObject(xmlSchemaValidCtxtPtr vctxt,
21924 xmlSchemaIDCMatcherPtr matcher,
21925 xmlSchemaIDCSelectPtr sel,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021926 int type)
21927{
21928 xmlSchemaIDCStateObjPtr sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021929
21930 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021931 * Reuse the state objects from the pool.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021932 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021933 if (vctxt->xpathStatePool != NULL) {
21934 sto = vctxt->xpathStatePool;
21935 vctxt->xpathStatePool = sto->next;
21936 sto->next = NULL;
21937 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021938 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021939 * Create a new state object.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021940 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021941 sto = (xmlSchemaIDCStateObjPtr) xmlMalloc(sizeof(xmlSchemaIDCStateObj));
21942 if (sto == NULL) {
21943 xmlSchemaVErrMemory(NULL,
21944 "allocating an IDC state object", NULL);
21945 return (-1);
21946 }
21947 memset(sto, 0, sizeof(xmlSchemaIDCStateObj));
21948 }
21949 /*
21950 * Add to global list.
21951 */
21952 if (vctxt->xpathStates != NULL)
21953 sto->next = vctxt->xpathStates;
21954 vctxt->xpathStates = sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021955
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021956 /*
21957 * Free the old xpath validation context.
21958 */
21959 if (sto->xpathCtxt != NULL)
21960 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
21961
21962 /*
21963 * Create a new XPath (pattern) validation context.
21964 */
21965 sto->xpathCtxt = (void *) xmlPatternGetStreamCtxt(
21966 (xmlPatternPtr) sel->xpathComp);
21967 if (sto->xpathCtxt == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021968 VERROR_INT("xmlSchemaIDCAddStateObject",
21969 "failed to create an XPath validation context");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021970 return (-1);
21971 }
21972 sto->type = type;
21973 sto->depth = vctxt->depth;
21974 sto->matcher = matcher;
21975 sto->sel = sel;
21976 sto->nbHistory = 0;
21977
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021978#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021979 xmlGenericError(xmlGenericErrorContext, "IDC: STO push '%s'\n",
21980 sto->sel->xpath);
21981#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021982 return (0);
21983}
21984
21985/**
21986 * xmlSchemaXPathEvaluate:
21987 * @vctxt: the WXS validation context
21988 * @nodeType: the nodeType of the current node
21989 *
21990 * Evaluates all active XPath state objects.
21991 *
21992 * Returns the number of IC "field" state objects which resolved to
21993 * this node, 0 if none resolved and -1 on internal errors.
21994 */
21995static int
21996xmlSchemaXPathEvaluate(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021997 xmlElementType nodeType)
21998{
21999 xmlSchemaIDCStateObjPtr sto, head = NULL, first;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022000 int res, resolved = 0, depth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022001
22002 if (vctxt->xpathStates == NULL)
22003 return (0);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022004
22005 if (nodeType == XML_ATTRIBUTE_NODE)
22006 depth++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022007#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022008 {
22009 xmlChar *str = NULL;
22010 xmlGenericError(xmlGenericErrorContext,
22011 "IDC: EVAL on %s, depth %d, type %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022012 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22013 vctxt->inode->localName), depth, nodeType);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022014 FREE_AND_NULL(str)
22015 }
22016#endif
22017 /*
22018 * Process all active XPath state objects.
22019 */
22020 first = vctxt->xpathStates;
22021 sto = first;
22022 while (sto != head) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022023#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022024 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022025 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] selector '%s'\n",
22026 sto->matcher->aidc->def->name, sto->sel->xpath);
22027 else
22028 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] field '%s'\n",
22029 sto->matcher->aidc->def->name, sto->sel->xpath);
22030#endif
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022031 if (nodeType == XML_ELEMENT_NODE)
22032 res = xmlStreamPush((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022033 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022034 else
22035 res = xmlStreamPushAttr((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022036 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022037
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022038 if (res == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022039 VERROR_INT("xmlSchemaXPathEvaluate",
22040 "calling xmlStreamPush()");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022041 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022042 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022043 if (res == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022044 goto next_sto;
22045 /*
22046 * Full match.
22047 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022048#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022049 xmlGenericError(xmlGenericErrorContext, "IDC: "
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022050 "MATCH\n");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022051#endif
22052 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022053 * Register a match in the state object history.
22054 */
22055 if (sto->history == NULL) {
22056 sto->history = (int *) xmlMalloc(5 * sizeof(int));
22057 if (sto->history == NULL) {
22058 xmlSchemaVErrMemory(NULL,
22059 "allocating the state object history", NULL);
22060 return(-1);
22061 }
22062 sto->sizeHistory = 10;
22063 } else if (sto->sizeHistory <= sto->nbHistory) {
22064 sto->sizeHistory *= 2;
22065 sto->history = (int *) xmlRealloc(sto->history,
22066 sto->sizeHistory * sizeof(int));
22067 if (sto->history == NULL) {
22068 xmlSchemaVErrMemory(NULL,
22069 "re-allocating the state object history", NULL);
22070 return(-1);
22071 }
22072 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022073 sto->history[sto->nbHistory++] = depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022074
22075#ifdef DEBUG_IDC
22076 xmlGenericError(xmlGenericErrorContext, "IDC: push match '%d'\n",
22077 vctxt->depth);
22078#endif
22079
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022080 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
22081 xmlSchemaIDCSelectPtr sel;
22082 /*
22083 * Activate state objects for the IDC fields of
22084 * the IDC selector.
22085 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022086#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022087 xmlGenericError(xmlGenericErrorContext, "IDC: "
22088 "activating field states\n");
22089#endif
22090 sel = sto->matcher->aidc->def->fields;
22091 while (sel != NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022092 if (xmlSchemaIDCAddStateObject(vctxt, sto->matcher,
22093 sel, XPATH_STATE_OBJ_TYPE_IDC_FIELD) == -1)
22094 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022095 sel = sel->next;
22096 }
22097 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
22098 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000022099 * An IDC key node was found by the IDC field.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022100 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022101#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022102 xmlGenericError(xmlGenericErrorContext,
22103 "IDC: key found\n");
22104#endif
22105 /*
22106 * Notify that the character value of this node is
22107 * needed.
22108 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022109 if (resolved == 0) {
22110 if ((vctxt->inode->flags &
22111 XML_SCHEMA_NODE_INFO_VALUE_NEEDED) == 0)
22112 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
22113 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022114 resolved++;
22115 }
22116next_sto:
22117 if (sto->next == NULL) {
22118 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022119 * Evaluate field state objects created on this node as well.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022120 */
22121 head = first;
22122 sto = vctxt->xpathStates;
22123 } else
22124 sto = sto->next;
22125 }
22126 return (resolved);
22127}
22128
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022129static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022130xmlSchemaFormatIDCKeySequence(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022131 xmlChar **buf,
22132 xmlSchemaPSVIIDCKeyPtr *seq,
22133 int count)
22134{
22135 int i, res;
22136 const xmlChar *value = NULL;
22137
22138 *buf = xmlStrdup(BAD_CAST "[");
22139 for (i = 0; i < count; i++) {
22140 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022141 res = xmlSchemaGetCanonValueWhtsp(seq[i]->val, &value,
22142 xmlSchemaGetWhiteSpaceFacetValue(seq[i]->type));
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022143 if (res == 0)
22144 *buf = xmlStrcat(*buf, value);
22145 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022146 VERROR_INT("xmlSchemaFormatIDCKeySequence",
22147 "failed to compute a canonical value");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022148 *buf = xmlStrcat(*buf, BAD_CAST "???");
22149 }
22150 if (i < count -1)
22151 *buf = xmlStrcat(*buf, BAD_CAST "', ");
22152 else
22153 *buf = xmlStrcat(*buf, BAD_CAST "'");
22154 if (value != NULL) {
22155 xmlFree((xmlChar *) value);
22156 value = NULL;
22157 }
22158 }
22159 *buf = xmlStrcat(*buf, BAD_CAST "]");
22160
22161 return (BAD_CAST *buf);
22162}
22163
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022164/**
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000022165 * xmlSchemaXPathPop:
22166 * @vctxt: the WXS validation context
22167 *
22168 * Pops all XPath states.
22169 *
22170 * Returns 0 on success and -1 on internal errors.
22171 */
22172static int
22173xmlSchemaXPathPop(xmlSchemaValidCtxtPtr vctxt)
22174{
22175 xmlSchemaIDCStateObjPtr sto;
22176 int res;
22177
22178 if (vctxt->xpathStates == NULL)
22179 return(0);
22180 sto = vctxt->xpathStates;
22181 do {
22182 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22183 if (res == -1)
22184 return (-1);
22185 sto = sto->next;
22186 } while (sto != NULL);
22187 return(0);
22188}
22189
22190/**
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022191 * xmlSchemaXPathProcessHistory:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022192 * @vctxt: the WXS validation context
22193 * @type: the simple/complex type of the current node if any at all
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022194 * @val: the precompiled value
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022195 *
22196 * Processes and pops the history items of the IDC state objects.
22197 * IDC key-sequences are validated/created on IDC bindings.
22198 *
22199 * Returns 0 on success and -1 on internal errors.
22200 */
22201static int
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022202xmlSchemaXPathProcessHistory(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022203 int depth)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022204{
22205 xmlSchemaIDCStateObjPtr sto, nextsto;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022206 int res, matchDepth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022207 xmlSchemaPSVIIDCKeyPtr key = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022208 xmlSchemaTypePtr type = vctxt->inode->typeDef, simpleType = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022209
22210 if (vctxt->xpathStates == NULL)
22211 return (0);
22212 sto = vctxt->xpathStates;
22213
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022214#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022215 {
22216 xmlChar *str = NULL;
22217 xmlGenericError(xmlGenericErrorContext,
22218 "IDC: BACK on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022219 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22220 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022221 FREE_AND_NULL(str)
22222 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022223#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022224 /*
22225 * Evaluate the state objects.
22226 */
22227 while (sto != NULL) {
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000022228 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22229 if (res == -1) {
22230 VERROR_INT("xmlSchemaXPathProcessHistory",
22231 "calling xmlStreamPop()");
22232 return (-1);
22233 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022234#ifdef DEBUG_IDC
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000022235 xmlGenericError(xmlGenericErrorContext, "IDC: stream pop '%s'\n",
22236 sto->sel->xpath);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022237#endif
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022238 if (sto->nbHistory == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022239 goto deregister_check;
22240
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022241 matchDepth = sto->history[sto->nbHistory -1];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022242
22243 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022244 * Only matches at the current depth are of interest.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022245 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022246 if (matchDepth != depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022247 sto = sto->next;
22248 continue;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022249 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022250 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022251 /*
22252 * NOTE: According to
22253 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2198
22254 * ... the simple-content of complex types is also allowed.
22255 */
22256
22257 if (WXS_IS_COMPLEX(type)) {
22258 if (WXS_HAS_SIMPLE_CONTENT(type)) {
22259 /*
22260 * Sanity check for complex types with simple content.
22261 */
22262 simpleType = type->contentTypeDef;
22263 if (simpleType == NULL) {
22264 VERROR_INT("xmlSchemaXPathProcessHistory",
22265 "field resolves to a CT with simple content "
22266 "but the CT is missing the ST definition");
22267 return (-1);
22268 }
22269 } else
22270 simpleType = NULL;
22271 } else
22272 simpleType = type;
22273 if (simpleType == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022274 xmlChar *str = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022275
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022276 /*
22277 * Not qualified if the field resolves to a node of non
22278 * simple type.
22279 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022280 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022281 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022282 WXS_BASIC_CAST sto->matcher->aidc->def,
22283 "The XPath '%s' of a field of %s does evaluate to a node of "
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022284 "non-simple type",
22285 sto->sel->xpath,
22286 xmlSchemaGetIDCDesignation(&str, sto->matcher->aidc->def));
22287 FREE_AND_NULL(str);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022288 sto->nbHistory--;
22289 goto deregister_check;
22290 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022291
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022292 if ((key == NULL) && (vctxt->inode->val == NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022293 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022294 * Failed to provide the normalized value; maybe
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022295 * the value was invalid.
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022296 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022297 VERROR(XML_SCHEMAV_CVC_IDC,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022298 WXS_BASIC_CAST sto->matcher->aidc->def,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000022299 "Warning: No precomputed value available, the value "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022300 "was either invalid or something strange happend");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022301 sto->nbHistory--;
22302 goto deregister_check;
22303 } else {
22304 xmlSchemaIDCMatcherPtr matcher = sto->matcher;
22305 xmlSchemaPSVIIDCKeyPtr *keySeq;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022306 int pos, idx;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022307
22308 /*
22309 * The key will be anchored on the matcher's list of
22310 * key-sequences. The position in this list is determined
22311 * by the target node's depth relative to the matcher's
22312 * depth of creation (i.e. the depth of the scope element).
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022313 *
22314 * Element Depth Pos List-entries
22315 * <scope> 0 NULL
22316 * <bar> 1 NULL
22317 * <target/> 2 2 target
22318 * <bar>
22319 * </scope>
22320 *
22321 * The size of the list is only dependant on the depth of
22322 * the tree.
22323 * An entry will be NULLed in selector_leave, i.e. when
22324 * we hit the target's
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022325 */
22326 pos = sto->depth - matcher->depth;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022327 idx = sto->sel->index;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022328
22329 /*
22330 * Create/grow the array of key-sequences.
22331 */
22332 if (matcher->keySeqs == NULL) {
22333 if (pos > 9)
22334 matcher->sizeKeySeqs = pos * 2;
22335 else
22336 matcher->sizeKeySeqs = 10;
22337 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22338 xmlMalloc(matcher->sizeKeySeqs *
22339 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22340 if (matcher->keySeqs == NULL) {
22341 xmlSchemaVErrMemory(NULL,
22342 "allocating an array of key-sequences",
22343 NULL);
22344 return(-1);
22345 }
22346 memset(matcher->keySeqs, 0,
22347 matcher->sizeKeySeqs *
22348 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22349 } else if (pos >= matcher->sizeKeySeqs) {
22350 int i = matcher->sizeKeySeqs;
22351
22352 matcher->sizeKeySeqs *= 2;
22353 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22354 xmlRealloc(matcher->keySeqs,
22355 matcher->sizeKeySeqs *
22356 sizeof(xmlSchemaPSVIIDCKeyPtr *));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022357 if (matcher->keySeqs == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022358 xmlSchemaVErrMemory(NULL,
22359 "reallocating an array of key-sequences",
22360 NULL);
22361 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022362 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022363 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022364 * The array needs to be NULLed.
22365 * TODO: Use memset?
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022366 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022367 for (; i < matcher->sizeKeySeqs; i++)
22368 matcher->keySeqs[i] = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022369 }
22370
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022371 /*
22372 * Get/create the key-sequence.
22373 */
22374 keySeq = matcher->keySeqs[pos];
22375 if (keySeq == NULL) {
22376 goto create_sequence;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022377 } else if (keySeq[idx] != NULL) {
22378 xmlChar *str = NULL;
22379 /*
22380 * cvc-identity-constraint:
22381 * 3 For each node in the ·target node set· all
22382 * of the {fields}, with that node as the context
22383 * node, evaluate to either an empty node-set or
22384 * a node-set with exactly one member, which must
22385 * have a simple type.
22386 *
22387 * The key was already set; report an error.
22388 */
22389 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22390 XML_SCHEMAV_CVC_IDC, NULL,
22391 WXS_BASIC_CAST matcher->aidc->def,
22392 "The XPath '%s' of a field of %s evaluates to a "
22393 "node-set with more than one member",
22394 sto->sel->xpath,
22395 xmlSchemaGetIDCDesignation(&str, matcher->aidc->def));
22396 FREE_AND_NULL(str);
22397 sto->nbHistory--;
22398 goto deregister_check;
22399 } else
22400 goto create_key;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022401
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022402create_sequence:
22403 /*
22404 * Create a key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022405 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022406 keySeq = (xmlSchemaPSVIIDCKeyPtr *) xmlMalloc(
22407 matcher->aidc->def->nbFields *
22408 sizeof(xmlSchemaPSVIIDCKeyPtr));
22409 if (keySeq == NULL) {
22410 xmlSchemaVErrMemory(NULL,
22411 "allocating an IDC key-sequence", NULL);
22412 return(-1);
22413 }
22414 memset(keySeq, 0, matcher->aidc->def->nbFields *
22415 sizeof(xmlSchemaPSVIIDCKeyPtr));
22416 matcher->keySeqs[pos] = keySeq;
22417create_key:
22418 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022419 * Create a key once per node only.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022420 */
22421 if (key == NULL) {
22422 key = (xmlSchemaPSVIIDCKeyPtr) xmlMalloc(
22423 sizeof(xmlSchemaPSVIIDCKey));
22424 if (key == NULL) {
22425 xmlSchemaVErrMemory(NULL,
22426 "allocating a IDC key", NULL);
22427 xmlFree(keySeq);
22428 matcher->keySeqs[pos] = NULL;
22429 return(-1);
22430 }
22431 /*
22432 * Consume the compiled value.
22433 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022434 key->type = simpleType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022435 key->val = vctxt->inode->val;
22436 vctxt->inode->val = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022437 /*
22438 * Store the key in a global list.
22439 */
22440 if (xmlSchemaIDCStoreKey(vctxt, key) == -1) {
22441 xmlSchemaIDCFreeKey(key);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022442 return (-1);
22443 }
22444 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022445 keySeq[idx] = key;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022446 }
22447 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022448
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022449 xmlSchemaPSVIIDCKeyPtr **keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022450 /* xmlSchemaPSVIIDCBindingPtr bind; */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022451 xmlSchemaPSVIIDCNodePtr ntItem;
22452 xmlSchemaIDCMatcherPtr matcher;
22453 xmlSchemaIDCPtr idc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022454 xmlSchemaItemListPtr targets;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022455 int pos, i, j, nbKeys;
22456 /*
22457 * Here we have the following scenario:
22458 * An IDC 'selector' state object resolved to a target node,
22459 * during the time this target node was in the
22460 * ancestor-or-self axis, the 'field' state object(s) looked
22461 * out for matching nodes to create a key-sequence for this
22462 * target node. Now we are back to this target node and need
22463 * to put the key-sequence, together with the target node
22464 * itself, into the node-table of the corresponding IDC
22465 * binding.
22466 */
22467 matcher = sto->matcher;
22468 idc = matcher->aidc->def;
22469 nbKeys = idc->nbFields;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022470 pos = depth - matcher->depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022471 /*
22472 * Check if the matcher has any key-sequences at all, plus
22473 * if it has a key-sequence for the current target node.
22474 */
22475 if ((matcher->keySeqs == NULL) ||
22476 (matcher->sizeKeySeqs <= pos)) {
22477 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22478 goto selector_key_error;
22479 else
22480 goto selector_leave;
22481 }
22482
22483 keySeq = &(matcher->keySeqs[pos]);
22484 if (*keySeq == NULL) {
22485 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22486 goto selector_key_error;
22487 else
22488 goto selector_leave;
22489 }
22490
22491 for (i = 0; i < nbKeys; i++) {
22492 if ((*keySeq)[i] == NULL) {
22493 /*
22494 * Not qualified, if not all fields did resolve.
22495 */
22496 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY) {
22497 /*
22498 * All fields of a "key" IDC must resolve.
22499 */
22500 goto selector_key_error;
22501 }
22502 goto selector_leave;
22503 }
22504 }
22505 /*
22506 * All fields did resolve.
22507 */
22508
22509 /*
22510 * 4.1 If the {identity-constraint category} is unique(/key),
22511 * then no two members of the ·qualified node set· have
22512 * ·key-sequences· whose members are pairwise equal, as
22513 * defined by Equal in [XML Schemas: Datatypes].
22514 *
22515 * Get the IDC binding from the matcher and check for
22516 * duplicate key-sequences.
22517 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022518#if 0
22519 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22520#endif
22521 targets = xmlSchemaIDCAcquireTargetList(vctxt, matcher);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022522 if ((idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) &&
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022523 (targets->nbItems != 0)) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022524 xmlSchemaPSVIIDCKeyPtr ckey, bkey, *bkeySeq;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022525
22526 i = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022527 res = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022528 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022529 * Compare the key-sequences, key by key.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022530 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022531 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022532 bkeySeq =
22533 ((xmlSchemaPSVIIDCNodePtr) targets->items[i])->keys;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022534 for (j = 0; j < nbKeys; j++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022535 ckey = (*keySeq)[j];
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022536 bkey = bkeySeq[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022537 res = xmlSchemaAreValuesEqual(ckey->val, bkey->val);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022538 if (res == -1) {
22539 return (-1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022540 } else if (res == 0) {
22541 /*
22542 * One of the keys differs, so the key-sequence
22543 * won't be equal; get out.
22544 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022545 break;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022546 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022547 }
22548 if (res == 1) {
22549 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022550 * Duplicate key-sequence found.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022551 */
22552 break;
22553 }
22554 i++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022555 } while (i < targets->nbItems);
22556 if (i != targets->nbItems) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022557 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022558 /*
22559 * TODO: Try to report the key-sequence.
22560 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022561 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022562 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022563 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022564 "Duplicate key-sequence %s in %s",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022565 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022566 (*keySeq), nbKeys),
22567 xmlSchemaGetIDCDesignation(&strB, idc));
22568 FREE_AND_NULL(str);
22569 FREE_AND_NULL(strB);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022570 goto selector_leave;
22571 }
22572 }
22573 /*
22574 * Add a node-table item to the IDC binding.
22575 */
22576 ntItem = (xmlSchemaPSVIIDCNodePtr) xmlMalloc(
22577 sizeof(xmlSchemaPSVIIDCNode));
22578 if (ntItem == NULL) {
22579 xmlSchemaVErrMemory(NULL,
22580 "allocating an IDC node-table item", NULL);
22581 xmlFree(*keySeq);
22582 *keySeq = NULL;
22583 return(-1);
22584 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022585 memset(ntItem, 0, sizeof(xmlSchemaPSVIIDCNode));
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022586
22587 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022588 * Store the node-table item in a global list.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022589 */
22590 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) {
22591 if (xmlSchemaIDCStoreNodeTableItem(vctxt, ntItem) == -1) {
22592 xmlFree(ntItem);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022593 xmlFree(*keySeq);
22594 *keySeq = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022595 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022596 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022597 ntItem->nodeQNameID = -1;
22598 } else {
22599 /*
22600 * Save a cached QName for this node on the IDC node, to be
22601 * able to report it, even if the node is not saved.
22602 */
22603 ntItem->nodeQNameID = xmlSchemaVAddNodeQName(vctxt,
22604 vctxt->inode->localName, vctxt->inode->nsName);
22605 if (ntItem->nodeQNameID == -1) {
22606 xmlFree(ntItem);
22607 xmlFree(*keySeq);
22608 *keySeq = NULL;
22609 return (-1);
22610 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022611 }
22612 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022613 * Init the node-table item: Save the node, position and
22614 * consume the key-sequence.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022615 */
22616 ntItem->node = vctxt->node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022617 ntItem->nodeLine = vctxt->inode->nodeLine;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022618 ntItem->keys = *keySeq;
22619 *keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022620#if 0
22621 if (xmlSchemaIDCAppendNodeTableItem(bind, ntItem) == -1) {
22622#endif
22623 if (xmlSchemaItemListAdd(targets, ntItem) == -1) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022624 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22625 /*
22626 * Free the item, since keyref items won't be
22627 * put on a global list.
22628 */
22629 xmlFree(ntItem->keys);
22630 xmlFree(ntItem);
22631 }
22632 return (-1);
22633 }
22634
22635 goto selector_leave;
22636selector_key_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022637 {
22638 xmlChar *str = NULL;
22639 /*
22640 * 4.2.1 (KEY) The ·target node set· and the
22641 * ·qualified node set· are equal, that is, every
22642 * member of the ·target node set· is also a member
22643 * of the ·qualified node set· and vice versa.
22644 */
22645 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22646 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022647 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022648 "Not all fields of %s evaluate to a node",
22649 xmlSchemaGetIDCDesignation(&str, idc), NULL);
22650 FREE_AND_NULL(str);
22651 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022652selector_leave:
22653 /*
22654 * Free the key-sequence if not added to the IDC table.
22655 */
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022656 if ((keySeq != NULL) && (*keySeq != NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022657 xmlFree(*keySeq);
22658 *keySeq = NULL;
22659 }
22660 } /* if selector */
22661
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022662 sto->nbHistory--;
22663
22664deregister_check:
22665 /*
22666 * Deregister state objects if they reach the depth of creation.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022667 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022668 if ((sto->nbHistory == 0) && (sto->depth == depth)) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022669#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022670 xmlGenericError(xmlGenericErrorContext, "IDC: STO pop '%s'\n",
22671 sto->sel->xpath);
22672#endif
22673 if (vctxt->xpathStates != sto) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022674 VERROR_INT("xmlSchemaXPathProcessHistory",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022675 "The state object to be removed is not the first "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022676 "in the list");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022677 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022678 nextsto = sto->next;
22679 /*
22680 * Unlink from the list of active XPath state objects.
22681 */
22682 vctxt->xpathStates = sto->next;
22683 sto->next = vctxt->xpathStatePool;
22684 /*
22685 * Link it to the pool of reusable state objects.
22686 */
22687 vctxt->xpathStatePool = sto;
22688 sto = nextsto;
22689 } else
22690 sto = sto->next;
22691 } /* while (sto != NULL) */
22692 return (0);
22693}
22694
22695/**
22696 * xmlSchemaIDCRegisterMatchers:
22697 * @vctxt: the WXS validation context
22698 * @elemDecl: the element declaration
22699 *
22700 * Creates helper objects to evaluate IDC selectors/fields
22701 * successively.
22702 *
22703 * Returns 0 if OK and -1 on internal errors.
22704 */
22705static int
22706xmlSchemaIDCRegisterMatchers(xmlSchemaValidCtxtPtr vctxt,
22707 xmlSchemaElementPtr elemDecl)
22708{
22709 xmlSchemaIDCMatcherPtr matcher, last = NULL;
22710 xmlSchemaIDCPtr idc, refIdc;
22711 xmlSchemaIDCAugPtr aidc;
22712
22713 idc = (xmlSchemaIDCPtr) elemDecl->idcs;
22714 if (idc == NULL)
22715 return (0);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022716
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022717#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022718 {
22719 xmlChar *str = NULL;
22720 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022721 "IDC: REGISTER on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022722 (char *) xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22723 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022724 FREE_AND_NULL(str)
22725 }
22726#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022727 if (vctxt->inode->idcMatchers != NULL) {
22728 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22729 "The chain of IDC matchers is expected to be empty");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022730 return (-1);
22731 }
22732 do {
22733 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22734 /*
22735 * Since IDCs bubbles are expensive we need to know the
22736 * depth at which the bubbles should stop; this will be
22737 * the depth of the top-most keyref IDC. If no keyref
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022738 * references a key/unique IDC, the keyrefDepth will
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022739 * be -1, indicating that no bubbles are needed.
22740 */
22741 refIdc = (xmlSchemaIDCPtr) idc->ref->item;
22742 if (refIdc != NULL) {
22743 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022744 * Remember that we have keyrefs on this node.
22745 */
22746 vctxt->inode->hasKeyrefs = 1;
22747 /*
22748 * Lookup the referenced augmented IDC info.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022749 */
22750 aidc = vctxt->aidcs;
22751 while (aidc != NULL) {
22752 if (aidc->def == refIdc)
22753 break;
22754 aidc = aidc->next;
22755 }
22756 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022757 VERROR_INT("xmlSchemaIDCRegisterMatchers",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022758 "Could not find an augmented IDC item for an IDC "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022759 "definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022760 return (-1);
22761 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022762 if ((aidc->keyrefDepth == -1) ||
22763 (vctxt->depth < aidc->keyrefDepth))
22764 aidc->keyrefDepth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022765 }
22766 }
22767 /*
22768 * Lookup the augmented IDC item for the IDC definition.
22769 */
22770 aidc = vctxt->aidcs;
22771 while (aidc != NULL) {
22772 if (aidc->def == idc)
22773 break;
22774 aidc = aidc->next;
22775 }
22776 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022777 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22778 "Could not find an augmented IDC item for an IDC definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022779 return (-1);
22780 }
22781 /*
22782 * Create an IDC matcher for every IDC definition.
22783 */
22784 matcher = (xmlSchemaIDCMatcherPtr)
22785 xmlMalloc(sizeof(xmlSchemaIDCMatcher));
22786 if (matcher == NULL) {
22787 xmlSchemaVErrMemory(vctxt,
22788 "allocating an IDC matcher", NULL);
22789 return (-1);
22790 }
22791 memset(matcher, 0, sizeof(xmlSchemaIDCMatcher));
22792 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022793 vctxt->inode->idcMatchers = matcher;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022794 else
22795 last->next = matcher;
22796 last = matcher;
22797
22798 matcher->type = IDC_MATCHER;
22799 matcher->depth = vctxt->depth;
22800 matcher->aidc = aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022801 matcher->idcType = aidc->def->type;
22802#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022803 xmlGenericError(xmlGenericErrorContext, "IDC: register matcher\n");
22804#endif
22805 /*
22806 * Init the automaton state object.
22807 */
22808 if (xmlSchemaIDCAddStateObject(vctxt, matcher,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022809 idc->selector, XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) == -1)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022810 return (-1);
22811
22812 idc = idc->next;
22813 } while (idc != NULL);
22814 return (0);
22815}
22816
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022817static int
22818xmlSchemaIDCFillNodeTables(xmlSchemaValidCtxtPtr vctxt,
22819 xmlSchemaNodeInfoPtr ielem)
22820{
22821 xmlSchemaPSVIIDCBindingPtr bind;
22822 int res, i, j, k, nbTargets, nbFields, nbDupls, nbNodeTable;
22823 xmlSchemaPSVIIDCKeyPtr *keys, *ntkeys;
22824 xmlSchemaPSVIIDCNodePtr *targets, *dupls;
22825
22826 xmlSchemaIDCMatcherPtr matcher = ielem->idcMatchers;
22827 /* vctxt->createIDCNodeTables */
22828 while (matcher != NULL) {
22829 /*
22830 * Skip keyref IDCs and empty IDC target-lists.
22831 */
22832 if ((matcher->aidc->def->type == XML_SCHEMA_TYPE_IDC_KEYREF) ||
22833 WXS_ILIST_IS_EMPTY(matcher->targets))
22834 {
22835 matcher = matcher->next;
22836 continue;
22837 }
22838 /*
22839 * If we _want_ the IDC node-table to be created in any case
22840 * then do so. Otherwise create them only if keyrefs need them.
22841 */
22842 if ((! vctxt->createIDCNodeTables) &&
22843 ((matcher->aidc->keyrefDepth == -1) ||
22844 (matcher->aidc->keyrefDepth > vctxt->depth)))
22845 {
22846 matcher = matcher->next;
22847 continue;
22848 }
22849 /*
22850 * Get/create the IDC binding on this element for the IDC definition.
22851 */
22852 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22853
22854 if (! WXS_ILIST_IS_EMPTY(bind->dupls)) {
22855 dupls = (xmlSchemaPSVIIDCNodePtr *) bind->dupls->items;
22856 nbDupls = bind->dupls->nbItems;
22857 } else {
22858 dupls = NULL;
22859 nbDupls = 0;
22860 }
22861 if (bind->nodeTable != NULL) {
22862 nbNodeTable = bind->nbNodes;
22863 } else {
22864 nbNodeTable = 0;
22865 }
22866
22867 if ((nbNodeTable == 0) && (nbDupls == 0)) {
22868 /*
22869 * Transfer all IDC target-nodes to the IDC node-table.
22870 */
22871 bind->nodeTable =
22872 (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
22873 bind->sizeNodes = matcher->targets->sizeItems;
22874 bind->nbNodes = matcher->targets->nbItems;
22875
22876 matcher->targets->items = NULL;
22877 matcher->targets->sizeItems = 0;
22878 matcher->targets->nbItems = 0;
22879 } else {
22880 /*
22881 * Compare the key-sequences and add to the IDC node-table.
22882 */
22883 nbTargets = matcher->targets->nbItems;
22884 targets = (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
22885 nbFields = matcher->aidc->def->nbFields;
22886 i = 0;
22887 do {
22888 keys = targets[i]->keys;
22889 if (nbDupls) {
22890 /*
22891 * Search in already found duplicates first.
22892 */
22893 j = 0;
22894 do {
22895 if (nbFields == 1) {
22896 res = xmlSchemaAreValuesEqual(keys[0]->val,
22897 dupls[j]->keys[0]->val);
22898 if (res == -1)
22899 goto internal_error;
22900 if (res == 1) {
22901 /*
22902 * Equal key-sequence.
22903 */
22904 goto next_target;
22905 }
22906 } else {
22907 res = 0;
22908 ntkeys = dupls[j]->keys;
22909 for (k = 0; k < nbFields; k++) {
22910 res = xmlSchemaAreValuesEqual(keys[k]->val,
22911 ntkeys[k]->val);
22912 if (res == -1)
22913 goto internal_error;
22914 if (res == 0) {
22915 /*
22916 * One of the keys differs.
22917 */
22918 break;
22919 }
22920 }
22921 if (res == 1) {
22922 /*
22923 * Equal key-sequence found.
22924 */
22925 goto next_target;
22926 }
22927 }
22928 j++;
22929 } while (j < nbDupls);
22930 }
22931 if (nbNodeTable) {
22932 j = 0;
22933 do {
22934 if (nbFields == 1) {
22935 res = xmlSchemaAreValuesEqual(keys[0]->val,
22936 bind->nodeTable[j]->keys[0]->val);
22937 if (res == -1)
22938 goto internal_error;
22939 if (res == 0) {
22940 /*
22941 * The key-sequence differs.
22942 */
22943 goto next_node_table_entry;
22944 }
22945 } else {
22946 res = 0;
22947 ntkeys = bind->nodeTable[j]->keys;
22948 for (k = 0; k < nbFields; k++) {
22949 res = xmlSchemaAreValuesEqual(keys[k]->val,
22950 ntkeys[k]->val);
22951 if (res == -1)
22952 goto internal_error;
22953 if (res == 0) {
22954 /*
22955 * One of the keys differs.
22956 */
22957 goto next_node_table_entry;
22958 }
22959 }
22960 }
22961 /*
22962 * Add the duplicate to the list of duplicates.
22963 */
22964 if (bind->dupls == NULL) {
22965 bind->dupls = xmlSchemaItemListCreate();
22966 if (bind->dupls == NULL)
22967 goto internal_error;
22968 }
22969 if (xmlSchemaItemListAdd(bind->dupls, bind->nodeTable[j]) == -1)
22970 goto internal_error;
22971 /*
22972 * Remove the duplicate entry from the IDC node-table.
22973 */
22974 bind->nodeTable[j] = bind->nodeTable[bind->nbNodes -1];
22975 bind->nbNodes--;
22976
22977 goto next_target;
22978
22979next_node_table_entry:
22980 j++;
22981 } while (j < nbNodeTable);
22982 }
22983 /*
22984 * If everything is fine, then add the IDC target-node to
22985 * the IDC node-table.
22986 */
22987 if (xmlSchemaIDCAppendNodeTableItem(bind, targets[i]) == -1)
22988 goto internal_error;
22989
22990next_target:
22991 i++;
22992 } while (i < nbTargets);
22993 }
22994 matcher = matcher->next;
22995 }
22996 return(0);
22997
22998internal_error:
22999 return(-1);
23000}
23001
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023002/**
23003 * xmlSchemaBubbleIDCNodeTables:
23004 * @depth: the current tree depth
23005 *
23006 * Merges IDC bindings of an element at @depth into the corresponding IDC
23007 * bindings of its parent element. If a duplicate note-table entry is found,
23008 * both, the parent node-table entry and child entry are discarded from the
23009 * node-table of the parent.
23010 *
23011 * Returns 0 if OK and -1 on internal errors.
23012 */
23013static int
23014xmlSchemaBubbleIDCNodeTables(xmlSchemaValidCtxtPtr vctxt)
23015{
23016 xmlSchemaPSVIIDCBindingPtr bind; /* IDC bindings of the current node. */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023017 xmlSchemaPSVIIDCBindingPtr *parTable, parBind = NULL; /* parent IDC bindings. */
23018 xmlSchemaPSVIIDCNodePtr node, parNode = NULL, *dupls, *parNodes; /* node-table entries. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023019 xmlSchemaIDCAugPtr aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023020 int i, j, k, ret = 0, nbFields, oldNum, oldDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023021
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023022 bind = vctxt->inode->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023023 if (bind == NULL) {
23024 /* Fine, no table, no bubbles. */
23025 return (0);
23026 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000023027
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023028 parTable = &(vctxt->elemInfos[vctxt->depth -1]->idcTable);
23029 /*
23030 * Walk all bindings; create new or add to existing bindings.
23031 * Remove duplicate key-sequences.
23032 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023033 while (bind != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023034
23035 if ((bind->nbNodes == 0) && WXS_ILIST_IS_EMPTY(bind->dupls))
23036 goto next_binding;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023037 /*
23038 * Check if the key/unique IDC table needs to be bubbled.
23039 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023040 if (! vctxt->createIDCNodeTables) {
23041 aidc = vctxt->aidcs;
23042 do {
23043 if (aidc->def == bind->definition) {
23044 if ((aidc->keyrefDepth == -1) ||
23045 (aidc->keyrefDepth >= vctxt->depth)) {
23046 goto next_binding;
23047 }
23048 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023049 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023050 aidc = aidc->next;
23051 } while (aidc != NULL);
23052 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023053
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023054 if (parTable != NULL)
23055 parBind = *parTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023056 /*
23057 * Search a matching parent binding for the
23058 * IDC definition.
23059 */
23060 while (parBind != NULL) {
23061 if (parBind->definition == bind->definition)
23062 break;
23063 parBind = parBind->next;
23064 }
23065
23066 if (parBind != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023067 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023068 * Compare every node-table entry of the child node,
23069 * i.e. the key-sequence within, ...
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023070 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023071 oldNum = parBind->nbNodes; /* Skip newly added items. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023072
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023073 if (! WXS_ILIST_IS_EMPTY(parBind->dupls)) {
23074 oldDupls = parBind->dupls->nbItems;
23075 dupls = (xmlSchemaPSVIIDCNodePtr *) parBind->dupls->items;
23076 } else {
23077 dupls = NULL;
23078 oldDupls = 0;
23079 }
23080
23081 parNodes = parBind->nodeTable;
23082 nbFields = bind->definition->nbFields;
23083
23084 for (i = 0; i < bind->nbNodes; i++) {
23085 node = bind->nodeTable[i];
23086 if (node == NULL)
23087 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023088 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023089 * ...with every key-sequence of the parent node, already
23090 * evaluated to be a duplicate key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023091 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023092 if (oldDupls) {
23093 j = 0;
23094 while (j < oldDupls) {
23095 if (nbFields == 1) {
23096 ret = xmlSchemaAreValuesEqual(
23097 node->keys[0]->val,
23098 dupls[j]->keys[0]->val);
23099 if (ret == -1)
23100 goto internal_error;
23101 if (ret == 0) {
23102 j++;
23103 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023104 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023105 } else {
23106 parNode = dupls[j];
23107 for (k = 0; k < nbFields; k++) {
23108 ret = xmlSchemaAreValuesEqual(
23109 node->keys[k]->val,
23110 parNode->keys[k]->val);
23111 if (ret == -1)
23112 goto internal_error;
23113 if (ret == 0)
23114 break;
23115 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023116 }
23117 if (ret == 1)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023118 /* Duplicate found. */
23119 break;
23120 j++;
23121 }
23122 if (j != oldDupls) {
23123 /* Duplicate found. Skip this entry. */
23124 continue;
23125 }
23126 }
23127 /*
23128 * ... and with every key-sequence of the parent node.
23129 */
23130 if (oldNum) {
23131 j = 0;
23132 while (j < oldNum) {
23133 if (nbFields == 1) {
23134 ret = xmlSchemaAreValuesEqual(
23135 node->keys[0]->val,
23136 parNodes[j]->keys[0]->val);
23137 if (ret == -1)
23138 goto internal_error;
23139 if (ret == 0) {
23140 j++;
23141 continue;
23142 }
23143 } else {
23144 parNode = parNodes[j];
23145 for (k = 0; k < nbFields; k++) {
23146 ret = xmlSchemaAreValuesEqual(
23147 node->keys[k]->val,
23148 parNode->keys[k]->val);
23149 if (ret == -1)
23150 goto internal_error;
23151 if (ret == 0)
23152 break;
23153 }
23154 }
23155 if (ret == 1)
23156 /* Duplicate found. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023157 break;
23158 j++;
23159 }
23160 if (j != oldNum) {
23161 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023162 * Handle duplicates. Move the duplicate in
23163 * the parent's node-table to the list of
23164 * duplicates.
23165 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023166 oldNum--;
23167 parBind->nbNodes--;
23168 /*
23169 * Move last old item to pos of duplicate.
23170 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023171 parNodes[j] = parNodes[oldNum];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023172
23173 if (parBind->nbNodes != oldNum) {
23174 /*
23175 * If new items exist, move last new item to
23176 * last of old items.
23177 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023178 parNodes[oldNum] =
23179 parNodes[parBind->nbNodes];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023180 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023181 if (parBind->dupls == NULL) {
23182 parBind->dupls = xmlSchemaItemListCreate();
23183 if (parBind->dupls == NULL)
23184 goto internal_error;
23185 }
23186 xmlSchemaItemListAdd(parBind->dupls, parNode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023187 } else {
23188 /*
23189 * Add the node-table entry (node and key-sequence) of
23190 * the child node to the node table of the parent node.
23191 */
23192 if (parBind->nodeTable == NULL) {
23193 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023194 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023195 if (parBind->nodeTable == NULL) {
23196 xmlSchemaVErrMemory(NULL,
23197 "allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023198 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023199 }
23200 parBind->sizeNodes = 1;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023201 } else if (parBind->nbNodes >= parBind->sizeNodes) {
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023202 parBind->sizeNodes *= 2;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023203 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23204 xmlRealloc(parBind->nodeTable, parBind->sizeNodes *
23205 sizeof(xmlSchemaPSVIIDCNodePtr));
23206 if (parBind->nodeTable == NULL) {
23207 xmlSchemaVErrMemory(NULL,
23208 "re-allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023209 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023210 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023211 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023212 /*
23213 * Append the new node-table entry to the 'new node-table
23214 * entries' section.
23215 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023216 parBind->nodeTable[parBind->nbNodes++] = node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023217 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023218
23219 }
23220
23221 }
23222 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023223 /*
23224 * No binding for the IDC was found: create a new one and
23225 * copy all node-tables.
23226 */
23227 parBind = xmlSchemaIDCNewBinding(bind->definition);
23228 if (parBind == NULL)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023229 goto internal_error;
23230
23231 /*
23232 * TODO: Hmm, how to optimize the initial number of
23233 * allocated entries?
23234 */
23235 if (bind->nbNodes != 0) {
23236 /*
23237 * Add all IDC node-table entries.
23238 */
23239 if (! vctxt->psviExposeIDCNodeTables) {
23240 /*
23241 * Just move the entries.
23242 * NOTE: this is quite save here, since
23243 * all the keyref lookups have already been
23244 * performed.
23245 */
23246 parBind->nodeTable = bind->nodeTable;
23247 bind->nodeTable = NULL;
23248 parBind->sizeNodes = bind->sizeNodes;
23249 bind->sizeNodes = 0;
23250 parBind->nbNodes = bind->nbNodes;
23251 bind->nbNodes = 0;
23252 } else {
23253 /*
23254 * Copy the entries.
23255 */
23256 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23257 xmlMalloc(bind->nbNodes *
23258 sizeof(xmlSchemaPSVIIDCNodePtr));
23259 if (parBind->nodeTable == NULL) {
23260 xmlSchemaVErrMemory(NULL,
23261 "allocating an array of IDC node-table "
23262 "items", NULL);
23263 xmlSchemaIDCFreeBinding(parBind);
23264 goto internal_error;
23265 }
23266 parBind->sizeNodes = bind->nbNodes;
23267 parBind->nbNodes = bind->nbNodes;
23268 memcpy(parBind->nodeTable, bind->nodeTable,
23269 bind->nbNodes * sizeof(xmlSchemaPSVIIDCNodePtr));
23270 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023271 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023272 if (bind->dupls) {
23273 /*
23274 * Move the duplicates.
23275 */
23276 if (parBind->dupls != NULL)
23277 xmlSchemaItemListFree(parBind->dupls);
23278 parBind->dupls = bind->dupls;
23279 bind->dupls = NULL;
23280 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023281 if (*parTable == NULL)
23282 *parTable = parBind;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023283 else {
23284 parBind->next = *parTable;
23285 *parTable = parBind;
23286 }
23287 }
23288
23289next_binding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023290 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023291 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023292 return (0);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023293
23294internal_error:
23295 return(-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023296}
23297
23298/**
23299 * xmlSchemaCheckCVCIDCKeyRef:
23300 * @vctxt: the WXS validation context
23301 * @elemDecl: the element declaration
23302 *
23303 * Check the cvc-idc-keyref constraints.
23304 */
23305static int
23306xmlSchemaCheckCVCIDCKeyRef(xmlSchemaValidCtxtPtr vctxt)
23307{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023308 xmlSchemaIDCMatcherPtr matcher;
23309 xmlSchemaPSVIIDCBindingPtr bind;
23310
23311 matcher = vctxt->inode->idcMatchers;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023312 /*
23313 * Find a keyref.
23314 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023315 while (matcher != NULL) {
23316 if ((matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) &&
23317 matcher->targets &&
23318 matcher->targets->nbItems)
23319 {
23320 int i, j, k, res, nbFields, hasDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023321 xmlSchemaPSVIIDCKeyPtr *refKeys, *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023322 xmlSchemaPSVIIDCNodePtr refNode = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023323
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023324 nbFields = matcher->aidc->def->nbFields;
23325
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023326 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023327 * Find the IDC node-table for the referenced IDC key/unique.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023328 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023329 bind = vctxt->inode->idcTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023330 while (bind != NULL) {
23331 if ((xmlSchemaIDCPtr) matcher->aidc->def->ref->item ==
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023332 bind->definition)
23333 break;
23334 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023335 }
23336 hasDupls = (bind && bind->dupls && bind->dupls->nbItems) ? 1 : 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023337 /*
23338 * Search for a matching key-sequences.
23339 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023340 for (i = 0; i < matcher->targets->nbItems; i++) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023341 res = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023342 refNode = matcher->targets->items[i];
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023343 if (bind != NULL) {
23344 refKeys = refNode->keys;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023345 for (j = 0; j < bind->nbNodes; j++) {
23346 keys = bind->nodeTable[j]->keys;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023347 for (k = 0; k < nbFields; k++) {
23348 res = xmlSchemaAreValuesEqual(keys[k]->val,
23349 refKeys[k]->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023350 if (res == 0)
23351 break;
23352 else if (res == -1) {
23353 return (-1);
23354 }
23355 }
23356 if (res == 1) {
23357 /*
23358 * Match found.
23359 */
23360 break;
23361 }
23362 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023363 if ((res == 0) && hasDupls) {
23364 /*
23365 * Search in duplicates
23366 */
23367 for (j = 0; j < bind->dupls->nbItems; j++) {
23368 keys = ((xmlSchemaPSVIIDCNodePtr)
23369 bind->dupls->items[j])->keys;
23370 for (k = 0; k < nbFields; k++) {
23371 res = xmlSchemaAreValuesEqual(keys[k]->val,
23372 refKeys[k]->val);
23373 if (res == 0)
23374 break;
23375 else if (res == -1) {
23376 return (-1);
23377 }
23378 }
23379 if (res == 1) {
23380 /*
23381 * Match in duplicates found.
23382 */
23383 xmlChar *str = NULL, *strB = NULL;
23384 xmlSchemaKeyrefErr(vctxt,
23385 XML_SCHEMAV_CVC_IDC, refNode,
23386 (xmlSchemaTypePtr) matcher->aidc->def,
23387 "More than one match found for "
23388 "key-sequence %s of keyref '%s'",
23389 xmlSchemaFormatIDCKeySequence(vctxt, &str,
23390 refNode->keys, nbFields),
23391 xmlSchemaGetComponentQName(&strB,
23392 matcher->aidc->def));
23393 FREE_AND_NULL(str);
23394 FREE_AND_NULL(strB);
23395 break;
23396 }
23397 }
23398 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023399 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023400
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023401 if (res == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023402 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023403 xmlSchemaKeyrefErr(vctxt,
23404 XML_SCHEMAV_CVC_IDC, refNode,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023405 (xmlSchemaTypePtr) matcher->aidc->def,
23406 "No match found for key-sequence %s of keyref '%s'",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000023407 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023408 refNode->keys, nbFields),
23409 xmlSchemaGetComponentQName(&strB, matcher->aidc->def));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023410 FREE_AND_NULL(str);
23411 FREE_AND_NULL(strB);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023412 }
23413 }
23414 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023415 matcher = matcher->next;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023416 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023417 /* TODO: Return an error if any error encountered. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023418 return (0);
23419}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023420
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023421/************************************************************************
23422 * *
23423 * XML Reader validation code *
23424 * *
23425 ************************************************************************/
23426
23427static xmlSchemaAttrInfoPtr
23428xmlSchemaGetFreshAttrInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023429{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023430 xmlSchemaAttrInfoPtr iattr;
23431 /*
23432 * Grow/create list of attribute infos.
23433 */
23434 if (vctxt->attrInfos == NULL) {
23435 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23436 xmlMalloc(sizeof(xmlSchemaAttrInfoPtr));
23437 vctxt->sizeAttrInfos = 1;
23438 if (vctxt->attrInfos == NULL) {
23439 xmlSchemaVErrMemory(vctxt,
23440 "allocating attribute info list", NULL);
23441 return (NULL);
23442 }
23443 } else if (vctxt->sizeAttrInfos <= vctxt->nbAttrInfos) {
23444 vctxt->sizeAttrInfos++;
23445 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23446 xmlRealloc(vctxt->attrInfos,
23447 vctxt->sizeAttrInfos * sizeof(xmlSchemaAttrInfoPtr));
23448 if (vctxt->attrInfos == NULL) {
23449 xmlSchemaVErrMemory(vctxt,
23450 "re-allocating attribute info list", NULL);
23451 return (NULL);
23452 }
23453 } else {
23454 iattr = vctxt->attrInfos[vctxt->nbAttrInfos++];
23455 if (iattr->localName != NULL) {
23456 VERROR_INT("xmlSchemaGetFreshAttrInfo",
23457 "attr info not cleared");
23458 return (NULL);
23459 }
23460 iattr->nodeType = XML_ATTRIBUTE_NODE;
23461 return (iattr);
23462 }
23463 /*
23464 * Create an attribute info.
23465 */
23466 iattr = (xmlSchemaAttrInfoPtr)
23467 xmlMalloc(sizeof(xmlSchemaAttrInfo));
23468 if (iattr == NULL) {
23469 xmlSchemaVErrMemory(vctxt, "creating new attribute info", NULL);
23470 return (NULL);
23471 }
23472 memset(iattr, 0, sizeof(xmlSchemaAttrInfo));
23473 iattr->nodeType = XML_ATTRIBUTE_NODE;
23474 vctxt->attrInfos[vctxt->nbAttrInfos++] = iattr;
23475
23476 return (iattr);
23477}
23478
23479static int
23480xmlSchemaValidatorPushAttribute(xmlSchemaValidCtxtPtr vctxt,
23481 xmlNodePtr attrNode,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023482 int nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023483 const xmlChar *localName,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023484 const xmlChar *nsName,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023485 int ownedNames,
23486 xmlChar *value,
23487 int ownedValue)
23488{
23489 xmlSchemaAttrInfoPtr attr;
23490
23491 attr = xmlSchemaGetFreshAttrInfo(vctxt);
23492 if (attr == NULL) {
23493 VERROR_INT("xmlSchemaPushAttribute",
23494 "calling xmlSchemaGetFreshAttrInfo()");
23495 return (-1);
23496 }
23497 attr->node = attrNode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023498 attr->nodeLine = nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023499 attr->state = XML_SCHEMAS_ATTR_UNKNOWN;
23500 attr->localName = localName;
23501 attr->nsName = nsName;
23502 if (ownedNames)
23503 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
23504 /*
23505 * Evaluate if it's an XSI attribute.
23506 */
23507 if (nsName != NULL) {
23508 if (xmlStrEqual(localName, BAD_CAST "nil")) {
23509 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23510 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NIL;
23511 }
23512 } else if (xmlStrEqual(localName, BAD_CAST "type")) {
23513 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23514 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_TYPE;
23515 }
23516 } else if (xmlStrEqual(localName, BAD_CAST "schemaLocation")) {
23517 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23518 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC;
23519 }
23520 } else if (xmlStrEqual(localName, BAD_CAST "noNamespaceSchemaLocation")) {
23521 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23522 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC;
23523 }
23524 } else if (xmlStrEqual(attr->nsName, xmlNamespaceNs)) {
23525 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XMLNS;
23526 }
23527 }
23528 attr->value = value;
23529 if (ownedValue)
23530 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
23531 if (attr->metaType != 0)
23532 attr->state = XML_SCHEMAS_ATTR_META;
23533 return (0);
23534}
23535
23536static void
23537xmlSchemaClearElemInfo(xmlSchemaNodeInfoPtr ielem)
23538{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023539 ielem->hasKeyrefs = 0;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000023540 ielem->appliedXPath = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023541 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
23542 FREE_AND_NULL(ielem->localName);
23543 FREE_AND_NULL(ielem->nsName);
23544 } else {
23545 ielem->localName = NULL;
23546 ielem->nsName = NULL;
23547 }
23548 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
23549 FREE_AND_NULL(ielem->value);
23550 } else {
23551 ielem->value = NULL;
23552 }
23553 if (ielem->val != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023554 /*
23555 * PSVI TODO: Be careful not to free it when the value is
23556 * exposed via PSVI.
23557 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023558 xmlSchemaFreeValue(ielem->val);
23559 ielem->val = NULL;
23560 }
23561 if (ielem->idcMatchers != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023562 /*
23563 * URGENT OPTIMIZE TODO: Use a pool of IDC matchers.
23564 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023565 xmlSchemaIDCFreeMatcherList(ielem->idcMatchers);
23566 ielem->idcMatchers = NULL;
23567 }
23568 if (ielem->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023569 /*
23570 * OPTIMIZE TODO: Use a pool of IDC tables??.
23571 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023572 xmlSchemaIDCFreeIDCTable(ielem->idcTable);
23573 ielem->idcTable = NULL;
23574 }
23575 if (ielem->regexCtxt != NULL) {
23576 xmlRegFreeExecCtxt(ielem->regexCtxt);
23577 ielem->regexCtxt = NULL;
23578 }
23579 if (ielem->nsBindings != NULL) {
23580 xmlFree((xmlChar **)ielem->nsBindings);
23581 ielem->nsBindings = NULL;
23582 ielem->nbNsBindings = 0;
23583 ielem->sizeNsBindings = 0;
23584 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023585}
23586
23587/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023588 * xmlSchemaGetFreshElemInfo:
23589 * @vctxt: the schema validation context
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023590 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023591 * Creates/reuses and initializes the element info item for
23592 * the currect tree depth.
23593 *
23594 * Returns the element info item or NULL on API or internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023595 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023596static xmlSchemaNodeInfoPtr
23597xmlSchemaGetFreshElemInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023598{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023599 xmlSchemaNodeInfoPtr info = NULL;
23600
23601 if (vctxt->depth > vctxt->sizeElemInfos) {
23602 VERROR_INT("xmlSchemaGetFreshElemInfo",
23603 "inconsistent depth encountered");
23604 return (NULL);
23605 }
23606 if (vctxt->elemInfos == NULL) {
23607 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23608 xmlMalloc(10 * sizeof(xmlSchemaNodeInfoPtr));
23609 if (vctxt->elemInfos == NULL) {
23610 xmlSchemaVErrMemory(vctxt,
23611 "allocating the element info array", NULL);
23612 return (NULL);
23613 }
23614 memset(vctxt->elemInfos, 0, 10 * sizeof(xmlSchemaNodeInfoPtr));
23615 vctxt->sizeElemInfos = 10;
23616 } else if (vctxt->sizeElemInfos <= vctxt->depth) {
23617 int i = vctxt->sizeElemInfos;
23618
23619 vctxt->sizeElemInfos *= 2;
23620 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23621 xmlRealloc(vctxt->elemInfos, vctxt->sizeElemInfos *
23622 sizeof(xmlSchemaNodeInfoPtr));
23623 if (vctxt->elemInfos == NULL) {
23624 xmlSchemaVErrMemory(vctxt,
23625 "re-allocating the element info array", NULL);
23626 return (NULL);
23627 }
23628 /*
23629 * We need the new memory to be NULLed.
23630 * TODO: Use memset instead?
23631 */
23632 for (; i < vctxt->sizeElemInfos; i++)
23633 vctxt->elemInfos[i] = NULL;
23634 } else
23635 info = vctxt->elemInfos[vctxt->depth];
23636
23637 if (info == NULL) {
23638 info = (xmlSchemaNodeInfoPtr)
23639 xmlMalloc(sizeof(xmlSchemaNodeInfo));
23640 if (info == NULL) {
23641 xmlSchemaVErrMemory(vctxt,
23642 "allocating an element info", NULL);
23643 return (NULL);
23644 }
23645 vctxt->elemInfos[vctxt->depth] = info;
23646 } else {
23647 if (info->localName != NULL) {
23648 VERROR_INT("xmlSchemaGetFreshElemInfo",
23649 "elem info has not been cleared");
23650 return (NULL);
23651 }
23652 }
23653 memset(info, 0, sizeof(xmlSchemaNodeInfo));
23654 info->nodeType = XML_ELEMENT_NODE;
23655 info->depth = vctxt->depth;
23656
23657 return (info);
23658}
23659
23660#define ACTIVATE_ATTRIBUTE(item) vctxt->inode = (xmlSchemaNodeInfoPtr) item;
23661#define ACTIVATE_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth];
23662#define ACTIVATE_PARENT_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth -1];
23663
23664static int
23665xmlSchemaValidateFacets(xmlSchemaAbstractCtxtPtr actxt,
23666 xmlNodePtr node,
23667 xmlSchemaTypePtr type,
23668 xmlSchemaValType valType,
23669 const xmlChar * value,
23670 xmlSchemaValPtr val,
23671 unsigned long length,
23672 int fireErrors)
23673{
23674 int ret, error = 0;
23675
23676 xmlSchemaTypePtr tmpType;
23677 xmlSchemaFacetLinkPtr facetLink;
23678 xmlSchemaFacetPtr facet;
23679 unsigned long len = 0;
23680 xmlSchemaWhitespaceValueType ws;
23681
23682 /*
23683 * In Libxml2, derived built-in types have currently no explicit facets.
23684 */
23685 if (type->type == XML_SCHEMA_TYPE_BASIC)
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000023686 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023687
23688 /*
23689 * NOTE: Do not jump away, if the facetSet of the given type is
23690 * empty: until now, "pattern" and "enumeration" facets of the
23691 * *base types* need to be checked as well.
23692 */
23693 if (type->facetSet == NULL)
23694 goto pattern_and_enum;
23695
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023696 if (! WXS_IS_ATOMIC(type)) {
23697 if (WXS_IS_LIST(type))
23698 goto WXS_IS_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023699 else
23700 goto pattern_and_enum;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023701 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023702 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023703 * Whitespace handling is only of importance for string-based
23704 * types.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023705 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023706 tmpType = xmlSchemaGetPrimitiveType(type);
23707 if ((tmpType->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023708 WXS_IS_ANY_SIMPLE_TYPE(tmpType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023709 ws = xmlSchemaGetWhiteSpaceFacetValue(type);
23710 } else
23711 ws = XML_SCHEMA_WHITESPACE_COLLAPSE;
23712 /*
23713 * If the value was not computed (for string or
23714 * anySimpleType based types), then use the provided
23715 * type.
23716 */
23717 if (val == NULL)
23718 valType = valType;
23719 else
23720 valType = xmlSchemaGetValType(val);
23721
23722 ret = 0;
23723 for (facetLink = type->facetSet; facetLink != NULL;
23724 facetLink = facetLink->next) {
23725 /*
23726 * Skip the pattern "whiteSpace": it is used to
23727 * format the character content beforehand.
23728 */
23729 switch (facetLink->facet->type) {
23730 case XML_SCHEMA_FACET_WHITESPACE:
23731 case XML_SCHEMA_FACET_PATTERN:
23732 case XML_SCHEMA_FACET_ENUMERATION:
23733 continue;
23734 case XML_SCHEMA_FACET_LENGTH:
23735 case XML_SCHEMA_FACET_MINLENGTH:
23736 case XML_SCHEMA_FACET_MAXLENGTH:
23737 ret = xmlSchemaValidateLengthFacetWhtsp(facetLink->facet,
23738 valType, value, val, &len, ws);
23739 break;
23740 default:
23741 ret = xmlSchemaValidateFacetWhtsp(facetLink->facet, ws,
23742 valType, value, val, ws);
23743 break;
23744 }
23745 if (ret < 0) {
23746 AERROR_INT("xmlSchemaValidateFacets",
23747 "validating against a atomic type facet");
23748 return (-1);
23749 } else if (ret > 0) {
23750 if (fireErrors)
23751 xmlSchemaFacetErr(actxt, ret, node,
23752 value, len, type, facetLink->facet, NULL, NULL, NULL);
23753 else
23754 return (ret);
23755 if (error == 0)
23756 error = ret;
23757 }
23758 ret = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023759 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023760
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023761WXS_IS_LIST:
23762 if (! WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023763 goto pattern_and_enum;
23764 /*
23765 * "length", "minLength" and "maxLength" of list types.
23766 */
23767 ret = 0;
23768 for (facetLink = type->facetSet; facetLink != NULL;
23769 facetLink = facetLink->next) {
23770
23771 switch (facetLink->facet->type) {
23772 case XML_SCHEMA_FACET_LENGTH:
23773 case XML_SCHEMA_FACET_MINLENGTH:
23774 case XML_SCHEMA_FACET_MAXLENGTH:
23775 ret = xmlSchemaValidateListSimpleTypeFacet(facetLink->facet,
23776 value, length, NULL);
23777 break;
23778 default:
23779 continue;
23780 }
23781 if (ret < 0) {
23782 AERROR_INT("xmlSchemaValidateFacets",
23783 "validating against a list type facet");
23784 return (-1);
23785 } else if (ret > 0) {
23786 if (fireErrors)
23787 xmlSchemaFacetErr(actxt, ret, node,
23788 value, length, type, facetLink->facet, NULL, NULL, NULL);
23789 else
23790 return (ret);
23791 if (error == 0)
23792 error = ret;
23793 }
23794 ret = 0;
23795 }
23796
23797pattern_and_enum:
23798 if (error >= 0) {
23799 int found = 0;
23800 /*
23801 * Process enumerations. Facet values are in the value space
23802 * of the defining type's base type. This seems to be a bug in the
23803 * XML Schema 1.0 spec. Use the whitespace type of the base type.
23804 * Only the first set of enumerations in the ancestor-or-self axis
23805 * is used for validation.
23806 */
23807 ret = 0;
23808 tmpType = type;
23809 do {
23810 for (facet = tmpType->facets; facet != NULL; facet = facet->next) {
23811 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
23812 continue;
23813 found = 1;
23814 ret = xmlSchemaAreValuesEqual(facet->val, val);
23815 if (ret == 1)
23816 break;
23817 else if (ret < 0) {
23818 AERROR_INT("xmlSchemaValidateFacets",
23819 "validating against an enumeration facet");
23820 return (-1);
23821 }
23822 }
23823 if (ret != 0)
23824 break;
23825 tmpType = tmpType->baseType;
23826 } while ((tmpType != NULL) &&
23827 (tmpType->type != XML_SCHEMA_TYPE_BASIC));
23828 if (found && (ret == 0)) {
23829 ret = XML_SCHEMAV_CVC_ENUMERATION_VALID;
23830 if (fireErrors) {
23831 xmlSchemaFacetErr(actxt, ret, node,
23832 value, 0, type, NULL, NULL, NULL, NULL);
23833 } else
23834 return (ret);
23835 if (error == 0)
23836 error = ret;
23837 }
23838 }
23839
23840 if (error >= 0) {
23841 int found;
23842 /*
23843 * Process patters. Pattern facets are ORed at type level
23844 * and ANDed if derived. Walk the base type axis.
23845 */
23846 tmpType = type;
23847 facet = NULL;
23848 do {
23849 found = 0;
23850 for (facetLink = tmpType->facetSet; facetLink != NULL;
23851 facetLink = facetLink->next) {
23852 if (facetLink->facet->type != XML_SCHEMA_FACET_PATTERN)
23853 continue;
23854 found = 1;
23855 /*
23856 * NOTE that for patterns, @value needs to be the
23857 * normalized vaule.
23858 */
23859 ret = xmlRegexpExec(facetLink->facet->regexp, value);
23860 if (ret == 1)
23861 break;
23862 else if (ret < 0) {
23863 AERROR_INT("xmlSchemaValidateFacets",
23864 "validating against a pattern facet");
23865 return (-1);
23866 } else {
23867 /*
23868 * Save the last non-validating facet.
23869 */
23870 facet = facetLink->facet;
23871 }
23872 }
23873 if (found && (ret != 1)) {
23874 ret = XML_SCHEMAV_CVC_PATTERN_VALID;
23875 if (fireErrors) {
23876 xmlSchemaFacetErr(actxt, ret, node,
23877 value, 0, type, facet, NULL, NULL, NULL);
23878 } else
23879 return (ret);
23880 if (error == 0)
23881 error = ret;
23882 break;
23883 }
23884 tmpType = tmpType->baseType;
23885 } while ((tmpType != NULL) && (tmpType->type != XML_SCHEMA_TYPE_BASIC));
23886 }
23887
23888 return (error);
23889}
23890
23891static xmlChar *
23892xmlSchemaNormalizeValue(xmlSchemaTypePtr type,
23893 const xmlChar *value)
23894{
23895 switch (xmlSchemaGetWhiteSpaceFacetValue(type)) {
23896 case XML_SCHEMA_WHITESPACE_COLLAPSE:
23897 return (xmlSchemaCollapseString(value));
23898 case XML_SCHEMA_WHITESPACE_REPLACE:
23899 return (xmlSchemaWhiteSpaceReplace(value));
23900 default:
23901 return (NULL);
23902 }
23903}
23904
23905static int
23906xmlSchemaValidateQName(xmlSchemaValidCtxtPtr vctxt,
23907 const xmlChar *value,
23908 xmlSchemaValPtr *val,
23909 int valNeeded)
23910{
23911 int ret;
23912 const xmlChar *nsName;
23913 xmlChar *local, *prefix = NULL;
23914
23915 ret = xmlValidateQName(value, 1);
23916 if (ret != 0) {
23917 if (ret == -1) {
23918 VERROR_INT("xmlSchemaValidateQName",
23919 "calling xmlValidateQName()");
23920 return (-1);
23921 }
23922 return( XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1);
23923 }
23924 /*
23925 * NOTE: xmlSplitQName2 will always return a duplicated
23926 * strings.
23927 */
23928 local = xmlSplitQName2(value, &prefix);
23929 if (local == NULL)
23930 local = xmlStrdup(value);
23931 /*
23932 * OPTIMIZE TODO: Use flags for:
23933 * - is there any namespace binding?
23934 * - is there a default namespace?
23935 */
23936 nsName = xmlSchemaLookupNamespace(vctxt, prefix);
23937
23938 if (prefix != NULL) {
23939 xmlFree(prefix);
23940 /*
23941 * A namespace must be found if the prefix is
23942 * NOT NULL.
23943 */
23944 if (nsName == NULL) {
23945 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023946 xmlSchemaCustomErr(ACTXT_CAST vctxt, ret, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023947 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023948 "The QName value '%s' has no "
23949 "corresponding namespace declaration in "
23950 "scope", value, NULL);
23951 if (local != NULL)
23952 xmlFree(local);
23953 return (ret);
23954 }
23955 }
23956 if (valNeeded && val) {
23957 if (nsName != NULL)
23958 *val = xmlSchemaNewQNameValue(
23959 BAD_CAST xmlStrdup(nsName), BAD_CAST local);
23960 else
23961 *val = xmlSchemaNewQNameValue(NULL,
23962 BAD_CAST local);
23963 } else
23964 xmlFree(local);
23965 return (0);
23966}
23967
23968/*
23969* cvc-simple-type
23970*/
23971static int
23972xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
23973 xmlNodePtr node,
23974 xmlSchemaTypePtr type,
23975 const xmlChar *value,
23976 xmlSchemaValPtr *retVal,
23977 int fireErrors,
23978 int normalize,
23979 int isNormalized)
23980{
23981 int ret = 0, valNeeded = (retVal) ? 1 : 0;
23982 xmlSchemaValPtr val = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023983 /* xmlSchemaWhitespaceValueType ws; */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023984 xmlChar *normValue = NULL;
23985
23986#define NORMALIZE(atype) \
23987 if ((! isNormalized) && \
23988 (normalize || (type->flags & XML_SCHEMAS_TYPE_NORMVALUENEEDED))) { \
23989 normValue = xmlSchemaNormalizeValue(atype, value); \
23990 if (normValue != NULL) \
23991 value = normValue; \
23992 isNormalized = 1; \
23993 }
23994
23995 if ((retVal != NULL) && (*retVal != NULL)) {
23996 xmlSchemaFreeValue(*retVal);
23997 *retVal = NULL;
23998 }
23999 /*
24000 * 3.14.4 Simple Type Definition Validation Rules
24001 * Validation Rule: String Valid
24002 */
24003 /*
24004 * 1 It is schema-valid with respect to that definition as defined
24005 * by Datatype Valid in [XML Schemas: Datatypes].
24006 */
24007 /*
24008 * 2.1 If The definition is ENTITY or is validly derived from ENTITY given
24009 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6), then
24010 * the string must be a ·declared entity name·.
24011 */
24012 /*
24013 * 2.2 If The definition is ENTITIES or is validly derived from ENTITIES
24014 * given the empty set, as defined in Type Derivation OK (Simple) (§3.14.6),
24015 * then every whitespace-delimited substring of the string must be a ·declared
24016 * entity name·.
24017 */
24018 /*
24019 * 2.3 otherwise no further condition applies.
24020 */
24021 if ((! valNeeded) && (type->flags & XML_SCHEMAS_TYPE_FACETSNEEDVALUE))
24022 valNeeded = 1;
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000024023 if (value == NULL)
24024 value = BAD_CAST "";
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024025 if (WXS_IS_ANY_SIMPLE_TYPE(type) || WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024026 xmlSchemaTypePtr biType; /* The built-in type. */
24027 /*
24028 * SPEC (1.2.1) "if {variety} is ·atomic· then the string must ·match·
24029 * a literal in the ·lexical space· of {base type definition}"
24030 */
24031 /*
24032 * Whitespace-normalize.
24033 */
24034 NORMALIZE(type);
24035 if (type->type != XML_SCHEMA_TYPE_BASIC) {
24036 /*
24037 * Get the built-in type.
24038 */
24039 biType = type->baseType;
24040 while ((biType != NULL) &&
24041 (biType->type != XML_SCHEMA_TYPE_BASIC))
24042 biType = biType->baseType;
24043
24044 if (biType == NULL) {
24045 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24046 "could not get the built-in type");
24047 goto internal_error;
24048 }
24049 } else
24050 biType = type;
24051 /*
24052 * NOTATIONs need to be processed here, since they need
24053 * to lookup in the hashtable of NOTATION declarations of the schema.
24054 */
24055 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
24056 switch (biType->builtInType) {
24057 case XML_SCHEMAS_NOTATION:
24058 ret = xmlSchemaValidateNotation(
24059 (xmlSchemaValidCtxtPtr) actxt,
24060 ((xmlSchemaValidCtxtPtr) actxt)->schema,
24061 NULL, value, &val, valNeeded);
24062 break;
24063 case XML_SCHEMAS_QNAME:
24064 ret = xmlSchemaValidateQName((xmlSchemaValidCtxtPtr) actxt,
24065 value, &val, valNeeded);
24066 break;
24067 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024068 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024069 if (valNeeded)
24070 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24071 value, &val, NULL);
24072 else
24073 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24074 value, NULL, NULL);
24075 break;
24076 }
24077 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
24078 switch (biType->builtInType) {
24079 case XML_SCHEMAS_NOTATION:
24080 ret = xmlSchemaValidateNotation(NULL,
24081 ((xmlSchemaParserCtxtPtr) actxt)->schema, node,
24082 value, &val, valNeeded);
24083 break;
24084 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024085 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024086 if (valNeeded)
24087 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24088 value, &val, node);
24089 else
24090 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24091 value, NULL, node);
24092 break;
24093 }
24094 } else {
24095 /*
24096 * Validation via a public API is not implemented yet.
24097 */
24098 TODO
24099 goto internal_error;
24100 }
24101 if (ret != 0) {
24102 if (ret < 0) {
24103 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24104 "validating against a built-in type");
24105 goto internal_error;
24106 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024107 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024108 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24109 else
24110 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24111 }
24112 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24113 /*
24114 * Check facets.
24115 */
24116 ret = xmlSchemaValidateFacets(actxt, node, type,
24117 (xmlSchemaValType) biType->builtInType, value, val,
24118 0, fireErrors);
24119 if (ret != 0) {
24120 if (ret < 0) {
24121 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24122 "validating facets of atomic simple type");
24123 goto internal_error;
24124 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024125 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024126 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24127 else
24128 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24129 }
24130 }
24131 if (fireErrors && (ret > 0))
24132 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024133 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024134
24135 xmlSchemaTypePtr itemType;
24136 const xmlChar *cur, *end;
24137 xmlChar *tmpValue = NULL;
24138 unsigned long len = 0;
24139 xmlSchemaValPtr prevVal = NULL, curVal = NULL;
24140 /* 1.2.2 if {variety} is ·list· then the string must be a sequence
24141 * of white space separated tokens, each of which ·match·es a literal
24142 * in the ·lexical space· of {item type definition}
24143 */
24144 /*
24145 * Note that XML_SCHEMAS_TYPE_NORMVALUENEEDED will be set if
24146 * the list type has an enum or pattern facet.
24147 */
24148 NORMALIZE(type);
24149 /*
24150 * VAL TODO: Optimize validation of empty values.
24151 * VAL TODO: We do not have computed values for lists.
24152 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024153 itemType = WXS_LIST_ITEMTYPE(type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024154 cur = value;
24155 do {
24156 while (IS_BLANK_CH(*cur))
24157 cur++;
24158 end = cur;
24159 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
24160 end++;
24161 if (end == cur)
24162 break;
24163 tmpValue = xmlStrndup(cur, end - cur);
24164 len++;
24165
24166 if (valNeeded)
24167 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24168 tmpValue, &curVal, fireErrors, 0, 1);
24169 else
24170 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24171 tmpValue, NULL, fireErrors, 0, 1);
24172 FREE_AND_NULL(tmpValue);
24173 if (curVal != NULL) {
24174 /*
24175 * Add to list of computed values.
24176 */
24177 if (val == NULL)
24178 val = curVal;
24179 else
24180 xmlSchemaValueAppend(prevVal, curVal);
24181 prevVal = curVal;
24182 curVal = NULL;
24183 }
24184 if (ret != 0) {
24185 if (ret < 0) {
24186 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24187 "validating an item of list simple type");
24188 goto internal_error;
24189 }
24190 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24191 break;
24192 }
24193 cur = end;
24194 } while (*cur != 0);
24195 FREE_AND_NULL(tmpValue);
24196 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24197 /*
24198 * Apply facets (pattern, enumeration).
24199 */
24200 ret = xmlSchemaValidateFacets(actxt, node, type,
24201 XML_SCHEMAS_UNKNOWN, value, val,
24202 len, fireErrors);
24203 if (ret != 0) {
24204 if (ret < 0) {
24205 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24206 "validating facets of list simple type");
24207 goto internal_error;
24208 }
24209 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24210 }
24211 }
24212 if (fireErrors && (ret > 0)) {
24213 /*
24214 * Report the normalized value.
24215 */
24216 normalize = 1;
24217 NORMALIZE(type);
24218 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24219 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024220 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024221 xmlSchemaTypeLinkPtr memberLink;
24222 /*
24223 * TODO: For all datatypes ·derived· by ·union· whiteSpace does
24224 * not apply directly; however, the normalization behavior of ·union·
24225 * types is controlled by the value of whiteSpace on that one of the
24226 * ·memberTypes· against which the ·union· is successfully validated.
24227 *
24228 * This means that the value is normalized by the first validating
24229 * member type, then the facets of the union type are applied. This
24230 * needs changing of the value!
24231 */
24232
24233 /*
24234 * 1.2.3 if {variety} is ·union· then the string must ·match· a
24235 * literal in the ·lexical space· of at least one member of
24236 * {member type definitions}
24237 */
24238 memberLink = xmlSchemaGetUnionSimpleTypeMemberTypes(type);
24239 if (memberLink == NULL) {
24240 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24241 "union simple type has no member types");
24242 goto internal_error;
24243 }
24244 /*
24245 * Always normalize union type values, since we currently
24246 * cannot store the whitespace information with the value
24247 * itself; otherwise a later value-comparison would be
24248 * not possible.
24249 */
24250 while (memberLink != NULL) {
24251 if (valNeeded)
24252 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24253 memberLink->type, value, &val, 0, 1, 0);
24254 else
24255 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24256 memberLink->type, value, NULL, 0, 1, 0);
24257 if (ret <= 0)
24258 break;
24259 memberLink = memberLink->next;
24260 }
24261 if (ret != 0) {
24262 if (ret < 0) {
24263 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24264 "validating members of union simple type");
24265 goto internal_error;
24266 }
24267 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24268 }
24269 /*
24270 * Apply facets (pattern, enumeration).
24271 */
24272 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24273 /*
24274 * The normalization behavior of ·union· types is controlled by
24275 * the value of whiteSpace on that one of the ·memberTypes·
24276 * against which the ·union· is successfully validated.
24277 */
24278 NORMALIZE(memberLink->type);
24279 ret = xmlSchemaValidateFacets(actxt, node, type,
24280 XML_SCHEMAS_UNKNOWN, value, val,
24281 0, fireErrors);
24282 if (ret != 0) {
24283 if (ret < 0) {
24284 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24285 "validating facets of union simple type");
24286 goto internal_error;
24287 }
24288 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24289 }
24290 }
24291 if (fireErrors && (ret > 0))
24292 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24293 }
24294
24295 if (normValue != NULL)
24296 xmlFree(normValue);
24297 if (ret == 0) {
24298 if (retVal != NULL)
24299 *retVal = val;
24300 else if (val != NULL)
24301 xmlSchemaFreeValue(val);
24302 } else if (val != NULL)
24303 xmlSchemaFreeValue(val);
24304 return (ret);
24305internal_error:
24306 if (normValue != NULL)
24307 xmlFree(normValue);
24308 if (val != NULL)
24309 xmlSchemaFreeValue(val);
24310 return (-1);
24311}
24312
24313static int
24314xmlSchemaVExpandQName(xmlSchemaValidCtxtPtr vctxt,
24315 const xmlChar *value,
24316 const xmlChar **nsName,
24317 const xmlChar **localName)
24318{
24319 int ret = 0;
24320
24321 if ((nsName == NULL) || (localName == NULL))
24322 return (-1);
24323 *nsName = NULL;
24324 *localName = NULL;
24325
24326 ret = xmlValidateQName(value, 1);
24327 if (ret == -1)
24328 return (-1);
24329 if (ret > 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024330 xmlSchemaSimpleTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024331 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
24332 value, xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), 1);
24333 return (1);
24334 }
24335 {
24336 xmlChar *local = NULL;
24337 xmlChar *prefix;
24338
24339 /*
24340 * NOTE: xmlSplitQName2 will return a duplicated
24341 * string.
24342 */
24343 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024344 if (local == NULL)
24345 *localName = xmlDictLookup(vctxt->dict, value, -1);
24346 else {
24347 *localName = xmlDictLookup(vctxt->dict, local, -1);
24348 xmlFree(local);
24349 }
24350
24351 *nsName = xmlSchemaLookupNamespace(vctxt, prefix);
24352
24353 if (prefix != NULL) {
24354 xmlFree(prefix);
24355 /*
24356 * A namespace must be found if the prefix is NOT NULL.
24357 */
24358 if (*nsName == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024359 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024360 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024361 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024362 "The QName value '%s' has no "
24363 "corresponding namespace declaration in scope",
24364 value, NULL);
24365 return (2);
24366 }
24367 }
24368 }
24369 return (0);
24370}
24371
24372static int
24373xmlSchemaProcessXSIType(xmlSchemaValidCtxtPtr vctxt,
24374 xmlSchemaAttrInfoPtr iattr,
24375 xmlSchemaTypePtr *localType,
24376 xmlSchemaElementPtr elemDecl)
24377{
24378 int ret = 0;
24379 /*
24380 * cvc-elt (3.3.4) : (4)
24381 * AND
24382 * Schema-Validity Assessment (Element) (cvc-assess-elt)
24383 * (1.2.1.2.1) - (1.2.1.2.4)
24384 * Handle 'xsi:type'.
24385 */
24386 if (localType == NULL)
24387 return (-1);
24388 *localType = NULL;
24389 if (iattr == NULL)
24390 return (0);
24391 else {
24392 const xmlChar *nsName = NULL, *local = NULL;
24393 /*
24394 * TODO: We should report a *warning* that the type was overriden
24395 * by the instance.
24396 */
24397 ACTIVATE_ATTRIBUTE(iattr);
24398 /*
24399 * (cvc-elt) (3.3.4) : (4.1)
24400 * (cvc-assess-elt) (1.2.1.2.2)
24401 */
24402 ret = xmlSchemaVExpandQName(vctxt, iattr->value,
24403 &nsName, &local);
24404 if (ret != 0) {
24405 if (ret < 0) {
24406 VERROR_INT("xmlSchemaValidateElementByDeclaration",
24407 "calling xmlSchemaQNameExpand() to validate the "
24408 "attribute 'xsi:type'");
24409 goto internal_error;
24410 }
24411 goto exit;
24412 }
24413 /*
24414 * (cvc-elt) (3.3.4) : (4.2)
24415 * (cvc-assess-elt) (1.2.1.2.3)
24416 */
24417 *localType = xmlSchemaGetType(vctxt->schema, local, nsName);
24418 if (*localType == NULL) {
24419 xmlChar *str = NULL;
24420
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024421 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024422 XML_SCHEMAV_CVC_ELT_4_2, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024423 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024424 "The QName value '%s' of the xsi:type attribute does not "
24425 "resolve to a type definition",
24426 xmlSchemaFormatQName(&str, nsName, local), NULL);
24427 FREE_AND_NULL(str);
24428 ret = vctxt->err;
24429 goto exit;
24430 }
24431 if (elemDecl != NULL) {
24432 int set = 0;
24433
24434 /*
24435 * SPEC cvc-elt (3.3.4) : (4.3) (Type Derivation OK)
24436 * "The ·local type definition· must be validly
24437 * derived from the {type definition} given the union of
24438 * the {disallowed substitutions} and the {type definition}'s
24439 * {prohibited substitutions}, as defined in
24440 * Type Derivation OK (Complex) (§3.4.6)
24441 * (if it is a complex type definition),
24442 * or given {disallowed substitutions} as defined in Type
24443 * Derivation OK (Simple) (§3.14.6) (if it is a simple type
24444 * definition)."
24445 *
24446 * {disallowed substitutions}: the "block" on the element decl.
24447 * {prohibited substitutions}: the "block" on the type def.
24448 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024449 /*
24450 * OPTIMIZE TODO: We could map types already evaluated
24451 * to be validly derived from other types to avoid checking
24452 * this over and over for the same types.
24453 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024454 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) ||
24455 (elemDecl->subtypes->flags &
24456 XML_SCHEMAS_TYPE_BLOCK_EXTENSION))
24457 set |= SUBSET_EXTENSION;
24458
24459 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) ||
24460 (elemDecl->subtypes->flags &
24461 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION))
24462 set |= SUBSET_RESTRICTION;
24463
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024464 /*
24465 * REMOVED and CHANGED since this produced a parser context
24466 * which adds to the string dict of the schema. So this would
24467 * change the schema and we don't want this. We don't need
24468 * the parser context anymore.
24469 *
24470 * if ((vctxt->pctxt == NULL) &&
24471 * (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
24472 * return (-1);
24473 */
24474
24475 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST vctxt, *localType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024476 elemDecl->subtypes, set) != 0) {
24477 xmlChar *str = NULL;
24478
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024479 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024480 XML_SCHEMAV_CVC_ELT_4_3, NULL, NULL,
24481 "The type definition '%s', specified by xsi:type, is "
24482 "blocked or not validly derived from the type definition "
24483 "of the element declaration",
24484 xmlSchemaFormatQName(&str,
24485 (*localType)->targetNamespace,
24486 (*localType)->name),
24487 NULL);
24488 FREE_AND_NULL(str);
24489 ret = vctxt->err;
24490 *localType = NULL;
24491 }
24492 }
24493 }
24494exit:
24495 ACTIVATE_ELEM;
24496 return (ret);
24497internal_error:
24498 ACTIVATE_ELEM;
24499 return (-1);
24500}
24501
24502static int
24503xmlSchemaValidateElemDecl(xmlSchemaValidCtxtPtr vctxt)
24504{
24505 xmlSchemaElementPtr elemDecl = vctxt->inode->decl;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024506 xmlSchemaTypePtr actualType = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024507
24508 /*
24509 * cvc-elt (3.3.4) : 1
24510 */
24511 if (elemDecl == NULL) {
24512 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL,
24513 "No matching declaration available");
24514 return (vctxt->err);
24515 }
24516 /*
24517 * cvc-elt (3.3.4) : 2
24518 */
24519 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) {
24520 VERROR(XML_SCHEMAV_CVC_ELT_2, NULL,
24521 "The element declaration is abstract");
24522 return (vctxt->err);
24523 }
24524 if (actualType == NULL) {
24525 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24526 "The type definition is absent");
24527 return (XML_SCHEMAV_CVC_TYPE_1);
24528 }
24529 if (vctxt->nbAttrInfos != 0) {
24530 int ret;
24531 xmlSchemaAttrInfoPtr iattr;
24532 /*
24533 * cvc-elt (3.3.4) : 3
24534 * Handle 'xsi:nil'.
24535 */
24536 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24537 XML_SCHEMA_ATTR_INFO_META_XSI_NIL);
24538 if (iattr) {
24539 ACTIVATE_ATTRIBUTE(iattr);
24540 /*
24541 * Validate the value.
24542 */
24543 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024544 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024545 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
24546 iattr->value, &(iattr->val), 1, 0, 0);
24547 ACTIVATE_ELEM;
24548 if (ret < 0) {
24549 VERROR_INT("xmlSchemaValidateElemDecl",
24550 "calling xmlSchemaVCheckCVCSimpleType() to "
24551 "validate the attribute 'xsi:nil'");
24552 return (-1);
24553 }
24554 if (ret == 0) {
24555 if ((elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) {
24556 /*
24557 * cvc-elt (3.3.4) : 3.1
24558 */
24559 VERROR(XML_SCHEMAV_CVC_ELT_3_1, NULL,
24560 "The element is not 'nillable'");
24561 /* Does not return an error on purpose. */
24562 } else {
24563 if (xmlSchemaValueGetAsBoolean(iattr->val)) {
24564 /*
24565 * cvc-elt (3.3.4) : 3.2.2
24566 */
24567 if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) &&
24568 (elemDecl->value != NULL)) {
24569 VERROR(XML_SCHEMAV_CVC_ELT_3_2_2, NULL,
24570 "The element cannot be 'nilled' because "
24571 "there is a fixed value constraint defined "
24572 "for it");
24573 /* Does not return an error on purpose. */
24574 } else
24575 vctxt->inode->flags |=
24576 XML_SCHEMA_ELEM_INFO_NILLED;
24577 }
24578 }
24579 }
24580 }
24581 /*
24582 * cvc-elt (3.3.4) : 4
24583 * Handle 'xsi:type'.
24584 */
24585 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24586 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
24587 if (iattr) {
24588 xmlSchemaTypePtr localType = NULL;
24589
24590 ret = xmlSchemaProcessXSIType(vctxt, iattr, &localType,
24591 elemDecl);
24592 if (ret != 0) {
24593 if (ret == -1) {
24594 VERROR_INT("xmlSchemaValidateElemDecl",
24595 "calling xmlSchemaProcessXSIType() to "
24596 "process the attribute 'xsi:type'");
24597 return (-1);
24598 }
24599 /* Does not return an error on purpose. */
24600 }
24601 if (localType != NULL) {
24602 vctxt->inode->flags |= XML_SCHEMA_ELEM_INFO_LOCAL_TYPE;
24603 actualType = localType;
24604 }
24605 }
24606 }
24607 /*
24608 * IDC: Register identity-constraint XPath matchers.
24609 */
24610 if ((elemDecl->idcs != NULL) &&
24611 (xmlSchemaIDCRegisterMatchers(vctxt, elemDecl) == -1))
24612 return (-1);
24613 /*
24614 * No actual type definition.
24615 */
24616 if (actualType == NULL) {
24617 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24618 "The type definition is absent");
24619 return (XML_SCHEMAV_CVC_TYPE_1);
24620 }
24621 /*
24622 * Remember the actual type definition.
24623 */
24624 vctxt->inode->typeDef = actualType;
24625
24626 return (0);
24627}
24628
24629static int
24630xmlSchemaVAttributesSimple(xmlSchemaValidCtxtPtr vctxt)
24631{
24632 xmlSchemaAttrInfoPtr iattr;
24633 int ret = 0, i;
24634
24635 /*
24636 * SPEC cvc-type (3.1.1)
24637 * "The attributes of must be empty, excepting those whose namespace
24638 * name is identical to http://www.w3.org/2001/XMLSchema-instance and
24639 * whose local name is one of type, nil, schemaLocation or
24640 * noNamespaceSchemaLocation."
24641 */
24642 if (vctxt->nbAttrInfos == 0)
24643 return (0);
24644 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24645 iattr = vctxt->attrInfos[i];
24646 if (! iattr->metaType) {
24647 ACTIVATE_ATTRIBUTE(iattr)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024648 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024649 XML_SCHEMAV_CVC_TYPE_3_1_1, iattr, NULL);
24650 ret = XML_SCHEMAV_CVC_TYPE_3_1_1;
24651 }
24652 }
24653 ACTIVATE_ELEM
24654 return (ret);
24655}
24656
24657/*
24658* Cleanup currently used attribute infos.
24659*/
24660static void
24661xmlSchemaClearAttrInfos(xmlSchemaValidCtxtPtr vctxt)
24662{
24663 int i;
24664 xmlSchemaAttrInfoPtr attr;
24665
24666 if (vctxt->nbAttrInfos == 0)
24667 return;
24668 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24669 attr = vctxt->attrInfos[i];
24670 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
24671 if (attr->localName != NULL)
24672 xmlFree((xmlChar *) attr->localName);
24673 if (attr->nsName != NULL)
24674 xmlFree((xmlChar *) attr->nsName);
24675 }
24676 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
24677 if (attr->value != NULL)
24678 xmlFree((xmlChar *) attr->value);
24679 }
24680 if (attr->val != NULL) {
24681 xmlSchemaFreeValue(attr->val);
24682 attr->val = NULL;
24683 }
24684 memset(attr, 0, sizeof(xmlSchemaAttrInfo));
24685 }
24686 vctxt->nbAttrInfos = 0;
24687}
24688
24689/*
24690* 3.4.4 Complex Type Definition Validation Rules
24691* Element Locally Valid (Complex Type) (cvc-complex-type)
24692* 3.2.4 Attribute Declaration Validation Rules
24693* Validation Rule: Attribute Locally Valid (cvc-attribute)
24694* Attribute Locally Valid (Use) (cvc-au)
24695*
24696* Only "assessed" attribute information items will be visible to
24697* IDCs. I.e. not "lax" (without declaration) and "skip" wild attributes.
24698*/
24699static int
24700xmlSchemaVAttributesComplex(xmlSchemaValidCtxtPtr vctxt)
24701{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024702 xmlSchemaTypePtr type = vctxt->inode->typeDef;
24703 xmlSchemaItemListPtr attrUseList;
24704 xmlSchemaAttributeUsePtr attrUse = NULL;
24705 xmlSchemaAttributePtr attrDecl = NULL;
24706 xmlSchemaAttrInfoPtr iattr, tmpiattr;
24707 int i, j, found, nbAttrs, nbUses;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024708 int xpathRes = 0, res, wildIDs = 0, fixed;
24709
24710 /*
24711 * SPEC (cvc-attribute)
24712 * (1) "The declaration must not be ·absent· (see Missing
24713 * Sub-components (§5.3) for how this can fail to be
24714 * the case)."
24715 * (2) "Its {type definition} must not be absent."
24716 *
24717 * NOTE (1) + (2): This is not handled here, since we currently do not
24718 * allow validation against schemas which have missing sub-components.
24719 *
24720 * SPEC (cvc-complex-type)
24721 * (3) "For each attribute information item in the element information
24722 * item's [attributes] excepting those whose [namespace name] is
24723 * identical to http://www.w3.org/2001/XMLSchema-instance and whose
24724 * [local name] is one of type, nil, schemaLocation or
24725 * noNamespaceSchemaLocation, the appropriate case among the following
24726 * must be true:
24727 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024728 */
24729 attrUseList = (xmlSchemaItemListPtr) type->attrUses;
24730 /*
24731 * @nbAttrs is the number of attributes present in the instance.
24732 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024733 nbAttrs = vctxt->nbAttrInfos;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024734 if (attrUseList != NULL)
24735 nbUses = attrUseList->nbItems;
24736 else
24737 nbUses = 0;
24738 for (i = 0; i < nbUses; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024739 found = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024740 attrUse = attrUseList->items[i];
24741 attrDecl = WXS_ATTRUSE_DECL(attrUse);
24742 for (j = 0; j < nbAttrs; j++) {
24743 iattr = vctxt->attrInfos[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024744 /*
24745 * SPEC (cvc-complex-type) (3)
24746 * Skip meta attributes.
24747 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024748 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024749 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024750 if (iattr->localName[0] != attrDecl->name[0])
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024751 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024752 if (!xmlStrEqual(iattr->localName, attrDecl->name))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024753 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024754 if (!xmlStrEqual(iattr->nsName, attrDecl->targetNamespace))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024755 continue;
24756 found = 1;
24757 /*
24758 * SPEC (cvc-complex-type)
24759 * (3.1) "If there is among the {attribute uses} an attribute
24760 * use with an {attribute declaration} whose {name} matches
24761 * the attribute information item's [local name] and whose
24762 * {target namespace} is identical to the attribute information
24763 * item's [namespace name] (where an ·absent· {target namespace}
24764 * is taken to be identical to a [namespace name] with no value),
24765 * then the attribute information must be ·valid· with respect
24766 * to that attribute use as per Attribute Locally Valid (Use)
24767 * (§3.5.4). In this case the {attribute declaration} of that
24768 * attribute use is the ·context-determined declaration· for the
24769 * attribute information item with respect to Schema-Validity
24770 * Assessment (Attribute) (§3.2.4) and
24771 * Assessment Outcome (Attribute) (§3.2.5).
24772 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024773 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
24774 iattr->use = attrUse;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024775 /*
24776 * Context-determined declaration.
24777 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024778 iattr->decl = attrDecl;
24779 iattr->typeDef = attrDecl->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024780 break;
24781 }
24782
24783 if (found)
24784 continue;
24785
24786 if (attrUse->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED) {
24787 /*
24788 * Handle non-existent, required attributes.
24789 *
24790 * SPEC (cvc-complex-type)
24791 * (4) "The {attribute declaration} of each attribute use in
24792 * the {attribute uses} whose {required} is true matches one
24793 * of the attribute information items in the element information
24794 * item's [attributes] as per clause 3.1 above."
24795 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024796 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24797 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024798 VERROR_INT(
24799 "xmlSchemaVAttributesComplex",
24800 "calling xmlSchemaGetFreshAttrInfo()");
24801 return (-1);
24802 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024803 tmpiattr->state = XML_SCHEMAS_ATTR_ERR_MISSING;
24804 tmpiattr->use = attrUse;
24805 tmpiattr->decl = attrDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024806 } else if ((attrUse->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
24807 ((attrUse->defValue != NULL) ||
24808 (attrDecl->defValue != NULL))) {
24809 /*
24810 * Handle non-existent, optional, default/fixed attributes.
24811 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024812 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24813 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024814 VERROR_INT(
24815 "xmlSchemaVAttributesComplex",
24816 "calling xmlSchemaGetFreshAttrInfo()");
24817 return (-1);
24818 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024819 tmpiattr->state = XML_SCHEMAS_ATTR_DEFAULT;
24820 tmpiattr->use = attrUse;
24821 tmpiattr->decl = attrDecl;
24822 tmpiattr->typeDef = attrDecl->subtypes;
24823 tmpiattr->localName = attrDecl->name;
24824 tmpiattr->nsName = attrDecl->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024825 }
24826 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024827
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024828 if (vctxt->nbAttrInfos == 0)
24829 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024830 nbUses = vctxt->nbAttrInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024831 /*
24832 * Validate against the wildcard.
24833 */
24834 if (type->attributeWildcard != NULL) {
24835 /*
24836 * SPEC (cvc-complex-type)
24837 * (3.2.1) "There must be an {attribute wildcard}."
24838 */
24839 for (i = 0; i < nbAttrs; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024840 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024841 /*
24842 * SPEC (cvc-complex-type) (3)
24843 * Skip meta attributes.
24844 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024845 if (iattr->state != XML_SCHEMAS_ATTR_UNKNOWN)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024846 continue;
24847 /*
24848 * SPEC (cvc-complex-type)
24849 * (3.2.2) "The attribute information item must be ·valid· with
24850 * respect to it as defined in Item Valid (Wildcard) (§3.10.4)."
24851 *
24852 * SPEC Item Valid (Wildcard) (cvc-wildcard)
24853 * "... its [namespace name] must be ·valid· with respect to
24854 * the wildcard constraint, as defined in Wildcard allows
24855 * Namespace Name (§3.10.4)."
24856 */
24857 if (xmlSchemaCheckCVCWildcardNamespace(type->attributeWildcard,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024858 iattr->nsName) == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024859 /*
24860 * Handle processContents.
24861 *
24862 * SPEC (cvc-wildcard):
24863 * processContents | context-determined declaration:
24864 * "strict" "mustFind"
24865 * "lax" "none"
24866 * "skip" "skip"
24867 */
24868 if (type->attributeWildcard->processContents ==
24869 XML_SCHEMAS_ANY_SKIP) {
24870 /*
24871 * context-determined declaration = "skip"
24872 *
24873 * SPEC PSVI Assessment Outcome (Attribute)
24874 * [validity] = "notKnown"
24875 * [validation attempted] = "none"
24876 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024877 iattr->state = XML_SCHEMAS_ATTR_WILD_SKIP;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024878 continue;
24879 }
24880 /*
24881 * Find an attribute declaration.
24882 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024883 iattr->decl = xmlSchemaGetAttributeDecl(vctxt->schema,
24884 iattr->localName, iattr->nsName);
24885 if (iattr->decl != NULL) {
24886 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024887 /*
24888 * SPEC (cvc-complex-type)
24889 * (5) "Let [Definition:] the wild IDs be the set of
24890 * all attribute information item to which clause 3.2
24891 * applied and whose ·validation· resulted in a
24892 * ·context-determined declaration· of mustFind or no
24893 * ·context-determined declaration· at all, and whose
24894 * [local name] and [namespace name] resolve (as
24895 * defined by QName resolution (Instance) (§3.15.4)) to
24896 * an attribute declaration whose {type definition} is
24897 * or is derived from ID. Then all of the following
24898 * must be true:"
24899 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024900 iattr->typeDef = WXS_ATTR_TYPEDEF(iattr->decl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024901 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024902 iattr->typeDef, XML_SCHEMAS_ID)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024903 /*
24904 * SPEC (5.1) "There must be no more than one
24905 * item in ·wild IDs·."
24906 */
24907 if (wildIDs != 0) {
24908 /* VAL TODO */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024909 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024910 TODO
24911 continue;
24912 }
24913 wildIDs++;
24914 /*
24915 * SPEC (cvc-complex-type)
24916 * (5.2) "If ·wild IDs· is non-empty, there must not
24917 * be any attribute uses among the {attribute uses}
24918 * whose {attribute declaration}'s {type definition}
24919 * is or is derived from ID."
24920 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024921 for (j = 0; j < attrUseList->nbItems; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024922 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024923 WXS_ATTRUSE_TYPEDEF(attrUseList->items[j]),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024924 XML_SCHEMAS_ID)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024925 /* URGENT VAL TODO: implement */
24926 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024927 TODO
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024928 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024929 }
24930 }
24931 }
24932 } else if (type->attributeWildcard->processContents ==
24933 XML_SCHEMAS_ANY_LAX) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024934 iattr->state = XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024935 /*
24936 * SPEC PSVI Assessment Outcome (Attribute)
24937 * [validity] = "notKnown"
24938 * [validation attempted] = "none"
24939 */
24940 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024941 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024942 }
24943 }
24944 }
24945 }
24946
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024947 if (vctxt->nbAttrInfos == 0)
24948 return (0);
24949
24950 /*
24951 * Validate values, create default attributes, evaluate IDCs.
24952 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024953 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024954 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024955 /*
24956 * VAL TODO: Note that we won't try to resolve IDCs to
24957 * "lax" and "skip" validated attributes. Check what to
24958 * do in this case.
24959 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024960 if ((iattr->state != XML_SCHEMAS_ATTR_ASSESSED) &&
24961 (iattr->state != XML_SCHEMAS_ATTR_DEFAULT))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024962 continue;
24963 /*
24964 * VAL TODO: What to do if the type definition is missing?
24965 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024966 if (iattr->typeDef == NULL) {
24967 iattr->state = XML_SCHEMAS_ATTR_ERR_NO_TYPE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024968 continue;
24969 }
24970
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024971 ACTIVATE_ATTRIBUTE(iattr);
Kasimier T. Buchcik828f6542005-06-09 11:23:39 +000024972 fixed = 0;
24973 xpathRes = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024974
24975 if (vctxt->xpathStates != NULL) {
24976 /*
24977 * Evaluate IDCs.
24978 */
24979 xpathRes = xmlSchemaXPathEvaluate(vctxt,
24980 XML_ATTRIBUTE_NODE);
24981 if (xpathRes == -1) {
24982 VERROR_INT("xmlSchemaVAttributesComplex",
24983 "calling xmlSchemaXPathEvaluate()");
24984 goto internal_error;
24985 }
24986 }
24987
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024988 if (iattr->state == XML_SCHEMAS_ATTR_DEFAULT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024989 /*
24990 * Default/fixed attributes.
24991 */
24992 if (xpathRes) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024993 if (iattr->use->defValue != NULL) {
24994 iattr->value = (xmlChar *) iattr->use->defValue;
24995 iattr->val = iattr->use->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024996 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024997 iattr->value = (xmlChar *) iattr->decl->defValue;
24998 iattr->val = iattr->decl->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024999 }
25000 /*
25001 * IDCs will consume the precomputed default value,
25002 * so we need to clone it.
25003 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025004 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025005 VERROR_INT("xmlSchemaVAttributesComplex",
25006 "default/fixed value on an attribute use was "
25007 "not precomputed");
25008 goto internal_error;
25009 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025010 iattr->val = xmlSchemaCopyValue(iattr->val);
25011 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025012 VERROR_INT("xmlSchemaVAttributesComplex",
25013 "calling xmlSchemaCopyValue()");
25014 goto internal_error;
25015 }
25016 }
25017 /*
25018 * PSVI: Add the default attribute to the current element.
25019 * VAL TODO: Should we use the *normalized* value? This currently
25020 * uses the *initial* value.
25021 */
25022 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025023 (iattr->node != NULL) && (iattr->node->doc != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025024 xmlChar *normValue;
25025 const xmlChar *value;
25026
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025027 value = iattr->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025028 /*
25029 * Normalize the value.
25030 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025031 normValue = xmlSchemaNormalizeValue(iattr->typeDef,
25032 iattr->value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025033 if (normValue != NULL)
25034 value = BAD_CAST normValue;
25035
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025036 if (iattr->nsName == NULL) {
25037 if (xmlNewProp(iattr->node->parent,
25038 iattr->localName, value) == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025039 VERROR_INT("xmlSchemaVAttributesComplex",
25040 "callling xmlNewProp()");
25041 if (normValue != NULL)
25042 xmlFree(normValue);
25043 goto internal_error;
25044 }
25045 } else {
25046 xmlNsPtr ns;
25047
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025048 ns = xmlSearchNsByHref(iattr->node->doc,
25049 iattr->node->parent, iattr->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025050 if (ns == NULL) {
25051 xmlChar prefix[12];
25052 int counter = 0;
25053
25054 /*
25055 * Create a namespace declaration on the validation
25056 * root node if no namespace declaration is in scope.
25057 */
25058 do {
25059 snprintf((char *) prefix, 12, "p%d", counter++);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025060 ns = xmlSearchNs(iattr->node->doc,
25061 iattr->node->parent, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025062 if (counter > 1000) {
25063 VERROR_INT(
25064 "xmlSchemaVAttributesComplex",
25065 "could not compute a ns prefix for a "
25066 "default/fixed attribute");
25067 if (normValue != NULL)
25068 xmlFree(normValue);
25069 goto internal_error;
25070 }
25071 } while (ns != NULL);
25072 ns = xmlNewNs(vctxt->validationRoot,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025073 iattr->nsName, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025074 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025075 /*
25076 * TODO:
25077 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0406.html
25078 * If we have QNames: do we need to ensure there's a
25079 * prefix defined for the QName?
25080 */
25081 xmlNewNsProp(iattr->node->parent, ns,
25082 iattr->localName, value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025083 }
25084 if (normValue != NULL)
25085 xmlFree(normValue);
25086 }
25087 /*
25088 * Go directly to IDC evaluation.
25089 */
25090 goto eval_idcs;
25091 }
25092 /*
25093 * Validate the value.
25094 */
25095 if (vctxt->value != NULL) {
25096 /*
25097 * Free last computed value; just for safety reasons.
25098 */
25099 xmlSchemaFreeValue(vctxt->value);
25100 vctxt->value = NULL;
25101 }
25102 /*
25103 * Note that the attribute *use* can be unavailable, if
25104 * the attribute was a wild attribute.
25105 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025106 if ((iattr->decl->flags & XML_SCHEMAS_ATTR_FIXED) ||
25107 ((iattr->use != NULL) &&
25108 (iattr->use->flags & XML_SCHEMAS_ATTR_FIXED)))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025109 fixed = 1;
25110 else
25111 fixed = 0;
25112 /*
25113 * SPEC (cvc-attribute)
25114 * (3) "The item's ·normalized value· must be locally ·valid·
25115 * with respect to that {type definition} as per
25116 * String Valid (§3.14.4)."
25117 *
25118 * VAL TODO: Do we already have the
25119 * "normalized attribute value" here?
25120 */
25121 if (xpathRes || fixed) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025122 iattr->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025123 /*
25124 * Request a computed value.
25125 */
25126 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025127 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025128 iattr->node, iattr->typeDef, iattr->value, &(iattr->val),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025129 1, 1, 0);
25130 } else {
25131 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025132 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025133 iattr->node, iattr->typeDef, iattr->value, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025134 1, 0, 0);
25135 }
25136
25137 if (res != 0) {
25138 if (res == -1) {
25139 VERROR_INT("xmlSchemaVAttributesComplex",
25140 "calling xmlSchemaStreamValidateSimpleTypeValue()");
25141 goto internal_error;
25142 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025143 iattr->state = XML_SCHEMAS_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025144 /*
25145 * SPEC PSVI Assessment Outcome (Attribute)
25146 * [validity] = "invalid"
25147 */
25148 goto eval_idcs;
25149 }
25150
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025151 if (fixed) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025152 /*
25153 * SPEC Attribute Locally Valid (Use) (cvc-au)
25154 * "For an attribute information item to be·valid·
25155 * with respect to an attribute use its *normalized*
25156 * value· must match the *canonical* lexical
25157 * representation of the attribute use's {value
25158 * constraint}value, if it is present and fixed."
25159 *
25160 * VAL TODO: The requirement for the *canonical* value
25161 * will be removed in XML Schema 1.1.
25162 */
25163 /*
25164 * SPEC Attribute Locally Valid (cvc-attribute)
25165 * (4) "The item's *actual* value· must match the *value* of
25166 * the {value constraint}, if it is present and fixed."
25167 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025168 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025169 /* VAL TODO: A value was not precomputed. */
25170 TODO
25171 goto eval_idcs;
25172 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025173 if ((iattr->use != NULL) &&
25174 (iattr->use->defValue != NULL)) {
25175 if (iattr->use->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025176 /* VAL TODO: A default value was not precomputed. */
25177 TODO
25178 goto eval_idcs;
25179 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025180 iattr->vcValue = iattr->use->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025181 /*
25182 if (xmlSchemaCompareValuesWhtsp(attr->val,
25183 (xmlSchemaWhitespaceValueType) ws,
25184 attr->use->defVal,
25185 (xmlSchemaWhitespaceValueType) ws) != 0) {
25186 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025187 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->use->defVal))
25188 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025189 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025190 if (iattr->decl->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025191 /* VAL TODO: A default value was not precomputed. */
25192 TODO
25193 goto eval_idcs;
25194 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025195 iattr->vcValue = iattr->decl->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025196 /*
25197 if (xmlSchemaCompareValuesWhtsp(attr->val,
25198 (xmlSchemaWhitespaceValueType) ws,
25199 attrDecl->defVal,
25200 (xmlSchemaWhitespaceValueType) ws) != 0) {
25201 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025202 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->decl->defVal))
25203 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025204 }
25205 /*
25206 * [validity] = "valid"
25207 */
25208 }
25209eval_idcs:
25210 /*
25211 * Evaluate IDCs.
25212 */
25213 if (xpathRes) {
25214 if (xmlSchemaXPathProcessHistory(vctxt,
25215 vctxt->depth +1) == -1) {
25216 VERROR_INT("xmlSchemaVAttributesComplex",
25217 "calling xmlSchemaXPathEvaluate()");
25218 goto internal_error;
25219 }
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000025220 } else if (vctxt->xpathStates != NULL)
25221 xmlSchemaXPathPop(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025222 }
25223
25224 /*
25225 * Report errors.
25226 */
25227 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025228 iattr = vctxt->attrInfos[i];
25229 if ((iattr->state == XML_SCHEMAS_ATTR_META) ||
25230 (iattr->state == XML_SCHEMAS_ATTR_ASSESSED) ||
25231 (iattr->state == XML_SCHEMAS_ATTR_WILD_SKIP) ||
25232 (iattr->state == XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025233 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025234 ACTIVATE_ATTRIBUTE(iattr);
25235 switch (iattr->state) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025236 case XML_SCHEMAS_ATTR_ERR_MISSING: {
25237 xmlChar *str = NULL;
25238 ACTIVATE_ELEM;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025239 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025240 XML_SCHEMAV_CVC_COMPLEX_TYPE_4, NULL, NULL,
25241 "The attribute '%s' is required but missing",
25242 xmlSchemaFormatQName(&str,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025243 iattr->decl->targetNamespace,
25244 iattr->decl->name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025245 NULL);
25246 FREE_AND_NULL(str)
25247 break;
25248 }
25249 case XML_SCHEMAS_ATTR_ERR_NO_TYPE:
25250 VERROR(XML_SCHEMAV_CVC_ATTRIBUTE_2, NULL,
25251 "The type definition is absent");
25252 break;
25253 case XML_SCHEMAS_ATTR_ERR_FIXED_VALUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025254 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025255 XML_SCHEMAV_CVC_AU, NULL, NULL,
25256 "The value '%s' does not match the fixed "
25257 "value constraint '%s'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025258 iattr->value, iattr->vcValue);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025259 break;
25260 case XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL:
25261 VERROR(XML_SCHEMAV_CVC_WILDCARD, NULL,
25262 "No matching global attribute declaration available, but "
25263 "demanded by the strict wildcard");
25264 break;
25265 case XML_SCHEMAS_ATTR_UNKNOWN:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025266 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025267 break;
25268 /*
25269 * MAYBE VAL TODO: One might report different error messages
25270 * for the following errors.
25271 */
25272 if (type->attributeWildcard == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025273 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025274 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_1, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025275 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025276 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025277 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_2, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025278 }
25279 break;
25280 default:
25281 break;
25282 }
25283 }
25284
25285 ACTIVATE_ELEM;
25286 return (0);
25287internal_error:
25288 ACTIVATE_ELEM;
25289 return (-1);
25290}
25291
25292static int
25293xmlSchemaValidateElemWildcard(xmlSchemaValidCtxtPtr vctxt,
25294 int *skip)
25295{
25296 xmlSchemaWildcardPtr wild = (xmlSchemaWildcardPtr) vctxt->inode->decl;
25297 /*
25298 * The namespace of the element was already identified to be
25299 * matching the wildcard.
25300 */
25301 if ((skip == NULL) || (wild == NULL) ||
25302 (wild->type != XML_SCHEMA_TYPE_ANY)) {
25303 VERROR_INT("xmlSchemaValidateElemWildcard",
25304 "bad arguments");
25305 return (-1);
25306 }
25307 *skip = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025308 if (wild->processContents == XML_SCHEMAS_ANY_SKIP) {
25309 /*
25310 * URGENT VAL TODO: Either we need to position the stream to the
25311 * next sibling, or walk the whole subtree.
25312 */
25313 *skip = 1;
25314 return (0);
25315 }
25316 {
25317 xmlSchemaElementPtr decl = NULL;
25318
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025319 decl = xmlSchemaGetElem(vctxt->schema,
25320 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025321 if (decl != NULL) {
25322 vctxt->inode->decl = decl;
25323 return (0);
25324 }
25325 }
25326 if (wild->processContents == XML_SCHEMAS_ANY_STRICT) {
25327 /* VAL TODO: Change to proper error code. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025328 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL, /* WXS_BASIC_CAST wild */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025329 "No matching global element declaration available, but "
25330 "demanded by the strict wildcard");
25331 return (vctxt->err);
25332 }
25333 if (vctxt->nbAttrInfos != 0) {
25334 xmlSchemaAttrInfoPtr iattr;
25335 /*
25336 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25337 * (1.2.1.2.1) - (1.2.1.2.3 )
25338 *
25339 * Use the xsi:type attribute for the type definition.
25340 */
25341 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25342 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
25343 if (iattr != NULL) {
25344 if (xmlSchemaProcessXSIType(vctxt, iattr,
25345 &(vctxt->inode->typeDef), NULL) == -1) {
25346 VERROR_INT("xmlSchemaValidateElemWildcard",
25347 "calling xmlSchemaProcessXSIType() to "
25348 "process the attribute 'xsi:nil'");
25349 return (-1);
25350 }
25351 /*
25352 * Don't return an error on purpose.
25353 */
25354 return (0);
25355 }
25356 }
25357 /*
25358 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25359 *
25360 * Fallback to "anyType".
25361 */
25362 vctxt->inode->typeDef =
25363 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
25364 return (0);
25365}
25366
25367/*
25368* xmlSchemaCheckCOSValidDefault:
25369*
25370* This will be called if: not nilled, no content and a default/fixed
25371* value is provided.
25372*/
25373
25374static int
25375xmlSchemaCheckCOSValidDefault(xmlSchemaValidCtxtPtr vctxt,
25376 const xmlChar *value,
25377 xmlSchemaValPtr *val)
25378{
25379 int ret = 0;
25380 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25381
25382 /*
25383 * cos-valid-default:
25384 * Schema Component Constraint: Element Default Valid (Immediate)
25385 * For a string to be a valid default with respect to a type
25386 * definition the appropriate case among the following must be true:
25387 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025388 if WXS_IS_COMPLEX(inode->typeDef) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025389 /*
25390 * Complex type.
25391 *
25392 * SPEC (2.1) "its {content type} must be a simple type definition
25393 * or mixed."
25394 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
25395 * type}'s particle must be ·emptiable· as defined by
25396 * Particle Emptiable (§3.9.6)."
25397 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025398 if ((! WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) &&
25399 ((! WXS_HAS_MIXED_CONTENT(inode->typeDef)) ||
25400 (! WXS_EMPTIABLE(inode->typeDef)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025401 ret = XML_SCHEMAP_COS_VALID_DEFAULT_2_1;
25402 /* NOTE that this covers (2.2.2) as well. */
25403 VERROR(ret, NULL,
25404 "For a string to be a valid default, the type definition "
25405 "must be a simple type or a complex type with simple content "
25406 "or mixed content and a particle emptiable");
25407 return(ret);
25408 }
25409 }
25410 /*
25411 * 1 If the type definition is a simple type definition, then the string
25412 * must be ·valid· with respect to that definition as defined by String
25413 * Valid (§3.14.4).
25414 *
25415 * AND
25416 *
25417 * 2.2.1 If the {content type} is a simple type definition, then the
25418 * string must be ·valid· with respect to that simple type definition
25419 * as defined by String Valid (§3.14.4).
25420 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025421 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025422
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025423 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025424 NULL, inode->typeDef, value, val, 1, 1, 0);
25425
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025426 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025427
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025428 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025429 NULL, inode->typeDef->contentTypeDef, value, val, 1, 1, 0);
25430 }
25431 if (ret < 0) {
25432 VERROR_INT("xmlSchemaCheckCOSValidDefault",
25433 "calling xmlSchemaVCheckCVCSimpleType()");
25434 }
25435 return (ret);
25436}
25437
25438static void
25439xmlSchemaVContentModelCallback(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
25440 const xmlChar * name ATTRIBUTE_UNUSED,
25441 xmlSchemaElementPtr item,
25442 xmlSchemaNodeInfoPtr inode)
25443{
25444 inode->decl = item;
25445#ifdef DEBUG_CONTENT
25446 {
25447 xmlChar *str = NULL;
25448
25449 if (item->type == XML_SCHEMA_TYPE_ELEMENT) {
25450 xmlGenericError(xmlGenericErrorContext,
25451 "AUTOMATON callback for '%s' [declaration]\n",
25452 xmlSchemaFormatQName(&str,
25453 inode->localName, inode->nsName));
25454 } else {
25455 xmlGenericError(xmlGenericErrorContext,
25456 "AUTOMATON callback for '%s' [wildcard]\n",
25457 xmlSchemaFormatQName(&str,
25458 inode->localName, inode->nsName));
25459
25460 }
25461 FREE_AND_NULL(str)
25462 }
25463#endif
25464}
25465
25466static int
25467xmlSchemaValidatorPushElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025468{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025469 vctxt->inode = xmlSchemaGetFreshElemInfo(vctxt);
25470 if (vctxt->inode == NULL) {
25471 VERROR_INT("xmlSchemaValidatorPushElem",
25472 "calling xmlSchemaGetFreshElemInfo()");
25473 return (-1);
25474 }
25475 vctxt->nbAttrInfos = 0;
25476 return (0);
25477}
25478
25479static int
25480xmlSchemaVCheckINodeDataType(xmlSchemaValidCtxtPtr vctxt,
25481 xmlSchemaNodeInfoPtr inode,
25482 xmlSchemaTypePtr type,
25483 const xmlChar *value)
25484{
25485 if (inode->flags & XML_SCHEMA_NODE_INFO_VALUE_NEEDED)
25486 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025487 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025488 type, value, &(inode->val), 1, 1, 0));
25489 else
25490 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025491 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025492 type, value, NULL, 1, 0, 0));
25493}
25494
25495
25496
25497/*
25498* Process END of element.
25499*/
25500static int
25501xmlSchemaValidatorPopElem(xmlSchemaValidCtxtPtr vctxt)
25502{
25503 int ret = 0;
25504 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25505
25506 if (vctxt->nbAttrInfos != 0)
25507 xmlSchemaClearAttrInfos(vctxt);
25508 if (inode->flags & XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED) {
25509 /*
25510 * This element was not expected;
25511 * we will not validate child elements of broken parents.
25512 * Skip validation of all content of the parent.
25513 */
25514 vctxt->skipDepth = vctxt->depth -1;
25515 goto end_elem;
25516 }
25517 if ((inode->typeDef == NULL) ||
25518 (inode->flags & XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE)) {
25519 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025520 * 1. the type definition might be missing if the element was
25521 * error prone
25522 * 2. it might be abstract.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025523 */
25524 goto end_elem;
25525 }
25526 /*
25527 * Check the content model.
25528 */
25529 if ((inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) ||
25530 (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)) {
25531
25532 /*
25533 * Workaround for "anyType".
25534 */
25535 if (inode->typeDef->builtInType == XML_SCHEMAS_ANYTYPE)
25536 goto character_content;
25537
25538 if ((inode->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) == 0) {
25539 xmlChar *values[10];
25540 int terminal, nbval = 10, nbneg;
25541
25542 if (inode->regexCtxt == NULL) {
25543 /*
25544 * Create the regex context.
25545 */
25546 inode->regexCtxt =
25547 xmlRegNewExecCtxt(inode->typeDef->contModel,
25548 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
25549 vctxt);
25550 if (inode->regexCtxt == NULL) {
25551 VERROR_INT("xmlSchemaValidatorPopElem",
25552 "failed to create a regex context");
25553 goto internal_error;
25554 }
25555#ifdef DEBUG_AUTOMATA
25556 xmlGenericError(xmlGenericErrorContext,
25557 "AUTOMATON create on '%s'\n", inode->localName);
25558#endif
25559 }
25560 /*
25561 * Get hold of the still expected content, since a further
25562 * call to xmlRegExecPushString() will loose this information.
25563 */
25564 xmlRegExecNextValues(inode->regexCtxt,
25565 &nbval, &nbneg, &values[0], &terminal);
25566 ret = xmlRegExecPushString(inode->regexCtxt, NULL, NULL);
25567 if (ret <= 0) {
25568 /*
25569 * Still missing something.
25570 */
25571 ret = 1;
25572 inode->flags |=
25573 XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025574 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025575 XML_SCHEMAV_ELEMENT_CONTENT, NULL, NULL,
25576 "Missing child element(s)",
25577 nbval, nbneg, values);
25578#ifdef DEBUG_AUTOMATA
25579 xmlGenericError(xmlGenericErrorContext,
25580 "AUTOMATON missing ERROR on '%s'\n",
25581 inode->localName);
25582#endif
25583 } else {
25584 /*
25585 * Content model is satisfied.
25586 */
25587 ret = 0;
25588#ifdef DEBUG_AUTOMATA
25589 xmlGenericError(xmlGenericErrorContext,
25590 "AUTOMATON succeeded on '%s'\n",
25591 inode->localName);
25592#endif
25593 }
25594
25595 }
25596 }
25597 if (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)
25598 goto end_elem;
25599
25600character_content:
25601
25602 if (vctxt->value != NULL) {
25603 xmlSchemaFreeValue(vctxt->value);
25604 vctxt->value = NULL;
25605 }
25606 /*
25607 * Check character content.
25608 */
25609 if (inode->decl == NULL) {
25610 /*
25611 * Speedup if no declaration exists.
25612 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025613 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025614 ret = xmlSchemaVCheckINodeDataType(vctxt,
25615 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025616 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025617 ret = xmlSchemaVCheckINodeDataType(vctxt,
25618 inode, inode->typeDef->contentTypeDef,
25619 inode->value);
25620 }
25621 if (ret < 0) {
25622 VERROR_INT("xmlSchemaValidatorPopElem",
25623 "calling xmlSchemaVCheckCVCSimpleType()");
25624 goto internal_error;
25625 }
25626 goto end_elem;
25627 }
25628 /*
25629 * cvc-elt (3.3.4) : 5
25630 * The appropriate case among the following must be true:
25631 */
25632 /*
25633 * cvc-elt (3.3.4) : 5.1
25634 * If the declaration has a {value constraint},
25635 * the item has neither element nor character [children] and
25636 * clause 3.2 has not applied, then all of the following must be true:
25637 */
25638 if ((inode->decl->value != NULL) &&
25639 (inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY) &&
25640 (! INODE_NILLED(inode))) {
25641 /*
25642 * cvc-elt (3.3.4) : 5.1.1
25643 * If the ·actual type definition· is a ·local type definition·
25644 * then the canonical lexical representation of the {value constraint}
25645 * value must be a valid default for the ·actual type definition· as
25646 * defined in Element Default Valid (Immediate) (§3.3.6).
25647 */
25648 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025649 * NOTE: 'local' above means types acquired by xsi:type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025650 * NOTE: Although the *canonical* value is stated, it is not
25651 * relevant if canonical or not. Additionally XML Schema 1.1
25652 * will removed this requirement as well.
25653 */
25654 if (inode->flags & XML_SCHEMA_ELEM_INFO_LOCAL_TYPE) {
25655
25656 ret = xmlSchemaCheckCOSValidDefault(vctxt,
25657 inode->decl->value, &(inode->val));
25658 if (ret != 0) {
25659 if (ret < 0) {
25660 VERROR_INT("xmlSchemaValidatorPopElem",
25661 "calling xmlSchemaCheckCOSValidDefault()");
25662 goto internal_error;
25663 }
25664 goto end_elem;
25665 }
25666 /*
25667 * Stop here, to avoid redundant validation of the value
25668 * (see following).
25669 */
25670 goto default_psvi;
25671 }
25672 /*
25673 * cvc-elt (3.3.4) : 5.1.2
25674 * The element information item with the canonical lexical
25675 * representation of the {value constraint} value used as its
25676 * ·normalized value· must be ·valid· with respect to the
25677 * ·actual type definition· as defined by Element Locally Valid (Type)
25678 * (§3.3.4).
25679 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025680 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025681 ret = xmlSchemaVCheckINodeDataType(vctxt,
25682 inode, inode->typeDef, inode->decl->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025683 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025684 ret = xmlSchemaVCheckINodeDataType(vctxt,
25685 inode, inode->typeDef->contentTypeDef,
25686 inode->decl->value);
25687 }
25688 if (ret != 0) {
25689 if (ret < 0) {
25690 VERROR_INT("xmlSchemaValidatorPopElem",
25691 "calling xmlSchemaVCheckCVCSimpleType()");
25692 goto internal_error;
25693 }
25694 goto end_elem;
25695 }
25696
25697default_psvi:
25698 /*
25699 * PSVI: Create a text node on the instance element.
25700 */
25701 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
25702 (inode->node != NULL)) {
25703 xmlNodePtr textChild;
25704 xmlChar *normValue;
25705 /*
25706 * VAL TODO: Normalize the value.
25707 */
25708 normValue = xmlSchemaNormalizeValue(inode->typeDef,
25709 inode->decl->value);
25710 if (normValue != NULL) {
25711 textChild = xmlNewText(BAD_CAST normValue);
25712 xmlFree(normValue);
25713 } else
25714 textChild = xmlNewText(inode->decl->value);
25715 if (textChild == NULL) {
25716 VERROR_INT("xmlSchemaValidatorPopElem",
25717 "calling xmlNewText()");
25718 goto internal_error;
25719 } else
25720 xmlAddChild(inode->node, textChild);
25721 }
25722
25723 } else if (! INODE_NILLED(inode)) {
25724 /*
25725 * 5.2.1 The element information item must be ·valid· with respect
25726 * to the ·actual type definition· as defined by Element Locally
25727 * Valid (Type) (§3.3.4).
25728 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025729 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025730 /*
25731 * SPEC (cvc-type) (3.1)
25732 * "If the type definition is a simple type definition, ..."
25733 * (3.1.3) "If clause 3.2 of Element Locally Valid
25734 * (Element) (§3.3.4) did not apply, then the ·normalized value·
25735 * must be ·valid· with respect to the type definition as defined
25736 * by String Valid (§3.14.4).
25737 */
25738 ret = xmlSchemaVCheckINodeDataType(vctxt,
25739 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025740 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025741 /*
25742 * SPEC (cvc-type) (3.2) "If the type definition is a complex type
25743 * definition, then the element information item must be
25744 * ·valid· with respect to the type definition as per
25745 * Element Locally Valid (Complex Type) (§3.4.4);"
25746 *
25747 * SPEC (cvc-complex-type) (2.2)
25748 * "If the {content type} is a simple type definition, ...
25749 * the ·normalized value· of the element information item is
25750 * ·valid· with respect to that simple type definition as
25751 * defined by String Valid (§3.14.4)."
25752 */
25753 ret = xmlSchemaVCheckINodeDataType(vctxt,
25754 inode, inode->typeDef->contentTypeDef, inode->value);
25755 }
25756 if (ret != 0) {
25757 if (ret < 0) {
25758 VERROR_INT("xmlSchemaValidatorPopElem",
25759 "calling xmlSchemaVCheckCVCSimpleType()");
25760 goto internal_error;
25761 }
25762 goto end_elem;
25763 }
25764 /*
25765 * 5.2.2 If there is a fixed {value constraint} and clause 3.2 has
25766 * not applied, all of the following must be true:
25767 */
25768 if ((inode->decl->value != NULL) &&
25769 (inode->decl->flags & XML_SCHEMAS_ELEM_FIXED)) {
25770
25771 /*
25772 * TODO: We will need a computed value, when comparison is
25773 * done on computed values.
25774 */
25775 /*
25776 * 5.2.2.1 The element information item must have no element
25777 * information item [children].
25778 */
25779 if (inode->flags &
25780 XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT) {
25781 ret = XML_SCHEMAV_CVC_ELT_5_2_2_1;
25782 VERROR(ret, NULL,
25783 "The content must not containt element nodes since "
25784 "there is a fixed value constraint");
25785 goto end_elem;
25786 } else {
25787 /*
25788 * 5.2.2.2 The appropriate case among the following must
25789 * be true:
25790 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025791 if (WXS_HAS_MIXED_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025792 /*
25793 * 5.2.2.2.1 If the {content type} of the ·actual type
25794 * definition· is mixed, then the *initial value* of the
25795 * item must match the canonical lexical representation
25796 * of the {value constraint} value.
25797 *
25798 * ... the *initial value* of an element information
25799 * item is the string composed of, in order, the
25800 * [character code] of each character information item in
25801 * the [children] of that element information item.
25802 */
25803 if (! xmlStrEqual(inode->value, inode->decl->value)){
25804 /*
25805 * VAL TODO: Report invalid & expected values as well.
25806 * VAL TODO: Implement the canonical stuff.
25807 */
25808 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025809 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025810 ret, NULL, NULL,
25811 "The initial value '%s' does not match the fixed "
25812 "value constraint '%s'",
25813 inode->value, inode->decl->value);
25814 goto end_elem;
25815 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025816 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025817 /*
25818 * 5.2.2.2.2 If the {content type} of the ·actual type
25819 * definition· is a simple type definition, then the
25820 * *actual value* of the item must match the canonical
25821 * lexical representation of the {value constraint} value.
25822 */
25823 /*
25824 * VAL TODO: *actual value* is the normalized value, impl.
25825 * this.
25826 * VAL TODO: Report invalid & expected values as well.
25827 * VAL TODO: Implement a comparison with the computed values.
25828 */
25829 if (! xmlStrEqual(inode->value,
25830 inode->decl->value)) {
25831 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_2;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025832 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025833 ret, NULL, NULL,
25834 "The actual value '%s' does not match the fixed "
25835 "value constraint '%s'",
25836 inode->value,
25837 inode->decl->value);
25838 goto end_elem;
25839 }
25840 }
25841 }
25842 }
25843 }
25844
25845end_elem:
25846 if (vctxt->depth < 0) {
25847 /* TODO: raise error? */
25848 return (0);
25849 }
25850 if (vctxt->depth == vctxt->skipDepth)
25851 vctxt->skipDepth = -1;
25852 /*
25853 * Evaluate the history of XPath state objects.
25854 */
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000025855 if (inode->appliedXPath &&
25856 (xmlSchemaXPathProcessHistory(vctxt, vctxt->depth) == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025857 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025858 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025859 * MAYBE TODO:
25860 * SPEC (6) "The element information item must be ·valid· with
25861 * respect to each of the {identity-constraint definitions} as per
25862 * Identity-constraint Satisfied (§3.11.4)."
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025863 */
25864 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025865 * PSVI TODO: If we expose IDC node-tables via PSVI then the tables
25866 * need to be built in any case.
25867 * We will currently build IDC node-tables and bubble them only if
25868 * keyrefs do exist.
25869 */
25870
25871 /*
25872 * Add the current IDC target-nodes to the IDC node-tables.
25873 */
25874 if ((inode->idcMatchers != NULL) &&
25875 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
25876 {
25877 if (xmlSchemaIDCFillNodeTables(vctxt, inode) == -1)
25878 goto internal_error;
25879 }
25880 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025881 * Validate IDC keyrefs.
25882 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025883 if (vctxt->inode->hasKeyrefs)
25884 if (xmlSchemaCheckCVCIDCKeyRef(vctxt) == -1)
25885 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025886 /*
25887 * Merge/free the IDC table.
25888 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025889 if (inode->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025890#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025891 xmlSchemaDebugDumpIDCTable(stdout,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025892 inode->nsName,
25893 inode->localName,
25894 inode->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025895#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025896 if ((vctxt->depth > 0) &&
25897 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
25898 {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025899 /*
25900 * Merge the IDC node table with the table of the parent node.
25901 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025902 if (xmlSchemaBubbleIDCNodeTables(vctxt) == -1)
25903 goto internal_error;
25904 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025905 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025906 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025907 * Clear the current ielem.
25908 * VAL TODO: Don't free the PSVI IDC tables if they are
25909 * requested for the PSVI.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025910 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025911 xmlSchemaClearElemInfo(inode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025912 /*
25913 * Skip further processing if we are on the validation root.
25914 */
25915 if (vctxt->depth == 0) {
25916 vctxt->depth--;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025917 vctxt->inode = NULL;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000025918 return (0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025919 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025920 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025921 * Reset the keyrefDepth if needed.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025922 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025923 if (vctxt->aidcs != NULL) {
25924 xmlSchemaIDCAugPtr aidc = vctxt->aidcs;
25925 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025926 if (aidc->keyrefDepth == vctxt->depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025927 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025928 * A 'keyrefDepth' of a key/unique IDC matches the current
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025929 * depth, this means that we are leaving the scope of the
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025930 * top-most keyref IDC which refers to this IDC.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025931 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025932 aidc->keyrefDepth = -1;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025933 }
25934 aidc = aidc->next;
25935 } while (aidc != NULL);
25936 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025937 vctxt->depth--;
25938 vctxt->inode = vctxt->elemInfos[vctxt->depth];
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000025939 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025940 * VAL TODO: 7 If the element information item is the ·validation root·, it must be
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000025941 * ·valid· per Validation Root Valid (ID/IDREF) (§3.3.4).
25942 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025943 return (ret);
25944
25945internal_error:
25946 vctxt->err = -1;
25947 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000025948}
25949
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025950/*
25951* 3.4.4 Complex Type Definition Validation Rules
25952* Validation Rule: Element Locally Valid (Complex Type) (cvc-complex-type)
25953*/
Daniel Veillardc0826a72004-08-10 14:17:33 +000025954static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025955xmlSchemaValidateChildElem(xmlSchemaValidCtxtPtr vctxt)
William M. Brack2f2a6632004-08-20 23:09:47 +000025956{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025957 xmlSchemaNodeInfoPtr pielem;
25958 xmlSchemaTypePtr ptype;
Daniel Veillard01fa6152004-06-29 17:04:39 +000025959 int ret = 0;
Daniel Veillard3646d642004-06-02 19:19:14 +000025960
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025961 if (vctxt->depth <= 0) {
25962 VERROR_INT("xmlSchemaValidateChildElem",
25963 "not intended for the validation root");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025964 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025965 }
25966 pielem = vctxt->elemInfos[vctxt->depth -1];
25967 if (pielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
25968 pielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025969 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025970 * Handle 'nilled' elements.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025971 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025972 if (INODE_NILLED(pielem)) {
25973 /*
25974 * SPEC (cvc-elt) (3.3.4) : (3.2.1)
25975 */
25976 ACTIVATE_PARENT_ELEM;
25977 ret = XML_SCHEMAV_CVC_ELT_3_2_1;
25978 VERROR(ret, NULL,
25979 "Neither character nor element content is allowed, "
25980 "because the element was 'nilled'");
25981 ACTIVATE_ELEM;
25982 goto unexpected_elem;
25983 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000025984
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025985 ptype = pielem->typeDef;
25986
25987 if (ptype->builtInType == XML_SCHEMAS_ANYTYPE) {
25988 /*
25989 * Workaround for "anyType": we have currently no content model
25990 * assigned for "anyType", so handle it explicitely.
25991 * "anyType" has an unbounded, lax "any" wildcard.
25992 */
25993 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
25994 vctxt->inode->localName,
25995 vctxt->inode->nsName);
25996
25997 if (vctxt->inode->decl == NULL) {
25998 xmlSchemaAttrInfoPtr iattr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000025999 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026000 * Process "xsi:type".
26001 * SPEC (cvc-assess-elt) (1.2.1.2.1) - (1.2.1.2.3)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026002 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026003 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
26004 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
26005 if (iattr != NULL) {
26006 ret = xmlSchemaProcessXSIType(vctxt, iattr,
26007 &(vctxt->inode->typeDef), NULL);
26008 if (ret != 0) {
26009 if (ret == -1) {
26010 VERROR_INT("xmlSchemaValidateChildElem",
26011 "calling xmlSchemaProcessXSIType() to "
26012 "process the attribute 'xsi:nil'");
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000026013 return (-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000026014 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026015 return (ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +000026016 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026017 } else {
26018 /*
26019 * Fallback to "anyType".
26020 *
26021 * SPEC (cvc-assess-elt)
26022 * "If the item cannot be ·strictly assessed·, [...]
26023 * an element information item's schema validity may be laxly
26024 * assessed if its ·context-determined declaration· is not
26025 * skip by ·validating· with respect to the ·ur-type
26026 * definition· as per Element Locally Valid (Type) (§3.3.4)."
26027 */
26028 vctxt->inode->typeDef =
26029 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026030 }
26031 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026032 return (0);
26033 }
26034
26035 switch (ptype->contentType) {
26036 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026037 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026038 * SPEC (2.1) "If the {content type} is empty, then the
26039 * element information item has no character or element
26040 * information item [children]."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026041 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026042 ACTIVATE_PARENT_ELEM
26043 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1;
26044 VERROR(ret, NULL,
26045 "Element content is not allowed, "
26046 "because the content type is empty");
26047 ACTIVATE_ELEM
26048 goto unexpected_elem;
26049 break;
26050
26051 case XML_SCHEMA_CONTENT_MIXED:
26052 case XML_SCHEMA_CONTENT_ELEMENTS: {
26053 xmlRegExecCtxtPtr regexCtxt;
26054 xmlChar *values[10];
26055 int terminal, nbval = 10, nbneg;
26056
26057 /* VAL TODO: Optimized "anyType" validation.*/
26058
26059 if (ptype->contModel == NULL) {
26060 VERROR_INT("xmlSchemaValidateChildElem",
26061 "type has elem content but no content model");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026062 return (-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000026063 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026064 /*
26065 * Safety belf for evaluation if the cont. model was already
26066 * examined to be invalid.
26067 */
26068 if (pielem->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) {
26069 VERROR_INT("xmlSchemaValidateChildElem",
26070 "validating elem, but elem content is already invalid");
26071 return (-1);
26072 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +000026073
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026074 regexCtxt = pielem->regexCtxt;
26075 if (regexCtxt == NULL) {
26076 /*
26077 * Create the regex context.
26078 */
26079 regexCtxt = xmlRegNewExecCtxt(ptype->contModel,
26080 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
26081 vctxt);
26082 if (regexCtxt == NULL) {
26083 VERROR_INT("xmlSchemaValidateChildElem",
26084 "failed to create a regex context");
26085 return (-1);
26086 }
26087 pielem->regexCtxt = regexCtxt;
26088#ifdef DEBUG_AUTOMATA
26089 xmlGenericError(xmlGenericErrorContext, "AUTOMATA create on '%s'\n",
26090 pielem->localName);
26091#endif
26092 }
26093
26094 /*
26095 * SPEC (2.4) "If the {content type} is element-only or mixed,
26096 * then the sequence of the element information item's
26097 * element information item [children], if any, taken in
26098 * order, is ·valid· with respect to the {content type}'s
26099 * particle, as defined in Element Sequence Locally Valid
26100 * (Particle) (§3.9.4)."
26101 */
26102 ret = xmlRegExecPushString2(regexCtxt,
26103 vctxt->inode->localName,
26104 vctxt->inode->nsName,
26105 vctxt->inode);
26106#ifdef DEBUG_AUTOMATA
26107 if (ret < 0)
26108 xmlGenericError(xmlGenericErrorContext,
26109 "AUTOMATON push ERROR for '%s' on '%s'\n",
26110 vctxt->inode->localName, pielem->localName);
26111 else
26112 xmlGenericError(xmlGenericErrorContext,
26113 "AUTOMATON push OK for '%s' on '%s'\n",
26114 vctxt->inode->localName, pielem->localName);
26115#endif
26116 if (vctxt->err == XML_SCHEMAV_INTERNAL) {
26117 VERROR_INT("xmlSchemaValidateChildElem",
26118 "calling xmlRegExecPushString2()");
26119 return (-1);
26120 }
26121 if (ret < 0) {
26122 xmlRegExecErrInfo(regexCtxt, NULL, &nbval, &nbneg,
26123 &values[0], &terminal);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026124 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026125 XML_SCHEMAV_ELEMENT_CONTENT, NULL,NULL,
26126 "This element is not expected",
26127 nbval, nbneg, values);
26128 ret = vctxt->err;
26129 goto unexpected_elem;
26130 } else
26131 ret = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026132 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026133 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026134 case XML_SCHEMA_CONTENT_SIMPLE:
26135 case XML_SCHEMA_CONTENT_BASIC:
26136 ACTIVATE_PARENT_ELEM
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026137 if (WXS_IS_COMPLEX(ptype)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026138 /*
26139 * SPEC (cvc-complex-type) (2.2)
26140 * "If the {content type} is a simple type definition, then
26141 * the element information item has no element information
26142 * item [children], ..."
26143 */
26144 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_2;
26145 VERROR(ret, NULL, "Element content is not allowed, "
26146 "because the content type is a simple type definition");
26147 } else {
26148 /*
26149 * SPEC (cvc-type) (3.1.2) "The element information item must
26150 * have no element information item [children]."
26151 */
26152 ret = XML_SCHEMAV_CVC_TYPE_3_1_2;
26153 VERROR(ret, NULL, "Element content is not allowed, "
26154 "because the type definition is simple");
26155 }
26156 ACTIVATE_ELEM
26157 ret = vctxt->err;
26158 goto unexpected_elem;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026159 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026160
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026161 default:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026162 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026163 }
26164 return (ret);
26165unexpected_elem:
26166 /*
26167 * Pop this element and set the skipDepth to skip
26168 * all further content of the parent element.
26169 */
26170 vctxt->skipDepth = vctxt->depth;
26171 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED;
26172 pielem->flags |= XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
26173 return (ret);
26174}
26175
26176#define XML_SCHEMA_PUSH_TEXT_PERSIST 1
26177#define XML_SCHEMA_PUSH_TEXT_CREATED 2
26178#define XML_SCHEMA_PUSH_TEXT_VOLATILE 3
26179
26180static int
26181xmlSchemaVPushText(xmlSchemaValidCtxtPtr vctxt,
26182 int nodeType, const xmlChar *value, int len,
26183 int mode, int *consumed)
26184{
26185 /*
26186 * Unfortunately we have to duplicate the text sometimes.
26187 * OPTIMIZE: Maybe we could skip it, if:
26188 * 1. content type is simple
26189 * 2. whitespace is "collapse"
26190 * 3. it consists of whitespace only
26191 *
26192 * Process character content.
26193 */
26194 if (consumed != NULL)
26195 *consumed = 0;
26196 if (INODE_NILLED(vctxt->inode)) {
26197 /*
26198 * SPEC cvc-elt (3.3.4 - 3.2.1)
26199 * "The element information item must have no character or
26200 * element information item [children]."
26201 */
26202 VERROR(XML_SCHEMAV_CVC_ELT_3_2_1, NULL,
26203 "Neither character nor element content is allowed "
26204 "because the element is 'nilled'");
26205 return (vctxt->err);
26206 }
26207 /*
26208 * SPEC (2.1) "If the {content type} is empty, then the
26209 * element information item has no character or element
26210 * information item [children]."
26211 */
26212 if (vctxt->inode->typeDef->contentType ==
26213 XML_SCHEMA_CONTENT_EMPTY) {
26214 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1, NULL,
26215 "Character content is not allowed, "
26216 "because the content type is empty");
26217 return (vctxt->err);
26218 }
26219
26220 if (vctxt->inode->typeDef->contentType ==
26221 XML_SCHEMA_CONTENT_ELEMENTS) {
26222 if ((nodeType != XML_TEXT_NODE) ||
26223 (! xmlSchemaIsBlank((xmlChar *) value, len))) {
26224 /*
26225 * SPEC cvc-complex-type (2.3)
26226 * "If the {content type} is element-only, then the
26227 * element information item has no character information
26228 * item [children] other than those whose [character
26229 * code] is defined as a white space in [XML 1.0 (Second
26230 * Edition)]."
26231 */
26232 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_3, NULL,
26233 "Character content other than whitespace is not allowed "
26234 "because the content type is 'element-only'");
26235 return (vctxt->err);
26236 }
26237 return (0);
26238 }
26239
26240 if ((value == NULL) || (value[0] == 0))
26241 return (0);
26242 /*
26243 * Save the value.
26244 * NOTE that even if the content type is *mixed*, we need the
26245 * *initial value* for default/fixed value constraints.
26246 */
26247 if ((vctxt->inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) &&
26248 ((vctxt->inode->decl == NULL) ||
26249 (vctxt->inode->decl->value == NULL)))
26250 return (0);
26251
26252 if (vctxt->inode->value == NULL) {
26253 /*
26254 * Set the value.
26255 */
26256 switch (mode) {
26257 case XML_SCHEMA_PUSH_TEXT_PERSIST:
26258 /*
26259 * When working on a tree.
26260 */
26261 vctxt->inode->value = value;
26262 break;
26263 case XML_SCHEMA_PUSH_TEXT_CREATED:
26264 /*
26265 * When working with the reader.
26266 * The value will be freed by the element info.
26267 */
26268 vctxt->inode->value = value;
26269 if (consumed != NULL)
26270 *consumed = 1;
26271 vctxt->inode->flags |=
26272 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26273 break;
26274 case XML_SCHEMA_PUSH_TEXT_VOLATILE:
26275 /*
26276 * When working with SAX.
26277 * The value will be freed by the element info.
26278 */
26279 if (len != -1)
26280 vctxt->inode->value = BAD_CAST xmlStrndup(value, len);
26281 else
26282 vctxt->inode->value = BAD_CAST xmlStrdup(value);
26283 vctxt->inode->flags |=
26284 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26285 break;
26286 default:
26287 break;
26288 }
26289 } else {
26290 /*
26291 * Concat the value.
26292 */
26293 if (vctxt->inode->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000026294 vctxt->inode->value = BAD_CAST xmlStrncat(
26295 (xmlChar *) vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026296 } else {
26297 vctxt->inode->value =
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026298 BAD_CAST xmlStrncatNew(vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026299 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26300 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026301 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026302
26303 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000026304}
26305
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026306static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026307xmlSchemaValidateElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000026308{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026309 int ret = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000026310
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026311 if ((vctxt->skipDepth != -1) &&
26312 (vctxt->depth >= vctxt->skipDepth)) {
26313 VERROR_INT("xmlSchemaValidateElem",
26314 "in skip-state");
26315 goto internal_error;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026316 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026317 if (vctxt->xsiAssemble) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026318 /*
26319 * URGENT TODO: Better to fully stop validation
26320 * if there was an error during dynamic schema construction.
26321 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026322 if (xmlSchemaAssembleByXSI(vctxt) == -1)
26323 goto internal_error;
26324 }
26325 if (vctxt->depth > 0) {
26326 /*
26327 * Validate this element against the content model
26328 * of the parent.
26329 */
26330 ret = xmlSchemaValidateChildElem(vctxt);
26331 if (ret != 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026332 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026333 VERROR_INT("xmlSchemaValidateElem",
26334 "calling xmlSchemaStreamValidateChildElement()");
26335 goto internal_error;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026336 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026337 goto exit;
26338 }
26339 if (vctxt->depth == vctxt->skipDepth)
26340 goto exit;
26341 if ((vctxt->inode->decl == NULL) &&
26342 (vctxt->inode->typeDef == NULL)) {
26343 VERROR_INT("xmlSchemaValidateElem",
26344 "the child element was valid but neither the "
26345 "declaration nor the type was set");
26346 goto internal_error;
26347 }
26348 } else {
26349 /*
26350 * Get the declaration of the validation root.
26351 */
26352 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26353 vctxt->inode->localName,
26354 vctxt->inode->nsName);
26355 if (vctxt->inode->decl == NULL) {
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026356 ret = XML_SCHEMAV_CVC_ELT_1;
26357 VERROR(ret, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026358 "No matching global declaration available "
26359 "for the validation root");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026360 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026361 }
26362 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026363
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026364 if (vctxt->inode->decl == NULL)
26365 goto type_validation;
26366
26367 if (vctxt->inode->decl->type == XML_SCHEMA_TYPE_ANY) {
26368 int skip;
26369 /*
26370 * Wildcards.
26371 */
26372 ret = xmlSchemaValidateElemWildcard(vctxt, &skip);
26373 if (ret != 0) {
26374 if (ret < 0) {
26375 VERROR_INT("xmlSchemaValidateElem",
26376 "calling xmlSchemaValidateElemWildcard()");
26377 goto internal_error;
26378 }
26379 goto exit;
26380 }
26381 if (skip) {
26382 vctxt->skipDepth = vctxt->depth;
26383 goto exit;
26384 }
26385 /*
26386 * The declaration might be set by the wildcard validation,
26387 * when the processContents is "lax" or "strict".
26388 */
26389 if (vctxt->inode->decl->type != XML_SCHEMA_TYPE_ELEMENT) {
26390 /*
26391 * Clear the "decl" field to not confuse further processing.
26392 */
26393 vctxt->inode->decl = NULL;
26394 goto type_validation;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026395 }
Daniel Veillard4255d502002-04-16 15:50:10 +000026396 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026397 /*
26398 * Validate against the declaration.
26399 */
26400 ret = xmlSchemaValidateElemDecl(vctxt);
26401 if (ret != 0) {
26402 if (ret < 0) {
26403 VERROR_INT("xmlSchemaValidateElem",
26404 "calling xmlSchemaValidateElemDecl()");
26405 goto internal_error;
26406 }
26407 goto exit;
26408 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026409 /*
26410 * Validate against the type definition.
26411 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026412type_validation:
26413
26414 if (vctxt->inode->typeDef == NULL) {
26415 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26416 ret = XML_SCHEMAV_CVC_TYPE_1;
26417 VERROR(ret, NULL,
26418 "The type definition is absent");
26419 goto exit;
26420 }
26421 if (vctxt->inode->typeDef->flags & XML_SCHEMAS_TYPE_ABSTRACT) {
26422 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26423 ret = XML_SCHEMAV_CVC_TYPE_2;
26424 VERROR(ret, NULL,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026425 "The type definition is abstract");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026426 goto exit;
26427 }
26428 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026429 * Evaluate IDCs. Do it here, since new IDC matchers are registered
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026430 * during validation against the declaration. This must be done
26431 * _before_ attribute validation.
26432 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026433 if (vctxt->xpathStates != NULL) {
26434 ret = xmlSchemaXPathEvaluate(vctxt, XML_ELEMENT_NODE);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000026435 vctxt->inode->appliedXPath = 1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026436 if (ret == -1) {
26437 VERROR_INT("xmlSchemaValidateElem",
26438 "calling xmlSchemaXPathEvaluate()");
26439 goto internal_error;
26440 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026441 }
26442 /*
26443 * Validate attributes.
26444 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026445 if (WXS_IS_COMPLEX(vctxt->inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026446 if ((vctxt->nbAttrInfos != 0) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026447 (vctxt->inode->typeDef->attrUses != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026448
26449 ret = xmlSchemaVAttributesComplex(vctxt);
26450 }
26451 } else if (vctxt->nbAttrInfos != 0) {
26452
26453 ret = xmlSchemaVAttributesSimple(vctxt);
26454 }
26455 /*
26456 * Clear registered attributes.
26457 */
26458 if (vctxt->nbAttrInfos != 0)
26459 xmlSchemaClearAttrInfos(vctxt);
26460 if (ret == -1) {
26461 VERROR_INT("xmlSchemaValidateElem",
26462 "calling attributes validation");
26463 goto internal_error;
26464 }
26465 /*
26466 * Don't return an error if attributes are invalid on purpose.
26467 */
26468 ret = 0;
26469
26470exit:
26471 if (ret != 0)
26472 vctxt->skipDepth = vctxt->depth;
26473 return (ret);
26474internal_error:
26475 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026476}
26477
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026478#ifdef XML_SCHEMA_READER_ENABLED
26479static int
26480xmlSchemaVReaderWalk(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026481{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026482 const int WHTSP = 13, SIGN_WHTSP = 14, END_ELEM = 15;
26483 int depth, nodeType, ret = 0, consumed;
26484 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026485
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026486 vctxt->depth = -1;
26487 ret = xmlTextReaderRead(vctxt->reader);
26488 /*
26489 * Move to the document element.
26490 */
26491 while (ret == 1) {
26492 nodeType = xmlTextReaderNodeType(vctxt->reader);
26493 if (nodeType == XML_ELEMENT_NODE)
26494 goto root_found;
26495 ret = xmlTextReaderRead(vctxt->reader);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026496 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026497 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026498
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026499root_found:
26500
26501 do {
26502 depth = xmlTextReaderDepth(vctxt->reader);
26503 nodeType = xmlTextReaderNodeType(vctxt->reader);
26504
26505 if (nodeType == XML_ELEMENT_NODE) {
26506
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026507 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026508 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26509 VERROR_INT("xmlSchemaVReaderWalk",
26510 "calling xmlSchemaValidatorPushElem()");
26511 goto internal_error;
26512 }
26513 ielem = vctxt->inode;
26514 ielem->localName = xmlTextReaderLocalName(vctxt->reader);
26515 ielem->nsName = xmlTextReaderNamespaceUri(vctxt->reader);
26516 ielem->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
26517 /*
26518 * Is the element empty?
26519 */
26520 ret = xmlTextReaderIsEmptyElement(vctxt->reader);
26521 if (ret == -1) {
26522 VERROR_INT("xmlSchemaVReaderWalk",
26523 "calling xmlTextReaderIsEmptyElement()");
26524 goto internal_error;
26525 }
26526 if (ret) {
26527 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26528 }
26529 /*
26530 * Register attributes.
26531 */
26532 vctxt->nbAttrInfos = 0;
26533 ret = xmlTextReaderMoveToFirstAttribute(vctxt->reader);
26534 if (ret == -1) {
26535 VERROR_INT("xmlSchemaVReaderWalk",
26536 "calling xmlTextReaderMoveToFirstAttribute()");
26537 goto internal_error;
26538 }
26539 if (ret == 1) {
26540 do {
26541 /*
26542 * VAL TODO: How do we know that the reader works on a
26543 * node tree, to be able to pass a node here?
26544 */
26545 if (xmlSchemaValidatorPushAttribute(vctxt, NULL,
26546 (const xmlChar *) xmlTextReaderLocalName(vctxt->reader),
26547 xmlTextReaderNamespaceUri(vctxt->reader), 1,
26548 xmlTextReaderValue(vctxt->reader), 1) == -1) {
26549
26550 VERROR_INT("xmlSchemaVReaderWalk",
26551 "calling xmlSchemaValidatorPushAttribute()");
26552 goto internal_error;
26553 }
26554 ret = xmlTextReaderMoveToNextAttribute(vctxt->reader);
26555 if (ret == -1) {
26556 VERROR_INT("xmlSchemaVReaderWalk",
26557 "calling xmlTextReaderMoveToFirstAttribute()");
26558 goto internal_error;
26559 }
26560 } while (ret == 1);
26561 /*
26562 * Back to element position.
26563 */
26564 ret = xmlTextReaderMoveToElement(vctxt->reader);
26565 if (ret == -1) {
26566 VERROR_INT("xmlSchemaVReaderWalk",
26567 "calling xmlTextReaderMoveToElement()");
26568 goto internal_error;
26569 }
26570 }
26571 /*
26572 * Validate the element.
26573 */
26574 ret= xmlSchemaValidateElem(vctxt);
26575 if (ret != 0) {
26576 if (ret == -1) {
26577 VERROR_INT("xmlSchemaVReaderWalk",
26578 "calling xmlSchemaValidateElem()");
26579 goto internal_error;
26580 }
26581 goto exit;
26582 }
26583 if (vctxt->depth == vctxt->skipDepth) {
26584 int curDepth;
26585 /*
26586 * Skip all content.
26587 */
26588 if ((ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY) == 0) {
26589 ret = xmlTextReaderRead(vctxt->reader);
26590 curDepth = xmlTextReaderDepth(vctxt->reader);
26591 while ((ret == 1) && (curDepth != depth)) {
26592 ret = xmlTextReaderRead(vctxt->reader);
26593 curDepth = xmlTextReaderDepth(vctxt->reader);
26594 }
26595 if (ret < 0) {
26596 /*
26597 * VAL TODO: A reader error occured; what to do here?
26598 */
26599 ret = 1;
26600 goto exit;
26601 }
26602 }
26603 goto leave_elem;
26604 }
26605 /*
26606 * READER VAL TODO: Is an END_ELEM really never called
26607 * if the elem is empty?
26608 */
26609 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26610 goto leave_elem;
26611 } else if (nodeType == END_ELEM) {
26612 /*
26613 * Process END of element.
26614 */
26615leave_elem:
26616 ret = xmlSchemaValidatorPopElem(vctxt);
26617 if (ret != 0) {
26618 if (ret < 0) {
26619 VERROR_INT("xmlSchemaVReaderWalk",
26620 "calling xmlSchemaValidatorPopElem()");
26621 goto internal_error;
26622 }
26623 goto exit;
26624 }
26625 if (vctxt->depth >= 0)
26626 ielem = vctxt->inode;
26627 else
26628 ielem = NULL;
26629 } else if ((nodeType == XML_TEXT_NODE) ||
26630 (nodeType == XML_CDATA_SECTION_NODE) ||
26631 (nodeType == WHTSP) ||
26632 (nodeType == SIGN_WHTSP)) {
26633 /*
26634 * Process character content.
26635 */
26636 xmlChar *value;
26637
26638 if ((nodeType == WHTSP) || (nodeType == SIGN_WHTSP))
26639 nodeType = XML_TEXT_NODE;
26640
26641 value = xmlTextReaderValue(vctxt->reader);
26642 ret = xmlSchemaVPushText(vctxt, nodeType, BAD_CAST value,
26643 -1, XML_SCHEMA_PUSH_TEXT_CREATED, &consumed);
26644 if (! consumed)
26645 xmlFree(value);
26646 if (ret == -1) {
26647 VERROR_INT("xmlSchemaVReaderWalk",
26648 "calling xmlSchemaVPushText()");
26649 goto internal_error;
26650 }
26651 } else if ((nodeType == XML_ENTITY_NODE) ||
26652 (nodeType == XML_ENTITY_REF_NODE)) {
26653 /*
26654 * VAL TODO: What to do with entities?
26655 */
26656 TODO
26657 }
26658 /*
26659 * Read next node.
26660 */
26661 ret = xmlTextReaderRead(vctxt->reader);
26662 } while (ret == 1);
26663
26664exit:
26665 return (ret);
26666internal_error:
26667 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026668}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026669#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000026670
26671/************************************************************************
26672 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026673 * SAX validation handlers *
Daniel Veillard4255d502002-04-16 15:50:10 +000026674 * *
26675 ************************************************************************/
26676
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026677/*
26678* Process text content.
26679*/
26680static void
26681xmlSchemaSAXHandleText(void *ctx,
26682 const xmlChar * ch,
26683 int len)
26684{
26685 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26686
26687 if (vctxt->depth < 0)
26688 return;
26689 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26690 return;
26691 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26692 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26693 if (xmlSchemaVPushText(vctxt, XML_TEXT_NODE, ch, len,
26694 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26695 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26696 "calling xmlSchemaVPushText()");
26697 vctxt->err = -1;
26698 xmlStopParser(vctxt->parserCtxt);
26699 }
26700}
26701
26702/*
26703* Process CDATA content.
26704*/
26705static void
26706xmlSchemaSAXHandleCDataSection(void *ctx,
26707 const xmlChar * ch,
26708 int len)
26709{
26710 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26711
26712 if (vctxt->depth < 0)
26713 return;
26714 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26715 return;
26716 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26717 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26718 if (xmlSchemaVPushText(vctxt, XML_CDATA_SECTION_NODE, ch, len,
26719 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26720 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26721 "calling xmlSchemaVPushText()");
26722 vctxt->err = -1;
26723 xmlStopParser(vctxt->parserCtxt);
26724 }
26725}
26726
26727static void
26728xmlSchemaSAXHandleReference(void *ctx ATTRIBUTE_UNUSED,
26729 const xmlChar * name ATTRIBUTE_UNUSED)
26730{
26731 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26732
26733 if (vctxt->depth < 0)
26734 return;
26735 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26736 return;
26737 /* SAX VAL TODO: What to do here? */
26738 TODO
26739}
26740
26741static void
26742xmlSchemaSAXHandleStartElementNs(void *ctx,
26743 const xmlChar * localname,
26744 const xmlChar * prefix ATTRIBUTE_UNUSED,
26745 const xmlChar * URI,
26746 int nb_namespaces,
26747 const xmlChar ** namespaces,
26748 int nb_attributes,
26749 int nb_defaulted ATTRIBUTE_UNUSED,
26750 const xmlChar ** attributes)
26751{
26752 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26753 int ret;
26754 xmlSchemaNodeInfoPtr ielem;
26755 int i, j;
26756
26757 /*
26758 * SAX VAL TODO: What to do with nb_defaulted?
26759 */
26760 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026761 * Skip elements if inside a "skip" wildcard or invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026762 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026763 vctxt->depth++;
26764 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026765 return;
26766 /*
26767 * Push the element.
26768 */
26769 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26770 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26771 "calling xmlSchemaValidatorPushElem()");
26772 goto internal_error;
26773 }
26774 ielem = vctxt->inode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026775 /*
26776 * TODO: Is this OK?
26777 */
26778 ielem->nodeLine = xmlSAX2GetLineNumber(vctxt->parserCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026779 ielem->localName = localname;
26780 ielem->nsName = URI;
26781 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26782 /*
26783 * Register namespaces on the elem info.
26784 */
26785 if (nb_namespaces != 0) {
26786 /*
26787 * Although the parser builds its own namespace list,
26788 * we have no access to it, so we'll use an own one.
26789 */
26790 for (i = 0, j = 0; i < nb_namespaces; i++, j += 2) {
26791 /*
26792 * Store prefix and namespace name.
26793 */
26794 if (ielem->nsBindings == NULL) {
26795 ielem->nsBindings =
26796 (const xmlChar **) xmlMalloc(10 *
26797 sizeof(const xmlChar *));
26798 if (ielem->nsBindings == NULL) {
26799 xmlSchemaVErrMemory(vctxt,
26800 "allocating namespace bindings for SAX validation",
26801 NULL);
26802 goto internal_error;
26803 }
26804 ielem->nbNsBindings = 0;
26805 ielem->sizeNsBindings = 5;
26806 } else if (ielem->sizeNsBindings <= ielem->nbNsBindings) {
26807 ielem->sizeNsBindings *= 2;
26808 ielem->nsBindings =
26809 (const xmlChar **) xmlRealloc(
26810 (void *) ielem->nsBindings,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026811 ielem->sizeNsBindings * 2 * sizeof(const xmlChar *));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026812 if (ielem->nsBindings == NULL) {
26813 xmlSchemaVErrMemory(vctxt,
26814 "re-allocating namespace bindings for SAX validation",
26815 NULL);
26816 goto internal_error;
26817 }
26818 }
26819
26820 ielem->nsBindings[ielem->nbNsBindings * 2] = namespaces[j];
26821 if (namespaces[j+1][0] == 0) {
26822 /*
26823 * Handle xmlns="".
26824 */
26825 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] = NULL;
26826 } else
26827 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] =
26828 namespaces[j+1];
26829 ielem->nbNsBindings++;
26830 }
26831 }
26832 /*
26833 * Register attributes.
26834 * SAX VAL TODO: We are not adding namespace declaration
26835 * attributes yet.
26836 */
26837 if (nb_attributes != 0) {
26838 xmlChar *value;
26839
26840 for (j = 0, i = 0; i < nb_attributes; i++, j += 5) {
26841 /*
26842 * Duplicate the value.
26843 */
26844 value = xmlStrndup(attributes[j+3],
26845 attributes[j+4] - attributes[j+3]);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026846 /*
26847 * TODO: Set the node line.
26848 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026849 ret = xmlSchemaValidatorPushAttribute(vctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026850 NULL, ielem->nodeLine, attributes[j], attributes[j+2], 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026851 value, 1);
26852 if (ret == -1) {
26853 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26854 "calling xmlSchemaValidatorPushAttribute()");
26855 goto internal_error;
26856 }
26857 }
26858 }
26859 /*
26860 * Validate the element.
26861 */
26862 ret = xmlSchemaValidateElem(vctxt);
26863 if (ret != 0) {
26864 if (ret == -1) {
26865 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26866 "calling xmlSchemaValidateElem()");
26867 goto internal_error;
26868 }
26869 goto exit;
26870 }
26871
26872exit:
26873 return;
26874internal_error:
26875 vctxt->err = -1;
26876 xmlStopParser(vctxt->parserCtxt);
26877 return;
26878}
26879
26880static void
26881xmlSchemaSAXHandleEndElementNs(void *ctx,
26882 const xmlChar * localname ATTRIBUTE_UNUSED,
26883 const xmlChar * prefix ATTRIBUTE_UNUSED,
26884 const xmlChar * URI ATTRIBUTE_UNUSED)
26885{
26886 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26887 int res;
26888
26889 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026890 * Skip elements if inside a "skip" wildcard or if invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026891 */
26892 if (vctxt->skipDepth != -1) {
26893 if (vctxt->depth > vctxt->skipDepth) {
26894 vctxt->depth--;
26895 return;
26896 } else
26897 vctxt->skipDepth = -1;
26898 }
26899 /*
26900 * SAX VAL TODO: Just a temporary check.
26901 */
26902 if ((!xmlStrEqual(vctxt->inode->localName, localname)) ||
26903 (!xmlStrEqual(vctxt->inode->nsName, URI))) {
26904 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
26905 "elem pop mismatch");
26906 }
26907 res = xmlSchemaValidatorPopElem(vctxt);
26908 if (res != 0) {
26909 if (res < 0) {
26910 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
26911 "calling xmlSchemaValidatorPopElem()");
26912 goto internal_error;
26913 }
26914 goto exit;
26915 }
26916exit:
26917 return;
26918internal_error:
26919 vctxt->err = -1;
26920 xmlStopParser(vctxt->parserCtxt);
26921 return;
26922}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026923
Daniel Veillard4255d502002-04-16 15:50:10 +000026924/************************************************************************
26925 * *
26926 * Validation interfaces *
26927 * *
26928 ************************************************************************/
26929
26930/**
26931 * xmlSchemaNewValidCtxt:
26932 * @schema: a precompiled XML Schemas
26933 *
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026934 * Create an XML Schemas validation context based on the given schema.
Daniel Veillard4255d502002-04-16 15:50:10 +000026935 *
26936 * Returns the validation context or NULL in case of error
26937 */
26938xmlSchemaValidCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000026939xmlSchemaNewValidCtxt(xmlSchemaPtr schema)
26940{
Daniel Veillard4255d502002-04-16 15:50:10 +000026941 xmlSchemaValidCtxtPtr ret;
26942
26943 ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt));
26944 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000026945 xmlSchemaVErrMemory(NULL, "allocating validation context", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000026946 return (NULL);
26947 }
26948 memset(ret, 0, sizeof(xmlSchemaValidCtxt));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026949 ret->type = XML_SCHEMA_CTXT_VALIDATOR;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026950 ret->dict = xmlDictCreate();
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000026951 ret->nodeQNames = xmlSchemaItemListCreate();
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026952 ret->schema = schema;
Daniel Veillard4255d502002-04-16 15:50:10 +000026953 return (ret);
26954}
26955
26956/**
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026957 * xmlSchemaClearValidCtxt:
26958 * @ctxt: the schema validation context
26959 *
26960 * Free the resources associated to the schema validation context;
26961 * leaves some fields alive intended for reuse of the context.
26962 */
26963static void
26964xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt)
26965{
26966 if (vctxt == NULL)
26967 return;
26968
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026969 /*
26970 * TODO: Should we clear the flags?
26971 * Might be problematic if one reuses the context
26972 * and assumes that the options remain the same.
26973 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026974 vctxt->flags = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026975 vctxt->validationRoot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026976 vctxt->doc = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000026977#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026978 vctxt->reader = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000026979#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000026980 vctxt->hasKeyrefs = 0;
26981
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026982 if (vctxt->value != NULL) {
26983 xmlSchemaFreeValue(vctxt->value);
26984 vctxt->value = NULL;
26985 }
26986 /*
26987 * Augmented IDC information.
26988 */
26989 if (vctxt->aidcs != NULL) {
26990 xmlSchemaIDCAugPtr cur = vctxt->aidcs, next;
26991 do {
26992 next = cur->next;
26993 xmlFree(cur);
26994 cur = next;
26995 } while (cur != NULL);
26996 vctxt->aidcs = NULL;
26997 }
26998 if (vctxt->idcNodes != NULL) {
26999 int i;
27000 xmlSchemaPSVIIDCNodePtr item;
27001
27002 for (i = 0; i < vctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027003 item = vctxt->idcNodes[i];
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027004 xmlFree(item->keys);
27005 xmlFree(item);
27006 }
27007 xmlFree(vctxt->idcNodes);
27008 vctxt->idcNodes = NULL;
27009 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027010 /*
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027011 * Note that we won't delete the XPath state pool here.
27012 */
27013 if (vctxt->xpathStates != NULL) {
27014 xmlSchemaFreeIDCStateObjList(vctxt->xpathStates);
27015 vctxt->xpathStates = NULL;
27016 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027017 /*
27018 * Attribute info.
27019 */
27020 if (vctxt->nbAttrInfos != 0) {
27021 xmlSchemaClearAttrInfos(vctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027022 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027023 /*
27024 * Element info.
27025 */
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027026 if (vctxt->elemInfos != NULL) {
27027 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027028 xmlSchemaNodeInfoPtr ei;
27029
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027030 for (i = 0; i < vctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027031 ei = vctxt->elemInfos[i];
27032 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027033 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027034 xmlSchemaClearElemInfo(ei);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027035 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027036 }
27037 xmlSchemaItemListClear(vctxt->nodeQNames);
27038 /* Recreate the dict. */
27039 xmlDictFree(vctxt->dict);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027040 /*
27041 * TODO: Is is save to recreate it? Do we have a scenario
27042 * where the user provides the dict?
27043 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027044 vctxt->dict = xmlDictCreate();
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027045}
27046
27047/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027048 * xmlSchemaFreeValidCtxt:
27049 * @ctxt: the schema validation context
27050 *
27051 * Free the resources associated to the schema validation context
27052 */
27053void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027054xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt)
27055{
Daniel Veillard4255d502002-04-16 15:50:10 +000027056 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027057 return;
Daniel Veillard88c58912002-04-23 07:12:20 +000027058 if (ctxt->value != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027059 xmlSchemaFreeValue(ctxt->value);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027060 if (ctxt->pctxt != NULL)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027061 xmlSchemaFreeParserCtxt(ctxt->pctxt);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027062 if (ctxt->idcNodes != NULL) {
27063 int i;
27064 xmlSchemaPSVIIDCNodePtr item;
27065
27066 for (i = 0; i < ctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027067 item = ctxt->idcNodes[i];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027068 xmlFree(item->keys);
27069 xmlFree(item);
27070 }
27071 xmlFree(ctxt->idcNodes);
27072 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027073 if (ctxt->idcKeys != NULL) {
27074 int i;
27075 for (i = 0; i < ctxt->nbIdcKeys; i++)
27076 xmlSchemaIDCFreeKey(ctxt->idcKeys[i]);
27077 xmlFree(ctxt->idcKeys);
27078 }
27079
27080 if (ctxt->xpathStates != NULL)
27081 xmlSchemaFreeIDCStateObjList(ctxt->xpathStates);
27082 if (ctxt->xpathStatePool != NULL)
27083 xmlSchemaFreeIDCStateObjList(ctxt->xpathStatePool);
27084
27085 /*
27086 * Augmented IDC information.
27087 */
27088 if (ctxt->aidcs != NULL) {
27089 xmlSchemaIDCAugPtr cur = ctxt->aidcs, next;
27090 do {
27091 next = cur->next;
27092 xmlFree(cur);
27093 cur = next;
27094 } while (cur != NULL);
27095 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027096 if (ctxt->attrInfos != NULL) {
27097 int i;
27098 xmlSchemaAttrInfoPtr attr;
27099
27100 /* Just a paranoid call to the cleanup. */
27101 if (ctxt->nbAttrInfos != 0)
27102 xmlSchemaClearAttrInfos(ctxt);
27103 for (i = 0; i < ctxt->sizeAttrInfos; i++) {
27104 attr = ctxt->attrInfos[i];
27105 xmlFree(attr);
27106 }
27107 xmlFree(ctxt->attrInfos);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000027108 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027109 if (ctxt->elemInfos != NULL) {
27110 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027111 xmlSchemaNodeInfoPtr ei;
27112
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027113 for (i = 0; i < ctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027114 ei = ctxt->elemInfos[i];
27115 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027116 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027117 xmlSchemaClearElemInfo(ei);
27118 xmlFree(ei);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027119 }
27120 xmlFree(ctxt->elemInfos);
27121 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027122 if (ctxt->nodeQNames != NULL)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027123 xmlSchemaItemListFree(ctxt->nodeQNames);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027124 if (ctxt->dict != NULL)
27125 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000027126 xmlFree(ctxt);
27127}
27128
27129/**
Daniel Veillardf10ae122005-07-10 19:03:16 +000027130 * xmlSchemaIsValid:
27131 * @ctxt: the schema validation context
27132 *
27133 * Check if any error was detected during validation.
27134 *
27135 * Returns 1 if valid so far, 0 if errors were detected, and -1 in case
27136 * of internal error.
27137 */
27138int
27139xmlSchemaIsValid(xmlSchemaValidCtxtPtr ctxt)
27140{
27141 if (ctxt == NULL)
27142 return(-1);
27143 return(ctxt->err == 0);
27144}
27145
27146/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027147 * xmlSchemaSetValidErrors:
27148 * @ctxt: a schema validation context
27149 * @err: the error function
27150 * @warn: the warning function
Daniel Veillarda9b66d02002-12-11 14:23:49 +000027151 * @ctx: the functions context
Daniel Veillard4255d502002-04-16 15:50:10 +000027152 *
William M. Brack2f2a6632004-08-20 23:09:47 +000027153 * Set the error and warning callback informations
Daniel Veillard4255d502002-04-16 15:50:10 +000027154 */
27155void
27156xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027157 xmlSchemaValidityErrorFunc err,
27158 xmlSchemaValidityWarningFunc warn, void *ctx)
27159{
Daniel Veillard4255d502002-04-16 15:50:10 +000027160 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027161 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000027162 ctxt->error = err;
27163 ctxt->warning = warn;
27164 ctxt->userData = ctx;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027165 if (ctxt->pctxt != NULL)
27166 xmlSchemaSetParserErrors(ctxt->pctxt, err, warn, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000027167}
27168
27169/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027170 * xmlSchemaSetValidStructuredErrors:
27171 * @ctxt: a schema validation context
27172 * @serror: the structured error function
27173 * @ctx: the functions context
27174 *
27175 * Set the structured error callback
27176 */
27177void
27178xmlSchemaSetValidStructuredErrors(xmlSchemaValidCtxtPtr ctxt,
27179 xmlStructuredErrorFunc serror, void *ctx)
27180{
27181 if (ctxt == NULL)
27182 return;
27183 ctxt->serror = serror;
27184 ctxt->error = NULL;
27185 ctxt->warning = NULL;
27186 ctxt->userData = ctx;
27187}
27188
27189/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000027190 * xmlSchemaGetValidErrors:
27191 * @ctxt: a XML-Schema validation context
27192 * @err: the error function result
27193 * @warn: the warning function result
27194 * @ctx: the functions context result
27195 *
27196 * Get the error and warning callback informations
27197 *
27198 * Returns -1 in case of error and 0 otherwise
27199 */
27200int
27201xmlSchemaGetValidErrors(xmlSchemaValidCtxtPtr ctxt,
27202 xmlSchemaValidityErrorFunc * err,
27203 xmlSchemaValidityWarningFunc * warn, void **ctx)
27204{
27205 if (ctxt == NULL)
27206 return (-1);
27207 if (err != NULL)
27208 *err = ctxt->error;
27209 if (warn != NULL)
27210 *warn = ctxt->warning;
27211 if (ctx != NULL)
27212 *ctx = ctxt->userData;
27213 return (0);
27214}
27215
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027216
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027217/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027218 * xmlSchemaSetValidOptions:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027219 * @ctxt: a schema validation context
27220 * @options: a combination of xmlSchemaValidOption
27221 *
27222 * Sets the options to be used during the validation.
27223 *
27224 * Returns 0 in case of success, -1 in case of an
27225 * API error.
27226 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027227int
27228xmlSchemaSetValidOptions(xmlSchemaValidCtxtPtr ctxt,
27229 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027230
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027231{
27232 int i;
27233
27234 if (ctxt == NULL)
27235 return (-1);
27236 /*
27237 * WARNING: Change the start value if adding to the
27238 * xmlSchemaValidOption.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027239 * TODO: Is there an other, more easy to maintain,
27240 * way?
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027241 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027242 for (i = 1; i < (int) sizeof(int) * 8; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027243 if (options & 1<<i)
27244 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027245 }
27246 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027247 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027248}
27249
27250/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027251 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027252 * @ctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027253 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027254 * Get the validation context options.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027255 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027256 * Returns the option combination or -1 on error.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027257 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027258int
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027259xmlSchemaValidCtxtGetOptions(xmlSchemaValidCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027260
27261{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027262 if (ctxt == NULL)
27263 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027264 else
27265 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027266}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027267
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027268static int
27269xmlSchemaVDocWalk(xmlSchemaValidCtxtPtr vctxt)
27270{
27271 xmlAttrPtr attr;
27272 int ret = 0;
27273 xmlSchemaNodeInfoPtr ielem = NULL;
27274 xmlNodePtr node, valRoot;
27275 const xmlChar *nsName;
27276
27277 /* DOC VAL TODO: Move this to the start function. */
27278 valRoot = xmlDocGetRootElement(vctxt->doc);
27279 if (valRoot == NULL) {
27280 /* VAL TODO: Error code? */
27281 VERROR(1, NULL, "The document has no document element");
27282 return (1);
27283 }
27284 vctxt->depth = -1;
27285 vctxt->validationRoot = valRoot;
27286 node = valRoot;
27287 while (node != NULL) {
27288 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27289 goto next_sibling;
27290 if (node->type == XML_ELEMENT_NODE) {
27291
27292 /*
27293 * Init the node-info.
27294 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027295 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027296 if (xmlSchemaValidatorPushElem(vctxt) == -1)
27297 goto internal_error;
27298 ielem = vctxt->inode;
27299 ielem->node = node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027300 ielem->nodeLine = node->line;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027301 ielem->localName = node->name;
27302 if (node->ns != NULL)
27303 ielem->nsName = node->ns->href;
27304 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
27305 /*
27306 * Register attributes.
27307 * DOC VAL TODO: We do not register namespace declaration
27308 * attributes yet.
27309 */
27310 vctxt->nbAttrInfos = 0;
27311 if (node->properties != NULL) {
27312 attr = node->properties;
27313 do {
27314 if (attr->ns != NULL)
27315 nsName = attr->ns->href;
27316 else
27317 nsName = NULL;
27318 ret = xmlSchemaValidatorPushAttribute(vctxt,
27319 (xmlNodePtr) attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027320 /*
27321 * Note that we give it the line number of the
27322 * parent element.
27323 */
27324 ielem->nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027325 attr->name, nsName, 0,
27326 xmlNodeListGetString(attr->doc, attr->children, 1), 1);
27327 if (ret == -1) {
27328 VERROR_INT("xmlSchemaDocWalk",
27329 "calling xmlSchemaValidatorPushAttribute()");
27330 goto internal_error;
27331 }
27332 attr = attr->next;
27333 } while (attr);
27334 }
27335 /*
27336 * Validate the element.
27337 */
27338 ret = xmlSchemaValidateElem(vctxt);
27339 if (ret != 0) {
27340 if (ret == -1) {
27341 VERROR_INT("xmlSchemaDocWalk",
27342 "calling xmlSchemaValidateElem()");
27343 goto internal_error;
27344 }
27345 /*
27346 * Don't stop validation; just skip the content
27347 * of this element.
27348 */
27349 goto leave_node;
27350 }
27351 if ((vctxt->skipDepth != -1) &&
27352 (vctxt->depth >= vctxt->skipDepth))
27353 goto leave_node;
27354 } else if ((node->type == XML_TEXT_NODE) ||
27355 (node->type == XML_CDATA_SECTION_NODE)) {
27356 /*
27357 * Process character content.
27358 */
27359 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
27360 ielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27361 ret = xmlSchemaVPushText(vctxt, node->type, node->content,
27362 -1, XML_SCHEMA_PUSH_TEXT_PERSIST, NULL);
27363 if (ret < 0) {
27364 VERROR_INT("xmlSchemaVDocWalk",
27365 "calling xmlSchemaVPushText()");
27366 goto internal_error;
27367 }
27368 /*
27369 * DOC VAL TODO: Should we skip further validation of the
27370 * element content here?
27371 */
27372 } else if ((node->type == XML_ENTITY_NODE) ||
27373 (node->type == XML_ENTITY_REF_NODE)) {
27374 /*
27375 * DOC VAL TODO: What to do with entities?
27376 */
27377 TODO
27378 } else {
27379 goto leave_node;
27380 /*
27381 * DOC VAL TODO: XInclude nodes, etc.
27382 */
27383 }
27384 /*
27385 * Walk the doc.
27386 */
27387 if (node->children != NULL) {
27388 node = node->children;
27389 continue;
27390 }
27391leave_node:
27392 if (node->type == XML_ELEMENT_NODE) {
27393 /*
27394 * Leaving the scope of an element.
27395 */
27396 if (node != vctxt->inode->node) {
27397 VERROR_INT("xmlSchemaVDocWalk",
27398 "element position mismatch");
27399 goto internal_error;
27400 }
27401 ret = xmlSchemaValidatorPopElem(vctxt);
27402 if (ret != 0) {
27403 if (ret < 0) {
27404 VERROR_INT("xmlSchemaVDocWalk",
27405 "calling xmlSchemaValidatorPopElem()");
27406 goto internal_error;
27407 }
27408 }
27409 if (node == valRoot)
27410 goto exit;
27411 }
27412next_sibling:
27413 if (node->next != NULL)
27414 node = node->next;
27415 else {
27416 node = node->parent;
27417 goto leave_node;
27418 }
27419 }
27420
27421exit:
27422 return (ret);
27423internal_error:
27424 return (-1);
27425}
27426
27427static int
Daniel Veillardf10ae122005-07-10 19:03:16 +000027428xmlSchemaPreRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027429 /*
27430 * Some initialization.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027431 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027432 vctxt->err = 0;
27433 vctxt->nberrors = 0;
27434 vctxt->depth = -1;
27435 vctxt->skipDepth = -1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027436 vctxt->xsiAssemble = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027437 vctxt->hasKeyrefs = 0;
27438#ifdef ENABLE_IDC_NODE_TABLES
27439 vctxt->createIDCNodeTables = 1;
27440#else
27441 vctxt->createIDCNodeTables = 0;
27442#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027443 /*
27444 * Create a schema + parser if necessary.
27445 */
27446 if (vctxt->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027447 xmlSchemaParserCtxtPtr pctxt;
27448
27449 vctxt->xsiAssemble = 1;
27450 /*
27451 * If not schema was given then we will create a schema
27452 * dynamically using XSI schema locations.
27453 *
27454 * Create the schema parser context.
27455 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027456 if ((vctxt->pctxt == NULL) &&
27457 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
27458 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027459 pctxt = vctxt->pctxt;
27460 pctxt->xsiAssemble = 1;
27461 /*
27462 * Create the schema.
27463 */
27464 vctxt->schema = xmlSchemaNewSchema(pctxt);
27465 if (vctxt->schema == NULL)
27466 return (-1);
27467 /*
27468 * Create the schema construction context.
27469 */
27470 pctxt->constructor = xmlSchemaConstructionCtxtCreate(pctxt->dict);
27471 if (pctxt->constructor == NULL)
27472 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000027473 pctxt->constructor->mainSchema = vctxt->schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027474 /*
27475 * Take ownership of the constructor to be able to free it.
27476 */
27477 pctxt->ownsConstructor = 1;
27478 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027479 /*
27480 * Augment the IDC definitions.
27481 */
27482 if (vctxt->schema->idcDef != NULL) {
27483 xmlHashScan(vctxt->schema->idcDef,
27484 (xmlHashScanner) xmlSchemaAugmentIDC, vctxt);
27485 }
Daniel Veillardf10ae122005-07-10 19:03:16 +000027486 return(0);
27487}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027488
Daniel Veillardf10ae122005-07-10 19:03:16 +000027489static void
27490xmlSchemaPostRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027491 if (vctxt->xsiAssemble) {
27492 if (vctxt->schema != NULL) {
27493 xmlSchemaFree(vctxt->schema);
27494 vctxt->schema = NULL;
27495 }
27496 }
27497 xmlSchemaClearValidCtxt(vctxt);
Daniel Veillardf10ae122005-07-10 19:03:16 +000027498}
27499
27500static int
27501xmlSchemaVStart(xmlSchemaValidCtxtPtr vctxt)
27502{
27503 int ret = 0;
27504
27505 if (xmlSchemaPreRun(vctxt) < 0)
27506 return(-1);
27507
27508 if (vctxt->doc != NULL) {
27509 /*
27510 * Tree validation.
27511 */
27512 ret = xmlSchemaVDocWalk(vctxt);
27513#ifdef LIBXML_READER_ENABLED
27514 } else if (vctxt->reader != NULL) {
27515 /*
27516 * XML Reader validation.
27517 */
27518#ifdef XML_SCHEMA_READER_ENABLED
27519 ret = xmlSchemaVReaderWalk(vctxt);
27520#endif
27521#endif
27522 } else if ((vctxt->sax != NULL) && (vctxt->parserCtxt != NULL)) {
27523 /*
27524 * SAX validation.
27525 */
27526 ret = xmlParseDocument(vctxt->parserCtxt);
27527 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027528 VERROR_INT("xmlSchemaVStart",
Daniel Veillardf10ae122005-07-10 19:03:16 +000027529 "no instance to validate");
27530 ret = -1;
27531 }
27532
27533 xmlSchemaPostRun(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027534 if (ret == 0)
27535 ret = vctxt->err;
27536 return (ret);
27537}
27538
27539/**
27540 * xmlSchemaValidateOneElement:
27541 * @ctxt: a schema validation context
27542 * @elem: an element node
27543 *
27544 * Validate a branch of a tree, starting with the given @elem.
27545 *
27546 * Returns 0 if the element and its subtree is valid, a positive error
27547 * code number otherwise and -1 in case of an internal or API error.
27548 */
27549int
27550xmlSchemaValidateOneElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem)
27551{
27552 if ((ctxt == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
27553 return (-1);
27554
27555 if (ctxt->schema == NULL)
27556 return (-1);
27557
27558 ctxt->doc = elem->doc;
27559 ctxt->node = elem;
27560 ctxt->validationRoot = elem;
27561 return(xmlSchemaVStart(ctxt));
27562}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027563
Daniel Veillard259f0df2004-08-18 09:13:18 +000027564/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027565 * xmlSchemaValidateDoc:
27566 * @ctxt: a schema validation context
27567 * @doc: a parsed document tree
27568 *
27569 * Validate a document tree in memory.
27570 *
27571 * Returns 0 if the document is schemas valid, a positive error code
27572 * number otherwise and -1 in case of internal or API error.
27573 */
27574int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027575xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc)
27576{
Daniel Veillard4255d502002-04-16 15:50:10 +000027577 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027578 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000027579
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027580 ctxt->doc = doc;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027581 ctxt->node = xmlDocGetRootElement(doc);
27582 if (ctxt->node == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027583 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027584 XML_SCHEMAV_DOCUMENT_ELEMENT_MISSING,
27585 (xmlNodePtr) doc, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027586 "The document has no document element", NULL, NULL);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027587 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027588 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027589 ctxt->validationRoot = ctxt->node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027590 return (xmlSchemaVStart(ctxt));
Daniel Veillard4255d502002-04-16 15:50:10 +000027591}
27592
Daniel Veillardcdc82732005-07-08 15:04:06 +000027593
27594/************************************************************************
27595 * *
27596 * Function and data for SAX streaming API *
27597 * *
27598 ************************************************************************/
27599typedef struct _xmlSchemaSplitSAXData xmlSchemaSplitSAXData;
27600typedef xmlSchemaSplitSAXData *xmlSchemaSplitSAXDataPtr;
27601
27602struct _xmlSchemaSplitSAXData {
27603 xmlSAXHandlerPtr user_sax;
27604 void *user_data;
27605 xmlSchemaValidCtxtPtr ctxt;
27606 xmlSAXHandlerPtr schemas_sax;
27607};
27608
Daniel Veillard971771e2005-07-09 17:32:57 +000027609#define XML_SAX_PLUG_MAGIC 0xdc43ba21
27610
27611struct _xmlSchemaSAXPlug {
27612 unsigned int magic;
27613
27614 /* the original callbacks informations */
27615 xmlSAXHandlerPtr *user_sax_ptr;
27616 xmlSAXHandlerPtr user_sax;
27617 void **user_data_ptr;
27618 void *user_data;
27619
27620 /* the block plugged back and validation informations */
27621 xmlSAXHandler schemas_sax;
27622 xmlSchemaValidCtxtPtr ctxt;
27623};
27624
Daniel Veillardcdc82732005-07-08 15:04:06 +000027625/* All those functions just bounces to the user provided SAX handlers */
27626static void
27627internalSubsetSplit(void *ctx, const xmlChar *name,
27628 const xmlChar *ExternalID, const xmlChar *SystemID)
27629{
Daniel Veillard971771e2005-07-09 17:32:57 +000027630 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027631 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27632 (ctxt->user_sax->internalSubset != NULL))
27633 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27634 SystemID);
27635}
27636
27637static int
27638isStandaloneSplit(void *ctx)
27639{
Daniel Veillard971771e2005-07-09 17:32:57 +000027640 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027641 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27642 (ctxt->user_sax->isStandalone != NULL))
27643 return(ctxt->user_sax->isStandalone(ctxt->user_data));
27644 return(0);
27645}
27646
27647static int
27648hasInternalSubsetSplit(void *ctx)
27649{
Daniel Veillard971771e2005-07-09 17:32:57 +000027650 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027651 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27652 (ctxt->user_sax->hasInternalSubset != NULL))
27653 return(ctxt->user_sax->hasInternalSubset(ctxt->user_data));
27654 return(0);
27655}
27656
27657static int
27658hasExternalSubsetSplit(void *ctx)
27659{
Daniel Veillard971771e2005-07-09 17:32:57 +000027660 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027661 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27662 (ctxt->user_sax->hasExternalSubset != NULL))
27663 return(ctxt->user_sax->hasExternalSubset(ctxt->user_data));
27664 return(0);
27665}
27666
27667static void
27668externalSubsetSplit(void *ctx, const xmlChar *name,
27669 const xmlChar *ExternalID, const xmlChar *SystemID)
27670{
Daniel Veillard971771e2005-07-09 17:32:57 +000027671 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027672 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27673 (ctxt->user_sax->internalSubset != NULL))
27674 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27675 SystemID);
27676}
27677
27678static xmlParserInputPtr
27679resolveEntitySplit(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
27680{
Daniel Veillard971771e2005-07-09 17:32:57 +000027681 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027682 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27683 (ctxt->user_sax->resolveEntity != NULL))
27684 return(ctxt->user_sax->resolveEntity(ctxt->user_data, publicId,
27685 systemId));
27686 return(NULL);
27687}
27688
27689static xmlEntityPtr
27690getEntitySplit(void *ctx, const xmlChar *name)
27691{
Daniel Veillard971771e2005-07-09 17:32:57 +000027692 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027693 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27694 (ctxt->user_sax->getEntity != NULL))
27695 return(ctxt->user_sax->getEntity(ctxt->user_data, name));
27696 return(NULL);
27697}
27698
27699static xmlEntityPtr
27700getParameterEntitySplit(void *ctx, const xmlChar *name)
27701{
Daniel Veillard971771e2005-07-09 17:32:57 +000027702 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027703 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27704 (ctxt->user_sax->getParameterEntity != NULL))
27705 return(ctxt->user_sax->getParameterEntity(ctxt->user_data, name));
27706 return(NULL);
27707}
27708
27709
27710static void
27711entityDeclSplit(void *ctx, const xmlChar *name, int type,
27712 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
27713{
Daniel Veillard971771e2005-07-09 17:32:57 +000027714 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027715 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27716 (ctxt->user_sax->entityDecl != NULL))
27717 ctxt->user_sax->entityDecl(ctxt->user_data, name, type, publicId,
27718 systemId, content);
27719}
27720
27721static void
27722attributeDeclSplit(void *ctx, const xmlChar * elem,
27723 const xmlChar * name, int type, int def,
27724 const xmlChar * defaultValue, xmlEnumerationPtr tree)
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->attributeDecl != NULL)) {
27729 ctxt->user_sax->attributeDecl(ctxt->user_data, elem, name, type,
27730 def, defaultValue, tree);
27731 } else {
27732 xmlFreeEnumeration(tree);
27733 }
27734}
27735
27736static void
27737elementDeclSplit(void *ctx, const xmlChar *name, int type,
27738 xmlElementContentPtr content)
27739{
Daniel Veillard971771e2005-07-09 17:32:57 +000027740 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027741 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27742 (ctxt->user_sax->elementDecl != NULL))
27743 ctxt->user_sax->elementDecl(ctxt->user_data, name, type, content);
27744}
27745
27746static void
27747notationDeclSplit(void *ctx, const xmlChar *name,
27748 const xmlChar *publicId, const xmlChar *systemId)
27749{
Daniel Veillard971771e2005-07-09 17:32:57 +000027750 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027751 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27752 (ctxt->user_sax->notationDecl != NULL))
27753 ctxt->user_sax->notationDecl(ctxt->user_data, name, publicId,
27754 systemId);
27755}
27756
27757static void
27758unparsedEntityDeclSplit(void *ctx, const xmlChar *name,
27759 const xmlChar *publicId, const xmlChar *systemId,
27760 const xmlChar *notationName)
27761{
Daniel Veillard971771e2005-07-09 17:32:57 +000027762 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027763 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27764 (ctxt->user_sax->unparsedEntityDecl != NULL))
27765 ctxt->user_sax->unparsedEntityDecl(ctxt->user_data, name, publicId,
27766 systemId, notationName);
27767}
27768
27769static void
27770setDocumentLocatorSplit(void *ctx, xmlSAXLocatorPtr loc)
27771{
Daniel Veillard971771e2005-07-09 17:32:57 +000027772 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027773 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27774 (ctxt->user_sax->setDocumentLocator != NULL))
27775 ctxt->user_sax->setDocumentLocator(ctxt->user_data, loc);
27776}
27777
27778static void
27779startDocumentSplit(void *ctx)
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->startDocument != NULL))
27784 ctxt->user_sax->startDocument(ctxt->user_data);
27785}
27786
27787static void
27788endDocumentSplit(void *ctx)
27789{
Daniel Veillard971771e2005-07-09 17:32:57 +000027790 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027791 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27792 (ctxt->user_sax->endDocument != NULL))
27793 ctxt->user_sax->endDocument(ctxt->user_data);
27794}
27795
27796static void
27797processingInstructionSplit(void *ctx, const xmlChar *target,
27798 const xmlChar *data)
27799{
Daniel Veillard971771e2005-07-09 17:32:57 +000027800 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027801 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27802 (ctxt->user_sax->processingInstruction != NULL))
27803 ctxt->user_sax->processingInstruction(ctxt->user_data, target, data);
27804}
27805
27806static void
27807commentSplit(void *ctx, const xmlChar *value)
27808{
Daniel Veillard971771e2005-07-09 17:32:57 +000027809 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027810 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27811 (ctxt->user_sax->comment != NULL))
27812 ctxt->user_sax->comment(ctxt->user_data, value);
27813}
27814
27815/*
27816 * Varargs error callbacks to the user application, harder ...
27817 */
27818
Daniel Veillardffa3c742005-07-21 13:24:09 +000027819static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027820warningSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027821 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027822 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27823 (ctxt->user_sax->warning != NULL)) {
27824 TODO
27825 }
27826}
Daniel Veillardffa3c742005-07-21 13:24:09 +000027827static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027828errorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027829 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027830 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27831 (ctxt->user_sax->error != NULL)) {
27832 TODO
27833 }
27834}
Daniel Veillardffa3c742005-07-21 13:24:09 +000027835static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027836fatalErrorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027837 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027838 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27839 (ctxt->user_sax->fatalError != NULL)) {
27840 TODO
27841 }
27842}
27843
27844/*
27845 * Those are function where both the user handler and the schemas handler
27846 * need to be called.
27847 */
27848static void
27849charactersSplit(void *ctx, const xmlChar *ch, int len)
27850{
Daniel Veillard971771e2005-07-09 17:32:57 +000027851 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027852 if (ctxt == NULL)
27853 return;
27854 if ((ctxt->user_sax != NULL) && (ctxt->user_sax->characters != NULL))
27855 ctxt->user_sax->characters(ctxt->user_data, ch, len);
27856 if (ctxt->ctxt != NULL)
27857 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
27858}
27859
27860static void
27861ignorableWhitespaceSplit(void *ctx, const xmlChar *ch, int len)
27862{
Daniel Veillard971771e2005-07-09 17:32:57 +000027863 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027864 if (ctxt == NULL)
27865 return;
27866 if ((ctxt->user_sax != NULL) &&
27867 (ctxt->user_sax->ignorableWhitespace != NULL))
27868 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, ch, len);
27869 if (ctxt->ctxt != NULL)
27870 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
27871}
27872
27873static void
27874cdataBlockSplit(void *ctx, const xmlChar *value, int len)
27875{
Daniel Veillard971771e2005-07-09 17:32:57 +000027876 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027877 if (ctxt == NULL)
27878 return;
27879 if ((ctxt->user_sax != NULL) &&
27880 (ctxt->user_sax->ignorableWhitespace != NULL))
27881 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, value, len);
27882 if (ctxt->ctxt != NULL)
27883 xmlSchemaSAXHandleCDataSection(ctxt->ctxt, value, len);
27884}
27885
27886static void
27887referenceSplit(void *ctx, const xmlChar *name)
27888{
Daniel Veillard971771e2005-07-09 17:32:57 +000027889 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027890 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27891 (ctxt->user_sax->reference != NULL))
27892 ctxt->user_sax->reference(ctxt->user_data, name);
27893 if (ctxt->ctxt != NULL)
27894 xmlSchemaSAXHandleReference(ctxt->user_data, name);
27895}
27896
27897static void
27898startElementNsSplit(void *ctx, const xmlChar * localname,
27899 const xmlChar * prefix, const xmlChar * URI,
27900 int nb_namespaces, const xmlChar ** namespaces,
27901 int nb_attributes, int nb_defaulted,
27902 const xmlChar ** attributes) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027903 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027904 if (ctxt == NULL)
27905 return;
27906 if ((ctxt->user_sax != NULL) &&
27907 (ctxt->user_sax->startElementNs != NULL))
27908 ctxt->user_sax->startElementNs(ctxt->user_data, localname, prefix,
27909 URI, nb_namespaces, namespaces,
27910 nb_attributes, nb_defaulted,
27911 attributes);
27912 if (ctxt->ctxt != NULL)
27913 xmlSchemaSAXHandleStartElementNs(ctxt->ctxt, localname, prefix,
27914 URI, nb_namespaces, namespaces,
27915 nb_attributes, nb_defaulted,
27916 attributes);
27917}
27918
27919static void
27920endElementNsSplit(void *ctx, const xmlChar * localname,
27921 const xmlChar * prefix, const xmlChar * URI) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027922 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027923 if (ctxt == NULL)
27924 return;
27925 if ((ctxt->user_sax != NULL) &&
27926 (ctxt->user_sax->endElementNs != NULL))
27927 ctxt->user_sax->endElementNs(ctxt->user_data, localname, prefix, URI);
27928 if (ctxt->ctxt != NULL)
27929 xmlSchemaSAXHandleEndElementNs(ctxt->ctxt, localname, prefix, URI);
27930}
27931
Daniel Veillard4255d502002-04-16 15:50:10 +000027932/**
Daniel Veillard971771e2005-07-09 17:32:57 +000027933 * xmlSchemaSAXPlug:
27934 * @ctxt: a schema validation context
Daniel Veillard1f33c4d2005-07-10 21:38:31 +000027935 * @sax: a pointer to the original xmlSAXHandlerPtr
27936 * @user_data: a pointer to the original SAX user data pointer
Daniel Veillard971771e2005-07-09 17:32:57 +000027937 *
27938 * Plug a SAX based validation layer in a SAX parsing event flow.
27939 * The original @saxptr and @dataptr data are replaced by new pointers
27940 * but the calls to the original will be maintained.
27941 *
27942 * Returns a pointer to a data structure needed to unplug the validation layer
27943 * or NULL in case of errors.
27944 */
27945xmlSchemaSAXPlugPtr
27946xmlSchemaSAXPlug(xmlSchemaValidCtxtPtr ctxt,
27947 xmlSAXHandlerPtr *sax, void **user_data)
27948{
27949 xmlSchemaSAXPlugPtr ret;
27950 xmlSAXHandlerPtr old_sax;
27951
27952 if ((ctxt == NULL) || (sax == NULL) || (user_data == NULL))
27953 return(NULL);
27954
27955 /*
27956 * We only allow to plug into SAX2 event streams
27957 */
27958 old_sax = *sax;
27959 if ((old_sax != NULL) && (old_sax->initialized != XML_SAX2_MAGIC))
27960 return(NULL);
27961 if ((old_sax != NULL) &&
27962 (old_sax->startElementNs == NULL) && (old_sax->endElementNs == NULL) &&
27963 ((old_sax->startElement != NULL) || (old_sax->endElement != NULL)))
27964 return(NULL);
27965
27966 /*
27967 * everything seems right allocate the local data needed for that layer
27968 */
27969 ret = (xmlSchemaSAXPlugPtr) xmlMalloc(sizeof(xmlSchemaSAXPlugStruct));
27970 if (ret == NULL) {
27971 return(NULL);
27972 }
27973 memset(ret, 0, sizeof(xmlSchemaSAXPlugStruct));
27974 ret->magic = XML_SAX_PLUG_MAGIC;
27975 ret->schemas_sax.initialized = XML_SAX2_MAGIC;
27976 ret->ctxt = ctxt;
27977 ret->user_sax_ptr = sax;
27978 ret->user_sax = old_sax;
27979 if (old_sax == NULL) {
27980 /*
27981 * go direct, no need for the split block and functions.
27982 */
27983 ret->schemas_sax.startElementNs = xmlSchemaSAXHandleStartElementNs;
27984 ret->schemas_sax.endElementNs = xmlSchemaSAXHandleEndElementNs;
27985 /*
27986 * Note that we use the same text-function for both, to prevent
27987 * the parser from testing for ignorable whitespace.
27988 */
27989 ret->schemas_sax.ignorableWhitespace = xmlSchemaSAXHandleText;
27990 ret->schemas_sax.characters = xmlSchemaSAXHandleText;
27991
27992 ret->schemas_sax.cdataBlock = xmlSchemaSAXHandleCDataSection;
27993 ret->schemas_sax.reference = xmlSchemaSAXHandleReference;
27994
27995 ret->user_data = ctxt;
27996 *user_data = ctxt;
27997 } else {
27998 /*
27999 * for each callback unused by Schemas initialize it to the Split
28000 * routine only if non NULL in the user block, this can speed up
28001 * things at the SAX level.
28002 */
28003 if (old_sax->internalSubset != NULL)
28004 ret->schemas_sax.internalSubset = internalSubsetSplit;
28005 if (old_sax->isStandalone != NULL)
28006 ret->schemas_sax.isStandalone = isStandaloneSplit;
28007 if (old_sax->hasInternalSubset != NULL)
28008 ret->schemas_sax.hasInternalSubset = hasInternalSubsetSplit;
28009 if (old_sax->hasExternalSubset != NULL)
28010 ret->schemas_sax.hasExternalSubset = hasExternalSubsetSplit;
28011 if (old_sax->resolveEntity != NULL)
28012 ret->schemas_sax.resolveEntity = resolveEntitySplit;
28013 if (old_sax->getEntity != NULL)
28014 ret->schemas_sax.getEntity = getEntitySplit;
28015 if (old_sax->entityDecl != NULL)
28016 ret->schemas_sax.entityDecl = entityDeclSplit;
28017 if (old_sax->notationDecl != NULL)
28018 ret->schemas_sax.notationDecl = notationDeclSplit;
28019 if (old_sax->attributeDecl != NULL)
28020 ret->schemas_sax.attributeDecl = attributeDeclSplit;
28021 if (old_sax->elementDecl != NULL)
28022 ret->schemas_sax.elementDecl = elementDeclSplit;
28023 if (old_sax->unparsedEntityDecl != NULL)
28024 ret->schemas_sax.unparsedEntityDecl = unparsedEntityDeclSplit;
28025 if (old_sax->setDocumentLocator != NULL)
28026 ret->schemas_sax.setDocumentLocator = setDocumentLocatorSplit;
28027 if (old_sax->startDocument != NULL)
28028 ret->schemas_sax.startDocument = startDocumentSplit;
28029 if (old_sax->endDocument != NULL)
28030 ret->schemas_sax.endDocument = endDocumentSplit;
28031 if (old_sax->processingInstruction != NULL)
28032 ret->schemas_sax.processingInstruction = processingInstructionSplit;
28033 if (old_sax->comment != NULL)
28034 ret->schemas_sax.comment = commentSplit;
28035 if (old_sax->warning != NULL)
28036 ret->schemas_sax.warning = warningSplit;
28037 if (old_sax->error != NULL)
28038 ret->schemas_sax.error = errorSplit;
28039 if (old_sax->fatalError != NULL)
28040 ret->schemas_sax.fatalError = fatalErrorSplit;
28041 if (old_sax->getParameterEntity != NULL)
28042 ret->schemas_sax.getParameterEntity = getParameterEntitySplit;
28043 if (old_sax->externalSubset != NULL)
28044 ret->schemas_sax.externalSubset = externalSubsetSplit;
28045
28046 /*
28047 * the 6 schemas callback have to go to the splitter functions
28048 * Note that we use the same text-function for ignorableWhitespace
28049 * if possible, to prevent the parser from testing for ignorable
28050 * whitespace.
28051 */
28052 ret->schemas_sax.characters = charactersSplit;
28053 if ((old_sax->ignorableWhitespace != NULL) &&
28054 (old_sax->ignorableWhitespace != old_sax->characters))
28055 ret->schemas_sax.ignorableWhitespace = ignorableWhitespaceSplit;
28056 else
28057 ret->schemas_sax.ignorableWhitespace = charactersSplit;
28058 ret->schemas_sax.cdataBlock = cdataBlockSplit;
28059 ret->schemas_sax.reference = referenceSplit;
28060 ret->schemas_sax.startElementNs = startElementNsSplit;
28061 ret->schemas_sax.endElementNs = endElementNsSplit;
28062
28063 ret->user_data_ptr = user_data;
28064 ret->user_data = *user_data;
28065 *user_data = ret;
28066 }
28067
28068 /*
28069 * plug the pointers back.
28070 */
28071 *sax = &(ret->schemas_sax);
Daniel Veillardf10ae122005-07-10 19:03:16 +000028072 ctxt->sax = *sax;
28073 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28074 xmlSchemaPreRun(ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028075 return(ret);
28076}
28077
28078/**
28079 * xmlSchemaSAXUnplug:
28080 * @plug: a data structure returned by xmlSchemaSAXPlug
28081 *
28082 * Unplug a SAX based validation layer in a SAX parsing event flow.
28083 * The original pointers used in the call are restored.
28084 *
28085 * Returns 0 in case of success and -1 in case of failure.
28086 */
28087int
28088xmlSchemaSAXUnplug(xmlSchemaSAXPlugPtr plug)
28089{
28090 xmlSAXHandlerPtr *sax;
28091 void **user_data;
28092
28093 if ((plug == NULL) || (plug->magic != XML_SAX_PLUG_MAGIC))
28094 return(-1);
28095 plug->magic = 0;
28096
Daniel Veillardf10ae122005-07-10 19:03:16 +000028097 xmlSchemaPostRun(plug->ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028098 /* restore the data */
28099 sax = plug->user_sax_ptr;
28100 *sax = plug->user_sax;
28101 if (plug->user_sax != NULL) {
28102 user_data = plug->user_data_ptr;
28103 *user_data = plug->user_data;
28104 }
28105
28106 /* free and return */
28107 xmlFree(plug);
28108 return(0);
28109}
28110
28111/**
Daniel Veillard4255d502002-04-16 15:50:10 +000028112 * xmlSchemaValidateStream:
28113 * @ctxt: a schema validation context
28114 * @input: the input to use for reading the data
28115 * @enc: an optional encoding information
28116 * @sax: a SAX handler for the resulting events
28117 * @user_data: the context to provide to the SAX handler.
28118 *
Daniel Veillardcdc82732005-07-08 15:04:06 +000028119 * Validate an input based on a flow of SAX event from the parser
28120 * and forward the events to the @sax handler with the provided @user_data
28121 * the user provided @sax handler must be a SAX2 one.
Daniel Veillard4255d502002-04-16 15:50:10 +000028122 *
28123 * Returns 0 if the document is schemas valid, a positive error code
28124 * number otherwise and -1 in case of internal or API error.
28125 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028126int
Daniel Veillard4255d502002-04-16 15:50:10 +000028127xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028128 xmlParserInputBufferPtr input, xmlCharEncoding enc,
28129 xmlSAXHandlerPtr sax, void *user_data)
28130{
Daniel Veillard971771e2005-07-09 17:32:57 +000028131 xmlSchemaSAXPlugPtr plug = NULL;
28132 xmlSAXHandlerPtr old_sax = NULL;
28133 xmlParserCtxtPtr pctxt = NULL;
28134 xmlParserInputPtr inputStream = NULL;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028135 int ret;
28136
Daniel Veillard4255d502002-04-16 15:50:10 +000028137 if ((ctxt == NULL) || (input == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028138 return (-1);
Daniel Veillardcdc82732005-07-08 15:04:06 +000028139
Daniel Veillardcdc82732005-07-08 15:04:06 +000028140 /*
28141 * prepare the parser
28142 */
28143 pctxt = xmlNewParserCtxt();
28144 if (pctxt == NULL)
28145 return (-1);
28146 old_sax = pctxt->sax;
Daniel Veillard971771e2005-07-09 17:32:57 +000028147 pctxt->sax = sax;
28148 pctxt->userData = user_data;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028149#if 0
28150 if (options)
28151 xmlCtxtUseOptions(pctxt, options);
28152#endif
Kasimier T. Buchcik85aed6d2005-07-08 18:06:18 +000028153 pctxt->linenumbers = 1;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028154
Daniel Veillardf0af8ec2005-07-08 17:27:33 +000028155 inputStream = xmlNewIOInputStream(pctxt, input, enc);;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028156 if (inputStream == NULL) {
28157 ret = -1;
28158 goto done;
28159 }
28160 inputPush(pctxt, inputStream);
28161 ctxt->parserCtxt = pctxt;
28162 ctxt->input = input;
28163
28164 /*
Daniel Veillard971771e2005-07-09 17:32:57 +000028165 * Plug the validation and launch the parsing
Daniel Veillardcdc82732005-07-08 15:04:06 +000028166 */
Daniel Veillard971771e2005-07-09 17:32:57 +000028167 plug = xmlSchemaSAXPlug(ctxt, &(pctxt->sax), &(pctxt->userData));
28168 if (plug == NULL) {
28169 ret = -1;
28170 goto done;
28171 }
28172 ctxt->input = input;
28173 ctxt->enc = enc;
28174 ctxt->sax = pctxt->sax;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028175 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28176 ret = xmlSchemaVStart(ctxt);
28177
28178 if ((ret == 0) && (! ctxt->parserCtxt->wellFormed)) {
28179 ret = ctxt->parserCtxt->errNo;
28180 if (ret == 0)
28181 ret = 1;
28182 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028183
28184done:
Daniel Veillard971771e2005-07-09 17:32:57 +000028185 ctxt->parserCtxt = NULL;
28186 ctxt->sax = NULL;
28187 ctxt->input = NULL;
28188 if (plug != NULL) {
28189 xmlSchemaSAXUnplug(plug);
28190 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028191 /* cleanup */
Daniel Veillard971771e2005-07-09 17:32:57 +000028192 if (pctxt != NULL) {
28193 pctxt->sax = old_sax;
28194 xmlFreeParserCtxt(pctxt);
28195 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028196 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +000028197}
28198
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028199/**
28200 * xmlSchemaValidateFile:
28201 * @ctxt: a schema validation context
Daniel Veillard81562d22005-06-15 13:27:56 +000028202 * @filename: the URI of the instance
28203 * @options: a future set of options, currently unused
28204 *
28205 * Do a schemas validation of the given resource, it will use the
28206 * SAX streamable validation internally.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028207 *
28208 * Returns 0 if the document is valid, a positive error code
28209 * number otherwise and -1 in case of an internal or API error.
28210 */
28211int
28212xmlSchemaValidateFile(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000028213 const char * filename,
28214 int options ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028215{
28216 int ret;
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028217 xmlParserInputBufferPtr input;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028218
28219 if ((ctxt == NULL) || (filename == NULL))
28220 return (-1);
28221
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028222 input = xmlParserInputBufferCreateFilename(filename,
28223 XML_CHAR_ENCODING_NONE);
28224 if (input == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028225 return (-1);
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028226 ret = xmlSchemaValidateStream(ctxt, input, XML_CHAR_ENCODING_NONE,
28227 NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028228 return (ret);
Daniel Veillard81562d22005-06-15 13:27:56 +000028229}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028230
Daniel Veillard5d4644e2005-04-01 13:11:58 +000028231#define bottom_xmlschemas
28232#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +000028233#endif /* LIBXML_SCHEMAS_ENABLED */