📄 xmlschemas.c
字号:
/* * schemas.c : implementation of the XML Schema handling and * schema validity checking * * See Copyright for the status of this software. * * Daniel Veillard <veillard@redhat.com> *//* * TODO: * - when types are redefined in includes, check that all * types in the redef list are equal * -> need a type equality operation. * - if we don't intend to use the schema for schemas, we * need to validate all schema attributes (ref, type, name) * against their types. * - Eliminate item creation for: ?? * * URGENT TODO: * - For xsi-driven schema acquisition, augment the IDCs after every * acquisition episode (xmlSchemaAugmentIDC). * * NOTES: * - Elimated item creation for: <restriction>, <extension>, * <simpleContent>, <complexContent>, <list>, <union> * * PROBLEMS: * - http://lists.w3.org/Archives/Public/www-xml-schema-comments/2005JulSep/0337.html * IDC XPath expression and chameleon includes: the targetNamespace is changed, so * XPath will have trouble to resolve to this namespace, since not known. * * * CONSTRAINTS: * * Schema Component Constraint: * All Group Limited (cos-all-limited) * Status: complete * (1.2) * In xmlSchemaGroupDefReferenceTermFixup() and * (2) * In xmlSchemaParseModelGroup() * TODO: Actually this should go to component-level checks, * but is done here due to performance. Move it to an other layer * is schema construction via an API is implemented. */#define IN_LIBXML#include "libxml.h"#ifdef LIBXML_SCHEMAS_ENABLED#include <string.h>#include <libxml/xmlmemory.h>#include <libxml/parser.h>#include <libxml/parserInternals.h>#include <libxml/hash.h>#include <libxml/uri.h>#include <libxml/xmlschemas.h>#include <libxml/schemasInternals.h>#include <libxml/xmlschemastypes.h>#include <libxml/xmlautomata.h>#include <libxml/xmlregexp.h>#include <libxml/dict.h>#include <libxml/encoding.h>#include <libxml/xmlIO.h>#ifdef LIBXML_PATTERN_ENABLED#include <libxml/pattern.h>#endif#ifdef LIBXML_READER_ENABLED#include <libxml/xmlreader.h>#endif/* #define DEBUG 1 *//* #define DEBUG_CONTENT 1 *//* #define DEBUG_TYPE 1 *//* #define DEBUG_CONTENT_REGEXP 1 *//* #define DEBUG_AUTOMATA 1 *//* #define DEBUG_IDC *//* #define DEBUG_IDC_NODE_TABLE *//* #define WXS_ELEM_DECL_CONS_ENABLED */#ifdef DEBUG_IDC #ifndef DEBUG_IDC_NODE_TABLE #define DEBUG_IDC_NODE_TABLE #endif#endif /* #define ENABLE_PARTICLE_RESTRICTION 1 */#define ENABLE_REDEFINE/* #define ENABLE_NAMED_LOCALS *//* #define ENABLE_IDC_NODE_TABLES_TEST */#define DUMP_CONTENT_MODEL#ifdef LIBXML_READER_ENABLED/* #define XML_SCHEMA_READER_ENABLED */#endif#define UNBOUNDED (1 << 30)#define TODO \ xmlGenericError(xmlGenericErrorContext, \ "Unimplemented block at %s:%d\n", \ __FILE__, __LINE__);#define XML_SCHEMAS_NO_NAMESPACE (const xmlChar *) "##"/* * The XML Schemas namespaces */static const xmlChar *xmlSchemaNs = (const xmlChar *) "http://www.w3.org/2001/XMLSchema";static const xmlChar *xmlSchemaInstanceNs = (const xmlChar *) "http://www.w3.org/2001/XMLSchema-instance";static const xmlChar *xmlNamespaceNs = (const xmlChar *) "http://www.w3.org/2000/xmlns/";/** Come casting macros.*/#define ACTXT_CAST (xmlSchemaAbstractCtxtPtr)#define PCTXT_CAST (xmlSchemaParserCtxtPtr)#define VCTXT_CAST (xmlSchemaValidCtxtPtr)#define WXS_BASIC_CAST (xmlSchemaBasicItemPtr)#define WXS_TREE_CAST (xmlSchemaTreeItemPtr)#define WXS_PTC_CAST (xmlSchemaParticlePtr)#define WXS_TYPE_CAST (xmlSchemaTypePtr)#define WXS_ELEM_CAST (xmlSchemaElementPtr)#define WXS_ATTR_GROUP_CAST (xmlSchemaAttributeGroupPtr)#define WXS_ATTR_CAST (xmlSchemaAttributePtr)#define WXS_ATTR_USE_CAST (xmlSchemaAttributeUsePtr)#define WXS_ATTR_PROHIB_CAST (xmlSchemaAttributeUseProhibPtr)#define WXS_MODEL_GROUPDEF_CAST (xmlSchemaModelGroupDefPtr)#define WXS_MODEL_GROUP_CAST (xmlSchemaModelGroupPtr)#define WXS_IDC_CAST (xmlSchemaIDCPtr)#define WXS_QNAME_CAST (xmlSchemaQNameRefPtr)#define WXS_LIST_CAST (xmlSchemaItemListPtr)/** Macros to query common properties of components.*/#define WXS_ITEM_NODE(i) xmlSchemaGetComponentNode(WXS_BASIC_CAST (i))#define WXS_ITEM_TYPE_NAME(i) xmlSchemaGetComponentTypeStr(WXS_BASIC_CAST (i))/** Macros for element declarations.*/#define WXS_ELEM_TYPEDEF(e) (e)->subtypes#define WXS_SUBST_HEAD(item) (item)->refDecl/** Macros for attribute declarations.*/#define WXS_ATTR_TYPEDEF(a) (a)->subtypes/** Macros for attribute uses.*/#define WXS_ATTRUSE_DECL(au) WXS_ATTR_CAST (WXS_ATTR_USE_CAST (au))->attrDecl#define WXS_ATTRUSE_TYPEDEF(au) WXS_ATTR_TYPEDEF(WXS_ATTRUSE_DECL( WXS_ATTR_USE_CAST au))#define WXS_ATTRUSE_DECL_NAME(au) (WXS_ATTRUSE_DECL(au))->name#define WXS_ATTRUSE_DECL_TNS(au) (WXS_ATTRUSE_DECL(au))->targetNamespace/** Macros for attribute groups.*/#define WXS_ATTR_GROUP_HAS_REFS(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_HAS_REFS)#define WXS_ATTR_GROUP_EXPANDED(ag) ((WXS_ATTR_GROUP_CAST (ag))->flags & XML_SCHEMAS_ATTRGROUP_WILDCARD_BUILDED)/** Macros for particles.*/#define WXS_PARTICLE(p) WXS_PTC_CAST (p)#define WXS_PARTICLE_TERM(p) (WXS_PARTICLE(p))->children#define WXS_PARTICLE_TERM_AS_ELEM(p) (WXS_ELEM_CAST WXS_PARTICLE_TERM(p))#define WXS_PARTICLE_MODEL(p) WXS_MODEL_GROUP_CAST WXS_PARTICLE(p)->children/** Macros for model groups definitions.*/#define WXS_MODELGROUPDEF_MODEL(mgd) (WXS_MODEL_GROUP_CAST (mgd))->children/** Macros for model groups.*/#define WXS_IS_MODEL_GROUP(i) \ (((i)->type == XML_SCHEMA_TYPE_SEQUENCE) || \ ((i)->type == XML_SCHEMA_TYPE_CHOICE) || \ ((i)->type == XML_SCHEMA_TYPE_ALL))#define WXS_MODELGROUP_PARTICLE(mg) WXS_PTC_CAST (mg)->children/** Macros for schema buckets.*/#define WXS_IS_BUCKET_INCREDEF(t) (((t) == XML_SCHEMA_SCHEMA_INCLUDE) || \ ((t) == XML_SCHEMA_SCHEMA_REDEFINE))#define WXS_IS_BUCKET_IMPMAIN(t) (((t) == XML_SCHEMA_SCHEMA_MAIN) || \ ((t) == XML_SCHEMA_SCHEMA_IMPORT))#define WXS_IMPBUCKET(b) ((xmlSchemaImportPtr) (b))#define WXS_INCBUCKET(b) ((xmlSchemaIncludePtr) (b))/** Macros for complex/simple types.*/#define WXS_IS_ANYTYPE(i) \ (( (i)->type == XML_SCHEMA_TYPE_BASIC) && \ ( (WXS_TYPE_CAST (i))->builtInType == XML_SCHEMAS_ANYTYPE))#define WXS_IS_COMPLEX(i) \ (((i)->type == XML_SCHEMA_TYPE_COMPLEX) || \ ((i)->builtInType == XML_SCHEMAS_ANYTYPE))#define WXS_IS_SIMPLE(item) \ ((item->type == XML_SCHEMA_TYPE_SIMPLE) || \ ((item->type == XML_SCHEMA_TYPE_BASIC) && \ (item->builtInType != XML_SCHEMAS_ANYTYPE)))#define WXS_IS_ANY_SIMPLE_TYPE(i) \ (((i)->type == XML_SCHEMA_TYPE_BASIC) && \ ((i)->builtInType == XML_SCHEMAS_ANYSIMPLETYPE))#define WXS_IS_RESTRICTION(t) \ ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_RESTRICTION)#define WXS_IS_EXTENSION(t) \ ((t)->flags & XML_SCHEMAS_TYPE_DERIVATION_METHOD_EXTENSION)#define WXS_IS_TYPE_NOT_FIXED(i) \ (((i)->type != XML_SCHEMA_TYPE_BASIC) && \ (((i)->flags & XML_SCHEMAS_TYPE_INTERNAL_RESOLVED) == 0))#define WXS_IS_TYPE_NOT_FIXED_1(item) \ (((item)->type != XML_SCHEMA_TYPE_BASIC) && \ (((item)->flags & XML_SCHEMAS_TYPE_FIXUP_1) == 0))#define WXS_TYPE_IS_GLOBAL(t) ((t)->flags & XML_SCHEMAS_TYPE_GLOBAL)#define WXS_TYPE_IS_LOCAL(t) (((t)->flags & XML_SCHEMAS_TYPE_GLOBAL) == 0)/** Macros for exclusively for complex types.*/#define WXS_HAS_COMPLEX_CONTENT(item) \ ((item->contentType == XML_SCHEMA_CONTENT_MIXED) || \ (item->contentType == XML_SCHEMA_CONTENT_EMPTY) || \ (item->contentType == XML_SCHEMA_CONTENT_ELEMENTS))#define WXS_HAS_SIMPLE_CONTENT(item) \ ((item->contentType == XML_SCHEMA_CONTENT_SIMPLE) || \ (item->contentType == XML_SCHEMA_CONTENT_BASIC))#define WXS_HAS_MIXED_CONTENT(item) \ (item->contentType == XML_SCHEMA_CONTENT_MIXED)#define WXS_EMPTIABLE(t) \ (xmlSchemaIsParticleEmptiable(WXS_PTC_CAST (t)->subtypes))#define WXS_TYPE_CONTENTTYPE(t) (t)->subtypes#define WXS_TYPE_PARTICLE(t) WXS_PTC_CAST (t)->subtypes#define WXS_TYPE_PARTICLE_TERM(t) WXS_PARTICLE_TERM(WXS_TYPE_PARTICLE(t))/** Macros for exclusively for simple types.*/#define WXS_LIST_ITEMTYPE(t) (t)->subtypes#define WXS_IS_ATOMIC(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_ATOMIC)#define WXS_IS_LIST(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_LIST)#define WXS_IS_UNION(t) (t->flags & XML_SCHEMAS_TYPE_VARIETY_UNION)/** Misc parser context macros.*/#define WXS_CONSTRUCTOR(ctx) (ctx)->constructor#define WXS_HAS_BUCKETS(ctx) \( (WXS_CONSTRUCTOR((ctx))->buckets != NULL) && \(WXS_CONSTRUCTOR((ctx))->buckets->nbItems > 0) )#define WXS_SUBST_GROUPS(ctx) WXS_CONSTRUCTOR((ctx))->substGroups#define WXS_BUCKET(ctx) WXS_CONSTRUCTOR((ctx))->bucket#define WXS_SCHEMA(ctx) (ctx)->schema#define WXS_ADD_LOCAL(ctx, item) \ xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->locals), 10, item)#define WXS_ADD_GLOBAL(ctx, item) \ xmlSchemaAddItemSize(&(WXS_BUCKET(ctx)->globals), 5, item)#define WXS_ADD_PENDING(ctx, item) \ xmlSchemaAddItemSize(&((ctx)->constructor->pending), 10, item)/** xmlSchemaItemList macros.*/#define WXS_ILIST_IS_EMPTY(l) ((l == NULL) || ((l)->nbItems == 0))/** Misc macros.*/#define IS_SCHEMA(node, type) \ ((node != NULL) && (node->ns != NULL) && \ (xmlStrEqual(node->name, (const xmlChar *) type)) && \ (xmlStrEqual(node->ns->href, xmlSchemaNs)))#define FREE_AND_NULL(str) if ((str) != NULL) { xmlFree((xmlChar *) (str)); str = NULL; }/** Since we put the default/fixed values into the dict, we can* use pointer comparison for those values.* REMOVED: (xmlStrEqual((v1), (v2)))*/#define WXS_ARE_DEFAULT_STR_EQUAL(v1, v2) ((v1) == (v2))#define INODE_NILLED(item) (item->flags & XML_SCHEMA_ELEM_INFO_NILLED)#define CAN_PARSE_SCHEMA(b) (((b)->doc != NULL) && ((b)->parsed == 0))#define HFAILURE if (res == -1) goto exit_failure;#define HERROR if (res != 0) goto exit_error;#define HSTOP(ctx) if ((ctx)->stop) goto exit;/** Some flags used for various schema constraints.*/#define SUBSET_RESTRICTION 1<<0#define SUBSET_EXTENSION 1<<1#define SUBSET_SUBSTITUTION 1<<2#define SUBSET_LIST 1<<3#define SUBSET_UNION 1<<4typedef struct _xmlSchemaNodeInfo xmlSchemaNodeInfo;typedef xmlSchemaNodeInfo *xmlSchemaNodeInfoPtr;typedef struct _xmlSchemaItemList xmlSchemaItemList;typedef xmlSchemaItemList *xmlSchemaItemListPtr;struct _xmlSchemaItemList { void **items; /* used for dynamic addition of schemata */ int nbItems; /* used for dynamic addition of schemata */ int sizeItems; /* used for dynamic addition of schemata */};#define XML_SCHEMA_CTXT_PARSER 1#define XML_SCHEMA_CTXT_VALIDATOR 2typedef struct _xmlSchemaAbstractCtxt xmlSchemaAbstractCtxt;typedef xmlSchemaAbstractCtxt *xmlSchemaAbstractCtxtPtr;struct _xmlSchemaAbstractCtxt { int type; /* E.g. XML_SCHEMA_CTXT_VALIDATOR */};typedef struct _xmlSchemaBucket xmlSchemaBucket;typedef xmlSchemaBucket *xmlSchemaBucketPtr;#define XML_SCHEMA_SCHEMA_MAIN 0#define XML_SCHEMA_SCHEMA_IMPORT 1#define XML_SCHEMA_SCHEMA_INCLUDE 2#define XML_SCHEMA_SCHEMA_REDEFINE 3/** * xmlSchemaSchemaRelation: * * Used to create a graph of schema relationships. */typedef struct _xmlSchemaSchemaRelation xmlSchemaSchemaRelation;typedef xmlSchemaSchemaRelation *xmlSchemaSchemaRelationPtr;struct _xmlSchemaSchemaRelation { xmlSchemaSchemaRelationPtr next; int type; /* E.g. XML_SCHEMA_SCHEMA_IMPORT */ const xmlChar *importNamespace; xmlSchemaBucketPtr bucket;};#define XML_SCHEMA_BUCKET_MARKED 1<<0#define XML_SCHEMA_BUCKET_COMPS_ADDED 1<<1struct _xmlSchemaBucket { int type; int flags; const xmlChar *schemaLocation; const xmlChar *origTargetNamespace; const xmlChar *targetNamespace; xmlDocPtr doc; xmlSchemaSchemaRelationPtr relations; int located; int parsed; int imported; int preserveDoc; xmlSchemaItemListPtr globals; /* Global components. */ xmlSchemaItemListPtr locals; /* Local components. */};/** * xmlSchemaImport: * (extends xmlSchemaBucket) * * Reflects a schema. Holds some information * about the schema and its toplevel components. Duplicate * toplevel components are not checked at this level. */typedef struct _xmlSchemaImport xmlSchemaImport;typedef xmlSchemaImport *xmlSchemaImportPtr;struct _xmlSchemaImport { int type; /* Main OR import OR include. */ int flags; const xmlChar *schemaLocation; /* The URI of the schema document. */ /* For chameleon includes, @origTargetNamespace will be NULL */ const xmlChar *origTargetNamespace; /* * For chameleon includes, @targetNamespace will be the * targetNamespace of the including schema. */ const xmlChar *targetNamespace; xmlDocPtr doc; /* The schema node-tree. */ /* @relations will hold any included/imported/redefined schemas. */ xmlSchemaSchemaRelationPtr relations; int located; int parsed; int imported; int preserveDoc; xmlSchemaItemListPtr globals; xmlSchemaItemListPtr locals; /* The imported schema. */ xmlSchemaPtr schema;};/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -