📄 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>
#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_ATTR_VALIDATION 0
/* #define DEBUG_IDC 1 */
/* #define DEBUG_INCLUDES 1 */
/* #define ENABLE_PARTICLE_RESTRICTION 1 */
#define DUMP_CONTENT_MODEL
#define XML_SCHEMA_SAX_ENABLED
#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/";
static const xmlChar *xmlSchemaElemDesElemDecl = (const xmlChar *)
"element decl.";
static const xmlChar *xmlSchemaElemDesAttrDecl = (const xmlChar *)
"attribute decl.";
static const xmlChar *xmlSchemaElemDesAttrRef = (const xmlChar *)
"attribute use";
static const xmlChar *xmlSchemaElemDesCT = (const xmlChar *)
"complex type";
static const xmlChar *xmlSchemaElemModelGrDef = (const xmlChar *)
"model group";
#if 0
static 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((xmlChar *) 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 HAS_SIMPLE_CONTENT(item) \
((item->contentType == XML_SCHEMA_CONTENT_SIMPLE) || \
(item->contentType == XML_SCHEMA_CONTENT_BASIC))
#define HAS_MIXED_CONTENT(item) (item->contentType == XML_SCHEMA_CONTENT_MIXED)
#define IS_PARTICLE_EMPTIABLE(item) \
(xmlSchemaIsParticleEmptiable((xmlSchemaParticlePtr) item->subtypes))
#define GET_NODE(item) xmlSchemaGetComponentNode((xmlSchemaBasicItemPtr) item)
#define GET_LIST_ITEM_TYPE(item) item->subtypes
#define VARIETY_ATOMIC(item) (item->flags & XML_SCHEMAS_TYPE_VARIETY_ATOMIC)
#define VARIETY_LIST(item) (item->flags & XML_SCHEMAS_TYPE_VARIETY_LIST)
#define VARIETY_UNION(item) (item->flags & XML_SCHEMAS_TYPE_VARIETY_UNION)
#define IS_MODEL_GROUP(item) \
((item->type == XML_SCHEMA_TYPE_SEQUENCE) || \
(item->type == XML_SCHEMA_TYPE_CHOICE) || \
(item->type == XML_SCHEMA_TYPE_ALL))
#define INODE_NILLED(item) (item->flags & XML_SCHEMA_ELEM_INFO_NILLED)
#define ELEM_TYPE(item) item->subtypes
#define GET_PARTICLE(item) (xmlSchemaParticlePtr) item->subtypes;
#define SUBST_GROUP_AFF(item) item->refDecl
#if 0
#define WXS_GET_NEXT(item) xmlSchemaGetNextComponent((xmlSchemaBasicItemPtr) item)
#endif
#define SUBSET_RESTRICTION 1<<0
#define SUBSET_EXTENSION 1<<1
#define SUBSET_SUBSTITUTION 1<<2
#define SUBSET_LIST 1<<3
#define SUBSET_UNION 1<<4
#define XML_SCHEMAS_PARSE_ERROR 1
#define SCHEMAS_PARSE_OPTIONS XML_PARSE_NOENT
typedef struct _xmlSchemaNodeInfo xmlSchemaNodeInfo;
typedef xmlSchemaNodeInfo *xmlSchemaNodeInfoPtr;
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 */
};
typedef struct _xmlSchemaAbstractCtxt xmlSchemaAbstractCtxt;
typedef xmlSchemaAbstractCtxt *xmlSchemaAbstractCtxtPtr;
struct _xmlSchemaAbstractCtxt {
int type;
};
#define XML_SCHEMA_CTXT_PARSER 1
#define XML_SCHEMA_CTXT_VALIDATOR 2
struct _xmlSchemaParserCtxt {
int type;
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;
xmlHashTablePtr substGroups;
int isS4S;
};
#define XML_SCHEMAS_ATTR_UNKNOWN 1
#define XML_SCHEMAS_ATTR_ASSESSED 2
#define XML_SCHEMAS_ATTR_PROHIBITED 3
#define XML_SCHEMAS_ATTR_ERR_MISSING 4
#define XML_SCHEMAS_ATTR_INVALID_VALUE 5
#define XML_SCHEMAS_ATTR_ERR_NO_TYPE 6
#define XML_SCHEMAS_ATTR_ERR_FIXED_VALUE 7
#define XML_SCHEMAS_ATTR_DEFAULT 8
#define XML_SCHEMAS_ATTR_VALIDATE_VALUE 9
#define XML_SCHEMAS_ATTR_ERR_WILD_STRICT_NO_DECL 10
#define XML_SCHEMAS_ATTR_HAS_ATTR_USE 11
#define XML_SCHEMAS_ATTR_HAS_ATTR_DECL 12
#define XML_SCHEMAS_ATTR_WILD_SKIP 13
#define XML_SCHEMAS_ATTR_WILD_LAX_NO_DECL 14
#define XML_SCHEMAS_ATTR_ERR_WILD_DUPLICATE_ID 15
#define XML_SCHEMAS_ATTR_ERR_WILD_AND_USE_ID 16
#define XML_SCHEMAS_ATTR_META 17
/**
* 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;
};
#define XML_SCHEMA_MODEL_GROUP_DEF_MARKED 1<<0
/**
* 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;
int flags;
};
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;
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -