05a12e0b34cb96c8e3b9d570acd9b7d80b4c3b10
[reactos.git] / sdk / lib / 3rdparty / libxml2 / xmlschemas.c
1 /*
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
10 /*
11 * 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.
15 * - if we don't intend to use the schema for schemas, we
16 * need to validate all schema attributes (ref, type, name)
17 * against their types.
18 * - Eliminate item creation for: ??
19 *
20 * URGENT TODO:
21 * - For xsi-driven schema acquisition, augment the IDCs after every
22 * acquisition episode (xmlSchemaAugmentIDC).
23 *
24 * NOTES:
25 * - Elimated item creation for: <restriction>, <extension>,
26 * <simpleContent>, <complexContent>, <list>, <union>
27 *
28 * 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.
46 */
47
48 /* To avoid EBCDIC trouble when parsing on zOS */
49 #if defined(__MVS__)
50 #pragma convert("ISO8859-1")
51 #endif
52
53 #define IN_LIBXML
54 #include "libxml.h"
55
56 #ifdef LIBXML_SCHEMAS_ENABLED
57
58 #include <string.h>
59 #include <libxml/xmlmemory.h>
60 #include <libxml/parser.h>
61 #include <libxml/parserInternals.h>
62 #include <libxml/hash.h>
63 #include <libxml/uri.h>
64 #include <libxml/xmlschemas.h>
65 #include <libxml/schemasInternals.h>
66 #include <libxml/xmlschemastypes.h>
67 #include <libxml/xmlautomata.h>
68 #include <libxml/xmlregexp.h>
69 #include <libxml/dict.h>
70 #include <libxml/encoding.h>
71 #include <libxml/xmlIO.h>
72 #ifdef LIBXML_PATTERN_ENABLED
73 #include <libxml/pattern.h>
74 #endif
75 #ifdef LIBXML_READER_ENABLED
76 #include <libxml/xmlreader.h>
77 #endif
78
79 /* #define DEBUG 1 */
80
81 /* #define DEBUG_CONTENT 1 */
82
83 /* #define DEBUG_TYPE 1 */
84
85 /* #define DEBUG_CONTENT_REGEXP 1 */
86
87 /* #define DEBUG_AUTOMATA 1 */
88
89 /* #define DEBUG_IDC */
90
91 /* #define DEBUG_IDC_NODE_TABLE */
92
93 /* #define WXS_ELEM_DECL_CONS_ENABLED */
94
95 #ifdef DEBUG_IDC
96 #ifndef DEBUG_IDC_NODE_TABLE
97 #define DEBUG_IDC_NODE_TABLE
98 #endif
99 #endif
100
101 /* #define ENABLE_PARTICLE_RESTRICTION 1 */
102
103 #define ENABLE_REDEFINE
104
105 /* #define ENABLE_NAMED_LOCALS */
106
107 /* #define ENABLE_IDC_NODE_TABLES_TEST */
108
109 #define DUMP_CONTENT_MODEL
110
111 #ifdef LIBXML_READER_ENABLED
112 /* #define XML_SCHEMA_READER_ENABLED */
113 #endif
114
115 #define UNBOUNDED (1 << 30)
116 #define TODO \
117 xmlGenericError(xmlGenericErrorContext, \
118 "Unimplemented block at %s:%d\n", \
119 __FILE__, __LINE__);
120
121 #define XML_SCHEMAS_NO_NAMESPACE (const xmlChar *) "##"
122
123 /*
124 * The XML Schemas namespaces
125 */
126 static const xmlChar *xmlSchemaNs = (const xmlChar *)
127 "http://www.w3.org/2001/XMLSchema";
128
129 static const xmlChar *xmlSchemaInstanceNs = (const xmlChar *)
130 "http://www.w3.org/2001/XMLSchema-instance";
131
132 static const xmlChar *xmlNamespaceNs = (const xmlChar *)
133 "http://www.w3.org/2000/xmlns/";
134
135 /*
136 * Come casting macros.
137 */
138 #define ACTXT_CAST (xmlSchemaAbstractCtxtPtr)
139 #define PCTXT_CAST (xmlSchemaParserCtxtPtr)
140 #define VCTXT_CAST (xmlSchemaValidCtxtPtr)
141 #define WXS_BASIC_CAST (xmlSchemaBasicItemPtr)
142 #define WXS_TREE_CAST (xmlSchemaTreeItemPtr)
143 #define WXS_PTC_CAST (xmlSchemaParticlePtr)
144 #define WXS_TYPE_CAST (xmlSchemaTypePtr)
145 #define WXS_ELEM_CAST (xmlSchemaElementPtr)
146 #define WXS_ATTR_GROUP_CAST (xmlSchemaAttributeGroupPtr)
147 #define WXS_ATTR_CAST (xmlSchemaAttributePtr)
148 #define WXS_ATTR_USE_CAST (xmlSchemaAttributeUsePtr)
149 #define WXS_ATTR_PROHIB_CAST (xmlSchemaAttributeUseProhibPtr)
150 #define WXS_MODEL_GROUPDEF_CAST (xmlSchemaModelGroupDefPtr)
151 #define WXS_MODEL_GROUP_CAST (xmlSchemaModelGroupPtr)
152 #define WXS_IDC_CAST (xmlSchemaIDCPtr)
153 #define WXS_QNAME_CAST (xmlSchemaQNameRefPtr)
154 #define WXS_LIST_CAST (xmlSchemaItemListPtr)
155
156 /*
157 * Macros to query common properties of components.
158 */
159 #define WXS_ITEM_NODE(i) xmlSchemaGetComponentNode(WXS_BASIC_CAST (i))
160
161 #define WXS_ITEM_TYPE_NAME(i) xmlSchemaGetComponentTypeStr(WXS_BASIC_CAST (i))
162 /*
163 * Macros for element declarations.
164 */
165 #define WXS_ELEM_TYPEDEF(e) (e)->subtypes
166
167 #define WXS_SUBST_HEAD(item) (item)->refDecl
168 /*
169 * Macros for attribute declarations.
170 */
171 #define WXS_ATTR_TYPEDEF(a) (a)->subtypes
172 /*
173 * Macros for attribute uses.
174 */
175 #define WXS_ATTRUSE_DECL(au) (WXS_ATTR_USE_CAST (au))->attrDecl
176
177 #define WXS_ATTRUSE_TYPEDEF(au) WXS_ATTR_TYPEDEF(WXS_ATTRUSE_DECL( WXS_ATTR_USE_CAST au))
178
179 #define WXS_ATTRUSE_DECL_NAME(au) (WXS_ATTRUSE_DECL(au))->name
180
181 #define WXS_ATTRUSE_DECL_TNS(au) (WXS_ATTRUSE_DECL(au))->targetNamespace
182 /*
183 * Macros for attribute groups.
184 */
185 #define WXS_ATTR_GROUP_HAS_REFS(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS)
186 #define WXS_ATTR_GROUP_EXPANDED(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED)
187 /*
188 * Macros for particles.
189 */
190 #define WXS_PARTICLE(p) WXS_PTC_CAST (p)
191
192 #define WXS_PARTICLE_TERM(p) (WXS_PARTICLE(p))->children
193
194 #define WXS_PARTICLE_TERM_AS_ELEM(p) (WXS_ELEM_CAST WXS_PARTICLE_TERM(p))
195
196 #define WXS_PARTICLE_MODEL(p) WXS_MODEL_GROUP_CAST WXS_PARTICLE(p)->children
197 /*
198 * Macros for model groups definitions.
199 */
200 #define WXS_MODELGROUPDEF_MODEL(mgd) (WXS_MODEL_GROUP_CAST (mgd))->children
201 /*
202 * Macros for model groups.
203 */
204 #define WXS_IS_MODEL_GROUP(i) \
205 (((i)->type == XML_SCHEMA_TYPE_SEQUENCE) || \
206 ((i)->type == XML_SCHEMA_TYPE_CHOICE) || \
207 ((i)->type == XML_SCHEMA_TYPE_ALL))
208
209 #define WXS_MODELGROUP_PARTICLE(mg) WXS_PTC_CAST (mg)->children
210 /*
211 * Macros for schema buckets.
212 */
213 #define WXS_IS_BUCKET_INCREDEF(t) (((t) == XML_SCHEMA_SCHEMA_INCLUDE) || \
214 ((t) == XML_SCHEMA_SCHEMA_REDEFINE))
215
216 #define WXS_IS_BUCKET_IMPMAIN(t) (((t) == XML_SCHEMA_SCHEMA_MAIN) || \
217 ((t) == XML_SCHEMA_SCHEMA_IMPORT))
218
219 #define WXS_IMPBUCKET(b) ((xmlSchemaImportPtr) (b))
220
221 #define WXS_INCBUCKET(b) ((xmlSchemaIncludePtr) (b))
222 /*
223 * Macros for complex/simple types.
224 */
225 #define WXS_IS_ANYTYPE(i) \
226 (( (i)->type == XML_SCHEMA_TYPE_BASIC) && \
227 ( (WXS_TYPE_CAST (i))->builtInType == XML_SCHEMAS_ANYTYPE))
228
229 #define WXS_IS_COMPLEX(i) \
230 (((i)->type == XML_SCHEMA_TYPE_COMPLEX) || \
231 ((i)->builtInType == XML_SCHEMAS_ANYTYPE))
232
233 #define WXS_IS_SIMPLE(item) \
234 ((item->type == XML_SCHEMA_TYPE_SIMPLE) || \
235 ((item->type == XML_SCHEMA_TYPE_BASIC) && \
236 (item->builtInType != XML_SCHEMAS_ANYTYPE)))
237
238 #define WXS_IS_ANY_SIMPLE_TYPE(i) \
239 (((i)->type == XML_SCHEMA_TYPE_BASIC) && \
240 ((i)->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))
241
242 #define WXS_IS_RESTRICTION(t) \
243 ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION)
244
245 #define WXS_IS_EXTENSION(t) \
246 ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION)
247
248 #define WXS_IS_TYPE_NOT_FIXED(i) \
249 (((i)->type != XML_SCHEMA_TYPE_BASIC) && \
250 (((i)->flags & XML_SCHEMAS_TYPE_INTERNAL_RESOLVED) == 0))
251
252 #define WXS_IS_TYPE_NOT_FIXED_1(item) \
253 (((item)->type != XML_SCHEMA_TYPE_BASIC) && \
254 (((item)->flags & XML_SCHEMAS_TYPE_FIXUP_1) == 0))
255
256 #define WXS_TYPE_IS_GLOBAL(t) ((t)->flags & XML_SCHEMAS_TYPE_GLOBAL)
257
258 #define WXS_TYPE_IS_LOCAL(t) (((t)->flags & XML_SCHEMAS_TYPE_GLOBAL) == 0)
259 /*
260 * Macros for exclusively for complex types.
261 */
262 #define WXS_HAS_COMPLEX_CONTENT(item) \
263 ((item->contentType == XML_SCHEMA_CONTENT_MIXED) || \
264 (item->contentType == XML_SCHEMA_CONTENT_EMPTY) || \
265 (item->contentType == XML_SCHEMA_CONTENT_ELEMENTS))
266
267 #define WXS_HAS_SIMPLE_CONTENT(item) \
268 ((item->contentType == XML_SCHEMA_CONTENT_SIMPLE) || \
269 (item->contentType == XML_SCHEMA_CONTENT_BASIC))
270
271 #define WXS_HAS_MIXED_CONTENT(item) \
272 (item->contentType == XML_SCHEMA_CONTENT_MIXED)
273
274 #define WXS_EMPTIABLE(t) \
275 (xmlSchemaIsParticleEmptiable(WXS_PTC_CAST (t)->subtypes))
276
277 #define WXS_TYPE_CONTENTTYPE(t) (t)->subtypes
278
279 #define WXS_TYPE_PARTICLE(t) WXS_PTC_CAST (t)->subtypes
280
281 #define WXS_TYPE_PARTICLE_TERM(t) WXS_PARTICLE_TERM(WXS_TYPE_PARTICLE(t))
282 /*
283 * Macros for exclusively for simple types.
284 */
285 #define WXS_LIST_ITEMTYPE(t) (t)->subtypes
286
287 #define WXS_IS_ATOMIC(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_ATOMIC)
288
289 #define WXS_IS_LIST(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_LIST)
290
291 #define WXS_IS_UNION(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_UNION)
292 /*
293 * Misc parser context macros.
294 */
295 #define WXS_CONSTRUCTOR(ctx) (ctx)->constructor
296
297 #define WXS_HAS_BUCKETS(ctx) \
298 ( (WXS_CONSTRUCTOR((ctx))->buckets != NULL) && \
299 (WXS_CONSTRUCTOR((ctx))->buckets->nbItems > 0) )
300
301 #define WXS_SUBST_GROUPS(ctx) WXS_CONSTRUCTOR((ctx))->substGroups
302
303 #define WXS_BUCKET(ctx) WXS_CONSTRUCTOR((ctx))->bucket
304
305 #define WXS_SCHEMA(ctx) (ctx)->schema
306
307 #define WXS_ADD_LOCAL(ctx, item) \
308 xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->locals), 10, item)
309
310 #define WXS_ADD_GLOBAL(ctx, item) \
311 xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->globals), 5, item)
312
313 #define WXS_ADD_PENDING(ctx, item) \
314 xmlSchemaAddItemSize(&((ctx)->constructor->pending), 10, item)
315 /*
316 * xmlSchemaItemList macros.
317 */
318 #define WXS_ILIST_IS_EMPTY(l) ((l == NULL) || ((l)->nbItems == 0))
319 /*
320 * Misc macros.
321 */
322 #define IS_SCHEMA(node, type) \
323 ((node != NULL) && (node->ns != NULL) && \
324 (xmlStrEqual(node->name, (const xmlChar *) type)) && \
325 (xmlStrEqual(node->ns->href, xmlSchemaNs)))
326
327 #define FREE_AND_NULL(str) if ((str) != NULL) { xmlFree((xmlChar *) (str)); str = NULL; }
328
329 /*
330 * Since we put the default/fixed values into the dict, we can
331 * use pointer comparison for those values.
332 * REMOVED: (xmlStrEqual((v1), (v2)))
333 */
334 #define WXS_ARE_DEFAULT_STR_EQUAL(v1, v2) ((v1) == (v2))
335
336 #define INODE_NILLED(item) (item->flags & XML_SCHEMA_ELEM_INFO_NILLED)
337
338 #define CAN_PARSE_SCHEMA(b) (((b)->doc != NULL) && ((b)->parsed == 0))
339
340 #define HFAILURE if (res == -1) goto exit_failure;
341
342 #define HERROR if (res != 0) goto exit_error;
343
344 #define HSTOP(ctx) if ((ctx)->stop) goto exit;
345 /*
346 * Some flags used for various schema constraints.
347 */
348 #define SUBSET_RESTRICTION 1<<0
349 #define SUBSET_EXTENSION 1<<1
350 #define SUBSET_SUBSTITUTION 1<<2
351 #define SUBSET_LIST 1<<3
352 #define SUBSET_UNION 1<<4
353
354 typedef struct _xmlSchemaNodeInfo xmlSchemaNodeInfo;
355 typedef xmlSchemaNodeInfo *xmlSchemaNodeInfoPtr;
356
357 typedef struct _xmlSchemaItemList xmlSchemaItemList;
358 typedef xmlSchemaItemList *xmlSchemaItemListPtr;
359 struct _xmlSchemaItemList {
360 void **items; /* used for dynamic addition of schemata */
361 int nbItems; /* used for dynamic addition of schemata */
362 int sizeItems; /* used for dynamic addition of schemata */
363 };
364
365 #define XML_SCHEMA_CTXT_PARSER 1
366 #define XML_SCHEMA_CTXT_VALIDATOR 2
367
368 typedef struct _xmlSchemaAbstractCtxt xmlSchemaAbstractCtxt;
369 typedef xmlSchemaAbstractCtxt *xmlSchemaAbstractCtxtPtr;
370 struct _xmlSchemaAbstractCtxt {
371 int type; /* E.g. XML_SCHEMA_CTXT_VALIDATOR */
372 void *dummy; /* Fix alignment issues */
373 };
374
375 typedef struct _xmlSchemaBucket xmlSchemaBucket;
376 typedef xmlSchemaBucket *xmlSchemaBucketPtr;
377
378 #define XML_SCHEMA_SCHEMA_MAIN 0
379 #define XML_SCHEMA_SCHEMA_IMPORT 1
380 #define XML_SCHEMA_SCHEMA_INCLUDE 2
381 #define XML_SCHEMA_SCHEMA_REDEFINE 3
382
383 /**
384 * xmlSchemaSchemaRelation:
385 *
386 * Used to create a graph of schema relationships.
387 */
388 typedef struct _xmlSchemaSchemaRelation xmlSchemaSchemaRelation;
389 typedef xmlSchemaSchemaRelation *xmlSchemaSchemaRelationPtr;
390 struct _xmlSchemaSchemaRelation {
391 xmlSchemaSchemaRelationPtr next;
392 int type; /* E.g. XML_SCHEMA_SCHEMA_IMPORT */
393 const xmlChar *importNamespace;
394 xmlSchemaBucketPtr bucket;
395 };
396
397 #define XML_SCHEMA_BUCKET_MARKED 1<<0
398 #define XML_SCHEMA_BUCKET_COMPS_ADDED 1<<1
399
400 struct _xmlSchemaBucket {
401 int type;
402 int flags;
403 const xmlChar *schemaLocation;
404 const xmlChar *origTargetNamespace;
405 const xmlChar *targetNamespace;
406 xmlDocPtr doc;
407 xmlSchemaSchemaRelationPtr relations;
408 int located;
409 int parsed;
410 int imported;
411 int preserveDoc;
412 xmlSchemaItemListPtr globals; /* Global components. */
413 xmlSchemaItemListPtr locals; /* Local components. */
414 };
415
416 /**
417 * xmlSchemaImport:
418 * (extends xmlSchemaBucket)
419 *
420 * Reflects a schema. Holds some information
421 * about the schema and its toplevel components. Duplicate
422 * toplevel components are not checked at this level.
423 */
424 typedef struct _xmlSchemaImport xmlSchemaImport;
425 typedef xmlSchemaImport *xmlSchemaImportPtr;
426 struct _xmlSchemaImport {
427 int type; /* Main OR import OR include. */
428 int flags;
429 const xmlChar *schemaLocation; /* The URI of the schema document. */
430 /* For chameleon includes, @origTargetNamespace will be NULL */
431 const xmlChar *origTargetNamespace;
432 /*
433 * For chameleon includes, @targetNamespace will be the
434 * targetNamespace of the including schema.
435 */
436 const xmlChar *targetNamespace;
437 xmlDocPtr doc; /* The schema node-tree. */
438 /* @relations will hold any included/imported/redefined schemas. */
439 xmlSchemaSchemaRelationPtr relations;
440 int located;
441 int parsed;
442 int imported;
443 int preserveDoc;
444 xmlSchemaItemListPtr globals;
445 xmlSchemaItemListPtr locals;
446 /* The imported schema. */
447 xmlSchemaPtr schema;
448 };
449
450 /*
451 * (extends xmlSchemaBucket)
452 */
453 typedef struct _xmlSchemaInclude xmlSchemaInclude;
454 typedef xmlSchemaInclude *xmlSchemaIncludePtr;
455 struct _xmlSchemaInclude {
456 int type;
457 int flags;
458 const xmlChar *schemaLocation;
459 const xmlChar *origTargetNamespace;
460 const xmlChar *targetNamespace;
461 xmlDocPtr doc;
462 xmlSchemaSchemaRelationPtr relations;
463 int located;
464 int parsed;
465 int imported;
466 int preserveDoc;
467 xmlSchemaItemListPtr globals; /* Global components. */
468 xmlSchemaItemListPtr locals; /* Local components. */
469
470 /* The owning main or import schema bucket. */
471 xmlSchemaImportPtr ownerImport;
472 };
473
474 /**
475 * xmlSchemaBasicItem:
476 *
477 * The abstract base type for schema components.
478 */
479 typedef struct _xmlSchemaBasicItem xmlSchemaBasicItem;
480 typedef xmlSchemaBasicItem *xmlSchemaBasicItemPtr;
481 struct _xmlSchemaBasicItem {
482 xmlSchemaTypeType type;
483 void *dummy; /* Fix alignment issues */
484 };
485
486 /**
487 * xmlSchemaAnnotItem:
488 *
489 * The abstract base type for annotated schema components.
490 * (Extends xmlSchemaBasicItem)
491 */
492 typedef struct _xmlSchemaAnnotItem xmlSchemaAnnotItem;
493 typedef xmlSchemaAnnotItem *xmlSchemaAnnotItemPtr;
494 struct _xmlSchemaAnnotItem {
495 xmlSchemaTypeType type;
496 xmlSchemaAnnotPtr annot;
497 };
498
499 /**
500 * xmlSchemaTreeItem:
501 *
502 * The abstract base type for tree-like structured schema components.
503 * (Extends xmlSchemaAnnotItem)
504 */
505 typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem;
506 typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr;
507 struct _xmlSchemaTreeItem {
508 xmlSchemaTypeType type;
509 xmlSchemaAnnotPtr annot;
510 xmlSchemaTreeItemPtr next;
511 xmlSchemaTreeItemPtr children;
512 };
513
514
515 #define XML_SCHEMA_ATTR_USE_FIXED 1<<0
516 /**
517 * xmlSchemaAttributeUsePtr:
518 *
519 * The abstract base type for tree-like structured schema components.
520 * (Extends xmlSchemaTreeItem)
521 */
522 typedef struct _xmlSchemaAttributeUse xmlSchemaAttributeUse;
523 typedef xmlSchemaAttributeUse *xmlSchemaAttributeUsePtr;
524 struct _xmlSchemaAttributeUse {
525 xmlSchemaTypeType type;
526 xmlSchemaAnnotPtr annot;
527 xmlSchemaAttributeUsePtr next; /* The next attr. use. */
528 /*
529 * The attr. decl. OR a QName-ref. to an attr. decl. OR
530 * a QName-ref. to an attribute group definition.
531 */
532 xmlSchemaAttributePtr attrDecl;
533
534 int flags;
535 xmlNodePtr node;
536 int occurs; /* required, optional */
537 const xmlChar * defValue;
538 xmlSchemaValPtr defVal;
539 };
540
541 /**
542 * xmlSchemaAttributeUseProhibPtr:
543 *
544 * A helper component to reflect attribute prohibitions.
545 * (Extends xmlSchemaBasicItem)
546 */
547 typedef struct _xmlSchemaAttributeUseProhib xmlSchemaAttributeUseProhib;
548 typedef xmlSchemaAttributeUseProhib *xmlSchemaAttributeUseProhibPtr;
549 struct _xmlSchemaAttributeUseProhib {
550 xmlSchemaTypeType type; /* == XML_SCHEMA_EXTRA_ATTR_USE_PROHIB */
551 xmlNodePtr node;
552 const xmlChar *name;
553 const xmlChar *targetNamespace;
554 int isRef;
555 };
556
557 /**
558 * xmlSchemaRedef:
559 */
560 typedef struct _xmlSchemaRedef xmlSchemaRedef;
561 typedef xmlSchemaRedef *xmlSchemaRedefPtr;
562 struct _xmlSchemaRedef {
563 xmlSchemaRedefPtr next;
564 xmlSchemaBasicItemPtr item; /* The redefining component. */
565 xmlSchemaBasicItemPtr reference; /* The referencing component. */
566 xmlSchemaBasicItemPtr target; /* The to-be-redefined component. */
567 const xmlChar *refName; /* The name of the to-be-redefined component. */
568 const xmlChar *refTargetNs; /* The target namespace of the
569 to-be-redefined comp. */
570 xmlSchemaBucketPtr targetBucket; /* The redefined schema. */
571 };
572
573 /**
574 * xmlSchemaConstructionCtxt:
575 */
576 typedef struct _xmlSchemaConstructionCtxt xmlSchemaConstructionCtxt;
577 typedef xmlSchemaConstructionCtxt *xmlSchemaConstructionCtxtPtr;
578 struct _xmlSchemaConstructionCtxt {
579 xmlSchemaPtr mainSchema; /* The main schema. */
580 xmlSchemaBucketPtr mainBucket; /* The main schema bucket */
581 xmlDictPtr dict;
582 xmlSchemaItemListPtr buckets; /* List of schema buckets. */
583 /* xmlSchemaItemListPtr relations; */ /* List of schema relations. */
584 xmlSchemaBucketPtr bucket; /* The current schema bucket */
585 xmlSchemaItemListPtr pending; /* All Components of all schemas that
586 need to be fixed. */
587 xmlHashTablePtr substGroups;
588 xmlSchemaRedefPtr redefs;
589 xmlSchemaRedefPtr lastRedef;
590 };
591
592 #define XML_SCHEMAS_PARSE_ERROR 1
593 #define SCHEMAS_PARSE_OPTIONS XML_PARSE_NOENT
594
595 struct _xmlSchemaParserCtxt {
596 int type;
597 void *errCtxt; /* user specific error context */
598 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
599 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
600 int err;
601 int nberrors;
602 xmlStructuredErrorFunc serror;
603
604 xmlSchemaConstructionCtxtPtr constructor;
605 int ownsConstructor; /* TODO: Move this to parser *flags*. */
606
607 /* xmlSchemaPtr topschema; */
608 /* xmlHashTablePtr namespaces; */
609
610 xmlSchemaPtr schema; /* The main schema in use */
611 int counter;
612
613 const xmlChar *URL;
614 xmlDocPtr doc;
615 int preserve; /* Whether the doc should be freed */
616
617 const char *buffer;
618 int size;
619
620 /*
621 * Used to build complex element content models
622 */
623 xmlAutomataPtr am;
624 xmlAutomataStatePtr start;
625 xmlAutomataStatePtr end;
626 xmlAutomataStatePtr state;
627
628 xmlDictPtr dict; /* dictionary for interned string names */
629 xmlSchemaTypePtr ctxtType; /* The current context simple/complex type */
630 int options;
631 xmlSchemaValidCtxtPtr vctxt;
632 int isS4S;
633 int isRedefine;
634 int xsiAssemble;
635 int stop; /* If the parser should stop; i.e. a critical error. */
636 const xmlChar *targetNamespace;
637 xmlSchemaBucketPtr redefined; /* The schema to be redefined. */
638
639 xmlSchemaRedefPtr redef; /* Used for redefinitions. */
640 int redefCounter; /* Used for redefinitions. */
641 xmlSchemaItemListPtr attrProhibs;
642 };
643
644 /**
645 * xmlSchemaQNameRef:
646 *
647 * A component reference item (not a schema component)
648 * (Extends xmlSchemaBasicItem)
649 */
650 typedef struct _xmlSchemaQNameRef xmlSchemaQNameRef;
651 typedef xmlSchemaQNameRef *xmlSchemaQNameRefPtr;
652 struct _xmlSchemaQNameRef {
653 xmlSchemaTypeType type;
654 xmlSchemaBasicItemPtr item; /* The resolved referenced item. */
655 xmlSchemaTypeType itemType;
656 const xmlChar *name;
657 const xmlChar *targetNamespace;
658 xmlNodePtr node;
659 };
660
661 /**
662 * xmlSchemaParticle:
663 *
664 * A particle component.
665 * (Extends xmlSchemaTreeItem)
666 */
667 typedef struct _xmlSchemaParticle xmlSchemaParticle;
668 typedef xmlSchemaParticle *xmlSchemaParticlePtr;
669 struct _xmlSchemaParticle {
670 xmlSchemaTypeType type;
671 xmlSchemaAnnotPtr annot;
672 xmlSchemaTreeItemPtr next; /* next particle */
673 xmlSchemaTreeItemPtr children; /* the "term" (e.g. a model group,
674 a group definition, a XML_SCHEMA_EXTRA_QNAMEREF (if a reference),
675 etc.) */
676 int minOccurs;
677 int maxOccurs;
678 xmlNodePtr node;
679 };
680
681 /**
682 * xmlSchemaModelGroup:
683 *
684 * A model group component.
685 * (Extends xmlSchemaTreeItem)
686 */
687 typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup;
688 typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr;
689 struct _xmlSchemaModelGroup {
690 xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_SEQUENCE, XML_SCHEMA_TYPE_CHOICE, XML_SCHEMA_TYPE_ALL */
691 xmlSchemaAnnotPtr annot;
692 xmlSchemaTreeItemPtr next; /* not used */
693 xmlSchemaTreeItemPtr children; /* first particle (OR "element decl" OR "wildcard") */
694 xmlNodePtr node;
695 };
696
697 #define XML_SCHEMA_MODEL_GROUP_DEF_MARKED 1<<0
698 #define XML_SCHEMA_MODEL_GROUP_DEF_REDEFINED 1<<1
699 /**
700 * xmlSchemaModelGroupDef:
701 *
702 * A model group definition component.
703 * (Extends xmlSchemaTreeItem)
704 */
705 typedef struct _xmlSchemaModelGroupDef xmlSchemaModelGroupDef;
706 typedef xmlSchemaModelGroupDef *xmlSchemaModelGroupDefPtr;
707 struct _xmlSchemaModelGroupDef {
708 xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_GROUP */
709 xmlSchemaAnnotPtr annot;
710 xmlSchemaTreeItemPtr next; /* not used */
711 xmlSchemaTreeItemPtr children; /* the "model group" */
712 const xmlChar *name;
713 const xmlChar *targetNamespace;
714 xmlNodePtr node;
715 int flags;
716 };
717
718 typedef struct _xmlSchemaIDC xmlSchemaIDC;
719 typedef xmlSchemaIDC *xmlSchemaIDCPtr;
720
721 /**
722 * xmlSchemaIDCSelect:
723 *
724 * The identity-constraint "field" and "selector" item, holding the
725 * XPath expression.
726 */
727 typedef struct _xmlSchemaIDCSelect xmlSchemaIDCSelect;
728 typedef xmlSchemaIDCSelect *xmlSchemaIDCSelectPtr;
729 struct _xmlSchemaIDCSelect {
730 xmlSchemaIDCSelectPtr next;
731 xmlSchemaIDCPtr idc;
732 int index; /* an index position if significant for IDC key-sequences */
733 const xmlChar *xpath; /* the XPath expression */
734 void *xpathComp; /* the compiled XPath expression */
735 };
736
737 /**
738 * xmlSchemaIDC:
739 *
740 * The identity-constraint definition component.
741 * (Extends xmlSchemaAnnotItem)
742 */
743
744 struct _xmlSchemaIDC {
745 xmlSchemaTypeType type;
746 xmlSchemaAnnotPtr annot;
747 xmlSchemaIDCPtr next;
748 xmlNodePtr node;
749 const xmlChar *name;
750 const xmlChar *targetNamespace;
751 xmlSchemaIDCSelectPtr selector;
752 xmlSchemaIDCSelectPtr fields;
753 int nbFields;
754 xmlSchemaQNameRefPtr ref;
755 };
756
757 /**
758 * xmlSchemaIDCAug:
759 *
760 * The augmented IDC information used for validation.
761 */
762 typedef struct _xmlSchemaIDCAug xmlSchemaIDCAug;
763 typedef xmlSchemaIDCAug *xmlSchemaIDCAugPtr;
764 struct _xmlSchemaIDCAug {
765 xmlSchemaIDCAugPtr next; /* next in a list */
766 xmlSchemaIDCPtr def; /* the IDC definition */
767 int keyrefDepth; /* the lowest tree level to which IDC
768 tables need to be bubbled upwards */
769 };
770
771 /**
772 * xmlSchemaPSVIIDCKeySequence:
773 *
774 * The key sequence of a node table item.
775 */
776 typedef struct _xmlSchemaPSVIIDCKey xmlSchemaPSVIIDCKey;
777 typedef xmlSchemaPSVIIDCKey *xmlSchemaPSVIIDCKeyPtr;
778 struct _xmlSchemaPSVIIDCKey {
779 xmlSchemaTypePtr type;
780 xmlSchemaValPtr val;
781 };
782
783 /**
784 * xmlSchemaPSVIIDCNode:
785 *
786 * The node table item of a node table.
787 */
788 typedef struct _xmlSchemaPSVIIDCNode xmlSchemaPSVIIDCNode;
789 typedef xmlSchemaPSVIIDCNode *xmlSchemaPSVIIDCNodePtr;
790 struct _xmlSchemaPSVIIDCNode {
791 xmlNodePtr node;
792 xmlSchemaPSVIIDCKeyPtr *keys;
793 int nodeLine;
794 int nodeQNameID;
795
796 };
797
798 /**
799 * xmlSchemaPSVIIDCBinding:
800 *
801 * The identity-constraint binding item of the [identity-constraint table].
802 */
803 typedef struct _xmlSchemaPSVIIDCBinding xmlSchemaPSVIIDCBinding;
804 typedef xmlSchemaPSVIIDCBinding *xmlSchemaPSVIIDCBindingPtr;
805 struct _xmlSchemaPSVIIDCBinding {
806 xmlSchemaPSVIIDCBindingPtr next; /* next binding of a specific node */
807 xmlSchemaIDCPtr definition; /* the IDC definition */
808 xmlSchemaPSVIIDCNodePtr *nodeTable; /* array of key-sequences */
809 int nbNodes; /* number of entries in the node table */
810 int sizeNodes; /* size of the node table */
811 xmlSchemaItemListPtr dupls;
812 };
813
814
815 #define XPATH_STATE_OBJ_TYPE_IDC_SELECTOR 1
816 #define XPATH_STATE_OBJ_TYPE_IDC_FIELD 2
817
818 #define XPATH_STATE_OBJ_MATCHES -2
819 #define XPATH_STATE_OBJ_BLOCKED -3
820
821 typedef struct _xmlSchemaIDCMatcher xmlSchemaIDCMatcher;
822 typedef xmlSchemaIDCMatcher *xmlSchemaIDCMatcherPtr;
823
824 /**
825 * xmlSchemaIDCStateObj:
826 *
827 * The state object used to evaluate XPath expressions.
828 */
829 typedef struct _xmlSchemaIDCStateObj xmlSchemaIDCStateObj;
830 typedef xmlSchemaIDCStateObj *xmlSchemaIDCStateObjPtr;
831 struct _xmlSchemaIDCStateObj {
832 int type;
833 xmlSchemaIDCStateObjPtr next; /* next if in a list */
834 int depth; /* depth of creation */
835 int *history; /* list of (depth, state-id) tuples */
836 int nbHistory;
837 int sizeHistory;
838 xmlSchemaIDCMatcherPtr matcher; /* the correspondent field/selector
839 matcher */
840 xmlSchemaIDCSelectPtr sel;
841 void *xpathCtxt;
842 };
843
844 #define IDC_MATCHER 0
845
846 /**
847 * xmlSchemaIDCMatcher:
848 *
849 * Used to evaluate IDC selectors (and fields).
850 */
851 struct _xmlSchemaIDCMatcher {
852 int type;
853 int depth; /* the tree depth at creation time */
854 xmlSchemaIDCMatcherPtr next; /* next in the list */
855 xmlSchemaIDCMatcherPtr nextCached; /* next in the cache list */
856 xmlSchemaIDCAugPtr aidc; /* the augmented IDC item */
857 int idcType;
858 xmlSchemaPSVIIDCKeyPtr **keySeqs; /* the key-sequences of the target
859 elements */
860 int sizeKeySeqs;
861 xmlSchemaItemListPtr targets; /* list of target-node
862 (xmlSchemaPSVIIDCNodePtr) entries */
863 };
864
865 /*
866 * Element info flags.
867 */
868 #define XML_SCHEMA_NODE_INFO_FLAG_OWNED_NAMES 1<<0
869 #define XML_SCHEMA_NODE_INFO_FLAG_OWNED_VALUES 1<<1
870 #define XML_SCHEMA_ELEM_INFO_NILLED 1<<2
871 #define XML_SCHEMA_ELEM_INFO_LOCAL_TYPE 1<<3
872
873 #define XML_SCHEMA_NODE_INFO_VALUE_NEEDED 1<<4
874 #define XML_SCHEMA_ELEM_INFO_EMPTY 1<<5
875 #define XML_SCHEMA_ELEM_INFO_HAS_CONTENT 1<<6
876
877 #define XML_SCHEMA_ELEM_INFO_HAS_ELEM_CONTENT 1<<7
878 #define XML_SCHEMA_ELEM_INFO_ERR_BAD_CONTENT 1<<8
879 #define XML_SCHEMA_NODE_INFO_ERR_NOT_EXPECTED 1<<9
880 #define XML_SCHEMA_NODE_INFO_ERR_BAD_TYPE 1<<10
881
882 /**
883 * xmlSchemaNodeInfo:
884 *
885 * Holds information of an element node.
886 */
887 struct _xmlSchemaNodeInfo {
888 int nodeType;
889 xmlNodePtr node;
890 int nodeLine;
891 const xmlChar *localName;
892 const xmlChar *nsName;
893 const xmlChar *value;
894 xmlSchemaValPtr val; /* the pre-computed value if any */
895 xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
896
897 int flags; /* combination of node info flags */
898
899 int valNeeded;
900 int normVal;
901
902 xmlSchemaElementPtr decl; /* the element/attribute declaration */
903 int depth;
904 xmlSchemaPSVIIDCBindingPtr idcTable; /* the table of PSVI IDC bindings
905 for the scope element*/
906 xmlSchemaIDCMatcherPtr idcMatchers; /* the IDC matchers for the scope
907 element */
908 xmlRegExecCtxtPtr regexCtxt;
909
910 const xmlChar **nsBindings; /* Namespace bindings on this element */
911 int nbNsBindings;
912 int sizeNsBindings;
913
914 int hasKeyrefs;
915 int appliedXPath; /* Indicates that an XPath has been applied. */
916 };
917
918 #define XML_SCHEMAS_ATTR_UNKNOWN 1
919 #define XML_SCHEMAS_ATTR_ASSESSED 2
920 #define XML_SCHEMAS_ATTR_PROHIBITED 3
921 #define XML_SCHEMAS_ATTR_ERR_MISSING 4
922 #define XML_SCHEMAS_ATTR_INVALID_VALUE 5
923 #define XML_SCHEMAS_ATTR_ERR_NO_TYPE 6
924 #define XML_SCHEMAS_ATTR_ERR_FIXED_VALUE 7
925 #define XML_SCHEMAS_ATTR_DEFAULT 8
926 #define XML_SCHEMAS_ATTR_VALIDATE_VALUE 9
927 #define XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL 10
928 #define XML_SCHEMAS_ATTR_HAS_ATTR_USE 11
929 #define XML_SCHEMAS_ATTR_HAS_ATTR_DECL 12
930 #define XML_SCHEMAS_ATTR_WILD_SKIP 13
931 #define XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL 14
932 #define XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID 15
933 #define XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID 16
934 #define XML_SCHEMAS_ATTR_META 17
935 /*
936 * @metaType values of xmlSchemaAttrInfo.
937 */
938 #define XML_SCHEMA_ATTR_INFO_META_XSI_TYPE 1
939 #define XML_SCHEMA_ATTR_INFO_META_XSI_NIL 2
940 #define XML_SCHEMA_ATTR_INFO_META_XSI_SCHEMA_LOC 3
941 #define XML_SCHEMA_ATTR_INFO_META_XSI_NO_NS_SCHEMA_LOC 4
942 #define XML_SCHEMA_ATTR_INFO_META_XMLNS 5
943
944 typedef struct _xmlSchemaAttrInfo xmlSchemaAttrInfo;
945 typedef xmlSchemaAttrInfo *xmlSchemaAttrInfoPtr;
946 struct _xmlSchemaAttrInfo {
947 int nodeType;
948 xmlNodePtr node;
949 int nodeLine;
950 const xmlChar *localName;
951 const xmlChar *nsName;
952 const xmlChar *value;
953 xmlSchemaValPtr val; /* the pre-computed value if any */
954 xmlSchemaTypePtr typeDef; /* the complex/simple type definition if any */
955 int flags; /* combination of node info flags */
956
957 xmlSchemaAttributePtr decl; /* the attribute declaration */
958 xmlSchemaAttributeUsePtr use; /* the attribute use */
959 int state;
960 int metaType;
961 const xmlChar *vcValue; /* the value constraint value */
962 xmlSchemaNodeInfoPtr parent;
963 };
964
965
966 #define XML_SCHEMA_VALID_CTXT_FLAG_STREAM 1
967 /**
968 * xmlSchemaValidCtxt:
969 *
970 * A Schemas validation context
971 */
972 struct _xmlSchemaValidCtxt {
973 int type;
974 void *errCtxt; /* user specific data block */
975 xmlSchemaValidityErrorFunc error; /* the callback in case of errors */
976 xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */
977 xmlStructuredErrorFunc serror;
978
979 xmlSchemaPtr schema; /* The schema in use */
980 xmlDocPtr doc;
981 xmlParserInputBufferPtr input;
982 xmlCharEncoding enc;
983 xmlSAXHandlerPtr sax;
984 xmlParserCtxtPtr parserCtxt;
985 void *user_data; /* TODO: What is this for? */
986 char *filename;
987
988 int err;
989 int nberrors;
990
991 xmlNodePtr node;
992 xmlNodePtr cur;
993 /* xmlSchemaTypePtr type; */
994
995 xmlRegExecCtxtPtr regexp;
996 xmlSchemaValPtr value;
997
998 int valueWS;
999 int options;
1000 xmlNodePtr validationRoot;
1001 xmlSchemaParserCtxtPtr pctxt;
1002 int xsiAssemble;
1003
1004 int depth;
1005 xmlSchemaNodeInfoPtr *elemInfos; /* array of element informations */
1006 int sizeElemInfos;
1007 xmlSchemaNodeInfoPtr inode; /* the current element information */
1008
1009 xmlSchemaIDCAugPtr aidcs; /* a list of augmented IDC informations */
1010
1011 xmlSchemaIDCStateObjPtr xpathStates; /* first active state object. */
1012 xmlSchemaIDCStateObjPtr xpathStatePool; /* first stored state object. */
1013 xmlSchemaIDCMatcherPtr idcMatcherCache; /* Cache for IDC matcher objects. */
1014
1015 xmlSchemaPSVIIDCNodePtr *idcNodes; /* list of all IDC node-table entries*/
1016 int nbIdcNodes;
1017 int sizeIdcNodes;
1018
1019 xmlSchemaPSVIIDCKeyPtr *idcKeys; /* list of all IDC node-table entries */
1020 int nbIdcKeys;
1021 int sizeIdcKeys;
1022
1023 int flags;
1024
1025 xmlDictPtr dict;
1026
1027 #ifdef LIBXML_READER_ENABLED
1028 xmlTextReaderPtr reader;
1029 #endif
1030
1031 xmlSchemaAttrInfoPtr *attrInfos;
1032 int nbAttrInfos;
1033 int sizeAttrInfos;
1034
1035 int skipDepth;
1036 xmlSchemaItemListPtr nodeQNames;
1037 int hasKeyrefs;
1038 int createIDCNodeTables;
1039 int psviExposeIDCNodeTables;
1040
1041 /* Locator for error reporting in streaming mode */
1042 xmlSchemaValidityLocatorFunc locFunc;
1043 void *locCtxt;
1044 };
1045
1046 /**
1047 * xmlSchemaSubstGroup:
1048 *
1049 *
1050 */
1051 typedef struct _xmlSchemaSubstGroup xmlSchemaSubstGroup;
1052 typedef xmlSchemaSubstGroup *xmlSchemaSubstGroupPtr;
1053 struct _xmlSchemaSubstGroup {
1054 xmlSchemaElementPtr head;
1055 xmlSchemaItemListPtr members;
1056 };
1057
1058 /************************************************************************
1059 * *
1060 * Some predeclarations *
1061 * *
1062 ************************************************************************/
1063
1064 static int xmlSchemaParseInclude(xmlSchemaParserCtxtPtr ctxt,
1065 xmlSchemaPtr schema,
1066 xmlNodePtr node);
1067 static int xmlSchemaParseRedefine(xmlSchemaParserCtxtPtr ctxt,
1068 xmlSchemaPtr schema,
1069 xmlNodePtr node);
1070 static int
1071 xmlSchemaTypeFixup(xmlSchemaTypePtr type,
1072 xmlSchemaAbstractCtxtPtr ctxt);
1073 static const xmlChar *
1074 xmlSchemaFacetTypeToString(xmlSchemaTypeType type);
1075 static int
1076 xmlSchemaParseImport(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1077 xmlNodePtr node);
1078 static int
1079 xmlSchemaCheckFacetValues(xmlSchemaTypePtr typeDecl,
1080 xmlSchemaParserCtxtPtr ctxt);
1081 static void
1082 xmlSchemaClearValidCtxt(xmlSchemaValidCtxtPtr vctxt);
1083 static xmlSchemaWhitespaceValueType
1084 xmlSchemaGetWhiteSpaceFacetValue(xmlSchemaTypePtr type);
1085 static xmlSchemaTreeItemPtr
1086 xmlSchemaParseModelGroup(xmlSchemaParserCtxtPtr ctxt, xmlSchemaPtr schema,
1087 xmlNodePtr node, xmlSchemaTypeType type,
1088 int withParticle);
1089 static const xmlChar *
1090 xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item);
1091 static xmlSchemaTypeLinkPtr
1092 xmlSchemaGetUnionSimpleTypeMemberTypes(xmlSchemaTypePtr type);
1093 static void
1094 xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
1095 const char *funcName,
1096 const char *message) LIBXML_ATTR_FORMAT(3,0);
1097 static int
1098 xmlSchemaCheckCOSSTDerivedOK(xmlSchemaAbstractCtxtPtr ctxt,
1099 xmlSchemaTypePtr type,
1100 xmlSchemaTypePtr baseType,
1101 int subset);
1102 static void
1103 xmlSchemaCheckElementDeclComponent(xmlSchemaElementPtr elemDecl,
1104 xmlSchemaParserCtxtPtr ctxt);
1105 static void
1106 xmlSchemaComponentListFree(xmlSchemaItemListPtr list);
1107 static xmlSchemaQNameRefPtr
1108 xmlSchemaParseAttributeGroupRef(xmlSchemaParserCtxtPtr pctxt,
1109 xmlSchemaPtr schema,
1110 xmlNodePtr node);
1111
1112 /************************************************************************
1113 * *
1114 * Helper functions *
1115 * *
1116 ************************************************************************/
1117
1118 /**
1119 * xmlSchemaItemTypeToStr:
1120 * @type: the type of the schema item
1121 *
1122 * Returns the component name of a schema item.
1123 */
1124 static const xmlChar *
1125 xmlSchemaItemTypeToStr(xmlSchemaTypeType type)
1126 {
1127 switch (type) {
1128 case XML_SCHEMA_TYPE_BASIC:
1129 return(BAD_CAST "simple type definition");
1130 case XML_SCHEMA_TYPE_SIMPLE:
1131 return(BAD_CAST "simple type definition");
1132 case XML_SCHEMA_TYPE_COMPLEX:
1133 return(BAD_CAST "complex type definition");
1134 case XML_SCHEMA_TYPE_ELEMENT:
1135 return(BAD_CAST "element declaration");
1136 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1137 return(BAD_CAST "attribute use");
1138 case XML_SCHEMA_TYPE_ATTRIBUTE:
1139 return(BAD_CAST "attribute declaration");
1140 case XML_SCHEMA_TYPE_GROUP:
1141 return(BAD_CAST "model group definition");
1142 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1143 return(BAD_CAST "attribute group definition");
1144 case XML_SCHEMA_TYPE_NOTATION:
1145 return(BAD_CAST "notation declaration");
1146 case XML_SCHEMA_TYPE_SEQUENCE:
1147 return(BAD_CAST "model group (sequence)");
1148 case XML_SCHEMA_TYPE_CHOICE:
1149 return(BAD_CAST "model group (choice)");
1150 case XML_SCHEMA_TYPE_ALL:
1151 return(BAD_CAST "model group (all)");
1152 case XML_SCHEMA_TYPE_PARTICLE:
1153 return(BAD_CAST "particle");
1154 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1155 return(BAD_CAST "unique identity-constraint");
1156 /* return(BAD_CAST "IDC (unique)"); */
1157 case XML_SCHEMA_TYPE_IDC_KEY:
1158 return(BAD_CAST "key identity-constraint");
1159 /* return(BAD_CAST "IDC (key)"); */
1160 case XML_SCHEMA_TYPE_IDC_KEYREF:
1161 return(BAD_CAST "keyref identity-constraint");
1162 /* return(BAD_CAST "IDC (keyref)"); */
1163 case XML_SCHEMA_TYPE_ANY:
1164 return(BAD_CAST "wildcard (any)");
1165 case XML_SCHEMA_EXTRA_QNAMEREF:
1166 return(BAD_CAST "[helper component] QName reference");
1167 case XML_SCHEMA_EXTRA_ATTR_USE_PROHIB:
1168 return(BAD_CAST "[helper component] attribute use prohibition");
1169 default:
1170 return(BAD_CAST "Not a schema component");
1171 }
1172 }
1173
1174 /**
1175 * xmlSchemaGetComponentTypeStr:
1176 * @type: the type of the schema item
1177 *
1178 * Returns the component name of a schema item.
1179 */
1180 static const xmlChar *
1181 xmlSchemaGetComponentTypeStr(xmlSchemaBasicItemPtr item)
1182 {
1183 switch (item->type) {
1184 case XML_SCHEMA_TYPE_BASIC:
1185 if (WXS_IS_COMPLEX(WXS_TYPE_CAST item))
1186 return(BAD_CAST "complex type definition");
1187 else
1188 return(BAD_CAST "simple type definition");
1189 default:
1190 return(xmlSchemaItemTypeToStr(item->type));
1191 }
1192 }
1193
1194 /**
1195 * xmlSchemaGetComponentNode:
1196 * @item: a schema component
1197 *
1198 * Returns node associated with the schema component.
1199 * NOTE that such a node need not be available; plus, a component's
1200 * node need not to reflect the component directly, since there is no
1201 * one-to-one relationship between the XML Schema representation and
1202 * the component representation.
1203 */
1204 static xmlNodePtr
1205 xmlSchemaGetComponentNode(xmlSchemaBasicItemPtr item)
1206 {
1207 switch (item->type) {
1208 case XML_SCHEMA_TYPE_ELEMENT:
1209 return (((xmlSchemaElementPtr) item)->node);
1210 case XML_SCHEMA_TYPE_ATTRIBUTE:
1211 return (((xmlSchemaAttributePtr) item)->node);
1212 case XML_SCHEMA_TYPE_COMPLEX:
1213 case XML_SCHEMA_TYPE_SIMPLE:
1214 return (((xmlSchemaTypePtr) item)->node);
1215 case XML_SCHEMA_TYPE_ANY:
1216 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1217 return (((xmlSchemaWildcardPtr) item)->node);
1218 case XML_SCHEMA_TYPE_PARTICLE:
1219 return (((xmlSchemaParticlePtr) item)->node);
1220 case XML_SCHEMA_TYPE_SEQUENCE:
1221 case XML_SCHEMA_TYPE_CHOICE:
1222 case XML_SCHEMA_TYPE_ALL:
1223 return (((xmlSchemaModelGroupPtr) item)->node);
1224 case XML_SCHEMA_TYPE_GROUP:
1225 return (((xmlSchemaModelGroupDefPtr) item)->node);
1226 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1227 return (((xmlSchemaAttributeGroupPtr) item)->node);
1228 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1229 case XML_SCHEMA_TYPE_IDC_KEY:
1230 case XML_SCHEMA_TYPE_IDC_KEYREF:
1231 return (((xmlSchemaIDCPtr) item)->node);
1232 case XML_SCHEMA_EXTRA_QNAMEREF:
1233 return(((xmlSchemaQNameRefPtr) item)->node);
1234 /* TODO: What to do with NOTATIONs?
1235 case XML_SCHEMA_TYPE_NOTATION:
1236 return (((xmlSchemaNotationPtr) item)->node);
1237 */
1238 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1239 return (((xmlSchemaAttributeUsePtr) item)->node);
1240 default:
1241 return (NULL);
1242 }
1243 }
1244
1245 #if 0
1246 /**
1247 * xmlSchemaGetNextComponent:
1248 * @item: a schema component
1249 *
1250 * Returns the next sibling of the schema component.
1251 */
1252 static xmlSchemaBasicItemPtr
1253 xmlSchemaGetNextComponent(xmlSchemaBasicItemPtr item)
1254 {
1255 switch (item->type) {
1256 case XML_SCHEMA_TYPE_ELEMENT:
1257 return ((xmlSchemaBasicItemPtr) ((xmlSchemaElementPtr) item)->next);
1258 case XML_SCHEMA_TYPE_ATTRIBUTE:
1259 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributePtr) item)->next);
1260 case XML_SCHEMA_TYPE_COMPLEX:
1261 case XML_SCHEMA_TYPE_SIMPLE:
1262 return ((xmlSchemaBasicItemPtr) ((xmlSchemaTypePtr) item)->next);
1263 case XML_SCHEMA_TYPE_ANY:
1264 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1265 return (NULL);
1266 case XML_SCHEMA_TYPE_PARTICLE:
1267 return ((xmlSchemaBasicItemPtr) ((xmlSchemaParticlePtr) item)->next);
1268 case XML_SCHEMA_TYPE_SEQUENCE:
1269 case XML_SCHEMA_TYPE_CHOICE:
1270 case XML_SCHEMA_TYPE_ALL:
1271 return (NULL);
1272 case XML_SCHEMA_TYPE_GROUP:
1273 return (NULL);
1274 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1275 return ((xmlSchemaBasicItemPtr) ((xmlSchemaAttributeGroupPtr) item)->next);
1276 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1277 case XML_SCHEMA_TYPE_IDC_KEY:
1278 case XML_SCHEMA_TYPE_IDC_KEYREF:
1279 return ((xmlSchemaBasicItemPtr) ((xmlSchemaIDCPtr) item)->next);
1280 default:
1281 return (NULL);
1282 }
1283 }
1284 #endif
1285
1286
1287 /**
1288 * xmlSchemaFormatQName:
1289 * @buf: the string buffer
1290 * @namespaceName: the namespace name
1291 * @localName: the local name
1292 *
1293 * Returns the given QName in the format "{namespaceName}localName" or
1294 * just "localName" if @namespaceName is NULL.
1295 *
1296 * Returns the localName if @namespaceName is NULL, a formatted
1297 * string otherwise.
1298 */
1299 static const xmlChar*
1300 xmlSchemaFormatQName(xmlChar **buf,
1301 const xmlChar *namespaceName,
1302 const xmlChar *localName)
1303 {
1304 FREE_AND_NULL(*buf)
1305 if (namespaceName != NULL) {
1306 *buf = xmlStrdup(BAD_CAST "{");
1307 *buf = xmlStrcat(*buf, namespaceName);
1308 *buf = xmlStrcat(*buf, BAD_CAST "}");
1309 }
1310 if (localName != NULL) {
1311 if (namespaceName == NULL)
1312 return(localName);
1313 *buf = xmlStrcat(*buf, localName);
1314 } else {
1315 *buf = xmlStrcat(*buf, BAD_CAST "(NULL)");
1316 }
1317 return ((const xmlChar *) *buf);
1318 }
1319
1320 static const xmlChar*
1321 xmlSchemaFormatQNameNs(xmlChar **buf, xmlNsPtr ns, const xmlChar *localName)
1322 {
1323 if (ns != NULL)
1324 return (xmlSchemaFormatQName(buf, ns->href, localName));
1325 else
1326 return (xmlSchemaFormatQName(buf, NULL, localName));
1327 }
1328
1329 static const xmlChar *
1330 xmlSchemaGetComponentName(xmlSchemaBasicItemPtr item)
1331 {
1332 switch (item->type) {
1333 case XML_SCHEMA_TYPE_ELEMENT:
1334 return (((xmlSchemaElementPtr) item)->name);
1335 case XML_SCHEMA_TYPE_ATTRIBUTE:
1336 return (((xmlSchemaAttributePtr) item)->name);
1337 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1338 return (((xmlSchemaAttributeGroupPtr) item)->name);
1339 case XML_SCHEMA_TYPE_BASIC:
1340 case XML_SCHEMA_TYPE_SIMPLE:
1341 case XML_SCHEMA_TYPE_COMPLEX:
1342 return (((xmlSchemaTypePtr) item)->name);
1343 case XML_SCHEMA_TYPE_GROUP:
1344 return (((xmlSchemaModelGroupDefPtr) item)->name);
1345 case XML_SCHEMA_TYPE_IDC_KEY:
1346 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1347 case XML_SCHEMA_TYPE_IDC_KEYREF:
1348 return (((xmlSchemaIDCPtr) item)->name);
1349 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1350 if (WXS_ATTRUSE_DECL(item) != NULL) {
1351 return(xmlSchemaGetComponentName(
1352 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1353 } else
1354 return(NULL);
1355 case XML_SCHEMA_EXTRA_QNAMEREF:
1356 return (((xmlSchemaQNameRefPtr) item)->name);
1357 case XML_SCHEMA_TYPE_NOTATION:
1358 return (((xmlSchemaNotationPtr) item)->name);
1359 default:
1360 /*
1361 * Other components cannot have names.
1362 */
1363 break;
1364 }
1365 return (NULL);
1366 }
1367
1368 #define xmlSchemaGetQNameRefName(r) (WXS_QNAME_CAST (r))->name
1369 #define xmlSchemaGetQNameRefTargetNs(r) (WXS_QNAME_CAST (r))->targetNamespace
1370 /*
1371 static const xmlChar *
1372 xmlSchemaGetQNameRefName(void *ref)
1373 {
1374 return(((xmlSchemaQNameRefPtr) ref)->name);
1375 }
1376
1377 static const xmlChar *
1378 xmlSchemaGetQNameRefTargetNs(void *ref)
1379 {
1380 return(((xmlSchemaQNameRefPtr) ref)->targetNamespace);
1381 }
1382 */
1383
1384 static const xmlChar *
1385 xmlSchemaGetComponentTargetNs(xmlSchemaBasicItemPtr item)
1386 {
1387 switch (item->type) {
1388 case XML_SCHEMA_TYPE_ELEMENT:
1389 return (((xmlSchemaElementPtr) item)->targetNamespace);
1390 case XML_SCHEMA_TYPE_ATTRIBUTE:
1391 return (((xmlSchemaAttributePtr) item)->targetNamespace);
1392 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1393 return (((xmlSchemaAttributeGroupPtr) item)->targetNamespace);
1394 case XML_SCHEMA_TYPE_BASIC:
1395 return (BAD_CAST "http://www.w3.org/2001/XMLSchema");
1396 case XML_SCHEMA_TYPE_SIMPLE:
1397 case XML_SCHEMA_TYPE_COMPLEX:
1398 return (((xmlSchemaTypePtr) item)->targetNamespace);
1399 case XML_SCHEMA_TYPE_GROUP:
1400 return (((xmlSchemaModelGroupDefPtr) item)->targetNamespace);
1401 case XML_SCHEMA_TYPE_IDC_KEY:
1402 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1403 case XML_SCHEMA_TYPE_IDC_KEYREF:
1404 return (((xmlSchemaIDCPtr) item)->targetNamespace);
1405 case XML_SCHEMA_TYPE_ATTRIBUTE_USE:
1406 if (WXS_ATTRUSE_DECL(item) != NULL) {
1407 return(xmlSchemaGetComponentTargetNs(
1408 WXS_BASIC_CAST WXS_ATTRUSE_DECL(item)));
1409 }
1410 /* TODO: Will returning NULL break something? */
1411 break;
1412 case XML_SCHEMA_EXTRA_QNAMEREF:
1413 return (((xmlSchemaQNameRefPtr) item)->targetNamespace);
1414 case XML_SCHEMA_TYPE_NOTATION:
1415 return (((xmlSchemaNotationPtr) item)->targetNamespace);
1416 default:
1417 /*
1418 * Other components cannot have names.
1419 */
1420 break;
1421 }
1422 return (NULL);
1423 }
1424
1425 static const xmlChar*
1426 xmlSchemaGetComponentQName(xmlChar **buf,
1427 void *item)
1428 {
1429 return (xmlSchemaFormatQName(buf,
1430 xmlSchemaGetComponentTargetNs((xmlSchemaBasicItemPtr) item),
1431 xmlSchemaGetComponentName((xmlSchemaBasicItemPtr) item)));
1432 }
1433
1434 static const xmlChar*
1435 xmlSchemaGetComponentDesignation(xmlChar **buf, void *item)
1436 {
1437 xmlChar *str = NULL;
1438
1439 *buf = xmlStrcat(*buf, WXS_ITEM_TYPE_NAME(item));
1440 *buf = xmlStrcat(*buf, BAD_CAST " '");
1441 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str,
1442 (xmlSchemaBasicItemPtr) item));
1443 *buf = xmlStrcat(*buf, BAD_CAST "'");
1444 FREE_AND_NULL(str);
1445 return(*buf);
1446 }
1447
1448 static const xmlChar*
1449 xmlSchemaGetIDCDesignation(xmlChar **buf, xmlSchemaIDCPtr idc)
1450 {
1451 return(xmlSchemaGetComponentDesignation(buf, idc));
1452 }
1453
1454 /**
1455 * xmlSchemaWildcardPCToString:
1456 * @pc: the type of processContents
1457 *
1458 * Returns a string representation of the type of
1459 * processContents.
1460 */
1461 static const xmlChar *
1462 xmlSchemaWildcardPCToString(int pc)
1463 {
1464 switch (pc) {
1465 case XML_SCHEMAS_ANY_SKIP:
1466 return (BAD_CAST "skip");
1467 case XML_SCHEMAS_ANY_LAX:
1468 return (BAD_CAST "lax");
1469 case XML_SCHEMAS_ANY_STRICT:
1470 return (BAD_CAST "strict");
1471 default:
1472 return (BAD_CAST "invalid process contents");
1473 }
1474 }
1475
1476 /**
1477 * xmlSchemaGetCanonValueWhtspExt:
1478 * @val: the precomputed value
1479 * @retValue: the returned value
1480 * @ws: the whitespace type of the value
1481 *
1482 * Get a the canonical representation of the value.
1483 * The caller has to free the returned retValue.
1484 *
1485 * Returns 0 if the value could be built and -1 in case of
1486 * API errors or if the value type is not supported yet.
1487 */
1488 static int
1489 xmlSchemaGetCanonValueWhtspExt(xmlSchemaValPtr val,
1490 xmlSchemaWhitespaceValueType ws,
1491 xmlChar **retValue)
1492 {
1493 int list;
1494 xmlSchemaValType valType;
1495 const xmlChar *value, *value2 = NULL;
1496
1497
1498 if ((retValue == NULL) || (val == NULL))
1499 return (-1);
1500 list = xmlSchemaValueGetNext(val) ? 1 : 0;
1501 *retValue = NULL;
1502 do {
1503 value = NULL;
1504 valType = xmlSchemaGetValType(val);
1505 switch (valType) {
1506 case XML_SCHEMAS_STRING:
1507 case XML_SCHEMAS_NORMSTRING:
1508 case XML_SCHEMAS_ANYSIMPLETYPE:
1509 value = xmlSchemaValueGetAsString(val);
1510 if (value != NULL) {
1511 if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE)
1512 value2 = xmlSchemaCollapseString(value);
1513 else if (ws == XML_SCHEMA_WHITESPACE_REPLACE)
1514 value2 = xmlSchemaWhiteSpaceReplace(value);
1515 if (value2 != NULL)
1516 value = value2;
1517 }
1518 break;
1519 default:
1520 if (xmlSchemaGetCanonValue(val, &value2) == -1) {
1521 if (value2 != NULL)
1522 xmlFree((xmlChar *) value2);
1523 goto internal_error;
1524 }
1525 value = value2;
1526 }
1527 if (*retValue == NULL)
1528 if (value == NULL) {
1529 if (! list)
1530 *retValue = xmlStrdup(BAD_CAST "");
1531 } else
1532 *retValue = xmlStrdup(value);
1533 else if (value != NULL) {
1534 /* List. */
1535 *retValue = xmlStrcat((xmlChar *) *retValue, BAD_CAST " ");
1536 *retValue = xmlStrcat((xmlChar *) *retValue, value);
1537 }
1538 FREE_AND_NULL(value2)
1539 val = xmlSchemaValueGetNext(val);
1540 } while (val != NULL);
1541
1542 return (0);
1543 internal_error:
1544 if (*retValue != NULL)
1545 xmlFree((xmlChar *) (*retValue));
1546 if (value2 != NULL)
1547 xmlFree((xmlChar *) value2);
1548 return (-1);
1549 }
1550
1551 /**
1552 * xmlSchemaFormatItemForReport:
1553 * @buf: the string buffer
1554 * @itemDes: the designation of the item
1555 * @itemName: the name of the item
1556 * @item: the item as an object
1557 * @itemNode: the node of the item
1558 * @local: the local name
1559 * @parsing: if the function is used during the parse
1560 *
1561 * Returns a representation of the given item used
1562 * for error reports.
1563 *
1564 * The following order is used to build the resulting
1565 * designation if the arguments are not NULL:
1566 * 1a. If itemDes not NULL -> itemDes
1567 * 1b. If (itemDes not NULL) and (itemName not NULL)
1568 * -> itemDes + itemName
1569 * 2. If the preceding was NULL and (item not NULL) -> item
1570 * 3. If the preceding was NULL and (itemNode not NULL) -> itemNode
1571 *
1572 * If the itemNode is an attribute node, the name of the attribute
1573 * will be appended to the result.
1574 *
1575 * Returns the formatted string and sets @buf to the resulting value.
1576 */
1577 static xmlChar*
1578 xmlSchemaFormatItemForReport(xmlChar **buf,
1579 const xmlChar *itemDes,
1580 xmlSchemaBasicItemPtr item,
1581 xmlNodePtr itemNode)
1582 {
1583 xmlChar *str = NULL;
1584 int named = 1;
1585
1586 if (*buf != NULL) {
1587 xmlFree(*buf);
1588 *buf = NULL;
1589 }
1590
1591 if (itemDes != NULL) {
1592 *buf = xmlStrdup(itemDes);
1593 } else if (item != NULL) {
1594 switch (item->type) {
1595 case XML_SCHEMA_TYPE_BASIC: {
1596 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1597
1598 if (WXS_IS_ATOMIC(type))
1599 *buf = xmlStrdup(BAD_CAST "atomic type 'xs:");
1600 else if (WXS_IS_LIST(type))
1601 *buf = xmlStrdup(BAD_CAST "list type 'xs:");
1602 else if (WXS_IS_UNION(type))
1603 *buf = xmlStrdup(BAD_CAST "union type 'xs:");
1604 else
1605 *buf = xmlStrdup(BAD_CAST "simple type 'xs:");
1606 *buf = xmlStrcat(*buf, type->name);
1607 *buf = xmlStrcat(*buf, BAD_CAST "'");
1608 }
1609 break;
1610 case XML_SCHEMA_TYPE_SIMPLE: {
1611 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1612
1613 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
1614 *buf = xmlStrdup(BAD_CAST"");
1615 } else {
1616 *buf = xmlStrdup(BAD_CAST "local ");
1617 }
1618 if (WXS_IS_ATOMIC(type))
1619 *buf = xmlStrcat(*buf, BAD_CAST "atomic type");
1620 else if (WXS_IS_LIST(type))
1621 *buf = xmlStrcat(*buf, BAD_CAST "list type");
1622 else if (WXS_IS_UNION(type))
1623 *buf = xmlStrcat(*buf, BAD_CAST "union type");
1624 else
1625 *buf = xmlStrcat(*buf, BAD_CAST "simple type");
1626 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
1627 *buf = xmlStrcat(*buf, BAD_CAST " '");
1628 *buf = xmlStrcat(*buf, type->name);
1629 *buf = xmlStrcat(*buf, BAD_CAST "'");
1630 }
1631 }
1632 break;
1633 case XML_SCHEMA_TYPE_COMPLEX: {
1634 xmlSchemaTypePtr type = WXS_TYPE_CAST item;
1635
1636 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL)
1637 *buf = xmlStrdup(BAD_CAST "");
1638 else
1639 *buf = xmlStrdup(BAD_CAST "local ");
1640 *buf = xmlStrcat(*buf, BAD_CAST "complex type");
1641 if (type->flags & XML_SCHEMAS_TYPE_GLOBAL) {
1642 *buf = xmlStrcat(*buf, BAD_CAST " '");
1643 *buf = xmlStrcat(*buf, type->name);
1644 *buf = xmlStrcat(*buf, BAD_CAST "'");
1645 }
1646 }
1647 break;
1648 case XML_SCHEMA_TYPE_ATTRIBUTE_USE: {
1649 xmlSchemaAttributeUsePtr ause;
1650
1651 ause = WXS_ATTR_USE_CAST item;
1652 *buf = xmlStrdup(BAD_CAST "attribute use ");
1653 if (WXS_ATTRUSE_DECL(ause) != NULL) {
1654 *buf = xmlStrcat(*buf, BAD_CAST "'");
1655 *buf = xmlStrcat(*buf,
1656 xmlSchemaGetComponentQName(&str, WXS_ATTRUSE_DECL(ause)));
1657 FREE_AND_NULL(str)
1658 *buf = xmlStrcat(*buf, BAD_CAST "'");
1659 } else {
1660 *buf = xmlStrcat(*buf, BAD_CAST "(unknown)");
1661 }
1662 }
1663 break;
1664 case XML_SCHEMA_TYPE_ATTRIBUTE: {
1665 xmlSchemaAttributePtr attr;
1666
1667 attr = (xmlSchemaAttributePtr) item;
1668 *buf = xmlStrdup(BAD_CAST "attribute decl.");
1669 *buf = xmlStrcat(*buf, BAD_CAST " '");
1670 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1671 attr->targetNamespace, attr->name));
1672 FREE_AND_NULL(str)
1673 *buf = xmlStrcat(*buf, BAD_CAST "'");
1674 }
1675 break;
1676 case XML_SCHEMA_TYPE_ATTRIBUTEGROUP:
1677 xmlSchemaGetComponentDesignation(buf, item);
1678 break;
1679 case XML_SCHEMA_TYPE_ELEMENT: {
1680 xmlSchemaElementPtr elem;
1681
1682 elem = (xmlSchemaElementPtr) item;
1683 *buf = xmlStrdup(BAD_CAST "element decl.");
1684 *buf = xmlStrcat(*buf, BAD_CAST " '");
1685 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1686 elem->targetNamespace, elem->name));
1687 *buf = xmlStrcat(*buf, BAD_CAST "'");
1688 }
1689 break;
1690 case XML_SCHEMA_TYPE_IDC_UNIQUE:
1691 case XML_SCHEMA_TYPE_IDC_KEY:
1692 case XML_SCHEMA_TYPE_IDC_KEYREF:
1693 if (item->type == XML_SCHEMA_TYPE_IDC_UNIQUE)
1694 *buf = xmlStrdup(BAD_CAST "unique '");
1695 else if (item->type == XML_SCHEMA_TYPE_IDC_KEY)
1696 *buf = xmlStrdup(BAD_CAST "key '");
1697 else
1698 *buf = xmlStrdup(BAD_CAST "keyRef '");
1699 *buf = xmlStrcat(*buf, ((xmlSchemaIDCPtr) item)->name);
1700 *buf = xmlStrcat(*buf, BAD_CAST "'");
1701 break;
1702 case XML_SCHEMA_TYPE_ANY:
1703 case XML_SCHEMA_TYPE_ANY_ATTRIBUTE:
1704 *buf = xmlStrdup(xmlSchemaWildcardPCToString(
1705 ((xmlSchemaWildcardPtr) item)->processContents));
1706 *buf = xmlStrcat(*buf, BAD_CAST " wildcard");
1707 break;
1708 case XML_SCHEMA_FACET_MININCLUSIVE:
1709 case XML_SCHEMA_FACET_MINEXCLUSIVE:
1710 case XML_SCHEMA_FACET_MAXINCLUSIVE:
1711 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
1712 case XML_SCHEMA_FACET_TOTALDIGITS:
1713 case XML_SCHEMA_FACET_FRACTIONDIGITS:
1714 case XML_SCHEMA_FACET_PATTERN:
1715 case XML_SCHEMA_FACET_ENUMERATION:
1716 case XML_SCHEMA_FACET_WHITESPACE:
1717 case XML_SCHEMA_FACET_LENGTH:
1718 case XML_SCHEMA_FACET_MAXLENGTH:
1719 case XML_SCHEMA_FACET_MINLENGTH:
1720 *buf = xmlStrdup(BAD_CAST "facet '");
1721 *buf = xmlStrcat(*buf, xmlSchemaFacetTypeToString(item->type));
1722 *buf = xmlStrcat(*buf, BAD_CAST "'");
1723 break;
1724 case XML_SCHEMA_TYPE_GROUP: {
1725 *buf = xmlStrdup(BAD_CAST "model group def.");
1726 *buf = xmlStrcat(*buf, BAD_CAST " '");
1727 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
1728 *buf = xmlStrcat(*buf, BAD_CAST "'");
1729 FREE_AND_NULL(str)
1730 }
1731 break;
1732 case XML_SCHEMA_TYPE_SEQUENCE:
1733 case XML_SCHEMA_TYPE_CHOICE:
1734 case XML_SCHEMA_TYPE_ALL:
1735 case XML_SCHEMA_TYPE_PARTICLE:
1736 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1737 break;
1738 case XML_SCHEMA_TYPE_NOTATION: {
1739 *buf = xmlStrdup(WXS_ITEM_TYPE_NAME(item));
1740 *buf = xmlStrcat(*buf, BAD_CAST " '");
1741 *buf = xmlStrcat(*buf, xmlSchemaGetComponentQName(&str, item));
1742 *buf = xmlStrcat(*buf, BAD_CAST "'");
1743 FREE_AND_NULL(str);
1744 }
1745 /* Falls through. */
1746 default:
1747 named = 0;
1748 }
1749 } else
1750 named = 0;
1751
1752 if ((named == 0) && (itemNode != NULL)) {
1753 xmlNodePtr elem;
1754
1755 if (itemNode->type == XML_ATTRIBUTE_NODE)
1756 elem = itemNode->parent;
1757 else
1758 elem = itemNode;
1759 *buf = xmlStrdup(BAD_CAST "Element '");
1760 if (elem->ns != NULL) {
1761 *buf = xmlStrcat(*buf,
1762 xmlSchemaFormatQName(&str, elem->ns->href, elem->name));
1763 FREE_AND_NULL(str)
1764 } else
1765 *buf = xmlStrcat(*buf, elem->name);
1766 *buf = xmlStrcat(*buf, BAD_CAST "'");
1767
1768 }
1769 if ((itemNode != NULL) && (itemNode->type == XML_ATTRIBUTE_NODE)) {
1770 *buf = xmlStrcat(*buf, BAD_CAST ", attribute '");
1771 if (itemNode->ns != NULL) {
1772 *buf = xmlStrcat(*buf, xmlSchemaFormatQName(&str,
1773 itemNode->ns->href, itemNode->name));
1774 FREE_AND_NULL(str)
1775 } else
1776 *buf = xmlStrcat(*buf, itemNode->name);
1777 *buf = xmlStrcat(*buf, BAD_CAST "'");
1778 }
1779 FREE_AND_NULL(str)
1780
1781 return (xmlEscapeFormatString(buf));
1782 }
1783
1784 /**
1785 * xmlSchemaFormatFacetEnumSet:
1786 * @buf: the string buffer
1787 * @type: the type holding the enumeration facets
1788 *
1789 * Builds a string consisting of all enumeration elements.
1790 *
1791 * Returns a string of all enumeration elements.
1792 */
1793 static const xmlChar *
1794 xmlSchemaFormatFacetEnumSet(xmlSchemaAbstractCtxtPtr actxt,
1795 xmlChar **buf, xmlSchemaTypePtr type)
1796 {
1797 xmlSchemaFacetPtr facet;
1798 xmlSchemaWhitespaceValueType ws;
1799 xmlChar *value = NULL;
1800 int res, found = 0;
1801
1802 if (*buf != NULL)
1803 xmlFree(*buf);
1804 *buf = NULL;
1805
1806 do {
1807 /*
1808 * Use the whitespace type of the base type.
1809 */
1810 ws = xmlSchemaGetWhiteSpaceFacetValue(type->baseType);
1811 for (facet = type->facets; facet != NULL; facet = facet->next) {
1812 if (facet->type != XML_SCHEMA_FACET_ENUMERATION)
1813 continue;
1814 found = 1;
1815 res = xmlSchemaGetCanonValueWhtspExt(facet->val,
1816 ws, &value);
1817 if (res == -1) {
1818 xmlSchemaInternalErr(actxt,
1819 "xmlSchemaFormatFacetEnumSet",
1820 "compute the canonical lexical representation");
1821 if (*buf != NULL)
1822 xmlFree(*buf);
1823 *buf = NULL;
1824 return (NULL);
1825 }
1826 if (*buf == NULL)
1827 *buf = xmlStrdup(BAD_CAST "'");
1828 else
1829 *buf = xmlStrcat(*buf, BAD_CAST ", '");
1830 *buf = xmlStrcat(*buf, BAD_CAST value);
1831 *buf = xmlStrcat(*buf, BAD_CAST "'");
1832 if (value != NULL) {
1833 xmlFree((xmlChar *)value);
1834 value = NULL;
1835 }
1836 }
1837 /*
1838 * The enumeration facet of a type restricts the enumeration
1839 * facet of the ancestor type; i.e., such restricted enumerations
1840 * do not belong to the set of the given type. Thus we break
1841 * on the first found enumeration.
1842 */
1843 if (found)
1844 break;
1845 type = type->baseType;
1846 } while ((type != NULL) && (type->type != XML_SCHEMA_TYPE_BASIC));
1847
1848 return ((const xmlChar *) *buf);
1849 }
1850
1851 /************************************************************************
1852 * *
1853 * Error functions *
1854 * *
1855 ************************************************************************/
1856
1857 #if 0
1858 static void
1859 xmlSchemaErrMemory(const char *msg)
1860 {
1861 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1862 msg);
1863 }
1864 #endif
1865
1866 static void
1867 xmlSchemaPSimpleErr(const char *msg)
1868 {
1869 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, NULL, NULL,
1870 msg);
1871 }
1872
1873 /**
1874 * xmlSchemaPErrMemory:
1875 * @node: a context node
1876 * @extra: extra informations
1877 *
1878 * Handle an out of memory condition
1879 */
1880 static void
1881 xmlSchemaPErrMemory(xmlSchemaParserCtxtPtr ctxt,
1882 const char *extra, xmlNodePtr node)
1883 {
1884 if (ctxt != NULL)
1885 ctxt->nberrors++;
1886 __xmlSimpleError(XML_FROM_SCHEMASP, XML_ERR_NO_MEMORY, node, NULL,
1887 extra);
1888 }
1889
1890 /**
1891 * xmlSchemaPErr:
1892 * @ctxt: the parsing context
1893 * @node: the context node
1894 * @error: the error code
1895 * @msg: the error message
1896 * @str1: extra data
1897 * @str2: extra data
1898 *
1899 * Handle a parser error
1900 */
1901 static void LIBXML_ATTR_FORMAT(4,0)
1902 xmlSchemaPErr(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1903 const char *msg, const xmlChar * str1, const xmlChar * str2)
1904 {
1905 xmlGenericErrorFunc channel = NULL;
1906 xmlStructuredErrorFunc schannel = NULL;
1907 void *data = NULL;
1908
1909 if (ctxt != NULL) {
1910 ctxt->nberrors++;
1911 ctxt->err = error;
1912 channel = ctxt->error;
1913 data = ctxt->errCtxt;
1914 schannel = ctxt->serror;
1915 }
1916 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
1917 error, XML_ERR_ERROR, NULL, 0,
1918 (const char *) str1, (const char *) str2, NULL, 0, 0,
1919 msg, str1, str2);
1920 }
1921
1922 /**
1923 * xmlSchemaPErr2:
1924 * @ctxt: the parsing context
1925 * @node: the context node
1926 * @node: the current child
1927 * @error: the error code
1928 * @msg: the error message
1929 * @str1: extra data
1930 * @str2: extra data
1931 *
1932 * Handle a parser error
1933 */
1934 static void LIBXML_ATTR_FORMAT(5,0)
1935 xmlSchemaPErr2(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node,
1936 xmlNodePtr child, int error,
1937 const char *msg, const xmlChar * str1, const xmlChar * str2)
1938 {
1939 if (child != NULL)
1940 xmlSchemaPErr(ctxt, child, error, msg, str1, str2);
1941 else
1942 xmlSchemaPErr(ctxt, node, error, msg, str1, str2);
1943 }
1944
1945
1946 /**
1947 * xmlSchemaPErrExt:
1948 * @ctxt: the parsing context
1949 * @node: the context node
1950 * @error: the error code
1951 * @strData1: extra data
1952 * @strData2: extra data
1953 * @strData3: extra data
1954 * @msg: the message
1955 * @str1: extra parameter for the message display
1956 * @str2: extra parameter for the message display
1957 * @str3: extra parameter for the message display
1958 * @str4: extra parameter for the message display
1959 * @str5: extra parameter for the message display
1960 *
1961 * Handle a parser error
1962 */
1963 static void LIBXML_ATTR_FORMAT(7,0)
1964 xmlSchemaPErrExt(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node, int error,
1965 const xmlChar * strData1, const xmlChar * strData2,
1966 const xmlChar * strData3, const char *msg, const xmlChar * str1,
1967 const xmlChar * str2, const xmlChar * str3, const xmlChar * str4,
1968 const xmlChar * str5)
1969 {
1970
1971 xmlGenericErrorFunc channel = NULL;
1972 xmlStructuredErrorFunc schannel = NULL;
1973 void *data = NULL;
1974
1975 if (ctxt != NULL) {
1976 ctxt->nberrors++;
1977 ctxt->err = error;
1978 channel = ctxt->error;
1979 data = ctxt->errCtxt;
1980 schannel = ctxt->serror;
1981 }
1982 __xmlRaiseError(schannel, channel, data, ctxt, node, XML_FROM_SCHEMASP,
1983 error, XML_ERR_ERROR, NULL, 0,
1984 (const char *) strData1, (const char *) strData2,
1985 (const char *) strData3, 0, 0, msg, str1, str2,
1986 str3, str4, str5);
1987 }
1988
1989 /************************************************************************
1990 * *
1991 * Allround error functions *
1992 * *
1993 ************************************************************************/
1994
1995 /**
1996 * xmlSchemaVTypeErrMemory:
1997 * @node: a context node
1998 * @extra: extra informations
1999 *
2000 * Handle an out of memory condition
2001 */
2002 static void
2003 xmlSchemaVErrMemory(xmlSchemaValidCtxtPtr ctxt,
2004 const char *extra, xmlNodePtr node)
2005 {
2006 if (ctxt != NULL) {
2007 ctxt->nberrors++;
2008 ctxt->err = XML_SCHEMAV_INTERNAL;
2009 }
2010 __xmlSimpleError(XML_FROM_SCHEMASV, XML_ERR_NO_MEMORY, node, NULL,
2011 extra);
2012 }
2013
2014 static void LIBXML_ATTR_FORMAT(2,0)
2015 xmlSchemaPSimpleInternalErr(xmlNodePtr node,
2016 const char *msg, const xmlChar *str)
2017 {
2018 __xmlSimpleError(XML_FROM_SCHEMASP, XML_SCHEMAP_INTERNAL, node,
2019 msg, (const char *) str);
2020 }
2021
2022 #define WXS_ERROR_TYPE_ERROR 1
2023 #define WXS_ERROR_TYPE_WARNING 2
2024 /**
2025 * xmlSchemaErr4Line:
2026 * @ctxt: the validation context
2027 * @errorLevel: the error level
2028 * @error: the error code
2029 * @node: the context node
2030 * @line: the line number
2031 * @msg: the error message
2032 * @str1: extra data
2033 * @str2: extra data
2034 * @str3: extra data
2035 * @str4: extra data
2036 *
2037 * Handle a validation error
2038 */
2039 static void LIBXML_ATTR_FORMAT(6,0)
2040 xmlSchemaErr4Line(xmlSchemaAbstractCtxtPtr ctxt,
2041 xmlErrorLevel errorLevel,
2042 int error, xmlNodePtr node, int line, const char *msg,
2043 const xmlChar *str1, const xmlChar *str2,
2044 const xmlChar *str3, const xmlChar *str4)
2045 {
2046 xmlStructuredErrorFunc schannel = NULL;
2047 xmlGenericErrorFunc channel = NULL;
2048 void *data = NULL;
2049
2050 if (ctxt != NULL) {
2051 if (ctxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2052 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) ctxt;
2053 const char *file = NULL;
2054 int col = 0;
2055 if (errorLevel != XML_ERR_WARNING) {
2056 vctxt->nberrors++;
2057 vctxt->err = error;
2058 channel = vctxt->error;
2059 } else {
2060 channel = vctxt->warning;
2061 }
2062 schannel = vctxt->serror;
2063 data = vctxt->errCtxt;
2064
2065 /*
2066 * Error node. If we specify a line number, then
2067 * do not channel any node to the error function.
2068 */
2069 if (line == 0) {
2070 if ((node == NULL) &&
2071 (vctxt->depth >= 0) &&
2072 (vctxt->inode != NULL)) {
2073 node = vctxt->inode->node;
2074 }
2075 /*
2076 * Get filename and line if no node-tree.
2077 */
2078 if ((node == NULL) &&
2079 (vctxt->parserCtxt != NULL) &&
2080 (vctxt->parserCtxt->input != NULL)) {
2081 file = vctxt->parserCtxt->input->filename;
2082 line = vctxt->parserCtxt->input->line;
2083 col = vctxt->parserCtxt->input->col;
2084 }
2085 } else {
2086 /*
2087 * Override the given node's (if any) position
2088 * and channel only the given line number.
2089 */
2090 node = NULL;
2091 /*
2092 * Get filename.
2093 */
2094 if (vctxt->doc != NULL)
2095 file = (const char *) vctxt->doc->URL;
2096 else if ((vctxt->parserCtxt != NULL) &&
2097 (vctxt->parserCtxt->input != NULL))
2098 file = vctxt->parserCtxt->input->filename;
2099 }
2100 if (vctxt->locFunc != NULL) {
2101 if ((file == NULL) || (line == 0)) {
2102 unsigned long l;
2103 const char *f;
2104 vctxt->locFunc(vctxt->locCtxt, &f, &l);
2105 if (file == NULL)
2106 file = f;
2107 if (line == 0)
2108 line = (int) l;
2109 }
2110 }
2111 if ((file == NULL) && (vctxt->filename != NULL))
2112 file = vctxt->filename;
2113
2114 __xmlRaiseError(schannel, channel, data, ctxt,
2115 node, XML_FROM_SCHEMASV,
2116 error, errorLevel, file, line,
2117 (const char *) str1, (const char *) str2,
2118 (const char *) str3, 0, col, msg, str1, str2, str3, str4);
2119
2120 } else if (ctxt->type == XML_SCHEMA_CTXT_PARSER) {
2121 xmlSchemaParserCtxtPtr pctxt = (xmlSchemaParserCtxtPtr) ctxt;
2122 if (errorLevel != XML_ERR_WARNING) {
2123 pctxt->nberrors++;
2124 pctxt->err = error;
2125 channel = pctxt->error;
2126 } else {
2127 channel = pctxt->warning;
2128 }
2129 schannel = pctxt->serror;
2130 data = pctxt->errCtxt;
2131 __xmlRaiseError(schannel, channel, data, ctxt,
2132 node, XML_FROM_SCHEMASP, error,
2133 errorLevel, NULL, 0,
2134 (const char *) str1, (const char *) str2,
2135 (const char *) str3, 0, 0, msg, str1, str2, str3, str4);
2136 } else {
2137 TODO
2138 }
2139 }
2140 }
2141
2142 /**
2143 * xmlSchemaErr3:
2144 * @ctxt: the validation context
2145 * @node: the context node
2146 * @error: the error code
2147 * @msg: the error message
2148 * @str1: extra data
2149 * @str2: extra data
2150 * @str3: extra data
2151 *
2152 * Handle a validation error
2153 */
2154 static void LIBXML_ATTR_FORMAT(4,0)
2155 xmlSchemaErr3(xmlSchemaAbstractCtxtPtr actxt,
2156 int error, xmlNodePtr node, const char *msg,
2157 const xmlChar *str1, const xmlChar *str2, const xmlChar *str3)
2158 {
2159 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2160 msg, str1, str2, str3, NULL);
2161 }
2162
2163 static void LIBXML_ATTR_FORMAT(4,0)
2164 xmlSchemaErr4(xmlSchemaAbstractCtxtPtr actxt,
2165 int error, xmlNodePtr node, const char *msg,
2166 const xmlChar *str1, const xmlChar *str2,
2167 const xmlChar *str3, const xmlChar *str4)
2168 {
2169 xmlSchemaErr4Line(actxt, XML_ERR_ERROR, error, node, 0,
2170 msg, str1, str2, str3, str4);
2171 }
2172
2173 static void LIBXML_ATTR_FORMAT(4,0)
2174 xmlSchemaErr(xmlSchemaAbstractCtxtPtr actxt,
2175 int error, xmlNodePtr node, const char *msg,
2176 const xmlChar *str1, const xmlChar *str2)
2177 {
2178 xmlSchemaErr4(actxt, error, node, msg, str1, str2, NULL, NULL);
2179 }
2180
2181 static xmlChar *
2182 xmlSchemaFormatNodeForError(xmlChar ** msg,
2183 xmlSchemaAbstractCtxtPtr actxt,
2184 xmlNodePtr node)
2185 {
2186 xmlChar *str = NULL;
2187
2188 *msg = NULL;
2189 if ((node != NULL) &&
2190 (node->type != XML_ELEMENT_NODE) &&
2191 (node->type != XML_ATTRIBUTE_NODE))
2192 {
2193 /*
2194 * Don't try to format other nodes than element and
2195 * attribute nodes.
2196 * Play safe and return an empty string.
2197 */
2198 *msg = xmlStrdup(BAD_CAST "");
2199 return(*msg);
2200 }
2201 if (node != NULL) {
2202 /*
2203 * Work on tree nodes.
2204 */
2205 if (node->type == XML_ATTRIBUTE_NODE) {
2206 xmlNodePtr elem = node->parent;
2207
2208 *msg = xmlStrdup(BAD_CAST "Element '");
2209 if (elem->ns != NULL)
2210 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2211 elem->ns->href, elem->name));
2212 else
2213 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2214 NULL, elem->name));
2215 FREE_AND_NULL(str);
2216 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2217 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2218 } else {
2219 *msg = xmlStrdup(BAD_CAST "Element '");
2220 }
2221 if (node->ns != NULL)
2222 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2223 node->ns->href, node->name));
2224 else
2225 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2226 NULL, node->name));
2227 FREE_AND_NULL(str);
2228 *msg = xmlStrcat(*msg, BAD_CAST "': ");
2229 } else if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR) {
2230 xmlSchemaValidCtxtPtr vctxt = (xmlSchemaValidCtxtPtr) actxt;
2231 /*
2232 * Work on node infos.
2233 */
2234 if (vctxt->inode->nodeType == XML_ATTRIBUTE_NODE) {
2235 xmlSchemaNodeInfoPtr ielem =
2236 vctxt->elemInfos[vctxt->depth];
2237
2238 *msg = xmlStrdup(BAD_CAST "Element '");
2239 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2240 ielem->nsName, ielem->localName));
2241 FREE_AND_NULL(str);
2242 *msg = xmlStrcat(*msg, BAD_CAST "', ");
2243 *msg = xmlStrcat(*msg, BAD_CAST "attribute '");
2244 } else {
2245 *msg = xmlStrdup(BAD_CAST "Element '");
2246 }
2247 *msg = xmlStrcat(*msg, xmlSchemaFormatQName(&str,
2248 vctxt->inode->nsName, vctxt->inode->localName));
2249 FREE_AND_NULL(str);
2250 *msg = xmlStrcat(*msg, BAD_CAST "': ");
2251 } else if (actxt->type == XML_SCHEMA_CTXT_PARSER) {
2252 /*
2253 * Hmm, no node while parsing?
2254 * Return an empty string, in case NULL will break something.
2255 */
2256 *msg = xmlStrdup(BAD_CAST "");
2257 } else {
2258 TODO
2259 return (NULL);
2260 }
2261
2262 /*
2263 * xmlSchemaFormatItemForReport() also returns an escaped format
2264 * string, so do this before calling it below (in the future).
2265 */
2266 xmlEscapeFormatString(msg);
2267
2268 /*
2269 * VAL TODO: The output of the given schema component is currently
2270 * disabled.
2271 */
2272 #if 0
2273 if ((type != NULL) && (xmlSchemaIsGlobalItem(type))) {
2274 *msg = xmlStrcat(*msg, BAD_CAST " [");
2275 *msg = xmlStrcat(*msg, xmlSchemaFormatItemForReport(&str,
2276 NULL, type, NULL, 0));
2277 FREE_AND_NULL(str)
2278 *msg = xmlStrcat(*msg, BAD_CAST "]");
2279 }
2280 #endif
2281 return (*msg);
2282 }
2283
2284 static void LIBXML_ATTR_FORMAT(3,0)
2285 xmlSchemaInternalErr2(xmlSchemaAbstractCtxtPtr actxt,
2286 const char *funcName,
2287 const char *message,
2288 const xmlChar *str1,
2289 const xmlChar *str2)
2290 {
2291 xmlChar *msg = NULL;
2292
2293 if (actxt == NULL)
2294 return;
2295 msg = xmlStrdup(BAD_CAST "Internal error: %s, ");
2296 msg = xmlStrcat(msg, BAD_CAST message);
2297 msg = xmlStrcat(msg, BAD_CAST ".\n");
2298
2299 if (actxt->type == XML_SCHEMA_CTXT_VALIDATOR)
2300 xmlSchemaErr3(actxt, XML_SCHEMAV_INTERNAL, NULL,
2301 (const char *) msg, (const xmlChar *) funcName, str1, str2);
2302 else if (actxt->type == XML_SCHEMA_CTXT_PARSER)
2303 xmlSchemaErr3(actxt, XML_SCHEMAP_INTERNAL, NULL,
2304 (const char *) msg, (const xmlChar *) funcName, str1, str2);
2305
2306 FREE_AND_NULL(msg)
2307 }
2308
2309 static void LIBXML_ATTR_FORMAT(3,0)
2310 xmlSchemaInternalErr(xmlSchemaAbstractCtxtPtr actxt,
2311 const char *funcName,
2312 const char *message)
2313 {
2314 xmlSchemaInternalErr2(actxt, funcName, message, NULL, NULL);
2315 }
2316
2317 #if 0
2318 static void LIBXML_ATTR_FORMAT(3,0)
2319 xmlSchemaPInternalErr(xmlSchemaParserCtxtPtr pctxt,
2320 const char *funcName,
2321 const char *message,
2322 const xmlChar *str1,
2323 const xmlChar *str2)
2324 {
2325 xmlSchemaInternalErr2(ACTXT_CAST pctxt, funcName, message,
2326 str1, str2);
2327 }
2328 #endif
2329
2330 static void LIBXML_ATTR_FORMAT(5,0)
2331 xmlSchemaCustomErr4(xmlSchemaAbstractCtxtPtr actxt,
2332 xmlParserErrors error,
2333 xmlNodePtr node,
2334 xmlSchemaBasicItemPtr item,
2335 const char *message,
2336 const xmlChar *str1, const xmlChar *str2,
2337 const xmlChar *str3, const xmlChar *str4)
2338 {
2339 xmlChar *msg = NULL;
2340
2341 if ((node == NULL) && (item != NULL) &&
2342 (actxt->type == XML_SCHEMA_CTXT_PARSER)) {
2343 node = WXS_ITEM_NODE(item);
2344 xmlSchemaFormatItemForReport(&msg, NULL, item, NULL);
2345 msg = xmlStrcat(msg, BAD_CAST ": ");
2346 } else
2347 xmlSchemaFormatNodeForError(&msg, actxt, node);
2348 msg = xmlStrcat(msg, (const xmlChar *) message);
2349 msg = xmlStrcat(msg, BAD_CAST ".\n");
2350 xmlSchemaErr4(actxt, error, node,
2351 (const char *) msg, str1, str2, str3, str4);
2352 FREE_AND_NULL(msg)
2353 }
2354
2355 static void LIBXML_ATTR_FORMAT(5,0)
2356 xmlSchemaCustomErr(xmlSchemaAbstractCtxtPtr actxt,
2357 xmlParserErrors error,
2358 xmlNodePtr node,
2359 xmlSchemaBasicItemPtr item,
2360 const char *message,
2361 const xmlChar *str1,
2362 const xmlChar *str2)
2363 {
2364 xmlSchemaCustomErr4(actxt, error, node, item,
2365 message, str1, str2, NULL, NULL);
2366 }
2367
2368
2369
2370 static void LIBXML_ATTR_FORMAT(5,0)
2371 xmlSchemaCustomWarning(xmlSchemaAbstractCtxtPtr actxt,
2372 xmlParserErrors error,
2373 xmlNodePtr node,
2374 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2375 const char *message,
2376 const xmlChar *str1,
2377 const xmlChar *str2,
2378 const xmlChar *str3)
2379 {
2380 xmlChar *msg = NULL;
2381
2382 xmlSchemaFormatNodeForError(&msg, actxt, node);
2383 msg = xmlStrcat(msg, (const xmlChar *) message);
2384 msg = xmlStrcat(msg, BAD_CAST ".\n");
2385
2386 /* URGENT TODO: Set the error code to something sane. */
2387 xmlSchemaErr4Line(actxt, XML_ERR_WARNING, error, node, 0,
2388 (const char *) msg, str1, str2, str3, NULL);
2389
2390 FREE_AND_NULL(msg)
2391 }
2392
2393
2394
2395 static void LIBXML_ATTR_FORMAT(5,0)
2396 xmlSchemaKeyrefErr(xmlSchemaValidCtxtPtr vctxt,
2397 xmlParserErrors error,
2398 xmlSchemaPSVIIDCNodePtr idcNode,
2399 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2400 const char *message,
2401 const xmlChar *str1,
2402 const xmlChar *str2)
2403 {
2404 xmlChar *msg = NULL, *qname = NULL;
2405
2406 msg = xmlStrdup(BAD_CAST "Element '%s': ");
2407 msg = xmlStrcat(msg, (const xmlChar *) message);
2408 msg = xmlStrcat(msg, BAD_CAST ".\n");
2409 xmlSchemaErr4Line(ACTXT_CAST vctxt, XML_ERR_ERROR,
2410 error, NULL, idcNode->nodeLine, (const char *) msg,
2411 xmlSchemaFormatQName(&qname,
2412 vctxt->nodeQNames->items[idcNode->nodeQNameID +1],
2413 vctxt->nodeQNames->items[idcNode->nodeQNameID]),
2414 str1, str2, NULL);
2415 FREE_AND_NULL(qname);
2416 FREE_AND_NULL(msg);
2417 }
2418
2419 static int
2420 xmlSchemaEvalErrorNodeType(xmlSchemaAbstractCtxtPtr actxt,
2421 xmlNodePtr node)
2422 {
2423 if (node != NULL)
2424 return (node->type);
2425 if ((actxt->type == XML_SCHEMA_CTXT_VALIDATOR) &&
2426 (((xmlSchemaValidCtxtPtr) actxt)->inode != NULL))
2427 return ( ((xmlSchemaValidCtxtPtr) actxt)->inode->nodeType);
2428 return (-1);
2429 }
2430
2431 static int
2432 xmlSchemaIsGlobalItem(xmlSchemaTypePtr item)
2433 {
2434 switch (item->type) {
2435 case XML_SCHEMA_TYPE_COMPLEX:
2436 case XML_SCHEMA_TYPE_SIMPLE:
2437 if (item->flags & XML_SCHEMAS_TYPE_GLOBAL)
2438 return(1);
2439 break;
2440 case XML_SCHEMA_TYPE_GROUP:
2441 return (1);
2442 case XML_SCHEMA_TYPE_ELEMENT:
2443 if ( ((xmlSchemaElementPtr) item)->flags &
2444 XML_SCHEMAS_ELEM_GLOBAL)
2445 return(1);
2446 break;
2447 case XML_SCHEMA_TYPE_ATTRIBUTE:
2448 if ( ((xmlSchemaAttributePtr) item)->flags &
2449 XML_SCHEMAS_ATTR_GLOBAL)
2450 return(1);
2451 break;
2452 /* Note that attribute groups are always global. */
2453 default:
2454 return(1);
2455 }
2456 return (0);
2457 }
2458
2459 static void
2460 xmlSchemaSimpleTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2461 xmlParserErrors error,
2462 xmlNodePtr node,
2463 const xmlChar *value,
2464 xmlSchemaTypePtr type,
2465 int displayValue)
2466 {
2467 xmlChar *msg = NULL;
2468
2469 xmlSchemaFormatNodeForError(&msg, actxt, node);
2470
2471 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2472 XML_ATTRIBUTE_NODE))
2473 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
2474 else
2475 msg = xmlStrcat(msg, BAD_CAST "The character content is not a valid "
2476 "value of ");
2477
2478 if (! xmlSchemaIsGlobalItem(type))
2479 msg = xmlStrcat(msg, BAD_CAST "the local ");
2480 else
2481 msg = xmlStrcat(msg, BAD_CAST "the ");
2482
2483 if (WXS_IS_ATOMIC(type))
2484 msg = xmlStrcat(msg, BAD_CAST "atomic type");
2485 else if (WXS_IS_LIST(type))
2486 msg = xmlStrcat(msg, BAD_CAST "list type");
2487 else if (WXS_IS_UNION(type))
2488 msg = xmlStrcat(msg, BAD_CAST "union type");
2489
2490 if (xmlSchemaIsGlobalItem(type)) {
2491 xmlChar *str = NULL;
2492 msg = xmlStrcat(msg, BAD_CAST " '");
2493 if (type->builtInType != 0) {
2494 msg = xmlStrcat(msg, BAD_CAST "xs:");
2495 str = xmlStrdup(type->name);
2496 } else {
2497 const xmlChar *qName = xmlSchemaFormatQName(&str, type->targetNamespace, type->name);
2498 if (!str)
2499 str = xmlStrdup(qName);
2500 }
2501 msg = xmlStrcat(msg, xmlEscapeFormatString(&str));
2502 msg = xmlStrcat(msg, BAD_CAST "'");
2503 FREE_AND_NULL(str);
2504 }
2505 msg = xmlStrcat(msg, BAD_CAST ".\n");
2506 if (displayValue || (xmlSchemaEvalErrorNodeType(actxt, node) ==
2507 XML_ATTRIBUTE_NODE))
2508 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2509 else
2510 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2511 FREE_AND_NULL(msg)
2512 }
2513
2514 static const xmlChar *
2515 xmlSchemaFormatErrorNodeQName(xmlChar ** str,
2516 xmlSchemaNodeInfoPtr ni,
2517 xmlNodePtr node)
2518 {
2519 if (node != NULL) {
2520 if (node->ns != NULL)
2521 return (xmlSchemaFormatQName(str, node->ns->href, node->name));
2522 else
2523 return (xmlSchemaFormatQName(str, NULL, node->name));
2524 } else if (ni != NULL)
2525 return (xmlSchemaFormatQName(str, ni->nsName, ni->localName));
2526 return (NULL);
2527 }
2528
2529 static void
2530 xmlSchemaIllegalAttrErr(xmlSchemaAbstractCtxtPtr actxt,
2531 xmlParserErrors error,
2532 xmlSchemaAttrInfoPtr ni,
2533 xmlNodePtr node)
2534 {
2535 xmlChar *msg = NULL, *str = NULL;
2536
2537 xmlSchemaFormatNodeForError(&msg, actxt, node);
2538 msg = xmlStrcat(msg, BAD_CAST "The attribute '%s' is not allowed.\n");
2539 xmlSchemaErr(actxt, error, node, (const char *) msg,
2540 xmlSchemaFormatErrorNodeQName(&str, (xmlSchemaNodeInfoPtr) ni, node),
2541 NULL);
2542 FREE_AND_NULL(str)
2543 FREE_AND_NULL(msg)
2544 }
2545
2546 static void LIBXML_ATTR_FORMAT(5,0)
2547 xmlSchemaComplexTypeErr(xmlSchemaAbstractCtxtPtr actxt,
2548 xmlParserErrors error,
2549 xmlNodePtr node,
2550 xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
2551 const char *message,
2552 int nbval,
2553 int nbneg,
2554 xmlChar **values)
2555 {
2556 xmlChar *str = NULL, *msg = NULL;
2557 xmlChar *localName, *nsName;
2558 const xmlChar *cur, *end;
2559 int i;
2560
2561 xmlSchemaFormatNodeForError(&msg, actxt, node);
2562 msg = xmlStrcat(msg, (const xmlChar *) message);
2563 msg = xmlStrcat(msg, BAD_CAST ".");
2564 /*
2565 * Note that is does not make sense to report that we have a
2566 * wildcard here, since the wildcard might be unfolded into
2567 * multiple transitions.
2568 */
2569 if (nbval + nbneg > 0) {
2570 if (nbval + nbneg > 1) {
2571 str = xmlStrdup(BAD_CAST " Expected is one of ( ");
2572 } else
2573 str = xmlStrdup(BAD_CAST " Expected is ( ");
2574 nsName = NULL;
2575
2576 for (i = 0; i < nbval + nbneg; i++) {
2577 cur = values[i];
2578 if (cur == NULL)
2579 continue;
2580 if ((cur[0] == 'n') && (cur[1] == 'o') && (cur[2] == 't') &&
2581 (cur[3] == ' ')) {
2582 cur += 4;
2583 str = xmlStrcat(str, BAD_CAST "##other");
2584 }
2585 /*
2586 * Get the local name.
2587 */
2588 localName = NULL;
2589
2590 end = cur;
2591 if (*end == '*') {
2592 localName = xmlStrdup(BAD_CAST "*");
2593 end++;
2594 } else {
2595 while ((*end != 0) && (*end != '|'))
2596 end++;
2597 localName = xmlStrncat(localName, BAD_CAST cur, end - cur);
2598 }
2599 if (*end != 0) {
2600 end++;
2601 /*
2602 * Skip "*|*" if they come with negated expressions, since
2603 * they represent the same negated wildcard.
2604 */
2605 if ((nbneg == 0) || (*end != '*') || (*localName != '*')) {
2606 /*
2607 * Get the namespace name.
2608 */
2609 cur = end;
2610 if (*end == '*') {
2611 nsName = xmlStrdup(BAD_CAST "{*}");
2612 } else {
2613 while (*end != 0)
2614 end++;
2615
2616 if (i >= nbval)
2617 nsName = xmlStrdup(BAD_CAST "{##other:");
2618 else
2619 nsName = xmlStrdup(BAD_CAST "{");
2620
2621 nsName = xmlStrncat(nsName, BAD_CAST cur, end - cur);
2622 nsName = xmlStrcat(nsName, BAD_CAST "}");
2623 }
2624 str = xmlStrcat(str, BAD_CAST nsName);
2625 FREE_AND_NULL(nsName)
2626 } else {
2627 FREE_AND_NULL(localName);
2628 continue;
2629 }
2630 }
2631 str = xmlStrcat(str, BAD_CAST localName);
2632 FREE_AND_NULL(localName);
2633
2634 if (i < nbval + nbneg -1)
2635 str = xmlStrcat(str, BAD_CAST ", ");
2636 }
2637 str = xmlStrcat(str, BAD_CAST " ).\n");
2638 msg = xmlStrcat(msg, xmlEscapeFormatString(&str));
2639 FREE_AND_NULL(str)
2640 } else
2641 msg = xmlStrcat(msg, BAD_CAST "\n");
2642 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2643 xmlFree(msg);
2644 }
2645
2646 static void LIBXML_ATTR_FORMAT(8,0)
2647 xmlSchemaFacetErr(xmlSchemaAbstractCtxtPtr actxt,
2648 xmlParserErrors error,
2649 xmlNodePtr node,
2650 const xmlChar *value,
2651 unsigned long length,
2652 xmlSchemaTypePtr type,
2653 xmlSchemaFacetPtr facet,
2654 const char *message,
2655 const xmlChar *str1,
2656 const xmlChar *str2)
2657 {
2658 xmlChar *str = NULL, *msg = NULL;
2659 xmlSchemaTypeType facetType;
2660 int nodeType = xmlSchemaEvalErrorNodeType(actxt, node);
2661
2662 xmlSchemaFormatNodeForError(&msg, actxt, node);
2663 if (error == XML_SCHEMAV_CVC_ENUMERATION_VALID) {
2664 facetType = XML_SCHEMA_FACET_ENUMERATION;
2665 /*
2666 * If enumerations are validated, one must not expect the
2667 * facet to be given.
2668 */
2669 } else
2670 facetType = facet->type;
2671 msg = xmlStrcat(msg, BAD_CAST "[");
2672 msg = xmlStrcat(msg, BAD_CAST "facet '");
2673 msg = xmlStrcat(msg, xmlSchemaFacetTypeToString(facetType));
2674 msg = xmlStrcat(msg, BAD_CAST "'] ");
2675 if (message == NULL) {
2676 /*
2677 * Use a default message.
2678 */
2679 if ((facetType == XML_SCHEMA_FACET_LENGTH) ||
2680 (facetType == XML_SCHEMA_FACET_MINLENGTH) ||
2681 (facetType == XML_SCHEMA_FACET_MAXLENGTH)) {
2682
2683 char len[25], actLen[25];
2684
2685 /* FIXME, TODO: What is the max expected string length of the
2686 * this value?
2687 */
2688 if (nodeType == XML_ATTRIBUTE_NODE)
2689 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has a length of '%s'; ");
2690 else
2691 msg = xmlStrcat(msg, BAD_CAST "The value has a length of '%s'; ");
2692
2693 snprintf(len, 24, "%lu", xmlSchemaGetFacetValueAsULong(facet));
2694 snprintf(actLen, 24, "%lu", length);
2695
2696 if (facetType == XML_SCHEMA_FACET_LENGTH)
2697 msg = xmlStrcat(msg,
2698 BAD_CAST "this differs from the allowed length of '%s'.\n");
2699 else if (facetType == XML_SCHEMA_FACET_MAXLENGTH)
2700 msg = xmlStrcat(msg,
2701 BAD_CAST "this exceeds the allowed maximum length of '%s'.\n");
2702 else if (facetType == XML_SCHEMA_FACET_MINLENGTH)
2703 msg = xmlStrcat(msg,
2704 BAD_CAST "this underruns the allowed minimum length of '%s'.\n");
2705
2706 if (nodeType == XML_ATTRIBUTE_NODE)
2707 xmlSchemaErr3(actxt, error, node, (const char *) msg,
2708 value, (const xmlChar *) actLen, (const xmlChar *) len);
2709 else
2710 xmlSchemaErr(actxt, error, node, (const char *) msg,
2711 (const xmlChar *) actLen, (const xmlChar *) len);
2712
2713 } else if (facetType == XML_SCHEMA_FACET_ENUMERATION) {
2714 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not an element "
2715 "of the set {%s}.\n");
2716 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2717 xmlSchemaFormatFacetEnumSet(actxt, &str, type));
2718 } else if (facetType == XML_SCHEMA_FACET_PATTERN) {
2719 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not accepted "
2720 "by the pattern '%s'.\n");
2721 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2722 facet->value);
2723 } else if (facetType == XML_SCHEMA_FACET_MININCLUSIVE) {
2724 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is less than the "
2725 "minimum value allowed ('%s').\n");
2726 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2727 facet->value);
2728 } else if (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) {
2729 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is greater than the "
2730 "maximum value allowed ('%s').\n");
2731 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2732 facet->value);
2733 } else if (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE) {
2734 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be greater than "
2735 "'%s'.\n");
2736 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2737 facet->value);
2738 } else if (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) {
2739 msg = xmlStrcat(msg, BAD_CAST "The value '%s' must be less than "
2740 "'%s'.\n");
2741 xmlSchemaErr(actxt, error, node, (const char *) msg, value,
2742 facet->value);
2743 } else if (facetType == XML_SCHEMA_FACET_TOTALDIGITS) {
2744 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more "
2745 "digits than are allowed ('%s').\n");
2746 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2747 facet->value);
2748 } else if (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) {
2749 msg = xmlStrcat(msg, BAD_CAST "The value '%s' has more fractional "
2750 "digits than are allowed ('%s').\n");
2751 xmlSchemaErr(actxt, error, node, (const char*) msg, value,
2752 facet->value);
2753 } else if (nodeType == XML_ATTRIBUTE_NODE) {
2754 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not facet-valid.\n");
2755 xmlSchemaErr(actxt, error, node, (const char *) msg, value, NULL);
2756 } else {
2757 msg = xmlStrcat(msg, BAD_CAST "The value is not facet-valid.\n");
2758 xmlSchemaErr(actxt, error, node, (const char *) msg, NULL, NULL);
2759 }
2760 } else {
2761 msg = xmlStrcat(msg, (const xmlChar *) message);
2762 msg = xmlStrcat(msg, BAD_CAST ".\n");
2763 xmlSchemaErr(actxt, error, node, (const char *) msg, str1, str2);
2764 }
2765 FREE_AND_NULL(str)
2766 xmlFree(msg);
2767 }
2768
2769 #define VERROR(err, type, msg) \
2770 xmlSchemaCustomErr(ACTXT_CAST vctxt, err, NULL, type, msg, NULL, NULL);
2771
2772 #define VERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST vctxt, func, msg);
2773
2774 #define PERROR_INT(func, msg) xmlSchemaInternalErr(ACTXT_CAST pctxt, func, msg);
2775 #define PERROR_INT2(func, msg) xmlSchemaInternalErr(ACTXT_CAST ctxt, func, msg);
2776
2777 #define AERROR_INT(func, msg) xmlSchemaInternalErr(actxt, func, msg);
2778
2779
2780 /**
2781 * xmlSchemaPMissingAttrErr:
2782 * @ctxt: the schema validation context
2783 * @ownerDes: the designation of the owner
2784 * @ownerName: the name of the owner
2785 * @ownerItem: the owner as a schema object
2786 * @ownerElem: the owner as an element node
2787 * @node: the parent element node of the missing attribute node
2788 * @type: the corresponding type of the attribute node
2789 *
2790 * Reports an illegal attribute.
2791 */
2792 static void
2793 xmlSchemaPMissingAttrErr(xmlSchemaParserCtxtPtr ctxt,
2794 xmlParserErrors error,
2795 xmlSchemaBasicItemPtr ownerItem,
2796 xmlNodePtr ownerElem,
2797 const char *name,
2798 const char *message)
2799 {
2800 xmlChar *des = NULL;
2801
2802 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
2803
2804 if (message != NULL)
2805 xmlSchemaPErr(ctxt, ownerElem, error, "%s: %s.\n", BAD_CAST des, BAD_CAST message);
2806 else
2807 xmlSchemaPErr(ctxt, ownerElem, error,
2808 "%s: The attribute '%s' is required but missing.\n",
2809 BAD_CAST des, BAD_CAST name);
2810 FREE_AND_NULL(des);
2811 }
2812
2813
2814 /**
2815 * xmlSchemaPResCompAttrErr:
2816 * @ctxt: the schema validation context
2817 * @error: the error code
2818 * @ownerDes: the designation of the owner
2819 * @ownerItem: the owner as a schema object
2820 * @ownerElem: the owner as an element node
2821 * @name: the name of the attribute holding the QName
2822 * @refName: the referenced local name
2823 * @refURI: the referenced namespace URI
2824 * @message: optional message
2825 *
2826 * Used to report QName attribute values that failed to resolve
2827 * to schema components.
2828 */
2829 static void
2830 xmlSchemaPResCompAttrErr(xmlSchemaParserCtxtPtr ctxt,
2831 xmlParserErrors error,
2832 xmlSchemaBasicItemPtr ownerItem,
2833 xmlNodePtr ownerElem,
2834 const char *name,
2835 const xmlChar *refName,
2836 const xmlChar *refURI,
2837 xmlSchemaTypeType refType,
2838 const char *refTypeStr)
2839 {
2840 xmlChar *des = NULL, *strA = NULL;
2841
2842 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
2843 if (refTypeStr == NULL)
2844 refTypeStr = (const char *) xmlSchemaItemTypeToStr(refType);
2845 xmlSchemaPErrExt(ctxt, ownerElem, error,
2846 NULL, NULL, NULL,
2847 "%s, attribute '%s': The QName value '%s' does not resolve to a(n) "
2848 "%s.\n", BAD_CAST des, BAD_CAST name,
2849 xmlSchemaFormatQName(&strA, refURI, refName),
2850 BAD_CAST refTypeStr, NULL);
2851 FREE_AND_NULL(des)
2852 FREE_AND_NULL(strA)
2853 }
2854
2855 /**
2856 * xmlSchemaPCustomAttrErr:
2857 * @ctxt: the schema parser context
2858 * @error: the error code
2859 * @ownerDes: the designation of the owner
2860 * @ownerItem: the owner as a schema object
2861 * @attr: the illegal attribute node
2862 *
2863 * Reports an illegal attribute during the parse.
2864 */
2865 static void
2866 xmlSchemaPCustomAttrErr(xmlSchemaParserCtxtPtr ctxt,
2867 xmlParserErrors error,
2868 xmlChar **ownerDes,
2869 xmlSchemaBasicItemPtr ownerItem,
2870 xmlAttrPtr attr,
2871 const char *msg)
2872 {
2873 xmlChar *des = NULL;
2874
2875 if (ownerDes == NULL)
2876 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, attr->parent);
2877 else if (*ownerDes == NULL) {
2878 xmlSchemaFormatItemForReport(ownerDes, NULL, ownerItem, attr->parent);
2879 des = *ownerDes;
2880 } else
2881 des = *ownerDes;
2882 if (attr == NULL) {
2883 xmlSchemaPErrExt(ctxt, NULL, error, NULL, NULL, NULL,
2884 "%s, attribute '%s': %s.\n",
2885 BAD_CAST des, (const xmlChar *) "Unknown",
2886 (const xmlChar *) msg, NULL, NULL);
2887 } else {
2888 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
2889 "%s, attribute '%s': %s.\n",
2890 BAD_CAST des, attr->name, (const xmlChar *) msg, NULL, NULL);
2891 }
2892 if (ownerDes == NULL)
2893 FREE_AND_NULL(des);
2894 }
2895
2896 /**
2897 * xmlSchemaPIllegalAttrErr:
2898 * @ctxt: the schema parser context
2899 * @error: the error code
2900 * @ownerDes: the designation of the attribute's owner
2901 * @ownerItem: the attribute's owner item
2902 * @attr: the illegal attribute node
2903 *
2904 * Reports an illegal attribute during the parse.
2905 */
2906 static void
2907 xmlSchemaPIllegalAttrErr(xmlSchemaParserCtxtPtr ctxt,
2908 xmlParserErrors error,
2909 xmlSchemaBasicItemPtr ownerComp ATTRIBUTE_UNUSED,
2910 xmlAttrPtr attr)
2911 {
2912 xmlChar *strA = NULL, *strB = NULL;
2913
2914 xmlSchemaFormatNodeForError(&strA, ACTXT_CAST ctxt, attr->parent);
2915 xmlSchemaErr4(ACTXT_CAST ctxt, error, (xmlNodePtr) attr,
2916 "%sThe attribute '%s' is not allowed.\n", BAD_CAST strA,
2917 xmlSchemaFormatQNameNs(&strB, attr->ns, attr->name),
2918 NULL, NULL);
2919 FREE_AND_NULL(strA);
2920 FREE_AND_NULL(strB);
2921 }
2922
2923 /**
2924 * xmlSchemaPCustomErr:
2925 * @ctxt: the schema parser context
2926 * @error: the error code
2927 * @itemDes: the designation of the schema item
2928 * @item: the schema item
2929 * @itemElem: the node of the schema item
2930 * @message: the error message
2931 * @str1: an optional param for the error message
2932 * @str2: an optional param for the error message
2933 * @str3: an optional param for the error message
2934 *
2935 * Reports an error during parsing.
2936 */
2937 static void LIBXML_ATTR_FORMAT(5,0)
2938 xmlSchemaPCustomErrExt(xmlSchemaParserCtxtPtr ctxt,
2939 xmlParserErrors error,
2940 xmlSchemaBasicItemPtr item,
2941 xmlNodePtr itemElem,
2942 const char *message,
2943 const xmlChar *str1,
2944 const xmlChar *str2,
2945 const xmlChar *str3)
2946 {
2947 xmlChar *des = NULL, *msg = NULL;
2948
2949 xmlSchemaFormatItemForReport(&des, NULL, item, itemElem);
2950 msg = xmlStrdup(BAD_CAST "%s: ");
2951 msg = xmlStrcat(msg, (const xmlChar *) message);
2952 msg = xmlStrcat(msg, BAD_CAST ".\n");
2953 if ((itemElem == NULL) && (item != NULL))
2954 itemElem = WXS_ITEM_NODE(item);
2955 xmlSchemaPErrExt(ctxt, itemElem, error, NULL, NULL, NULL,
2956 (const char *) msg, BAD_CAST des, str1, str2, str3, NULL);
2957 FREE_AND_NULL(des);
2958 FREE_AND_NULL(msg);
2959 }
2960
2961 /**
2962 * xmlSchemaPCustomErr:
2963 * @ctxt: the schema parser context
2964 * @error: the error code
2965 * @itemDes: the designation of the schema item
2966 * @item: the schema item
2967 * @itemElem: the node of the schema item
2968 * @message: the error message
2969 * @str1: the optional param for the error message
2970 *
2971 * Reports an error during parsing.
2972 */
2973 static void LIBXML_ATTR_FORMAT(5,0)
2974 xmlSchemaPCustomErr(xmlSchemaParserCtxtPtr ctxt,
2975 xmlParserErrors error,
2976 xmlSchemaBasicItemPtr item,
2977 xmlNodePtr itemElem,
2978 const char *message,
2979 const xmlChar *str1)
2980 {
2981 xmlSchemaPCustomErrExt(ctxt, error, item, itemElem, message,
2982 str1, NULL, NULL);
2983 }
2984
2985 /**
2986 * xmlSchemaPAttrUseErr:
2987 * @ctxt: the schema parser context
2988 * @error: the error code
2989 * @itemDes: the designation of the schema type
2990 * @item: the schema type
2991 * @itemElem: the node of the schema type
2992 * @attr: the invalid schema attribute
2993 * @message: the error message
2994 * @str1: the optional param for the error message
2995 *
2996 * Reports an attribute use error during parsing.
2997 */
2998 static void LIBXML_ATTR_FORMAT(6,0)
2999 xmlSchemaPAttrUseErr4(xmlSchemaParserCtxtPtr ctxt,
3000 xmlParserErrors error,
3001 xmlNodePtr node,
3002 xmlSchemaBasicItemPtr ownerItem,
3003 const xmlSchemaAttributeUsePtr attruse,
3004 const char *message,
3005 const xmlChar *str1, const xmlChar *str2,
3006 const xmlChar *str3,const xmlChar *str4)
3007 {
3008 xmlChar *str = NULL, *msg = NULL;
3009
3010 xmlSchemaFormatItemForReport(&msg, NULL, ownerItem, NULL);
3011 msg = xmlStrcat(msg, BAD_CAST ", ");
3012 msg = xmlStrcat(msg,
3013 BAD_CAST xmlSchemaFormatItemForReport(&str, NULL,
3014 WXS_BASIC_CAST attruse, NULL));
3015 FREE_AND_NULL(str);
3016 msg = xmlStrcat(msg, BAD_CAST ": ");
3017 msg = xmlStrcat(msg, (const xmlChar *) message);
3018 msg = xmlStrcat(msg, BAD_CAST ".\n");
3019 xmlSchemaErr4(ACTXT_CAST ctxt, error, node,
3020 (const char *) msg, str1, str2, str3, str4);
3021 xmlFree(msg);
3022 }
3023
3024 /**
3025 * xmlSchemaPIllegalFacetAtomicErr:
3026 * @ctxt: the schema parser context
3027 * @error: the error code
3028 * @type: the schema type
3029 * @baseType: the base type of type
3030 * @facet: the illegal facet
3031 *
3032 * Reports an illegal facet for atomic simple types.
3033 */
3034 static void
3035 xmlSchemaPIllegalFacetAtomicErr(xmlSchemaParserCtxtPtr ctxt,
3036 xmlParserErrors error,
3037 xmlSchemaTypePtr type,
3038 xmlSchemaTypePtr baseType,
3039 xmlSchemaFacetPtr facet)
3040 {
3041 xmlChar *des = NULL, *strT = NULL;
3042
3043 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type, type->node);
3044 xmlSchemaPErrExt(ctxt, type->node, error, NULL, NULL, NULL,
3045 "%s: The facet '%s' is not allowed on types derived from the "
3046 "type %s.\n",
3047 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type),
3048 xmlSchemaFormatItemForReport(&strT, NULL, WXS_BASIC_CAST baseType, NULL),
3049 NULL, NULL);
3050 FREE_AND_NULL(des);
3051 FREE_AND_NULL(strT);
3052 }
3053
3054 /**
3055 * xmlSchemaPIllegalFacetListUnionErr:
3056 * @ctxt: the schema parser context
3057 * @error: the error code
3058 * @itemDes: the designation of the schema item involved
3059 * @item: the schema item involved
3060 * @facet: the illegal facet
3061 *
3062 * Reports an illegal facet for <list> and <union>.
3063 */
3064 static void
3065 xmlSchemaPIllegalFacetListUnionErr(xmlSchemaParserCtxtPtr ctxt,
3066 xmlParserErrors error,
3067 xmlSchemaTypePtr type,
3068 xmlSchemaFacetPtr facet)
3069 {
3070 xmlChar *des = NULL;
3071
3072 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST type,
3073 type->node);
3074 xmlSchemaPErr(ctxt, type->node, error,
3075 "%s: The facet '%s' is not allowed.\n",
3076 BAD_CAST des, xmlSchemaFacetTypeToString(facet->type));
3077 FREE_AND_NULL(des);
3078 }
3079
3080 /**
3081 * xmlSchemaPMutualExclAttrErr:
3082 * @ctxt: the schema validation context
3083 * @error: the error code
3084 * @elemDes: the designation of the parent element node
3085 * @attr: the bad attribute node
3086 * @type: the corresponding type of the attribute node
3087 *
3088 * Reports an illegal attribute.
3089 */
3090 static void
3091 xmlSchemaPMutualExclAttrErr(xmlSchemaParserCtxtPtr ctxt,
3092 xmlParserErrors error,
3093 xmlSchemaBasicItemPtr ownerItem,
3094 xmlAttrPtr attr,
3095 const char *name1,
3096 const char *name2)
3097 {
3098 xmlChar *des = NULL;
3099
3100 xmlSchemaFormatItemForReport(&des, NULL, WXS_BASIC_CAST ownerItem, attr->parent);
3101 xmlSchemaPErrExt(ctxt, (xmlNodePtr) attr, error, NULL, NULL, NULL,
3102 "%s: The attributes '%s' and '%s' are mutually exclusive.\n",
3103 BAD_CAST des, BAD_CAST name1, BAD_CAST name2, NULL, NULL);
3104 FREE_AND_NULL(des);
3105 }
3106
3107 /**
3108 * xmlSchemaPSimpleTypeErr:
3109 * @ctxt: the schema validation context
3110 * @error: the error code
3111 * @type: the type specifier
3112 * @ownerDes: the designation of the owner
3113 * @ownerItem: the schema object if existent
3114 * @node: the validated node
3115 * @value: the validated value
3116 *
3117 * Reports a simple type validation error.
3118 * TODO: Should this report the value of an element as well?
3119 */
3120 static void LIBXML_ATTR_FORMAT(8,0)
3121 xmlSchemaPSimpleTypeErr(xmlSchemaParserCtxtPtr ctxt,
3122 xmlParserErrors error,
3123 xmlSchemaBasicItemPtr ownerItem ATTRIBUTE_UNUSED,
3124 xmlNodePtr node,
3125 xmlSchemaTypePtr type,
3126 const char *expected,
3127 const xmlChar *value,
3128 const char *message,
3129 const xmlChar *str1,
3130 const xmlChar *str2)
3131 {
3132 xmlChar *msg = NULL;
3133
3134 xmlSchemaFormatNodeForError(&msg, ACTXT_CAST ctxt, node);
3135 if (message == NULL) {
3136 /*
3137 * Use default messages.
3138 */
3139 if (type != NULL) {
3140 if (node->type == XML_ATTRIBUTE_NODE)
3141 msg = xmlStrcat(msg, BAD_CAST "'%s' is not a valid value of ");
3142 else
3143 msg = xmlStrcat(msg, BAD_CAST "The character content is not a "
3144 "valid value of ");
3145 if (! xmlSchemaIsGlobalItem(type))
3146 msg = xmlStrcat(msg, BAD_CAST "the local ");
3147 else
3148 msg = xmlStrcat(msg, BAD_CAST "the ");
3149
3150 if (WXS_IS_ATOMIC(type))
3151 msg = xmlStrcat(msg, BAD_CAST "atomic type");
3152 else if (WXS_IS_LIST(type))
3153 msg = xmlStrcat(msg, BAD_CAST "list type");
3154 else if (WXS_IS_UNION(type))
3155 msg = xmlStrcat(msg, BAD_CAST "union type");
3156
3157 if (xmlSchemaIsGlobalItem(type)) {
3158 xmlChar *str = NULL;
3159 msg = xmlStrcat(msg, BAD_CAST " '");
3160 if (type->builtInType != 0) {
3161 msg = xmlStrcat(msg, BAD_CAST "xs:");
3162 str = xmlStrdup(type->name);
3163 } else {
3164 const xmlChar *qName = xmlSchemaFormatQName(&str, type->targetNamespace, type->name);
3165 if (!str)
3166 str = xmlStrdup(qName);
3167 }
3168 msg = xmlStrcat(msg, xmlEscapeFormatString(&str));
3169 msg = xmlStrcat(msg, BAD_CAST "'.");
3170 FREE_AND_NULL(str);
3171 }
3172 } else {
3173 if (node->type == XML_ATTRIBUTE_NODE)
3174 msg = xmlStrcat(msg, BAD_CAST "The value '%s' is not valid.");
3175 else
3176 msg = xmlStrcat(msg, BAD_CAST "The character content is not "
3177 "valid.");
3178 }
3179 if (expected) {
3180 xmlChar *expectedEscaped = xmlCharStrdup(expected);
3181 msg = xmlStrcat(msg, BAD_CAST " Expected is '");
3182 msg = xmlStrcat(msg, xmlEscapeFormatString(&expectedEscaped));
3183 FREE_AND_NULL(expectedEscaped);
3184 msg = xmlStrcat(msg, BAD_CAST "'.\n");
3185 } else
3186 msg = xmlStrcat(msg, BAD_CAST "\n");
3187 if (node->type == XML_ATTRIBUTE_NODE)
3188 xmlSchemaPErr(ctxt, node, error, (const char *) msg, value, NULL);
3189 else
3190 xmlSchemaPErr(ctxt, node, error, (const char *) msg, NULL, NULL);
3191 } else {
3192 msg = xmlStrcat(msg, BAD_CAST message);
3193 msg = xmlStrcat(msg, BAD_CAST ".\n");
3194 xmlSchemaPErrExt(ctxt, node, error, NULL, NULL, NULL,
3195 (const char*) msg, str1, str2, NULL, NULL, NULL);
3196 }
3197 /* Cleanup. */
3198 FREE_AND_NULL(msg)
3199 }
3200
3201 /**
3202 * xmlSchemaPContentErr:
3203 * @ctxt: the schema parser context
3204 * @error: the error code
3205 * @onwerDes: the designation of the holder of the content
3206 * @ownerItem: the owner item of the holder of the content
3207 * @ownerElem: the node of the holder of the content
3208 * @child: the invalid child node
3209 * @message: the optional error message
3210 * @content: the optional string describing the correct content
3211 *
3212 * Reports an error concerning the content of a schema element.
3213 */
3214 static void
3215 xmlSchemaPContentErr(xmlSchemaParserCtxtPtr ctxt,
3216 xmlParserErrors error,
3217 xmlSchemaBasicItemPtr ownerItem,
3218 xmlNodePtr ownerElem,
3219 xmlNodePtr child,
3220 const char *message,
3221 const char *content)
3222 {
3223 xmlChar *des = NULL;
3224
3225 xmlSchemaFormatItemForReport(&des, NULL, ownerItem, ownerElem);
3226 if (message != NULL)
3227 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3228 "%s: %s.\n",
3229 BAD_CAST des, BAD_CAST message);
3230 else {
3231 if (content != NULL) {
3232 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3233 "%s: The content is not valid. Expected is %s.\n",
3234 BAD_CAST des, BAD_CAST content);
3235 } else {
3236 xmlSchemaPErr2(ctxt, ownerElem, child, error,
3237 "%s: The content is not valid.\n",
3238 BAD_CAST des, NULL);
3239 }
3240 }
3241 FREE_AND_NULL(des)
3242 }
3243
3244 /************************************************************************
3245 * *
3246 * Streamable error functions *
3247 * *
3248 ************************************************************************/
3249
3250
3251
3252
3253 /************************************************************************
3254 * *
3255 * Validation helper functions *
3256 * *
3257 ************************************************************************/
3258
3259
3260 /************************************************************************
3261 * *
3262 * Allocation functions *
3263 * *
3264 ************************************************************************/
3265
3266 /**
3267 * xmlSchemaNewSchemaForParserCtxt:
3268 * @ctxt: a schema validation context
3269 *
3270 * Allocate a new Schema structure.
3271 *
3272 * Returns the newly allocated structure or NULL in case or error
3273 */
3274 static xmlSchemaPtr
3275 xmlSchemaNewSchema(xmlSchemaParserCtxtPtr ctxt)
3276 {
3277 xmlSchemaPtr ret;
3278
3279 ret = (xmlSchemaPtr) xmlMalloc(sizeof(xmlSchema));
3280 if (ret == NULL) {
3281 xmlSchemaPErrMemory(ctxt, "allocating schema", NULL);
3282 return (NULL);
3283 }
3284 memset(ret, 0, sizeof(xmlSchema));
3285 ret->dict = ctxt->dict;
3286 xmlDictReference(ret->dict);
3287
3288 return (ret);
3289 }
3290
3291 /**
3292 * xmlSchemaNewFacet:
3293 *
3294 * Allocate a new Facet structure.
3295 *
3296 * Returns the newly allocated structure or NULL in case or error
3297 */
3298 xmlSchemaFacetPtr
3299 xmlSchemaNewFacet(void)
3300 {
3301 xmlSchemaFacetPtr ret;
3302
3303 ret = (xmlSchemaFacetPtr) xmlMalloc(sizeof(xmlSchemaFacet));
3304 if (ret == NULL) {
3305 return (NULL);
3306 }
3307 memset(ret, 0, sizeof(xmlSchemaFacet));
3308
3309 return (ret);
3310 }
3311
3312 /**
3313 * xmlSchemaNewAnnot:
3314 * @ctxt: a schema validation context
3315 * @node: a node
3316 *
3317 * Allocate a new annotation structure.
3318 *
3319 * Returns the newly allocated structure or NULL in case or error
3320 */
3321 static xmlSchemaAnnotPtr
3322 xmlSchemaNewAnnot(xmlSchemaParserCtxtPtr ctxt, xmlNodePtr node)
3323 {
3324 xmlSchemaAnnotPtr ret;
3325
3326 ret = (xmlSchemaAnnotPtr) xmlMalloc(sizeof(xmlSchemaAnnot));
3327 if (ret == NULL) {
3328 xmlSchemaPErrMemory(ctxt, "allocating annotation", node);
3329 return (NULL);
3330 }
3331 memset(ret, 0, sizeof(xmlSchemaAnnot));
3332 ret->content = node;
3333 return (ret);
3334 }
3335
3336 static xmlSchemaItemListPtr
3337 xmlSchemaItemListCreate(void)
3338 {
3339 xmlSchemaItemListPtr ret;
3340
3341 ret = xmlMalloc(sizeof(xmlSchemaItemList));
3342 if (ret == NULL) {
3343 xmlSchemaPErrMemory(NULL,
3344 "allocating an item list structure", NULL);
3345 return (NULL);
3346 }
3347 memset(ret, 0, sizeof(xmlSchemaItemList));
3348 return (ret);
3349 }
3350
3351 static void
3352 xmlSchemaItemListClear(xmlSchemaItemListPtr list)
3353 {
3354 if (list->items != NULL) {
3355 xmlFree(list->items);
3356 list->items = NULL;
3357 }
3358 list->nbItems = 0;
3359 list->sizeItems = 0;
3360 }
3361
3362 static int
3363 xmlSchemaItemListAdd(xmlSchemaItemListPtr list, void *item)
3364 {
3365 if (list->items == NULL) {
3366 list->items = (void **) xmlMalloc(
3367 20 * sizeof(void *));
3368 if (list->items == NULL) {
3369 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3370 return(-1);
3371 }
3372 list->sizeItems = 20;
3373 } else if (list->sizeItems <= list->nbItems) {
3374 list->sizeItems *= 2;
3375 list->items = (void **) xmlRealloc(list->items,
3376 list->sizeItems * sizeof(void *));
3377 if (list->items == NULL) {
3378 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3379 list->sizeItems = 0;
3380 return(-1);
3381 }
3382 }
3383 list->items[list->nbItems++] = item;
3384 return(0);
3385 }
3386
3387 static int
3388 xmlSchemaItemListAddSize(xmlSchemaItemListPtr list,
3389 int initialSize,
3390 void *item)
3391 {
3392 if (list->items == NULL) {
3393 if (initialSize <= 0)
3394 initialSize = 1;
3395 list->items = (void **) xmlMalloc(
3396 initialSize * sizeof(void *));
3397 if (list->items == NULL) {
3398 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3399 return(-1);
3400 }
3401 list->sizeItems = initialSize;
3402 } else if (list->sizeItems <= list->nbItems) {
3403 list->sizeItems *= 2;
3404 list->items = (void **) xmlRealloc(list->items,
3405 list->sizeItems * sizeof(void *));
3406 if (list->items == NULL) {
3407 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3408 list->sizeItems = 0;
3409 return(-1);
3410 }
3411 }
3412 list->items[list->nbItems++] = item;
3413 return(0);
3414 }
3415
3416 static int
3417 xmlSchemaItemListInsert(xmlSchemaItemListPtr list, void *item, int idx)
3418 {
3419 if (list->items == NULL) {
3420 list->items = (void **) xmlMalloc(
3421 20 * sizeof(void *));
3422 if (list->items == NULL) {
3423 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3424 return(-1);
3425 }
3426 list->sizeItems = 20;
3427 } else if (list->sizeItems <= list->nbItems) {
3428 list->sizeItems *= 2;
3429 list->items = (void **) xmlRealloc(list->items,
3430 list->sizeItems * sizeof(void *));
3431 if (list->items == NULL) {
3432 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3433 list->sizeItems = 0;
3434 return(-1);
3435 }
3436 }
3437 /*
3438 * Just append if the index is greater/equal than the item count.
3439 */
3440 if (idx >= list->nbItems) {
3441 list->items[list->nbItems++] = item;
3442 } else {
3443 int i;
3444 for (i = list->nbItems; i > idx; i--)
3445 list->items[i] = list->items[i-1];
3446 list->items[idx] = item;
3447 list->nbItems++;
3448 }
3449 return(0);
3450 }
3451
3452 #if 0 /* enable if ever needed */
3453 static int
3454 xmlSchemaItemListInsertSize(xmlSchemaItemListPtr list,
3455 int initialSize,
3456 void *item,
3457 int idx)
3458 {
3459 if (list->items == NULL) {
3460 if (initialSize <= 0)
3461 initialSize = 1;
3462 list->items = (void **) xmlMalloc(
3463 initialSize * sizeof(void *));
3464 if (list->items == NULL) {
3465 xmlSchemaPErrMemory(NULL, "allocating new item list", NULL);
3466 return(-1);
3467 }
3468 list->sizeItems = initialSize;
3469 } else if (list->sizeItems <= list->nbItems) {
3470 list->sizeItems *= 2;
3471 list->items = (void **) xmlRealloc(list->items,
3472 list->sizeItems * sizeof(void *));
3473 if (list->items == NULL) {
3474 xmlSchemaPErrMemory(NULL, "growing item list", NULL);
3475 list->sizeItems = 0;
3476 return(-1);
3477 }
3478 }
3479 /*
3480 * Just append if the index is greater/equal than the item count.
3481 */
3482 if (idx >= list->nbItems) {
3483 list->items[list->nbItems++] = item;
3484 } else {
3485 int i;
3486 for (i = list->nbItems; i > idx; i--)
3487 list->items[i] = list->items[i-1];
3488 list->items[idx] = item;
3489 list->nbItems++;
3490 }
3491 return(0);
3492 }
3493 #endif
3494
3495 static int
3496 xmlSchemaItemListRemove(xmlSchemaItemListPtr list, int idx)
3497 {
3498 int i;
3499 if ((list->items == NULL) || (idx >= list->nbItems)) {
3500 xmlSchemaPSimpleErr("Internal error: xmlSchemaItemListRemove, "
3501 "index error.\n");
3502 return(-1);
3503 }
3504
3505 if (list->nbItems == 1) {
3506 /* TODO: Really free the list? */
3507 xmlFree(list->items);
3508 list->items = NULL;
3509 list->nbItems = 0;
3510 list->sizeItems = 0;
3511 } else if (list->nbItems -1 == idx) {
3512 list->nbItems--;
3513 } else {
3514 for (i = idx; i < list->nbItems -1; i++)
3515 list->items[i] = list->items[i+1];
3516 list->nbItems--;
3517 }
3518 return(0);
3519 }
3520
3521 /**
3522 * xmlSchemaItemListFree:
3523 * @annot: a schema type structure
3524 *
3525 * Deallocate a annotation structure
3526 */
3527 static void
3528 xmlSchemaItemListFree(xmlSchemaItemListPtr list)
3529 {
3530 if (list == NULL)
3531 return;
3532 if (list->items != NULL)
3533 xmlFree(list->items);
3534 xmlFree(list);
3535 }
3536
3537 static void
3538 xmlSchemaBucketFree(xmlSchemaBucketPtr bucket)
3539 {
3540 if (bucket == NULL)
3541 return;
3542 if (bucket->globals != NULL) {
3543 xmlSchemaComponentListFree(bucket->globals);
3544 xmlSchemaItemListFree(bucket->globals);
3545 }
3546 if (bucket->locals != NULL) {
3547 xmlSchemaComponentListFree(bucket->locals);
3548 xmlSchemaItemListFree(bucket->locals);
3549 }
3550 if (bucket->relations != NULL) {
3551 xmlSchemaSchemaRelationPtr prev, cur = bucket->relations;
3552 do {
3553 prev = cur;
3554 cur = cur->next;
3555 xmlFree(prev);
3556 } while (cur != NULL);
3557 }
3558 if ((! bucket->preserveDoc) && (bucket->doc != NULL)) {
3559 xmlFreeDoc(bucket->doc);
3560 }
3561 if (bucket->type == XML_SCHEMA_SCHEMA_IMPORT) {
3562 if (WXS_IMPBUCKET(bucket)->schema != NULL)
3563 xmlSchemaFree(WXS_IMPBUCKET(bucket)->schema);
3564 }
3565 xmlFree(bucket);
3566 }
3567
3568 static xmlSchemaBucketPtr
3569 xmlSchemaBucketCreate(xmlSchemaParserCtxtPtr pctxt,
3570 int type, const xmlChar *targetNamespace)
3571 {
3572 xmlSchemaBucketPtr ret;
3573 int size;
3574 xmlSchemaPtr mainSchema;
3575
3576 if (WXS_CONSTRUCTOR(pctxt)->mainSchema == NULL) {
3577 PERROR_INT("xmlSchemaBucketCreate",
3578 "no main schema on constructor");
3579 return(NULL);
3580 }
3581 mainSchema = WXS_CONSTRUCTOR(pctxt)->mainSchema;
3582 /* Create the schema bucket. */
3583 if (WXS_IS_BUCKET_INCREDEF(type))
3584 size = sizeof(xmlSchemaInclude);
3585 else
3586 size = sizeof(xmlSchemaImport);
3587 ret = (xmlSchemaBucketPtr) xmlMalloc(size);
3588 if (ret == NULL) {
3589 xmlSchemaPErrMemory(NULL, "allocating schema bucket", NULL);
3590 return(NULL);
3591 }
3592 memset(ret, 0, size);
3593 ret->targetNamespace = targetNamespace;
3594 ret->type = type;
3595 ret->globals = xmlSchemaItemListCreate();
3596 if (ret->globals == NULL) {
3597 xmlFree(ret);
3598 return(NULL);
3599 }
3600 ret->locals = xmlSchemaItemListCreate();
3601 if (ret->locals == NULL) {
3602 xmlFree(ret);
3603 return(NULL);
3604 }
3605 /*
3606 * The following will assure that only the first bucket is marked as
3607 * XML_SCHEMA_SCHEMA_MAIN and it points to the *main* schema.
3608 * For each following import buckets an xmlSchema will be created.
3609 * An xmlSchema will be created for every distinct targetNamespace.
3610 * We assign the targetNamespace to the schemata here.
3611 */
3612 if (! WXS_HAS_BUCKETS(pctxt)) {
3613 if (WXS_IS_BUCKET_INCREDEF(type)) {
3614 PERROR_INT("xmlSchemaBucketCreate",
3615 "first bucket but it's an include or redefine");
3616 xmlSchemaBucketFree(ret);
3617 return(NULL);
3618 }
3619 /* Force the type to be XML_SCHEMA_SCHEMA_MAIN. */
3620 ret->type = XML_SCHEMA_SCHEMA_MAIN;
3621 /* Point to the *main* schema. */
3622 WXS_CONSTRUCTOR(pctxt)->mainBucket = ret;
3623 WXS_IMPBUCKET(ret)->schema = mainSchema;
3624 /*
3625 * Ensure that the main schema gets a targetNamespace.
3626 */
3627 mainSchema->targetNamespace = targetNamespace;
3628 } else {
3629 if (type == XML_SCHEMA_SCHEMA_MAIN) {
3630 PERROR_INT("xmlSchemaBucketCreate",
3631 "main bucket but it's not the first one");
3632 xmlSchemaBucketFree(ret);
3633 return(NULL);
3634 } else if (type == XML_SCHEMA_SCHEMA_IMPORT) {
3635 /*
3636 * Create a schema for imports and assign the
3637 * targetNamespace.
3638 */
3639 WXS_IMPBUCKET(ret)->schema = xmlSchemaNewSchema(pctxt);
3640 if (WXS_IMPBUCKET(ret)->schema == NULL) {
3641 xmlSchemaBucketFree(ret);
3642 return(NULL);
3643 }
3644 WXS_IMPBUCKET(ret)->schema->targetNamespace = targetNamespace;
3645 }
3646 }
3647 if (WXS_IS_BUCKET_IMPMAIN(type)) {
3648 int res;
3649 /*
3650 * Imports go into the "schemasImports" slot of the main *schema*.
3651 * Note that we create an import entry for the main schema as well; i.e.,
3652 * even if there's only one schema, we'll get an import.
3653 */
3654 if (mainSchema->schemasImports == NULL) {
3655 mainSchema->schemasImports = xmlHashCreateDict(5,
3656 WXS_CONSTRUCTOR(pctxt)->dict);
3657 if (mainSchema->schemasImports == NULL) {
3658 xmlSchemaBucketFree(ret);
3659 return(NULL);
3660 }
3661 }
3662 if (targetNamespace == NULL)
3663 res = xmlHashAddEntry(mainSchema->schemasImports,
3664 XML_SCHEMAS_NO_NAMESPACE, ret);
3665 else
3666 res = xmlHashAddEntry(mainSchema->schemasImports,
3667 targetNamespace, ret);
3668 if (res != 0) {
3669 PERROR_INT("xmlSchemaBucketCreate",
3670 "failed to add the schema bucket to the hash");
3671 xmlSchemaBucketFree(ret);
3672 return(NULL);
3673 }
3674 } else {
3675 /* Set the @ownerImport of an include bucket. */
3676 if (WXS_IS_BUCKET_IMPMAIN(WXS_CONSTRUCTOR(pctxt)->bucket->type))
3677 WXS_INCBUCKET(ret)->ownerImport =
3678 WXS_IMPBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket);
3679 else
3680 WXS_INCBUCKET(ret)->ownerImport =
3681 WXS_INCBUCKET(WXS_CONSTRUCTOR(pctxt)->bucket)->ownerImport;
3682
3683 /* Includes got into the "includes" slot of the *main* schema. */
3684 if (mainSchema->includes == NULL) {
3685 mainSchema->includes = xmlSchemaItemListCreate();
3686 if (mainSchema->includes == NULL) {
3687 xmlSchemaBucketFree(ret);
3688 return(NULL);
3689 }
3690 }
3691 xmlSchemaItemListAdd(mainSchema->includes, ret);
3692 }
3693 /*
3694 * Add to list of all buckets; this is used for lookup
3695 * during schema construction time only.
3696 */
3697 if (xmlSchemaItemListAdd(WXS_CONSTRUCTOR(pctxt)->buckets, ret) == -1)
3698 return(NULL);
3699 return(ret);
3700 }
3701
3702 static int
3703 xmlSchemaAddItemSize(xmlSchemaItemListPtr *list, int initialSize, void *item)
3704 {
3705 if (*list == NULL) {
3706 *list = xmlSchemaItemListCreate();
3707 if (*list == NULL)
3708 return(-1);
3709 }