📄 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: ?? * * NOTES: * - Elimated item creation for: <restriction>, <extension>, * <simpleContent>, <complexContent>, <list>, <union> * */#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>#ifdef LIBXML_PATTERN_ENABLED#include <libxml/pattern.h>#endif/* #define DEBUG 1 *//* #define DEBUG_CONTENT 1 *//* #define DEBUG_TYPE 1 *//* #define DEBUG_CONTENT_REGEXP 1 *//* #define DEBUG_AUTOMATA 1 *//* #define DEBUG_ATTR_VALIDATION 1 *//* #define DEBUG_UNION_VALIDATION 1 *//* #define DEBUG_IDC 1 *//* #define DEBUG_INCLUDES 1 */#define DUMP_CONTENT_MODEL#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 *xmlSchemaElemDesElemDecl = (const xmlChar *) "Element decl.";static const xmlChar *xmlSchemaElemDesElemRef = (const xmlChar *) "Element ref.";static const xmlChar *xmlSchemaElemDesAttrDecl = (const xmlChar *) "Attribute decl.";static const xmlChar *xmlSchemaElemDesAttrRef = (const xmlChar *) "Attribute ref.";static const xmlChar *xmlSchemaElemDesST = (const xmlChar *) "simple type";static const xmlChar *xmlSchemaElemDesCT = (const xmlChar *) "complex type";static const xmlChar *xmlSchemaElemModelGrDef = (const xmlChar *) "Model group";#if 0static const xmlChar *xmlSchemaElemModelGrRef = (const xmlChar *) "Model group ref.";#endif#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(str); \ str = NULL; \ }#define IS_ANYTYPE(item) \ ((item->type == XML_SCHEMA_TYPE_BASIC) && \ (item->builtInType == XML_SCHEMAS_ANYTYPE))#define IS_COMPLEX_TYPE(item) \ ((item->type == XML_SCHEMA_TYPE_COMPLEX) || \ (item->builtInType == XML_SCHEMAS_ANYTYPE))#define IS_SIMPLE_TYPE(item) \ ((item->type == XML_SCHEMA_TYPE_SIMPLE) || \ ((item->type == XML_SCHEMA_TYPE_BASIC) && \ (item->builtInType != XML_SCHEMAS_ANYTYPE))) #define IS_ANY_SIMPLE_TYPE(item) \ ((item->type == XML_SCHEMA_TYPE_BASIC) && \ (item->builtInType == XML_SCHEMAS_ANYSIMPLETYPE)) #define IS_NOT_TYPEFIXED(item) \ ((item->type != XML_SCHEMA_TYPE_BASIC) && \ ((item->flags & XML_SCHEMAS_TYPE_INTERNAL_RESOLVED) == 0))#define HAS_COMPLEX_CONTENT(item) \ ((item->contentType == XML_SCHEMA_CONTENT_MIXED) || \ (item->contentType == XML_SCHEMA_CONTENT_EMPTY) || \ (item->contentType == XML_SCHEMA_CONTENT_ELEMENTS))#define GET_NODE(item) xmlSchemaGetComponentNode((xmlSchemaBasicItemPtr) item)#define IS_MODEL_GROUP(item) \ ((item->type == XML_SCHEMA_TYPE_SEQUENCE) || \ (item->type == XML_SCHEMA_TYPE_CHOICE) || \ (item->type == XML_SCHEMA_TYPE_ALL))#if 0#define WXS_GET_NEXT(item) xmlSchemaGetNextComponent((xmlSchemaBasicItemPtr) item)#endif/*#define XML_SCHEMAS_VAL_WTSP_PRESERVE 0#define XML_SCHEMAS_VAL_WTSP_REPLACE 1#define XML_SCHEMAS_VAL_WTSP_COLLAPSE 2*/#define XML_SCHEMAS_PARSE_ERROR 1#define SCHEMAS_PARSE_OPTIONS XML_PARSE_NOENT/** XML_SCHEMA_VAL_LOCATE_BY_NSNAME = 1<<2* locate schemata to be imported* using the namespace name; otherwise* the location URI will be used *//** xmlSchemaParserOption:** This is the set of XML Schema parser options.*typedef enum { XML_SCHEMA_PAR_LOCATE_BY_NSNAME = 1<<0 * locate schemata to be imported * using the namespace name; otherwise * the location URI will be used *} xmlSchemaParserOption;*/typedef struct _xmlSchemaItemList xmlSchemaAssemble;typedef xmlSchemaAssemble *xmlSchemaAssemblePtr;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 */};struct _xmlSchemaParserCtxt { void *userData; /* user specific data block */ xmlSchemaValidityErrorFunc error; /* the callback in case of errors */ xmlSchemaValidityWarningFunc warning; /* the callback in case of warning */ xmlSchemaValidError err; int nberrors; xmlStructuredErrorFunc serror; xmlSchemaPtr topschema; /* The main schema */ xmlHashTablePtr namespaces; /* Hash table of namespaces to schemas */ xmlSchemaPtr schema; /* The schema in use */ const xmlChar *container; /* the current element, group, ... */ int counter; const xmlChar *URL; xmlDocPtr doc; int preserve; /* Whether the doc should be freed */ const char *buffer; int size; /* * Used to build complex element content models */ xmlAutomataPtr am; xmlAutomataStatePtr start; xmlAutomataStatePtr end; xmlAutomataStatePtr state; xmlDictPtr dict; /* dictionnary for interned string names */ int includes; /* the inclusion level, 0 for root or imports */ xmlSchemaTypePtr ctxtType; /* The current context simple/complex type */ xmlSchemaTypePtr parentItem; /* The current parent schema item */ xmlSchemaAssemblePtr assemble; int options; xmlSchemaValidCtxtPtr vctxt; const xmlChar **localImports; /* list of locally imported namespaces */ int sizeLocalImports; int nbLocalImports;};#define XML_SCHEMAS_ATTR_UNKNOWN 1#define XML_SCHEMAS_ATTR_CHECKED 2#define XML_SCHEMAS_ATTR_PROHIBITED 3#define XML_SCHEMAS_ATTR_MISSING 4#define XML_SCHEMAS_ATTR_INVALID_VALUE 5#define XML_SCHEMAS_ATTR_TYPE_NOT_RESOLVED 6#define XML_SCHEMAS_ATTR_INVALID_FIXED_VALUE 7#define XML_SCHEMAS_ATTR_DEFAULT 8#define XML_SCHEMAS_ATTR_VALIDATE_VALUE 9#define XML_SCHEMAS_ATTR_WILD_NO_DECL 10typedef struct _xmlSchemaAttrState xmlSchemaAttrState;typedef xmlSchemaAttrState *xmlSchemaAttrStatePtr;struct _xmlSchemaAttrState { xmlSchemaAttrStatePtr next; xmlAttrPtr attr; int state; xmlSchemaAttributePtr decl; const xmlChar *value;};/** * xmlSchemaBasicItem: * * The abstract base type for schema components. */typedef struct _xmlSchemaBasicItem xmlSchemaBasicItem;typedef xmlSchemaBasicItem *xmlSchemaBasicItemPtr;struct _xmlSchemaBasicItem { xmlSchemaTypeType type;};/** * xmlSchemaAnnotItem: * * The abstract base type for annotated schema components. * (Extends xmlSchemaBasicItem) */typedef struct _xmlSchemaAnnotItem xmlSchemaAnnotItem;typedef xmlSchemaAnnotItem *xmlSchemaAnnotItemPtr;struct _xmlSchemaAnnotItem { xmlSchemaTypeType type; xmlSchemaAnnotPtr annot;};/** * xmlSchemaTreeItem: * * The abstract base type for tree-like structured schema components. * (Extends xmlSchemaAnnotItem) */typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem;typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr;struct _xmlSchemaTreeItem { xmlSchemaTypeType type; xmlSchemaAnnotPtr annot; xmlSchemaTreeItemPtr next; xmlSchemaTreeItemPtr children;}; /** * xmlSchemaQNameRef: * * A component reference item (not a schema component) * (Extends xmlSchemaBasicItem) */typedef struct _xmlSchemaQNameRef xmlSchemaQNameRef;typedef xmlSchemaQNameRef *xmlSchemaQNameRefPtr;struct _xmlSchemaQNameRef { xmlSchemaTypeType type; xmlSchemaBasicItemPtr item; xmlSchemaTypeType itemType; const xmlChar *name; const xmlChar *targetNamespace;};/** * xmlSchemaParticle: * * A particle component. * (Extends xmlSchemaTreeItem) */typedef struct _xmlSchemaParticle xmlSchemaParticle;typedef xmlSchemaParticle *xmlSchemaParticlePtr;struct _xmlSchemaParticle { xmlSchemaTypeType type; xmlSchemaAnnotPtr annot; xmlSchemaTreeItemPtr next; /* next particle (OR "element decl" OR "wildcard") */ xmlSchemaTreeItemPtr children; /* the "term" ("model group" OR "group definition") */ int minOccurs; int maxOccurs; xmlNodePtr node;};/** * xmlSchemaModelGroup: * * A model group component. * (Extends xmlSchemaTreeItem) */typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup;typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr;struct _xmlSchemaModelGroup { xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_SEQUENCE, XML_SCHEMA_TYPE_CHOICE, XML_SCHEMA_TYPE_ALL */ xmlSchemaAnnotPtr annot; xmlSchemaTreeItemPtr next; /* not used */ xmlSchemaTreeItemPtr children; /* first particle (OR "element decl" OR "wildcard") */ xmlNodePtr node;};/** * xmlSchemaModelGroupDef: * * A model group definition component. * (Extends xmlSchemaTreeItem) */typedef struct _xmlSchemaModelGroupDef xmlSchemaModelGroupDef;typedef xmlSchemaModelGroupDef *xmlSchemaModelGroupDefPtr;struct _xmlSchemaModelGroupDef { xmlSchemaTypeType type; /* XML_SCHEMA_TYPE_GROUP */ xmlSchemaAnnotPtr annot; xmlSchemaTreeItemPtr next; /* not used */ xmlSchemaTreeItemPtr children; /* the "model group" */ const xmlChar *name; const xmlChar *targetNamespace; xmlNodePtr node;};typedef struct _xmlSchemaIDC xmlSchemaIDC;typedef xmlSchemaIDC *xmlSchemaIDCPtr;/** * xmlSchemaIDCSelect: * * The identity-constraint "field" and "selector" item, holding the * XPath expression. */typedef struct _xmlSchemaIDCSelect xmlSchemaIDCSelect;typedef xmlSchemaIDCSelect *xmlSchemaIDCSelectPtr;struct _xmlSchemaIDCSelect { xmlSchemaIDCSelectPtr next; xmlSchemaIDCPtr idc; int index; /* an index position if significant for IDC key-sequences */ const xmlChar *xpath; /* the XPath expression */ void *xpathComp; /* the compiled XPath expression */};/** * xmlSchemaIDC: * * The identity-constraint definition component. * (Extends xmlSchemaAnnotItem) */struct _xmlSchemaIDC { xmlSchemaTypeType type; xmlSchemaAnnotPtr annot; xmlSchemaIDCPtr next; xmlNodePtr node; const xmlChar *name; const xmlChar *targetNamespace; xmlSchemaIDCSelectPtr selector; xmlSchemaIDCSelectPtr fields; int nbFields; xmlSchemaQNameRefPtr ref;};/** * xmlSchemaIDCAug: * * The augmented IDC information used for validation. */typedef struct _xmlSchemaIDCAug xmlSchemaIDCAug;typedef xmlSchemaIDCAug *xmlSchemaIDCAugPtr;struct _xmlSchemaIDCAug { xmlSchemaIDCAugPtr next; /* next in a list */ xmlSchemaIDCPtr def; /* the IDC definition */ int bubbleDepth; /* the lowest tree level to which IDC tables need to be bubbled upwards */};/** * xmlSchemaPSVIIDCKeySequence: * * The key sequence of a node table item. */typedef struct _xmlSchemaPSVIIDCKey xmlSchemaPSVIIDCKey;typedef xmlSchemaPSVIIDCKey *xmlSchemaPSVIIDCKeyPtr;struct _xmlSchemaPSVIIDCKey { xmlSchemaTypePtr type; xmlSchemaValPtr compValue;};/** * xmlSchemaPSVIIDCNode: * * The node table item of a node table. */typedef struct _xmlSchemaPSVIIDCNode xmlSchemaPSVIIDCNode;typedef xmlSchemaPSVIIDCNode *xmlSchemaPSVIIDCNodePtr;struct _xmlSchemaPSVIIDCNode { xmlNodePtr node; xmlSchemaPSVIIDCKeyPtr *keys;};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -