blob: 1ec8ac88ca59e989950dab441f25de2663363201 [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;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +0000583 void *errCtxt; /* user specific error context */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +0000584 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;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +0000960 void *errCtxt; /* user specific data block */
Daniel Veillardd0c9c322003-10-10 00:49:42 +0000961 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;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00001883 data = ctxt->errCtxt;
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;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00001949 data = ctxt->errCtxt;
Daniel Veillard3646d642004-06-02 19:19:14 +00001950 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;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00002029 data = vctxt->errCtxt;
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;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00002081 data = pctxt->errCtxt;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002082 __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. Buchcik69dea3a2005-11-07 14:02:44 +00002140 if ((node != NULL) &&
2141 (node->type != XML_ELEMENT_NODE) &&
2142 (node->type != XML_ATTRIBUTE_NODE))
2143 {
2144 /*
2145 * Don't try to format other nodes than element and
2146 * attribute nodes.
2147 * Play save and return an empty string.
2148 */
2149 *msg = xmlStrdup(BAD_CAST "");
2150 return(*msg);
2151 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002152 if (node != NULL) {
2153 /*
2154 * Work on tree nodes.
2155 */
2156 if (node->type == XML_ATTRIBUTE_NODE) {
2157 xmlNodePtr elem = node->parent;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002158
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002159 *msg = xmlStrdup(BAD_CAST "Element '");
2160 if (elem->ns != NULL)
2161 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2162 elem->ns->href, elem->name));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002163 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002164 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2165 NULL, elem->name));
2166 FREE_AND_NULL(str);
2167 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2168 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2169 } else {
2170 *msg = xmlStrdup(BAD_CAST "Element '");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00002171 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002172 if (node->ns != NULL)
2173 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2174 node->ns->href, node->name));
Daniel Veillardc0826a72004-08-10 14:17:33 +00002175 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002176 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2177 NULL, node->name));
2178 FREE_AND_NULL(str);
2179 *msg = xmlStrcat(*msg, BAD_CAST "': ");
2180 } else if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2181 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) actxt;
2182 /*
2183 * Work on node infos.
2184 */
2185 if (vctxt->inode->nodeType == XML_ATTRIBUTE_NODE) {
2186 xmlSchemaNodeInfoPtr ielem =
2187 vctxt->elemInfos[vctxt->depth];
2188
2189 *msg = xmlStrdup(BAD_CAST "Element '");
2190 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2191 ielem->nsName, ielem->localName));
2192 FREE_AND_NULL(str);
2193 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2194 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2195 } else {
2196 *msg = xmlStrdup(BAD_CAST "Element '");
2197 }
2198 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2199 vctxt->inode->nsName, vctxt->inode->localName));
2200 FREE_AND_NULL(str);
2201 *msg = xmlStrcat(*msg, BAD_CAST "': ");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002202 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
2203 /*
2204 * Hmm, no node while parsing?
2205 * Return an empty string, in case NULL will break something.
2206 */
2207 *msg = xmlStrdup(BAD_CAST "");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002208 } else {
2209 TODO
2210 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002211 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002212 /*
2213 * VAL TODO: The output of the given schema component is currently
2214 * disabled.
2215 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002216#if 0
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002217 if ((type != NULL) && (xmlSchemaIsGlobalItem(type))) {
2218 *msg = xmlStrcat(*msg, BAD_CAST " [");
2219 *msg = xmlStrcat(*msg, xmlSchemaFormatItemForReport(&str,
2220 NULL, type, NULL, 0));
2221 FREE_AND_NULL(str)
2222 *msg = xmlStrcat(*msg, BAD_CAST "]");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002223 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002224#endif
2225 return (*msg);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002226}
2227
Daniel Veillardc0826a72004-08-10 14:17:33 +00002228static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002229xmlSchemaInternalErr2(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002230 const char *funcName,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002231 const char *message,
2232 const xmlChar *str1,
2233 const xmlChar *str2)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002234{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002235 xmlChar *msg = NULL;
2236
2237 msg = xmlStrdup(BAD_CAST "Internal error: ");
2238 msg = xmlStrcat(msg, BAD_CAST funcName);
2239 msg = xmlStrcat(msg, BAD_CAST ", ");
2240 msg = xmlStrcat(msg, BAD_CAST message);
2241 msg = xmlStrcat(msg, BAD_CAST ".\n");
2242
2243 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR)
2244 xmlSchemaErr(actxt, XML_SCHEMAV_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002245 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002246
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002247 else if (actxt->type == XML_SCHEMA_CTXT_PARSER)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002248 xmlSchemaErr(actxt, XML_SCHEMAP_INTERNAL, NULL,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002249 (const char *) msg, str1, str2);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002250
2251 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002252}
2253
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002254static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002255xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
2256 const char *funcName,
2257 const char *message)
2258{
2259 xmlSchemaInternalErr2(actxt, funcName, message, NULL, NULL);
2260}
2261
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002262#if 0
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002263static void
2264xmlSchemaPInternalErr(xmlSchemaParserCtxtPtr pctxt,
2265 const char *funcName,
2266 const char *message,
2267 const xmlChar *str1,
2268 const xmlChar *str2)
2269{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002270 xmlSchemaInternalErr2(ACTXT_CAST pctxt, funcName, message,
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002271 str1, str2);
2272}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002273#endif
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +00002274
2275static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002276xmlSchemaCustomErr4(xmlSchemaAbstractCtxtPtr actxt,
2277 xmlParserErrors error,
2278 xmlNodePtr node,
2279 xmlSchemaBasicItemPtr item,
2280 const char *message,
2281 const xmlChar *str1, const xmlChar *str2,
2282 const xmlChar *str3, const xmlChar *str4)
2283{
2284 xmlChar *msg = NULL;
2285
2286 if ((node == NULL) && (item != NULL) &&
2287 (actxt->type == XML_SCHEMA_CTXT_PARSER)) {
2288 node = WXS_ITEM_NODE(item);
2289 xmlSchemaFormatItemForReport(&msg, NULL, item, NULL);
2290 msg = xmlStrcat(msg, BAD_CAST ": ");
2291 } else
2292 xmlSchemaFormatNodeForError(&msg, actxt, node);
2293 msg = xmlStrcat(msg, (const xmlChar *) message);
2294 msg = xmlStrcat(msg, BAD_CAST ".\n");
2295 xmlSchemaErr4(actxt, error, node,
2296 (const char *) msg, str1, str2, str3, str4);
2297 FREE_AND_NULL(msg)
2298}
2299
2300static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002301xmlSchemaCustomErr(xmlSchemaAbstractCtxtPtr actxt,
2302 xmlParserErrors error,
2303 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002304 xmlSchemaBasicItemPtr item,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002305 const char *message,
2306 const xmlChar *str1,
2307 const xmlChar *str2)
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002308{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002309 xmlSchemaCustomErr4(actxt, error, node, item,
2310 message, str1, str2, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002311}
2312
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002313
2314
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002315static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002316xmlSchemaCustomWarning(xmlSchemaAbstractCtxtPtr actxt,
2317 xmlParserErrors error,
2318 xmlNodePtr node,
2319 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2320 const char *message,
2321 const xmlChar *str1,
2322 const xmlChar *str2,
2323 const xmlChar *str3)
2324{
2325 xmlChar *msg = NULL;
2326
2327 xmlSchemaFormatNodeForError(&msg, actxt, node);
2328 msg = xmlStrcat(msg, (const xmlChar *) message);
2329 msg = xmlStrcat(msg, BAD_CAST ".\n");
2330
2331 /* URGENT TODO: Set the error code to something sane. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002332 xmlSchemaErr4Line(actxt, XML_ERR_WARNING, error, node, 0,
2333 (const char *) msg, str1, str2, str3, NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002334
2335 FREE_AND_NULL(msg)
2336}
2337
2338
2339
2340static void
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002341xmlSchemaKeyrefErr(xmlSchemaValidCtxtPtr vctxt,
2342 xmlParserErrors error,
2343 xmlSchemaPSVIIDCNodePtr idcNode,
2344 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2345 const char *message,
2346 const xmlChar *str1,
2347 const xmlChar *str2)
2348{
2349 xmlChar *msg = NULL, *qname = NULL;
2350
2351 msg = xmlStrdup(BAD_CAST "Element '%s': ");
2352 msg = xmlStrcat(msg, (const xmlChar *) message);
2353 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002354 xmlSchemaErr4Line(ACTXT_CAST vctxt, XML_ERR_ERROR,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002355 error, NULL, idcNode->nodeLine, (const char *) msg,
2356 xmlSchemaFormatQName(&qname,
2357 vctxt->nodeQNames->items[idcNode->nodeQNameID +1],
2358 vctxt->nodeQNames->items[idcNode->nodeQNameID]),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002359 str1, str2, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00002360 FREE_AND_NULL(qname);
2361 FREE_AND_NULL(msg);
2362}
2363
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002364static int
2365xmlSchemaEvalErrorNodeType(xmlSchemaAbstractCtxtPtr actxt,
2366 xmlNodePtr node)
2367{
2368 if (node != NULL)
2369 return (node->type);
2370 if ((actxt->type == XML_SCHEMA_CTXT_VALIDATOR) &&
2371 (((xmlSchemaValidCtxtPtr) actxt)->inode != NULL))
2372 return ( ((xmlSchemaValidCtxtPtr) actxt)->inode->nodeType);
2373 return (-1);
2374}
2375
2376static int
2377xmlSchemaIsGlobalItem(xmlSchemaTypePtr item)
2378{
2379 switch (item->type) {
2380 case XML_SCHEMA_TYPE_COMPLEX:
2381 case XML_SCHEMA_TYPE_SIMPLE:
2382 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL)
2383 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002384 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002385 case XML_SCHEMA_TYPE_GROUP:
2386 return (1);
2387 case XML_SCHEMA_TYPE_ELEMENT:
2388 if ( ((xmlSchemaElementPtr) item)->flags &
2389 XML_SCHEMAS_ELEM_GLOBAL)
2390 return(1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002391 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002392 case XML_SCHEMA_TYPE_ATTRIBUTE:
2393 if ( ((xmlSchemaAttributePtr) item)->flags &
2394 XML_SCHEMAS_ATTR_GLOBAL)
2395 return(1);
2396 break;
2397 /* Note that attribute groups are always global. */
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002398 default:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002399 return(1);
2400 }
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +00002401 return (0);
2402}
2403
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002404static void
2405xmlSchemaSimpleTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2406 xmlParserErrors error,
2407 xmlNodePtr node,
2408 const xmlChar *value,
2409 xmlSchemaTypePtr type,
2410 int displayValue)
2411{
2412 xmlChar *msg = NULL;
2413
2414 xmlSchemaFormatNodeForError(&msg, actxt, node);
2415
2416 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2417 XML_ATTRIBUTE_NODE))
2418 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
2419 else
2420 msg = xmlStrcat(msg, BAD_CAST "The character content is not a valid "
2421 "value of ");
2422
2423 if (! xmlSchemaIsGlobalItem(type))
2424 msg = xmlStrcat(msg, BAD_CAST "the local ");
2425 else
2426 msg = xmlStrcat(msg, BAD_CAST "the ");
2427
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002428 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002429 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002430 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002431 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002432 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002433 msg = xmlStrcat(msg, BAD_CAST "union type");
2434
2435 if (xmlSchemaIsGlobalItem(type)) {
2436 xmlChar *str = NULL;
2437 msg = xmlStrcat(msg, BAD_CAST " '");
2438 if (type->builtInType != 0) {
2439 msg = xmlStrcat(msg, BAD_CAST "xs:");
2440 msg = xmlStrcat(msg, type->name);
2441 } else
2442 msg = xmlStrcat(msg,
2443 xmlSchemaFormatQName(&str,
2444 type->targetNamespace, type->name));
2445 msg = xmlStrcat(msg, BAD_CAST "'");
2446 FREE_AND_NULL(str);
2447 }
2448 msg = xmlStrcat(msg, BAD_CAST ".\n");
2449 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2450 XML_ATTRIBUTE_NODE))
2451 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2452 else
2453 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2454 FREE_AND_NULL(msg)
2455}
2456
Daniel Veillardc0826a72004-08-10 14:17:33 +00002457static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002458xmlSchemaFormatErrorNodeQName(xmlChar ** str,
2459 xmlSchemaNodeInfoPtr ni,
2460 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002461{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002462 if (node != NULL) {
2463 if (node->ns != NULL)
2464 return (xmlSchemaFormatQName(str, node->ns->href, node->name));
2465 else
2466 return (xmlSchemaFormatQName(str, NULL, node->name));
2467 } else if (ni != NULL)
2468 return (xmlSchemaFormatQName(str, ni->nsName, ni->localName));
2469 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002470}
2471
Daniel Veillardc0826a72004-08-10 14:17:33 +00002472static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002473xmlSchemaIllegalAttrErr(xmlSchemaAbstractCtxtPtr actxt,
2474 xmlParserErrors error,
2475 xmlSchemaAttrInfoPtr ni,
2476 xmlNodePtr node)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002477{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002478 xmlChar *msg = NULL, *str = NULL;
2479
2480 xmlSchemaFormatNodeForError(&msg, actxt, node);
2481 msg = xmlStrcat(msg, BAD_CAST "The attribute '%s' is not allowed.\n");
2482 xmlSchemaErr(actxt, error, node, (const char *) msg,
2483 xmlSchemaFormatErrorNodeQName(&str, (xmlSchemaNodeInfoPtr) ni, node),
2484 NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002485 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002486 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002487}
2488
Daniel Veillardc0826a72004-08-10 14:17:33 +00002489static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002490xmlSchemaComplexTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2491 xmlParserErrors error,
2492 xmlNodePtr node,
2493 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002494 const char *message,
2495 int nbval,
2496 int nbneg,
2497 xmlChar **values)
2498{
2499 xmlChar *str = NULL, *msg = NULL;
2500 xmlChar *localName, *nsName;
2501 const xmlChar *cur, *end;
Daniel Veillard77005e62005-07-19 16:26:18 +00002502 int i, is_not;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002503
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002504 xmlSchemaFormatNodeForError(&msg, actxt, node);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002505 msg = xmlStrcat(msg, (const xmlChar *) message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002506 msg = xmlStrcat(msg, BAD_CAST ".");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002507 /*
2508 * Note that is does not make sense to report that we have a
2509 * wildcard here, since the wildcard might be unfolded into
2510 * multiple transitions.
2511 */
2512 if (nbval + nbneg > 0) {
2513 if (nbval + nbneg > 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002514 str = xmlStrdup(BAD_CAST " Expected is one of ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002515 } else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002516 str = xmlStrdup(BAD_CAST " Expected is ( ");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002517 nsName = NULL;
2518
2519 for (i = 0; i < nbval + nbneg; i++) {
2520 cur = values[i];
Daniel Veillard77005e62005-07-19 16:26:18 +00002521 if (cur == NULL)
2522 continue;
2523 if ((cur[0] == 'n') && (cur[1] == 'o') && (cur[2] == 't') &&
2524 (cur[3] == ' ')) {
2525 is_not = 1;
2526 cur += 4;
Daniel Veillard6e65e152005-08-09 11:09:52 +00002527 str = xmlStrcat(str, BAD_CAST "##other");
Daniel Veillard77005e62005-07-19 16:26:18 +00002528 } else {
2529 is_not = 0;
2530 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002531 /*
2532 * Get the local name.
2533 */
2534 localName = NULL;
2535
2536 end = cur;
2537 if (*end == '*') {
2538 localName = xmlStrdup(BAD_CAST "*");
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002539 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002540 } else {
2541 while ((*end != 0) && (*end != '|'))
2542 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002543 localName = xmlStrncat(localName, BAD_CAST cur, end - cur);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002544 }
2545 if (*end != 0) {
Kasimier T. Buchcikc0e833f2005-04-19 15:02:20 +00002546 end++;
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002547 /*
2548 * Skip "*|*" if they come with negated expressions, since
2549 * they represent the same negated wildcard.
2550 */
2551 if ((nbneg == 0) || (*end != '*') || (*localName != '*')) {
2552 /*
2553 * Get the namespace name.
2554 */
2555 cur = end;
2556 if (*end == '*') {
2557 nsName = xmlStrdup(BAD_CAST "{*}");
2558 } else {
2559 while (*end != 0)
2560 end++;
2561
2562 if (i >= nbval)
2563 nsName = xmlStrdup(BAD_CAST "{##other:");
2564 else
2565 nsName = xmlStrdup(BAD_CAST "{");
2566
2567 nsName = xmlStrncat(nsName, BAD_CAST cur, end - cur);
2568 nsName = xmlStrcat(nsName, BAD_CAST "}");
2569 }
2570 str = xmlStrcat(str, BAD_CAST nsName);
2571 FREE_AND_NULL(nsName)
2572 } else {
2573 FREE_AND_NULL(localName);
2574 continue;
2575 }
2576 }
2577 str = xmlStrcat(str, BAD_CAST localName);
2578 FREE_AND_NULL(localName);
2579
2580 if (i < nbval + nbneg -1)
2581 str = xmlStrcat(str, BAD_CAST ", ");
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00002582 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002583 str = xmlStrcat(str, BAD_CAST " ).\n");
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002584 msg = xmlStrcat(msg, BAD_CAST str);
2585 FREE_AND_NULL(str)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002586 } else
2587 msg = xmlStrcat(msg, BAD_CAST "\n");
2588 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002589 xmlFree(msg);
2590}
2591
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002592static void
2593xmlSchemaFacetErr(xmlSchemaAbstractCtxtPtr actxt,
2594 xmlParserErrors error,
2595 xmlNodePtr node,
2596 const xmlChar *value,
2597 unsigned long length,
2598 xmlSchemaTypePtr type,
2599 xmlSchemaFacetPtr facet,
2600 const char *message,
2601 const xmlChar *str1,
2602 const xmlChar *str2)
2603{
2604 xmlChar *str = NULL, *msg = NULL;
2605 xmlSchemaTypeType facetType;
2606 int nodeType = xmlSchemaEvalErrorNodeType(actxt, node);
2607
2608 xmlSchemaFormatNodeForError(&msg, actxt, node);
2609 if (error == XML_SCHEMAV_CVC_ENUMERATION_VALID) {
2610 facetType = XML_SCHEMA_FACET_ENUMERATION;
2611 /*
2612 * If enumerations are validated, one must not expect the
2613 * facet to be given.
2614 */
2615 } else
2616 facetType = facet->type;
2617 msg = xmlStrcat(msg, BAD_CAST "[");
2618 msg = xmlStrcat(msg, BAD_CAST "facet '");
2619 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facetType));
2620 msg = xmlStrcat(msg, BAD_CAST "'] ");
2621 if (message == NULL) {
2622 /*
2623 * Use a default message.
2624 */
2625 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
2626 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
2627 (facetType == XML_SCHEMA_FACET_MAXLENGTH)) {
2628
2629 char len[25], actLen[25];
2630
2631 /* FIXME, TODO: What is the max expected string length of the
2632 * this value?
2633 */
2634 if (nodeType == XML_ATTRIBUTE_NODE)
2635 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has a length of '%s'; ");
2636 else
2637 msg = xmlStrcat(msg, BAD_CAST "The value has a length of '%s'; ");
2638
2639 snprintf(len, 24, "%lu", xmlSchemaGetFacetValueAsULong(facet));
2640 snprintf(actLen, 24, "%lu", length);
2641
2642 if (facetType == XML_SCHEMA_FACET_LENGTH)
2643 msg = xmlStrcat(msg,
2644 BAD_CAST "this differs from the allowed length of '%s'.\n");
2645 else if (facetType == XML_SCHEMA_FACET_MAXLENGTH)
2646 msg = xmlStrcat(msg,
2647 BAD_CAST "this exceeds the allowed maximum length of '%s'.\n");
2648 else if (facetType == XML_SCHEMA_FACET_MINLENGTH)
2649 msg = xmlStrcat(msg,
2650 BAD_CAST "this underruns the allowed minimum length of '%s'.\n");
2651
2652 if (nodeType == XML_ATTRIBUTE_NODE)
2653 xmlSchemaErr3(actxt, error, node, (const char *) msg,
2654 value, (const xmlChar *) actLen, (const xmlChar *) len);
2655 else
2656 xmlSchemaErr(actxt, error, node, (const char *) msg,
2657 (const xmlChar *) actLen, (const xmlChar *) len);
2658
2659 } else if (facetType == XML_SCHEMA_FACET_ENUMERATION) {
2660 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not an element "
2661 "of the set {%s}.\n");
2662 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2663 xmlSchemaFormatFacetEnumSet(actxt, &str, type));
2664 } else if (facetType == XML_SCHEMA_FACET_PATTERN) {
2665 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not accepted "
2666 "by the pattern '%s'.\n");
2667 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2668 facet->value);
2669 } else if (facetType == XML_SCHEMA_FACET_MININCLUSIVE) {
2670 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is less than the "
2671 "minimum value allowed ('%s').\n");
2672 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2673 facet->value);
2674 } else if (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) {
2675 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is greater than the "
2676 "maximum value allowed ('%s').\n");
2677 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2678 facet->value);
2679 } else if (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE) {
2680 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be less than "
2681 "'%s'.\n");
2682 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2683 facet->value);
2684 } else if (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) {
2685 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be more than "
2686 "'%s'.\n");
2687 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2688 facet->value);
2689 } else if (facetType == XML_SCHEMA_FACET_TOTALDIGITS) {
2690 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more "
2691 "digits than are allowed ('%s').\n");
2692 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2693 facet->value);
2694 } else if (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) {
2695 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more fractional "
2696 "digits than are allowed ('%s').\n");
2697 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2698 facet->value);
2699 } else if (nodeType == XML_ATTRIBUTE_NODE) {
2700 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not facet-valid.\n");
2701 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2702 } else {
2703 msg = xmlStrcat(msg, BAD_CAST "The value is not facet-valid.\n");
2704 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2705 }
2706 } else {
2707 msg = xmlStrcat(msg, (const xmlChar *) message);
2708 msg = xmlStrcat(msg, BAD_CAST ".\n");
2709 xmlSchemaErr(actxt, error, node, (const char *) msg, str1, str2);
2710 }
2711 FREE_AND_NULL(str)
2712 xmlFree(msg);
2713}
2714
2715#define VERROR(err, type, msg) \
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002716 xmlSchemaCustomErr(ACTXT_CAST vctxt, err, NULL, type, msg, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002717
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002718#define VERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST vctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002719
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00002720#define PERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST pctxt, func, msg);
2721#define PERROR_INT2(func, msg) xmlSchemaInternalErr(ACTXT_CAST ctxt, func, msg);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002722
2723#define AERROR_INT(func, msg) xmlSchemaInternalErr(actxt, func, msg);
2724
2725
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +00002726/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00002727 * xmlSchemaPMissingAttrErr:
2728 * @ctxt: the schema validation context
2729 * @ownerDes: the designation of the owner
2730 * @ownerName: the name of the owner
2731 * @ownerItem: the owner as a schema object
2732 * @ownerElem: the owner as an element node
2733 * @node: the parent element node of the missing attribute node
2734 * @type: the corresponding type of the attribute node
2735 *
2736 * Reports an illegal attribute.
2737 */
2738static void
2739xmlSchemaPMissingAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002740 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002741 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002742 xmlNodePtr ownerElem,
2743 const char *name,
2744 const char *message)
2745{
2746 xmlChar *des = NULL;
2747
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002748 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
2749
Daniel Veillardc0826a72004-08-10 14:17:33 +00002750 if (message != NULL)
2751 xmlSchemaPErr(ctxt, ownerElem, error, "%s: %s.\n", BAD_CAST des, BAD_CAST message);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002752 else
2753 xmlSchemaPErr(ctxt, ownerElem, error,
2754 "%s: The attribute '%s' is required but missing.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002755 BAD_CAST des, BAD_CAST name);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002756 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002757}
2758
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002759
Daniel Veillardc0826a72004-08-10 14:17:33 +00002760/**
2761 * xmlSchemaPResCompAttrErr:
2762 * @ctxt: the schema validation context
2763 * @error: the error code
2764 * @ownerDes: the designation of the owner
2765 * @ownerItem: the owner as a schema object
2766 * @ownerElem: the owner as an element node
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002767 * @name: the name of the attribute holding the QName
Daniel Veillardc0826a72004-08-10 14:17:33 +00002768 * @refName: the referenced local name
2769 * @refURI: the referenced namespace URI
2770 * @message: optional message
2771 *
2772 * Used to report QName attribute values that failed to resolve
2773 * to schema components.
2774 */
2775static void
2776xmlSchemaPResCompAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002777 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002778 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002779 xmlNodePtr ownerElem,
2780 const char *name,
2781 const xmlChar *refName,
2782 const xmlChar *refURI,
2783 xmlSchemaTypeType refType,
2784 const char *refTypeStr)
2785{
2786 xmlChar *des = NULL, *strA = NULL;
2787
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002788 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002789 if (refTypeStr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002790 refTypeStr = (const char *) xmlSchemaItemTypeToStr(refType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002791 xmlSchemaPErrExt(ctxt, ownerElem, error,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002792 NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002793 "%s, attribute '%s': The QName value '%s' does not resolve to a(n) "
2794 "%s.\n", BAD_CAST des, BAD_CAST name,
2795 xmlSchemaFormatQName(&strA, refURI, refName),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002796 BAD_CAST refTypeStr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002797 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002798 FREE_AND_NULL(strA)
2799}
2800
William M. Brack2f2a6632004-08-20 23:09:47 +00002801/**
2802 * xmlSchemaPCustomAttrErr:
2803 * @ctxt: the schema parser context
2804 * @error: the error code
2805 * @ownerDes: the designation of the owner
2806 * @ownerItem: the owner as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002807 * @attr: the illegal attribute node
William M. Brack2f2a6632004-08-20 23:09:47 +00002808 *
2809 * Reports an illegal attribute during the parse.
2810 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002811static void
2812xmlSchemaPCustomAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002813 xmlParserErrors error,
William M. Brack2f2a6632004-08-20 23:09:47 +00002814 xmlChar **ownerDes,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002815 xmlSchemaBasicItemPtr ownerItem,
William M. Brack2f2a6632004-08-20 23:09:47 +00002816 xmlAttrPtr attr,
2817 const char *msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002818{
2819 xmlChar *des = NULL;
2820
2821 if (ownerDes == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002822 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002823 else if (*ownerDes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002824 xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002825 des = *ownerDes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002826 } else
2827 des = *ownerDes;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002828 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002829 "%s, attribute '%s': %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00002830 BAD_CAST des, attr->name, (const xmlChar *) msg, NULL, NULL);
2831 if (ownerDes == NULL)
2832 FREE_AND_NULL(des);
2833}
2834
2835/**
2836 * xmlSchemaPIllegalAttrErr:
William M. Brack2f2a6632004-08-20 23:09:47 +00002837 * @ctxt: the schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00002838 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00002839 * @ownerDes: the designation of the attribute's owner
2840 * @ownerItem: the attribute's owner item
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002841 * @attr: the illegal attribute node
Daniel Veillardc0826a72004-08-10 14:17:33 +00002842 *
William M. Brack2f2a6632004-08-20 23:09:47 +00002843 * Reports an illegal attribute during the parse.
Daniel Veillardc0826a72004-08-10 14:17:33 +00002844 */
2845static void
2846xmlSchemaPIllegalAttrErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002847 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002848 xmlSchemaBasicItemPtr ownerComp ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002849 xmlAttrPtr attr)
2850{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002851 xmlChar *strA = NULL, *strB = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00002852
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002853 xmlSchemaFormatNodeForError(&strA, ACTXT_CAST ctxt, attr->parent);
2854 xmlSchemaErr4(ACTXT_CAST ctxt, error, (xmlNodePtr) attr,
2855 "%sThe attribute '%s' is not allowed.\n", BAD_CAST strA,
2856 xmlSchemaFormatQNameNs(&strB, attr->ns, attr->name),
2857 NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002858 FREE_AND_NULL(strA);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002859 FREE_AND_NULL(strB);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002860}
2861
William M. Brack2f2a6632004-08-20 23:09:47 +00002862/**
2863 * xmlSchemaPCustomErr:
2864 * @ctxt: the schema parser context
2865 * @error: the error code
2866 * @itemDes: the designation of the schema item
2867 * @item: the schema item
2868 * @itemElem: the node of the schema item
2869 * @message: the error message
2870 * @str1: an optional param for the error message
2871 * @str2: an optional param for the error message
2872 * @str3: an optional param for the error message
2873 *
2874 * Reports an error during parsing.
2875 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002876static void
2877xmlSchemaPCustomErrExt(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002878 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002879 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002880 xmlNodePtr itemElem,
2881 const char *message,
2882 const xmlChar *str1,
2883 const xmlChar *str2,
2884 const xmlChar *str3)
2885{
2886 xmlChar *des = NULL, *msg = NULL;
2887
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002888 xmlSchemaFormatItemForReport(&des, NULL, item, itemElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002889 msg = xmlStrdup(BAD_CAST "%s: ");
2890 msg = xmlStrcat(msg, (const xmlChar *) message);
2891 msg = xmlStrcat(msg, BAD_CAST ".\n");
2892 if ((itemElem == NULL) && (item != NULL))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002893 itemElem = WXS_ITEM_NODE(item);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002894 xmlSchemaPErrExt(ctxt, itemElem, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002895 (const char *) msg, BAD_CAST des, str1, str2, str3, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002896 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002897 FREE_AND_NULL(msg);
2898}
2899
William M. Brack2f2a6632004-08-20 23:09:47 +00002900/**
2901 * xmlSchemaPCustomErr:
2902 * @ctxt: the schema parser context
2903 * @error: the error code
2904 * @itemDes: the designation of the schema item
2905 * @item: the schema item
2906 * @itemElem: the node of the schema item
2907 * @message: the error message
2908 * @str1: the optional param for the error message
2909 *
2910 * Reports an error during parsing.
2911 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002912static void
2913xmlSchemaPCustomErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002914 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002915 xmlSchemaBasicItemPtr item,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002916 xmlNodePtr itemElem,
2917 const char *message,
2918 const xmlChar *str1)
2919{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002920 xmlSchemaPCustomErrExt(ctxt, error, item, itemElem, message,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002921 str1, NULL, NULL);
2922}
2923
William M. Brack2f2a6632004-08-20 23:09:47 +00002924/**
2925 * xmlSchemaPAttrUseErr:
2926 * @ctxt: the schema parser context
2927 * @error: the error code
2928 * @itemDes: the designation of the schema type
2929 * @item: the schema type
2930 * @itemElem: the node of the schema type
2931 * @attr: the invalid schema attribute
2932 * @message: the error message
2933 * @str1: the optional param for the error message
2934 *
2935 * Reports an attribute use error during parsing.
2936 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002937static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002938xmlSchemaPAttrUseErr4(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002939 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002940 xmlNodePtr node,
2941 xmlSchemaBasicItemPtr ownerItem,
2942 const xmlSchemaAttributeUsePtr attruse,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002943 const char *message,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002944 const xmlChar *str1, const xmlChar *str2,
2945 const xmlChar *str3,const xmlChar *str4)
Daniel Veillardc0826a72004-08-10 14:17:33 +00002946{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002947 xmlChar *str = NULL, *msg = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002948
2949 xmlSchemaFormatItemForReport(&msg, NULL, ownerItem, NULL);
2950 msg = xmlStrcat(msg, BAD_CAST ", ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002951 msg = xmlStrcat(msg,
2952 BAD_CAST xmlSchemaFormatItemForReport(&str, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002953 WXS_BASIC_CAST attruse, NULL));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002954 FREE_AND_NULL(str);
2955 msg = xmlStrcat(msg, BAD_CAST ": ");
Daniel Veillardc0826a72004-08-10 14:17:33 +00002956 msg = xmlStrcat(msg, (const xmlChar *) message);
2957 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002958 xmlSchemaErr4(ACTXT_CAST ctxt, error, node,
2959 (const char *) msg, str1, str2, str3, str4);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002960 xmlFree(msg);
2961}
2962
William M. Brack2f2a6632004-08-20 23:09:47 +00002963/**
2964 * xmlSchemaPIllegalFacetAtomicErr:
2965 * @ctxt: the schema parser context
2966 * @error: the error code
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002967 * @type: the schema type
2968 * @baseType: the base type of type
William M. Brack2f2a6632004-08-20 23:09:47 +00002969 * @facet: the illegal facet
2970 *
2971 * Reports an illegal facet for atomic simple types.
2972 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00002973static void
2974xmlSchemaPIllegalFacetAtomicErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00002975 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002976 xmlSchemaTypePtr type,
2977 xmlSchemaTypePtr baseType,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002978 xmlSchemaFacetPtr facet)
2979{
2980 xmlChar *des = NULL, *strT = NULL;
2981
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002982 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type, type->node);
2983 xmlSchemaPErrExt(ctxt, type->node, error, NULL, NULL, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00002984 "%s: The facet '%s' is not allowed on types derived from the "
2985 "type %s.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00002986 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002987 xmlSchemaFormatItemForReport(&strT, NULL, WXS_BASIC_CAST baseType, NULL),
Daniel Veillardc0826a72004-08-10 14:17:33 +00002988 NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00002989 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00002990 FREE_AND_NULL(strT);
2991}
2992
William M. Brack2f2a6632004-08-20 23:09:47 +00002993/**
2994 * xmlSchemaPIllegalFacetListUnionErr:
2995 * @ctxt: the schema parser context
2996 * @error: the error code
2997 * @itemDes: the designation of the schema item involved
2998 * @item: the schema item involved
2999 * @facet: the illegal facet
3000 *
3001 * Reports an illegal facet for <list> and <union>.
3002 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003003static void
3004xmlSchemaPIllegalFacetListUnionErr(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003005 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003006 xmlSchemaTypePtr type,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003007 xmlSchemaFacetPtr facet)
3008{
3009 xmlChar *des = NULL, *strT = NULL;
3010
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003011 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type,
3012 type->node);
3013 xmlSchemaPErr(ctxt, type->node, error,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003014 "%s: The facet '%s' is not allowed.\n",
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00003015 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003016 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003017 FREE_AND_NULL(strT);
3018}
3019
3020/**
3021 * xmlSchemaPMutualExclAttrErr:
3022 * @ctxt: the schema validation context
3023 * @error: the error code
3024 * @elemDes: the designation of the parent element node
3025 * @attr: the bad attribute node
3026 * @type: the corresponding type of the attribute node
3027 *
3028 * Reports an illegal attribute.
3029 */
3030static void
3031xmlSchemaPMutualExclAttrErr(xmlSchemaParserCtxtPtr ctxt,
3032 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003033 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003034 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003035 const char *name1,
3036 const char *name2)
3037{
3038 xmlChar *des = NULL;
3039
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003040 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST ownerItem, attr->parent);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003041 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003042 "%s: The attributes '%s' and '%s' are mutually exclusive.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003043 BAD_CAST des, BAD_CAST name1, BAD_CAST name2, NULL, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003044 FREE_AND_NULL(des);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003045}
3046
3047/**
3048 * xmlSchemaPSimpleTypeErr:
3049 * @ctxt: the schema validation context
3050 * @error: the error code
William M. Brack2f2a6632004-08-20 23:09:47 +00003051 * @type: the type specifier
Daniel Veillardc0826a72004-08-10 14:17:33 +00003052 * @ownerDes: the designation of the owner
3053 * @ownerItem: the schema object if existent
3054 * @node: the validated node
3055 * @value: the validated value
3056 *
3057 * Reports a simple type validation error.
3058 * TODO: Should this report the value of an element as well?
3059 */
3060static void
3061xmlSchemaPSimpleTypeErr(xmlSchemaParserCtxtPtr ctxt,
3062 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003063 xmlSchemaBasicItemPtr ownerItem ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003064 xmlNodePtr node,
William M. Brack2f2a6632004-08-20 23:09:47 +00003065 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003066 const char *expected,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003067 const xmlChar *value,
3068 const char *message,
3069 const xmlChar *str1,
3070 const xmlChar *str2)
3071{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003072 xmlChar *msg = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00003073
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003074 xmlSchemaFormatNodeForError(&msg, ACTXT_CAST ctxt, node);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003075 if (message == NULL) {
3076 /*
3077 * Use default messages.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003078 */
3079 if (type != NULL) {
3080 if (node->type == XML_ATTRIBUTE_NODE)
3081 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
3082 else
3083 msg = xmlStrcat(msg, BAD_CAST "The character content is not a "
3084 "valid value of ");
3085 if (! xmlSchemaIsGlobalItem(type))
3086 msg = xmlStrcat(msg, BAD_CAST "the local ");
3087 else
3088 msg = xmlStrcat(msg, BAD_CAST "the ");
3089
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003090 if (WXS_IS_ATOMIC(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003091 msg = xmlStrcat(msg, BAD_CAST "atomic type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003092 else if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003093 msg = xmlStrcat(msg, BAD_CAST "list type");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003094 else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003095 msg = xmlStrcat(msg, BAD_CAST "union type");
3096
3097 if (xmlSchemaIsGlobalItem(type)) {
3098 xmlChar *str = NULL;
3099 msg = xmlStrcat(msg, BAD_CAST " '");
3100 if (type->builtInType != 0) {
3101 msg = xmlStrcat(msg, BAD_CAST "xs:");
3102 msg = xmlStrcat(msg, type->name);
3103 } else
3104 msg = xmlStrcat(msg,
3105 xmlSchemaFormatQName(&str,
3106 type->targetNamespace, type->name));
3107 msg = xmlStrcat(msg, BAD_CAST "'.");
3108 FREE_AND_NULL(str);
3109 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00003110 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003111 if (node->type == XML_ATTRIBUTE_NODE)
3112 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not valid.");
3113 else
3114 msg = xmlStrcat(msg, BAD_CAST "The character content is not "
3115 "valid.");
3116 }
3117 if (expected) {
3118 msg = xmlStrcat(msg, BAD_CAST " Expected is '");
3119 msg = xmlStrcat(msg, BAD_CAST expected);
3120 msg = xmlStrcat(msg, BAD_CAST "'.\n");
3121 } else
3122 msg = xmlStrcat(msg, BAD_CAST "\n");
Daniel Veillardc0826a72004-08-10 14:17:33 +00003123 if (node->type == XML_ATTRIBUTE_NODE)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003124 xmlSchemaPErr(ctxt, node, error, (const char *) msg, value, NULL);
3125 else
3126 xmlSchemaPErr(ctxt, node, error, (const char *) msg, NULL, NULL);
3127 } else {
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003128 msg = xmlStrcat(msg, BAD_CAST message);
Kasimier T. Buchcik11162b72005-07-28 00:50:22 +00003129 msg = xmlStrcat(msg, BAD_CAST ".\n");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003130 xmlSchemaPErrExt(ctxt, node, error, NULL, NULL, NULL,
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +00003131 (const char*) msg, str1, str2, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003132 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003133 /* Cleanup. */
3134 FREE_AND_NULL(msg)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003135}
3136
William M. Brack2f2a6632004-08-20 23:09:47 +00003137/**
3138 * xmlSchemaPContentErr:
3139 * @ctxt: the schema parser context
3140 * @error: the error code
3141 * @onwerDes: the designation of the holder of the content
3142 * @ownerItem: the owner item of the holder of the content
3143 * @ownerElem: the node of the holder of the content
3144 * @child: the invalid child node
3145 * @message: the optional error message
3146 * @content: the optional string describing the correct content
3147 *
3148 * Reports an error concerning the content of a schema element.
3149 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00003150static void
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003151xmlSchemaPContentErr(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003152 xmlParserErrors error,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003153 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003154 xmlNodePtr ownerElem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00003155 xmlNodePtr child,
3156 const char *message,
3157 const char *content)
3158{
3159 xmlChar *des = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003160
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003161 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003162 if (message != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003163 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3164 "%s: %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003165 BAD_CAST des, BAD_CAST message);
3166 else {
3167 if (content != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003168 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3169 "%s: The content is not valid. Expected is %s.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003170 BAD_CAST des, BAD_CAST content);
3171 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003172 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3173 "%s: The content is not valid.\n",
Daniel Veillardc0826a72004-08-10 14:17:33 +00003174 BAD_CAST des, NULL);
3175 }
3176 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003177 FREE_AND_NULL(des)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003178}
3179
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003180/************************************************************************
3181 * *
3182 * Streamable error functions *
3183 * *
3184 ************************************************************************/
Kasimier T. Buchcik8b418172004-11-17 13:14:27 +00003185
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003186
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003187
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003188
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003189/************************************************************************
3190 * *
3191 * Validation helper functions *
3192 * *
3193 ************************************************************************/
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +00003194
Daniel Veillardc0826a72004-08-10 14:17:33 +00003195
Daniel Veillard4255d502002-04-16 15:50:10 +00003196/************************************************************************
3197 * *
3198 * Allocation functions *
3199 * *
3200 ************************************************************************/
3201
3202/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003203 * xmlSchemaNewSchemaForParserCtxt:
William M. Brack08171912003-12-29 02:52:11 +00003204 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003205 *
3206 * Allocate a new Schema structure.
3207 *
3208 * Returns the newly allocated structure or NULL in case or error
3209 */
3210static xmlSchemaPtr
3211xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt)
3212{
3213 xmlSchemaPtr ret;
3214
3215 ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema));
3216 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003217 xmlSchemaPErrMemory(ctxt, "allocating schema", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00003218 return (NULL);
3219 }
3220 memset(ret, 0, sizeof(xmlSchema));
Daniel Veillardbe9c6322003-11-22 20:37:51 +00003221 ret->dict = ctxt->dict;
Daniel Veillard500a1de2004-03-22 15:22:58 +00003222 xmlDictReference(ret->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00003223
3224 return (ret);
3225}
3226
3227/**
3228 * xmlSchemaNewFacet:
Daniel Veillard4255d502002-04-16 15:50:10 +00003229 *
3230 * Allocate a new Facet structure.
3231 *
3232 * Returns the newly allocated structure or NULL in case or error
3233 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003234xmlSchemaFacetPtr
3235xmlSchemaNewFacet(void)
Daniel Veillard4255d502002-04-16 15:50:10 +00003236{
3237 xmlSchemaFacetPtr ret;
3238
3239 ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet));
3240 if (ret == NULL) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003241 return (NULL);
3242 }
3243 memset(ret, 0, sizeof(xmlSchemaFacet));
3244
3245 return (ret);
3246}
3247
3248/**
3249 * xmlSchemaNewAnnot:
William M. Brack08171912003-12-29 02:52:11 +00003250 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +00003251 * @node: a node
3252 *
3253 * Allocate a new annotation structure.
3254 *
3255 * Returns the newly allocated structure or NULL in case or error
3256 */
3257static xmlSchemaAnnotPtr
3258xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
3259{
3260 xmlSchemaAnnotPtr ret;
3261
3262 ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot));
3263 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003264 xmlSchemaPErrMemory(ctxt, "allocating annotation", node);
Daniel Veillard4255d502002-04-16 15:50:10 +00003265 return (NULL);
3266 }
3267 memset(ret, 0, sizeof(xmlSchemaAnnot));
3268 ret->content = node;
3269 return (ret);
3270}
3271
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003272static xmlSchemaItemListPtr
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003273xmlSchemaItemListCreate(void)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003274{
3275 xmlSchemaItemListPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003276
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003277 ret = xmlMalloc(sizeof(xmlSchemaItemList));
3278 if (ret == NULL) {
3279 xmlSchemaPErrMemory(NULL,
3280 "allocating an item list structure", NULL);
3281 return (NULL);
3282 }
3283 memset(ret, 0, sizeof(xmlSchemaItemList));
3284 return (ret);
3285}
3286
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003287static void
3288xmlSchemaItemListClear(xmlSchemaItemListPtr list)
3289{
3290 if (list->items != NULL) {
3291 xmlFree(list->items);
3292 list->items = NULL;
3293 }
3294 list->nbItems = 0;
3295 list->sizeItems = 0;
3296}
3297
3298static int
3299xmlSchemaItemListAdd(xmlSchemaItemListPtr list, void *item)
3300{
3301 if (list->items == NULL) {
3302 list->items = (void **) xmlMalloc(
3303 20 * sizeof(void *));
3304 if (list->items == NULL) {
3305 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3306 return(-1);
3307 }
3308 list->sizeItems = 20;
3309 } else if (list->sizeItems <= list->nbItems) {
3310 list->sizeItems *= 2;
3311 list->items = (void **) xmlRealloc(list->items,
3312 list->sizeItems * sizeof(void *));
3313 if (list->items == NULL) {
3314 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3315 list->sizeItems = 0;
3316 return(-1);
3317 }
3318 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00003319 list->items[list->nbItems++] = item;
3320 return(0);
3321}
3322
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003323static int
3324xmlSchemaItemListAddSize(xmlSchemaItemListPtr list,
3325 int initialSize,
3326 void *item)
3327{
3328 if (list->items == NULL) {
3329 if (initialSize <= 0)
3330 initialSize = 1;
3331 list->items = (void **) xmlMalloc(
3332 initialSize * sizeof(void *));
3333 if (list->items == NULL) {
3334 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3335 return(-1);
3336 }
3337 list->sizeItems = initialSize;
3338 } else if (list->sizeItems <= list->nbItems) {
3339 list->sizeItems *= 2;
3340 list->items = (void **) xmlRealloc(list->items,
3341 list->sizeItems * sizeof(void *));
3342 if (list->items == NULL) {
3343 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3344 list->sizeItems = 0;
3345 return(-1);
3346 }
3347 }
3348 list->items[list->nbItems++] = item;
3349 return(0);
3350}
3351
3352static int
3353xmlSchemaItemListInsert(xmlSchemaItemListPtr list, void *item, int idx)
3354{
3355 if (list->items == NULL) {
3356 list->items = (void **) xmlMalloc(
3357 20 * sizeof(void *));
3358 if (list->items == NULL) {
3359 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3360 return(-1);
3361 }
3362 list->sizeItems = 20;
3363 } else if (list->sizeItems <= list->nbItems) {
3364 list->sizeItems *= 2;
3365 list->items = (void **) xmlRealloc(list->items,
3366 list->sizeItems * sizeof(void *));
3367 if (list->items == NULL) {
3368 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3369 list->sizeItems = 0;
3370 return(-1);
3371 }
3372 }
3373 /*
3374 * Just append if the index is greater/equal than the item count.
3375 */
3376 if (idx >= list->nbItems) {
3377 list->items[list->nbItems++] = item;
3378 } else {
3379 int i;
3380 for (i = list->nbItems; i > idx; i--)
3381 list->items[i] = list->items[i-1];
3382 list->items[idx] = item;
3383 list->nbItems++;
3384 }
3385 return(0);
3386}
3387
3388#if 0 /* enable if ever needed */
3389static int
3390xmlSchemaItemListInsertSize(xmlSchemaItemListPtr list,
3391 int initialSize,
3392 void *item,
3393 int idx)
3394{
3395 if (list->items == NULL) {
3396 if (initialSize <= 0)
3397 initialSize = 1;
3398 list->items = (void **) xmlMalloc(
3399 initialSize * sizeof(void *));
3400 if (list->items == NULL) {
3401 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3402 return(-1);
3403 }
3404 list->sizeItems = initialSize;
3405 } else if (list->sizeItems <= list->nbItems) {
3406 list->sizeItems *= 2;
3407 list->items = (void **) xmlRealloc(list->items,
3408 list->sizeItems * sizeof(void *));
3409 if (list->items == NULL) {
3410 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3411 list->sizeItems = 0;
3412 return(-1);
3413 }
3414 }
3415 /*
3416 * Just append if the index is greater/equal than the item count.
3417 */
3418 if (idx >= list->nbItems) {
3419 list->items[list->nbItems++] = item;
3420 } else {
3421 int i;
3422 for (i = list->nbItems; i > idx; i--)
3423 list->items[i] = list->items[i-1];
3424 list->items[idx] = item;
3425 list->nbItems++;
3426 }
3427 return(0);
3428}
3429#endif
3430
3431static int
3432xmlSchemaItemListRemove(xmlSchemaItemListPtr list, int idx)
3433{
3434 int i;
3435 if ((list->items == NULL) || (idx >= list->nbItems)) {
3436 xmlSchemaPSimpleErr("Internal error: xmlSchemaItemListRemove, "
3437 "index error.\n");
3438 return(-1);
3439 }
3440
3441 if (list->nbItems == 1) {
3442 /* TODO: Really free the list? */
3443 xmlFree(list->items);
3444 list->items = NULL;
3445 list->nbItems = 0;
3446 list->sizeItems = 0;
3447 } else if (list->nbItems -1 == idx) {
3448 list->nbItems--;
3449 } else {
3450 for (i = idx; i < list->nbItems -1; i++)
3451 list->items[i] = list->items[i+1];
3452 list->nbItems--;
3453 }
3454 return(0);
3455}
3456
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003457/**
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003458 * xmlSchemaItemListFree:
3459 * @annot: a schema type structure
3460 *
3461 * Deallocate a annotation structure
3462 */
3463static void
3464xmlSchemaItemListFree(xmlSchemaItemListPtr list)
3465{
3466 if (list == NULL)
3467 return;
3468 if (list->items != NULL)
3469 xmlFree(list->items);
3470 xmlFree(list);
3471}
3472
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003473static void
3474xmlSchemaBucketFree(xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003475{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003476 if (bucket == NULL)
3477 return;
3478 if (bucket->globals != NULL) {
3479 xmlSchemaComponentListFree(bucket->globals);
3480 xmlSchemaItemListFree(bucket->globals);
3481 }
3482 if (bucket->locals != NULL) {
3483 xmlSchemaComponentListFree(bucket->locals);
3484 xmlSchemaItemListFree(bucket->locals);
3485 }
3486 if (bucket->relations != NULL) {
3487 xmlSchemaSchemaRelationPtr prev, cur = bucket->relations;
3488 do {
3489 prev = cur;
3490 cur = cur->next;
3491 xmlFree(prev);
3492 } while (cur != NULL);
3493 }
3494 if ((! bucket->preserveDoc) && (bucket->doc != NULL)) {
3495 xmlFreeDoc(bucket->doc);
3496 }
3497 if (bucket->type == XML_SCHEMA_SCHEMA_IMPORT) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003498 if (WXS_IMPBUCKET(bucket)->schema != NULL)
3499 xmlSchemaFree(WXS_IMPBUCKET(bucket)->schema);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003500 }
3501 xmlFree(bucket);
3502}
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003503
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003504static xmlSchemaBucketPtr
3505xmlSchemaBucketCreate(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003506 int type, const xmlChar *targetNamespace)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003507{
3508 xmlSchemaBucketPtr ret;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003509 int size;
3510 xmlSchemaPtr mainSchema;
3511
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003512 if (WXS_CONSTRUCTOR(pctxt)->mainSchema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003513 PERROR_INT("xmlSchemaBucketCreate",
3514 "no main schema on constructor");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003515 return(NULL);
3516 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003517 mainSchema = WXS_CONSTRUCTOR(pctxt)->mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003518 /* Create the schema bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003519 if (WXS_IS_BUCKET_INCREDEF(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003520 size = sizeof(xmlSchemaInclude);
3521 else
3522 size = sizeof(xmlSchemaImport);
3523 ret = (xmlSchemaBucketPtr) xmlMalloc(size);
3524 if (ret == NULL) {
3525 xmlSchemaPErrMemory(NULL, "allocating schema bucket", NULL);
3526 return(NULL);
3527 }
3528 memset(ret, 0, size);
3529 ret->targetNamespace = targetNamespace;
3530 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003531 ret->globals = xmlSchemaItemListCreate();
3532 if (ret->globals == NULL) {
3533 xmlFree(ret);
3534 return(NULL);
3535 }
3536 ret->locals = xmlSchemaItemListCreate();
3537 if (ret->locals == NULL) {
3538 xmlFree(ret);
3539 return(NULL);
3540 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003541 /*
3542 * The following will assure that only the first bucket is marked as
3543 * XML_SCHEMA_SCHEMA_MAIN and it points to the *main* schema.
3544 * For each following import buckets an xmlSchema will be created.
3545 */
3546 if (! WXS_HAS_BUCKETS(pctxt)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003547 if (WXS_IS_BUCKET_INCREDEF(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003548 PERROR_INT("xmlSchemaBucketCreate",
3549 "first bucket but it's an include or redefine");
3550 xmlSchemaBucketFree(ret);
3551 return(NULL);
3552 }
3553 /* Force the type to be XML_SCHEMA_SCHEMA_MAIN. */
3554 ret->type = XML_SCHEMA_SCHEMA_MAIN;
3555 /* Point to the *main* schema. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003556 WXS_CONSTRUCTOR(pctxt)->mainBucket = ret;
3557 WXS_IMPBUCKET(ret)->schema = mainSchema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003558 } else {
3559 if (type == XML_SCHEMA_SCHEMA_MAIN) {
3560 PERROR_INT("xmlSchemaBucketCreate",
3561 "main bucket but it's not the first one");
3562 xmlSchemaBucketFree(ret);
3563 return(NULL);
3564 } else if (type == XML_SCHEMA_SCHEMA_IMPORT) {
3565 /*
3566 * Create a schema for imports.
3567 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003568 WXS_IMPBUCKET(ret)->schema = xmlSchemaNewSchema(pctxt);
3569 if (WXS_IMPBUCKET(ret)->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003570 xmlSchemaBucketFree(ret);
3571 return(NULL);
3572 }
3573 }
3574 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003575 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003576 int res;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003577 /* Imports go into the "schemasImports" slot of the main *schema*. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003578 if (mainSchema->schemasImports == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003579 mainSchema->schemasImports = xmlHashCreateDict(5,
3580 WXS_CONSTRUCTOR(pctxt)->dict);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003581 if (mainSchema->schemasImports == NULL) {
3582 xmlSchemaBucketFree(ret);
3583 return(NULL);
3584 }
3585 }
3586 if (targetNamespace == NULL)
3587 res = xmlHashAddEntry(mainSchema->schemasImports,
3588 XML_SCHEMAS_NO_NAMESPACE, ret);
3589 else
3590 res = xmlHashAddEntry(mainSchema->schemasImports,
3591 targetNamespace, ret);
3592 if (res != 0) {
3593 PERROR_INT("xmlSchemaBucketCreate",
3594 "failed to add the schema bucket to the hash");
3595 xmlSchemaBucketFree(ret);
3596 return(NULL);
3597 }
3598 } else {
3599 /* Set the @ownerImport of an include bucket. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003600 if (WXS_IS_BUCKET_IMPMAIN(WXS_CONSTRUCTOR(pctxt)->bucket->type))
3601 WXS_INCBUCKET(ret)->ownerImport =
3602 WXS_IMPBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003603 else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003604 WXS_INCBUCKET(ret)->ownerImport =
3605 WXS_INCBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket)->ownerImport;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003606
3607 /* Includes got into the "includes" slot of the *main* schema. */
3608 if (mainSchema->includes == NULL) {
3609 mainSchema->includes = xmlSchemaItemListCreate();
3610 if (mainSchema->includes == NULL) {
3611 xmlSchemaBucketFree(ret);
3612 return(NULL);
3613 }
3614 }
3615 xmlSchemaItemListAdd(mainSchema->includes, ret);
3616 }
3617 /*
3618 * Add to list of all buckets; this is used for lookup
3619 * during schema construction time only.
3620 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003621 if (xmlSchemaItemListAdd(WXS_CONSTRUCTOR(pctxt)->buckets, ret) == -1)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003622 return(NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00003623 return(ret);
3624}
3625
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003626static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003627xmlSchemaAddItemSize(xmlSchemaItemListPtr *list, int initialSize, void *item)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003628{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003629 if (*list == NULL) {
3630 *list = xmlSchemaItemListCreate();
3631 if (*list == NULL)
3632 return(-1);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003633 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003634 xmlSchemaItemListAddSize(*list, initialSize, item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003635 return(0);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003636}
3637
3638/**
Daniel Veillardfdc91562002-07-01 21:52:03 +00003639 * xmlSchemaFreeAnnot:
3640 * @annot: a schema type structure
3641 *
3642 * Deallocate a annotation structure
3643 */
3644static void
3645xmlSchemaFreeAnnot(xmlSchemaAnnotPtr annot)
3646{
3647 if (annot == NULL)
3648 return;
Kasimier T. Buchcik004b5462005-08-08 12:43:09 +00003649 if (annot->next == NULL) {
3650 xmlFree(annot);
3651 } else {
3652 xmlSchemaAnnotPtr prev;
3653
3654 do {
3655 prev = annot;
3656 annot = annot->next;
3657 xmlFree(prev);
3658 } while (annot != NULL);
3659 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00003660}
3661
3662/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003663 * xmlSchemaFreeNotation:
3664 * @schema: a schema notation structure
3665 *
3666 * Deallocate a Schema Notation structure.
3667 */
3668static void
3669xmlSchemaFreeNotation(xmlSchemaNotationPtr nota)
3670{
3671 if (nota == NULL)
3672 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003673 xmlFree(nota);
3674}
3675
3676/**
3677 * xmlSchemaFreeAttribute:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003678 * @attr: an attribute declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00003679 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003680 * Deallocates an attribute declaration structure.
Daniel Veillard4255d502002-04-16 15:50:10 +00003681 */
3682static void
3683xmlSchemaFreeAttribute(xmlSchemaAttributePtr attr)
3684{
3685 if (attr == NULL)
3686 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003687 if (attr->annot != NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00003688 xmlSchemaFreeAnnot(attr->annot);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003689 if (attr->defVal != NULL)
3690 xmlSchemaFreeValue(attr->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003691 xmlFree(attr);
3692}
3693
3694/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003695 * xmlSchemaFreeAttributeUse:
3696 * @use: an attribute use
3697 *
3698 * Deallocates an attribute use structure.
3699 */
3700static void
3701xmlSchemaFreeAttributeUse(xmlSchemaAttributeUsePtr use)
3702{
3703 if (use == NULL)
3704 return;
3705 if (use->annot != NULL)
3706 xmlSchemaFreeAnnot(use->annot);
3707 if (use->defVal != NULL)
3708 xmlSchemaFreeValue(use->defVal);
3709 xmlFree(use);
3710}
3711
3712/**
3713 * xmlSchemaFreeAttributeUseProhib:
3714 * @prohib: an attribute use prohibition
3715 *
3716 * Deallocates an attribute use structure.
3717 */
3718static void
3719xmlSchemaFreeAttributeUseProhib(xmlSchemaAttributeUseProhibPtr prohib)
3720{
3721 if (prohib == NULL)
3722 return;
3723 xmlFree(prohib);
3724}
3725
3726/**
Daniel Veillard3646d642004-06-02 19:19:14 +00003727 * xmlSchemaFreeWildcardNsSet:
3728 * set: a schema wildcard namespace
3729 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003730 * Deallocates a list of wildcard constraint structures.
Daniel Veillard3646d642004-06-02 19:19:14 +00003731 */
3732static void
3733xmlSchemaFreeWildcardNsSet(xmlSchemaWildcardNsPtr set)
3734{
3735 xmlSchemaWildcardNsPtr next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003736
Daniel Veillard3646d642004-06-02 19:19:14 +00003737 while (set != NULL) {
3738 next = set->next;
3739 xmlFree(set);
3740 set = next;
3741 }
3742}
3743
3744/**
3745 * xmlSchemaFreeWildcard:
Daniel Veillard01fa6152004-06-29 17:04:39 +00003746 * @wildcard: a wildcard structure
Daniel Veillard3646d642004-06-02 19:19:14 +00003747 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00003748 * Deallocates a wildcard structure.
Daniel Veillard3646d642004-06-02 19:19:14 +00003749 */
Daniel Veillard01fa6152004-06-29 17:04:39 +00003750void
Daniel Veillard3646d642004-06-02 19:19:14 +00003751xmlSchemaFreeWildcard(xmlSchemaWildcardPtr wildcard)
3752{
3753 if (wildcard == NULL)
3754 return;
3755 if (wildcard->annot != NULL)
3756 xmlSchemaFreeAnnot(wildcard->annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003757 if (wildcard->nsSet != NULL)
3758 xmlSchemaFreeWildcardNsSet(wildcard->nsSet);
3759 if (wildcard->negNsSet != NULL)
3760 xmlFree(wildcard->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +00003761 xmlFree(wildcard);
3762}
3763
3764/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003765 * xmlSchemaFreeAttributeGroup:
3766 * @schema: a schema attribute group structure
3767 *
3768 * Deallocate a Schema Attribute Group structure.
3769 */
3770static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003771xmlSchemaFreeAttributeGroup(xmlSchemaAttributeGroupPtr attrGr)
Daniel Veillard4255d502002-04-16 15:50:10 +00003772{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003773 if (attrGr == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00003774 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003775 if (attrGr->annot != NULL)
3776 xmlSchemaFreeAnnot(attrGr->annot);
3777 if (attrGr->attrUses != NULL)
3778 xmlSchemaItemListFree(WXS_LIST_CAST attrGr->attrUses);
3779 xmlFree(attrGr);
Daniel Veillard3646d642004-06-02 19:19:14 +00003780}
3781
3782/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003783 * xmlSchemaFreeQNameRef:
3784 * @item: a QName reference structure
3785 *
3786 * Deallocatea a QName reference structure.
3787 */
3788static void
3789xmlSchemaFreeQNameRef(xmlSchemaQNameRefPtr item)
3790{
3791 xmlFree(item);
3792}
3793
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00003794/**
Daniel Veillard01fa6152004-06-29 17:04:39 +00003795 * xmlSchemaFreeTypeLinkList:
3796 * @alink: a type link
3797 *
3798 * Deallocate a list of types.
3799 */
3800static void
3801xmlSchemaFreeTypeLinkList(xmlSchemaTypeLinkPtr link)
3802{
3803 xmlSchemaTypeLinkPtr next;
3804
3805 while (link != NULL) {
3806 next = link->next;
3807 xmlFree(link);
3808 link = next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003809 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00003810}
3811
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003812static void
3813xmlSchemaFreeIDCStateObjList(xmlSchemaIDCStateObjPtr sto)
3814{
3815 xmlSchemaIDCStateObjPtr next;
3816 while (sto != NULL) {
3817 next = sto->next;
3818 if (sto->history != NULL)
3819 xmlFree(sto->history);
3820 if (sto->xpathCtxt != NULL)
3821 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
3822 xmlFree(sto);
3823 sto = next;
3824 }
3825}
3826
3827/**
3828 * xmlSchemaFreeIDC:
3829 * @idc: a identity-constraint definition
3830 *
3831 * Deallocates an identity-constraint definition.
3832 */
3833static void
3834xmlSchemaFreeIDC(xmlSchemaIDCPtr idcDef)
3835{
3836 xmlSchemaIDCSelectPtr cur, prev;
3837
3838 if (idcDef == NULL)
3839 return;
3840 if (idcDef->annot != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003841 xmlSchemaFreeAnnot(idcDef->annot);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003842 /* Selector */
3843 if (idcDef->selector != NULL) {
3844 if (idcDef->selector->xpathComp != NULL)
3845 xmlFreePattern((xmlPatternPtr) idcDef->selector->xpathComp);
3846 xmlFree(idcDef->selector);
3847 }
3848 /* Fields */
3849 if (idcDef->fields != NULL) {
3850 cur = idcDef->fields;
3851 do {
3852 prev = cur;
3853 cur = cur->next;
3854 if (prev->xpathComp != NULL)
3855 xmlFreePattern((xmlPatternPtr) prev->xpathComp);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003856 xmlFree(prev);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003857 } while (cur != NULL);
3858 }
3859 xmlFree(idcDef);
3860}
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00003861
Daniel Veillard01fa6152004-06-29 17:04:39 +00003862/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003863 * xmlSchemaFreeElement:
3864 * @schema: a schema element structure
3865 *
3866 * Deallocate a Schema Element structure.
3867 */
3868static void
3869xmlSchemaFreeElement(xmlSchemaElementPtr elem)
3870{
3871 if (elem == NULL)
3872 return;
Daniel Veillard32370232002-10-16 14:08:14 +00003873 if (elem->annot != NULL)
3874 xmlSchemaFreeAnnot(elem->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003875 if (elem->contModel != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003876 xmlRegFreeRegexp(elem->contModel);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00003877 if (elem->defVal != NULL)
3878 xmlSchemaFreeValue(elem->defVal);
Daniel Veillard4255d502002-04-16 15:50:10 +00003879 xmlFree(elem);
3880}
3881
3882/**
3883 * xmlSchemaFreeFacet:
3884 * @facet: a schema facet structure
3885 *
3886 * Deallocate a Schema Facet structure.
3887 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003888void
Daniel Veillard4255d502002-04-16 15:50:10 +00003889xmlSchemaFreeFacet(xmlSchemaFacetPtr facet)
3890{
3891 if (facet == NULL)
3892 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003893 if (facet->val != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003894 xmlSchemaFreeValue(facet->val);
Daniel Veillard4255d502002-04-16 15:50:10 +00003895 if (facet->regexp != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003896 xmlRegFreeRegexp(facet->regexp);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003897 if (facet->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003898 xmlSchemaFreeAnnot(facet->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003899 xmlFree(facet);
3900}
3901
3902/**
3903 * xmlSchemaFreeType:
3904 * @type: a schema type structure
3905 *
3906 * Deallocate a Schema Type structure.
3907 */
3908void
3909xmlSchemaFreeType(xmlSchemaTypePtr type)
3910{
3911 if (type == NULL)
3912 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00003913 if (type->annot != NULL)
Daniel Veillard32370232002-10-16 14:08:14 +00003914 xmlSchemaFreeAnnot(type->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00003915 if (type->facets != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003916 xmlSchemaFacetPtr facet, next;
Daniel Veillard4255d502002-04-16 15:50:10 +00003917
Daniel Veillardd0c9c322003-10-10 00:49:42 +00003918 facet = type->facets;
3919 while (facet != NULL) {
3920 next = facet->next;
3921 xmlSchemaFreeFacet(facet);
3922 facet = next;
3923 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003924 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003925 if (type->attrUses != NULL)
3926 xmlSchemaItemListFree((xmlSchemaItemListPtr) type->attrUses);
Daniel Veillard01fa6152004-06-29 17:04:39 +00003927 if (type->memberTypes != NULL)
3928 xmlSchemaFreeTypeLinkList(type->memberTypes);
3929 if (type->facetSet != NULL) {
3930 xmlSchemaFacetLinkPtr next, link;
3931
3932 link = type->facetSet;
3933 do {
3934 next = link->next;
3935 xmlFree(link);
3936 link = next;
3937 } while (link != NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00003938 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00003939 if (type->contModel != NULL)
3940 xmlRegFreeRegexp(type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +00003941 xmlFree(type);
3942}
3943
3944/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00003945 * xmlSchemaFreeModelGroupDef:
3946 * @item: a schema model group definition
3947 *
3948 * Deallocates a schema model group definition.
3949 */
3950static void
3951xmlSchemaFreeModelGroupDef(xmlSchemaModelGroupDefPtr item)
3952{
3953 if (item->annot != NULL)
3954 xmlSchemaFreeAnnot(item->annot);
3955 xmlFree(item);
3956}
3957
3958/**
3959 * xmlSchemaFreeModelGroup:
3960 * @item: a schema model group
3961 *
3962 * Deallocates a schema model group structure.
3963 */
3964static void
3965xmlSchemaFreeModelGroup(xmlSchemaModelGroupPtr item)
3966{
3967 if (item->annot != NULL)
3968 xmlSchemaFreeAnnot(item->annot);
3969 xmlFree(item);
3970}
3971
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003972static void
3973xmlSchemaComponentListFree(xmlSchemaItemListPtr list)
3974{
3975 if ((list == NULL) || (list->nbItems == 0))
3976 return;
3977 {
3978 xmlSchemaTreeItemPtr item;
3979 xmlSchemaTreeItemPtr *items = (xmlSchemaTreeItemPtr *) list->items;
3980 int i;
3981
3982 for (i = 0; i < list->nbItems; i++) {
3983 item = items[i];
3984 if (item == NULL)
3985 continue;
3986 switch (item->type) {
3987 case XML_SCHEMA_TYPE_SIMPLE:
3988 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003989 xmlSchemaFreeType((xmlSchemaTypePtr) item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00003990 break;
3991 case XML_SCHEMA_TYPE_ATTRIBUTE:
3992 xmlSchemaFreeAttribute((xmlSchemaAttributePtr) item);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00003993 break;
3994 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
3995 xmlSchemaFreeAttributeUse((xmlSchemaAttributeUsePtr) item);
3996 break;
3997 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
3998 xmlSchemaFreeAttributeUseProhib(
3999 (xmlSchemaAttributeUseProhibPtr) item);
4000 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004001 case XML_SCHEMA_TYPE_ELEMENT:
4002 xmlSchemaFreeElement((xmlSchemaElementPtr) item);
4003 break;
4004 case XML_SCHEMA_TYPE_PARTICLE:
4005 if (item->annot != NULL)
4006 xmlSchemaFreeAnnot(item->annot);
4007 xmlFree(item);
4008 break;
4009 case XML_SCHEMA_TYPE_SEQUENCE:
4010 case XML_SCHEMA_TYPE_CHOICE:
4011 case XML_SCHEMA_TYPE_ALL:
4012 xmlSchemaFreeModelGroup((xmlSchemaModelGroupPtr) item);
4013 break;
4014 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
4015 xmlSchemaFreeAttributeGroup(
4016 (xmlSchemaAttributeGroupPtr) item);
4017 break;
4018 case XML_SCHEMA_TYPE_GROUP:
4019 xmlSchemaFreeModelGroupDef(
4020 (xmlSchemaModelGroupDefPtr) item);
4021 break;
4022 case XML_SCHEMA_TYPE_ANY:
4023 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
4024 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr) item);
4025 break;
4026 case XML_SCHEMA_TYPE_IDC_KEY:
4027 case XML_SCHEMA_TYPE_IDC_UNIQUE:
4028 case XML_SCHEMA_TYPE_IDC_KEYREF:
4029 xmlSchemaFreeIDC((xmlSchemaIDCPtr) item);
4030 break;
4031 case XML_SCHEMA_TYPE_NOTATION:
4032 xmlSchemaFreeNotation((xmlSchemaNotationPtr) item);
4033 break;
4034 case XML_SCHEMA_EXTRA_QNAMEREF:
4035 xmlSchemaFreeQNameRef((xmlSchemaQNameRefPtr) item);
4036 break;
4037 default: {
4038 /* TODO: This should never be hit. */
4039 xmlSchemaPSimpleInternalErr(NULL,
4040 "Internal error: xmlSchemaComponentListFree, "
4041 "unexpected component type '%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004042 (const xmlChar *) WXS_ITEM_TYPE_NAME(item));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004043 }
4044 break;
4045 }
4046 }
4047 list->nbItems = 0;
4048 }
4049}
4050
Daniel Veillardb0f397e2003-12-23 23:30:53 +00004051/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004052 * xmlSchemaFree:
4053 * @schema: a schema structure
4054 *
4055 * Deallocate a Schema structure.
4056 */
4057void
4058xmlSchemaFree(xmlSchemaPtr schema)
4059{
4060 if (schema == NULL)
4061 return;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004062 /* @volatiles is not used anymore :-/ */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004063 if (schema->volatiles != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004064 TODO
4065 /*
4066 * Note that those slots are not responsible for freeing
4067 * schema components anymore; this will now be done by
4068 * the schema buckets.
4069 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004070 if (schema->notaDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004071 xmlHashFree(schema->notaDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004072 if (schema->attrDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004073 xmlHashFree(schema->attrDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004074 if (schema->attrgrpDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004075 xmlHashFree(schema->attrgrpDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004076 if (schema->elemDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004077 xmlHashFree(schema->elemDecl, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004078 if (schema->typeDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004079 xmlHashFree(schema->typeDecl, NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004080 if (schema->groupDecl != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004081 xmlHashFree(schema->groupDecl, NULL);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004082 if (schema->idcDef != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004083 xmlHashFree(schema->idcDef, NULL);
4084
Daniel Veillard1d913862003-11-21 00:28:39 +00004085 if (schema->schemasImports != NULL)
4086 xmlHashFree(schema->schemasImports,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004087 (xmlHashDeallocator) xmlSchemaBucketFree);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004088 if (schema->includes != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004089 xmlSchemaItemListPtr list = (xmlSchemaItemListPtr) schema->includes;
4090 int i;
4091 for (i = 0; i < list->nbItems; i++) {
4092 xmlSchemaBucketFree((xmlSchemaBucketPtr) list->items[i]);
4093 }
4094 xmlSchemaItemListFree(list);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00004095 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004096 if (schema->annot != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004097 xmlSchemaFreeAnnot(schema->annot);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004098 /* Never free the doc here, since this will be done by the buckets. */
4099
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004100 xmlDictFree(schema->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +00004101 xmlFree(schema);
4102}
4103
4104/************************************************************************
4105 * *
Daniel Veillard4255d502002-04-16 15:50:10 +00004106 * Debug functions *
4107 * *
4108 ************************************************************************/
4109
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004110#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004111
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004112static void
4113xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output); /* forward */
4114
Daniel Veillard4255d502002-04-16 15:50:10 +00004115/**
4116 * xmlSchemaElementDump:
4117 * @elem: an element
4118 * @output: the file output
4119 *
4120 * Dump the element
4121 */
4122static void
4123xmlSchemaElementDump(xmlSchemaElementPtr elem, FILE * output,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004124 const xmlChar * name ATTRIBUTE_UNUSED,
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004125 const xmlChar * namespace ATTRIBUTE_UNUSED,
4126 const xmlChar * context ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00004127{
4128 if (elem == NULL)
4129 return;
4130
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004131
4132 fprintf(output, "Element");
4133 if (elem->flags & XML_SCHEMAS_ELEM_GLOBAL)
4134 fprintf(output, " (global)");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004135 fprintf(output, ": '%s' ", elem->name);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004136 if (namespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004137 fprintf(output, "ns '%s'", namespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004138 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004139#if 0
Daniel Veillard4255d502002-04-16 15:50:10 +00004140 if ((elem->minOccurs != 1) || (elem->maxOccurs != 1)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004141 fprintf(output, " min %d ", elem->minOccurs);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004142 if (elem->maxOccurs >= UNBOUNDED)
4143 fprintf(output, "max: unbounded\n");
4144 else if (elem->maxOccurs != 1)
4145 fprintf(output, "max: %d\n", elem->maxOccurs);
4146 else
4147 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004148 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004149#endif
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004150 /*
4151 * Misc other properties.
4152 */
4153 if ((elem->flags & XML_SCHEMAS_ELEM_NILLABLE) ||
4154 (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT) ||
4155 (elem->flags & XML_SCHEMAS_ELEM_FIXED) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004156 (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)) {
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004157 fprintf(output, " props: ");
4158 if (elem->flags & XML_SCHEMAS_ELEM_FIXED)
4159 fprintf(output, "[fixed] ");
4160 if (elem->flags & XML_SCHEMAS_ELEM_DEFAULT)
4161 fprintf(output, "[default] ");
4162 if (elem->flags & XML_SCHEMAS_ELEM_ABSTRACT)
4163 fprintf(output, "[abstract] ");
4164 if (elem->flags & XML_SCHEMAS_ELEM_NILLABLE)
4165 fprintf(output, "[nillable] ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004166 fprintf(output, "\n");
Daniel Veillard4255d502002-04-16 15:50:10 +00004167 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004168 /*
4169 * Default/fixed value.
4170 */
Daniel Veillard4255d502002-04-16 15:50:10 +00004171 if (elem->value != NULL)
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004172 fprintf(output, " value: '%s'\n", elem->value);
4173 /*
4174 * Type.
4175 */
4176 if (elem->namedType != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004177 fprintf(output, " type: '%s' ", elem->namedType);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004178 if (elem->namedTypeNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004179 fprintf(output, "ns '%s'\n", elem->namedTypeNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004180 else
4181 fprintf(output, "\n");
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004182 } else if (elem->subtypes != NULL) {
4183 /*
4184 * Dump local types.
4185 */
4186 xmlSchemaTypeDump(elem->subtypes, output);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004187 }
4188 /*
4189 * Substitution group.
4190 */
4191 if (elem->substGroup != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004192 fprintf(output, " substitutionGroup: '%s' ", elem->substGroup);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004193 if (elem->substGroupNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004194 fprintf(output, "ns '%s'\n", elem->substGroupNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004195 else
4196 fprintf(output, "\n");
4197 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004198}
4199
4200/**
4201 * xmlSchemaAnnotDump:
4202 * @output: the file output
4203 * @annot: a annotation
4204 *
4205 * Dump the annotation
4206 */
4207static void
4208xmlSchemaAnnotDump(FILE * output, xmlSchemaAnnotPtr annot)
4209{
4210 xmlChar *content;
4211
4212 if (annot == NULL)
4213 return;
4214
4215 content = xmlNodeGetContent(annot->content);
4216 if (content != NULL) {
4217 fprintf(output, " Annot: %s\n", content);
4218 xmlFree(content);
4219 } else
4220 fprintf(output, " Annot: empty\n");
4221}
4222
4223/**
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004224 * xmlSchemaContentModelDump:
4225 * @particle: the schema particle
4226 * @output: the file output
4227 * @depth: the depth used for intentation
Daniel Veillard4255d502002-04-16 15:50:10 +00004228 *
4229 * Dump a SchemaType structure
4230 */
4231static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004232xmlSchemaContentModelDump(xmlSchemaParticlePtr particle, FILE * output, int depth)
4233{
4234 xmlChar *str = NULL;
4235 xmlSchemaTreeItemPtr term;
4236 char shift[100];
4237 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004238
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004239 if (particle == NULL)
4240 return;
4241 for (i = 0;((i < depth) && (i < 25));i++)
4242 shift[2 * i] = shift[2 * i + 1] = ' ';
4243 shift[2 * i] = shift[2 * i + 1] = 0;
4244 fprintf(output, shift);
4245 if (particle->children == NULL) {
4246 fprintf(output, "MISSING particle term\n");
4247 return;
4248 }
4249 term = particle->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004250 if (term == NULL) {
4251 fprintf(output, "(NULL)");
4252 } else {
4253 switch (term->type) {
4254 case XML_SCHEMA_TYPE_ELEMENT:
4255 fprintf(output, "ELEM '%s'", xmlSchemaFormatQName(&str,
4256 ((xmlSchemaElementPtr)term)->targetNamespace,
4257 ((xmlSchemaElementPtr)term)->name));
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004258 FREE_AND_NULL(str);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004259 break;
4260 case XML_SCHEMA_TYPE_SEQUENCE:
4261 fprintf(output, "SEQUENCE");
4262 break;
4263 case XML_SCHEMA_TYPE_CHOICE:
4264 fprintf(output, "CHOICE");
4265 break;
4266 case XML_SCHEMA_TYPE_ALL:
4267 fprintf(output, "ALL");
4268 break;
4269 case XML_SCHEMA_TYPE_ANY:
4270 fprintf(output, "ANY");
4271 break;
4272 default:
4273 fprintf(output, "UNKNOWN\n");
4274 return;
4275 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004276 }
4277 if (particle->minOccurs != 1)
4278 fprintf(output, " min: %d", particle->minOccurs);
4279 if (particle->maxOccurs >= UNBOUNDED)
4280 fprintf(output, " max: unbounded");
4281 else if (particle->maxOccurs != 1)
4282 fprintf(output, " max: %d", particle->maxOccurs);
4283 fprintf(output, "\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004284 if (term &&
4285 ((term->type == XML_SCHEMA_TYPE_SEQUENCE) ||
4286 (term->type == XML_SCHEMA_TYPE_CHOICE) ||
4287 (term->type == XML_SCHEMA_TYPE_ALL)) &&
4288 (term->children != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004289 xmlSchemaContentModelDump((xmlSchemaParticlePtr) term->children,
4290 output, depth +1);
4291 }
4292 if (particle->next != NULL)
4293 xmlSchemaContentModelDump((xmlSchemaParticlePtr) particle->next,
4294 output, depth);
4295}
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004296
4297/**
4298 * xmlSchemaAttrUsesDump:
4299 * @uses: attribute uses list
4300 * @output: the file output
4301 *
4302 * Dumps a list of attribute use components.
4303 */
4304static void
4305xmlSchemaAttrUsesDump(xmlSchemaItemListPtr uses, FILE * output)
4306{
4307 xmlSchemaAttributeUsePtr use;
4308 xmlSchemaAttributeUseProhibPtr prohib;
4309 xmlSchemaQNameRefPtr ref;
4310 const xmlChar *name, *tns;
4311 xmlChar *str = NULL;
4312 int i;
4313
4314 if ((uses == NULL) || (uses->nbItems == 0))
4315 return;
4316
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004317 fprintf(output, " attributes:\n");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004318 for (i = 0; i < uses->nbItems; i++) {
4319 use = uses->items[i];
4320 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
4321 fprintf(output, " [prohibition] ");
4322 prohib = (xmlSchemaAttributeUseProhibPtr) use;
4323 name = prohib->name;
4324 tns = prohib->targetNamespace;
4325 } else if (use->type == XML_SCHEMA_EXTRA_QNAMEREF) {
4326 fprintf(output, " [reference] ");
4327 ref = (xmlSchemaQNameRefPtr) use;
4328 name = ref->name;
4329 tns = ref->targetNamespace;
4330 } else {
4331 fprintf(output, " [use] ");
4332 name = WXS_ATTRUSE_DECL_NAME(use);
4333 tns = WXS_ATTRUSE_DECL_TNS(use);
4334 }
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004335 fprintf(output, "'%s'\n",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004336 (const char *) xmlSchemaFormatQName(&str, tns, name));
4337 FREE_AND_NULL(str);
4338 }
4339}
4340
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004341/**
4342 * xmlSchemaTypeDump:
4343 * @output: the file output
4344 * @type: a type structure
4345 *
4346 * Dump a SchemaType structure
4347 */
4348static void
Daniel Veillard4255d502002-04-16 15:50:10 +00004349xmlSchemaTypeDump(xmlSchemaTypePtr type, FILE * output)
4350{
4351 if (type == NULL) {
4352 fprintf(output, "Type: NULL\n");
4353 return;
4354 }
4355 fprintf(output, "Type: ");
4356 if (type->name != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004357 fprintf(output, "'%s' ", type->name);
Daniel Veillard4255d502002-04-16 15:50:10 +00004358 else
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004359 fprintf(output, "(no name) ");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004360 if (type->targetNamespace != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004361 fprintf(output, "ns '%s' ", type->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004362 switch (type->type) {
4363 case XML_SCHEMA_TYPE_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004364 fprintf(output, "[basic] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004365 break;
4366 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004367 fprintf(output, "[simple] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004368 break;
4369 case XML_SCHEMA_TYPE_COMPLEX:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004370 fprintf(output, "[complex] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004371 break;
4372 case XML_SCHEMA_TYPE_SEQUENCE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004373 fprintf(output, "[sequence] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004374 break;
4375 case XML_SCHEMA_TYPE_CHOICE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004376 fprintf(output, "[choice] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004377 break;
4378 case XML_SCHEMA_TYPE_ALL:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004379 fprintf(output, "[all] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004380 break;
4381 case XML_SCHEMA_TYPE_UR:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004382 fprintf(output, "[ur] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004383 break;
4384 case XML_SCHEMA_TYPE_RESTRICTION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004385 fprintf(output, "[restriction] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004386 break;
4387 case XML_SCHEMA_TYPE_EXTENSION:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004388 fprintf(output, "[extension] ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004389 break;
4390 default:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004391 fprintf(output, "[unknown type %d] ", type->type);
Daniel Veillard4255d502002-04-16 15:50:10 +00004392 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004393 }
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004394 fprintf(output, "content: ");
Daniel Veillard4255d502002-04-16 15:50:10 +00004395 switch (type->contentType) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004396 case XML_SCHEMA_CONTENT_UNKNOWN:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004397 fprintf(output, "[unknown] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004398 break;
4399 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004400 fprintf(output, "[empty] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004401 break;
4402 case XML_SCHEMA_CONTENT_ELEMENTS:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004403 fprintf(output, "[element] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004404 break;
4405 case XML_SCHEMA_CONTENT_MIXED:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004406 fprintf(output, "[mixed] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004407 break;
4408 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004409 /* not used. */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004410 break;
4411 case XML_SCHEMA_CONTENT_BASIC:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004412 fprintf(output, "[basic] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004413 break;
4414 case XML_SCHEMA_CONTENT_SIMPLE:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004415 fprintf(output, "[simple] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004416 break;
4417 case XML_SCHEMA_CONTENT_ANY:
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004418 fprintf(output, "[any] ");
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004419 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00004420 }
4421 fprintf(output, "\n");
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004422 if (type->base != NULL) {
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004423 fprintf(output, " base type: '%s'", type->base);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004424 if (type->baseNs != NULL)
Kasimier T. Buchcikee8e8ae2005-10-17 14:15:37 +00004425 fprintf(output, " ns '%s'\n", type->baseNs);
Kasimier T. Buchcikf500aec2005-01-19 16:30:24 +00004426 else
4427 fprintf(output, "\n");
4428 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004429 if (type->attrUses != NULL)
4430 xmlSchemaAttrUsesDump(type->attrUses, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004431 if (type->annot != NULL)
4432 xmlSchemaAnnotDump(output, type->annot);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004433#ifdef DUMP_CONTENT_MODEL
4434 if ((type->type == XML_SCHEMA_TYPE_COMPLEX) &&
4435 (type->subtypes != NULL)) {
4436 xmlSchemaContentModelDump((xmlSchemaParticlePtr) type->subtypes,
4437 output, 1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004438 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004439#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00004440}
4441
4442/**
4443 * xmlSchemaDump:
4444 * @output: the file output
4445 * @schema: a schema structure
4446 *
4447 * Dump a Schema structure.
4448 */
4449void
4450xmlSchemaDump(FILE * output, xmlSchemaPtr schema)
4451{
Daniel Veillardce682bc2004-11-05 17:22:25 +00004452 if (output == NULL)
4453 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004454 if (schema == NULL) {
4455 fprintf(output, "Schemas: NULL\n");
4456 return;
4457 }
4458 fprintf(output, "Schemas: ");
4459 if (schema->name != NULL)
4460 fprintf(output, "%s, ", schema->name);
4461 else
4462 fprintf(output, "no name, ");
4463 if (schema->targetNamespace != NULL)
Daniel Veillard580ced82003-03-21 21:22:48 +00004464 fprintf(output, "%s", (const char *) schema->targetNamespace);
Daniel Veillard4255d502002-04-16 15:50:10 +00004465 else
4466 fprintf(output, "no target namespace");
4467 fprintf(output, "\n");
4468 if (schema->annot != NULL)
4469 xmlSchemaAnnotDump(output, schema->annot);
Daniel Veillard4255d502002-04-16 15:50:10 +00004470 xmlHashScan(schema->typeDecl, (xmlHashScanner) xmlSchemaTypeDump,
4471 output);
4472 xmlHashScanFull(schema->elemDecl,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004473 (xmlHashScannerFull) xmlSchemaElementDump, output);
Daniel Veillard4255d502002-04-16 15:50:10 +00004474}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004475
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004476#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004477/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004478 * xmlSchemaDebugDumpIDCTable:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004479 * @vctxt: the WXS validation context
4480 *
4481 * Displays the current IDC table for debug purposes.
4482 */
4483static void
4484xmlSchemaDebugDumpIDCTable(FILE * output,
4485 const xmlChar *namespaceName,
4486 const xmlChar *localName,
4487 xmlSchemaPSVIIDCBindingPtr bind)
4488{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004489 xmlChar *str = NULL;
4490 const xmlChar *value;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004491 xmlSchemaPSVIIDCNodePtr tab;
4492 xmlSchemaPSVIIDCKeyPtr key;
4493 int i, j, res;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004494
4495 fprintf(output, "IDC: TABLES on %s\n",
4496 xmlSchemaFormatQName(&str, namespaceName, localName));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004497 FREE_AND_NULL(str)
4498
4499 if (bind == NULL)
4500 return;
4501 do {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004502 fprintf(output, "IDC: BINDING %s\n",
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004503 xmlSchemaGetComponentQName(&str,
4504 bind->definition));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004505 FREE_AND_NULL(str)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004506 for (i = 0; i < bind->nbNodes; i++) {
4507 tab = bind->nodeTable[i];
4508 fprintf(output, " ( ");
4509 for (j = 0; j < bind->definition->nbFields; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004510 key = tab->keys[j];
4511 if ((key != NULL) && (key->val != NULL)) {
4512 res = xmlSchemaGetCanonValue(key->val, &value);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004513 if (res >= 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004514 fprintf(output, "\"%s\" ", value);
4515 else
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004516 fprintf(output, "CANON-VALUE-FAILED ");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004517 if (res == 0)
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00004518 FREE_AND_NULL(value)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004519 } else if (key != NULL)
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00004520 fprintf(output, "(no val), ");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004521 else
4522 fprintf(output, "(key missing), ");
4523 }
4524 fprintf(output, ")\n");
4525 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +00004526 if (bind->dupls && bind->dupls->nbItems) {
4527 fprintf(output, "IDC: dupls:\n");
4528 for (i = 0; i < bind->dupls->nbItems; i++) {
4529 tab = bind->dupls->items[i];
4530 fprintf(output, " ( ");
4531 for (j = 0; j < bind->definition->nbFields; j++) {
4532 key = tab->keys[j];
4533 if ((key != NULL) && (key->val != NULL)) {
4534 res = xmlSchemaGetCanonValue(key->val, &value);
4535 if (res >= 0)
4536 fprintf(output, "\"%s\" ", value);
4537 else
4538 fprintf(output, "CANON-VALUE-FAILED ");
4539 if (res == 0)
4540 FREE_AND_NULL(value)
4541 } else if (key != NULL)
4542 fprintf(output, "(no val), ");
4543 else
4544 fprintf(output, "(key missing), ");
4545 }
4546 fprintf(output, ")\n");
4547 }
4548 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00004549 bind = bind->next;
4550 } while (bind != NULL);
4551}
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00004552#endif /* DEBUG_IDC */
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00004553#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillard4255d502002-04-16 15:50:10 +00004554
4555/************************************************************************
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004556 * *
4557 * Utilities *
4558 * *
4559 ************************************************************************/
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004560
Daniel Veillardc0826a72004-08-10 14:17:33 +00004561/**
4562 * xmlSchemaGetPropNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004563 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004564 * @name: the name of the attribute
4565 *
4566 * Seeks an attribute with a name of @name in
4567 * no namespace.
4568 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004569 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004570 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +00004571static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004572xmlSchemaGetPropNode(xmlNodePtr node, const char *name)
Daniel Veillard01fa6152004-06-29 17:04:39 +00004573{
4574 xmlAttrPtr prop;
4575
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004576 if ((node == NULL) || (name == NULL))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004577 return(NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004578 prop = node->properties;
4579 while (prop != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004580 if ((prop->ns == NULL) && xmlStrEqual(prop->name, BAD_CAST name))
Daniel Veillardc0826a72004-08-10 14:17:33 +00004581 return(prop);
4582 prop = prop->next;
4583 }
4584 return (NULL);
4585}
4586
4587/**
4588 * xmlSchemaGetPropNodeNs:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004589 * @node: the element node
Daniel Veillardc0826a72004-08-10 14:17:33 +00004590 * @uri: the uri
4591 * @name: the name of the attribute
4592 *
4593 * Seeks an attribute with a local name of @name and
4594 * a namespace URI of @uri.
4595 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004596 * Returns the attribute or NULL if not present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00004597 */
4598static xmlAttrPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004599xmlSchemaGetPropNodeNs(xmlNodePtr node, const char *uri, const char *name)
Daniel Veillardc0826a72004-08-10 14:17:33 +00004600{
4601 xmlAttrPtr prop;
4602
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004603 if ((node == NULL) || (name == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004604 return(NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00004605 prop = node->properties;
4606 while (prop != NULL) {
4607 if ((prop->ns != NULL) &&
4608 xmlStrEqual(prop->name, BAD_CAST name) &&
4609 xmlStrEqual(prop->ns->href, BAD_CAST uri))
Daniel Veillard01fa6152004-06-29 17:04:39 +00004610 return(prop);
4611 prop = prop->next;
4612 }
4613 return (NULL);
4614}
4615
4616static const xmlChar *
4617xmlSchemaGetNodeContent(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
4618{
4619 xmlChar *val;
4620 const xmlChar *ret;
4621
4622 val = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004623 if (val == NULL)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00004624 val = xmlStrdup((xmlChar *)"");
Daniel Veillard01fa6152004-06-29 17:04:39 +00004625 ret = xmlDictLookup(ctxt->dict, val, -1);
4626 xmlFree(val);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004627 return(ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +00004628}
4629
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004630static const xmlChar *
4631xmlSchemaGetNodeContentNoDict(xmlNodePtr node)
4632{
4633 return((const xmlChar*) xmlNodeGetContent(node));
4634}
4635
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004636/**
4637 * xmlSchemaGetProp:
4638 * @ctxt: the parser context
4639 * @node: the node
4640 * @name: the property name
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004641 *
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004642 * Read a attribute value and internalize the string
4643 *
4644 * Returns the string or NULL if not present.
4645 */
4646static const xmlChar *
4647xmlSchemaGetProp(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
4648 const char *name)
4649{
4650 xmlChar *val;
4651 const xmlChar *ret;
4652
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004653 val = xmlGetNoNsProp(node, BAD_CAST name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004654 if (val == NULL)
4655 return(NULL);
4656 ret = xmlDictLookup(ctxt->dict, val, -1);
4657 xmlFree(val);
4658 return(ret);
4659}
4660
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004661/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +00004662 * *
4663 * Parsing functions *
4664 * *
4665 ************************************************************************/
4666
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004667#define WXS_FIND_GLOBAL_ITEM(slot) \
4668 if (xmlStrEqual(nsName, schema->targetNamespace)) { \
4669 ret = xmlHashLookup(schema->slot, name); \
4670 if (ret != NULL) goto exit; \
4671 } \
4672 if (xmlHashSize(schema->schemasImports) > 1) { \
4673 xmlSchemaImportPtr import; \
4674 if (nsName == NULL) \
4675 import = xmlHashLookup(schema->schemasImports, \
4676 XML_SCHEMAS_NO_NAMESPACE); \
4677 else \
4678 import = xmlHashLookup(schema->schemasImports, nsName); \
4679 if (import == NULL) \
4680 goto exit; \
4681 ret = xmlHashLookup(import->schema->slot, name); \
4682 }
4683
Daniel Veillard4255d502002-04-16 15:50:10 +00004684/**
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004685 * xmlSchemaGetElem:
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004686 * @schema: the schema context
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004687 * @name: the element name
4688 * @ns: the element namespace
4689 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004690 * Lookup a global element declaration in the schema.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004691 *
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00004692 * Returns the element declaration or NULL if not found.
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004693 */
4694static xmlSchemaElementPtr
4695xmlSchemaGetElem(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004696 const xmlChar * nsName)
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004697{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004698 xmlSchemaElementPtr ret = NULL;
4699
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004700 if ((name == NULL) || (schema == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004701 return(NULL);
4702 if (schema != NULL) {
4703 WXS_FIND_GLOBAL_ITEM(elemDecl)
4704 }
4705exit:
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004706#ifdef DEBUG
4707 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004708 if (nsName == NULL)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004709 fprintf(stderr, "Unable to lookup element decl. %s", name);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004710 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004711 fprintf(stderr, "Unable to lookup element decl. %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004712 nsName);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00004713 }
4714#endif
4715 return (ret);
4716}
4717
4718/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004719 * xmlSchemaGetType:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004720 * @schema: the main schema
4721 * @name: the type's name
4722 * nsName: the type's namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00004723 *
4724 * Lookup a type in the schemas or the predefined types
4725 *
Daniel Veillarda84c0b32003-06-02 16:58:46 +00004726 * Returns the group definition or NULL if not found.
Daniel Veillard4255d502002-04-16 15:50:10 +00004727 */
4728static xmlSchemaTypePtr
4729xmlSchemaGetType(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004730 const xmlChar * nsName)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004731{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004732 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00004733
4734 if (name == NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004735 return (NULL);
4736 /* First try the built-in types. */
4737 if ((nsName != NULL) && xmlStrEqual(nsName, xmlSchemaNs)) {
4738 ret = xmlSchemaGetPredefinedType(name, nsName);
4739 if (ret != NULL)
4740 goto exit;
4741 /*
4742 * Note that we try the parsed schemas as well here
4743 * since one might have parsed the S4S, which contain more
4744 * than the built-in types.
4745 * TODO: Can we optimize this?
4746 */
4747 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004748 if (schema != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004749 WXS_FIND_GLOBAL_ITEM(typeDecl)
4750 }
4751exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004752
Daniel Veillard4255d502002-04-16 15:50:10 +00004753#ifdef DEBUG
4754 if (ret == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004755 if (nsName == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004756 fprintf(stderr, "Unable to lookup type %s", name);
4757 else
4758 fprintf(stderr, "Unable to lookup type %s:%s", name,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00004759 nsName);
Daniel Veillard4255d502002-04-16 15:50:10 +00004760 }
4761#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004762 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00004763}
4764
Daniel Veillard3646d642004-06-02 19:19:14 +00004765/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004766 * xmlSchemaGetAttributeDecl:
4767 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004768 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004769 * @ns: the target namespace of the attribute
Daniel Veillard3646d642004-06-02 19:19:14 +00004770 *
4771 * Lookup a an attribute in the schema or imported schemas
4772 *
4773 * Returns the attribute declaration or NULL if not found.
4774 */
4775static xmlSchemaAttributePtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004776xmlSchemaGetAttributeDecl(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004777 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004778{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004779 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004780
4781 if ((name == NULL) || (schema == NULL))
4782 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004783 if (schema != NULL) {
4784 WXS_FIND_GLOBAL_ITEM(attrDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004785 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004786exit:
Daniel Veillard3646d642004-06-02 19:19:14 +00004787#ifdef DEBUG
4788 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004789 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004790 fprintf(stderr, "Unable to lookup attribute %s", name);
4791 else
4792 fprintf(stderr, "Unable to lookup attribute %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004793 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004794 }
4795#endif
4796 return (ret);
4797}
4798
4799/**
4800 * xmlSchemaGetAttributeGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004801 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004802 * @name: the name of the attribute group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004803 * @ns: the target namespace of the attribute group
Daniel Veillard3646d642004-06-02 19:19:14 +00004804 *
4805 * Lookup a an attribute group in the schema or imported schemas
4806 *
4807 * Returns the attribute group definition or NULL if not found.
4808 */
4809static xmlSchemaAttributeGroupPtr
4810xmlSchemaGetAttributeGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004811 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004812{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004813 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004814
4815 if ((name == NULL) || (schema == NULL))
4816 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004817 if (schema != NULL) {
4818 WXS_FIND_GLOBAL_ITEM(attrgrpDecl)
4819 }
4820exit:
4821 /* TODO:
4822 if ((ret != NULL) && (ret->redef != NULL)) {
4823 * Return the last redefinition. *
4824 ret = ret->redef;
Daniel Veillard3646d642004-06-02 19:19:14 +00004825 }
William M. Brack2f2a6632004-08-20 23:09:47 +00004826 */
Daniel Veillard3646d642004-06-02 19:19:14 +00004827#ifdef DEBUG
4828 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004829 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004830 fprintf(stderr, "Unable to lookup attribute group %s", name);
4831 else
4832 fprintf(stderr, "Unable to lookup attribute group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004833 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004834 }
4835#endif
4836 return (ret);
4837}
4838
4839/**
4840 * xmlSchemaGetGroup:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004841 * @schema: the context of the schema
Daniel Veillard3646d642004-06-02 19:19:14 +00004842 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004843 * @ns: the target namespace of the group
Daniel Veillard3646d642004-06-02 19:19:14 +00004844 *
4845 * Lookup a group in the schema or imported schemas
4846 *
4847 * Returns the group definition or NULL if not found.
4848 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004849static xmlSchemaModelGroupDefPtr
Daniel Veillard3646d642004-06-02 19:19:14 +00004850xmlSchemaGetGroup(xmlSchemaPtr schema, const xmlChar * name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004851 const xmlChar * nsName)
Daniel Veillard3646d642004-06-02 19:19:14 +00004852{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004853 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +00004854
4855 if ((name == NULL) || (schema == NULL))
4856 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004857 if (schema != NULL) {
4858 WXS_FIND_GLOBAL_ITEM(groupDecl)
Daniel Veillard3646d642004-06-02 19:19:14 +00004859 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004860exit:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004861
Daniel Veillard3646d642004-06-02 19:19:14 +00004862#ifdef DEBUG
4863 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004864 if (nsName == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +00004865 fprintf(stderr, "Unable to lookup group %s", name);
4866 else
4867 fprintf(stderr, "Unable to lookup group %s:%s", name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004868 nsName);
Daniel Veillard3646d642004-06-02 19:19:14 +00004869 }
4870#endif
4871 return (ret);
4872}
4873
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004874static xmlSchemaNotationPtr
4875xmlSchemaGetNotation(xmlSchemaPtr schema,
4876 const xmlChar *name,
4877 const xmlChar *nsName)
4878{
4879 xmlSchemaNotationPtr ret = NULL;
4880
4881 if ((name == NULL) || (schema == NULL))
4882 return (NULL);
4883 if (schema != NULL) {
4884 WXS_FIND_GLOBAL_ITEM(notaDecl)
4885 }
4886exit:
4887 return (ret);
4888}
4889
4890static xmlSchemaIDCPtr
4891xmlSchemaGetIDC(xmlSchemaPtr schema,
4892 const xmlChar *name,
4893 const xmlChar *nsName)
4894{
4895 xmlSchemaIDCPtr ret = NULL;
4896
4897 if ((name == NULL) || (schema == NULL))
4898 return (NULL);
4899 if (schema != NULL) {
4900 WXS_FIND_GLOBAL_ITEM(idcDef)
4901 }
4902exit:
4903 return (ret);
4904}
4905
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004906/**
4907 * xmlSchemaGetNamedComponent:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004908 * @schema: the schema
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004909 * @name: the name of the group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004910 * @ns: the target namespace of the group
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004911 *
4912 * Lookup a group in the schema or imported schemas
4913 *
4914 * Returns the group definition or NULL if not found.
4915 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004916static xmlSchemaBasicItemPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004917xmlSchemaGetNamedComponent(xmlSchemaPtr schema,
4918 xmlSchemaTypeType itemType,
4919 const xmlChar *name,
4920 const xmlChar *targetNs)
4921{
4922 switch (itemType) {
4923 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004924 return ((xmlSchemaBasicItemPtr) xmlSchemaGetGroup(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004925 name, targetNs));
4926 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004927 return ((xmlSchemaBasicItemPtr) xmlSchemaGetElem(schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004928 name, targetNs));
4929 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00004930 TODO
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00004931 return (NULL);
4932 }
4933}
4934
Daniel Veillard4255d502002-04-16 15:50:10 +00004935/************************************************************************
4936 * *
4937 * Parsing functions *
4938 * *
4939 ************************************************************************/
4940
4941#define IS_BLANK_NODE(n) \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004942 (((n)->type == XML_TEXT_NODE) && (xmlSchemaIsBlank((n)->content, -1)))
Daniel Veillard4255d502002-04-16 15:50:10 +00004943
4944/**
4945 * xmlSchemaIsBlank:
4946 * @str: a string
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004947 * @len: the length of the string or -1
Daniel Veillard4255d502002-04-16 15:50:10 +00004948 *
4949 * Check if a string is ignorable
4950 *
4951 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
4952 */
4953static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004954xmlSchemaIsBlank(xmlChar * str, int len)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004955{
Daniel Veillard4255d502002-04-16 15:50:10 +00004956 if (str == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004957 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004958 if (len < 0) {
4959 while (*str != 0) {
4960 if (!(IS_BLANK_CH(*str)))
4961 return (0);
4962 str++;
4963 }
4964 } else while ((*str != 0) && (len != 0)) {
4965 if (!(IS_BLANK_CH(*str)))
4966 return (0);
4967 str++;
4968 len--;
Daniel Veillard4255d502002-04-16 15:50:10 +00004969 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00004970
Daniel Veillardd0c9c322003-10-10 00:49:42 +00004971 return (1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004972}
4973
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004974#define WXS_COMP_NAME(c, t) ((t) (c))->name
4975#define WXS_COMP_TNS(c, t) ((t) (c))->targetNamespace
4976/*
4977* xmlSchemaFindRedefCompInGraph:
4978* ATTENTION TODO: This uses pointer comp. for strings.
4979*/
4980static xmlSchemaBasicItemPtr
4981xmlSchemaFindRedefCompInGraph(xmlSchemaBucketPtr bucket,
4982 xmlSchemaTypeType type,
4983 const xmlChar *name,
4984 const xmlChar *nsName)
4985{
4986 xmlSchemaBasicItemPtr ret;
4987 int i;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00004988
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00004989 if ((bucket == NULL) || (name == NULL))
4990 return(NULL);
4991 if ((bucket->globals == NULL) ||
4992 (bucket->globals->nbItems == 0))
4993 goto subschemas;
4994 /*
4995 * Search in global components.
4996 */
4997 for (i = 0; i < bucket->globals->nbItems; i++) {
4998 ret = bucket->globals->items[i];
4999 if (ret->type == type) {
5000 switch (type) {
5001 case XML_SCHEMA_TYPE_COMPLEX:
5002 case XML_SCHEMA_TYPE_SIMPLE:
5003 if ((WXS_COMP_NAME(ret, xmlSchemaTypePtr) == name) &&
5004 (WXS_COMP_TNS(ret, xmlSchemaTypePtr) ==
5005 nsName))
5006 {
5007 return(ret);
5008 }
5009 break;
5010 case XML_SCHEMA_TYPE_GROUP:
5011 if ((WXS_COMP_NAME(ret,
5012 xmlSchemaModelGroupDefPtr) == name) &&
5013 (WXS_COMP_TNS(ret,
5014 xmlSchemaModelGroupDefPtr) == nsName))
5015 {
5016 return(ret);
5017 }
5018 break;
5019 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
5020 if ((WXS_COMP_NAME(ret,
5021 xmlSchemaAttributeGroupPtr) == name) &&
5022 (WXS_COMP_TNS(ret,
5023 xmlSchemaAttributeGroupPtr) == nsName))
5024 {
5025 return(ret);
5026 }
5027 default:
5028 /* Should not be hit. */
5029 return(NULL);
5030 }
5031 }
5032 }
5033subschemas:
5034 /*
5035 * Process imported/included schemas.
5036 */
5037 if (bucket->relations != NULL) {
5038 xmlSchemaSchemaRelationPtr rel = bucket->relations;
5039
5040 /*
5041 * TODO: Marking the bucket will not avoid multiple searches
5042 * in the same schema, but avoids at least circularity.
5043 */
5044 bucket->flags |= XML_SCHEMA_BUCKET_MARKED;
5045 do {
5046 if ((rel->bucket != NULL) &&
5047 ((rel->bucket->flags & XML_SCHEMA_BUCKET_MARKED) == 0)) {
5048 ret = xmlSchemaFindRedefCompInGraph(rel->bucket,
5049 type, name, nsName);
5050 if (ret != NULL)
5051 return(ret);
5052 }
5053 rel = rel->next;
5054 } while (rel != NULL);
5055 bucket->flags ^= XML_SCHEMA_BUCKET_MARKED;
5056 }
5057 return(NULL);
5058}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005059
5060/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005061 * xmlSchemaAddNotation:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005062 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005063 * @schema: the schema being built
5064 * @name: the item name
5065 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00005066 * Add an XML schema annotation declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00005067 * *WARNING* this interface is highly subject to change
5068 *
5069 * Returns the new struture or NULL in case of error
5070 */
5071static xmlSchemaNotationPtr
5072xmlSchemaAddNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005073 const xmlChar *name, const xmlChar *nsName,
5074 xmlNodePtr node ATTRIBUTE_UNUSED)
Daniel Veillard4255d502002-04-16 15:50:10 +00005075{
5076 xmlSchemaNotationPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005077
5078 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5079 return (NULL);
5080
Daniel Veillard4255d502002-04-16 15:50:10 +00005081 ret = (xmlSchemaNotationPtr) xmlMalloc(sizeof(xmlSchemaNotation));
5082 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005083 xmlSchemaPErrMemory(ctxt, "add annotation", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005084 return (NULL);
5085 }
5086 memset(ret, 0, sizeof(xmlSchemaNotation));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005087 ret->type = XML_SCHEMA_TYPE_NOTATION;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005088 ret->name = name;
5089 ret->targetNamespace = nsName;
5090 /* TODO: do we need the node to be set?
5091 * ret->node = node;*/
5092 WXS_ADD_GLOBAL(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005093 return (ret);
5094}
5095
Daniel Veillard4255d502002-04-16 15:50:10 +00005096/**
5097 * xmlSchemaAddAttribute:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005098 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005099 * @schema: the schema being built
5100 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005101 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005102 *
5103 * Add an XML schema Attrribute declaration
5104 * *WARNING* this interface is highly subject to change
5105 *
5106 * Returns the new struture or NULL in case of error
5107 */
5108static xmlSchemaAttributePtr
5109xmlSchemaAddAttribute(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005110 const xmlChar * name, const xmlChar * nsName,
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00005111 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005112{
5113 xmlSchemaAttributePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005114
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005115 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005116 return (NULL);
5117
5118 ret = (xmlSchemaAttributePtr) xmlMalloc(sizeof(xmlSchemaAttribute));
5119 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005120 xmlSchemaPErrMemory(ctxt, "allocating attribute", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005121 return (NULL);
5122 }
5123 memset(ret, 0, sizeof(xmlSchemaAttribute));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005124 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE;
5125 ret->node = node;
5126 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005127 ret->targetNamespace = nsName;
5128
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005129 if (topLevel)
5130 WXS_ADD_GLOBAL(ctxt, ret);
5131 else
5132 WXS_ADD_LOCAL(ctxt, ret);
5133 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005134 return (ret);
5135}
5136
5137/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005138 * xmlSchemaAddAttributeUse:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005139 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005140 * @schema: the schema being built
5141 * @name: the item name
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005142 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005143 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005144 * Add an XML schema Attrribute declaration
5145 * *WARNING* this interface is highly subject to change
5146 *
5147 * Returns the new struture or NULL in case of error
5148 */
5149static xmlSchemaAttributeUsePtr
5150xmlSchemaAddAttributeUse(xmlSchemaParserCtxtPtr pctxt,
5151 xmlNodePtr node)
5152{
5153 xmlSchemaAttributeUsePtr ret = NULL;
5154
5155 if (pctxt == NULL)
5156 return (NULL);
5157
5158 ret = (xmlSchemaAttributeUsePtr) xmlMalloc(sizeof(xmlSchemaAttributeUse));
5159 if (ret == NULL) {
5160 xmlSchemaPErrMemory(pctxt, "allocating attribute", NULL);
5161 return (NULL);
5162 }
5163 memset(ret, 0, sizeof(xmlSchemaAttributeUse));
5164 ret->type = XML_SCHEMA_TYPE_ATTRIBUTE_USE;
5165 ret->node = node;
5166
5167 WXS_ADD_LOCAL(pctxt, ret);
5168 return (ret);
5169}
5170
5171/*
5172* xmlSchemaAddRedef:
5173*
5174* Adds a redefinition information. This is used at a later stage to:
5175* resolve references to the redefined components and to check constraints.
5176*/
5177static xmlSchemaRedefPtr
5178xmlSchemaAddRedef(xmlSchemaParserCtxtPtr pctxt,
5179 xmlSchemaBucketPtr targetBucket,
5180 void *item,
5181 const xmlChar *refName,
5182 const xmlChar *refTargetNs)
5183{
5184 xmlSchemaRedefPtr ret;
5185
5186 ret = (xmlSchemaRedefPtr)
5187 xmlMalloc(sizeof(xmlSchemaRedef));
5188 if (ret == NULL) {
5189 xmlSchemaPErrMemory(pctxt,
5190 "allocating redefinition info", NULL);
5191 return (NULL);
5192 }
5193 memset(ret, 0, sizeof(xmlSchemaRedef));
5194 ret->item = item;
5195 ret->targetBucket = targetBucket;
5196 ret->refName = refName;
5197 ret->refTargetNs = refTargetNs;
5198 if (WXS_CONSTRUCTOR(pctxt)->redefs == NULL)
5199 WXS_CONSTRUCTOR(pctxt)->redefs = ret;
5200 else
5201 WXS_CONSTRUCTOR(pctxt)->lastRedef->next = ret;
5202 WXS_CONSTRUCTOR(pctxt)->lastRedef = ret;
5203
5204 return (ret);
5205}
5206
5207/**
5208 * xmlSchemaAddAttributeGroupDefinition:
5209 * @ctxt: a schema parser context
5210 * @schema: the schema being built
5211 * @name: the item name
5212 * @nsName: the target namespace
5213 * @node: the corresponding node
5214 *
5215 * Add an XML schema Attrribute Group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00005216 *
5217 * Returns the new struture or NULL in case of error
5218 */
5219static xmlSchemaAttributeGroupPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005220xmlSchemaAddAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
5221 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
5222 const xmlChar *name,
5223 const xmlChar *nsName,
5224 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00005225{
5226 xmlSchemaAttributeGroupPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005227
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005228 if ((pctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005229 return (NULL);
5230
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005231 ret = (xmlSchemaAttributeGroupPtr)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005232 xmlMalloc(sizeof(xmlSchemaAttributeGroup));
Daniel Veillard4255d502002-04-16 15:50:10 +00005233 if (ret == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005234 xmlSchemaPErrMemory(pctxt, "allocating attribute group", NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005235 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005236 }
5237 memset(ret, 0, sizeof(xmlSchemaAttributeGroup));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005238 ret->type = XML_SCHEMA_TYPE_ATTRIBUTEGROUP;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005239 ret->name = name;
5240 ret->targetNamespace = nsName;
5241 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005242
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005243 /* TODO: Remove the flag. */
5244 ret->flags |= XML_SCHEMAS_ATTRGROUP_GLOBAL;
5245 if (pctxt->isRedefine) {
5246 pctxt->redef = xmlSchemaAddRedef(pctxt, pctxt->redefined,
5247 ret, name, nsName);
5248 if (pctxt->redef == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005249 xmlFree(ret);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005250 return(NULL);
5251 }
5252 pctxt->redefCounter = 0;
5253 }
5254 WXS_ADD_GLOBAL(pctxt, ret);
5255 WXS_ADD_PENDING(pctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005256 return (ret);
5257}
5258
5259/**
5260 * xmlSchemaAddElement:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00005261 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005262 * @schema: the schema being built
5263 * @name: the type name
5264 * @namespace: the type namespace
5265 *
5266 * Add an XML schema Element declaration
5267 * *WARNING* this interface is highly subject to change
5268 *
5269 * Returns the new struture or NULL in case of error
5270 */
5271static xmlSchemaElementPtr
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005272xmlSchemaAddElement(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005273 const xmlChar * name, const xmlChar * nsName,
William M. Brack2f2a6632004-08-20 23:09:47 +00005274 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005275{
5276 xmlSchemaElementPtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005277
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005278 if ((ctxt == NULL) || (name == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005279 return (NULL);
5280
Daniel Veillard4255d502002-04-16 15:50:10 +00005281 ret = (xmlSchemaElementPtr) xmlMalloc(sizeof(xmlSchemaElement));
5282 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005283 xmlSchemaPErrMemory(ctxt, "allocating element", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005284 return (NULL);
5285 }
5286 memset(ret, 0, sizeof(xmlSchemaElement));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005287 ret->type = XML_SCHEMA_TYPE_ELEMENT;
5288 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005289 ret->targetNamespace = nsName;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005290 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005291
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005292 if (topLevel)
5293 WXS_ADD_GLOBAL(ctxt, ret);
5294 else
5295 WXS_ADD_LOCAL(ctxt, ret);
5296 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005297 return (ret);
5298}
5299
5300/**
5301 * xmlSchemaAddType:
Daniel Veillard01fa6152004-06-29 17:04:39 +00005302 * @ctxt: a schema parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00005303 * @schema: the schema being built
5304 * @name: the item name
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005305 * @namespace: the namespace
Daniel Veillard4255d502002-04-16 15:50:10 +00005306 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00005307 * Add an XML schema item
Daniel Veillard4255d502002-04-16 15:50:10 +00005308 * *WARNING* this interface is highly subject to change
5309 *
5310 * Returns the new struture or NULL in case of error
5311 */
5312static xmlSchemaTypePtr
5313xmlSchemaAddType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005314 xmlSchemaTypeType type,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00005315 const xmlChar * name, const xmlChar * nsName,
5316 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00005317{
5318 xmlSchemaTypePtr ret = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00005319
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005320 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00005321 return (NULL);
5322
5323 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
5324 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005325 xmlSchemaPErrMemory(ctxt, "allocating type", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005326 return (NULL);
5327 }
5328 memset(ret, 0, sizeof(xmlSchemaType));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005329 ret->type = type;
5330 ret->name = name;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005331 ret->targetNamespace = nsName;
5332 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005333 if (topLevel) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005334 if (ctxt->isRedefine) {
5335 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5336 ret, name, nsName);
5337 if (ctxt->redef == NULL) {
5338 xmlFree(ret);
5339 return(NULL);
5340 }
5341 ctxt->redefCounter = 0;
5342 }
5343 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005344 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005345 WXS_ADD_LOCAL(ctxt, ret);
5346 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005347 return (ret);
5348}
5349
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005350static xmlSchemaQNameRefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005351xmlSchemaNewQNameRef(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005352 xmlSchemaTypeType refType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005353 const xmlChar *refName,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005354 const xmlChar *refNs)
5355{
5356 xmlSchemaQNameRefPtr ret;
5357
5358 ret = (xmlSchemaQNameRefPtr)
5359 xmlMalloc(sizeof(xmlSchemaQNameRef));
5360 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005361 xmlSchemaPErrMemory(pctxt,
5362 "allocating QName reference item", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005363 return (NULL);
5364 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005365 ret->node = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005366 ret->type = XML_SCHEMA_EXTRA_QNAMEREF;
5367 ret->name = refName;
5368 ret->targetNamespace = refNs;
5369 ret->item = NULL;
5370 ret->itemType = refType;
5371 /*
5372 * Store the reference item in the schema.
5373 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005374 WXS_ADD_LOCAL(pctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005375 return (ret);
5376}
5377
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005378static xmlSchemaAttributeUseProhibPtr
5379xmlSchemaAddAttributeUseProhib(xmlSchemaParserCtxtPtr pctxt)
5380{
5381 xmlSchemaAttributeUseProhibPtr ret;
5382
5383 ret = (xmlSchemaAttributeUseProhibPtr)
5384 xmlMalloc(sizeof(xmlSchemaAttributeUseProhib));
5385 if (ret == NULL) {
5386 xmlSchemaPErrMemory(pctxt,
5387 "allocating attribute use prohibition", NULL);
5388 return (NULL);
5389 }
5390 memset(ret, 0, sizeof(xmlSchemaAttributeUseProhib));
5391 ret->type = XML_SCHEMA_EXTRA_ATTR_USE_PROHIB;
5392 WXS_ADD_LOCAL(pctxt, ret);
5393 return (ret);
5394}
5395
5396
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005397/**
5398 * xmlSchemaAddModelGroup:
5399 * @ctxt: a schema parser context
5400 * @schema: the schema being built
5401 * @type: the "compositor" type of the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005402 * @node: the node in the schema doc
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005403 *
5404 * Adds a schema model group
5405 * *WARNING* this interface is highly subject to change
5406 *
5407 * Returns the new struture or NULL in case of error
5408 */
5409static xmlSchemaModelGroupPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005410xmlSchemaAddModelGroup(xmlSchemaParserCtxtPtr ctxt,
5411 xmlSchemaPtr schema,
5412 xmlSchemaTypeType type,
5413 xmlNodePtr node)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005414{
5415 xmlSchemaModelGroupPtr ret = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005416
5417 if ((ctxt == NULL) || (schema == NULL))
5418 return (NULL);
5419
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005420 ret = (xmlSchemaModelGroupPtr)
5421 xmlMalloc(sizeof(xmlSchemaModelGroup));
5422 if (ret == NULL) {
5423 xmlSchemaPErrMemory(ctxt, "allocating model group component",
5424 NULL);
5425 return (NULL);
5426 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005427 memset(ret, 0, sizeof(xmlSchemaModelGroup));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005428 ret->type = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005429 ret->node = node;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005430 WXS_ADD_LOCAL(ctxt, ret);
5431 if ((type == XML_SCHEMA_TYPE_SEQUENCE) ||
5432 (type == XML_SCHEMA_TYPE_CHOICE))
5433 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005434 return (ret);
5435}
5436
5437
5438/**
5439 * xmlSchemaAddParticle:
5440 * @ctxt: a schema parser context
5441 * @schema: the schema being built
5442 * @node: the corresponding node in the schema doc
5443 * @min: the minOccurs
5444 * @max: the maxOccurs
5445 *
5446 * Adds an XML schema particle component.
5447 * *WARNING* this interface is highly subject to change
5448 *
5449 * Returns the new struture or NULL in case of error
5450 */
5451static xmlSchemaParticlePtr
5452xmlSchemaAddParticle(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5453 xmlNodePtr node, int min, int max)
5454{
5455 xmlSchemaParticlePtr ret = NULL;
5456 if ((ctxt == NULL) || (schema == NULL))
5457 return (NULL);
5458
5459#ifdef DEBUG
5460 fprintf(stderr, "Adding particle component\n");
5461#endif
5462 ret = (xmlSchemaParticlePtr)
5463 xmlMalloc(sizeof(xmlSchemaParticle));
5464 if (ret == NULL) {
5465 xmlSchemaPErrMemory(ctxt, "allocating particle component",
5466 NULL);
5467 return (NULL);
5468 }
5469 ret->type = XML_SCHEMA_TYPE_PARTICLE;
5470 ret->annot = NULL;
5471 ret->node = node;
5472 ret->minOccurs = min;
5473 ret->maxOccurs = max;
5474 ret->next = NULL;
5475 ret->children = NULL;
5476
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005477 WXS_ADD_LOCAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005478 /*
5479 * Note that addition to pending components will be done locally
5480 * to the specific parsing function, since the most particles
5481 * need not to be fixed up (i.e. the reference to be resolved).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005482 * REMOVED: WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005483 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005484 return (ret);
5485}
5486
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005487/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005488 * xmlSchemaAddModelGroupDefinition:
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005489 * @ctxt: a schema validation context
5490 * @schema: the schema being built
5491 * @name: the group name
5492 *
5493 * Add an XML schema Group definition
5494 *
5495 * Returns the new struture or NULL in case of error
5496 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005497static xmlSchemaModelGroupDefPtr
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005498xmlSchemaAddModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
5499 xmlSchemaPtr schema,
5500 const xmlChar *name,
5501 const xmlChar *nsName,
5502 xmlNodePtr node)
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005503{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005504 xmlSchemaModelGroupDefPtr ret = NULL;
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005505
5506 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5507 return (NULL);
5508
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005509 ret = (xmlSchemaModelGroupDefPtr)
5510 xmlMalloc(sizeof(xmlSchemaModelGroupDef));
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005511 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005512 xmlSchemaPErrMemory(ctxt, "adding group", NULL);
Daniel Veillarda84c0b32003-06-02 16:58:46 +00005513 return (NULL);
5514 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005515 memset(ret, 0, sizeof(xmlSchemaModelGroupDef));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005516 ret->name = name;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005517 ret->type = XML_SCHEMA_TYPE_GROUP;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005518 ret->node = node;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005519 ret->targetNamespace = nsName;
5520
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005521 if (ctxt->isRedefine) {
5522 ctxt->redef = xmlSchemaAddRedef(ctxt, ctxt->redefined,
5523 ret, name, nsName);
5524 if (ctxt->redef == NULL) {
5525 xmlFree(ret);
5526 return(NULL);
5527 }
5528 ctxt->redefCounter = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005529 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005530 WXS_ADD_GLOBAL(ctxt, ret);
5531 WXS_ADD_PENDING(ctxt, ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00005532 return (ret);
5533}
5534
Daniel Veillard3646d642004-06-02 19:19:14 +00005535/**
5536 * xmlSchemaNewWildcardNs:
5537 * @ctxt: a schema validation context
5538 *
5539 * Creates a new wildcard namespace constraint.
5540 *
5541 * Returns the new struture or NULL in case of error
5542 */
5543static xmlSchemaWildcardNsPtr
5544xmlSchemaNewWildcardNsConstraint(xmlSchemaParserCtxtPtr ctxt)
5545{
5546 xmlSchemaWildcardNsPtr ret;
5547
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005548 ret = (xmlSchemaWildcardNsPtr)
Daniel Veillard3646d642004-06-02 19:19:14 +00005549 xmlMalloc(sizeof(xmlSchemaWildcardNs));
5550 if (ret == NULL) {
5551 xmlSchemaPErrMemory(ctxt, "creating wildcard namespace constraint", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005552 return (NULL);
Daniel Veillard3646d642004-06-02 19:19:14 +00005553 }
5554 ret->value = NULL;
5555 ret->next = NULL;
5556 return (ret);
5557}
5558
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005559static xmlSchemaIDCPtr
5560xmlSchemaAddIDC(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5561 const xmlChar *name, const xmlChar *nsName,
5562 int category, xmlNodePtr node)
5563{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005564 xmlSchemaIDCPtr ret = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005565
5566 if ((ctxt == NULL) || (schema == NULL) || (name == NULL))
5567 return (NULL);
5568
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005569 ret = (xmlSchemaIDCPtr) xmlMalloc(sizeof(xmlSchemaIDC));
5570 if (ret == NULL) {
5571 xmlSchemaPErrMemory(ctxt,
5572 "allocating an identity-constraint definition", NULL);
5573 return (NULL);
5574 }
5575 memset(ret, 0, sizeof(xmlSchemaIDC));
5576 /* The target namespace of the parent element declaration. */
5577 ret->targetNamespace = nsName;
5578 ret->name = name;
5579 ret->type = category;
5580 ret->node = node;
5581
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005582 WXS_ADD_GLOBAL(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005583 /*
5584 * Only keyrefs need to be fixup up.
5585 */
5586 if (category == XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005587 WXS_ADD_PENDING(ctxt, ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005588 return (ret);
5589}
5590
Daniel Veillard3646d642004-06-02 19:19:14 +00005591/**
5592 * xmlSchemaAddWildcard:
5593 * @ctxt: a schema validation context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005594 * @schema: a schema
5595 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005596 * Adds a wildcard.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005597 * It corresponds to a xsd:anyAttribute and xsd:any.
Daniel Veillard3646d642004-06-02 19:19:14 +00005598 *
5599 * Returns the new struture or NULL in case of error
5600 */
5601static xmlSchemaWildcardPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005602xmlSchemaAddWildcard(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
5603 xmlSchemaTypeType type, xmlNodePtr node)
Daniel Veillard3646d642004-06-02 19:19:14 +00005604{
5605 xmlSchemaWildcardPtr ret = NULL;
5606
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005607 if ((ctxt == NULL) || (schema == NULL))
Daniel Veillard3646d642004-06-02 19:19:14 +00005608 return (NULL);
5609
5610 ret = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard));
5611 if (ret == NULL) {
5612 xmlSchemaPErrMemory(ctxt, "adding wildcard", NULL);
5613 return (NULL);
5614 }
5615 memset(ret, 0, sizeof(xmlSchemaWildcard));
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005616 ret->type = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005617 ret->node = node;
5618 WXS_ADD_LOCAL(ctxt, ret);
Daniel Veillard3646d642004-06-02 19:19:14 +00005619 return (ret);
5620}
5621
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005622static void
5623xmlSchemaSubstGroupFree(xmlSchemaSubstGroupPtr group)
5624{
5625 if (group == NULL)
5626 return;
5627 if (group->members != NULL)
5628 xmlSchemaItemListFree(group->members);
5629 xmlFree(group);
5630}
5631
5632static xmlSchemaSubstGroupPtr
5633xmlSchemaSubstGroupAdd(xmlSchemaParserCtxtPtr pctxt,
5634 xmlSchemaElementPtr head)
5635{
5636 xmlSchemaSubstGroupPtr ret;
5637
5638 /* Init subst group hash. */
5639 if (WXS_SUBST_GROUPS(pctxt) == NULL) {
5640 WXS_SUBST_GROUPS(pctxt) = xmlHashCreateDict(10, pctxt->dict);
5641 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5642 return(NULL);
5643 }
5644 /* Create a new substitution group. */
5645 ret = (xmlSchemaSubstGroupPtr) xmlMalloc(sizeof(xmlSchemaSubstGroup));
5646 if (ret == NULL) {
5647 xmlSchemaPErrMemory(NULL,
5648 "allocating a substitution group container", NULL);
5649 return(NULL);
5650 }
5651 memset(ret, 0, sizeof(xmlSchemaSubstGroup));
5652 ret->head = head;
5653 /* Create list of members. */
5654 ret->members = xmlSchemaItemListCreate();
5655 if (ret->members == NULL) {
5656 xmlSchemaSubstGroupFree(ret);
5657 return(NULL);
5658 }
5659 /* Add subst group to hash. */
5660 if (xmlHashAddEntry2(WXS_SUBST_GROUPS(pctxt),
5661 head->name, head->targetNamespace, ret) != 0) {
5662 PERROR_INT("xmlSchemaSubstGroupAdd",
5663 "failed to add a new substitution container");
5664 xmlSchemaSubstGroupFree(ret);
5665 return(NULL);
5666 }
5667 return(ret);
5668}
5669
5670static xmlSchemaSubstGroupPtr
5671xmlSchemaSubstGroupGet(xmlSchemaParserCtxtPtr pctxt,
5672 xmlSchemaElementPtr head)
5673{
5674 if (WXS_SUBST_GROUPS(pctxt) == NULL)
5675 return(NULL);
5676 return(xmlHashLookup2(WXS_SUBST_GROUPS(pctxt),
5677 head->name, head->targetNamespace));
5678
5679}
5680
5681/**
5682 * xmlSchemaAddElementSubstitutionMember:
5683 * @pctxt: a schema parser context
5684 * @head: the head of the substitution group
5685 * @member: the new member of the substitution group
5686 *
5687 * Allocate a new annotation structure.
5688 *
5689 * Returns the newly allocated structure or NULL in case or error
5690 */
5691static int
5692xmlSchemaAddElementSubstitutionMember(xmlSchemaParserCtxtPtr pctxt,
5693 xmlSchemaElementPtr head,
5694 xmlSchemaElementPtr member)
5695{
5696 xmlSchemaSubstGroupPtr substGroup = NULL;
5697
5698 if ((pctxt == NULL) || (head == NULL) || (member == NULL))
5699 return (-1);
5700
5701 substGroup = xmlSchemaSubstGroupGet(pctxt, head);
5702 if (substGroup == NULL)
5703 substGroup = xmlSchemaSubstGroupAdd(pctxt, head);
5704 if (substGroup == NULL)
5705 return(-1);
5706 if (xmlSchemaItemListAdd(substGroup->members, member) == -1)
5707 return(-1);
5708 return(0);
5709}
5710
Daniel Veillard4255d502002-04-16 15:50:10 +00005711/************************************************************************
5712 * *
5713 * Utilities for parsing *
5714 * *
5715 ************************************************************************/
5716
Daniel Veillard4255d502002-04-16 15:50:10 +00005717/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00005718 * xmlSchemaPValAttrNodeQNameValue:
5719 * @ctxt: a schema parser context
5720 * @schema: the schema context
5721 * @ownerDes: the designation of the parent element
5722 * @ownerItem: the parent as a schema object
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005723 * @value: the QName value
Daniel Veillardc0826a72004-08-10 14:17:33 +00005724 * @local: the resulting local part if found, the attribute value otherwise
5725 * @uri: the resulting namespace URI if found
5726 *
5727 * Extracts the local name and the URI of a QName value and validates it.
5728 * This one is intended to be used on attribute values that
5729 * should resolve to schema components.
5730 *
5731 * Returns 0, in case the QName is valid, a positive error code
5732 * if not valid and -1 if an internal error occurs.
5733 */
5734static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005735xmlSchemaPValAttrNodeQNameValue(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005736 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005737 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005738 xmlAttrPtr attr,
5739 const xmlChar *value,
5740 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005741 const xmlChar **local)
5742{
5743 const xmlChar *pref;
5744 xmlNsPtr ns;
5745 int len, ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005746
Daniel Veillardc0826a72004-08-10 14:17:33 +00005747 *uri = NULL;
5748 *local = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00005749 ret = xmlValidateQName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005750 if (ret > 0) {
5751 xmlSchemaPSimpleTypeErr(ctxt,
5752 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5753 ownerItem, (xmlNodePtr) attr,
5754 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
5755 NULL, value, NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005756 *local = value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005757 return (ctxt->err);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005758 } else if (ret < 0)
5759 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005760
5761 if (!strchr((char *) value, ':')) {
Daniel Veillard24505b02005-07-28 23:49:35 +00005762 ns = xmlSearchNs(attr->doc, attr->parent, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005763 if (ns)
5764 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
5765 else if (schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005766 /* TODO: move XML_SCHEMAS_INCLUDING_CONVERT_NS to the
5767 * parser context. */
Daniel Veillardc0826a72004-08-10 14:17:33 +00005768 /*
5769 * This one takes care of included schemas with no
5770 * target namespace.
5771 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00005772 *uri = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005773 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00005774 *local = xmlDictLookup(ctxt->dict, value, -1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005775 return (0);
5776 }
5777 /*
5778 * At this point xmlSplitQName3 has to return a local name.
5779 */
5780 *local = xmlSplitQName3(value, &len);
5781 *local = xmlDictLookup(ctxt->dict, *local, -1);
5782 pref = xmlDictLookup(ctxt->dict, value, len);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005783 ns = xmlSearchNs(attr->doc, attr->parent, pref);
5784 if (ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005785 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005786 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005787 ownerItem, (xmlNodePtr) attr,
5788 xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), NULL, value,
5789 "The value '%s' of simple type 'xs:QName' has no "
5790 "corresponding namespace declaration in scope", value, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005791 return (ctxt->err);
5792 } else {
5793 *uri = xmlDictLookup(ctxt->dict, ns->href, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005794 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00005795 return (0);
5796}
5797
5798/**
5799 * xmlSchemaPValAttrNodeQName:
5800 * @ctxt: a schema parser context
5801 * @schema: the schema context
5802 * @ownerDes: the designation of the owner element
5803 * @ownerItem: the owner as a schema object
5804 * @attr: the attribute node
5805 * @local: the resulting local part if found, the attribute value otherwise
5806 * @uri: the resulting namespace URI if found
5807 *
5808 * Extracts and validates the QName of an attribute value.
5809 * This one is intended to be used on attribute values that
5810 * should resolve to schema components.
5811 *
5812 * Returns 0, in case the QName is valid, a positive error code
5813 * if not valid and -1 if an internal error occurs.
5814 */
5815static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005816xmlSchemaPValAttrNodeQName(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005817 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005818 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005819 xmlAttrPtr attr,
5820 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005821 const xmlChar **local)
5822{
5823 const xmlChar *value;
5824
5825 value = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005826 return (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005827 ownerItem, attr, value, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005828}
5829
5830/**
5831 * xmlSchemaPValAttrQName:
5832 * @ctxt: a schema parser context
5833 * @schema: the schema context
5834 * @ownerDes: the designation of the parent element
5835 * @ownerItem: the owner as a schema object
5836 * @ownerElem: the parent node of the attribute
5837 * @name: the name of the attribute
5838 * @local: the resulting local part if found, the attribute value otherwise
5839 * @uri: the resulting namespace URI if found
5840 *
5841 * Extracts and validates the QName of an attribute value.
5842 *
5843 * Returns 0, in case the QName is valid, a positive error code
5844 * if not valid and -1 if an internal error occurs.
5845 */
5846static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005847xmlSchemaPValAttrQName(xmlSchemaParserCtxtPtr ctxt,
5848 xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005849 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005850 xmlNodePtr ownerElem,
5851 const char *name,
5852 const xmlChar **uri,
Daniel Veillardc0826a72004-08-10 14:17:33 +00005853 const xmlChar **local)
5854{
5855 xmlAttrPtr attr;
5856
5857 attr = xmlSchemaGetPropNode(ownerElem, name);
5858 if (attr == NULL) {
5859 *local = NULL;
5860 *uri = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005861 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00005862 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005863 return (xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005864 ownerItem, attr, uri, local));
Daniel Veillardc0826a72004-08-10 14:17:33 +00005865}
5866
5867/**
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005868 * xmlSchemaPValAttrID:
5869 * @ctxt: a schema parser context
5870 * @schema: the schema context
5871 * @ownerDes: the designation of the parent element
5872 * @ownerItem: the owner as a schema object
5873 * @ownerElem: the parent node of the attribute
5874 * @name: the name of the attribute
5875 *
5876 * Extracts and validates the ID of an attribute value.
5877 *
5878 * Returns 0, in case the ID is valid, a positive error code
5879 * if not valid and -1 if an internal error occurs.
5880 */
5881static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005882xmlSchemaPValAttrNodeID(xmlSchemaParserCtxtPtr ctxt, xmlAttrPtr attr)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005883{
5884 int ret;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005885 const xmlChar *value;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005886
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005887 if (attr == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005888 return(0);
5889 value = xmlSchemaGetNodeContentNoDict((xmlNodePtr) attr);
5890 ret = xmlValidateNCName(value, 1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005891 if (ret == 0) {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005892 /*
5893 * NOTE: the IDness might have already be declared in the DTD
5894 */
5895 if (attr->atype != XML_ATTRIBUTE_ID) {
5896 xmlIDPtr res;
5897 xmlChar *strip;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005898
5899 /*
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005900 * TODO: Use xmlSchemaStrip here; it's not exported at this
5901 * moment.
5902 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005903 strip = xmlSchemaCollapseString(value);
5904 if (strip != NULL) {
5905 xmlFree((xmlChar *) value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005906 value = strip;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005907 }
5908 res = xmlAddID(NULL, attr->doc, value, attr);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005909 if (res == NULL) {
5910 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005911 xmlSchemaPSimpleTypeErr(ctxt,
5912 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005913 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005914 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5915 NULL, NULL, "Duplicate value '%s' of simple "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005916 "type 'xs:ID'", value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005917 } else
5918 attr->atype = XML_ATTRIBUTE_ID;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005919 }
5920 } else if (ret > 0) {
5921 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005922 xmlSchemaPSimpleTypeErr(ctxt,
5923 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005924 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005925 xmlSchemaGetBuiltInType(XML_SCHEMAS_ID),
5926 NULL, NULL, "The value '%s' of simple type 'xs:ID' is "
5927 "not a valid 'xs:NCName'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005928 value, NULL);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005929 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005930 if (value != NULL)
5931 xmlFree((xmlChar *)value);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005932
5933 return (ret);
5934}
5935
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00005936static int
5937xmlSchemaPValAttrID(xmlSchemaParserCtxtPtr ctxt,
5938 xmlNodePtr ownerElem,
5939 const xmlChar *name)
5940{
5941 xmlAttrPtr attr;
5942
5943 attr = xmlSchemaGetPropNode(ownerElem, (const char *) name);
5944 if (attr == NULL)
5945 return(0);
5946 return(xmlSchemaPValAttrNodeID(ctxt, attr));
5947
5948}
5949
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +00005950/**
Daniel Veillard4255d502002-04-16 15:50:10 +00005951 * xmlGetMaxOccurs:
5952 * @ctxt: a schema validation context
5953 * @node: a subtree containing XML Schema informations
5954 *
5955 * Get the maxOccurs property
5956 *
5957 * Returns the default if not found, or the value
5958 */
5959static int
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005960xmlGetMaxOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
5961 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005962{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00005963 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00005964 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005965 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00005966
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005967 attr = xmlSchemaGetPropNode(node, "maxOccurs");
5968 if (attr == NULL)
5969 return (def);
5970 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00005971
5972 if (xmlStrEqual(val, (const xmlChar *) "unbounded")) {
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005973 if (max != UNBOUNDED) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005974 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005975 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5976 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005977 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005978 val, NULL, NULL, NULL);
5979 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005980 } else
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005981 return (UNBOUNDED); /* encoding it with -1 might be another option */
Daniel Veillard4255d502002-04-16 15:50:10 +00005982 }
5983
5984 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00005985 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005986 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005987 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005988 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005989 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
5990 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00005991 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00005992 val, NULL, NULL, NULL);
5993 return (def);
5994 }
Daniel Veillard4255d502002-04-16 15:50:10 +00005995 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00005996 ret = ret * 10 + (*cur - '0');
5997 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00005998 }
William M. Brack76e95df2003-10-18 16:20:14 +00005999 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006000 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006001 /*
6002 * TODO: Restrict the maximal value to Integer.
6003 */
6004 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006005 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006006 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6007 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006008 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006009 val, NULL, NULL, NULL);
6010 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006011 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006012 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006013}
6014
6015/**
6016 * xmlGetMinOccurs:
6017 * @ctxt: a schema validation context
6018 * @node: a subtree containing XML Schema informations
6019 *
6020 * Get the minOccurs property
6021 *
6022 * Returns the default if not found, or the value
6023 */
6024static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006025xmlGetMinOccurs(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006026 int min, int max, int def, const char *expected)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006027{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006028 const xmlChar *val, *cur;
Daniel Veillard4255d502002-04-16 15:50:10 +00006029 int ret = 0;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006030 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006031
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006032 attr = xmlSchemaGetPropNode(node, "minOccurs");
6033 if (attr == NULL)
6034 return (def);
6035 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Daniel Veillard4255d502002-04-16 15:50:10 +00006036 cur = val;
William M. Brack76e95df2003-10-18 16:20:14 +00006037 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006038 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006039 if (*cur == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006040 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006041 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6042 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006043 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006044 val, NULL, NULL, NULL);
6045 return (def);
6046 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006047 while ((*cur >= '0') && (*cur <= '9')) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006048 ret = ret * 10 + (*cur - '0');
6049 cur++;
Daniel Veillard4255d502002-04-16 15:50:10 +00006050 }
William M. Brack76e95df2003-10-18 16:20:14 +00006051 while (IS_BLANK_CH(*cur))
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006052 cur++;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006053 /*
6054 * TODO: Restrict the maximal value to Integer.
6055 */
6056 if ((*cur != 0) || (ret < min) || ((max != -1) && (ret > max))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006057 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006058 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
6059 /* XML_SCHEMAP_INVALID_MINOCCURS, */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006060 NULL, (xmlNodePtr) attr, NULL, expected,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006061 val, NULL, NULL, NULL);
6062 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006063 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006064 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +00006065}
6066
6067/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006068 * xmlSchemaPGetBoolNodeValue:
6069 * @ctxt: a schema validation context
6070 * @ownerDes: owner designation
6071 * @ownerItem: the owner as a schema item
6072 * @node: the node holding the value
6073 *
6074 * Converts a boolean string value into 1 or 0.
6075 *
6076 * Returns 0 or 1.
6077 */
6078static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006079xmlSchemaPGetBoolNodeValue(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006080 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006081 xmlNodePtr node)
6082{
6083 xmlChar *value = NULL;
6084 int res = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006085
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006086 value = xmlNodeGetContent(node);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006087 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006088 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006089 * An instance of a datatype that is defined as ·boolean·
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006090 * can have the following legal literals {true, false, 1, 0}.
6091 */
6092 if (xmlStrEqual(BAD_CAST value, BAD_CAST "true"))
6093 res = 1;
6094 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "false"))
6095 res = 0;
6096 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "1"))
6097 res = 1;
6098 else if (xmlStrEqual(BAD_CAST value, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006099 res = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006100 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006101 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006102 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006103 ownerItem, node,
6104 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6105 NULL, BAD_CAST value,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00006106 NULL, NULL, NULL);
6107 }
6108 if (value != NULL)
6109 xmlFree(value);
6110 return (res);
6111}
6112
6113/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006114 * xmlGetBooleanProp:
6115 * @ctxt: a schema validation context
6116 * @node: a subtree containing XML Schema informations
6117 * @name: the attribute name
6118 * @def: the default value
6119 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006120 * Evaluate if a boolean property is set
Daniel Veillard4255d502002-04-16 15:50:10 +00006121 *
6122 * Returns the default if not found, 0 if found to be false,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006123 * 1 if found to be true
Daniel Veillard4255d502002-04-16 15:50:10 +00006124 */
6125static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006126xmlGetBooleanProp(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006127 xmlNodePtr node,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006128 const char *name, int def)
6129{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006130 const xmlChar *val;
Daniel Veillard4255d502002-04-16 15:50:10 +00006131
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006132 val = xmlSchemaGetProp(ctxt, node, name);
Daniel Veillard4255d502002-04-16 15:50:10 +00006133 if (val == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006134 return (def);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006135 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006136 * 3.2.2.1 Lexical representation
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006137 * An instance of a datatype that is defined as ·boolean·
Daniel Veillardc0826a72004-08-10 14:17:33 +00006138 * can have the following legal literals {true, false, 1, 0}.
6139 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006140 if (xmlStrEqual(val, BAD_CAST "true"))
6141 def = 1;
6142 else if (xmlStrEqual(val, BAD_CAST "false"))
6143 def = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006144 else if (xmlStrEqual(val, BAD_CAST "1"))
6145 def = 1;
6146 else if (xmlStrEqual(val, BAD_CAST "0"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006147 def = 0;
6148 else {
6149 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006150 XML_SCHEMAP_INVALID_BOOLEAN,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006151 NULL,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00006152 (xmlNodePtr) xmlSchemaGetPropNode(node, name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006153 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
6154 NULL, val, NULL, NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006155 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006156 return (def);
Daniel Veillard4255d502002-04-16 15:50:10 +00006157}
6158
6159/************************************************************************
6160 * *
6161 * Shema extraction from an Infoset *
6162 * *
6163 ************************************************************************/
6164static xmlSchemaTypePtr xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr
6165 ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00006166 xmlNodePtr node,
6167 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006168static xmlSchemaTypePtr xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr
6169 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006170 xmlSchemaPtr schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006171 xmlNodePtr node,
Daniel Veillard3646d642004-06-02 19:19:14 +00006172 int topLevel);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006173static xmlSchemaTypePtr xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr
6174 ctxt,
Daniel Veillard4255d502002-04-16 15:50:10 +00006175 xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006176 xmlNodePtr node,
6177 xmlSchemaTypeType parentType);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006178static xmlSchemaBasicItemPtr
6179xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
6180 xmlSchemaPtr schema,
6181 xmlNodePtr node,
6182 xmlSchemaItemListPtr uses,
6183 int parentType);
Daniel Veillard4255d502002-04-16 15:50:10 +00006184static xmlSchemaTypePtr xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt,
6185 xmlSchemaPtr schema,
6186 xmlNodePtr node);
Daniel Veillard3646d642004-06-02 19:19:14 +00006187static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006188xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6189 xmlSchemaPtr schema, xmlNodePtr node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006190
6191/**
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006192 * xmlSchemaPValAttrNodeValue:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006193 *
Daniel Veillard01fa6152004-06-29 17:04:39 +00006194 * @ctxt: a schema parser context
Daniel Veillardc0826a72004-08-10 14:17:33 +00006195 * @ownerDes: the designation of the parent element
6196 * @ownerItem: the schema object owner if existent
6197 * @attr: the schema attribute node being validated
6198 * @value: the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006199 * @type: the built-in type to be validated against
Daniel Veillard01fa6152004-06-29 17:04:39 +00006200 *
6201 * Validates a value against the given built-in type.
6202 * This one is intended to be used internally for validation
6203 * of schema attribute values during parsing of the schema.
6204 *
6205 * Returns 0 if the value is valid, a positive error code
6206 * number otherwise and -1 in case of an internal or API error.
6207 */
6208static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006209xmlSchemaPValAttrNodeValue(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006210 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006211 xmlAttrPtr attr,
6212 const xmlChar *value,
6213 xmlSchemaTypePtr type)
Daniel Veillard01fa6152004-06-29 17:04:39 +00006214{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006215
6216 int ret = 0;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006217
6218 /*
6219 * NOTE: Should we move this to xmlschematypes.c? Hmm, but this
6220 * one is really meant to be used internally, so better not.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006221 */
6222 if ((pctxt == NULL) || (type == NULL) || (attr == NULL))
Daniel Veillard01fa6152004-06-29 17:04:39 +00006223 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006224 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6225 PERROR_INT("xmlSchemaPValAttrNodeValue",
6226 "the given type is not a built-in type");
6227 return (-1);
6228 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006229 switch (type->builtInType) {
6230 case XML_SCHEMAS_NCNAME:
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006231 case XML_SCHEMAS_QNAME:
6232 case XML_SCHEMAS_ANYURI:
6233 case XML_SCHEMAS_TOKEN:
6234 case XML_SCHEMAS_LANGUAGE:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006235 ret = xmlSchemaValPredefTypeNode(type, value, NULL,
6236 (xmlNodePtr) attr);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006237 break;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006238 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006239 PERROR_INT("xmlSchemaPValAttrNodeValue",
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +00006240 "validation using the given type is not supported while "
6241 "parsing a schema");
Daniel Veillard01fa6152004-06-29 17:04:39 +00006242 return (-1);
6243 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006244 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006245 /*
6246 * TODO: Should we use the S4S error codes instead?
6247 */
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006248 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006249 PERROR_INT("xmlSchemaPValAttrNodeValue",
6250 "failed to validate a schema attribute value");
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +00006251 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006252 } else if (ret > 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006253 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006254 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
6255 else
6256 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
6257 xmlSchemaPSimpleTypeErr(pctxt,
6258 ret, ownerItem, (xmlNodePtr) attr,
6259 type, NULL, value, NULL, NULL, NULL);
6260 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00006261 return (ret);
6262}
6263
6264/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006265 * xmlSchemaPValAttrNode:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006266 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006267 * @ctxt: a schema parser context
6268 * @ownerDes: the designation of the parent element
6269 * @ownerItem: the schema object owner if existent
6270 * @attr: the schema attribute node being validated
6271 * @type: the built-in type to be validated against
6272 * @value: the resulting value if any
6273 *
6274 * Extracts and validates a value against the given built-in type.
6275 * This one is intended to be used internally for validation
6276 * of schema attribute values during parsing of the schema.
6277 *
6278 * Returns 0 if the value is valid, a positive error code
6279 * number otherwise and -1 in case of an internal or API error.
6280 */
6281static int
6282xmlSchemaPValAttrNode(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006283 xmlSchemaBasicItemPtr ownerItem,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006284 xmlAttrPtr attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006285 xmlSchemaTypePtr type,
6286 const xmlChar **value)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006287{
Daniel Veillardc0826a72004-08-10 14:17:33 +00006288 const xmlChar *val;
6289
6290 if ((ctxt == NULL) || (type == NULL) || (attr == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006291 return (-1);
6292
Daniel Veillardc0826a72004-08-10 14:17:33 +00006293 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
6294 if (value != NULL)
6295 *value = val;
6296
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006297 return (xmlSchemaPValAttrNodeValue(ctxt, ownerItem, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006298 val, type));
Daniel Veillardc0826a72004-08-10 14:17:33 +00006299}
6300
6301/**
6302 * xmlSchemaPValAttr:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006303 *
Daniel Veillardc0826a72004-08-10 14:17:33 +00006304 * @ctxt: a schema parser context
6305 * @node: the element node of the attribute
6306 * @ownerDes: the designation of the parent element
6307 * @ownerItem: the schema object owner if existent
6308 * @ownerElem: the owner element node
6309 * @name: the name of the schema attribute node
6310 * @type: the built-in type to be validated against
6311 * @value: the resulting value if any
6312 *
6313 * Extracts and validates a value against the given built-in type.
6314 * This one is intended to be used internally for validation
6315 * of schema attribute values during parsing of the schema.
6316 *
6317 * Returns 0 if the value is valid, a positive error code
6318 * number otherwise and -1 in case of an internal or API error.
6319 */
6320static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006321xmlSchemaPValAttr(xmlSchemaParserCtxtPtr ctxt,
6322 xmlSchemaBasicItemPtr ownerItem,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006323 xmlNodePtr ownerElem,
6324 const char *name,
6325 xmlSchemaTypePtr type,
6326 const xmlChar **value)
6327{
6328 xmlAttrPtr attr;
6329
6330 if ((ctxt == NULL) || (type == NULL)) {
6331 if (value != NULL)
6332 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006333 return (-1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006334 }
6335 if (type->type != XML_SCHEMA_TYPE_BASIC) {
6336 if (value != NULL)
6337 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006338 xmlSchemaPErr(ctxt, ownerElem,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +00006339 XML_SCHEMAP_INTERNAL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006340 "Internal error: xmlSchemaPValAttr, the given "
6341 "type '%s' is not a built-in type.\n",
6342 type->name, NULL);
6343 return (-1);
6344 }
6345 attr = xmlSchemaGetPropNode(ownerElem, name);
6346 if (attr == NULL) {
6347 if (value != NULL)
6348 *value = NULL;
6349 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006350 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006351 return (xmlSchemaPValAttrNode(ctxt, ownerItem, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006352 type, value));
6353}
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006354
6355static int
6356xmlSchemaCheckReference(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006357 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006358 xmlNodePtr node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006359 xmlAttrPtr attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006360 const xmlChar *namespaceName)
6361{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006362 /* TODO: Pointer comparison instead? */
6363 if (xmlStrEqual(pctxt->targetNamespace, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006364 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006365 if (xmlStrEqual(xmlSchemaNs, namespaceName))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006366 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006367 /*
6368 * Check if the referenced namespace was <import>ed.
6369 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006370 if (WXS_BUCKET(pctxt)->relations != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006371 xmlSchemaSchemaRelationPtr rel;
6372
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006373 rel = WXS_BUCKET(pctxt)->relations;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006374 do {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006375 if (WXS_IS_BUCKET_IMPMAIN(rel->type) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006376 xmlStrEqual(namespaceName, rel->importNamespace))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006377 return (0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006378 rel = rel->next;
6379 } while (rel != NULL);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006380 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006381 /*
6382 * No matching <import>ed namespace found.
6383 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006384 {
6385 xmlNodePtr n = (attr != NULL) ? (xmlNodePtr) attr : node;
6386
6387 if (namespaceName == NULL)
6388 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6389 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6390 "References from this schema to components in no "
6391 "namespace are not allowed, since not indicated by an "
6392 "import statement", NULL, NULL);
6393 else
6394 xmlSchemaCustomErr(ACTXT_CAST pctxt,
6395 XML_SCHEMAP_SRC_RESOLVE, n, NULL,
6396 "References from this schema to components in the "
6397 "namespace '%s' are not allowed, since not indicated by an "
6398 "import statement", namespaceName, NULL);
6399 }
6400 return (XML_SCHEMAP_SRC_RESOLVE);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00006401}
6402
Daniel Veillardc0826a72004-08-10 14:17:33 +00006403/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006404 * xmlSchemaParseLocalAttributes:
Daniel Veillard4255d502002-04-16 15:50:10 +00006405 * @ctxt: a schema validation context
6406 * @schema: the schema being built
6407 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006408 * @type: the hosting type where the attributes will be anchored
Daniel Veillard4255d502002-04-16 15:50:10 +00006409 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006410 * Parses attribute uses and attribute declarations and
6411 * attribute group references.
Daniel Veillard4255d502002-04-16 15:50:10 +00006412 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006413static int
6414xmlSchemaParseLocalAttributes(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6415 xmlNodePtr *child, xmlSchemaItemListPtr *list,
6416 int parentType, int *hasRefs)
Daniel Veillard4255d502002-04-16 15:50:10 +00006417{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006418 void *item;
Daniel Veillard4255d502002-04-16 15:50:10 +00006419
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006420 while ((IS_SCHEMA((*child), "attribute")) ||
6421 (IS_SCHEMA((*child), "attributeGroup"))) {
6422 if (IS_SCHEMA((*child), "attribute")) {
6423 item = xmlSchemaParseLocalAttribute(ctxt, schema, *child,
6424 *list, parentType);
6425 } else {
6426 item = xmlSchemaParseAttributeGroupRef(ctxt, schema, *child);
6427 if ((item != NULL) && (hasRefs != NULL))
6428 *hasRefs = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006429 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006430 if (item != NULL) {
6431 if (*list == NULL) {
6432 /* TODO: Customize grow factor. */
6433 *list = xmlSchemaItemListCreate();
6434 if (*list == NULL)
6435 return(-1);
6436 }
6437 if (xmlSchemaItemListAddSize(*list, 2, item) == -1)
6438 return(-1);
6439 }
6440 *child = (*child)->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006441 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006442 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00006443}
6444
6445/**
6446 * xmlSchemaParseAnnotation:
6447 * @ctxt: a schema validation context
6448 * @schema: the schema being built
6449 * @node: a subtree containing XML Schema informations
6450 *
6451 * parse a XML schema Attrribute declaration
6452 * *WARNING* this interface is highly subject to change
6453 *
William M. Bracke7091952004-05-11 15:09:58 +00006454 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00006455 * 1 in case of success.
6456 */
6457static xmlSchemaAnnotPtr
6458xmlSchemaParseAnnotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006459 xmlNodePtr node, int needed)
Daniel Veillard4255d502002-04-16 15:50:10 +00006460{
6461 xmlSchemaAnnotPtr ret;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006462 xmlNodePtr child = NULL;
6463 xmlAttrPtr attr;
6464 int barked = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00006465
Daniel Veillardc0826a72004-08-10 14:17:33 +00006466 /*
6467 * INFO: S4S completed.
6468 */
6469 /*
6470 * id = ID
6471 * {any attributes with non-schema namespace . . .}>
6472 * Content: (appinfo | documentation)*
6473 */
Daniel Veillard4255d502002-04-16 15:50:10 +00006474 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6475 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006476 if (needed)
6477 ret = xmlSchemaNewAnnot(ctxt, node);
6478 else
6479 ret = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006480 attr = node->properties;
6481 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006482 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006483 (!xmlStrEqual(attr->name, BAD_CAST "id"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006484 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006485 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006486
6487 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006488 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006489 }
6490 attr = attr->next;
6491 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006492 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006493 /*
6494 * And now for the children...
6495 */
6496 child = node->children;
6497 while (child != NULL) {
6498 if (IS_SCHEMA(child, "appinfo")) {
6499 /* TODO: make available the content of "appinfo". */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006500 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00006501 * source = anyURI
6502 * {any attributes with non-schema namespace . . .}>
6503 * Content: ({any})*
6504 */
6505 attr = child->properties;
6506 while (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006507 if (((attr->ns == NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006508 (!xmlStrEqual(attr->name, BAD_CAST "source"))) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006509 ((attr->ns != NULL) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00006510 xmlStrEqual(attr->ns->href, xmlSchemaNs))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006511
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006512 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006513 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006514 }
6515 attr = attr->next;
6516 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006517 xmlSchemaPValAttr(ctxt, NULL, child, "source",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006518 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006519 child = child->next;
6520 } else if (IS_SCHEMA(child, "documentation")) {
6521 /* TODO: make available the content of "documentation". */
6522 /*
6523 * source = anyURI
6524 * {any attributes with non-schema namespace . . .}>
6525 * Content: ({any})*
6526 */
6527 attr = child->properties;
6528 while (attr != NULL) {
6529 if (attr->ns == NULL) {
6530 if (!xmlStrEqual(attr->name, BAD_CAST "source")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006531 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006532 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006533 }
6534 } else {
6535 if (xmlStrEqual(attr->ns->href, xmlSchemaNs) ||
6536 (xmlStrEqual(attr->name, BAD_CAST "lang") &&
6537 (!xmlStrEqual(attr->ns->href, XML_XML_NAMESPACE)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006538
6539 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006540 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006541 }
6542 }
6543 attr = attr->next;
6544 }
6545 /*
6546 * Attribute "xml:lang".
6547 */
6548 attr = xmlSchemaGetPropNodeNs(child, (const char *) XML_XML_NAMESPACE, "lang");
6549 if (attr != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006550 xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006551 xmlSchemaGetBuiltInType(XML_SCHEMAS_LANGUAGE), NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006552 child = child->next;
6553 } else {
6554 if (!barked)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006555 xmlSchemaPContentErr(ctxt,
6556 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006557 NULL, node, child, NULL, "(appinfo | documentation)*");
Daniel Veillardc0826a72004-08-10 14:17:33 +00006558 barked = 1;
6559 child = child->next;
6560 }
6561 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006562
Daniel Veillard4255d502002-04-16 15:50:10 +00006563 return (ret);
6564}
6565
6566/**
6567 * xmlSchemaParseFacet:
6568 * @ctxt: a schema validation context
6569 * @schema: the schema being built
6570 * @node: a subtree containing XML Schema informations
6571 *
6572 * parse a XML schema Facet declaration
6573 * *WARNING* this interface is highly subject to change
6574 *
6575 * Returns the new type structure or NULL in case of error
6576 */
6577static xmlSchemaFacetPtr
6578xmlSchemaParseFacet(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006579 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006580{
6581 xmlSchemaFacetPtr facet;
6582 xmlNodePtr child = NULL;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006583 const xmlChar *value;
Daniel Veillard4255d502002-04-16 15:50:10 +00006584
6585 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6586 return (NULL);
6587
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00006588 facet = xmlSchemaNewFacet();
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006589 if (facet == NULL) {
6590 xmlSchemaPErrMemory(ctxt, "allocating facet", node);
6591 return (NULL);
6592 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006593 facet->node = node;
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006594 value = xmlSchemaGetProp(ctxt, node, "value");
Daniel Veillard4255d502002-04-16 15:50:10 +00006595 if (value == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006596 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_FACET_NO_VALUE,
6597 "Facet %s has no value\n", node->name, NULL);
6598 xmlSchemaFreeFacet(facet);
Daniel Veillard4255d502002-04-16 15:50:10 +00006599 return (NULL);
6600 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006601 if (IS_SCHEMA(node, "minInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006602 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006603 } else if (IS_SCHEMA(node, "minExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006604 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006605 } else if (IS_SCHEMA(node, "maxInclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006606 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006607 } else if (IS_SCHEMA(node, "maxExclusive")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006608 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006609 } else if (IS_SCHEMA(node, "totalDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006610 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006611 } else if (IS_SCHEMA(node, "fractionDigits")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006612 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006613 } else if (IS_SCHEMA(node, "pattern")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006614 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006615 } else if (IS_SCHEMA(node, "enumeration")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006616 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006617 } else if (IS_SCHEMA(node, "whiteSpace")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006618 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006619 } else if (IS_SCHEMA(node, "length")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006620 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006621 } else if (IS_SCHEMA(node, "maxLength")) {
Daniel Veillard4255d502002-04-16 15:50:10 +00006622 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
6623 } else if (IS_SCHEMA(node, "minLength")) {
6624 facet->type = XML_SCHEMA_FACET_MINLENGTH;
6625 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006626 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_TYPE,
6627 "Unknown facet type %s\n", node->name, NULL);
6628 xmlSchemaFreeFacet(facet);
6629 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006630 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006631 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +00006632 facet->value = value;
Daniel Veillard01fa6152004-06-29 17:04:39 +00006633 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
6634 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
6635 const xmlChar *fixed;
6636
6637 fixed = xmlSchemaGetProp(ctxt, node, "fixed");
6638 if (fixed != NULL) {
6639 if (xmlStrEqual(fixed, BAD_CAST "true"))
6640 facet->fixed = 1;
6641 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006642 }
Daniel Veillard4255d502002-04-16 15:50:10 +00006643 child = node->children;
6644
6645 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006646 facet->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006647 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006648 }
6649 if (child != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006650 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_UNKNOWN_FACET_CHILD,
6651 "Facet %s has unexpected child content\n",
6652 node->name, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006653 }
6654 return (facet);
6655}
6656
6657/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00006658 * xmlSchemaParseWildcardNs:
6659 * @ctxt: a schema parser context
6660 * @wildc: the wildcard, already created
6661 * @node: a subtree containing XML Schema informations
6662 *
6663 * Parses the attribute "processContents" and "namespace"
6664 * of a xsd:anyAttribute and xsd:any.
6665 * *WARNING* this interface is highly subject to change
6666 *
6667 * Returns 0 if everything goes fine, a positive error code
6668 * if something is not valid and -1 if an internal error occurs.
6669 */
6670static int
6671xmlSchemaParseWildcardNs(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006672 xmlSchemaPtr schema ATTRIBUTE_UNUSED,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006673 xmlSchemaWildcardPtr wildc,
6674 xmlNodePtr node)
6675{
6676 const xmlChar *pc, *ns, *dictnsItem;
6677 int ret = 0;
6678 xmlChar *nsItem;
6679 xmlSchemaWildcardNsPtr tmp, lastNs = NULL;
6680 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006681
Daniel Veillardc0826a72004-08-10 14:17:33 +00006682 pc = xmlSchemaGetProp(ctxt, node, "processContents");
6683 if ((pc == NULL)
6684 || (xmlStrEqual(pc, (const xmlChar *) "strict"))) {
6685 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
6686 } else if (xmlStrEqual(pc, (const xmlChar *) "skip")) {
6687 wildc->processContents = XML_SCHEMAS_ANY_SKIP;
6688 } else if (xmlStrEqual(pc, (const xmlChar *) "lax")) {
6689 wildc->processContents = XML_SCHEMAS_ANY_LAX;
6690 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006691 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006692 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006693 NULL, node,
6694 NULL, "(strict | skip | lax)", pc,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006695 NULL, NULL, NULL);
6696 wildc->processContents = XML_SCHEMAS_ANY_STRICT;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006697 ret = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006698 }
6699 /*
6700 * Build the namespace constraints.
6701 */
6702 attr = xmlSchemaGetPropNode(node, "namespace");
6703 ns = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00006704 if ((attr == NULL) || (xmlStrEqual(ns, BAD_CAST "##any")))
Daniel Veillardc0826a72004-08-10 14:17:33 +00006705 wildc->any = 1;
6706 else if (xmlStrEqual(ns, BAD_CAST "##other")) {
6707 wildc->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006708 if (wildc->negNsSet == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006709 return (-1);
6710 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006711 wildc->negNsSet->value = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006712 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00006713 const xmlChar *end, *cur;
6714
6715 cur = ns;
6716 do {
6717 while (IS_BLANK_CH(*cur))
6718 cur++;
6719 end = cur;
6720 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
6721 end++;
6722 if (end == cur)
6723 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006724 nsItem = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006725 if ((xmlStrEqual(nsItem, BAD_CAST "##other")) ||
6726 (xmlStrEqual(nsItem, BAD_CAST "##any"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006727 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006728 XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006729 NULL, (xmlNodePtr) attr,
6730 NULL,
6731 "((##any | ##other) | List of (xs:anyURI | "
6732 "(##targetNamespace | ##local)))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00006733 nsItem, NULL, NULL, NULL);
6734 ret = XML_SCHEMAP_WILDCARD_INVALID_NS_MEMBER;
6735 } else {
6736 if (xmlStrEqual(nsItem, BAD_CAST "##targetNamespace")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00006737 dictnsItem = ctxt->targetNamespace;
Daniel Veillardc0826a72004-08-10 14:17:33 +00006738 } else if (xmlStrEqual(nsItem, BAD_CAST "##local")) {
6739 dictnsItem = NULL;
6740 } else {
6741 /*
6742 * Validate the item (anyURI).
6743 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006744 xmlSchemaPValAttrNodeValue(ctxt, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00006745 nsItem, xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI));
6746 dictnsItem = xmlDictLookup(ctxt->dict, nsItem, -1);
6747 }
6748 /*
6749 * Avoid dublicate namespaces.
6750 */
6751 tmp = wildc->nsSet;
6752 while (tmp != NULL) {
6753 if (dictnsItem == tmp->value)
6754 break;
6755 tmp = tmp->next;
6756 }
6757 if (tmp == NULL) {
6758 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
6759 if (tmp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006760 xmlFree(nsItem);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006761 return (-1);
6762 }
6763 tmp->value = dictnsItem;
6764 tmp->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006765 if (wildc->nsSet == NULL)
Daniel Veillardc0826a72004-08-10 14:17:33 +00006766 wildc->nsSet = tmp;
6767 else
6768 lastNs->next = tmp;
6769 lastNs = tmp;
6770 }
6771
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006772 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00006773 xmlFree(nsItem);
6774 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006775 } while (*cur != 0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00006776 }
6777 return (ret);
6778}
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006779
6780static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006781xmlSchemaPCheckParticleCorrect_2(xmlSchemaParserCtxtPtr ctxt,
6782 xmlSchemaParticlePtr item ATTRIBUTE_UNUSED,
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006783 xmlNodePtr node,
6784 int minOccurs,
6785 int maxOccurs) {
6786
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006787 if ((maxOccurs == 0) && ( minOccurs == 0))
6788 return (0);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006789 if (maxOccurs != UNBOUNDED) {
6790 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006791 * TODO: Maybe we should better not create the particle,
6792 * if min/max is invalid, since it could confuse the build of the
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006793 * content model.
6794 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006795 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006796 * 3.9.6 Schema Component Constraint: Particle Correct
6797 *
6798 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006799 if (maxOccurs < 1) {
6800 /*
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006801 * 2.2 {max occurs} must be greater than or equal to 1.
6802 */
6803 xmlSchemaPCustomAttrErr(ctxt,
6804 XML_SCHEMAP_P_PROPS_CORRECT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006805 NULL, NULL,
6806 xmlSchemaGetPropNode(node, "maxOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006807 "The value must be greater than or equal to 1");
6808 return (XML_SCHEMAP_P_PROPS_CORRECT_2_2);
6809 } else if (minOccurs > maxOccurs) {
6810 /*
6811 * 2.1 {min occurs} must not be greater than {max occurs}.
6812 */
6813 xmlSchemaPCustomAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006814 XML_SCHEMAP_P_PROPS_CORRECT_2_1,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006815 NULL, NULL,
6816 xmlSchemaGetPropNode(node, "minOccurs"),
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006817 "The value must not be greater than the value of 'maxOccurs'");
6818 return (XML_SCHEMAP_P_PROPS_CORRECT_2_1);
6819 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006820 }
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006821 return (0);
6822}
6823
Daniel Veillardc0826a72004-08-10 14:17:33 +00006824/**
Daniel Veillard4255d502002-04-16 15:50:10 +00006825 * xmlSchemaParseAny:
6826 * @ctxt: a schema validation context
6827 * @schema: the schema being built
6828 * @node: a subtree containing XML Schema informations
6829 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006830 * Parsea a XML schema <any> element. A particle and wildcard
6831 * will be created (except if minOccurs==maxOccurs==0, in this case
6832 * nothing will be created).
Daniel Veillard4255d502002-04-16 15:50:10 +00006833 * *WARNING* this interface is highly subject to change
6834 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006835 * Returns the particle or NULL in case of error or if minOccurs==maxOccurs==0
Daniel Veillard4255d502002-04-16 15:50:10 +00006836 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006837static xmlSchemaParticlePtr
Daniel Veillard4255d502002-04-16 15:50:10 +00006838xmlSchemaParseAny(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
6839 xmlNodePtr node)
6840{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006841 xmlSchemaParticlePtr particle;
Daniel Veillard4255d502002-04-16 15:50:10 +00006842 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006843 xmlSchemaWildcardPtr wild;
6844 int min, max;
6845 xmlAttrPtr attr;
6846 xmlSchemaAnnotPtr annot = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00006847
6848 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6849 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006850 /*
6851 * Check for illegal attributes.
6852 */
6853 attr = node->properties;
6854 while (attr != NULL) {
6855 if (attr->ns == NULL) {
6856 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
6857 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
6858 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
6859 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
6860 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006861 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006862 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006863 }
6864 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006865 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006866 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006867 }
6868 attr = attr->next;
6869 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006870 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006871 /*
6872 * minOccurs/maxOccurs.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006873 */
6874 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
6875 "(xs:nonNegativeInteger | unbounded)");
6876 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1,
6877 "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006878 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
6879 /*
6880 * Create & parse the wildcard.
6881 */
6882 wild = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY, node);
6883 if (wild == NULL)
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00006884 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006885 xmlSchemaParseWildcardNs(ctxt, schema, wild, node);
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006886 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006887 * And now for the children...
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +00006888 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006889 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00006890 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006891 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006892 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006893 }
6894 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006895 xmlSchemaPContentErr(ctxt,
6896 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006897 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006898 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006899 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006900 /*
6901 * No component if minOccurs==maxOccurs==0.
6902 */
6903 if ((min == 0) && (max == 0)) {
6904 /* Don't free the wildcard, since it's already on the list. */
6905 return (NULL);
6906 }
6907 /*
6908 * Create the particle.
6909 */
6910 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
6911 if (particle == NULL)
6912 return (NULL);
6913 particle->annot = annot;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00006914 particle->children = (xmlSchemaTreeItemPtr) wild;
Daniel Veillard4255d502002-04-16 15:50:10 +00006915
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006916 return (particle);
Daniel Veillard4255d502002-04-16 15:50:10 +00006917}
6918
6919/**
6920 * xmlSchemaParseNotation:
6921 * @ctxt: a schema validation context
6922 * @schema: the schema being built
6923 * @node: a subtree containing XML Schema informations
6924 *
6925 * parse a XML schema Notation declaration
6926 *
6927 * Returns the new structure or NULL in case of error
6928 */
6929static xmlSchemaNotationPtr
6930xmlSchemaParseNotation(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006931 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006932{
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006933 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00006934 xmlSchemaNotationPtr ret;
6935 xmlNodePtr child = NULL;
6936
6937 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6938 return (NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +00006939 name = xmlSchemaGetProp(ctxt, node, "name");
Daniel Veillard4255d502002-04-16 15:50:10 +00006940 if (name == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006941 xmlSchemaPErr2(ctxt, node, child, XML_SCHEMAP_NOTATION_NO_NAME,
6942 "Notation has no name\n", NULL, NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00006943 return (NULL);
6944 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006945 ret = xmlSchemaAddNotation(ctxt, schema, name,
6946 ctxt->targetNamespace, node);
6947 if (ret == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +00006948 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006949 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006950
6951 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006952 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00006953 child = child->next;
6954 }
6955
Daniel Veillard4255d502002-04-16 15:50:10 +00006956 child = node->children;
6957 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006958 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006959 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00006960 }
6961 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006962 xmlSchemaPContentErr(ctxt,
6963 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00006964 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006965 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00006966 }
6967
6968 return (ret);
6969}
6970
6971/**
6972 * xmlSchemaParseAnyAttribute:
6973 * @ctxt: a schema validation context
6974 * @schema: the schema being built
6975 * @node: a subtree containing XML Schema informations
6976 *
6977 * parse a XML schema AnyAttrribute declaration
6978 * *WARNING* this interface is highly subject to change
6979 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006980 * Returns a wildcard or NULL.
Daniel Veillard4255d502002-04-16 15:50:10 +00006981 */
Daniel Veillard3646d642004-06-02 19:19:14 +00006982static xmlSchemaWildcardPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +00006983xmlSchemaParseAnyAttribute(xmlSchemaParserCtxtPtr ctxt,
6984 xmlSchemaPtr schema, xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00006985{
Daniel Veillard3646d642004-06-02 19:19:14 +00006986 xmlSchemaWildcardPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00006987 xmlNodePtr child = NULL;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006988 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00006989
6990 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
6991 return (NULL);
6992
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00006993 ret = xmlSchemaAddWildcard(ctxt, schema, XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
6994 node);
Daniel Veillard4255d502002-04-16 15:50:10 +00006995 if (ret == NULL) {
6996 return (NULL);
6997 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00006998 /*
6999 * Check for illegal attributes.
7000 */
7001 attr = node->properties;
7002 while (attr != NULL) {
7003 if (attr->ns == NULL) {
7004 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7005 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
7006 (!xmlStrEqual(attr->name, BAD_CAST "processContents"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007007 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007008 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007009 }
7010 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007011 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007012 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007013 }
7014 attr = attr->next;
7015 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007016 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007017 /*
7018 * Parse the namespace list.
7019 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007020 if (xmlSchemaParseWildcardNs(ctxt, schema, ret, node) != 0)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007021 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007022 /*
7023 * And now for the children...
7024 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007025 child = node->children;
7026 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007027 ret->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007028 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007029 }
7030 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00007031 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007032 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007033 NULL, node, child,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007034 NULL, "(annotation?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00007035 }
7036
7037 return (ret);
7038}
7039
7040
7041/**
7042 * xmlSchemaParseAttribute:
7043 * @ctxt: a schema validation context
7044 * @schema: the schema being built
7045 * @node: a subtree containing XML Schema informations
7046 *
7047 * parse a XML schema Attrribute declaration
7048 * *WARNING* this interface is highly subject to change
7049 *
William M. Bracke7091952004-05-11 15:09:58 +00007050 * Returns the attribute declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00007051 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007052static xmlSchemaBasicItemPtr
7053xmlSchemaParseLocalAttribute(xmlSchemaParserCtxtPtr pctxt,
7054 xmlSchemaPtr schema,
7055 xmlNodePtr node,
7056 xmlSchemaItemListPtr uses,
7057 int parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +00007058{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007059 const xmlChar *attrValue, *name = NULL, *ns = NULL;
7060 xmlSchemaAttributeUsePtr use = NULL;
7061 xmlNodePtr child = NULL;
7062 xmlAttrPtr attr;
7063 const xmlChar *tmpNs = NULL, *tmpName = NULL, *defValue = NULL;
7064 int isRef = 0, occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7065 int nberrors, hasForm = 0, defValueType = 0;
7066
7067#define WXS_ATTR_DEF_VAL_DEFAULT 1
7068#define WXS_ATTR_DEF_VAL_FIXED 2
7069
7070 /*
7071 * 3.2.3 Constraints on XML Representations of Attribute Declarations
7072 */
7073
7074 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7075 return (NULL);
7076 attr = xmlSchemaGetPropNode(node, "ref");
7077 if (attr != NULL) {
7078 if (xmlSchemaPValAttrNodeQName(pctxt, schema,
7079 NULL, attr, &tmpNs, &tmpName) != 0) {
7080 return (NULL);
7081 }
7082 if (xmlSchemaCheckReference(pctxt, schema, node, attr, tmpNs) != 0)
7083 return(NULL);
7084 isRef = 1;
7085 }
7086 nberrors = pctxt->nberrors;
7087 /*
7088 * Check for illegal attributes.
7089 */
7090 attr = node->properties;
7091 while (attr != NULL) {
7092 if (attr->ns == NULL) {
7093 if (isRef) {
7094 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7095 xmlSchemaPValAttrNodeID(pctxt, attr);
7096 goto attr_next;
7097 } else if (xmlStrEqual(attr->name, BAD_CAST "ref")) {
7098 goto attr_next;
7099 }
7100 } else {
7101 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
7102 goto attr_next;
7103 } else if (xmlStrEqual(attr->name, BAD_CAST "id")) {
7104 xmlSchemaPValAttrNodeID(pctxt, attr);
7105 goto attr_next;
7106 } else if (xmlStrEqual(attr->name, BAD_CAST "type")) {
7107 xmlSchemaPValAttrNodeQName(pctxt, schema, NULL,
7108 attr, &tmpNs, &tmpName);
7109 goto attr_next;
7110 } else if (xmlStrEqual(attr->name, BAD_CAST "form")) {
7111 /*
7112 * Evaluate the target namespace
7113 */
7114 hasForm = 1;
7115 attrValue = xmlSchemaGetNodeContent(pctxt,
7116 (xmlNodePtr) attr);
7117 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
7118 ns = pctxt->targetNamespace;
7119 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified"))
7120 {
7121 xmlSchemaPSimpleTypeErr(pctxt,
7122 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
7123 NULL, (xmlNodePtr) attr,
7124 NULL, "(qualified | unqualified)",
7125 attrValue, NULL, NULL, NULL);
7126 }
7127 goto attr_next;
7128 }
7129 }
7130 if (xmlStrEqual(attr->name, BAD_CAST "use")) {
7131
7132 attrValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7133 /* TODO: Maybe we need to normalize the value beforehand. */
7134 if (xmlStrEqual(attrValue, BAD_CAST "optional"))
7135 occurs = XML_SCHEMAS_ATTR_USE_OPTIONAL;
7136 else if (xmlStrEqual(attrValue, BAD_CAST "prohibited"))
7137 occurs = XML_SCHEMAS_ATTR_USE_PROHIBITED;
7138 else if (xmlStrEqual(attrValue, BAD_CAST "required"))
7139 occurs = XML_SCHEMAS_ATTR_USE_REQUIRED;
7140 else {
7141 xmlSchemaPSimpleTypeErr(pctxt,
7142 XML_SCHEMAP_INVALID_ATTR_USE,
7143 NULL, (xmlNodePtr) attr,
7144 NULL, "(optional | prohibited | required)",
7145 attrValue, NULL, NULL, NULL);
7146 }
7147 goto attr_next;
7148 } else if (xmlStrEqual(attr->name, BAD_CAST "default")) {
7149 /*
7150 * 3.2.3 : 1
7151 * default and fixed must not both be present.
7152 */
7153 if (defValue) {
7154 xmlSchemaPMutualExclAttrErr(pctxt,
7155 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7156 NULL, attr, "default", "fixed");
7157 } else {
7158 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7159 defValueType = WXS_ATTR_DEF_VAL_DEFAULT;
7160 }
7161 goto attr_next;
7162 } else if (xmlStrEqual(attr->name, BAD_CAST "fixed")) {
7163 /*
7164 * 3.2.3 : 1
7165 * default and fixed must not both be present.
7166 */
7167 if (defValue) {
7168 xmlSchemaPMutualExclAttrErr(pctxt,
7169 XML_SCHEMAP_SRC_ATTRIBUTE_1,
7170 NULL, attr, "default", "fixed");
7171 } else {
7172 defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
7173 defValueType = WXS_ATTR_DEF_VAL_FIXED;
7174 }
7175 goto attr_next;
7176 }
7177 } else if (! xmlStrEqual(attr->ns->href, xmlSchemaNs))
7178 goto attr_next;
7179
7180 xmlSchemaPIllegalAttrErr(pctxt,
7181 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7182
7183attr_next:
7184 attr = attr->next;
7185 }
7186 /*
7187 * 3.2.3 : 2
7188 * If default and use are both present, use must have
7189 * the actual value optional.
7190 */
7191 if ((defValueType == WXS_ATTR_DEF_VAL_DEFAULT) &&
7192 (occurs != XML_SCHEMAS_ATTR_USE_OPTIONAL)) {
7193 xmlSchemaPSimpleTypeErr(pctxt,
7194 XML_SCHEMAP_SRC_ATTRIBUTE_2,
7195 NULL, node, NULL,
7196 "(optional | prohibited | required)", NULL,
7197 "The value of the attribute 'use' must be 'optional' "
7198 "if the attribute 'default' is present",
7199 NULL, NULL);
7200 }
7201 /*
7202 * We want correct attributes.
7203 */
7204 if (nberrors != pctxt->nberrors)
7205 return(NULL);
7206 if (! isRef) {
7207 xmlSchemaAttributePtr attrDecl;
7208
7209 /* TODO: move XML_SCHEMAS_QUALIF_ATTR to the parser. */
7210 if ((! hasForm) && (schema->flags & XML_SCHEMAS_QUALIF_ATTR))
7211 ns = pctxt->targetNamespace;
7212 /*
7213 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7214 * TODO: Move this to the component layer.
7215 */
7216 if (xmlStrEqual(ns, xmlSchemaInstanceNs)) {
7217 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7218 XML_SCHEMAP_NO_XSI,
7219 node, NULL,
7220 "The target namespace must not match '%s'",
7221 xmlSchemaInstanceNs, NULL);
7222 }
7223 attr = xmlSchemaGetPropNode(node, "name");
7224 if (attr == NULL) {
7225 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7226 NULL, node, "name", NULL);
7227 return (NULL);
7228 }
7229 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7230 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7231 return (NULL);
7232 }
7233 /*
7234 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7235 * TODO: Move this to the component layer.
7236 */
7237 if (xmlStrEqual(name, BAD_CAST "xmlns")) {
7238 xmlSchemaPSimpleTypeErr(pctxt,
7239 XML_SCHEMAP_NO_XMLNS,
7240 NULL, (xmlNodePtr) attr,
7241 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7242 "The value of the attribute must not match 'xmlns'",
7243 NULL, NULL);
7244 return (NULL);
7245 }
7246 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED)
7247 goto check_children;
7248 /*
7249 * Create the attribute use component.
7250 */
7251 use = xmlSchemaAddAttributeUse(pctxt, node);
7252 if (use == NULL)
7253 return(NULL);
7254 use->occurs = occurs;
7255 /*
7256 * Create the attribute declaration.
7257 */
7258 attrDecl = xmlSchemaAddAttribute(pctxt, schema, name, ns, node, 0);
7259 if (attrDecl == NULL)
7260 return (NULL);
7261 if (tmpName != NULL) {
7262 attrDecl->typeName = tmpName;
7263 attrDecl->typeNs = tmpNs;
7264 }
7265 use->attrDecl = attrDecl;
7266 /*
7267 * Value constraint.
7268 */
7269 if (defValue != NULL) {
7270 attrDecl->defValue = defValue;
7271 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7272 attrDecl->flags |= XML_SCHEMAS_ATTR_FIXED;
7273 }
7274 } else if (occurs != XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7275 xmlSchemaQNameRefPtr ref;
7276
7277 /*
7278 * Create the attribute use component.
7279 */
7280 use = xmlSchemaAddAttributeUse(pctxt, node);
7281 if (use == NULL)
7282 return(NULL);
7283 /*
7284 * We need to resolve the reference at later stage.
7285 */
7286 WXS_ADD_PENDING(pctxt, use);
7287 use->occurs = occurs;
7288 /*
7289 * Create a QName reference to the attribute declaration.
7290 */
7291 ref = xmlSchemaNewQNameRef(pctxt, XML_SCHEMA_TYPE_ATTRIBUTE,
7292 tmpName, tmpNs);
7293 if (ref == NULL)
7294 return(NULL);
7295 /*
7296 * Assign the reference. This will be substituted for the
7297 * referenced attribute declaration when the QName is resolved.
7298 */
7299 use->attrDecl = WXS_ATTR_CAST ref;
7300 /*
7301 * Value constraint.
7302 */
7303 if (defValue != NULL)
7304 use->defValue = defValue;
7305 if (defValueType == WXS_ATTR_DEF_VAL_FIXED)
7306 use->flags |= XML_SCHEMA_ATTR_USE_FIXED;
7307 }
7308
7309check_children:
7310 /*
7311 * And now for the children...
7312 */
7313 child = node->children;
7314 if (occurs == XML_SCHEMAS_ATTR_USE_PROHIBITED) {
7315 xmlSchemaAttributeUseProhibPtr prohib;
7316
7317 if (IS_SCHEMA(child, "annotation")) {
7318 xmlSchemaParseAnnotation(pctxt, schema, child, 0);
7319 child = child->next;
7320 }
7321 if (child != NULL) {
7322 xmlSchemaPContentErr(pctxt,
7323 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7324 NULL, node, child, NULL,
7325 "(annotation?)");
7326 }
7327 /*
7328 * Check for pointlessness of attribute prohibitions.
7329 */
7330 if (parentType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) {
7331 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7332 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7333 node, NULL,
7334 "Skipping attribute use prohibition, since it is "
7335 "pointless inside an <attributeGroup>",
7336 NULL, NULL, NULL);
7337 return(NULL);
7338 } else if (parentType == XML_SCHEMA_TYPE_EXTENSION) {
7339 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7340 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7341 node, NULL,
7342 "Skipping attribute use prohibition, since it is "
7343 "pointless when extending a type",
7344 NULL, NULL, NULL);
7345 return(NULL);
7346 }
7347 if (! isRef) {
7348 tmpName = name;
7349 tmpNs = ns;
7350 }
7351 /*
7352 * Check for duplicate attribute prohibitions.
7353 */
7354 if (uses) {
7355 int i;
7356
7357 for (i = 0; i < uses->nbItems; i++) {
7358 use = uses->items[i];
7359 if ((use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) &&
7360 (tmpName == (WXS_ATTR_PROHIB_CAST use)->name) &&
7361 (tmpNs == (WXS_ATTR_PROHIB_CAST use)->targetNamespace))
7362 {
7363 xmlChar *str = NULL;
7364
7365 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
7366 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
7367 node, NULL,
7368 "Skipping duplicate attribute use prohibition '%s'",
7369 xmlSchemaFormatQName(&str, tmpNs, tmpName),
7370 NULL, NULL);
7371 FREE_AND_NULL(str)
7372 return(NULL);
7373 }
7374 }
7375 }
7376 /*
7377 * Create the attribute prohibition helper component.
7378 */
7379 prohib = xmlSchemaAddAttributeUseProhib(pctxt);
7380 if (prohib == NULL)
7381 return(NULL);
7382 prohib->node = node;
7383 prohib->name = tmpName;
7384 prohib->targetNamespace = tmpNs;
7385 if (isRef) {
7386 /*
7387 * We need at least to resolve to the attribute declaration.
7388 */
7389 WXS_ADD_PENDING(pctxt, prohib);
7390 }
7391 return(WXS_BASIC_CAST prohib);
7392 } else {
7393 if (IS_SCHEMA(child, "annotation")) {
7394 /*
7395 * TODO: Should this go into the attr decl?
7396 */
7397 use->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
7398 child = child->next;
7399 }
7400 if (isRef) {
7401 if (child != NULL) {
7402 if (IS_SCHEMA(child, "simpleType"))
7403 /*
7404 * 3.2.3 : 3.2
7405 * If ref is present, then all of <simpleType>,
7406 * form and type must be absent.
7407 */
7408 xmlSchemaPContentErr(pctxt,
7409 XML_SCHEMAP_SRC_ATTRIBUTE_3_2,
7410 NULL, node, child, NULL,
7411 "(annotation?)");
7412 else
7413 xmlSchemaPContentErr(pctxt,
7414 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7415 NULL, node, child, NULL,
7416 "(annotation?)");
7417 }
7418 } else {
7419 if (IS_SCHEMA(child, "simpleType")) {
7420 if (WXS_ATTRUSE_DECL(use)->typeName != NULL) {
7421 /*
7422 * 3.2.3 : 4
7423 * type and <simpleType> must not both be present.
7424 */
7425 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7426 NULL, node, child,
7427 "The attribute 'type' and the <simpleType> child "
7428 "are mutually exclusive", NULL);
7429 } else
7430 WXS_ATTRUSE_TYPEDEF(use) =
7431 xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7432 child = child->next;
7433 }
7434 if (child != NULL)
7435 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7436 NULL, node, child, NULL,
7437 "(annotation?, simpleType?)");
7438 }
7439 }
7440 return (WXS_BASIC_CAST use);
7441}
7442
7443
7444static xmlSchemaAttributePtr
7445xmlSchemaParseGlobalAttribute(xmlSchemaParserCtxtPtr pctxt,
7446 xmlSchemaPtr schema,
7447 xmlNodePtr node)
7448{
7449 const xmlChar *attrValue;
Daniel Veillard4255d502002-04-16 15:50:10 +00007450 xmlSchemaAttributePtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007451 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007452 xmlAttrPtr attr;
William M. Bracke7091952004-05-11 15:09:58 +00007453
7454 /*
7455 * Note that the w3c spec assumes the schema to be validated with schema
7456 * for schemas beforehand.
7457 *
7458 * 3.2.3 Constraints on XML Representations of Attribute Declarations
William M. Bracke7091952004-05-11 15:09:58 +00007459 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007460 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7461 return (NULL);
7462 /*
7463 * 3.2.3 : 3.1
7464 * One of ref or name must be present, but not both
7465 */
7466 attr = xmlSchemaGetPropNode(node, "name");
7467 if (attr == NULL) {
7468 xmlSchemaPMissingAttrErr(pctxt, XML_SCHEMAP_S4S_ATTR_MISSING,
7469 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007470 return (NULL);
7471 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007472 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
7473 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0) {
7474 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007475 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007476 /*
7477 * 3.2.6 Schema Component Constraint: xmlns Not Allowed
7478 * TODO: Move this to the component layer.
7479 */
7480 if (xmlStrEqual(attrValue, BAD_CAST "xmlns")) {
7481 xmlSchemaPSimpleTypeErr(pctxt,
7482 XML_SCHEMAP_NO_XMLNS,
7483 NULL, (xmlNodePtr) attr,
7484 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), NULL, NULL,
7485 "The value of the attribute must not match 'xmlns'",
7486 NULL, NULL);
7487 return (NULL);
7488 }
7489 /*
7490 * 3.2.6 Schema Component Constraint: xsi: Not Allowed
7491 * TODO: Move this to the component layer.
7492 * Or better leave it here and add it to the component layer
7493 * if we have a schema construction API.
7494 */
7495 if (xmlStrEqual(pctxt->targetNamespace, xmlSchemaInstanceNs)) {
7496 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7497 XML_SCHEMAP_NO_XSI, node, NULL,
7498 "The target namespace must not match '%s'",
7499 xmlSchemaInstanceNs, NULL);
7500 }
7501
7502 ret = xmlSchemaAddAttribute(pctxt, schema, attrValue,
7503 pctxt->targetNamespace, node, 1);
7504 if (ret == NULL)
7505 return (NULL);
7506 ret->flags |= XML_SCHEMAS_ATTR_GLOBAL;
7507
7508 /*
7509 * Check for illegal attributes.
7510 */
7511 attr = node->properties;
7512 while (attr != NULL) {
7513 if (attr->ns == NULL) {
7514 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
7515 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
7516 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
7517 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7518 (!xmlStrEqual(attr->name, BAD_CAST "type")))
7519 {
7520 xmlSchemaPIllegalAttrErr(pctxt,
7521 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7522 }
7523 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7524 xmlSchemaPIllegalAttrErr(pctxt,
7525 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7526 }
7527 attr = attr->next;
7528 }
7529 xmlSchemaPValAttrQName(pctxt, schema, NULL,
7530 node, "type", &ret->typeNs, &ret->typeName);
7531
7532 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
William M. Bracke7091952004-05-11 15:09:58 +00007533 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007534 * Attribute "fixed".
William M. Bracke7091952004-05-11 15:09:58 +00007535 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007536 ret->defValue = xmlSchemaGetProp(pctxt, node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007537 if (ret->defValue != NULL)
7538 ret->flags |= XML_SCHEMAS_ATTR_FIXED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007539 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007540 * Attribute "default".
7541 */
7542 attr = xmlSchemaGetPropNode(node, "default");
7543 if (attr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007544 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00007545 * 3.2.3 : 1
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007546 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00007547 */
7548 if (ret->flags & XML_SCHEMAS_ATTR_FIXED) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007549 xmlSchemaPMutualExclAttrErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_1,
7550 WXS_BASIC_CAST ret, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00007551 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007552 ret->defValue = xmlSchemaGetNodeContent(pctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007553 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00007554 /*
7555 * And now for the children...
7556 */
Daniel Veillard4255d502002-04-16 15:50:10 +00007557 child = node->children;
7558 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007559 ret->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007560 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007561 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007562 if (IS_SCHEMA(child, "simpleType")) {
7563 if (ret->typeName != NULL) {
7564 /*
7565 * 3.2.3 : 4
7566 * type and <simpleType> must not both be present.
7567 */
7568 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_SRC_ATTRIBUTE_4,
7569 NULL, node, child,
7570 "The attribute 'type' and the <simpleType> child "
7571 "are mutually exclusive", NULL);
7572 } else
7573 ret->subtypes = xmlSchemaParseSimpleType(pctxt, schema, child, 0);
7574 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007575 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007576 if (child != NULL)
7577 xmlSchemaPContentErr(pctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7578 NULL, node, child, NULL,
7579 "(annotation?, simpleType?)");
7580
Daniel Veillard4255d502002-04-16 15:50:10 +00007581 return (ret);
7582}
7583
7584/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007585 * xmlSchemaParseAttributeGroupRef:
Daniel Veillard4255d502002-04-16 15:50:10 +00007586 * @ctxt: a schema validation context
7587 * @schema: the schema being built
7588 * @node: a subtree containing XML Schema informations
7589 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007590 * Parse an attribute group definition reference.
7591 * Note that a reference to an attribute group does not
7592 * correspond to any component at all.
Daniel Veillard4255d502002-04-16 15:50:10 +00007593 * *WARNING* this interface is highly subject to change
7594 *
7595 * Returns the attribute group or NULL in case of error.
7596 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007597static xmlSchemaQNameRefPtr
7598xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
7599 xmlSchemaPtr schema,
7600 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00007601{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007602 xmlSchemaQNameRefPtr ret;
Daniel Veillard4255d502002-04-16 15:50:10 +00007603 xmlNodePtr child = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007604 xmlAttrPtr attr;
7605 const xmlChar *refNs = NULL, *ref = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00007606
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007607 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00007608 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007609
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007610 attr = xmlSchemaGetPropNode(node, "ref");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007611 if (attr == NULL) {
7612 xmlSchemaPMissingAttrErr(pctxt,
7613 XML_SCHEMAP_S4S_ATTR_MISSING,
7614 NULL, node, "ref", NULL);
7615 return (NULL);
7616 }
7617 xmlSchemaPValAttrNodeQName(pctxt, schema,
7618 NULL, attr, &refNs, &ref);
7619 if (xmlSchemaCheckReference(pctxt, schema, node, attr, refNs) != 0)
7620 return(NULL);
7621
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007622 /*
7623 * Check for illegal attributes.
7624 */
7625 attr = node->properties;
7626 while (attr != NULL) {
7627 if (attr->ns == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007628 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007629 (!xmlStrEqual(attr->name, BAD_CAST "id")))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007630 {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007631 xmlSchemaPIllegalAttrErr(pctxt,
7632 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007633 }
7634 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007635 xmlSchemaPIllegalAttrErr(pctxt,
7636 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007637 }
7638 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007639 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00007640 /* Attribute ID */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007641 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7642
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007643 /*
7644 * And now for the children...
7645 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007646 child = node->children;
Daniel Veillard4255d502002-04-16 15:50:10 +00007647 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007648 /*
7649 * TODO: We do not have a place to store the annotation, do we?
7650 */
7651 xmlSchemaParseAnnotation(pctxt, schema, child, 0);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00007652 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00007653 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007654 if (child != NULL) {
7655 xmlSchemaPContentErr(pctxt,
7656 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
7657 NULL, node, child, NULL,
7658 "(annotation?)");
7659 }
7660
7661 /*
7662 * Handle attribute group redefinitions.
7663 */
7664 if (pctxt->isRedefine && pctxt->redef &&
7665 (pctxt->redef->item->type ==
7666 XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
7667 (ref == pctxt->redef->refName) &&
7668 (refNs == pctxt->redef->refTargetNs))
7669 {
7670 /*
7671 * SPEC src-redefine:
7672 * (7.1) "If it has an <attributeGroup> among its contents
7673 * the ·actual value· of whose ref [attribute] is the same
7674 * as the ·actual value· of its own name attribute plus
7675 * target namespace, then it must have exactly one such group."
7676 */
7677 if (pctxt->redefCounter != 0) {
7678 xmlChar *str = NULL;
7679
7680 xmlSchemaCustomErr(ACTXT_CAST pctxt,
7681 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
7682 "The redefining attribute group definition "
7683 "'%s' must not contain more than one "
7684 "reference to the redefined definition",
7685 xmlSchemaFormatQName(&str, refNs, ref), NULL);
7686 FREE_AND_NULL(str);
7687 return(NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007688 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007689 pctxt->redefCounter++;
7690 /*
7691 * URGENT TODO: How to ensure that the reference will not be
7692 * handled by the normal component resolution mechanism?
7693 */
7694 ret = xmlSchemaNewQNameRef(pctxt,
7695 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7696 if (ret == NULL)
7697 return(NULL);
7698 ret->node = node;
7699 pctxt->redef->reference = WXS_BASIC_CAST ret;
7700 } else {
7701 /*
7702 * Create a QName-reference helper component. We will substitute this
7703 * component for the attribute uses of the referenced attribute group
7704 * definition.
7705 */
7706 ret = xmlSchemaNewQNameRef(pctxt,
7707 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, ref, refNs);
7708 if (ret == NULL)
7709 return(NULL);
7710 ret->node = node;
7711 /* Add to pending items, to be able to resolve the reference. */
7712 WXS_ADD_PENDING(pctxt, ret);
7713 }
7714 return (ret);
7715}
7716
7717/**
7718 * xmlSchemaParseAttributeGroupDefinition:
7719 * @pctxt: a schema validation context
7720 * @schema: the schema being built
7721 * @node: a subtree containing XML Schema informations
7722 *
7723 * parse a XML schema Attribute Group declaration
7724 * *WARNING* this interface is highly subject to change
7725 *
7726 * Returns the attribute group definition or NULL in case of error.
7727 */
7728static xmlSchemaAttributeGroupPtr
7729xmlSchemaParseAttributeGroupDefinition(xmlSchemaParserCtxtPtr pctxt,
7730 xmlSchemaPtr schema,
7731 xmlNodePtr node)
7732{
7733 const xmlChar *name;
7734 xmlSchemaAttributeGroupPtr ret;
7735 xmlNodePtr child = NULL;
7736 xmlAttrPtr attr;
7737 int hasRefs = 0;
7738
7739 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
7740 return (NULL);
7741
7742 attr = xmlSchemaGetPropNode(node, "name");
7743 if (attr == NULL) {
7744 xmlSchemaPMissingAttrErr(pctxt,
7745 XML_SCHEMAP_S4S_ATTR_MISSING,
7746 NULL, node, "name", NULL);
7747 return (NULL);
7748 }
7749 /*
7750 * The name is crucial, exit if invalid.
7751 */
7752 if (xmlSchemaPValAttrNode(pctxt,
7753 NULL, attr,
7754 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
7755 return (NULL);
7756 }
7757 ret = xmlSchemaAddAttributeGroupDefinition(pctxt, schema,
7758 name, pctxt->targetNamespace, node);
7759 if (ret == NULL)
7760 return (NULL);
7761 /*
7762 * Check for illegal attributes.
7763 */
7764 attr = node->properties;
7765 while (attr != NULL) {
7766 if (attr->ns == NULL) {
7767 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
7768 (!xmlStrEqual(attr->name, BAD_CAST "id")))
7769 {
7770 xmlSchemaPIllegalAttrErr(pctxt,
7771 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7772 }
7773 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
7774 xmlSchemaPIllegalAttrErr(pctxt,
7775 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
7776 }
7777 attr = attr->next;
7778 }
7779 /* Attribute ID */
7780 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
7781 /*
7782 * And now for the children...
7783 */
7784 child = node->children;
7785 if (IS_SCHEMA(child, "annotation")) {
7786 ret->annot = xmlSchemaParseAnnotation(pctxt, schema, child, 1);
7787 child = child->next;
7788 }
7789 /*
7790 * Parse contained attribute decls/refs.
7791 */
7792 if (xmlSchemaParseLocalAttributes(pctxt, schema, &child,
7793 (xmlSchemaItemListPtr *) &(ret->attrUses),
7794 XML_SCHEMA_TYPE_ATTRIBUTEGROUP, &hasRefs) == -1)
7795 return(NULL);
7796 if (hasRefs)
7797 ret->flags |= XML_SCHEMAS_ATTRGROUP_HAS_REFS;
7798 /*
7799 * Parse the attribute wildcard.
7800 */
7801 if (IS_SCHEMA(child, "anyAttribute")) {
7802 ret->attributeWildcard = xmlSchemaParseAnyAttribute(pctxt,
7803 schema, child);
7804 child = child->next;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00007805 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007806 if (child != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007807 xmlSchemaPContentErr(pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007808 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007809 NULL, node, child, NULL,
7810 "(annotation?, ((attribute | attributeGroup)*, anyAttribute?))");
Daniel Veillard4255d502002-04-16 15:50:10 +00007811 }
Daniel Veillard4255d502002-04-16 15:50:10 +00007812 return (ret);
7813}
7814
7815/**
William M. Brack2f2a6632004-08-20 23:09:47 +00007816 * xmlSchemaPValAttrFormDefault:
7817 * @value: the value
7818 * @flags: the flags to be modified
7819 * @flagQualified: the specific flag for "qualified"
7820 *
7821 * Returns 0 if the value is valid, 1 otherwise.
7822 */
7823static int
7824xmlSchemaPValAttrFormDefault(const xmlChar *value,
7825 int *flags,
7826 int flagQualified)
7827{
7828 if (xmlStrEqual(value, BAD_CAST "qualified")) {
7829 if ((*flags & flagQualified) == 0)
7830 *flags |= flagQualified;
7831 } else if (!xmlStrEqual(value, BAD_CAST "unqualified"))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007832 return (1);
7833
William M. Brack2f2a6632004-08-20 23:09:47 +00007834 return (0);
7835}
7836
7837/**
Daniel Veillardc0826a72004-08-10 14:17:33 +00007838 * xmlSchemaPValAttrBlockFinal:
7839 * @value: the value
7840 * @flags: the flags to be modified
7841 * @flagAll: the specific flag for "#all"
7842 * @flagExtension: the specific flag for "extension"
7843 * @flagRestriction: the specific flag for "restriction"
7844 * @flagSubstitution: the specific flag for "substitution"
7845 * @flagList: the specific flag for "list"
7846 * @flagUnion: the specific flag for "union"
7847 *
7848 * Validates the value of the attribute "final" and "block". The value
7849 * is converted into the specified flag values and returned in @flags.
7850 *
7851 * Returns 0 if the value is valid, 1 otherwise.
7852 */
7853
7854static int
7855xmlSchemaPValAttrBlockFinal(const xmlChar *value,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007856 int *flags,
Daniel Veillardc0826a72004-08-10 14:17:33 +00007857 int flagAll,
7858 int flagExtension,
7859 int flagRestriction,
7860 int flagSubstitution,
7861 int flagList,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007862 int flagUnion)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007863{
7864 int ret = 0;
7865
7866 /*
7867 * TODO: This does not check for dublicate entries.
7868 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00007869 if ((flags == NULL) || (value == NULL))
7870 return (-1);
7871 if (value[0] == 0)
7872 return (0);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007873 if (xmlStrEqual(value, BAD_CAST "#all")) {
7874 if (flagAll != -1)
7875 *flags |= flagAll;
7876 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007877 if (flagExtension != -1)
7878 *flags |= flagExtension;
7879 if (flagRestriction != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007880 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007881 if (flagSubstitution != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007882 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007883 if (flagList != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007884 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007885 if (flagUnion != -1)
Daniel Veillardc0826a72004-08-10 14:17:33 +00007886 *flags |= flagUnion;
7887 }
7888 } else {
7889 const xmlChar *end, *cur = value;
7890 xmlChar *item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007891
Daniel Veillardc0826a72004-08-10 14:17:33 +00007892 do {
7893 while (IS_BLANK_CH(*cur))
7894 cur++;
7895 end = cur;
7896 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
7897 end++;
7898 if (end == cur)
7899 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007900 item = xmlStrndup(cur, end - cur);
Daniel Veillardc0826a72004-08-10 14:17:33 +00007901 if (xmlStrEqual(item, BAD_CAST "extension")) {
7902 if (flagExtension != -1) {
7903 if ((*flags & flagExtension) == 0)
7904 *flags |= flagExtension;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007905 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007906 ret = 1;
7907 } else if (xmlStrEqual(item, BAD_CAST "restriction")) {
7908 if (flagRestriction != -1) {
7909 if ((*flags & flagRestriction) == 0)
7910 *flags |= flagRestriction;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007911 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007912 ret = 1;
7913 } else if (xmlStrEqual(item, BAD_CAST "substitution")) {
7914 if (flagSubstitution != -1) {
7915 if ((*flags & flagSubstitution) == 0)
7916 *flags |= flagSubstitution;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007917 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007918 ret = 1;
7919 } else if (xmlStrEqual(item, BAD_CAST "list")) {
7920 if (flagList != -1) {
7921 if ((*flags & flagList) == 0)
7922 *flags |= flagList;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007923 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007924 ret = 1;
7925 } else if (xmlStrEqual(item, BAD_CAST "union")) {
7926 if (flagUnion != -1) {
7927 if ((*flags & flagUnion) == 0)
7928 *flags |= flagUnion;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007929 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007930 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007931 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00007932 ret = 1;
7933 if (item != NULL)
7934 xmlFree(item);
7935 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007936 } while ((ret == 0) && (*cur != 0));
7937 }
7938
Daniel Veillardc0826a72004-08-10 14:17:33 +00007939 return (ret);
7940}
7941
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007942static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007943xmlSchemaCheckCSelectorXPath(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007944 xmlSchemaIDCPtr idc,
7945 xmlSchemaIDCSelectPtr selector,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007946 xmlAttrPtr attr,
7947 int isField)
7948{
7949 xmlNodePtr node;
7950
7951 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007952 * c-selector-xpath:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007953 * Schema Component Constraint: Selector Value OK
7954 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007955 * TODO: 1 The {selector} must be a valid XPath expression, as defined
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007956 * in [XPath].
7957 */
7958 if (selector == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007959 xmlSchemaPErr(ctxt, idc->node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007960 XML_SCHEMAP_INTERNAL,
7961 "Internal error: xmlSchemaCheckCSelectorXPath, "
7962 "the selector is not specified.\n", NULL, NULL);
7963 return (-1);
7964 }
7965 if (attr == NULL)
7966 node = idc->node;
7967 else
7968 node = (xmlNodePtr) attr;
7969 if (selector->xpath == NULL) {
7970 xmlSchemaPCustomErr(ctxt,
7971 /* TODO: Adjust error code. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00007972 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00007973 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007974 "The XPath expression of the selector is not valid", NULL);
7975 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
7976 } else {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007977 const xmlChar **nsArray = NULL;
7978 xmlNsPtr *nsList = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00007979 /*
7980 * Compile the XPath expression.
7981 */
7982 /*
7983 * TODO: We need the array of in-scope namespaces for compilation.
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00007984 * TODO: Call xmlPatterncompile with different options for selector/
7985 * field.
7986 */
7987 nsList = xmlGetNsList(attr->doc, attr->parent);
7988 /*
7989 * Build an array of prefixes and namespaces.
7990 */
7991 if (nsList != NULL) {
7992 int i, count = 0;
7993 xmlNsPtr ns;
7994
7995 for (i = 0; nsList[i] != NULL; i++)
7996 count++;
7997
7998 nsArray = (const xmlChar **) xmlMalloc(
7999 (count * 2 + 1) * sizeof(const xmlChar *));
8000 if (nsArray == NULL) {
8001 xmlSchemaPErrMemory(ctxt, "allocating a namespace array",
8002 NULL);
Kasimier T. Buchcik940ab0c2005-10-19 17:00:53 +00008003 xmlFree(nsList);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008004 return (-1);
8005 }
8006 for (i = 0; i < count; i++) {
8007 ns = nsList[i];
8008 nsArray[2 * i] = nsList[i]->href;
8009 nsArray[2 * i + 1] = nsList[i]->prefix;
8010 }
8011 nsArray[count * 2] = NULL;
8012 xmlFree(nsList);
8013 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008014 /*
8015 * TODO: Differentiate between "selector" and "field".
8016 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008017 if (isField)
8018 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008019 NULL, XML_PATTERN_XSFIELD, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008020 else
8021 selector->xpathComp = (void *) xmlPatterncompile(selector->xpath,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00008022 NULL, XML_PATTERN_XSSEL, nsArray);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008023 if (nsArray != NULL)
8024 xmlFree((xmlChar **) nsArray);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008025
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +00008026 if (selector->xpathComp == NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008027 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +00008028 /* TODO: Adjust error code? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008029 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008030 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008031 "The XPath expression '%s' could not be "
8032 "compiled", selector->xpath);
8033 return (XML_SCHEMAP_S4S_ATTR_INVALID_VALUE);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008034 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008035 }
8036 return (0);
8037}
8038
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008039#define ADD_ANNOTATION(annot) \
8040 xmlSchemaAnnotPtr cur = item->annot; \
8041 if (item->annot == NULL) { \
8042 item->annot = annot; \
8043 return (annot); \
8044 } \
8045 cur = item->annot; \
8046 if (cur->next != NULL) { \
8047 cur = cur->next; \
8048 } \
8049 cur->next = annot;
8050
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008051/**
8052 * xmlSchemaAssignAnnotation:
8053 * @item: the schema component
8054 * @annot: the annotation
8055 *
8056 * Adds the annotation to the given schema component.
8057 *
8058 * Returns the given annotaion.
8059 */
8060static xmlSchemaAnnotPtr
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008061xmlSchemaAddAnnotation(xmlSchemaAnnotItemPtr annItem,
8062 xmlSchemaAnnotPtr annot)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008063{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008064 if ((annItem == NULL) || (annot == NULL))
8065 return (NULL);
8066 switch (annItem->type) {
8067 case XML_SCHEMA_TYPE_ELEMENT: {
8068 xmlSchemaElementPtr item = (xmlSchemaElementPtr) annItem;
8069 ADD_ANNOTATION(annot)
8070 }
8071 break;
8072 case XML_SCHEMA_TYPE_ATTRIBUTE: {
8073 xmlSchemaAttributePtr item = (xmlSchemaAttributePtr) annItem;
8074 ADD_ANNOTATION(annot)
8075 }
8076 break;
8077 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
8078 case XML_SCHEMA_TYPE_ANY: {
8079 xmlSchemaWildcardPtr item = (xmlSchemaWildcardPtr) annItem;
8080 ADD_ANNOTATION(annot)
8081 }
8082 break;
8083 case XML_SCHEMA_TYPE_PARTICLE:
8084 case XML_SCHEMA_TYPE_IDC_KEY:
8085 case XML_SCHEMA_TYPE_IDC_KEYREF:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008086 case XML_SCHEMA_TYPE_IDC_UNIQUE: {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008087 xmlSchemaAnnotItemPtr item = (xmlSchemaAnnotItemPtr) annItem;
8088 ADD_ANNOTATION(annot)
8089 }
8090 break;
8091 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP: {
8092 xmlSchemaAttributeGroupPtr item =
8093 (xmlSchemaAttributeGroupPtr) annItem;
8094 ADD_ANNOTATION(annot)
8095 }
8096 break;
8097 case XML_SCHEMA_TYPE_NOTATION: {
8098 xmlSchemaNotationPtr item = (xmlSchemaNotationPtr) annItem;
8099 ADD_ANNOTATION(annot)
8100 }
8101 break;
8102 case XML_SCHEMA_FACET_MININCLUSIVE:
8103 case XML_SCHEMA_FACET_MINEXCLUSIVE:
8104 case XML_SCHEMA_FACET_MAXINCLUSIVE:
8105 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
8106 case XML_SCHEMA_FACET_TOTALDIGITS:
8107 case XML_SCHEMA_FACET_FRACTIONDIGITS:
8108 case XML_SCHEMA_FACET_PATTERN:
8109 case XML_SCHEMA_FACET_ENUMERATION:
8110 case XML_SCHEMA_FACET_WHITESPACE:
8111 case XML_SCHEMA_FACET_LENGTH:
8112 case XML_SCHEMA_FACET_MAXLENGTH:
8113 case XML_SCHEMA_FACET_MINLENGTH: {
8114 xmlSchemaFacetPtr item = (xmlSchemaFacetPtr) annItem;
8115 ADD_ANNOTATION(annot)
8116 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008117 break;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008118 case XML_SCHEMA_TYPE_SIMPLE:
8119 case XML_SCHEMA_TYPE_COMPLEX: {
8120 xmlSchemaTypePtr item = (xmlSchemaTypePtr) annItem;
8121 ADD_ANNOTATION(annot)
8122 }
8123 break;
8124 case XML_SCHEMA_TYPE_GROUP: {
8125 xmlSchemaModelGroupDefPtr item = (xmlSchemaModelGroupDefPtr) annItem;
8126 ADD_ANNOTATION(annot)
8127 }
8128 break;
8129 case XML_SCHEMA_TYPE_SEQUENCE:
8130 case XML_SCHEMA_TYPE_CHOICE:
8131 case XML_SCHEMA_TYPE_ALL: {
8132 xmlSchemaModelGroupPtr item = (xmlSchemaModelGroupPtr) annItem;
8133 ADD_ANNOTATION(annot)
8134 }
8135 break;
8136 default:
8137 xmlSchemaPCustomErr(NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008138 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008139 NULL, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008140 "Internal error: xmlSchemaAddAnnotation, "
8141 "The item is not a annotated schema component", NULL);
8142 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008143 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008144 return (annot);
8145}
8146
8147/**
8148 * xmlSchemaParseIDCSelectorAndField:
8149 * @ctxt: a schema validation context
8150 * @schema: the schema being built
8151 * @node: a subtree containing XML Schema informations
8152 *
8153 * Parses a XML Schema identity-contraint definition's
8154 * <selector> and <field> elements.
8155 *
8156 * Returns the parsed identity-constraint definition.
8157 */
8158static xmlSchemaIDCSelectPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008159xmlSchemaParseIDCSelectorAndField(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008160 xmlSchemaPtr schema,
8161 xmlSchemaIDCPtr idc,
8162 xmlNodePtr node,
8163 int isField)
8164{
8165 xmlSchemaIDCSelectPtr item;
8166 xmlNodePtr child = NULL;
8167 xmlAttrPtr attr;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008168
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008169 /*
8170 * Check for illegal attributes.
8171 */
8172 attr = node->properties;
8173 while (attr != NULL) {
8174 if (attr->ns == NULL) {
8175 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8176 (!xmlStrEqual(attr->name, BAD_CAST "xpath"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008177 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008178 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008179 }
8180 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008181 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008182 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008183 }
8184 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008185 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008186 /*
8187 * Create the item.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008188 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008189 item = (xmlSchemaIDCSelectPtr) xmlMalloc(sizeof(xmlSchemaIDCSelect));
8190 if (item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008191 xmlSchemaPErrMemory(ctxt,
8192 "allocating a 'selector' of an identity-constraint definition",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008193 NULL);
8194 return (NULL);
8195 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008196 memset(item, 0, sizeof(xmlSchemaIDCSelect));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008197 /*
8198 * Attribute "xpath" (mandatory).
8199 */
8200 attr = xmlSchemaGetPropNode(node, "xpath");
8201 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008202 xmlSchemaPMissingAttrErr(ctxt,
8203 XML_SCHEMAP_S4S_ATTR_MISSING,
8204 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008205 "name", NULL);
8206 } else {
8207 item->xpath = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8208 /*
8209 * URGENT TODO: "field"s have an other syntax than "selector"s.
8210 */
8211
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +00008212 if (xmlSchemaCheckCSelectorXPath(ctxt, idc, item, attr,
8213 isField) == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008214 xmlSchemaPErr(ctxt,
8215 (xmlNodePtr) attr,
8216 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008217 "Internal error: xmlSchemaParseIDCSelectorAndField, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008218 "validating the XPath expression of a IDC selector.\n",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008219 NULL, NULL);
8220 }
8221
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008222 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008223 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008224 /*
8225 * And now for the children...
8226 */
8227 child = node->children;
8228 if (IS_SCHEMA(child, "annotation")) {
8229 /*
8230 * Add the annotation to the parent IDC.
8231 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008232 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008233 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008234 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008235 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008236 if (child != NULL) {
8237 xmlSchemaPContentErr(ctxt,
8238 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008239 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008240 NULL, "(annotation?)");
8241 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008242
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008243 return (item);
8244}
8245
8246/**
8247 * xmlSchemaParseIDC:
8248 * @ctxt: a schema validation context
8249 * @schema: the schema being built
8250 * @node: a subtree containing XML Schema informations
8251 *
8252 * Parses a XML Schema identity-contraint definition.
8253 *
8254 * Returns the parsed identity-constraint definition.
8255 */
8256static xmlSchemaIDCPtr
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008257xmlSchemaParseIDC(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008258 xmlSchemaPtr schema,
8259 xmlNodePtr node,
8260 xmlSchemaTypeType idcCategory,
8261 const xmlChar *targetNamespace)
8262{
8263 xmlSchemaIDCPtr item = NULL;
8264 xmlNodePtr child = NULL;
8265 xmlAttrPtr attr;
8266 const xmlChar *name = NULL;
8267 xmlSchemaIDCSelectPtr field = NULL, lastField = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008268
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008269 /*
8270 * Check for illegal attributes.
8271 */
8272 attr = node->properties;
8273 while (attr != NULL) {
8274 if (attr->ns == NULL) {
8275 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8276 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8277 ((idcCategory != XML_SCHEMA_TYPE_IDC_KEYREF) ||
8278 (!xmlStrEqual(attr->name, BAD_CAST "refer")))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008279 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008280 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008281 }
8282 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008283 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008284 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008285 }
8286 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008287 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008288 /*
8289 * Attribute "name" (mandatory).
8290 */
8291 attr = xmlSchemaGetPropNode(node, "name");
8292 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008293 xmlSchemaPMissingAttrErr(ctxt,
8294 XML_SCHEMAP_S4S_ATTR_MISSING,
8295 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008296 "name", NULL);
8297 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008298 } else if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008299 NULL, attr,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008300 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
8301 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008302 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008303 /* Create the component. */
8304 item = xmlSchemaAddIDC(ctxt, schema, name, targetNamespace,
8305 idcCategory, node);
8306 if (item == NULL)
8307 return(NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008308
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008309 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008310 if (idcCategory == XML_SCHEMA_TYPE_IDC_KEYREF) {
8311 /*
8312 * Attribute "refer" (mandatory).
8313 */
8314 attr = xmlSchemaGetPropNode(node, "refer");
8315 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008316 xmlSchemaPMissingAttrErr(ctxt,
8317 XML_SCHEMAP_S4S_ATTR_MISSING,
8318 NULL, node,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008319 "refer", NULL);
8320 } else {
8321 /*
8322 * Create a reference item.
8323 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008324 item->ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_IDC_KEY,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008325 NULL, NULL);
8326 if (item->ref == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008327 return (NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008328 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008329 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008330 &(item->ref->targetNamespace),
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008331 &(item->ref->name));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008332 xmlSchemaCheckReference(ctxt, schema, node, attr,
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00008333 item->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008334 }
8335 }
8336 /*
8337 * And now for the children...
8338 */
8339 child = node->children;
8340 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008341 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008342 child = child->next;
8343 }
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008344 if (child == NULL) {
8345 xmlSchemaPContentErr(ctxt,
8346 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008347 NULL, node, child,
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +00008348 "A child element is missing",
8349 "(annotation?, (selector, field+))");
8350 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008351 /*
8352 * Child element <selector>.
8353 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008354 if (IS_SCHEMA(child, "selector")) {
8355 item->selector = xmlSchemaParseIDCSelectorAndField(ctxt, schema,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008356 item, child, 0);
8357 child = child->next;
8358 /*
8359 * Child elements <field>.
8360 */
8361 if (IS_SCHEMA(child, "field")) {
8362 do {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008363 field = xmlSchemaParseIDCSelectorAndField(ctxt, schema,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008364 item, child, 1);
8365 if (field != NULL) {
8366 field->index = item->nbFields;
8367 item->nbFields++;
8368 if (lastField != NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008369 lastField->next = field;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008370 else
8371 item->fields = field;
8372 lastField = field;
8373 }
8374 child = child->next;
8375 } while (IS_SCHEMA(child, "field"));
8376 } else {
8377 xmlSchemaPContentErr(ctxt,
8378 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008379 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008380 NULL, "(annotation?, (selector, field+))");
8381 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008382 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008383 if (child != NULL) {
8384 xmlSchemaPContentErr(ctxt,
8385 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008386 NULL, node, child,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008387 NULL, "(annotation?, (selector, field+))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008388 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008389
8390 return (item);
8391}
8392
Daniel Veillardc0826a72004-08-10 14:17:33 +00008393/**
Daniel Veillard4255d502002-04-16 15:50:10 +00008394 * xmlSchemaParseElement:
8395 * @ctxt: a schema validation context
8396 * @schema: the schema being built
8397 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008398 * @topLevel: indicates if this is global declaration
Daniel Veillard4255d502002-04-16 15:50:10 +00008399 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008400 * Parses a XML schema element declaration.
Daniel Veillard4255d502002-04-16 15:50:10 +00008401 * *WARNING* this interface is highly subject to change
8402 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008403 * Returns the element declaration or a particle; NULL in case
8404 * of an error or if the particle has minOccurs==maxOccurs==0.
Daniel Veillard4255d502002-04-16 15:50:10 +00008405 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008406static xmlSchemaBasicItemPtr
Daniel Veillard4255d502002-04-16 15:50:10 +00008407xmlSchemaParseElement(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008408 xmlNodePtr node, int *isElemRef, int topLevel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008409{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008410 xmlSchemaElementPtr decl = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008411 xmlSchemaParticlePtr particle = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008412 xmlSchemaAnnotPtr annot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008413 xmlNodePtr child = NULL;
8414 xmlAttrPtr attr, nameAttr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008415 int min, max, isRef = 0;
8416 xmlChar *des = NULL;
William M. Bracke7091952004-05-11 15:09:58 +00008417
8418 /* 3.3.3 Constraints on XML Representations of Element Declarations */
8419 /* TODO: Complete implementation of 3.3.6 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008420
Daniel Veillard4255d502002-04-16 15:50:10 +00008421 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008422 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008423
8424 if (isElemRef != NULL)
8425 *isElemRef = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008426 /*
8427 * If we get a "ref" attribute on a local <element> we will assume it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008428 * a reference - even if there's a "name" attribute; this seems to be more
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008429 * robust.
8430 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008431 nameAttr = xmlSchemaGetPropNode(node, "name");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008432 attr = xmlSchemaGetPropNode(node, "ref");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008433 if ((topLevel) || (attr == NULL)) {
8434 if (nameAttr == NULL) {
8435 xmlSchemaPMissingAttrErr(ctxt,
8436 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008437 NULL, node, "name", NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008438 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008439 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008440 } else
Daniel Veillardc0826a72004-08-10 14:17:33 +00008441 isRef = 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008442
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008443 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008444 child = node->children;
8445 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008446 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008447 child = child->next;
8448 }
8449 /*
8450 * Skip particle part if a global declaration.
8451 */
8452 if (topLevel)
8453 goto declaration_part;
8454 /*
8455 * The particle part ==================================================
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008456 */
8457 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
8458 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1, "(xs:nonNegativeInteger | unbounded)");
8459 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008460 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
8461 if (particle == NULL)
8462 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008463
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008464 /* ret->flags |= XML_SCHEMAS_ELEM_REF; */
8465
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008466 if (isRef) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008467 const xmlChar *refNs = NULL, *ref = NULL;
8468 xmlSchemaQNameRefPtr refer = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008469 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008470 * The reference part =============================================
Daniel Veillardc0826a72004-08-10 14:17:33 +00008471 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008472 if (isElemRef != NULL)
8473 *isElemRef = 1;
8474
Daniel Veillardc0826a72004-08-10 14:17:33 +00008475 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008476 NULL, attr, &refNs, &ref);
8477 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008478 /*
8479 * SPEC (3.3.3 : 2.1) "One of ref or name must be present, but not both"
Daniel Veillardc0826a72004-08-10 14:17:33 +00008480 */
8481 if (nameAttr != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008482 xmlSchemaPMutualExclAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008483 XML_SCHEMAP_SRC_ELEMENT_2_1, NULL, nameAttr, "ref", "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008484 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008485 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008486 * Check for illegal attributes.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008487 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00008488 attr = node->properties;
8489 while (attr != NULL) {
8490 if (attr->ns == NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008491 if (xmlStrEqual(attr->name, BAD_CAST "ref") ||
8492 xmlStrEqual(attr->name, BAD_CAST "name") ||
8493 xmlStrEqual(attr->name, BAD_CAST "id") ||
8494 xmlStrEqual(attr->name, BAD_CAST "maxOccurs") ||
8495 xmlStrEqual(attr->name, BAD_CAST "minOccurs"))
8496 {
8497 attr = attr->next;
8498 continue;
8499 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008500 /* SPEC (3.3.3 : 2.2) */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008501 xmlSchemaPCustomAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008502 XML_SCHEMAP_SRC_ELEMENT_2_2,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008503 NULL, NULL, attr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008504 "Only the attributes 'minOccurs', 'maxOccurs' and "
8505 "'id' are allowed in addition to 'ref'");
8506 break;
8507 }
8508 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
8509 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008510 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008511 }
8512 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008513 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008514 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008515 * No children except <annotation> expected.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008516 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008517 if (child != NULL) {
8518 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008519 NULL, node, child, NULL, "(annotation?)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008520 }
8521 if ((min == 0) && (max == 0))
8522 goto return_null;
8523 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008524 * Create the reference item and attach it to the particle.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008525 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008526 refer = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_ELEMENT,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008527 ref, refNs);
8528 if (refer == NULL)
8529 goto return_null;
8530 particle->children = (xmlSchemaTreeItemPtr) refer;
8531 particle->annot = annot;
8532 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008533 * Add the particle to pending components, since the reference
8534 * need to be resolved.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008535 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008536 WXS_ADD_PENDING(ctxt, particle);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008537 return ((xmlSchemaBasicItemPtr) particle);
8538 }
8539 /*
8540 * The declaration part ===============================================
8541 */
8542declaration_part:
8543 {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008544 const xmlChar *ns = NULL, *fixed, *name, *attrValue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008545 xmlSchemaIDCPtr curIDC = NULL, lastIDC = NULL;
8546
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008547 if (xmlSchemaPValAttrNode(ctxt, NULL, nameAttr,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008548 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008549 goto return_null;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008550 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008551 * Evaluate the target namespace.
8552 */
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008553 if (topLevel) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008554 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008555 } else {
8556 attr = xmlSchemaGetPropNode(node, "form");
8557 if (attr != NULL) {
8558 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8559 if (xmlStrEqual(attrValue, BAD_CAST "qualified")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008560 ns = ctxt->targetNamespace;
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008561 } else if (!xmlStrEqual(attrValue, BAD_CAST "unqualified")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008562 xmlSchemaPSimpleTypeErr(ctxt,
8563 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
8564 NULL, (xmlNodePtr) attr,
8565 NULL, "(qualified | unqualified)",
8566 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcikb4a9e642004-08-30 10:56:53 +00008567 }
8568 } else if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008569 ns = ctxt->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008570 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008571 decl = xmlSchemaAddElement(ctxt, name, ns, node, topLevel);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008572 if (decl == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008573 goto return_null;
William M. Bracke7091952004-05-11 15:09:58 +00008574 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008575 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008576 * Check for illegal attributes.
8577 */
William M. Bracke7091952004-05-11 15:09:58 +00008578 attr = node->properties;
8579 while (attr != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008580 if (attr->ns == NULL) {
8581 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
8582 (!xmlStrEqual(attr->name, BAD_CAST "type")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008583 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008584 (!xmlStrEqual(attr->name, BAD_CAST "default")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008585 (!xmlStrEqual(attr->name, BAD_CAST "fixed")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008586 (!xmlStrEqual(attr->name, BAD_CAST "block")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008587 (!xmlStrEqual(attr->name, BAD_CAST "nillable")))
8588 {
8589 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008590 if ((!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008591 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008592 (!xmlStrEqual(attr->name, BAD_CAST "form")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00008593 {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008594 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008595 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008596 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008597 } else if ((!xmlStrEqual(attr->name, BAD_CAST "final")) &&
8598 (!xmlStrEqual(attr->name, BAD_CAST "abstract")) &&
Daniel Veillardc0826a72004-08-10 14:17:33 +00008599 (!xmlStrEqual(attr->name, BAD_CAST "substitutionGroup"))) {
8600
8601 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008602 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008603 }
8604 }
8605 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008606
Daniel Veillardc0826a72004-08-10 14:17:33 +00008607 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008608 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Bracke7091952004-05-11 15:09:58 +00008609 }
8610 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008611 }
William M. Bracke7091952004-05-11 15:09:58 +00008612 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008613 * Extract/validate attributes.
8614 */
8615 if (topLevel) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008616 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00008617 * Process top attributes of global element declarations here.
8618 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008619 decl->flags |= XML_SCHEMAS_ELEM_GLOBAL;
8620 decl->flags |= XML_SCHEMAS_ELEM_TOPLEVEL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008621 xmlSchemaPValAttrQName(ctxt, schema,
8622 NULL, node, "substitutionGroup",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008623 &(decl->substGroupNs), &(decl->substGroup));
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008624 if (xmlGetBooleanProp(ctxt, node, "abstract", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008625 decl->flags |= XML_SCHEMAS_ELEM_ABSTRACT;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008626 /*
8627 * Attribute "final".
8628 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008629 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008630 if (attr == NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008631 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
8632 decl->flags |= XML_SCHEMAS_ELEM_FINAL_EXTENSION;
8633 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
8634 decl->flags |= XML_SCHEMAS_ELEM_FINAL_RESTRICTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008635 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008636 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8637 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008638 -1,
8639 XML_SCHEMAS_ELEM_FINAL_EXTENSION,
8640 XML_SCHEMAS_ELEM_FINAL_RESTRICTION, -1, -1, -1) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008641 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008642 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008643 NULL, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008644 NULL, "(#all | List of (extension | restriction))",
Daniel Veillardc0826a72004-08-10 14:17:33 +00008645 attrValue, NULL, NULL, NULL);
8646 }
8647 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008648 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008649 /*
8650 * Attribute "block".
8651 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008652 attr = xmlSchemaGetPropNode(node, "block");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008653 if (attr == NULL) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +00008654 /*
8655 * Apply default "block" values.
8656 */
8657 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
8658 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_RESTRICTION;
8659 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
8660 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_EXTENSION;
8661 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
8662 decl->flags |= XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008663 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008664 attrValue = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
8665 if (xmlSchemaPValAttrBlockFinal(attrValue, &(decl->flags),
Daniel Veillardc0826a72004-08-10 14:17:33 +00008666 -1,
8667 XML_SCHEMAS_ELEM_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008668 XML_SCHEMAS_ELEM_BLOCK_RESTRICTION,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008669 XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION, -1, -1) != 0) {
8670 xmlSchemaPSimpleTypeErr(ctxt,
8671 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008672 NULL, (xmlNodePtr) attr,
William M. Brack2f2a6632004-08-20 23:09:47 +00008673 NULL, "(#all | List of (extension | "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008674 "restriction | substitution))", attrValue,
8675 NULL, NULL, NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00008676 }
8677 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008678 if (xmlGetBooleanProp(ctxt, node, "nillable", 0))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008679 decl->flags |= XML_SCHEMAS_ELEM_NILLABLE;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008680
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008681 attr = xmlSchemaGetPropNode(node, "type");
8682 if (attr != NULL) {
8683 xmlSchemaPValAttrNodeQName(ctxt, schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008684 NULL, attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008685 &(decl->namedTypeNs), &(decl->namedType));
8686 xmlSchemaCheckReference(ctxt, schema, node,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008687 attr, decl->namedTypeNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008688 }
8689 decl->value = xmlSchemaGetProp(ctxt, node, "default");
8690 attr = xmlSchemaGetPropNode(node, "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008691 if (attr != NULL) {
8692 fixed = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008693 if (decl->value != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008694 /*
8695 * 3.3.3 : 1
8696 * default and fixed must not both be present.
Daniel Veillardc0826a72004-08-10 14:17:33 +00008697 */
8698 xmlSchemaPMutualExclAttrErr(ctxt,
8699 XML_SCHEMAP_SRC_ELEMENT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008700 NULL, attr, "default", "fixed");
Daniel Veillardc0826a72004-08-10 14:17:33 +00008701 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008702 decl->flags |= XML_SCHEMAS_ELEM_FIXED;
8703 decl->value = fixed;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008704 }
William M. Bracke7091952004-05-11 15:09:58 +00008705 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008706 /*
8707 * And now for the children...
8708 */
William M. Bracke7091952004-05-11 15:09:58 +00008709 if (IS_SCHEMA(child, "complexType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008710 /*
8711 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008712 * "type" and either <simpleType> or <complexType> are mutually
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008713 * exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008714 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008715 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008716 xmlSchemaPContentErr(ctxt,
8717 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008718 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008719 "The attribute 'type' and the <complexType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008720 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008721 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008722 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseComplexType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008723 child = child->next;
8724 } else if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008725 /*
8726 * 3.3.3 : 3
Daniel Veillardc0826a72004-08-10 14:17:33 +00008727 * "type" and either <simpleType> or <complexType> are
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008728 * mutually exclusive
Daniel Veillardc0826a72004-08-10 14:17:33 +00008729 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008730 if (decl->namedType != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008731 xmlSchemaPContentErr(ctxt,
8732 XML_SCHEMAP_SRC_ELEMENT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008733 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008734 "The attribute 'type' and the <simpleType> child are "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008735 "mutually exclusive", NULL);
William M. Bracke7091952004-05-11 15:09:58 +00008736 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008737 WXS_ELEM_TYPEDEF(decl) = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Bracke7091952004-05-11 15:09:58 +00008738 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008739 }
William M. Bracke7091952004-05-11 15:09:58 +00008740 while ((IS_SCHEMA(child, "unique")) ||
Daniel Veillardc0826a72004-08-10 14:17:33 +00008741 (IS_SCHEMA(child, "key")) || (IS_SCHEMA(child, "keyref"))) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008742 if (IS_SCHEMA(child, "unique")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008743 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008744 XML_SCHEMA_TYPE_IDC_UNIQUE, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008745 } else if (IS_SCHEMA(child, "key")) {
8746 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008747 XML_SCHEMA_TYPE_IDC_KEY, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008748 } else if (IS_SCHEMA(child, "keyref")) {
8749 curIDC = xmlSchemaParseIDC(ctxt, schema, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008750 XML_SCHEMA_TYPE_IDC_KEYREF, decl->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008751 }
8752 if (lastIDC != NULL)
8753 lastIDC->next = curIDC;
8754 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008755 decl->idcs = (void *) curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008756 lastIDC = curIDC;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +00008757 child = child->next;
William M. Bracke7091952004-05-11 15:09:58 +00008758 }
8759 if (child != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00008760 xmlSchemaPContentErr(ctxt,
8761 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008762 NULL, node, child,
Daniel Veillardc0826a72004-08-10 14:17:33 +00008763 NULL, "(annotation?, ((simpleType | complexType)?, "
8764 "(unique | key | keyref)*))");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008765 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008766 decl->annot = annot;
Daniel Veillardc0826a72004-08-10 14:17:33 +00008767 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00008768 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008769 * NOTE: Element Declaration Representation OK 4. will be checked at a
Daniel Veillardc0826a72004-08-10 14:17:33 +00008770 * different layer.
8771 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008772 FREE_AND_NULL(des)
8773 if (topLevel)
8774 return ((xmlSchemaBasicItemPtr) decl);
8775 else {
8776 particle->children = (xmlSchemaTreeItemPtr) decl;
8777 return ((xmlSchemaBasicItemPtr) particle);
8778 }
8779
8780return_null:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008781 FREE_AND_NULL(des);
8782 if (annot != NULL) {
8783 if (particle != NULL)
8784 particle->annot = NULL;
8785 if (decl != NULL)
8786 decl->annot = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008787 xmlSchemaFreeAnnot(annot);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008788 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008789 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00008790}
8791
8792/**
8793 * xmlSchemaParseUnion:
8794 * @ctxt: a schema validation context
8795 * @schema: the schema being built
8796 * @node: a subtree containing XML Schema informations
8797 *
8798 * parse a XML schema Union definition
8799 * *WARNING* this interface is highly subject to change
8800 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008801 * Returns -1 in case of internal error, 0 in case of success and a positive
8802 * error code otherwise.
Daniel Veillard4255d502002-04-16 15:50:10 +00008803 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008804static int
Daniel Veillard4255d502002-04-16 15:50:10 +00008805xmlSchemaParseUnion(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008806 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008807{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008808 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008809 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008810 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008811 const xmlChar *cur = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00008812
8813 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008814 return (-1);
8815 /* Not a component, don't create it. */
8816 type = ctxt->ctxtType;
8817 /*
8818 * Mark the simple type as being of variety "union".
8819 */
8820 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008821 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008822 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
8823 * then the ·simple ur-type definition·."
8824 */
8825 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008826 /*
8827 * Check for illegal attributes.
8828 */
8829 attr = node->properties;
8830 while (attr != NULL) {
8831 if (attr->ns == NULL) {
8832 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
8833 (!xmlStrEqual(attr->name, BAD_CAST "memberTypes"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008834 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008835 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008836 }
8837 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008838 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008839 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00008840 }
8841 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008842 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008843 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +00008844 /*
8845 * Attribute "memberTypes". This is a list of QNames.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008846 * TODO: Check the value to contain anything.
William M. Brack2f2a6632004-08-20 23:09:47 +00008847 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008848 attr = xmlSchemaGetPropNode(node, "memberTypes");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008849 if (attr != NULL) {
8850 const xmlChar *end;
8851 xmlChar *tmp;
8852 const xmlChar *localName, *nsName;
8853 xmlSchemaTypeLinkPtr link, lastLink = NULL;
8854 xmlSchemaQNameRefPtr ref;
8855
8856 cur = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008857 type->base = cur;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008858 do {
8859 while (IS_BLANK_CH(*cur))
8860 cur++;
8861 end = cur;
8862 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
8863 end++;
8864 if (end == cur)
8865 break;
8866 tmp = xmlStrndup(cur, end - cur);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008867 if (xmlSchemaPValAttrNodeQNameValue(ctxt, schema,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008868 NULL, attr, BAD_CAST tmp, &nsName, &localName) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008869 /*
8870 * Create the member type link.
8871 */
8872 link = (xmlSchemaTypeLinkPtr)
8873 xmlMalloc(sizeof(xmlSchemaTypeLink));
8874 if (link == NULL) {
8875 xmlSchemaPErrMemory(ctxt, "xmlSchemaParseUnion, "
8876 "allocating a type link", NULL);
8877 return (-1);
8878 }
8879 link->type = NULL;
8880 link->next = NULL;
8881 if (lastLink == NULL)
8882 type->memberTypes = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008883 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008884 lastLink->next = link;
8885 lastLink = link;
8886 /*
8887 * Create a reference item.
8888 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00008889 ref = xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_SIMPLE,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008890 localName, nsName);
8891 if (ref == NULL) {
8892 FREE_AND_NULL(tmp)
8893 return (-1);
8894 }
8895 /*
8896 * Assign the reference to the link, it will be resolved
8897 * later during fixup of the union simple type.
8898 */
8899 link->type = (xmlSchemaTypePtr) ref;
8900 }
8901 FREE_AND_NULL(tmp)
8902 cur = end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008903 } while (*cur != 0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008904
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008905 }
William M. Brack2f2a6632004-08-20 23:09:47 +00008906 /*
8907 * And now for the children...
8908 */
Daniel Veillard4255d502002-04-16 15:50:10 +00008909 child = node->children;
8910 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008911 /*
8912 * Add the annotation to the simple type ancestor.
8913 */
8914 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008915 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008916 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00008917 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008918 if (IS_SCHEMA(child, "simpleType")) {
8919 xmlSchemaTypePtr subtype, last = NULL;
8920
8921 /*
8922 * Anchor the member types in the "subtypes" field of the
8923 * simple type.
8924 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008925 while (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008926 subtype = (xmlSchemaTypePtr)
8927 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
8928 if (subtype != NULL) {
8929 if (last == NULL) {
8930 type->subtypes = subtype;
8931 last = subtype;
8932 } else {
8933 last->next = subtype;
8934 last = subtype;
8935 }
8936 last->next = NULL;
8937 }
8938 child = child->next;
8939 }
Daniel Veillard4255d502002-04-16 15:50:10 +00008940 }
8941 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00008942 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008943 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008944 NULL, node, child, NULL, "(annotation?, simpleType*)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008945 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008946 if ((attr == NULL) && (type->subtypes == NULL)) {
8947 /*
8948 * src-union-memberTypes-or-simpleTypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008949 * Either the memberTypes [attribute] of the <union> element must
8950 * be non-empty or there must be at least one simpleType [child].
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008951 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008952 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008953 XML_SCHEMAP_SRC_UNION_MEMBERTYPES_OR_SIMPLETYPES,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00008954 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008955 "Either the attribute 'memberTypes' or "
8956 "at least one <simpleType> child must be present", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008957 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008958 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +00008959}
8960
8961/**
8962 * xmlSchemaParseList:
8963 * @ctxt: a schema validation context
8964 * @schema: the schema being built
8965 * @node: a subtree containing XML Schema informations
8966 *
8967 * parse a XML schema List definition
8968 * *WARNING* this interface is highly subject to change
8969 *
William M. Bracke7091952004-05-11 15:09:58 +00008970 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +00008971 * 1 in case of success.
8972 */
8973static xmlSchemaTypePtr
8974xmlSchemaParseList(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillardd0c9c322003-10-10 00:49:42 +00008975 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00008976{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008977 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +00008978 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +00008979 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +00008980
8981 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
8982 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00008983 /* Not a component, don't create it. */
8984 type = ctxt->ctxtType;
8985 /*
8986 * Mark the type as being of variety "list".
8987 */
8988 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00008989 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +00008990 * SPEC (Base type) (2) "If the <list> or <union> alternative is chosen,
8991 * then the ·simple ur-type definition·."
8992 */
8993 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
William M. Brack2f2a6632004-08-20 23:09:47 +00008994 /*
8995 * Check for illegal attributes.
8996 */
8997 attr = node->properties;
8998 while (attr != NULL) {
8999 if (attr->ns == NULL) {
9000 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9001 (!xmlStrEqual(attr->name, BAD_CAST "itemType"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009002 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009003 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009004 }
9005 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009006 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009007 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +00009008 }
9009 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009010 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009011
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009012 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009013
William M. Brack2f2a6632004-08-20 23:09:47 +00009014 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009015 * Attribute "itemType". NOTE that we will use the "ref" and "refNs"
9016 * fields for holding the reference to the itemType.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009017 *
9018 * REVAMP TODO: Use the "base" and "baseNs" fields, since we will remove
9019 * the "ref" fields.
William M. Brack2f2a6632004-08-20 23:09:47 +00009020 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009021 xmlSchemaPValAttrQName(ctxt, schema, NULL,
9022 node, "itemType", &(type->baseNs), &(type->base));
William M. Brack2f2a6632004-08-20 23:09:47 +00009023 /*
9024 * And now for the children...
9025 */
Daniel Veillard4255d502002-04-16 15:50:10 +00009026 child = node->children;
9027 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009028 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009029 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009030 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009031 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009032 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009033 /*
9034 * src-list-itemType-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009035 * Either the itemType [attribute] or the <simpleType> [child] of
9036 * the <list> element must be present, but not both.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009037 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009038 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009039 xmlSchemaPCustomErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009040 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009041 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +00009042 "The attribute 'itemType' and the <simpleType> child "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009043 "are mutually exclusive", NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009044 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009045 type->subtypes = xmlSchemaParseSimpleType(ctxt, schema, child, 0);
William M. Brack2f2a6632004-08-20 23:09:47 +00009046 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009047 child = child->next;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009048 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009049 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009050 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009051 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009052 "Either the attribute 'itemType' or the <simpleType> child "
9053 "must be present", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009054 }
9055 if (child != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009056 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009057 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009058 NULL, node, child, NULL, "(annotation?, simpleType?)");
Daniel Veillard4255d502002-04-16 15:50:10 +00009059 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009060 if ((type->base == NULL) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009061 (type->subtypes == NULL) &&
9062 (xmlSchemaGetPropNode(node, "itemType") == NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009063 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009064 XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009065 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009066 "Either the attribute 'itemType' or the <simpleType> child "
9067 "must be present", NULL);
9068 }
9069 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00009070}
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009071
Daniel Veillard4255d502002-04-16 15:50:10 +00009072/**
9073 * xmlSchemaParseSimpleType:
9074 * @ctxt: a schema validation context
9075 * @schema: the schema being built
9076 * @node: a subtree containing XML Schema informations
9077 *
9078 * parse a XML schema Simple Type definition
9079 * *WARNING* this interface is highly subject to change
9080 *
William M. Bracke7091952004-05-11 15:09:58 +00009081 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillardc0826a72004-08-10 14:17:33 +00009082 * 1 in case of success.
Daniel Veillard4255d502002-04-16 15:50:10 +00009083 */
9084static xmlSchemaTypePtr
9085xmlSchemaParseSimpleType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +00009086 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +00009087{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009088 xmlSchemaTypePtr type, oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009089 xmlNodePtr child = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009090 const xmlChar *attrValue = NULL;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009091 xmlAttrPtr attr;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009092 int hasRestriction = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00009093
9094 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
9095 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009096
Daniel Veillardc0826a72004-08-10 14:17:33 +00009097 if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009098 attr = xmlSchemaGetPropNode(node, "name");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009099 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009100 xmlSchemaPMissingAttrErr(ctxt,
9101 XML_SCHEMAP_S4S_ATTR_MISSING,
9102 NULL, node,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009103 "name", NULL);
9104 return (NULL);
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009105 } else {
9106 if (xmlSchemaPValAttrNode(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009107 NULL, attr,
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009108 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &attrValue) != 0)
9109 return (NULL);
9110 /*
9111 * Skip built-in types.
9112 */
9113 if (ctxt->isS4S) {
9114 xmlSchemaTypePtr biType;
9115
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009116 if (ctxt->isRedefine) {
9117 /*
9118 * REDEFINE: Disallow redefinition of built-in-types.
9119 * TODO: It seems that the spec does not say anything
9120 * about this case.
9121 */
9122 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009123 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009124 "Redefinition of built-in simple types is not "
9125 "supported", NULL);
9126 return(NULL);
9127 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +00009128 biType = xmlSchemaGetPredefinedType(attrValue, xmlSchemaNs);
9129 if (biType != NULL)
9130 return (biType);
9131 }
Daniel Veillard01fa6152004-06-29 17:04:39 +00009132 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009133 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009134 /*
9135 * TargetNamespace:
9136 * SPEC "The ·actual value· of the targetNamespace [attribute]
9137 * of the <schema> ancestor element information item if present,
9138 * otherwise ·absent·.
9139 */
Daniel Veillardc0826a72004-08-10 14:17:33 +00009140 if (topLevel == 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009141#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009142 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009143#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +00009144 /*
Daniel Veillardc0826a72004-08-10 14:17:33 +00009145 * Parse as local simple type definition.
Daniel Veillard01fa6152004-06-29 17:04:39 +00009146 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009147#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +00009148 snprintf(buf, 39, "#ST%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009149 type = xmlSchemaAddType(ctxt, schema,
9150 XML_SCHEMA_TYPE_SIMPLE,
9151 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009152 ctxt->targetNamespace, node, 0);
9153#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009154 type = xmlSchemaAddType(ctxt, schema,
9155 XML_SCHEMA_TYPE_SIMPLE,
9156 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009157#endif
Daniel Veillardc0826a72004-08-10 14:17:33 +00009158 if (type == NULL)
9159 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009160 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009161 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009162 /*
9163 * Check for illegal attributes.
9164 */
9165 attr = node->properties;
9166 while (attr != NULL) {
9167 if (attr->ns == NULL) {
9168 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009169 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009170 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillard01fa6152004-06-29 17:04:39 +00009171 }
Daniel Veillardc0826a72004-08-10 14:17:33 +00009172 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009173 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009174 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009175 }
9176 attr = attr->next;
Daniel Veillard01fa6152004-06-29 17:04:39 +00009177 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009178 } else {
Daniel Veillardc0826a72004-08-10 14:17:33 +00009179 /*
9180 * Parse as global simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009181 *
9182 * Note that attrValue is the value of the attribute "name" here.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009183 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009184 type = xmlSchemaAddType(ctxt, schema, XML_SCHEMA_TYPE_SIMPLE,
9185 attrValue, ctxt->targetNamespace, node, 1);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009186 if (type == NULL)
9187 return (NULL);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009188 type->type = XML_SCHEMA_TYPE_SIMPLE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009189 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009190 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
9191 /*
9192 * Check for illegal attributes.
9193 */
9194 attr = node->properties;
9195 while (attr != NULL) {
9196 if (attr->ns == NULL) {
9197 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9198 (!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009199 (!xmlStrEqual(attr->name, BAD_CAST "final"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009200 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009201 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009202 }
9203 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009204 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009205 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Daniel Veillardc0826a72004-08-10 14:17:33 +00009206 }
9207 attr = attr->next;
9208 }
9209 /*
9210 * Attribute "final".
9211 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009212 attr = xmlSchemaGetPropNode(node, "final");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009213 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009214 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9215 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
9216 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9217 type->flags |= XML_SCHEMAS_TYPE_FINAL_LIST;
9218 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9219 type->flags |= XML_SCHEMAS_TYPE_FINAL_UNION;
Daniel Veillardc0826a72004-08-10 14:17:33 +00009220 } else {
9221 attrValue = xmlSchemaGetProp(ctxt, node, "final");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009222 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
9223 -1, -1, XML_SCHEMAS_TYPE_FINAL_RESTRICTION, -1,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009224 XML_SCHEMAS_TYPE_FINAL_LIST,
9225 XML_SCHEMAS_TYPE_FINAL_UNION) != 0) {
9226
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009227 xmlSchemaPSimpleTypeErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009228 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009229 WXS_BASIC_CAST type, (xmlNodePtr) attr,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009230 NULL, "(#all | List of (list | union | restriction)",
Daniel Veillardc0826a72004-08-10 14:17:33 +00009231 attrValue, NULL, NULL, NULL);
9232 }
9233 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009234 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009235 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009236 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillardc0826a72004-08-10 14:17:33 +00009237 /*
9238 * And now for the children...
9239 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009240 oldCtxtType = ctxt->ctxtType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009241
Daniel Veillardc0826a72004-08-10 14:17:33 +00009242 ctxt->ctxtType = type;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009243
Daniel Veillard4255d502002-04-16 15:50:10 +00009244 child = node->children;
9245 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009246 type->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009247 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009248 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009249 if (child == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009250 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009251 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009252 "(annotation?, (restriction | list | union))");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009253 } else if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009254 xmlSchemaParseRestriction(ctxt, schema, child,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009255 XML_SCHEMA_TYPE_SIMPLE);
9256 hasRestriction = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009257 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009258 } else if (IS_SCHEMA(child, "list")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009259 xmlSchemaParseList(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009260 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009261 } else if (IS_SCHEMA(child, "union")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009262 xmlSchemaParseUnion(ctxt, schema, child);
Daniel Veillardd0c9c322003-10-10 00:49:42 +00009263 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +00009264 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009265 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009266 xmlSchemaPContentErr(ctxt, XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009267 NULL, node, child, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +00009268 "(annotation?, (restriction | list | union))");
Daniel Veillard4255d502002-04-16 15:50:10 +00009269 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009270 /*
9271 * REDEFINE: SPEC src-redefine (5)
9272 * "Within the [children], each <simpleType> must have a
9273 * <restriction> among its [children] ... the ·actual value· of whose
9274 * base [attribute] must be the same as the ·actual value· of its own
9275 * name attribute plus target namespace;"
9276 */
9277 if (topLevel && ctxt->isRedefine && (! hasRestriction)) {
9278 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009279 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009280 "<simpleType> must have a <restriction> child", NULL);
9281 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009282
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00009283 ctxt->ctxtType = oldCtxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +00009284 return (type);
9285}
9286
Daniel Veillard4255d502002-04-16 15:50:10 +00009287/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009288 * xmlSchemaParseModelGroupDefRef:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009289 * @ctxt: the parser context
9290 * @schema: the schema being built
9291 * @node: the node
Daniel Veillard4255d502002-04-16 15:50:10 +00009292 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009293 * Parses a reference to a model group definition.
Daniel Veillard4255d502002-04-16 15:50:10 +00009294 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009295 * We will return a particle component with a qname-component or
9296 * NULL in case of an error.
Daniel Veillard4255d502002-04-16 15:50:10 +00009297 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009298static xmlSchemaTreeItemPtr
9299xmlSchemaParseModelGroupDefRef(xmlSchemaParserCtxtPtr ctxt,
9300 xmlSchemaPtr schema,
9301 xmlNodePtr node)
Daniel Veillard4255d502002-04-16 15:50:10 +00009302{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009303 xmlSchemaParticlePtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009304 xmlNodePtr child = NULL;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009305 xmlAttrPtr attr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009306 const xmlChar *ref = NULL, *refNs = NULL;
9307 int min, max;
9308
9309 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009310 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009311
9312 attr = xmlSchemaGetPropNode(node, "ref");
9313 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009314 xmlSchemaPMissingAttrErr(ctxt,
9315 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009316 NULL, node, "ref", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009317 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009318 } else if (xmlSchemaPValAttrNodeQName(ctxt, schema, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009319 attr, &refNs, &ref) != 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009320 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009321 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009322 xmlSchemaCheckReference(ctxt, schema, node, attr, refNs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009323 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009324 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009325 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009326 /*
9327 * Check for illegal attributes.
9328 */
9329 attr = node->properties;
9330 while (attr != NULL) {
9331 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009332 if ((!xmlStrEqual(attr->name, BAD_CAST "ref")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009333 (!xmlStrEqual(attr->name, BAD_CAST "id")) &&
9334 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs")) &&
9335 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009336 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009337 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009338 }
9339 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009340 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009341 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009342 }
9343 attr = attr->next;
9344 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009345 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009346 item = xmlSchemaAddParticle(ctxt, schema, node, min, max);
9347 if (item == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009348 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009349 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009350 * Create a qname-reference and set as the term; it will be substituted
9351 * for the model group after the reference has been resolved.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009352 */
9353 item->children = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009354 xmlSchemaNewQNameRef(ctxt, XML_SCHEMA_TYPE_GROUP, ref, refNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009355 xmlSchemaPCheckParticleCorrect_2(ctxt, item, node, min, max);
9356 /*
9357 * And now for the children...
9358 */
9359 child = node->children;
9360 /* TODO: Is annotation even allowed for a model group reference? */
9361 if (IS_SCHEMA(child, "annotation")) {
9362 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009363 * TODO: What to do exactly with the annotation?
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009364 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009365 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009366 child = child->next;
9367 }
9368 if (child != NULL) {
9369 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009370 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009371 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009372 "(annotation?)");
9373 }
9374 /*
9375 * Corresponds to no component at all if minOccurs==maxOccurs==0.
9376 */
9377 if ((min == 0) && (max == 0))
9378 return (NULL);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009379
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009380 return ((xmlSchemaTreeItemPtr) item);
9381}
9382
9383/**
9384 * xmlSchemaParseModelGroupDefinition:
9385 * @ctxt: a schema validation context
9386 * @schema: the schema being built
9387 * @node: a subtree containing XML Schema informations
9388 *
9389 * Parses a XML schema model group definition.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009390 *
9391 * Note that the contraint src-redefine (6.2) can't be applied until
9392 * references have been resolved. So we will do this at the
9393 * component fixup level.
9394 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009395 * *WARNING* this interface is highly subject to change
9396 *
9397 * Returns -1 in case of error, 0 if the declaration is improper and
9398 * 1 in case of success.
9399 */
9400static xmlSchemaModelGroupDefPtr
9401xmlSchemaParseModelGroupDefinition(xmlSchemaParserCtxtPtr ctxt,
9402 xmlSchemaPtr schema,
9403 xmlNodePtr node)
9404{
9405 xmlSchemaModelGroupDefPtr item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009406 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009407 xmlAttrPtr attr;
9408 const xmlChar *name;
Daniel Veillard4255d502002-04-16 15:50:10 +00009409
9410 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009411 return (NULL);
9412
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009413 attr = xmlSchemaGetPropNode(node, "name");
9414 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009415 xmlSchemaPMissingAttrErr(ctxt,
9416 XML_SCHEMAP_S4S_ATTR_MISSING,
9417 NULL, node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009418 "name", NULL);
9419 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009420 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009421 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
9422 return (NULL);
9423 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009424 item = xmlSchemaAddModelGroupDefinition(ctxt, schema, name,
9425 ctxt->targetNamespace, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009426 if (item == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009427 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009428 /*
9429 * Check for illegal attributes.
9430 */
9431 attr = node->properties;
9432 while (attr != NULL) {
9433 if (attr->ns == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009434 if ((!xmlStrEqual(attr->name, BAD_CAST "name")) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009435 (!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009436 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009437 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009438 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009439 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009440 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009441 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009442 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009443 attr = attr->next;
9444 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009445 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009446 /*
9447 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009448 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009449 child = node->children;
9450 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009451 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009452 child = child->next;
9453 }
9454 if (IS_SCHEMA(child, "all")) {
9455 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9456 XML_SCHEMA_TYPE_ALL, 0);
9457 child = child->next;
9458 } else if (IS_SCHEMA(child, "choice")) {
9459 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9460 XML_SCHEMA_TYPE_CHOICE, 0);
9461 child = child->next;
9462 } else if (IS_SCHEMA(child, "sequence")) {
9463 item->children = xmlSchemaParseModelGroup(ctxt, schema, child,
9464 XML_SCHEMA_TYPE_SEQUENCE, 0);
9465 child = child->next;
9466 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009467
9468
9469
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009470 if (child != NULL) {
9471 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009472 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009473 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009474 "(annotation?, (all | choice | sequence)?)");
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009475 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009476 return (item);
Daniel Veillard4255d502002-04-16 15:50:10 +00009477}
9478
9479/**
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009480 * xmlSchemaCleanupDoc:
9481 * @ctxt: a schema validation context
9482 * @node: the root of the document.
9483 *
9484 * removes unwanted nodes in a schemas document tree
9485 */
9486static void
9487xmlSchemaCleanupDoc(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr root)
9488{
9489 xmlNodePtr delete, cur;
9490
9491 if ((ctxt == NULL) || (root == NULL)) return;
9492
9493 /*
9494 * Remove all the blank text nodes
9495 */
9496 delete = NULL;
9497 cur = root;
9498 while (cur != NULL) {
9499 if (delete != NULL) {
9500 xmlUnlinkNode(delete);
9501 xmlFreeNode(delete);
9502 delete = NULL;
9503 }
9504 if (cur->type == XML_TEXT_NODE) {
9505 if (IS_BLANK_NODE(cur)) {
9506 if (xmlNodeGetSpacePreserve(cur) != 1) {
9507 delete = cur;
9508 }
9509 }
9510 } else if ((cur->type != XML_ELEMENT_NODE) &&
9511 (cur->type != XML_CDATA_SECTION_NODE)) {
9512 delete = cur;
9513 goto skip_children;
9514 }
9515
9516 /*
9517 * Skip to next node
9518 */
9519 if (cur->children != NULL) {
9520 if ((cur->children->type != XML_ENTITY_DECL) &&
9521 (cur->children->type != XML_ENTITY_REF_NODE) &&
9522 (cur->children->type != XML_ENTITY_NODE)) {
9523 cur = cur->children;
9524 continue;
9525 }
9526 }
9527 skip_children:
9528 if (cur->next != NULL) {
9529 cur = cur->next;
9530 continue;
9531 }
9532
9533 do {
9534 cur = cur->parent;
9535 if (cur == NULL)
9536 break;
9537 if (cur == root) {
9538 cur = NULL;
9539 break;
9540 }
9541 if (cur->next != NULL) {
9542 cur = cur->next;
9543 break;
9544 }
9545 } while (cur != NULL);
9546 }
9547 if (delete != NULL) {
9548 xmlUnlinkNode(delete);
9549 xmlFreeNode(delete);
9550 delete = NULL;
9551 }
9552}
9553
William M. Brack2f2a6632004-08-20 23:09:47 +00009554
William M. Brack2f2a6632004-08-20 23:09:47 +00009555static void
9556xmlSchemaClearSchemaDefaults(xmlSchemaPtr schema)
9557{
9558 if (schema->flags & XML_SCHEMAS_QUALIF_ELEM)
9559 schema->flags ^= XML_SCHEMAS_QUALIF_ELEM;
9560
9561 if (schema->flags & XML_SCHEMAS_QUALIF_ATTR)
9562 schema->flags ^= XML_SCHEMAS_QUALIF_ATTR;
9563
9564 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
9565 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_EXTENSION;
9566 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
9567 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION;
9568 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_LIST)
9569 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_LIST;
9570 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_UNION)
9571 schema->flags ^= XML_SCHEMAS_FINAL_DEFAULT_UNION;
9572
9573 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
9574 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION;
9575 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
9576 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION;
9577 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION)
9578 schema->flags ^= XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION;
9579}
9580
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009581static int
9582xmlSchemaParseSchemaElement(xmlSchemaParserCtxtPtr ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009583 xmlSchemaPtr schema,
9584 xmlNodePtr node)
9585{
9586 xmlAttrPtr attr;
9587 const xmlChar *val;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009588 int res = 0, oldErrs = ctxt->nberrors;
William M. Brack2f2a6632004-08-20 23:09:47 +00009589
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009590 /*
9591 * Those flags should be moved to the parser context flags,
9592 * since they are not visible at the component level. I.e.
9593 * they are used if processing schema *documents* only.
9594 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009595 res = xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009596 HFAILURE;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +00009597
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009598 /*
9599 * Since the version is of type xs:token, we won't bother to
9600 * check it.
9601 */
9602 /* REMOVED:
9603 attr = xmlSchemaGetPropNode(node, "version");
9604 if (attr != NULL) {
9605 res = xmlSchemaPValAttrNode(ctxt, NULL, NULL, attr,
9606 xmlSchemaGetBuiltInType(XML_SCHEMAS_TOKEN), &val);
9607 HFAILURE;
9608 }
9609 */
9610 attr = xmlSchemaGetPropNode(node, "targetNamespace");
9611 if (attr != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009612 res = xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009613 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI), NULL);
9614 HFAILURE;
9615 if (res != 0) {
9616 ctxt->stop = XML_SCHEMAP_S4S_ATTR_INVALID_VALUE;
9617 goto exit;
9618 }
9619 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009620 attr = xmlSchemaGetPropNode(node, "elementFormDefault");
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_ELEM);
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_ELEMFORMDEFAULT_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, "attributeFormDefault");
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 = xmlSchemaPValAttrFormDefault(val, &schema->flags,
9637 XML_SCHEMAS_QUALIF_ATTR);
9638 HFAILURE;
9639 if (res != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009640 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009641 XML_SCHEMAP_ATTRFORMDEFAULT_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009642 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009643 "(qualified | unqualified)", val, NULL, NULL, NULL);
9644 }
9645 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009646 attr = xmlSchemaGetPropNode(node, "finalDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009647 if (attr != NULL) {
9648 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009649 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009650 XML_SCHEMAS_FINAL_DEFAULT_EXTENSION,
9651 XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION,
9652 -1,
9653 XML_SCHEMAS_FINAL_DEFAULT_LIST,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009654 XML_SCHEMAS_FINAL_DEFAULT_UNION);
9655 HFAILURE;
9656 if (res != 0) {
William M. Brack2f2a6632004-08-20 23:09:47 +00009657 xmlSchemaPSimpleTypeErr(ctxt,
9658 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009659 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009660 "(#all | List of (extension | restriction | list | union))",
9661 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009662 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009663 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009664 attr = xmlSchemaGetPropNode(node, "blockDefault");
William M. Brack2f2a6632004-08-20 23:09:47 +00009665 if (attr != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009666 val = xmlSchemaGetNodeContent(ctxt, (xmlNodePtr) attr);
9667 res = xmlSchemaPValAttrBlockFinal(val, &(schema->flags), -1,
William M. Brack2f2a6632004-08-20 23:09:47 +00009668 XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION,
9669 XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009670 XML_SCHEMAS_BLOCK_DEFAULT_SUBSTITUTION, -1, -1);
9671 HFAILURE;
9672 if (res != 0) {
9673 xmlSchemaPSimpleTypeErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +00009674 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009675 NULL, (xmlNodePtr) attr, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +00009676 "(#all | List of (extension | restriction | substitution))",
9677 val, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009678 }
William M. Brack2f2a6632004-08-20 23:09:47 +00009679 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009680
9681exit:
9682 if (oldErrs != ctxt->nberrors)
9683 res = ctxt->err;
9684 return(res);
9685exit_failure:
9686 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +00009687}
9688
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009689/**
9690 * xmlSchemaParseSchemaTopLevel:
9691 * @ctxt: a schema validation context
9692 * @schema: the schemas
9693 * @nodes: the list of top level nodes
9694 *
9695 * Returns the internal XML Schema structure built from the resource or
9696 * NULL in case of error
9697 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009698static int
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009699xmlSchemaParseSchemaTopLevel(xmlSchemaParserCtxtPtr ctxt,
9700 xmlSchemaPtr schema, xmlNodePtr nodes)
9701{
9702 xmlNodePtr child;
9703 xmlSchemaAnnotPtr annot;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009704 int res = 0, oldErrs, tmpOldErrs;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009705
9706 if ((ctxt == NULL) || (schema == NULL) || (nodes == NULL))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009707 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009708
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009709 oldErrs = ctxt->nberrors;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009710 child = nodes;
9711 while ((IS_SCHEMA(child, "include")) ||
9712 (IS_SCHEMA(child, "import")) ||
9713 (IS_SCHEMA(child, "redefine")) ||
9714 (IS_SCHEMA(child, "annotation"))) {
9715 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009716 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009717 if (schema->annot == NULL)
9718 schema->annot = annot;
9719 else
9720 xmlSchemaFreeAnnot(annot);
9721 } else if (IS_SCHEMA(child, "import")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009722 tmpOldErrs = ctxt->nberrors;
9723 res = xmlSchemaParseImport(ctxt, schema, child);
9724 HFAILURE;
9725 HSTOP(ctxt);
9726 if (tmpOldErrs != ctxt->nberrors)
9727 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009728 } else if (IS_SCHEMA(child, "include")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009729 tmpOldErrs = ctxt->nberrors;
9730 res = xmlSchemaParseInclude(ctxt, schema, child);
9731 HFAILURE;
9732 HSTOP(ctxt);
9733 if (tmpOldErrs != ctxt->nberrors)
9734 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009735 } else if (IS_SCHEMA(child, "redefine")) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009736 tmpOldErrs = ctxt->nberrors;
9737 res = xmlSchemaParseRedefine(ctxt, schema, child);
9738 HFAILURE;
9739 HSTOP(ctxt);
9740 if (tmpOldErrs != ctxt->nberrors)
9741 goto exit;
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009742 }
9743 child = child->next;
9744 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009745 /*
9746 * URGENT TODO: Change the functions to return int results.
9747 * We need especially to catch internal errors.
9748 */
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009749 while (child != NULL) {
9750 if (IS_SCHEMA(child, "complexType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009751 xmlSchemaParseComplexType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009752 child = child->next;
9753 } else if (IS_SCHEMA(child, "simpleType")) {
Daniel Veillard3646d642004-06-02 19:19:14 +00009754 xmlSchemaParseSimpleType(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009755 child = child->next;
9756 } else if (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009757 xmlSchemaParseElement(ctxt, schema, child, NULL, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009758 child = child->next;
9759 } else if (IS_SCHEMA(child, "attribute")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009760 xmlSchemaParseGlobalAttribute(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009761 child = child->next;
9762 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009763 xmlSchemaParseAttributeGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009764 child = child->next;
9765 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +00009766 xmlSchemaParseModelGroupDefinition(ctxt, schema, child);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009767 child = child->next;
9768 } else if (IS_SCHEMA(child, "notation")) {
9769 xmlSchemaParseNotation(ctxt, schema, child);
9770 child = child->next;
9771 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009772 xmlSchemaPContentErr(ctxt,
9773 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009774 NULL, child->parent, child,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009775 NULL, "((include | import | redefine | annotation)*, "
9776 "(((simpleType | complexType | group | attributeGroup) "
9777 "| element | attribute | notation), annotation*)*)");
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009778 child = child->next;
9779 }
9780 while (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009781 /*
9782 * TODO: We should add all annotations.
9783 */
9784 annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009785 if (schema->annot == NULL)
9786 schema->annot = annot;
9787 else
9788 xmlSchemaFreeAnnot(annot);
9789 child = child->next;
9790 }
9791 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009792exit:
Daniel Veillard01fa6152004-06-29 17:04:39 +00009793 ctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009794 if (oldErrs != ctxt->nberrors)
9795 res = ctxt->err;
9796 return(res);
9797exit_failure:
9798 return(-1);
Daniel Veillardbd2904b2003-11-25 15:38:59 +00009799}
9800
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009801static xmlSchemaSchemaRelationPtr
9802xmlSchemaSchemaRelationCreate(void)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009803{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009804 xmlSchemaSchemaRelationPtr ret;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009805
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009806 ret = (xmlSchemaSchemaRelationPtr)
9807 xmlMalloc(sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009808 if (ret == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009809 xmlSchemaPErrMemory(NULL, "allocating schema relation", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009810 return(NULL);
9811 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009812 memset(ret, 0, sizeof(xmlSchemaSchemaRelation));
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009813 return(ret);
9814}
9815
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009816#if 0
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009817static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009818xmlSchemaSchemaRelationFree(xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009819{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009820 xmlFree(rel);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +00009821}
9822#endif
9823
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009824static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009825xmlSchemaRedefListFree(xmlSchemaRedefPtr redef)
9826{
9827 xmlSchemaRedefPtr prev;
9828
9829 while (redef != NULL) {
9830 prev = redef;
9831 redef = redef->next;
9832 xmlFree(prev);
9833 }
9834}
9835
9836static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009837xmlSchemaConstructionCtxtFree(xmlSchemaConstructionCtxtPtr con)
9838{
9839 /*
9840 * After the construction context has been freed, there will be
9841 * no schema graph available any more. Only the schema buckets
9842 * will stay alive, which are put into the "schemasImports" and
9843 * "includes" slots of the xmlSchema.
9844 */
9845 if (con->buckets != NULL)
9846 xmlSchemaItemListFree(con->buckets);
9847 if (con->pending != NULL)
9848 xmlSchemaItemListFree(con->pending);
9849 if (con->substGroups != NULL)
9850 xmlHashFree(con->substGroups,
9851 (xmlHashDeallocator) xmlSchemaSubstGroupFree);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009852 if (con->redefs != NULL)
9853 xmlSchemaRedefListFree(con->redefs);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009854 if (con->dict != NULL)
9855 xmlDictFree(con->dict);
9856 xmlFree(con);
9857}
9858
9859static xmlSchemaConstructionCtxtPtr
9860xmlSchemaConstructionCtxtCreate(xmlDictPtr dict)
9861{
9862 xmlSchemaConstructionCtxtPtr ret;
9863
9864 ret = (xmlSchemaConstructionCtxtPtr)
9865 xmlMalloc(sizeof(xmlSchemaConstructionCtxt));
9866 if (ret == NULL) {
9867 xmlSchemaPErrMemory(NULL,
9868 "allocating schema construction context", NULL);
9869 return (NULL);
9870 }
9871 memset(ret, 0, sizeof(xmlSchemaConstructionCtxt));
9872
9873 ret->buckets = xmlSchemaItemListCreate();
9874 if (ret->buckets == NULL) {
9875 xmlSchemaPErrMemory(NULL,
9876 "allocating list of schema buckets", NULL);
9877 xmlFree(ret);
9878 return (NULL);
9879 }
9880 ret->pending = xmlSchemaItemListCreate();
9881 if (ret->pending == NULL) {
9882 xmlSchemaPErrMemory(NULL,
9883 "allocating list of pending global components", NULL);
9884 xmlSchemaConstructionCtxtFree(ret);
9885 return (NULL);
9886 }
9887 ret->dict = dict;
9888 xmlDictReference(dict);
9889 return(ret);
9890}
9891
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009892static xmlSchemaParserCtxtPtr
9893xmlSchemaParserCtxtCreate(void)
9894{
9895 xmlSchemaParserCtxtPtr ret;
9896
9897 ret = (xmlSchemaParserCtxtPtr) xmlMalloc(sizeof(xmlSchemaParserCtxt));
9898 if (ret == NULL) {
9899 xmlSchemaPErrMemory(NULL, "allocating schema parser context",
9900 NULL);
9901 return (NULL);
9902 }
9903 memset(ret, 0, sizeof(xmlSchemaParserCtxt));
9904 ret->type = XML_SCHEMA_CTXT_PARSER;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +00009905 ret->attrProhibs = xmlSchemaItemListCreate();
9906 if (ret->attrProhibs == NULL) {
9907 xmlFree(ret);
9908 return(NULL);
9909 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009910 return(ret);
9911}
9912
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009913/**
9914 * xmlSchemaNewParserCtxtUseDict:
9915 * @URL: the location of the schema
9916 * @dict: the dictionary to be used
9917 *
9918 * Create an XML Schemas parse context for that file/resource expected
9919 * to contain an XML Schemas file.
9920 *
9921 * Returns the parser context or NULL in case of error
9922 */
9923static xmlSchemaParserCtxtPtr
9924xmlSchemaNewParserCtxtUseDict(const char *URL, xmlDictPtr dict)
9925{
9926 xmlSchemaParserCtxtPtr ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009927
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +00009928 ret = xmlSchemaParserCtxtCreate();
9929 if (ret == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009930 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009931 ret->dict = dict;
9932 xmlDictReference(dict);
9933 if (URL != NULL)
9934 ret->URL = xmlDictLookup(dict, (const xmlChar *) URL, -1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009935 return (ret);
9936}
9937
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +00009938static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009939xmlSchemaCreatePCtxtOnVCtxt(xmlSchemaValidCtxtPtr vctxt)
9940{
9941 if (vctxt->pctxt == NULL) {
9942 if (vctxt->schema != NULL)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009943 vctxt->pctxt =
9944 xmlSchemaNewParserCtxtUseDict("*", vctxt->schema->dict);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009945 else
9946 vctxt->pctxt = xmlSchemaNewParserCtxt("*");
9947 if (vctxt->pctxt == NULL) {
9948 VERROR_INT("xmlSchemaCreatePCtxtOnVCtxt",
9949 "failed to create a temp. parser context");
9950 return (-1);
9951 }
9952 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009953 xmlSchemaSetParserErrors(vctxt->pctxt, vctxt->error,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +00009954 vctxt->warning, vctxt->errCtxt);
9955 xmlSchemaSetParserStructuredErrors(vctxt->pctxt, vctxt->serror,
9956 vctxt->errCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +00009957 }
9958 return (0);
9959}
9960
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +00009961/**
9962 * xmlSchemaGetSchemaBucket:
9963 * @pctxt: the schema parser context
9964 * @schemaLocation: the URI of the schema document
9965 *
9966 * Returns a schema bucket if it was already parsed.
9967 *
9968 * Returns a schema bucket if it was already parsed from
9969 * @schemaLocation, NULL otherwise.
9970 */
9971static xmlSchemaBucketPtr
9972xmlSchemaGetSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
9973 const xmlChar *schemaLocation)
9974{
9975 xmlSchemaBucketPtr cur;
9976 xmlSchemaItemListPtr list;
9977
9978 list = pctxt->constructor->buckets;
9979 if (list->nbItems == 0)
9980 return(NULL);
9981 else {
9982 int i;
9983 for (i = 0; i < list->nbItems; i++) {
9984 cur = (xmlSchemaBucketPtr) list->items[i];
9985 /* Pointer comparison! */
9986 if (cur->schemaLocation == schemaLocation)
9987 return(cur);
9988 }
9989 }
9990 return(NULL);
9991}
9992
9993static xmlSchemaBucketPtr
9994xmlSchemaGetChameleonSchemaBucket(xmlSchemaParserCtxtPtr pctxt,
9995 const xmlChar *schemaLocation,
9996 const xmlChar *targetNamespace)
9997{
9998 xmlSchemaBucketPtr cur;
9999 xmlSchemaItemListPtr list;
10000
10001 list = pctxt->constructor->buckets;
10002 if (list->nbItems == 0)
10003 return(NULL);
10004 else {
10005 int i;
10006 for (i = 0; i < list->nbItems; i++) {
10007 cur = (xmlSchemaBucketPtr) list->items[i];
10008 /* Pointer comparison! */
10009 if ((cur->origTargetNamespace == NULL) &&
10010 (cur->schemaLocation == schemaLocation) &&
10011 (cur->targetNamespace == targetNamespace))
10012 return(cur);
10013 }
10014 }
10015 return(NULL);
10016}
10017
10018
10019#define IS_BAD_SCHEMA_DOC(b) \
10020 (((b)->doc == NULL) && ((b)->schemaLocation != NULL))
10021
10022static xmlSchemaBucketPtr
10023xmlSchemaGetSchemaBucketByTNS(xmlSchemaParserCtxtPtr pctxt,
10024 const xmlChar *targetNamespace,
10025 int imported)
10026{
10027 xmlSchemaBucketPtr cur;
10028 xmlSchemaItemListPtr list;
10029
10030 list = pctxt->constructor->buckets;
10031 if (list->nbItems == 0)
10032 return(NULL);
10033 else {
10034 int i;
10035 for (i = 0; i < list->nbItems; i++) {
10036 cur = (xmlSchemaBucketPtr) list->items[i];
10037 if ((! IS_BAD_SCHEMA_DOC(cur)) &&
10038 (cur->origTargetNamespace == targetNamespace) &&
10039 ((imported && cur->imported) ||
10040 ((!imported) && (!cur->imported))))
10041 return(cur);
10042 }
10043 }
10044 return(NULL);
10045}
10046
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010047static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010048xmlSchemaParseNewDocWithContext(xmlSchemaParserCtxtPtr pctxt,
10049 xmlSchemaPtr schema,
10050 xmlSchemaBucketPtr bucket)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010051{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010052 int oldFlags;
10053 xmlDocPtr oldDoc;
10054 xmlNodePtr node;
10055 int ret, oldErrs;
10056 xmlSchemaBucketPtr oldbucket = pctxt->constructor->bucket;
10057
10058 /*
10059 * Save old values; reset the *main* schema.
10060 * URGENT TODO: This is not good; move the per-document information
10061 * to the parser.
10062 */
10063 oldFlags = schema->flags;
10064 oldDoc = schema->doc;
10065 if (schema->flags != 0)
10066 xmlSchemaClearSchemaDefaults(schema);
10067 schema->doc = bucket->doc;
10068 /* !! REMOVED: schema->targetNamespace = bucket->targetNamespace; */
10069 pctxt->schema = schema;
10070 /*
10071 * Keep the current target namespace on the parser *not* on the
10072 * main schema.
10073 */
10074 pctxt->targetNamespace = bucket->targetNamespace;
10075 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010076
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010077 if ((bucket->targetNamespace != NULL) &&
10078 xmlStrEqual(bucket->targetNamespace, xmlSchemaNs)) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010079 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010080 * We are parsing the schema for schemas!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010081 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010082 pctxt->isS4S = 1;
10083 }
10084 /* Mark it as parsed, even if parsing fails. */
10085 bucket->parsed++;
10086 /* Compile the schema doc. */
10087 node = xmlDocGetRootElement(bucket->doc);
10088 ret = xmlSchemaParseSchemaElement(pctxt, schema, node);
10089 if (ret != 0)
10090 goto exit;
10091 /* An empty schema; just get out. */
10092 if (node->children == NULL)
10093 goto exit;
10094 oldErrs = pctxt->nberrors;
10095 ret = xmlSchemaParseSchemaTopLevel(pctxt, schema, node->children);
10096 if (ret != 0)
10097 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010098 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010099 * TODO: Not nice, but I'm not 100% sure we will get always an error
10100 * as a result of the obove functions; so better rely on pctxt->err
10101 * as well.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010102 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010103 if ((ret == 0) && (oldErrs != pctxt->nberrors)) {
10104 ret = pctxt->err;
10105 goto exit;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010106 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010107
10108exit:
10109 WXS_CONSTRUCTOR(pctxt)->bucket = oldbucket;
10110 /* Restore schema values. */
10111 schema->doc = oldDoc;
10112 schema->flags = oldFlags;
10113 return(ret);
10114}
10115
10116static int
10117xmlSchemaParseNewDoc(xmlSchemaParserCtxtPtr pctxt,
10118 xmlSchemaPtr schema,
10119 xmlSchemaBucketPtr bucket)
10120{
10121 xmlSchemaParserCtxtPtr newpctxt;
10122 int res = 0;
10123
10124 if (bucket == NULL)
10125 return(0);
10126 if (bucket->parsed) {
10127 PERROR_INT("xmlSchemaParseNewDoc",
10128 "reparsing a schema doc");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010129 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010130 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010131 if (bucket->doc == NULL) {
10132 PERROR_INT("xmlSchemaParseNewDoc",
10133 "parsing a schema doc, but there's no doc");
10134 return(-1);
Kasimier T. Buchcik5dd55d92004-11-26 12:35:21 +000010135 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010136 if (pctxt->constructor == NULL) {
10137 PERROR_INT("xmlSchemaParseNewDoc",
10138 "no constructor");
10139 return(-1);
10140 }
10141 /* Create and init the temporary parser context. */
10142 newpctxt = xmlSchemaNewParserCtxtUseDict(
10143 (const char *) bucket->schemaLocation, pctxt->dict);
10144 if (newpctxt == NULL)
10145 return(-1);
10146 newpctxt->constructor = pctxt->constructor;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010147 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010148 * TODO: Can we avoid that the parser knows about the main schema?
10149 * It would be better if he knows about the current schema bucket
10150 * only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010151 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010152 newpctxt->schema = schema;
10153 xmlSchemaSetParserErrors(newpctxt, pctxt->error, pctxt->warning,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000010154 pctxt->errCtxt);
10155 xmlSchemaSetParserStructuredErrors(newpctxt, pctxt->serror,
10156 pctxt->errCtxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010157 newpctxt->counter = pctxt->counter;
10158
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010159
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010160 res = xmlSchemaParseNewDocWithContext(newpctxt, schema, bucket);
10161
10162 /* Channel back errors and cleanup the temporary parser context. */
10163 if (res != 0)
10164 pctxt->err = res;
10165 pctxt->nberrors += newpctxt->nberrors;
10166 pctxt->counter = newpctxt->counter;
10167 newpctxt->constructor = NULL;
10168 /* Free the parser context. */
10169 xmlSchemaFreeParserCtxt(newpctxt);
10170 return(res);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010171}
William M. Brack2f2a6632004-08-20 23:09:47 +000010172
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010173static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010174xmlSchemaSchemaRelationAddChild(xmlSchemaBucketPtr bucket,
10175 xmlSchemaSchemaRelationPtr rel)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010176{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010177 xmlSchemaSchemaRelationPtr cur = bucket->relations;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010178
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010179 if (cur == NULL) {
10180 bucket->relations = rel;
10181 return;
10182 }
10183 while (cur->next != NULL)
10184 cur = cur->next;
10185 cur->next = rel;
10186}
10187
10188
10189static const xmlChar *
10190xmlSchemaBuildAbsoluteURI(xmlDictPtr dict, const xmlChar* location,
10191 xmlNodePtr ctxtNode)
10192{
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010193 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010194 * Build an absolue location URI.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010195 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010196 if (location != NULL) {
10197 if (ctxtNode == NULL)
10198 return(location);
10199 else {
10200 xmlChar *base, *URI;
10201 const xmlChar *ret = NULL;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010202
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010203 base = xmlNodeGetBase(ctxtNode->doc, ctxtNode);
10204 if (base == NULL) {
10205 URI = xmlBuildURI(location, ctxtNode->doc->URL);
10206 } else {
10207 URI = xmlBuildURI(location, base);
10208 xmlFree(base);
10209 }
10210 if (URI != NULL) {
10211 ret = xmlDictLookup(dict, URI, -1);
10212 xmlFree(URI);
10213 return(ret);
10214 }
10215 }
10216 }
10217 return(NULL);
10218}
10219
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010220
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010221
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010222/**
10223 * xmlSchemaAddSchemaDoc:
10224 * @pctxt: a schema validation context
10225 * @schema: the schema being built
10226 * @node: a subtree containing XML Schema informations
10227 *
10228 * Parse an included (and to-be-redefined) XML schema document.
10229 *
10230 * Returns 0 on success, a positive error code on errors and
10231 * -1 in case of an internal or API error.
10232 */
10233
10234static int
10235xmlSchemaAddSchemaDoc(xmlSchemaParserCtxtPtr pctxt,
10236 int type, /* import or include or redefine */
10237 const xmlChar *schemaLocation,
10238 xmlDocPtr schemaDoc,
10239 const char *schemaBuffer,
10240 int schemaBufferLen,
10241 xmlNodePtr invokingNode,
10242 const xmlChar *sourceTargetNamespace,
10243 const xmlChar *importNamespace,
10244 xmlSchemaBucketPtr *bucket)
10245{
10246 const xmlChar *targetNamespace = NULL;
10247 xmlSchemaSchemaRelationPtr relation = NULL;
10248 xmlDocPtr doc = NULL;
10249 int res = 0, err = 0, located = 0, preserveDoc = 0;
10250 xmlSchemaBucketPtr bkt = NULL;
10251
10252 if (bucket != NULL)
10253 *bucket = NULL;
10254
10255 switch (type) {
10256 case XML_SCHEMA_SCHEMA_IMPORT:
10257 case XML_SCHEMA_SCHEMA_MAIN:
10258 err = XML_SCHEMAP_SRC_IMPORT;
10259 break;
10260 case XML_SCHEMA_SCHEMA_INCLUDE:
10261 err = XML_SCHEMAP_SRC_INCLUDE;
10262 break;
10263 case XML_SCHEMA_SCHEMA_REDEFINE:
10264 err = XML_SCHEMAP_SRC_REDEFINE;
10265 break;
10266 }
10267
10268
10269 /* Special handling for the main schema:
10270 * skip the location and relation logic and just parse the doc.
10271 * We need just a bucket to be returned in this case.
10272 */
10273 if ((type == XML_SCHEMA_SCHEMA_MAIN) || (! WXS_HAS_BUCKETS(pctxt)))
10274 goto doc_load;
10275
10276 /* Note that we expect the location to be an absulute URI. */
10277 if (schemaLocation != NULL) {
10278 bkt = xmlSchemaGetSchemaBucket(pctxt, schemaLocation);
10279 if ((bkt != NULL) &&
10280 (pctxt->constructor->bucket == bkt)) {
10281 /* Report self-imports/inclusions/redefinitions. */
10282
10283 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10284 invokingNode, NULL,
10285 "The schema must not import/include/redefine itself",
10286 NULL, NULL);
10287 goto exit;
10288 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000010289 }
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010290 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010291 * Create a relation for the graph of schemas.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010292 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010293 relation = xmlSchemaSchemaRelationCreate();
10294 if (relation == NULL)
10295 return(-1);
10296 xmlSchemaSchemaRelationAddChild(pctxt->constructor->bucket,
10297 relation);
10298 relation->type = type;
10299
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010300 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010301 * Save the namespace import information.
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010302 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010303 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010304 relation->importNamespace = importNamespace;
10305 if (schemaLocation == NULL) {
10306 /*
10307 * No location; this is just an import of the namespace.
10308 * Note that we don't assign a bucket to the relation
10309 * in this case.
10310 */
10311 goto exit;
10312 }
10313 targetNamespace = importNamespace;
10314 }
10315
10316 /* Did we already fetch the doc? */
10317 if (bkt != NULL) {
10318 /* TODO: The following nasty cases will produce an error. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010319 if ((WXS_IS_BUCKET_IMPMAIN(type)) && (! bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010320 /* We included/redefined and then try to import a schema. */
10321 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10322 invokingNode, NULL,
10323 "The schema document '%s' cannot be imported, since "
10324 "it was already included or redefined",
10325 schemaLocation, NULL);
10326 goto exit;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010327 } else if ((! WXS_IS_BUCKET_IMPMAIN(type)) && (bkt->imported)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010328 /* We imported and then try to include/redefine a schema. */
10329 xmlSchemaCustomErr(ACTXT_CAST pctxt, err,
10330 invokingNode, NULL,
10331 "The schema document '%s' cannot be included or "
10332 "redefined, since it was already imported",
10333 schemaLocation, NULL);
10334 goto exit;
10335 }
10336 }
10337
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010338 if (WXS_IS_BUCKET_IMPMAIN(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010339 /*
10340 * Given that the schemaLocation [attribute] is only a hint, it is open
10341 * to applications to ignore all but the first <import> for a given
10342 * namespace, regardless of the ·actual value· of schemaLocation, but
10343 * such a strategy risks missing useful information when new
10344 * schemaLocations are offered.
10345 *
10346 * We will use the first <import> that comes with a location.
10347 * Further <import>s *with* a location, will result in an error.
10348 * TODO: Better would be to just report a warning here, but
10349 * we'll try it this way until someone complains.
10350 *
10351 * Schema Document Location Strategy:
10352 * 3 Based on the namespace name, identify an existing schema document,
10353 * either as a resource which is an XML document or a <schema> element
10354 * information item, in some local schema repository;
10355 * 5 Attempt to resolve the namespace name to locate such a resource.
10356 *
10357 * NOTE: (3) and (5) are not supported.
10358 */
10359 if (bkt != NULL) {
10360 relation->bucket = bkt;
10361 goto exit;
10362 }
10363 bkt = xmlSchemaGetSchemaBucketByTNS(pctxt,
10364 importNamespace, 1);
10365
10366 if (bkt != NULL) {
10367 relation->bucket = bkt;
10368 if (bkt->schemaLocation == NULL) {
10369 /* First given location of the schema; load the doc. */
10370 bkt->schemaLocation = schemaLocation;
10371 } else {
10372 if (!xmlStrEqual(schemaLocation,
10373 bkt->schemaLocation)) {
10374 /*
10375 * Additional location given; just skip it.
10376 * URGENT TODO: We should report a warning here.
10377 * res = XML_SCHEMAP_SRC_IMPORT;
10378 */
10379 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10380 XML_SCHEMAP_WARN_SKIP_SCHEMA,
10381 invokingNode, NULL,
10382 "Skipping import of schema located at '%s' for the "
10383 "namespace '%s', since this namespace was already "
10384 "imported with the schema located at '%s'",
10385 schemaLocation, importNamespace, bkt->schemaLocation);
10386 }
10387 goto exit;
10388 }
10389 }
10390 /*
10391 * No bucket + first location: load the doc and create a
10392 * bucket.
10393 */
10394 } else {
10395 /* <include> and <redefine> */
10396 if (bkt != NULL) {
10397
10398 if ((bkt->origTargetNamespace == NULL) &&
10399 (bkt->targetNamespace != sourceTargetNamespace)) {
10400 xmlSchemaBucketPtr chamel;
10401
10402 /*
10403 * Chameleon include/redefine: skip loading only if it was
10404 * aleady build for the targetNamespace of the including
10405 * schema.
10406 */
10407 /*
10408 * URGENT TODO: If the schema is a chameleon-include then copy
10409 * the components into the including schema and modify the
10410 * targetNamespace of those components, do nothing otherwise.
10411 * NOTE: This is currently worked-around by compiling the
10412 * chameleon for every destinct including targetNamespace; thus
10413 * not performant at the moment.
10414 * TODO: Check when the namespace in wildcards for chameleons
10415 * needs to be converted: before we built wildcard intersections
10416 * or after.
10417 * Answer: after!
10418 */
10419 chamel = xmlSchemaGetChameleonSchemaBucket(pctxt,
10420 schemaLocation, sourceTargetNamespace);
10421 if (chamel != NULL) {
10422 /* A fitting chameleon was already parsed; NOP. */
10423 relation->bucket = chamel;
10424 goto exit;
10425 }
10426 /*
10427 * We need to parse the chameleon again for a different
10428 * targetNamespace.
10429 * CHAMELEON TODO: Optimize this by only parsing the
10430 * chameleon once, and then copying the components to
10431 * the new targetNamespace.
10432 */
10433 bkt = NULL;
10434 } else {
10435 relation->bucket = bkt;
10436 goto exit;
10437 }
10438 }
10439 }
10440 if ((bkt != NULL) && (bkt->doc != NULL)) {
10441 PERROR_INT("xmlSchemaAddSchemaDoc",
10442 "trying to load a schema doc, but a doc is already "
10443 "assigned to the schema bucket");
10444 goto exit_failure;
10445 }
10446
10447doc_load:
10448 /*
10449 * Load the document.
10450 */
10451 if (schemaDoc != NULL) {
10452 doc = schemaDoc;
10453 /* Don' free this one, since it was provided by the caller. */
10454 preserveDoc = 1;
10455 /* TODO: Does the context or the doc hold the location? */
10456 if (schemaDoc->URL != NULL)
10457 schemaLocation = xmlDictLookup(pctxt->dict,
10458 schemaDoc->URL, -1);
10459
10460 } else if ((schemaLocation != NULL) || (schemaBuffer != NULL)) {
10461 xmlParserCtxtPtr parserCtxt;
10462
10463 parserCtxt = xmlNewParserCtxt();
10464 if (parserCtxt == NULL) {
10465 xmlSchemaPErrMemory(NULL, "xmlSchemaGetDoc, "
10466 "allocating a parser context", NULL);
10467 goto exit_failure;
10468 }
10469 if ((pctxt->dict != NULL) && (parserCtxt->dict != NULL)) {
10470 /*
10471 * TODO: Do we have to burden the schema parser dict with all
10472 * the content of the schema doc?
10473 */
10474 xmlDictFree(parserCtxt->dict);
10475 parserCtxt->dict = pctxt->dict;
10476 xmlDictReference(parserCtxt->dict);
10477 }
10478 if (schemaLocation != NULL) {
10479 /* Parse from file. */
10480 doc = xmlCtxtReadFile(parserCtxt, (const char *) schemaLocation,
10481 NULL, SCHEMAS_PARSE_OPTIONS);
10482 } else if (schemaBuffer != NULL) {
10483 /* Parse from memory buffer. */
10484 doc = xmlCtxtReadMemory(parserCtxt, schemaBuffer, schemaBufferLen,
10485 NULL, NULL, SCHEMAS_PARSE_OPTIONS);
10486 schemaLocation = xmlStrdup(BAD_CAST "in_memory_buffer");
10487 if (doc != NULL)
10488 doc->URL = schemaLocation;
10489 }
10490 /*
10491 * For <import>:
10492 * 2.1 The referent is (a fragment of) a resource which is an
10493 * XML document (see clause 1.1), which in turn corresponds to
10494 * a <schema> element information item in a well-formed information
10495 * set, which in turn corresponds to a valid schema.
10496 * TODO: (2.1) fragments of XML documents are not supported.
10497 *
10498 * 2.2 The referent is a <schema> element information item in
10499 * a well-formed information set, which in turn corresponds
10500 * to a valid schema.
10501 * TODO: (2.2) is not supported.
10502 */
10503 if (doc == NULL) {
10504 xmlErrorPtr lerr;
10505 lerr = xmlGetLastError();
10506 /*
10507 * Check if this a parser error, or if the document could
10508 * just not be located.
10509 * TODO: Try to find specific error codes to react only on
10510 * localisation failures.
10511 */
10512 if ((lerr == NULL) || (lerr->domain != XML_FROM_IO)) {
10513 /*
10514 * We assume a parser error here.
10515 */
10516 located = 1;
10517 /* TODO: Error code ?? */
10518 res = XML_SCHEMAP_SRC_IMPORT_2_1;
10519 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10520 invokingNode, NULL,
10521 "Failed to parse the XML resource '%s'",
10522 schemaLocation, NULL);
10523 }
10524 }
10525 xmlFreeParserCtxt(parserCtxt);
10526 if ((doc == NULL) && located)
10527 goto exit_error;
10528 } else {
10529 xmlSchemaPErr(pctxt, NULL,
10530 XML_SCHEMAP_NOTHING_TO_PARSE,
10531 "No information for parsing was provided with the "
10532 "given schema parser context.\n",
10533 NULL, NULL);
10534 goto exit_failure;
10535 }
10536 /*
10537 * Preprocess the document.
10538 */
10539 if (doc != NULL) {
10540 xmlNodePtr docElem = NULL;
10541
10542 located = 1;
10543 docElem = xmlDocGetRootElement(doc);
10544 if (docElem == NULL) {
10545 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOROOT,
10546 invokingNode, NULL,
10547 "The document '%s' has no document element",
10548 schemaLocation, NULL);
10549 xmlFreeDoc(doc);
10550 doc = NULL;
10551 goto exit_error;
10552 }
10553 /*
10554 * Remove all the blank text nodes.
10555 */
10556 xmlSchemaCleanupDoc(pctxt, docElem);
10557 /*
10558 * Check the schema's top level element.
10559 */
10560 if (!IS_SCHEMA(docElem, "schema")) {
10561 xmlSchemaCustomErr(ACTXT_CAST pctxt, XML_SCHEMAP_NOT_SCHEMA,
10562 invokingNode, NULL,
10563 "The XML document '%s' is not a schema document",
10564 schemaLocation, NULL);
10565 xmlFreeDoc(doc);
10566 doc = NULL;
10567 goto exit_error;
10568 }
10569 /*
10570 * Note that we don't apply a type check for the
10571 * targetNamespace value here.
10572 */
10573 targetNamespace = xmlSchemaGetProp(pctxt, docElem,
10574 "targetNamespace");
10575 }
10576
10577/* after_doc_loading: */
10578 if ((bkt == NULL) && located) {
10579 /* Only create a bucket if the schema was located. */
10580 bkt = xmlSchemaBucketCreate(pctxt, type,
10581 targetNamespace);
10582 if (bkt == NULL)
10583 goto exit_failure;
10584 }
10585 if (bkt != NULL) {
10586 bkt->schemaLocation = schemaLocation;
10587 bkt->located = located;
10588 if (doc != NULL) {
10589 bkt->doc = doc;
10590 bkt->targetNamespace = targetNamespace;
10591 bkt->origTargetNamespace = targetNamespace;
10592 if (preserveDoc)
10593 bkt->preserveDoc = 1;
10594 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010595 if (WXS_IS_BUCKET_IMPMAIN(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010596 bkt->imported++;
10597 /*
10598 * Add it to the graph of schemas.
10599 */
10600 if (relation != NULL)
10601 relation->bucket = bkt;
10602 }
10603
10604exit:
10605 /*
10606 * Return the bucket explicitely; this is needed for the
10607 * main schema.
10608 */
10609 if (bucket != NULL)
10610 *bucket = bkt;
10611 return (0);
10612
10613exit_error:
10614 if ((doc != NULL) && (! preserveDoc)) {
10615 xmlFreeDoc(doc);
10616 if (bkt != NULL)
10617 bkt->doc = NULL;
10618 }
10619 return(pctxt->err);
10620
10621exit_failure:
10622 if ((doc != NULL) && (! preserveDoc)) {
10623 xmlFreeDoc(doc);
10624 if (bkt != NULL)
10625 bkt->doc = NULL;
10626 }
10627 return (-1);
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010628}
10629
William M. Brack2f2a6632004-08-20 23:09:47 +000010630/**
10631 * xmlSchemaParseImport:
10632 * @ctxt: a schema validation context
10633 * @schema: the schema being built
10634 * @node: a subtree containing XML Schema informations
10635 *
10636 * parse a XML schema Import definition
10637 * *WARNING* this interface is highly subject to change
10638 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010639 * Returns 0 in case of success, a positive error code if
10640 * not valid and -1 in case of an internal error.
William M. Brack2f2a6632004-08-20 23:09:47 +000010641 */
10642static int
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010643xmlSchemaParseImport(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
William M. Brack2f2a6632004-08-20 23:09:47 +000010644 xmlNodePtr node)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010645{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000010646 xmlNodePtr child;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010647 const xmlChar *namespaceName = NULL, *schemaLocation = NULL;
10648 const xmlChar *thisTargetNamespace;
William M. Brack2f2a6632004-08-20 23:09:47 +000010649 xmlAttrPtr attr;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010650 int ret = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010651 xmlSchemaBucketPtr bucket = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000010652
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010653 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
William M. Brack2f2a6632004-08-20 23:09:47 +000010654 return (-1);
10655
10656 /*
10657 * Check for illegal attributes.
10658 */
10659 attr = node->properties;
10660 while (attr != NULL) {
10661 if (attr->ns == NULL) {
10662 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10663 (!xmlStrEqual(attr->name, BAD_CAST "namespace")) &&
10664 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010665 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010666 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010667 }
10668 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010669 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010670 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000010671 }
10672 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010673 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010674 /*
10675 * Extract and validate attributes.
10676 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010677 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010678 "namespace", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010679 &namespaceName) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010680 xmlSchemaPSimpleTypeErr(pctxt,
10681 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010682 NULL, node,
10683 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010684 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010685 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010686 }
10687
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010688 if (xmlSchemaPValAttr(pctxt, NULL, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010689 "schemaLocation", xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
William M. Brack2f2a6632004-08-20 23:09:47 +000010690 &schemaLocation) != 0) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010691 xmlSchemaPSimpleTypeErr(pctxt,
10692 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010693 NULL, node,
10694 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010695 NULL, namespaceName, NULL, NULL, NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010696 return (pctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010697 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010698 /*
10699 * And now for the children...
10700 */
10701 child = node->children;
10702 if (IS_SCHEMA(child, "annotation")) {
10703 /*
10704 * the annotation here is simply discarded ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000010705 * TODO: really?
William M. Brack2f2a6632004-08-20 23:09:47 +000010706 */
10707 child = child->next;
10708 }
10709 if (child != NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010710 xmlSchemaPContentErr(pctxt,
10711 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010712 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000010713 "(annotation?)");
10714 }
10715 /*
10716 * Apply additional constraints.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010717 *
10718 * Note that it is important to use the original @targetNamespace
10719 * (or none at all), to rule out imports of schemas _with_ a
10720 * @targetNamespace if the importing schema is a chameleon schema
10721 * (with no @targetNamespace).
William M. Brack2f2a6632004-08-20 23:09:47 +000010722 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010723 thisTargetNamespace = WXS_BUCKET(pctxt)->origTargetNamespace;
Kasimier T. Buchcikb474fa42005-03-11 12:34:42 +000010724 if (namespaceName != NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000010725 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010726 * 1.1 If the namespace [attribute] is present, then its ·actual value·
10727 * must not match the ·actual value· of the enclosing <schema>'s
William M. Brack2f2a6632004-08-20 23:09:47 +000010728 * targetNamespace [attribute].
10729 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010730 if (xmlStrEqual(thisTargetNamespace, namespaceName)) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010731 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010732 XML_SCHEMAP_SRC_IMPORT_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010733 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010734 "The value of the attribute 'namespace' must not match "
10735 "the target namespace '%s' of the importing schema",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010736 thisTargetNamespace);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010737 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010738 }
10739 } else {
10740 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010741 * 1.2 If the namespace [attribute] is not present, then the enclosing
William M. Brack2f2a6632004-08-20 23:09:47 +000010742 * <schema> must have a targetNamespace [attribute].
10743 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010744 if (thisTargetNamespace == NULL) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010745 xmlSchemaPCustomErr(pctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000010746 XML_SCHEMAP_SRC_IMPORT_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010747 NULL, node,
William M. Brack2f2a6632004-08-20 23:09:47 +000010748 "The attribute 'namespace' must be existent if "
10749 "the importing schema has no target namespace",
10750 NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010751 return (pctxt->err);
William M. Brack2f2a6632004-08-20 23:09:47 +000010752 }
10753 }
William M. Brack2f2a6632004-08-20 23:09:47 +000010754 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010755 * Locate and acquire the schema document.
William M. Brack2f2a6632004-08-20 23:09:47 +000010756 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010757 if (schemaLocation != NULL)
10758 schemaLocation = xmlSchemaBuildAbsoluteURI(pctxt->dict,
10759 schemaLocation, node);
10760 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010761 schemaLocation, NULL, NULL, 0, node, thisTargetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010762 namespaceName, &bucket);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000010763
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010764 if (ret != 0)
10765 return(ret);
10766
10767 /*
10768 * For <import>: "It is *not* an error for the application
10769 * schema reference strategy to fail."
10770 * So just don't parse if no schema document was found.
10771 * Note that we will get no bucket if the schema could not be
10772 * located or if there was no schemaLocation.
10773 */
10774 if ((bucket == NULL) && (schemaLocation != NULL)) {
10775 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
10776 XML_SCHEMAP_WARN_UNLOCATED_SCHEMA,
10777 node, NULL,
10778 "Failed to locate a schema at location '%s'. "
10779 "Skipping the import", schemaLocation, NULL, NULL);
10780 }
10781
10782 if ((bucket != NULL) && CAN_PARSE_SCHEMA(bucket)) {
10783 ret = xmlSchemaParseNewDoc(pctxt, schema, bucket);
10784 }
10785
10786 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000010787}
10788
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010789static int
10790xmlSchemaParseIncludeOrRedefineAttrs(xmlSchemaParserCtxtPtr pctxt,
10791 xmlSchemaPtr schema,
10792 xmlNodePtr node,
10793 xmlChar **schemaLocation,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010794 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010795{
10796 xmlAttrPtr attr;
10797
10798 if ((pctxt == NULL) || (schema == NULL) || (node == NULL) ||
10799 (schemaLocation == NULL))
10800 return (-1);
10801
10802 *schemaLocation = NULL;
10803 /*
10804 * Check for illegal attributes.
10805 * Applies for both <include> and <redefine>.
10806 */
10807 attr = node->properties;
10808 while (attr != NULL) {
10809 if (attr->ns == NULL) {
10810 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
10811 (!xmlStrEqual(attr->name, BAD_CAST "schemaLocation"))) {
10812 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010813 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010814 }
10815 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
10816 xmlSchemaPIllegalAttrErr(pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010817 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010818 }
10819 attr = attr->next;
10820 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010821 xmlSchemaPValAttrID(pctxt, node, BAD_CAST "id");
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010822 /*
10823 * Preliminary step, extract the URI-Reference and make an URI
10824 * from the base.
10825 */
10826 /*
10827 * Attribute "schemaLocation" is mandatory.
10828 */
10829 attr = xmlSchemaGetPropNode(node, "schemaLocation");
10830 if (attr != NULL) {
10831 xmlChar *base = NULL;
10832 xmlChar *uri = NULL;
10833
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010834 if (xmlSchemaPValAttrNode(pctxt, NULL, attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010835 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYURI),
10836 (const xmlChar **) schemaLocation) != 0)
10837 goto exit_error;
10838 base = xmlNodeGetBase(node->doc, node);
10839 if (base == NULL) {
10840 uri = xmlBuildURI(*schemaLocation, node->doc->URL);
10841 } else {
10842 uri = xmlBuildURI(*schemaLocation, base);
10843 xmlFree(base);
10844 }
10845 if (uri == NULL) {
10846 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
10847 "could not build an URI from the schemaLocation")
10848 goto exit_failure;
10849 }
10850 (*schemaLocation) = (xmlChar *) xmlDictLookup(pctxt->dict, uri, -1);
10851 xmlFree(uri);
10852 } else {
10853 xmlSchemaPMissingAttrErr(pctxt,
10854 XML_SCHEMAP_S4S_ATTR_MISSING,
10855 NULL, node, "schemaLocation", NULL);
10856 goto exit_error;
10857 }
10858 /*
10859 * Report self-inclusion and self-redefinition.
10860 */
10861 if (xmlStrEqual(*schemaLocation, pctxt->URL)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010862 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010863 xmlSchemaPCustomErr(pctxt,
10864 XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010865 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010866 "The schema document '%s' cannot redefine itself.",
10867 *schemaLocation);
10868 } else {
10869 xmlSchemaPCustomErr(pctxt,
10870 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010871 NULL, node,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010872 "The schema document '%s' cannot include itself.",
10873 *schemaLocation);
10874 }
10875 goto exit_error;
10876 }
10877
10878 return(0);
10879exit_error:
10880 return(pctxt->err);
10881exit_failure:
10882 return(-1);
10883}
10884
10885static int
10886xmlSchemaParseIncludeOrRedefine(xmlSchemaParserCtxtPtr pctxt,
10887 xmlSchemaPtr schema,
10888 xmlNodePtr node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010889 int type)
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010890{
10891 xmlNodePtr child = NULL;
10892 const xmlChar *schemaLocation = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000010893 int res = 0; /* hasRedefinitions = 0 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010894 int isChameleon = 0, wasChameleon = 0;
10895 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010896
10897 if ((pctxt == NULL) || (schema == NULL) || (node == NULL))
10898 return (-1);
10899
10900 /*
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000010901 * Parse attributes. Note that the returned schemaLocation will
10902 * be already converted to an absolute URI.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010903 */
10904 res = xmlSchemaParseIncludeOrRedefineAttrs(pctxt, schema,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010905 node, (xmlChar **) (&schemaLocation), type);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010906 if (res != 0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010907 return(res);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010908 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010909 * Load and add the schema document.
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010910 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010911 res = xmlSchemaAddSchemaDoc(pctxt, type, schemaLocation, NULL,
10912 NULL, 0, node, pctxt->targetNamespace, NULL, &bucket);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000010913 if (res != 0)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010914 return(res);
10915 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010916 * If we get no schema bucket back, then this means that the schema
10917 * document could not be located or was broken XML or was not
10918 * a schema document.
10919 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010920 if ((bucket == NULL) || (bucket->doc == NULL)) {
10921 if (type == XML_SCHEMA_SCHEMA_INCLUDE) {
10922 /*
10923 * WARNING for <include>:
10924 * We will raise an error if the schema cannot be located
10925 * for inclusions, since the that was the feedback from the
10926 * schema people. I.e. the following spec piece will *not* be
10927 * satisfied:
10928 * SPEC src-include: "It is not an error for the ·actual value· of the
10929 * schemaLocation [attribute] to fail to resolve it all, in which
10930 * case no corresponding inclusion is performed.
10931 * So do we need a warning report here?"
10932 */
10933 res = XML_SCHEMAP_SRC_INCLUDE;
10934 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10935 node, NULL,
10936 "Failed to load the document '%s' for inclusion",
10937 schemaLocation, NULL);
10938 } else {
10939 /*
10940 * NOTE: This was changed to raise an error even if no redefinitions
10941 * are specified.
10942 *
10943 * SPEC src-redefine (1)
10944 * "If there are any element information items among the [children]
10945 * other than <annotation> then the ·actual value· of the
10946 * schemaLocation [attribute] must successfully resolve."
10947 * TODO: Ask the WG if a the location has always to resolve
10948 * here as well!
10949 */
10950 res = XML_SCHEMAP_SRC_REDEFINE;
10951 xmlSchemaCustomErr(ACTXT_CAST pctxt, res,
10952 node, NULL,
10953 "Failed to load the document '%s' for redefinition",
10954 schemaLocation, NULL);
10955 }
10956 } else {
10957 /*
10958 * Check targetNamespace sanity before parsing the new schema.
10959 * TODO: Note that we won't check further content if the
10960 * targetNamespace was bad.
10961 */
10962 if (bucket->origTargetNamespace != NULL) {
10963 /*
10964 * SPEC src-include (2.1)
10965 * "SII has a targetNamespace [attribute], and its ·actual
10966 * value· is identical to the ·actual value· of the targetNamespace
10967 * [attribute] of SIIÂ’ (which must have such an [attribute])."
10968 */
10969 if (pctxt->targetNamespace == NULL) {
10970 xmlSchemaCustomErr(ACTXT_CAST pctxt,
10971 XML_SCHEMAP_SRC_INCLUDE,
10972 node, NULL,
10973 "The target namespace of the included/redefined schema "
10974 "'%s' has to be absent, since the including/redefining "
10975 "schema has no target namespace",
10976 schemaLocation, NULL);
10977 goto exit_error;
10978 } else if (!xmlStrEqual(bucket->origTargetNamespace,
10979 pctxt->targetNamespace)) {
10980 /* TODO: Change error function. */
10981 xmlSchemaPCustomErrExt(pctxt,
10982 XML_SCHEMAP_SRC_INCLUDE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000010983 NULL, node,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000010984 "The target namespace '%s' of the included/redefined "
10985 "schema '%s' differs from '%s' of the "
10986 "including/redefining schema",
10987 bucket->origTargetNamespace, schemaLocation,
10988 pctxt->targetNamespace);
10989 goto exit_error;
10990 }
10991 } else if (pctxt->targetNamespace != NULL) {
10992 /*
10993 * Chameleons: the original target namespace will
10994 * differ from the resulting namespace.
10995 */
10996 isChameleon = 1;
10997 if (bucket->parsed &&
10998 (bucket->targetNamespace != pctxt->targetNamespace)) {
10999 /*
11000 * This is a sanity check, I dunno yet if this can happen.
11001 */
11002 PERROR_INT("xmlSchemaParseIncludeOrRedefine",
11003 "trying to use an already parsed schema for a "
11004 "different targetNamespace");
11005 return(-1);
11006 }
11007 bucket->targetNamespace = pctxt->targetNamespace;
11008 }
11009 }
11010 /*
11011 * Parse the schema.
11012 */
11013 if (bucket && (!bucket->parsed) && (bucket->doc != NULL)) {
11014 if (isChameleon) {
11015 /* TODO: Get rid of this flag on the schema itself. */
11016 if ((schema->flags & XML_SCHEMAS_INCLUDING_CONVERT_NS) == 0) {
11017 schema->flags |= XML_SCHEMAS_INCLUDING_CONVERT_NS;
11018 } else
11019 wasChameleon = 1;
11020 }
11021 xmlSchemaParseNewDoc(pctxt, schema, bucket);
11022 /* Restore chameleon flag. */
11023 if (isChameleon && (!wasChameleon))
11024 schema->flags ^= XML_SCHEMAS_INCLUDING_CONVERT_NS;
11025 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011026 /*
11027 * And now for the children...
11028 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011029 child = node->children;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011030 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011031 /*
11032 * Parse (simpleType | complexType | group | attributeGroup))*
11033 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011034 pctxt->redefined = bucket;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011035 /*
11036 * How to proceed if the redefined schema was not located?
11037 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011038 pctxt->isRedefine = 1;
11039 while (IS_SCHEMA(child, "annotation") ||
11040 IS_SCHEMA(child, "simpleType") ||
11041 IS_SCHEMA(child, "complexType") ||
11042 IS_SCHEMA(child, "group") ||
11043 IS_SCHEMA(child, "attributeGroup")) {
11044 if (IS_SCHEMA(child, "annotation")) {
11045 /*
11046 * TODO: discard or not?
11047 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011048 } else if (IS_SCHEMA(child, "simpleType")) {
11049 xmlSchemaParseSimpleType(pctxt, schema, child, 1);
11050 } else if (IS_SCHEMA(child, "complexType")) {
11051 xmlSchemaParseComplexType(pctxt, schema, child, 1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011052 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011053 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011054 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011055 xmlSchemaParseModelGroupDefinition(pctxt,
11056 schema, child);
11057 } else if (IS_SCHEMA(child, "attributeGroup")) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000011058 /* hasRedefinitions = 1; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011059 xmlSchemaParseAttributeGroupDefinition(pctxt, schema,
11060 child);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011061 }
11062 child = child->next;
11063 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011064 pctxt->redefined = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011065 pctxt->isRedefine = 0;
11066 } else {
11067 if (IS_SCHEMA(child, "annotation")) {
11068 /*
11069 * TODO: discard or not?
11070 */
11071 child = child->next;
11072 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011073 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011074 if (child != NULL) {
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011075 res = XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011076 if (type == XML_SCHEMA_SCHEMA_REDEFINE) {
11077 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011078 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011079 "(annotation | (simpleType | complexType | group | attributeGroup))*");
11080 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011081 xmlSchemaPContentErr(pctxt, res,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011082 NULL, node, child, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011083 "(annotation?)");
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011084 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011085 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011086 return(res);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011087
11088exit_error:
11089 return(pctxt->err);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011090}
11091
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011092static int
11093xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11094 xmlNodePtr node)
11095{
11096 int res;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011097#ifndef ENABLE_REDEFINE
11098 TODO
11099 return(0);
11100#endif
11101 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11102 XML_SCHEMA_SCHEMA_REDEFINE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011103 if (res != 0)
11104 return(res);
11105 return(0);
11106}
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011107
11108static int
11109xmlSchemaParseInclude(xmlSchemaParserCtxtPtr pctxt, xmlSchemaPtr schema,
11110 xmlNodePtr node)
11111{
11112 int res;
11113
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011114 res = xmlSchemaParseIncludeOrRedefine(pctxt, schema, node,
11115 XML_SCHEMA_SCHEMA_INCLUDE);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011116 if (res != 0)
11117 return(res);
11118 return(0);
11119}
11120
Daniel Veillardbd2904b2003-11-25 15:38:59 +000011121/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011122 * xmlSchemaParseModelGroup:
Daniel Veillard4255d502002-04-16 15:50:10 +000011123 * @ctxt: a schema validation context
11124 * @schema: the schema being built
11125 * @node: a subtree containing XML Schema informations
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011126 * @type: the "compositor" type
11127 * @particleNeeded: if a a model group with a particle
Daniel Veillard4255d502002-04-16 15:50:10 +000011128 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011129 * parse a XML schema Sequence definition.
11130 * Applies parts of:
11131 * Schema Representation Constraint:
11132 * Redefinition Constraints and Semantics (src-redefine)
11133 * (6.1), (6.1.1), (6.1.2)
11134 *
11135 * Schema Component Constraint:
11136 * All Group Limited (cos-all-limited) (2)
11137 * TODO: Actually this should go to component-level checks,
11138 * but is done here due to performance. Move it to an other layer
11139 * is schema construction via an API is implemented.
11140 *
Daniel Veillard4255d502002-04-16 15:50:10 +000011141 * *WARNING* this interface is highly subject to change
11142 *
William M. Bracke7091952004-05-11 15:09:58 +000011143 * Returns -1 in case of error, 0 if the declaration is improper and
Daniel Veillard4255d502002-04-16 15:50:10 +000011144 * 1 in case of success.
11145 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011146static xmlSchemaTreeItemPtr
11147xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
11148 xmlNodePtr node, xmlSchemaTypeType type,
11149 int withParticle)
Daniel Veillard4255d502002-04-16 15:50:10 +000011150{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011151 xmlSchemaModelGroupPtr item;
11152 xmlSchemaParticlePtr particle = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000011153 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011154 xmlAttrPtr attr;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011155 int min = 1, max = 1, isElemRef, hasRefs = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000011156
11157 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011158 return (NULL);
11159 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011160 * Create a model group with the given compositor.
William M. Brack2f2a6632004-08-20 23:09:47 +000011161 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000011162 item = xmlSchemaAddModelGroup(ctxt, schema, type, node);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011163 if (item == NULL)
11164 return (NULL);
11165
11166 if (withParticle) {
11167 if (type == XML_SCHEMA_TYPE_ALL) {
11168 min = xmlGetMinOccurs(ctxt, node, 0, 1, 1, "(0 | 1)");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011169 max = xmlGetMaxOccurs(ctxt, node, 1, 1, 1, "1");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011170 } else {
11171 /* choice + sequence */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011172 min = xmlGetMinOccurs(ctxt, node, 0, -1, 1, "xs:nonNegativeInteger");
11173 max = xmlGetMaxOccurs(ctxt, node, 0, UNBOUNDED, 1,
11174 "(xs:nonNegativeInteger | unbounded)");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000011175 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011176 xmlSchemaPCheckParticleCorrect_2(ctxt, NULL, node, min, max);
11177 /*
11178 * Create a particle
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011179 */
11180 particle = xmlSchemaAddParticle(ctxt, schema, node, min, max);
11181 if (particle == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011182 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011183 particle->children = (xmlSchemaTreeItemPtr) item;
11184 /*
11185 * Check for illegal attributes.
11186 */
11187 attr = node->properties;
11188 while (attr != NULL) {
11189 if (attr->ns == NULL) {
11190 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11191 (!xmlStrEqual(attr->name, BAD_CAST "maxOccurs")) &&
11192 (!xmlStrEqual(attr->name, BAD_CAST "minOccurs"))) {
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);
William M. Brack2f2a6632004-08-20 23:09:47 +000011199 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011200 attr = attr->next;
William M. Brack2f2a6632004-08-20 23:09:47 +000011201 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011202 } else {
11203 /*
11204 * Check for illegal attributes.
11205 */
11206 attr = node->properties;
11207 while (attr != NULL) {
11208 if (attr->ns == NULL) {
11209 if (!xmlStrEqual(attr->name, BAD_CAST "id")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011210 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011211 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011212 }
11213 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011214 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011215 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011216 }
11217 attr = attr->next;
11218 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011219 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011220
William M. Brack2f2a6632004-08-20 23:09:47 +000011221 /*
11222 * Extract and validate attributes.
11223 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011224 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011225 /*
11226 * And now for the children...
11227 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011228 child = node->children;
11229 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011230 item->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011231 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011232 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011233 if (type == XML_SCHEMA_TYPE_ALL) {
11234 xmlSchemaParticlePtr part, last = NULL;
11235
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011236 while (IS_SCHEMA(child, "element")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011237 part = (xmlSchemaParticlePtr) xmlSchemaParseElement(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011238 schema, child, &isElemRef, 0);
11239 /*
11240 * SPEC cos-all-limited (2)
11241 * "The {max occurs} of all the particles in the {particles}
11242 * of the ('all') group must be 0 or 1.
11243 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011244 if (part != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011245 if (isElemRef)
11246 hasRefs++;
11247 if (part->minOccurs > 1) {
11248 xmlSchemaPCustomErr(ctxt,
11249 XML_SCHEMAP_COS_ALL_LIMITED,
11250 NULL, child,
11251 "Invalid value for minOccurs (must be 0 or 1)",
11252 NULL);
11253 /* Reset to 1. */
11254 part->minOccurs = 1;
11255 }
11256 if (part->maxOccurs > 1) {
11257 xmlSchemaPCustomErr(ctxt,
11258 XML_SCHEMAP_COS_ALL_LIMITED,
11259 NULL, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011260 "Invalid value for maxOccurs (must be 0 or 1)",
11261 NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011262 /* Reset to 1. */
11263 part->maxOccurs = 1;
11264 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011265 if (last == NULL)
11266 item->children = (xmlSchemaTreeItemPtr) part;
11267 else
11268 last->next = (xmlSchemaTreeItemPtr) part;
11269 last = part;
11270 }
11271 child = child->next;
11272 }
11273 if (child != NULL) {
11274 xmlSchemaPContentErr(ctxt,
11275 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011276 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011277 "(annotation?, (annotation?, element*)");
11278 }
11279 } else {
11280 /* choice + sequence */
11281 xmlSchemaTreeItemPtr part = NULL, last = NULL;
11282
11283 while ((IS_SCHEMA(child, "element")) ||
11284 (IS_SCHEMA(child, "group")) ||
11285 (IS_SCHEMA(child, "any")) ||
11286 (IS_SCHEMA(child, "choice")) ||
11287 (IS_SCHEMA(child, "sequence"))) {
11288
11289 if (IS_SCHEMA(child, "element")) {
11290 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011291 xmlSchemaParseElement(ctxt, schema, child, &isElemRef, 0);
11292 if (part && isElemRef)
11293 hasRefs++;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011294 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011295 part =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011296 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011297 if (part != NULL)
11298 hasRefs++;
11299 /*
11300 * Handle redefinitions.
11301 */
11302 if (ctxt->isRedefine && ctxt->redef &&
11303 (ctxt->redef->item->type == XML_SCHEMA_TYPE_GROUP) &&
11304 part && part->children)
11305 {
11306 if ((xmlSchemaGetQNameRefName(part->children) ==
11307 ctxt->redef->refName) &&
11308 (xmlSchemaGetQNameRefTargetNs(part->children) ==
11309 ctxt->redef->refTargetNs))
11310 {
11311 /*
11312 * SPEC src-redefine:
11313 * (6.1) "If it has a <group> among its contents at
11314 * some level the ·actual value· of whose ref
11315 * [attribute] is the same as the ·actual value· of
11316 * its own name attribute plus target namespace, then
11317 * all of the following must be true:"
11318 * (6.1.1) "It must have exactly one such group."
11319 */
11320 if (ctxt->redefCounter != 0) {
11321 xmlChar *str = NULL;
11322
11323 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11324 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11325 "The redefining model group definition "
11326 "'%s' must not contain more than one "
11327 "reference to the redefined definition",
11328 xmlSchemaFormatQName(&str,
11329 ctxt->redef->refTargetNs,
11330 ctxt->redef->refName),
11331 NULL);
11332 FREE_AND_NULL(str)
11333 part = NULL;
11334 } else if (((WXS_PARTICLE(part))->minOccurs != 1) ||
11335 ((WXS_PARTICLE(part))->maxOccurs != 1))
11336 {
11337 xmlChar *str = NULL;
11338 /*
11339 * SPEC src-redefine:
11340 * (6.1.2) "The ·actual value· of both that
11341 * group's minOccurs and maxOccurs [attribute]
11342 * must be 1 (or ·absent·).
11343 */
11344 xmlSchemaCustomErr(ACTXT_CAST ctxt,
11345 XML_SCHEMAP_SRC_REDEFINE, child, NULL,
11346 "The redefining model group definition "
11347 "'%s' must not contain a reference to the "
11348 "redefined definition with a "
11349 "maxOccurs/minOccurs other than 1",
11350 xmlSchemaFormatQName(&str,
11351 ctxt->redef->refTargetNs,
11352 ctxt->redef->refName),
11353 NULL);
11354 FREE_AND_NULL(str)
11355 part = NULL;
11356 }
11357 ctxt->redef->reference = WXS_BASIC_CAST part;
11358 ctxt->redefCounter++;
11359 }
11360 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011361 } else if (IS_SCHEMA(child, "any")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011362 part = (xmlSchemaTreeItemPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011363 xmlSchemaParseAny(ctxt, schema, child);
11364 } else if (IS_SCHEMA(child, "choice")) {
11365 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11366 XML_SCHEMA_TYPE_CHOICE, 1);
11367 } else if (IS_SCHEMA(child, "sequence")) {
11368 part = xmlSchemaParseModelGroup(ctxt, schema, child,
11369 XML_SCHEMA_TYPE_SEQUENCE, 1);
11370 }
11371 if (part != NULL) {
11372 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011373 item->children = part;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011374 else
11375 last->next = part;
11376 last = part;
11377 }
11378 child = child->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011379 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011380 if (child != NULL) {
11381 xmlSchemaPContentErr(ctxt,
11382 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011383 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011384 "(annotation?, (element | group | choice | sequence | any)*)");
11385 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011386 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011387 if ((max == 0) && (min == 0))
11388 return (NULL);
11389 if (hasRefs) {
11390 /*
11391 * We need to resolve references.
11392 */
11393 WXS_ADD_PENDING(ctxt, item);
11394 }
11395 if (withParticle)
11396 return ((xmlSchemaTreeItemPtr) particle);
11397 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011398 return ((xmlSchemaTreeItemPtr) item);
Daniel Veillard4255d502002-04-16 15:50:10 +000011399}
11400
11401/**
11402 * xmlSchemaParseRestriction:
11403 * @ctxt: a schema validation context
11404 * @schema: the schema being built
11405 * @node: a subtree containing XML Schema informations
Daniel Veillard4255d502002-04-16 15:50:10 +000011406 *
11407 * parse a XML schema Restriction definition
11408 * *WARNING* this interface is highly subject to change
11409 *
11410 * Returns the type definition or NULL in case of error
11411 */
11412static xmlSchemaTypePtr
11413xmlSchemaParseRestriction(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011414 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011415{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011416 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011417 xmlNodePtr child = NULL;
William M. Brack2f2a6632004-08-20 23:09:47 +000011418 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011419
11420 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11421 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011422 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011423 type = ctxt->ctxtType;
11424 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011425
11426 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011427 * Check for illegal attributes.
11428 */
11429 attr = node->properties;
11430 while (attr != NULL) {
11431 if (attr->ns == NULL) {
11432 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11433 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011434 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011435 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011436 }
11437 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011438 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011439 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
William M. Brack2f2a6632004-08-20 23:09:47 +000011440 }
11441 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011442 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011443 /*
11444 * Extract and validate attributes.
11445 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011446 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
William M. Brack2f2a6632004-08-20 23:09:47 +000011447 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011448 * Attribute
William M. Brack2f2a6632004-08-20 23:09:47 +000011449 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011450 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011451 * Extract the base type. The "base" attribute is mandatory if inside
11452 * a complex type or if redefining.
11453 *
11454 * SPEC (1.2) "...otherwise (<restriction> has no <simpleType> "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011455 * among its [children]), the simple type definition which is
11456 * the {content type} of the type definition ·resolved· to by
11457 * the ·actual value· of the base [attribute]"
11458 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011459 if (xmlSchemaPValAttrQName(ctxt, schema, NULL, node, "base",
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011460 &(type->baseNs), &(type->base)) == 0)
11461 {
11462 if ((type->base == NULL) && (type->type == XML_SCHEMA_TYPE_COMPLEX)) {
11463 xmlSchemaPMissingAttrErr(ctxt,
11464 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011465 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011466 } else if ((ctxt->isRedefine) &&
11467 (type->flags & XML_SCHEMAS_TYPE_GLOBAL))
11468 {
11469 if (type->base == NULL) {
11470 xmlSchemaPMissingAttrErr(ctxt,
11471 XML_SCHEMAP_S4S_ATTR_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011472 NULL, node, "base", NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011473 } else if ((! xmlStrEqual(type->base, type->name)) ||
11474 (! xmlStrEqual(type->baseNs, type->targetNamespace)))
11475 {
11476 xmlChar *str1 = NULL, *str2 = NULL;
11477 /*
11478 * REDEFINE: SPEC src-redefine (5)
11479 * "Within the [children], each <simpleType> must have a
11480 * <restriction> among its [children] ... the ·actual value· of
11481 * whose base [attribute] must be the same as the ·actual value·
11482 * of its own name attribute plus target namespace;"
11483 */
11484 xmlSchemaPCustomErrExt(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011485 NULL, node, "This is a redefinition, but the QName "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011486 "value '%s' of the 'base' attribute does not match the "
11487 "type's designation '%s'",
11488 xmlSchemaFormatQName(&str1, type->baseNs, type->base),
11489 xmlSchemaFormatQName(&str1, type->targetNamespace,
11490 type->name), NULL);
11491 FREE_AND_NULL(str1);
11492 FREE_AND_NULL(str2);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011493 /* Avoid confusion and erase the values. */
11494 type->base = NULL;
11495 type->baseNs = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011496 }
11497 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011498 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011499 /*
11500 * And now for the children...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011501 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011502 child = node->children;
11503 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011504 /*
11505 * Add the annotation to the simple type ancestor.
11506 */
11507 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011508 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011509 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011510 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011511 if (parentType == XML_SCHEMA_TYPE_SIMPLE) {
11512 /*
11513 * Corresponds to <simpleType><restriction><simpleType>.
11514 */
William M. Brack2f2a6632004-08-20 23:09:47 +000011515 if (IS_SCHEMA(child, "simpleType")) {
11516 if (type->base != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011517 /*
William M. Brack2f2a6632004-08-20 23:09:47 +000011518 * src-restriction-base-or-simpleType
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011519 * Either the base [attribute] or the simpleType [child] of the
11520 * <restriction> element must be present, but not both.
William M. Brack2f2a6632004-08-20 23:09:47 +000011521 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011522 xmlSchemaPContentErr(ctxt,
William M. Brack2f2a6632004-08-20 23:09:47 +000011523 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011524 NULL, node, child,
William M. Brack2f2a6632004-08-20 23:09:47 +000011525 "The attribute 'base' and the <simpleType> child are "
11526 "mutually exclusive", NULL);
11527 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011528 type->baseType = (xmlSchemaTypePtr)
William M. Brack2f2a6632004-08-20 23:09:47 +000011529 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011530 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011531 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011532 } else if (type->base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011533 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011534 XML_SCHEMAP_SRC_RESTRICTION_BASE_OR_SIMPLETYPE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011535 NULL, node, child,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011536 "Either the attribute 'base' or a <simpleType> child "
11537 "must be present", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000011538 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011539 } else if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11540 /*
11541 * Corresponds to <complexType><complexContent><restriction>...
11542 * followed by:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011543 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011544 * Model groups <all>, <choice> and <sequence>.
11545 */
11546 if (IS_SCHEMA(child, "all")) {
11547 type->subtypes = (xmlSchemaTypePtr)
11548 xmlSchemaParseModelGroup(ctxt, schema, child,
11549 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011550 child = child->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011551 } else if (IS_SCHEMA(child, "choice")) {
11552 type->subtypes = (xmlSchemaTypePtr)
11553 xmlSchemaParseModelGroup(ctxt,
11554 schema, child, XML_SCHEMA_TYPE_CHOICE, 1);
11555 child = child->next;
11556 } else if (IS_SCHEMA(child, "sequence")) {
11557 type->subtypes = (xmlSchemaTypePtr)
11558 xmlSchemaParseModelGroup(ctxt, schema, child,
11559 XML_SCHEMA_TYPE_SEQUENCE, 1);
11560 child = child->next;
11561 /*
11562 * Model group reference <group>.
11563 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011564 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011565 type->subtypes = (xmlSchemaTypePtr)
11566 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011567 /*
11568 * Note that the reference will be resolved in
11569 * xmlSchemaResolveTypeReferences();
11570 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011571 child = child->next;
11572 }
11573 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011574 /*
11575 * Corresponds to <complexType><simpleContent><restriction>...
11576 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011577 * "1.1 the simple type definition corresponding to the <simpleType>
11578 * among the [children] of <restriction> if there is one;"
11579 */
11580 if (IS_SCHEMA(child, "simpleType")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011581 /*
11582 * We will store the to-be-restricted simple type in
11583 * type->contentTypeDef *temporarily*.
11584 */
11585 type->contentTypeDef = (xmlSchemaTypePtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011586 xmlSchemaParseSimpleType(ctxt, schema, child, 0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011587 if ( type->contentTypeDef == NULL)
11588 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011589 child = child->next;
11590 }
William M. Brack2f2a6632004-08-20 23:09:47 +000011591 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011592
11593 if ((parentType == XML_SCHEMA_TYPE_SIMPLE) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011594 (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011595 xmlSchemaFacetPtr facet, lastfacet = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011596 /*
11597 * Corresponds to <complexType><simpleContent><restriction>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011598 * <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011599 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011600
Daniel Veillard01fa6152004-06-29 17:04:39 +000011601 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011602 * Add the facets to the simple type ancestor.
Daniel Veillard01fa6152004-06-29 17:04:39 +000011603 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000011604 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011605 * TODO: Datatypes: 4.1.3 Constraints on XML Representation of
11606 * Simple Type Definition Schema Representation Constraint:
Daniel Veillardc0826a72004-08-10 14:17:33 +000011607 * *Single Facet Value*
11608 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000011609 while ((IS_SCHEMA(child, "minInclusive")) ||
11610 (IS_SCHEMA(child, "minExclusive")) ||
11611 (IS_SCHEMA(child, "maxInclusive")) ||
11612 (IS_SCHEMA(child, "maxExclusive")) ||
11613 (IS_SCHEMA(child, "totalDigits")) ||
11614 (IS_SCHEMA(child, "fractionDigits")) ||
11615 (IS_SCHEMA(child, "pattern")) ||
11616 (IS_SCHEMA(child, "enumeration")) ||
11617 (IS_SCHEMA(child, "whiteSpace")) ||
11618 (IS_SCHEMA(child, "length")) ||
11619 (IS_SCHEMA(child, "maxLength")) ||
11620 (IS_SCHEMA(child, "minLength"))) {
11621 facet = xmlSchemaParseFacet(ctxt, schema, child);
11622 if (facet != NULL) {
11623 if (lastfacet == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011624 type->facets = facet;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011625 else
11626 lastfacet->next = facet;
11627 lastfacet = facet;
11628 lastfacet->next = NULL;
11629 }
11630 child = child->next;
11631 }
11632 /*
11633 * Create links for derivation and validation.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011634 */
11635 if (type->facets != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000011636 xmlSchemaFacetLinkPtr facetLink, lastFacetLink = NULL;
11637
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011638 facet = type->facets;
11639 do {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011640 facetLink = (xmlSchemaFacetLinkPtr)
11641 xmlMalloc(sizeof(xmlSchemaFacetLink));
Daniel Veillard01fa6152004-06-29 17:04:39 +000011642 if (facetLink == NULL) {
William M. Brack2f2a6632004-08-20 23:09:47 +000011643 xmlSchemaPErrMemory(ctxt, "allocating a facet link", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000011644 xmlFree(facetLink);
11645 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011646 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000011647 facetLink->facet = facet;
11648 facetLink->next = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011649 if (lastFacetLink == NULL)
11650 type->facetSet = facetLink;
Daniel Veillard01fa6152004-06-29 17:04:39 +000011651 else
11652 lastFacetLink->next = facetLink;
11653 lastFacetLink = facetLink;
11654 facet = facet->next;
11655 } while (facet != NULL);
11656 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011657 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011658 if (type->type == XML_SCHEMA_TYPE_COMPLEX) {
11659 /*
11660 * Attribute uses/declarations.
11661 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011662 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11663 (xmlSchemaItemListPtr *) &(type->attrUses),
11664 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
11665 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011666 /*
11667 * Attribute wildcard.
11668 */
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011669 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011670 type->attributeWildcard =
11671 xmlSchemaParseAnyAttribute(ctxt, schema, child);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011672 child = child->next;
11673 }
11674 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011675 if (child != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011676 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11677 xmlSchemaPContentErr(ctxt,
11678 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011679 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011680 "annotation?, (group | all | choice | sequence)?, "
11681 "((attribute | attributeGroup)*, anyAttribute?))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011682 } else if (parentType == XML_SCHEMA_TYPE_SIMPLE_CONTENT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011683 xmlSchemaPContentErr(ctxt,
11684 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011685 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011686 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11687 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11688 "length | minLength | maxLength | enumeration | whiteSpace | "
11689 "pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))");
11690 } else {
11691 /* Simple type */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011692 xmlSchemaPContentErr(ctxt,
11693 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011694 NULL, node, child, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000011695 "(annotation?, (simpleType?, (minExclusive | minInclusive | "
11696 "maxExclusive | maxInclusive | totalDigits | fractionDigits | "
11697 "length | minLength | maxLength | enumeration | whiteSpace | "
11698 "pattern)*))");
11699 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011700 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011701 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011702}
11703
11704/**
11705 * xmlSchemaParseExtension:
11706 * @ctxt: a schema validation context
11707 * @schema: the schema being built
11708 * @node: a subtree containing XML Schema informations
11709 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011710 * Parses an <extension>, which is found inside a
11711 * <simpleContent> or <complexContent>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011712 * *WARNING* this interface is highly subject to change.
Daniel Veillard4255d502002-04-16 15:50:10 +000011713 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011714 * TODO: Returns the type definition or NULL in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +000011715 */
11716static xmlSchemaTypePtr
11717xmlSchemaParseExtension(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011718 xmlNodePtr node, xmlSchemaTypeType parentType)
Daniel Veillard4255d502002-04-16 15:50:10 +000011719{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011720 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011721 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011722 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011723
11724 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
11725 return (NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011726 /* Not a component, don't create it. */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011727 type = ctxt->ctxtType;
11728 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION;
Daniel Veillard4255d502002-04-16 15:50:10 +000011729
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011730 /*
11731 * Check for illegal attributes.
11732 */
11733 attr = node->properties;
11734 while (attr != NULL) {
11735 if (attr->ns == NULL) {
11736 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
11737 (!xmlStrEqual(attr->name, BAD_CAST "base"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011738 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011739 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011740 }
11741 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011742 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011743 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011744 }
11745 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011746 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011747
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011748 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011749
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011750 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011751 * Attribute "base" - mandatory.
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011752 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011753 if ((xmlSchemaPValAttrQName(ctxt, schema, NULL, node,
11754 "base", &(type->baseNs), &(type->base)) == 0) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011755 (type->base == NULL)) {
Kasimier T. Buchcik289dbb42005-03-17 12:39:31 +000011756 xmlSchemaPMissingAttrErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011757 XML_SCHEMAP_S4S_ATTR_MISSING,
11758 NULL, node, "base", NULL);
11759 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011760 /*
11761 * And now for the children...
11762 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011763 child = node->children;
11764 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011765 /*
11766 * Add the annotation to the type ancestor.
11767 */
11768 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011769 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011770 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011771 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011772 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11773 /*
11774 * Corresponds to <complexType><complexContent><extension>... and:
11775 *
11776 * Model groups <all>, <choice>, <sequence> and <group>.
11777 */
11778 if (IS_SCHEMA(child, "all")) {
11779 type->subtypes = (xmlSchemaTypePtr)
11780 xmlSchemaParseModelGroup(ctxt, schema,
11781 child, XML_SCHEMA_TYPE_ALL, 1);
11782 child = child->next;
11783 } else if (IS_SCHEMA(child, "choice")) {
11784 type->subtypes = (xmlSchemaTypePtr)
11785 xmlSchemaParseModelGroup(ctxt, schema,
11786 child, XML_SCHEMA_TYPE_CHOICE, 1);
11787 child = child->next;
11788 } else if (IS_SCHEMA(child, "sequence")) {
11789 type->subtypes = (xmlSchemaTypePtr)
11790 xmlSchemaParseModelGroup(ctxt, schema,
11791 child, XML_SCHEMA_TYPE_SEQUENCE, 1);
11792 child = child->next;
11793 } else if (IS_SCHEMA(child, "group")) {
11794 type->subtypes = (xmlSchemaTypePtr)
11795 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011796 /*
11797 * Note that the reference will be resolved in
11798 * xmlSchemaResolveTypeReferences();
11799 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011800 child = child->next;
11801 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011802 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011803 if (child != NULL) {
11804 /*
11805 * Attribute uses/declarations.
11806 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011807 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
11808 (xmlSchemaItemListPtr *) &(type->attrUses),
11809 XML_SCHEMA_TYPE_EXTENSION, NULL) == -1)
11810 return(NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011811 /*
11812 * Attribute wildcard.
11813 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011814 if (IS_SCHEMA(child, "anyAttribute")) {
11815 ctxt->ctxtType->attributeWildcard =
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000011816 xmlSchemaParseAnyAttribute(ctxt, schema, child);
11817 child = child->next;
11818 }
11819 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011820 if (child != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011821 if (parentType == XML_SCHEMA_TYPE_COMPLEX_CONTENT) {
11822 /* Complex content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011823 xmlSchemaPContentErr(ctxt,
11824 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011825 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011826 "(annotation?, ((group | all | choice | sequence)?, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011827 "((attribute | attributeGroup)*, anyAttribute?)))");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011828 } else {
11829 /* Simple content extension. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011830 xmlSchemaPContentErr(ctxt,
11831 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011832 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011833 "(annotation?, ((attribute | attributeGroup)*, "
11834 "anyAttribute?))");
11835 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011836 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011837 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000011838}
11839
11840/**
11841 * xmlSchemaParseSimpleContent:
11842 * @ctxt: a schema validation context
11843 * @schema: the schema being built
11844 * @node: a subtree containing XML Schema informations
11845 *
11846 * parse a XML schema SimpleContent definition
11847 * *WARNING* this interface is highly subject to change
11848 *
11849 * Returns the type definition or NULL in case of error
11850 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011851static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011852xmlSchemaParseSimpleContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011853 xmlSchemaPtr schema, xmlNodePtr node,
11854 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011855{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011856 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011857 xmlNodePtr child = NULL;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011858 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011859
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011860 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11861 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011862 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011863 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011864 /* Not a component, don't create it. */
11865 type = ctxt->ctxtType;
11866 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
11867 /*
11868 * Check for illegal attributes.
11869 */
11870 attr = node->properties;
11871 while (attr != NULL) {
11872 if (attr->ns == NULL) {
11873 if ((!xmlStrEqual(attr->name, BAD_CAST "id"))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011874 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011875 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011876 }
11877 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011878 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011879 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011880 }
11881 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011882 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011883
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011884 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Daniel Veillard4255d502002-04-16 15:50:10 +000011885
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011886 /*
11887 * And now for the children...
11888 */
Daniel Veillard4255d502002-04-16 15:50:10 +000011889 child = node->children;
11890 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011891 /*
11892 * Add the annotation to the complex type ancestor.
11893 */
11894 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011895 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011896 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011897 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011898 if (child == NULL) {
11899 xmlSchemaPContentErr(ctxt,
11900 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011901 NULL, node, NULL, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011902 "(annotation?, (restriction | extension))");
11903 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011904 if (child == NULL) {
11905 xmlSchemaPContentErr(ctxt,
11906 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011907 NULL, node, NULL, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011908 "(annotation?, (restriction | extension))");
11909 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011910 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011911 xmlSchemaParseRestriction(ctxt, schema, child,
11912 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011913 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011914 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011915 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011916 xmlSchemaParseExtension(ctxt, schema, child,
11917 XML_SCHEMA_TYPE_SIMPLE_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011918 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011919 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011920 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011921 if (child != NULL) {
11922 xmlSchemaPContentErr(ctxt,
11923 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011924 NULL, node, child, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011925 "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000011926 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011927 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000011928}
11929
11930/**
11931 * xmlSchemaParseComplexContent:
11932 * @ctxt: a schema validation context
11933 * @schema: the schema being built
11934 * @node: a subtree containing XML Schema informations
11935 *
11936 * parse a XML schema ComplexContent definition
11937 * *WARNING* this interface is highly subject to change
11938 *
11939 * Returns the type definition or NULL in case of error
11940 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011941static int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011942xmlSchemaParseComplexContent(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011943 xmlSchemaPtr schema, xmlNodePtr node,
11944 int *hasRestrictionOrExtension)
Daniel Veillard4255d502002-04-16 15:50:10 +000011945{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011946 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000011947 xmlNodePtr child = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011948 xmlAttrPtr attr;
Daniel Veillard4255d502002-04-16 15:50:10 +000011949
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011950 if ((ctxt == NULL) || (schema == NULL) || (node == NULL) ||
11951 (hasRestrictionOrExtension == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011952 return (-1);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011953 *hasRestrictionOrExtension = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011954 /* Not a component, don't create it. */
11955 type = ctxt->ctxtType;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011956 /*
11957 * Check for illegal attributes.
11958 */
11959 attr = node->properties;
11960 while (attr != NULL) {
11961 if (attr->ns == NULL) {
11962 if ((!xmlStrEqual(attr->name, BAD_CAST "id")) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011963 (!xmlStrEqual(attr->name, BAD_CAST "mixed")))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011964 {
11965 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011966 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011967 }
11968 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
11969 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011970 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011971 }
11972 attr = attr->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000011973 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011974
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011975 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000011976
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011977 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011978 * Set the 'mixed' on the complex type ancestor.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011979 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011980 if (xmlGetBooleanProp(ctxt, node, "mixed", 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011981 if ((type->flags & XML_SCHEMAS_TYPE_MIXED) == 0)
11982 type->flags |= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000011983 }
Daniel Veillard4255d502002-04-16 15:50:10 +000011984 child = node->children;
11985 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000011986 /*
11987 * Add the annotation to the complex type ancestor.
11988 */
11989 xmlSchemaAddAnnotation((xmlSchemaAnnotItemPtr) type,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011990 xmlSchemaParseAnnotation(ctxt, schema, child, 1));
Daniel Veillardd0c9c322003-10-10 00:49:42 +000011991 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000011992 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011993 if (child == NULL) {
11994 xmlSchemaPContentErr(ctxt,
11995 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000011996 NULL, node, NULL,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000011997 NULL, "(annotation?, (restriction | extension))");
11998 }
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000011999 if (child == NULL) {
12000 xmlSchemaPContentErr(ctxt,
12001 XML_SCHEMAP_S4S_ELEM_MISSING,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012002 NULL, node, NULL,
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012003 NULL, "(annotation?, (restriction | extension))");
12004 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012005 if (IS_SCHEMA(child, "restriction")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012006 xmlSchemaParseRestriction(ctxt, schema, child,
12007 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012008 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012009 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012010 } else if (IS_SCHEMA(child, "extension")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012011 xmlSchemaParseExtension(ctxt, schema, child,
12012 XML_SCHEMA_TYPE_COMPLEX_CONTENT);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012013 (*hasRestrictionOrExtension) = 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012014 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012015 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012016 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012017 xmlSchemaPContentErr(ctxt,
12018 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012019 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012020 NULL, "(annotation?, (restriction | extension))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012021 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012022 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000012023}
12024
12025/**
12026 * xmlSchemaParseComplexType:
12027 * @ctxt: a schema validation context
12028 * @schema: the schema being built
12029 * @node: a subtree containing XML Schema informations
12030 *
12031 * parse a XML schema Complex Type definition
12032 * *WARNING* this interface is highly subject to change
12033 *
12034 * Returns the type definition or NULL in case of error
12035 */
12036static xmlSchemaTypePtr
12037xmlSchemaParseComplexType(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
Daniel Veillard3646d642004-06-02 19:19:14 +000012038 xmlNodePtr node, int topLevel)
Daniel Veillard4255d502002-04-16 15:50:10 +000012039{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012040 xmlSchemaTypePtr type, ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012041 xmlNodePtr child = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012042 const xmlChar *name = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012043 xmlAttrPtr attr;
12044 const xmlChar *attrValue;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012045#ifdef ENABLE_NAMED_LOCALS
Daniel Veillard1a380b82004-10-21 16:00:06 +000012046 char buf[40];
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012047#endif
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012048 int final = 0, block = 0, hasRestrictionOrExtension = 0;
Daniel Veillard1a380b82004-10-21 16:00:06 +000012049
Daniel Veillard4255d502002-04-16 15:50:10 +000012050
12051 if ((ctxt == NULL) || (schema == NULL) || (node == NULL))
12052 return (NULL);
12053
Daniel Veillard01fa6152004-06-29 17:04:39 +000012054 ctxtType = ctxt->ctxtType;
12055
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012056 if (topLevel) {
12057 attr = xmlSchemaGetPropNode(node, "name");
12058 if (attr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012059 xmlSchemaPMissingAttrErr(ctxt,
12060 XML_SCHEMAP_S4S_ATTR_MISSING, NULL, node, "name", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012061 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012062 } else if (xmlSchemaPValAttrNode(ctxt, NULL, attr,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012063 xmlSchemaGetBuiltInType(XML_SCHEMAS_NCNAME), &name) != 0) {
12064 return (NULL);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012065 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012066 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012067
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012068 if (topLevel == 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012069 /*
12070 * Parse as local complex type definition.
12071 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012072#ifdef ENABLE_NAMED_LOCALS
Kasimier T. Buchcik87250a92005-01-28 15:59:53 +000012073 snprintf(buf, 39, "#CT%d", ctxt->counter++ + 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012074 type = xmlSchemaAddType(ctxt, schema,
12075 XML_SCHEMA_TYPE_COMPLEX,
12076 xmlDictLookup(ctxt->dict, (const xmlChar *)buf, -1),
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012077 ctxt->targetNamespace, node, 0);
12078#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012079 type = xmlSchemaAddType(ctxt, schema,
12080 XML_SCHEMA_TYPE_COMPLEX,
12081 NULL, ctxt->targetNamespace, node, 0);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012082#endif
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012083 if (type == NULL)
12084 return (NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012085 name = type->name;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012086 type->node = node;
12087 type->type = XML_SCHEMA_TYPE_COMPLEX;
12088 /*
12089 * TODO: We need the target namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012090 */
12091 } else {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012092 /*
12093 * Parse as global complex type definition.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012094 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012095 type = xmlSchemaAddType(ctxt, schema,
12096 XML_SCHEMA_TYPE_COMPLEX,
12097 name, ctxt->targetNamespace, node, 1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012098 if (type == NULL)
12099 return (NULL);
12100 type->node = node;
12101 type->type = XML_SCHEMA_TYPE_COMPLEX;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012102 type->flags |= XML_SCHEMAS_TYPE_GLOBAL;
Kasimier T. Buchcik31113c72005-01-13 16:57:20 +000012103 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012104 type->targetNamespace = ctxt->targetNamespace;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012105 /*
12106 * Handle attributes.
12107 */
12108 attr = node->properties;
12109 while (attr != NULL) {
12110 if (attr->ns == NULL) {
12111 if (xmlStrEqual(attr->name, BAD_CAST "id")) {
12112 /*
12113 * Attribute "id".
12114 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012115 xmlSchemaPValAttrID(ctxt, node, BAD_CAST "id");
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012116 } else if (xmlStrEqual(attr->name, BAD_CAST "mixed")) {
12117 /*
12118 * Attribute "mixed".
12119 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012120 if (xmlSchemaPGetBoolNodeValue(ctxt,
12121 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012122 type->flags |= XML_SCHEMAS_TYPE_MIXED;
12123 } else if (topLevel) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012124 /*
12125 * Attributes of global complex type definitions.
12126 */
12127 if (xmlStrEqual(attr->name, BAD_CAST "name")) {
12128 /* Pass. */
12129 } else if (xmlStrEqual(attr->name, BAD_CAST "abstract")) {
12130 /*
12131 * Attribute "abstract".
12132 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012133 if (xmlSchemaPGetBoolNodeValue(ctxt,
12134 NULL, (xmlNodePtr) attr))
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012135 type->flags |= XML_SCHEMAS_TYPE_ABSTRACT;
12136 } else if (xmlStrEqual(attr->name, BAD_CAST "final")) {
12137 /*
12138 * Attribute "final".
12139 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012140 attrValue = xmlSchemaGetNodeContent(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012141 (xmlNodePtr) attr);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012142 if (xmlSchemaPValAttrBlockFinal(attrValue,
12143 &(type->flags),
12144 -1,
12145 XML_SCHEMAS_TYPE_FINAL_EXTENSION,
12146 XML_SCHEMAS_TYPE_FINAL_RESTRICTION,
12147 -1, -1, -1) != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012148 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012149 xmlSchemaPSimpleTypeErr(ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012150 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012151 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012152 "(#all | List of (extension | restriction))",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012153 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012154 } else
12155 final = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012156 } else if (xmlStrEqual(attr->name, BAD_CAST "block")) {
12157 /*
12158 * Attribute "block".
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012159 */
12160 attrValue = xmlSchemaGetNodeContent(ctxt,
12161 (xmlNodePtr) attr);
12162 if (xmlSchemaPValAttrBlockFinal(attrValue, &(type->flags),
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012163 -1,
12164 XML_SCHEMAS_TYPE_BLOCK_EXTENSION,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012165 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012166 -1, -1, -1) != 0) {
12167 xmlSchemaPSimpleTypeErr(ctxt,
12168 XML_SCHEMAP_S4S_ATTR_INVALID_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012169 NULL, (xmlNodePtr) attr, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012170 "(#all | List of (extension | restriction)) ",
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012171 attrValue, NULL, NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012172 } else
12173 block = 1;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012174 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012175 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012176 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012177 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012178 } else {
12179 xmlSchemaPIllegalAttrErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012180 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012181 }
12182 } else if (xmlStrEqual(attr->ns->href, xmlSchemaNs)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012183 xmlSchemaPIllegalAttrErr(ctxt,
12184 XML_SCHEMAP_S4S_ATTR_NOT_ALLOWED, NULL, attr);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012185 }
12186 attr = attr->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012187 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012188 if (! block) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000012189 /*
12190 * Apply default "block" values.
12191 */
12192 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_RESTRICTION)
12193 type->flags |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
12194 if (schema->flags & XML_SCHEMAS_BLOCK_DEFAULT_EXTENSION)
12195 type->flags |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
12196 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012197 if (! final) {
12198 /*
12199 * Apply default "block" values.
12200 */
12201 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_RESTRICTION)
12202 type->flags |= XML_SCHEMAS_TYPE_FINAL_RESTRICTION;
12203 if (schema->flags & XML_SCHEMAS_FINAL_DEFAULT_EXTENSION)
12204 type->flags |= XML_SCHEMAS_TYPE_FINAL_EXTENSION;
12205 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012206 /*
12207 * And now for the children...
12208 */
Daniel Veillard4255d502002-04-16 15:50:10 +000012209 child = node->children;
12210 if (IS_SCHEMA(child, "annotation")) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012211 type->annot = xmlSchemaParseAnnotation(ctxt, schema, child, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012212 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012213 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012214 ctxt->ctxtType = type;
Daniel Veillard4255d502002-04-16 15:50:10 +000012215 if (IS_SCHEMA(child, "simpleContent")) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012216 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012217 * <complexType><simpleContent>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012218 * 3.4.3 : 2.2
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012219 * Specifying mixed='true' when the <simpleContent>
12220 * alternative is chosen has no effect
12221 */
William M. Bracke7091952004-05-11 15:09:58 +000012222 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
12223 type->flags ^= XML_SCHEMAS_TYPE_MIXED;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012224 xmlSchemaParseSimpleContent(ctxt, schema, child,
12225 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012226 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012227 } else if (IS_SCHEMA(child, "complexContent")) {
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012228 /*
12229 * <complexType><complexContent>...
12230 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012231 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012232 xmlSchemaParseComplexContent(ctxt, schema, child,
12233 &hasRestrictionOrExtension);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012234 child = child->next;
Daniel Veillard4255d502002-04-16 15:50:10 +000012235 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012236 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012237 * E.g <complexType><sequence>... or <complexType><attribute>... etc.
12238 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012239 * SPEC
12240 * "...the third alternative (neither <simpleContent> nor
12241 * <complexContent>) is chosen. This case is understood as shorthand
12242 * for complex content restricting the ·ur-type definition·, and the
12243 * details of the mappings should be modified as necessary.
12244 */
12245 type->baseType = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
12246 type->flags |= XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012247 /*
12248 * Parse model groups.
12249 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012250 if (IS_SCHEMA(child, "all")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012251 type->subtypes = (xmlSchemaTypePtr)
12252 xmlSchemaParseModelGroup(ctxt, schema, child,
12253 XML_SCHEMA_TYPE_ALL, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012254 child = child->next;
12255 } else if (IS_SCHEMA(child, "choice")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012256 type->subtypes = (xmlSchemaTypePtr)
12257 xmlSchemaParseModelGroup(ctxt, schema, child,
12258 XML_SCHEMA_TYPE_CHOICE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012259 child = child->next;
12260 } else if (IS_SCHEMA(child, "sequence")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012261 type->subtypes = (xmlSchemaTypePtr)
12262 xmlSchemaParseModelGroup(ctxt, schema, child,
12263 XML_SCHEMA_TYPE_SEQUENCE, 1);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012264 child = child->next;
12265 } else if (IS_SCHEMA(child, "group")) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012266 type->subtypes = (xmlSchemaTypePtr)
12267 xmlSchemaParseModelGroupDefRef(ctxt, schema, child);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012268 /*
12269 * Note that the reference will be resolved in
12270 * xmlSchemaResolveTypeReferences();
12271 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012272 child = child->next;
12273 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012274 /*
12275 * Parse attribute decls/refs.
12276 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012277 if (xmlSchemaParseLocalAttributes(ctxt, schema, &child,
12278 (xmlSchemaItemListPtr *) &(type->attrUses),
12279 XML_SCHEMA_TYPE_RESTRICTION, NULL) == -1)
12280 return(NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012281 /*
12282 * Parse attribute wildcard.
12283 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012284 if (IS_SCHEMA(child, "anyAttribute")) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012285 type->attributeWildcard = xmlSchemaParseAnyAttribute(ctxt, schema, child);
12286 child = child->next;
12287 }
Daniel Veillard4255d502002-04-16 15:50:10 +000012288 }
12289 if (child != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012290 xmlSchemaPContentErr(ctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012291 XML_SCHEMAP_S4S_ELEM_NOT_ALLOWED,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012292 NULL, node, child,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012293 NULL, "(annotation?, (simpleContent | complexContent | "
12294 "((group | all | choice | sequence)?, ((attribute | "
12295 "attributeGroup)*, anyAttribute?))))");
Daniel Veillard4255d502002-04-16 15:50:10 +000012296 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012297 /*
12298 * REDEFINE: SPEC src-redefine (5)
12299 */
12300 if (topLevel && ctxt->isRedefine && (! hasRestrictionOrExtension)) {
12301 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_SRC_REDEFINE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012302 NULL, node, "This is a redefinition, thus the "
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000012303 "<complexType> must have a <restriction> or <extension> "
12304 "grand-child", NULL);
12305 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000012306 ctxt->ctxtType = ctxtType;
Daniel Veillard4255d502002-04-16 15:50:10 +000012307 return (type);
12308}
12309
Daniel Veillard4255d502002-04-16 15:50:10 +000012310/************************************************************************
12311 * *
12312 * Validating using Schemas *
12313 * *
12314 ************************************************************************/
12315
12316/************************************************************************
12317 * *
12318 * Reading/Writing Schemas *
12319 * *
12320 ************************************************************************/
12321
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012322#if 0 /* Will be enabled if it is clear what options are needed. */
12323/**
12324 * xmlSchemaParserCtxtSetOptions:
12325 * @ctxt: a schema parser context
12326 * @options: a combination of xmlSchemaParserOption
12327 *
12328 * Sets the options to be used during the parse.
12329 *
12330 * Returns 0 in case of success, -1 in case of an
12331 * API error.
12332 */
12333static int
12334xmlSchemaParserCtxtSetOptions(xmlSchemaParserCtxtPtr ctxt,
12335 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012336
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012337{
12338 int i;
12339
12340 if (ctxt == NULL)
12341 return (-1);
12342 /*
12343 * WARNING: Change the start value if adding to the
12344 * xmlSchemaParseOption.
12345 */
12346 for (i = 1; i < (int) sizeof(int) * 8; i++) {
12347 if (options & 1<<i) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012348 return (-1);
12349 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012350 }
12351 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012352 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012353}
12354
12355/**
12356 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012357 * @ctxt: a schema parser context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012358 *
12359 * Returns the option combination of the parser context.
12360 */
12361static int
12362xmlSchemaParserCtxtGetOptions(xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012363
12364{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012365 if (ctxt == NULL)
12366 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012367 else
12368 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012369}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012370#endif
12371
Daniel Veillard4255d502002-04-16 15:50:10 +000012372/**
12373 * xmlSchemaNewParserCtxt:
12374 * @URL: the location of the schema
12375 *
12376 * Create an XML Schemas parse context for that file/resource expected
12377 * to contain an XML Schemas file.
12378 *
12379 * Returns the parser context or NULL in case of error
12380 */
12381xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012382xmlSchemaNewParserCtxt(const char *URL)
12383{
Daniel Veillard4255d502002-04-16 15:50:10 +000012384 xmlSchemaParserCtxtPtr ret;
12385
12386 if (URL == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012387 return (NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000012388
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012389 ret = xmlSchemaParserCtxtCreate();
12390 if (ret == NULL)
12391 return(NULL);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012392 ret->dict = xmlDictCreate();
12393 ret->URL = xmlDictLookup(ret->dict, (const xmlChar *) URL, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012394 return (ret);
12395}
12396
12397/**
Daniel Veillard6045c902002-10-09 21:13:59 +000012398 * xmlSchemaNewMemParserCtxt:
12399 * @buffer: a pointer to a char array containing the schemas
12400 * @size: the size of the array
12401 *
12402 * Create an XML Schemas parse context for that memory buffer expected
12403 * to contain an XML Schemas file.
12404 *
12405 * Returns the parser context or NULL in case of error
12406 */
12407xmlSchemaParserCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012408xmlSchemaNewMemParserCtxt(const char *buffer, int size)
12409{
Daniel Veillard6045c902002-10-09 21:13:59 +000012410 xmlSchemaParserCtxtPtr ret;
12411
12412 if ((buffer == NULL) || (size <= 0))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012413 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012414 ret = xmlSchemaParserCtxtCreate();
12415 if (ret == NULL)
12416 return(NULL);
Daniel Veillard6045c902002-10-09 21:13:59 +000012417 ret->buffer = buffer;
12418 ret->size = size;
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012419 ret->dict = xmlDictCreate();
Daniel Veillard6045c902002-10-09 21:13:59 +000012420 return (ret);
12421}
12422
12423/**
Daniel Veillard9d751502003-10-29 13:21:47 +000012424 * xmlSchemaNewDocParserCtxt:
12425 * @doc: a preparsed document tree
12426 *
12427 * Create an XML Schemas parse context for that document.
12428 * NB. The document may be modified during the parsing process.
12429 *
12430 * Returns the parser context or NULL in case of error
12431 */
12432xmlSchemaParserCtxtPtr
12433xmlSchemaNewDocParserCtxt(xmlDocPtr doc)
12434{
12435 xmlSchemaParserCtxtPtr ret;
12436
12437 if (doc == NULL)
12438 return (NULL);
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000012439 ret = xmlSchemaParserCtxtCreate();
12440 if (ret == NULL)
12441 return(NULL);
Daniel Veillard9d751502003-10-29 13:21:47 +000012442 ret->doc = doc;
William M. Brackcf9eadf2003-12-25 13:24:05 +000012443 ret->dict = xmlDictCreate();
Daniel Veillarddda22c12004-01-24 08:31:30 +000012444 /* The application has responsibility for the document */
12445 ret->preserve = 1;
Daniel Veillard9d751502003-10-29 13:21:47 +000012446
12447 return (ret);
12448}
12449
12450/**
Daniel Veillard4255d502002-04-16 15:50:10 +000012451 * xmlSchemaFreeParserCtxt:
12452 * @ctxt: the schema parser context
12453 *
12454 * Free the resources associated to the schema parser context
12455 */
12456void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012457xmlSchemaFreeParserCtxt(xmlSchemaParserCtxtPtr ctxt)
12458{
Daniel Veillard4255d502002-04-16 15:50:10 +000012459 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012460 return;
Daniel Veillarddda22c12004-01-24 08:31:30 +000012461 if (ctxt->doc != NULL && !ctxt->preserve)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012462 xmlFreeDoc(ctxt->doc);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012463 if (ctxt->vctxt != NULL) {
12464 xmlSchemaFreeValidCtxt(ctxt->vctxt);
12465 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012466 if (ctxt->ownsConstructor && (ctxt->constructor != NULL)) {
12467 xmlSchemaConstructionCtxtFree(ctxt->constructor);
12468 ctxt->constructor = NULL;
12469 ctxt->ownsConstructor = 0;
12470 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012471 if (ctxt->attrProhibs != NULL)
12472 xmlSchemaItemListFree(ctxt->attrProhibs);
Daniel Veillardbe9c6322003-11-22 20:37:51 +000012473 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000012474 xmlFree(ctxt);
12475}
12476
12477/************************************************************************
12478 * *
12479 * Building the content models *
12480 * *
12481 ************************************************************************/
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012482
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012483static void
12484xmlSchemaBuildContentModelForSubstGroup(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012485 xmlSchemaParticlePtr particle, int counter, xmlAutomataStatePtr end)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012486{
Daniel Veillarda980bef2005-07-18 21:34:03 +000012487 xmlAutomataStatePtr start, tmp;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012488 xmlSchemaElementPtr elemDecl, member;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012489 xmlSchemaSubstGroupPtr substGroup;
12490 int i;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012491
12492 elemDecl = (xmlSchemaElementPtr) particle->children;
12493 /*
12494 * Wrap the substitution group with a CHOICE.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012495 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012496 start = pctxt->state;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012497 if (end == NULL)
12498 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012499 substGroup = xmlSchemaSubstGroupGet(pctxt, elemDecl);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012500 if (substGroup == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012501 xmlSchemaPErr(pctxt, WXS_ITEM_NODE(particle),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012502 XML_SCHEMAP_INTERNAL,
12503 "Internal error: xmlSchemaBuildContentModelForSubstGroup, "
12504 "declaration is marked having a subst. group but none "
12505 "available.\n", elemDecl->name, NULL);
12506 return;
12507 }
Daniel Veillarda980bef2005-07-18 21:34:03 +000012508 if (counter >= 0) {
12509 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012510 * NOTE that we put the declaration in, even if it's abstract.
12511 * However, an error will be raised during *validation* if an element
12512 * information item shall be validated against an abstract element
12513 * declaration.
Daniel Veillarda980bef2005-07-18 21:34:03 +000012514 */
12515 tmp = xmlAutomataNewCountedTrans(pctxt->am, start, NULL, counter);
12516 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12517 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12518 /*
12519 * Add subst. group members.
12520 */
12521 for (i = 0; i < substGroup->members->nbItems; i++) {
12522 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12523 xmlAutomataNewTransition2(pctxt->am, tmp, end,
12524 member->name, member->targetNamespace, member);
12525 }
12526 } else if (particle->maxOccurs == 1) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012527 /*
12528 * NOTE that we put the declaration in, even if it's abstract,
12529 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012530 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012531 xmlAutomataNewTransition2(pctxt->am,
12532 start, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012533 elemDecl->name, elemDecl->targetNamespace, elemDecl), end);
12534 /*
12535 * Add subst. group members.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012536 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012537 for (i = 0; i < substGroup->members->nbItems; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012538 member = (xmlSchemaElementPtr) substGroup->members->items[i];
Daniel Veillarda980bef2005-07-18 21:34:03 +000012539 tmp = xmlAutomataNewOnceTrans2(pctxt->am, start, NULL,
12540 member->name, member->targetNamespace,
12541 1, 1, member);
12542 xmlAutomataNewEpsilon(pctxt->am, tmp, end);
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012543 }
12544 } else {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012545 xmlAutomataStatePtr hop;
12546 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12547 UNBOUNDED : particle->maxOccurs - 1;
12548 int minOccurs = particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
12549
12550 counter =
12551 xmlAutomataNewCounter(pctxt->am, minOccurs,
12552 maxOccurs);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012553 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012554
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012555 xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012556 xmlAutomataNewTransition2(pctxt->am,
12557 start, NULL,
12558 elemDecl->name, elemDecl->targetNamespace, elemDecl),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012559 hop);
12560 /*
Daniel Veillarda980bef2005-07-18 21:34:03 +000012561 * Add subst. group members.
12562 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012563 for (i = 0; i < substGroup->members->nbItems; i++) {
12564 member = (xmlSchemaElementPtr) substGroup->members->items[i];
12565 xmlAutomataNewEpsilon(pctxt->am,
12566 xmlAutomataNewTransition2(pctxt->am,
12567 start, NULL,
12568 member->name, member->targetNamespace, member),
12569 hop);
12570 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012571 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12572 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
12573 }
12574 if (particle->minOccurs == 0)
12575 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012576 pctxt->state = end;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012577}
12578
12579static void
12580xmlSchemaBuildContentModelForElement(xmlSchemaParserCtxtPtr ctxt,
12581 xmlSchemaParticlePtr particle)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012582{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000012583 if (((xmlSchemaElementPtr) particle->children)->flags &
12584 XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012585 /*
12586 * Substitution groups.
12587 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012588 xmlSchemaBuildContentModelForSubstGroup(ctxt, particle, -1, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012589 } else {
12590 xmlSchemaElementPtr elemDecl;
12591 xmlAutomataStatePtr start;
12592
12593 elemDecl = (xmlSchemaElementPtr) particle->children;
12594
12595 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012596 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012597 if (particle->maxOccurs == 1) {
12598 start = ctxt->state;
12599 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012600 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12601 } else if ((particle->maxOccurs >= UNBOUNDED) &&
12602 (particle->minOccurs < 2)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012603 /* Special case. */
12604 start = ctxt->state;
12605 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12606 elemDecl->name, elemDecl->targetNamespace, elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012607 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, start);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012608 } else {
12609 int counter;
12610 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12611 UNBOUNDED : particle->maxOccurs - 1;
12612 int minOccurs = particle->minOccurs < 1 ?
12613 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012614
12615 start = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012616 counter = xmlAutomataNewCounter(ctxt->am, minOccurs, maxOccurs);
12617 ctxt->state = xmlAutomataNewTransition2(ctxt->am, start, NULL,
12618 elemDecl->name, elemDecl->targetNamespace, elemDecl);
12619 xmlAutomataNewCountedTrans(ctxt->am, ctxt->state, start, counter);
12620 ctxt->state = xmlAutomataNewCounterTrans(ctxt->am, ctxt->state,
12621 NULL, counter);
12622 }
12623 if (particle->minOccurs == 0)
12624 xmlAutomataNewEpsilon(ctxt->am, start, ctxt->state);
12625 }
12626}
12627
Daniel Veillard4255d502002-04-16 15:50:10 +000012628/**
12629 * xmlSchemaBuildAContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012630 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012631 * @particle: the particle component
12632 * @name: the complex type's name whose content is being built
Daniel Veillard4255d502002-04-16 15:50:10 +000012633 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012634 * Create the automaton for the {content type} of a complex type.
12635 *
Daniel Veillard4255d502002-04-16 15:50:10 +000012636 */
12637static void
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012638xmlSchemaBuildAContentModel(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012639 xmlSchemaParticlePtr particle)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012640{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012641 if (particle == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012642 PERROR_INT("xmlSchemaBuildAContentModel", "particle is NULL");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012643 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012644 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012645 if (particle->children == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012646 /*
12647 * Just return in this case. A missing "term" of the particle
12648 * might arise due to an invalid "term" component.
12649 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012650 return;
12651 }
12652
12653 switch (particle->children->type) {
12654 case XML_SCHEMA_TYPE_ANY: {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012655 xmlAutomataStatePtr start, end;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012656 xmlSchemaWildcardPtr wild;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012657 xmlSchemaWildcardNsPtr ns;
Daniel Veillard32370232002-10-16 14:08:14 +000012658
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012659 wild = (xmlSchemaWildcardPtr) particle->children;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012660
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012661 start = pctxt->state;
12662 end = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012663
12664 if (particle->maxOccurs == 1) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012665 if (wild->any == 1) {
12666 /*
12667 * We need to add both transitions:
12668 *
12669 * 1. the {"*", "*"} for elements in a namespace.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012670 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012671 pctxt->state =
12672 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012673 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012674 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012675 /*
12676 * 2. the {"*"} for elements in no namespace.
12677 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012678 pctxt->state =
12679 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012680 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012681 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012682
12683 } else if (wild->nsSet != NULL) {
12684 ns = wild->nsSet;
12685 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012686 pctxt->state = start;
12687 pctxt->state = xmlAutomataNewTransition2(pctxt->am,
12688 pctxt->state, NULL, BAD_CAST "*", ns->value, wild);
12689 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012690 ns = ns->next;
12691 } while (ns != NULL);
12692
12693 } else if (wild->negNsSet != NULL) {
Daniel Veillard6e65e152005-08-09 11:09:52 +000012694 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
12695 start, end, BAD_CAST "*", wild->negNsSet->value,
12696 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012697 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000012698 } else {
12699 int counter;
12700 xmlAutomataStatePtr hop;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012701 int maxOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012702 particle->maxOccurs == UNBOUNDED ? UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012703 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012704 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012705
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012706 counter = xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12707 hop = xmlAutomataNewState(pctxt->am);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012708 if (wild->any == 1) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012709 pctxt->state =
12710 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012711 start, NULL, BAD_CAST "*", BAD_CAST "*", wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012712 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
12713 pctxt->state =
12714 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012715 start, NULL, BAD_CAST "*", NULL, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012716 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012717 } else if (wild->nsSet != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000012718 ns = wild->nsSet;
12719 do {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012720 pctxt->state =
12721 xmlAutomataNewTransition2(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012722 start, NULL, BAD_CAST "*", ns->value, wild);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012723 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012724 ns = ns->next;
12725 } while (ns != NULL);
12726
12727 } else if (wild->negNsSet != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012728 pctxt->state = xmlAutomataNewNegTrans(pctxt->am,
Daniel Veillard9efc4762005-07-19 14:33:55 +000012729 start, hop, BAD_CAST "*", wild->negNsSet->value,
12730 wild);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012731 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012732 xmlAutomataNewCountedTrans(pctxt->am, hop, start, counter);
12733 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardc0826a72004-08-10 14:17:33 +000012734 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012735 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012736 xmlAutomataNewEpsilon(pctxt->am, start, end);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012737 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012738 pctxt->state = end;
Daniel Veillardc0826a72004-08-10 14:17:33 +000012739 break;
12740 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012741 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012742 xmlSchemaBuildContentModelForElement(pctxt, particle);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000012743 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012744 case XML_SCHEMA_TYPE_SEQUENCE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012745 xmlSchemaTreeItemPtr sub;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012746
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012747 /*
12748 * If max and min occurances are default (1) then
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012749 * simply iterate over the particles of the <sequence>.
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012750 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012751 if ((particle->minOccurs == 1) && (particle->maxOccurs == 1)) {
12752 sub = particle->children->children;
12753 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012754 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012755 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012756 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012757 }
12758 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012759 xmlAutomataStatePtr oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012760
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012761 if (particle->maxOccurs >= UNBOUNDED) {
12762 if (particle->minOccurs > 1) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012763 xmlAutomataStatePtr tmp;
12764 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012765
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012766 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012767 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012768 oldstate = pctxt->state;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012769
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012770 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012771 particle->minOccurs - 1, UNBOUNDED);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012772
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012773 sub = particle->children->children;
12774 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012775 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012776 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012777 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012778 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012779 tmp = pctxt->state;
12780 xmlAutomataNewCountedTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012781 oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012782 pctxt->state =
12783 xmlAutomataNewCounterTrans(pctxt->am, tmp,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012784 NULL, counter);
Daniel Veillardb39bc392002-10-26 19:29:51 +000012785
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012786 } else {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012787 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Daniel Veillardafc05b62005-07-17 06:11:19 +000012788 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012789 oldstate = pctxt->state;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012790
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012791 sub = particle->children->children;
12792 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012793 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012794 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012795 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012796 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012797 xmlAutomataNewEpsilon(pctxt->am, pctxt->state,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012798 oldstate);
Daniel Veillardafc05b62005-07-17 06:11:19 +000012799 /*
12800 * epsilon needed to block previous trans from
12801 * being allowed to enter back from another
12802 * construct
12803 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012804 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
12805 pctxt->state, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012806 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012807 xmlAutomataNewEpsilon(pctxt->am,
12808 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012809 }
12810 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012811 } else if ((particle->maxOccurs > 1)
12812 || (particle->minOccurs > 1)) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012813 xmlAutomataStatePtr tmp;
12814 int counter;
Daniel Veillardb39bc392002-10-26 19:29:51 +000012815
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012816 pctxt->state = xmlAutomataNewEpsilon(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012817 oldstate, NULL);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012818 oldstate = pctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +000012819
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012820 counter = xmlAutomataNewCounter(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012821 particle->minOccurs - 1,
12822 particle->maxOccurs - 1);
Daniel Veillard4255d502002-04-16 15:50:10 +000012823
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. Buchcik041c5ff2005-07-23 14:42:34 +000012830 tmp = pctxt->state;
12831 xmlAutomataNewCountedTrans(pctxt->am,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012832 tmp, oldstate, counter);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012833 pctxt->state =
12834 xmlAutomataNewCounterTrans(pctxt->am, tmp, NULL,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012835 counter);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012836 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012837 xmlAutomataNewEpsilon(pctxt->am,
12838 oldstate, pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012839 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012840 } else {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012841 sub = particle->children->children;
12842 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012843 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012844 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012845 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012846 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012847 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012848 xmlAutomataNewEpsilon(pctxt->am, oldstate,
12849 pctxt->state);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012850 }
12851 }
12852 }
12853 break;
12854 }
12855 case XML_SCHEMA_TYPE_CHOICE:{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012856 xmlSchemaTreeItemPtr sub;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012857 xmlAutomataStatePtr start, end;
Daniel Veillardb509f152002-04-17 16:28:10 +000012858
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012859 start = pctxt->state;
12860 end = xmlAutomataNewState(pctxt->am);
Daniel Veillard7646b182002-04-20 06:41:40 +000012861
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012862 /*
12863 * iterate over the subtypes and remerge the end with an
12864 * epsilon transition
12865 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012866 if (particle->maxOccurs == 1) {
12867 sub = particle->children->children;
12868 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012869 pctxt->state = start;
12870 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012871 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012872 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, end);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012873 sub = sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012874 }
12875 } else {
12876 int counter;
Daniel Veillardafc05b62005-07-17 06:11:19 +000012877 xmlAutomataStatePtr hop, base;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012878 int maxOccurs = particle->maxOccurs == UNBOUNDED ?
12879 UNBOUNDED : particle->maxOccurs - 1;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012880 int minOccurs =
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012881 particle->minOccurs < 1 ? 0 : particle->minOccurs - 1;
Daniel Veillard7646b182002-04-20 06:41:40 +000012882
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012883 /*
12884 * use a counter to keep track of the number of transtions
12885 * which went through the choice.
12886 */
12887 counter =
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012888 xmlAutomataNewCounter(pctxt->am, minOccurs, maxOccurs);
12889 hop = xmlAutomataNewState(pctxt->am);
12890 base = xmlAutomataNewState(pctxt->am);
Daniel Veillard6231e842002-04-18 11:54:04 +000012891
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012892 sub = particle->children->children;
12893 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012894 pctxt->state = base;
12895 xmlSchemaBuildAContentModel(pctxt,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012896 (xmlSchemaParticlePtr) sub);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012897 xmlAutomataNewEpsilon(pctxt->am, pctxt->state, hop);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012898 sub = sub->next;
Daniel Veillarda980bef2005-07-18 21:34:03 +000012899 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012900 xmlAutomataNewEpsilon(pctxt->am, start, base);
12901 xmlAutomataNewCountedTrans(pctxt->am, hop, base, counter);
12902 xmlAutomataNewCounterTrans(pctxt->am, hop, end, counter);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012903 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012904 if (particle->minOccurs == 0) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012905 xmlAutomataNewEpsilon(pctxt->am, start, end);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012906 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012907 pctxt->state = end;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012908 break;
12909 }
12910 case XML_SCHEMA_TYPE_ALL:{
Kasimier T. Buchcik2c9aac02005-07-21 22:14:12 +000012911 xmlAutomataStatePtr start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012912 xmlSchemaParticlePtr sub;
12913 xmlSchemaElementPtr elemDecl;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012914 int lax;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012915
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012916 sub = (xmlSchemaParticlePtr) particle->children->children;
12917 if (sub == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012918 break;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012919 start = pctxt->state;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012920 while (sub != NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012921 pctxt->state = start;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012922
12923 elemDecl = (xmlSchemaElementPtr) sub->children;
12924 if (elemDecl == NULL) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012925 PERROR_INT("xmlSchemaBuildAContentModel",
12926 "<element> particle has no term");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012927 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012928 };
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000012929 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012930 * NOTE: The {max occurs} of all the particles in the
Kasimier T. Buchcikff858ca2005-02-18 11:37:58 +000012931 * {particles} of the group must be 0 or 1; this is
12932 * already ensured during the parse of the content of
12933 * <all>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012934 */
Daniel Veillarda980bef2005-07-18 21:34:03 +000012935 if (elemDecl->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) {
12936 int counter;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012937
Daniel Veillarda980bef2005-07-18 21:34:03 +000012938 /*
12939 * This is an abstract group, we need to share
12940 * the same counter for all the element transitions
12941 * derived from the group
12942 */
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012943 counter = xmlAutomataNewCounter(pctxt->am,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012944 sub->minOccurs, sub->maxOccurs);
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012945 xmlSchemaBuildContentModelForSubstGroup(pctxt,
12946 sub, counter, pctxt->state);
Daniel Veillarda980bef2005-07-18 21:34:03 +000012947 } else {
12948 if ((sub->minOccurs == 1) &&
12949 (sub->maxOccurs == 1)) {
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012950 xmlAutomataNewOnceTrans2(pctxt->am, pctxt->state,
12951 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012952 elemDecl->name,
12953 elemDecl->targetNamespace,
12954 1, 1, elemDecl);
12955 } else if ((sub->minOccurs == 0) &&
12956 (sub->maxOccurs == 1)) {
12957
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012958 xmlAutomataNewCountTrans2(pctxt->am, pctxt->state,
12959 pctxt->state,
Daniel Veillarda980bef2005-07-18 21:34:03 +000012960 elemDecl->name,
12961 elemDecl->targetNamespace,
12962 0,
12963 1,
12964 elemDecl);
12965 }
12966 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012967 sub = (xmlSchemaParticlePtr) sub->next;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012968 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000012969 lax = particle->minOccurs == 0;
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012970 pctxt->state =
12971 xmlAutomataNewAllTrans(pctxt->am, pctxt->state, NULL, lax);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012972 break;
12973 }
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012974 case XML_SCHEMA_TYPE_GROUP:
12975 /*
12976 * If we hit a model group definition, then this means that
12977 * it was empty, thus was not substituted for the containing
12978 * model group. Just do nothing in this case.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012979 * TODO: But the group should be substituted and not occur at
12980 * all in the content model at this point. Fix this.
Kasimier T. Buchcik041c5ff2005-07-23 14:42:34 +000012981 */
12982 break;
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012983 default:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000012984 xmlSchemaInternalErr2(ACTXT_CAST pctxt,
12985 "xmlSchemaBuildAContentModel",
12986 "found unexpected term of type '%s' in content model",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000012987 WXS_ITEM_TYPE_NAME(particle->children), NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012988 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000012989 }
12990}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000012991
Daniel Veillard4255d502002-04-16 15:50:10 +000012992/**
12993 * xmlSchemaBuildContentModel:
Daniel Veillard4255d502002-04-16 15:50:10 +000012994 * @ctxt: the schema parser context
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000012995 * @type: the complex type definition
Daniel Veillarda84c0b32003-06-02 16:58:46 +000012996 * @name: the element name
Daniel Veillard4255d502002-04-16 15:50:10 +000012997 *
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000012998 * Builds the content model of the complex type.
Daniel Veillard4255d502002-04-16 15:50:10 +000012999 */
13000static void
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013001xmlSchemaBuildContentModel(xmlSchemaTypePtr type,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013002 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013003{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013004 if ((type->type != XML_SCHEMA_TYPE_COMPLEX) ||
13005 (type->contModel != NULL) ||
13006 ((type->contentType != XML_SCHEMA_CONTENT_ELEMENTS) &&
13007 (type->contentType != XML_SCHEMA_CONTENT_MIXED)))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013008 return;
Daniel Veillarddecd64d2002-04-18 14:41:51 +000013009
13010#ifdef DEBUG_CONTENT
13011 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013012 "Building content model for %s\n", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000013013#endif
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013014 ctxt->am = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013015 ctxt->am = xmlNewAutomata();
13016 if (ctxt->am == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013017 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013018 "Cannot create automata for complex type %s\n", type->name);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013019 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000013020 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013021 ctxt->state = xmlAutomataGetInitState(ctxt->am);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013022 /*
13023 * Build the automaton.
13024 */
13025 xmlSchemaBuildAContentModel(ctxt, WXS_TYPE_PARTICLE(type));
Daniel Veillard4255d502002-04-16 15:50:10 +000013026 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013027 type->contModel = xmlAutomataCompile(ctxt->am);
13028 if (type->contModel == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013029 xmlSchemaPCustomErr(ctxt,
13030 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013031 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013032 "Failed to compile the content model", NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013033 } else if (xmlRegexpIsDeterminist(type->contModel) != 1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013034 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000013035 XML_SCHEMAP_NOT_DETERMINISTIC,
13036 /* XML_SCHEMAS_ERR_NOTDETERMINIST, */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013037 WXS_BASIC_CAST type, type->node,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013038 "The content model is not determinist", NULL);
Daniel Veillarde19fc232002-04-22 16:01:24 +000013039 } else {
Daniel Veillard118aed72002-09-24 14:13:13 +000013040#ifdef DEBUG_CONTENT_REGEXP
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013041 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013042 "Content model of %s:\n", type->name);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000013043 xmlRegexpPrint(stderr, type->contModel);
Daniel Veillard4255d502002-04-16 15:50:10 +000013044#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +000013045 }
Daniel Veillarda84c0b32003-06-02 16:58:46 +000013046 ctxt->state = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +000013047 xmlFreeAutomata(ctxt->am);
13048 ctxt->am = NULL;
13049}
13050
13051/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013052 * xmlSchemaResolveElementReferences:
Daniel Veillard4255d502002-04-16 15:50:10 +000013053 * @elem: the schema element context
13054 * @ctxt: the schema parser context
13055 *
Daniel Veillardc0826a72004-08-10 14:17:33 +000013056 * Resolves the references of an element declaration
13057 * or particle, which has an element declaration as it's
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013058 * term.
Daniel Veillard4255d502002-04-16 15:50:10 +000013059 */
13060static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013061xmlSchemaResolveElementReferences(xmlSchemaElementPtr elemDecl,
13062 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000013063{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013064 if ((ctxt == NULL) || (elemDecl == NULL) ||
13065 ((elemDecl != NULL) &&
13066 (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_RESOLVED)))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000013067 return;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013068 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_RESOLVED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013069
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013070 if ((elemDecl->subtypes == NULL) && (elemDecl->namedType != NULL)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013071 xmlSchemaTypePtr type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013072
13073 /* (type definition) ... otherwise the type definition ·resolved·
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013074 * to by the ·actual value· of the type [attribute] ...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013075 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013076 type = xmlSchemaGetType(ctxt->schema, elemDecl->namedType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013077 elemDecl->namedTypeNs);
13078 if (type == NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000013079 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000013080 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013081 WXS_BASIC_CAST elemDecl, elemDecl->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013082 "type", elemDecl->namedType, elemDecl->namedTypeNs,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013083 XML_SCHEMA_TYPE_BASIC, "type definition");
13084 } else
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013085 elemDecl->subtypes = type;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013086 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013087 if (elemDecl->substGroup != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013088 xmlSchemaElementPtr substHead;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013089
Daniel Veillardc0826a72004-08-10 14:17:33 +000013090 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013091 * FIXME TODO: Do we need a new field in _xmlSchemaElement for
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013092 * substitutionGroup?
Daniel Veillard01fa6152004-06-29 17:04:39 +000013093 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013094 substHead = xmlSchemaGetElem(ctxt->schema, elemDecl->substGroup,
13095 elemDecl->substGroupNs);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013096 if (substHead == NULL) {
13097 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013098 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013099 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013100 "substitutionGroup", elemDecl->substGroup,
13101 elemDecl->substGroupNs, XML_SCHEMA_TYPE_ELEMENT, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013102 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013103 xmlSchemaResolveElementReferences(substHead, ctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013104 /*
13105 * Set the "substitution group affiliation".
13106 * NOTE that now we use the "refDecl" field for this.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013107 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013108 WXS_SUBST_HEAD(elemDecl) = substHead;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013109 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013110 * The type definitions is set to:
13111 * SPEC "...the {type definition} of the element
13112 * declaration ·resolved· to by the ·actual value·
13113 * of the substitutionGroup [attribute], if present"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013114 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013115 if (elemDecl->subtypes == NULL)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013116 elemDecl->subtypes = substHead->subtypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013117 }
13118 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000013119 /*
13120 * SPEC "The definition of anyType serves as the default type definition
13121 * for element declarations whose XML representation does not specify one."
13122 */
13123 if ((elemDecl->subtypes == NULL) &&
13124 (elemDecl->namedType == NULL) &&
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013125 (elemDecl->substGroup == NULL))
13126 elemDecl->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
William M. Bracke7091952004-05-11 15:09:58 +000013127}
13128
13129/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013130 * xmlSchemaResolveUnionMemberTypes:
William M. Bracke7091952004-05-11 15:09:58 +000013131 * @ctxt: the schema parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013132 * @type: the schema simple type definition
William M. Bracke7091952004-05-11 15:09:58 +000013133 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013134 * Checks and builds the "member type definitions" property of the union
13135 * simple type. This handles part (1), part (2) is done in
13136 * xmlSchemaFinishMemberTypeDefinitionsProperty()
13137 *
Daniel Veillard01fa6152004-06-29 17:04:39 +000013138 * Returns -1 in case of an internal error, 0 otherwise.
William M. Bracke7091952004-05-11 15:09:58 +000013139 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013140static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013141xmlSchemaResolveUnionMemberTypes(xmlSchemaParserCtxtPtr ctxt,
13142 xmlSchemaTypePtr type)
Daniel Veillard377e1a92004-04-16 16:30:05 +000013143{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013144
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013145 xmlSchemaTypeLinkPtr link, lastLink, newLink;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013146 xmlSchemaTypePtr memberType;
Daniel Veillard377e1a92004-04-16 16:30:05 +000013147
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013148 /*
13149 * SPEC (1) "If the <union> alternative is chosen, then [Definition:]
13150 * define the explicit members as the type definitions ·resolved·
13151 * to by the items in the ·actual value· of the memberTypes [attribute],
13152 * if any, followed by the type definitions corresponding to the
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000013153 * <simpleType>s among the [children] of <union>, if any."
Daniel Veillard01fa6152004-06-29 17:04:39 +000013154 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013155 /*
13156 * Resolve references.
13157 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013158 link = type->memberTypes;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013159 lastLink = NULL;
13160 while (link != NULL) {
13161 const xmlChar *name, *nsName;
Daniel Veillardc0826a72004-08-10 14:17:33 +000013162
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013163 name = ((xmlSchemaQNameRefPtr) link->type)->name;
13164 nsName = ((xmlSchemaQNameRefPtr) link->type)->targetNamespace;
13165
13166 memberType = xmlSchemaGetType(ctxt->schema, name, nsName);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013167 if ((memberType == NULL) || (! WXS_IS_SIMPLE(memberType))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013168 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013169 WXS_BASIC_CAST type, type->node, "memberTypes",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013170 name, nsName, XML_SCHEMA_TYPE_SIMPLE, NULL);
13171 /*
13172 * Remove the member type link.
13173 */
13174 if (lastLink == NULL)
13175 type->memberTypes = link->next;
13176 else
13177 lastLink->next = link->next;
13178 newLink = link;
13179 link = link->next;
13180 xmlFree(newLink);
13181 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013182 link->type = memberType;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000013183 lastLink = link;
13184 link = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013185 }
13186 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013187 /*
13188 * Add local simple types,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013189 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000013190 memberType = type->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013191 while (memberType != NULL) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000013192 link = (xmlSchemaTypeLinkPtr) xmlMalloc(sizeof(xmlSchemaTypeLink));
13193 if (link == NULL) {
13194 xmlSchemaPErrMemory(ctxt, "allocating a type link", NULL);
13195 return (-1);
13196 }
13197 link->type = memberType;
13198 link->next = NULL;
13199 if (lastLink == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013200 type->memberTypes = link;
13201 else
Daniel Veillard01fa6152004-06-29 17:04:39 +000013202 lastLink->next = link;
13203 lastLink = link;
13204 memberType = memberType->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013205 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000013206 return (0);
Daniel Veillard377e1a92004-04-16 16:30:05 +000013207}
13208
Daniel Veillard4255d502002-04-16 15:50:10 +000013209/**
Daniel Veillard3646d642004-06-02 19:19:14 +000013210 * xmlSchemaIsDerivedFromBuiltInType:
13211 * @ctxt: the schema parser context
13212 * @type: the type definition
13213 * @valType: the value type
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013214 *
Daniel Veillard3646d642004-06-02 19:19:14 +000013215 *
13216 * Returns 1 if the type has the given value type, or
13217 * is derived from such a type.
13218 */
William M. Brack803812b2004-06-03 02:11:24 +000013219static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013220xmlSchemaIsDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
Daniel Veillard3646d642004-06-02 19:19:14 +000013221{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013222 if (type == NULL)
13223 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013224 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013225 return (0);
13226 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13227 if (type->builtInType == valType)
13228 return(1);
13229 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13230 (type->builtInType == XML_SCHEMAS_ANYTYPE))
13231 return (0);
13232 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13233 } else
13234 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
Daniel Veillard3646d642004-06-02 19:19:14 +000013235
13236 return (0);
13237}
13238
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013239#if 0
13240/**
13241 * xmlSchemaIsDerivedFromBuiltInType:
13242 * @ctxt: the schema parser context
13243 * @type: the type definition
13244 * @valType: the value type
13245 *
13246 *
13247 * Returns 1 if the type has the given value type, or
13248 * is derived from such a type.
13249 */
13250static int
13251xmlSchemaIsUserDerivedFromBuiltInType(xmlSchemaTypePtr type, int valType)
13252{
13253 if (type == NULL)
13254 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013255 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013256 return (0);
13257 if (type->type == XML_SCHEMA_TYPE_BASIC) {
13258 if (type->builtInType == valType)
13259 return(1);
13260 return (0);
13261 } else
13262 return(xmlSchemaIsDerivedFromBuiltInType(type->subtypes, valType));
13263
13264 return (0);
13265}
13266#endif
13267
13268static xmlSchemaTypePtr
13269xmlSchemaQueryBuiltInType(xmlSchemaTypePtr type)
13270{
13271 if (type == NULL)
13272 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013273 if (WXS_IS_COMPLEX(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013274 return (NULL);
13275 if (type->type == XML_SCHEMA_TYPE_BASIC)
13276 return(type);
13277 else
13278 return(xmlSchemaQueryBuiltInType(type->subtypes));
13279
13280 return (NULL);
13281}
13282
Daniel Veillard3646d642004-06-02 19:19:14 +000013283/**
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013284 * xmlSchemaGetPrimitiveType:
Daniel Veillard01fa6152004-06-29 17:04:39 +000013285 * @type: the simpleType definition
13286 *
13287 * Returns the primitive type of the given type or
13288 * NULL in case of error.
13289 */
13290static xmlSchemaTypePtr
13291xmlSchemaGetPrimitiveType(xmlSchemaTypePtr type)
13292{
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013293
Daniel Veillard01fa6152004-06-29 17:04:39 +000013294 while (type != NULL) {
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013295 /*
13296 * Note that anySimpleType is actually not a primitive type
13297 * but we need that here.
13298 */
13299 if ((type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) ||
13300 (type->flags & XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE))
Daniel Veillard01fa6152004-06-29 17:04:39 +000013301 return (type);
13302 type = type->baseType;
13303 }
13304
13305 return (NULL);
13306}
13307
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013308#if 0
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013309/**
13310 * xmlSchemaGetBuiltInTypeAncestor:
13311 * @type: the simpleType definition
13312 *
13313 * Returns the primitive type of the given type or
13314 * NULL in case of error.
13315 */
13316static xmlSchemaTypePtr
13317xmlSchemaGetBuiltInTypeAncestor(xmlSchemaTypePtr type)
13318{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013319 if (WXS_IS_LIST(type) || WXS_IS_UNION(type))
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000013320 return (0);
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013321 while (type != NULL) {
13322 if (type->type == XML_SCHEMA_TYPE_BASIC)
13323 return (type);
13324 type = type->baseType;
13325 }
13326
13327 return (NULL);
13328}
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000013329#endif
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000013330
Daniel Veillard01fa6152004-06-29 17:04:39 +000013331/**
Daniel Veillard50355f02004-06-08 17:52:16 +000013332 * xmlSchemaCloneWildcardNsConstraints:
13333 * @ctxt: the schema parser context
13334 * @dest: the destination wildcard
13335 * @source: the source wildcard
13336 *
13337 * Clones the namespace constraints of source
13338 * and assignes them to dest.
13339 * Returns -1 on internal error, 0 otherwise.
13340 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013341static int
13342xmlSchemaCloneWildcardNsConstraints(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013343 xmlSchemaWildcardPtr dest,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013344 xmlSchemaWildcardPtr source)
Daniel Veillard3646d642004-06-02 19:19:14 +000013345{
13346 xmlSchemaWildcardNsPtr cur, tmp, last;
13347
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013348 if ((source == NULL) || (dest == NULL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013349 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013350 dest->any = source->any;
Daniel Veillard3646d642004-06-02 19:19:14 +000013351 cur = source->nsSet;
13352 last = NULL;
13353 while (cur != NULL) {
13354 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
13355 if (tmp == NULL)
13356 return(-1);
13357 tmp->value = cur->value;
13358 if (last == NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013359 dest->nsSet = tmp;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013360 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013361 last->next = tmp;
13362 last = tmp;
13363 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013364 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013365 if (dest->negNsSet != NULL)
13366 xmlSchemaFreeWildcardNsSet(dest->negNsSet);
Daniel Veillard3646d642004-06-02 19:19:14 +000013367 if (source->negNsSet != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013368 dest->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13369 if (dest->negNsSet == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013370 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013371 dest->negNsSet->value = source->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013372 } else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013373 dest->negNsSet = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000013374 return(0);
13375}
13376
Daniel Veillard50355f02004-06-08 17:52:16 +000013377/**
13378 * xmlSchemaUnionWildcards:
13379 * @ctxt: the schema parser context
13380 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013381 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013382 *
13383 * Unions the namespace constraints of the given wildcards.
13384 * @completeWild will hold the resulting union.
13385 * Returns a positive error code on failure, -1 in case of an
13386 * internal error, 0 otherwise.
13387 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013388static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013389xmlSchemaUnionWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013390 xmlSchemaWildcardPtr completeWild,
13391 xmlSchemaWildcardPtr curWild)
13392{
13393 xmlSchemaWildcardNsPtr cur, curB, tmp;
13394
13395 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013396 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013397 * value.
13398 */
13399 if ((completeWild->any == curWild->any) &&
13400 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13401 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013402
Daniel Veillard3646d642004-06-02 19:19:14 +000013403 if ((completeWild->negNsSet == NULL) ||
13404 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013405
Daniel Veillard3646d642004-06-02 19:19:14 +000013406 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013407 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013408
13409 /*
13410 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013411 */
13412 cur = completeWild->nsSet;
13413 while (cur != NULL) {
13414 found = 0;
13415 curB = curWild->nsSet;
13416 while (curB != NULL) {
13417 if (cur->value == curB->value) {
13418 found = 1;
13419 break;
13420 }
13421 curB = curB->next;
13422 }
13423 if (!found)
13424 break;
13425 cur = cur->next;
13426 }
13427 if (found)
13428 return(0);
13429 } else
13430 return(0);
13431 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013432 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013433 /*
13434 * 2 If either O1 or O2 is any, then any must be the value
13435 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013436 if (completeWild->any != curWild->any) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013437 if (completeWild->any == 0) {
13438 completeWild->any = 1;
13439 if (completeWild->nsSet != NULL) {
13440 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13441 completeWild->nsSet = NULL;
13442 }
13443 if (completeWild->negNsSet != NULL) {
13444 xmlFree(completeWild->negNsSet);
13445 completeWild->negNsSet = NULL;
13446 }
13447 }
Daniel Veillard50355f02004-06-08 17:52:16 +000013448 return (0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013449 }
13450 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013451 * 3 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013452 * then the union of those sets must be the value.
13453 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013454 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013455 int found;
13456 xmlSchemaWildcardNsPtr start;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013457
Daniel Veillard3646d642004-06-02 19:19:14 +000013458 cur = curWild->nsSet;
13459 start = completeWild->nsSet;
13460 while (cur != NULL) {
13461 found = 0;
13462 curB = start;
13463 while (curB != NULL) {
13464 if (cur->value == curB->value) {
13465 found = 1;
13466 break;
13467 }
13468 curB = curB->next;
13469 }
13470 if (!found) {
13471 tmp = xmlSchemaNewWildcardNsConstraint(ctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013472 if (tmp == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013473 return (-1);
13474 tmp->value = cur->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013475 tmp->next = completeWild->nsSet;
Daniel Veillard3646d642004-06-02 19:19:14 +000013476 completeWild->nsSet = tmp;
13477 }
13478 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013479 }
13480
Daniel Veillard3646d642004-06-02 19:19:14 +000013481 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013482 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013483 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013484 * 4 If the two are negations of different values (namespace names
Daniel Veillard3646d642004-06-02 19:19:14 +000013485 * or ·absent·), then a pair of not and ·absent· must be the value.
13486 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013487 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013488 (curWild->negNsSet != NULL) &&
13489 (completeWild->negNsSet->value != curWild->negNsSet->value)) {
13490 completeWild->negNsSet->value = NULL;
Daniel Veillard50355f02004-06-08 17:52:16 +000013491
13492 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013493 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013494 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013495 * 5.
13496 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013497 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013498 (completeWild->negNsSet->value != NULL) &&
13499 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013500 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013501 (curWild->negNsSet->value != NULL) &&
13502 (completeWild->nsSet != NULL))) {
13503
13504 int nsFound, absentFound = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013505
Daniel Veillard3646d642004-06-02 19:19:14 +000013506 if (completeWild->nsSet != NULL) {
13507 cur = completeWild->nsSet;
13508 curB = curWild->negNsSet;
13509 } else {
13510 cur = curWild->nsSet;
13511 curB = completeWild->negNsSet;
13512 }
13513 nsFound = 0;
13514 while (cur != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013515 if (cur->value == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013516 absentFound = 1;
13517 else if (cur->value == curB->value)
13518 nsFound = 1;
13519 if (nsFound && absentFound)
13520 break;
13521 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013522 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013523
13524 if (nsFound && absentFound) {
13525 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013526 * 5.1 If the set S includes both the negated namespace
Daniel Veillard3646d642004-06-02 19:19:14 +000013527 * name and ·absent·, then any must be the value.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013528 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013529 completeWild->any = 1;
13530 if (completeWild->nsSet != NULL) {
13531 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13532 completeWild->nsSet = NULL;
13533 }
13534 if (completeWild->negNsSet != NULL) {
13535 xmlFree(completeWild->negNsSet);
13536 completeWild->negNsSet = NULL;
13537 }
13538 } else if (nsFound && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013539 /*
13540 * 5.2 If the set S includes the negated namespace name
13541 * but not ·absent·, then a pair of not and ·absent· must
Daniel Veillard3646d642004-06-02 19:19:14 +000013542 * be the value.
13543 */
13544 if (completeWild->nsSet != NULL) {
13545 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13546 completeWild->nsSet = NULL;
13547 }
13548 if (completeWild->negNsSet == NULL) {
13549 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13550 if (completeWild->negNsSet == NULL)
13551 return (-1);
13552 }
13553 completeWild->negNsSet->value = NULL;
13554 } else if ((!nsFound) && absentFound) {
13555 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013556 * 5.3 If the set S includes ·absent· but not the negated
Daniel Veillard3646d642004-06-02 19:19:14 +000013557 * namespace name, then the union is not expressible.
13558 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013559 xmlSchemaPErr(ctxt, completeWild->node,
Daniel Veillard3646d642004-06-02 19:19:14 +000013560 XML_SCHEMAP_UNION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013561 "The union of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013562 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013563 return(XML_SCHEMAP_UNION_NOT_EXPRESSIBLE);
Daniel Veillard3646d642004-06-02 19:19:14 +000013564 } else if ((!nsFound) && (!absentFound)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013565 /*
13566 * 5.4 If the set S does not include either the negated namespace
13567 * name or ·absent·, then whichever of O1 or O2 is a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013568 * and a namespace name must be the value.
13569 */
13570 if (completeWild->negNsSet == NULL) {
13571 if (completeWild->nsSet != NULL) {
13572 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13573 completeWild->nsSet = NULL;
13574 }
13575 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13576 if (completeWild->negNsSet == NULL)
13577 return (-1);
13578 completeWild->negNsSet->value = curWild->negNsSet->value;
13579 }
13580 }
13581 return (0);
13582 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013583 /*
Daniel Veillard3646d642004-06-02 19:19:14 +000013584 * 6.
13585 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013586 if (((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013587 (completeWild->negNsSet->value == NULL) &&
13588 (curWild->nsSet != NULL)) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013589 ((curWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013590 (curWild->negNsSet->value == NULL) &&
13591 (completeWild->nsSet != NULL))) {
13592
13593 if (completeWild->nsSet != NULL) {
13594 cur = completeWild->nsSet;
13595 } else {
13596 cur = curWild->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013597 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013598 while (cur != NULL) {
13599 if (cur->value == NULL) {
13600 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013601 * 6.1 If the set S includes ·absent·, then any must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013602 * value.
13603 */
13604 completeWild->any = 1;
13605 if (completeWild->nsSet != NULL) {
13606 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13607 completeWild->nsSet = NULL;
13608 }
13609 if (completeWild->negNsSet != NULL) {
13610 xmlFree(completeWild->negNsSet);
13611 completeWild->negNsSet = NULL;
13612 }
13613 return (0);
13614 }
13615 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013616 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013617 if (completeWild->negNsSet == NULL) {
13618 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013619 * 6.2 If the set S does not include ·absent·, then a pair of not
Daniel Veillard3646d642004-06-02 19:19:14 +000013620 * and ·absent· must be the value.
13621 */
13622 if (completeWild->nsSet != NULL) {
13623 xmlSchemaFreeWildcardNsSet(completeWild->nsSet);
13624 completeWild->nsSet = NULL;
13625 }
13626 completeWild->negNsSet = xmlSchemaNewWildcardNsConstraint(ctxt);
13627 if (completeWild->negNsSet == NULL)
13628 return (-1);
13629 completeWild->negNsSet->value = NULL;
13630 }
13631 return (0);
13632 }
13633 return (0);
13634
13635}
13636
Daniel Veillard50355f02004-06-08 17:52:16 +000013637/**
13638 * xmlSchemaIntersectWildcards:
13639 * @ctxt: the schema parser context
13640 * @completeWild: the first wildcard
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013641 * @curWild: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013642 *
13643 * Intersects the namespace constraints of the given wildcards.
13644 * @completeWild will hold the resulting intersection.
13645 * Returns a positive error code on failure, -1 in case of an
13646 * internal error, 0 otherwise.
13647 */
Daniel Veillard3646d642004-06-02 19:19:14 +000013648static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013649xmlSchemaIntersectWildcards(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard3646d642004-06-02 19:19:14 +000013650 xmlSchemaWildcardPtr completeWild,
13651 xmlSchemaWildcardPtr curWild)
13652{
William M. Brack803812b2004-06-03 02:11:24 +000013653 xmlSchemaWildcardNsPtr cur, curB, prev, tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013654
13655 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013656 * 1 If O1 and O2 are the same value, then that value must be the
Daniel Veillard3646d642004-06-02 19:19:14 +000013657 * value.
13658 */
13659 if ((completeWild->any == curWild->any) &&
13660 ((completeWild->nsSet == NULL) == (curWild->nsSet == NULL)) &&
13661 ((completeWild->negNsSet == NULL) == (curWild->negNsSet == NULL))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013662
Daniel Veillard3646d642004-06-02 19:19:14 +000013663 if ((completeWild->negNsSet == NULL) ||
13664 (completeWild->negNsSet->value == curWild->negNsSet->value)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013665
Daniel Veillard3646d642004-06-02 19:19:14 +000013666 if (completeWild->nsSet != NULL) {
William M. Brack803812b2004-06-03 02:11:24 +000013667 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013668
13669 /*
13670 * Check equality of sets.
Daniel Veillard3646d642004-06-02 19:19:14 +000013671 */
13672 cur = completeWild->nsSet;
13673 while (cur != NULL) {
13674 found = 0;
13675 curB = curWild->nsSet;
13676 while (curB != NULL) {
13677 if (cur->value == curB->value) {
13678 found = 1;
13679 break;
13680 }
13681 curB = curB->next;
13682 }
13683 if (!found)
13684 break;
13685 cur = cur->next;
13686 }
13687 if (found)
13688 return(0);
13689 } else
13690 return(0);
13691 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013692 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013693 /*
13694 * 2 If either O1 or O2 is any, then the other must be the value.
13695 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013696 if ((completeWild->any != curWild->any) && (completeWild->any)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013697 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013698 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013699 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013700 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013701 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013702 * 3 If either O1 or O2 is a pair of not and a value (a namespace
13703 * name or ·absent·) and the other is a set of (namespace names or
13704 * ·absent·), then that set, minus the negated value if it was in
Daniel Veillard3646d642004-06-02 19:19:14 +000013705 * the set, minus ·absent· if it was in the set, must be the value.
13706 */
13707 if (((completeWild->negNsSet != NULL) && (curWild->nsSet != NULL)) ||
13708 ((curWild->negNsSet != NULL) && (completeWild->nsSet != NULL))) {
13709 const xmlChar *neg;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013710
Daniel Veillard3646d642004-06-02 19:19:14 +000013711 if (completeWild->nsSet == NULL) {
13712 neg = completeWild->negNsSet->value;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013713 if (xmlSchemaCloneWildcardNsConstraints(ctxt, completeWild, curWild) == -1)
Daniel Veillard3646d642004-06-02 19:19:14 +000013714 return(-1);
13715 } else
13716 neg = curWild->negNsSet->value;
13717 /*
13718 * Remove absent and negated.
13719 */
13720 prev = NULL;
13721 cur = completeWild->nsSet;
13722 while (cur != NULL) {
13723 if (cur->value == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013724 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013725 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013726 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013727 prev->next = cur->next;
13728 xmlFree(cur);
13729 break;
13730 }
13731 prev = cur;
13732 cur = cur->next;
13733 }
13734 if (neg != NULL) {
13735 prev = NULL;
13736 cur = completeWild->nsSet;
13737 while (cur != NULL) {
13738 if (cur->value == neg) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013739 if (prev == NULL)
Daniel Veillard3646d642004-06-02 19:19:14 +000013740 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013741 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013742 prev->next = cur->next;
13743 xmlFree(cur);
13744 break;
13745 }
13746 prev = cur;
13747 cur = cur->next;
13748 }
13749 }
13750
13751 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013752 }
Daniel Veillard3646d642004-06-02 19:19:14 +000013753 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013754 * 4 If both O1 and O2 are sets of (namespace names or ·absent·),
Daniel Veillard3646d642004-06-02 19:19:14 +000013755 * then the intersection of those sets must be the value.
13756 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013757 if ((completeWild->nsSet != NULL) && (curWild->nsSet != NULL)) {
Daniel Veillard3646d642004-06-02 19:19:14 +000013758 int found;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013759
Daniel Veillard3646d642004-06-02 19:19:14 +000013760 cur = completeWild->nsSet;
13761 prev = NULL;
13762 while (cur != NULL) {
13763 found = 0;
13764 curB = curWild->nsSet;
13765 while (curB != NULL) {
13766 if (cur->value == curB->value) {
13767 found = 1;
13768 break;
13769 }
13770 curB = curB->next;
13771 }
13772 if (!found) {
13773 if (prev == NULL)
13774 completeWild->nsSet = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013775 else
Daniel Veillard3646d642004-06-02 19:19:14 +000013776 prev->next = cur->next;
13777 tmp = cur->next;
13778 xmlFree(cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013779 cur = tmp;
Daniel Veillard3646d642004-06-02 19:19:14 +000013780 continue;
13781 }
13782 prev = cur;
13783 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013784 }
13785
Daniel Veillard3646d642004-06-02 19:19:14 +000013786 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013787 }
13788 /* 5 If the two are negations of different namespace names,
Daniel Veillard3646d642004-06-02 19:19:14 +000013789 * then the intersection is not expressible
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013790 */
13791 if ((completeWild->negNsSet != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013792 (curWild->negNsSet != NULL) &&
13793 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013794 (completeWild->negNsSet->value != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013795 (curWild->negNsSet->value != NULL)) {
13796
13797 xmlSchemaPErr(ctxt, completeWild->node, XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE,
Daniel Veillardc0826a72004-08-10 14:17:33 +000013798 "The intersection of the wilcard is not expressible.\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013799 NULL, NULL);
Daniel Veillard50355f02004-06-08 17:52:16 +000013800 return(XML_SCHEMAP_INTERSECTION_NOT_EXPRESSIBLE);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013801 }
13802 /*
13803 * 6 If the one is a negation of a namespace name and the other
13804 * is a negation of ·absent·, then the one which is the negation
Daniel Veillard3646d642004-06-02 19:19:14 +000013805 * of a namespace name must be the value.
13806 */
13807 if ((completeWild->negNsSet != NULL) && (curWild->negNsSet != NULL) &&
13808 (completeWild->negNsSet->value != curWild->negNsSet->value) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013809 (completeWild->negNsSet->value == NULL)) {
13810 completeWild->negNsSet->value = curWild->negNsSet->value;
Daniel Veillard3646d642004-06-02 19:19:14 +000013811 }
13812 return(0);
13813}
13814
Daniel Veillard50355f02004-06-08 17:52:16 +000013815/**
13816 * xmlSchemaIsWildcardNsConstraintSubset:
13817 * @ctxt: the schema parser context
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013818 * @sub: the first wildcard
13819 * @super: the second wildcard
Daniel Veillard50355f02004-06-08 17:52:16 +000013820 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013821 * Schema Component Constraint: Wildcard Subset (cos-ns-subset)
13822 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013823 * Returns 0 if the namespace constraint of @sub is an intensional
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013824 * subset of @super, 1 otherwise.
Daniel Veillard50355f02004-06-08 17:52:16 +000013825 */
13826static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013827xmlSchemaCheckCOSNSSubset(xmlSchemaWildcardPtr sub,
13828 xmlSchemaWildcardPtr super)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013829{
Daniel Veillard50355f02004-06-08 17:52:16 +000013830 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013831 * 1 super must be any.
Daniel Veillard50355f02004-06-08 17:52:16 +000013832 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013833 if (super->any)
13834 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013835 /*
13836 * 2.1 sub must be a pair of not and a namespace name or ·absent·.
13837 * 2.2 super must be a pair of not and the same value.
13838 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013839 if ((sub->negNsSet != NULL) &&
13840 (super->negNsSet != NULL) &&
13841 (sub->negNsSet->value == sub->negNsSet->value))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013842 return (0);
13843 /*
13844 * 3.1 sub must be a set whose members are either namespace names or ·absent·.
Daniel Veillard50355f02004-06-08 17:52:16 +000013845 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013846 if (sub->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013847 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013848 * 3.2.1 super must be the same set or a superset thereof.
Daniel Veillard50355f02004-06-08 17:52:16 +000013849 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013850 if (super->nsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013851 xmlSchemaWildcardNsPtr cur, curB;
13852 int found = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013853
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013854 cur = sub->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013855 while (cur != NULL) {
13856 found = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013857 curB = super->nsSet;
Daniel Veillard50355f02004-06-08 17:52:16 +000013858 while (curB != NULL) {
13859 if (cur->value == curB->value) {
13860 found = 1;
13861 break;
13862 }
13863 curB = curB->next;
13864 }
13865 if (!found)
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013866 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013867 cur = cur->next;
13868 }
13869 if (found)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013870 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013871 } else if (super->negNsSet != NULL) {
Daniel Veillard50355f02004-06-08 17:52:16 +000013872 xmlSchemaWildcardNsPtr cur;
13873 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013874 * 3.2.2 super must be a pair of not and a namespace name or
13875 * ·absent· and that value must not be in sub's set.
Daniel Veillard50355f02004-06-08 17:52:16 +000013876 */
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013877 cur = sub->nsSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013878 while (cur != NULL) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013879 if (cur->value == super->negNsSet->value)
13880 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013881 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013882 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013883 return (0);
Daniel Veillard50355f02004-06-08 17:52:16 +000013884 }
13885 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000013886 return (1);
Daniel Veillard50355f02004-06-08 17:52:16 +000013887}
13888
Daniel Veillard50355f02004-06-08 17:52:16 +000013889static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013890xmlSchemaGetEffectiveValueConstraint(xmlSchemaAttributeUsePtr attruse,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013891 int *fixed,
13892 const xmlChar **value,
13893 xmlSchemaValPtr *val)
13894{
13895 *fixed = 0;
13896 *value = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013897 if (val != 0)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013898 *val = NULL;
13899
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013900 if (attruse->defValue != NULL) {
13901 *value = attruse->defValue;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013902 if (val != NULL)
13903 *val = attruse->defVal;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013904 if (attruse->flags & XML_SCHEMA_ATTR_USE_FIXED)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013905 *fixed = 1;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013906 return(1);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000013907 } else if ((attruse->attrDecl != NULL) &&
13908 (attruse->attrDecl->defValue != NULL)) {
13909 *value = attruse->attrDecl->defValue;
13910 if (val != NULL)
13911 *val = attruse->attrDecl->defVal;
13912 if (attruse->attrDecl->flags & XML_SCHEMAS_ATTR_FIXED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013913 *fixed = 1;
13914 return(1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013915 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013916 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000013917}
Daniel Veillard3646d642004-06-02 19:19:14 +000013918/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013919 * xmlSchemaCheckCVCWildcardNamespace:
Daniel Veillard3646d642004-06-02 19:19:14 +000013920 * @wild: the wildcard
13921 * @ns: the namespace
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013922 *
13923 * Validation Rule: Wildcard allows Namespace Name
13924 * (cvc-wildcard-namespace)
13925 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013926 * Returns 0 if the given namespace matches the wildcard,
13927 * 1 otherwise and -1 on API errors.
Daniel Veillard3646d642004-06-02 19:19:14 +000013928 */
13929static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013930xmlSchemaCheckCVCWildcardNamespace(xmlSchemaWildcardPtr wild,
13931 const xmlChar* ns)
Daniel Veillard3646d642004-06-02 19:19:14 +000013932{
13933 if (wild == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013934 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000013935
13936 if (wild->any)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013937 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013938 else if (wild->nsSet != NULL) {
13939 xmlSchemaWildcardNsPtr cur;
13940
13941 cur = wild->nsSet;
13942 while (cur != NULL) {
13943 if (xmlStrEqual(cur->value, ns))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013944 return(0);
Daniel Veillard3646d642004-06-02 19:19:14 +000013945 cur = cur->next;
13946 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013947 } else if ((wild->negNsSet != NULL) && (ns != NULL) &&
Daniel Veillard3646d642004-06-02 19:19:14 +000013948 (!xmlStrEqual(wild->negNsSet->value, ns)))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013949 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000013950
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013951 return(1);
13952}
13953
13954#define XML_SCHEMA_ACTION_DERIVE 0
13955#define XML_SCHEMA_ACTION_REDEFINE 1
13956
13957#define WXS_ACTION_STR(a) \
13958((a) == XML_SCHEMA_ACTION_DERIVE) ? (const xmlChar *) "base" : (const xmlChar *) "redefined"
13959
13960/*
13961* Schema Component Constraint:
13962* Derivation Valid (Restriction, Complex)
13963* derivation-ok-restriction (2) - (4)
13964*
13965* ATTENTION:
13966* In XML Schema 1.1 this will be:
13967* Validation Rule:
13968* Checking complex type subsumption (practicalSubsumption) (1, 2 and 3)
13969*
13970*/
13971static int
13972xmlSchemaCheckDerivationOKRestriction2to4(xmlSchemaParserCtxtPtr pctxt,
13973 int action,
13974 xmlSchemaBasicItemPtr item,
13975 xmlSchemaBasicItemPtr baseItem,
13976 xmlSchemaItemListPtr uses,
13977 xmlSchemaItemListPtr baseUses,
13978 xmlSchemaWildcardPtr wild,
13979 xmlSchemaWildcardPtr baseWild)
13980{
13981 xmlSchemaAttributeUsePtr cur = NULL, bcur;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000013982 int i, j, found; /* err = 0; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000013983 const xmlChar *bEffValue;
13984 int effFixed;
13985
13986 if (uses != NULL) {
13987 for (i = 0; i < uses->nbItems; i++) {
13988 cur = uses->items[i];
13989 found = 0;
13990 if (baseUses == NULL)
13991 goto not_found;
13992 for (j = 0; j < baseUses->nbItems; j++) {
13993 bcur = baseUses->items[j];
13994 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
13995 WXS_ATTRUSE_DECL_NAME(bcur)) &&
13996 (WXS_ATTRUSE_DECL_TNS(cur) ==
13997 WXS_ATTRUSE_DECL_TNS(bcur)))
13998 {
13999 /*
14000 * (2.1) "If there is an attribute use in the {attribute
14001 * uses} of the {base type definition} (call this B) whose
14002 * {attribute declaration} has the same {name} and {target
14003 * namespace}, then all of the following must be true:"
14004 */
14005 found = 1;
14006
14007 if ((cur->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
14008 (bcur->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED))
14009 {
14010 xmlChar *str = NULL;
14011 /*
14012 * (2.1.1) "one of the following must be true:"
14013 * (2.1.1.1) "B's {required} is false."
14014 * (2.1.1.2) "R's {required} is true."
14015 */
14016 xmlSchemaPAttrUseErr4(pctxt,
14017 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_1,
14018 WXS_ITEM_NODE(item), item, cur,
14019 "The 'optional' attribute use is inconsistent "
14020 "with the corresponding 'required' attribute use of "
14021 "the %s %s",
14022 WXS_ACTION_STR(action),
14023 xmlSchemaGetComponentDesignation(&str, baseItem),
14024 NULL, NULL);
14025 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014026 /* err = pctxt->err; */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014027 } else if (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014028 WXS_ATTRUSE_TYPEDEF(cur),
14029 WXS_ATTRUSE_TYPEDEF(bcur), 0) != 0)
14030 {
14031 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
14032
14033 /*
14034 * SPEC (2.1.2) "R's {attribute declaration}'s
14035 * {type definition} must be validly derived from
14036 * B's {type definition} given the empty set as
14037 * defined in Type Derivation OK (Simple) (§3.14.6)."
14038 */
14039 xmlSchemaPAttrUseErr4(pctxt,
14040 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_2,
14041 WXS_ITEM_NODE(item), item, cur,
14042 "The attribute declaration's %s "
14043 "is not validly derived from "
14044 "the corresponding %s of the "
14045 "attribute declaration in the %s %s",
14046 xmlSchemaGetComponentDesignation(&strA,
14047 WXS_ATTRUSE_TYPEDEF(cur)),
14048 xmlSchemaGetComponentDesignation(&strB,
14049 WXS_ATTRUSE_TYPEDEF(bcur)),
14050 WXS_ACTION_STR(action),
14051 xmlSchemaGetComponentDesignation(&strC, baseItem));
14052 /* xmlSchemaGetComponentDesignation(&str, baseItem), */
14053 FREE_AND_NULL(strA);
14054 FREE_AND_NULL(strB);
14055 FREE_AND_NULL(strC);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014056 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014057 } else {
14058 /*
14059 * 2.1.3 [Definition:] Let the effective value
14060 * constraint of an attribute use be its {value
14061 * constraint}, if present, otherwise its {attribute
14062 * declaration}'s {value constraint} .
14063 */
14064 xmlSchemaGetEffectiveValueConstraint(bcur,
14065 &effFixed, &bEffValue, NULL);
14066 /*
14067 * 2.1.3 ... one of the following must be true
14068 *
14069 * 2.1.3.1 B's ·effective value constraint· is
14070 * ·absent· or default.
14071 */
14072 if ((bEffValue != NULL) &&
14073 (effFixed == 1)) {
14074 const xmlChar *rEffValue = NULL;
14075
14076 xmlSchemaGetEffectiveValueConstraint(bcur,
14077 &effFixed, &rEffValue, NULL);
14078 /*
14079 * 2.1.3.2 R's ·effective value constraint· is
14080 * fixed with the same string as B's.
14081 * MAYBE TODO: Compare the computed values.
14082 * Hmm, it says "same string" so
14083 * string-equality might really be sufficient.
14084 */
14085 if ((effFixed == 0) ||
14086 (! WXS_ARE_DEFAULT_STR_EQUAL(rEffValue, bEffValue)))
14087 {
14088 xmlChar *str = NULL;
14089
14090 xmlSchemaPAttrUseErr4(pctxt,
14091 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_1_3,
14092 WXS_ITEM_NODE(item), item, cur,
14093 "The effective value constraint of the "
14094 "attribute use is inconsistent with "
14095 "its correspondent in the %s %s",
14096 WXS_ACTION_STR(action),
14097 xmlSchemaGetComponentDesignation(&str,
14098 baseItem),
14099 NULL, NULL);
14100 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014101 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014102 }
14103 }
14104 }
14105 break;
14106 }
14107 }
14108not_found:
14109 if (!found) {
14110 /*
14111 * (2.2) "otherwise the {base type definition} must have an
14112 * {attribute wildcard} and the {target namespace} of the
14113 * R's {attribute declaration} must be ·valid· with respect
14114 * to that wildcard, as defined in Wildcard allows Namespace
14115 * Name (§3.10.4)."
14116 */
14117 if ((baseWild == NULL) ||
14118 (xmlSchemaCheckCVCWildcardNamespace(baseWild,
14119 (WXS_ATTRUSE_DECL(cur))->targetNamespace) != 0))
14120 {
14121 xmlChar *str = NULL;
14122
14123 xmlSchemaPAttrUseErr4(pctxt,
14124 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_2_2,
14125 WXS_ITEM_NODE(item), item, cur,
14126 "Neither a matching attribute use, "
14127 "nor a matching wildcard exists in the %s %s",
14128 WXS_ACTION_STR(action),
14129 xmlSchemaGetComponentDesignation(&str, baseItem),
14130 NULL, NULL);
14131 FREE_AND_NULL(str);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000014132 /* err = pctxt->err; */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014133 }
14134 }
14135 }
14136 }
14137 /*
14138 * SPEC derivation-ok-restriction (3):
14139 * (3) "For each attribute use in the {attribute uses} of the {base type
14140 * definition} whose {required} is true, there must be an attribute
14141 * use with an {attribute declaration} with the same {name} and
14142 * {target namespace} as its {attribute declaration} in the {attribute
14143 * uses} of the complex type definition itself whose {required} is true.
14144 */
14145 if (baseUses != NULL) {
14146 for (j = 0; j < baseUses->nbItems; j++) {
14147 bcur = baseUses->items[j];
14148 if (bcur->occurs != XML_SCHEMAS_ATTR_USE_REQUIRED)
14149 continue;
14150 found = 0;
14151 if (uses != NULL) {
14152 for (i = 0; i < uses->nbItems; i++) {
14153 cur = uses->items[i];
14154 if ((WXS_ATTRUSE_DECL_NAME(cur) ==
14155 WXS_ATTRUSE_DECL_NAME(bcur)) &&
14156 (WXS_ATTRUSE_DECL_TNS(cur) ==
14157 WXS_ATTRUSE_DECL_TNS(bcur))) {
14158 found = 1;
14159 break;
14160 }
14161 }
14162 }
14163 if (!found) {
14164 xmlChar *strA = NULL, *strB = NULL;
14165
14166 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14167 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_3,
14168 NULL, item,
14169 "A matching attribute use for the "
14170 "'required' %s of the %s %s is missing",
14171 xmlSchemaGetComponentDesignation(&strA, bcur),
14172 WXS_ACTION_STR(action),
14173 xmlSchemaGetComponentDesignation(&strB, baseItem),
14174 NULL);
14175 FREE_AND_NULL(strA);
14176 FREE_AND_NULL(strB);
14177 }
14178 }
14179 }
14180 /*
14181 * derivation-ok-restriction (4)
14182 */
14183 if (wild != NULL) {
14184 /*
14185 * (4) "If there is an {attribute wildcard}, all of the
14186 * following must be true:"
14187 */
14188 if (baseWild == NULL) {
14189 xmlChar *str = NULL;
14190
14191 /*
14192 * (4.1) "The {base type definition} must also have one."
14193 */
14194 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14195 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_1,
14196 NULL, item,
14197 "The %s has an attribute wildcard, "
14198 "but the %s %s '%s' does not have one",
14199 WXS_ITEM_TYPE_NAME(item),
14200 WXS_ACTION_STR(action),
14201 WXS_ITEM_TYPE_NAME(baseItem),
14202 xmlSchemaGetComponentQName(&str, baseItem));
14203 FREE_AND_NULL(str);
14204 return(pctxt->err);
14205 } else if ((baseWild->any == 0) &&
14206 xmlSchemaCheckCOSNSSubset(wild, baseWild))
14207 {
14208 xmlChar *str = NULL;
14209 /*
14210 * (4.2) "The complex type definition's {attribute wildcard}'s
14211 * {namespace constraint} must be a subset of the {base type
14212 * definition}'s {attribute wildcard}'s {namespace constraint},
14213 * as defined by Wildcard Subset (§3.10.6)."
14214 */
14215 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14216 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_2,
14217 NULL, item,
14218 "The attribute wildcard is not a valid "
14219 "subset of the wildcard in the %s %s '%s'",
14220 WXS_ACTION_STR(action),
14221 WXS_ITEM_TYPE_NAME(baseItem),
14222 xmlSchemaGetComponentQName(&str, baseItem),
14223 NULL);
14224 FREE_AND_NULL(str);
14225 return(pctxt->err);
14226 }
14227 /* 4.3 Unless the {base type definition} is the ·ur-type
14228 * definition·, the complex type definition's {attribute
14229 * wildcard}'s {process contents} must be identical to or
14230 * stronger than the {base type definition}'s {attribute
14231 * wildcard}'s {process contents}, where strict is stronger
14232 * than lax is stronger than skip.
14233 */
14234 if ((! WXS_IS_ANYTYPE(baseItem)) &&
14235 (wild->processContents < baseWild->processContents)) {
14236 xmlChar *str = NULL;
14237 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
14238 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_4_3,
14239 NULL, baseItem,
14240 "The {process contents} of the attribute wildcard is "
14241 "weaker than the one in the %s %s '%s'",
14242 WXS_ACTION_STR(action),
14243 WXS_ITEM_TYPE_NAME(baseItem),
14244 xmlSchemaGetComponentQName(&str, baseItem),
14245 NULL);
14246 FREE_AND_NULL(str)
14247 return(pctxt->err);
14248 }
14249 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014250 return(0);
14251}
14252
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014253
14254static int
14255xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
14256 xmlSchemaBasicItemPtr item,
14257 xmlSchemaWildcardPtr *completeWild,
14258 xmlSchemaItemListPtr list,
14259 xmlSchemaItemListPtr prohibs);
Daniel Veillard3646d642004-06-02 19:19:14 +000014260/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014261 * xmlSchemaFixupTypeAttributeUses:
Daniel Veillard3646d642004-06-02 19:19:14 +000014262 * @ctxt: the schema parser context
14263 * @type: the complex type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014264 *
Daniel Veillard3646d642004-06-02 19:19:14 +000014265 *
14266 * Builds the wildcard and the attribute uses on the given complex type.
14267 * Returns -1 if an internal error occurs, 0 otherwise.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014268 *
14269 * ATTENTION TODO: Experimantally this uses pointer comparisons for
14270 * strings, so recheck this if we start to hardcode some schemata, since
14271 * they might not be in the same dict.
14272 * NOTE: It is allowed to "extend" the xs:anyType type.
Daniel Veillard3646d642004-06-02 19:19:14 +000014273 */
14274static int
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014275xmlSchemaFixupTypeAttributeUses(xmlSchemaParserCtxtPtr pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014276 xmlSchemaTypePtr type)
Daniel Veillard3646d642004-06-02 19:19:14 +000014277{
14278 xmlSchemaTypePtr baseType = NULL;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014279 xmlSchemaAttributeUsePtr use;
14280 xmlSchemaItemListPtr uses, baseUses, prohibs = NULL;
Daniel Veillard3646d642004-06-02 19:19:14 +000014281
Daniel Veillard01fa6152004-06-29 17:04:39 +000014282 if (type->baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014283 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014284 "no base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000014285 return (-1);
14286 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014287 baseType = type->baseType;
14288 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014289 if (xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014290 return(-1);
14291
14292 uses = type->attrUses;
14293 baseUses = baseType->attrUses;
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014294 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014295 * Expand attribute group references. And build the 'complete'
14296 * wildcard, i.e. intersect multiple wildcards.
14297 * Move attribute prohibitions into a separate list.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014298 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014299 if (uses != NULL) {
14300 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014301 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014302 * This one will transfer all attr. prohibitions
14303 * into pctxt->attrProhibs.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014304 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014305 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14306 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14307 pctxt->attrProhibs) == -1)
14308 {
14309 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14310 "failed to expand attributes");
14311 }
14312 if (pctxt->attrProhibs->nbItems != 0)
14313 prohibs = pctxt->attrProhibs;
14314 } else {
14315 if (xmlSchemaExpandAttributeGroupRefs(pctxt,
14316 WXS_BASIC_CAST type, &(type->attributeWildcard), uses,
14317 NULL) == -1)
14318 {
14319 PERROR_INT("xmlSchemaFixupTypeAttributeUses",
14320 "failed to expand attributes");
14321 }
14322 }
14323 }
14324 /*
14325 * Inherit the attribute uses of the base type.
14326 */
14327 if (baseUses != NULL) {
14328 int i, j;
14329 xmlSchemaAttributeUseProhibPtr pro;
14330
14331 if (WXS_IS_RESTRICTION(type)) {
14332 int usesCount;
14333 xmlSchemaAttributeUsePtr tmp;
14334
14335 if (uses != NULL)
14336 usesCount = uses->nbItems;
14337 else
14338 usesCount = 0;
14339
14340 /* Restriction. */
14341 for (i = 0; i < baseUses->nbItems; i++) {
14342 use = baseUses->items[i];
14343 if (prohibs) {
14344 /*
14345 * Filter out prohibited uses.
14346 */
14347 for (j = 0; j < prohibs->nbItems; j++) {
14348 pro = prohibs->items[j];
14349 if ((WXS_ATTRUSE_DECL_NAME(use) == pro->name) &&
14350 (WXS_ATTRUSE_DECL_TNS(use) ==
14351 pro->targetNamespace))
14352 {
14353 goto inherit_next;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014354 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014355 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014356 }
14357 if (usesCount) {
14358 /*
14359 * Filter out existing uses.
14360 */
14361 for (j = 0; j < usesCount; j++) {
14362 tmp = uses->items[j];
14363 if ((WXS_ATTRUSE_DECL_NAME(use) ==
14364 WXS_ATTRUSE_DECL_NAME(tmp)) &&
14365 (WXS_ATTRUSE_DECL_TNS(use) ==
14366 WXS_ATTRUSE_DECL_TNS(tmp)))
14367 {
14368 goto inherit_next;
14369 }
14370 }
14371 }
14372 if (uses == NULL) {
14373 type->attrUses = xmlSchemaItemListCreate();
14374 if (type->attrUses == NULL)
14375 goto exit_failure;
14376 uses = type->attrUses;
14377 }
14378 xmlSchemaItemListAddSize(uses, 2, use);
14379inherit_next: {}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014380 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014381 } else {
14382 /* Extension. */
14383 for (i = 0; i < baseUses->nbItems; i++) {
14384 use = baseUses->items[i];
14385 if (uses == NULL) {
14386 type->attrUses = xmlSchemaItemListCreate();
14387 if (type->attrUses == NULL)
14388 goto exit_failure;
14389 uses = type->attrUses;
14390 }
14391 xmlSchemaItemListAddSize(uses, baseUses->nbItems, use);
14392 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014393 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014394 }
14395 /*
14396 * Shrink attr. uses.
14397 */
14398 if (uses) {
14399 if (uses->nbItems == 0) {
14400 xmlSchemaItemListFree(uses);
14401 type->attrUses = NULL;
14402 }
14403 /*
14404 * TODO: We could shrink the size of the array
14405 * to fit the actual number of items.
14406 */
14407 }
14408 /*
14409 * Compute the complete wildcard.
14410 */
14411 if (WXS_IS_EXTENSION(type)) {
14412 if (baseType->attributeWildcard != NULL) {
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014413 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014414 * (3.2.2.1) "If the ·base wildcard· is non-·absent·, then
14415 * the appropriate case among the following:"
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014416 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014417 if (type->attributeWildcard != NULL) {
14418 /*
14419 * Union the complete wildcard with the base wildcard.
14420 * SPEC {attribute wildcard}
14421 * (3.2.2.1.2) "otherwise a wildcard whose {process contents}
14422 * and {annotation} are those of the ·complete wildcard·,
14423 * and whose {namespace constraint} is the intensional union
14424 * of the {namespace constraint} of the ·complete wildcard·
14425 * and of the ·base wildcard·, as defined in Attribute
14426 * Wildcard Union (§3.10.6)."
14427 */
14428 if (xmlSchemaUnionWildcards(pctxt, type->attributeWildcard,
14429 baseType->attributeWildcard) == -1)
14430 goto exit_failure;
14431 } else {
14432 /*
14433 * (3.2.2.1.1) "If the ·complete wildcard· is ·absent·,
14434 * then the ·base wildcard·."
14435 */
14436 type->attributeWildcard = baseType->attributeWildcard;
14437 }
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014438 } else {
14439 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014440 * (3.2.2.2) "otherwise (the ·base wildcard· is ·absent·) the
14441 * ·complete wildcard"
14442 * NOOP
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014443 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014444 }
Daniel Veillard3646d642004-06-02 19:19:14 +000014445 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014446 /*
14447 * SPEC {attribute wildcard}
14448 * (3.1) "If the <restriction> alternative is chosen, then the
14449 * ·complete wildcard·;"
14450 * NOOP
14451 */
Daniel Veillard50355f02004-06-08 17:52:16 +000014452 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014453
Daniel Veillard3646d642004-06-02 19:19:14 +000014454 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014455
14456exit_failure:
14457 return(-1);
Daniel Veillard3646d642004-06-02 19:19:14 +000014458}
14459
14460/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000014461 * xmlSchemaTypeFinalContains:
14462 * @schema: the schema
14463 * @type: the type definition
14464 * @final: the final
14465 *
14466 * Evaluates if a type definition contains the given "final".
14467 * This does take "finalDefault" into account as well.
14468 *
14469 * Returns 1 if the type does containt the given "final",
14470 * 0 otherwise.
14471 */
14472static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014473xmlSchemaTypeFinalContains(xmlSchemaTypePtr type, int final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014474{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014475 if (type == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014476 return (0);
14477 if (type->flags & final)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014478 return (1);
14479 else
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014480 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014481}
14482
14483/**
14484 * xmlSchemaGetUnionSimpleTypeMemberTypes:
14485 * @type: the Union Simple Type
14486 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014487 * Returns a list of member types of @type if existing,
Daniel Veillard01fa6152004-06-29 17:04:39 +000014488 * returns NULL otherwise.
14489 */
Daniel Veillardb7c6ac42004-06-29 22:01:27 +000014490static xmlSchemaTypeLinkPtr
Daniel Veillard01fa6152004-06-29 17:04:39 +000014491xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type)
14492{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014493 while ((type != NULL) && (type->type == XML_SCHEMA_TYPE_SIMPLE)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014494 if (type->memberTypes != NULL)
14495 return (type->memberTypes);
14496 else
14497 type = type->baseType;
14498 }
14499 return (NULL);
14500}
14501
14502/**
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014503 * xmlSchemaGetParticleTotalRangeMin:
14504 * @particle: the particle
14505 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014506 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014507 * (all and sequence) + (choice)
14508 *
14509 * Returns the minimun Effective Total Range.
14510 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014511static int
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014512xmlSchemaGetParticleTotalRangeMin(xmlSchemaParticlePtr particle)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014513{
14514 if ((particle->children == NULL) ||
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014515 (particle->minOccurs == 0))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014516 return (0);
14517 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014518 int min = -1, cur;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014519 xmlSchemaParticlePtr part =
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014520 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014521
14522 if (part == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014523 return (0);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014524 while (part != NULL) {
14525 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14526 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014527 cur = part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014528 else
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014529 cur = xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014530 if (cur == 0)
14531 return (0);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014532 if ((min > cur) || (min == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014533 min = cur;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014534 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014535 }
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014536 return (particle->minOccurs * min);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014537 } else {
14538 /* <all> and <sequence> */
14539 int sum = 0;
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014540 xmlSchemaParticlePtr part =
14541 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014542
14543 if (part == NULL)
14544 return (0);
14545 do {
14546 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14547 (part->children->type == XML_SCHEMA_TYPE_ANY))
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014548 sum += part->minOccurs;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014549 else
14550 sum += xmlSchemaGetParticleTotalRangeMin(part);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014551 part = (xmlSchemaParticlePtr) part->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014552 } while (part != NULL);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014553 return (particle->minOccurs * sum);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014554 }
14555}
14556
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014557/**
14558 * xmlSchemaGetParticleTotalRangeMax:
14559 * @particle: the particle
14560 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014561 * Schema Component Constraint: Effective Total Range
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014562 * (all and sequence) + (choice)
14563 *
14564 * Returns the maximum Effective Total Range.
14565 */
14566static int
14567xmlSchemaGetParticleTotalRangeMax(xmlSchemaParticlePtr particle)
14568{
14569 if ((particle->children == NULL) ||
14570 (particle->children->children == NULL))
14571 return (0);
14572 if (particle->children->type == XML_SCHEMA_TYPE_CHOICE) {
14573 int max = -1, cur;
14574 xmlSchemaParticlePtr part =
14575 (xmlSchemaParticlePtr) particle->children->children;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014576
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014577 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14578 if (part->children == NULL)
14579 continue;
14580 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14581 (part->children->type == XML_SCHEMA_TYPE_ANY))
14582 cur = part->maxOccurs;
14583 else
14584 cur = xmlSchemaGetParticleTotalRangeMax(part);
14585 if (cur == UNBOUNDED)
14586 return (UNBOUNDED);
14587 if ((max < cur) || (max == -1))
14588 max = cur;
14589 }
14590 /* TODO: Handle overflows? */
14591 return (particle->maxOccurs * max);
14592 } else {
14593 /* <all> and <sequence> */
14594 int sum = 0, cur;
14595 xmlSchemaParticlePtr part =
14596 (xmlSchemaParticlePtr) particle->children->children;
14597
14598 for (; part != NULL; part = (xmlSchemaParticlePtr) part->next) {
14599 if (part->children == NULL)
14600 continue;
14601 if ((part->children->type == XML_SCHEMA_TYPE_ELEMENT) ||
14602 (part->children->type == XML_SCHEMA_TYPE_ANY))
14603 cur = part->maxOccurs;
14604 else
14605 cur = xmlSchemaGetParticleTotalRangeMax(part);
14606 if (cur == UNBOUNDED)
14607 return (UNBOUNDED);
14608 if ((cur > 0) && (particle->maxOccurs == UNBOUNDED))
14609 return (UNBOUNDED);
14610 sum += cur;
14611 }
14612 /* TODO: Handle overflows? */
14613 return (particle->maxOccurs * sum);
14614 }
14615}
14616
14617/**
14618 * xmlSchemaIsParticleEmptiable:
14619 * @particle: the particle
14620 *
14621 * Schema Component Constraint: Particle Emptiable
14622 * Checks whether the given particle is emptiable.
14623 *
14624 * Returns 1 if emptiable, 0 otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014625 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014626static int
14627xmlSchemaIsParticleEmptiable(xmlSchemaParticlePtr particle)
14628{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014629 /*
14630 * SPEC (1) "Its {min occurs} is 0."
14631 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014632 if ((particle == NULL) || (particle->minOccurs == 0) ||
14633 (particle->children == NULL))
14634 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014635 /*
14636 * SPEC (2) "Its {term} is a group and the minimum part of the
14637 * effective total range of that group, [...] is 0."
14638 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014639 if (WXS_IS_MODEL_GROUP(particle->children)) {
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014640 if (xmlSchemaGetParticleTotalRangeMin(particle) == 0)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014641 return (1);
14642 }
14643 return (0);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014644}
14645
14646/**
14647 * xmlSchemaCheckCOSSTDerivedOK:
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014648 * @actxt: a context
Daniel Veillard01fa6152004-06-29 17:04:39 +000014649 * @type: the derived simple type definition
14650 * @baseType: the base type definition
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014651 * @subset: the subset of ('restriction', ect.)
Daniel Veillard01fa6152004-06-29 17:04:39 +000014652 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014653 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000014654 * Type Derivation OK (Simple) (cos-st-derived-OK)
14655 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014656 * Checks wheter @type can be validly
Daniel Veillard01fa6152004-06-29 17:04:39 +000014657 * derived from @baseType.
14658 *
14659 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014660 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014661static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014662xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014663 xmlSchemaTypePtr type,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014664 xmlSchemaTypePtr baseType,
14665 int subset)
14666{
Daniel Veillard01fa6152004-06-29 17:04:39 +000014667 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014668 * 1 They are the same type definition.
14669 * TODO: The identy check might have to be more complex than this.
14670 */
14671 if (type == baseType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014672 return (0);
14673 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000014674 * 2.1 restriction is not in the subset, or in the {final}
14675 * of its own {base type definition};
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014676 *
14677 * NOTE that this will be used also via "xsi:type".
14678 *
14679 * TODO: Revise this, it looks strange. How can the "type"
14680 * not be fixed or *in* fixing?
Daniel Veillard01fa6152004-06-29 17:04:39 +000014681 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014682 if (WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014683 if (xmlSchemaTypeFixup(type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014684 return(-1);
14685 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014686 if (xmlSchemaTypeFixup(baseType, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014687 return(-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014688 if ((subset & SUBSET_RESTRICTION) ||
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014689 (xmlSchemaTypeFinalContains(type->baseType,
14690 XML_SCHEMAS_TYPE_FINAL_RESTRICTION))) {
14691 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000014692 }
14693 /* 2.2 */
14694 if (type->baseType == baseType) {
14695 /*
14696 * 2.2.1 D's ·base type definition· is B.
14697 */
14698 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014699 }
14700 /*
14701 * 2.2.2 D's ·base type definition· is not the ·ur-type definition·
14702 * and is validly derived from B given the subset, as defined by this
14703 * constraint.
Daniel Veillard01fa6152004-06-29 17:04:39 +000014704 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014705 if ((! WXS_IS_ANYTYPE(type->baseType)) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014706 (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000014707 baseType, subset) == 0)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014708 return (0);
14709 }
14710 /*
14711 * 2.2.3 D's {variety} is list or union and B is the ·simple ur-type
Daniel Veillard01fa6152004-06-29 17:04:39 +000014712 * definition·.
14713 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014714 if (WXS_IS_ANY_SIMPLE_TYPE(baseType) &&
14715 (WXS_IS_LIST(type) || WXS_IS_UNION(type))) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014716 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014717 }
14718 /*
14719 * 2.2.4 B's {variety} is union and D is validly derived from a type
14720 * definition in B's {member type definitions} given the subset, as
Daniel Veillard01fa6152004-06-29 17:04:39 +000014721 * defined by this constraint.
14722 *
14723 * NOTE: This seems not to involve built-in types, since there is no
14724 * built-in Union Simple Type.
14725 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014726 if (WXS_IS_UNION(baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000014727 xmlSchemaTypeLinkPtr cur;
14728
14729 cur = baseType->memberTypes;
14730 while (cur != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014731 if (WXS_IS_TYPE_NOT_FIXED(cur->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014732 if (xmlSchemaTypeFixup(cur->type, actxt) == -1)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014733 return(-1);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000014734 if (xmlSchemaCheckCOSSTDerivedOK(actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014735 type, cur->type, subset) == 0)
14736 {
14737 /*
14738 * It just has to be validly derived from at least one
14739 * member-type.
14740 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000014741 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014742 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014743 cur = cur->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014744 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014745 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000014746 return (XML_SCHEMAP_COS_ST_DERIVED_OK_2_2);
14747}
14748
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014749/**
14750 * xmlSchemaCheckTypeDefCircularInternal:
14751 * @pctxt: the schema parser context
14752 * @ctxtType: the type definition
14753 * @ancestor: an ancestor of @ctxtType
14754 *
14755 * Checks st-props-correct (2) + ct-props-correct (3).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014756 * Circular type definitions are not allowed.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014757 *
14758 * Returns XML_SCHEMAP_ST_PROPS_CORRECT_2 if the given type is
14759 * circular, 0 otherwise.
14760 */
14761static int
14762xmlSchemaCheckTypeDefCircularInternal(xmlSchemaParserCtxtPtr pctxt,
14763 xmlSchemaTypePtr ctxtType,
14764 xmlSchemaTypePtr ancestor)
14765{
14766 int ret;
14767
14768 if ((ancestor == NULL) || (ancestor->type == XML_SCHEMA_TYPE_BASIC))
14769 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014770
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014771 if (ctxtType == ancestor) {
14772 xmlSchemaPCustomErr(pctxt,
14773 XML_SCHEMAP_ST_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014774 WXS_BASIC_CAST ctxtType, WXS_ITEM_NODE(ctxtType),
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014775 "The definition is circular", NULL);
14776 return (XML_SCHEMAP_ST_PROPS_CORRECT_2);
14777 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014778 if (ancestor->flags & XML_SCHEMAS_TYPE_MARKED) {
14779 /*
14780 * Avoid inifinite recursion on circular types not yet checked.
14781 */
14782 return (0);
14783 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014784 ancestor->flags |= XML_SCHEMAS_TYPE_MARKED;
14785 ret = xmlSchemaCheckTypeDefCircularInternal(pctxt, ctxtType,
14786 ancestor->baseType);
14787 ancestor->flags ^= XML_SCHEMAS_TYPE_MARKED;
14788 return (ret);
14789}
14790
14791/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014792 * xmlSchemaCheckTypeDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014793 * @item: the complex/simple type definition
14794 * @ctxt: the parser context
14795 * @name: the name
14796 *
14797 * Checks for circular type definitions.
14798 */
14799static void
14800xmlSchemaCheckTypeDefCircular(xmlSchemaTypePtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014801 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014802{
14803 if ((item == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014804 (item->type == XML_SCHEMA_TYPE_BASIC) ||
14805 (item->baseType == NULL))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014806 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014807 xmlSchemaCheckTypeDefCircularInternal(ctxt, item,
14808 item->baseType);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000014809}
Daniel Veillard01fa6152004-06-29 17:04:39 +000014810
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014811/*
14812* Simple Type Definition Representation OK (src-simple-type) 4
14813*
14814* "4 Circular union type definition is disallowed. That is, if the
14815* <union> alternative is chosen, there must not be any entries in the
14816* memberTypes [attribute] at any depth which resolve to the component
14817* corresponding to the <simpleType>."
14818*
14819* Note that this should work on the *representation* of a component,
14820* thus assumes any union types in the member types not being yet
14821* substituted. At this stage we need the variety of the types
14822* to be already computed.
14823*/
14824static int
14825xmlSchemaCheckUnionTypeDefCircularRecur(xmlSchemaParserCtxtPtr pctxt,
14826 xmlSchemaTypePtr ctxType,
14827 xmlSchemaTypeLinkPtr members)
14828{
14829 xmlSchemaTypeLinkPtr member;
14830 xmlSchemaTypePtr memberType;
14831
14832 member = members;
14833 while (member != NULL) {
14834 memberType = member->type;
14835 while ((memberType != NULL) &&
14836 (memberType->type != XML_SCHEMA_TYPE_BASIC)) {
14837 if (memberType == ctxType) {
14838 xmlSchemaPCustomErr(pctxt,
14839 XML_SCHEMAP_SRC_SIMPLE_TYPE_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014840 WXS_BASIC_CAST ctxType, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014841 "The union type definition is circular", NULL);
14842 return (XML_SCHEMAP_SRC_SIMPLE_TYPE_4);
14843 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014844 if ((WXS_IS_UNION(memberType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014845 ((memberType->flags & XML_SCHEMAS_TYPE_MARKED) == 0))
14846 {
14847 int res;
14848 memberType->flags |= XML_SCHEMAS_TYPE_MARKED;
14849 res = xmlSchemaCheckUnionTypeDefCircularRecur(pctxt,
14850 ctxType,
14851 xmlSchemaGetUnionSimpleTypeMemberTypes(memberType));
14852 memberType->flags ^= XML_SCHEMAS_TYPE_MARKED;
14853 if (res != 0)
14854 return(res);
14855 }
14856 memberType = memberType->baseType;
14857 }
14858 member = member->next;
14859 }
14860 return(0);
14861}
14862
14863static int
14864xmlSchemaCheckUnionTypeDefCircular(xmlSchemaParserCtxtPtr pctxt,
14865 xmlSchemaTypePtr type)
14866{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014867 if (! WXS_IS_UNION(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014868 return(0);
14869 return(xmlSchemaCheckUnionTypeDefCircularRecur(pctxt, type,
14870 type->memberTypes));
14871}
14872
Daniel Veillard01fa6152004-06-29 17:04:39 +000014873/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014874 * xmlSchemaResolveTypeReferences:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014875 * @item: the complex/simple type definition
14876 * @ctxt: the parser context
14877 * @name: the name
14878 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014879 * Resolvese type definition references
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014880 */
14881static void
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000014882xmlSchemaResolveTypeReferences(xmlSchemaTypePtr typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014883 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014884{
14885 if (typeDef == NULL)
14886 return;
14887
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014888 /*
14889 * Resolve the base type.
14890 */
14891 if (typeDef->baseType == NULL) {
14892 typeDef->baseType = xmlSchemaGetType(ctxt->schema,
14893 typeDef->base, typeDef->baseNs);
14894 if (typeDef->baseType == NULL) {
14895 xmlSchemaPResCompAttrErr(ctxt,
14896 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014897 WXS_BASIC_CAST typeDef, typeDef->node,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014898 "base", typeDef->base, typeDef->baseNs,
14899 XML_SCHEMA_TYPE_SIMPLE, NULL);
14900 return;
14901 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014902 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014903 if (WXS_IS_SIMPLE(typeDef)) {
14904 if (WXS_IS_UNION(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014905 /*
14906 * Resolve the memberTypes.
14907 */
14908 xmlSchemaResolveUnionMemberTypes(ctxt, typeDef);
14909 return;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014910 } else if (WXS_IS_LIST(typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014911 /*
14912 * Resolve the itemType.
14913 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014914 if ((typeDef->subtypes == NULL) && (typeDef->base != NULL)) {
14915
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014916 typeDef->subtypes = xmlSchemaGetType(ctxt->schema,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014917 typeDef->base, typeDef->baseNs);
14918
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014919 if ((typeDef->subtypes == NULL) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014920 (! WXS_IS_SIMPLE(typeDef->subtypes)))
14921 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014922 typeDef->subtypes = NULL;
14923 xmlSchemaPResCompAttrErr(ctxt,
14924 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014925 WXS_BASIC_CAST typeDef, typeDef->node,
14926 "itemType", typeDef->base, typeDef->baseNs,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014927 XML_SCHEMA_TYPE_SIMPLE, NULL);
14928 }
14929 }
14930 return;
14931 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000014932 }
14933 /*
14934 * The ball of letters below means, that if we have a particle
14935 * which has a QName-helper component as its {term}, we want
14936 * to resolve it...
14937 */
14938 else if ((WXS_TYPE_CONTENTTYPE(typeDef) != NULL) &&
14939 ((WXS_TYPE_CONTENTTYPE(typeDef))->type ==
14940 XML_SCHEMA_TYPE_PARTICLE) &&
14941 (WXS_TYPE_PARTICLE_TERM(typeDef) != NULL) &&
14942 ((WXS_TYPE_PARTICLE_TERM(typeDef))->type ==
14943 XML_SCHEMA_EXTRA_QNAMEREF))
14944 {
14945 xmlSchemaQNameRefPtr ref =
14946 WXS_QNAME_CAST WXS_TYPE_PARTICLE_TERM(typeDef);
14947 xmlSchemaModelGroupDefPtr groupDef;
14948
14949 /*
14950 * URGENT TODO: Test this.
14951 */
14952 WXS_TYPE_PARTICLE_TERM(typeDef) = NULL;
14953 /*
14954 * Resolve the MG definition reference.
14955 */
14956 groupDef =
14957 WXS_MODEL_GROUPDEF_CAST xmlSchemaGetNamedComponent(ctxt->schema,
14958 ref->itemType, ref->name, ref->targetNamespace);
14959 if (groupDef == NULL) {
14960 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
14961 NULL, WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)),
14962 "ref", ref->name, ref->targetNamespace, ref->itemType,
14963 NULL);
14964 /* Remove the particle. */
14965 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14966 } else if (WXS_MODELGROUPDEF_MODEL(groupDef) == NULL)
14967 /* Remove the particle. */
14968 WXS_TYPE_CONTENTTYPE(typeDef) = NULL;
14969 else {
14970 /*
14971 * Assign the MG definition's {model group} to the
14972 * particle's {term}.
14973 */
14974 WXS_TYPE_PARTICLE_TERM(typeDef) = WXS_MODELGROUPDEF_MODEL(groupDef);
14975
14976 if (WXS_MODELGROUPDEF_MODEL(groupDef)->type == XML_SCHEMA_TYPE_ALL) {
14977 /*
14978 * SPEC cos-all-limited (1.2)
14979 * "1.2 the {term} property of a particle with
14980 * {max occurs}=1 which is part of a pair which constitutes
14981 * the {content type} of a complex type definition."
14982 */
14983 if ((WXS_TYPE_PARTICLE(typeDef))->maxOccurs != 1) {
14984 xmlSchemaCustomErr(ACTXT_CAST ctxt,
14985 /* TODO: error code */
14986 XML_SCHEMAP_COS_ALL_LIMITED,
14987 WXS_ITEM_NODE(WXS_TYPE_PARTICLE(typeDef)), NULL,
14988 "The particle's {max occurs} must be 1, since the "
14989 "reference resolves to an 'all' model group",
14990 NULL, NULL);
14991 }
14992 }
14993 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000014994 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000014995}
14996
14997
14998
14999/**
Daniel Veillard01fa6152004-06-29 17:04:39 +000015000 * xmlSchemaCheckSTPropsCorrect:
15001 * @ctxt: the schema parser context
15002 * @type: the simple type definition
15003 *
15004 * Checks st-props-correct.
15005 *
15006 * Returns 0 if the properties are correct,
15007 * if not, a positive error code and -1 on internal
15008 * errors.
15009 */
15010static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015011xmlSchemaCheckSTPropsCorrect(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015012 xmlSchemaTypePtr type)
15013{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015014 xmlSchemaTypePtr baseType = type->baseType;
Daniel Veillardc0826a72004-08-10 14:17:33 +000015015 xmlChar *str = NULL;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015016
Daniel Veillardc0826a72004-08-10 14:17:33 +000015017 /* STATE: error funcs converted. */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015018 /*
15019 * Schema Component Constraint: Simple Type Definition Properties Correct
15020 *
15021 * NOTE: This is somehow redundant, since we actually built a simple type
15022 * to have all the needed information; this acts as an self test.
15023 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015024 /* Base type: If the datatype has been ·derived· by ·restriction·
15025 * then the Simple Type Definition component from which it is ·derived·,
15026 * otherwise the Simple Type Definition for anySimpleType (§4.1.6).
Daniel Veillard01fa6152004-06-29 17:04:39 +000015027 */
15028 if (baseType == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015029 /*
15030 * TODO: Think about: "modulo the impact of Missing
15031 * Sub-components (§5.3)."
15032 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015033 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015034 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015035 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015036 "No base type existent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015037 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015038
Daniel Veillard01fa6152004-06-29 17:04:39 +000015039 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015040 if (! WXS_IS_SIMPLE(baseType)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015041 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015042 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015043 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015044 "The base type '%s' is not a simple type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015045 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015046 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015047 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15048 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015049 if ( (WXS_IS_LIST(type) || WXS_IS_UNION(type)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015050 (WXS_IS_RESTRICTION(type) == 0) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015051 (! WXS_IS_ANY_SIMPLE_TYPE(baseType))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015052 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015053 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015054 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015055 "A type, derived by list or union, must have"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015056 "the simple ur-type definition as base type, not '%s'",
15057 xmlSchemaGetComponentQName(&str, baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015058 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015059 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15060 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015061 /*
15062 * Variety: One of {atomic, list, union}.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015063 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015064 if ((! WXS_IS_ATOMIC(type)) && (! WXS_IS_UNION(type)) &&
15065 (! WXS_IS_LIST(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015066 xmlSchemaPCustomErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015067 XML_SCHEMAP_ST_PROPS_CORRECT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015068 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015069 "The variety is absent", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015070 return (XML_SCHEMAP_ST_PROPS_CORRECT_1);
15071 }
Daniel Veillardc0826a72004-08-10 14:17:33 +000015072 /* TODO: Finish this. Hmm, is this finished? */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015073
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015074 /*
15075 * 3 The {final} of the {base type definition} must not contain restriction.
15076 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015077 if (xmlSchemaTypeFinalContains(baseType,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015078 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
15079 xmlSchemaPCustomErr(ctxt,
15080 XML_SCHEMAP_ST_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015081 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015082 "The 'final' of its base type '%s' must not contain "
15083 "'restriction'",
15084 xmlSchemaGetComponentQName(&str, baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015085 FREE_AND_NULL(str)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015086 return (XML_SCHEMAP_ST_PROPS_CORRECT_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015087 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015088
15089 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015090 * 2 All simple type definitions must be derived ultimately from the ·simple
15091 * ur-type definition (so· circular definitions are disallowed). That is, it
15092 * must be possible to reach a built-in primitive datatype or the ·simple
15093 * ur-type definition· by repeatedly following the {base type definition}.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015094 *
15095 * NOTE: this is done in xmlSchemaCheckTypeDefCircular().
Daniel Veillard01fa6152004-06-29 17:04:39 +000015096 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015097 return (0);
15098}
15099
15100/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015101 * xmlSchemaCheckCOSSTRestricts:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015102 * @ctxt: the schema parser context
15103 * @type: the simple type definition
15104 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015105 * Schema Component Constraint:
15106 * Derivation Valid (Restriction, Simple) (cos-st-restricts)
15107
15108 * Checks if the given @type (simpleType) is derived validly by restriction.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015109 * STATUS:
Daniel Veillard01fa6152004-06-29 17:04:39 +000015110 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015111 * Returns -1 on internal errors, 0 if the type is validly derived,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015112 * a positive error code otherwise.
15113 */
15114static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015115xmlSchemaCheckCOSSTRestricts(xmlSchemaParserCtxtPtr pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015116 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015117{
Daniel Veillardc0826a72004-08-10 14:17:33 +000015118 xmlChar *str = NULL;
15119
Daniel Veillard01fa6152004-06-29 17:04:39 +000015120 if (type->type != XML_SCHEMA_TYPE_SIMPLE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015121 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15122 "given type is not a user-derived simpleType");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015123 return (-1);
15124 }
15125
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015126 if (WXS_IS_ATOMIC(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015127 xmlSchemaTypePtr primitive;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015128 /*
15129 * 1.1 The {base type definition} must be an atomic simple
Daniel Veillard01fa6152004-06-29 17:04:39 +000015130 * type definition or a built-in primitive datatype.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015131 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015132 if (! WXS_IS_ATOMIC(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015133 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015134 XML_SCHEMAP_COS_ST_RESTRICTS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015135 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015136 "The base type '%s' is not an atomic simple type",
15137 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015138 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015139 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_1);
15140 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015141 /* 1.2 The {final} of the {base type definition} must not contain
Daniel Veillard01fa6152004-06-29 17:04:39 +000015142 * restriction.
15143 */
Daniel Veillardc0826a72004-08-10 14:17:33 +000015144 /* OPTIMIZE TODO : This is already done in xmlSchemaCheckStPropsCorrect */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015145 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015146 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015147 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015148 XML_SCHEMAP_COS_ST_RESTRICTS_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015149 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015150 "The final of its base type '%s' must not contain 'restriction'",
15151 xmlSchemaGetComponentQName(&str, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015152 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015153 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_2);
15154 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015155
15156 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015157 * 1.3.1 DF must be an allowed constraining facet for the {primitive
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015158 * type definition}, as specified in the appropriate subsection of 3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015159 * Primitive datatypes.
15160 */
15161 if (type->facets != NULL) {
15162 xmlSchemaFacetPtr facet;
15163 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015164
Daniel Veillard01fa6152004-06-29 17:04:39 +000015165 primitive = xmlSchemaGetPrimitiveType(type);
15166 if (primitive == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015167 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15168 "failed to get primitive type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015169 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015170 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015171 facet = type->facets;
15172 do {
15173 if (xmlSchemaIsBuiltInTypeFacet(primitive, facet->type) == 0) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015174 ok = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015175 xmlSchemaPIllegalFacetAtomicErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015176 XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015177 type, primitive, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015178 }
15179 facet = facet->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015180 } while (facet != NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015181 if (ok == 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015182 return (XML_SCHEMAP_COS_ST_RESTRICTS_1_3_1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015183 }
15184 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015185 * SPEC (1.3.2) "If there is a facet of the same kind in the {facets}
15186 * of the {base type definition} (call this BF),then the DF's {value}
15187 * must be a valid restriction of BF's {value} as defined in
15188 * [XML Schemas: Datatypes]."
15189 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015190 * NOTE (1.3.2) Facet derivation constraints are currently handled in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015191 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015192 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015193 } else if (WXS_IS_LIST(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015194 xmlSchemaTypePtr itemType = NULL;
15195
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015196 itemType = type->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015197 if ((itemType == NULL) || (! WXS_IS_SIMPLE(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015198 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15199 "failed to evaluate the item type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015200 return (-1);
15201 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015202 if (WXS_IS_TYPE_NOT_FIXED(itemType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015203 xmlSchemaTypeFixup(itemType, ACTXT_CAST pctxt);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015204 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015205 * 2.1 The {item type definition} must have a {variety} of atomic or
15206 * union (in which case all the {member type definitions}
Daniel Veillard01fa6152004-06-29 17:04:39 +000015207 * must be atomic).
15208 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015209 if ((! WXS_IS_ATOMIC(itemType)) &&
15210 (! WXS_IS_UNION(itemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015211 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015212 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015213 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015214 "The item type '%s' does not have a variety of atomic or union",
15215 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015216 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015217 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015218 } else if (WXS_IS_UNION(itemType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015219 xmlSchemaTypeLinkPtr member;
15220
15221 member = itemType->memberTypes;
15222 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015223 if (! WXS_IS_ATOMIC(member->type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015224 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015225 XML_SCHEMAP_COS_ST_RESTRICTS_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015226 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015227 "The item type is a union type, but the "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015228 "member type '%s' of this item type is not atomic",
15229 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015230 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015231 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_1);
15232 }
15233 member = member->next;
15234 }
15235 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015236
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015237 if (WXS_IS_ANY_SIMPLE_TYPE(type->baseType)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015238 xmlSchemaFacetPtr facet;
15239 /*
15240 * This is the case if we have: <simpleType><list ..
15241 */
15242 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015243 * 2.3.1
15244 * 2.3.1.1 The {final} of the {item type definition} must not
Daniel Veillard01fa6152004-06-29 17:04:39 +000015245 * contain list.
15246 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015247 if (xmlSchemaTypeFinalContains(itemType,
15248 XML_SCHEMAS_TYPE_FINAL_LIST)) {
15249 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015250 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015251 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015252 "The final of its item type '%s' must not contain 'list'",
15253 xmlSchemaGetComponentQName(&str, itemType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015254 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015255 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_1);
15256 }
15257 /*
15258 * 2.3.1.2 The {facets} must only contain the whiteSpace
15259 * facet component.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015260 * OPTIMIZE TODO: the S4S already disallows any facet
15261 * to be specified.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015262 */
15263 if (type->facets != NULL) {
15264 facet = type->facets;
15265 do {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015266 if (facet->type != XML_SCHEMA_FACET_WHITESPACE) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015267 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015268 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015269 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015270 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_1_2);
15271 }
15272 facet = facet->next;
15273 } while (facet != NULL);
15274 }
15275 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015276 * MAYBE TODO: (Hmm, not really) Datatypes states:
15277 * A ·list· datatype can be ·derived· from an ·atomic· datatype
15278 * whose ·lexical space· allows space (such as string or anyURI)or
15279 * a ·union· datatype any of whose {member type definitions}'s
Daniel Veillard01fa6152004-06-29 17:04:39 +000015280 * ·lexical space· allows space.
15281 */
15282 } else {
15283 /*
15284 * This is the case if we have: <simpleType><restriction ...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015285 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015286 */
15287 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015288 * 2.3.2
Daniel Veillard01fa6152004-06-29 17:04:39 +000015289 * 2.3.2.1 The {base type definition} must have a {variety} of list.
15290 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015291 if (! WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015292 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015293 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015294 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015295 "The base type '%s' must be a list type",
15296 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015297 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015298 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_1);
15299 }
15300 /*
15301 * 2.3.2.2 The {final} of the {base type definition} must not
15302 * contain restriction.
15303 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015304 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015305 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015306 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015307 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015308 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015309 "The 'final' of the base type '%s' must not contain 'restriction'",
15310 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015311 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015312 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_2);
15313 }
15314 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015315 * 2.3.2.3 The {item type definition} must be validly derived
Daniel Veillard01fa6152004-06-29 17:04:39 +000015316 * from the {base type definition}'s {item type definition} given
15317 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6).
15318 */
15319 {
15320 xmlSchemaTypePtr baseItemType;
15321
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015322 baseItemType = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015323 if ((baseItemType == NULL) || (! WXS_IS_SIMPLE(baseItemType))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015324 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15325 "failed to eval the item type of a base type");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015326 return (-1);
15327 }
15328 if ((itemType != baseItemType) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015329 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt, itemType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015330 baseItemType, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015331 xmlChar *strBIT = NULL, *strBT = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015332 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015333 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015334 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015335 "The item type '%s' is not validly derived from "
15336 "the item type '%s' of the base type '%s'",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015337 xmlSchemaGetComponentQName(&str, itemType),
15338 xmlSchemaGetComponentQName(&strBIT, baseItemType),
15339 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015340
15341 FREE_AND_NULL(str)
15342 FREE_AND_NULL(strBIT)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015343 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015344 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_3);
15345 }
15346 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015347
Daniel Veillard01fa6152004-06-29 17:04:39 +000015348 if (type->facets != NULL) {
15349 xmlSchemaFacetPtr facet;
15350 int ok = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015351 /*
15352 * 2.3.2.4 Only length, minLength, maxLength, whiteSpace, pattern
Daniel Veillard01fa6152004-06-29 17:04:39 +000015353 * and enumeration facet components are allowed among the {facets}.
15354 */
15355 facet = type->facets;
15356 do {
15357 switch (facet->type) {
15358 case XML_SCHEMA_FACET_LENGTH:
15359 case XML_SCHEMA_FACET_MINLENGTH:
15360 case XML_SCHEMA_FACET_MAXLENGTH:
15361 case XML_SCHEMA_FACET_WHITESPACE:
15362 /*
15363 * TODO: 2.5.1.2 List datatypes
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015364 * The value of ·whiteSpace· is fixed to the value collapse.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015365 */
15366 case XML_SCHEMA_FACET_PATTERN:
15367 case XML_SCHEMA_FACET_ENUMERATION:
15368 break;
15369 default: {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015370 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015371 XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015372 type, facet);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015373 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015374 * We could return, but it's nicer to report all
Daniel Veillard01fa6152004-06-29 17:04:39 +000015375 * invalid facets.
15376 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015377 ok = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015378 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015379 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015380 facet = facet->next;
15381 } while (facet != NULL);
15382 if (ok == 0)
15383 return (XML_SCHEMAP_COS_ST_RESTRICTS_2_3_2_4);
15384 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015385 * SPEC (2.3.2.5) (same as 1.3.2)
15386 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015387 * NOTE (2.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015388 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015389 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015390 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015391 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015392 } else if (WXS_IS_UNION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015393 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015394 * 3.1 The {member type definitions} must all have {variety} of
Daniel Veillard01fa6152004-06-29 17:04:39 +000015395 * atomic or list.
15396 */
15397 xmlSchemaTypeLinkPtr member;
15398
15399 member = type->memberTypes;
15400 while (member != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015401 if (WXS_IS_TYPE_NOT_FIXED(member->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015402 xmlSchemaTypeFixup(member->type, ACTXT_CAST pctxt);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015403
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015404 if ((! WXS_IS_ATOMIC(member->type)) &&
15405 (! WXS_IS_LIST(member->type))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015406 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015407 XML_SCHEMAP_COS_ST_RESTRICTS_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015408 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015409 "The member type '%s' is neither an atomic, nor a list type",
15410 xmlSchemaGetComponentQName(&str, member->type));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015411 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015412 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_1);
15413 }
15414 member = member->next;
15415 }
15416 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015417 * 3.3.1 If the {base type definition} is the ·simple ur-type
15418 * definition·
Daniel Veillard01fa6152004-06-29 17:04:39 +000015419 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015420 if (type->baseType->builtInType == XML_SCHEMAS_ANYSIMPLETYPE) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015421 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015422 * 3.3.1.1 All of the {member type definitions} must have a
Daniel Veillard01fa6152004-06-29 17:04:39 +000015423 * {final} which does not contain union.
15424 */
15425 member = type->memberTypes;
15426 while (member != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015427 if (xmlSchemaTypeFinalContains(member->type,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015428 XML_SCHEMAS_TYPE_FINAL_UNION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015429 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015430 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015431 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015432 "The 'final' of member type '%s' contains 'union'",
15433 xmlSchemaGetComponentQName(&str, member->type));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015434 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015435 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1);
15436 }
15437 member = member->next;
15438 }
15439 /*
15440 * 3.3.1.2 The {facets} must be empty.
15441 */
15442 if (type->facetSet != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015443 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015444 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015445 WXS_BASIC_CAST type, NULL,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015446 "No facets allowed", NULL);
Daniel Veillard01fa6152004-06-29 17:04:39 +000015447 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_1_2);
15448 }
15449 } else {
15450 /*
15451 * 3.3.2.1 The {base type definition} must have a {variety} of union.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015452 * I.e. the variety of "list" is inherited.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015453 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015454 if (! WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015455 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015456 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015457 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015458 "The base type '%s' is not a union type",
15459 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015460 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015461 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_1);
15462 }
15463 /*
15464 * 3.3.2.2 The {final} of the {base type definition} must not contain restriction.
15465 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015466 if (xmlSchemaTypeFinalContains(type->baseType,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015467 XML_SCHEMAS_TYPE_FINAL_RESTRICTION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015468 xmlSchemaPCustomErr(pctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015469 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015470 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015471 "The 'final' of its base type '%s' must not contain 'restriction'",
15472 xmlSchemaGetComponentQName(&str, type->baseType));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015473 FREE_AND_NULL(str)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015474 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_2);
15475 }
15476 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015477 * 3.3.2.3 The {member type definitions}, in order, must be validly
15478 * derived from the corresponding type definitions in the {base
15479 * type definition}'s {member type definitions} given the empty set,
Daniel Veillard01fa6152004-06-29 17:04:39 +000015480 * as defined in Type Derivation OK (Simple) (§3.14.6).
15481 */
15482 {
15483 xmlSchemaTypeLinkPtr baseMember;
15484
15485 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015486 * OPTIMIZE: if the type is restricting, it has no local defined
15487 * member types and inherits the member types of the base type;
Daniel Veillard01fa6152004-06-29 17:04:39 +000015488 * thus a check for equality can be skipped.
15489 */
15490 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015491 * Even worse: I cannot see a scenario where a restricting
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015492 * union simple type can have other member types as the member
Daniel Veillard01fa6152004-06-29 17:04:39 +000015493 * types of it's base type. This check seems not necessary with
15494 * respect to the derivation process in libxml2.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000015495 * But necessary if constructing types with an API.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015496 */
15497 if (type->memberTypes != NULL) {
15498 member = type->memberTypes;
15499 baseMember = xmlSchemaGetUnionSimpleTypeMemberTypes(type->baseType);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015500 if ((member == NULL) && (baseMember != NULL)) {
15501 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15502 "different number of member types in base");
15503 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015504 while (member != NULL) {
15505 if (baseMember == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015506 PERROR_INT("xmlSchemaCheckCOSSTRestricts",
15507 "different number of member types in base");
Daniel Veillard01fa6152004-06-29 17:04:39 +000015508 }
15509 if ((member->type != baseMember->type) &&
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015510 (xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015511 member->type, baseMember->type, 0) != 0)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000015512 xmlChar *strBMT = NULL, *strBT = NULL;
15513
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015514 xmlSchemaPCustomErrExt(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015515 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015516 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015517 "The member type %s is not validly "
15518 "derived from its corresponding member "
15519 "type %s of the base type %s",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015520 xmlSchemaGetComponentQName(&str, member->type),
15521 xmlSchemaGetComponentQName(&strBMT, baseMember->type),
15522 xmlSchemaGetComponentQName(&strBT, type->baseType));
Daniel Veillardc0826a72004-08-10 14:17:33 +000015523 FREE_AND_NULL(str)
15524 FREE_AND_NULL(strBMT)
15525 FREE_AND_NULL(strBT)
Daniel Veillard01fa6152004-06-29 17:04:39 +000015526 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_3);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015527 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015528 member = member->next;
15529 baseMember = baseMember->next;
15530 }
15531 }
15532 }
15533 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015534 * 3.3.2.4 Only pattern and enumeration facet components are
Daniel Veillard01fa6152004-06-29 17:04:39 +000015535 * allowed among the {facets}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015536 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015537 if (type->facets != NULL) {
15538 xmlSchemaFacetPtr facet;
15539 int ok = 1;
15540
15541 facet = type->facets;
15542 do {
15543 if ((facet->type != XML_SCHEMA_FACET_PATTERN) &&
15544 (facet->type != XML_SCHEMA_FACET_ENUMERATION)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015545 xmlSchemaPIllegalFacetListUnionErr(pctxt,
Daniel Veillardc0826a72004-08-10 14:17:33 +000015546 XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015547 type, facet);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015548 ok = 0;
15549 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015550 facet = facet->next;
15551 } while (facet != NULL);
15552 if (ok == 0)
15553 return (XML_SCHEMAP_COS_ST_RESTRICTS_3_3_2_4);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015554
Daniel Veillard01fa6152004-06-29 17:04:39 +000015555 }
15556 /*
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015557 * SPEC (3.3.2.5) (same as 1.3.2)
15558 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015559 * NOTE (3.3.2.5) This is currently done in
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015560 * xmlSchemaDeriveAndValidateFacets()
Daniel Veillard01fa6152004-06-29 17:04:39 +000015561 */
15562 }
15563 }
15564
15565 return (0);
15566}
15567
15568/**
15569 * xmlSchemaCheckSRCSimpleType:
15570 * @ctxt: the schema parser context
15571 * @type: the simple type definition
15572 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015573 * Checks crc-simple-type constraints.
Daniel Veillard01fa6152004-06-29 17:04:39 +000015574 *
15575 * Returns 0 if the constraints are satisfied,
15576 * if not a positive error code and -1 on internal
15577 * errors.
15578 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015579#if 0
Daniel Veillard01fa6152004-06-29 17:04:39 +000015580static int
15581xmlSchemaCheckSRCSimpleType(xmlSchemaParserCtxtPtr ctxt,
15582 xmlSchemaTypePtr type)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015583{
15584 /*
Daniel Veillard01fa6152004-06-29 17:04:39 +000015585 * src-simple-type.1 The corresponding simple type definition, if any,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015586 * must satisfy the conditions set out in Constraints on Simple Type
15587 * Definition Schema Components (§3.14.6).
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015588 */
15589 if (WXS_IS_RESTRICTION(type)) {
Daniel Veillard01fa6152004-06-29 17:04:39 +000015590 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015591 * src-simple-type.2 "If the <restriction> alternative is chosen,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015592 * either it must have a base [attribute] or a <simpleType> among its
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015593 * [children], but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015594 * NOTE: This is checked in the parse function of <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015595 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015596 /*
15597 *
15598 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015599 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015600 /* src-simple-type.3 "If the <list> alternative is chosen, either it must have
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015601 * an itemType [attribute] or a <simpleType> among its [children],
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015602 * but not both."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015603 *
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015604 * NOTE: This is checked in the parse function of <list>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015605 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015606 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015607 /*
15608 * src-simple-type.4 is checked in xmlSchemaCheckUnionTypeDefCircular().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015609 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000015610 }
Daniel Veillard01fa6152004-06-29 17:04:39 +000015611 return (0);
15612}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015613#endif
Daniel Veillard01fa6152004-06-29 17:04:39 +000015614
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015615static int
15616xmlSchemaCreateVCtxtOnPCtxt(xmlSchemaParserCtxtPtr ctxt)
15617{
15618 if (ctxt->vctxt == NULL) {
15619 ctxt->vctxt = xmlSchemaNewValidCtxt(NULL);
15620 if (ctxt->vctxt == NULL) {
15621 xmlSchemaPErr(ctxt, NULL,
Kasimier T. Buchcikbea23542004-08-25 20:35:45 +000015622 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015623 "Internal error: xmlSchemaCreateVCtxtOnPCtxt, "
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015624 "failed to create a temp. validation context.\n",
15625 NULL, NULL);
15626 return (-1);
15627 }
15628 /* TODO: Pass user data. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015629 xmlSchemaSetValidErrors(ctxt->vctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000015630 ctxt->error, ctxt->warning, ctxt->errCtxt);
15631 xmlSchemaSetValidStructuredErrors(ctxt->vctxt,
15632 ctxt->serror, ctxt->errCtxt);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015633 }
15634 return (0);
15635}
15636
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015637static int
15638xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
15639 xmlNodePtr node,
15640 xmlSchemaTypePtr type,
15641 const xmlChar *value,
15642 xmlSchemaValPtr *retVal,
15643 int fireErrors,
15644 int normalize,
15645 int isNormalized);
15646
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015647/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015648 * xmlSchemaParseCheckCOSValidDefault:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015649 * @pctxt: the schema parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015650 * @type: the simple type definition
15651 * @value: the default value
15652 * @node: an optional node (the holder of the value)
15653 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015654 * Schema Component Constraint: Element Default Valid (Immediate)
15655 * (cos-valid-default)
15656 * This will be used by the parser only. For the validator there's
15657 * an other version.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015658 *
15659 * Returns 0 if the constraints are satisfied,
15660 * if not, a positive error code and -1 on internal
15661 * errors.
15662 */
15663static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015664xmlSchemaParseCheckCOSValidDefault(xmlSchemaParserCtxtPtr pctxt,
15665 xmlNodePtr node,
15666 xmlSchemaTypePtr type,
15667 const xmlChar *value,
15668 xmlSchemaValPtr *val)
15669{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015670 int ret = 0;
15671
15672 /*
15673 * cos-valid-default:
15674 * Schema Component Constraint: Element Default Valid (Immediate)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015675 * For a string to be a valid default with respect to a type
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015676 * definition the appropriate case among the following must be true:
15677 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015678 if WXS_IS_COMPLEX(type) {
William M. Brack2f2a6632004-08-20 23:09:47 +000015679 /*
15680 * Complex type.
15681 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015682 * SPEC (2.1) "its {content type} must be a simple type definition
15683 * or mixed."
15684 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015685 * type}'s particle must be ·emptiable· as defined by
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015686 * Particle Emptiable (§3.9.6)."
William M. Brack2f2a6632004-08-20 23:09:47 +000015687 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015688 if ((! WXS_HAS_SIMPLE_CONTENT(type)) &&
15689 ((! WXS_HAS_MIXED_CONTENT(type)) || (! WXS_EMPTIABLE(type)))) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015690 /* NOTE that this covers (2.2.2) as well. */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015691 xmlSchemaPCustomErr(pctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015692 XML_SCHEMAP_COS_VALID_DEFAULT_2_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015693 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015694 "For a string to be a valid default, the type definition "
15695 "must be a simple type or a complex type with mixed content "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015696 "and a particle emptiable", NULL);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015697 return(XML_SCHEMAP_COS_VALID_DEFAULT_2_1);
15698 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015699 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015700 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015701 * 1 If the type definition is a simple type definition, then the string
15702 * must be ·valid· with respect to that definition as defined by String
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015703 * Valid (§3.14.4).
15704 *
15705 * AND
15706 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015707 * 2.2.1 If the {content type} is a simple type definition, then the
15708 * string must be ·valid· with respect to that simple type definition
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015709 * as defined by String Valid (§3.14.4).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015710 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015711 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015712 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015713 type, value, val, 1, 1, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015714 else if (WXS_HAS_SIMPLE_CONTENT(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015715 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt, node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015716 type->contentTypeDef, value, val, 1, 1, 0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015717 else
15718 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015719
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015720 if (ret < 0) {
15721 PERROR_INT("xmlSchemaParseCheckCOSValidDefault",
15722 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015723 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015724
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000015725 return (ret);
William M. Brack2f2a6632004-08-20 23:09:47 +000015726}
15727
15728/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015729 * xmlSchemaCheckCTPropsCorrect:
William M. Brack2f2a6632004-08-20 23:09:47 +000015730 * @ctxt: the schema parser context
15731 * @type: the complex type definition
15732 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015733 *.(4.6) Constraints on Complex Type Definition Schema Components
15734 * Schema Component Constraint:
15735 * Complex Type Definition Properties Correct (ct-props-correct)
15736 * STATUS: (seems) complete
William M. Brack2f2a6632004-08-20 23:09:47 +000015737 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015738 * Returns 0 if the constraints are satisfied, a positive
15739 * error code if not and -1 if an internal error occured.
William M. Brack2f2a6632004-08-20 23:09:47 +000015740 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015741static int
15742xmlSchemaCheckCTPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
15743 xmlSchemaTypePtr type)
William M. Brack2f2a6632004-08-20 23:09:47 +000015744{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015745 /*
15746 * TODO: Correct the error code; XML_SCHEMAP_SRC_CT_1 is used temporarily.
15747 *
15748 * SPEC (1) "The values of the properties of a complex type definition must
15749 * be as described in the property tableau in The Complex Type Definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015750 * Schema Component (§3.4.1), modulo the impact of Missing
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015751 * Sub-components (§5.3)."
15752 */
15753 if ((type->baseType != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015754 (WXS_IS_SIMPLE(type->baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015755 (WXS_IS_EXTENSION(type) == 0)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015756 /*
15757 * SPEC (2) "If the {base type definition} is a simple type definition,
15758 * the {derivation method} must be extension."
15759 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015760 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015761 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015762 NULL, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015763 "If the base type is a simple type, the derivation method must be "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015764 "'extension'", NULL, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015765 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015766 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015767 /*
15768 * SPEC (3) "Circular definitions are disallowed, except for the ·ur-type
15769 * definition·. That is, it must be possible to reach the ·ur-type
15770 * definition by repeatedly following the {base type definition}."
15771 *
15772 * NOTE (3) is done in xmlSchemaCheckTypeDefCircular().
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015773 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015774 /*
15775 * NOTE that (4) and (5) need the following:
15776 * - attribute uses need to be already inherited (apply attr. prohibitions)
15777 * - attribute group references need to be expanded already
15778 * - simple types need to be typefixed already
15779 */
15780 if (type->attrUses &&
15781 (((xmlSchemaItemListPtr) type->attrUses)->nbItems > 1))
15782 {
15783 xmlSchemaItemListPtr uses = (xmlSchemaItemListPtr) type->attrUses;
15784 xmlSchemaAttributeUsePtr use, tmp;
15785 int i, j, hasId = 0;
15786
15787 for (i = uses->nbItems -1; i >= 0; i--) {
15788 use = uses->items[i];
15789
15790 /*
15791 * SPEC ct-props-correct
15792 * (4) "Two distinct attribute declarations in the
15793 * {attribute uses} must not have identical {name}s and
15794 * {target namespace}s."
15795 */
15796 if (i > 0) {
15797 for (j = i -1; j >= 0; j--) {
15798 tmp = uses->items[j];
15799 if ((WXS_ATTRUSE_DECL_NAME(use) ==
15800 WXS_ATTRUSE_DECL_NAME(tmp)) &&
15801 (WXS_ATTRUSE_DECL_TNS(use) ==
15802 WXS_ATTRUSE_DECL_TNS(tmp)))
15803 {
15804 xmlChar *str = NULL;
15805
15806 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15807 XML_SCHEMAP_AG_PROPS_CORRECT,
15808 NULL, WXS_BASIC_CAST type,
15809 "Duplicate %s",
15810 xmlSchemaGetComponentDesignation(&str, use),
15811 NULL);
15812 FREE_AND_NULL(str);
15813 /*
15814 * Remove the duplicate.
15815 */
15816 if (xmlSchemaItemListRemove(uses, i) == -1)
15817 goto exit_failure;
15818 goto next_use;
15819 }
15820 }
15821 }
15822 /*
15823 * SPEC ct-props-correct
15824 * (5) "Two distinct attribute declarations in the
15825 * {attribute uses} must not have {type definition}s which
15826 * are or are derived from ID."
15827 */
15828 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
15829 if (xmlSchemaIsDerivedFromBuiltInType(
15830 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
15831 {
15832 if (hasId) {
15833 xmlChar *str = NULL;
15834
15835 xmlSchemaCustomErr(ACTXT_CAST pctxt,
15836 XML_SCHEMAP_AG_PROPS_CORRECT,
15837 NULL, WXS_BASIC_CAST type,
15838 "There must not exist more than one attribute "
15839 "declaration of type 'xs:ID' "
15840 "(or derived from 'xs:ID'). The %s violates this "
15841 "constraint",
15842 xmlSchemaGetComponentDesignation(&str, use),
15843 NULL);
15844 FREE_AND_NULL(str);
15845 if (xmlSchemaItemListRemove(uses, i) == -1)
15846 goto exit_failure;
15847 }
15848
15849 hasId = 1;
15850 }
15851 }
15852next_use: {}
15853 }
15854 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015855 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015856exit_failure:
15857 return(-1);
William M. Brack2f2a6632004-08-20 23:09:47 +000015858}
15859
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015860static int
15861xmlSchemaAreEqualTypes(xmlSchemaTypePtr typeA,
15862 xmlSchemaTypePtr typeB)
15863{
15864 /*
15865 * TODO: This should implement component-identity
15866 * in the future.
15867 */
15868 if ((typeA == NULL) || (typeB == NULL))
15869 return (0);
15870 return (typeA == typeB);
15871}
15872
15873/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015874 * xmlSchemaCheckCOSCTDerivedOK:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015875 * @ctxt: the schema parser context
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015876 * @type: the to-be derived complex type definition
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015877 * @baseType: the base complex type definition
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000015878 * @set: the given set
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015879 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015880 * Schema Component Constraint:
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015881 * Type Derivation OK (Complex) (cos-ct-derived-ok)
15882 *
15883 * STATUS: completed
15884 *
15885 * Returns 0 if the constraints are satisfied, or 1
15886 * if not.
15887 */
15888static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015889xmlSchemaCheckCOSCTDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015890 xmlSchemaTypePtr type,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015891 xmlSchemaTypePtr baseType,
15892 int set)
15893{
15894 int equal = xmlSchemaAreEqualTypes(type, baseType);
15895 /* TODO: Error codes. */
15896 /*
15897 * SPEC "For a complex type definition (call it D, for derived)
15898 * to be validly derived from a type definition (call this
15899 * B, for base) given a subset of {extension, restriction}
15900 * all of the following must be true:"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015901 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015902 if (! equal) {
15903 /*
15904 * SPEC (1) "If B and D are not the same type definition, then the
15905 * {derivation method} of D must not be in the subset."
15906 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000015907 if (((set & SUBSET_EXTENSION) && (WXS_IS_EXTENSION(type))) ||
15908 ((set & SUBSET_RESTRICTION) && (WXS_IS_RESTRICTION(type))))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015909 return (1);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015910 } else {
15911 /*
15912 * SPEC (2.1) "B and D must be the same type definition."
15913 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015914 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015915 }
15916 /*
15917 * SPEC (2.2) "B must be D's {base type definition}."
15918 */
15919 if (type->baseType == baseType)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015920 return (0);
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015921 /*
15922 * SPEC (2.3.1) "D's {base type definition} must not be the ·ur-type
15923 * definition·."
15924 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015925 if (WXS_IS_ANYTYPE(type->baseType))
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000015926 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015927
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015928 if (WXS_IS_COMPLEX(type->baseType)) {
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015929 /*
15930 * SPEC (2.3.2.1) "If D's {base type definition} is complex, then it
15931 * must be validly derived from B given the subset as defined by this
15932 * constraint."
15933 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015934 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015935 baseType, set));
15936 } else {
15937 /*
15938 * SPEC (2.3.2.2) "If D's {base type definition} is simple, then it
15939 * must be validly derived from B given the subset as defined in Type
15940 * Derivation OK (Simple) (§3.14.6).
15941 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015942 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type->baseType,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015943 baseType, set));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015944 }
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015945}
15946
15947/**
15948 * xmlSchemaCheckCOSDerivedOK:
15949 * @type: the derived simple type definition
15950 * @baseType: the base type definition
15951 *
15952 * Calls:
15953 * Type Derivation OK (Simple) AND Type Derivation OK (Complex)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015954 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015955 * Checks wheter @type can be validly derived from @baseType.
15956 *
15957 * Returns 0 on success, an positive error code otherwise.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015958 */
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015959static int
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015960xmlSchemaCheckCOSDerivedOK(xmlSchemaAbstractCtxtPtr actxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015961 xmlSchemaTypePtr type,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000015962 xmlSchemaTypePtr baseType,
15963 int set)
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015964{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015965 if (WXS_IS_SIMPLE(type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015966 return (xmlSchemaCheckCOSSTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015967 else
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000015968 return (xmlSchemaCheckCOSCTDerivedOK(actxt, type, baseType, set));
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000015969}
15970
William M. Brack2f2a6632004-08-20 23:09:47 +000015971/**
15972 * xmlSchemaCheckCOSCTExtends:
15973 * @ctxt: the schema parser context
15974 * @type: the complex type definition
15975 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015976 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015977 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015978 * Derivation Valid (Extension) (cos-ct-extends)
15979 *
15980 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015981 * missing:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015982 * (1.5)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000015983 * (1.4.3.2.2.2) "Particle Valid (Extension)"
William M. Brack2f2a6632004-08-20 23:09:47 +000015984 *
15985 * Returns 0 if the constraints are satisfied, a positive
15986 * error code if not and -1 if an internal error occured.
15987 */
15988static int
15989xmlSchemaCheckCOSCTExtends(xmlSchemaParserCtxtPtr ctxt,
15990 xmlSchemaTypePtr type)
15991{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015992 xmlSchemaTypePtr base = type->baseType;
15993 /*
15994 * TODO: Correct the error code; XML_SCHEMAP_COS_CT_EXTENDS_1_1 is used
15995 * temporarily only.
William M. Brack2f2a6632004-08-20 23:09:47 +000015996 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000015997 /*
15998 * SPEC (1) "If the {base type definition} is a complex type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000015999 * then all of the following must be true:"
16000 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016001 if (WXS_IS_COMPLEX(base)) {
William M. Brack2f2a6632004-08-20 23:09:47 +000016002 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016003 * SPEC (1.1) "The {final} of the {base type definition} must not
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016004 * contain extension."
William M. Brack2f2a6632004-08-20 23:09:47 +000016005 */
16006 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16007 xmlSchemaPCustomErr(ctxt,
16008 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016009 WXS_BASIC_CAST type, NULL,
William M. Brack2f2a6632004-08-20 23:09:47 +000016010 "The 'final' of the base type definition "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016011 "contains 'extension'", NULL);
William M. Brack2f2a6632004-08-20 23:09:47 +000016012 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16013 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016014
16015 /*
16016 * ATTENTION: The constrains (1.2) and (1.3) are not applied,
16017 * since they are automatically satisfied through the
16018 * inheriting mechanism.
16019 * Note that even if redefining components, the inheriting mechanism
16020 * is used.
16021 */
16022#if 0
William M. Brack2f2a6632004-08-20 23:09:47 +000016023 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016024 * SPEC (1.2) "Its {attribute uses} must be a subset of the {attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016025 * uses}
16026 * of the complex type definition itself, that is, for every attribute
16027 * use in the {attribute uses} of the {base type definition}, there
16028 * must be an attribute use in the {attribute uses} of the complex
16029 * type definition itself whose {attribute declaration} has the same
16030 * {name}, {target namespace} and {type definition} as its attribute
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016031 * declaration"
William M. Brack2f2a6632004-08-20 23:09:47 +000016032 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016033 if (base->attrUses != NULL) {
16034 int i, j, found;
16035 xmlSchemaAttributeUsePtr use, buse;
William M. Brack2f2a6632004-08-20 23:09:47 +000016036
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016037 for (i = 0; i < (WXS_LIST_CAST base->attrUses)->nbItems; i ++) {
16038 buse = (WXS_LIST_CAST base->attrUses)->items[i];
16039 found = 0;
16040 if (type->attrUses != NULL) {
16041 use = (WXS_LIST_CAST type->attrUses)->items[j];
16042 for (j = 0; j < (WXS_LIST_CAST type->attrUses)->nbItems; j ++)
16043 {
16044 if ((WXS_ATTRUSE_DECL_NAME(use) ==
16045 WXS_ATTRUSE_DECL_NAME(buse)) &&
16046 (WXS_ATTRUSE_DECL_TNS(use) ==
16047 WXS_ATTRUSE_DECL_TNS(buse)) &&
16048 (WXS_ATTRUSE_TYPEDEF(use) ==
16049 WXS_ATTRUSE_TYPEDEF(buse))
16050 {
16051 found = 1;
16052 break;
16053 }
16054 }
16055 }
16056 if (! found) {
16057 xmlChar *str = NULL;
16058
16059 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16060 XML_SCHEMAP_COS_CT_EXTENDS_1_2,
16061 NULL, WXS_BASIC_CAST type,
16062 /*
16063 * TODO: The report does not indicate that also the
16064 * type needs to be the same.
16065 */
16066 "This type is missing a matching correspondent "
16067 "for its {base type}'s %s in its {attribute uses}",
16068 xmlSchemaGetComponentDesignation(&str,
16069 buse->children),
16070 NULL);
16071 FREE_AND_NULL(str)
16072 }
16073 }
16074 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016075 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016076 * SPEC (1.3) "If it has an {attribute wildcard}, the complex type
16077 * definition must also have one, and the base type definition's
16078 * {attribute wildcard}'s {namespace constraint} must be a subset
16079 * of the complex type definition's {attribute wildcard}'s {namespace
16080 * constraint}, as defined by Wildcard Subset (§3.10.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016081 */
16082
16083 /*
16084 * MAYBE TODO: Enable if ever needed. But this will be needed only
16085 * if created the type via a schema construction API.
16086 */
16087 if (base->attributeWildcard != NULL) {
16088 if (type->attributeWilcard == NULL) {
16089 xmlChar *str = NULL;
16090
16091 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16092 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16093 NULL, type,
16094 "The base %s has an attribute wildcard, "
16095 "but this type is missing an attribute wildcard",
16096 xmlSchemaGetComponentDesignation(&str, base));
16097 FREE_AND_NULL(str)
16098
16099 } else if (xmlSchemaCheckCOSNSSubset(
16100 base->attributeWildcard, type->attributeWildcard))
16101 {
16102 xmlChar *str = NULL;
16103
16104 xmlSchemaCustomErr(ACTXT_CAST pctxt,
16105 XML_SCHEMAP_COS_CT_EXTENDS_1_3,
16106 NULL, type,
16107 "The attribute wildcard is not a valid "
16108 "superset of the one in the base %s",
16109 xmlSchemaGetComponentDesignation(&str, base));
16110 FREE_AND_NULL(str)
16111 }
16112 }
16113#endif
16114 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016115 * SPEC (1.4) "One of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016116 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016117 if ((type->contentTypeDef != NULL) &&
16118 (type->contentTypeDef == base->contentTypeDef)) {
16119 /*
16120 * SPEC (1.4.1) "The {content type} of the {base type definition}
16121 * and the {content type} of the complex type definition itself
16122 * must be the same simple type definition"
16123 * PASS
16124 */
16125 } else if ((type->contentType == XML_SCHEMA_CONTENT_EMPTY) &&
16126 (base->contentType == XML_SCHEMA_CONTENT_EMPTY) ) {
16127 /*
16128 * SPEC (1.4.2) "The {content type} of both the {base type
16129 * definition} and the complex type definition itself must
16130 * be empty."
16131 * PASS
16132 */
16133 } else {
16134 /*
16135 * SPEC (1.4.3) "All of the following must be true:"
16136 */
16137 if (type->subtypes == NULL) {
16138 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016139 * SPEC 1.4.3.1 The {content type} of the complex type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016140 * definition itself must specify a particle.
16141 */
16142 xmlSchemaPCustomErr(ctxt,
16143 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016144 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016145 "The content type must specify a particle", NULL);
16146 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16147 }
16148 /*
16149 * SPEC (1.4.3.2) "One of the following must be true:"
16150 */
16151 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16152 /*
16153 * SPEC (1.4.3.2.1) "The {content type} of the {base type
16154 * definition} must be empty.
16155 * PASS
16156 */
16157 } else {
16158 /*
16159 * SPEC (1.4.3.2.2) "All of the following must be true:"
16160 */
16161 if ((type->contentType != base->contentType) ||
16162 ((type->contentType != XML_SCHEMA_CONTENT_MIXED) &&
16163 (type->contentType != XML_SCHEMA_CONTENT_ELEMENTS))) {
16164 /*
16165 * SPEC (1.4.3.2.2.1) "Both {content type}s must be mixed
16166 * or both must be element-only."
16167 */
16168 xmlSchemaPCustomErr(ctxt,
16169 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016170 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016171 "The content type of both, the type and its base "
16172 "type, must either 'mixed' or 'element-only'", NULL);
16173 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016174 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016175 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016176 * URGENT TODO SPEC (1.4.3.2.2.2) "The particle of the
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016177 * complex type definition must be a ·valid extension·
16178 * of the {base type definition}'s particle, as defined
16179 * in Particle Valid (Extension) (§3.9.6)."
16180 *
16181 * NOTE that we won't check "Particle Valid (Extension)",
16182 * since it is ensured by the derivation process in
16183 * xmlSchemaTypeFixup(). We need to implement this when heading
16184 * for a construction API
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016185 * TODO: !! This is needed to be checked if redefining a type !!
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016186 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016187 }
16188 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016189 * URGENT TODO (1.5)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016190 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016191 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016192 } else {
16193 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016194 * SPEC (2) "If the {base type definition} is a simple type definition,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016195 * then all of the following must be true:"
William M. Brack2f2a6632004-08-20 23:09:47 +000016196 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016197 if (type->contentTypeDef != base) {
16198 /*
16199 * SPEC (2.1) "The {content type} must be the same simple type
16200 * definition."
16201 */
16202 xmlSchemaPCustomErr(ctxt,
16203 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016204 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016205 "The content type must be the simple base type", NULL);
16206 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
16207 }
16208 if (base->flags & XML_SCHEMAS_TYPE_FINAL_EXTENSION) {
16209 /*
16210 * SPEC (2.2) "The {final} of the {base type definition} must not
16211 * contain extension"
16212 * NOTE that this is the same as (1.1).
16213 */
16214 xmlSchemaPCustomErr(ctxt,
16215 XML_SCHEMAP_COS_CT_EXTENDS_1_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016216 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016217 "The 'final' of the base type definition "
16218 "contains 'extension'", NULL);
16219 return (XML_SCHEMAP_COS_CT_EXTENDS_1_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016220 }
William M. Brack2f2a6632004-08-20 23:09:47 +000016221 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016222 return (0);
William M. Brack2f2a6632004-08-20 23:09:47 +000016223}
16224
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016225/**
16226 * xmlSchemaCheckDerivationOKRestriction:
16227 * @ctxt: the schema parser context
16228 * @type: the complex type definition
16229 *
16230 * (3.4.6) Constraints on Complex Type Definition Schema Components
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016231 * Schema Component Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016232 * Derivation Valid (Restriction, Complex) (derivation-ok-restriction)
16233 *
16234 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016235 * missing:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016236 * (5.4.2) ???
16237 *
16238 * ATTENTION:
16239 * In XML Schema 1.1 this will be:
16240 * Validation Rule: Checking complex type subsumption
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016241 *
16242 * Returns 0 if the constraints are satisfied, a positive
16243 * error code if not and -1 if an internal error occured.
16244 */
16245static int
16246xmlSchemaCheckDerivationOKRestriction(xmlSchemaParserCtxtPtr ctxt,
16247 xmlSchemaTypePtr type)
16248{
16249 xmlSchemaTypePtr base;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016250
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016251 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016252 * TODO: Correct the error code; XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1 is used
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016253 * temporarily only.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016254 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016255 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016256 if (! WXS_IS_COMPLEX(base)) {
16257 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16258 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16259 type->node, WXS_BASIC_CAST type,
16260 "The base type must be a complex type", NULL, NULL);
16261 return(ctxt->err);
16262 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016263 if (base->flags & XML_SCHEMAS_TYPE_FINAL_RESTRICTION) {
16264 /*
16265 * SPEC (1) "The {base type definition} must be a complex type
16266 * definition whose {final} does not contain restriction."
16267 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016268 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16269 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16270 type->node, WXS_BASIC_CAST type,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016271 "The 'final' of the base type definition "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016272 "contains 'restriction'", NULL, NULL);
16273 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016274 }
16275 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016276 * SPEC (2), (3) and (4)
16277 * Those are handled in a separate function, since the
16278 * same constraints are needed for redefinition of
16279 * attribute groups as well.
16280 */
16281 if (xmlSchemaCheckDerivationOKRestriction2to4(ctxt,
16282 XML_SCHEMA_ACTION_DERIVE,
16283 WXS_BASIC_CAST type, WXS_BASIC_CAST base,
16284 type->attrUses, base->attrUses,
16285 type->attributeWildcard,
16286 base->attributeWildcard) == -1)
16287 {
16288 return(-1);
16289 }
16290 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016291 * SPEC (5) "One of the following must be true:"
16292 */
16293 if (base->builtInType == XML_SCHEMAS_ANYTYPE) {
16294 /*
16295 * SPEC (5.1) "The {base type definition} must be the
16296 * ·ur-type definition·."
16297 * PASS
16298 */
16299 } else if ((type->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16300 (type->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16301 /*
16302 * SPEC (5.2.1) "The {content type} of the complex type definition
16303 * must be a simple type definition"
16304 *
16305 * SPEC (5.2.2) "One of the following must be true:"
16306 */
16307 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016308 (base->contentType == XML_SCHEMA_CONTENT_BASIC))
16309 {
16310 int err;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016311 /*
16312 * SPEC (5.2.2.1) "The {content type} of the {base type
16313 * definition} must be a simple type definition from which
16314 * the {content type} is validly derived given the empty
16315 * set as defined in Type Derivation OK (Simple) (§3.14.6)."
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016316 *
16317 * ATTENTION TODO: This seems not needed if the type implicitely
16318 * derived from the base type.
16319 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016320 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016321 err = xmlSchemaCheckCOSSTDerivedOK(ACTXT_CAST ctxt,
16322 type->contentTypeDef, base->contentTypeDef, 0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016323 if (err != 0) {
16324 xmlChar *strA = NULL, *strB = NULL;
16325
16326 if (err == -1)
16327 return(-1);
16328 xmlSchemaCustomErr(ACTXT_CAST ctxt,
16329 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16330 NULL, WXS_BASIC_CAST type,
16331 "The {content type} %s is not validly derived from the "
16332 "base type's {content type} %s",
16333 xmlSchemaGetComponentDesignation(&strA,
16334 type->contentTypeDef),
16335 xmlSchemaGetComponentDesignation(&strB,
16336 base->contentTypeDef));
16337 FREE_AND_NULL(strA);
16338 FREE_AND_NULL(strB);
16339 return(ctxt->err);
16340 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016341 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
16342 (xmlSchemaIsParticleEmptiable(
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016343 (xmlSchemaParticlePtr) base->subtypes))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016344 /*
16345 * SPEC (5.2.2.2) "The {base type definition} must be mixed
16346 * and have a particle which is ·emptiable· as defined in
16347 * Particle Emptiable (§3.9.6)."
16348 * PASS
16349 */
16350 } else {
16351 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016352 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16353 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016354 "The content type of the base type must be either "
16355 "a simple type or 'mixed' and an emptiable particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016356 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016357 }
16358 } else if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16359 /*
16360 * SPEC (5.3.1) "The {content type} of the complex type itself must
16361 * be empty"
16362 */
16363 if (base->contentType == XML_SCHEMA_CONTENT_EMPTY) {
16364 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016365 * SPEC (5.3.2.1) "The {content type} of the {base type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016366 * definition} must also be empty."
16367 * PASS
16368 */
16369 } else if (((base->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
16370 (base->contentType == XML_SCHEMA_CONTENT_MIXED)) &&
16371 xmlSchemaIsParticleEmptiable(
16372 (xmlSchemaParticlePtr) base->subtypes)) {
16373 /*
16374 * SPEC (5.3.2.2) "The {content type} of the {base type
16375 * definition} must be elementOnly or mixed and have a particle
16376 * which is ·emptiable· as defined in Particle Emptiable (§3.9.6)."
16377 * PASS
16378 */
16379 } else {
16380 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016381 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16382 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016383 "The content type of the base type must be either "
16384 "empty or 'mixed' (or 'elements-only') and an emptiable "
16385 "particle", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016386 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016387 }
16388 } else if ((type->contentType == XML_SCHEMA_CONTENT_ELEMENTS) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016389 WXS_HAS_MIXED_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016390 /*
16391 * SPEC (5.4.1.1) "The {content type} of the complex type definition
16392 * itself must be element-only"
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016393 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016394 if (WXS_HAS_MIXED_CONTENT(type) && (! WXS_HAS_MIXED_CONTENT(base))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016395 /*
16396 * SPEC (5.4.1.2) "The {content type} of the complex type
16397 * definition itself and of the {base type definition} must be
16398 * mixed"
16399 */
16400 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016401 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16402 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016403 "If the content type is 'mixed', then the content type of the "
16404 "base type must also be 'mixed'", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016405 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016406 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016407 /*
16408 * SPEC (5.4.2) "The particle of the complex type definition itself
16409 * must be a ·valid restriction· of the particle of the {content
16410 * type} of the {base type definition} as defined in Particle Valid
16411 * (Restriction) (§3.9.6).
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016412 *
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016413 * URGENT TODO: (5.4.2)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016414 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016415 } else {
16416 xmlSchemaPCustomErr(ctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016417 XML_SCHEMAP_DERIVATION_OK_RESTRICTION_1,
16418 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016419 "The type is not a valid restriction of its base type", NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016420 return (ctxt->err);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016421 }
16422 return (0);
16423}
16424
16425/**
Kasimier T. Buchcik5e371a02005-04-05 15:08:05 +000016426 * xmlSchemaCheckCTComponent:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016427 * @ctxt: the schema parser context
16428 * @type: the complex type definition
16429 *
16430 * (3.4.6) Constraints on Complex Type Definition Schema Components
16431 *
16432 * Returns 0 if the constraints are satisfied, a positive
16433 * error code if not and -1 if an internal error occured.
16434 */
16435static int
16436xmlSchemaCheckCTComponent(xmlSchemaParserCtxtPtr ctxt,
16437 xmlSchemaTypePtr type)
16438{
16439 int ret;
16440 /*
16441 * Complex Type Definition Properties Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016442 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016443 ret = xmlSchemaCheckCTPropsCorrect(ctxt, type);
16444 if (ret != 0)
16445 return (ret);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016446 if (WXS_IS_EXTENSION(type))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016447 ret = xmlSchemaCheckCOSCTExtends(ctxt, type);
16448 else
16449 ret = xmlSchemaCheckDerivationOKRestriction(ctxt, type);
16450 return (ret);
16451}
16452
16453/**
16454 * xmlSchemaCheckSRCCT:
16455 * @ctxt: the schema parser context
16456 * @type: the complex type definition
16457 *
16458 * (3.4.3) Constraints on XML Representations of Complex Type Definitions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016459 * Schema Representation Constraint:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016460 * Complex Type Definition Representation OK (src-ct)
16461 *
16462 * Returns 0 if the constraints are satisfied, a positive
16463 * error code if not and -1 if an internal error occured.
16464 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016465static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016466xmlSchemaCheckSRCCT(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016467 xmlSchemaTypePtr type)
16468{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016469 xmlSchemaTypePtr base;
16470 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016471
16472 /*
16473 * TODO: Adjust the error codes here, as I used
16474 * XML_SCHEMAP_SRC_CT_1 only yet.
16475 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016476 base = type->baseType;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016477 if (! WXS_HAS_SIMPLE_CONTENT(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016478 /*
16479 * 1 If the <complexContent> alternative is chosen, the type definition
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016480 * ·resolved· to by the ·actual value· of the base [attribute]
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016481 * must be a complex type definition;
16482 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016483 if (! WXS_IS_COMPLEX(base)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016484 xmlChar *str = NULL;
16485 xmlSchemaPCustomErr(ctxt,
16486 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016487 WXS_BASIC_CAST type, type->node,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016488 "If using <complexContent>, the base type is expected to be "
16489 "a complex type. The base type '%s' is a simple type",
16490 xmlSchemaFormatQName(&str, base->targetNamespace,
16491 base->name));
16492 FREE_AND_NULL(str)
16493 return (XML_SCHEMAP_SRC_CT_1);
16494 }
16495 } else {
16496 /*
16497 * SPEC
16498 * 2 If the <simpleContent> alternative is chosen, all of the
16499 * following must be true:
16500 * 2.1 The type definition ·resolved· to by the ·actual value· of the
16501 * base [attribute] must be one of the following:
16502 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016503 if (WXS_IS_SIMPLE(base)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016504 if (WXS_IS_EXTENSION(type) == 0) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016505 xmlChar *str = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016506 /*
16507 * 2.1.3 only if the <extension> alternative is also
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016508 * chosen, a simple type definition.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016509 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016510 /* TODO: Change error code to ..._SRC_CT_2_1_3. */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016511 xmlSchemaPCustomErr(ctxt,
16512 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016513 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016514 "If using <simpleContent> and <restriction>, the base "
16515 "type must be a complex type. The base type '%s' is "
16516 "a simple type",
16517 xmlSchemaFormatQName(&str, base->targetNamespace,
16518 base->name));
16519 FREE_AND_NULL(str)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016520 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016521 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016522 } else {
16523 /* Base type is a complex type. */
16524 if ((base->contentType == XML_SCHEMA_CONTENT_SIMPLE) ||
16525 (base->contentType == XML_SCHEMA_CONTENT_BASIC)) {
16526 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016527 * 2.1.1 a complex type definition whose {content type} is a
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016528 * simple type definition;
16529 * PASS
16530 */
16531 if (base->contentTypeDef == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016532 xmlSchemaPCustomErr(ctxt, XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016533 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016534 "Internal error: xmlSchemaCheckSRCCT, "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016535 "'%s', base type has no content type",
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016536 type->name);
16537 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016538 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016539 } else if ((base->contentType == XML_SCHEMA_CONTENT_MIXED) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016540 (WXS_IS_RESTRICTION(type))) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016541
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016542 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016543 * 2.1.2 only if the <restriction> alternative is also
16544 * chosen, a complex type definition whose {content type}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016545 * is mixed and a particle emptiable.
16546 */
16547 if (! xmlSchemaIsParticleEmptiable(
16548 (xmlSchemaParticlePtr) base->subtypes)) {
16549 ret = XML_SCHEMAP_SRC_CT_1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016550 } else
16551 /*
16552 * Attention: at this point the <simpleType> child is in
16553 * ->contentTypeDef (put there during parsing).
16554 */
16555 if (type->contentTypeDef == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016556 xmlChar *str = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016557 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016558 * 2.2 If clause 2.1.2 above is satisfied, then there
16559 * must be a <simpleType> among the [children] of
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016560 * <restriction>.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016561 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016562 /* TODO: Change error code to ..._SRC_CT_2_2. */
16563 xmlSchemaPCustomErr(ctxt,
16564 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016565 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016566 "A <simpleType> is expected among the children "
16567 "of <restriction>, if <simpleContent> is used and "
16568 "the base type '%s' is a complex type",
16569 xmlSchemaFormatQName(&str, base->targetNamespace,
16570 base->name));
16571 FREE_AND_NULL(str)
16572 return (XML_SCHEMAP_SRC_CT_1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016573 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016574 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016575 ret = XML_SCHEMAP_SRC_CT_1;
16576 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016577 }
16578 if (ret > 0) {
16579 xmlChar *str = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000016580 if (WXS_IS_RESTRICTION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016581 xmlSchemaPCustomErr(ctxt,
16582 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016583 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016584 "If <simpleContent> and <restriction> is used, the "
16585 "base type must be a simple type or a complex type with "
16586 "mixed content and particle emptiable. The base type "
16587 "'%s' is none of those",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016588 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016589 base->name));
16590 } else {
16591 xmlSchemaPCustomErr(ctxt,
16592 XML_SCHEMAP_SRC_CT_1,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016593 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016594 "If <simpleContent> and <extension> is used, the "
16595 "base type must be a simple type. The base type '%s' "
16596 "is a complex type",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016597 xmlSchemaFormatQName(&str, base->targetNamespace,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016598 base->name));
16599 }
16600 FREE_AND_NULL(str)
16601 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016602 }
16603 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016604 * SPEC (3) "The corresponding complex type definition component must
16605 * satisfy the conditions set out in Constraints on Complex Type
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016606 * Definition Schema Components (§3.4.6);"
16607 * NOTE (3) will be done in xmlSchemaTypeFixup().
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016608 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016609 /*
16610 * SPEC (4) If clause 2.2.1 or clause 2.2.2 in the correspondence specification
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016611 * above for {attribute wildcard} is satisfied, the intensional
16612 * intersection must be expressible, as defined in Attribute Wildcard
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016613 * Intersection (§3.10.6).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016614 * NOTE (4) is done in xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016615 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000016616 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000016617}
William M. Brack2f2a6632004-08-20 23:09:47 +000016618
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016619#ifdef ENABLE_PARTICLE_RESTRICTION
16620/**
16621 * xmlSchemaCheckParticleRangeOK:
16622 * @ctxt: the schema parser context
16623 * @type: the complex type definition
16624 *
16625 * (3.9.6) Constraints on Particle Schema Components
16626 * Schema Component Constraint:
16627 * Occurrence Range OK (range-ok)
16628 *
16629 * STATUS: complete
16630 *
16631 * Returns 0 if the constraints are satisfied, a positive
16632 * error code if not and -1 if an internal error occured.
16633 */
16634static int
16635xmlSchemaCheckParticleRangeOK(int rmin, int rmax,
16636 int bmin, int bmax)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016637{
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016638 if (rmin < bmin)
16639 return (1);
16640 if ((bmax != UNBOUNDED) &&
16641 (rmax > bmax))
16642 return (1);
16643 return (0);
16644}
16645
16646/**
16647 * xmlSchemaCheckRCaseNameAndTypeOK:
16648 * @ctxt: the schema parser context
16649 * @r: the restricting element declaration particle
16650 * @b: the base element declaration particle
16651 *
16652 * (3.9.6) Constraints on Particle Schema Components
16653 * Schema Component Constraint:
16654 * Particle Restriction OK (Elt:Elt -- NameAndTypeOK)
16655 * (rcase-NameAndTypeOK)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016656 *
16657 * STATUS:
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016658 * MISSING (3.2.3)
16659 * CLARIFY: (3.2.2)
16660 *
16661 * Returns 0 if the constraints are satisfied, a positive
16662 * error code if not and -1 if an internal error occured.
16663 */
16664static int
16665xmlSchemaCheckRCaseNameAndTypeOK(xmlSchemaParserCtxtPtr ctxt,
16666 xmlSchemaParticlePtr r,
16667 xmlSchemaParticlePtr b)
16668{
16669 xmlSchemaElementPtr elemR, elemB;
16670
16671 /* TODO: Error codes (rcase-NameAndTypeOK). */
16672 elemR = (xmlSchemaElementPtr) r->children;
16673 elemB = (xmlSchemaElementPtr) b->children;
16674 /*
16675 * SPEC (1) "The declarations' {name}s and {target namespace}s are
16676 * the same."
16677 */
16678 if ((elemR != elemB) &&
16679 ((! xmlStrEqual(elemR->name, elemB->name)) ||
16680 (! xmlStrEqual(elemR->targetNamespace, elemB->targetNamespace))))
16681 return (1);
16682 /*
16683 * SPEC (2) "R's occurrence range is a valid restriction of B's
16684 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16685 */
16686 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16687 b->minOccurs, b->maxOccurs) != 0)
16688 return (1);
16689 /*
16690 * SPEC (3.1) "Both B's declaration's {scope} and R's declaration's
16691 * {scope} are global."
16692 */
16693 if (elemR == elemB)
16694 return (0);
16695 /*
16696 * SPEC (3.2.1) "Either B's {nillable} is true or R's {nillable} is false."
16697 */
16698 if (((elemB->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) &&
16699 (elemR->flags & XML_SCHEMAS_ELEM_NILLABLE))
16700 return (1);
16701 /*
16702 * SPEC (3.2.2) "either B's declaration's {value constraint} is absent,
16703 * or is not fixed, or R's declaration's {value constraint} is fixed
16704 * with the same value."
16705 */
16706 if ((elemB->value != NULL) && (elemB->flags & XML_SCHEMAS_ELEM_FIXED) &&
16707 ((elemR->value == NULL) ||
16708 ((elemR->flags & XML_SCHEMAS_ELEM_FIXED) == 0) ||
16709 /* TODO: Equality of the initial value or normalized or canonical? */
16710 (! xmlStrEqual(elemR->value, elemB->value))))
16711 return (1);
16712 /*
16713 * TODO: SPEC (3.2.3) "R's declaration's {identity-constraint
16714 * definitions} is a subset of B's declaration's {identity-constraint
16715 * definitions}, if any."
16716 */
16717 if (elemB->idcs != NULL) {
16718 /* TODO */
16719 }
16720 /*
16721 * SPEC (3.2.4) "R's declaration's {disallowed substitutions} is a
16722 * superset of B's declaration's {disallowed substitutions}."
16723 */
16724 if (((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) &&
16725 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) == 0)) ||
16726 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) &&
16727 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) == 0)) ||
16728 ((elemB->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) &&
16729 ((elemR->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION) == 0)))
16730 return (1);
16731 /*
16732 * SPEC (3.2.5) "R's {type definition} is validly derived given
16733 * {extension, list, union} from B's {type definition}"
16734 *
16735 * BADSPEC TODO: What's the point of adding "list" and "union" to the
16736 * set, if the corresponding constraints handle "restriction" and
16737 * "extension" only?
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016738 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016739 */
16740 {
16741 int set = 0;
16742
16743 set |= SUBSET_EXTENSION;
16744 set |= SUBSET_LIST;
16745 set |= SUBSET_UNION;
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000016746 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST ctxt, elemR->subtypes,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016747 elemB->subtypes, set) != 0)
16748 return (1);
16749 }
16750 return (0);
16751}
16752
16753/**
16754 * xmlSchemaCheckRCaseNSCompat:
16755 * @ctxt: the schema parser context
16756 * @r: the restricting element declaration particle
16757 * @b: the base wildcard particle
16758 *
16759 * (3.9.6) Constraints on Particle Schema Components
16760 * Schema Component Constraint:
16761 * Particle Derivation OK (Elt:Any -- NSCompat)
16762 * (rcase-NSCompat)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016763 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016764 * STATUS: complete
16765 *
16766 * Returns 0 if the constraints are satisfied, a positive
16767 * error code if not and -1 if an internal error occured.
16768 */
16769static int
16770xmlSchemaCheckRCaseNSCompat(xmlSchemaParserCtxtPtr ctxt,
16771 xmlSchemaParticlePtr r,
16772 xmlSchemaParticlePtr b)
16773{
16774 /* TODO:Error codes (rcase-NSCompat). */
16775 /*
16776 * SPEC "For an element declaration particle to be a ·valid restriction·
16777 * of a wildcard particle all of the following must be true:"
16778 *
16779 * SPEC (1) "The element declaration's {target namespace} is ·valid·
16780 * with respect to the wildcard's {namespace constraint} as defined by
16781 * Wildcard allows Namespace Name (§3.10.4)."
16782 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016783 if (xmlSchemaCheckCVCWildcardNamespace((xmlSchemaWildcardPtr) b->children,
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016784 ((xmlSchemaElementPtr) r->children)->targetNamespace) != 0)
16785 return (1);
16786 /*
16787 * SPEC (2) "R's occurrence range is a valid restriction of B's
16788 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16789 */
16790 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16791 b->minOccurs, b->maxOccurs) != 0)
16792 return (1);
16793
16794 return (0);
16795}
16796
16797/**
16798 * xmlSchemaCheckRCaseRecurseAsIfGroup:
16799 * @ctxt: the schema parser context
16800 * @r: the restricting element declaration particle
16801 * @b: the base model group particle
16802 *
16803 * (3.9.6) Constraints on Particle Schema Components
16804 * Schema Component Constraint:
16805 * Particle Derivation OK (Elt:All/Choice/Sequence -- RecurseAsIfGroup)
16806 * (rcase-RecurseAsIfGroup)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016807 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016808 * STATUS: TODO
16809 *
16810 * Returns 0 if the constraints are satisfied, a positive
16811 * error code if not and -1 if an internal error occured.
16812 */
16813static int
16814xmlSchemaCheckRCaseRecurseAsIfGroup(xmlSchemaParserCtxtPtr ctxt,
16815 xmlSchemaParticlePtr r,
16816 xmlSchemaParticlePtr b)
16817{
16818 /* TODO: Error codes (rcase-RecurseAsIfGroup). */
16819 TODO
16820 return (0);
16821}
16822
16823/**
16824 * xmlSchemaCheckRCaseNSSubset:
16825 * @ctxt: the schema parser context
16826 * @r: the restricting wildcard particle
16827 * @b: the base wildcard particle
16828 *
16829 * (3.9.6) Constraints on Particle Schema Components
16830 * Schema Component Constraint:
16831 * Particle Derivation OK (Any:Any -- NSSubset)
16832 * (rcase-NSSubset)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016833 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016834 * STATUS: complete
16835 *
16836 * Returns 0 if the constraints are satisfied, a positive
16837 * error code if not and -1 if an internal error occured.
16838 */
16839static int
16840xmlSchemaCheckRCaseNSSubset(xmlSchemaParserCtxtPtr ctxt,
16841 xmlSchemaParticlePtr r,
16842 xmlSchemaParticlePtr b,
16843 int isAnyTypeBase)
16844{
16845 /* TODO: Error codes (rcase-NSSubset). */
16846 /*
16847 * SPEC (1) "R's occurrence range is a valid restriction of B's
16848 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16849 */
16850 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
16851 b->minOccurs, b->maxOccurs))
16852 return (1);
16853 /*
16854 * SPEC (2) "R's {namespace constraint} must be an intensional subset
16855 * of B's {namespace constraint} as defined by Wildcard Subset (§3.10.6)."
16856 */
16857 if (xmlSchemaCheckCOSNSSubset((xmlSchemaWildcardPtr) r->children,
16858 (xmlSchemaWildcardPtr) b->children))
16859 return (1);
16860 /*
16861 * SPEC (3) "Unless B is the content model wildcard of the ·ur-type
16862 * definition·, R's {process contents} must be identical to or stronger
16863 * than B's {process contents}, where strict is stronger than lax is
16864 * stronger than skip."
16865 */
16866 if (! isAnyTypeBase) {
16867 if ( ((xmlSchemaWildcardPtr) r->children)->processContents <
16868 ((xmlSchemaWildcardPtr) b->children)->processContents)
16869 return (1);
16870 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016871
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016872 return (0);
16873}
16874
16875/**
16876 * xmlSchemaCheckCOSParticleRestrict:
16877 * @ctxt: the schema parser context
16878 * @type: the complex type definition
16879 *
16880 * (3.9.6) Constraints on Particle Schema Components
16881 * Schema Component Constraint:
16882 * Particle Valid (Restriction) (cos-particle-restrict)
16883 *
16884 * STATUS: TODO
16885 *
16886 * Returns 0 if the constraints are satisfied, a positive
16887 * error code if not and -1 if an internal error occured.
16888 */
16889static int
16890xmlSchemaCheckCOSParticleRestrict(xmlSchemaParserCtxtPtr ctxt,
16891 xmlSchemaParticlePtr r,
16892 xmlSchemaParticlePtr b)
16893{
16894 int ret = 0;
16895
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000016896 /*part = WXS_TYPE_PARTICLE(type);
16897 basePart = WXS_TYPE_PARTICLE(base);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016898 */
16899
16900 TODO
16901
16902 /*
16903 * SPEC (1) "They are the same particle."
16904 */
16905 if (r == b)
16906 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016907
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016908
16909 return (0);
16910}
16911
16912/**
16913 * xmlSchemaCheckRCaseNSRecurseCheckCardinality:
16914 * @ctxt: the schema parser context
16915 * @r: the model group particle
16916 * @b: the base wildcard particle
16917 *
16918 * (3.9.6) Constraints on Particle Schema Components
16919 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016920 * Particle Derivation OK (All/Choice/Sequence:Any --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016921 * NSRecurseCheckCardinality)
16922 * (rcase-NSRecurseCheckCardinality)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016923 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016924 * STATUS: TODO: subst-groups
16925 *
16926 * Returns 0 if the constraints are satisfied, a positive
16927 * error code if not and -1 if an internal error occured.
16928 */
16929static int
16930xmlSchemaCheckRCaseNSRecurseCheckCardinality(xmlSchemaParserCtxtPtr ctxt,
16931 xmlSchemaParticlePtr r,
16932 xmlSchemaParticlePtr b)
16933{
16934 xmlSchemaParticlePtr part;
16935 /* TODO: Error codes (rcase-NSRecurseCheckCardinality). */
16936 if ((r->children == NULL) || (r->children->children == NULL))
16937 return (-1);
16938 /*
16939 * SPEC "For a group particle to be a ·valid restriction· of a
16940 * wildcard particle..."
16941 *
16942 * SPEC (1) "Every member of the {particles} of the group is a ·valid
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016943 * restriction· of the wildcard as defined by
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016944 * Particle Valid (Restriction) (§3.9.6)."
16945 */
16946 part = (xmlSchemaParticlePtr) r->children->children;
16947 do {
16948 if (xmlSchemaCheckCOSParticleRestrict(ctxt, part, b))
16949 return (1);
16950 part = (xmlSchemaParticlePtr) part->next;
16951 } while (part != NULL);
16952 /*
16953 * SPEC (2) "The effective total range of the group [...] is a
16954 * valid restriction of B's occurrence range as defined by
16955 * Occurrence Range OK (§3.9.6)."
16956 */
16957 if (xmlSchemaCheckParticleRangeOK(
16958 xmlSchemaGetParticleTotalRangeMin(r),
16959 xmlSchemaGetParticleTotalRangeMax(r),
16960 b->minOccurs, b->maxOccurs) != 0)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016961 return (1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016962 return (0);
16963}
16964
16965/**
16966 * xmlSchemaCheckRCaseRecurse:
16967 * @ctxt: the schema parser context
16968 * @r: the <all> or <sequence> model group particle
16969 * @b: the base <all> or <sequence> model group particle
16970 *
16971 * (3.9.6) Constraints on Particle Schema Components
16972 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016973 * Particle Derivation OK (All:All,Sequence:Sequence --
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016974 Recurse)
16975 * (rcase-Recurse)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000016976 *
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000016977 * STATUS: ?
16978 * TODO: subst-groups
16979 *
16980 * Returns 0 if the constraints are satisfied, a positive
16981 * error code if not and -1 if an internal error occured.
16982 */
16983static int
16984xmlSchemaCheckRCaseRecurse(xmlSchemaParserCtxtPtr ctxt,
16985 xmlSchemaParticlePtr r,
16986 xmlSchemaParticlePtr b)
16987{
16988 /* xmlSchemaParticlePtr part; */
16989 /* TODO: Error codes (rcase-Recurse). */
16990 if ((r->children == NULL) || (b->children == NULL) ||
16991 (r->children->type != b->children->type))
16992 return (-1);
16993 /*
16994 * SPEC "For an all or sequence group particle to be a ·valid
16995 * restriction· of another group particle with the same {compositor}..."
16996 *
16997 * SPEC (1) "R's occurrence range is a valid restriction of B's
16998 * occurrence range as defined by Occurrence Range OK (§3.9.6)."
16999 */
17000 if (xmlSchemaCheckParticleRangeOK(r->minOccurs, r->maxOccurs,
17001 b->minOccurs, b->maxOccurs))
17002 return (1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017003
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000017004
17005 return (0);
17006}
17007
17008#endif
17009
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017010#define FACET_RESTR_MUTUAL_ERR(fac1, fac2) \
17011 xmlSchemaPCustomErrExt(pctxt, \
17012 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017013 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017014 "It is an error for both '%s' and '%s' to be specified on the "\
17015 "same type definition", \
17016 BAD_CAST xmlSchemaFacetTypeToString(fac1->type), \
17017 BAD_CAST xmlSchemaFacetTypeToString(fac2->type), NULL);
17018
17019#define FACET_RESTR_ERR(fac1, msg) \
17020 xmlSchemaPCustomErr(pctxt, \
17021 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017022 WXS_BASIC_CAST fac1, fac1->node, \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017023 msg, NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017024
17025#define FACET_RESTR_FIXED_ERR(fac) \
17026 xmlSchemaPCustomErr(pctxt, \
17027 XML_SCHEMAP_INVALID_FACET_VALUE, \
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017028 WXS_BASIC_CAST fac, fac->node, \
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017029 "The base type's facet is 'fixed', thus the value must not " \
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017030 "differ", NULL);
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017031
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017032static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017033xmlSchemaDeriveFacetErr(xmlSchemaParserCtxtPtr pctxt,
17034 xmlSchemaFacetPtr facet1,
17035 xmlSchemaFacetPtr facet2,
17036 int lessGreater,
17037 int orEqual,
17038 int ofBase)
17039{
17040 xmlChar *msg = NULL;
17041
17042 msg = xmlStrdup(BAD_CAST "'");
17043 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet1->type));
17044 msg = xmlStrcat(msg, BAD_CAST "' has to be");
17045 if (lessGreater == 0)
17046 msg = xmlStrcat(msg, BAD_CAST " equal to");
17047 if (lessGreater == 1)
17048 msg = xmlStrcat(msg, BAD_CAST " greater than");
17049 else
17050 msg = xmlStrcat(msg, BAD_CAST " less than");
17051
17052 if (orEqual)
17053 msg = xmlStrcat(msg, BAD_CAST " or equal to");
17054 msg = xmlStrcat(msg, BAD_CAST " '");
17055 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facet2->type));
17056 if (ofBase)
17057 msg = xmlStrcat(msg, BAD_CAST "' of the base type");
17058 else
17059 msg = xmlStrcat(msg, BAD_CAST "'");
17060
17061 xmlSchemaPCustomErr(pctxt,
17062 XML_SCHEMAP_INVALID_FACET_VALUE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017063 WXS_BASIC_CAST facet1, NULL,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017064 (const char *) msg, NULL);
17065
17066 if (msg != NULL)
17067 xmlFree(msg);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000017068}
17069
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017070/*
17071* xmlSchemaDeriveAndValidateFacets:
17072*
17073* Schema Component Constraint: Simple Type Restriction (Facets)
17074* (st-restrict-facets)
17075*/
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017076static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017077xmlSchemaDeriveAndValidateFacets(xmlSchemaParserCtxtPtr pctxt,
17078 xmlSchemaTypePtr type)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017079{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017080 xmlSchemaTypePtr base = type->baseType;
17081 xmlSchemaFacetLinkPtr link, cur, last = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017082 xmlSchemaFacetPtr facet, bfacet,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017083 flength = NULL, ftotdig = NULL, ffracdig = NULL,
17084 fmaxlen = NULL, fminlen = NULL, /* facets of the current type */
17085 fmininc = NULL, fmaxinc = NULL,
17086 fminexc = NULL, fmaxexc = NULL,
17087 bflength = NULL, bftotdig = NULL, bffracdig = NULL,
17088 bfmaxlen = NULL, bfminlen = NULL, /* facets of the base type */
17089 bfmininc = NULL, bfmaxinc = NULL,
17090 bfminexc = NULL, bfmaxexc = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017091 int res; /* err = 0, fixedErr; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017092
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017093 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017094 * SPEC st-restrict-facets 1:
17095 * "The {variety} of R is the same as that of B."
17096 */
17097 /*
17098 * SPEC st-restrict-facets 2:
17099 * "If {variety} is atomic, the {primitive type definition}
17100 * of R is the same as that of B."
17101 *
17102 * NOTE: we leave 1 & 2 out for now, since this will be
17103 * satisfied by the derivation process.
17104 * CONSTRUCTION TODO: Maybe needed if using a construction API.
17105 */
17106 /*
17107 * SPEC st-restrict-facets 3:
17108 * "The {facets} of R are the union of S and the {facets}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017109 * of B, eliminating duplicates. To eliminate duplicates,
17110 * when a facet of the same kind occurs in both S and the
17111 * {facets} of B, the one in the {facets} of B is not
17112 * included, with the exception of enumeration and pattern
17113 * facets, for which multiple occurrences with distinct values
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017114 * are allowed."
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017115 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017116
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017117 if ((type->facetSet == NULL) && (base->facetSet == NULL))
17118 return (0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017119
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017120 last = type->facetSet;
17121 if (last != NULL)
17122 while (last->next != NULL)
17123 last = last->next;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017124
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017125 for (cur = type->facetSet; cur != NULL; cur = cur->next) {
17126 facet = cur->facet;
17127 switch (facet->type) {
17128 case XML_SCHEMA_FACET_LENGTH:
17129 flength = facet; break;
17130 case XML_SCHEMA_FACET_MINLENGTH:
17131 fminlen = facet; break;
17132 case XML_SCHEMA_FACET_MININCLUSIVE:
17133 fmininc = facet; break;
17134 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17135 fminexc = facet; break;
17136 case XML_SCHEMA_FACET_MAXLENGTH:
17137 fmaxlen = facet; break;
17138 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17139 fmaxinc = facet; break;
17140 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17141 fmaxexc = facet; break;
17142 case XML_SCHEMA_FACET_TOTALDIGITS:
17143 ftotdig = facet; break;
17144 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17145 ffracdig = facet; break;
17146 default:
17147 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017148 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017149 }
17150 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17151 facet = cur->facet;
17152 switch (facet->type) {
17153 case XML_SCHEMA_FACET_LENGTH:
17154 bflength = facet; break;
17155 case XML_SCHEMA_FACET_MINLENGTH:
17156 bfminlen = facet; break;
17157 case XML_SCHEMA_FACET_MININCLUSIVE:
17158 bfmininc = facet; break;
17159 case XML_SCHEMA_FACET_MINEXCLUSIVE:
17160 bfminexc = facet; break;
17161 case XML_SCHEMA_FACET_MAXLENGTH:
17162 bfmaxlen = facet; break;
17163 case XML_SCHEMA_FACET_MAXINCLUSIVE:
17164 bfmaxinc = facet; break;
17165 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
17166 bfmaxexc = facet; break;
17167 case XML_SCHEMA_FACET_TOTALDIGITS:
17168 bftotdig = facet; break;
17169 case XML_SCHEMA_FACET_FRACTIONDIGITS:
17170 bffracdig = facet; break;
17171 default:
17172 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017173 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017174 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017175 /*
17176 * length and minLength or maxLength (2.2) + (3.2)
17177 */
17178 if (flength && (fminlen || fmaxlen)) {
17179 FACET_RESTR_ERR(flength, "It is an error for both 'length' and "
17180 "either of 'minLength' or 'maxLength' to be specified on "
17181 "the same type definition")
17182 }
17183 /*
17184 * Mutual exclusions in the same derivation step.
17185 */
17186 if ((fmaxinc) && (fmaxexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017187 /*
17188 * SCC "maxInclusive and maxExclusive"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017189 */
17190 FACET_RESTR_MUTUAL_ERR(fmaxinc, fmaxexc)
17191 }
17192 if ((fmininc) && (fminexc)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017193 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017194 * SCC "minInclusive and minExclusive"
17195 */
17196 FACET_RESTR_MUTUAL_ERR(fmininc, fminexc)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017197 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017198
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017199 if (flength && bflength) {
17200 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017201 * SCC "length valid restriction"
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017202 * The values have to be equal.
17203 */
17204 res = xmlSchemaCompareValues(flength->val, bflength->val);
17205 if (res == -2)
17206 goto internal_error;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017207 if (res != 0)
17208 xmlSchemaDeriveFacetErr(pctxt, flength, bflength, 0, 0, 1);
17209 if ((res != 0) && (bflength->fixed)) {
17210 FACET_RESTR_FIXED_ERR(flength)
17211 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017212
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017213 }
17214 if (fminlen && bfminlen) {
17215 /*
17216 * SCC "minLength valid restriction"
17217 * minLength >= BASE minLength
17218 */
17219 res = xmlSchemaCompareValues(fminlen->val, bfminlen->val);
17220 if (res == -2)
17221 goto internal_error;
17222 if (res == -1)
17223 xmlSchemaDeriveFacetErr(pctxt, fminlen, bfminlen, 1, 1, 1);
17224 if ((res != 0) && (bfminlen->fixed)) {
17225 FACET_RESTR_FIXED_ERR(fminlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017226 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017227 }
17228 if (fmaxlen && bfmaxlen) {
17229 /*
17230 * SCC "maxLength valid restriction"
17231 * maxLength <= BASE minLength
17232 */
17233 res = xmlSchemaCompareValues(fmaxlen->val, bfmaxlen->val);
17234 if (res == -2)
17235 goto internal_error;
17236 if (res == 1)
17237 xmlSchemaDeriveFacetErr(pctxt, fmaxlen, bfmaxlen, -1, 1, 1);
17238 if ((res != 0) && (bfmaxlen->fixed)) {
17239 FACET_RESTR_FIXED_ERR(fmaxlen)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017240 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017241 }
17242 /*
17243 * SCC "length and minLength or maxLength"
17244 */
17245 if (! flength)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017246 flength = bflength;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017247 if (flength) {
17248 if (! fminlen)
17249 flength = bflength;
17250 if (fminlen) {
17251 /* (1.1) length >= minLength */
17252 res = xmlSchemaCompareValues(flength->val, fminlen->val);
17253 if (res == -2)
17254 goto internal_error;
17255 if (res == -1)
17256 xmlSchemaDeriveFacetErr(pctxt, flength, fminlen, 1, 1, 0);
17257 }
17258 if (! fmaxlen)
17259 fmaxlen = bfmaxlen;
17260 if (fmaxlen) {
17261 /* (2.1) length <= maxLength */
17262 res = xmlSchemaCompareValues(flength->val, fmaxlen->val);
17263 if (res == -2)
17264 goto internal_error;
17265 if (res == 1)
17266 xmlSchemaDeriveFacetErr(pctxt, flength, fmaxlen, -1, 1, 0);
17267 }
17268 }
17269 if (fmaxinc) {
17270 /*
17271 * "maxInclusive"
17272 */
17273 if (fmininc) {
17274 /* SCC "maxInclusive >= minInclusive" */
17275 res = xmlSchemaCompareValues(fmaxinc->val, fmininc->val);
17276 if (res == -2)
17277 goto internal_error;
17278 if (res == -1) {
17279 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, fmininc, 1, 1, 0);
17280 }
17281 }
17282 /*
17283 * SCC "maxInclusive valid restriction"
17284 */
17285 if (bfmaxinc) {
17286 /* maxInclusive <= BASE maxInclusive */
17287 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxinc->val);
17288 if (res == -2)
17289 goto internal_error;
17290 if (res == 1)
17291 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxinc, -1, 1, 1);
17292 if ((res != 0) && (bfmaxinc->fixed)) {
17293 FACET_RESTR_FIXED_ERR(fmaxinc)
17294 }
17295 }
17296 if (bfmaxexc) {
17297 /* maxInclusive < BASE maxExclusive */
17298 res = xmlSchemaCompareValues(fmaxinc->val, bfmaxexc->val);
17299 if (res == -2)
17300 goto internal_error;
17301 if (res != -1) {
17302 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmaxexc, -1, 0, 1);
17303 }
17304 }
17305 if (bfmininc) {
17306 /* maxInclusive >= BASE minInclusive */
17307 res = xmlSchemaCompareValues(fmaxinc->val, bfmininc->val);
17308 if (res == -2)
17309 goto internal_error;
17310 if (res == -1) {
17311 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfmininc, 1, 1, 1);
17312 }
17313 }
17314 if (bfminexc) {
17315 /* maxInclusive > BASE minExclusive */
17316 res = xmlSchemaCompareValues(fmaxinc->val, bfminexc->val);
17317 if (res == -2)
17318 goto internal_error;
17319 if (res != 1) {
17320 xmlSchemaDeriveFacetErr(pctxt, fmaxinc, bfminexc, 1, 0, 1);
17321 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017322 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017323 }
17324 if (fmaxexc) {
17325 /*
17326 * "maxExclusive >= minExclusive"
17327 */
17328 if (fminexc) {
17329 res = xmlSchemaCompareValues(fmaxexc->val, fminexc->val);
17330 if (res == -2)
17331 goto internal_error;
17332 if (res == -1) {
17333 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, fminexc, 1, 1, 0);
17334 }
17335 }
17336 /*
17337 * "maxExclusive valid restriction"
17338 */
17339 if (bfmaxexc) {
17340 /* maxExclusive <= BASE maxExclusive */
17341 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxexc->val);
17342 if (res == -2)
17343 goto internal_error;
17344 if (res == 1) {
17345 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxexc, -1, 1, 1);
17346 }
17347 if ((res != 0) && (bfmaxexc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017348 FACET_RESTR_FIXED_ERR(fmaxexc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017349 }
17350 }
17351 if (bfmaxinc) {
17352 /* maxExclusive <= BASE maxInclusive */
17353 res = xmlSchemaCompareValues(fmaxexc->val, bfmaxinc->val);
17354 if (res == -2)
17355 goto internal_error;
17356 if (res == 1) {
17357 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmaxinc, -1, 1, 1);
17358 }
17359 }
17360 if (bfmininc) {
17361 /* maxExclusive > BASE minInclusive */
17362 res = xmlSchemaCompareValues(fmaxexc->val, bfmininc->val);
17363 if (res == -2)
17364 goto internal_error;
17365 if (res != 1) {
17366 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfmininc, 1, 0, 1);
17367 }
17368 }
17369 if (bfminexc) {
17370 /* maxExclusive > BASE minExclusive */
17371 res = xmlSchemaCompareValues(fmaxexc->val, bfminexc->val);
17372 if (res == -2)
17373 goto internal_error;
17374 if (res != 1) {
17375 xmlSchemaDeriveFacetErr(pctxt, fmaxexc, bfminexc, 1, 0, 1);
17376 }
17377 }
17378 }
17379 if (fminexc) {
17380 /*
17381 * "minExclusive < maxInclusive"
17382 */
17383 if (fmaxinc) {
17384 res = xmlSchemaCompareValues(fminexc->val, fmaxinc->val);
17385 if (res == -2)
17386 goto internal_error;
17387 if (res != -1) {
17388 xmlSchemaDeriveFacetErr(pctxt, fminexc, fmaxinc, -1, 0, 0);
17389 }
17390 }
17391 /*
17392 * "minExclusive valid restriction"
17393 */
17394 if (bfminexc) {
17395 /* minExclusive >= BASE minExclusive */
17396 res = xmlSchemaCompareValues(fminexc->val, bfminexc->val);
17397 if (res == -2)
17398 goto internal_error;
17399 if (res == -1) {
17400 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfminexc, 1, 1, 1);
17401 }
17402 if ((res != 0) && (bfminexc->fixed)) {
17403 FACET_RESTR_FIXED_ERR(fminexc)
17404 }
17405 }
17406 if (bfmaxinc) {
17407 /* minExclusive <= BASE maxInclusive */
17408 res = xmlSchemaCompareValues(fminexc->val, bfmaxinc->val);
17409 if (res == -2)
17410 goto internal_error;
17411 if (res == 1) {
17412 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxinc, -1, 1, 1);
17413 }
17414 }
17415 if (bfmininc) {
17416 /* minExclusive >= BASE minInclusive */
17417 res = xmlSchemaCompareValues(fminexc->val, bfmininc->val);
17418 if (res == -2)
17419 goto internal_error;
17420 if (res == -1) {
17421 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmininc, 1, 1, 1);
17422 }
17423 }
17424 if (bfmaxexc) {
17425 /* minExclusive < BASE maxExclusive */
17426 res = xmlSchemaCompareValues(fminexc->val, bfmaxexc->val);
17427 if (res == -2)
17428 goto internal_error;
17429 if (res != -1) {
17430 xmlSchemaDeriveFacetErr(pctxt, fminexc, bfmaxexc, -1, 0, 1);
17431 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017432 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017433 }
17434 if (fmininc) {
17435 /*
17436 * "minInclusive < maxExclusive"
17437 */
17438 if (fmaxexc) {
17439 res = xmlSchemaCompareValues(fmininc->val, fmaxexc->val);
17440 if (res == -2)
17441 goto internal_error;
17442 if (res != -1) {
17443 xmlSchemaDeriveFacetErr(pctxt, fmininc, fmaxexc, -1, 0, 0);
17444 }
17445 }
17446 /*
17447 * "minExclusive valid restriction"
17448 */
17449 if (bfmininc) {
17450 /* minInclusive >= BASE minInclusive */
17451 res = xmlSchemaCompareValues(fmininc->val, bfmininc->val);
17452 if (res == -2)
17453 goto internal_error;
17454 if (res == -1) {
17455 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmininc, 1, 1, 1);
17456 }
17457 if ((res != 0) && (bfmininc->fixed)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017458 FACET_RESTR_FIXED_ERR(fmininc)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017459 }
17460 }
17461 if (bfmaxinc) {
17462 /* minInclusive <= BASE maxInclusive */
17463 res = xmlSchemaCompareValues(fmininc->val, bfmaxinc->val);
17464 if (res == -2)
17465 goto internal_error;
Daniel Veillard0a119eb2005-07-20 13:46:00 +000017466 if (res == 1) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017467 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxinc, -1, 1, 1);
17468 }
17469 }
17470 if (bfminexc) {
17471 /* minInclusive > BASE minExclusive */
17472 res = xmlSchemaCompareValues(fmininc->val, bfminexc->val);
17473 if (res == -2)
17474 goto internal_error;
17475 if (res != 1)
17476 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfminexc, 1, 0, 1);
17477 }
17478 if (bfmaxexc) {
17479 /* minInclusive < BASE maxExclusive */
17480 res = xmlSchemaCompareValues(fmininc->val, bfmaxexc->val);
17481 if (res == -2)
17482 goto internal_error;
17483 if (res != -1)
17484 xmlSchemaDeriveFacetErr(pctxt, fmininc, bfmaxexc, -1, 0, 1);
17485 }
17486 }
17487 if (ftotdig && bftotdig) {
17488 /*
17489 * SCC " totalDigits valid restriction"
17490 * totalDigits <= BASE totalDigits
17491 */
17492 res = xmlSchemaCompareValues(ftotdig->val, bftotdig->val);
17493 if (res == -2)
17494 goto internal_error;
17495 if (res == 1)
17496 xmlSchemaDeriveFacetErr(pctxt, ftotdig, bftotdig,
17497 -1, 1, 1);
17498 if ((res != 0) && (bftotdig->fixed)) {
17499 FACET_RESTR_FIXED_ERR(ftotdig)
17500 }
17501 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017502 if (ffracdig && bffracdig) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017503 /*
17504 * SCC "fractionDigits valid restriction"
17505 * fractionDigits <= BASE fractionDigits
17506 */
17507 res = xmlSchemaCompareValues(ffracdig->val, bffracdig->val);
17508 if (res == -2)
17509 goto internal_error;
17510 if (res == 1)
17511 xmlSchemaDeriveFacetErr(pctxt, ffracdig, bffracdig,
17512 -1, 1, 1);
17513 if ((res != 0) && (bffracdig->fixed)) {
17514 FACET_RESTR_FIXED_ERR(ffracdig)
17515 }
17516 }
17517 /*
17518 * SCC "fractionDigits less than or equal to totalDigits"
17519 */
17520 if (! ftotdig)
17521 ftotdig = bftotdig;
17522 if (! ffracdig)
17523 ffracdig = bffracdig;
17524 if (ftotdig && ffracdig) {
17525 res = xmlSchemaCompareValues(ffracdig->val, ftotdig->val);
17526 if (res == -2)
17527 goto internal_error;
17528 if (res == 1)
17529 xmlSchemaDeriveFacetErr(pctxt, ffracdig, ftotdig,
17530 -1, 1, 0);
17531 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017532 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017533 * *Enumerations* won' be added here, since only the first set
17534 * of enumerations in the ancestor-or-self axis is used
17535 * for validation, plus we need to use the base type of those
17536 * enumerations for whitespace.
17537 *
17538 * *Patterns*: won't be add here, since they are ORed at
17539 * type level and ANDed at ancestor level. This will
17540 * happed during validation by walking the base axis
17541 * of the type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017542 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017543 for (cur = base->facetSet; cur != NULL; cur = cur->next) {
17544 bfacet = cur->facet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017545 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017546 * Special handling of enumerations and patterns.
17547 * TODO: hmm, they should not appear in the set, so remove this.
17548 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017549 if ((bfacet->type == XML_SCHEMA_FACET_PATTERN) ||
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017550 (bfacet->type == XML_SCHEMA_FACET_ENUMERATION))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017551 continue;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017552 /*
17553 * Search for a duplicate facet in the current type.
17554 */
17555 link = type->facetSet;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000017556 /* err = 0; */
17557 /* fixedErr = 0; */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017558 while (link != NULL) {
17559 facet = link->facet;
17560 if (facet->type == bfacet->type) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017561 switch (facet->type) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017562 case XML_SCHEMA_FACET_WHITESPACE:
17563 /*
17564 * The whitespace must be stronger.
17565 */
17566 if (facet->whitespace < bfacet->whitespace) {
17567 FACET_RESTR_ERR(flength,
17568 "The 'whitespace' value has to be equal to "
17569 "or stronger than the 'whitespace' value of "
17570 "the base type")
17571 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017572 if ((bfacet->fixed) &&
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017573 (facet->whitespace != bfacet->whitespace)) {
17574 FACET_RESTR_FIXED_ERR(facet)
17575 }
17576 break;
17577 default:
17578 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017579 }
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017580 /* Duplicate found. */
17581 break;
17582 }
17583 link = link->next;
17584 }
17585 /*
17586 * If no duplicate was found: add the base types's facet
17587 * to the set.
17588 */
17589 if (link == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017590 link = (xmlSchemaFacetLinkPtr)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017591 xmlMalloc(sizeof(xmlSchemaFacetLink));
17592 if (link == NULL) {
17593 xmlSchemaPErrMemory(pctxt,
17594 "deriving facets, creating a facet link", NULL);
17595 return (-1);
17596 }
17597 link->facet = cur->facet;
17598 link->next = NULL;
17599 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017600 type->facetSet = link;
17601 else
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017602 last->next = link;
17603 last = link;
17604 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017605
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017606 }
17607
17608 return (0);
17609internal_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017610 PERROR_INT("xmlSchemaDeriveAndValidateFacets",
17611 "an error occured");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017612 return (-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017613}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000017614
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017615static int
17616xmlSchemaFinishMemberTypeDefinitionsProperty(xmlSchemaParserCtxtPtr pctxt,
17617 xmlSchemaTypePtr type)
17618{
17619 xmlSchemaTypeLinkPtr link, lastLink, prevLink, subLink, newLink;
17620 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017621 * The actual value is then formed by replacing any union type
17622 * definition in the ·explicit members· with the members of their
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017623 * {member type definitions}, in order.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017624 *
17625 * TODO: There's a bug entry at
17626 * "http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0287.html"
17627 * which indicates that we'll keep the union types the future.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017628 */
17629 link = type->memberTypes;
17630 while (link != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017631
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017632 if (WXS_IS_TYPE_NOT_FIXED(link->type))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000017633 xmlSchemaTypeFixup(link->type, ACTXT_CAST pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017634
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017635 if (WXS_IS_UNION(link->type)) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017636 subLink = xmlSchemaGetUnionSimpleTypeMemberTypes(link->type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017637 if (subLink != NULL) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017638 link->type = subLink->type;
17639 if (subLink->next != NULL) {
17640 lastLink = link->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017641 subLink = subLink->next;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017642 prevLink = link;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017643 while (subLink != NULL) {
17644 newLink = (xmlSchemaTypeLinkPtr)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017645 xmlMalloc(sizeof(xmlSchemaTypeLink));
17646 if (newLink == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017647 xmlSchemaPErrMemory(pctxt, "allocating a type link",
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017648 NULL);
17649 return (-1);
17650 }
17651 newLink->type = subLink->type;
17652 prevLink->next = newLink;
17653 prevLink = newLink;
17654 newLink->next = lastLink;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017655
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017656 subLink = subLink->next;
17657 }
17658 }
17659 }
17660 }
17661 link = link->next;
17662 }
17663 return (0);
17664}
17665
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017666static void
17667xmlSchemaTypeFixupOptimFacets(xmlSchemaTypePtr type)
17668{
17669 int has = 0, needVal = 0, normVal = 0;
17670
17671 has = (type->baseType->flags & XML_SCHEMAS_TYPE_HAS_FACETS) ? 1 : 0;
17672 if (has) {
17673 needVal = (type->baseType->flags &
17674 XML_SCHEMAS_TYPE_FACETSNEEDVALUE) ? 1 : 0;
17675 normVal = (type->baseType->flags &
17676 XML_SCHEMAS_TYPE_NORMVALUENEEDED) ? 1 : 0;
17677 }
17678 if (type->facets != NULL) {
17679 xmlSchemaFacetPtr fac;
17680
17681 for (fac = type->facets; fac != NULL; fac = fac->next) {
17682 switch (fac->type) {
17683 case XML_SCHEMA_FACET_WHITESPACE:
17684 break;
17685 case XML_SCHEMA_FACET_PATTERN:
17686 normVal = 1;
17687 has = 1;
17688 break;
17689 case XML_SCHEMA_FACET_ENUMERATION:
17690 needVal = 1;
17691 normVal = 1;
17692 has = 1;
17693 break;
17694 default:
17695 has = 1;
17696 break;
17697 }
17698 }
17699 }
17700 if (normVal)
17701 type->flags |= XML_SCHEMAS_TYPE_NORMVALUENEEDED;
17702 if (needVal)
17703 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17704 if (has)
17705 type->flags |= XML_SCHEMAS_TYPE_HAS_FACETS;
17706
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017707 if (has && (! needVal) && WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017708 xmlSchemaTypePtr prim = xmlSchemaGetPrimitiveType(type);
17709 /*
17710 * OPTIMIZE VAL TODO: Some facets need a computed value.
17711 */
17712 if ((prim->builtInType != XML_SCHEMAS_ANYSIMPLETYPE) &&
17713 (prim->builtInType != XML_SCHEMAS_STRING)) {
17714 type->flags |= XML_SCHEMAS_TYPE_FACETSNEEDVALUE;
17715 }
17716 }
17717}
17718
17719static int
17720xmlSchemaTypeFixupWhitespace(xmlSchemaTypePtr type)
17721{
17722
17723
17724 /*
17725 * Evaluate the whitespace-facet value.
17726 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017727 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017728 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17729 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017730 } else if (WXS_IS_UNION(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017731 return (0);
17732
17733 if (type->facetSet != NULL) {
17734 xmlSchemaFacetLinkPtr lin;
17735
17736 for (lin = type->facetSet; lin != NULL; lin = lin->next) {
17737 if (lin->facet->type == XML_SCHEMA_FACET_WHITESPACE) {
17738 switch (lin->facet->whitespace) {
17739 case XML_SCHEMAS_FACET_PRESERVE:
17740 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17741 break;
17742 case XML_SCHEMAS_FACET_REPLACE:
17743 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17744 break;
17745 case XML_SCHEMAS_FACET_COLLAPSE:
17746 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17747 break;
17748 default:
17749 return (-1);
17750 }
17751 return (0);
17752 }
17753 }
17754 }
17755 /*
17756 * For all ·atomic· datatypes other than string (and types ·derived·
17757 * by ·restriction· from it) the value of whiteSpace is fixed to
17758 * collapse
17759 */
17760 {
17761 xmlSchemaTypePtr anc;
17762
17763 for (anc = type->baseType; anc != NULL &&
17764 anc->builtInType != XML_SCHEMAS_ANYTYPE;
17765 anc = anc->baseType) {
17766
17767 if (anc->type == XML_SCHEMA_TYPE_BASIC) {
17768 if (anc->builtInType == XML_SCHEMAS_NORMSTRING) {
17769 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_REPLACE;
17770
17771 } else if ((anc->builtInType == XML_SCHEMAS_STRING) ||
17772 (anc->builtInType == XML_SCHEMAS_ANYSIMPLETYPE)) {
17773 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE;
17774
17775 } else
17776 type->flags |= XML_SCHEMAS_TYPE_WHITESPACE_COLLAPSE;
17777 break;
17778 }
17779 }
17780 return (0);
17781 }
17782 return (0);
17783}
17784
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017785static int
17786xmlSchemaFixupSimpleTypeStageOne(xmlSchemaParserCtxtPtr pctxt,
17787 xmlSchemaTypePtr type)
Daniel Veillard4255d502002-04-16 15:50:10 +000017788{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017789 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17790 return(0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017791 if (! WXS_IS_TYPE_NOT_FIXED_1(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017792 return(0);
17793 type->flags |= XML_SCHEMAS_TYPE_FIXUP_1;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000017794
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017795 if (WXS_IS_LIST(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017796 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017797 * Corresponds to <simpleType><list>...
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017798 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017799 if (type->subtypes == NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017800 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017801 * This one is really needed, so get out.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017802 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017803 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017804 "list type has no item-type assigned");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017805 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017806 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017807 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017808 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017809 * Corresponds to <simpleType><union>...
17810 */
17811 if (type->memberTypes == NULL) {
17812 /*
17813 * This one is really needed, so get out.
17814 */
17815 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17816 "union type has no member-types assigned");
17817 return(-1);
17818 }
17819 } else {
17820 /*
17821 * Corresponds to <simpleType><restriction>...
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017822 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017823 if (type->baseType == NULL) {
17824 PERROR_INT("xmlSchemaFixupSimpleTypeStageOne",
17825 "type has no base-type assigned");
17826 return(-1);
17827 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017828 if (WXS_IS_TYPE_NOT_FIXED_1(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017829 if (xmlSchemaFixupSimpleTypeStageOne(pctxt, type->baseType) == -1)
17830 return(-1);
17831 /*
17832 * Variety
17833 * If the <restriction> alternative is chosen, then the
17834 * {variety} of the {base type definition}.
17835 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017836 if (WXS_IS_ATOMIC(type->baseType))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017837 type->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017838 else if (WXS_IS_LIST(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017839 type->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017840 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017841 * Inherit the itemType.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017842 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017843 type->subtypes = type->baseType->subtypes;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017844 } else if (WXS_IS_UNION(type->baseType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017845 type->flags |= XML_SCHEMAS_TYPE_VARIETY_UNION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017846 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017847 * NOTE that we won't assign the memberTypes of the base,
17848 * since this will make trouble when freeing them; we will
17849 * use a lookup function to access them instead.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017850 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017851 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017852 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017853 return(0);
17854}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017855
Daniel Veillard8651f532002-04-17 09:06:27 +000017856#ifdef DEBUG_TYPE
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017857xmlSchemaDebugFixedType(xmlSchemaParserCtxtPtr pctxt,
17858 xmlSchemaTypePtr type)
17859{
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017860 if (type->node != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017861 xmlGenericError(xmlGenericErrorContext,
17862 "Type of %s : %s:%d :", name,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017863 type->node->doc->URL,
17864 xmlGetLineNo(type->node));
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017865 } else {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000017866 xmlGenericError(xmlGenericErrorContext, "Type of %s :", name);
Daniel Veillarddecd64d2002-04-18 14:41:51 +000017867 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017868 if ((WXS_IS_SIMPLE(type)) || (WXS_IS_COMPLEX(type))) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017869 switch (type->contentType) {
17870 case XML_SCHEMA_CONTENT_SIMPLE:
17871 xmlGenericError(xmlGenericErrorContext, "simple\n");
17872 break;
17873 case XML_SCHEMA_CONTENT_ELEMENTS:
17874 xmlGenericError(xmlGenericErrorContext, "elements\n");
17875 break;
17876 case XML_SCHEMA_CONTENT_UNKNOWN:
17877 xmlGenericError(xmlGenericErrorContext, "unknown !!!\n");
17878 break;
17879 case XML_SCHEMA_CONTENT_EMPTY:
17880 xmlGenericError(xmlGenericErrorContext, "empty\n");
17881 break;
17882 case XML_SCHEMA_CONTENT_MIXED:
17883 if (xmlSchemaIsParticleEmptiable((xmlSchemaParticlePtr)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000017884 type->subtypes))
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000017885 xmlGenericError(xmlGenericErrorContext,
17886 "mixed as emptiable particle\n");
17887 else
17888 xmlGenericError(xmlGenericErrorContext, "mixed\n");
17889 break;
17890 /* Removed, since not used. */
17891 /*
17892 case XML_SCHEMA_CONTENT_MIXED_OR_ELEMENTS:
17893 xmlGenericError(xmlGenericErrorContext, "mixed or elems\n");
17894 break;
17895 */
17896 case XML_SCHEMA_CONTENT_BASIC:
17897 xmlGenericError(xmlGenericErrorContext, "basic\n");
17898 break;
17899 default:
17900 xmlGenericError(xmlGenericErrorContext,
17901 "not registered !!!\n");
17902 break;
17903 }
Daniel Veillard8651f532002-04-17 09:06:27 +000017904 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017905}
Daniel Veillard8651f532002-04-17 09:06:27 +000017906#endif
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017907
17908/*
17909* 3.14.6 Constraints on Simple Type Definition Schema Components
17910*/
17911static int
17912xmlSchemaFixupSimpleTypeStageTwo(xmlSchemaParserCtxtPtr pctxt,
17913 xmlSchemaTypePtr type)
17914{
17915 int res, olderrs = pctxt->nberrors;
17916
17917 if (type->type != XML_SCHEMA_TYPE_SIMPLE)
17918 return(-1);
17919
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017920 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017921 return(0);
17922
17923 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
17924 type->contentType = XML_SCHEMA_CONTENT_SIMPLE;
17925
17926 if (type->baseType == NULL) {
17927 PERROR_INT("xmlSchemaFixupSimpleTypeStageTwo",
17928 "missing baseType");
17929 goto exit_failure;
17930 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017931 if (WXS_IS_TYPE_NOT_FIXED(type->baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000017932 xmlSchemaTypeFixup(type->baseType, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017933 /*
17934 * If a member type of a union is a union itself, we need to substitute
17935 * that member type for its member types.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000017936 * NOTE that this might change in WXS 1.1; i.e. we will keep the union
17937 * types in WXS 1.1.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000017938 */
17939 if ((type->memberTypes != NULL) &&
17940 (xmlSchemaFinishMemberTypeDefinitionsProperty(pctxt, type) == -1))
17941 return(-1);
17942 /*
17943 * SPEC src-simple-type 1
17944 * "The corresponding simple type definition, if any, must satisfy
17945 * the conditions set out in Constraints on Simple Type Definition
17946 * Schema Components (§3.14.6)."
17947 */
17948 /*
17949 * Schema Component Constraint: Simple Type Definition Properties Correct
17950 * (st-props-correct)
17951 */
17952 res = xmlSchemaCheckSTPropsCorrect(pctxt, type);
17953 HFAILURE HERROR
17954 /*
17955 * Schema Component Constraint: Derivation Valid (Restriction, Simple)
17956 * (cos-st-restricts)
17957 */
17958 res = xmlSchemaCheckCOSSTRestricts(pctxt, type);
17959 HFAILURE HERROR
17960 /*
17961 * TODO: Removed the error report, since it got annoying to get an
17962 * extra error report, if anything failed until now.
17963 * Enable this if needed.
17964 *
17965 * xmlSchemaPErr(ctxt, type->node,
17966 * XML_SCHEMAP_SRC_SIMPLE_TYPE_1,
17967 * "Simple type '%s' does not satisfy the constraints "
17968 * "on simple type definitions.\n",
17969 * type->name, NULL);
17970 */
17971 /*
17972 * Schema Component Constraint: Simple Type Restriction (Facets)
17973 * (st-restrict-facets)
17974 */
17975 res = xmlSchemaCheckFacetValues(type, pctxt);
17976 HFAILURE HERROR
17977 if ((type->facetSet != NULL) ||
17978 (type->baseType->facetSet != NULL)) {
17979 res = xmlSchemaDeriveAndValidateFacets(pctxt, type);
17980 HFAILURE HERROR
17981 }
17982 /*
17983 * Whitespace value.
17984 */
17985 res = xmlSchemaTypeFixupWhitespace(type);
17986 HFAILURE HERROR
17987 xmlSchemaTypeFixupOptimFacets(type);
17988
17989exit_error:
17990#ifdef DEBUG_TYPE
17991 xmlSchemaDebugFixedType(pctxt, type);
17992#endif
17993 if (olderrs != pctxt->nberrors)
17994 return(pctxt->err);
17995 return(0);
17996
17997exit_failure:
17998#ifdef DEBUG_TYPE
17999 xmlSchemaDebugFixedType(pctxt, type);
18000#endif
18001 return(-1);
18002}
18003
18004static int
18005xmlSchemaFixupComplexType(xmlSchemaParserCtxtPtr pctxt,
18006 xmlSchemaTypePtr type)
18007{
18008 int res = 0, olderrs = pctxt->nberrors;
18009 xmlSchemaTypePtr baseType = type->baseType;
18010
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018011 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018012 return(0);
18013 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_RESOLVED;
18014 if (baseType == NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018015 PERROR_INT("xmlSchemaFixupComplexType",
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018016 "missing baseType");
18017 goto exit_failure;
18018 }
18019 /*
18020 * Fixup the base type.
18021 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018022 if (WXS_IS_TYPE_NOT_FIXED(baseType))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018023 xmlSchemaTypeFixup(baseType, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018024 if (baseType->flags & XML_SCHEMAS_TYPE_INTERNAL_INVALID) {
18025 /*
18026 * Skip fixup if the base type is invalid.
18027 * TODO: Generate a warning!
18028 */
18029 return(0);
18030 }
18031 /*
18032 * This basically checks if the base type can be derived.
18033 */
18034 res = xmlSchemaCheckSRCCT(pctxt, type);
18035 HFAILURE HERROR
18036 /*
18037 * Fixup the content type.
18038 */
18039 if (type->contentType == XML_SCHEMA_CONTENT_SIMPLE) {
18040 /*
18041 * Corresponds to <complexType><simpleContent>...
18042 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018043 if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018044 (baseType->contentTypeDef != NULL) &&
18045 (WXS_IS_RESTRICTION(type))) {
18046 xmlSchemaTypePtr contentBase, content;
18047#ifdef ENABLE_NAMED_LOCALS
18048 char buf[30];
18049 const xmlChar *tmpname;
18050#endif
18051 /*
18052 * SPEC (1) If <restriction> + base type is <complexType>,
18053 * "whose own {content type} is a simple type..."
18054 */
18055 if (type->contentTypeDef != NULL) {
18056 /*
18057 * SPEC (1.1) "the simple type definition corresponding to the
18058 * <simpleType> among the [children] of <restriction> if there
18059 * is one;"
18060 * Note that this "<simpleType> among the [children]" was put
18061 * into ->contentTypeDef during parsing.
18062 */
18063 contentBase = type->contentTypeDef;
18064 type->contentTypeDef = NULL;
18065 } else {
18066 /*
18067 * (1.2) "...otherwise (<restriction> has no <simpleType>
18068 * among its [children]), the simple type definition which
18069 * is the {content type} of the ... base type."
18070 */
18071 contentBase = baseType->contentTypeDef;
18072 }
18073 /*
18074 * SPEC
18075 * "... a simple type definition which restricts the simple
18076 * type definition identified in clause 1.1 or clause 1.2
18077 * with a set of facet components"
18078 *
18079 * Create the anonymous simple type, which will be the content
18080 * type of the complex type.
18081 */
18082#ifdef ENABLE_NAMED_LOCALS
18083 snprintf(buf, 29, "#scST%d", ++(pctxt->counter));
18084 tmpname = xmlDictLookup(pctxt->dict, BAD_CAST buf, -1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018085 content = xmlSchemaAddType(pctxt, pctxt->schema,
18086 XML_SCHEMA_TYPE_SIMPLE, tmpname, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018087 type->node, 0);
18088#else
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018089 content = xmlSchemaAddType(pctxt, pctxt->schema,
18090 XML_SCHEMA_TYPE_SIMPLE, NULL, type->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018091 type->node, 0);
18092#endif
18093 if (content == NULL)
18094 goto exit_failure;
18095 /*
18096 * We will use the same node as for the <complexType>
18097 * to have it somehow anchored in the schema doc.
18098 */
18099 content->type = XML_SCHEMA_TYPE_SIMPLE;
18100 content->baseType = contentBase;
18101 /*
18102 * Move the facets, previously anchored on the
18103 * complexType during parsing.
18104 */
18105 content->facets = type->facets;
18106 type->facets = NULL;
18107 content->facetSet = type->facetSet;
18108 type->facetSet = NULL;
18109
18110 type->contentTypeDef = content;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018111 if (WXS_IS_TYPE_NOT_FIXED(contentBase))
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018112 xmlSchemaTypeFixup(contentBase, ACTXT_CAST pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018113 /*
18114 * Fixup the newly created type. We don't need to check
18115 * for circularity here.
18116 */
18117 res = xmlSchemaFixupSimpleTypeStageOne(pctxt, content);
18118 HFAILURE HERROR
18119 res = xmlSchemaFixupSimpleTypeStageTwo(pctxt, content);
18120 HFAILURE HERROR
18121
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018122 } else if ((WXS_IS_COMPLEX(baseType)) &&
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018123 (baseType->contentType == XML_SCHEMA_CONTENT_MIXED) &&
18124 (WXS_IS_RESTRICTION(type))) {
18125 /*
18126 * SPEC (2) If <restriction> + base is a mixed <complexType> with
18127 * an emptiable particle, then a simple type definition which
18128 * restricts the <restriction>'s <simpleType> child.
18129 */
18130 if ((type->contentTypeDef == NULL) ||
18131 (type->contentTypeDef->baseType == NULL)) {
18132 /*
18133 * TODO: Check if this ever happens.
18134 */
18135 xmlSchemaPCustomErr(pctxt,
18136 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018137 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018138 "Internal error: xmlSchemaTypeFixup, "
18139 "complex type '%s': the <simpleContent><restriction> "
18140 "is missing a <simpleType> child, but was not catched "
18141 "by xmlSchemaCheckSRCCT()", type->name);
18142 goto exit_failure;
18143 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018144 } else if ((WXS_IS_COMPLEX(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018145 /*
18146 * SPEC (3) If <extension> + base is <complexType> with
18147 * <simpleType> content, "...then the {content type} of that
18148 * complex type definition"
18149 */
18150 if (baseType->contentTypeDef == NULL) {
18151 /*
18152 * TODO: Check if this ever happens. xmlSchemaCheckSRCCT
18153 * should have catched this already.
18154 */
18155 xmlSchemaPCustomErr(pctxt,
18156 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018157 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018158 "Internal error: xmlSchemaTypeFixup, "
18159 "complex type '%s': the <extension>ed base type is "
18160 "a complex type with no simple content type",
18161 type->name);
18162 goto exit_failure;
18163 }
18164 type->contentTypeDef = baseType->contentTypeDef;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018165 } else if ((WXS_IS_SIMPLE(baseType)) && WXS_IS_EXTENSION(type)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018166 /*
18167 * SPEC (4) <extension> + base is <simpleType>
18168 * "... then that simple type definition"
18169 */
18170 type->contentTypeDef = baseType;
18171 } else {
18172 /*
18173 * TODO: Check if this ever happens.
18174 */
18175 xmlSchemaPCustomErr(pctxt,
18176 XML_SCHEMAP_INTERNAL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018177 WXS_BASIC_CAST type, NULL,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018178 "Internal error: xmlSchemaTypeFixup, "
18179 "complex type '%s' with <simpleContent>: unhandled "
18180 "derivation case", type->name);
18181 goto exit_failure;
18182 }
18183 } else {
18184 int dummySequence = 0;
18185 xmlSchemaParticlePtr particle =
18186 (xmlSchemaParticlePtr) type->subtypes;
18187 /*
18188 * Corresponds to <complexType><complexContent>...
18189 *
18190 * NOTE that the effective mixed was already set during parsing of
18191 * <complexType> and <complexContent>; its flag value is
18192 * XML_SCHEMAS_TYPE_MIXED.
18193 *
18194 * Compute the "effective content":
18195 * (2.1.1) + (2.1.2) + (2.1.3)
18196 */
18197 if ((particle == NULL) ||
18198 ((particle->type == XML_SCHEMA_TYPE_PARTICLE) &&
18199 ((particle->children->type == XML_SCHEMA_TYPE_ALL) ||
18200 (particle->children->type == XML_SCHEMA_TYPE_SEQUENCE) ||
18201 ((particle->children->type == XML_SCHEMA_TYPE_CHOICE) &&
18202 (particle->minOccurs == 0))) &&
18203 ( ((xmlSchemaTreeItemPtr) particle->children)->children == NULL))) {
18204 if (type->flags & XML_SCHEMAS_TYPE_MIXED) {
18205 /*
18206 * SPEC (2.1.4) "If the ·effective mixed· is true, then
18207 * a particle whose properties are as follows:..."
18208 *
18209 * Empty sequence model group with
18210 * minOccurs/maxOccurs = 1 (i.e. a "particle emptiable").
18211 * NOTE that we sill assign it the <complexType> node to
18212 * somehow anchor it in the doc.
18213 */
18214 if ((particle == NULL) ||
18215 (particle->children->type != XML_SCHEMA_TYPE_SEQUENCE)) {
18216 /*
18217 * Create the particle.
18218 */
18219 particle = xmlSchemaAddParticle(pctxt, pctxt->schema,
18220 type->node, 1, 1);
18221 if (particle == NULL)
18222 goto exit_failure;
18223 /*
18224 * Create the model group.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018225 */ /* URGENT TODO: avoid adding to pending items. */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018226 particle->children = (xmlSchemaTreeItemPtr)
18227 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18228 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18229 if (particle->children == NULL)
18230 goto exit_failure;
18231
18232 type->subtypes = (xmlSchemaTypePtr) particle;
18233 }
18234 dummySequence = 1;
18235 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18236 } else {
18237 /*
18238 * SPEC (2.1.5) "otherwise empty"
18239 */
18240 type->contentType = XML_SCHEMA_CONTENT_EMPTY;
18241 }
18242 } else {
18243 /*
18244 * SPEC (2.2) "otherwise the particle corresponding to the
18245 * <all>, <choice>, <group> or <sequence> among the
18246 * [children]."
18247 */
18248 type->contentType = XML_SCHEMA_CONTENT_ELEMENTS;
18249 }
18250 /*
18251 * Compute the "content type".
18252 */
18253 if (WXS_IS_RESTRICTION(type)) {
18254 /*
18255 * SPEC (3.1) "If <restriction>..."
18256 * (3.1.1) + (3.1.2) */
18257 if (type->contentType != XML_SCHEMA_CONTENT_EMPTY) {
18258 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18259 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18260 }
18261 } else {
18262 /*
18263 * SPEC (3.2) "If <extension>..."
18264 */
18265 if (type->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18266 /*
18267 * SPEC (3.2.1)
18268 */
18269 type->contentType = baseType->contentType;
18270 type->subtypes = baseType->subtypes;
18271 /*
18272 * NOTE that the effective mixed is ignored here.
18273 */
18274 } else if (baseType->contentType == XML_SCHEMA_CONTENT_EMPTY) {
18275 /*
18276 * SPEC (3.2.2)
18277 */
18278 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18279 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18280 } else {
18281 /*
18282 * SPEC (3.2.3)
18283 */
18284 if (type->flags & XML_SCHEMAS_TYPE_MIXED)
18285 type->contentType = XML_SCHEMA_CONTENT_MIXED;
18286 /*
18287 * "A model group whose {compositor} is sequence and whose
18288 * {particles} are..."
18289 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018290 if ((WXS_TYPE_PARTICLE(type) != NULL) &&
18291 (WXS_TYPE_PARTICLE_TERM(type) != NULL) &&
18292 ((WXS_TYPE_PARTICLE_TERM(type))->type ==
18293 XML_SCHEMA_TYPE_ALL))
18294 {
18295 /*
18296 * SPEC cos-all-limited (1)
18297 */
18298 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18299 /* TODO: error code */
18300 XML_SCHEMAP_COS_ALL_LIMITED,
18301 WXS_ITEM_NODE(type), NULL,
18302 "The type has an 'all' model group in its "
18303 "{content type} and thus cannot be derived from "
18304 "a non-empty type, since this would produce a "
18305 "'sequence' model group containing the 'all' "
18306 "model group; 'all' model groups are not "
18307 "allowed to appear inside other model groups",
18308 NULL, NULL);
18309
18310 } else if ((WXS_TYPE_PARTICLE(baseType) != NULL) &&
18311 (WXS_TYPE_PARTICLE_TERM(baseType) != NULL) &&
18312 ((WXS_TYPE_PARTICLE_TERM(baseType))->type ==
18313 XML_SCHEMA_TYPE_ALL))
18314 {
18315 /*
18316 * SPEC cos-all-limited (1)
18317 */
18318 xmlSchemaCustomErr(ACTXT_CAST pctxt,
18319 /* TODO: error code */
18320 XML_SCHEMAP_COS_ALL_LIMITED,
18321 WXS_ITEM_NODE(type), NULL,
18322 "A type cannot be derived by extension from a type "
18323 "which has an 'all' model group in its "
18324 "{content type}, since this would produce a "
18325 "'sequence' model group containing the 'all' "
18326 "model group; 'all' model groups are not "
18327 "allowed to appear inside other model groups",
18328 NULL, NULL);
18329
18330 } else if (! dummySequence) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018331 xmlSchemaTreeItemPtr effectiveContent =
18332 (xmlSchemaTreeItemPtr) type->subtypes;
18333 /*
18334 * Create the particle.
18335 */
18336 particle = xmlSchemaAddParticle(pctxt, pctxt->schema,
18337 type->node, 1, 1);
18338 if (particle == NULL)
18339 goto exit_failure;
18340 /*
18341 * Create the "sequence" model group.
18342 */
18343 particle->children = (xmlSchemaTreeItemPtr)
18344 xmlSchemaAddModelGroup(pctxt, pctxt->schema,
18345 XML_SCHEMA_TYPE_SEQUENCE, type->node);
18346 if (particle->children == NULL)
18347 goto exit_failure;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018348 WXS_TYPE_CONTENTTYPE(type) = (xmlSchemaTypePtr) particle;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018349 /*
18350 * SPEC "the particle of the {content type} of
18351 * the ... base ..."
18352 * Create a duplicate of the base type's particle
18353 * and assign its "term" to it.
18354 */
18355 particle->children->children =
18356 (xmlSchemaTreeItemPtr) xmlSchemaAddParticle(pctxt,
18357 pctxt->schema, type->node,
18358 ((xmlSchemaParticlePtr) type->subtypes)->minOccurs,
18359 ((xmlSchemaParticlePtr) type->subtypes)->maxOccurs);
18360 if (particle->children->children == NULL)
18361 goto exit_failure;
18362 particle = (xmlSchemaParticlePtr)
18363 particle->children->children;
18364 particle->children =
18365 ((xmlSchemaParticlePtr) baseType->subtypes)->children;
18366 /*
18367 * SPEC "followed by the ·effective content·."
18368 */
18369 particle->next = effectiveContent;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018370 /*
18371 * This all will result in:
18372 * new-particle
18373 * --> new-sequence(
18374 * new-particle
18375 * --> base-model,
18376 * this-particle
18377 * --> this-model
18378 * )
18379 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018380 } else {
18381 /*
18382 * This is the case when there is already an empty
18383 * <sequence> with minOccurs==maxOccurs==1.
18384 * Just add the base types's content type.
18385 * NOTE that, although we miss to add an intermediate
18386 * <sequence>, this should produce no difference to
18387 * neither the regex compilation of the content model,
18388 * nor to the complex type contraints.
18389 */
18390 particle->children->children =
18391 (xmlSchemaTreeItemPtr) baseType->subtypes;
18392 }
18393 }
18394 }
18395 }
18396 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018397 * Now fixup attribute uses:
18398 * - expand attr. group references
18399 * - intersect attribute wildcards
18400 * - inherit attribute uses of the base type
18401 * - inherit or union attr. wildcards if extending
18402 * - apply attr. use prohibitions if restricting
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018403 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018404 res = xmlSchemaFixupTypeAttributeUses(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018405 HFAILURE HERROR
18406 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018407 * Apply the complex type component constraints; this will not
18408 * check attributes, since this is done in
18409 * xmlSchemaFixupTypeAttributeUses().
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018410 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018411 res = xmlSchemaCheckCTComponent(pctxt, type);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018412 HFAILURE HERROR
18413
18414#ifdef DEBUG_TYPE
18415 xmlSchemaDebugFixedType(pctxt, type);
18416#endif
18417 if (olderrs != pctxt->nberrors)
18418 return(pctxt->err);
18419 else
18420 return(0);
18421
18422exit_error:
18423 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18424#ifdef DEBUG_TYPE
18425 xmlSchemaDebugFixedType(pctxt, type);
18426#endif
18427 return(pctxt->err);
18428
18429exit_failure:
18430 type->flags |= XML_SCHEMAS_TYPE_INTERNAL_INVALID;
18431#ifdef DEBUG_TYPE
18432 xmlSchemaDebugFixedType(pctxt, type);
18433#endif
18434 return(-1);
18435}
18436
18437
18438/**
18439 * xmlSchemaTypeFixup:
18440 * @typeDecl: the schema type definition
18441 * @ctxt: the schema parser context
18442 *
18443 * Fixes the content model of the type.
18444 * URGENT TODO: We need an int result!
18445 */
18446static int
18447xmlSchemaTypeFixup(xmlSchemaTypePtr type,
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018448 xmlSchemaAbstractCtxtPtr actxt)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018449{
18450 if (type == NULL)
18451 return(0);
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018452 if (actxt->type != XML_SCHEMA_CTXT_PARSER) {
18453 AERROR_INT("xmlSchemaTypeFixup",
18454 "this function needs a parser context");
18455 return(-1);
18456 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018457 if (! WXS_IS_TYPE_NOT_FIXED(type))
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018458 return(0);
18459 if (type->type == XML_SCHEMA_TYPE_COMPLEX)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018460 return(xmlSchemaFixupComplexType(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018461 else if (type->type == XML_SCHEMA_TYPE_SIMPLE)
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000018462 return(xmlSchemaFixupSimpleTypeStageTwo(PCTXT_CAST actxt, type));
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018463 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000018464}
18465
18466/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018467 * xmlSchemaCheckFacet:
18468 * @facet: the facet
18469 * @typeDecl: the schema type definition
Daniel Veillard81562d22005-06-15 13:27:56 +000018470 * @pctxt: the schema parser context or NULL
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018471 * @name: the optional name of the type
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018472 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018473 * Checks and computes the values of facets.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018474 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018475 * Returns 0 if valid, a positive error code if not valid and
18476 * -1 in case of an internal or API error.
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018477 */
18478int
18479xmlSchemaCheckFacet(xmlSchemaFacetPtr facet,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018480 xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018481 xmlSchemaParserCtxtPtr pctxt,
18482 const xmlChar * name ATTRIBUTE_UNUSED)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018483{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018484 int ret = 0, ctxtGiven;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018485
Daniel Veillardce682bc2004-11-05 17:22:25 +000018486 if ((facet == NULL) || (typeDecl == NULL))
18487 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018488 /*
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000018489 * TODO: will the parser context be given if used from
18490 * the relaxNG module?
18491 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018492 if (pctxt == NULL)
18493 ctxtGiven = 0;
18494 else
18495 ctxtGiven = 1;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018496
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018497 switch (facet->type) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018498 case XML_SCHEMA_FACET_MININCLUSIVE:
18499 case XML_SCHEMA_FACET_MINEXCLUSIVE:
18500 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018501 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
18502 case XML_SCHEMA_FACET_ENUMERATION: {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018503 /*
18504 * Okay we need to validate the value
18505 * at that point.
18506 */
Daniel Veillard01fa6152004-06-29 17:04:39 +000018507 xmlSchemaTypePtr base;
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018508
18509 /* 4.3.5.5 Constraints on enumeration Schema Components
18510 * Schema Component Constraint: enumeration valid restriction
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018511 * It is an ·error· if any member of {value} is not in the
18512 * ·value space· of {base type definition}.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018513 *
18514 * minInclusive, maxInclusive, minExclusive, maxExclusive:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018515 * The value ·must· be in the
18516 * ·value space· of the ·base type·.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018517 */
18518 /*
18519 * This function is intended to deliver a compiled value
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018520 * on the facet. In this implementation of XML Schemata the
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018521 * type holding a facet, won't be a built-in type.
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018522 * Thus to ensure that other API
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018523 * calls (relaxng) do work, if the given type is a built-in
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018524 * type, we will assume that the given built-in type *is
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018525 * already* the base type.
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018526 */
18527 if (typeDecl->type != XML_SCHEMA_TYPE_BASIC) {
18528 base = typeDecl->baseType;
18529 if (base == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018530 PERROR_INT("xmlSchemaCheckFacet",
18531 "a type user derived type has no base type");
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018532 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018533 }
Daniel Veillardf4b05d02004-07-05 13:10:37 +000018534 } else
18535 base = typeDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018536
18537 if (! ctxtGiven) {
18538 /*
18539 * A context is needed if called from RelaxNG.
18540 */
18541 pctxt = xmlSchemaNewParserCtxt("*");
18542 if (pctxt == NULL)
18543 return (-1);
Daniel Veillard01fa6152004-06-29 17:04:39 +000018544 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018545 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018546 * NOTE: This call does not check the content nodes,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018547 * since they are not available:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018548 * facet->node is just the node holding the facet
18549 * definition, *not* the attribute holding the *value*
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000018550 * of the facet.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018551 */
18552 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018553 ACTXT_CAST pctxt, facet->node, base,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018554 facet->value, &(facet->val), 1, 1, 0);
18555 if (ret != 0) {
18556 if (ret < 0) {
18557 /* No error message for RelaxNG. */
18558 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018559 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018560 XML_SCHEMAP_INTERNAL, facet->node, NULL,
18561 "Internal error: xmlSchemaCheckFacet, "
18562 "failed to validate the value '%s' of the "
18563 "facet '%s' against the base type",
18564 facet->value, xmlSchemaFacetTypeToString(facet->type));
18565 }
18566 goto internal_error;
18567 }
18568 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18569 /* No error message for RelaxNG. */
18570 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018571 xmlChar *str = NULL;
18572
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018573 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018574 ret, facet->node, WXS_BASIC_CAST facet,
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018575 "The value '%s' of the facet does not validate "
18576 "against the base type '%s'",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018577 facet->value,
18578 xmlSchemaFormatQName(&str,
18579 base->targetNamespace, base->name));
18580 FREE_AND_NULL(str);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018581 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018582 goto exit;
18583 } else if (facet->val == NULL) {
18584 if (ctxtGiven) {
18585 PERROR_INT("xmlSchemaCheckFacet",
18586 "value was not computed");
18587 }
18588 TODO
18589 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018590 break;
18591 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018592 case XML_SCHEMA_FACET_PATTERN:
18593 facet->regexp = xmlRegexpCompile(facet->value);
18594 if (facet->regexp == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018595 ret = XML_SCHEMAP_REGEXP_INVALID;
18596 /* No error message for RelaxNG. */
18597 if (ctxtGiven) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018598 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018599 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018600 "The value '%s' of the facet 'pattern' is not a "
18601 "valid regular expression",
18602 facet->value, NULL);
18603 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018604 }
18605 break;
18606 case XML_SCHEMA_FACET_TOTALDIGITS:
18607 case XML_SCHEMA_FACET_FRACTIONDIGITS:
18608 case XML_SCHEMA_FACET_LENGTH:
18609 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018610 case XML_SCHEMA_FACET_MINLENGTH:
18611
18612 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
18613 ret = xmlSchemaValidatePredefinedType(
18614 xmlSchemaGetBuiltInType(XML_SCHEMAS_PINTEGER),
18615 facet->value, &(facet->val));
18616 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018617 ret = xmlSchemaValidatePredefinedType(
18618 xmlSchemaGetBuiltInType(XML_SCHEMAS_NNINTEGER),
18619 facet->value, &(facet->val));
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018620 }
18621 if (ret != 0) {
18622 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018623 /* No error message for RelaxNG. */
18624 if (ctxtGiven) {
Kasimier T. Buchcik69dea3a2005-11-07 14:02:44 +000018625 PERROR_INT("xmlSchemaCheckFacet",
18626 "validating facet value");
18627 }
18628 goto internal_error;
18629 }
18630 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18631 /* No error message for RelaxNG. */
18632 if (ctxtGiven) {
18633 /* error code */
18634 xmlSchemaCustomErr4(ACTXT_CAST pctxt,
18635 ret, facet->node, WXS_BASIC_CAST typeDecl,
18636 "The value '%s' of the facet '%s' is not a valid '%s'",
18637 facet->value,
18638 xmlSchemaFacetTypeToString(facet->type),
18639 (facet->type != XML_SCHEMA_FACET_TOTALDIGITS) ?
18640 BAD_CAST "nonNegativeInteger" :
18641 BAD_CAST "positiveInteger",
18642 NULL);
18643 }
18644 }
18645 break;
18646
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018647 case XML_SCHEMA_FACET_WHITESPACE:{
18648 if (xmlStrEqual(facet->value, BAD_CAST "preserve")) {
18649 facet->whitespace = XML_SCHEMAS_FACET_PRESERVE;
18650 } else if (xmlStrEqual(facet->value, BAD_CAST "replace")) {
18651 facet->whitespace = XML_SCHEMAS_FACET_REPLACE;
18652 } else if (xmlStrEqual(facet->value, BAD_CAST "collapse")) {
18653 facet->whitespace = XML_SCHEMAS_FACET_COLLAPSE;
18654 } else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018655 ret = XML_SCHEMAP_INVALID_FACET_VALUE;
18656 /* No error message for RelaxNG. */
18657 if (ctxtGiven) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000018658 /* error was previously: XML_SCHEMAP_INVALID_WHITE_SPACE */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018659 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018660 ret, facet->node, WXS_BASIC_CAST typeDecl,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018661 "The value '%s' of the facet 'whitespace' is not "
18662 "valid", facet->value, NULL);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018663 }
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018664 }
18665 }
18666 default:
18667 break;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018668 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018669exit:
18670 if ((! ctxtGiven) && (pctxt != NULL))
18671 xmlSchemaFreeParserCtxt(pctxt);
Daniel Veillardd0c9c322003-10-10 00:49:42 +000018672 return (ret);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018673internal_error:
18674 if ((! ctxtGiven) && (pctxt != NULL))
18675 xmlSchemaFreeParserCtxt(pctxt);
18676 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000018677}
18678
18679/**
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018680 * xmlSchemaCheckFacetValues:
Daniel Veillard4255d502002-04-16 15:50:10 +000018681 * @typeDecl: the schema type definition
18682 * @ctxt: the schema parser context
18683 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018684 * Checks the default values types, especially for facets
Daniel Veillard4255d502002-04-16 15:50:10 +000018685 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018686static int
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018687xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018688 xmlSchemaParserCtxtPtr pctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000018689{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018690 int res, olderrs = pctxt->nberrors;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018691 const xmlChar *name = typeDecl->name;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018692 /*
18693 * NOTE: It is intended to use the facets list, instead
18694 * of facetSet.
18695 */
18696 if (typeDecl->facets != NULL) {
18697 xmlSchemaFacetPtr facet = typeDecl->facets;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018698
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018699 /*
18700 * Temporarily assign the "schema" to the validation context
18701 * of the parser context. This is needed for NOTATION validation.
18702 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018703 if (pctxt->vctxt == NULL) {
18704 if (xmlSchemaCreateVCtxtOnPCtxt(pctxt) == -1)
18705 return(-1);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000018706 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018707 pctxt->vctxt->schema = pctxt->schema;
Daniel Veillard01fa6152004-06-29 17:04:39 +000018708 while (facet != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018709 res = xmlSchemaCheckFacet(facet, typeDecl, pctxt, name);
18710 HFAILURE
Daniel Veillard01fa6152004-06-29 17:04:39 +000018711 facet = facet->next;
18712 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018713 pctxt->vctxt->schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018714 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018715 if (olderrs != pctxt->nberrors)
18716 return(pctxt->err);
18717 return(0);
18718exit_failure:
18719 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000018720}
18721
18722/**
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018723 * xmlSchemaGetCircModelGrDefRef:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018724 * @ctxtMGroup: the searched model group
18725 * @selfMGroup: the second searched model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018726 * @particle: the first particle
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018727 *
18728 * This one is intended to be used by
18729 * xmlSchemaCheckGroupDefCircular only.
18730 *
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018731 * Returns the particle with the circular model group definition reference,
18732 * otherwise NULL.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018733 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018734static xmlSchemaTreeItemPtr
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018735xmlSchemaGetCircModelGrDefRef(xmlSchemaModelGroupDefPtr groupDef,
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018736 xmlSchemaTreeItemPtr particle)
18737{
18738 xmlSchemaTreeItemPtr circ = NULL;
18739 xmlSchemaTreeItemPtr term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018740 xmlSchemaModelGroupDefPtr gdef;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018741
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018742 for (; particle != NULL; particle = particle->next) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018743 term = particle->children;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018744 if (term == NULL)
18745 continue;
18746 switch (term->type) {
18747 case XML_SCHEMA_TYPE_GROUP:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018748 gdef = (xmlSchemaModelGroupDefPtr) term;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018749 if (gdef == groupDef)
18750 return (particle);
18751 /*
18752 * Mark this model group definition to avoid infinite
18753 * recursion on circular references not yet examined.
18754 */
18755 if (gdef->flags & XML_SCHEMA_MODEL_GROUP_DEF_MARKED)
18756 continue;
18757 if (gdef->children != NULL) {
18758 gdef->flags |= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18759 circ = xmlSchemaGetCircModelGrDefRef(groupDef,
18760 gdef->children->children);
18761 gdef->flags ^= XML_SCHEMA_MODEL_GROUP_DEF_MARKED;
18762 if (circ != NULL)
18763 return (circ);
18764 }
18765 break;
18766 case XML_SCHEMA_TYPE_SEQUENCE:
18767 case XML_SCHEMA_TYPE_CHOICE:
18768 case XML_SCHEMA_TYPE_ALL:
18769 circ = xmlSchemaGetCircModelGrDefRef(groupDef, term->children);
18770 if (circ != NULL)
18771 return (circ);
18772 break;
18773 default:
18774 break;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018775 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018776 }
18777 return (NULL);
18778}
18779
18780/**
18781 * xmlSchemaCheckGroupDefCircular:
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018782 * @item: the model group definition
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018783 * @ctxt: the parser context
18784 * @name: the name
18785 *
18786 * Checks for circular references to model group definitions.
18787 */
18788static void
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018789xmlSchemaCheckGroupDefCircular(xmlSchemaModelGroupDefPtr item,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018790 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018791{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018792 /*
18793 * Schema Component Constraint: Model Group Correct
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018794 * 2 Circular groups are disallowed. That is, within the {particles}
18795 * of a group there must not be at any depth a particle whose {term}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018796 * is the group itself.
18797 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018798 if ((item == NULL) ||
18799 (item->type != XML_SCHEMA_TYPE_GROUP) ||
18800 (item->children == NULL))
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018801 return;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018802 {
18803 xmlSchemaTreeItemPtr circ;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018804
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018805 circ = xmlSchemaGetCircModelGrDefRef(item, item->children->children);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018806 if (circ != NULL) {
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018807 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018808 /*
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018809 * TODO: The error report is not adequate: this constraint
18810 * is defined for model groups but not definitions, but since
18811 * there cannot be any circular model groups without a model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018812 * definition (if not using a construction API), we check those
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018813 * defintions only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018814 */
18815 xmlSchemaPCustomErr(ctxt,
18816 XML_SCHEMAP_MG_PROPS_CORRECT_2,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018817 NULL, WXS_ITEM_NODE(circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018818 "Circular reference to the model group definition '%s' "
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018819 "defined", xmlSchemaFormatQName(&str,
18820 item->targetNamespace, item->name));
18821 FREE_AND_NULL(str)
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018822 /*
18823 * NOTE: We will cut the reference to avoid further
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018824 * confusion of the processor. This is a fatal error.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018825 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000018826 circ->children = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018827 }
18828 }
18829}
18830
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018831/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018832 * xmlSchemaModelGroupToModelGroupDefFixup:
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018833 * @ctxt: the parser context
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018834 * @mg: the model group
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018835 *
18836 * Assigns the model group of model group definitions to the "term"
18837 * of the referencing particle.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018838 * In xmlSchemaResolveModelGroupParticleReferences the model group
18839 * definitions were assigned to the "term", since needed for the
18840 * circularity check.
18841 *
18842 * Schema Component Constraint:
18843 * All Group Limited (cos-all-limited) (1.2)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018844 */
18845static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018846xmlSchemaModelGroupToModelGroupDefFixup(
18847 xmlSchemaParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
18848 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018849{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018850 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
18851
18852 while (particle != NULL) {
18853 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
18854 ((WXS_PARTICLE_TERM(particle))->type !=
18855 XML_SCHEMA_TYPE_GROUP))
18856 {
18857 particle = WXS_PTC_CAST particle->next;
18858 continue;
18859 }
18860 if (WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle)) == NULL) {
18861 /*
18862 * TODO: Remove the particle.
18863 */
18864 WXS_PARTICLE_TERM(particle) = NULL;
18865 particle = WXS_PTC_CAST particle->next;
18866 continue;
18867 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018868 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018869 * Assign the model group to the {term} of the particle.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018870 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018871 WXS_PARTICLE_TERM(particle) =
18872 WXS_TREE_CAST WXS_MODELGROUPDEF_MODEL(WXS_PARTICLE_TERM(particle));
18873
18874 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018875 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000018876}
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018877
18878/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018879 * xmlSchemaCheckAttrGroupCircularRecur:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018880 * @ctxtGr: the searched attribute group
18881 * @attr: the current attribute list to be processed
18882 *
18883 * This one is intended to be used by
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018884 * xmlSchemaCheckAttrGroupCircular only.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018885 *
18886 * Returns the circular attribute grou reference, otherwise NULL.
18887 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018888static xmlSchemaQNameRefPtr
18889xmlSchemaCheckAttrGroupCircularRecur(xmlSchemaAttributeGroupPtr ctxtGr,
18890 xmlSchemaItemListPtr list)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018891{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018892 xmlSchemaAttributeGroupPtr gr;
18893 xmlSchemaQNameRefPtr ref, circ;
18894 int i;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018895 /*
18896 * We will search for an attribute group reference which
18897 * references the context attribute group.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018898 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018899 for (i = 0; i < list->nbItems; i++) {
18900 ref = list->items[i];
18901 if ((ref->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
18902 (ref->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP) &&
18903 (ref->item != NULL))
18904 {
18905 gr = WXS_ATTR_GROUP_CAST ref->item;
18906 if (gr == ctxtGr)
18907 return(ref);
18908 if (gr->flags & XML_SCHEMAS_ATTRGROUP_MARKED)
18909 continue;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018910 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018911 * Mark as visited to avoid infinite recursion on
18912 * circular references not yet examined.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018913 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018914 if ((gr->attrUses) &&
18915 (gr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS))
18916 {
18917 gr->flags |= XML_SCHEMAS_ATTRGROUP_MARKED;
18918 circ = xmlSchemaCheckAttrGroupCircularRecur(ctxtGr,
18919 (xmlSchemaItemListPtr) gr->attrUses);
18920 gr->flags ^= XML_SCHEMAS_ATTRGROUP_MARKED;
18921 if (circ != NULL)
18922 return (circ);
18923 }
18924
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018925 }
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018926 }
18927 return (NULL);
18928}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018929
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018930/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018931 * xmlSchemaCheckAttrGroupCircular:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018932 * attrGr: the attribute group definition
18933 * @ctxt: the parser context
18934 * @name: the name
18935 *
18936 * Checks for circular references of attribute groups.
18937 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018938static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000018939xmlSchemaCheckAttrGroupCircular(xmlSchemaAttributeGroupPtr attrGr,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018940 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018941{
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018942 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018943 * Schema Representation Constraint:
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018944 * Attribute Group Definition Representation OK
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000018945 * 3 Circular group reference is disallowed outside <redefine>.
18946 * That is, unless this element information item's parent is
18947 * <redefine>, then among the [children], if any, there must
18948 * not be an <attributeGroup> with ref [attribute] which resolves
18949 * to the component corresponding to this <attributeGroup>. Indirect
18950 * circularity is also ruled out. That is, when QName resolution
18951 * (Schema Document) (§3.15.3) is applied to a ·QName· arising from
18952 * any <attributeGroup>s with a ref [attribute] among the [children],
18953 * it must not be the case that a ·QName· is encountered at any depth
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018954 * which resolves to the component corresponding to this <attributeGroup>.
18955 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018956 if (attrGr->attrUses == NULL)
18957 return(0);
18958 else if ((attrGr->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS) == 0)
18959 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018960 else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018961 xmlSchemaQNameRefPtr circ;
18962
18963 circ = xmlSchemaCheckAttrGroupCircularRecur(attrGr,
18964 (xmlSchemaItemListPtr) attrGr->attrUses);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018965 if (circ != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018966 xmlChar *str = NULL;
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018967 /*
18968 * TODO: Report the referenced attr group as QName.
18969 */
18970 xmlSchemaPCustomErr(ctxt,
18971 XML_SCHEMAP_SRC_ATTRIBUTE_GROUP_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018972 NULL, WXS_ITEM_NODE(WXS_BASIC_CAST circ),
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018973 "Circular reference to the attribute group '%s' "
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018974 "defined", xmlSchemaGetComponentQName(&str, attrGr));
18975 FREE_AND_NULL(str);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018976 /*
18977 * NOTE: We will cut the reference to avoid further
18978 * confusion of the processor.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018979 * BADSPEC TODO: The spec should define how to process in this case.
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018980 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018981 circ->item = NULL;
18982 return(ctxt->err);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000018983 }
18984 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000018985 return(0);
18986}
18987
18988static int
18989xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
18990 xmlSchemaAttributeGroupPtr attrGr);
18991
18992/**
18993 * xmlSchemaExpandAttributeGroupRefs:
18994 * @pctxt: the parser context
18995 * @node: the node of the component holding the attribute uses
18996 * @completeWild: the intersected wildcard to be returned
18997 * @list: the attribute uses
18998 *
18999 * Substitutes contained attribute group references
19000 * for their attribute uses. Wilcards are intersected.
19001 * Attribute use prohibitions are removed from the list
19002 * and returned via the @prohibs list.
19003 * Pointlessness of attr. prohibs, if a matching attr. decl
19004 * is existent a well, are checked.
19005 */
19006static int
19007xmlSchemaExpandAttributeGroupRefs(xmlSchemaParserCtxtPtr pctxt,
19008 xmlSchemaBasicItemPtr item,
19009 xmlSchemaWildcardPtr *completeWild,
19010 xmlSchemaItemListPtr list,
19011 xmlSchemaItemListPtr prohibs)
19012{
19013 xmlSchemaAttributeGroupPtr gr;
19014 xmlSchemaAttributeUsePtr use;
19015 xmlSchemaItemListPtr sublist;
19016 int i, j;
19017 int created = (*completeWild == NULL) ? 0 : 1;
19018
19019 if (prohibs)
19020 prohibs->nbItems = 0;
19021
19022 for (i = 0; i < list->nbItems; i++) {
19023 use = list->items[i];
19024
19025 if (use->type == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB) {
19026 if (prohibs == NULL) {
19027 PERROR_INT("xmlSchemaExpandAttributeGroupRefs",
19028 "unexpected attr prohibition found");
19029 return(-1);
19030 }
19031 /*
19032 * Remove from attribute uses.
19033 */
19034 if (xmlSchemaItemListRemove(list, i) == -1)
19035 return(-1);
19036 i--;
19037 /*
19038 * Note that duplicate prohibitions were already
19039 * handled at parsing time.
19040 */
19041 /*
19042 * Add to list of prohibitions.
19043 */
19044 xmlSchemaItemListAddSize(prohibs, 2, use);
19045 continue;
19046 }
19047 if ((use->type == XML_SCHEMA_EXTRA_QNAMEREF) &&
19048 ((WXS_QNAME_CAST use)->itemType == XML_SCHEMA_TYPE_ATTRIBUTEGROUP))
19049 {
19050 if ((WXS_QNAME_CAST use)->item == NULL)
19051 return(-1);
19052 gr = WXS_ATTR_GROUP_CAST (WXS_QNAME_CAST use)->item;
19053 /*
19054 * Expand the referenced attr. group.
19055 * TODO: remove this, this is done in a previous step, so
19056 * already done here.
19057 */
19058 if ((gr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED) == 0) {
19059 if (xmlSchemaAttributeGroupExpandRefs(pctxt, gr) == -1)
19060 return(-1);
19061 }
19062 /*
19063 * Build the 'complete' wildcard; i.e. intersect multiple
19064 * wildcards.
19065 */
19066 if (gr->attributeWildcard != NULL) {
19067 if (*completeWild == NULL) {
19068 *completeWild = gr->attributeWildcard;
19069 } else {
19070 if (! created) {
19071 xmlSchemaWildcardPtr tmpWild;
19072
19073 /*
19074 * Copy the first encountered wildcard as context,
19075 * except for the annotation.
19076 *
19077 * Although the complete wildcard might not correspond
19078 * to any node in the schema, we will anchor it on
19079 * the node of the owner component.
19080 */
19081 tmpWild = xmlSchemaAddWildcard(pctxt, pctxt->schema,
19082 XML_SCHEMA_TYPE_ANY_ATTRIBUTE,
19083 WXS_ITEM_NODE(item));
19084 if (tmpWild == NULL)
19085 return(-1);
19086 if (xmlSchemaCloneWildcardNsConstraints(pctxt,
19087 tmpWild, *completeWild) == -1)
19088 return (-1);
19089 tmpWild->processContents = (*completeWild)->processContents;
19090 *completeWild = tmpWild;
19091 created = 1;
19092 }
19093
19094 if (xmlSchemaIntersectWildcards(pctxt, *completeWild,
19095 gr->attributeWildcard) == -1)
19096 return(-1);
19097 }
19098 }
19099 /*
19100 * Just remove the reference if the referenced group does not
19101 * contain any attribute uses.
19102 */
19103 if (gr->attrUses == NULL) {
19104 if (xmlSchemaItemListRemove(list, i) == -1)
19105 return(-1);
19106 i--;
19107 continue;
19108 }
19109 /*
19110 * Add the attribute uses.
19111 */
19112 sublist = ((xmlSchemaItemListPtr) gr->attrUses);
19113 if (sublist->nbItems != 0) {
19114 list->items[i] = sublist->items[0];
19115 if (sublist->nbItems != 1) {
19116 for (j = 1; j < sublist->nbItems; j++) {
19117 i++;
19118 if (xmlSchemaItemListInsert(list,
19119 sublist->items[j], i) == -1)
19120 return(-1);
19121 }
19122 }
19123 }
19124 }
19125
19126 }
19127 /*
19128 * Handle pointless prohibitions of declared attributes.
19129 */
19130 if (prohibs && (prohibs->nbItems != 0) && (list->nbItems != 0)) {
19131 xmlSchemaAttributeUseProhibPtr prohib;
19132
19133 for (i = prohibs->nbItems -1; i >= 0; i--) {
19134 prohib = prohibs->items[i];
19135 for (j = 0; j < list->nbItems; j++) {
19136 use = list->items[j];
19137
19138 if ((prohib->name == WXS_ATTRUSE_DECL_NAME(use)) &&
19139 (prohib->targetNamespace == WXS_ATTRUSE_DECL_TNS(use)))
19140 {
19141 xmlChar *str = NULL;
19142
19143 xmlSchemaCustomWarning(ACTXT_CAST pctxt,
19144 XML_SCHEMAP_WARN_ATTR_POINTLESS_PROH,
19145 prohib->node, NULL,
19146 "Skipping pointless attribute use prohibition "
19147 "'%s', since a corresponding attribute use "
19148 "exists already in the type definition",
19149 xmlSchemaFormatQName(&str,
19150 prohib->targetNamespace, prohib->name),
19151 NULL, NULL);
19152 FREE_AND_NULL(str);
19153 /*
19154 * Remove the prohibition.
19155 */
19156 if (xmlSchemaItemListRemove(prohibs, i) == -1)
19157 return(-1);
19158 break;
19159 }
19160 }
19161 }
19162 }
19163 return(0);
19164}
19165
19166/**
19167 * xmlSchemaAttributeGroupExpandRefs:
19168 * @pctxt: the parser context
19169 * @attrGr: the attribute group definition
19170 *
19171 * Computation of:
19172 * {attribute uses} property
19173 * {attribute wildcard} property
19174 *
19175 * Substitutes contained attribute group references
19176 * for their attribute uses. Wilcards are intersected.
19177 */
19178static int
19179xmlSchemaAttributeGroupExpandRefs(xmlSchemaParserCtxtPtr pctxt,
19180 xmlSchemaAttributeGroupPtr attrGr)
19181{
19182 if ((attrGr->attrUses == NULL) ||
19183 (attrGr->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED))
19184 return(0);
19185
19186 attrGr->flags |= XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED;
19187 if (xmlSchemaExpandAttributeGroupRefs(pctxt, WXS_BASIC_CAST attrGr,
19188 &(attrGr->attributeWildcard), attrGr->attrUses, NULL) == -1)
19189 return(-1);
19190 return(0);
19191}
19192
19193/**
19194 * xmlSchemaAttributeGroupExpandRefs:
19195 * @pctxt: the parser context
19196 * @attrGr: the attribute group definition
19197 *
19198 * Substitutes contained attribute group references
19199 * for their attribute uses. Wilcards are intersected.
19200 *
19201 * Schema Component Constraint:
19202 * Attribute Group Definition Properties Correct (ag-props-correct)
19203 */
19204static int
19205xmlSchemaCheckAGPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19206 xmlSchemaAttributeGroupPtr attrGr)
19207{
19208 /*
19209 * SPEC ag-props-correct
19210 * (1) "The values of the properties of an attribute group definition
19211 * must be as described in the property tableau in The Attribute
19212 * Group Definition Schema Component (§3.6.1), modulo the impact of
19213 * Missing Sub-components (§5.3);"
19214 */
19215
19216 if ((attrGr->attrUses != NULL) &&
19217 (WXS_LIST_CAST attrGr->attrUses)->nbItems > 1)
19218 {
19219 xmlSchemaItemListPtr uses = WXS_LIST_CAST attrGr->attrUses;
19220 xmlSchemaAttributeUsePtr use, tmp;
19221 int i, j, hasId = 0;
19222
19223 for (i = uses->nbItems -1; i >= 0; i--) {
19224 use = uses->items[i];
19225 /*
19226 * SPEC ag-props-correct
19227 * (2) "Two distinct members of the {attribute uses} must not have
19228 * {attribute declaration}s both of whose {name}s match and whose
19229 * {target namespace}s are identical."
19230 */
19231 if (i > 0) {
19232 for (j = i -1; j >= 0; j--) {
19233 tmp = uses->items[j];
19234 if ((WXS_ATTRUSE_DECL_NAME(use) ==
19235 WXS_ATTRUSE_DECL_NAME(tmp)) &&
19236 (WXS_ATTRUSE_DECL_TNS(use) ==
19237 WXS_ATTRUSE_DECL_TNS(tmp)))
19238 {
19239 xmlChar *str = NULL;
19240
19241 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19242 XML_SCHEMAP_AG_PROPS_CORRECT,
19243 attrGr->node, WXS_BASIC_CAST attrGr,
19244 "Duplicate %s",
19245 xmlSchemaGetComponentDesignation(&str, use),
19246 NULL);
19247 FREE_AND_NULL(str);
19248 /*
19249 * Remove the duplicate.
19250 */
19251 if (xmlSchemaItemListRemove(uses, i) == -1)
19252 return(-1);
19253 goto next_use;
19254 }
19255 }
19256 }
19257 /*
19258 * SPEC ag-props-correct
19259 * (3) "Two distinct members of the {attribute uses} must not have
19260 * {attribute declaration}s both of whose {type definition}s are or
19261 * are derived from ID."
19262 * TODO: Does 'derived' include member-types of unions?
19263 */
19264 if (WXS_ATTRUSE_TYPEDEF(use) != NULL) {
19265 if (xmlSchemaIsDerivedFromBuiltInType(
19266 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
19267 {
19268 if (hasId) {
19269 xmlChar *str = NULL;
19270
19271 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19272 XML_SCHEMAP_AG_PROPS_CORRECT,
19273 attrGr->node, WXS_BASIC_CAST attrGr,
19274 "There must not exist more than one attribute "
19275 "declaration of type 'xs:ID' "
19276 "(or derived from 'xs:ID'). The %s violates this "
19277 "constraint",
19278 xmlSchemaGetComponentDesignation(&str, use),
19279 NULL);
19280 FREE_AND_NULL(str);
19281 if (xmlSchemaItemListRemove(uses, i) == -1)
19282 return(-1);
19283 }
19284 hasId = 1;
19285 }
19286 }
19287next_use: {}
19288 }
19289 }
19290 return(0);
Kasimier T. Buchcik383434b2004-09-03 18:55:35 +000019291}
19292
19293/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019294 * xmlSchemaResolveAttrGroupReferences:
Daniel Veillard13e04c62002-04-23 17:51:29 +000019295 * @attrgrpDecl: the schema attribute definition
19296 * @ctxt: the schema parser context
19297 * @name: the attribute name
19298 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019299 * Resolves references to attribute group definitions.
Daniel Veillard13e04c62002-04-23 17:51:29 +000019300 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019301static int
19302xmlSchemaResolveAttrGroupReferences(xmlSchemaQNameRefPtr ref,
19303 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard13e04c62002-04-23 17:51:29 +000019304{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019305 xmlSchemaAttributeGroupPtr group;
Daniel Veillard13e04c62002-04-23 17:51:29 +000019306
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019307 if (ref->item != NULL)
19308 return(0);
19309 group = xmlSchemaGetAttributeGroup(ctxt->schema,
19310 ref->name,
19311 ref->targetNamespace);
19312 if (group == NULL) {
19313 xmlSchemaPResCompAttrErr(ctxt,
19314 XML_SCHEMAP_SRC_RESOLVE,
19315 NULL, ref->node,
19316 "ref", ref->name, ref->targetNamespace,
19317 ref->itemType, NULL);
19318 return(ctxt->err);
Daniel Veillard3646d642004-06-02 19:19:14 +000019319 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019320 ref->item = WXS_BASIC_CAST group;
19321 return(0);
Daniel Veillard13e04c62002-04-23 17:51:29 +000019322}
19323
19324/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019325 * xmlSchemaCheckAttrPropsCorrect:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019326 * @item: an schema attribute declaration/use
19327 * @ctxt: a schema parser context
19328 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019329 *
19330 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019331 * Schema Component Constraint:
19332 * Attribute Declaration Properties Correct (a-props-correct)
Daniel Veillard4255d502002-04-16 15:50:10 +000019333 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019334 * Validates the value constraints of an attribute declaration/use.
19335 * NOTE that this needs the simle type definitions to be already
19336 * builded and checked.
Daniel Veillard4255d502002-04-16 15:50:10 +000019337 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019338static int
19339xmlSchemaCheckAttrPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19340 xmlSchemaAttributePtr attr)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019341{
19342
19343 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019344 * SPEC a-props-correct (1)
19345 * "The values of the properties of an attribute declaration must
19346 * be as described in the property tableau in The Attribute
19347 * Declaration Schema Component (§3.2.1), modulo the impact of
19348 * Missing Sub-components (§5.3)."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019349 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019350
19351 if (WXS_ATTR_TYPEDEF(attr) == NULL)
19352 return(0);
19353
19354 if (attr->defValue != NULL) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019355 int ret;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019356
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019357 /*
19358 * SPEC a-props-correct (3)
19359 * "If the {type definition} is or is derived from ID then there
19360 * must not be a {value constraint}."
19361 */
19362 if (xmlSchemaIsDerivedFromBuiltInType(
19363 WXS_ATTR_TYPEDEF(attr), XML_SCHEMAS_ID))
19364 {
19365 xmlSchemaCustomErr(ACTXT_CAST pctxt,
19366 XML_SCHEMAP_A_PROPS_CORRECT_3,
19367 NULL, WXS_BASIC_CAST attr,
19368 "Value constraints are not allowed if the type definition "
19369 "is or is derived from xs:ID",
19370 NULL, NULL);
19371 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019372 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019373 /*
19374 * SPEC a-props-correct (2)
19375 * "if there is a {value constraint}, the canonical lexical
19376 * representation of its value must be ·valid· with respect
19377 * to the {type definition} as defined in String Valid (§3.14.4)."
19378 * TODO: Don't care about the *cononical* stuff here, this requirement
19379 * will be removed in WXS 1.1 anyway.
19380 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019381 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019382 attr->node, WXS_ATTR_TYPEDEF(attr),
19383 attr->defValue, &(attr->defVal),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019384 1, 1, 0);
19385 if (ret != 0) {
19386 if (ret < 0) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019387 PERROR_INT("xmlSchemaCheckAttrPropsCorrect",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019388 "calling xmlSchemaVCheckCVCSimpleType()");
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019389 return(-1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019390 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019391 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019392 XML_SCHEMAP_A_PROPS_CORRECT_2,
19393 NULL, WXS_BASIC_CAST attr,
19394 "The value of the value constraint is not valid",
19395 NULL, NULL);
19396 return(pctxt->err);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019397 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019398 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019399
19400 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019401}
19402
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019403static xmlSchemaElementPtr
19404xmlSchemaCheckSubstGroupCircular(xmlSchemaElementPtr elemDecl,
19405 xmlSchemaElementPtr ancestor)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019406{
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019407 xmlSchemaElementPtr ret;
19408
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019409 if (WXS_SUBST_HEAD(ancestor) == NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019410 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019411 if (WXS_SUBST_HEAD(ancestor) == elemDecl)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019412 return (ancestor);
19413
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019414 if (WXS_SUBST_HEAD(ancestor)->flags & XML_SCHEMAS_ELEM_CIRCULAR)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019415 return (NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019416 WXS_SUBST_HEAD(ancestor)->flags |= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019417 ret = xmlSchemaCheckSubstGroupCircular(elemDecl,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019418 WXS_SUBST_HEAD(ancestor));
19419 WXS_SUBST_HEAD(ancestor)->flags ^= XML_SCHEMAS_ELEM_CIRCULAR;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019420
19421 return (ret);
19422}
19423
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019424/**
19425 * xmlSchemaCheckElemPropsCorrect:
19426 * @ctxt: a schema parser context
19427 * @decl: the element declaration
19428 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019429 *
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019430 * Schema Component Constraint:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019431 * Element Declaration Properties Correct (e-props-correct)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019432 *
19433 * STATUS:
19434 * missing: (6)
19435 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019436static int
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019437xmlSchemaCheckElemPropsCorrect(xmlSchemaParserCtxtPtr pctxt,
19438 xmlSchemaElementPtr elemDecl)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019439{
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019440 int ret = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019441 xmlSchemaTypePtr typeDef = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019442 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019443 * SPEC (1) "The values of the properties of an element declaration
19444 * must be as described in the property tableau in The Element
19445 * Declaration Schema Component (§3.3.1), modulo the impact of Missing
19446 * Sub-components (§5.3)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019447 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019448 if (WXS_SUBST_HEAD(elemDecl) != NULL) {
19449 xmlSchemaElementPtr head = WXS_SUBST_HEAD(elemDecl), circ;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019450
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019451 xmlSchemaCheckElementDeclComponent(head, pctxt);
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019452 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019453 * SPEC (3) "If there is a non-·absent· {substitution group
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019454 * affiliation}, then {scope} must be global."
19455 */
19456 if ((elemDecl->flags & XML_SCHEMAS_ELEM_GLOBAL) == 0) {
19457 xmlSchemaPCustomErr(pctxt,
19458 XML_SCHEMAP_E_PROPS_CORRECT_3,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019459 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019460 "Only global element declarations can have a "
19461 "substitution group affiliation", NULL);
19462 ret = XML_SCHEMAP_E_PROPS_CORRECT_3;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019463 }
19464 /*
19465 * TODO: SPEC (6) "Circular substitution groups are disallowed.
19466 * That is, it must not be possible to return to an element declaration
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019467 * by repeatedly following the {substitution group affiliation}
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019468 * property."
19469 */
19470 if (head == elemDecl)
19471 circ = head;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019472 else if (WXS_SUBST_HEAD(head) != NULL)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019473 circ = xmlSchemaCheckSubstGroupCircular(head, head);
19474 else
19475 circ = NULL;
19476 if (circ != NULL) {
19477 xmlChar *strA = NULL, *strB = NULL;
19478
19479 xmlSchemaPCustomErrExt(pctxt,
19480 XML_SCHEMAP_E_PROPS_CORRECT_6,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019481 WXS_BASIC_CAST circ, NULL,
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019482 "The element declaration '%s' defines a circular "
19483 "substitution group to element declaration '%s'",
19484 xmlSchemaGetComponentQName(&strA, circ),
19485 xmlSchemaGetComponentQName(&strB, head),
19486 NULL);
19487 FREE_AND_NULL(strA)
19488 FREE_AND_NULL(strB)
19489 ret = XML_SCHEMAP_E_PROPS_CORRECT_6;
19490 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019491 /*
19492 * SPEC (4) "If there is a {substitution group affiliation},
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019493 * the {type definition}
19494 * of the element declaration must be validly derived from the {type
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019495 * definition} of the {substitution group affiliation}, given the value
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019496 * of the {substitution group exclusions} of the {substitution group
19497 * affiliation}, as defined in Type Derivation OK (Complex) (§3.4.6)
19498 * (if the {type definition} is complex) or as defined in
19499 * Type Derivation OK (Simple) (§3.14.6) (if the {type definition} is
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019500 * simple)."
19501 *
19502 * NOTE: {substitution group exclusions} means the values of the
19503 * attribute "final".
19504 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019505
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019506 if (typeDef != WXS_ELEM_TYPEDEF(WXS_SUBST_HEAD(elemDecl))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019507 int set = 0;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019508
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019509 if (head->flags & XML_SCHEMAS_ELEM_FINAL_EXTENSION)
19510 set |= SUBSET_EXTENSION;
19511 if (head->flags & XML_SCHEMAS_ELEM_FINAL_RESTRICTION)
19512 set |= SUBSET_RESTRICTION;
19513
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000019514 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST pctxt, typeDef,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019515 WXS_ELEM_TYPEDEF(head), set) != 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019516 xmlChar *strA = NULL, *strB = NULL, *strC = NULL;
19517
19518 ret = XML_SCHEMAP_E_PROPS_CORRECT_4;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019519 xmlSchemaPCustomErrExt(pctxt,
19520 XML_SCHEMAP_E_PROPS_CORRECT_4,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019521 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019522 "The type definition '%s' was "
19523 "either rejected by the substitution group "
19524 "affiliation '%s', or not validly derived from its type "
19525 "definition '%s'",
19526 xmlSchemaGetComponentQName(&strA, typeDef),
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019527 xmlSchemaGetComponentQName(&strB, head),
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019528 xmlSchemaGetComponentQName(&strC, WXS_ELEM_TYPEDEF(head)));
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019529 FREE_AND_NULL(strA)
19530 FREE_AND_NULL(strB)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019531 FREE_AND_NULL(strC)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019532 }
19533 }
19534 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019535 /*
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019536 * SPEC (5) "If the {type definition} or {type definition}'s
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019537 * {content type}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019538 * is or is derived from ID then there must not be a {value constraint}.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019539 * Note: The use of ID as a type definition for elements goes beyond
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019540 * XML 1.0, and should be avoided if backwards compatibility is desired"
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019541 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019542 if ((elemDecl->value != NULL) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019543 ((WXS_IS_SIMPLE(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019544 xmlSchemaIsDerivedFromBuiltInType(typeDef, XML_SCHEMAS_ID)) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019545 (WXS_IS_COMPLEX(typeDef) &&
19546 WXS_HAS_SIMPLE_CONTENT(typeDef) &&
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019547 xmlSchemaIsDerivedFromBuiltInType(typeDef->contentTypeDef,
19548 XML_SCHEMAS_ID)))) {
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019549
19550 ret = XML_SCHEMAP_E_PROPS_CORRECT_5;
19551 xmlSchemaPCustomErr(pctxt,
19552 XML_SCHEMAP_E_PROPS_CORRECT_5,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019553 WXS_BASIC_CAST elemDecl, NULL,
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019554 "The type definition (or type definition's content type) is or "
19555 "is derived from ID; value constraints are not allowed in "
19556 "conjunction with such a type definition", NULL);
19557 } else if (elemDecl->value != NULL) {
19558 int vcret;
19559 xmlNodePtr node = NULL;
19560
19561 /*
19562 * SPEC (2) "If there is a {value constraint}, the canonical lexical
19563 * representation of its value must be ·valid· with respect to the
19564 * {type definition} as defined in Element Default Valid (Immediate)
19565 * (§3.3.6)."
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019566 */
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019567 if (typeDef == NULL) {
19568 xmlSchemaPErr(pctxt, elemDecl->node,
19569 XML_SCHEMAP_INTERNAL,
19570 "Internal error: xmlSchemaCheckElemPropsCorrect, "
19571 "type is missing... skipping validation of "
19572 "the value constraint", NULL, NULL);
19573 return (-1);
19574 }
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019575 if (elemDecl->node != NULL) {
19576 if (elemDecl->flags & XML_SCHEMAS_ELEM_FIXED)
19577 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19578 BAD_CAST "fixed");
19579 else
19580 node = (xmlNodePtr) xmlHasProp(elemDecl->node,
19581 BAD_CAST "default");
19582 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019583 vcret = xmlSchemaParseCheckCOSValidDefault(pctxt, node,
19584 typeDef, elemDecl->value, &(elemDecl->defVal));
19585 if (vcret != 0) {
19586 if (vcret < 0) {
19587 PERROR_INT("xmlSchemaElemCheckValConstr",
19588 "failed to validate the value constraint of an "
19589 "element declaration");
19590 return (-1);
19591 }
19592 return (vcret);
19593 }
19594 }
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019595
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019596 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019597}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019598
19599/**
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019600 * xmlSchemaCheckElemSubstGroup:
19601 * @ctxt: a schema parser context
19602 * @decl: the element declaration
19603 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019604 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019605 * Schema Component Constraint:
19606 * Substitution Group (cos-equiv-class)
19607 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019608 * In Libxml2 the subst. groups will be precomputed, in terms of that
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019609 * a list will be built for each subst. group head, holding all direct
19610 * referents to this head.
19611 * NOTE that this function needs:
19612 * 1. circular subst. groups to be checked beforehand
19613 * 2. the declaration's type to be derived from the head's type
19614 *
19615 * STATUS:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019616 *
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019617 */
19618static void
19619xmlSchemaCheckElemSubstGroup(xmlSchemaParserCtxtPtr ctxt,
19620 xmlSchemaElementPtr elemDecl)
19621{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019622 if ((WXS_SUBST_HEAD(elemDecl) == NULL) ||
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019623 /* SPEC (1) "Its {abstract} is false." */
19624 (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT))
19625 return;
19626 {
19627 xmlSchemaElementPtr head;
19628 xmlSchemaTypePtr headType, type;
19629 int set, methSet;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019630 /*
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019631 * SPEC (2) "It is validly substitutable for HEAD subject to HEAD's
19632 * {disallowed substitutions} as the blocking constraint, as defined in
19633 * Substitution Group OK (Transitive) (§3.3.6)."
19634 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019635 for (head = WXS_SUBST_HEAD(elemDecl); head != NULL;
19636 head = WXS_SUBST_HEAD(head)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019637 set = 0;
19638 methSet = 0;
19639 /*
19640 * The blocking constraints.
19641 */
19642 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_SUBSTITUTION)
19643 continue;
19644 headType = head->subtypes;
19645 type = elemDecl->subtypes;
19646 if (headType == type)
19647 goto add_member;
19648 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION)
19649 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19650 if (head->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION)
19651 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19652 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019653 * SPEC: Substitution Group OK (Transitive) (2.3)
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019654 * "The set of all {derivation method}s involved in the
19655 * derivation of D's {type definition} from C's {type definition}
19656 * does not intersect with the union of the blocking constraint,
19657 * C's {prohibited substitutions} (if C is complex, otherwise the
19658 * empty set) and the {prohibited substitutions} (respectively the
19659 * empty set) of any intermediate {type definition}s in the
19660 * derivation of D's {type definition} from C's {type definition}."
19661 */
19662 /*
19663 * OPTIMIZE TODO: Optimize this a bit, since, if traversing the
19664 * subst.head axis, the methSet does not need to be computed for
19665 * the full depth over and over.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019666 */
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019667 /*
19668 * The set of all {derivation method}s involved in the derivation
19669 */
19670 while ((type != NULL) && (type != headType)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019671 if ((WXS_IS_EXTENSION(type)) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019672 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19673 methSet |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019674
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019675 if (WXS_IS_RESTRICTION(type) &&
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019676 ((methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19677 methSet |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19678
19679 type = type->baseType;
19680 }
19681 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019682 * The {prohibited substitutions} of all intermediate types +
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019683 * the head's type.
19684 */
19685 type = elemDecl->subtypes->baseType;
19686 while (type != NULL) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019687 if (WXS_IS_COMPLEX(type)) {
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019688 if ((type->flags &
19689 XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19690 ((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) == 0))
19691 set |= XML_SCHEMAS_TYPE_BLOCK_EXTENSION;
19692 if ((type->flags &
19693 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19694 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) == 0))
19695 set |= XML_SCHEMAS_TYPE_BLOCK_RESTRICTION;
19696 } else
19697 break;
19698 if (type == headType)
19699 break;
19700 type = type->baseType;
19701 }
19702 if ((set != 0) &&
19703 (((set & XML_SCHEMAS_TYPE_BLOCK_EXTENSION) &&
19704 (methSet & XML_SCHEMAS_TYPE_BLOCK_EXTENSION)) ||
19705 ((set & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION) &&
19706 (methSet & XML_SCHEMAS_TYPE_BLOCK_RESTRICTION)))) {
19707 continue;
19708 }
19709add_member:
19710 xmlSchemaAddElementSubstitutionMember(ctxt, head, elemDecl);
19711 if ((head->flags & XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD) == 0)
19712 head->flags |= XML_SCHEMAS_ELEM_SUBST_GROUP_HEAD;
19713 }
19714 }
19715}
19716
19717/**
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019718 * xmlSchemaCheckElementDeclComponent
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019719 * @item: an schema element declaration/particle
19720 * @ctxt: a schema parser context
19721 * @name: the name of the attribute
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019722 *
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019723 * Validates the value constraints of an element declaration.
19724 *
19725 * Fixes finish doing the computations on the element declarations.
19726 */
19727static void
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019728xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019729 xmlSchemaParserCtxtPtr ctxt)
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000019730{
19731 if (elemDecl == NULL)
19732 return;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019733 if (elemDecl->flags & XML_SCHEMAS_ELEM_INTERNAL_CHECKED)
19734 return;
19735 elemDecl->flags |= XML_SCHEMAS_ELEM_INTERNAL_CHECKED;
Kasimier T. Buchcik016d92e2005-04-08 20:08:40 +000019736 if (xmlSchemaCheckElemPropsCorrect(ctxt, elemDecl) == 0)
19737 xmlSchemaCheckElemSubstGroup(ctxt, elemDecl);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019738}
19739
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019740/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019741 * xmlSchemaResolveModelGroupParticleReferences:
19742 * @particle: a particle component
19743 * @ctxt: a parser context
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019744 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019745 * Resolves references of a model group's {particles} to
19746 * model group definitions and to element declarations.
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019747 */
19748static void
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019749xmlSchemaResolveModelGroupParticleReferences(
19750 xmlSchemaParserCtxtPtr ctxt,
19751 xmlSchemaModelGroupPtr mg)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019752{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019753 xmlSchemaParticlePtr particle = WXS_MODELGROUP_PARTICLE(mg);
19754 xmlSchemaQNameRefPtr ref;
19755 xmlSchemaBasicItemPtr refItem;
19756
19757 /*
19758 * URGENT TODO: Test this.
19759 */
19760 while (particle != NULL) {
19761 if ((WXS_PARTICLE_TERM(particle) == NULL) ||
19762 ((WXS_PARTICLE_TERM(particle))->type !=
19763 XML_SCHEMA_EXTRA_QNAMEREF))
19764 {
19765 goto next_particle;
19766 }
19767 ref = WXS_QNAME_CAST WXS_PARTICLE_TERM(particle);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019768 /*
19769 * Resolve the reference.
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019770 * NULL the {term} by default.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019771 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019772 particle->children = NULL;
19773
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019774 refItem = xmlSchemaGetNamedComponent(ctxt->schema,
19775 ref->itemType, ref->name, ref->targetNamespace);
19776 if (refItem == NULL) {
19777 xmlSchemaPResCompAttrErr(ctxt, XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019778 NULL, WXS_ITEM_NODE(particle), "ref", ref->name,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019779 ref->targetNamespace, ref->itemType, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019780 /* TODO: remove the particle. */
19781 goto next_particle;
19782 }
19783 if (refItem->type == XML_SCHEMA_TYPE_GROUP) {
19784 if (WXS_MODELGROUPDEF_MODEL(refItem) == NULL)
19785 /* TODO: remove the particle. */
19786 goto next_particle;
19787 /*
19788 * NOTE that we will assign the model group definition
19789 * itself to the "term" of the particle. This will ease
19790 * the check for circular model group definitions. After
19791 * that the "term" will be assigned the model group of the
19792 * model group definition.
19793 */
19794 if ((WXS_MODELGROUPDEF_MODEL(refItem))->type ==
19795 XML_SCHEMA_TYPE_ALL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019796 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019797 * SPEC cos-all-limited (1)
19798 * SPEC cos-all-limited (1.2)
19799 * "It appears only as the value of one or both of the
19800 * following properties:"
19801 * (1.1) "the {model group} property of a model group
19802 * definition."
19803 * (1.2) "the {term} property of a particle [... of] the "
19804 * {content type} of a complex type definition."
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000019805 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019806 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19807 /* TODO: error code */
19808 XML_SCHEMAP_COS_ALL_LIMITED,
19809 WXS_ITEM_NODE(particle), NULL,
19810 "A model group definition is referenced, but "
19811 "it contains an 'all' model group, which "
19812 "cannot be contained by model groups",
19813 NULL, NULL);
19814 /* TODO: remove the particle. */
19815 goto next_particle;
19816 }
19817 particle->children = (xmlSchemaTreeItemPtr) refItem;
19818 } else {
19819 /*
19820 * TODO: Are referenced element declarations the only
19821 * other components we expect here?
19822 */
19823 particle->children = (xmlSchemaTreeItemPtr) refItem;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019824 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019825next_particle:
19826 particle = WXS_PTC_CAST particle->next;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019827 }
19828}
19829
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019830static int
19831xmlSchemaAreValuesEqual(xmlSchemaValPtr x,
19832 xmlSchemaValPtr y)
19833{
19834 xmlSchemaTypePtr tx, ty, ptx, pty;
19835 int ret;
19836
19837 while (x != NULL) {
19838 /* Same types. */
19839 tx = xmlSchemaGetBuiltInType(xmlSchemaGetValType(x));
19840 ty = xmlSchemaGetBuiltInType(xmlSchemaGetValType(y));
19841 ptx = xmlSchemaGetPrimitiveType(tx);
19842 pty = xmlSchemaGetPrimitiveType(ty);
19843 /*
19844 * (1) if a datatype T' is ·derived· by ·restriction· from an
19845 * atomic datatype T then the ·value space· of T' is a subset of
19846 * the ·value space· of T. */
19847 /*
19848 * (2) if datatypes T' and T'' are ·derived· by ·restriction·
19849 * from a common atomic ancestor T then the ·value space·s of T'
19850 * and T'' may overlap.
19851 */
19852 if (ptx != pty)
19853 return(0);
19854 /*
19855 * We assume computed values to be normalized, so do a fast
19856 * string comparison for string based types.
19857 */
19858 if ((ptx->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019859 WXS_IS_ANY_SIMPLE_TYPE(ptx)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019860 if (! xmlStrEqual(
19861 xmlSchemaValueGetAsString(x),
19862 xmlSchemaValueGetAsString(y)))
19863 return (0);
19864 } else {
19865 ret = xmlSchemaCompareValuesWhtsp(
19866 x, XML_SCHEMA_WHITESPACE_PRESERVE,
19867 y, XML_SCHEMA_WHITESPACE_PRESERVE);
19868 if (ret == -2)
19869 return(-1);
19870 if (ret != 0)
19871 return(0);
19872 }
19873 /*
19874 * Lists.
19875 */
19876 x = xmlSchemaValueGetNext(x);
19877 if (x != NULL) {
19878 y = xmlSchemaValueGetNext(y);
19879 if (y == NULL)
19880 return (0);
19881 } else if (xmlSchemaValueGetNext(y) != NULL)
19882 return (0);
19883 else
19884 return (1);
19885 }
19886 return (0);
19887}
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000019888
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019889/**
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019890 * xmlSchemaResolveAttrUseReferences:
19891 * @item: an attribute use
19892 * @ctxt: a parser context
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019893 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019894 * Resolves the referenced attribute declaration.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000019895 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019896static int
19897xmlSchemaResolveAttrUseReferences(xmlSchemaAttributeUsePtr ause,
19898 xmlSchemaParserCtxtPtr ctxt)
Daniel Veillard4255d502002-04-16 15:50:10 +000019899{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019900 if ((ctxt == NULL) || (ause == NULL))
19901 return(-1);
19902 if ((ause->attrDecl == NULL) ||
19903 (ause->attrDecl->type != XML_SCHEMA_EXTRA_QNAMEREF))
19904 return(0);
19905
19906 {
19907 xmlSchemaQNameRefPtr ref = WXS_QNAME_CAST ause->attrDecl;
19908
19909 /*
19910 * TODO: Evaluate, what errors could occur if the declaration is not
19911 * found.
19912 */
19913 ause->attrDecl = xmlSchemaGetAttributeDecl(ctxt->schema,
19914 ref->name, ref->targetNamespace);
19915 if (ause->attrDecl == NULL) {
19916 xmlSchemaPResCompAttrErr(ctxt,
19917 XML_SCHEMAP_SRC_RESOLVE,
19918 WXS_BASIC_CAST ause, ause->node,
19919 "ref", ref->name, ref->targetNamespace,
19920 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
19921 return(ctxt->err);;
19922 }
19923 }
19924 return(0);
19925}
19926
19927/**
19928 * xmlSchemaCheckAttrUsePropsCorrect:
19929 * @ctxt: a parser context
19930 * @use: an attribute use
19931 *
19932 * Schema Component Constraint:
19933 * Attribute Use Correct (au-props-correct)
19934 *
19935 */
19936static int
19937xmlSchemaCheckAttrUsePropsCorrect(xmlSchemaParserCtxtPtr ctxt,
19938 xmlSchemaAttributeUsePtr use)
19939{
19940 if ((ctxt == NULL) || (use == NULL))
19941 return(-1);
19942 if ((use->defValue == NULL) || (WXS_ATTRUSE_DECL(use) == NULL) ||
19943 ((WXS_ATTRUSE_DECL(use))->type != XML_SCHEMA_TYPE_ATTRIBUTE))
19944 return(0);
19945
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000019946 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019947 * SPEC au-props-correct (1)
19948 * "The values of the properties of an attribute use must be as
19949 * described in the property tableau in The Attribute Use Schema
19950 * Component (§3.5.1), modulo the impact of Missing
19951 * Sub-components (§5.3)."
Daniel Veillardc0826a72004-08-10 14:17:33 +000019952 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000019953
19954 if (((WXS_ATTRUSE_DECL(use))->defValue != NULL) &&
19955 ((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMAS_ATTR_FIXED) &&
19956 ((use->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
19957 {
19958 xmlSchemaPCustomErr(ctxt,
19959 XML_SCHEMAP_AU_PROPS_CORRECT_2,
19960 WXS_BASIC_CAST use, NULL,
19961 "The attribute declaration has a 'fixed' value constraint "
19962 ", thus the attribute use must also have a 'fixed' value "
19963 "constraint",
19964 NULL);
19965 return(ctxt->err);
19966 }
19967 /*
19968 * Compute and check the value constraint's value.
19969 */
19970 if ((use->defVal != NULL) && (WXS_ATTRUSE_TYPEDEF(use) != NULL)) {
19971 int ret;
19972 /*
19973 * TODO: The spec seems to be missing a check of the
19974 * value constraint of the attribute use. We will do it here.
19975 */
19976 /*
19977 * SPEC a-props-correct (3)
19978 */
19979 if (xmlSchemaIsDerivedFromBuiltInType(
19980 WXS_ATTRUSE_TYPEDEF(use), XML_SCHEMAS_ID))
19981 {
19982 xmlSchemaCustomErr(ACTXT_CAST ctxt,
19983 XML_SCHEMAP_AU_PROPS_CORRECT,
19984 NULL, WXS_BASIC_CAST use,
19985 "Value constraints are not allowed if the type definition "
19986 "is or is derived from xs:ID",
19987 NULL, NULL);
19988 return(ctxt->err);
19989 }
19990
19991 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST ctxt,
19992 use->node, WXS_ATTRUSE_TYPEDEF(use),
19993 use->defValue, &(use->defVal),
19994 1, 1, 0);
19995 if (ret != 0) {
19996 if (ret < 0) {
19997 PERROR_INT2("xmlSchemaCheckAttrUsePropsCorrect",
19998 "calling xmlSchemaVCheckCVCSimpleType()");
19999 return(-1);
20000 }
20001 xmlSchemaCustomErr(ACTXT_CAST ctxt,
20002 XML_SCHEMAP_AU_PROPS_CORRECT,
20003 NULL, WXS_BASIC_CAST use,
20004 "The value of the value constraint is not valid",
20005 NULL, NULL);
20006 return(ctxt->err);
20007 }
20008 }
20009 /*
20010 * SPEC au-props-correct (2)
20011 * "If the {attribute declaration} has a fixed
20012 * {value constraint}, then if the attribute use itself has a
20013 * {value constraint}, it must also be fixed and its value must match
20014 * that of the {attribute declaration}'s {value constraint}."
20015 */
20016 if (((WXS_ATTRUSE_DECL(use))->defVal != NULL) &&
20017 (((WXS_ATTRUSE_DECL(use))->flags & XML_SCHEMA_ATTR_USE_FIXED) == 0))
20018 {
20019 if (! xmlSchemaAreValuesEqual(use->defVal,
20020 (WXS_ATTRUSE_DECL(use))->defVal))
20021 {
20022 xmlSchemaPCustomErr(ctxt,
20023 XML_SCHEMAP_AU_PROPS_CORRECT_2,
20024 WXS_BASIC_CAST use, NULL,
20025 "The 'fixed' value constraint of the attribute use "
20026 "must match the attribute declaration's value "
20027 "constraint '%s'",
20028 (WXS_ATTRUSE_DECL(use))->defValue);
20029 }
20030 return(ctxt->err);
20031 }
20032 return(0);
20033}
20034
20035
20036
20037
20038/**
20039 * xmlSchemaResolveAttrTypeReferences:
20040 * @item: an attribute declaration
20041 * @ctxt: a parser context
20042 *
20043 * Resolves the referenced type definition component.
20044 */
20045static int
20046xmlSchemaResolveAttrTypeReferences(xmlSchemaAttributePtr item,
20047 xmlSchemaParserCtxtPtr ctxt)
20048{
Daniel Veillard01fa6152004-06-29 17:04:39 +000020049 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020050 * The simple type definition corresponding to the <simpleType> element
20051 * information item in the [children], if present, otherwise the simple
20052 * type definition ·resolved· to by the ·actual value· of the type
Daniel Veillard01fa6152004-06-29 17:04:39 +000020053 * [attribute], if present, otherwise the ·simple ur-type definition·.
20054 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020055 if (item->flags & XML_SCHEMAS_ATTR_INTERNAL_RESOLVED)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020056 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020057 item->flags |= XML_SCHEMAS_ATTR_INTERNAL_RESOLVED;
20058 if (item->subtypes != NULL)
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020059 return(0);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020060 if (item->typeName != NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000020061 xmlSchemaTypePtr type;
Daniel Veillard4255d502002-04-16 15:50:10 +000020062
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020063 type = xmlSchemaGetType(ctxt->schema, item->typeName,
20064 item->typeNs);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020065 if ((type == NULL) || (! WXS_IS_SIMPLE(type))) {
Daniel Veillardc0826a72004-08-10 14:17:33 +000020066 xmlSchemaPResCompAttrErr(ctxt,
Daniel Veillard01fa6152004-06-29 17:04:39 +000020067 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020068 WXS_BASIC_CAST item, item->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020069 "type", item->typeName, item->typeNs,
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020070 XML_SCHEMA_TYPE_SIMPLE, NULL);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020071 return(ctxt->err);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020072 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000020073 item->subtypes = type;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020074
Daniel Veillard3646d642004-06-02 19:19:14 +000020075 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020076 /*
20077 * The type defaults to the xs:anySimpleType.
20078 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020079 item->subtypes = xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYSIMPLETYPE);
20080 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020081 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +000020082}
20083
20084/**
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020085 * xmlSchemaResolveIDCKeyReferences:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020086 * @idc: the identity-constraint definition
20087 * @ctxt: the schema parser context
20088 * @name: the attribute name
20089 *
20090 * Resolve keyRef references to key/unique IDCs.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020091 * Schema Component Constraint:
20092 * Identity-constraint Definition Properties Correct (c-props-correct)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020093 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020094static int
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020095xmlSchemaResolveIDCKeyReferences(xmlSchemaIDCPtr idc,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020096 xmlSchemaParserCtxtPtr pctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020097{
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020098 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020099 return(0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020100 if (idc->ref->name != NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020101 idc->ref->item = (xmlSchemaBasicItemPtr)
20102 xmlSchemaGetIDC(pctxt->schema, idc->ref->name,
20103 idc->ref->targetNamespace);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020104 if (idc->ref->item == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020105 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020106 * TODO: It is actually not an error to fail to resolve
20107 * at this stage. BUT we need to be that strict!
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020108 */
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020109 xmlSchemaPResCompAttrErr(pctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020110 XML_SCHEMAP_SRC_RESOLVE,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020111 WXS_BASIC_CAST idc, idc->node,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020112 "refer", idc->ref->name,
20113 idc->ref->targetNamespace,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020114 XML_SCHEMA_TYPE_IDC_KEY, NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020115 return(pctxt->err);
20116 } else if (idc->ref->item->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
20117 /*
20118 * SPEC c-props-correct (1)
20119 */
20120 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20121 XML_SCHEMAP_C_PROPS_CORRECT,
20122 NULL, WXS_BASIC_CAST idc,
20123 "The keyref references a keyref",
20124 NULL, NULL);
20125 idc->ref->item = NULL;
20126 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020127 } else {
20128 if (idc->nbFields !=
20129 ((xmlSchemaIDCPtr) idc->ref->item)->nbFields) {
20130 xmlChar *str = NULL;
20131 xmlSchemaIDCPtr refer;
20132
20133 refer = (xmlSchemaIDCPtr) idc->ref->item;
20134 /*
20135 * SPEC c-props-correct(2)
20136 * "If the {identity-constraint category} is keyref,
20137 * the cardinality of the {fields} must equal that of
20138 * the {fields} of the {referenced key}.
20139 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020140 xmlSchemaCustomErr(ACTXT_CAST pctxt,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020141 XML_SCHEMAP_C_PROPS_CORRECT,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020142 NULL, WXS_BASIC_CAST idc,
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020143 "The cardinality of the keyref differs from the "
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020144 "cardinality of the referenced key/unique '%s'",
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020145 xmlSchemaFormatQName(&str, refer->targetNamespace,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020146 refer->name),
20147 NULL);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020148 FREE_AND_NULL(str)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020149 return(pctxt->err);
Kasimier T. Buchcikc306d902005-07-08 21:37:44 +000020150 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020151 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020152 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020153 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020154}
20155
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020156static int
20157xmlSchemaResolveAttrUseProhibReferences(xmlSchemaAttributeUseProhibPtr prohib,
20158 xmlSchemaParserCtxtPtr pctxt)
20159{
20160 if (xmlSchemaGetAttributeDecl(pctxt->schema, prohib->name,
20161 prohib->targetNamespace) == NULL) {
20162
20163 xmlSchemaPResCompAttrErr(pctxt,
20164 XML_SCHEMAP_SRC_RESOLVE,
20165 NULL, prohib->node,
20166 "ref", prohib->name, prohib->targetNamespace,
20167 XML_SCHEMA_TYPE_ATTRIBUTE, NULL);
20168 return(XML_SCHEMAP_SRC_RESOLVE);
20169 }
20170 return(0);
20171}
20172
20173#define WXS_REDEFINED_TYPE(c) \
20174(((xmlSchemaTypePtr) item)->flags & XML_SCHEMAS_TYPE_REDEFINED)
20175
20176#define WXS_REDEFINED_MODEL_GROUP_DEF(c) \
20177(((xmlSchemaModelGroupDefPtr) item)->flags & XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20178
20179#define WXS_REDEFINED_ATTR_GROUP(c) \
20180(((xmlSchemaAttributeGroupPtr) item)->flags & XML_SCHEMAS_ATTRGROUP_REDEFINED)
20181
20182static int
20183xmlSchemaCheckSRCRedefineFirst(xmlSchemaParserCtxtPtr pctxt)
20184{
20185 int err = 0;
20186 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20187 xmlSchemaBasicItemPtr prev, item;
20188 int wasRedefined;
20189
20190 if (redef == NULL)
20191 return(0);
20192
20193 do {
20194 item = redef->item;
20195 /*
20196 * First try to locate the redefined component in the
20197 * schema graph starting with the redefined schema.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020198 * NOTE: According to this schema bug entry:
20199 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005OctDec/0019.html
20200 * it's not clear if the referenced component needs to originate
20201 * from the <redefine>d schema _document_ or the schema; the latter
20202 * would include all imported and included sub-schemas of the
20203 * <redefine>d schema. Currenlty we latter approach is used.
20204 * SUPPLEMENT: It seems that the WG moves towards the latter
20205 * approach, so we are doing it right.
20206 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020207 */
20208 prev = xmlSchemaFindRedefCompInGraph(
20209 redef->targetBucket, item->type,
20210 redef->refName, redef->refTargetNs);
20211 if (prev == NULL) {
20212 xmlChar *str = NULL;
20213 xmlNodePtr node;
20214
20215 /*
20216 * SPEC src-redefine:
20217 * (6.2.1) "The ·actual value· of its own name attribute plus
20218 * target namespace must successfully ·resolve· to a model
20219 * group definition in I."
20220 * (7.2.1) "The ·actual value· of its own name attribute plus
20221 * target namespace must successfully ·resolve· to an attribute
20222 * group definition in I."
20223
20224 *
20225 * Note that, if we are redefining with the use of references
20226 * to components, the spec assumes the src-resolve to be used;
20227 * but this won't assure that we search only *inside* the
20228 * redefined schema.
20229 */
20230 if (redef->reference)
20231 node = WXS_ITEM_NODE(redef->reference);
20232 else
20233 node = WXS_ITEM_NODE(item);
20234 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20235 /*
20236 * TODO: error code.
20237 * Probably XML_SCHEMAP_SRC_RESOLVE, if this is using the
20238 * reference kind.
20239 */
20240 XML_SCHEMAP_SRC_REDEFINE, node, NULL,
20241 "The %s to be redefined could not be found in "
20242 "the redefined schema",
20243 WXS_ITEM_TYPE_NAME(item),
20244 xmlSchemaFormatQName(&str, redef->refTargetNs,
20245 redef->refName));
20246 FREE_AND_NULL(str);
20247 err = pctxt->err;
20248 redef = redef->next;
20249 continue;
20250 }
20251 /*
20252 * TODO: Obtaining and setting the redefinition state is really
20253 * clumsy.
20254 */
20255 wasRedefined = 0;
20256 switch (item->type) {
20257 case XML_SCHEMA_TYPE_COMPLEX:
20258 case XML_SCHEMA_TYPE_SIMPLE:
20259 if ((WXS_TYPE_CAST prev)->flags &
20260 XML_SCHEMAS_TYPE_REDEFINED)
20261 {
20262 wasRedefined = 1;
20263 break;
20264 }
20265 /* Mark it as redefined. */
20266 (WXS_TYPE_CAST prev)->flags |= XML_SCHEMAS_TYPE_REDEFINED;
20267 /*
20268 * Assign the redefined type to the
20269 * base type of the redefining type.
20270 * TODO: How
20271 */
20272 ((xmlSchemaTypePtr) item)->baseType =
20273 (xmlSchemaTypePtr) prev;
20274 break;
20275 case XML_SCHEMA_TYPE_GROUP:
20276 if ((WXS_MODEL_GROUPDEF_CAST prev)->flags &
20277 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED)
20278 {
20279 wasRedefined = 1;
20280 break;
20281 }
20282 /* Mark it as redefined. */
20283 (WXS_MODEL_GROUPDEF_CAST prev)->flags |=
20284 XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED;
20285 if (redef->reference != NULL) {
20286 /*
20287 * Overwrite the QName-reference with the
20288 * referenced model group def.
20289 */
20290 (WXS_PTC_CAST redef->reference)->children =
20291 WXS_TREE_CAST prev;
20292 }
20293 redef->target = prev;
20294 break;
20295 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20296 if ((WXS_ATTR_GROUP_CAST prev)->flags &
20297 XML_SCHEMAS_ATTRGROUP_REDEFINED)
20298 {
20299 wasRedefined = 1;
20300 break;
20301 }
20302 (WXS_ATTR_GROUP_CAST prev)->flags |=
20303 XML_SCHEMAS_ATTRGROUP_REDEFINED;
20304 if (redef->reference != NULL) {
20305 /*
20306 * Assign the redefined attribute group to the
20307 * QName-reference component.
20308 * This is the easy case, since we will just
20309 * expand the redefined group.
20310 */
20311 (WXS_QNAME_CAST redef->reference)->item = prev;
20312 redef->target = NULL;
20313 } else {
20314 /*
20315 * This is the complicated case: we need
20316 * to apply src-redefine (7.2.2) at a later
20317 * stage, i.e. when attribute group references
20318 * have beed expanded and simple types have
20319 * beed fixed.
20320 */
20321 redef->target = prev;
20322 }
20323 break;
20324 default:
20325 PERROR_INT("xmlSchemaResolveRedefReferences",
20326 "Unexpected redefined component type");
20327 return(-1);
20328 }
20329 if (wasRedefined) {
20330 xmlChar *str = NULL;
20331 xmlNodePtr node;
20332
20333 if (redef->reference)
20334 node = WXS_ITEM_NODE(redef->reference);
20335 else
20336 node = WXS_ITEM_NODE(redef->item);
20337
20338 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20339 /* TODO: error code. */
20340 XML_SCHEMAP_SRC_REDEFINE,
20341 node, NULL,
20342 "The referenced %s was already redefined. Multiple "
20343 "redefinition of the same component is not supported",
20344 xmlSchemaGetComponentDesignation(&str, prev),
20345 NULL);
20346 FREE_AND_NULL(str)
20347 err = pctxt->err;
20348 redef = redef->next;
20349 continue;
20350 }
20351 redef = redef->next;
20352 } while (redef != NULL);
20353
20354 return(err);
20355}
20356
20357static int
20358xmlSchemaCheckSRCRedefineSecond(xmlSchemaParserCtxtPtr pctxt)
20359{
20360 int err = 0;
20361 xmlSchemaRedefPtr redef = WXS_CONSTRUCTOR(pctxt)->redefs;
20362 xmlSchemaBasicItemPtr item;
20363
20364 if (redef == NULL)
20365 return(0);
20366
20367 do {
20368 if (redef->target == NULL) {
20369 redef = redef->next;
20370 continue;
20371 }
20372 item = redef->item;
20373
20374 switch (item->type) {
20375 case XML_SCHEMA_TYPE_SIMPLE:
20376 case XML_SCHEMA_TYPE_COMPLEX:
20377 /*
20378 * Since the spec wants the {name} of the redefined
20379 * type to be 'absent', we'll NULL it.
20380 */
20381 (WXS_TYPE_CAST redef->target)->name = NULL;
20382
20383 /*
20384 * TODO: Seems like there's nothing more to do. The normal
20385 * inheritance mechanism is used. But not 100% sure.
20386 */
20387 break;
20388 case XML_SCHEMA_TYPE_GROUP:
20389 /*
20390 * URGENT TODO:
20391 * SPEC src-redefine:
20392 * (6.2.2) "The {model group} of the model group definition
20393 * which corresponds to it per XML Representation of Model
20394 * Group Definition Schema Components (§3.7.2) must be a
20395 * ·valid restriction· of the {model group} of that model
20396 * group definition in I, as defined in Particle Valid
20397 * (Restriction) (§3.9.6)."
20398 */
20399 break;
20400 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20401 /*
20402 * SPEC src-redefine:
20403 * (7.2.2) "The {attribute uses} and {attribute wildcard} of
20404 * the attribute group definition which corresponds to it
20405 * per XML Representation of Attribute Group Definition Schema
20406 * Components (§3.6.2) must be ·valid restrictions· of the
20407 * {attribute uses} and {attribute wildcard} of that attribute
20408 * group definition in I, as defined in clause 2, clause 3 and
20409 * clause 4 of Derivation Valid (Restriction, Complex)
20410 * (§3.4.6) (where references to the base type definition are
20411 * understood as references to the attribute group definition
20412 * in I)."
20413 */
20414 err = xmlSchemaCheckDerivationOKRestriction2to4(pctxt,
20415 XML_SCHEMA_ACTION_REDEFINE,
20416 item, redef->target,
20417 (WXS_ATTR_GROUP_CAST item)->attrUses,
20418 (WXS_ATTR_GROUP_CAST redef->target)->attrUses,
20419 (WXS_ATTR_GROUP_CAST item)->attributeWildcard,
20420 (WXS_ATTR_GROUP_CAST redef->target)->attributeWildcard);
20421 if (err == -1)
20422 return(-1);
20423 break;
20424 default:
20425 break;
20426 }
20427 redef = redef->next;
20428 } while (redef != NULL);
20429 return(0);
20430}
20431
20432
20433static int
20434xmlSchemaAddComponents(xmlSchemaParserCtxtPtr pctxt,
20435 xmlSchemaBucketPtr bucket)
20436{
20437 xmlSchemaBasicItemPtr item;
20438 int err;
20439 xmlHashTablePtr *table;
20440 const xmlChar *name;
20441 int i;
20442
20443#define WXS_GET_GLOBAL_HASH(c, s, slot) { \
20444 if (WXS_IS_BUCKET_IMPMAIN((c)->type)) \
20445 table = &(WXS_IMPBUCKET((c))->schema->slot); \
20446 else \
20447 table = &(WXS_INCBUCKET((c))->ownerImport->schema->slot); }
20448
20449 /*
20450 * Add global components to the schema's hash tables.
20451 * This is the place where duplicate components will be
20452 * detected.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000020453 * TODO: I think normally we should support imports of the
20454 * same namespace from multiple locations. We don't do currently,
20455 * but if we do then according to:
20456 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2224
20457 * we would need, if imported directly, to import redefined
20458 * components as well to be able to catch clashing components.
20459 * (I hope I'll still know what this means after some months :-()
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020460 */
20461 if (bucket == NULL)
20462 return(-1);
20463 if (bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED)
20464 return(0);
20465 bucket->flags |= XML_SCHEMA_BUCKET_COMPS_ADDED;
20466
20467 for (i = 0; i < bucket->globals->nbItems; i++) {
20468 item = bucket->globals->items[i];
20469 table = NULL;
20470 switch (item->type) {
20471 case XML_SCHEMA_TYPE_COMPLEX:
20472 case XML_SCHEMA_TYPE_SIMPLE:
20473 if (WXS_REDEFINED_TYPE(item))
20474 continue;
20475 name = (WXS_TYPE_CAST item)->name;
20476 WXS_GET_GLOBAL_HASH(bucket, schema, typeDecl)
20477 break;
20478 case XML_SCHEMA_TYPE_ELEMENT:
20479 name = (WXS_ELEM_CAST item)->name;
20480 WXS_GET_GLOBAL_HASH(bucket, schema, elemDecl)
20481 break;
20482 case XML_SCHEMA_TYPE_ATTRIBUTE:
20483 name = (WXS_ATTR_CAST item)->name;
20484 WXS_GET_GLOBAL_HASH(bucket, schema, attrDecl)
20485 break;
20486 case XML_SCHEMA_TYPE_GROUP:
20487 if (WXS_REDEFINED_MODEL_GROUP_DEF(item))
20488 continue;
20489 name = (WXS_MODEL_GROUPDEF_CAST item)->name;
20490 WXS_GET_GLOBAL_HASH(bucket, schema, groupDecl)
20491 break;
20492 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20493 if (WXS_REDEFINED_ATTR_GROUP(item))
20494 continue;
20495 name = (WXS_ATTR_GROUP_CAST item)->name;
20496 WXS_GET_GLOBAL_HASH(bucket, schema, attrgrpDecl)
20497 break;
20498 case XML_SCHEMA_TYPE_IDC_KEY:
20499 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20500 case XML_SCHEMA_TYPE_IDC_KEYREF:
20501 name = (WXS_IDC_CAST item)->name;
20502 WXS_GET_GLOBAL_HASH(bucket, schema, idcDef)
20503 break;
20504 case XML_SCHEMA_TYPE_NOTATION:
20505 name = ((xmlSchemaNotationPtr) item)->name;
20506 WXS_GET_GLOBAL_HASH(bucket, schema, notaDecl)
20507 break;
20508 default:
20509 PERROR_INT("xmlSchemaAddComponents",
20510 "Unexpected global component type");
20511 continue;
20512 }
20513 if (*table == NULL) {
20514 *table = xmlHashCreateDict(10, pctxt->dict);
20515 if (*table == NULL) {
20516 PERROR_INT("xmlSchemaAddComponents",
20517 "failed to create a component hash table");
20518 return(-1);
20519 }
20520 }
20521 err = xmlHashAddEntry(*table, name, item);
20522 if (err != 0) {
20523 xmlChar *str = NULL;
20524
20525 xmlSchemaCustomErr(ACTXT_CAST pctxt,
20526 XML_SCHEMAP_REDEFINED_TYPE,
20527 WXS_ITEM_NODE(item),
20528 WXS_BASIC_CAST item,
20529 "A global %s '%s' does already exist",
20530 WXS_ITEM_TYPE_NAME(item),
20531 xmlSchemaGetComponentQName(&str, item));
20532 FREE_AND_NULL(str);
20533 }
20534 }
20535 /*
20536 * Process imported/included schemas.
20537 */
20538 if (bucket->relations != NULL) {
20539 xmlSchemaSchemaRelationPtr rel = bucket->relations;
20540 do {
20541 if ((rel->bucket != NULL) &&
20542 ((rel->bucket->flags & XML_SCHEMA_BUCKET_COMPS_ADDED) == 0)) {
20543 if (xmlSchemaAddComponents(pctxt, rel->bucket) == -1)
20544 return(-1);
20545 }
20546 rel = rel->next;
20547 } while (rel != NULL);
20548 }
20549 return(0);
20550}
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020551
20552static int
20553xmlSchemaFixupComponents(xmlSchemaParserCtxtPtr pctxt)
20554{
20555 xmlSchemaConstructionCtxtPtr con = pctxt->constructor;
20556 xmlSchemaTreeItemPtr item, *items;
20557 int nbItems, i;
20558
20559#define FIXHFAILURE if (pctxt->err == XML_SCHEMAP_INTERNAL) goto exit_failure;
20560
20561 if ((con->pending == NULL) ||
20562 (con->pending->nbItems == 0))
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020563 return(0);
20564
20565 /* TODO:
20566 * SPEC (src-redefine):
20567 * (6.2) "If it has no such self-reference, then all of the
20568 * following must be true:"
20569
20570 * (6.2.2) The {model group} of the model group definition which
20571 * corresponds to it per XML Representation of Model Group
20572 * Definition Schema Components (§3.7.2) must be a ·valid
20573 * restriction· of the {model group} of that model group definition
20574 * in I, as defined in Particle Valid (Restriction) (§3.9.6)."
20575 */
20576 xmlSchemaCheckSRCRedefineFirst(pctxt);
20577
20578 /*
20579 * Add global components to the schemata's hash tables.
20580 */
20581 xmlSchemaAddComponents(pctxt, WXS_CONSTRUCTOR(pctxt)->mainBucket);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020582
20583 pctxt->ctxtType = NULL;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020584 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20585 nbItems = con->pending->nbItems;
20586 /*
20587 * Now that we have parsed *all* the schema document(s) and converted
20588 * them to schema components, we can resolve references, apply component
20589 * constraints, create the FSA from the content model, etc.
20590 */
20591 /*
20592 * Resolve references of..
20593 *
20594 * 1. element declarations:
20595 * - the type definition
20596 * - the substitution group affiliation
20597 * 2. simple/complex types:
20598 * - the base type definition
20599 * - the memberTypes of union types
20600 * - the itemType of list types
20601 * 3. attributes declarations and attribute uses:
20602 * - the type definition
20603 * - if an attribute use, then the attribute declaration
20604 * 4. attribute group references:
20605 * - the attribute group definition
20606 * 5. particles:
20607 * - the term of the particle (e.g. a model group)
20608 * 6. IDC key-references:
20609 * - the referenced IDC 'key' or 'unique' definition
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020610 * 7. Attribute prohibitions which had a "ref" attribute.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020611 */
20612 for (i = 0; i < nbItems; i++) {
20613 item = items[i];
20614 switch (item->type) {
20615 case XML_SCHEMA_TYPE_ELEMENT:
20616 xmlSchemaResolveElementReferences(
20617 (xmlSchemaElementPtr) item, pctxt);
20618 FIXHFAILURE;
20619 break;
20620 case XML_SCHEMA_TYPE_COMPLEX:
20621 case XML_SCHEMA_TYPE_SIMPLE:
20622 xmlSchemaResolveTypeReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020623 (xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020624 FIXHFAILURE;
20625 break;
20626 case XML_SCHEMA_TYPE_ATTRIBUTE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020627 xmlSchemaResolveAttrTypeReferences(
20628 (xmlSchemaAttributePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020629 FIXHFAILURE;
20630 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020631 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20632 xmlSchemaResolveAttrUseReferences(
20633 (xmlSchemaAttributeUsePtr) item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020634 FIXHFAILURE;
20635 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020636 case XML_SCHEMA_EXTRA_QNAMEREF:
20637 if ((WXS_QNAME_CAST item)->itemType ==
20638 XML_SCHEMA_TYPE_ATTRIBUTEGROUP)
20639 {
20640 xmlSchemaResolveAttrGroupReferences(
20641 WXS_QNAME_CAST item, pctxt);
20642 }
20643 FIXHFAILURE;
20644 break;
20645 case XML_SCHEMA_TYPE_SEQUENCE:
20646 case XML_SCHEMA_TYPE_CHOICE:
20647 case XML_SCHEMA_TYPE_ALL:
20648 xmlSchemaResolveModelGroupParticleReferences(pctxt,
20649 WXS_MODEL_GROUP_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020650 FIXHFAILURE;
20651 break;
20652 case XML_SCHEMA_TYPE_IDC_KEY:
20653 case XML_SCHEMA_TYPE_IDC_UNIQUE:
20654 case XML_SCHEMA_TYPE_IDC_KEYREF:
20655 xmlSchemaResolveIDCKeyReferences(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020656 (xmlSchemaIDCPtr) item, pctxt);
20657 FIXHFAILURE;
20658 break;
20659 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
20660 /*
20661 * Handle attribue prohibition which had a
20662 * "ref" attribute.
20663 */
20664 xmlSchemaResolveAttrUseProhibReferences(
20665 WXS_ATTR_PROHIB_CAST item, pctxt);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020666 FIXHFAILURE;
20667 break;
20668 default:
20669 break;
20670 }
20671 }
20672 if (pctxt->nberrors != 0)
20673 goto exit_error;
20674
20675 /*
20676 * Now that all references are resolved we
20677 * can check for circularity of...
20678 * 1. the base axis of type definitions
20679 * 2. nested model group definitions
20680 * 3. nested attribute group definitions
20681 * TODO: check for circual substitution groups.
20682 */
20683 for (i = 0; i < nbItems; i++) {
20684 item = items[i];
20685 /*
20686 * Let's better stop on the first error here.
20687 */
20688 switch (item->type) {
20689 case XML_SCHEMA_TYPE_COMPLEX:
20690 case XML_SCHEMA_TYPE_SIMPLE:
20691 xmlSchemaCheckTypeDefCircular(
20692 (xmlSchemaTypePtr) item, pctxt);
20693 FIXHFAILURE;
20694 if (pctxt->nberrors != 0)
20695 goto exit_error;
20696 break;
20697 case XML_SCHEMA_TYPE_GROUP:
20698 xmlSchemaCheckGroupDefCircular(
20699 (xmlSchemaModelGroupDefPtr) item, pctxt);
20700 FIXHFAILURE;
20701 if (pctxt->nberrors != 0)
20702 goto exit_error;
20703 break;
20704 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20705 xmlSchemaCheckAttrGroupCircular(
20706 (xmlSchemaAttributeGroupPtr) item, pctxt);
20707 FIXHFAILURE;
20708 if (pctxt->nberrors != 0)
20709 goto exit_error;
20710 break;
20711 default:
20712 break;
20713 }
20714 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020715 if (pctxt->nberrors != 0)
20716 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020717 /*
20718 * Model group definition references:
20719 * Such a reference is reflected by a particle at the component
20720 * level. Until now the 'term' of such particles pointed
20721 * to the model group definition; this was done, in order to
20722 * ease circularity checks. Now we need to set the 'term' of
20723 * such particles to the model group of the model group definition.
20724 */
20725 for (i = 0; i < nbItems; i++) {
20726 item = items[i];
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020727 switch (item->type) {
20728 case XML_SCHEMA_TYPE_SEQUENCE:
20729 case XML_SCHEMA_TYPE_CHOICE:
20730 xmlSchemaModelGroupToModelGroupDefFixup(pctxt,
20731 WXS_MODEL_GROUP_CAST item);
20732 break;
20733 default:
20734 break;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020735 }
20736 }
20737 if (pctxt->nberrors != 0)
20738 goto exit_error;
20739 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020740 * Expand attribute group references of attribute group definitions.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020741 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020742 for (i = 0; i < nbItems; i++) {
20743 item = items[i];
20744 switch (item->type) {
20745 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20746 if ((! WXS_ATTR_GROUP_EXPANDED(item)) &&
20747 WXS_ATTR_GROUP_HAS_REFS(item))
20748 {
20749 xmlSchemaAttributeGroupExpandRefs(pctxt,
20750 WXS_ATTR_GROUP_CAST item);
20751 FIXHFAILURE;
20752 }
20753 break;
20754 default:
20755 break;
20756 }
20757 }
20758 if (pctxt->nberrors != 0)
20759 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020760 /*
20761 * First compute the variety of simple types. This is needed as
20762 * a seperate step, since otherwise we won't be able to detect
20763 * circular union types in all cases.
20764 */
20765 for (i = 0; i < nbItems; i++) {
20766 item = items[i];
20767 switch (item->type) {
20768 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020769 if (WXS_IS_TYPE_NOT_FIXED_1((xmlSchemaTypePtr) item)) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020770 xmlSchemaFixupSimpleTypeStageOne(pctxt,
20771 (xmlSchemaTypePtr) item);
20772 FIXHFAILURE;
20773 }
20774 break;
20775 default:
20776 break;
20777 }
20778 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020779 if (pctxt->nberrors != 0)
20780 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020781 /*
20782 * Detect circular union types. Note that this needs the variety to
20783 * be already computed.
20784 */
20785 for (i = 0; i < nbItems; i++) {
20786 item = items[i];
20787 switch (item->type) {
20788 case XML_SCHEMA_TYPE_SIMPLE:
20789 if (((xmlSchemaTypePtr) item)->memberTypes != NULL) {
20790 xmlSchemaCheckUnionTypeDefCircular(pctxt,
20791 (xmlSchemaTypePtr) item);
20792 FIXHFAILURE;
20793 }
20794 break;
20795 default:
20796 break;
20797 }
20798 }
20799 if (pctxt->nberrors != 0)
20800 goto exit_error;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020801
20802 /*
20803 * Do the complete type fixup for simple types.
20804 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020805 for (i = 0; i < nbItems; i++) {
20806 item = items[i];
20807 switch (item->type) {
20808 case XML_SCHEMA_TYPE_SIMPLE:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020809 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20810 xmlSchemaFixupSimpleTypeStageTwo(pctxt, WXS_TYPE_CAST item);
20811 FIXHFAILURE;
20812 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020813 break;
20814 default:
20815 break;
20816 }
20817 }
20818 if (pctxt->nberrors != 0)
20819 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020820 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020821 * At this point we need all simple types to be builded and checked.
20822 */
20823 /*
20824 * Apply contraints for attribute declarations.
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020825 */
20826 for (i = 0; i < nbItems; i++) {
20827 item = items[i];
20828 switch (item->type) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020829 case XML_SCHEMA_TYPE_ATTRIBUTE:
20830 xmlSchemaCheckAttrPropsCorrect(pctxt, WXS_ATTR_CAST item);
20831 FIXHFAILURE;
20832 break;
20833 default:
20834 break;
20835 }
20836 }
20837 if (pctxt->nberrors != 0)
20838 goto exit_error;
20839 /*
20840 * Apply constraints for attribute uses.
20841 */
20842 for (i = 0; i < nbItems; i++) {
20843 item = items[i];
20844 switch (item->type) {
20845 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
20846 if (((xmlSchemaAttributeUsePtr)item)->defValue != NULL) {
20847 xmlSchemaCheckAttrUsePropsCorrect(pctxt,
20848 WXS_ATTR_USE_CAST item);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020849 FIXHFAILURE;
20850 }
20851 break;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020852 default:
20853 break;
20854 }
20855 }
20856 if (pctxt->nberrors != 0)
20857 goto exit_error;
20858
20859 /*
20860 * Apply constraints for attribute group definitions.
20861 */
20862 for (i = 0; i < nbItems; i++) {
20863 item = items[i];
20864 switch (item->type) {
20865 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
20866 if (( (WXS_ATTR_GROUP_CAST item)->attrUses != NULL) &&
20867 ( (WXS_LIST_CAST (WXS_ATTR_GROUP_CAST item)->attrUses)->nbItems > 1))
20868 {
20869 xmlSchemaCheckAGPropsCorrect(pctxt, WXS_ATTR_GROUP_CAST item);
20870 FIXHFAILURE;
20871 }
20872 break;
20873 default:
20874 break;
20875 }
20876 }
20877 if (pctxt->nberrors != 0)
20878 goto exit_error;
20879
20880 /*
20881 * Apply constraints for redefinitions.
20882 */
20883 if (WXS_CONSTRUCTOR(pctxt)->redefs != NULL)
20884 xmlSchemaCheckSRCRedefineSecond(pctxt);
20885 if (pctxt->nberrors != 0)
20886 goto exit_error;
20887
20888 /*
20889 * Fixup complex types.
20890 */
20891 for (i = 0; i < nbItems; i++) {
20892 item = con->pending->items[i];
20893 switch (item->type) {
20894 case XML_SCHEMA_TYPE_COMPLEX:
20895 if (WXS_IS_TYPE_NOT_FIXED(WXS_TYPE_CAST item)) {
20896 xmlSchemaFixupComplexType(pctxt, WXS_TYPE_CAST item);
20897 FIXHFAILURE;
20898 }
20899 break;
20900 default:
20901 break;
20902 }
20903 }
20904 if (pctxt->nberrors != 0)
20905 goto exit_error;
20906
20907 /*
20908 * The list could have changed, since xmlSchemaFixupComplexType()
20909 * will create particles and model groups in some cases.
20910 */
20911 items = (xmlSchemaTreeItemPtr *) con->pending->items;
20912 nbItems = con->pending->nbItems;
20913
20914 /*
20915 * At this point all complex types need to be builded and checked.
20916 */
20917 /*
20918 * Apply some constraints for element declarations.
20919 */
20920 for (i = 0; i < nbItems; i++) {
20921 item = items[i];
20922 switch (item->type) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020923 case XML_SCHEMA_TYPE_ELEMENT:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020924
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020925 if ((((xmlSchemaElementPtr) item)->flags &
20926 XML_SCHEMAS_ELEM_INTERNAL_CHECKED) == 0) {
20927 xmlSchemaCheckElementDeclComponent(
20928 (xmlSchemaElementPtr) item, pctxt);
20929 FIXHFAILURE;
20930 }
20931 break;
20932 default:
20933 break;
20934 }
20935 }
20936 if (pctxt->nberrors != 0)
20937 goto exit_error;
20938 /*
20939 * Finally we can build the automaton from the content model of
20940 * complex types.
20941 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020942
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020943 for (i = 0; i < nbItems; i++) {
20944 item = items[i];
20945 switch (item->type) {
20946 case XML_SCHEMA_TYPE_COMPLEX:
20947 xmlSchemaBuildContentModel((xmlSchemaTypePtr) item, pctxt);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020948 /* FIXHFAILURE; */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020949 break;
20950 default:
20951 break;
20952 }
20953 }
20954 if (pctxt->nberrors != 0)
20955 goto exit_error;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020956 /*
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000020957 * URGENT TODO: cos-element-consistent
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020958 */
20959 con->pending->nbItems = 0;
20960 return(0);
20961exit_error:
20962 con->pending->nbItems = 0;
20963 return(pctxt->err);
20964exit_failure:
20965 con->pending->nbItems = 0;
20966 return(-1);
20967}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000020968/**
Daniel Veillard4255d502002-04-16 15:50:10 +000020969 * xmlSchemaParse:
20970 * @ctxt: a schema validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000020971 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000020972 * parse a schema definition resource and build an internal
Daniel Veillard4255d502002-04-16 15:50:10 +000020973 * XML Shema struture which can be used to validate instances.
Daniel Veillard4255d502002-04-16 15:50:10 +000020974 *
20975 * Returns the internal XML Schema structure built from the resource or
20976 * NULL in case of error
20977 */
20978xmlSchemaPtr
20979xmlSchemaParse(xmlSchemaParserCtxtPtr ctxt)
20980{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020981 xmlSchemaPtr schema = NULL;
20982 xmlSchemaBucketPtr bucket = NULL;
20983 int res;
Daniel Veillard4255d502002-04-16 15:50:10 +000020984
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020985 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000020986 * This one is used if the schema to be parsed was specified via
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000020987 * the API; i.e. not automatically by the validated instance document.
20988 */
20989
Daniel Veillard4255d502002-04-16 15:50:10 +000020990 xmlSchemaInitTypes();
20991
Daniel Veillard6045c902002-10-09 21:13:59 +000020992 if (ctxt == NULL)
Daniel Veillard4255d502002-04-16 15:50:10 +000020993 return (NULL);
20994
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020995 /* TODO: Init the context. Is this all we need?*/
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000020996 ctxt->nberrors = 0;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000020997 ctxt->err = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000020998 ctxt->counter = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000020999
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021000 /* Create the *main* schema. */
21001 schema = xmlSchemaNewSchema(ctxt);
21002 if (schema == NULL)
21003 goto exit_failure;
Daniel Veillard4255d502002-04-16 15:50:10 +000021004 /*
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021005 * Create the schema constructor.
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021006 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021007 if (ctxt->constructor == NULL) {
21008 ctxt->constructor = xmlSchemaConstructionCtxtCreate(ctxt->dict);
21009 if (ctxt->constructor == NULL)
21010 return(NULL);
21011 /* Take ownership of the constructor to be able to free it. */
21012 ctxt->ownsConstructor = 1;
21013 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021014 ctxt->constructor->mainSchema = schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021015 /*
21016 * Locate and add the schema document.
21017 */
21018 res = xmlSchemaAddSchemaDoc(ctxt, XML_SCHEMA_SCHEMA_MAIN,
21019 ctxt->URL, ctxt->doc, ctxt->buffer, ctxt->size, NULL,
21020 NULL, NULL, &bucket);
21021 if (res == -1)
21022 goto exit_failure;
21023 if (res != 0)
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000021024 goto exit;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021025
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021026 if (bucket == NULL) {
21027 /* TODO: Error code, actually we failed to *locate* the schema. */
21028 if (ctxt->URL)
21029 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21030 NULL, NULL,
21031 "Failed to locate the main schema resource at '%s'",
21032 ctxt->URL, NULL);
21033 else
21034 xmlSchemaCustomErr(ACTXT_CAST ctxt, XML_SCHEMAP_FAILED_LOAD,
21035 NULL, NULL,
21036 "Failed to locate the main schema resource",
21037 NULL, NULL);
21038 goto exit;
21039 }
21040 /* Set the main schema bucket. */
21041 ctxt->constructor->bucket = bucket;
21042 ctxt->targetNamespace = bucket->targetNamespace;
21043 schema->targetNamespace = bucket->targetNamespace;
21044
21045 /* Then do the parsing for good. */
21046 if (xmlSchemaParseNewDocWithContext(ctxt, schema, bucket) == -1)
21047 goto exit_failure;
Kasimier T. Buchcik1a99a392005-04-08 11:23:56 +000021048 if (ctxt->nberrors != 0)
21049 goto exit;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021050
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021051 schema->doc = bucket->doc;
21052 schema->preserve = ctxt->preserve;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021053
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021054 ctxt->schema = schema;
21055
21056 if (xmlSchemaFixupComponents(ctxt) == -1)
21057 goto exit_failure;
21058
21059 /*
21060 * TODO: This is not nice, since we cannot distinguish from the
21061 * result if there was an internal error or not.
21062 */
21063exit:
21064 if (ctxt->nberrors != 0) {
21065 if (schema) {
21066 xmlSchemaFree(schema);
21067 schema = NULL;
21068 }
21069 if (ctxt->constructor) {
21070 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21071 ctxt->constructor = NULL;
21072 ctxt->ownsConstructor = 0;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000021073 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021074 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021075 ctxt->schema = NULL;
21076 return(schema);
21077exit_failure:
21078 /*
21079 * Quite verbose, but should catch internal errors, which were
21080 * not communitated.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021081 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021082 if (schema) {
21083 xmlSchemaFree(schema);
21084 schema = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021085 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021086 if (ctxt->constructor) {
21087 xmlSchemaConstructionCtxtFree(ctxt->constructor);
21088 ctxt->constructor = NULL;
21089 ctxt->ownsConstructor = 0;
Daniel Veillard75bb3bb2003-05-12 15:25:56 +000021090 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021091 PERROR_INT2("xmlSchemaParse",
21092 "An internal error occured");
21093 ctxt->schema = NULL;
21094 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000021095}
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021096
Daniel Veillard4255d502002-04-16 15:50:10 +000021097/**
Daniel Veillard01c13b52002-12-10 15:19:08 +000021098 * xmlSchemaSetParserErrors:
Daniel Veillard4255d502002-04-16 15:50:10 +000021099 * @ctxt: a schema validation context
Daniel Veillard01c13b52002-12-10 15:19:08 +000021100 * @err: the error callback
21101 * @warn: the warning callback
21102 * @ctx: contextual data for the callbacks
Daniel Veillard4255d502002-04-16 15:50:10 +000021103 *
Daniel Veillard01c13b52002-12-10 15:19:08 +000021104 * Set the callback functions used to handle errors for a validation context
Daniel Veillard4255d502002-04-16 15:50:10 +000021105 */
21106void
21107xmlSchemaSetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021108 xmlSchemaValidityErrorFunc err,
21109 xmlSchemaValidityWarningFunc warn, void *ctx)
21110{
Daniel Veillard4255d502002-04-16 15:50:10 +000021111 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021112 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000021113 ctxt->error = err;
21114 ctxt->warning = warn;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021115 ctxt->errCtxt = ctx;
21116 if (ctxt->vctxt != NULL)
21117 xmlSchemaSetValidErrors(ctxt->vctxt, err, warn, ctx);
21118}
21119
21120/**
21121 * xmlSchemaSetParserStructuredErrors:
21122 * @ctxt: a schema parser context
21123 * @serror: the structured error function
21124 * @ctx: the functions context
21125 *
21126 * Set the structured error callback
21127 */
21128void
21129xmlSchemaSetParserStructuredErrors(xmlSchemaParserCtxtPtr ctxt,
21130 xmlStructuredErrorFunc serror,
21131 void *ctx)
21132{
21133 if (ctxt == NULL)
21134 return;
21135 ctxt->serror = serror;
21136 ctxt->errCtxt = ctx;
21137 if (ctxt->vctxt != NULL)
21138 xmlSchemaSetValidStructuredErrors(ctxt->vctxt, serror, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000021139}
21140
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021141/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000021142 * xmlSchemaGetParserErrors:
21143 * @ctxt: a XMl-Schema parser context
21144 * @err: the error callback result
21145 * @warn: the warning callback result
21146 * @ctx: contextual data for the callbacks result
21147 *
21148 * Get the callback information used to handle errors for a parser context
21149 *
21150 * Returns -1 in case of failure, 0 otherwise
21151 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021152int
Daniel Veillard259f0df2004-08-18 09:13:18 +000021153xmlSchemaGetParserErrors(xmlSchemaParserCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021154 xmlSchemaValidityErrorFunc * err,
21155 xmlSchemaValidityWarningFunc * warn, void **ctx)
Daniel Veillard259f0df2004-08-18 09:13:18 +000021156{
21157 if (ctxt == NULL)
21158 return(-1);
21159 if (err != NULL)
21160 *err = ctxt->error;
21161 if (warn != NULL)
21162 *warn = ctxt->warning;
21163 if (ctx != NULL)
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000021164 *ctx = ctxt->errCtxt;
Daniel Veillard259f0df2004-08-18 09:13:18 +000021165 return(0);
21166}
21167
21168/**
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021169 * xmlSchemaFacetTypeToString:
21170 * @type: the facet type
21171 *
21172 * Convert the xmlSchemaTypeType to a char string.
21173 *
21174 * Returns the char string representation of the facet type if the
21175 * type is a facet and an "Internal Error" string otherwise.
21176 */
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021177static const xmlChar *
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021178xmlSchemaFacetTypeToString(xmlSchemaTypeType type)
21179{
21180 switch (type) {
21181 case XML_SCHEMA_FACET_PATTERN:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021182 return (BAD_CAST "pattern");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021183 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021184 return (BAD_CAST "maxExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021185 case XML_SCHEMA_FACET_MAXINCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021186 return (BAD_CAST "maxInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021187 case XML_SCHEMA_FACET_MINEXCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021188 return (BAD_CAST "minExclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021189 case XML_SCHEMA_FACET_MININCLUSIVE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021190 return (BAD_CAST "minInclusive");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021191 case XML_SCHEMA_FACET_WHITESPACE:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021192 return (BAD_CAST "whiteSpace");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021193 case XML_SCHEMA_FACET_ENUMERATION:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021194 return (BAD_CAST "enumeration");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021195 case XML_SCHEMA_FACET_LENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021196 return (BAD_CAST "length");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021197 case XML_SCHEMA_FACET_MAXLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021198 return (BAD_CAST "maxLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021199 case XML_SCHEMA_FACET_MINLENGTH:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021200 return (BAD_CAST "minLength");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021201 case XML_SCHEMA_FACET_TOTALDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021202 return (BAD_CAST "totalDigits");
Daniel Veillardd0c9c322003-10-10 00:49:42 +000021203 case XML_SCHEMA_FACET_FRACTIONDIGITS:
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021204 return (BAD_CAST "fractionDigits");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021205 default:
21206 break;
21207 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021208 return (BAD_CAST "Internal Error");
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021209}
21210
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021211static xmlSchemaWhitespaceValueType
Daniel Veillardc0826a72004-08-10 14:17:33 +000021212xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type)
21213{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021214 /*
21215 * The normalization type can be changed only for types which are derived
Daniel Veillardc0826a72004-08-10 14:17:33 +000021216 * from xsd:string.
21217 */
21218 if (type->type == XML_SCHEMA_TYPE_BASIC) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021219 /*
21220 * Note that we assume a whitespace of preserve for anySimpleType.
21221 */
21222 if ((type->builtInType == XML_SCHEMAS_STRING) ||
21223 (type->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
21224 return(XML_SCHEMA_WHITESPACE_PRESERVE);
Kasimier T. Buchcik91feaf82004-11-12 14:04:58 +000021225 else if (type->builtInType == XML_SCHEMAS_NORMSTRING)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021226 return(XML_SCHEMA_WHITESPACE_REPLACE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021227 else {
21228 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021229 * For all ·atomic· datatypes other than string (and types ·derived·
21230 * by ·restriction· from it) the value of whiteSpace is fixed to
Daniel Veillardc0826a72004-08-10 14:17:33 +000021231 * collapse
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021232 * Note that this includes built-in list datatypes.
Daniel Veillardc0826a72004-08-10 14:17:33 +000021233 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021234 return(XML_SCHEMA_WHITESPACE_COLLAPSE);
Daniel Veillardc0826a72004-08-10 14:17:33 +000021235 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021236 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021237 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021238 * For list types the facet "whiteSpace" is fixed to "collapse".
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021239 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021240 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021241 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021242 return (XML_SCHEMA_WHITESPACE_UNKNOWN);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021243 } else if (WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021244 if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_PRESERVE)
21245 return (XML_SCHEMA_WHITESPACE_PRESERVE);
21246 else if (type->flags & XML_SCHEMAS_TYPE_WHITESPACE_REPLACE)
21247 return (XML_SCHEMA_WHITESPACE_REPLACE);
21248 else
21249 return (XML_SCHEMA_WHITESPACE_COLLAPSE);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000021250 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021251 return (-1);
Daniel Veillardd3b9cd82003-04-09 11:24:17 +000021252}
21253
Daniel Veillard4255d502002-04-16 15:50:10 +000021254/************************************************************************
21255 * *
21256 * Simple type validation *
21257 * *
21258 ************************************************************************/
Daniel Veillard377e1a92004-04-16 16:30:05 +000021259
Daniel Veillard4255d502002-04-16 15:50:10 +000021260
21261/************************************************************************
21262 * *
21263 * DOM Validation code *
21264 * *
21265 ************************************************************************/
21266
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021267/**
21268 * xmlSchemaAssembleByLocation:
21269 * @pctxt: a schema parser context
21270 * @vctxt: a schema validation context
21271 * @schema: the existing schema
21272 * @node: the node that fired the assembling
21273 * @nsName: the namespace name of the new schema
21274 * @location: the location of the schema
21275 *
21276 * Expands an existing schema by an additional schema.
21277 *
21278 * Returns 0 if the new schema is correct, a positive error code
21279 * number otherwise and -1 in case of an internal or API error.
21280 */
21281static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021282xmlSchemaAssembleByLocation(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021283 xmlSchemaPtr schema,
21284 xmlNodePtr node,
21285 const xmlChar *nsName,
21286 const xmlChar *location)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021287{
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021288 int ret = 0;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021289 xmlSchemaParserCtxtPtr pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021290 xmlSchemaBucketPtr bucket = NULL;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021291
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021292 if ((vctxt == NULL) || (schema == NULL))
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021293 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021294
21295 if (vctxt->pctxt == NULL) {
21296 VERROR_INT("xmlSchemaAssembleByLocation",
21297 "no parser context available");
21298 return(-1);
21299 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021300 pctxt = vctxt->pctxt;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021301 if (pctxt->constructor == NULL) {
21302 PERROR_INT("xmlSchemaAssembleByLocation",
21303 "no constructor");
21304 return(-1);
21305 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021306 /*
21307 * Acquire the schema document.
21308 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021309 location = xmlSchemaBuildAbsoluteURI(pctxt->dict,
21310 location, node);
21311 /*
21312 * Note that we pass XML_SCHEMA_SCHEMA_IMPORT here;
21313 * the process will automatically change this to
21314 * XML_SCHEMA_SCHEMA_MAIN if it is the first schema document.
21315 */
21316 ret = xmlSchemaAddSchemaDoc(pctxt, XML_SCHEMA_SCHEMA_IMPORT,
21317 location, NULL, NULL, 0, node, NULL, nsName,
21318 &bucket);
21319 if (ret != 0)
21320 return(ret);
21321 if (bucket == NULL) {
21322 PERROR_INT("xmlSchemaAssembleByLocation",
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021323 "no schema bucket acquired");
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021324 return(-1);
21325 }
21326 /*
21327 * The first located schema will be handled as if all other
21328 * schemas imported by XSI were imported by this first schema.
21329 */
21330 if ((bucket != NULL) &&
21331 (WXS_CONSTRUCTOR(pctxt)->bucket == NULL))
21332 WXS_CONSTRUCTOR(pctxt)->bucket = bucket;
21333 /*
21334 * TODO: Is this handled like an import? I.e. is it not an error
21335 * if the schema cannot be located?
21336 */
21337 if ((bucket == NULL) || (! CAN_PARSE_SCHEMA(bucket)))
21338 return(0);
21339 /*
21340 * We will reuse the parser context for every schema imported
21341 * directly via XSI. So reset the context.
21342 */
21343 pctxt->nberrors = 0;
21344 pctxt->err = 0;
21345 pctxt->doc = bucket->doc;
21346
21347 ret = xmlSchemaParseNewDocWithContext(pctxt, schema, bucket);
21348 if (ret == -1) {
21349 pctxt->doc = NULL;
21350 goto exit_failure;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021351 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021352 /* Paranoid error channelling. */
21353 if ((ret == 0) && (pctxt->nberrors != 0))
21354 ret = pctxt->err;
21355 if (pctxt->nberrors == 0) {
21356 /*
21357 * Only bother to fixup pending components, if there was
21358 * no error yet.
21359 */
21360 xmlSchemaFixupComponents(pctxt);
21361 vctxt->nberrors += pctxt->nberrors;
21362 } else {
21363 /* Add to validation error sum. */
21364 vctxt->nberrors += pctxt->nberrors;
21365 }
21366 pctxt->doc = NULL;
21367 return(ret);
21368exit_failure:
21369 pctxt->doc = NULL;
21370 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021371}
21372
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021373static xmlSchemaAttrInfoPtr
21374xmlSchemaGetMetaAttrInfo(xmlSchemaValidCtxtPtr vctxt,
21375 int metaType)
21376{
21377 if (vctxt->nbAttrInfos == 0)
21378 return (NULL);
21379 {
21380 int i;
21381 xmlSchemaAttrInfoPtr iattr;
21382
21383 for (i = 0; i < vctxt->nbAttrInfos; i++) {
21384 iattr = vctxt->attrInfos[i];
21385 if (iattr->metaType == metaType)
21386 return (iattr);
21387 }
21388
21389 }
21390 return (NULL);
21391}
21392
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021393/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021394 * xmlSchemaAssembleByXSI:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021395 * @vctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021396 *
21397 * Expands an existing schema by an additional schema using
21398 * the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute
21399 * of an instance. If xsi:noNamespaceSchemaLocation is used, @noNamespace
21400 * must be set to 1.
21401 *
21402 * Returns 0 if the new schema is correct, a positive error code
21403 * number otherwise and -1 in case of an internal or API error.
21404 */
21405static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021406xmlSchemaAssembleByXSI(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021407{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021408 const xmlChar *cur, *end;
21409 const xmlChar *nsname = NULL, *location;
21410 int count = 0;
21411 int ret = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021412 xmlSchemaAttrInfoPtr iattr;
21413
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021414 /*
21415 * Parse the value; we will assume an even number of values
21416 * to be given (this is how Xerces and XSV work).
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000021417 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021418 * URGENT TODO: !! This needs to work for both
21419 * @noNamespaceSchemaLocation AND @schemaLocation on the same
21420 * element !!
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021421 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021422 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
21423 XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC);
21424 if (iattr == NULL)
21425 xmlSchemaGetMetaAttrInfo(vctxt,
21426 XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC);
21427 if (iattr == NULL)
21428 return (0);
21429 cur = iattr->value;
21430 do {
21431 if (iattr->metaType == XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC) {
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021432 /*
21433 * Get the namespace name.
21434 */
21435 while (IS_BLANK_CH(*cur))
21436 cur++;
21437 end = cur;
21438 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21439 end++;
21440 if (end == cur)
21441 break;
21442 count++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021443 nsname = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021444 cur = end;
21445 }
21446 /*
21447 * Get the URI.
21448 */
21449 while (IS_BLANK_CH(*cur))
21450 cur++;
21451 end = cur;
21452 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
21453 end++;
21454 if (end == cur)
21455 break;
21456 count++;
21457 location = xmlDictLookup(vctxt->schema->dict, cur, end - cur);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021458 cur = end;
21459 ret = xmlSchemaAssembleByLocation(vctxt, vctxt->schema,
21460 iattr->node, nsname, location);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021461 if (ret == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021462 VERROR_INT("xmlSchemaAssembleByXSI",
21463 "assembling schemata");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021464 return (-1);
21465 }
21466 } while (*cur != 0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021467 return (ret);
21468}
21469
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021470static const xmlChar *
21471xmlSchemaLookupNamespace(xmlSchemaValidCtxtPtr vctxt,
21472 const xmlChar *prefix)
21473{
21474 if (vctxt->sax != NULL) {
21475 int i, j;
21476 xmlSchemaNodeInfoPtr inode;
21477
21478 for (i = vctxt->depth; i >= 0; i--) {
21479 if (vctxt->elemInfos[i]->nbNsBindings != 0) {
21480 inode = vctxt->elemInfos[i];
21481 for (j = 0; j < inode->nbNsBindings * 2; j += 2) {
21482 if (((prefix == NULL) &&
21483 (inode->nsBindings[j] == NULL)) ||
21484 ((prefix != NULL) && xmlStrEqual(prefix,
21485 inode->nsBindings[j]))) {
21486
21487 /*
21488 * Note that the namespace bindings are already
21489 * in a string dict.
21490 */
21491 return (inode->nsBindings[j+1]);
21492 }
21493 }
21494 }
21495 }
21496 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021497#ifdef LIBXML_WRITER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021498 } else if (vctxt->reader != NULL) {
21499 xmlChar *nsName;
21500
21501 nsName = xmlTextReaderLookupNamespace(vctxt->reader, prefix);
21502 if (nsName != NULL) {
21503 const xmlChar *ret;
21504
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021505 ret = xmlDictLookup(vctxt->dict, nsName, -1);
21506 xmlFree(nsName);
21507 return (ret);
21508 } else
21509 return (NULL);
Daniel Veillard39e5c892005-07-03 22:48:50 +000021510#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021511 } else {
21512 xmlNsPtr ns;
21513
21514 if ((vctxt->inode->node == NULL) ||
21515 (vctxt->inode->node->doc == NULL)) {
21516 VERROR_INT("xmlSchemaLookupNamespace",
21517 "no node or node's doc avaliable");
21518 return (NULL);
21519 }
21520 ns = xmlSearchNs(vctxt->inode->node->doc,
21521 vctxt->inode->node, prefix);
21522 if (ns != NULL)
21523 return (ns->href);
21524 return (NULL);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021525 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000021526}
21527
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021528/*
21529* This one works on the schema of the validation context.
21530*/
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021531static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021532xmlSchemaValidateNotation(xmlSchemaValidCtxtPtr vctxt,
21533 xmlSchemaPtr schema,
21534 xmlNodePtr node,
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021535 const xmlChar *value,
21536 xmlSchemaValPtr *val,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021537 int valNeeded)
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021538{
21539 int ret;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021540
21541 if (vctxt && (vctxt->schema == NULL)) {
21542 VERROR_INT("xmlSchemaValidateNotation",
21543 "a schema is needed on the validation context");
21544 return (-1);
21545 }
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021546 ret = xmlValidateQName(value, 1);
21547 if (ret != 0)
21548 return (ret);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021549 {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021550 xmlChar *localName = NULL;
21551 xmlChar *prefix = NULL;
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021552
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021553 localName = xmlSplitQName2(value, &prefix);
21554 if (prefix != NULL) {
21555 const xmlChar *nsName = NULL;
21556
21557 if (vctxt != NULL)
21558 nsName = xmlSchemaLookupNamespace(vctxt, BAD_CAST prefix);
21559 else if (node != NULL) {
21560 xmlNsPtr ns = xmlSearchNs(node->doc, node, prefix);
21561 if (ns != NULL)
21562 nsName = ns->href;
21563 } else {
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021564 xmlFree(prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021565 xmlFree(localName);
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021566 return (1);
21567 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021568 if (nsName == NULL) {
21569 xmlFree(prefix);
21570 xmlFree(localName);
21571 return (1);
21572 }
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021573 if (xmlSchemaGetNotation(schema, localName, nsName) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021574 if (valNeeded && (val != NULL)) {
21575 (*val) = xmlSchemaNewNOTATIONValue(BAD_CAST localName,
21576 BAD_CAST xmlStrdup(nsName));
21577 if (*val == NULL)
21578 ret = -1;
21579 }
21580 } else
Kasimier T. Buchcikb06b4de2005-02-17 19:00:23 +000021581 ret = 1;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021582 xmlFree(prefix);
21583 xmlFree(localName);
21584 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000021585 if (xmlSchemaGetNotation(schema, value, NULL) != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021586 if (valNeeded && (val != NULL)) {
21587 (*val) = xmlSchemaNewNOTATIONValue(
21588 BAD_CAST xmlStrdup(value), NULL);
21589 if (*val == NULL)
21590 ret = -1;
21591 }
21592 } else
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021593 return (1);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021594 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021595 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021596 return (ret);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000021597}
21598
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000021599static int
21600xmlSchemaVAddNodeQName(xmlSchemaValidCtxtPtr vctxt,
21601 const xmlChar* lname,
21602 const xmlChar* nsname)
21603{
21604 int i;
21605
21606 lname = xmlDictLookup(vctxt->dict, lname, -1);
21607 if (lname == NULL)
21608 return(-1);
21609 if (nsname != NULL) {
21610 nsname = xmlDictLookup(vctxt->dict, nsname, -1);
21611 if (nsname == NULL)
21612 return(-1);
21613 }
21614 for (i = 0; i < vctxt->nodeQNames->nbItems; i += 2) {
21615 if ((vctxt->nodeQNames->items [i] == lname) &&
21616 (vctxt->nodeQNames->items[i +1] == nsname))
21617 /* Already there */
21618 return(i);
21619 }
21620 /* Add new entry. */
21621 i = vctxt->nodeQNames->nbItems;
21622 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) lname);
21623 xmlSchemaItemListAdd(vctxt->nodeQNames, (void *) nsname);
21624 return(i);
21625}
21626
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021627/************************************************************************
21628 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021629 * Validation of identity-constraints (IDC) *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021630 * *
21631 ************************************************************************/
21632
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021633/**
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021634 * xmlSchemaAugmentIDC:
21635 * @idcDef: the IDC definition
21636 *
21637 * Creates an augmented IDC definition item.
21638 *
21639 * Returns the item, or NULL on internal errors.
21640 */
21641static void
21642xmlSchemaAugmentIDC(xmlSchemaIDCPtr idcDef,
21643 xmlSchemaValidCtxtPtr vctxt)
21644{
21645 xmlSchemaIDCAugPtr aidc;
21646
21647 aidc = (xmlSchemaIDCAugPtr) xmlMalloc(sizeof(xmlSchemaIDCAug));
21648 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021649 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021650 "xmlSchemaAugmentIDC: allocating an augmented IDC definition",
21651 NULL);
21652 return;
21653 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021654 aidc->keyrefDepth = -1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021655 aidc->def = idcDef;
21656 aidc->next = NULL;
21657 if (vctxt->aidcs == NULL)
21658 vctxt->aidcs = aidc;
21659 else {
21660 aidc->next = vctxt->aidcs;
21661 vctxt->aidcs = aidc;
21662 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021663 /*
21664 * Save if we have keyrefs at all.
21665 */
21666 if ((vctxt->hasKeyrefs == 0) &&
21667 (idcDef->type == XML_SCHEMA_TYPE_IDC_KEYREF))
21668 vctxt->hasKeyrefs = 1;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021669}
21670
21671/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021672 * xmlSchemaIDCNewBinding:
21673 * @idcDef: the IDC definition of this binding
21674 *
21675 * Creates a new IDC binding.
21676 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021677 * Returns the new IDC binding, NULL on internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021678 */
21679static xmlSchemaPSVIIDCBindingPtr
21680xmlSchemaIDCNewBinding(xmlSchemaIDCPtr idcDef)
21681{
21682 xmlSchemaPSVIIDCBindingPtr ret;
21683
21684 ret = (xmlSchemaPSVIIDCBindingPtr) xmlMalloc(
21685 sizeof(xmlSchemaPSVIIDCBinding));
21686 if (ret == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021687 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021688 "allocating a PSVI IDC binding item", NULL);
21689 return (NULL);
21690 }
21691 memset(ret, 0, sizeof(xmlSchemaPSVIIDCBinding));
21692 ret->definition = idcDef;
21693 return (ret);
21694}
21695
21696/**
21697 * xmlSchemaIDCStoreNodeTableItem:
21698 * @vctxt: the WXS validation context
21699 * @item: the IDC node table item
21700 *
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021701 * The validation context is used to store IDC node table items.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021702 * They are stored to avoid copying them if IDC node-tables are merged
21703 * with corresponding parent IDC node-tables (bubbling).
21704 *
21705 * Returns 0 if succeeded, -1 on internal errors.
21706 */
21707static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021708xmlSchemaIDCStoreNodeTableItem(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021709 xmlSchemaPSVIIDCNodePtr item)
21710{
21711 /*
21712 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021713 */
21714 if (vctxt->idcNodes == NULL) {
21715 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021716 xmlMalloc(20 * sizeof(xmlSchemaPSVIIDCNodePtr));
21717 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021718 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021719 "allocating the IDC node table item list", NULL);
21720 return (-1);
21721 }
21722 vctxt->sizeIdcNodes = 20;
21723 } else if (vctxt->sizeIdcNodes <= vctxt->nbIdcNodes) {
21724 vctxt->sizeIdcNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021725 vctxt->idcNodes = (xmlSchemaPSVIIDCNodePtr *)
21726 xmlRealloc(vctxt->idcNodes, vctxt->sizeIdcNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021727 sizeof(xmlSchemaPSVIIDCNodePtr));
21728 if (vctxt->idcNodes == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021729 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021730 "re-allocating the IDC node table item list", NULL);
21731 return (-1);
21732 }
21733 }
21734 vctxt->idcNodes[vctxt->nbIdcNodes++] = item;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021735
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021736 return (0);
21737}
21738
21739/**
21740 * xmlSchemaIDCStoreKey:
21741 * @vctxt: the WXS validation context
21742 * @item: the IDC key
21743 *
21744 * The validation context is used to store an IDC key.
21745 *
21746 * Returns 0 if succeeded, -1 on internal errors.
21747 */
21748static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021749xmlSchemaIDCStoreKey(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021750 xmlSchemaPSVIIDCKeyPtr key)
21751{
21752 /*
21753 * Add to gobal list.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021754 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021755 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021756 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021757 xmlMalloc(40 * sizeof(xmlSchemaPSVIIDCKeyPtr));
21758 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021759 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021760 "allocating the IDC key storage list", NULL);
21761 return (-1);
21762 }
21763 vctxt->sizeIdcKeys = 40;
21764 } else if (vctxt->sizeIdcKeys <= vctxt->nbIdcKeys) {
21765 vctxt->sizeIdcKeys *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021766 vctxt->idcKeys = (xmlSchemaPSVIIDCKeyPtr *)
21767 xmlRealloc(vctxt->idcKeys, vctxt->sizeIdcKeys *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021768 sizeof(xmlSchemaPSVIIDCKeyPtr));
21769 if (vctxt->idcKeys == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021770 xmlSchemaVErrMemory(vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021771 "re-allocating the IDC key storage list", NULL);
21772 return (-1);
21773 }
21774 }
21775 vctxt->idcKeys[vctxt->nbIdcKeys++] = key;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021776
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021777 return (0);
21778}
21779
21780/**
21781 * xmlSchemaIDCAppendNodeTableItem:
21782 * @bind: the IDC binding
21783 * @ntItem: the node-table item
21784 *
21785 * Appends the IDC node-table item to the binding.
21786 *
21787 * Returns 0 on success and -1 on internal errors.
21788 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021789static int
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021790xmlSchemaIDCAppendNodeTableItem(xmlSchemaPSVIIDCBindingPtr bind,
21791 xmlSchemaPSVIIDCNodePtr ntItem)
21792{
21793 if (bind->nodeTable == NULL) {
21794 bind->sizeNodes = 10;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021795 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021796 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
21797 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021798 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021799 "allocating an array of IDC node-table items", NULL);
21800 return(-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021801 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021802 } else if (bind->sizeNodes <= bind->nbNodes) {
21803 bind->sizeNodes *= 2;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021804 bind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
21805 xmlRealloc(bind->nodeTable, bind->sizeNodes *
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021806 sizeof(xmlSchemaPSVIIDCNodePtr));
21807 if (bind->nodeTable == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021808 xmlSchemaVErrMemory(NULL,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021809 "re-allocating an array of IDC node-table items", NULL);
21810 return(-1);
21811 }
21812 }
21813 bind->nodeTable[bind->nbNodes++] = ntItem;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021814 return(0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021815}
21816
21817/**
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021818 * xmlSchemaIDCAcquireBinding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021819 * @vctxt: the WXS validation context
21820 * @matcher: the IDC matcher
21821 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021822 * Looks up an PSVI IDC binding, for the IDC definition and
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021823 * of the given matcher. If none found, a new one is created
21824 * and added to the IDC table.
21825 *
21826 * Returns an IDC binding or NULL on internal errors.
21827 */
21828static xmlSchemaPSVIIDCBindingPtr
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021829xmlSchemaIDCAcquireBinding(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021830 xmlSchemaIDCMatcherPtr matcher)
21831{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021832 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021833
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021834 ielem = vctxt->elemInfos[matcher->depth];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021835
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021836 if (ielem->idcTable == NULL) {
21837 ielem->idcTable = xmlSchemaIDCNewBinding(matcher->aidc->def);
21838 if (ielem->idcTable == NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021839 return (NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021840 return(ielem->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021841 } else {
21842 xmlSchemaPSVIIDCBindingPtr bind = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021843
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021844 bind = ielem->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021845 do {
21846 if (bind->definition == matcher->aidc->def)
21847 return(bind);
21848 if (bind->next == NULL) {
21849 bind->next = xmlSchemaIDCNewBinding(matcher->aidc->def);
21850 if (bind->next == NULL)
21851 return (NULL);
21852 return(bind->next);
21853 }
21854 bind = bind->next;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021855 } while (bind != NULL);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021856 }
21857 return (NULL);
21858}
21859
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021860static xmlSchemaItemListPtr
21861xmlSchemaIDCAcquireTargetList(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
21862 xmlSchemaIDCMatcherPtr matcher)
21863{
21864 if (matcher->targets == NULL)
21865 matcher->targets = xmlSchemaItemListCreate();
21866 return(matcher->targets);
21867}
21868
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021869/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021870 * xmlSchemaIDCFreeKey:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021871 * @key: the IDC key
21872 *
21873 * Frees an IDC key together with its compiled value.
21874 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021875static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021876xmlSchemaIDCFreeKey(xmlSchemaPSVIIDCKeyPtr key)
21877{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021878 if (key->val != NULL)
21879 xmlSchemaFreeValue(key->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021880 xmlFree(key);
21881}
21882
21883/**
21884 * xmlSchemaIDCFreeBinding:
21885 *
21886 * Frees an IDC binding. Note that the node table-items
21887 * are not freed.
21888 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000021889static void
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021890xmlSchemaIDCFreeBinding(xmlSchemaPSVIIDCBindingPtr bind)
21891{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021892 if (bind->nodeTable != NULL)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021893 xmlFree(bind->nodeTable);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021894 if (bind->dupls != NULL)
21895 xmlSchemaItemListFree(bind->dupls);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021896 xmlFree(bind);
21897}
21898
21899/**
21900 * xmlSchemaIDCFreeIDCTable:
21901 * @bind: the first IDC binding in the list
21902 *
21903 * Frees an IDC table, i.e. all the IDC bindings in the list.
21904 */
21905static void
21906xmlSchemaIDCFreeIDCTable(xmlSchemaPSVIIDCBindingPtr bind)
21907{
21908 xmlSchemaPSVIIDCBindingPtr prev;
21909
21910 while (bind != NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000021911 prev = bind;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021912 bind = bind->next;
21913 xmlSchemaIDCFreeBinding(prev);
21914 }
21915}
21916
21917/**
21918 * xmlSchemaIDCFreeMatcherList:
21919 * @matcher: the first IDC matcher in the list
21920 *
21921 * Frees a list of IDC matchers.
21922 */
21923static void
21924xmlSchemaIDCFreeMatcherList(xmlSchemaIDCMatcherPtr matcher)
21925{
21926 xmlSchemaIDCMatcherPtr next;
21927
21928 while (matcher != NULL) {
21929 next = matcher->next;
21930 if (matcher->keySeqs != NULL) {
21931 int i;
21932 for (i = 0; i < matcher->sizeKeySeqs; i++)
21933 if (matcher->keySeqs[i] != NULL)
21934 xmlFree(matcher->keySeqs[i]);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000021935 xmlFree(matcher->keySeqs);
21936 }
21937 if (matcher->targets != NULL) {
21938 if (matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) {
21939 int i;
21940 xmlSchemaPSVIIDCNodePtr idcNode;
21941 /*
21942 * Node-table items for keyrefs are not stored globally
21943 * to the validation context, since they are not bubbled.
21944 * We need to free them here.
21945 */
21946 for (i = 0; i < matcher->targets->nbItems; i++) {
21947 idcNode =
21948 (xmlSchemaPSVIIDCNodePtr) matcher->targets->items[i];
21949 xmlFree(idcNode->keys);
21950 xmlFree(idcNode);
21951 }
21952 }
21953 xmlSchemaItemListFree(matcher->targets);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021954 }
21955 xmlFree(matcher);
21956 matcher = next;
21957 }
21958}
21959
21960/**
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021961 * xmlSchemaIDCAddStateObject:
21962 * @vctxt: the WXS validation context
21963 * @matcher: the IDC matcher
21964 * @sel: the XPath information
21965 * @parent: the parent "selector" state object if any
21966 * @type: "selector" or "field"
21967 *
21968 * Creates/reuses and activates state objects for the given
21969 * XPath information; if the XPath expression consists of unions,
21970 * multiple state objects are created for every unioned expression.
21971 *
21972 * Returns 0 on success and -1 on internal errors.
21973 */
21974static int
21975xmlSchemaIDCAddStateObject(xmlSchemaValidCtxtPtr vctxt,
21976 xmlSchemaIDCMatcherPtr matcher,
21977 xmlSchemaIDCSelectPtr sel,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021978 int type)
21979{
21980 xmlSchemaIDCStateObjPtr sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021981
21982 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021983 * Reuse the state objects from the pool.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021984 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021985 if (vctxt->xpathStatePool != NULL) {
21986 sto = vctxt->xpathStatePool;
21987 vctxt->xpathStatePool = sto->next;
21988 sto->next = NULL;
21989 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021990 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021991 * Create a new state object.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000021992 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000021993 sto = (xmlSchemaIDCStateObjPtr) xmlMalloc(sizeof(xmlSchemaIDCStateObj));
21994 if (sto == NULL) {
21995 xmlSchemaVErrMemory(NULL,
21996 "allocating an IDC state object", NULL);
21997 return (-1);
21998 }
21999 memset(sto, 0, sizeof(xmlSchemaIDCStateObj));
22000 }
22001 /*
22002 * Add to global list.
22003 */
22004 if (vctxt->xpathStates != NULL)
22005 sto->next = vctxt->xpathStates;
22006 vctxt->xpathStates = sto;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022007
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022008 /*
22009 * Free the old xpath validation context.
22010 */
22011 if (sto->xpathCtxt != NULL)
22012 xmlFreeStreamCtxt((xmlStreamCtxtPtr) sto->xpathCtxt);
22013
22014 /*
22015 * Create a new XPath (pattern) validation context.
22016 */
22017 sto->xpathCtxt = (void *) xmlPatternGetStreamCtxt(
22018 (xmlPatternPtr) sel->xpathComp);
22019 if (sto->xpathCtxt == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022020 VERROR_INT("xmlSchemaIDCAddStateObject",
22021 "failed to create an XPath validation context");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022022 return (-1);
22023 }
22024 sto->type = type;
22025 sto->depth = vctxt->depth;
22026 sto->matcher = matcher;
22027 sto->sel = sel;
22028 sto->nbHistory = 0;
22029
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022030#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022031 xmlGenericError(xmlGenericErrorContext, "IDC: STO push '%s'\n",
22032 sto->sel->xpath);
22033#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022034 return (0);
22035}
22036
22037/**
22038 * xmlSchemaXPathEvaluate:
22039 * @vctxt: the WXS validation context
22040 * @nodeType: the nodeType of the current node
22041 *
22042 * Evaluates all active XPath state objects.
22043 *
22044 * Returns the number of IC "field" state objects which resolved to
22045 * this node, 0 if none resolved and -1 on internal errors.
22046 */
22047static int
22048xmlSchemaXPathEvaluate(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022049 xmlElementType nodeType)
22050{
22051 xmlSchemaIDCStateObjPtr sto, head = NULL, first;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022052 int res, resolved = 0, depth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022053
22054 if (vctxt->xpathStates == NULL)
22055 return (0);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022056
22057 if (nodeType == XML_ATTRIBUTE_NODE)
22058 depth++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022059#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022060 {
22061 xmlChar *str = NULL;
22062 xmlGenericError(xmlGenericErrorContext,
22063 "IDC: EVAL on %s, depth %d, type %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022064 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22065 vctxt->inode->localName), depth, nodeType);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022066 FREE_AND_NULL(str)
22067 }
22068#endif
22069 /*
22070 * Process all active XPath state objects.
22071 */
22072 first = vctxt->xpathStates;
22073 sto = first;
22074 while (sto != head) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022075#ifdef DEBUG_IDC
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022076 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022077 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] selector '%s'\n",
22078 sto->matcher->aidc->def->name, sto->sel->xpath);
22079 else
22080 xmlGenericError(xmlGenericErrorContext, "IDC: ['%s'] field '%s'\n",
22081 sto->matcher->aidc->def->name, sto->sel->xpath);
22082#endif
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022083 if (nodeType == XML_ELEMENT_NODE)
22084 res = xmlStreamPush((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022085 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022086 else
22087 res = xmlStreamPushAttr((xmlStreamCtxtPtr) sto->xpathCtxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022088 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022089
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022090 if (res == -1) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022091 VERROR_INT("xmlSchemaXPathEvaluate",
22092 "calling xmlStreamPush()");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022093 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022094 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022095 if (res == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022096 goto next_sto;
22097 /*
22098 * Full match.
22099 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022100#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022101 xmlGenericError(xmlGenericErrorContext, "IDC: "
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022102 "MATCH\n");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022103#endif
22104 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022105 * Register a match in the state object history.
22106 */
22107 if (sto->history == NULL) {
22108 sto->history = (int *) xmlMalloc(5 * sizeof(int));
22109 if (sto->history == NULL) {
22110 xmlSchemaVErrMemory(NULL,
22111 "allocating the state object history", NULL);
22112 return(-1);
22113 }
22114 sto->sizeHistory = 10;
22115 } else if (sto->sizeHistory <= sto->nbHistory) {
22116 sto->sizeHistory *= 2;
22117 sto->history = (int *) xmlRealloc(sto->history,
22118 sto->sizeHistory * sizeof(int));
22119 if (sto->history == NULL) {
22120 xmlSchemaVErrMemory(NULL,
22121 "re-allocating the state object history", NULL);
22122 return(-1);
22123 }
22124 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022125 sto->history[sto->nbHistory++] = depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022126
22127#ifdef DEBUG_IDC
22128 xmlGenericError(xmlGenericErrorContext, "IDC: push match '%d'\n",
22129 vctxt->depth);
22130#endif
22131
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022132 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
22133 xmlSchemaIDCSelectPtr sel;
22134 /*
22135 * Activate state objects for the IDC fields of
22136 * the IDC selector.
22137 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022138#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022139 xmlGenericError(xmlGenericErrorContext, "IDC: "
22140 "activating field states\n");
22141#endif
22142 sel = sto->matcher->aidc->def->fields;
22143 while (sel != NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022144 if (xmlSchemaIDCAddStateObject(vctxt, sto->matcher,
22145 sel, XPATH_STATE_OBJ_TYPE_IDC_FIELD) == -1)
22146 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022147 sel = sel->next;
22148 }
22149 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
22150 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000022151 * An IDC key node was found by the IDC field.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022152 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022153#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022154 xmlGenericError(xmlGenericErrorContext,
22155 "IDC: key found\n");
22156#endif
22157 /*
22158 * Notify that the character value of this node is
22159 * needed.
22160 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022161 if (resolved == 0) {
22162 if ((vctxt->inode->flags &
22163 XML_SCHEMA_NODE_INFO_VALUE_NEEDED) == 0)
22164 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
22165 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022166 resolved++;
22167 }
22168next_sto:
22169 if (sto->next == NULL) {
22170 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022171 * Evaluate field state objects created on this node as well.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022172 */
22173 head = first;
22174 sto = vctxt->xpathStates;
22175 } else
22176 sto = sto->next;
22177 }
22178 return (resolved);
22179}
22180
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022181static const xmlChar *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022182xmlSchemaFormatIDCKeySequence(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022183 xmlChar **buf,
22184 xmlSchemaPSVIIDCKeyPtr *seq,
22185 int count)
22186{
22187 int i, res;
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022188 xmlChar *value = NULL;
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022189
22190 *buf = xmlStrdup(BAD_CAST "[");
22191 for (i = 0; i < count; i++) {
22192 *buf = xmlStrcat(*buf, BAD_CAST "'");
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022193 res = xmlSchemaGetCanonValueWhtspExt(seq[i]->val,
22194 xmlSchemaGetWhiteSpaceFacetValue(seq[i]->type),
22195 &value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022196 if (res == 0)
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022197 *buf = xmlStrcat(*buf, BAD_CAST value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022198 else {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022199 VERROR_INT("xmlSchemaFormatIDCKeySequence",
22200 "failed to compute a canonical value");
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022201 *buf = xmlStrcat(*buf, BAD_CAST "???");
22202 }
22203 if (i < count -1)
22204 *buf = xmlStrcat(*buf, BAD_CAST "', ");
22205 else
22206 *buf = xmlStrcat(*buf, BAD_CAST "'");
22207 if (value != NULL) {
Kasimier T. Buchcik77e76822005-11-11 12:52:32 +000022208 xmlFree(value);
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022209 value = NULL;
22210 }
22211 }
22212 *buf = xmlStrcat(*buf, BAD_CAST "]");
22213
22214 return (BAD_CAST *buf);
22215}
22216
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022217/**
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000022218 * xmlSchemaXPathPop:
22219 * @vctxt: the WXS validation context
22220 *
22221 * Pops all XPath states.
22222 *
22223 * Returns 0 on success and -1 on internal errors.
22224 */
22225static int
22226xmlSchemaXPathPop(xmlSchemaValidCtxtPtr vctxt)
22227{
22228 xmlSchemaIDCStateObjPtr sto;
22229 int res;
22230
22231 if (vctxt->xpathStates == NULL)
22232 return(0);
22233 sto = vctxt->xpathStates;
22234 do {
22235 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22236 if (res == -1)
22237 return (-1);
22238 sto = sto->next;
22239 } while (sto != NULL);
22240 return(0);
22241}
22242
22243/**
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022244 * xmlSchemaXPathProcessHistory:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022245 * @vctxt: the WXS validation context
22246 * @type: the simple/complex type of the current node if any at all
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022247 * @val: the precompiled value
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022248 *
22249 * Processes and pops the history items of the IDC state objects.
22250 * IDC key-sequences are validated/created on IDC bindings.
22251 *
22252 * Returns 0 on success and -1 on internal errors.
22253 */
22254static int
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022255xmlSchemaXPathProcessHistory(xmlSchemaValidCtxtPtr vctxt,
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022256 int depth)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022257{
22258 xmlSchemaIDCStateObjPtr sto, nextsto;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022259 int res, matchDepth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022260 xmlSchemaPSVIIDCKeyPtr key = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022261 xmlSchemaTypePtr type = vctxt->inode->typeDef, simpleType = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022262
22263 if (vctxt->xpathStates == NULL)
22264 return (0);
22265 sto = vctxt->xpathStates;
22266
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022267#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022268 {
22269 xmlChar *str = NULL;
22270 xmlGenericError(xmlGenericErrorContext,
22271 "IDC: BACK on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022272 xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22273 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022274 FREE_AND_NULL(str)
22275 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022276#endif
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022277 /*
22278 * Evaluate the state objects.
22279 */
22280 while (sto != NULL) {
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000022281 res = xmlStreamPop((xmlStreamCtxtPtr) sto->xpathCtxt);
22282 if (res == -1) {
22283 VERROR_INT("xmlSchemaXPathProcessHistory",
22284 "calling xmlStreamPop()");
22285 return (-1);
22286 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022287#ifdef DEBUG_IDC
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000022288 xmlGenericError(xmlGenericErrorContext, "IDC: stream pop '%s'\n",
22289 sto->sel->xpath);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022290#endif
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022291 if (sto->nbHistory == 0)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022292 goto deregister_check;
22293
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022294 matchDepth = sto->history[sto->nbHistory -1];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022295
22296 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022297 * Only matches at the current depth are of interest.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022298 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022299 if (matchDepth != depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022300 sto = sto->next;
22301 continue;
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022302 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022303 if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_FIELD) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022304 /*
22305 * NOTE: According to
22306 * http://www.w3.org/Bugs/Public/show_bug.cgi?id=2198
22307 * ... the simple-content of complex types is also allowed.
22308 */
22309
22310 if (WXS_IS_COMPLEX(type)) {
22311 if (WXS_HAS_SIMPLE_CONTENT(type)) {
22312 /*
22313 * Sanity check for complex types with simple content.
22314 */
22315 simpleType = type->contentTypeDef;
22316 if (simpleType == NULL) {
22317 VERROR_INT("xmlSchemaXPathProcessHistory",
22318 "field resolves to a CT with simple content "
22319 "but the CT is missing the ST definition");
22320 return (-1);
22321 }
22322 } else
22323 simpleType = NULL;
22324 } else
22325 simpleType = type;
22326 if (simpleType == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022327 xmlChar *str = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022328
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022329 /*
22330 * Not qualified if the field resolves to a node of non
22331 * simple type.
22332 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022333 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022334 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022335 WXS_BASIC_CAST sto->matcher->aidc->def,
22336 "The XPath '%s' of a field of %s does evaluate to a node of "
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022337 "non-simple type",
22338 sto->sel->xpath,
22339 xmlSchemaGetIDCDesignation(&str, sto->matcher->aidc->def));
22340 FREE_AND_NULL(str);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022341 sto->nbHistory--;
22342 goto deregister_check;
22343 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022344
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022345 if ((key == NULL) && (vctxt->inode->val == NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022346 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022347 * Failed to provide the normalized value; maybe
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022348 * the value was invalid.
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000022349 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022350 VERROR(XML_SCHEMAV_CVC_IDC,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022351 WXS_BASIC_CAST sto->matcher->aidc->def,
Kasimier T. Buchcik6e224f12005-02-17 11:10:44 +000022352 "Warning: No precomputed value available, the value "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022353 "was either invalid or something strange happend");
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022354 sto->nbHistory--;
22355 goto deregister_check;
22356 } else {
22357 xmlSchemaIDCMatcherPtr matcher = sto->matcher;
22358 xmlSchemaPSVIIDCKeyPtr *keySeq;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022359 int pos, idx;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022360
22361 /*
22362 * The key will be anchored on the matcher's list of
22363 * key-sequences. The position in this list is determined
22364 * by the target node's depth relative to the matcher's
22365 * depth of creation (i.e. the depth of the scope element).
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022366 *
22367 * Element Depth Pos List-entries
22368 * <scope> 0 NULL
22369 * <bar> 1 NULL
22370 * <target/> 2 2 target
22371 * <bar>
22372 * </scope>
22373 *
22374 * The size of the list is only dependant on the depth of
22375 * the tree.
22376 * An entry will be NULLed in selector_leave, i.e. when
22377 * we hit the target's
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022378 */
22379 pos = sto->depth - matcher->depth;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022380 idx = sto->sel->index;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022381
22382 /*
22383 * Create/grow the array of key-sequences.
22384 */
22385 if (matcher->keySeqs == NULL) {
22386 if (pos > 9)
22387 matcher->sizeKeySeqs = pos * 2;
22388 else
22389 matcher->sizeKeySeqs = 10;
22390 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22391 xmlMalloc(matcher->sizeKeySeqs *
22392 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22393 if (matcher->keySeqs == NULL) {
22394 xmlSchemaVErrMemory(NULL,
22395 "allocating an array of key-sequences",
22396 NULL);
22397 return(-1);
22398 }
22399 memset(matcher->keySeqs, 0,
22400 matcher->sizeKeySeqs *
22401 sizeof(xmlSchemaPSVIIDCKeyPtr *));
22402 } else if (pos >= matcher->sizeKeySeqs) {
22403 int i = matcher->sizeKeySeqs;
22404
22405 matcher->sizeKeySeqs *= 2;
22406 matcher->keySeqs = (xmlSchemaPSVIIDCKeyPtr **)
22407 xmlRealloc(matcher->keySeqs,
22408 matcher->sizeKeySeqs *
22409 sizeof(xmlSchemaPSVIIDCKeyPtr *));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022410 if (matcher->keySeqs == NULL) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022411 xmlSchemaVErrMemory(NULL,
22412 "reallocating an array of key-sequences",
22413 NULL);
22414 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022415 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022416 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022417 * The array needs to be NULLed.
22418 * TODO: Use memset?
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022419 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022420 for (; i < matcher->sizeKeySeqs; i++)
22421 matcher->keySeqs[i] = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022422 }
22423
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022424 /*
22425 * Get/create the key-sequence.
22426 */
22427 keySeq = matcher->keySeqs[pos];
22428 if (keySeq == NULL) {
22429 goto create_sequence;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022430 } else if (keySeq[idx] != NULL) {
22431 xmlChar *str = NULL;
22432 /*
22433 * cvc-identity-constraint:
22434 * 3 For each node in the ·target node set· all
22435 * of the {fields}, with that node as the context
22436 * node, evaluate to either an empty node-set or
22437 * a node-set with exactly one member, which must
22438 * have a simple type.
22439 *
22440 * The key was already set; report an error.
22441 */
22442 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22443 XML_SCHEMAV_CVC_IDC, NULL,
22444 WXS_BASIC_CAST matcher->aidc->def,
22445 "The XPath '%s' of a field of %s evaluates to a "
22446 "node-set with more than one member",
22447 sto->sel->xpath,
22448 xmlSchemaGetIDCDesignation(&str, matcher->aidc->def));
22449 FREE_AND_NULL(str);
22450 sto->nbHistory--;
22451 goto deregister_check;
22452 } else
22453 goto create_key;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022454
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022455create_sequence:
22456 /*
22457 * Create a key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022458 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022459 keySeq = (xmlSchemaPSVIIDCKeyPtr *) xmlMalloc(
22460 matcher->aidc->def->nbFields *
22461 sizeof(xmlSchemaPSVIIDCKeyPtr));
22462 if (keySeq == NULL) {
22463 xmlSchemaVErrMemory(NULL,
22464 "allocating an IDC key-sequence", NULL);
22465 return(-1);
22466 }
22467 memset(keySeq, 0, matcher->aidc->def->nbFields *
22468 sizeof(xmlSchemaPSVIIDCKeyPtr));
22469 matcher->keySeqs[pos] = keySeq;
22470create_key:
22471 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022472 * Create a key once per node only.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022473 */
22474 if (key == NULL) {
22475 key = (xmlSchemaPSVIIDCKeyPtr) xmlMalloc(
22476 sizeof(xmlSchemaPSVIIDCKey));
22477 if (key == NULL) {
22478 xmlSchemaVErrMemory(NULL,
22479 "allocating a IDC key", NULL);
22480 xmlFree(keySeq);
22481 matcher->keySeqs[pos] = NULL;
22482 return(-1);
22483 }
22484 /*
22485 * Consume the compiled value.
22486 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022487 key->type = simpleType;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022488 key->val = vctxt->inode->val;
22489 vctxt->inode->val = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022490 /*
22491 * Store the key in a global list.
22492 */
22493 if (xmlSchemaIDCStoreKey(vctxt, key) == -1) {
22494 xmlSchemaIDCFreeKey(key);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022495 return (-1);
22496 }
22497 }
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022498 keySeq[idx] = key;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022499 }
22500 } else if (sto->type == XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022501
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022502 xmlSchemaPSVIIDCKeyPtr **keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022503 /* xmlSchemaPSVIIDCBindingPtr bind; */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022504 xmlSchemaPSVIIDCNodePtr ntItem;
22505 xmlSchemaIDCMatcherPtr matcher;
22506 xmlSchemaIDCPtr idc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022507 xmlSchemaItemListPtr targets;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022508 int pos, i, j, nbKeys;
22509 /*
22510 * Here we have the following scenario:
22511 * An IDC 'selector' state object resolved to a target node,
22512 * during the time this target node was in the
22513 * ancestor-or-self axis, the 'field' state object(s) looked
22514 * out for matching nodes to create a key-sequence for this
22515 * target node. Now we are back to this target node and need
22516 * to put the key-sequence, together with the target node
22517 * itself, into the node-table of the corresponding IDC
22518 * binding.
22519 */
22520 matcher = sto->matcher;
22521 idc = matcher->aidc->def;
22522 nbKeys = idc->nbFields;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022523 pos = depth - matcher->depth;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022524 /*
22525 * Check if the matcher has any key-sequences at all, plus
22526 * if it has a key-sequence for the current target node.
22527 */
22528 if ((matcher->keySeqs == NULL) ||
22529 (matcher->sizeKeySeqs <= pos)) {
22530 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22531 goto selector_key_error;
22532 else
22533 goto selector_leave;
22534 }
22535
22536 keySeq = &(matcher->keySeqs[pos]);
22537 if (*keySeq == NULL) {
22538 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY)
22539 goto selector_key_error;
22540 else
22541 goto selector_leave;
22542 }
22543
22544 for (i = 0; i < nbKeys; i++) {
22545 if ((*keySeq)[i] == NULL) {
22546 /*
22547 * Not qualified, if not all fields did resolve.
22548 */
22549 if (idc->type == XML_SCHEMA_TYPE_IDC_KEY) {
22550 /*
22551 * All fields of a "key" IDC must resolve.
22552 */
22553 goto selector_key_error;
22554 }
22555 goto selector_leave;
22556 }
22557 }
22558 /*
22559 * All fields did resolve.
22560 */
22561
22562 /*
22563 * 4.1 If the {identity-constraint category} is unique(/key),
22564 * then no two members of the ·qualified node set· have
22565 * ·key-sequences· whose members are pairwise equal, as
22566 * defined by Equal in [XML Schemas: Datatypes].
22567 *
22568 * Get the IDC binding from the matcher and check for
22569 * duplicate key-sequences.
22570 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022571#if 0
22572 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22573#endif
22574 targets = xmlSchemaIDCAcquireTargetList(vctxt, matcher);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022575 if ((idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) &&
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022576 (targets->nbItems != 0)) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022577 xmlSchemaPSVIIDCKeyPtr ckey, bkey, *bkeySeq;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022578
22579 i = 0;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022580 res = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022581 /*
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022582 * Compare the key-sequences, key by key.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022583 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022584 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022585 bkeySeq =
22586 ((xmlSchemaPSVIIDCNodePtr) targets->items[i])->keys;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022587 for (j = 0; j < nbKeys; j++) {
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022588 ckey = (*keySeq)[j];
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022589 bkey = bkeySeq[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022590 res = xmlSchemaAreValuesEqual(ckey->val, bkey->val);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022591 if (res == -1) {
22592 return (-1);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022593 } else if (res == 0) {
22594 /*
22595 * One of the keys differs, so the key-sequence
22596 * won't be equal; get out.
22597 */
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022598 break;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022599 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022600 }
22601 if (res == 1) {
22602 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022603 * Duplicate key-sequence found.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022604 */
22605 break;
22606 }
22607 i++;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022608 } while (i < targets->nbItems);
22609 if (i != targets->nbItems) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022610 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022611 /*
22612 * TODO: Try to report the key-sequence.
22613 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022614 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022615 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022616 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022617 "Duplicate key-sequence %s in %s",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000022618 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022619 (*keySeq), nbKeys),
22620 xmlSchemaGetIDCDesignation(&strB, idc));
22621 FREE_AND_NULL(str);
22622 FREE_AND_NULL(strB);
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022623 goto selector_leave;
22624 }
22625 }
22626 /*
22627 * Add a node-table item to the IDC binding.
22628 */
22629 ntItem = (xmlSchemaPSVIIDCNodePtr) xmlMalloc(
22630 sizeof(xmlSchemaPSVIIDCNode));
22631 if (ntItem == NULL) {
22632 xmlSchemaVErrMemory(NULL,
22633 "allocating an IDC node-table item", NULL);
22634 xmlFree(*keySeq);
22635 *keySeq = NULL;
22636 return(-1);
22637 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022638 memset(ntItem, 0, sizeof(xmlSchemaPSVIIDCNode));
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022639
22640 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022641 * Store the node-table item in a global list.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022642 */
22643 if (idc->type != XML_SCHEMA_TYPE_IDC_KEYREF) {
22644 if (xmlSchemaIDCStoreNodeTableItem(vctxt, ntItem) == -1) {
22645 xmlFree(ntItem);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022646 xmlFree(*keySeq);
22647 *keySeq = NULL;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022648 return (-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022649 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022650 ntItem->nodeQNameID = -1;
22651 } else {
22652 /*
22653 * Save a cached QName for this node on the IDC node, to be
22654 * able to report it, even if the node is not saved.
22655 */
22656 ntItem->nodeQNameID = xmlSchemaVAddNodeQName(vctxt,
22657 vctxt->inode->localName, vctxt->inode->nsName);
22658 if (ntItem->nodeQNameID == -1) {
22659 xmlFree(ntItem);
22660 xmlFree(*keySeq);
22661 *keySeq = NULL;
22662 return (-1);
22663 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022664 }
22665 /*
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022666 * Init the node-table item: Save the node, position and
22667 * consume the key-sequence.
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022668 */
22669 ntItem->node = vctxt->node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000022670 ntItem->nodeLine = vctxt->inode->nodeLine;
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022671 ntItem->keys = *keySeq;
22672 *keySeq = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022673#if 0
22674 if (xmlSchemaIDCAppendNodeTableItem(bind, ntItem) == -1) {
22675#endif
22676 if (xmlSchemaItemListAdd(targets, ntItem) == -1) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022677 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22678 /*
22679 * Free the item, since keyref items won't be
22680 * put on a global list.
22681 */
22682 xmlFree(ntItem->keys);
22683 xmlFree(ntItem);
22684 }
22685 return (-1);
22686 }
22687
22688 goto selector_leave;
22689selector_key_error:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022690 {
22691 xmlChar *str = NULL;
22692 /*
22693 * 4.2.1 (KEY) The ·target node set· and the
22694 * ·qualified node set· are equal, that is, every
22695 * member of the ·target node set· is also a member
22696 * of the ·qualified node set· and vice versa.
22697 */
22698 xmlSchemaCustomErr(ACTXT_CAST vctxt,
22699 XML_SCHEMAV_CVC_IDC, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000022700 WXS_BASIC_CAST idc,
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000022701 "Not all fields of %s evaluate to a node",
22702 xmlSchemaGetIDCDesignation(&str, idc), NULL);
22703 FREE_AND_NULL(str);
22704 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022705selector_leave:
22706 /*
22707 * Free the key-sequence if not added to the IDC table.
22708 */
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022709 if ((keySeq != NULL) && (*keySeq != NULL)) {
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022710 xmlFree(*keySeq);
22711 *keySeq = NULL;
22712 }
22713 } /* if selector */
22714
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022715 sto->nbHistory--;
22716
22717deregister_check:
22718 /*
22719 * Deregister state objects if they reach the depth of creation.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022720 */
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000022721 if ((sto->nbHistory == 0) && (sto->depth == depth)) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022722#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022723 xmlGenericError(xmlGenericErrorContext, "IDC: STO pop '%s'\n",
22724 sto->sel->xpath);
22725#endif
22726 if (vctxt->xpathStates != sto) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022727 VERROR_INT("xmlSchemaXPathProcessHistory",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022728 "The state object to be removed is not the first "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022729 "in the list");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022730 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022731 nextsto = sto->next;
22732 /*
22733 * Unlink from the list of active XPath state objects.
22734 */
22735 vctxt->xpathStates = sto->next;
22736 sto->next = vctxt->xpathStatePool;
22737 /*
22738 * Link it to the pool of reusable state objects.
22739 */
22740 vctxt->xpathStatePool = sto;
22741 sto = nextsto;
22742 } else
22743 sto = sto->next;
22744 } /* while (sto != NULL) */
22745 return (0);
22746}
22747
22748/**
22749 * xmlSchemaIDCRegisterMatchers:
22750 * @vctxt: the WXS validation context
22751 * @elemDecl: the element declaration
22752 *
22753 * Creates helper objects to evaluate IDC selectors/fields
22754 * successively.
22755 *
22756 * Returns 0 if OK and -1 on internal errors.
22757 */
22758static int
22759xmlSchemaIDCRegisterMatchers(xmlSchemaValidCtxtPtr vctxt,
22760 xmlSchemaElementPtr elemDecl)
22761{
22762 xmlSchemaIDCMatcherPtr matcher, last = NULL;
22763 xmlSchemaIDCPtr idc, refIdc;
22764 xmlSchemaIDCAugPtr aidc;
22765
22766 idc = (xmlSchemaIDCPtr) elemDecl->idcs;
22767 if (idc == NULL)
22768 return (0);
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000022769
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022770#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022771 {
22772 xmlChar *str = NULL;
22773 xmlGenericError(xmlGenericErrorContext,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022774 "IDC: REGISTER on %s, depth %d\n",
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022775 (char *) xmlSchemaFormatQName(&str, vctxt->inode->nsName,
22776 vctxt->inode->localName), vctxt->depth);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022777 FREE_AND_NULL(str)
22778 }
22779#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022780 if (vctxt->inode->idcMatchers != NULL) {
22781 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22782 "The chain of IDC matchers is expected to be empty");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022783 return (-1);
22784 }
22785 do {
22786 if (idc->type == XML_SCHEMA_TYPE_IDC_KEYREF) {
22787 /*
22788 * Since IDCs bubbles are expensive we need to know the
22789 * depth at which the bubbles should stop; this will be
22790 * the depth of the top-most keyref IDC. If no keyref
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022791 * references a key/unique IDC, the keyrefDepth will
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022792 * be -1, indicating that no bubbles are needed.
22793 */
22794 refIdc = (xmlSchemaIDCPtr) idc->ref->item;
22795 if (refIdc != NULL) {
22796 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022797 * Remember that we have keyrefs on this node.
22798 */
22799 vctxt->inode->hasKeyrefs = 1;
22800 /*
22801 * Lookup the referenced augmented IDC info.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022802 */
22803 aidc = vctxt->aidcs;
22804 while (aidc != NULL) {
22805 if (aidc->def == refIdc)
22806 break;
22807 aidc = aidc->next;
22808 }
22809 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022810 VERROR_INT("xmlSchemaIDCRegisterMatchers",
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022811 "Could not find an augmented IDC item for an IDC "
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022812 "definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022813 return (-1);
22814 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022815 if ((aidc->keyrefDepth == -1) ||
22816 (vctxt->depth < aidc->keyrefDepth))
22817 aidc->keyrefDepth = vctxt->depth;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022818 }
22819 }
22820 /*
22821 * Lookup the augmented IDC item for the IDC definition.
22822 */
22823 aidc = vctxt->aidcs;
22824 while (aidc != NULL) {
22825 if (aidc->def == idc)
22826 break;
22827 aidc = aidc->next;
22828 }
22829 if (aidc == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022830 VERROR_INT("xmlSchemaIDCRegisterMatchers",
22831 "Could not find an augmented IDC item for an IDC definition");
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022832 return (-1);
22833 }
22834 /*
22835 * Create an IDC matcher for every IDC definition.
22836 */
22837 matcher = (xmlSchemaIDCMatcherPtr)
22838 xmlMalloc(sizeof(xmlSchemaIDCMatcher));
22839 if (matcher == NULL) {
22840 xmlSchemaVErrMemory(vctxt,
22841 "allocating an IDC matcher", NULL);
22842 return (-1);
22843 }
22844 memset(matcher, 0, sizeof(xmlSchemaIDCMatcher));
22845 if (last == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000022846 vctxt->inode->idcMatchers = matcher;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022847 else
22848 last->next = matcher;
22849 last = matcher;
22850
22851 matcher->type = IDC_MATCHER;
22852 matcher->depth = vctxt->depth;
22853 matcher->aidc = aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022854 matcher->idcType = aidc->def->type;
22855#ifdef DEBUG_IDC
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022856 xmlGenericError(xmlGenericErrorContext, "IDC: register matcher\n");
22857#endif
22858 /*
22859 * Init the automaton state object.
22860 */
22861 if (xmlSchemaIDCAddStateObject(vctxt, matcher,
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000022862 idc->selector, XPATH_STATE_OBJ_TYPE_IDC_SELECTOR) == -1)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000022863 return (-1);
22864
22865 idc = idc->next;
22866 } while (idc != NULL);
22867 return (0);
22868}
22869
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000022870static int
22871xmlSchemaIDCFillNodeTables(xmlSchemaValidCtxtPtr vctxt,
22872 xmlSchemaNodeInfoPtr ielem)
22873{
22874 xmlSchemaPSVIIDCBindingPtr bind;
22875 int res, i, j, k, nbTargets, nbFields, nbDupls, nbNodeTable;
22876 xmlSchemaPSVIIDCKeyPtr *keys, *ntkeys;
22877 xmlSchemaPSVIIDCNodePtr *targets, *dupls;
22878
22879 xmlSchemaIDCMatcherPtr matcher = ielem->idcMatchers;
22880 /* vctxt->createIDCNodeTables */
22881 while (matcher != NULL) {
22882 /*
22883 * Skip keyref IDCs and empty IDC target-lists.
22884 */
22885 if ((matcher->aidc->def->type == XML_SCHEMA_TYPE_IDC_KEYREF) ||
22886 WXS_ILIST_IS_EMPTY(matcher->targets))
22887 {
22888 matcher = matcher->next;
22889 continue;
22890 }
22891 /*
22892 * If we _want_ the IDC node-table to be created in any case
22893 * then do so. Otherwise create them only if keyrefs need them.
22894 */
22895 if ((! vctxt->createIDCNodeTables) &&
22896 ((matcher->aidc->keyrefDepth == -1) ||
22897 (matcher->aidc->keyrefDepth > vctxt->depth)))
22898 {
22899 matcher = matcher->next;
22900 continue;
22901 }
22902 /*
22903 * Get/create the IDC binding on this element for the IDC definition.
22904 */
22905 bind = xmlSchemaIDCAcquireBinding(vctxt, matcher);
22906
22907 if (! WXS_ILIST_IS_EMPTY(bind->dupls)) {
22908 dupls = (xmlSchemaPSVIIDCNodePtr *) bind->dupls->items;
22909 nbDupls = bind->dupls->nbItems;
22910 } else {
22911 dupls = NULL;
22912 nbDupls = 0;
22913 }
22914 if (bind->nodeTable != NULL) {
22915 nbNodeTable = bind->nbNodes;
22916 } else {
22917 nbNodeTable = 0;
22918 }
22919
22920 if ((nbNodeTable == 0) && (nbDupls == 0)) {
22921 /*
22922 * Transfer all IDC target-nodes to the IDC node-table.
22923 */
22924 bind->nodeTable =
22925 (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
22926 bind->sizeNodes = matcher->targets->sizeItems;
22927 bind->nbNodes = matcher->targets->nbItems;
22928
22929 matcher->targets->items = NULL;
22930 matcher->targets->sizeItems = 0;
22931 matcher->targets->nbItems = 0;
22932 } else {
22933 /*
22934 * Compare the key-sequences and add to the IDC node-table.
22935 */
22936 nbTargets = matcher->targets->nbItems;
22937 targets = (xmlSchemaPSVIIDCNodePtr *) matcher->targets->items;
22938 nbFields = matcher->aidc->def->nbFields;
22939 i = 0;
22940 do {
22941 keys = targets[i]->keys;
22942 if (nbDupls) {
22943 /*
22944 * Search in already found duplicates first.
22945 */
22946 j = 0;
22947 do {
22948 if (nbFields == 1) {
22949 res = xmlSchemaAreValuesEqual(keys[0]->val,
22950 dupls[j]->keys[0]->val);
22951 if (res == -1)
22952 goto internal_error;
22953 if (res == 1) {
22954 /*
22955 * Equal key-sequence.
22956 */
22957 goto next_target;
22958 }
22959 } else {
22960 res = 0;
22961 ntkeys = dupls[j]->keys;
22962 for (k = 0; k < nbFields; k++) {
22963 res = xmlSchemaAreValuesEqual(keys[k]->val,
22964 ntkeys[k]->val);
22965 if (res == -1)
22966 goto internal_error;
22967 if (res == 0) {
22968 /*
22969 * One of the keys differs.
22970 */
22971 break;
22972 }
22973 }
22974 if (res == 1) {
22975 /*
22976 * Equal key-sequence found.
22977 */
22978 goto next_target;
22979 }
22980 }
22981 j++;
22982 } while (j < nbDupls);
22983 }
22984 if (nbNodeTable) {
22985 j = 0;
22986 do {
22987 if (nbFields == 1) {
22988 res = xmlSchemaAreValuesEqual(keys[0]->val,
22989 bind->nodeTable[j]->keys[0]->val);
22990 if (res == -1)
22991 goto internal_error;
22992 if (res == 0) {
22993 /*
22994 * The key-sequence differs.
22995 */
22996 goto next_node_table_entry;
22997 }
22998 } else {
22999 res = 0;
23000 ntkeys = bind->nodeTable[j]->keys;
23001 for (k = 0; k < nbFields; k++) {
23002 res = xmlSchemaAreValuesEqual(keys[k]->val,
23003 ntkeys[k]->val);
23004 if (res == -1)
23005 goto internal_error;
23006 if (res == 0) {
23007 /*
23008 * One of the keys differs.
23009 */
23010 goto next_node_table_entry;
23011 }
23012 }
23013 }
23014 /*
23015 * Add the duplicate to the list of duplicates.
23016 */
23017 if (bind->dupls == NULL) {
23018 bind->dupls = xmlSchemaItemListCreate();
23019 if (bind->dupls == NULL)
23020 goto internal_error;
23021 }
23022 if (xmlSchemaItemListAdd(bind->dupls, bind->nodeTable[j]) == -1)
23023 goto internal_error;
23024 /*
23025 * Remove the duplicate entry from the IDC node-table.
23026 */
23027 bind->nodeTable[j] = bind->nodeTable[bind->nbNodes -1];
23028 bind->nbNodes--;
23029
23030 goto next_target;
23031
23032next_node_table_entry:
23033 j++;
23034 } while (j < nbNodeTable);
23035 }
23036 /*
23037 * If everything is fine, then add the IDC target-node to
23038 * the IDC node-table.
23039 */
23040 if (xmlSchemaIDCAppendNodeTableItem(bind, targets[i]) == -1)
23041 goto internal_error;
23042
23043next_target:
23044 i++;
23045 } while (i < nbTargets);
23046 }
23047 matcher = matcher->next;
23048 }
23049 return(0);
23050
23051internal_error:
23052 return(-1);
23053}
23054
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023055/**
23056 * xmlSchemaBubbleIDCNodeTables:
23057 * @depth: the current tree depth
23058 *
23059 * Merges IDC bindings of an element at @depth into the corresponding IDC
23060 * bindings of its parent element. If a duplicate note-table entry is found,
23061 * both, the parent node-table entry and child entry are discarded from the
23062 * node-table of the parent.
23063 *
23064 * Returns 0 if OK and -1 on internal errors.
23065 */
23066static int
23067xmlSchemaBubbleIDCNodeTables(xmlSchemaValidCtxtPtr vctxt)
23068{
23069 xmlSchemaPSVIIDCBindingPtr bind; /* IDC bindings of the current node. */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023070 xmlSchemaPSVIIDCBindingPtr *parTable, parBind = NULL; /* parent IDC bindings. */
23071 xmlSchemaPSVIIDCNodePtr node, parNode = NULL, *dupls, *parNodes; /* node-table entries. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023072 xmlSchemaIDCAugPtr aidc;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023073 int i, j, k, ret = 0, nbFields, oldNum, oldDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023074
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023075 bind = vctxt->inode->idcTable;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023076 if (bind == NULL) {
23077 /* Fine, no table, no bubbles. */
23078 return (0);
23079 }
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000023080
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023081 parTable = &(vctxt->elemInfos[vctxt->depth -1]->idcTable);
23082 /*
23083 * Walk all bindings; create new or add to existing bindings.
23084 * Remove duplicate key-sequences.
23085 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023086 while (bind != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023087
23088 if ((bind->nbNodes == 0) && WXS_ILIST_IS_EMPTY(bind->dupls))
23089 goto next_binding;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023090 /*
23091 * Check if the key/unique IDC table needs to be bubbled.
23092 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023093 if (! vctxt->createIDCNodeTables) {
23094 aidc = vctxt->aidcs;
23095 do {
23096 if (aidc->def == bind->definition) {
23097 if ((aidc->keyrefDepth == -1) ||
23098 (aidc->keyrefDepth >= vctxt->depth)) {
23099 goto next_binding;
23100 }
23101 break;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023102 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023103 aidc = aidc->next;
23104 } while (aidc != NULL);
23105 }
Kasimier T. Buchcik0a598522005-01-31 17:02:07 +000023106
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023107 if (parTable != NULL)
23108 parBind = *parTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023109 /*
23110 * Search a matching parent binding for the
23111 * IDC definition.
23112 */
23113 while (parBind != NULL) {
23114 if (parBind->definition == bind->definition)
23115 break;
23116 parBind = parBind->next;
23117 }
23118
23119 if (parBind != NULL) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023120 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023121 * Compare every node-table entry of the child node,
23122 * i.e. the key-sequence within, ...
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023123 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023124 oldNum = parBind->nbNodes; /* Skip newly added items. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023125
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023126 if (! WXS_ILIST_IS_EMPTY(parBind->dupls)) {
23127 oldDupls = parBind->dupls->nbItems;
23128 dupls = (xmlSchemaPSVIIDCNodePtr *) parBind->dupls->items;
23129 } else {
23130 dupls = NULL;
23131 oldDupls = 0;
23132 }
23133
23134 parNodes = parBind->nodeTable;
23135 nbFields = bind->definition->nbFields;
23136
23137 for (i = 0; i < bind->nbNodes; i++) {
23138 node = bind->nodeTable[i];
23139 if (node == NULL)
23140 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023141 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023142 * ...with every key-sequence of the parent node, already
23143 * evaluated to be a duplicate key-sequence.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023144 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023145 if (oldDupls) {
23146 j = 0;
23147 while (j < oldDupls) {
23148 if (nbFields == 1) {
23149 ret = xmlSchemaAreValuesEqual(
23150 node->keys[0]->val,
23151 dupls[j]->keys[0]->val);
23152 if (ret == -1)
23153 goto internal_error;
23154 if (ret == 0) {
23155 j++;
23156 continue;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023157 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023158 } else {
23159 parNode = dupls[j];
23160 for (k = 0; k < nbFields; k++) {
23161 ret = xmlSchemaAreValuesEqual(
23162 node->keys[k]->val,
23163 parNode->keys[k]->val);
23164 if (ret == -1)
23165 goto internal_error;
23166 if (ret == 0)
23167 break;
23168 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023169 }
23170 if (ret == 1)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023171 /* Duplicate found. */
23172 break;
23173 j++;
23174 }
23175 if (j != oldDupls) {
23176 /* Duplicate found. Skip this entry. */
23177 continue;
23178 }
23179 }
23180 /*
23181 * ... and with every key-sequence of the parent node.
23182 */
23183 if (oldNum) {
23184 j = 0;
23185 while (j < oldNum) {
23186 if (nbFields == 1) {
23187 ret = xmlSchemaAreValuesEqual(
23188 node->keys[0]->val,
23189 parNodes[j]->keys[0]->val);
23190 if (ret == -1)
23191 goto internal_error;
23192 if (ret == 0) {
23193 j++;
23194 continue;
23195 }
23196 } else {
23197 parNode = parNodes[j];
23198 for (k = 0; k < nbFields; k++) {
23199 ret = xmlSchemaAreValuesEqual(
23200 node->keys[k]->val,
23201 parNode->keys[k]->val);
23202 if (ret == -1)
23203 goto internal_error;
23204 if (ret == 0)
23205 break;
23206 }
23207 }
23208 if (ret == 1)
23209 /* Duplicate found. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023210 break;
23211 j++;
23212 }
23213 if (j != oldNum) {
23214 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023215 * Handle duplicates. Move the duplicate in
23216 * the parent's node-table to the list of
23217 * duplicates.
23218 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023219 oldNum--;
23220 parBind->nbNodes--;
23221 /*
23222 * Move last old item to pos of duplicate.
23223 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023224 parNodes[j] = parNodes[oldNum];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023225
23226 if (parBind->nbNodes != oldNum) {
23227 /*
23228 * If new items exist, move last new item to
23229 * last of old items.
23230 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023231 parNodes[oldNum] =
23232 parNodes[parBind->nbNodes];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023233 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023234 if (parBind->dupls == NULL) {
23235 parBind->dupls = xmlSchemaItemListCreate();
23236 if (parBind->dupls == NULL)
23237 goto internal_error;
23238 }
23239 xmlSchemaItemListAdd(parBind->dupls, parNode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023240 } else {
23241 /*
23242 * Add the node-table entry (node and key-sequence) of
23243 * the child node to the node table of the parent node.
23244 */
23245 if (parBind->nodeTable == NULL) {
23246 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023247 xmlMalloc(10 * sizeof(xmlSchemaPSVIIDCNodePtr));
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023248 if (parBind->nodeTable == NULL) {
23249 xmlSchemaVErrMemory(NULL,
23250 "allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023251 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023252 }
23253 parBind->sizeNodes = 1;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023254 } else if (parBind->nbNodes >= parBind->sizeNodes) {
Kasimier T. Buchcikb34ad1b2005-03-21 20:10:31 +000023255 parBind->sizeNodes *= 2;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023256 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23257 xmlRealloc(parBind->nodeTable, parBind->sizeNodes *
23258 sizeof(xmlSchemaPSVIIDCNodePtr));
23259 if (parBind->nodeTable == NULL) {
23260 xmlSchemaVErrMemory(NULL,
23261 "re-allocating IDC list of node-table items", NULL);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023262 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023263 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023264 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023265 /*
23266 * Append the new node-table entry to the 'new node-table
23267 * entries' section.
23268 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023269 parBind->nodeTable[parBind->nbNodes++] = node;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023270 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023271
23272 }
23273
23274 }
23275 } else {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023276 /*
23277 * No binding for the IDC was found: create a new one and
23278 * copy all node-tables.
23279 */
23280 parBind = xmlSchemaIDCNewBinding(bind->definition);
23281 if (parBind == NULL)
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023282 goto internal_error;
23283
23284 /*
23285 * TODO: Hmm, how to optimize the initial number of
23286 * allocated entries?
23287 */
23288 if (bind->nbNodes != 0) {
23289 /*
23290 * Add all IDC node-table entries.
23291 */
23292 if (! vctxt->psviExposeIDCNodeTables) {
23293 /*
23294 * Just move the entries.
23295 * NOTE: this is quite save here, since
23296 * all the keyref lookups have already been
23297 * performed.
23298 */
23299 parBind->nodeTable = bind->nodeTable;
23300 bind->nodeTable = NULL;
23301 parBind->sizeNodes = bind->sizeNodes;
23302 bind->sizeNodes = 0;
23303 parBind->nbNodes = bind->nbNodes;
23304 bind->nbNodes = 0;
23305 } else {
23306 /*
23307 * Copy the entries.
23308 */
23309 parBind->nodeTable = (xmlSchemaPSVIIDCNodePtr *)
23310 xmlMalloc(bind->nbNodes *
23311 sizeof(xmlSchemaPSVIIDCNodePtr));
23312 if (parBind->nodeTable == NULL) {
23313 xmlSchemaVErrMemory(NULL,
23314 "allocating an array of IDC node-table "
23315 "items", NULL);
23316 xmlSchemaIDCFreeBinding(parBind);
23317 goto internal_error;
23318 }
23319 parBind->sizeNodes = bind->nbNodes;
23320 parBind->nbNodes = bind->nbNodes;
23321 memcpy(parBind->nodeTable, bind->nodeTable,
23322 bind->nbNodes * sizeof(xmlSchemaPSVIIDCNodePtr));
23323 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023324 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023325 if (bind->dupls) {
23326 /*
23327 * Move the duplicates.
23328 */
23329 if (parBind->dupls != NULL)
23330 xmlSchemaItemListFree(parBind->dupls);
23331 parBind->dupls = bind->dupls;
23332 bind->dupls = NULL;
23333 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023334 if (*parTable == NULL)
23335 *parTable = parBind;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023336 else {
23337 parBind->next = *parTable;
23338 *parTable = parBind;
23339 }
23340 }
23341
23342next_binding:
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023343 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023344 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023345 return (0);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023346
23347internal_error:
23348 return(-1);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023349}
23350
23351/**
23352 * xmlSchemaCheckCVCIDCKeyRef:
23353 * @vctxt: the WXS validation context
23354 * @elemDecl: the element declaration
23355 *
23356 * Check the cvc-idc-keyref constraints.
23357 */
23358static int
23359xmlSchemaCheckCVCIDCKeyRef(xmlSchemaValidCtxtPtr vctxt)
23360{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023361 xmlSchemaIDCMatcherPtr matcher;
23362 xmlSchemaPSVIIDCBindingPtr bind;
23363
23364 matcher = vctxt->inode->idcMatchers;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023365 /*
23366 * Find a keyref.
23367 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023368 while (matcher != NULL) {
23369 if ((matcher->idcType == XML_SCHEMA_TYPE_IDC_KEYREF) &&
23370 matcher->targets &&
23371 matcher->targets->nbItems)
23372 {
23373 int i, j, k, res, nbFields, hasDupls;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023374 xmlSchemaPSVIIDCKeyPtr *refKeys, *keys;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023375 xmlSchemaPSVIIDCNodePtr refNode = NULL;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023376
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023377 nbFields = matcher->aidc->def->nbFields;
23378
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023379 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023380 * Find the IDC node-table for the referenced IDC key/unique.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023381 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023382 bind = vctxt->inode->idcTable;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023383 while (bind != NULL) {
23384 if ((xmlSchemaIDCPtr) matcher->aidc->def->ref->item ==
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023385 bind->definition)
23386 break;
23387 bind = bind->next;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023388 }
23389 hasDupls = (bind && bind->dupls && bind->dupls->nbItems) ? 1 : 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023390 /*
23391 * Search for a matching key-sequences.
23392 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023393 for (i = 0; i < matcher->targets->nbItems; i++) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023394 res = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023395 refNode = matcher->targets->items[i];
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023396 if (bind != NULL) {
23397 refKeys = refNode->keys;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023398 for (j = 0; j < bind->nbNodes; j++) {
23399 keys = bind->nodeTable[j]->keys;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023400 for (k = 0; k < nbFields; k++) {
23401 res = xmlSchemaAreValuesEqual(keys[k]->val,
23402 refKeys[k]->val);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023403 if (res == 0)
23404 break;
23405 else if (res == -1) {
23406 return (-1);
23407 }
23408 }
23409 if (res == 1) {
23410 /*
23411 * Match found.
23412 */
23413 break;
23414 }
23415 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023416 if ((res == 0) && hasDupls) {
23417 /*
23418 * Search in duplicates
23419 */
23420 for (j = 0; j < bind->dupls->nbItems; j++) {
23421 keys = ((xmlSchemaPSVIIDCNodePtr)
23422 bind->dupls->items[j])->keys;
23423 for (k = 0; k < nbFields; k++) {
23424 res = xmlSchemaAreValuesEqual(keys[k]->val,
23425 refKeys[k]->val);
23426 if (res == 0)
23427 break;
23428 else if (res == -1) {
23429 return (-1);
23430 }
23431 }
23432 if (res == 1) {
23433 /*
23434 * Match in duplicates found.
23435 */
23436 xmlChar *str = NULL, *strB = NULL;
23437 xmlSchemaKeyrefErr(vctxt,
23438 XML_SCHEMAV_CVC_IDC, refNode,
23439 (xmlSchemaTypePtr) matcher->aidc->def,
23440 "More than one match found for "
23441 "key-sequence %s of keyref '%s'",
23442 xmlSchemaFormatIDCKeySequence(vctxt, &str,
23443 refNode->keys, nbFields),
23444 xmlSchemaGetComponentQName(&strB,
23445 matcher->aidc->def));
23446 FREE_AND_NULL(str);
23447 FREE_AND_NULL(strB);
23448 break;
23449 }
23450 }
23451 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023452 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023453
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023454 if (res == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023455 xmlChar *str = NULL, *strB = NULL;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023456 xmlSchemaKeyrefErr(vctxt,
23457 XML_SCHEMAV_CVC_IDC, refNode,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023458 (xmlSchemaTypePtr) matcher->aidc->def,
23459 "No match found for key-sequence %s of keyref '%s'",
Kasimier T. Buchcikc872afb2005-04-18 10:57:04 +000023460 xmlSchemaFormatIDCKeySequence(vctxt, &str,
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023461 refNode->keys, nbFields),
23462 xmlSchemaGetComponentQName(&strB, matcher->aidc->def));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023463 FREE_AND_NULL(str);
23464 FREE_AND_NULL(strB);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023465 }
23466 }
23467 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023468 matcher = matcher->next;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023469 }
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023470 /* TODO: Return an error if any error encountered. */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023471 return (0);
23472}
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023473
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023474/************************************************************************
23475 * *
23476 * XML Reader validation code *
23477 * *
23478 ************************************************************************/
23479
23480static xmlSchemaAttrInfoPtr
23481xmlSchemaGetFreshAttrInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023482{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023483 xmlSchemaAttrInfoPtr iattr;
23484 /*
23485 * Grow/create list of attribute infos.
23486 */
23487 if (vctxt->attrInfos == NULL) {
23488 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23489 xmlMalloc(sizeof(xmlSchemaAttrInfoPtr));
23490 vctxt->sizeAttrInfos = 1;
23491 if (vctxt->attrInfos == NULL) {
23492 xmlSchemaVErrMemory(vctxt,
23493 "allocating attribute info list", NULL);
23494 return (NULL);
23495 }
23496 } else if (vctxt->sizeAttrInfos <= vctxt->nbAttrInfos) {
23497 vctxt->sizeAttrInfos++;
23498 vctxt->attrInfos = (xmlSchemaAttrInfoPtr *)
23499 xmlRealloc(vctxt->attrInfos,
23500 vctxt->sizeAttrInfos * sizeof(xmlSchemaAttrInfoPtr));
23501 if (vctxt->attrInfos == NULL) {
23502 xmlSchemaVErrMemory(vctxt,
23503 "re-allocating attribute info list", NULL);
23504 return (NULL);
23505 }
23506 } else {
23507 iattr = vctxt->attrInfos[vctxt->nbAttrInfos++];
23508 if (iattr->localName != NULL) {
23509 VERROR_INT("xmlSchemaGetFreshAttrInfo",
23510 "attr info not cleared");
23511 return (NULL);
23512 }
23513 iattr->nodeType = XML_ATTRIBUTE_NODE;
23514 return (iattr);
23515 }
23516 /*
23517 * Create an attribute info.
23518 */
23519 iattr = (xmlSchemaAttrInfoPtr)
23520 xmlMalloc(sizeof(xmlSchemaAttrInfo));
23521 if (iattr == NULL) {
23522 xmlSchemaVErrMemory(vctxt, "creating new attribute info", NULL);
23523 return (NULL);
23524 }
23525 memset(iattr, 0, sizeof(xmlSchemaAttrInfo));
23526 iattr->nodeType = XML_ATTRIBUTE_NODE;
23527 vctxt->attrInfos[vctxt->nbAttrInfos++] = iattr;
23528
23529 return (iattr);
23530}
23531
23532static int
23533xmlSchemaValidatorPushAttribute(xmlSchemaValidCtxtPtr vctxt,
23534 xmlNodePtr attrNode,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023535 int nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023536 const xmlChar *localName,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023537 const xmlChar *nsName,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023538 int ownedNames,
23539 xmlChar *value,
23540 int ownedValue)
23541{
23542 xmlSchemaAttrInfoPtr attr;
23543
23544 attr = xmlSchemaGetFreshAttrInfo(vctxt);
23545 if (attr == NULL) {
23546 VERROR_INT("xmlSchemaPushAttribute",
23547 "calling xmlSchemaGetFreshAttrInfo()");
23548 return (-1);
23549 }
23550 attr->node = attrNode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000023551 attr->nodeLine = nodeLine;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023552 attr->state = XML_SCHEMAS_ATTR_UNKNOWN;
23553 attr->localName = localName;
23554 attr->nsName = nsName;
23555 if (ownedNames)
23556 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
23557 /*
23558 * Evaluate if it's an XSI attribute.
23559 */
23560 if (nsName != NULL) {
23561 if (xmlStrEqual(localName, BAD_CAST "nil")) {
23562 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23563 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NIL;
23564 }
23565 } else if (xmlStrEqual(localName, BAD_CAST "type")) {
23566 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23567 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_TYPE;
23568 }
23569 } else if (xmlStrEqual(localName, BAD_CAST "schemaLocation")) {
23570 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23571 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC;
23572 }
23573 } else if (xmlStrEqual(localName, BAD_CAST "noNamespaceSchemaLocation")) {
23574 if (xmlStrEqual(attr->nsName, xmlSchemaInstanceNs)) {
23575 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC;
23576 }
23577 } else if (xmlStrEqual(attr->nsName, xmlNamespaceNs)) {
23578 attr->metaType = XML_SCHEMA_ATTR_INFO_META_XMLNS;
23579 }
23580 }
23581 attr->value = value;
23582 if (ownedValue)
23583 attr->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
23584 if (attr->metaType != 0)
23585 attr->state = XML_SCHEMAS_ATTR_META;
23586 return (0);
23587}
23588
23589static void
23590xmlSchemaClearElemInfo(xmlSchemaNodeInfoPtr ielem)
23591{
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023592 ielem->hasKeyrefs = 0;
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000023593 ielem->appliedXPath = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023594 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
23595 FREE_AND_NULL(ielem->localName);
23596 FREE_AND_NULL(ielem->nsName);
23597 } else {
23598 ielem->localName = NULL;
23599 ielem->nsName = NULL;
23600 }
23601 if (ielem->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
23602 FREE_AND_NULL(ielem->value);
23603 } else {
23604 ielem->value = NULL;
23605 }
23606 if (ielem->val != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023607 /*
23608 * PSVI TODO: Be careful not to free it when the value is
23609 * exposed via PSVI.
23610 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023611 xmlSchemaFreeValue(ielem->val);
23612 ielem->val = NULL;
23613 }
23614 if (ielem->idcMatchers != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023615 /*
23616 * URGENT OPTIMIZE TODO: Use a pool of IDC matchers.
23617 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023618 xmlSchemaIDCFreeMatcherList(ielem->idcMatchers);
23619 ielem->idcMatchers = NULL;
23620 }
23621 if (ielem->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000023622 /*
23623 * OPTIMIZE TODO: Use a pool of IDC tables??.
23624 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023625 xmlSchemaIDCFreeIDCTable(ielem->idcTable);
23626 ielem->idcTable = NULL;
23627 }
23628 if (ielem->regexCtxt != NULL) {
23629 xmlRegFreeExecCtxt(ielem->regexCtxt);
23630 ielem->regexCtxt = NULL;
23631 }
23632 if (ielem->nsBindings != NULL) {
23633 xmlFree((xmlChar **)ielem->nsBindings);
23634 ielem->nsBindings = NULL;
23635 ielem->nbNsBindings = 0;
23636 ielem->sizeNsBindings = 0;
23637 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023638}
23639
23640/**
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023641 * xmlSchemaGetFreshElemInfo:
23642 * @vctxt: the schema validation context
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023643 *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023644 * Creates/reuses and initializes the element info item for
23645 * the currect tree depth.
23646 *
23647 * Returns the element info item or NULL on API or internal errors.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023648 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023649static xmlSchemaNodeInfoPtr
23650xmlSchemaGetFreshElemInfo(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023651{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023652 xmlSchemaNodeInfoPtr info = NULL;
23653
23654 if (vctxt->depth > vctxt->sizeElemInfos) {
23655 VERROR_INT("xmlSchemaGetFreshElemInfo",
23656 "inconsistent depth encountered");
23657 return (NULL);
23658 }
23659 if (vctxt->elemInfos == NULL) {
23660 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23661 xmlMalloc(10 * sizeof(xmlSchemaNodeInfoPtr));
23662 if (vctxt->elemInfos == NULL) {
23663 xmlSchemaVErrMemory(vctxt,
23664 "allocating the element info array", NULL);
23665 return (NULL);
23666 }
23667 memset(vctxt->elemInfos, 0, 10 * sizeof(xmlSchemaNodeInfoPtr));
23668 vctxt->sizeElemInfos = 10;
23669 } else if (vctxt->sizeElemInfos <= vctxt->depth) {
23670 int i = vctxt->sizeElemInfos;
23671
23672 vctxt->sizeElemInfos *= 2;
23673 vctxt->elemInfos = (xmlSchemaNodeInfoPtr *)
23674 xmlRealloc(vctxt->elemInfos, vctxt->sizeElemInfos *
23675 sizeof(xmlSchemaNodeInfoPtr));
23676 if (vctxt->elemInfos == NULL) {
23677 xmlSchemaVErrMemory(vctxt,
23678 "re-allocating the element info array", NULL);
23679 return (NULL);
23680 }
23681 /*
23682 * We need the new memory to be NULLed.
23683 * TODO: Use memset instead?
23684 */
23685 for (; i < vctxt->sizeElemInfos; i++)
23686 vctxt->elemInfos[i] = NULL;
23687 } else
23688 info = vctxt->elemInfos[vctxt->depth];
23689
23690 if (info == NULL) {
23691 info = (xmlSchemaNodeInfoPtr)
23692 xmlMalloc(sizeof(xmlSchemaNodeInfo));
23693 if (info == NULL) {
23694 xmlSchemaVErrMemory(vctxt,
23695 "allocating an element info", NULL);
23696 return (NULL);
23697 }
23698 vctxt->elemInfos[vctxt->depth] = info;
23699 } else {
23700 if (info->localName != NULL) {
23701 VERROR_INT("xmlSchemaGetFreshElemInfo",
23702 "elem info has not been cleared");
23703 return (NULL);
23704 }
23705 }
23706 memset(info, 0, sizeof(xmlSchemaNodeInfo));
23707 info->nodeType = XML_ELEMENT_NODE;
23708 info->depth = vctxt->depth;
23709
23710 return (info);
23711}
23712
23713#define ACTIVATE_ATTRIBUTE(item) vctxt->inode = (xmlSchemaNodeInfoPtr) item;
23714#define ACTIVATE_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth];
23715#define ACTIVATE_PARENT_ELEM vctxt->inode = vctxt->elemInfos[vctxt->depth -1];
23716
23717static int
23718xmlSchemaValidateFacets(xmlSchemaAbstractCtxtPtr actxt,
23719 xmlNodePtr node,
23720 xmlSchemaTypePtr type,
23721 xmlSchemaValType valType,
23722 const xmlChar * value,
23723 xmlSchemaValPtr val,
23724 unsigned long length,
23725 int fireErrors)
23726{
23727 int ret, error = 0;
23728
23729 xmlSchemaTypePtr tmpType;
23730 xmlSchemaFacetLinkPtr facetLink;
23731 xmlSchemaFacetPtr facet;
23732 unsigned long len = 0;
23733 xmlSchemaWhitespaceValueType ws;
23734
23735 /*
23736 * In Libxml2, derived built-in types have currently no explicit facets.
23737 */
23738 if (type->type == XML_SCHEMA_TYPE_BASIC)
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000023739 return (0);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023740
23741 /*
23742 * NOTE: Do not jump away, if the facetSet of the given type is
23743 * empty: until now, "pattern" and "enumeration" facets of the
23744 * *base types* need to be checked as well.
23745 */
23746 if (type->facetSet == NULL)
23747 goto pattern_and_enum;
23748
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023749 if (! WXS_IS_ATOMIC(type)) {
23750 if (WXS_IS_LIST(type))
23751 goto WXS_IS_LIST;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023752 else
23753 goto pattern_and_enum;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023754 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023755 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023756 * Whitespace handling is only of importance for string-based
23757 * types.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023758 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023759 tmpType = xmlSchemaGetPrimitiveType(type);
23760 if ((tmpType->builtInType == XML_SCHEMAS_STRING) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023761 WXS_IS_ANY_SIMPLE_TYPE(tmpType)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023762 ws = xmlSchemaGetWhiteSpaceFacetValue(type);
23763 } else
23764 ws = XML_SCHEMA_WHITESPACE_COLLAPSE;
23765 /*
23766 * If the value was not computed (for string or
23767 * anySimpleType based types), then use the provided
23768 * type.
23769 */
23770 if (val == NULL)
23771 valType = valType;
23772 else
23773 valType = xmlSchemaGetValType(val);
23774
23775 ret = 0;
23776 for (facetLink = type->facetSet; facetLink != NULL;
23777 facetLink = facetLink->next) {
23778 /*
23779 * Skip the pattern "whiteSpace": it is used to
23780 * format the character content beforehand.
23781 */
23782 switch (facetLink->facet->type) {
23783 case XML_SCHEMA_FACET_WHITESPACE:
23784 case XML_SCHEMA_FACET_PATTERN:
23785 case XML_SCHEMA_FACET_ENUMERATION:
23786 continue;
23787 case XML_SCHEMA_FACET_LENGTH:
23788 case XML_SCHEMA_FACET_MINLENGTH:
23789 case XML_SCHEMA_FACET_MAXLENGTH:
23790 ret = xmlSchemaValidateLengthFacetWhtsp(facetLink->facet,
23791 valType, value, val, &len, ws);
23792 break;
23793 default:
23794 ret = xmlSchemaValidateFacetWhtsp(facetLink->facet, ws,
23795 valType, value, val, ws);
23796 break;
23797 }
23798 if (ret < 0) {
23799 AERROR_INT("xmlSchemaValidateFacets",
23800 "validating against a atomic type facet");
23801 return (-1);
23802 } else if (ret > 0) {
23803 if (fireErrors)
23804 xmlSchemaFacetErr(actxt, ret, node,
23805 value, len, type, facetLink->facet, NULL, NULL, NULL);
23806 else
23807 return (ret);
23808 if (error == 0)
23809 error = ret;
23810 }
23811 ret = 0;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000023812 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023813
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000023814WXS_IS_LIST:
23815 if (! WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000023816 goto pattern_and_enum;
23817 /*
23818 * "length", "minLength" and "maxLength" of list types.
23819 */
23820 ret = 0;
23821 for (facetLink = type->facetSet; facetLink != NULL;
23822 facetLink = facetLink->next) {
23823
23824 switch (facetLink->facet->type) {
23825 case XML_SCHEMA_FACET_LENGTH:
23826 case XML_SCHEMA_FACET_MINLENGTH:
23827 case XML_SCHEMA_FACET_MAXLENGTH:
23828 ret = xmlSchemaValidateListSimpleTypeFacet(facetLink->facet,
23829 value, length, NULL);
23830 break;
23831 default:
23832 continue;
23833 }
23834 if (ret < 0) {
23835 AERROR_INT("xmlSchemaValidateFacets",
23836 "validating against a list type facet");
23837 return (-1);
23838 } else if (ret > 0) {
23839 if (fireErrors)
23840 xmlSchemaFacetErr(actxt, ret, node,
23841 value, length, type, facetLink->facet, NULL, NULL, NULL);
23842 else
23843 return (ret);
23844 if (error == 0)
23845 error = ret;
23846 }
23847 ret = 0;
23848 }
23849
23850pattern_and_enum:
23851 if (error >= 0) {
23852 int found = 0;
23853 /*
23854 * Process enumerations. Facet values are in the value space
23855 * of the defining type's base type. This seems to be a bug in the
23856 * XML Schema 1.0 spec. Use the whitespace type of the base type.
23857 * Only the first set of enumerations in the ancestor-or-self axis
23858 * is used for validation.
23859 */
23860 ret = 0;
23861 tmpType = type;
23862 do {
23863 for (facet = tmpType->facets; facet != NULL; facet = facet->next) {
23864 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
23865 continue;
23866 found = 1;
23867 ret = xmlSchemaAreValuesEqual(facet->val, val);
23868 if (ret == 1)
23869 break;
23870 else if (ret < 0) {
23871 AERROR_INT("xmlSchemaValidateFacets",
23872 "validating against an enumeration facet");
23873 return (-1);
23874 }
23875 }
23876 if (ret != 0)
23877 break;
23878 tmpType = tmpType->baseType;
23879 } while ((tmpType != NULL) &&
23880 (tmpType->type != XML_SCHEMA_TYPE_BASIC));
23881 if (found && (ret == 0)) {
23882 ret = XML_SCHEMAV_CVC_ENUMERATION_VALID;
23883 if (fireErrors) {
23884 xmlSchemaFacetErr(actxt, ret, node,
23885 value, 0, type, NULL, NULL, NULL, NULL);
23886 } else
23887 return (ret);
23888 if (error == 0)
23889 error = ret;
23890 }
23891 }
23892
23893 if (error >= 0) {
23894 int found;
23895 /*
23896 * Process patters. Pattern facets are ORed at type level
23897 * and ANDed if derived. Walk the base type axis.
23898 */
23899 tmpType = type;
23900 facet = NULL;
23901 do {
23902 found = 0;
23903 for (facetLink = tmpType->facetSet; facetLink != NULL;
23904 facetLink = facetLink->next) {
23905 if (facetLink->facet->type != XML_SCHEMA_FACET_PATTERN)
23906 continue;
23907 found = 1;
23908 /*
23909 * NOTE that for patterns, @value needs to be the
23910 * normalized vaule.
23911 */
23912 ret = xmlRegexpExec(facetLink->facet->regexp, value);
23913 if (ret == 1)
23914 break;
23915 else if (ret < 0) {
23916 AERROR_INT("xmlSchemaValidateFacets",
23917 "validating against a pattern facet");
23918 return (-1);
23919 } else {
23920 /*
23921 * Save the last non-validating facet.
23922 */
23923 facet = facetLink->facet;
23924 }
23925 }
23926 if (found && (ret != 1)) {
23927 ret = XML_SCHEMAV_CVC_PATTERN_VALID;
23928 if (fireErrors) {
23929 xmlSchemaFacetErr(actxt, ret, node,
23930 value, 0, type, facet, NULL, NULL, NULL);
23931 } else
23932 return (ret);
23933 if (error == 0)
23934 error = ret;
23935 break;
23936 }
23937 tmpType = tmpType->baseType;
23938 } while ((tmpType != NULL) && (tmpType->type != XML_SCHEMA_TYPE_BASIC));
23939 }
23940
23941 return (error);
23942}
23943
23944static xmlChar *
23945xmlSchemaNormalizeValue(xmlSchemaTypePtr type,
23946 const xmlChar *value)
23947{
23948 switch (xmlSchemaGetWhiteSpaceFacetValue(type)) {
23949 case XML_SCHEMA_WHITESPACE_COLLAPSE:
23950 return (xmlSchemaCollapseString(value));
23951 case XML_SCHEMA_WHITESPACE_REPLACE:
23952 return (xmlSchemaWhiteSpaceReplace(value));
23953 default:
23954 return (NULL);
23955 }
23956}
23957
23958static int
23959xmlSchemaValidateQName(xmlSchemaValidCtxtPtr vctxt,
23960 const xmlChar *value,
23961 xmlSchemaValPtr *val,
23962 int valNeeded)
23963{
23964 int ret;
23965 const xmlChar *nsName;
23966 xmlChar *local, *prefix = NULL;
23967
23968 ret = xmlValidateQName(value, 1);
23969 if (ret != 0) {
23970 if (ret == -1) {
23971 VERROR_INT("xmlSchemaValidateQName",
23972 "calling xmlValidateQName()");
23973 return (-1);
23974 }
23975 return( XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1);
23976 }
23977 /*
23978 * NOTE: xmlSplitQName2 will always return a duplicated
23979 * strings.
23980 */
23981 local = xmlSplitQName2(value, &prefix);
23982 if (local == NULL)
23983 local = xmlStrdup(value);
23984 /*
23985 * OPTIMIZE TODO: Use flags for:
23986 * - is there any namespace binding?
23987 * - is there a default namespace?
23988 */
23989 nsName = xmlSchemaLookupNamespace(vctxt, prefix);
23990
23991 if (prefix != NULL) {
23992 xmlFree(prefix);
23993 /*
23994 * A namespace must be found if the prefix is
23995 * NOT NULL.
23996 */
23997 if (nsName == NULL) {
23998 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000023999 xmlSchemaCustomErr(ACTXT_CAST vctxt, ret, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024000 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024001 "The QName value '%s' has no "
24002 "corresponding namespace declaration in "
24003 "scope", value, NULL);
24004 if (local != NULL)
24005 xmlFree(local);
24006 return (ret);
24007 }
24008 }
24009 if (valNeeded && val) {
24010 if (nsName != NULL)
24011 *val = xmlSchemaNewQNameValue(
24012 BAD_CAST xmlStrdup(nsName), BAD_CAST local);
24013 else
24014 *val = xmlSchemaNewQNameValue(NULL,
24015 BAD_CAST local);
24016 } else
24017 xmlFree(local);
24018 return (0);
24019}
24020
24021/*
24022* cvc-simple-type
24023*/
24024static int
24025xmlSchemaVCheckCVCSimpleType(xmlSchemaAbstractCtxtPtr actxt,
24026 xmlNodePtr node,
24027 xmlSchemaTypePtr type,
24028 const xmlChar *value,
24029 xmlSchemaValPtr *retVal,
24030 int fireErrors,
24031 int normalize,
24032 int isNormalized)
24033{
24034 int ret = 0, valNeeded = (retVal) ? 1 : 0;
24035 xmlSchemaValPtr val = NULL;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024036 /* xmlSchemaWhitespaceValueType ws; */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024037 xmlChar *normValue = NULL;
24038
24039#define NORMALIZE(atype) \
24040 if ((! isNormalized) && \
24041 (normalize || (type->flags & XML_SCHEMAS_TYPE_NORMVALUENEEDED))) { \
24042 normValue = xmlSchemaNormalizeValue(atype, value); \
24043 if (normValue != NULL) \
24044 value = normValue; \
24045 isNormalized = 1; \
24046 }
24047
24048 if ((retVal != NULL) && (*retVal != NULL)) {
24049 xmlSchemaFreeValue(*retVal);
24050 *retVal = NULL;
24051 }
24052 /*
24053 * 3.14.4 Simple Type Definition Validation Rules
24054 * Validation Rule: String Valid
24055 */
24056 /*
24057 * 1 It is schema-valid with respect to that definition as defined
24058 * by Datatype Valid in [XML Schemas: Datatypes].
24059 */
24060 /*
24061 * 2.1 If The definition is ENTITY or is validly derived from ENTITY given
24062 * the empty set, as defined in Type Derivation OK (Simple) (§3.14.6), then
24063 * the string must be a ·declared entity name·.
24064 */
24065 /*
24066 * 2.2 If The definition is ENTITIES or is validly derived from ENTITIES
24067 * given the empty set, as defined in Type Derivation OK (Simple) (§3.14.6),
24068 * then every whitespace-delimited substring of the string must be a ·declared
24069 * entity name·.
24070 */
24071 /*
24072 * 2.3 otherwise no further condition applies.
24073 */
24074 if ((! valNeeded) && (type->flags & XML_SCHEMAS_TYPE_FACETSNEEDVALUE))
24075 valNeeded = 1;
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000024076 if (value == NULL)
24077 value = BAD_CAST "";
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024078 if (WXS_IS_ANY_SIMPLE_TYPE(type) || WXS_IS_ATOMIC(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024079 xmlSchemaTypePtr biType; /* The built-in type. */
24080 /*
24081 * SPEC (1.2.1) "if {variety} is ·atomic· then the string must ·match·
24082 * a literal in the ·lexical space· of {base type definition}"
24083 */
24084 /*
24085 * Whitespace-normalize.
24086 */
24087 NORMALIZE(type);
24088 if (type->type != XML_SCHEMA_TYPE_BASIC) {
24089 /*
24090 * Get the built-in type.
24091 */
24092 biType = type->baseType;
24093 while ((biType != NULL) &&
24094 (biType->type != XML_SCHEMA_TYPE_BASIC))
24095 biType = biType->baseType;
24096
24097 if (biType == NULL) {
24098 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24099 "could not get the built-in type");
24100 goto internal_error;
24101 }
24102 } else
24103 biType = type;
24104 /*
24105 * NOTATIONs need to be processed here, since they need
24106 * to lookup in the hashtable of NOTATION declarations of the schema.
24107 */
24108 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
24109 switch (biType->builtInType) {
24110 case XML_SCHEMAS_NOTATION:
24111 ret = xmlSchemaValidateNotation(
24112 (xmlSchemaValidCtxtPtr) actxt,
24113 ((xmlSchemaValidCtxtPtr) actxt)->schema,
24114 NULL, value, &val, valNeeded);
24115 break;
24116 case XML_SCHEMAS_QNAME:
24117 ret = xmlSchemaValidateQName((xmlSchemaValidCtxtPtr) actxt,
24118 value, &val, valNeeded);
24119 break;
24120 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024121 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024122 if (valNeeded)
24123 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24124 value, &val, NULL);
24125 else
24126 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24127 value, NULL, NULL);
24128 break;
24129 }
24130 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
24131 switch (biType->builtInType) {
24132 case XML_SCHEMAS_NOTATION:
24133 ret = xmlSchemaValidateNotation(NULL,
24134 ((xmlSchemaParserCtxtPtr) actxt)->schema, node,
24135 value, &val, valNeeded);
24136 break;
24137 default:
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000024138 /* ws = xmlSchemaGetWhiteSpaceFacetValue(type); */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024139 if (valNeeded)
24140 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24141 value, &val, node);
24142 else
24143 ret = xmlSchemaValPredefTypeNodeNoNorm(biType,
24144 value, NULL, node);
24145 break;
24146 }
24147 } else {
24148 /*
24149 * Validation via a public API is not implemented yet.
24150 */
24151 TODO
24152 goto internal_error;
24153 }
24154 if (ret != 0) {
24155 if (ret < 0) {
24156 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24157 "validating against a built-in type");
24158 goto internal_error;
24159 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024160 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024161 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24162 else
24163 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24164 }
24165 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24166 /*
24167 * Check facets.
24168 */
24169 ret = xmlSchemaValidateFacets(actxt, node, type,
24170 (xmlSchemaValType) biType->builtInType, value, val,
24171 0, fireErrors);
24172 if (ret != 0) {
24173 if (ret < 0) {
24174 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24175 "validating facets of atomic simple type");
24176 goto internal_error;
24177 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024178 if (WXS_IS_LIST(type))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024179 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24180 else
24181 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1;
24182 }
24183 }
24184 if (fireErrors && (ret > 0))
24185 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024186 } else if (WXS_IS_LIST(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024187
24188 xmlSchemaTypePtr itemType;
24189 const xmlChar *cur, *end;
24190 xmlChar *tmpValue = NULL;
24191 unsigned long len = 0;
24192 xmlSchemaValPtr prevVal = NULL, curVal = NULL;
24193 /* 1.2.2 if {variety} is ·list· then the string must be a sequence
24194 * of white space separated tokens, each of which ·match·es a literal
24195 * in the ·lexical space· of {item type definition}
24196 */
24197 /*
24198 * Note that XML_SCHEMAS_TYPE_NORMVALUENEEDED will be set if
24199 * the list type has an enum or pattern facet.
24200 */
24201 NORMALIZE(type);
24202 /*
24203 * VAL TODO: Optimize validation of empty values.
24204 * VAL TODO: We do not have computed values for lists.
24205 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024206 itemType = WXS_LIST_ITEMTYPE(type);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024207 cur = value;
24208 do {
24209 while (IS_BLANK_CH(*cur))
24210 cur++;
24211 end = cur;
24212 while ((*end != 0) && (!(IS_BLANK_CH(*end))))
24213 end++;
24214 if (end == cur)
24215 break;
24216 tmpValue = xmlStrndup(cur, end - cur);
24217 len++;
24218
24219 if (valNeeded)
24220 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24221 tmpValue, &curVal, fireErrors, 0, 1);
24222 else
24223 ret = xmlSchemaVCheckCVCSimpleType(actxt, node, itemType,
24224 tmpValue, NULL, fireErrors, 0, 1);
24225 FREE_AND_NULL(tmpValue);
24226 if (curVal != NULL) {
24227 /*
24228 * Add to list of computed values.
24229 */
24230 if (val == NULL)
24231 val = curVal;
24232 else
24233 xmlSchemaValueAppend(prevVal, curVal);
24234 prevVal = curVal;
24235 curVal = NULL;
24236 }
24237 if (ret != 0) {
24238 if (ret < 0) {
24239 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24240 "validating an item of list simple type");
24241 goto internal_error;
24242 }
24243 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24244 break;
24245 }
24246 cur = end;
24247 } while (*cur != 0);
24248 FREE_AND_NULL(tmpValue);
24249 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24250 /*
24251 * Apply facets (pattern, enumeration).
24252 */
24253 ret = xmlSchemaValidateFacets(actxt, node, type,
24254 XML_SCHEMAS_UNKNOWN, value, val,
24255 len, fireErrors);
24256 if (ret != 0) {
24257 if (ret < 0) {
24258 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24259 "validating facets of list simple type");
24260 goto internal_error;
24261 }
24262 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_2;
24263 }
24264 }
24265 if (fireErrors && (ret > 0)) {
24266 /*
24267 * Report the normalized value.
24268 */
24269 normalize = 1;
24270 NORMALIZE(type);
24271 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24272 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024273 } else if (WXS_IS_UNION(type)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024274 xmlSchemaTypeLinkPtr memberLink;
24275 /*
24276 * TODO: For all datatypes ·derived· by ·union· whiteSpace does
24277 * not apply directly; however, the normalization behavior of ·union·
24278 * types is controlled by the value of whiteSpace on that one of the
24279 * ·memberTypes· against which the ·union· is successfully validated.
24280 *
24281 * This means that the value is normalized by the first validating
24282 * member type, then the facets of the union type are applied. This
24283 * needs changing of the value!
24284 */
24285
24286 /*
24287 * 1.2.3 if {variety} is ·union· then the string must ·match· a
24288 * literal in the ·lexical space· of at least one member of
24289 * {member type definitions}
24290 */
24291 memberLink = xmlSchemaGetUnionSimpleTypeMemberTypes(type);
24292 if (memberLink == NULL) {
24293 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24294 "union simple type has no member types");
24295 goto internal_error;
24296 }
24297 /*
24298 * Always normalize union type values, since we currently
24299 * cannot store the whitespace information with the value
24300 * itself; otherwise a later value-comparison would be
24301 * not possible.
24302 */
24303 while (memberLink != NULL) {
24304 if (valNeeded)
24305 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24306 memberLink->type, value, &val, 0, 1, 0);
24307 else
24308 ret = xmlSchemaVCheckCVCSimpleType(actxt, node,
24309 memberLink->type, value, NULL, 0, 1, 0);
24310 if (ret <= 0)
24311 break;
24312 memberLink = memberLink->next;
24313 }
24314 if (ret != 0) {
24315 if (ret < 0) {
24316 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24317 "validating members of union simple type");
24318 goto internal_error;
24319 }
24320 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24321 }
24322 /*
24323 * Apply facets (pattern, enumeration).
24324 */
24325 if ((ret == 0) && (type->flags & XML_SCHEMAS_TYPE_HAS_FACETS)) {
24326 /*
24327 * The normalization behavior of ·union· types is controlled by
24328 * the value of whiteSpace on that one of the ·memberTypes·
24329 * against which the ·union· is successfully validated.
24330 */
24331 NORMALIZE(memberLink->type);
24332 ret = xmlSchemaValidateFacets(actxt, node, type,
24333 XML_SCHEMAS_UNKNOWN, value, val,
24334 0, fireErrors);
24335 if (ret != 0) {
24336 if (ret < 0) {
24337 AERROR_INT("xmlSchemaVCheckCVCSimpleType",
24338 "validating facets of union simple type");
24339 goto internal_error;
24340 }
24341 ret = XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_3;
24342 }
24343 }
24344 if (fireErrors && (ret > 0))
24345 xmlSchemaSimpleTypeErr(actxt, ret, node, value, type, 1);
24346 }
24347
24348 if (normValue != NULL)
24349 xmlFree(normValue);
24350 if (ret == 0) {
24351 if (retVal != NULL)
24352 *retVal = val;
24353 else if (val != NULL)
24354 xmlSchemaFreeValue(val);
24355 } else if (val != NULL)
24356 xmlSchemaFreeValue(val);
24357 return (ret);
24358internal_error:
24359 if (normValue != NULL)
24360 xmlFree(normValue);
24361 if (val != NULL)
24362 xmlSchemaFreeValue(val);
24363 return (-1);
24364}
24365
24366static int
24367xmlSchemaVExpandQName(xmlSchemaValidCtxtPtr vctxt,
24368 const xmlChar *value,
24369 const xmlChar **nsName,
24370 const xmlChar **localName)
24371{
24372 int ret = 0;
24373
24374 if ((nsName == NULL) || (localName == NULL))
24375 return (-1);
24376 *nsName = NULL;
24377 *localName = NULL;
24378
24379 ret = xmlValidateQName(value, 1);
24380 if (ret == -1)
24381 return (-1);
24382 if (ret > 0) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024383 xmlSchemaSimpleTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024384 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
24385 value, xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME), 1);
24386 return (1);
24387 }
24388 {
24389 xmlChar *local = NULL;
24390 xmlChar *prefix;
24391
24392 /*
24393 * NOTE: xmlSplitQName2 will return a duplicated
24394 * string.
24395 */
24396 local = xmlSplitQName2(value, &prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024397 if (local == NULL)
24398 *localName = xmlDictLookup(vctxt->dict, value, -1);
24399 else {
24400 *localName = xmlDictLookup(vctxt->dict, local, -1);
24401 xmlFree(local);
24402 }
24403
24404 *nsName = xmlSchemaLookupNamespace(vctxt, prefix);
24405
24406 if (prefix != NULL) {
24407 xmlFree(prefix);
24408 /*
24409 * A namespace must be found if the prefix is NOT NULL.
24410 */
24411 if (*nsName == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024412 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024413 XML_SCHEMAV_CVC_DATATYPE_VALID_1_2_1, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024414 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024415 "The QName value '%s' has no "
24416 "corresponding namespace declaration in scope",
24417 value, NULL);
24418 return (2);
24419 }
24420 }
24421 }
24422 return (0);
24423}
24424
24425static int
24426xmlSchemaProcessXSIType(xmlSchemaValidCtxtPtr vctxt,
24427 xmlSchemaAttrInfoPtr iattr,
24428 xmlSchemaTypePtr *localType,
24429 xmlSchemaElementPtr elemDecl)
24430{
24431 int ret = 0;
24432 /*
24433 * cvc-elt (3.3.4) : (4)
24434 * AND
24435 * Schema-Validity Assessment (Element) (cvc-assess-elt)
24436 * (1.2.1.2.1) - (1.2.1.2.4)
24437 * Handle 'xsi:type'.
24438 */
24439 if (localType == NULL)
24440 return (-1);
24441 *localType = NULL;
24442 if (iattr == NULL)
24443 return (0);
24444 else {
24445 const xmlChar *nsName = NULL, *local = NULL;
24446 /*
24447 * TODO: We should report a *warning* that the type was overriden
24448 * by the instance.
24449 */
24450 ACTIVATE_ATTRIBUTE(iattr);
24451 /*
24452 * (cvc-elt) (3.3.4) : (4.1)
24453 * (cvc-assess-elt) (1.2.1.2.2)
24454 */
24455 ret = xmlSchemaVExpandQName(vctxt, iattr->value,
24456 &nsName, &local);
24457 if (ret != 0) {
24458 if (ret < 0) {
24459 VERROR_INT("xmlSchemaValidateElementByDeclaration",
24460 "calling xmlSchemaQNameExpand() to validate the "
24461 "attribute 'xsi:type'");
24462 goto internal_error;
24463 }
24464 goto exit;
24465 }
24466 /*
24467 * (cvc-elt) (3.3.4) : (4.2)
24468 * (cvc-assess-elt) (1.2.1.2.3)
24469 */
24470 *localType = xmlSchemaGetType(vctxt->schema, local, nsName);
24471 if (*localType == NULL) {
24472 xmlChar *str = NULL;
24473
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024474 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024475 XML_SCHEMAV_CVC_ELT_4_2, NULL,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024476 WXS_BASIC_CAST xmlSchemaGetBuiltInType(XML_SCHEMAS_QNAME),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024477 "The QName value '%s' of the xsi:type attribute does not "
24478 "resolve to a type definition",
24479 xmlSchemaFormatQName(&str, nsName, local), NULL);
24480 FREE_AND_NULL(str);
24481 ret = vctxt->err;
24482 goto exit;
24483 }
24484 if (elemDecl != NULL) {
24485 int set = 0;
24486
24487 /*
24488 * SPEC cvc-elt (3.3.4) : (4.3) (Type Derivation OK)
24489 * "The ·local type definition· must be validly
24490 * derived from the {type definition} given the union of
24491 * the {disallowed substitutions} and the {type definition}'s
24492 * {prohibited substitutions}, as defined in
24493 * Type Derivation OK (Complex) (§3.4.6)
24494 * (if it is a complex type definition),
24495 * or given {disallowed substitutions} as defined in Type
24496 * Derivation OK (Simple) (§3.14.6) (if it is a simple type
24497 * definition)."
24498 *
24499 * {disallowed substitutions}: the "block" on the element decl.
24500 * {prohibited substitutions}: the "block" on the type def.
24501 */
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024502 /*
24503 * OPTIMIZE TODO: We could map types already evaluated
24504 * to be validly derived from other types to avoid checking
24505 * this over and over for the same types.
24506 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024507 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_EXTENSION) ||
24508 (elemDecl->subtypes->flags &
24509 XML_SCHEMAS_TYPE_BLOCK_EXTENSION))
24510 set |= SUBSET_EXTENSION;
24511
24512 if ((elemDecl->flags & XML_SCHEMAS_ELEM_BLOCK_RESTRICTION) ||
24513 (elemDecl->subtypes->flags &
24514 XML_SCHEMAS_TYPE_BLOCK_RESTRICTION))
24515 set |= SUBSET_RESTRICTION;
24516
Kasimier T. Buchcikf326e772005-10-20 11:06:00 +000024517 /*
24518 * REMOVED and CHANGED since this produced a parser context
24519 * which adds to the string dict of the schema. So this would
24520 * change the schema and we don't want this. We don't need
24521 * the parser context anymore.
24522 *
24523 * if ((vctxt->pctxt == NULL) &&
24524 * (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
24525 * return (-1);
24526 */
24527
24528 if (xmlSchemaCheckCOSDerivedOK(ACTXT_CAST vctxt, *localType,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024529 elemDecl->subtypes, set) != 0) {
24530 xmlChar *str = NULL;
24531
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024532 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024533 XML_SCHEMAV_CVC_ELT_4_3, NULL, NULL,
24534 "The type definition '%s', specified by xsi:type, is "
24535 "blocked or not validly derived from the type definition "
24536 "of the element declaration",
24537 xmlSchemaFormatQName(&str,
24538 (*localType)->targetNamespace,
24539 (*localType)->name),
24540 NULL);
24541 FREE_AND_NULL(str);
24542 ret = vctxt->err;
24543 *localType = NULL;
24544 }
24545 }
24546 }
24547exit:
24548 ACTIVATE_ELEM;
24549 return (ret);
24550internal_error:
24551 ACTIVATE_ELEM;
24552 return (-1);
24553}
24554
24555static int
24556xmlSchemaValidateElemDecl(xmlSchemaValidCtxtPtr vctxt)
24557{
24558 xmlSchemaElementPtr elemDecl = vctxt->inode->decl;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024559 xmlSchemaTypePtr actualType = WXS_ELEM_TYPEDEF(elemDecl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024560
24561 /*
24562 * cvc-elt (3.3.4) : 1
24563 */
24564 if (elemDecl == NULL) {
24565 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL,
24566 "No matching declaration available");
24567 return (vctxt->err);
24568 }
24569 /*
24570 * cvc-elt (3.3.4) : 2
24571 */
24572 if (elemDecl->flags & XML_SCHEMAS_ELEM_ABSTRACT) {
24573 VERROR(XML_SCHEMAV_CVC_ELT_2, NULL,
24574 "The element declaration is abstract");
24575 return (vctxt->err);
24576 }
24577 if (actualType == NULL) {
24578 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24579 "The type definition is absent");
24580 return (XML_SCHEMAV_CVC_TYPE_1);
24581 }
24582 if (vctxt->nbAttrInfos != 0) {
24583 int ret;
24584 xmlSchemaAttrInfoPtr iattr;
24585 /*
24586 * cvc-elt (3.3.4) : 3
24587 * Handle 'xsi:nil'.
24588 */
24589 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24590 XML_SCHEMA_ATTR_INFO_META_XSI_NIL);
24591 if (iattr) {
24592 ACTIVATE_ATTRIBUTE(iattr);
24593 /*
24594 * Validate the value.
24595 */
24596 ret = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024597 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024598 xmlSchemaGetBuiltInType(XML_SCHEMAS_BOOLEAN),
24599 iattr->value, &(iattr->val), 1, 0, 0);
24600 ACTIVATE_ELEM;
24601 if (ret < 0) {
24602 VERROR_INT("xmlSchemaValidateElemDecl",
24603 "calling xmlSchemaVCheckCVCSimpleType() to "
24604 "validate the attribute 'xsi:nil'");
24605 return (-1);
24606 }
24607 if (ret == 0) {
24608 if ((elemDecl->flags & XML_SCHEMAS_ELEM_NILLABLE) == 0) {
24609 /*
24610 * cvc-elt (3.3.4) : 3.1
24611 */
24612 VERROR(XML_SCHEMAV_CVC_ELT_3_1, NULL,
24613 "The element is not 'nillable'");
24614 /* Does not return an error on purpose. */
24615 } else {
24616 if (xmlSchemaValueGetAsBoolean(iattr->val)) {
24617 /*
24618 * cvc-elt (3.3.4) : 3.2.2
24619 */
24620 if ((elemDecl->flags & XML_SCHEMAS_ELEM_FIXED) &&
24621 (elemDecl->value != NULL)) {
24622 VERROR(XML_SCHEMAV_CVC_ELT_3_2_2, NULL,
24623 "The element cannot be 'nilled' because "
24624 "there is a fixed value constraint defined "
24625 "for it");
24626 /* Does not return an error on purpose. */
24627 } else
24628 vctxt->inode->flags |=
24629 XML_SCHEMA_ELEM_INFO_NILLED;
24630 }
24631 }
24632 }
24633 }
24634 /*
24635 * cvc-elt (3.3.4) : 4
24636 * Handle 'xsi:type'.
24637 */
24638 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
24639 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
24640 if (iattr) {
24641 xmlSchemaTypePtr localType = NULL;
24642
24643 ret = xmlSchemaProcessXSIType(vctxt, iattr, &localType,
24644 elemDecl);
24645 if (ret != 0) {
24646 if (ret == -1) {
24647 VERROR_INT("xmlSchemaValidateElemDecl",
24648 "calling xmlSchemaProcessXSIType() to "
24649 "process the attribute 'xsi:type'");
24650 return (-1);
24651 }
24652 /* Does not return an error on purpose. */
24653 }
24654 if (localType != NULL) {
24655 vctxt->inode->flags |= XML_SCHEMA_ELEM_INFO_LOCAL_TYPE;
24656 actualType = localType;
24657 }
24658 }
24659 }
24660 /*
24661 * IDC: Register identity-constraint XPath matchers.
24662 */
24663 if ((elemDecl->idcs != NULL) &&
24664 (xmlSchemaIDCRegisterMatchers(vctxt, elemDecl) == -1))
24665 return (-1);
24666 /*
24667 * No actual type definition.
24668 */
24669 if (actualType == NULL) {
24670 VERROR(XML_SCHEMAV_CVC_TYPE_1, NULL,
24671 "The type definition is absent");
24672 return (XML_SCHEMAV_CVC_TYPE_1);
24673 }
24674 /*
24675 * Remember the actual type definition.
24676 */
24677 vctxt->inode->typeDef = actualType;
24678
24679 return (0);
24680}
24681
24682static int
24683xmlSchemaVAttributesSimple(xmlSchemaValidCtxtPtr vctxt)
24684{
24685 xmlSchemaAttrInfoPtr iattr;
24686 int ret = 0, i;
24687
24688 /*
24689 * SPEC cvc-type (3.1.1)
24690 * "The attributes of must be empty, excepting those whose namespace
24691 * name is identical to http://www.w3.org/2001/XMLSchema-instance and
24692 * whose local name is one of type, nil, schemaLocation or
24693 * noNamespaceSchemaLocation."
24694 */
24695 if (vctxt->nbAttrInfos == 0)
24696 return (0);
24697 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24698 iattr = vctxt->attrInfos[i];
24699 if (! iattr->metaType) {
24700 ACTIVATE_ATTRIBUTE(iattr)
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000024701 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024702 XML_SCHEMAV_CVC_TYPE_3_1_1, iattr, NULL);
24703 ret = XML_SCHEMAV_CVC_TYPE_3_1_1;
24704 }
24705 }
24706 ACTIVATE_ELEM
24707 return (ret);
24708}
24709
24710/*
24711* Cleanup currently used attribute infos.
24712*/
24713static void
24714xmlSchemaClearAttrInfos(xmlSchemaValidCtxtPtr vctxt)
24715{
24716 int i;
24717 xmlSchemaAttrInfoPtr attr;
24718
24719 if (vctxt->nbAttrInfos == 0)
24720 return;
24721 for (i = 0; i < vctxt->nbAttrInfos; i++) {
24722 attr = vctxt->attrInfos[i];
24723 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES) {
24724 if (attr->localName != NULL)
24725 xmlFree((xmlChar *) attr->localName);
24726 if (attr->nsName != NULL)
24727 xmlFree((xmlChar *) attr->nsName);
24728 }
24729 if (attr->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
24730 if (attr->value != NULL)
24731 xmlFree((xmlChar *) attr->value);
24732 }
24733 if (attr->val != NULL) {
24734 xmlSchemaFreeValue(attr->val);
24735 attr->val = NULL;
24736 }
24737 memset(attr, 0, sizeof(xmlSchemaAttrInfo));
24738 }
24739 vctxt->nbAttrInfos = 0;
24740}
24741
24742/*
24743* 3.4.4 Complex Type Definition Validation Rules
24744* Element Locally Valid (Complex Type) (cvc-complex-type)
24745* 3.2.4 Attribute Declaration Validation Rules
24746* Validation Rule: Attribute Locally Valid (cvc-attribute)
24747* Attribute Locally Valid (Use) (cvc-au)
24748*
24749* Only "assessed" attribute information items will be visible to
24750* IDCs. I.e. not "lax" (without declaration) and "skip" wild attributes.
24751*/
24752static int
24753xmlSchemaVAttributesComplex(xmlSchemaValidCtxtPtr vctxt)
24754{
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024755 xmlSchemaTypePtr type = vctxt->inode->typeDef;
24756 xmlSchemaItemListPtr attrUseList;
24757 xmlSchemaAttributeUsePtr attrUse = NULL;
24758 xmlSchemaAttributePtr attrDecl = NULL;
24759 xmlSchemaAttrInfoPtr iattr, tmpiattr;
24760 int i, j, found, nbAttrs, nbUses;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024761 int xpathRes = 0, res, wildIDs = 0, fixed;
24762
24763 /*
24764 * SPEC (cvc-attribute)
24765 * (1) "The declaration must not be ·absent· (see Missing
24766 * Sub-components (§5.3) for how this can fail to be
24767 * the case)."
24768 * (2) "Its {type definition} must not be absent."
24769 *
24770 * NOTE (1) + (2): This is not handled here, since we currently do not
24771 * allow validation against schemas which have missing sub-components.
24772 *
24773 * SPEC (cvc-complex-type)
24774 * (3) "For each attribute information item in the element information
24775 * item's [attributes] excepting those whose [namespace name] is
24776 * identical to http://www.w3.org/2001/XMLSchema-instance and whose
24777 * [local name] is one of type, nil, schemaLocation or
24778 * noNamespaceSchemaLocation, the appropriate case among the following
24779 * must be true:
24780 *
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024781 */
24782 attrUseList = (xmlSchemaItemListPtr) type->attrUses;
24783 /*
24784 * @nbAttrs is the number of attributes present in the instance.
24785 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024786 nbAttrs = vctxt->nbAttrInfos;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024787 if (attrUseList != NULL)
24788 nbUses = attrUseList->nbItems;
24789 else
24790 nbUses = 0;
24791 for (i = 0; i < nbUses; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024792 found = 0;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024793 attrUse = attrUseList->items[i];
24794 attrDecl = WXS_ATTRUSE_DECL(attrUse);
24795 for (j = 0; j < nbAttrs; j++) {
24796 iattr = vctxt->attrInfos[j];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024797 /*
24798 * SPEC (cvc-complex-type) (3)
24799 * Skip meta attributes.
24800 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024801 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024802 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024803 if (iattr->localName[0] != attrDecl->name[0])
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024804 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024805 if (!xmlStrEqual(iattr->localName, attrDecl->name))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024806 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024807 if (!xmlStrEqual(iattr->nsName, attrDecl->targetNamespace))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024808 continue;
24809 found = 1;
24810 /*
24811 * SPEC (cvc-complex-type)
24812 * (3.1) "If there is among the {attribute uses} an attribute
24813 * use with an {attribute declaration} whose {name} matches
24814 * the attribute information item's [local name] and whose
24815 * {target namespace} is identical to the attribute information
24816 * item's [namespace name] (where an ·absent· {target namespace}
24817 * is taken to be identical to a [namespace name] with no value),
24818 * then the attribute information must be ·valid· with respect
24819 * to that attribute use as per Attribute Locally Valid (Use)
24820 * (§3.5.4). In this case the {attribute declaration} of that
24821 * attribute use is the ·context-determined declaration· for the
24822 * attribute information item with respect to Schema-Validity
24823 * Assessment (Attribute) (§3.2.4) and
24824 * Assessment Outcome (Attribute) (§3.2.5).
24825 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024826 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
24827 iattr->use = attrUse;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024828 /*
24829 * Context-determined declaration.
24830 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024831 iattr->decl = attrDecl;
24832 iattr->typeDef = attrDecl->subtypes;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024833 break;
24834 }
24835
24836 if (found)
24837 continue;
24838
24839 if (attrUse->occurs == XML_SCHEMAS_ATTR_USE_REQUIRED) {
24840 /*
24841 * Handle non-existent, required attributes.
24842 *
24843 * SPEC (cvc-complex-type)
24844 * (4) "The {attribute declaration} of each attribute use in
24845 * the {attribute uses} whose {required} is true matches one
24846 * of the attribute information items in the element information
24847 * item's [attributes] as per clause 3.1 above."
24848 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024849 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24850 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024851 VERROR_INT(
24852 "xmlSchemaVAttributesComplex",
24853 "calling xmlSchemaGetFreshAttrInfo()");
24854 return (-1);
24855 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024856 tmpiattr->state = XML_SCHEMAS_ATTR_ERR_MISSING;
24857 tmpiattr->use = attrUse;
24858 tmpiattr->decl = attrDecl;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024859 } else if ((attrUse->occurs == XML_SCHEMAS_ATTR_USE_OPTIONAL) &&
24860 ((attrUse->defValue != NULL) ||
24861 (attrDecl->defValue != NULL))) {
24862 /*
24863 * Handle non-existent, optional, default/fixed attributes.
24864 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024865 tmpiattr = xmlSchemaGetFreshAttrInfo(vctxt);
24866 if (tmpiattr == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024867 VERROR_INT(
24868 "xmlSchemaVAttributesComplex",
24869 "calling xmlSchemaGetFreshAttrInfo()");
24870 return (-1);
24871 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024872 tmpiattr->state = XML_SCHEMAS_ATTR_DEFAULT;
24873 tmpiattr->use = attrUse;
24874 tmpiattr->decl = attrDecl;
24875 tmpiattr->typeDef = attrDecl->subtypes;
24876 tmpiattr->localName = attrDecl->name;
24877 tmpiattr->nsName = attrDecl->targetNamespace;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024878 }
24879 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024880
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024881 if (vctxt->nbAttrInfos == 0)
24882 return (0);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024883 nbUses = vctxt->nbAttrInfos;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024884 /*
24885 * Validate against the wildcard.
24886 */
24887 if (type->attributeWildcard != NULL) {
24888 /*
24889 * SPEC (cvc-complex-type)
24890 * (3.2.1) "There must be an {attribute wildcard}."
24891 */
24892 for (i = 0; i < nbAttrs; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024893 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024894 /*
24895 * SPEC (cvc-complex-type) (3)
24896 * Skip meta attributes.
24897 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024898 if (iattr->state != XML_SCHEMAS_ATTR_UNKNOWN)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024899 continue;
24900 /*
24901 * SPEC (cvc-complex-type)
24902 * (3.2.2) "The attribute information item must be ·valid· with
24903 * respect to it as defined in Item Valid (Wildcard) (§3.10.4)."
24904 *
24905 * SPEC Item Valid (Wildcard) (cvc-wildcard)
24906 * "... its [namespace name] must be ·valid· with respect to
24907 * the wildcard constraint, as defined in Wildcard allows
24908 * Namespace Name (§3.10.4)."
24909 */
24910 if (xmlSchemaCheckCVCWildcardNamespace(type->attributeWildcard,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024911 iattr->nsName) == 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024912 /*
24913 * Handle processContents.
24914 *
24915 * SPEC (cvc-wildcard):
24916 * processContents | context-determined declaration:
24917 * "strict" "mustFind"
24918 * "lax" "none"
24919 * "skip" "skip"
24920 */
24921 if (type->attributeWildcard->processContents ==
24922 XML_SCHEMAS_ANY_SKIP) {
24923 /*
24924 * context-determined declaration = "skip"
24925 *
24926 * SPEC PSVI Assessment Outcome (Attribute)
24927 * [validity] = "notKnown"
24928 * [validation attempted] = "none"
24929 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024930 iattr->state = XML_SCHEMAS_ATTR_WILD_SKIP;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024931 continue;
24932 }
24933 /*
24934 * Find an attribute declaration.
24935 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024936 iattr->decl = xmlSchemaGetAttributeDecl(vctxt->schema,
24937 iattr->localName, iattr->nsName);
24938 if (iattr->decl != NULL) {
24939 iattr->state = XML_SCHEMAS_ATTR_ASSESSED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024940 /*
24941 * SPEC (cvc-complex-type)
24942 * (5) "Let [Definition:] the wild IDs be the set of
24943 * all attribute information item to which clause 3.2
24944 * applied and whose ·validation· resulted in a
24945 * ·context-determined declaration· of mustFind or no
24946 * ·context-determined declaration· at all, and whose
24947 * [local name] and [namespace name] resolve (as
24948 * defined by QName resolution (Instance) (§3.15.4)) to
24949 * an attribute declaration whose {type definition} is
24950 * or is derived from ID. Then all of the following
24951 * must be true:"
24952 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024953 iattr->typeDef = WXS_ATTR_TYPEDEF(iattr->decl);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024954 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024955 iattr->typeDef, XML_SCHEMAS_ID)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024956 /*
24957 * SPEC (5.1) "There must be no more than one
24958 * item in ·wild IDs·."
24959 */
24960 if (wildIDs != 0) {
24961 /* VAL TODO */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024962 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024963 TODO
24964 continue;
24965 }
24966 wildIDs++;
24967 /*
24968 * SPEC (cvc-complex-type)
24969 * (5.2) "If ·wild IDs· is non-empty, there must not
24970 * be any attribute uses among the {attribute uses}
24971 * whose {attribute declaration}'s {type definition}
24972 * is or is derived from ID."
24973 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024974 for (j = 0; j < attrUseList->nbItems; j++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024975 if (xmlSchemaIsDerivedFromBuiltInType(
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024976 WXS_ATTRUSE_TYPEDEF(attrUseList->items[j]),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024977 XML_SCHEMAS_ID)) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024978 /* URGENT VAL TODO: implement */
24979 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024980 TODO
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024981 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024982 }
24983 }
24984 }
24985 } else if (type->attributeWildcard->processContents ==
24986 XML_SCHEMAS_ANY_LAX) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024987 iattr->state = XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024988 /*
24989 * SPEC PSVI Assessment Outcome (Attribute)
24990 * [validity] = "notKnown"
24991 * [validation attempted] = "none"
24992 */
24993 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000024994 iattr->state = XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000024995 }
24996 }
24997 }
24998 }
24999
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025000 if (vctxt->nbAttrInfos == 0)
25001 return (0);
25002
25003 /*
25004 * Validate values, create default attributes, evaluate IDCs.
25005 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025006 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025007 iattr = vctxt->attrInfos[i];
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025008 /*
25009 * VAL TODO: Note that we won't try to resolve IDCs to
25010 * "lax" and "skip" validated attributes. Check what to
25011 * do in this case.
25012 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025013 if ((iattr->state != XML_SCHEMAS_ATTR_ASSESSED) &&
25014 (iattr->state != XML_SCHEMAS_ATTR_DEFAULT))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025015 continue;
25016 /*
25017 * VAL TODO: What to do if the type definition is missing?
25018 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025019 if (iattr->typeDef == NULL) {
25020 iattr->state = XML_SCHEMAS_ATTR_ERR_NO_TYPE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025021 continue;
25022 }
25023
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025024 ACTIVATE_ATTRIBUTE(iattr);
Kasimier T. Buchcik828f6542005-06-09 11:23:39 +000025025 fixed = 0;
25026 xpathRes = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025027
25028 if (vctxt->xpathStates != NULL) {
25029 /*
25030 * Evaluate IDCs.
25031 */
25032 xpathRes = xmlSchemaXPathEvaluate(vctxt,
25033 XML_ATTRIBUTE_NODE);
25034 if (xpathRes == -1) {
25035 VERROR_INT("xmlSchemaVAttributesComplex",
25036 "calling xmlSchemaXPathEvaluate()");
25037 goto internal_error;
25038 }
25039 }
25040
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025041 if (iattr->state == XML_SCHEMAS_ATTR_DEFAULT) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025042 /*
25043 * Default/fixed attributes.
25044 */
25045 if (xpathRes) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025046 if (iattr->use->defValue != NULL) {
25047 iattr->value = (xmlChar *) iattr->use->defValue;
25048 iattr->val = iattr->use->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025049 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025050 iattr->value = (xmlChar *) iattr->decl->defValue;
25051 iattr->val = iattr->decl->defVal;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025052 }
25053 /*
25054 * IDCs will consume the precomputed default value,
25055 * so we need to clone it.
25056 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025057 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025058 VERROR_INT("xmlSchemaVAttributesComplex",
25059 "default/fixed value on an attribute use was "
25060 "not precomputed");
25061 goto internal_error;
25062 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025063 iattr->val = xmlSchemaCopyValue(iattr->val);
25064 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025065 VERROR_INT("xmlSchemaVAttributesComplex",
25066 "calling xmlSchemaCopyValue()");
25067 goto internal_error;
25068 }
25069 }
25070 /*
25071 * PSVI: Add the default attribute to the current element.
25072 * VAL TODO: Should we use the *normalized* value? This currently
25073 * uses the *initial* value.
25074 */
25075 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025076 (iattr->node != NULL) && (iattr->node->doc != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025077 xmlChar *normValue;
25078 const xmlChar *value;
25079
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025080 value = iattr->value;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025081 /*
25082 * Normalize the value.
25083 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025084 normValue = xmlSchemaNormalizeValue(iattr->typeDef,
25085 iattr->value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025086 if (normValue != NULL)
25087 value = BAD_CAST normValue;
25088
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025089 if (iattr->nsName == NULL) {
25090 if (xmlNewProp(iattr->node->parent,
25091 iattr->localName, value) == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025092 VERROR_INT("xmlSchemaVAttributesComplex",
25093 "callling xmlNewProp()");
25094 if (normValue != NULL)
25095 xmlFree(normValue);
25096 goto internal_error;
25097 }
25098 } else {
25099 xmlNsPtr ns;
25100
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025101 ns = xmlSearchNsByHref(iattr->node->doc,
25102 iattr->node->parent, iattr->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025103 if (ns == NULL) {
25104 xmlChar prefix[12];
25105 int counter = 0;
25106
25107 /*
25108 * Create a namespace declaration on the validation
25109 * root node if no namespace declaration is in scope.
25110 */
25111 do {
25112 snprintf((char *) prefix, 12, "p%d", counter++);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025113 ns = xmlSearchNs(iattr->node->doc,
25114 iattr->node->parent, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025115 if (counter > 1000) {
25116 VERROR_INT(
25117 "xmlSchemaVAttributesComplex",
25118 "could not compute a ns prefix for a "
25119 "default/fixed attribute");
25120 if (normValue != NULL)
25121 xmlFree(normValue);
25122 goto internal_error;
25123 }
25124 } while (ns != NULL);
25125 ns = xmlNewNs(vctxt->validationRoot,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025126 iattr->nsName, BAD_CAST prefix);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025127 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025128 /*
25129 * TODO:
25130 * http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0406.html
25131 * If we have QNames: do we need to ensure there's a
25132 * prefix defined for the QName?
25133 */
25134 xmlNewNsProp(iattr->node->parent, ns,
25135 iattr->localName, value);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025136 }
25137 if (normValue != NULL)
25138 xmlFree(normValue);
25139 }
25140 /*
25141 * Go directly to IDC evaluation.
25142 */
25143 goto eval_idcs;
25144 }
25145 /*
25146 * Validate the value.
25147 */
25148 if (vctxt->value != NULL) {
25149 /*
25150 * Free last computed value; just for safety reasons.
25151 */
25152 xmlSchemaFreeValue(vctxt->value);
25153 vctxt->value = NULL;
25154 }
25155 /*
25156 * Note that the attribute *use* can be unavailable, if
25157 * the attribute was a wild attribute.
25158 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025159 if ((iattr->decl->flags & XML_SCHEMAS_ATTR_FIXED) ||
25160 ((iattr->use != NULL) &&
25161 (iattr->use->flags & XML_SCHEMAS_ATTR_FIXED)))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025162 fixed = 1;
25163 else
25164 fixed = 0;
25165 /*
25166 * SPEC (cvc-attribute)
25167 * (3) "The item's ·normalized value· must be locally ·valid·
25168 * with respect to that {type definition} as per
25169 * String Valid (§3.14.4)."
25170 *
25171 * VAL TODO: Do we already have the
25172 * "normalized attribute value" here?
25173 */
25174 if (xpathRes || fixed) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025175 iattr->flags |= XML_SCHEMA_NODE_INFO_VALUE_NEEDED;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025176 /*
25177 * Request a computed value.
25178 */
25179 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025180 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025181 iattr->node, iattr->typeDef, iattr->value, &(iattr->val),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025182 1, 1, 0);
25183 } else {
25184 res = xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025185 ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025186 iattr->node, iattr->typeDef, iattr->value, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025187 1, 0, 0);
25188 }
25189
25190 if (res != 0) {
25191 if (res == -1) {
25192 VERROR_INT("xmlSchemaVAttributesComplex",
25193 "calling xmlSchemaStreamValidateSimpleTypeValue()");
25194 goto internal_error;
25195 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025196 iattr->state = XML_SCHEMAS_ATTR_INVALID_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025197 /*
25198 * SPEC PSVI Assessment Outcome (Attribute)
25199 * [validity] = "invalid"
25200 */
25201 goto eval_idcs;
25202 }
25203
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025204 if (fixed) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025205 /*
25206 * SPEC Attribute Locally Valid (Use) (cvc-au)
25207 * "For an attribute information item to be·valid·
25208 * with respect to an attribute use its *normalized*
25209 * value· must match the *canonical* lexical
25210 * representation of the attribute use's {value
25211 * constraint}value, if it is present and fixed."
25212 *
25213 * VAL TODO: The requirement for the *canonical* value
25214 * will be removed in XML Schema 1.1.
25215 */
25216 /*
25217 * SPEC Attribute Locally Valid (cvc-attribute)
25218 * (4) "The item's *actual* value· must match the *value* of
25219 * the {value constraint}, if it is present and fixed."
25220 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025221 if (iattr->val == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025222 /* VAL TODO: A value was not precomputed. */
25223 TODO
25224 goto eval_idcs;
25225 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025226 if ((iattr->use != NULL) &&
25227 (iattr->use->defValue != NULL)) {
25228 if (iattr->use->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025229 /* VAL TODO: A default value was not precomputed. */
25230 TODO
25231 goto eval_idcs;
25232 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025233 iattr->vcValue = iattr->use->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025234 /*
25235 if (xmlSchemaCompareValuesWhtsp(attr->val,
25236 (xmlSchemaWhitespaceValueType) ws,
25237 attr->use->defVal,
25238 (xmlSchemaWhitespaceValueType) ws) != 0) {
25239 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025240 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->use->defVal))
25241 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025242 } else {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025243 if (iattr->decl->defVal == NULL) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025244 /* VAL TODO: A default value was not precomputed. */
25245 TODO
25246 goto eval_idcs;
25247 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025248 iattr->vcValue = iattr->decl->defValue;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025249 /*
25250 if (xmlSchemaCompareValuesWhtsp(attr->val,
25251 (xmlSchemaWhitespaceValueType) ws,
25252 attrDecl->defVal,
25253 (xmlSchemaWhitespaceValueType) ws) != 0) {
25254 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025255 if (! xmlSchemaAreValuesEqual(iattr->val, iattr->decl->defVal))
25256 iattr->state = XML_SCHEMAS_ATTR_ERR_FIXED_VALUE;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025257 }
25258 /*
25259 * [validity] = "valid"
25260 */
25261 }
25262eval_idcs:
25263 /*
25264 * Evaluate IDCs.
25265 */
25266 if (xpathRes) {
25267 if (xmlSchemaXPathProcessHistory(vctxt,
25268 vctxt->depth +1) == -1) {
25269 VERROR_INT("xmlSchemaVAttributesComplex",
25270 "calling xmlSchemaXPathEvaluate()");
25271 goto internal_error;
25272 }
Kasimier T. Buchcik800cbac2005-08-09 12:31:55 +000025273 } else if (vctxt->xpathStates != NULL)
25274 xmlSchemaXPathPop(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025275 }
25276
25277 /*
25278 * Report errors.
25279 */
25280 for (i = 0; i < vctxt->nbAttrInfos; i++) {
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025281 iattr = vctxt->attrInfos[i];
25282 if ((iattr->state == XML_SCHEMAS_ATTR_META) ||
25283 (iattr->state == XML_SCHEMAS_ATTR_ASSESSED) ||
25284 (iattr->state == XML_SCHEMAS_ATTR_WILD_SKIP) ||
25285 (iattr->state == XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025286 continue;
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025287 ACTIVATE_ATTRIBUTE(iattr);
25288 switch (iattr->state) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025289 case XML_SCHEMAS_ATTR_ERR_MISSING: {
25290 xmlChar *str = NULL;
25291 ACTIVATE_ELEM;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025292 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025293 XML_SCHEMAV_CVC_COMPLEX_TYPE_4, NULL, NULL,
25294 "The attribute '%s' is required but missing",
25295 xmlSchemaFormatQName(&str,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025296 iattr->decl->targetNamespace,
25297 iattr->decl->name),
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025298 NULL);
25299 FREE_AND_NULL(str)
25300 break;
25301 }
25302 case XML_SCHEMAS_ATTR_ERR_NO_TYPE:
25303 VERROR(XML_SCHEMAV_CVC_ATTRIBUTE_2, NULL,
25304 "The type definition is absent");
25305 break;
25306 case XML_SCHEMAS_ATTR_ERR_FIXED_VALUE:
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025307 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025308 XML_SCHEMAV_CVC_AU, NULL, NULL,
25309 "The value '%s' does not match the fixed "
25310 "value constraint '%s'",
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025311 iattr->value, iattr->vcValue);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025312 break;
25313 case XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL:
25314 VERROR(XML_SCHEMAV_CVC_WILDCARD, NULL,
25315 "No matching global attribute declaration available, but "
25316 "demanded by the strict wildcard");
25317 break;
25318 case XML_SCHEMAS_ATTR_UNKNOWN:
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025319 if (iattr->metaType)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025320 break;
25321 /*
25322 * MAYBE VAL TODO: One might report different error messages
25323 * for the following errors.
25324 */
25325 if (type->attributeWildcard == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025326 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025327 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_1, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025328 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025329 xmlSchemaIllegalAttrErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025330 XML_SCHEMAV_CVC_COMPLEX_TYPE_3_2_2, iattr, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025331 }
25332 break;
25333 default:
25334 break;
25335 }
25336 }
25337
25338 ACTIVATE_ELEM;
25339 return (0);
25340internal_error:
25341 ACTIVATE_ELEM;
25342 return (-1);
25343}
25344
25345static int
25346xmlSchemaValidateElemWildcard(xmlSchemaValidCtxtPtr vctxt,
25347 int *skip)
25348{
25349 xmlSchemaWildcardPtr wild = (xmlSchemaWildcardPtr) vctxt->inode->decl;
25350 /*
25351 * The namespace of the element was already identified to be
25352 * matching the wildcard.
25353 */
25354 if ((skip == NULL) || (wild == NULL) ||
25355 (wild->type != XML_SCHEMA_TYPE_ANY)) {
25356 VERROR_INT("xmlSchemaValidateElemWildcard",
25357 "bad arguments");
25358 return (-1);
25359 }
25360 *skip = 0;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025361 if (wild->processContents == XML_SCHEMAS_ANY_SKIP) {
25362 /*
25363 * URGENT VAL TODO: Either we need to position the stream to the
25364 * next sibling, or walk the whole subtree.
25365 */
25366 *skip = 1;
25367 return (0);
25368 }
25369 {
25370 xmlSchemaElementPtr decl = NULL;
25371
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025372 decl = xmlSchemaGetElem(vctxt->schema,
25373 vctxt->inode->localName, vctxt->inode->nsName);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025374 if (decl != NULL) {
25375 vctxt->inode->decl = decl;
25376 return (0);
25377 }
25378 }
25379 if (wild->processContents == XML_SCHEMAS_ANY_STRICT) {
25380 /* VAL TODO: Change to proper error code. */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025381 VERROR(XML_SCHEMAV_CVC_ELT_1, NULL, /* WXS_BASIC_CAST wild */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025382 "No matching global element declaration available, but "
25383 "demanded by the strict wildcard");
25384 return (vctxt->err);
25385 }
25386 if (vctxt->nbAttrInfos != 0) {
25387 xmlSchemaAttrInfoPtr iattr;
25388 /*
25389 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25390 * (1.2.1.2.1) - (1.2.1.2.3 )
25391 *
25392 * Use the xsi:type attribute for the type definition.
25393 */
25394 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
25395 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
25396 if (iattr != NULL) {
25397 if (xmlSchemaProcessXSIType(vctxt, iattr,
25398 &(vctxt->inode->typeDef), NULL) == -1) {
25399 VERROR_INT("xmlSchemaValidateElemWildcard",
25400 "calling xmlSchemaProcessXSIType() to "
25401 "process the attribute 'xsi:nil'");
25402 return (-1);
25403 }
25404 /*
25405 * Don't return an error on purpose.
25406 */
25407 return (0);
25408 }
25409 }
25410 /*
25411 * SPEC Validation Rule: Schema-Validity Assessment (Element)
25412 *
25413 * Fallback to "anyType".
25414 */
25415 vctxt->inode->typeDef =
25416 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
25417 return (0);
25418}
25419
25420/*
25421* xmlSchemaCheckCOSValidDefault:
25422*
25423* This will be called if: not nilled, no content and a default/fixed
25424* value is provided.
25425*/
25426
25427static int
25428xmlSchemaCheckCOSValidDefault(xmlSchemaValidCtxtPtr vctxt,
25429 const xmlChar *value,
25430 xmlSchemaValPtr *val)
25431{
25432 int ret = 0;
25433 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25434
25435 /*
25436 * cos-valid-default:
25437 * Schema Component Constraint: Element Default Valid (Immediate)
25438 * For a string to be a valid default with respect to a type
25439 * definition the appropriate case among the following must be true:
25440 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025441 if WXS_IS_COMPLEX(inode->typeDef) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025442 /*
25443 * Complex type.
25444 *
25445 * SPEC (2.1) "its {content type} must be a simple type definition
25446 * or mixed."
25447 * SPEC (2.2.2) "If the {content type} is mixed, then the {content
25448 * type}'s particle must be ·emptiable· as defined by
25449 * Particle Emptiable (§3.9.6)."
25450 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025451 if ((! WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) &&
25452 ((! WXS_HAS_MIXED_CONTENT(inode->typeDef)) ||
25453 (! WXS_EMPTIABLE(inode->typeDef)))) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025454 ret = XML_SCHEMAP_COS_VALID_DEFAULT_2_1;
25455 /* NOTE that this covers (2.2.2) as well. */
25456 VERROR(ret, NULL,
25457 "For a string to be a valid default, the type definition "
25458 "must be a simple type or a complex type with simple content "
25459 "or mixed content and a particle emptiable");
25460 return(ret);
25461 }
25462 }
25463 /*
25464 * 1 If the type definition is a simple type definition, then the string
25465 * must be ·valid· with respect to that definition as defined by String
25466 * Valid (§3.14.4).
25467 *
25468 * AND
25469 *
25470 * 2.2.1 If the {content type} is a simple type definition, then the
25471 * string must be ·valid· with respect to that simple type definition
25472 * as defined by String Valid (§3.14.4).
25473 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025474 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025475
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025476 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025477 NULL, inode->typeDef, value, val, 1, 1, 0);
25478
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025479 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025480
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025481 ret = xmlSchemaVCheckCVCSimpleType(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025482 NULL, inode->typeDef->contentTypeDef, value, val, 1, 1, 0);
25483 }
25484 if (ret < 0) {
25485 VERROR_INT("xmlSchemaCheckCOSValidDefault",
25486 "calling xmlSchemaVCheckCVCSimpleType()");
25487 }
25488 return (ret);
25489}
25490
25491static void
25492xmlSchemaVContentModelCallback(xmlSchemaValidCtxtPtr vctxt ATTRIBUTE_UNUSED,
25493 const xmlChar * name ATTRIBUTE_UNUSED,
25494 xmlSchemaElementPtr item,
25495 xmlSchemaNodeInfoPtr inode)
25496{
25497 inode->decl = item;
25498#ifdef DEBUG_CONTENT
25499 {
25500 xmlChar *str = NULL;
25501
25502 if (item->type == XML_SCHEMA_TYPE_ELEMENT) {
25503 xmlGenericError(xmlGenericErrorContext,
25504 "AUTOMATON callback for '%s' [declaration]\n",
25505 xmlSchemaFormatQName(&str,
25506 inode->localName, inode->nsName));
25507 } else {
25508 xmlGenericError(xmlGenericErrorContext,
25509 "AUTOMATON callback for '%s' [wildcard]\n",
25510 xmlSchemaFormatQName(&str,
25511 inode->localName, inode->nsName));
25512
25513 }
25514 FREE_AND_NULL(str)
25515 }
25516#endif
25517}
25518
25519static int
25520xmlSchemaValidatorPushElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025521{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025522 vctxt->inode = xmlSchemaGetFreshElemInfo(vctxt);
25523 if (vctxt->inode == NULL) {
25524 VERROR_INT("xmlSchemaValidatorPushElem",
25525 "calling xmlSchemaGetFreshElemInfo()");
25526 return (-1);
25527 }
25528 vctxt->nbAttrInfos = 0;
25529 return (0);
25530}
25531
25532static int
25533xmlSchemaVCheckINodeDataType(xmlSchemaValidCtxtPtr vctxt,
25534 xmlSchemaNodeInfoPtr inode,
25535 xmlSchemaTypePtr type,
25536 const xmlChar *value)
25537{
25538 if (inode->flags & XML_SCHEMA_NODE_INFO_VALUE_NEEDED)
25539 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025540 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025541 type, value, &(inode->val), 1, 1, 0));
25542 else
25543 return (xmlSchemaVCheckCVCSimpleType(
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025544 ACTXT_CAST vctxt, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025545 type, value, NULL, 1, 0, 0));
25546}
25547
25548
25549
25550/*
25551* Process END of element.
25552*/
25553static int
25554xmlSchemaValidatorPopElem(xmlSchemaValidCtxtPtr vctxt)
25555{
25556 int ret = 0;
25557 xmlSchemaNodeInfoPtr inode = vctxt->inode;
25558
25559 if (vctxt->nbAttrInfos != 0)
25560 xmlSchemaClearAttrInfos(vctxt);
25561 if (inode->flags & XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED) {
25562 /*
25563 * This element was not expected;
25564 * we will not validate child elements of broken parents.
25565 * Skip validation of all content of the parent.
25566 */
25567 vctxt->skipDepth = vctxt->depth -1;
25568 goto end_elem;
25569 }
25570 if ((inode->typeDef == NULL) ||
25571 (inode->flags & XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE)) {
25572 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000025573 * 1. the type definition might be missing if the element was
25574 * error prone
25575 * 2. it might be abstract.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025576 */
25577 goto end_elem;
25578 }
25579 /*
25580 * Check the content model.
25581 */
25582 if ((inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) ||
25583 (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)) {
25584
25585 /*
25586 * Workaround for "anyType".
25587 */
25588 if (inode->typeDef->builtInType == XML_SCHEMAS_ANYTYPE)
25589 goto character_content;
25590
25591 if ((inode->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) == 0) {
25592 xmlChar *values[10];
25593 int terminal, nbval = 10, nbneg;
25594
25595 if (inode->regexCtxt == NULL) {
25596 /*
25597 * Create the regex context.
25598 */
25599 inode->regexCtxt =
25600 xmlRegNewExecCtxt(inode->typeDef->contModel,
25601 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
25602 vctxt);
25603 if (inode->regexCtxt == NULL) {
25604 VERROR_INT("xmlSchemaValidatorPopElem",
25605 "failed to create a regex context");
25606 goto internal_error;
25607 }
25608#ifdef DEBUG_AUTOMATA
25609 xmlGenericError(xmlGenericErrorContext,
25610 "AUTOMATON create on '%s'\n", inode->localName);
25611#endif
25612 }
25613 /*
25614 * Get hold of the still expected content, since a further
25615 * call to xmlRegExecPushString() will loose this information.
25616 */
25617 xmlRegExecNextValues(inode->regexCtxt,
25618 &nbval, &nbneg, &values[0], &terminal);
25619 ret = xmlRegExecPushString(inode->regexCtxt, NULL, NULL);
25620 if (ret <= 0) {
25621 /*
25622 * Still missing something.
25623 */
25624 ret = 1;
25625 inode->flags |=
25626 XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025627 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025628 XML_SCHEMAV_ELEMENT_CONTENT, NULL, NULL,
25629 "Missing child element(s)",
25630 nbval, nbneg, values);
25631#ifdef DEBUG_AUTOMATA
25632 xmlGenericError(xmlGenericErrorContext,
25633 "AUTOMATON missing ERROR on '%s'\n",
25634 inode->localName);
25635#endif
25636 } else {
25637 /*
25638 * Content model is satisfied.
25639 */
25640 ret = 0;
25641#ifdef DEBUG_AUTOMATA
25642 xmlGenericError(xmlGenericErrorContext,
25643 "AUTOMATON succeeded on '%s'\n",
25644 inode->localName);
25645#endif
25646 }
25647
25648 }
25649 }
25650 if (inode->typeDef->contentType == XML_SCHEMA_CONTENT_ELEMENTS)
25651 goto end_elem;
25652
25653character_content:
25654
25655 if (vctxt->value != NULL) {
25656 xmlSchemaFreeValue(vctxt->value);
25657 vctxt->value = NULL;
25658 }
25659 /*
25660 * Check character content.
25661 */
25662 if (inode->decl == NULL) {
25663 /*
25664 * Speedup if no declaration exists.
25665 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025666 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025667 ret = xmlSchemaVCheckINodeDataType(vctxt,
25668 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025669 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025670 ret = xmlSchemaVCheckINodeDataType(vctxt,
25671 inode, inode->typeDef->contentTypeDef,
25672 inode->value);
25673 }
25674 if (ret < 0) {
25675 VERROR_INT("xmlSchemaValidatorPopElem",
25676 "calling xmlSchemaVCheckCVCSimpleType()");
25677 goto internal_error;
25678 }
25679 goto end_elem;
25680 }
25681 /*
25682 * cvc-elt (3.3.4) : 5
25683 * The appropriate case among the following must be true:
25684 */
25685 /*
25686 * cvc-elt (3.3.4) : 5.1
25687 * If the declaration has a {value constraint},
25688 * the item has neither element nor character [children] and
25689 * clause 3.2 has not applied, then all of the following must be true:
25690 */
25691 if ((inode->decl->value != NULL) &&
25692 (inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY) &&
25693 (! INODE_NILLED(inode))) {
25694 /*
25695 * cvc-elt (3.3.4) : 5.1.1
25696 * If the ·actual type definition· is a ·local type definition·
25697 * then the canonical lexical representation of the {value constraint}
25698 * value must be a valid default for the ·actual type definition· as
25699 * defined in Element Default Valid (Immediate) (§3.3.6).
25700 */
25701 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025702 * NOTE: 'local' above means types acquired by xsi:type.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025703 * NOTE: Although the *canonical* value is stated, it is not
25704 * relevant if canonical or not. Additionally XML Schema 1.1
25705 * will removed this requirement as well.
25706 */
25707 if (inode->flags & XML_SCHEMA_ELEM_INFO_LOCAL_TYPE) {
25708
25709 ret = xmlSchemaCheckCOSValidDefault(vctxt,
25710 inode->decl->value, &(inode->val));
25711 if (ret != 0) {
25712 if (ret < 0) {
25713 VERROR_INT("xmlSchemaValidatorPopElem",
25714 "calling xmlSchemaCheckCOSValidDefault()");
25715 goto internal_error;
25716 }
25717 goto end_elem;
25718 }
25719 /*
25720 * Stop here, to avoid redundant validation of the value
25721 * (see following).
25722 */
25723 goto default_psvi;
25724 }
25725 /*
25726 * cvc-elt (3.3.4) : 5.1.2
25727 * The element information item with the canonical lexical
25728 * representation of the {value constraint} value used as its
25729 * ·normalized value· must be ·valid· with respect to the
25730 * ·actual type definition· as defined by Element Locally Valid (Type)
25731 * (§3.3.4).
25732 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025733 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025734 ret = xmlSchemaVCheckINodeDataType(vctxt,
25735 inode, inode->typeDef, inode->decl->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025736 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025737 ret = xmlSchemaVCheckINodeDataType(vctxt,
25738 inode, inode->typeDef->contentTypeDef,
25739 inode->decl->value);
25740 }
25741 if (ret != 0) {
25742 if (ret < 0) {
25743 VERROR_INT("xmlSchemaValidatorPopElem",
25744 "calling xmlSchemaVCheckCVCSimpleType()");
25745 goto internal_error;
25746 }
25747 goto end_elem;
25748 }
25749
25750default_psvi:
25751 /*
25752 * PSVI: Create a text node on the instance element.
25753 */
25754 if ((vctxt->options & XML_SCHEMA_VAL_VC_I_CREATE) &&
25755 (inode->node != NULL)) {
25756 xmlNodePtr textChild;
25757 xmlChar *normValue;
25758 /*
25759 * VAL TODO: Normalize the value.
25760 */
25761 normValue = xmlSchemaNormalizeValue(inode->typeDef,
25762 inode->decl->value);
25763 if (normValue != NULL) {
25764 textChild = xmlNewText(BAD_CAST normValue);
25765 xmlFree(normValue);
25766 } else
25767 textChild = xmlNewText(inode->decl->value);
25768 if (textChild == NULL) {
25769 VERROR_INT("xmlSchemaValidatorPopElem",
25770 "calling xmlNewText()");
25771 goto internal_error;
25772 } else
25773 xmlAddChild(inode->node, textChild);
25774 }
25775
25776 } else if (! INODE_NILLED(inode)) {
25777 /*
25778 * 5.2.1 The element information item must be ·valid· with respect
25779 * to the ·actual type definition· as defined by Element Locally
25780 * Valid (Type) (§3.3.4).
25781 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025782 if (WXS_IS_SIMPLE(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025783 /*
25784 * SPEC (cvc-type) (3.1)
25785 * "If the type definition is a simple type definition, ..."
25786 * (3.1.3) "If clause 3.2 of Element Locally Valid
25787 * (Element) (§3.3.4) did not apply, then the ·normalized value·
25788 * must be ·valid· with respect to the type definition as defined
25789 * by String Valid (§3.14.4).
25790 */
25791 ret = xmlSchemaVCheckINodeDataType(vctxt,
25792 inode, inode->typeDef, inode->value);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025793 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025794 /*
25795 * SPEC (cvc-type) (3.2) "If the type definition is a complex type
25796 * definition, then the element information item must be
25797 * ·valid· with respect to the type definition as per
25798 * Element Locally Valid (Complex Type) (§3.4.4);"
25799 *
25800 * SPEC (cvc-complex-type) (2.2)
25801 * "If the {content type} is a simple type definition, ...
25802 * the ·normalized value· of the element information item is
25803 * ·valid· with respect to that simple type definition as
25804 * defined by String Valid (§3.14.4)."
25805 */
25806 ret = xmlSchemaVCheckINodeDataType(vctxt,
25807 inode, inode->typeDef->contentTypeDef, inode->value);
25808 }
25809 if (ret != 0) {
25810 if (ret < 0) {
25811 VERROR_INT("xmlSchemaValidatorPopElem",
25812 "calling xmlSchemaVCheckCVCSimpleType()");
25813 goto internal_error;
25814 }
25815 goto end_elem;
25816 }
25817 /*
25818 * 5.2.2 If there is a fixed {value constraint} and clause 3.2 has
25819 * not applied, all of the following must be true:
25820 */
25821 if ((inode->decl->value != NULL) &&
25822 (inode->decl->flags & XML_SCHEMAS_ELEM_FIXED)) {
25823
25824 /*
25825 * TODO: We will need a computed value, when comparison is
25826 * done on computed values.
25827 */
25828 /*
25829 * 5.2.2.1 The element information item must have no element
25830 * information item [children].
25831 */
25832 if (inode->flags &
25833 XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT) {
25834 ret = XML_SCHEMAV_CVC_ELT_5_2_2_1;
25835 VERROR(ret, NULL,
25836 "The content must not containt element nodes since "
25837 "there is a fixed value constraint");
25838 goto end_elem;
25839 } else {
25840 /*
25841 * 5.2.2.2 The appropriate case among the following must
25842 * be true:
25843 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025844 if (WXS_HAS_MIXED_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025845 /*
25846 * 5.2.2.2.1 If the {content type} of the ·actual type
25847 * definition· is mixed, then the *initial value* of the
25848 * item must match the canonical lexical representation
25849 * of the {value constraint} value.
25850 *
25851 * ... the *initial value* of an element information
25852 * item is the string composed of, in order, the
25853 * [character code] of each character information item in
25854 * the [children] of that element information item.
25855 */
25856 if (! xmlStrEqual(inode->value, inode->decl->value)){
25857 /*
25858 * VAL TODO: Report invalid & expected values as well.
25859 * VAL TODO: Implement the canonical stuff.
25860 */
25861 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025862 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025863 ret, NULL, NULL,
25864 "The initial value '%s' does not match the fixed "
25865 "value constraint '%s'",
25866 inode->value, inode->decl->value);
25867 goto end_elem;
25868 }
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000025869 } else if (WXS_HAS_SIMPLE_CONTENT(inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025870 /*
25871 * 5.2.2.2.2 If the {content type} of the ·actual type
25872 * definition· is a simple type definition, then the
25873 * *actual value* of the item must match the canonical
25874 * lexical representation of the {value constraint} value.
25875 */
25876 /*
25877 * VAL TODO: *actual value* is the normalized value, impl.
25878 * this.
25879 * VAL TODO: Report invalid & expected values as well.
25880 * VAL TODO: Implement a comparison with the computed values.
25881 */
25882 if (! xmlStrEqual(inode->value,
25883 inode->decl->value)) {
25884 ret = XML_SCHEMAV_CVC_ELT_5_2_2_2_2;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000025885 xmlSchemaCustomErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025886 ret, NULL, NULL,
25887 "The actual value '%s' does not match the fixed "
25888 "value constraint '%s'",
25889 inode->value,
25890 inode->decl->value);
25891 goto end_elem;
25892 }
25893 }
25894 }
25895 }
25896 }
25897
25898end_elem:
25899 if (vctxt->depth < 0) {
25900 /* TODO: raise error? */
25901 return (0);
25902 }
25903 if (vctxt->depth == vctxt->skipDepth)
25904 vctxt->skipDepth = -1;
25905 /*
25906 * Evaluate the history of XPath state objects.
25907 */
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000025908 if (inode->appliedXPath &&
25909 (xmlSchemaXPathProcessHistory(vctxt, vctxt->depth) == -1))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025910 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025911 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025912 * MAYBE TODO:
25913 * SPEC (6) "The element information item must be ·valid· with
25914 * respect to each of the {identity-constraint definitions} as per
25915 * Identity-constraint Satisfied (§3.11.4)."
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025916 */
25917 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025918 * PSVI TODO: If we expose IDC node-tables via PSVI then the tables
25919 * need to be built in any case.
25920 * We will currently build IDC node-tables and bubble them only if
25921 * keyrefs do exist.
25922 */
25923
25924 /*
25925 * Add the current IDC target-nodes to the IDC node-tables.
25926 */
25927 if ((inode->idcMatchers != NULL) &&
25928 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
25929 {
25930 if (xmlSchemaIDCFillNodeTables(vctxt, inode) == -1)
25931 goto internal_error;
25932 }
25933 /*
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025934 * Validate IDC keyrefs.
25935 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025936 if (vctxt->inode->hasKeyrefs)
25937 if (xmlSchemaCheckCVCIDCKeyRef(vctxt) == -1)
25938 goto internal_error;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025939 /*
25940 * Merge/free the IDC table.
25941 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025942 if (inode->idcTable != NULL) {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025943#ifdef DEBUG_IDC_NODE_TABLE
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025944 xmlSchemaDebugDumpIDCTable(stdout,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025945 inode->nsName,
25946 inode->localName,
25947 inode->idcTable);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025948#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025949 if ((vctxt->depth > 0) &&
25950 (vctxt->hasKeyrefs || vctxt->createIDCNodeTables))
25951 {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025952 /*
25953 * Merge the IDC node table with the table of the parent node.
25954 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025955 if (xmlSchemaBubbleIDCNodeTables(vctxt) == -1)
25956 goto internal_error;
25957 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025958 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025959 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025960 * Clear the current ielem.
25961 * VAL TODO: Don't free the PSVI IDC tables if they are
25962 * requested for the PSVI.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025963 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025964 xmlSchemaClearElemInfo(inode);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025965 /*
25966 * Skip further processing if we are on the validation root.
25967 */
25968 if (vctxt->depth == 0) {
25969 vctxt->depth--;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025970 vctxt->inode = NULL;
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000025971 return (0);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025972 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025973 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025974 * Reset the keyrefDepth if needed.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025975 */
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025976 if (vctxt->aidcs != NULL) {
25977 xmlSchemaIDCAugPtr aidc = vctxt->aidcs;
25978 do {
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025979 if (aidc->keyrefDepth == vctxt->depth) {
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025980 /*
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025981 * A 'keyrefDepth' of a key/unique IDC matches the current
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025982 * depth, this means that we are leaving the scope of the
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025983 * top-most keyref IDC which refers to this IDC.
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025984 */
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000025985 aidc->keyrefDepth = -1;
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000025986 }
25987 aidc = aidc->next;
25988 } while (aidc != NULL);
25989 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025990 vctxt->depth--;
25991 vctxt->inode = vctxt->elemInfos[vctxt->depth];
Kasimier T. Buchcik25799ce2005-02-15 14:39:48 +000025992 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025993 * VAL TODO: 7 If the element information item is the ·validation root·, it must be
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000025994 * ·valid· per Validation Root Valid (ID/IDREF) (§3.3.4).
25995 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000025996 return (ret);
25997
25998internal_error:
25999 vctxt->err = -1;
26000 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026001}
26002
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026003/*
26004* 3.4.4 Complex Type Definition Validation Rules
26005* Validation Rule: Element Locally Valid (Complex Type) (cvc-complex-type)
26006*/
Daniel Veillardc0826a72004-08-10 14:17:33 +000026007static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026008xmlSchemaValidateChildElem(xmlSchemaValidCtxtPtr vctxt)
William M. Brack2f2a6632004-08-20 23:09:47 +000026009{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026010 xmlSchemaNodeInfoPtr pielem;
26011 xmlSchemaTypePtr ptype;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026012 int ret = 0;
Daniel Veillard3646d642004-06-02 19:19:14 +000026013
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026014 if (vctxt->depth <= 0) {
26015 VERROR_INT("xmlSchemaValidateChildElem",
26016 "not intended for the validation root");
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026017 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026018 }
26019 pielem = vctxt->elemInfos[vctxt->depth -1];
26020 if (pielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26021 pielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026022 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026023 * Handle 'nilled' elements.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026024 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026025 if (INODE_NILLED(pielem)) {
26026 /*
26027 * SPEC (cvc-elt) (3.3.4) : (3.2.1)
26028 */
26029 ACTIVATE_PARENT_ELEM;
26030 ret = XML_SCHEMAV_CVC_ELT_3_2_1;
26031 VERROR(ret, NULL,
26032 "Neither character nor element content is allowed, "
26033 "because the element was 'nilled'");
26034 ACTIVATE_ELEM;
26035 goto unexpected_elem;
26036 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026037
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026038 ptype = pielem->typeDef;
26039
26040 if (ptype->builtInType == XML_SCHEMAS_ANYTYPE) {
26041 /*
26042 * Workaround for "anyType": we have currently no content model
26043 * assigned for "anyType", so handle it explicitely.
26044 * "anyType" has an unbounded, lax "any" wildcard.
26045 */
26046 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26047 vctxt->inode->localName,
26048 vctxt->inode->nsName);
26049
26050 if (vctxt->inode->decl == NULL) {
26051 xmlSchemaAttrInfoPtr iattr;
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026052 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026053 * Process "xsi:type".
26054 * SPEC (cvc-assess-elt) (1.2.1.2.1) - (1.2.1.2.3)
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026055 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026056 iattr = xmlSchemaGetMetaAttrInfo(vctxt,
26057 XML_SCHEMA_ATTR_INFO_META_XSI_TYPE);
26058 if (iattr != NULL) {
26059 ret = xmlSchemaProcessXSIType(vctxt, iattr,
26060 &(vctxt->inode->typeDef), NULL);
26061 if (ret != 0) {
26062 if (ret == -1) {
26063 VERROR_INT("xmlSchemaValidateChildElem",
26064 "calling xmlSchemaProcessXSIType() to "
26065 "process the attribute 'xsi:nil'");
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000026066 return (-1);
Kasimier T. Buchcik187ea5f2005-04-19 11:25:43 +000026067 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026068 return (ret);
Daniel Veillard01fa6152004-06-29 17:04:39 +000026069 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026070 } else {
26071 /*
26072 * Fallback to "anyType".
26073 *
26074 * SPEC (cvc-assess-elt)
26075 * "If the item cannot be ·strictly assessed·, [...]
26076 * an element information item's schema validity may be laxly
26077 * assessed if its ·context-determined declaration· is not
26078 * skip by ·validating· with respect to the ·ur-type
26079 * definition· as per Element Locally Valid (Type) (§3.3.4)."
26080 */
26081 vctxt->inode->typeDef =
26082 xmlSchemaGetBuiltInType(XML_SCHEMAS_ANYTYPE);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026083 }
26084 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026085 return (0);
26086 }
26087
26088 switch (ptype->contentType) {
26089 case XML_SCHEMA_CONTENT_EMPTY:
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026090 /*
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026091 * SPEC (2.1) "If the {content type} is empty, then the
26092 * element information item has no character or element
26093 * information item [children]."
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026094 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026095 ACTIVATE_PARENT_ELEM
26096 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1;
26097 VERROR(ret, NULL,
26098 "Element content is not allowed, "
26099 "because the content type is empty");
26100 ACTIVATE_ELEM
26101 goto unexpected_elem;
26102 break;
26103
26104 case XML_SCHEMA_CONTENT_MIXED:
26105 case XML_SCHEMA_CONTENT_ELEMENTS: {
26106 xmlRegExecCtxtPtr regexCtxt;
26107 xmlChar *values[10];
26108 int terminal, nbval = 10, nbneg;
26109
26110 /* VAL TODO: Optimized "anyType" validation.*/
26111
26112 if (ptype->contModel == NULL) {
26113 VERROR_INT("xmlSchemaValidateChildElem",
26114 "type has elem content but no content model");
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000026115 return (-1);
Kasimier T. Buchcik478d6932005-03-16 16:29:18 +000026116 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026117 /*
26118 * Safety belf for evaluation if the cont. model was already
26119 * examined to be invalid.
26120 */
26121 if (pielem->flags & XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT) {
26122 VERROR_INT("xmlSchemaValidateChildElem",
26123 "validating elem, but elem content is already invalid");
26124 return (-1);
26125 }
Kasimier T. Buchcikc3af19d2005-01-13 12:10:11 +000026126
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026127 regexCtxt = pielem->regexCtxt;
26128 if (regexCtxt == NULL) {
26129 /*
26130 * Create the regex context.
26131 */
26132 regexCtxt = xmlRegNewExecCtxt(ptype->contModel,
26133 (xmlRegExecCallbacks) xmlSchemaVContentModelCallback,
26134 vctxt);
26135 if (regexCtxt == NULL) {
26136 VERROR_INT("xmlSchemaValidateChildElem",
26137 "failed to create a regex context");
26138 return (-1);
26139 }
26140 pielem->regexCtxt = regexCtxt;
26141#ifdef DEBUG_AUTOMATA
26142 xmlGenericError(xmlGenericErrorContext, "AUTOMATA create on '%s'\n",
26143 pielem->localName);
26144#endif
26145 }
26146
26147 /*
26148 * SPEC (2.4) "If the {content type} is element-only or mixed,
26149 * then the sequence of the element information item's
26150 * element information item [children], if any, taken in
26151 * order, is ·valid· with respect to the {content type}'s
26152 * particle, as defined in Element Sequence Locally Valid
26153 * (Particle) (§3.9.4)."
26154 */
26155 ret = xmlRegExecPushString2(regexCtxt,
26156 vctxt->inode->localName,
26157 vctxt->inode->nsName,
26158 vctxt->inode);
26159#ifdef DEBUG_AUTOMATA
26160 if (ret < 0)
26161 xmlGenericError(xmlGenericErrorContext,
26162 "AUTOMATON push ERROR for '%s' on '%s'\n",
26163 vctxt->inode->localName, pielem->localName);
26164 else
26165 xmlGenericError(xmlGenericErrorContext,
26166 "AUTOMATON push OK for '%s' on '%s'\n",
26167 vctxt->inode->localName, pielem->localName);
26168#endif
26169 if (vctxt->err == XML_SCHEMAV_INTERNAL) {
26170 VERROR_INT("xmlSchemaValidateChildElem",
26171 "calling xmlRegExecPushString2()");
26172 return (-1);
26173 }
26174 if (ret < 0) {
26175 xmlRegExecErrInfo(regexCtxt, NULL, &nbval, &nbneg,
26176 &values[0], &terminal);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026177 xmlSchemaComplexTypeErr(ACTXT_CAST vctxt,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026178 XML_SCHEMAV_ELEMENT_CONTENT, NULL,NULL,
26179 "This element is not expected",
26180 nbval, nbneg, values);
26181 ret = vctxt->err;
26182 goto unexpected_elem;
26183 } else
26184 ret = 0;
Daniel Veillard01fa6152004-06-29 17:04:39 +000026185 }
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026186 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026187 case XML_SCHEMA_CONTENT_SIMPLE:
26188 case XML_SCHEMA_CONTENT_BASIC:
26189 ACTIVATE_PARENT_ELEM
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026190 if (WXS_IS_COMPLEX(ptype)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026191 /*
26192 * SPEC (cvc-complex-type) (2.2)
26193 * "If the {content type} is a simple type definition, then
26194 * the element information item has no element information
26195 * item [children], ..."
26196 */
26197 ret = XML_SCHEMAV_CVC_COMPLEX_TYPE_2_2;
26198 VERROR(ret, NULL, "Element content is not allowed, "
26199 "because the content type is a simple type definition");
26200 } else {
26201 /*
26202 * SPEC (cvc-type) (3.1.2) "The element information item must
26203 * have no element information item [children]."
26204 */
26205 ret = XML_SCHEMAV_CVC_TYPE_3_1_2;
26206 VERROR(ret, NULL, "Element content is not allowed, "
26207 "because the type definition is simple");
26208 }
26209 ACTIVATE_ELEM
26210 ret = vctxt->err;
26211 goto unexpected_elem;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026212 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026213
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026214 default:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026215 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026216 }
26217 return (ret);
26218unexpected_elem:
26219 /*
26220 * Pop this element and set the skipDepth to skip
26221 * all further content of the parent element.
26222 */
26223 vctxt->skipDepth = vctxt->depth;
26224 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED;
26225 pielem->flags |= XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT;
26226 return (ret);
26227}
26228
26229#define XML_SCHEMA_PUSH_TEXT_PERSIST 1
26230#define XML_SCHEMA_PUSH_TEXT_CREATED 2
26231#define XML_SCHEMA_PUSH_TEXT_VOLATILE 3
26232
26233static int
26234xmlSchemaVPushText(xmlSchemaValidCtxtPtr vctxt,
26235 int nodeType, const xmlChar *value, int len,
26236 int mode, int *consumed)
26237{
26238 /*
26239 * Unfortunately we have to duplicate the text sometimes.
26240 * OPTIMIZE: Maybe we could skip it, if:
26241 * 1. content type is simple
26242 * 2. whitespace is "collapse"
26243 * 3. it consists of whitespace only
26244 *
26245 * Process character content.
26246 */
26247 if (consumed != NULL)
26248 *consumed = 0;
26249 if (INODE_NILLED(vctxt->inode)) {
26250 /*
26251 * SPEC cvc-elt (3.3.4 - 3.2.1)
26252 * "The element information item must have no character or
26253 * element information item [children]."
26254 */
26255 VERROR(XML_SCHEMAV_CVC_ELT_3_2_1, NULL,
26256 "Neither character nor element content is allowed "
26257 "because the element is 'nilled'");
26258 return (vctxt->err);
26259 }
26260 /*
26261 * SPEC (2.1) "If the {content type} is empty, then the
26262 * element information item has no character or element
26263 * information item [children]."
26264 */
26265 if (vctxt->inode->typeDef->contentType ==
26266 XML_SCHEMA_CONTENT_EMPTY) {
26267 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_1, NULL,
26268 "Character content is not allowed, "
26269 "because the content type is empty");
26270 return (vctxt->err);
26271 }
26272
26273 if (vctxt->inode->typeDef->contentType ==
26274 XML_SCHEMA_CONTENT_ELEMENTS) {
26275 if ((nodeType != XML_TEXT_NODE) ||
26276 (! xmlSchemaIsBlank((xmlChar *) value, len))) {
26277 /*
26278 * SPEC cvc-complex-type (2.3)
26279 * "If the {content type} is element-only, then the
26280 * element information item has no character information
26281 * item [children] other than those whose [character
26282 * code] is defined as a white space in [XML 1.0 (Second
26283 * Edition)]."
26284 */
26285 VERROR(XML_SCHEMAV_CVC_COMPLEX_TYPE_2_3, NULL,
26286 "Character content other than whitespace is not allowed "
26287 "because the content type is 'element-only'");
26288 return (vctxt->err);
26289 }
26290 return (0);
26291 }
26292
26293 if ((value == NULL) || (value[0] == 0))
26294 return (0);
26295 /*
26296 * Save the value.
26297 * NOTE that even if the content type is *mixed*, we need the
26298 * *initial value* for default/fixed value constraints.
26299 */
26300 if ((vctxt->inode->typeDef->contentType == XML_SCHEMA_CONTENT_MIXED) &&
26301 ((vctxt->inode->decl == NULL) ||
26302 (vctxt->inode->decl->value == NULL)))
26303 return (0);
26304
26305 if (vctxt->inode->value == NULL) {
26306 /*
26307 * Set the value.
26308 */
26309 switch (mode) {
26310 case XML_SCHEMA_PUSH_TEXT_PERSIST:
26311 /*
26312 * When working on a tree.
26313 */
26314 vctxt->inode->value = value;
26315 break;
26316 case XML_SCHEMA_PUSH_TEXT_CREATED:
26317 /*
26318 * When working with the reader.
26319 * The value will be freed by the element info.
26320 */
26321 vctxt->inode->value = value;
26322 if (consumed != NULL)
26323 *consumed = 1;
26324 vctxt->inode->flags |=
26325 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26326 break;
26327 case XML_SCHEMA_PUSH_TEXT_VOLATILE:
26328 /*
26329 * When working with SAX.
26330 * The value will be freed by the element info.
26331 */
26332 if (len != -1)
26333 vctxt->inode->value = BAD_CAST xmlStrndup(value, len);
26334 else
26335 vctxt->inode->value = BAD_CAST xmlStrdup(value);
26336 vctxt->inode->flags |=
26337 XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26338 break;
26339 default:
26340 break;
26341 }
26342 } else {
26343 /*
26344 * Concat the value.
26345 */
26346 if (vctxt->inode->flags & XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES) {
Kasimier T. Buchcik9c215eb2005-06-21 08:38:49 +000026347 vctxt->inode->value = BAD_CAST xmlStrncat(
26348 (xmlChar *) vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026349 } else {
26350 vctxt->inode->value =
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026351 BAD_CAST xmlStrncatNew(vctxt->inode->value, value, len);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026352 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES;
26353 }
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026354 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026355
26356 return (0);
Daniel Veillard4255d502002-04-16 15:50:10 +000026357}
26358
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026359static int
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026360xmlSchemaValidateElem(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik9b77aa02005-03-04 22:04:16 +000026361{
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026362 int ret = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +000026363
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026364 if ((vctxt->skipDepth != -1) &&
26365 (vctxt->depth >= vctxt->skipDepth)) {
26366 VERROR_INT("xmlSchemaValidateElem",
26367 "in skip-state");
26368 goto internal_error;
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000026369 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026370 if (vctxt->xsiAssemble) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026371 /*
26372 * URGENT TODO: Better to fully stop validation
26373 * if there was an error during dynamic schema construction.
26374 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026375 if (xmlSchemaAssembleByXSI(vctxt) == -1)
26376 goto internal_error;
26377 }
26378 if (vctxt->depth > 0) {
26379 /*
26380 * Validate this element against the content model
26381 * of the parent.
26382 */
26383 ret = xmlSchemaValidateChildElem(vctxt);
26384 if (ret != 0) {
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026385 if (ret < 0) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026386 VERROR_INT("xmlSchemaValidateElem",
26387 "calling xmlSchemaStreamValidateChildElement()");
26388 goto internal_error;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026389 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026390 goto exit;
26391 }
26392 if (vctxt->depth == vctxt->skipDepth)
26393 goto exit;
26394 if ((vctxt->inode->decl == NULL) &&
26395 (vctxt->inode->typeDef == NULL)) {
26396 VERROR_INT("xmlSchemaValidateElem",
26397 "the child element was valid but neither the "
26398 "declaration nor the type was set");
26399 goto internal_error;
26400 }
26401 } else {
26402 /*
26403 * Get the declaration of the validation root.
26404 */
26405 vctxt->inode->decl = xmlSchemaGetElem(vctxt->schema,
26406 vctxt->inode->localName,
26407 vctxt->inode->nsName);
26408 if (vctxt->inode->decl == NULL) {
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026409 ret = XML_SCHEMAV_CVC_ELT_1;
26410 VERROR(ret, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026411 "No matching global declaration available "
26412 "for the validation root");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026413 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026414 }
26415 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026416
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026417 if (vctxt->inode->decl == NULL)
26418 goto type_validation;
26419
26420 if (vctxt->inode->decl->type == XML_SCHEMA_TYPE_ANY) {
26421 int skip;
26422 /*
26423 * Wildcards.
26424 */
26425 ret = xmlSchemaValidateElemWildcard(vctxt, &skip);
26426 if (ret != 0) {
26427 if (ret < 0) {
26428 VERROR_INT("xmlSchemaValidateElem",
26429 "calling xmlSchemaValidateElemWildcard()");
26430 goto internal_error;
26431 }
26432 goto exit;
26433 }
26434 if (skip) {
26435 vctxt->skipDepth = vctxt->depth;
26436 goto exit;
26437 }
26438 /*
26439 * The declaration might be set by the wildcard validation,
26440 * when the processContents is "lax" or "strict".
26441 */
26442 if (vctxt->inode->decl->type != XML_SCHEMA_TYPE_ELEMENT) {
26443 /*
26444 * Clear the "decl" field to not confuse further processing.
26445 */
26446 vctxt->inode->decl = NULL;
26447 goto type_validation;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026448 }
Daniel Veillard4255d502002-04-16 15:50:10 +000026449 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026450 /*
26451 * Validate against the declaration.
26452 */
26453 ret = xmlSchemaValidateElemDecl(vctxt);
26454 if (ret != 0) {
26455 if (ret < 0) {
26456 VERROR_INT("xmlSchemaValidateElem",
26457 "calling xmlSchemaValidateElemDecl()");
26458 goto internal_error;
26459 }
26460 goto exit;
26461 }
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026462 /*
26463 * Validate against the type definition.
26464 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026465type_validation:
26466
26467 if (vctxt->inode->typeDef == NULL) {
26468 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26469 ret = XML_SCHEMAV_CVC_TYPE_1;
26470 VERROR(ret, NULL,
26471 "The type definition is absent");
26472 goto exit;
26473 }
26474 if (vctxt->inode->typeDef->flags & XML_SCHEMAS_TYPE_ABSTRACT) {
26475 vctxt->inode->flags |= XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE;
26476 ret = XML_SCHEMAV_CVC_TYPE_2;
26477 VERROR(ret, NULL,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026478 "The type definition is abstract");
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026479 goto exit;
26480 }
26481 /*
Kasimier T. Buchcik9ca11bf2005-06-14 19:24:47 +000026482 * Evaluate IDCs. Do it here, since new IDC matchers are registered
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026483 * during validation against the declaration. This must be done
26484 * _before_ attribute validation.
26485 */
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026486 if (vctxt->xpathStates != NULL) {
26487 ret = xmlSchemaXPathEvaluate(vctxt, XML_ELEMENT_NODE);
Kasimier T. Buchcik65c2f1d2005-10-17 12:39:58 +000026488 vctxt->inode->appliedXPath = 1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000026489 if (ret == -1) {
26490 VERROR_INT("xmlSchemaValidateElem",
26491 "calling xmlSchemaXPathEvaluate()");
26492 goto internal_error;
26493 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026494 }
26495 /*
26496 * Validate attributes.
26497 */
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026498 if (WXS_IS_COMPLEX(vctxt->inode->typeDef)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026499 if ((vctxt->nbAttrInfos != 0) ||
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000026500 (vctxt->inode->typeDef->attrUses != NULL)) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026501
26502 ret = xmlSchemaVAttributesComplex(vctxt);
26503 }
26504 } else if (vctxt->nbAttrInfos != 0) {
26505
26506 ret = xmlSchemaVAttributesSimple(vctxt);
26507 }
26508 /*
26509 * Clear registered attributes.
26510 */
26511 if (vctxt->nbAttrInfos != 0)
26512 xmlSchemaClearAttrInfos(vctxt);
26513 if (ret == -1) {
26514 VERROR_INT("xmlSchemaValidateElem",
26515 "calling attributes validation");
26516 goto internal_error;
26517 }
26518 /*
26519 * Don't return an error if attributes are invalid on purpose.
26520 */
26521 ret = 0;
26522
26523exit:
26524 if (ret != 0)
26525 vctxt->skipDepth = vctxt->depth;
26526 return (ret);
26527internal_error:
26528 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026529}
26530
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026531#ifdef XML_SCHEMA_READER_ENABLED
26532static int
26533xmlSchemaVReaderWalk(xmlSchemaValidCtxtPtr vctxt)
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026534{
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026535 const int WHTSP = 13, SIGN_WHTSP = 14, END_ELEM = 15;
26536 int depth, nodeType, ret = 0, consumed;
26537 xmlSchemaNodeInfoPtr ielem;
Kasimier T. Buchcik5eba91f2004-09-08 09:17:27 +000026538
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026539 vctxt->depth = -1;
26540 ret = xmlTextReaderRead(vctxt->reader);
26541 /*
26542 * Move to the document element.
26543 */
26544 while (ret == 1) {
26545 nodeType = xmlTextReaderNodeType(vctxt->reader);
26546 if (nodeType == XML_ELEMENT_NODE)
26547 goto root_found;
26548 ret = xmlTextReaderRead(vctxt->reader);
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026549 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026550 goto exit;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000026551
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026552root_found:
26553
26554 do {
26555 depth = xmlTextReaderDepth(vctxt->reader);
26556 nodeType = xmlTextReaderNodeType(vctxt->reader);
26557
26558 if (nodeType == XML_ELEMENT_NODE) {
26559
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026560 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026561 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26562 VERROR_INT("xmlSchemaVReaderWalk",
26563 "calling xmlSchemaValidatorPushElem()");
26564 goto internal_error;
26565 }
26566 ielem = vctxt->inode;
26567 ielem->localName = xmlTextReaderLocalName(vctxt->reader);
26568 ielem->nsName = xmlTextReaderNamespaceUri(vctxt->reader);
26569 ielem->flags |= XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES;
26570 /*
26571 * Is the element empty?
26572 */
26573 ret = xmlTextReaderIsEmptyElement(vctxt->reader);
26574 if (ret == -1) {
26575 VERROR_INT("xmlSchemaVReaderWalk",
26576 "calling xmlTextReaderIsEmptyElement()");
26577 goto internal_error;
26578 }
26579 if (ret) {
26580 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26581 }
26582 /*
26583 * Register attributes.
26584 */
26585 vctxt->nbAttrInfos = 0;
26586 ret = xmlTextReaderMoveToFirstAttribute(vctxt->reader);
26587 if (ret == -1) {
26588 VERROR_INT("xmlSchemaVReaderWalk",
26589 "calling xmlTextReaderMoveToFirstAttribute()");
26590 goto internal_error;
26591 }
26592 if (ret == 1) {
26593 do {
26594 /*
26595 * VAL TODO: How do we know that the reader works on a
26596 * node tree, to be able to pass a node here?
26597 */
26598 if (xmlSchemaValidatorPushAttribute(vctxt, NULL,
26599 (const xmlChar *) xmlTextReaderLocalName(vctxt->reader),
26600 xmlTextReaderNamespaceUri(vctxt->reader), 1,
26601 xmlTextReaderValue(vctxt->reader), 1) == -1) {
26602
26603 VERROR_INT("xmlSchemaVReaderWalk",
26604 "calling xmlSchemaValidatorPushAttribute()");
26605 goto internal_error;
26606 }
26607 ret = xmlTextReaderMoveToNextAttribute(vctxt->reader);
26608 if (ret == -1) {
26609 VERROR_INT("xmlSchemaVReaderWalk",
26610 "calling xmlTextReaderMoveToFirstAttribute()");
26611 goto internal_error;
26612 }
26613 } while (ret == 1);
26614 /*
26615 * Back to element position.
26616 */
26617 ret = xmlTextReaderMoveToElement(vctxt->reader);
26618 if (ret == -1) {
26619 VERROR_INT("xmlSchemaVReaderWalk",
26620 "calling xmlTextReaderMoveToElement()");
26621 goto internal_error;
26622 }
26623 }
26624 /*
26625 * Validate the element.
26626 */
26627 ret= xmlSchemaValidateElem(vctxt);
26628 if (ret != 0) {
26629 if (ret == -1) {
26630 VERROR_INT("xmlSchemaVReaderWalk",
26631 "calling xmlSchemaValidateElem()");
26632 goto internal_error;
26633 }
26634 goto exit;
26635 }
26636 if (vctxt->depth == vctxt->skipDepth) {
26637 int curDepth;
26638 /*
26639 * Skip all content.
26640 */
26641 if ((ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY) == 0) {
26642 ret = xmlTextReaderRead(vctxt->reader);
26643 curDepth = xmlTextReaderDepth(vctxt->reader);
26644 while ((ret == 1) && (curDepth != depth)) {
26645 ret = xmlTextReaderRead(vctxt->reader);
26646 curDepth = xmlTextReaderDepth(vctxt->reader);
26647 }
26648 if (ret < 0) {
26649 /*
26650 * VAL TODO: A reader error occured; what to do here?
26651 */
26652 ret = 1;
26653 goto exit;
26654 }
26655 }
26656 goto leave_elem;
26657 }
26658 /*
26659 * READER VAL TODO: Is an END_ELEM really never called
26660 * if the elem is empty?
26661 */
26662 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26663 goto leave_elem;
26664 } else if (nodeType == END_ELEM) {
26665 /*
26666 * Process END of element.
26667 */
26668leave_elem:
26669 ret = xmlSchemaValidatorPopElem(vctxt);
26670 if (ret != 0) {
26671 if (ret < 0) {
26672 VERROR_INT("xmlSchemaVReaderWalk",
26673 "calling xmlSchemaValidatorPopElem()");
26674 goto internal_error;
26675 }
26676 goto exit;
26677 }
26678 if (vctxt->depth >= 0)
26679 ielem = vctxt->inode;
26680 else
26681 ielem = NULL;
26682 } else if ((nodeType == XML_TEXT_NODE) ||
26683 (nodeType == XML_CDATA_SECTION_NODE) ||
26684 (nodeType == WHTSP) ||
26685 (nodeType == SIGN_WHTSP)) {
26686 /*
26687 * Process character content.
26688 */
26689 xmlChar *value;
26690
26691 if ((nodeType == WHTSP) || (nodeType == SIGN_WHTSP))
26692 nodeType = XML_TEXT_NODE;
26693
26694 value = xmlTextReaderValue(vctxt->reader);
26695 ret = xmlSchemaVPushText(vctxt, nodeType, BAD_CAST value,
26696 -1, XML_SCHEMA_PUSH_TEXT_CREATED, &consumed);
26697 if (! consumed)
26698 xmlFree(value);
26699 if (ret == -1) {
26700 VERROR_INT("xmlSchemaVReaderWalk",
26701 "calling xmlSchemaVPushText()");
26702 goto internal_error;
26703 }
26704 } else if ((nodeType == XML_ENTITY_NODE) ||
26705 (nodeType == XML_ENTITY_REF_NODE)) {
26706 /*
26707 * VAL TODO: What to do with entities?
26708 */
26709 TODO
26710 }
26711 /*
26712 * Read next node.
26713 */
26714 ret = xmlTextReaderRead(vctxt->reader);
26715 } while (ret == 1);
26716
26717exit:
26718 return (ret);
26719internal_error:
26720 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000026721}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026722#endif
Daniel Veillard4255d502002-04-16 15:50:10 +000026723
26724/************************************************************************
26725 * *
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026726 * SAX validation handlers *
Daniel Veillard4255d502002-04-16 15:50:10 +000026727 * *
26728 ************************************************************************/
26729
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026730/*
26731* Process text content.
26732*/
26733static void
26734xmlSchemaSAXHandleText(void *ctx,
26735 const xmlChar * ch,
26736 int len)
26737{
26738 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26739
26740 if (vctxt->depth < 0)
26741 return;
26742 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26743 return;
26744 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26745 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26746 if (xmlSchemaVPushText(vctxt, XML_TEXT_NODE, ch, len,
26747 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26748 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26749 "calling xmlSchemaVPushText()");
26750 vctxt->err = -1;
26751 xmlStopParser(vctxt->parserCtxt);
26752 }
26753}
26754
26755/*
26756* Process CDATA content.
26757*/
26758static void
26759xmlSchemaSAXHandleCDataSection(void *ctx,
26760 const xmlChar * ch,
26761 int len)
26762{
26763 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26764
26765 if (vctxt->depth < 0)
26766 return;
26767 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26768 return;
26769 if (vctxt->inode->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
26770 vctxt->inode->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
26771 if (xmlSchemaVPushText(vctxt, XML_CDATA_SECTION_NODE, ch, len,
26772 XML_SCHEMA_PUSH_TEXT_VOLATILE, NULL) == -1) {
26773 VERROR_INT("xmlSchemaSAXHandleCDataSection",
26774 "calling xmlSchemaVPushText()");
26775 vctxt->err = -1;
26776 xmlStopParser(vctxt->parserCtxt);
26777 }
26778}
26779
26780static void
26781xmlSchemaSAXHandleReference(void *ctx ATTRIBUTE_UNUSED,
26782 const xmlChar * name ATTRIBUTE_UNUSED)
26783{
26784 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26785
26786 if (vctxt->depth < 0)
26787 return;
26788 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
26789 return;
26790 /* SAX VAL TODO: What to do here? */
26791 TODO
26792}
26793
26794static void
26795xmlSchemaSAXHandleStartElementNs(void *ctx,
26796 const xmlChar * localname,
26797 const xmlChar * prefix ATTRIBUTE_UNUSED,
26798 const xmlChar * URI,
26799 int nb_namespaces,
26800 const xmlChar ** namespaces,
26801 int nb_attributes,
26802 int nb_defaulted ATTRIBUTE_UNUSED,
26803 const xmlChar ** attributes)
26804{
26805 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26806 int ret;
26807 xmlSchemaNodeInfoPtr ielem;
26808 int i, j;
26809
26810 /*
26811 * SAX VAL TODO: What to do with nb_defaulted?
26812 */
26813 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026814 * Skip elements if inside a "skip" wildcard or invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026815 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026816 vctxt->depth++;
26817 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026818 return;
26819 /*
26820 * Push the element.
26821 */
26822 if (xmlSchemaValidatorPushElem(vctxt) == -1) {
26823 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26824 "calling xmlSchemaValidatorPushElem()");
26825 goto internal_error;
26826 }
26827 ielem = vctxt->inode;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026828 /*
26829 * TODO: Is this OK?
26830 */
26831 ielem->nodeLine = xmlSAX2GetLineNumber(vctxt->parserCtxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026832 ielem->localName = localname;
26833 ielem->nsName = URI;
26834 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
26835 /*
26836 * Register namespaces on the elem info.
26837 */
26838 if (nb_namespaces != 0) {
26839 /*
26840 * Although the parser builds its own namespace list,
26841 * we have no access to it, so we'll use an own one.
26842 */
26843 for (i = 0, j = 0; i < nb_namespaces; i++, j += 2) {
26844 /*
26845 * Store prefix and namespace name.
26846 */
26847 if (ielem->nsBindings == NULL) {
26848 ielem->nsBindings =
26849 (const xmlChar **) xmlMalloc(10 *
26850 sizeof(const xmlChar *));
26851 if (ielem->nsBindings == NULL) {
26852 xmlSchemaVErrMemory(vctxt,
26853 "allocating namespace bindings for SAX validation",
26854 NULL);
26855 goto internal_error;
26856 }
26857 ielem->nbNsBindings = 0;
26858 ielem->sizeNsBindings = 5;
26859 } else if (ielem->sizeNsBindings <= ielem->nbNsBindings) {
26860 ielem->sizeNsBindings *= 2;
26861 ielem->nsBindings =
26862 (const xmlChar **) xmlRealloc(
26863 (void *) ielem->nsBindings,
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026864 ielem->sizeNsBindings * 2 * sizeof(const xmlChar *));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026865 if (ielem->nsBindings == NULL) {
26866 xmlSchemaVErrMemory(vctxt,
26867 "re-allocating namespace bindings for SAX validation",
26868 NULL);
26869 goto internal_error;
26870 }
26871 }
26872
26873 ielem->nsBindings[ielem->nbNsBindings * 2] = namespaces[j];
26874 if (namespaces[j+1][0] == 0) {
26875 /*
26876 * Handle xmlns="".
26877 */
26878 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] = NULL;
26879 } else
26880 ielem->nsBindings[ielem->nbNsBindings * 2 + 1] =
26881 namespaces[j+1];
26882 ielem->nbNsBindings++;
26883 }
26884 }
26885 /*
26886 * Register attributes.
26887 * SAX VAL TODO: We are not adding namespace declaration
26888 * attributes yet.
26889 */
26890 if (nb_attributes != 0) {
26891 xmlChar *value;
26892
26893 for (j = 0, i = 0; i < nb_attributes; i++, j += 5) {
26894 /*
26895 * Duplicate the value.
26896 */
26897 value = xmlStrndup(attributes[j+3],
26898 attributes[j+4] - attributes[j+3]);
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026899 /*
26900 * TODO: Set the node line.
26901 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026902 ret = xmlSchemaValidatorPushAttribute(vctxt,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000026903 NULL, ielem->nodeLine, attributes[j], attributes[j+2], 0,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026904 value, 1);
26905 if (ret == -1) {
26906 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26907 "calling xmlSchemaValidatorPushAttribute()");
26908 goto internal_error;
26909 }
26910 }
26911 }
26912 /*
26913 * Validate the element.
26914 */
26915 ret = xmlSchemaValidateElem(vctxt);
26916 if (ret != 0) {
26917 if (ret == -1) {
26918 VERROR_INT("xmlSchemaSAXHandleStartElementNs",
26919 "calling xmlSchemaValidateElem()");
26920 goto internal_error;
26921 }
26922 goto exit;
26923 }
26924
26925exit:
26926 return;
26927internal_error:
26928 vctxt->err = -1;
26929 xmlStopParser(vctxt->parserCtxt);
26930 return;
26931}
26932
26933static void
26934xmlSchemaSAXHandleEndElementNs(void *ctx,
26935 const xmlChar * localname ATTRIBUTE_UNUSED,
26936 const xmlChar * prefix ATTRIBUTE_UNUSED,
26937 const xmlChar * URI ATTRIBUTE_UNUSED)
26938{
26939 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctx;
26940 int res;
26941
26942 /*
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000026943 * Skip elements if inside a "skip" wildcard or if invalid.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026944 */
26945 if (vctxt->skipDepth != -1) {
26946 if (vctxt->depth > vctxt->skipDepth) {
26947 vctxt->depth--;
26948 return;
26949 } else
26950 vctxt->skipDepth = -1;
26951 }
26952 /*
26953 * SAX VAL TODO: Just a temporary check.
26954 */
26955 if ((!xmlStrEqual(vctxt->inode->localName, localname)) ||
26956 (!xmlStrEqual(vctxt->inode->nsName, URI))) {
26957 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
26958 "elem pop mismatch");
26959 }
26960 res = xmlSchemaValidatorPopElem(vctxt);
26961 if (res != 0) {
26962 if (res < 0) {
26963 VERROR_INT("xmlSchemaSAXHandleEndElementNs",
26964 "calling xmlSchemaValidatorPopElem()");
26965 goto internal_error;
26966 }
26967 goto exit;
26968 }
26969exit:
26970 return;
26971internal_error:
26972 vctxt->err = -1;
26973 xmlStopParser(vctxt->parserCtxt);
26974 return;
26975}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000026976
Daniel Veillard4255d502002-04-16 15:50:10 +000026977/************************************************************************
26978 * *
26979 * Validation interfaces *
26980 * *
26981 ************************************************************************/
26982
26983/**
26984 * xmlSchemaNewValidCtxt:
26985 * @schema: a precompiled XML Schemas
26986 *
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000026987 * Create an XML Schemas validation context based on the given schema.
Daniel Veillard4255d502002-04-16 15:50:10 +000026988 *
26989 * Returns the validation context or NULL in case of error
26990 */
26991xmlSchemaValidCtxtPtr
Daniel Veillardd0c9c322003-10-10 00:49:42 +000026992xmlSchemaNewValidCtxt(xmlSchemaPtr schema)
26993{
Daniel Veillard4255d502002-04-16 15:50:10 +000026994 xmlSchemaValidCtxtPtr ret;
26995
26996 ret = (xmlSchemaValidCtxtPtr) xmlMalloc(sizeof(xmlSchemaValidCtxt));
26997 if (ret == NULL) {
Daniel Veillardd0c9c322003-10-10 00:49:42 +000026998 xmlSchemaVErrMemory(NULL, "allocating validation context", NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +000026999 return (NULL);
27000 }
27001 memset(ret, 0, sizeof(xmlSchemaValidCtxt));
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027002 ret->type = XML_SCHEMA_CTXT_VALIDATOR;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027003 ret->dict = xmlDictCreate();
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027004 ret->nodeQNames = xmlSchemaItemListCreate();
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027005 ret->schema = schema;
Daniel Veillard4255d502002-04-16 15:50:10 +000027006 return (ret);
27007}
27008
27009/**
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027010 * xmlSchemaClearValidCtxt:
27011 * @ctxt: the schema validation context
27012 *
27013 * Free the resources associated to the schema validation context;
27014 * leaves some fields alive intended for reuse of the context.
27015 */
27016static void
27017xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt)
27018{
27019 if (vctxt == NULL)
27020 return;
27021
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027022 /*
27023 * TODO: Should we clear the flags?
27024 * Might be problematic if one reuses the context
27025 * and assumes that the options remain the same.
27026 */
Kasimier T. Buchcikaba15f72005-04-01 15:17:27 +000027027 vctxt->flags = 0;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027028 vctxt->validationRoot = NULL;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027029 vctxt->doc = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000027030#ifdef LIBXML_READER_ENABLED
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027031 vctxt->reader = NULL;
Daniel Veillard39e5c892005-07-03 22:48:50 +000027032#endif
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027033 vctxt->hasKeyrefs = 0;
27034
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027035 if (vctxt->value != NULL) {
27036 xmlSchemaFreeValue(vctxt->value);
27037 vctxt->value = NULL;
27038 }
27039 /*
27040 * Augmented IDC information.
27041 */
27042 if (vctxt->aidcs != NULL) {
27043 xmlSchemaIDCAugPtr cur = vctxt->aidcs, next;
27044 do {
27045 next = cur->next;
27046 xmlFree(cur);
27047 cur = next;
27048 } while (cur != NULL);
27049 vctxt->aidcs = NULL;
27050 }
27051 if (vctxt->idcNodes != NULL) {
27052 int i;
27053 xmlSchemaPSVIIDCNodePtr item;
27054
27055 for (i = 0; i < vctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027056 item = vctxt->idcNodes[i];
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027057 xmlFree(item->keys);
27058 xmlFree(item);
27059 }
27060 xmlFree(vctxt->idcNodes);
27061 vctxt->idcNodes = NULL;
27062 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027063 /*
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027064 * Note that we won't delete the XPath state pool here.
27065 */
27066 if (vctxt->xpathStates != NULL) {
27067 xmlSchemaFreeIDCStateObjList(vctxt->xpathStates);
27068 vctxt->xpathStates = NULL;
27069 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027070 /*
27071 * Attribute info.
27072 */
27073 if (vctxt->nbAttrInfos != 0) {
27074 xmlSchemaClearAttrInfos(vctxt);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027075 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027076 /*
27077 * Element info.
27078 */
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027079 if (vctxt->elemInfos != NULL) {
27080 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027081 xmlSchemaNodeInfoPtr ei;
27082
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027083 for (i = 0; i < vctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027084 ei = vctxt->elemInfos[i];
27085 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027086 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027087 xmlSchemaClearElemInfo(ei);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027088 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027089 }
27090 xmlSchemaItemListClear(vctxt->nodeQNames);
27091 /* Recreate the dict. */
27092 xmlDictFree(vctxt->dict);
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027093 /*
27094 * TODO: Is is save to recreate it? Do we have a scenario
27095 * where the user provides the dict?
27096 */
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027097 vctxt->dict = xmlDictCreate();
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027098}
27099
27100/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027101 * xmlSchemaFreeValidCtxt:
27102 * @ctxt: the schema validation context
27103 *
27104 * Free the resources associated to the schema validation context
27105 */
27106void
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027107xmlSchemaFreeValidCtxt(xmlSchemaValidCtxtPtr ctxt)
27108{
Daniel Veillard4255d502002-04-16 15:50:10 +000027109 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027110 return;
Daniel Veillard88c58912002-04-23 07:12:20 +000027111 if (ctxt->value != NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027112 xmlSchemaFreeValue(ctxt->value);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027113 if (ctxt->pctxt != NULL)
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027114 xmlSchemaFreeParserCtxt(ctxt->pctxt);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027115 if (ctxt->idcNodes != NULL) {
27116 int i;
27117 xmlSchemaPSVIIDCNodePtr item;
27118
27119 for (i = 0; i < ctxt->nbIdcNodes; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027120 item = ctxt->idcNodes[i];
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027121 xmlFree(item->keys);
27122 xmlFree(item);
27123 }
27124 xmlFree(ctxt->idcNodes);
27125 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027126 if (ctxt->idcKeys != NULL) {
27127 int i;
27128 for (i = 0; i < ctxt->nbIdcKeys; i++)
27129 xmlSchemaIDCFreeKey(ctxt->idcKeys[i]);
27130 xmlFree(ctxt->idcKeys);
27131 }
27132
27133 if (ctxt->xpathStates != NULL)
27134 xmlSchemaFreeIDCStateObjList(ctxt->xpathStates);
27135 if (ctxt->xpathStatePool != NULL)
27136 xmlSchemaFreeIDCStateObjList(ctxt->xpathStatePool);
27137
27138 /*
27139 * Augmented IDC information.
27140 */
27141 if (ctxt->aidcs != NULL) {
27142 xmlSchemaIDCAugPtr cur = ctxt->aidcs, next;
27143 do {
27144 next = cur->next;
27145 xmlFree(cur);
27146 cur = next;
27147 } while (cur != NULL);
27148 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027149 if (ctxt->attrInfos != NULL) {
27150 int i;
27151 xmlSchemaAttrInfoPtr attr;
27152
27153 /* Just a paranoid call to the cleanup. */
27154 if (ctxt->nbAttrInfos != 0)
27155 xmlSchemaClearAttrInfos(ctxt);
27156 for (i = 0; i < ctxt->sizeAttrInfos; i++) {
27157 attr = ctxt->attrInfos[i];
27158 xmlFree(attr);
27159 }
27160 xmlFree(ctxt->attrInfos);
Kasimier T. Buchcikbd2a7d12005-02-16 12:27:25 +000027161 }
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027162 if (ctxt->elemInfos != NULL) {
27163 int i;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027164 xmlSchemaNodeInfoPtr ei;
27165
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027166 for (i = 0; i < ctxt->sizeElemInfos; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027167 ei = ctxt->elemInfos[i];
27168 if (ei == NULL)
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027169 break;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027170 xmlSchemaClearElemInfo(ei);
27171 xmlFree(ei);
Kasimier T. Buchcike8a550b2005-01-27 12:49:31 +000027172 }
27173 xmlFree(ctxt->elemInfos);
27174 }
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027175 if (ctxt->nodeQNames != NULL)
Kasimier T. Buchcik630215b2005-08-22 10:15:39 +000027176 xmlSchemaItemListFree(ctxt->nodeQNames);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027177 if (ctxt->dict != NULL)
27178 xmlDictFree(ctxt->dict);
Daniel Veillard4255d502002-04-16 15:50:10 +000027179 xmlFree(ctxt);
27180}
27181
27182/**
Daniel Veillardf10ae122005-07-10 19:03:16 +000027183 * xmlSchemaIsValid:
27184 * @ctxt: the schema validation context
27185 *
27186 * Check if any error was detected during validation.
27187 *
27188 * Returns 1 if valid so far, 0 if errors were detected, and -1 in case
27189 * of internal error.
27190 */
27191int
27192xmlSchemaIsValid(xmlSchemaValidCtxtPtr ctxt)
27193{
27194 if (ctxt == NULL)
27195 return(-1);
27196 return(ctxt->err == 0);
27197}
27198
27199/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027200 * xmlSchemaSetValidErrors:
27201 * @ctxt: a schema validation context
27202 * @err: the error function
27203 * @warn: the warning function
Daniel Veillarda9b66d02002-12-11 14:23:49 +000027204 * @ctx: the functions context
Daniel Veillard4255d502002-04-16 15:50:10 +000027205 *
William M. Brack2f2a6632004-08-20 23:09:47 +000027206 * Set the error and warning callback informations
Daniel Veillard4255d502002-04-16 15:50:10 +000027207 */
27208void
27209xmlSchemaSetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027210 xmlSchemaValidityErrorFunc err,
27211 xmlSchemaValidityWarningFunc warn, void *ctx)
27212{
Daniel Veillard4255d502002-04-16 15:50:10 +000027213 if (ctxt == NULL)
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027214 return;
Daniel Veillard4255d502002-04-16 15:50:10 +000027215 ctxt->error = err;
27216 ctxt->warning = warn;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027217 ctxt->errCtxt = ctx;
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027218 if (ctxt->pctxt != NULL)
27219 xmlSchemaSetParserErrors(ctxt->pctxt, err, warn, ctx);
Daniel Veillard4255d502002-04-16 15:50:10 +000027220}
27221
27222/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027223 * xmlSchemaSetValidStructuredErrors:
27224 * @ctxt: a schema validation context
27225 * @serror: the structured error function
27226 * @ctx: the functions context
27227 *
27228 * Set the structured error callback
27229 */
27230void
27231xmlSchemaSetValidStructuredErrors(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027232 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027233{
27234 if (ctxt == NULL)
27235 return;
27236 ctxt->serror = serror;
27237 ctxt->error = NULL;
27238 ctxt->warning = NULL;
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027239 ctxt->errCtxt = ctx;
27240 if (ctxt->pctxt != NULL)
27241 xmlSchemaSetParserStructuredErrors(ctxt->pctxt, serror, ctx);
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000027242}
27243
27244/**
Daniel Veillard259f0df2004-08-18 09:13:18 +000027245 * xmlSchemaGetValidErrors:
27246 * @ctxt: a XML-Schema validation context
27247 * @err: the error function result
27248 * @warn: the warning function result
27249 * @ctx: the functions context result
27250 *
27251 * Get the error and warning callback informations
27252 *
27253 * Returns -1 in case of error and 0 otherwise
27254 */
27255int
27256xmlSchemaGetValidErrors(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027257 xmlSchemaValidityErrorFunc * err,
27258 xmlSchemaValidityWarningFunc * warn, void **ctx)
Daniel Veillard259f0df2004-08-18 09:13:18 +000027259{
27260 if (ctxt == NULL)
27261 return (-1);
27262 if (err != NULL)
27263 *err = ctxt->error;
27264 if (warn != NULL)
27265 *warn = ctxt->warning;
27266 if (ctx != NULL)
Kasimier T. Buchcik90b5ebc2005-11-18 17:18:27 +000027267 *ctx = ctxt->errCtxt;
Daniel Veillard259f0df2004-08-18 09:13:18 +000027268 return (0);
27269}
27270
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027271
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027272/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027273 * xmlSchemaSetValidOptions:
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027274 * @ctxt: a schema validation context
27275 * @options: a combination of xmlSchemaValidOption
27276 *
27277 * Sets the options to be used during the validation.
27278 *
27279 * Returns 0 in case of success, -1 in case of an
27280 * API error.
27281 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027282int
27283xmlSchemaSetValidOptions(xmlSchemaValidCtxtPtr ctxt,
27284 int options)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027285
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027286{
27287 int i;
27288
27289 if (ctxt == NULL)
27290 return (-1);
27291 /*
27292 * WARNING: Change the start value if adding to the
27293 * xmlSchemaValidOption.
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027294 * TODO: Is there an other, more easy to maintain,
27295 * way?
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027296 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027297 for (i = 1; i < (int) sizeof(int) * 8; i++) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027298 if (options & 1<<i)
27299 return (-1);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027300 }
27301 ctxt->options = options;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027302 return (0);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027303}
27304
27305/**
Daniel Veillard6927b102004-10-27 17:29:04 +000027306 * xmlSchemaValidCtxtGetOptions:
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027307 * @ctxt: a schema validation context
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027308 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027309 * Get the validation context options.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027310 *
William M. Brack21e4ef22005-01-02 09:53:13 +000027311 * Returns the option combination or -1 on error.
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027312 */
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027313int
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027314xmlSchemaValidCtxtGetOptions(xmlSchemaValidCtxtPtr ctxt)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027315
27316{
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027317 if (ctxt == NULL)
27318 return (-1);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027319 else
27320 return (ctxt->options);
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027321}
Kasimier T. Buchcik87876402004-09-29 13:29:03 +000027322
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027323static int
27324xmlSchemaVDocWalk(xmlSchemaValidCtxtPtr vctxt)
27325{
27326 xmlAttrPtr attr;
27327 int ret = 0;
27328 xmlSchemaNodeInfoPtr ielem = NULL;
27329 xmlNodePtr node, valRoot;
27330 const xmlChar *nsName;
27331
27332 /* DOC VAL TODO: Move this to the start function. */
27333 valRoot = xmlDocGetRootElement(vctxt->doc);
27334 if (valRoot == NULL) {
27335 /* VAL TODO: Error code? */
27336 VERROR(1, NULL, "The document has no document element");
27337 return (1);
27338 }
27339 vctxt->depth = -1;
27340 vctxt->validationRoot = valRoot;
27341 node = valRoot;
27342 while (node != NULL) {
27343 if ((vctxt->skipDepth != -1) && (vctxt->depth >= vctxt->skipDepth))
27344 goto next_sibling;
27345 if (node->type == XML_ELEMENT_NODE) {
27346
27347 /*
27348 * Init the node-info.
27349 */
Kasimier T. Buchcik84a56e32005-06-16 12:44:35 +000027350 vctxt->depth++;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027351 if (xmlSchemaValidatorPushElem(vctxt) == -1)
27352 goto internal_error;
27353 ielem = vctxt->inode;
27354 ielem->node = node;
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027355 ielem->nodeLine = node->line;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027356 ielem->localName = node->name;
27357 if (node->ns != NULL)
27358 ielem->nsName = node->ns->href;
27359 ielem->flags |= XML_SCHEMA_ELEM_INFO_EMPTY;
27360 /*
27361 * Register attributes.
27362 * DOC VAL TODO: We do not register namespace declaration
27363 * attributes yet.
27364 */
27365 vctxt->nbAttrInfos = 0;
27366 if (node->properties != NULL) {
27367 attr = node->properties;
27368 do {
27369 if (attr->ns != NULL)
27370 nsName = attr->ns->href;
27371 else
27372 nsName = NULL;
27373 ret = xmlSchemaValidatorPushAttribute(vctxt,
27374 (xmlNodePtr) attr,
Kasimier T. Buchcik764b3d62005-08-12 12:25:23 +000027375 /*
27376 * Note that we give it the line number of the
27377 * parent element.
27378 */
27379 ielem->nodeLine,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027380 attr->name, nsName, 0,
27381 xmlNodeListGetString(attr->doc, attr->children, 1), 1);
27382 if (ret == -1) {
27383 VERROR_INT("xmlSchemaDocWalk",
27384 "calling xmlSchemaValidatorPushAttribute()");
27385 goto internal_error;
27386 }
27387 attr = attr->next;
27388 } while (attr);
27389 }
27390 /*
27391 * Validate the element.
27392 */
27393 ret = xmlSchemaValidateElem(vctxt);
27394 if (ret != 0) {
27395 if (ret == -1) {
27396 VERROR_INT("xmlSchemaDocWalk",
27397 "calling xmlSchemaValidateElem()");
27398 goto internal_error;
27399 }
27400 /*
27401 * Don't stop validation; just skip the content
27402 * of this element.
27403 */
27404 goto leave_node;
27405 }
27406 if ((vctxt->skipDepth != -1) &&
27407 (vctxt->depth >= vctxt->skipDepth))
27408 goto leave_node;
27409 } else if ((node->type == XML_TEXT_NODE) ||
27410 (node->type == XML_CDATA_SECTION_NODE)) {
27411 /*
27412 * Process character content.
27413 */
27414 if (ielem->flags & XML_SCHEMA_ELEM_INFO_EMPTY)
27415 ielem->flags ^= XML_SCHEMA_ELEM_INFO_EMPTY;
27416 ret = xmlSchemaVPushText(vctxt, node->type, node->content,
27417 -1, XML_SCHEMA_PUSH_TEXT_PERSIST, NULL);
27418 if (ret < 0) {
27419 VERROR_INT("xmlSchemaVDocWalk",
27420 "calling xmlSchemaVPushText()");
27421 goto internal_error;
27422 }
27423 /*
27424 * DOC VAL TODO: Should we skip further validation of the
27425 * element content here?
27426 */
27427 } else if ((node->type == XML_ENTITY_NODE) ||
27428 (node->type == XML_ENTITY_REF_NODE)) {
27429 /*
27430 * DOC VAL TODO: What to do with entities?
27431 */
27432 TODO
27433 } else {
27434 goto leave_node;
27435 /*
27436 * DOC VAL TODO: XInclude nodes, etc.
27437 */
27438 }
27439 /*
27440 * Walk the doc.
27441 */
27442 if (node->children != NULL) {
27443 node = node->children;
27444 continue;
27445 }
27446leave_node:
27447 if (node->type == XML_ELEMENT_NODE) {
27448 /*
27449 * Leaving the scope of an element.
27450 */
27451 if (node != vctxt->inode->node) {
27452 VERROR_INT("xmlSchemaVDocWalk",
27453 "element position mismatch");
27454 goto internal_error;
27455 }
27456 ret = xmlSchemaValidatorPopElem(vctxt);
27457 if (ret != 0) {
27458 if (ret < 0) {
27459 VERROR_INT("xmlSchemaVDocWalk",
27460 "calling xmlSchemaValidatorPopElem()");
27461 goto internal_error;
27462 }
27463 }
27464 if (node == valRoot)
27465 goto exit;
27466 }
27467next_sibling:
27468 if (node->next != NULL)
27469 node = node->next;
27470 else {
27471 node = node->parent;
27472 goto leave_node;
27473 }
27474 }
27475
27476exit:
27477 return (ret);
27478internal_error:
27479 return (-1);
27480}
27481
27482static int
Daniel Veillardf10ae122005-07-10 19:03:16 +000027483xmlSchemaPreRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027484 /*
27485 * Some initialization.
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027486 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027487 vctxt->err = 0;
27488 vctxt->nberrors = 0;
27489 vctxt->depth = -1;
27490 vctxt->skipDepth = -1;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027491 vctxt->xsiAssemble = 0;
Kasimier T. Buchcik27820272005-10-14 14:33:48 +000027492 vctxt->hasKeyrefs = 0;
27493#ifdef ENABLE_IDC_NODE_TABLES
27494 vctxt->createIDCNodeTables = 1;
27495#else
27496 vctxt->createIDCNodeTables = 0;
27497#endif
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027498 /*
27499 * Create a schema + parser if necessary.
27500 */
27501 if (vctxt->schema == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027502 xmlSchemaParserCtxtPtr pctxt;
27503
27504 vctxt->xsiAssemble = 1;
27505 /*
27506 * If not schema was given then we will create a schema
27507 * dynamically using XSI schema locations.
27508 *
27509 * Create the schema parser context.
27510 */
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027511 if ((vctxt->pctxt == NULL) &&
27512 (xmlSchemaCreatePCtxtOnVCtxt(vctxt) == -1))
27513 return (-1);
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027514 pctxt = vctxt->pctxt;
27515 pctxt->xsiAssemble = 1;
27516 /*
27517 * Create the schema.
27518 */
27519 vctxt->schema = xmlSchemaNewSchema(pctxt);
27520 if (vctxt->schema == NULL)
27521 return (-1);
27522 /*
27523 * Create the schema construction context.
27524 */
27525 pctxt->constructor = xmlSchemaConstructionCtxtCreate(pctxt->dict);
27526 if (pctxt->constructor == NULL)
27527 return(-1);
Kasimier T. Buchcik570faa52005-10-10 13:18:40 +000027528 pctxt->constructor->mainSchema = vctxt->schema;
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027529 /*
27530 * Take ownership of the constructor to be able to free it.
27531 */
27532 pctxt->ownsConstructor = 1;
27533 }
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027534 /*
27535 * Augment the IDC definitions.
27536 */
27537 if (vctxt->schema->idcDef != NULL) {
27538 xmlHashScan(vctxt->schema->idcDef,
27539 (xmlHashScanner) xmlSchemaAugmentIDC, vctxt);
27540 }
Daniel Veillardf10ae122005-07-10 19:03:16 +000027541 return(0);
27542}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027543
Daniel Veillardf10ae122005-07-10 19:03:16 +000027544static void
27545xmlSchemaPostRun(xmlSchemaValidCtxtPtr vctxt) {
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027546 if (vctxt->xsiAssemble) {
27547 if (vctxt->schema != NULL) {
27548 xmlSchemaFree(vctxt->schema);
27549 vctxt->schema = NULL;
27550 }
27551 }
27552 xmlSchemaClearValidCtxt(vctxt);
Daniel Veillardf10ae122005-07-10 19:03:16 +000027553}
27554
27555static int
27556xmlSchemaVStart(xmlSchemaValidCtxtPtr vctxt)
27557{
27558 int ret = 0;
27559
27560 if (xmlSchemaPreRun(vctxt) < 0)
27561 return(-1);
27562
27563 if (vctxt->doc != NULL) {
27564 /*
27565 * Tree validation.
27566 */
27567 ret = xmlSchemaVDocWalk(vctxt);
27568#ifdef LIBXML_READER_ENABLED
27569 } else if (vctxt->reader != NULL) {
27570 /*
27571 * XML Reader validation.
27572 */
27573#ifdef XML_SCHEMA_READER_ENABLED
27574 ret = xmlSchemaVReaderWalk(vctxt);
27575#endif
27576#endif
27577 } else if ((vctxt->sax != NULL) && (vctxt->parserCtxt != NULL)) {
27578 /*
27579 * SAX validation.
27580 */
27581 ret = xmlParseDocument(vctxt->parserCtxt);
27582 } else {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027583 VERROR_INT("xmlSchemaVStart",
Daniel Veillardf10ae122005-07-10 19:03:16 +000027584 "no instance to validate");
27585 ret = -1;
27586 }
27587
27588 xmlSchemaPostRun(vctxt);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027589 if (ret == 0)
27590 ret = vctxt->err;
27591 return (ret);
27592}
27593
27594/**
27595 * xmlSchemaValidateOneElement:
27596 * @ctxt: a schema validation context
27597 * @elem: an element node
27598 *
27599 * Validate a branch of a tree, starting with the given @elem.
27600 *
27601 * Returns 0 if the element and its subtree is valid, a positive error
27602 * code number otherwise and -1 in case of an internal or API error.
27603 */
27604int
27605xmlSchemaValidateOneElement(xmlSchemaValidCtxtPtr ctxt, xmlNodePtr elem)
27606{
27607 if ((ctxt == NULL) || (elem == NULL) || (elem->type != XML_ELEMENT_NODE))
27608 return (-1);
27609
27610 if (ctxt->schema == NULL)
27611 return (-1);
27612
27613 ctxt->doc = elem->doc;
27614 ctxt->node = elem;
27615 ctxt->validationRoot = elem;
27616 return(xmlSchemaVStart(ctxt));
27617}
Kasimier T. Buchcik876a6db2004-09-16 11:31:52 +000027618
Daniel Veillard259f0df2004-08-18 09:13:18 +000027619/**
Daniel Veillard4255d502002-04-16 15:50:10 +000027620 * xmlSchemaValidateDoc:
27621 * @ctxt: a schema validation context
27622 * @doc: a parsed document tree
27623 *
27624 * Validate a document tree in memory.
27625 *
27626 * Returns 0 if the document is schemas valid, a positive error code
27627 * number otherwise and -1 in case of internal or API error.
27628 */
27629int
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027630xmlSchemaValidateDoc(xmlSchemaValidCtxtPtr ctxt, xmlDocPtr doc)
27631{
Daniel Veillard4255d502002-04-16 15:50:10 +000027632 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000027633 return (-1);
Daniel Veillard4255d502002-04-16 15:50:10 +000027634
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027635 ctxt->doc = doc;
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027636 ctxt->node = xmlDocGetRootElement(doc);
27637 if (ctxt->node == NULL) {
Kasimier T. Buchcik22c0c462005-09-12 19:09:46 +000027638 xmlSchemaCustomErr(ACTXT_CAST ctxt,
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027639 XML_SCHEMAV_DOCUMENT_ELEMENT_MISSING,
27640 (xmlNodePtr) doc, NULL,
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027641 "The document has no document element", NULL, NULL);
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027642 return (ctxt->err);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027643 }
Kasimier T. Buchcik7f3efa92005-03-07 17:41:58 +000027644 ctxt->validationRoot = ctxt->node;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000027645 return (xmlSchemaVStart(ctxt));
Daniel Veillard4255d502002-04-16 15:50:10 +000027646}
27647
Daniel Veillardcdc82732005-07-08 15:04:06 +000027648
27649/************************************************************************
27650 * *
27651 * Function and data for SAX streaming API *
27652 * *
27653 ************************************************************************/
27654typedef struct _xmlSchemaSplitSAXData xmlSchemaSplitSAXData;
27655typedef xmlSchemaSplitSAXData *xmlSchemaSplitSAXDataPtr;
27656
27657struct _xmlSchemaSplitSAXData {
27658 xmlSAXHandlerPtr user_sax;
27659 void *user_data;
27660 xmlSchemaValidCtxtPtr ctxt;
27661 xmlSAXHandlerPtr schemas_sax;
27662};
27663
Daniel Veillard971771e2005-07-09 17:32:57 +000027664#define XML_SAX_PLUG_MAGIC 0xdc43ba21
27665
27666struct _xmlSchemaSAXPlug {
27667 unsigned int magic;
27668
27669 /* the original callbacks informations */
27670 xmlSAXHandlerPtr *user_sax_ptr;
27671 xmlSAXHandlerPtr user_sax;
27672 void **user_data_ptr;
27673 void *user_data;
27674
27675 /* the block plugged back and validation informations */
27676 xmlSAXHandler schemas_sax;
27677 xmlSchemaValidCtxtPtr ctxt;
27678};
27679
Daniel Veillardcdc82732005-07-08 15:04:06 +000027680/* All those functions just bounces to the user provided SAX handlers */
27681static void
27682internalSubsetSplit(void *ctx, const xmlChar *name,
27683 const xmlChar *ExternalID, const xmlChar *SystemID)
27684{
Daniel Veillard971771e2005-07-09 17:32:57 +000027685 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027686 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27687 (ctxt->user_sax->internalSubset != NULL))
27688 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27689 SystemID);
27690}
27691
27692static int
27693isStandaloneSplit(void *ctx)
27694{
Daniel Veillard971771e2005-07-09 17:32:57 +000027695 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027696 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27697 (ctxt->user_sax->isStandalone != NULL))
27698 return(ctxt->user_sax->isStandalone(ctxt->user_data));
27699 return(0);
27700}
27701
27702static int
27703hasInternalSubsetSplit(void *ctx)
27704{
Daniel Veillard971771e2005-07-09 17:32:57 +000027705 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027706 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27707 (ctxt->user_sax->hasInternalSubset != NULL))
27708 return(ctxt->user_sax->hasInternalSubset(ctxt->user_data));
27709 return(0);
27710}
27711
27712static int
27713hasExternalSubsetSplit(void *ctx)
27714{
Daniel Veillard971771e2005-07-09 17:32:57 +000027715 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027716 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27717 (ctxt->user_sax->hasExternalSubset != NULL))
27718 return(ctxt->user_sax->hasExternalSubset(ctxt->user_data));
27719 return(0);
27720}
27721
27722static void
27723externalSubsetSplit(void *ctx, const xmlChar *name,
27724 const xmlChar *ExternalID, const xmlChar *SystemID)
27725{
Daniel Veillard971771e2005-07-09 17:32:57 +000027726 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027727 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27728 (ctxt->user_sax->internalSubset != NULL))
27729 ctxt->user_sax->internalSubset(ctxt->user_data, name, ExternalID,
27730 SystemID);
27731}
27732
27733static xmlParserInputPtr
27734resolveEntitySplit(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
27735{
Daniel Veillard971771e2005-07-09 17:32:57 +000027736 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027737 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27738 (ctxt->user_sax->resolveEntity != NULL))
27739 return(ctxt->user_sax->resolveEntity(ctxt->user_data, publicId,
27740 systemId));
27741 return(NULL);
27742}
27743
27744static xmlEntityPtr
27745getEntitySplit(void *ctx, const xmlChar *name)
27746{
Daniel Veillard971771e2005-07-09 17:32:57 +000027747 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027748 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27749 (ctxt->user_sax->getEntity != NULL))
27750 return(ctxt->user_sax->getEntity(ctxt->user_data, name));
27751 return(NULL);
27752}
27753
27754static xmlEntityPtr
27755getParameterEntitySplit(void *ctx, const xmlChar *name)
27756{
Daniel Veillard971771e2005-07-09 17:32:57 +000027757 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027758 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27759 (ctxt->user_sax->getParameterEntity != NULL))
27760 return(ctxt->user_sax->getParameterEntity(ctxt->user_data, name));
27761 return(NULL);
27762}
27763
27764
27765static void
27766entityDeclSplit(void *ctx, const xmlChar *name, int type,
27767 const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
27768{
Daniel Veillard971771e2005-07-09 17:32:57 +000027769 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027770 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27771 (ctxt->user_sax->entityDecl != NULL))
27772 ctxt->user_sax->entityDecl(ctxt->user_data, name, type, publicId,
27773 systemId, content);
27774}
27775
27776static void
27777attributeDeclSplit(void *ctx, const xmlChar * elem,
27778 const xmlChar * name, int type, int def,
27779 const xmlChar * defaultValue, xmlEnumerationPtr tree)
27780{
Daniel Veillard971771e2005-07-09 17:32:57 +000027781 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027782 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27783 (ctxt->user_sax->attributeDecl != NULL)) {
27784 ctxt->user_sax->attributeDecl(ctxt->user_data, elem, name, type,
27785 def, defaultValue, tree);
27786 } else {
27787 xmlFreeEnumeration(tree);
27788 }
27789}
27790
27791static void
27792elementDeclSplit(void *ctx, const xmlChar *name, int type,
27793 xmlElementContentPtr content)
27794{
Daniel Veillard971771e2005-07-09 17:32:57 +000027795 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027796 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27797 (ctxt->user_sax->elementDecl != NULL))
27798 ctxt->user_sax->elementDecl(ctxt->user_data, name, type, content);
27799}
27800
27801static void
27802notationDeclSplit(void *ctx, const xmlChar *name,
27803 const xmlChar *publicId, const xmlChar *systemId)
27804{
Daniel Veillard971771e2005-07-09 17:32:57 +000027805 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027806 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27807 (ctxt->user_sax->notationDecl != NULL))
27808 ctxt->user_sax->notationDecl(ctxt->user_data, name, publicId,
27809 systemId);
27810}
27811
27812static void
27813unparsedEntityDeclSplit(void *ctx, const xmlChar *name,
27814 const xmlChar *publicId, const xmlChar *systemId,
27815 const xmlChar *notationName)
27816{
Daniel Veillard971771e2005-07-09 17:32:57 +000027817 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027818 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27819 (ctxt->user_sax->unparsedEntityDecl != NULL))
27820 ctxt->user_sax->unparsedEntityDecl(ctxt->user_data, name, publicId,
27821 systemId, notationName);
27822}
27823
27824static void
27825setDocumentLocatorSplit(void *ctx, xmlSAXLocatorPtr loc)
27826{
Daniel Veillard971771e2005-07-09 17:32:57 +000027827 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027828 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27829 (ctxt->user_sax->setDocumentLocator != NULL))
27830 ctxt->user_sax->setDocumentLocator(ctxt->user_data, loc);
27831}
27832
27833static void
27834startDocumentSplit(void *ctx)
27835{
Daniel Veillard971771e2005-07-09 17:32:57 +000027836 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027837 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27838 (ctxt->user_sax->startDocument != NULL))
27839 ctxt->user_sax->startDocument(ctxt->user_data);
27840}
27841
27842static void
27843endDocumentSplit(void *ctx)
27844{
Daniel Veillard971771e2005-07-09 17:32:57 +000027845 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027846 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27847 (ctxt->user_sax->endDocument != NULL))
27848 ctxt->user_sax->endDocument(ctxt->user_data);
27849}
27850
27851static void
27852processingInstructionSplit(void *ctx, const xmlChar *target,
27853 const xmlChar *data)
27854{
Daniel Veillard971771e2005-07-09 17:32:57 +000027855 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027856 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27857 (ctxt->user_sax->processingInstruction != NULL))
27858 ctxt->user_sax->processingInstruction(ctxt->user_data, target, data);
27859}
27860
27861static void
27862commentSplit(void *ctx, const xmlChar *value)
27863{
Daniel Veillard971771e2005-07-09 17:32:57 +000027864 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027865 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27866 (ctxt->user_sax->comment != NULL))
27867 ctxt->user_sax->comment(ctxt->user_data, value);
27868}
27869
27870/*
27871 * Varargs error callbacks to the user application, harder ...
27872 */
27873
Daniel Veillardffa3c742005-07-21 13:24:09 +000027874static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027875warningSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027876 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027877 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27878 (ctxt->user_sax->warning != NULL)) {
27879 TODO
27880 }
27881}
Daniel Veillardffa3c742005-07-21 13:24:09 +000027882static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027883errorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027884 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027885 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27886 (ctxt->user_sax->error != NULL)) {
27887 TODO
27888 }
27889}
Daniel Veillardffa3c742005-07-21 13:24:09 +000027890static void XMLCDECL
Daniel Veillardafc05b62005-07-17 06:11:19 +000027891fatalErrorSplit(void *ctx, const char *msg ATTRIBUTE_UNUSED, ...) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027892 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027893 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27894 (ctxt->user_sax->fatalError != NULL)) {
27895 TODO
27896 }
27897}
27898
27899/*
27900 * Those are function where both the user handler and the schemas handler
27901 * need to be called.
27902 */
27903static void
27904charactersSplit(void *ctx, const xmlChar *ch, int len)
27905{
Daniel Veillard971771e2005-07-09 17:32:57 +000027906 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027907 if (ctxt == NULL)
27908 return;
27909 if ((ctxt->user_sax != NULL) && (ctxt->user_sax->characters != NULL))
27910 ctxt->user_sax->characters(ctxt->user_data, ch, len);
27911 if (ctxt->ctxt != NULL)
27912 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
27913}
27914
27915static void
27916ignorableWhitespaceSplit(void *ctx, const xmlChar *ch, int len)
27917{
Daniel Veillard971771e2005-07-09 17:32:57 +000027918 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027919 if (ctxt == NULL)
27920 return;
27921 if ((ctxt->user_sax != NULL) &&
27922 (ctxt->user_sax->ignorableWhitespace != NULL))
27923 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, ch, len);
27924 if (ctxt->ctxt != NULL)
27925 xmlSchemaSAXHandleText(ctxt->ctxt, ch, len);
27926}
27927
27928static void
27929cdataBlockSplit(void *ctx, const xmlChar *value, int len)
27930{
Daniel Veillard971771e2005-07-09 17:32:57 +000027931 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027932 if (ctxt == NULL)
27933 return;
27934 if ((ctxt->user_sax != NULL) &&
27935 (ctxt->user_sax->ignorableWhitespace != NULL))
27936 ctxt->user_sax->ignorableWhitespace(ctxt->user_data, value, len);
27937 if (ctxt->ctxt != NULL)
27938 xmlSchemaSAXHandleCDataSection(ctxt->ctxt, value, len);
27939}
27940
27941static void
27942referenceSplit(void *ctx, const xmlChar *name)
27943{
Daniel Veillard971771e2005-07-09 17:32:57 +000027944 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027945 if ((ctxt != NULL) && (ctxt->user_sax != NULL) &&
27946 (ctxt->user_sax->reference != NULL))
27947 ctxt->user_sax->reference(ctxt->user_data, name);
27948 if (ctxt->ctxt != NULL)
27949 xmlSchemaSAXHandleReference(ctxt->user_data, name);
27950}
27951
27952static void
27953startElementNsSplit(void *ctx, const xmlChar * localname,
27954 const xmlChar * prefix, const xmlChar * URI,
27955 int nb_namespaces, const xmlChar ** namespaces,
27956 int nb_attributes, int nb_defaulted,
27957 const xmlChar ** attributes) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027958 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027959 if (ctxt == NULL)
27960 return;
27961 if ((ctxt->user_sax != NULL) &&
27962 (ctxt->user_sax->startElementNs != NULL))
27963 ctxt->user_sax->startElementNs(ctxt->user_data, localname, prefix,
27964 URI, nb_namespaces, namespaces,
27965 nb_attributes, nb_defaulted,
27966 attributes);
27967 if (ctxt->ctxt != NULL)
27968 xmlSchemaSAXHandleStartElementNs(ctxt->ctxt, localname, prefix,
27969 URI, nb_namespaces, namespaces,
27970 nb_attributes, nb_defaulted,
27971 attributes);
27972}
27973
27974static void
27975endElementNsSplit(void *ctx, const xmlChar * localname,
27976 const xmlChar * prefix, const xmlChar * URI) {
Daniel Veillard971771e2005-07-09 17:32:57 +000027977 xmlSchemaSAXPlugPtr ctxt = (xmlSchemaSAXPlugPtr) ctx;
Daniel Veillardcdc82732005-07-08 15:04:06 +000027978 if (ctxt == NULL)
27979 return;
27980 if ((ctxt->user_sax != NULL) &&
27981 (ctxt->user_sax->endElementNs != NULL))
27982 ctxt->user_sax->endElementNs(ctxt->user_data, localname, prefix, URI);
27983 if (ctxt->ctxt != NULL)
27984 xmlSchemaSAXHandleEndElementNs(ctxt->ctxt, localname, prefix, URI);
27985}
27986
Daniel Veillard4255d502002-04-16 15:50:10 +000027987/**
Daniel Veillard971771e2005-07-09 17:32:57 +000027988 * xmlSchemaSAXPlug:
27989 * @ctxt: a schema validation context
Daniel Veillard1f33c4d2005-07-10 21:38:31 +000027990 * @sax: a pointer to the original xmlSAXHandlerPtr
27991 * @user_data: a pointer to the original SAX user data pointer
Daniel Veillard971771e2005-07-09 17:32:57 +000027992 *
27993 * Plug a SAX based validation layer in a SAX parsing event flow.
27994 * The original @saxptr and @dataptr data are replaced by new pointers
27995 * but the calls to the original will be maintained.
27996 *
27997 * Returns a pointer to a data structure needed to unplug the validation layer
27998 * or NULL in case of errors.
27999 */
28000xmlSchemaSAXPlugPtr
28001xmlSchemaSAXPlug(xmlSchemaValidCtxtPtr ctxt,
28002 xmlSAXHandlerPtr *sax, void **user_data)
28003{
28004 xmlSchemaSAXPlugPtr ret;
28005 xmlSAXHandlerPtr old_sax;
28006
28007 if ((ctxt == NULL) || (sax == NULL) || (user_data == NULL))
28008 return(NULL);
28009
28010 /*
28011 * We only allow to plug into SAX2 event streams
28012 */
28013 old_sax = *sax;
28014 if ((old_sax != NULL) && (old_sax->initialized != XML_SAX2_MAGIC))
28015 return(NULL);
28016 if ((old_sax != NULL) &&
28017 (old_sax->startElementNs == NULL) && (old_sax->endElementNs == NULL) &&
28018 ((old_sax->startElement != NULL) || (old_sax->endElement != NULL)))
28019 return(NULL);
28020
28021 /*
28022 * everything seems right allocate the local data needed for that layer
28023 */
28024 ret = (xmlSchemaSAXPlugPtr) xmlMalloc(sizeof(xmlSchemaSAXPlugStruct));
28025 if (ret == NULL) {
28026 return(NULL);
28027 }
28028 memset(ret, 0, sizeof(xmlSchemaSAXPlugStruct));
28029 ret->magic = XML_SAX_PLUG_MAGIC;
28030 ret->schemas_sax.initialized = XML_SAX2_MAGIC;
28031 ret->ctxt = ctxt;
28032 ret->user_sax_ptr = sax;
28033 ret->user_sax = old_sax;
28034 if (old_sax == NULL) {
28035 /*
28036 * go direct, no need for the split block and functions.
28037 */
28038 ret->schemas_sax.startElementNs = xmlSchemaSAXHandleStartElementNs;
28039 ret->schemas_sax.endElementNs = xmlSchemaSAXHandleEndElementNs;
28040 /*
28041 * Note that we use the same text-function for both, to prevent
28042 * the parser from testing for ignorable whitespace.
28043 */
28044 ret->schemas_sax.ignorableWhitespace = xmlSchemaSAXHandleText;
28045 ret->schemas_sax.characters = xmlSchemaSAXHandleText;
28046
28047 ret->schemas_sax.cdataBlock = xmlSchemaSAXHandleCDataSection;
28048 ret->schemas_sax.reference = xmlSchemaSAXHandleReference;
28049
28050 ret->user_data = ctxt;
28051 *user_data = ctxt;
28052 } else {
28053 /*
28054 * for each callback unused by Schemas initialize it to the Split
28055 * routine only if non NULL in the user block, this can speed up
28056 * things at the SAX level.
28057 */
28058 if (old_sax->internalSubset != NULL)
28059 ret->schemas_sax.internalSubset = internalSubsetSplit;
28060 if (old_sax->isStandalone != NULL)
28061 ret->schemas_sax.isStandalone = isStandaloneSplit;
28062 if (old_sax->hasInternalSubset != NULL)
28063 ret->schemas_sax.hasInternalSubset = hasInternalSubsetSplit;
28064 if (old_sax->hasExternalSubset != NULL)
28065 ret->schemas_sax.hasExternalSubset = hasExternalSubsetSplit;
28066 if (old_sax->resolveEntity != NULL)
28067 ret->schemas_sax.resolveEntity = resolveEntitySplit;
28068 if (old_sax->getEntity != NULL)
28069 ret->schemas_sax.getEntity = getEntitySplit;
28070 if (old_sax->entityDecl != NULL)
28071 ret->schemas_sax.entityDecl = entityDeclSplit;
28072 if (old_sax->notationDecl != NULL)
28073 ret->schemas_sax.notationDecl = notationDeclSplit;
28074 if (old_sax->attributeDecl != NULL)
28075 ret->schemas_sax.attributeDecl = attributeDeclSplit;
28076 if (old_sax->elementDecl != NULL)
28077 ret->schemas_sax.elementDecl = elementDeclSplit;
28078 if (old_sax->unparsedEntityDecl != NULL)
28079 ret->schemas_sax.unparsedEntityDecl = unparsedEntityDeclSplit;
28080 if (old_sax->setDocumentLocator != NULL)
28081 ret->schemas_sax.setDocumentLocator = setDocumentLocatorSplit;
28082 if (old_sax->startDocument != NULL)
28083 ret->schemas_sax.startDocument = startDocumentSplit;
28084 if (old_sax->endDocument != NULL)
28085 ret->schemas_sax.endDocument = endDocumentSplit;
28086 if (old_sax->processingInstruction != NULL)
28087 ret->schemas_sax.processingInstruction = processingInstructionSplit;
28088 if (old_sax->comment != NULL)
28089 ret->schemas_sax.comment = commentSplit;
28090 if (old_sax->warning != NULL)
28091 ret->schemas_sax.warning = warningSplit;
28092 if (old_sax->error != NULL)
28093 ret->schemas_sax.error = errorSplit;
28094 if (old_sax->fatalError != NULL)
28095 ret->schemas_sax.fatalError = fatalErrorSplit;
28096 if (old_sax->getParameterEntity != NULL)
28097 ret->schemas_sax.getParameterEntity = getParameterEntitySplit;
28098 if (old_sax->externalSubset != NULL)
28099 ret->schemas_sax.externalSubset = externalSubsetSplit;
28100
28101 /*
28102 * the 6 schemas callback have to go to the splitter functions
28103 * Note that we use the same text-function for ignorableWhitespace
28104 * if possible, to prevent the parser from testing for ignorable
28105 * whitespace.
28106 */
28107 ret->schemas_sax.characters = charactersSplit;
28108 if ((old_sax->ignorableWhitespace != NULL) &&
28109 (old_sax->ignorableWhitespace != old_sax->characters))
28110 ret->schemas_sax.ignorableWhitespace = ignorableWhitespaceSplit;
28111 else
28112 ret->schemas_sax.ignorableWhitespace = charactersSplit;
28113 ret->schemas_sax.cdataBlock = cdataBlockSplit;
28114 ret->schemas_sax.reference = referenceSplit;
28115 ret->schemas_sax.startElementNs = startElementNsSplit;
28116 ret->schemas_sax.endElementNs = endElementNsSplit;
28117
28118 ret->user_data_ptr = user_data;
28119 ret->user_data = *user_data;
28120 *user_data = ret;
28121 }
28122
28123 /*
28124 * plug the pointers back.
28125 */
28126 *sax = &(ret->schemas_sax);
Daniel Veillardf10ae122005-07-10 19:03:16 +000028127 ctxt->sax = *sax;
28128 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28129 xmlSchemaPreRun(ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028130 return(ret);
28131}
28132
28133/**
28134 * xmlSchemaSAXUnplug:
28135 * @plug: a data structure returned by xmlSchemaSAXPlug
28136 *
28137 * Unplug a SAX based validation layer in a SAX parsing event flow.
28138 * The original pointers used in the call are restored.
28139 *
28140 * Returns 0 in case of success and -1 in case of failure.
28141 */
28142int
28143xmlSchemaSAXUnplug(xmlSchemaSAXPlugPtr plug)
28144{
28145 xmlSAXHandlerPtr *sax;
28146 void **user_data;
28147
28148 if ((plug == NULL) || (plug->magic != XML_SAX_PLUG_MAGIC))
28149 return(-1);
28150 plug->magic = 0;
28151
Daniel Veillardf10ae122005-07-10 19:03:16 +000028152 xmlSchemaPostRun(plug->ctxt);
Daniel Veillard971771e2005-07-09 17:32:57 +000028153 /* restore the data */
28154 sax = plug->user_sax_ptr;
28155 *sax = plug->user_sax;
28156 if (plug->user_sax != NULL) {
28157 user_data = plug->user_data_ptr;
28158 *user_data = plug->user_data;
28159 }
28160
28161 /* free and return */
28162 xmlFree(plug);
28163 return(0);
28164}
28165
28166/**
Daniel Veillard4255d502002-04-16 15:50:10 +000028167 * xmlSchemaValidateStream:
28168 * @ctxt: a schema validation context
28169 * @input: the input to use for reading the data
28170 * @enc: an optional encoding information
28171 * @sax: a SAX handler for the resulting events
28172 * @user_data: the context to provide to the SAX handler.
28173 *
Daniel Veillardcdc82732005-07-08 15:04:06 +000028174 * Validate an input based on a flow of SAX event from the parser
28175 * and forward the events to the @sax handler with the provided @user_data
28176 * the user provided @sax handler must be a SAX2 one.
Daniel Veillard4255d502002-04-16 15:50:10 +000028177 *
28178 * Returns 0 if the document is schemas valid, a positive error code
28179 * number otherwise and -1 in case of internal or API error.
28180 */
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028181int
Daniel Veillard4255d502002-04-16 15:50:10 +000028182xmlSchemaValidateStream(xmlSchemaValidCtxtPtr ctxt,
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028183 xmlParserInputBufferPtr input, xmlCharEncoding enc,
28184 xmlSAXHandlerPtr sax, void *user_data)
28185{
Daniel Veillard971771e2005-07-09 17:32:57 +000028186 xmlSchemaSAXPlugPtr plug = NULL;
28187 xmlSAXHandlerPtr old_sax = NULL;
28188 xmlParserCtxtPtr pctxt = NULL;
28189 xmlParserInputPtr inputStream = NULL;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028190 int ret;
28191
Daniel Veillard4255d502002-04-16 15:50:10 +000028192 if ((ctxt == NULL) || (input == NULL))
Daniel Veillardd0c9c322003-10-10 00:49:42 +000028193 return (-1);
Daniel Veillardcdc82732005-07-08 15:04:06 +000028194
Daniel Veillardcdc82732005-07-08 15:04:06 +000028195 /*
28196 * prepare the parser
28197 */
28198 pctxt = xmlNewParserCtxt();
28199 if (pctxt == NULL)
28200 return (-1);
28201 old_sax = pctxt->sax;
Daniel Veillard971771e2005-07-09 17:32:57 +000028202 pctxt->sax = sax;
28203 pctxt->userData = user_data;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028204#if 0
28205 if (options)
28206 xmlCtxtUseOptions(pctxt, options);
28207#endif
Kasimier T. Buchcik85aed6d2005-07-08 18:06:18 +000028208 pctxt->linenumbers = 1;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028209
Daniel Veillardf0af8ec2005-07-08 17:27:33 +000028210 inputStream = xmlNewIOInputStream(pctxt, input, enc);;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028211 if (inputStream == NULL) {
28212 ret = -1;
28213 goto done;
28214 }
28215 inputPush(pctxt, inputStream);
28216 ctxt->parserCtxt = pctxt;
28217 ctxt->input = input;
28218
28219 /*
Daniel Veillard971771e2005-07-09 17:32:57 +000028220 * Plug the validation and launch the parsing
Daniel Veillardcdc82732005-07-08 15:04:06 +000028221 */
Daniel Veillard971771e2005-07-09 17:32:57 +000028222 plug = xmlSchemaSAXPlug(ctxt, &(pctxt->sax), &(pctxt->userData));
28223 if (plug == NULL) {
28224 ret = -1;
28225 goto done;
28226 }
28227 ctxt->input = input;
28228 ctxt->enc = enc;
28229 ctxt->sax = pctxt->sax;
Daniel Veillardcdc82732005-07-08 15:04:06 +000028230 ctxt->flags |= XML_SCHEMA_VALID_CTXT_FLAG_STREAM;
28231 ret = xmlSchemaVStart(ctxt);
28232
28233 if ((ret == 0) && (! ctxt->parserCtxt->wellFormed)) {
28234 ret = ctxt->parserCtxt->errNo;
28235 if (ret == 0)
28236 ret = 1;
28237 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028238
28239done:
Daniel Veillard971771e2005-07-09 17:32:57 +000028240 ctxt->parserCtxt = NULL;
28241 ctxt->sax = NULL;
28242 ctxt->input = NULL;
28243 if (plug != NULL) {
28244 xmlSchemaSAXUnplug(plug);
28245 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028246 /* cleanup */
Daniel Veillard971771e2005-07-09 17:32:57 +000028247 if (pctxt != NULL) {
28248 pctxt->sax = old_sax;
28249 xmlFreeParserCtxt(pctxt);
28250 }
Daniel Veillardcdc82732005-07-08 15:04:06 +000028251 return (ret);
Daniel Veillard4255d502002-04-16 15:50:10 +000028252}
28253
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028254/**
28255 * xmlSchemaValidateFile:
28256 * @ctxt: a schema validation context
Daniel Veillard81562d22005-06-15 13:27:56 +000028257 * @filename: the URI of the instance
28258 * @options: a future set of options, currently unused
28259 *
28260 * Do a schemas validation of the given resource, it will use the
28261 * SAX streamable validation internally.
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028262 *
28263 * Returns 0 if the document is valid, a positive error code
28264 * number otherwise and -1 in case of an internal or API error.
28265 */
28266int
28267xmlSchemaValidateFile(xmlSchemaValidCtxtPtr ctxt,
Kasimier T. Buchcikc63fbbf2005-06-15 12:54:05 +000028268 const char * filename,
28269 int options ATTRIBUTE_UNUSED)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028270{
28271 int ret;
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028272 xmlParserInputBufferPtr input;
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028273
28274 if ((ctxt == NULL) || (filename == NULL))
28275 return (-1);
28276
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028277 input = xmlParserInputBufferCreateFilename(filename,
28278 XML_CHAR_ENCODING_NONE);
28279 if (input == NULL)
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028280 return (-1);
Kasimier T. Buchcik72d3adc2005-07-08 16:43:37 +000028281 ret = xmlSchemaValidateStream(ctxt, input, XML_CHAR_ENCODING_NONE,
28282 NULL, NULL);
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028283 return (ret);
Daniel Veillard81562d22005-06-15 13:27:56 +000028284}
Kasimier T. Buchcik4efd90d2005-06-09 10:32:53 +000028285
Daniel Veillard5d4644e2005-04-01 13:11:58 +000028286#define bottom_xmlschemas
28287#include "elfgcchack.h"
Daniel Veillard4255d502002-04-16 15:50:10 +000028288#endif /* LIBXML_SCHEMAS_ENABLED */