Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * relaxng.c : implementation of the Relax-NG handling and validity checking |
| 3 | * |
| 4 | * See Copyright for the status of this software. |
| 5 | * |
| 6 | * Daniel Veillard <veillard@redhat.com> |
| 7 | */ |
| 8 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 9 | /** |
| 10 | * TODO: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 11 | * - error reporting |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 12 | * - handle namespace declarations as attributes. |
Daniel Veillard | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 13 | * - add support for DTD compatibility spec |
| 14 | * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 15 | * - report better mem allocations at runtime and abort immediately. |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 18 | #define IN_LIBXML |
| 19 | #include "libxml.h" |
| 20 | |
| 21 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 22 | |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
| 25 | #include <libxml/xmlmemory.h> |
| 26 | #include <libxml/parser.h> |
| 27 | #include <libxml/parserInternals.h> |
| 28 | #include <libxml/hash.h> |
| 29 | #include <libxml/uri.h> |
| 30 | |
| 31 | #include <libxml/relaxng.h> |
| 32 | |
| 33 | #include <libxml/xmlschemastypes.h> |
| 34 | #include <libxml/xmlautomata.h> |
| 35 | #include <libxml/xmlregexp.h> |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 36 | #include <libxml/xmlschemastypes.h> |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * The Relax-NG namespace |
| 40 | */ |
| 41 | static const xmlChar *xmlRelaxNGNs = (const xmlChar *) |
| 42 | "http://relaxng.org/ns/structure/1.0"; |
| 43 | |
| 44 | #define IS_RELAXNG(node, type) \ |
| 45 | ((node != NULL) && (node->ns != NULL) && \ |
| 46 | (xmlStrEqual(node->name, (const xmlChar *) type)) && \ |
| 47 | (xmlStrEqual(node->ns->href, xmlRelaxNGNs))) |
| 48 | |
| 49 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 50 | /* #define DEBUG 1 */ |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 51 | /* #define DEBUG_GRAMMAR 1 */ |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 52 | /* #define DEBUG_CONTENT 1 */ |
| 53 | /* #define DEBUG_TYPE 1 */ |
| 54 | /* #define DEBUG_VALID 1 */ |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 55 | /* #define DEBUG_INTERLEAVE 1 */ |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 56 | /* #define DEBUG_LIST 1 */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 57 | /* #define DEBUG_INCLUDE */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 58 | /* #define DEBUG_ERROR 1 */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 59 | /* #define DEBUG_COMPILE 1 */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 60 | /* #define DEBUG_PROGRESSIVE 1 */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 61 | |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 62 | #define MAX_ERROR 5 |
| 63 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 64 | #define TODO \ |
| 65 | xmlGenericError(xmlGenericErrorContext, \ |
| 66 | "Unimplemented block at %s:%d\n", \ |
| 67 | __FILE__, __LINE__); |
| 68 | |
| 69 | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
| 70 | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
| 71 | |
| 72 | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
| 73 | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
| 74 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 75 | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
| 76 | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
| 77 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 78 | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
| 79 | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
| 80 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 81 | typedef enum { |
| 82 | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
| 83 | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
| 84 | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
| 85 | } xmlRelaxNGCombine; |
| 86 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 87 | typedef enum { |
| 88 | XML_RELAXNG_CONTENT_ERROR = -1, |
| 89 | XML_RELAXNG_CONTENT_EMPTY = 0, |
| 90 | XML_RELAXNG_CONTENT_SIMPLE, |
| 91 | XML_RELAXNG_CONTENT_COMPLEX |
| 92 | } xmlRelaxNGContentType; |
| 93 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 94 | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
| 95 | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
| 96 | |
| 97 | struct _xmlRelaxNGGrammar { |
| 98 | xmlRelaxNGGrammarPtr parent;/* the parent grammar if any */ |
| 99 | xmlRelaxNGGrammarPtr children;/* the children grammar if any */ |
| 100 | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
| 101 | xmlRelaxNGDefinePtr start; /* <start> content */ |
| 102 | xmlRelaxNGCombine combine; /* the default combine value */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 103 | xmlRelaxNGDefinePtr startList;/* list of <start> definitions */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 104 | xmlHashTablePtr defs; /* define* */ |
| 105 | xmlHashTablePtr refs; /* references */ |
| 106 | }; |
| 107 | |
| 108 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 109 | typedef enum { |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 110 | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 111 | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
| 112 | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 113 | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 114 | XML_RELAXNG_TEXT, /* textual content */ |
| 115 | XML_RELAXNG_ELEMENT, /* an element */ |
| 116 | XML_RELAXNG_DATATYPE, /* extenal data type definition */ |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 117 | XML_RELAXNG_PARAM, /* extenal data type parameter */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 118 | XML_RELAXNG_VALUE, /* value from an extenal data type definition */ |
| 119 | XML_RELAXNG_LIST, /* a list of patterns */ |
| 120 | XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */ |
| 121 | XML_RELAXNG_DEF, /* a definition */ |
| 122 | XML_RELAXNG_REF, /* reference to a definition */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 123 | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 124 | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 125 | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
| 126 | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 127 | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
| 128 | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
| 129 | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 130 | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 131 | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 132 | } xmlRelaxNGType; |
| 133 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 134 | #define IS_NULLABLE (1 << 0) |
| 135 | #define IS_NOT_NULLABLE (1 << 1) |
| 136 | #define IS_INDETERMINIST (1 << 2) |
| 137 | #define IS_MIXED (1 << 3) |
| 138 | #define IS_TRIABLE (1 << 4) |
| 139 | #define IS_PROCESSED (1 << 5) |
| 140 | #define IS_COMPILABLE (1 << 6) |
| 141 | #define IS_NOT_COMPILABLE (1 << 7) |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 142 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 143 | struct _xmlRelaxNGDefine { |
| 144 | xmlRelaxNGType type; /* the type of definition */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 145 | xmlNodePtr node; /* the node in the source */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 146 | xmlChar *name; /* the element local name if present */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 147 | xmlChar *ns; /* the namespace local name if present */ |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 148 | xmlChar *value; /* value when available */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 149 | void *data; /* data lib or specific pointer */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 150 | xmlRelaxNGDefinePtr content;/* the expected content */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 151 | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 152 | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 153 | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 154 | xmlRelaxNGDefinePtr nameClass;/* the nameClass definition if any */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 155 | xmlRelaxNGDefinePtr nextHash;/* next define in defs/refs hash tables */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 156 | short depth; /* used for the cycle detection */ |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 157 | short dflags; /* define related flags */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 158 | xmlRegexpPtr contModel; /* a compiled content model if available */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | /** |
| 162 | * _xmlRelaxNG: |
| 163 | * |
| 164 | * A RelaxNGs definition |
| 165 | */ |
| 166 | struct _xmlRelaxNG { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 167 | void *_private; /* unused by the library for users or bindings */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 168 | xmlRelaxNGGrammarPtr topgrammar; |
| 169 | xmlDocPtr doc; |
| 170 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 171 | int idref; /* requires idref checking */ |
| 172 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 173 | xmlHashTablePtr defs; /* define */ |
| 174 | xmlHashTablePtr refs; /* references */ |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 175 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 176 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 177 | int defNr; /* number of defines used */ |
| 178 | xmlRelaxNGDefinePtr *defTab;/* pointer to the allocated definitions */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 179 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 182 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
| 183 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
| 184 | #define XML_RELAXNG_IN_LIST (1 << 2) |
| 185 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
| 186 | #define XML_RELAXNG_IN_START (1 << 4) |
| 187 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
| 188 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
| 189 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 190 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
| 191 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 192 | |
| 193 | struct _xmlRelaxNGParserCtxt { |
| 194 | void *userData; /* user specific data block */ |
| 195 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 196 | xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 197 | xmlRelaxNGValidErr err; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 198 | |
| 199 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 200 | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 201 | xmlRelaxNGGrammarPtr parentgrammar;/* the parent grammar */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 202 | int flags; /* parser flags */ |
| 203 | int nbErrors; /* number of errors at parse time */ |
| 204 | int nbWarnings; /* number of warnings at parse time */ |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 205 | const xmlChar *define; /* the current define scope */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 206 | xmlRelaxNGDefinePtr def; /* the current define */ |
| 207 | |
| 208 | int nbInterleaves; |
| 209 | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 210 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 211 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 212 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 213 | xmlChar *URL; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 214 | xmlDocPtr document; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 215 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 216 | int defNr; /* number of defines used */ |
| 217 | int defMax; /* number of defines aloocated */ |
| 218 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
| 219 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 220 | const char *buffer; |
| 221 | int size; |
| 222 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 223 | /* the document stack */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 224 | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 225 | int docNr; /* Depth of the parsing stack */ |
| 226 | int docMax; /* Max depth of the parsing stack */ |
| 227 | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 228 | |
| 229 | /* the include stack */ |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 230 | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 231 | int incNr; /* Depth of the include parsing stack */ |
| 232 | int incMax; /* Max depth of the parsing stack */ |
| 233 | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 234 | |
| 235 | int idref; /* requires idref checking */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 236 | |
| 237 | /* used to compile content models */ |
| 238 | xmlAutomataPtr am; /* the automata */ |
| 239 | xmlAutomataStatePtr state; /* used to build the automata */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | #define FLAGS_IGNORABLE 1 |
| 243 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 244 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 245 | |
| 246 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 247 | * xmlRelaxNGInterleaveGroup: |
| 248 | * |
| 249 | * A RelaxNGs partition set associated to lists of definitions |
| 250 | */ |
| 251 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 252 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 253 | struct _xmlRelaxNGInterleaveGroup { |
| 254 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 255 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 256 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 257 | }; |
| 258 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 259 | #define IS_DETERMINIST 1 |
| 260 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 261 | /** |
| 262 | * xmlRelaxNGPartitions: |
| 263 | * |
| 264 | * A RelaxNGs partition associated to an interleave group |
| 265 | */ |
| 266 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 267 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 268 | struct _xmlRelaxNGPartition { |
| 269 | int nbgroups; /* number of groups in the partitions */ |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 270 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 271 | right group when possible */ |
| 272 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 273 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 274 | }; |
| 275 | |
| 276 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 277 | * xmlRelaxNGValidState: |
| 278 | * |
| 279 | * A RelaxNGs validation state |
| 280 | */ |
| 281 | #define MAX_ATTR 20 |
| 282 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 283 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 284 | struct _xmlRelaxNGValidState { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 285 | xmlNodePtr node; /* the current node */ |
| 286 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 287 | int nbAttrs; /* the number of attributes */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 288 | int maxAttrs; /* the size of attrs */ |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 289 | int nbAttrLeft; /* the number of attributes left to validate */ |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 290 | xmlChar *value; /* the value when operating on string */ |
| 291 | xmlChar *endvalue; /* the end value when operating on string */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 292 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 293 | }; |
| 294 | |
| 295 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 296 | * xmlRelaxNGStates: |
| 297 | * |
| 298 | * A RelaxNGs container for validation state |
| 299 | */ |
| 300 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 301 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 302 | struct _xmlRelaxNGStates { |
| 303 | int nbState; /* the number of states */ |
| 304 | int maxState; /* the size of the array */ |
| 305 | xmlRelaxNGValidStatePtr *tabState; |
| 306 | }; |
| 307 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 308 | #define ERROR_IS_DUP 1 |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 309 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 310 | * xmlRelaxNGValidError: |
| 311 | * |
| 312 | * A RelaxNGs validation error |
| 313 | */ |
| 314 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 315 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 316 | struct _xmlRelaxNGValidError { |
| 317 | xmlRelaxNGValidErr err; /* the error number */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 318 | int flags; /* flags */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 319 | xmlNodePtr node; /* the current node */ |
| 320 | xmlNodePtr seq; /* the current child */ |
| 321 | const xmlChar * arg1; /* first arg */ |
| 322 | const xmlChar * arg2; /* second arg */ |
| 323 | }; |
| 324 | |
| 325 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 326 | * xmlRelaxNGValidCtxt: |
| 327 | * |
| 328 | * A RelaxNGs validation context |
| 329 | */ |
| 330 | |
| 331 | struct _xmlRelaxNGValidCtxt { |
| 332 | void *userData; /* user specific data block */ |
| 333 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 334 | xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */ |
| 335 | |
| 336 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 337 | xmlDocPtr doc; /* the document being validated */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 338 | int flags; /* validation flags */ |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 339 | int depth; /* validation depth */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 340 | int idref; /* requires idref checking */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 341 | int errNo; /* the first error found */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 342 | |
| 343 | /* |
| 344 | * Errors accumulated in branches may have to be stacked to be |
| 345 | * provided back when it's sure they affect validation. |
| 346 | */ |
| 347 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
| 348 | int errNr; /* Depth of the error stack */ |
| 349 | int errMax; /* Max depth of the error stack */ |
| 350 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 351 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 352 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 353 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 354 | |
| 355 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 356 | int freeStatesNr; |
| 357 | int freeStatesMax; |
| 358 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * This is used for "progressive" validation |
| 362 | */ |
| 363 | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
| 364 | int elemNr; /* the number of element validated */ |
| 365 | int elemMax; /* the max depth of elements */ |
| 366 | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
| 367 | int pstate; /* progressive state */ |
| 368 | xmlNodePtr pnode; /* the current node */ |
| 369 | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 370 | }; |
| 371 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 372 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 373 | * xmlRelaxNGInclude: |
| 374 | * |
| 375 | * Structure associated to a RelaxNGs document element |
| 376 | */ |
| 377 | struct _xmlRelaxNGInclude { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 378 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 379 | xmlChar *href; /* the normalized href value */ |
| 380 | xmlDocPtr doc; /* the associated XML document */ |
| 381 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 382 | xmlRelaxNGPtr schema; /* the schema */ |
| 383 | }; |
| 384 | |
| 385 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 386 | * xmlRelaxNGDocument: |
| 387 | * |
| 388 | * Structure associated to a RelaxNGs document element |
| 389 | */ |
| 390 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 391 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 392 | xmlChar *href; /* the normalized href value */ |
| 393 | xmlDocPtr doc; /* the associated XML document */ |
| 394 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 395 | xmlRelaxNGPtr schema; /* the schema */ |
| 396 | }; |
| 397 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 398 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 399 | /************************************************************************ |
| 400 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 401 | * Preliminary type checking interfaces * |
| 402 | * * |
| 403 | ************************************************************************/ |
| 404 | /** |
| 405 | * xmlRelaxNGTypeHave: |
| 406 | * @data: data needed for the library |
| 407 | * @type: the type name |
| 408 | * @value: the value to check |
| 409 | * |
| 410 | * Function provided by a type library to check if a type is exported |
| 411 | * |
| 412 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 413 | */ |
| 414 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar *type); |
| 415 | |
| 416 | /** |
| 417 | * xmlRelaxNGTypeCheck: |
| 418 | * @data: data needed for the library |
| 419 | * @type: the type name |
| 420 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 421 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 422 | * |
| 423 | * Function provided by a type library to check if a value match a type |
| 424 | * |
| 425 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 426 | */ |
| 427 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar *type, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 428 | const xmlChar *value, void **result, |
| 429 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 430 | |
| 431 | /** |
| 432 | * xmlRelaxNGFacetCheck: |
| 433 | * @data: data needed for the library |
| 434 | * @type: the type name |
| 435 | * @facet: the facet name |
| 436 | * @val: the facet value |
| 437 | * @strval: the string value |
| 438 | * @value: the value to check |
| 439 | * |
| 440 | * Function provided by a type library to check a value facet |
| 441 | * |
| 442 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 443 | */ |
| 444 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar *type, |
| 445 | const xmlChar *facet, const xmlChar *val, |
| 446 | const xmlChar *strval, void *value); |
| 447 | |
| 448 | /** |
| 449 | * xmlRelaxNGTypeFree: |
| 450 | * @data: data needed for the library |
| 451 | * @result: the value to free |
| 452 | * |
| 453 | * Function provided by a type library to free a returned result |
| 454 | */ |
| 455 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 456 | |
| 457 | /** |
| 458 | * xmlRelaxNGTypeCompare: |
| 459 | * @data: data needed for the library |
| 460 | * @type: the type name |
| 461 | * @value1: the first value |
| 462 | * @value2: the second value |
| 463 | * |
| 464 | * Function provided by a type library to compare two values accordingly |
| 465 | * to a type. |
| 466 | * |
| 467 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 468 | */ |
| 469 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar *type, |
| 470 | const xmlChar *value1, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 471 | xmlNodePtr ctxt1, |
| 472 | void *comp1, |
| 473 | const xmlChar *value2, |
| 474 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 475 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 476 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 477 | struct _xmlRelaxNGTypeLibrary { |
| 478 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 479 | void *data; /* data needed for the library */ |
| 480 | xmlRelaxNGTypeHave have; /* the export function */ |
| 481 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 482 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 483 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 484 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 485 | }; |
| 486 | |
| 487 | /************************************************************************ |
| 488 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 489 | * Allocation functions * |
| 490 | * * |
| 491 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 492 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 493 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 494 | static void xmlRelaxNGNormExtSpace(xmlChar *value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 495 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 496 | static int xmlRelaxNGEqualValidState( |
| 497 | xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 498 | xmlRelaxNGValidStatePtr state1, |
| 499 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 500 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 501 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 502 | |
| 503 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 504 | * xmlRelaxNGFreeDocument: |
| 505 | * @docu: a document structure |
| 506 | * |
| 507 | * Deallocate a RelaxNG document structure. |
| 508 | */ |
| 509 | static void |
| 510 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 511 | { |
| 512 | if (docu == NULL) |
| 513 | return; |
| 514 | |
| 515 | if (docu->href != NULL) |
| 516 | xmlFree(docu->href); |
| 517 | if (docu->doc != NULL) |
| 518 | xmlFreeDoc(docu->doc); |
| 519 | if (docu->schema != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 520 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 521 | xmlFree(docu); |
| 522 | } |
| 523 | |
| 524 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 525 | * xmlRelaxNGFreeDocumentList: |
| 526 | * @docu: a list of document structure |
| 527 | * |
| 528 | * Deallocate a RelaxNG document structures. |
| 529 | */ |
| 530 | static void |
| 531 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 532 | { |
| 533 | xmlRelaxNGDocumentPtr next; |
| 534 | while (docu != NULL) { |
| 535 | next = docu->next; |
| 536 | xmlRelaxNGFreeDocument(docu); |
| 537 | docu = next; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 542 | * xmlRelaxNGFreeInclude: |
| 543 | * @incl: a include structure |
| 544 | * |
| 545 | * Deallocate a RelaxNG include structure. |
| 546 | */ |
| 547 | static void |
| 548 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 549 | { |
| 550 | if (incl == NULL) |
| 551 | return; |
| 552 | |
| 553 | if (incl->href != NULL) |
| 554 | xmlFree(incl->href); |
| 555 | if (incl->doc != NULL) |
| 556 | xmlFreeDoc(incl->doc); |
| 557 | if (incl->schema != NULL) |
| 558 | xmlRelaxNGFree(incl->schema); |
| 559 | xmlFree(incl); |
| 560 | } |
| 561 | |
| 562 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 563 | * xmlRelaxNGFreeIncludeList: |
| 564 | * @incl: a include structure list |
| 565 | * |
| 566 | * Deallocate a RelaxNG include structure. |
| 567 | */ |
| 568 | static void |
| 569 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 570 | { |
| 571 | xmlRelaxNGIncludePtr next; |
| 572 | while (incl != NULL) { |
| 573 | next = incl->next; |
| 574 | xmlRelaxNGFreeInclude(incl); |
| 575 | incl = next; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 580 | * xmlRelaxNGNewRelaxNG: |
| 581 | * @ctxt: a Relax-NG validation context (optional) |
| 582 | * |
| 583 | * Allocate a new RelaxNG structure. |
| 584 | * |
| 585 | * Returns the newly allocated structure or NULL in case or error |
| 586 | */ |
| 587 | static xmlRelaxNGPtr |
| 588 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 589 | { |
| 590 | xmlRelaxNGPtr ret; |
| 591 | |
| 592 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 593 | if (ret == NULL) { |
| 594 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 595 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 596 | ctxt->nbErrors++; |
| 597 | return (NULL); |
| 598 | } |
| 599 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 600 | |
| 601 | return (ret); |
| 602 | } |
| 603 | |
| 604 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 605 | * xmlRelaxNGFreeInnerSchema: |
| 606 | * @schema: a schema structure |
| 607 | * |
| 608 | * Deallocate a RelaxNG schema structure. |
| 609 | */ |
| 610 | static void |
| 611 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 612 | { |
| 613 | if (schema == NULL) |
| 614 | return; |
| 615 | |
| 616 | if (schema->doc != NULL) |
| 617 | xmlFreeDoc(schema->doc); |
| 618 | if (schema->defTab != NULL) { |
| 619 | int i; |
| 620 | |
| 621 | for (i = 0;i < schema->defNr;i++) |
| 622 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 623 | xmlFree(schema->defTab); |
| 624 | } |
| 625 | |
| 626 | xmlFree(schema); |
| 627 | } |
| 628 | |
| 629 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 630 | * xmlRelaxNGFree: |
| 631 | * @schema: a schema structure |
| 632 | * |
| 633 | * Deallocate a RelaxNG structure. |
| 634 | */ |
| 635 | void |
| 636 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 637 | { |
| 638 | if (schema == NULL) |
| 639 | return; |
| 640 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 641 | if (schema->topgrammar != NULL) |
| 642 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
| 643 | if (schema->doc != NULL) |
| 644 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 645 | if (schema->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 646 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 647 | if (schema->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 648 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 649 | if (schema->defTab != NULL) { |
| 650 | int i; |
| 651 | |
| 652 | for (i = 0;i < schema->defNr;i++) |
| 653 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 654 | xmlFree(schema->defTab); |
| 655 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 656 | |
| 657 | xmlFree(schema); |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * xmlRelaxNGNewGrammar: |
| 662 | * @ctxt: a Relax-NG validation context (optional) |
| 663 | * |
| 664 | * Allocate a new RelaxNG grammar. |
| 665 | * |
| 666 | * Returns the newly allocated structure or NULL in case or error |
| 667 | */ |
| 668 | static xmlRelaxNGGrammarPtr |
| 669 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 670 | { |
| 671 | xmlRelaxNGGrammarPtr ret; |
| 672 | |
| 673 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 674 | if (ret == NULL) { |
| 675 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 676 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 677 | ctxt->nbErrors++; |
| 678 | return (NULL); |
| 679 | } |
| 680 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 681 | |
| 682 | return (ret); |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * xmlRelaxNGFreeGrammar: |
| 687 | * @grammar: a grammar structure |
| 688 | * |
| 689 | * Deallocate a RelaxNG grammar structure. |
| 690 | */ |
| 691 | static void |
| 692 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 693 | { |
| 694 | if (grammar == NULL) |
| 695 | return; |
| 696 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 697 | if (grammar->children != NULL) { |
| 698 | xmlRelaxNGFreeGrammar(grammar->children); |
| 699 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 700 | if (grammar->next != NULL) { |
| 701 | xmlRelaxNGFreeGrammar(grammar->next); |
| 702 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 703 | if (grammar->refs != NULL) { |
| 704 | xmlHashFree(grammar->refs, NULL); |
| 705 | } |
| 706 | if (grammar->defs != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 707 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | xmlFree(grammar); |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * xmlRelaxNGNewDefine: |
| 715 | * @ctxt: a Relax-NG validation context |
| 716 | * @node: the node in the input document. |
| 717 | * |
| 718 | * Allocate a new RelaxNG define. |
| 719 | * |
| 720 | * Returns the newly allocated structure or NULL in case or error |
| 721 | */ |
| 722 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 723 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 724 | { |
| 725 | xmlRelaxNGDefinePtr ret; |
| 726 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 727 | if (ctxt->defMax == 0) { |
| 728 | ctxt->defMax = 16; |
| 729 | ctxt->defNr = 0; |
| 730 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 731 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 732 | if (ctxt->defTab == NULL) { |
| 733 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 734 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 735 | ctxt->nbErrors++; |
| 736 | return (NULL); |
| 737 | } |
| 738 | } else if (ctxt->defMax <= ctxt->defNr) { |
| 739 | xmlRelaxNGDefinePtr *tmp; |
| 740 | ctxt->defMax *= 2; |
| 741 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 742 | ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 743 | if (tmp == NULL) { |
| 744 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 745 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 746 | ctxt->nbErrors++; |
| 747 | return (NULL); |
| 748 | } |
| 749 | ctxt->defTab = tmp; |
| 750 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 751 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 752 | if (ret == NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 753 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 754 | ctxt->error(ctxt->userData, "Out of memory\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 755 | ctxt->nbErrors++; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 756 | return(NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 757 | } |
| 758 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 759 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 760 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 761 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 762 | return (ret); |
| 763 | } |
| 764 | |
| 765 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 766 | * xmlRelaxNGFreePartition: |
| 767 | * @partitions: a partition set structure |
| 768 | * |
| 769 | * Deallocate RelaxNG partition set structures. |
| 770 | */ |
| 771 | static void |
| 772 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) { |
| 773 | xmlRelaxNGInterleaveGroupPtr group; |
| 774 | int j; |
| 775 | |
| 776 | if (partitions != NULL) { |
| 777 | if (partitions->groups != NULL) { |
| 778 | for (j = 0;j < partitions->nbgroups;j++) { |
| 779 | group = partitions->groups[j]; |
| 780 | if (group != NULL) { |
| 781 | if (group->defs != NULL) |
| 782 | xmlFree(group->defs); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 783 | if (group->attrs != NULL) |
| 784 | xmlFree(group->attrs); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 785 | xmlFree(group); |
| 786 | } |
| 787 | } |
| 788 | xmlFree(partitions->groups); |
| 789 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 790 | if (partitions->triage != NULL) { |
| 791 | xmlHashFree(partitions->triage, NULL); |
| 792 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 793 | xmlFree(partitions); |
| 794 | } |
| 795 | } |
| 796 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 797 | * xmlRelaxNGFreeDefine: |
| 798 | * @define: a define structure |
| 799 | * |
| 800 | * Deallocate a RelaxNG define structure. |
| 801 | */ |
| 802 | static void |
| 803 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 804 | { |
| 805 | if (define == NULL) |
| 806 | return; |
| 807 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 808 | if ((define->type == XML_RELAXNG_VALUE) && |
| 809 | (define->attrs != NULL)) { |
| 810 | xmlRelaxNGTypeLibraryPtr lib; |
| 811 | |
| 812 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 813 | if ((lib != NULL) && (lib->freef != NULL)) |
| 814 | lib->freef(lib->data, (void *) define->attrs); |
| 815 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 816 | if ((define->data != NULL) && |
| 817 | (define->type == XML_RELAXNG_INTERLEAVE)) |
| 818 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 819 | if ((define->data != NULL) && |
| 820 | (define->type == XML_RELAXNG_CHOICE)) |
| 821 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 822 | if (define->name != NULL) |
| 823 | xmlFree(define->name); |
| 824 | if (define->ns != NULL) |
| 825 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 826 | if (define->value != NULL) |
| 827 | xmlFree(define->value); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 828 | if (define->contModel != NULL) |
| 829 | xmlRegFreeRegexp(define->contModel); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 830 | xmlFree(define); |
| 831 | } |
| 832 | |
| 833 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 834 | * xmlRelaxNGNewStates: |
| 835 | * @ctxt: a Relax-NG validation context |
| 836 | * @size: the default size for the container |
| 837 | * |
| 838 | * Allocate a new RelaxNG validation state container |
| 839 | * TODO: keep a pool in the ctxt |
| 840 | * |
| 841 | * Returns the newly allocated structure or NULL in case or error |
| 842 | */ |
| 843 | static xmlRelaxNGStatesPtr |
| 844 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 845 | { |
| 846 | xmlRelaxNGStatesPtr ret; |
| 847 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 848 | if ((ctxt != NULL) && |
| 849 | (ctxt->freeState != NULL) && |
| 850 | (ctxt->freeStatesNr > 0)) { |
| 851 | ctxt->freeStatesNr--; |
| 852 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 853 | ret->nbState = 0; |
| 854 | return(ret); |
| 855 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 856 | if (size < 16) size = 16; |
| 857 | |
| 858 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
| 859 | (size - 1) * sizeof(xmlRelaxNGValidStatePtr)); |
| 860 | if (ret == NULL) { |
| 861 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 862 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 863 | return (NULL); |
| 864 | } |
| 865 | ret->nbState = 0; |
| 866 | ret->maxState = size; |
| 867 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc( |
| 868 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 869 | if (ret->tabState == NULL) { |
| 870 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 871 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 872 | xmlFree(ret->tabState); |
| 873 | return (NULL); |
| 874 | } |
| 875 | return(ret); |
| 876 | } |
| 877 | |
| 878 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 879 | * xmlRelaxNGAddStateUniq: |
| 880 | * @ctxt: a Relax-NG validation context |
| 881 | * @states: the states container |
| 882 | * @state: the validation state |
| 883 | * |
| 884 | * Add a RelaxNG validation state to the container without checking |
| 885 | * for unicity. |
| 886 | * |
| 887 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 888 | */ |
| 889 | static int |
| 890 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
| 891 | xmlRelaxNGStatesPtr states, |
| 892 | xmlRelaxNGValidStatePtr state) |
| 893 | { |
| 894 | if (state == NULL) { |
| 895 | return(-1); |
| 896 | } |
| 897 | if (states->nbState >= states->maxState) { |
| 898 | xmlRelaxNGValidStatePtr *tmp; |
| 899 | int size; |
| 900 | |
| 901 | size = states->maxState * 2; |
| 902 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 903 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 904 | if (tmp == NULL) { |
| 905 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 906 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 907 | return(-1); |
| 908 | } |
| 909 | states->tabState = tmp; |
| 910 | states->maxState = size; |
| 911 | } |
| 912 | states->tabState[states->nbState++] = state; |
| 913 | return(1); |
| 914 | } |
| 915 | |
| 916 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 917 | * xmlRelaxNGAddState: |
| 918 | * @ctxt: a Relax-NG validation context |
| 919 | * @states: the states container |
| 920 | * @state: the validation state |
| 921 | * |
| 922 | * Add a RelaxNG validation state to the container |
| 923 | * |
| 924 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 925 | */ |
| 926 | static int |
| 927 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGStatesPtr states, |
| 928 | xmlRelaxNGValidStatePtr state) |
| 929 | { |
| 930 | int i; |
| 931 | |
| 932 | if (state == NULL) { |
| 933 | return(-1); |
| 934 | } |
| 935 | if (states->nbState >= states->maxState) { |
| 936 | xmlRelaxNGValidStatePtr *tmp; |
| 937 | int size; |
| 938 | |
| 939 | size = states->maxState * 2; |
| 940 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 941 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 942 | if (tmp == NULL) { |
| 943 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 944 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 945 | return(-1); |
| 946 | } |
| 947 | states->tabState = tmp; |
| 948 | states->maxState = size; |
| 949 | } |
| 950 | for (i = 0;i < states->nbState;i++) { |
| 951 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 952 | xmlRelaxNGFreeValidState(ctxt, state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 953 | return(0); |
| 954 | } |
| 955 | } |
| 956 | states->tabState[states->nbState++] = state; |
| 957 | return(1); |
| 958 | } |
| 959 | |
| 960 | /** |
| 961 | * xmlRelaxNGFreeStates: |
| 962 | * @ctxt: a Relax-NG validation context |
| 963 | * @states: teh container |
| 964 | * |
| 965 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 966 | */ |
| 967 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 968 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 969 | xmlRelaxNGStatesPtr states) |
| 970 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 971 | if (states == NULL) |
| 972 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 973 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
| 974 | ctxt->freeStatesMax = 40; |
| 975 | ctxt->freeStatesNr = 0; |
| 976 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 977 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 978 | if (ctxt->freeStates == NULL) { |
| 979 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 980 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 981 | } |
| 982 | } else if ((ctxt != NULL) && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 983 | xmlRelaxNGStatesPtr *tmp; |
| 984 | |
| 985 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 986 | 2 * ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 987 | if (tmp == NULL) { |
| 988 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 989 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 990 | xmlFree(states->tabState); |
| 991 | xmlFree(states); |
| 992 | return; |
| 993 | } |
| 994 | ctxt->freeStates = tmp; |
| 995 | ctxt->freeStatesMax *= 2; |
| 996 | } |
| 997 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 998 | xmlFree(states->tabState); |
| 999 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1000 | } else { |
| 1001 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1006 | * xmlRelaxNGNewValidState: |
| 1007 | * @ctxt: a Relax-NG validation context |
| 1008 | * @node: the current node or NULL for the document |
| 1009 | * |
| 1010 | * Allocate a new RelaxNG validation state |
| 1011 | * |
| 1012 | * Returns the newly allocated structure or NULL in case or error |
| 1013 | */ |
| 1014 | static xmlRelaxNGValidStatePtr |
| 1015 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 1016 | { |
| 1017 | xmlRelaxNGValidStatePtr ret; |
| 1018 | xmlAttrPtr attr; |
| 1019 | xmlAttrPtr attrs[MAX_ATTR]; |
| 1020 | int nbAttrs = 0; |
| 1021 | xmlNodePtr root = NULL; |
| 1022 | |
| 1023 | if (node == NULL) { |
| 1024 | root = xmlDocGetRootElement(ctxt->doc); |
| 1025 | if (root == NULL) |
| 1026 | return(NULL); |
| 1027 | } else { |
| 1028 | attr = node->properties; |
| 1029 | while (attr != NULL) { |
| 1030 | if (nbAttrs < MAX_ATTR) |
| 1031 | attrs[nbAttrs++] = attr; |
| 1032 | else |
| 1033 | nbAttrs++; |
| 1034 | attr = attr->next; |
| 1035 | } |
| 1036 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1037 | if ((ctxt->freeState != NULL) && |
| 1038 | (ctxt->freeState->nbState > 0)) { |
| 1039 | ctxt->freeState->nbState--; |
| 1040 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1041 | } else { |
| 1042 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1043 | if (ret == NULL) { |
| 1044 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1045 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1046 | return (NULL); |
| 1047 | } |
| 1048 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1049 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1050 | ret->value = NULL; |
| 1051 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1052 | if (node == NULL) { |
| 1053 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1054 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1055 | } else { |
| 1056 | ret->node = node; |
| 1057 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1058 | } |
| 1059 | ret->nbAttrs = 0; |
| 1060 | if (nbAttrs > 0) { |
| 1061 | if (ret->attrs == NULL) { |
| 1062 | if (nbAttrs < 4) ret->maxAttrs = 4; |
| 1063 | else ret->maxAttrs = nbAttrs; |
| 1064 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1065 | sizeof(xmlAttrPtr)); |
| 1066 | if (ret->attrs == NULL) { |
| 1067 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1068 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1069 | return (ret); |
| 1070 | } |
| 1071 | } else if (ret->maxAttrs < nbAttrs) { |
| 1072 | xmlAttrPtr *tmp; |
| 1073 | |
| 1074 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1075 | sizeof(xmlAttrPtr)); |
| 1076 | if (tmp == NULL) { |
| 1077 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1078 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1079 | return (ret); |
| 1080 | } |
| 1081 | ret->attrs = tmp; |
| 1082 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1083 | ret->nbAttrs = nbAttrs; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1084 | if (nbAttrs < MAX_ATTR) { |
| 1085 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1086 | } else { |
| 1087 | attr = node->properties; |
| 1088 | nbAttrs = 0; |
| 1089 | while (attr != NULL) { |
| 1090 | ret->attrs[nbAttrs++] = attr; |
| 1091 | attr = attr->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1095 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1096 | return (ret); |
| 1097 | } |
| 1098 | |
| 1099 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1100 | * xmlRelaxNGCopyValidState: |
| 1101 | * @ctxt: a Relax-NG validation context |
| 1102 | * @state: a validation state |
| 1103 | * |
| 1104 | * Copy the validation state |
| 1105 | * |
| 1106 | * Returns the newly allocated structure or NULL in case or error |
| 1107 | */ |
| 1108 | static xmlRelaxNGValidStatePtr |
| 1109 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1110 | xmlRelaxNGValidStatePtr state) |
| 1111 | { |
| 1112 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1113 | unsigned int maxAttrs; |
| 1114 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1115 | |
| 1116 | if (state == NULL) |
| 1117 | return(NULL); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1118 | if ((ctxt->freeState != NULL) && |
| 1119 | (ctxt->freeState->nbState > 0)) { |
| 1120 | ctxt->freeState->nbState--; |
| 1121 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1122 | } else { |
| 1123 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1124 | if (ret == NULL) { |
| 1125 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1126 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1127 | return (NULL); |
| 1128 | } |
| 1129 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1130 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1131 | attrs = ret->attrs; |
| 1132 | maxAttrs = ret->maxAttrs; |
| 1133 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1134 | ret->attrs = attrs; |
| 1135 | ret->maxAttrs = maxAttrs; |
| 1136 | if (state->nbAttrs > 0) { |
| 1137 | if (ret->attrs == NULL) { |
| 1138 | ret->maxAttrs = state->maxAttrs; |
| 1139 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1140 | sizeof(xmlAttrPtr)); |
| 1141 | if (ret->attrs == NULL) { |
| 1142 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1143 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1144 | ret->nbAttrs = 0; |
| 1145 | return (ret); |
| 1146 | } |
| 1147 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1148 | xmlAttrPtr *tmp; |
| 1149 | |
| 1150 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1151 | sizeof(xmlAttrPtr)); |
| 1152 | if (tmp == NULL) { |
| 1153 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1154 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1155 | ret->nbAttrs = 0; |
| 1156 | return (ret); |
| 1157 | } |
| 1158 | ret->maxAttrs = state->maxAttrs; |
| 1159 | } |
| 1160 | memcpy(ret->attrs, state->attrs, state->nbAttrs * sizeof(xmlAttrPtr)); |
| 1161 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1162 | return(ret); |
| 1163 | } |
| 1164 | |
| 1165 | /** |
| 1166 | * xmlRelaxNGEqualValidState: |
| 1167 | * @ctxt: a Relax-NG validation context |
| 1168 | * @state1: a validation state |
| 1169 | * @state2: a validation state |
| 1170 | * |
| 1171 | * Compare the validation states for equality |
| 1172 | * |
| 1173 | * Returns 1 if equald, 0 otherwise |
| 1174 | */ |
| 1175 | static int |
| 1176 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1177 | xmlRelaxNGValidStatePtr state1, |
| 1178 | xmlRelaxNGValidStatePtr state2) |
| 1179 | { |
| 1180 | int i; |
| 1181 | |
| 1182 | if ((state1 == NULL) || (state2 == NULL)) |
| 1183 | return(0); |
| 1184 | if (state1 == state2) |
| 1185 | return(1); |
| 1186 | if (state1->node != state2->node) |
| 1187 | return(0); |
| 1188 | if (state1->seq != state2->seq) |
| 1189 | return(0); |
| 1190 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
| 1191 | return(0); |
| 1192 | if (state1->nbAttrs != state2->nbAttrs) |
| 1193 | return(0); |
| 1194 | if (state1->endvalue != state2->endvalue) |
| 1195 | return(0); |
| 1196 | if ((state1->value != state2->value) && |
| 1197 | (!xmlStrEqual(state1->value, state2->value))) |
| 1198 | return(0); |
| 1199 | for (i = 0;i < state1->nbAttrs;i++) { |
| 1200 | if (state1->attrs[i] != state2->attrs[i]) |
| 1201 | return(0); |
| 1202 | } |
| 1203 | return(1); |
| 1204 | } |
| 1205 | |
| 1206 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1207 | * xmlRelaxNGFreeValidState: |
| 1208 | * @state: a validation state structure |
| 1209 | * |
| 1210 | * Deallocate a RelaxNG validation state structure. |
| 1211 | */ |
| 1212 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1213 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1214 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1215 | { |
| 1216 | if (state == NULL) |
| 1217 | return; |
| 1218 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1219 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
| 1220 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
| 1221 | } |
| 1222 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
| 1223 | if (state->attrs != NULL) |
| 1224 | xmlFree(state->attrs); |
| 1225 | xmlFree(state); |
| 1226 | } else { |
| 1227 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
| 1228 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | /************************************************************************ |
| 1232 | * * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1233 | * Document functions * |
| 1234 | * * |
| 1235 | ************************************************************************/ |
| 1236 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
| 1237 | xmlDocPtr doc); |
| 1238 | |
| 1239 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1240 | * xmlRelaxNGIncludePush: |
| 1241 | * @ctxt: the parser context |
| 1242 | * @value: the element doc |
| 1243 | * |
| 1244 | * Pushes a new include on top of the include stack |
| 1245 | * |
| 1246 | * Returns 0 in case of error, the index in the stack otherwise |
| 1247 | */ |
| 1248 | static int |
| 1249 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1250 | xmlRelaxNGIncludePtr value) |
| 1251 | { |
| 1252 | if (ctxt->incTab == NULL) { |
| 1253 | ctxt->incMax = 4; |
| 1254 | ctxt->incNr = 0; |
| 1255 | ctxt->incTab = (xmlRelaxNGIncludePtr *) xmlMalloc( |
| 1256 | ctxt->incMax * sizeof(ctxt->incTab[0])); |
| 1257 | if (ctxt->incTab == NULL) { |
| 1258 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1259 | return (0); |
| 1260 | } |
| 1261 | } |
| 1262 | if (ctxt->incNr >= ctxt->incMax) { |
| 1263 | ctxt->incMax *= 2; |
| 1264 | ctxt->incTab = |
| 1265 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
| 1266 | ctxt->incMax * |
| 1267 | sizeof(ctxt->incTab[0])); |
| 1268 | if (ctxt->incTab == NULL) { |
| 1269 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1270 | return (0); |
| 1271 | } |
| 1272 | } |
| 1273 | ctxt->incTab[ctxt->incNr] = value; |
| 1274 | ctxt->inc = value; |
| 1275 | return (ctxt->incNr++); |
| 1276 | } |
| 1277 | |
| 1278 | /** |
| 1279 | * xmlRelaxNGIncludePop: |
| 1280 | * @ctxt: the parser context |
| 1281 | * |
| 1282 | * Pops the top include from the include stack |
| 1283 | * |
| 1284 | * Returns the include just removed |
| 1285 | */ |
| 1286 | static xmlRelaxNGIncludePtr |
| 1287 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1288 | { |
| 1289 | xmlRelaxNGIncludePtr ret; |
| 1290 | |
| 1291 | if (ctxt->incNr <= 0) |
| 1292 | return (0); |
| 1293 | ctxt->incNr--; |
| 1294 | if (ctxt->incNr > 0) |
| 1295 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1296 | else |
| 1297 | ctxt->inc = NULL; |
| 1298 | ret = ctxt->incTab[ctxt->incNr]; |
| 1299 | ctxt->incTab[ctxt->incNr] = 0; |
| 1300 | return (ret); |
| 1301 | } |
| 1302 | |
| 1303 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1304 | * xmlRelaxNGRemoveRedefine: |
| 1305 | * @ctxt: the parser context |
| 1306 | * @URL: the normalized URL |
| 1307 | * @target: the included target |
| 1308 | * @name: the define name to eliminate |
| 1309 | * |
| 1310 | * Applies the elimination algorithm of 4.7 |
| 1311 | * |
| 1312 | * Returns 0 in case of error, 1 in case of success. |
| 1313 | */ |
| 1314 | static int |
| 1315 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
| 1316 | const xmlChar *URL ATTRIBUTE_UNUSED, |
| 1317 | xmlNodePtr target, const xmlChar *name) { |
| 1318 | int found = 0; |
| 1319 | xmlNodePtr tmp, tmp2; |
| 1320 | xmlChar *name2; |
| 1321 | |
| 1322 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1323 | if (name == NULL) |
| 1324 | xmlGenericError(xmlGenericErrorContext, |
| 1325 | "Elimination of <include> start from %s\n", URL); |
| 1326 | else |
| 1327 | xmlGenericError(xmlGenericErrorContext, |
| 1328 | "Elimination of <include> define %s from %s\n", name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1329 | #endif |
| 1330 | tmp = target; |
| 1331 | while (tmp != NULL) { |
| 1332 | tmp2 = tmp->next; |
| 1333 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1334 | found = 1; |
| 1335 | xmlUnlinkNode(tmp); |
| 1336 | xmlFreeNode(tmp); |
| 1337 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1338 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1339 | xmlRelaxNGNormExtSpace(name2); |
| 1340 | if (name2 != NULL) { |
| 1341 | if (xmlStrEqual(name, name2)) { |
| 1342 | found = 1; |
| 1343 | xmlUnlinkNode(tmp); |
| 1344 | xmlFreeNode(tmp); |
| 1345 | } |
| 1346 | xmlFree(name2); |
| 1347 | } |
| 1348 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1349 | xmlChar *href = NULL; |
| 1350 | xmlRelaxNGDocumentPtr inc = tmp->_private; |
| 1351 | |
| 1352 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1353 | (inc->doc->children != NULL)) { |
| 1354 | |
| 1355 | if (xmlStrEqual(inc->doc->children->name, BAD_CAST "grammar")) { |
| 1356 | #ifdef DEBUG_INCLUDE |
| 1357 | href = xmlGetProp(tmp, BAD_CAST "href"); |
| 1358 | #endif |
| 1359 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1360 | inc->doc->children->children, name) == 1) { |
| 1361 | found = 1; |
| 1362 | } |
| 1363 | if (href != NULL) |
| 1364 | xmlFree(href); |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | tmp = tmp2; |
| 1369 | } |
| 1370 | return(found); |
| 1371 | } |
| 1372 | |
| 1373 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1374 | * xmlRelaxNGLoadInclude: |
| 1375 | * @ctxt: the parser context |
| 1376 | * @URL: the normalized URL |
| 1377 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1378 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1379 | * |
| 1380 | * First lookup if the document is already loaded into the parser context, |
| 1381 | * check against recursion. If not found the resource is loaded and |
| 1382 | * the content is preprocessed before being returned back to the caller. |
| 1383 | * |
| 1384 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1385 | */ |
| 1386 | static xmlRelaxNGIncludePtr |
| 1387 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1388 | xmlNodePtr node, const xmlChar *ns) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1389 | xmlRelaxNGIncludePtr ret = NULL; |
| 1390 | xmlDocPtr doc; |
| 1391 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1392 | xmlNodePtr root, cur; |
| 1393 | |
| 1394 | #ifdef DEBUG_INCLUDE |
| 1395 | xmlGenericError(xmlGenericErrorContext, |
| 1396 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
| 1397 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1398 | |
| 1399 | /* |
| 1400 | * check against recursion in the stack |
| 1401 | */ |
| 1402 | for (i = 0;i < ctxt->incNr;i++) { |
| 1403 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1404 | if (ctxt->error != NULL) |
| 1405 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1406 | "Detected an Include recursion for %s\n", |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1407 | URL); |
| 1408 | ctxt->nbErrors++; |
| 1409 | return(NULL); |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1414 | * load the document |
| 1415 | */ |
| 1416 | doc = xmlParseFile((const char *) URL); |
| 1417 | if (doc == NULL) { |
| 1418 | if (ctxt->error != NULL) |
| 1419 | ctxt->error(ctxt->userData, |
| 1420 | "xmlRelaxNG: could not load %s\n", URL); |
| 1421 | ctxt->nbErrors++; |
| 1422 | return (NULL); |
| 1423 | } |
| 1424 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1425 | #ifdef DEBUG_INCLUDE |
| 1426 | xmlGenericError(xmlGenericErrorContext, |
| 1427 | "Parsed %s Okay\n", URL); |
| 1428 | #endif |
| 1429 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1430 | /* |
| 1431 | * Allocate the document structures and register it first. |
| 1432 | */ |
| 1433 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1434 | if (ret == NULL) { |
| 1435 | if (ctxt->error != NULL) |
| 1436 | ctxt->error(ctxt->userData, |
| 1437 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1438 | ctxt->nbErrors++; |
| 1439 | xmlFreeDoc(doc); |
| 1440 | return (NULL); |
| 1441 | } |
| 1442 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1443 | ret->doc = doc; |
| 1444 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1445 | ret->next = ctxt->includes; |
| 1446 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1447 | |
| 1448 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1449 | * transmit the ns if needed |
| 1450 | */ |
| 1451 | if (ns != NULL) { |
| 1452 | root = xmlDocGetRootElement(doc); |
| 1453 | if (root != NULL) { |
| 1454 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1455 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1461 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1462 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1463 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1464 | |
| 1465 | /* |
| 1466 | * Some preprocessing of the document content, this include recursing |
| 1467 | * in the include stack. |
| 1468 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1469 | #ifdef DEBUG_INCLUDE |
| 1470 | xmlGenericError(xmlGenericErrorContext, |
| 1471 | "cleanup of %s\n", URL); |
| 1472 | #endif |
| 1473 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1474 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1475 | if (doc == NULL) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1476 | ctxt->inc = NULL; |
| 1477 | return(NULL); |
| 1478 | } |
| 1479 | |
| 1480 | /* |
| 1481 | * Pop up the include from the stack |
| 1482 | */ |
| 1483 | xmlRelaxNGIncludePop(ctxt); |
| 1484 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1485 | #ifdef DEBUG_INCLUDE |
| 1486 | xmlGenericError(xmlGenericErrorContext, |
| 1487 | "Checking of %s\n", URL); |
| 1488 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1489 | /* |
| 1490 | * Check that the top element is a grammar |
| 1491 | */ |
| 1492 | root = xmlDocGetRootElement(doc); |
| 1493 | if (root == NULL) { |
| 1494 | if (ctxt->error != NULL) |
| 1495 | ctxt->error(ctxt->userData, |
| 1496 | "xmlRelaxNG: included document is empty %s\n", URL); |
| 1497 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1498 | return (NULL); |
| 1499 | } |
| 1500 | if (!IS_RELAXNG(root, "grammar")) { |
| 1501 | if (ctxt->error != NULL) |
| 1502 | ctxt->error(ctxt->userData, |
| 1503 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1504 | URL); |
| 1505 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1506 | return (NULL); |
| 1507 | } |
| 1508 | |
| 1509 | /* |
| 1510 | * Elimination of redefined rules in the include. |
| 1511 | */ |
| 1512 | cur = node->children; |
| 1513 | while (cur != NULL) { |
| 1514 | if (IS_RELAXNG(cur, "start")) { |
| 1515 | int found = 0; |
| 1516 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1517 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1518 | if (!found) { |
| 1519 | if (ctxt->error != NULL) |
| 1520 | ctxt->error(ctxt->userData, |
| 1521 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1522 | URL); |
| 1523 | ctxt->nbErrors++; |
| 1524 | } |
| 1525 | } else if (IS_RELAXNG(cur, "define")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1526 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1527 | |
| 1528 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1529 | if (name == NULL) { |
| 1530 | if (ctxt->error != NULL) |
| 1531 | ctxt->error(ctxt->userData, |
| 1532 | "xmlRelaxNG: include %s has define without name\n", |
| 1533 | URL); |
| 1534 | ctxt->nbErrors++; |
| 1535 | } else { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1536 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1537 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1538 | xmlRelaxNGNormExtSpace(name); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1539 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1540 | root->children, name); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1541 | if (!found) { |
| 1542 | if (ctxt->error != NULL) |
| 1543 | ctxt->error(ctxt->userData, |
| 1544 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1545 | URL, name); |
| 1546 | ctxt->nbErrors++; |
| 1547 | } |
| 1548 | xmlFree(name); |
| 1549 | } |
| 1550 | } |
| 1551 | cur = cur->next; |
| 1552 | } |
| 1553 | |
| 1554 | |
| 1555 | return(ret); |
| 1556 | } |
| 1557 | |
| 1558 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1559 | * xmlRelaxNGValidErrorPush: |
| 1560 | * @ctxt: the validation context |
| 1561 | * @err: the error code |
| 1562 | * @arg1: the first string argument |
| 1563 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1564 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1565 | * |
| 1566 | * Pushes a new error on top of the error stack |
| 1567 | * |
| 1568 | * Returns 0 in case of error, the index in the stack otherwise |
| 1569 | */ |
| 1570 | static int |
| 1571 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1572 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1573 | { |
| 1574 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1575 | #ifdef DEBUG_ERROR |
| 1576 | xmlGenericError(xmlGenericErrorContext, |
| 1577 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
| 1578 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1579 | if (ctxt->errTab == NULL) { |
| 1580 | ctxt->errMax = 8; |
| 1581 | ctxt->errNr = 0; |
| 1582 | ctxt->errTab = (xmlRelaxNGValidErrorPtr) xmlMalloc( |
| 1583 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
| 1584 | if (ctxt->errTab == NULL) { |
| 1585 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1586 | return (0); |
| 1587 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1588 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1589 | } |
| 1590 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1591 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1592 | ctxt->errTab = |
| 1593 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1594 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1595 | if (ctxt->errTab == NULL) { |
| 1596 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1597 | return (0); |
| 1598 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1599 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1600 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1601 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1602 | (ctxt->err->node == ctxt->state->node) && |
| 1603 | (ctxt->err->err == err)) |
| 1604 | return(ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1605 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1606 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1607 | if (dup) { |
| 1608 | cur->arg1 = xmlStrdup(arg1); |
| 1609 | cur->arg2 = xmlStrdup(arg2); |
| 1610 | cur->flags = ERROR_IS_DUP; |
| 1611 | } else { |
| 1612 | cur->arg1 = arg1; |
| 1613 | cur->arg2 = arg2; |
| 1614 | cur->flags = 0; |
| 1615 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1616 | if (ctxt->state != NULL) { |
| 1617 | cur->node = ctxt->state->node; |
| 1618 | cur->seq = ctxt->state->seq; |
| 1619 | } else { |
| 1620 | cur->node = NULL; |
| 1621 | cur->seq = NULL; |
| 1622 | } |
| 1623 | ctxt->err = cur; |
| 1624 | return (ctxt->errNr++); |
| 1625 | } |
| 1626 | |
| 1627 | /** |
| 1628 | * xmlRelaxNGValidErrorPop: |
| 1629 | * @ctxt: the validation context |
| 1630 | * |
| 1631 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1632 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1633 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1634 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1635 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1636 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1637 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1638 | if (ctxt->errNr <= 0) { |
| 1639 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1640 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1641 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1642 | ctxt->errNr--; |
| 1643 | if (ctxt->errNr > 0) |
| 1644 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1645 | else |
| 1646 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1647 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1648 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1649 | if (cur->arg1 != NULL) |
| 1650 | xmlFree((xmlChar *)cur->arg1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1651 | cur->arg1 = NULL; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1652 | if (cur->arg2 != NULL) |
| 1653 | xmlFree((xmlChar *)cur->arg2); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1654 | cur->arg2 = NULL; |
| 1655 | cur->flags = 0; |
| 1656 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1659 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1660 | * xmlRelaxNGDocumentPush: |
| 1661 | * @ctxt: the parser context |
| 1662 | * @value: the element doc |
| 1663 | * |
| 1664 | * Pushes a new doc on top of the doc stack |
| 1665 | * |
| 1666 | * Returns 0 in case of error, the index in the stack otherwise |
| 1667 | */ |
| 1668 | static int |
| 1669 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1670 | xmlRelaxNGDocumentPtr value) |
| 1671 | { |
| 1672 | if (ctxt->docTab == NULL) { |
| 1673 | ctxt->docMax = 4; |
| 1674 | ctxt->docNr = 0; |
| 1675 | ctxt->docTab = (xmlRelaxNGDocumentPtr *) xmlMalloc( |
| 1676 | ctxt->docMax * sizeof(ctxt->docTab[0])); |
| 1677 | if (ctxt->docTab == NULL) { |
| 1678 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1679 | return (0); |
| 1680 | } |
| 1681 | } |
| 1682 | if (ctxt->docNr >= ctxt->docMax) { |
| 1683 | ctxt->docMax *= 2; |
| 1684 | ctxt->docTab = |
| 1685 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
| 1686 | ctxt->docMax * |
| 1687 | sizeof(ctxt->docTab[0])); |
| 1688 | if (ctxt->docTab == NULL) { |
| 1689 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1690 | return (0); |
| 1691 | } |
| 1692 | } |
| 1693 | ctxt->docTab[ctxt->docNr] = value; |
| 1694 | ctxt->doc = value; |
| 1695 | return (ctxt->docNr++); |
| 1696 | } |
| 1697 | |
| 1698 | /** |
| 1699 | * xmlRelaxNGDocumentPop: |
| 1700 | * @ctxt: the parser context |
| 1701 | * |
| 1702 | * Pops the top doc from the doc stack |
| 1703 | * |
| 1704 | * Returns the doc just removed |
| 1705 | */ |
| 1706 | static xmlRelaxNGDocumentPtr |
| 1707 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1708 | { |
| 1709 | xmlRelaxNGDocumentPtr ret; |
| 1710 | |
| 1711 | if (ctxt->docNr <= 0) |
| 1712 | return (0); |
| 1713 | ctxt->docNr--; |
| 1714 | if (ctxt->docNr > 0) |
| 1715 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1716 | else |
| 1717 | ctxt->doc = NULL; |
| 1718 | ret = ctxt->docTab[ctxt->docNr]; |
| 1719 | ctxt->docTab[ctxt->docNr] = 0; |
| 1720 | return (ret); |
| 1721 | } |
| 1722 | |
| 1723 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1724 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1725 | * @ctxt: the parser context |
| 1726 | * @URL: the normalized URL |
| 1727 | * @ns: the inherited ns if any |
| 1728 | * |
| 1729 | * First lookup if the document is already loaded into the parser context, |
| 1730 | * check against recursion. If not found the resource is loaded and |
| 1731 | * the content is preprocessed before being returned back to the caller. |
| 1732 | * |
| 1733 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1734 | */ |
| 1735 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1736 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1737 | const xmlChar *ns) { |
| 1738 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1739 | xmlDocPtr doc; |
| 1740 | xmlNodePtr root; |
| 1741 | int i; |
| 1742 | |
| 1743 | /* |
| 1744 | * check against recursion in the stack |
| 1745 | */ |
| 1746 | for (i = 0;i < ctxt->docNr;i++) { |
| 1747 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1748 | if (ctxt->error != NULL) |
| 1749 | ctxt->error(ctxt->userData, |
| 1750 | "Detected an externalRef recursion for %s\n", |
| 1751 | URL); |
| 1752 | ctxt->nbErrors++; |
| 1753 | return(NULL); |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1758 | * load the document |
| 1759 | */ |
| 1760 | doc = xmlParseFile((const char *) URL); |
| 1761 | if (doc == NULL) { |
| 1762 | if (ctxt->error != NULL) |
| 1763 | ctxt->error(ctxt->userData, |
| 1764 | "xmlRelaxNG: could not load %s\n", URL); |
| 1765 | ctxt->nbErrors++; |
| 1766 | return (NULL); |
| 1767 | } |
| 1768 | |
| 1769 | /* |
| 1770 | * Allocate the document structures and register it first. |
| 1771 | */ |
| 1772 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1773 | if (ret == NULL) { |
| 1774 | if (ctxt->error != NULL) |
| 1775 | ctxt->error(ctxt->userData, |
| 1776 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1777 | ctxt->nbErrors++; |
| 1778 | xmlFreeDoc(doc); |
| 1779 | return (NULL); |
| 1780 | } |
| 1781 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1782 | ret->doc = doc; |
| 1783 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1784 | ret->next = ctxt->documents; |
| 1785 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1786 | |
| 1787 | /* |
| 1788 | * transmit the ns if needed |
| 1789 | */ |
| 1790 | if (ns != NULL) { |
| 1791 | root = xmlDocGetRootElement(doc); |
| 1792 | if (root != NULL) { |
| 1793 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1794 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1795 | } |
| 1796 | } |
| 1797 | } |
| 1798 | |
| 1799 | /* |
| 1800 | * push it on the stack and register it in the hash table |
| 1801 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1802 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1803 | |
| 1804 | /* |
| 1805 | * Some preprocessing of the document content |
| 1806 | */ |
| 1807 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1808 | if (doc == NULL) { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1809 | ctxt->doc = NULL; |
| 1810 | return(NULL); |
| 1811 | } |
| 1812 | |
| 1813 | xmlRelaxNGDocumentPop(ctxt); |
| 1814 | |
| 1815 | return(ret); |
| 1816 | } |
| 1817 | |
| 1818 | /************************************************************************ |
| 1819 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1820 | * Error functions * |
| 1821 | * * |
| 1822 | ************************************************************************/ |
| 1823 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1824 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 1825 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 1826 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 1827 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 1828 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1829 | |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1830 | static const char * |
| 1831 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) { |
| 1832 | if (def == NULL) |
| 1833 | return("none"); |
| 1834 | switch(def->type) { |
| 1835 | case XML_RELAXNG_EMPTY: return("empty"); |
| 1836 | case XML_RELAXNG_NOT_ALLOWED: return("notAllowed"); |
| 1837 | case XML_RELAXNG_EXCEPT: return("except"); |
| 1838 | case XML_RELAXNG_TEXT: return("text"); |
| 1839 | case XML_RELAXNG_ELEMENT: return("element"); |
| 1840 | case XML_RELAXNG_DATATYPE: return("datatype"); |
| 1841 | case XML_RELAXNG_VALUE: return("value"); |
| 1842 | case XML_RELAXNG_LIST: return("list"); |
| 1843 | case XML_RELAXNG_ATTRIBUTE: return("attribute"); |
| 1844 | case XML_RELAXNG_DEF: return("def"); |
| 1845 | case XML_RELAXNG_REF: return("ref"); |
| 1846 | case XML_RELAXNG_EXTERNALREF: return("externalRef"); |
| 1847 | case XML_RELAXNG_PARENTREF: return("parentRef"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1848 | case XML_RELAXNG_OPTIONAL: return("optional"); |
| 1849 | case XML_RELAXNG_ZEROORMORE: return("zeroOrMore"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1850 | case XML_RELAXNG_ONEORMORE: return("oneOrMore"); |
| 1851 | case XML_RELAXNG_CHOICE: return("choice"); |
| 1852 | case XML_RELAXNG_GROUP: return("group"); |
| 1853 | case XML_RELAXNG_INTERLEAVE: return("interleave"); |
| 1854 | case XML_RELAXNG_START: return("start"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1855 | case XML_RELAXNG_NOOP: return("noop"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1856 | case XML_RELAXNG_PARAM: return("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1857 | } |
| 1858 | return("unknown"); |
| 1859 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1860 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1861 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1862 | * xmlRelaxNGGetErrorString: |
| 1863 | * @err: the error code |
| 1864 | * @arg1: the first string argument |
| 1865 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1866 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1867 | * computes a formatted error string for the given error code and args |
| 1868 | * |
| 1869 | * Returns the error string, it must be deallocated by the caller |
| 1870 | */ |
| 1871 | static xmlChar * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1872 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar *arg1, |
| 1873 | const xmlChar *arg2) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1874 | char msg[1000]; |
| 1875 | |
| 1876 | if (arg1 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1877 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1878 | if (arg2 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1879 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1880 | |
| 1881 | msg[0] = 0; |
| 1882 | switch (err) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1883 | case XML_RELAXNG_OK: |
| 1884 | return(NULL); |
| 1885 | case XML_RELAXNG_ERR_MEMORY: |
| 1886 | return(xmlCharStrdup("out of memory")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1887 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1888 | snprintf(msg, 1000, "failed to validate type %s", arg1); |
| 1889 | break; |
| 1890 | case XML_RELAXNG_ERR_TYPEVAL: |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 1891 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'", arg1, arg2); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1892 | break; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1893 | case XML_RELAXNG_ERR_DUPID: |
| 1894 | snprintf(msg, 1000, "ID %s redefined", arg1); |
| 1895 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1896 | case XML_RELAXNG_ERR_TYPECMP: |
| 1897 | snprintf(msg, 1000, "failed to compare type %s", arg1); |
| 1898 | break; |
| 1899 | case XML_RELAXNG_ERR_NOSTATE: |
| 1900 | return(xmlCharStrdup("Internal error: no state")); |
| 1901 | case XML_RELAXNG_ERR_NODEFINE: |
| 1902 | return(xmlCharStrdup("Internal error: no define")); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1903 | case XML_RELAXNG_ERR_INTERNAL: |
| 1904 | snprintf(msg, 1000, "Internal error: %s", arg1); |
| 1905 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1906 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 1907 | snprintf(msg, 1000, "Extra data in list: %s", arg1); |
| 1908 | break; |
| 1909 | case XML_RELAXNG_ERR_INTERNODATA: |
| 1910 | return(xmlCharStrdup("Internal: interleave block has no data")); |
| 1911 | case XML_RELAXNG_ERR_INTERSEQ: |
| 1912 | return(xmlCharStrdup("Invalid sequence in interleave")); |
| 1913 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 1914 | snprintf(msg, 1000, "Extra element %s in interleave", arg1); |
| 1915 | break; |
| 1916 | case XML_RELAXNG_ERR_ELEMNAME: |
| 1917 | snprintf(msg, 1000, "Expecting element %s, got %s", arg1, arg2); |
| 1918 | break; |
| 1919 | case XML_RELAXNG_ERR_ELEMNONS: |
| 1920 | snprintf(msg, 1000, "Expecting a namespace for element %s", arg1); |
| 1921 | break; |
| 1922 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 1923 | snprintf(msg, 1000, "Element %s has wrong namespace: expecting %s", |
| 1924 | arg1, arg2); |
| 1925 | break; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 1926 | case XML_RELAXNG_ERR_ELEMWRONG: |
| 1927 | snprintf(msg, 1000, "Did not expect element %s there", |
| 1928 | arg1); |
| 1929 | break; |
| 1930 | case XML_RELAXNG_ERR_TEXTWRONG: |
| 1931 | snprintf(msg, 1000, "Did not expect text in element %s content", |
| 1932 | arg1); |
| 1933 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1934 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 1935 | snprintf(msg, 1000, "Expecting no namespace for element %s", arg1); |
| 1936 | break; |
| 1937 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 1938 | snprintf(msg, 1000, "Expecting element %s to be empty", arg1); |
| 1939 | break; |
| 1940 | case XML_RELAXNG_ERR_NOELEM: |
| 1941 | snprintf(msg, 1000, "Expecting an element %s, got nothing", arg1); |
| 1942 | break; |
| 1943 | case XML_RELAXNG_ERR_NOTELEM: |
| 1944 | return(xmlCharStrdup("Expecting an element got text")); |
| 1945 | case XML_RELAXNG_ERR_ATTRVALID: |
| 1946 | snprintf(msg, 1000, "Element %s failed to validate attributes", |
| 1947 | arg1); |
| 1948 | break; |
| 1949 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 1950 | snprintf(msg, 1000, "Element %s failed to validate content", |
| 1951 | arg1); |
| 1952 | break; |
| 1953 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 1954 | snprintf(msg, 1000, "Element %s has extra content: %s", |
| 1955 | arg1, arg2); |
| 1956 | break; |
| 1957 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 1958 | snprintf(msg, 1000, "Invalid attribute %s for element %s", |
| 1959 | arg1, arg2); |
| 1960 | break; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 1961 | case XML_RELAXNG_ERR_LACKDATA: |
| 1962 | snprintf(msg, 1000, "Datatype element %s contains no data", |
| 1963 | arg1); |
| 1964 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1965 | case XML_RELAXNG_ERR_DATAELEM: |
| 1966 | snprintf(msg, 1000, "Datatype element %s has child elements", |
| 1967 | arg1); |
| 1968 | break; |
| 1969 | case XML_RELAXNG_ERR_VALELEM: |
| 1970 | snprintf(msg, 1000, "Value element %s has child elements", |
| 1971 | arg1); |
| 1972 | break; |
| 1973 | case XML_RELAXNG_ERR_LISTELEM: |
| 1974 | snprintf(msg, 1000, "List element %s has child elements", |
| 1975 | arg1); |
| 1976 | break; |
| 1977 | case XML_RELAXNG_ERR_DATATYPE: |
| 1978 | snprintf(msg, 1000, "Error validating datatype %s", |
| 1979 | arg1); |
| 1980 | break; |
| 1981 | case XML_RELAXNG_ERR_VALUE: |
| 1982 | snprintf(msg, 1000, "Error validating value %s", |
| 1983 | arg1); |
| 1984 | break; |
| 1985 | case XML_RELAXNG_ERR_LIST: |
| 1986 | return(xmlCharStrdup("Error validating list")); |
| 1987 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 1988 | return(xmlCharStrdup("No top grammar defined")); |
| 1989 | case XML_RELAXNG_ERR_EXTRADATA: |
| 1990 | return(xmlCharStrdup("Extra data in the document")); |
| 1991 | default: |
| 1992 | TODO |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1993 | } |
| 1994 | if (msg[0] == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1995 | snprintf(msg, 1000, "Unknown error code %d", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1996 | } |
| 1997 | msg[1000] = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1998 | return(xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | /** |
| 2002 | * xmlRelaxNGValidErrorContext: |
| 2003 | * @ctxt: the validation context |
| 2004 | * @node: the node |
| 2005 | * @child: the node child generating the problem. |
| 2006 | * |
| 2007 | * Dump informations about the kocation of the error in the instance |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2008 | */ |
| 2009 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2010 | xmlRelaxNGValidErrorContext(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, |
| 2011 | xmlNodePtr child) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2012 | { |
| 2013 | int line = 0; |
| 2014 | const xmlChar *file = NULL; |
| 2015 | const xmlChar *name = NULL; |
| 2016 | const char *type = "error"; |
| 2017 | |
| 2018 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 2019 | return; |
| 2020 | |
| 2021 | if (child != NULL) |
| 2022 | node = child; |
| 2023 | |
| 2024 | if (node != NULL) { |
| 2025 | if ((node->type == XML_DOCUMENT_NODE) || |
| 2026 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
| 2027 | xmlDocPtr doc = (xmlDocPtr) node; |
| 2028 | |
| 2029 | file = doc->URL; |
| 2030 | } else { |
| 2031 | /* |
| 2032 | * Try to find contextual informations to report |
| 2033 | */ |
| 2034 | if (node->type == XML_ELEMENT_NODE) { |
| 2035 | line = (int) node->content; |
| 2036 | } else if ((node->prev != NULL) && |
| 2037 | (node->prev->type == XML_ELEMENT_NODE)) { |
| 2038 | line = (int) node->prev->content; |
| 2039 | } else if ((node->parent != NULL) && |
| 2040 | (node->parent->type == XML_ELEMENT_NODE)) { |
| 2041 | line = (int) node->parent->content; |
| 2042 | } |
| 2043 | if ((node->doc != NULL) && (node->doc->URL != NULL)) |
| 2044 | file = node->doc->URL; |
| 2045 | if (node->name != NULL) |
| 2046 | name = node->name; |
| 2047 | } |
| 2048 | } |
| 2049 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2050 | type = "RNG validity error"; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2051 | |
| 2052 | if ((file != NULL) && (line != 0) && (name != NULL)) |
| 2053 | ctxt->error(ctxt->userData, "%s: file %s line %d element %s\n", |
| 2054 | type, file, line, name); |
| 2055 | else if ((file != NULL) && (name != NULL)) |
| 2056 | ctxt->error(ctxt->userData, "%s: file %s element %s\n", |
| 2057 | type, file, name); |
| 2058 | else if ((file != NULL) && (line != 0)) |
| 2059 | ctxt->error(ctxt->userData, "%s: file %s line %d\n", type, file, line); |
| 2060 | else if (file != NULL) |
| 2061 | ctxt->error(ctxt->userData, "%s: file %s\n", type, file); |
| 2062 | else if (name != NULL) |
| 2063 | ctxt->error(ctxt->userData, "%s: element %s\n", type, name); |
| 2064 | else |
| 2065 | ctxt->error(ctxt->userData, "%s\n", type); |
| 2066 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2067 | |
| 2068 | /** |
| 2069 | * xmlRelaxNGShowValidError: |
| 2070 | * @ctxt: the validation context |
| 2071 | * @err: the error number |
| 2072 | * @node: the node |
| 2073 | * @child: the node child generating the problem. |
| 2074 | * @arg1: the first argument |
| 2075 | * @arg2: the second argument |
| 2076 | * |
| 2077 | * Show a validation error. |
| 2078 | */ |
| 2079 | static void |
| 2080 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
| 2081 | xmlNodePtr node, xmlNodePtr child, |
| 2082 | const xmlChar *arg1, const xmlChar *arg2) |
| 2083 | { |
| 2084 | xmlChar *msg; |
| 2085 | |
| 2086 | if (ctxt->error == NULL) |
| 2087 | return; |
| 2088 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2089 | #ifdef DEBUG_ERROR |
| 2090 | xmlGenericError(xmlGenericErrorContext, |
| 2091 | "Show error %d\n", err); |
| 2092 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2093 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2094 | if (msg == NULL) |
| 2095 | return; |
| 2096 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2097 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 2098 | ctxt->errNo = err; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2099 | xmlRelaxNGValidErrorContext(ctxt, node, child); |
| 2100 | ctxt->error(ctxt->userData, "%s\n", msg); |
| 2101 | xmlFree(msg); |
| 2102 | } |
| 2103 | |
| 2104 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2105 | * xmlRelaxNGPopErrors: |
| 2106 | * @ctxt: the validation context |
| 2107 | * @level: the error level in the stack |
| 2108 | * |
| 2109 | * pop and discard all errors until the given level is reached |
| 2110 | */ |
| 2111 | static void |
| 2112 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) { |
| 2113 | int i; |
| 2114 | xmlRelaxNGValidErrorPtr err; |
| 2115 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2116 | #ifdef DEBUG_ERROR |
| 2117 | xmlGenericError(xmlGenericErrorContext, |
| 2118 | "Pop errors till level %d\n", level); |
| 2119 | #endif |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2120 | for (i = level;i < ctxt->errNr;i++) { |
| 2121 | err = &ctxt->errTab[i]; |
| 2122 | if (err->flags & ERROR_IS_DUP) { |
| 2123 | if (err->arg1 != NULL) |
| 2124 | xmlFree((xmlChar *)err->arg1); |
| 2125 | err->arg1 = NULL; |
| 2126 | if (err->arg2 != NULL) |
| 2127 | xmlFree((xmlChar *)err->arg2); |
| 2128 | err->arg2 = NULL; |
| 2129 | err->flags = 0; |
| 2130 | } |
| 2131 | } |
| 2132 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2133 | if (ctxt->errNr <= 0) |
| 2134 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2135 | } |
| 2136 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2137 | * xmlRelaxNGDumpValidError: |
| 2138 | * @ctxt: the validation context |
| 2139 | * |
| 2140 | * Show all validation error over a given index. |
| 2141 | */ |
| 2142 | static void |
| 2143 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2144 | int i, j, k; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2145 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2146 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2147 | #ifdef DEBUG_ERROR |
| 2148 | xmlGenericError(xmlGenericErrorContext, |
| 2149 | "Dumping error stack %d errors\n", ctxt->errNr); |
| 2150 | #endif |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2151 | for (i = 0, k = 0;i < ctxt->errNr;i++) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2152 | err = &ctxt->errTab[i]; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2153 | if (k < MAX_ERROR) { |
| 2154 | for (j = 0;j < i;j++) { |
| 2155 | dup = &ctxt->errTab[j]; |
| 2156 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2157 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2158 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2159 | goto skip; |
| 2160 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2161 | } |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2162 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2163 | err->arg1, err->arg2); |
| 2164 | k++; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2165 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2166 | skip: |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2167 | if (err->flags & ERROR_IS_DUP) { |
| 2168 | if (err->arg1 != NULL) |
| 2169 | xmlFree((xmlChar *)err->arg1); |
| 2170 | err->arg1 = NULL; |
| 2171 | if (err->arg2 != NULL) |
| 2172 | xmlFree((xmlChar *)err->arg2); |
| 2173 | err->arg2 = NULL; |
| 2174 | err->flags = 0; |
| 2175 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2176 | } |
| 2177 | ctxt->errNr = 0; |
| 2178 | } |
| 2179 | /** |
| 2180 | * xmlRelaxNGAddValidError: |
| 2181 | * @ctxt: the validation context |
| 2182 | * @err: the error number |
| 2183 | * @arg1: the first argument |
| 2184 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2185 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2186 | * |
| 2187 | * Register a validation error, either generating it if it's sure |
| 2188 | * or stacking it for later handling if unsure. |
| 2189 | */ |
| 2190 | static void |
| 2191 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2192 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2193 | { |
| 2194 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 2195 | return; |
| 2196 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2197 | #ifdef DEBUG_ERROR |
| 2198 | xmlGenericError(xmlGenericErrorContext, |
| 2199 | "Adding error %d\n", err); |
| 2200 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2201 | /* |
| 2202 | * generate the error directly |
| 2203 | */ |
| 2204 | if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) { |
| 2205 | xmlNodePtr node, seq; |
| 2206 | /* |
| 2207 | * Flush first any stacked error which might be the |
| 2208 | * real cause of the problem. |
| 2209 | */ |
| 2210 | if (ctxt->errNr != 0) |
| 2211 | xmlRelaxNGDumpValidError(ctxt); |
| 2212 | if (ctxt->state != NULL) { |
| 2213 | node = ctxt->state->node; |
| 2214 | seq = ctxt->state->seq; |
| 2215 | } else { |
| 2216 | node = seq = NULL; |
| 2217 | } |
| 2218 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
| 2219 | } |
| 2220 | /* |
| 2221 | * Stack the error for later processing if needed |
| 2222 | */ |
| 2223 | else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2224 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2225 | } |
| 2226 | } |
| 2227 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2228 | |
| 2229 | /************************************************************************ |
| 2230 | * * |
| 2231 | * Type library hooks * |
| 2232 | * * |
| 2233 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2234 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
| 2235 | const xmlChar *str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2236 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2237 | /** |
| 2238 | * xmlRelaxNGSchemaTypeHave: |
| 2239 | * @data: data needed for the library |
| 2240 | * @type: the type name |
| 2241 | * |
| 2242 | * Check if the given type is provided by |
| 2243 | * the W3C XMLSchema Datatype library. |
| 2244 | * |
| 2245 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2246 | */ |
| 2247 | static int |
| 2248 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2249 | const xmlChar *type) { |
| 2250 | xmlSchemaTypePtr typ; |
| 2251 | |
| 2252 | if (type == NULL) |
| 2253 | return(-1); |
| 2254 | typ = xmlSchemaGetPredefinedType(type, |
| 2255 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2256 | if (typ == NULL) |
| 2257 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2258 | return(1); |
| 2259 | } |
| 2260 | |
| 2261 | /** |
| 2262 | * xmlRelaxNGSchemaTypeCheck: |
| 2263 | * @data: data needed for the library |
| 2264 | * @type: the type name |
| 2265 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2266 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2267 | * |
| 2268 | * Check if the given type and value are validated by |
| 2269 | * the W3C XMLSchema Datatype library. |
| 2270 | * |
| 2271 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2272 | */ |
| 2273 | static int |
| 2274 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2275 | const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2276 | const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2277 | void **result, |
| 2278 | xmlNodePtr node) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2279 | xmlSchemaTypePtr typ; |
| 2280 | int ret; |
| 2281 | |
| 2282 | /* |
| 2283 | * TODO: the type should be cached ab provided back, interface subject |
| 2284 | * to changes. |
| 2285 | * TODO: handle facets, may require an additional interface and keep |
| 2286 | * the value returned from the validation. |
| 2287 | */ |
| 2288 | if ((type == NULL) || (value == NULL)) |
| 2289 | return(-1); |
| 2290 | typ = xmlSchemaGetPredefinedType(type, |
| 2291 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2292 | if (typ == NULL) |
| 2293 | return(-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2294 | ret = xmlSchemaValPredefTypeNode(typ, value, |
| 2295 | (xmlSchemaValPtr *) result, node); |
| 2296 | if (ret == 2) /* special ID error code */ |
| 2297 | return(2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2298 | if (ret == 0) |
| 2299 | return(1); |
| 2300 | if (ret > 0) |
| 2301 | return(0); |
| 2302 | return(-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2306 | * xmlRelaxNGSchemaFacetCheck: |
| 2307 | * @data: data needed for the library |
| 2308 | * @type: the type name |
| 2309 | * @facet: the facet name |
| 2310 | * @val: the facet value |
| 2311 | * @strval: the string value |
| 2312 | * @value: the value to check |
| 2313 | * |
| 2314 | * Function provided by a type library to check a value facet |
| 2315 | * |
| 2316 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2317 | */ |
| 2318 | static int |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2319 | xmlRelaxNGSchemaFacetCheck (void *data ATTRIBUTE_UNUSED, const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2320 | const xmlChar *facetname, const xmlChar *val, |
| 2321 | const xmlChar *strval, void *value) { |
| 2322 | xmlSchemaFacetPtr facet; |
| 2323 | xmlSchemaTypePtr typ; |
| 2324 | int ret; |
| 2325 | |
| 2326 | if ((type == NULL) || (strval == NULL)) |
| 2327 | return(-1); |
| 2328 | typ = xmlSchemaGetPredefinedType(type, |
| 2329 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2330 | if (typ == NULL) |
| 2331 | return(-1); |
| 2332 | |
| 2333 | facet = xmlSchemaNewFacet(); |
| 2334 | if (facet == NULL) |
| 2335 | return(-1); |
| 2336 | |
| 2337 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
| 2338 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
| 2339 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
| 2340 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
| 2341 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
| 2342 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
| 2343 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
| 2344 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
| 2345 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
| 2346 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
| 2347 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
| 2348 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
| 2349 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
| 2350 | facet->type = XML_SCHEMA_FACET_PATTERN; |
| 2351 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
| 2352 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
| 2353 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
| 2354 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
| 2355 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
| 2356 | facet->type = XML_SCHEMA_FACET_LENGTH; |
| 2357 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
| 2358 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2359 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2360 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2361 | } else { |
| 2362 | xmlSchemaFreeFacet(facet); |
| 2363 | return(-1); |
| 2364 | } |
| 2365 | facet->value = xmlStrdup(val); |
| 2366 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2367 | if (ret != 0) { |
| 2368 | xmlSchemaFreeFacet(facet); |
| 2369 | return(-1); |
| 2370 | } |
| 2371 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2372 | xmlSchemaFreeFacet(facet); |
| 2373 | if (ret != 0) |
| 2374 | return(-1); |
| 2375 | return(0); |
| 2376 | } |
| 2377 | |
| 2378 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2379 | * xmlRelaxNGSchemaFreeValue: |
| 2380 | * @data: data needed for the library |
| 2381 | * @value: the value to free |
| 2382 | * |
| 2383 | * Function provided by a type library to free a Schemas value |
| 2384 | * |
| 2385 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2386 | */ |
| 2387 | static void |
| 2388 | xmlRelaxNGSchemaFreeValue (void *data ATTRIBUTE_UNUSED, void *value) { |
| 2389 | xmlSchemaFreeValue(value); |
| 2390 | } |
| 2391 | |
| 2392 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2393 | * xmlRelaxNGSchemaTypeCompare: |
| 2394 | * @data: data needed for the library |
| 2395 | * @type: the type name |
| 2396 | * @value1: the first value |
| 2397 | * @value2: the second value |
| 2398 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2399 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2400 | * Datatype library. |
| 2401 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2402 | * Returns 1 if equal, 0 if no and -1 in case of error. |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2403 | */ |
| 2404 | static int |
| 2405 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2406 | const xmlChar *type, |
| 2407 | const xmlChar *value1, |
| 2408 | xmlNodePtr ctxt1, |
| 2409 | void *comp1, |
| 2410 | const xmlChar *value2, |
| 2411 | xmlNodePtr ctxt2) { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2412 | int ret; |
| 2413 | xmlSchemaTypePtr typ; |
| 2414 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2415 | |
| 2416 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
| 2417 | return(-1); |
| 2418 | typ = xmlSchemaGetPredefinedType(type, |
| 2419 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2420 | if (typ == NULL) |
| 2421 | return(-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2422 | if (comp1 == NULL) { |
| 2423 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2424 | if (ret != 0) |
| 2425 | return(-1); |
| 2426 | if (res1 == NULL) |
| 2427 | return(-1); |
| 2428 | } else { |
| 2429 | res1 = (xmlSchemaValPtr) comp1; |
| 2430 | } |
| 2431 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2432 | if (ret != 0) { |
| 2433 | xmlSchemaFreeValue(res1); |
| 2434 | return(-1); |
| 2435 | } |
| 2436 | if (res1 == NULL) { |
| 2437 | xmlSchemaFreeValue(res1); |
| 2438 | return(-1); |
| 2439 | } |
| 2440 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2441 | if (res1 != (xmlSchemaValPtr) comp1) |
| 2442 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2443 | xmlSchemaFreeValue(res2); |
| 2444 | if (ret == -2) |
| 2445 | return(-1); |
| 2446 | if (ret == 0) |
| 2447 | return(1); |
| 2448 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | /** |
| 2452 | * xmlRelaxNGDefaultTypeHave: |
| 2453 | * @data: data needed for the library |
| 2454 | * @type: the type name |
| 2455 | * |
| 2456 | * Check if the given type is provided by |
| 2457 | * the default datatype library. |
| 2458 | * |
| 2459 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2460 | */ |
| 2461 | static int |
| 2462 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar *type) { |
| 2463 | if (type == NULL) |
| 2464 | return(-1); |
| 2465 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2466 | return(1); |
| 2467 | if (xmlStrEqual(type, BAD_CAST "token")) |
| 2468 | return(1); |
| 2469 | return(0); |
| 2470 | } |
| 2471 | |
| 2472 | /** |
| 2473 | * xmlRelaxNGDefaultTypeCheck: |
| 2474 | * @data: data needed for the library |
| 2475 | * @type: the type name |
| 2476 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2477 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2478 | * |
| 2479 | * Check if the given type and value are validated by |
| 2480 | * the default datatype library. |
| 2481 | * |
| 2482 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2483 | */ |
| 2484 | static int |
| 2485 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
| 2486 | const xmlChar *type ATTRIBUTE_UNUSED, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2487 | const xmlChar *value ATTRIBUTE_UNUSED, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2488 | void **result ATTRIBUTE_UNUSED, |
| 2489 | xmlNodePtr node ATTRIBUTE_UNUSED) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2490 | if (value == NULL) |
| 2491 | return(-1); |
| 2492 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2493 | return(1); |
| 2494 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2495 | return(1); |
| 2496 | } |
| 2497 | |
| 2498 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | /** |
| 2502 | * xmlRelaxNGDefaultTypeCompare: |
| 2503 | * @data: data needed for the library |
| 2504 | * @type: the type name |
| 2505 | * @value1: the first value |
| 2506 | * @value2: the second value |
| 2507 | * |
| 2508 | * Compare two values accordingly a type from the default |
| 2509 | * datatype library. |
| 2510 | * |
| 2511 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2512 | */ |
| 2513 | static int |
| 2514 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2515 | const xmlChar *type, |
| 2516 | const xmlChar *value1, |
| 2517 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2518 | void *comp1 ATTRIBUTE_UNUSED, |
| 2519 | const xmlChar *value2, |
| 2520 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2521 | int ret = -1; |
| 2522 | |
| 2523 | if (xmlStrEqual(type, BAD_CAST "string")) { |
| 2524 | ret = xmlStrEqual(value1, value2); |
| 2525 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
| 2526 | if (!xmlStrEqual(value1, value2)) { |
| 2527 | xmlChar *nval, *nvalue; |
| 2528 | |
| 2529 | /* |
| 2530 | * TODO: trivial optimizations are possible by |
| 2531 | * computing at compile-time |
| 2532 | */ |
| 2533 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2534 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
| 2535 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2536 | if ((nval == NULL) || (nvalue == NULL)) |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2537 | ret = -1; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2538 | else if (xmlStrEqual(nval, nvalue)) |
| 2539 | ret = 1; |
| 2540 | else |
| 2541 | ret = 0; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2542 | if (nval != NULL) |
| 2543 | xmlFree(nval); |
| 2544 | if (nvalue != NULL) |
| 2545 | xmlFree(nvalue); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2546 | } else |
| 2547 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2548 | } |
| 2549 | return(ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | static int xmlRelaxNGTypeInitialized = 0; |
| 2553 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2554 | |
| 2555 | /** |
| 2556 | * xmlRelaxNGFreeTypeLibrary: |
| 2557 | * @lib: the type library structure |
| 2558 | * @namespace: the URI bound to the library |
| 2559 | * |
| 2560 | * Free the structure associated to the type library |
| 2561 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2562 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2563 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
| 2564 | const xmlChar *namespace ATTRIBUTE_UNUSED) { |
| 2565 | if (lib == NULL) |
| 2566 | return; |
| 2567 | if (lib->namespace != NULL) |
| 2568 | xmlFree((xmlChar *)lib->namespace); |
| 2569 | xmlFree(lib); |
| 2570 | } |
| 2571 | |
| 2572 | /** |
| 2573 | * xmlRelaxNGRegisterTypeLibrary: |
| 2574 | * @namespace: the URI bound to the library |
| 2575 | * @data: data associated to the library |
| 2576 | * @have: the provide function |
| 2577 | * @check: the checking function |
| 2578 | * @comp: the comparison function |
| 2579 | * |
| 2580 | * Register a new type library |
| 2581 | * |
| 2582 | * Returns 0 in case of success and -1 in case of error. |
| 2583 | */ |
| 2584 | static int |
| 2585 | xmlRelaxNGRegisterTypeLibrary(const xmlChar *namespace, void *data, |
| 2586 | xmlRelaxNGTypeHave have, xmlRelaxNGTypeCheck check, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2587 | xmlRelaxNGTypeCompare comp, xmlRelaxNGFacetCheck facet, |
| 2588 | xmlRelaxNGTypeFree freef) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2589 | xmlRelaxNGTypeLibraryPtr lib; |
| 2590 | int ret; |
| 2591 | |
| 2592 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
| 2593 | (check == NULL) || (comp == NULL)) |
| 2594 | return(-1); |
| 2595 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
| 2596 | xmlGenericError(xmlGenericErrorContext, |
| 2597 | "Relax-NG types library '%s' already registered\n", |
| 2598 | namespace); |
| 2599 | return(-1); |
| 2600 | } |
| 2601 | lib = (xmlRelaxNGTypeLibraryPtr) xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
| 2602 | if (lib == NULL) { |
| 2603 | xmlGenericError(xmlGenericErrorContext, |
| 2604 | "Relax-NG types library '%s' malloc() failed\n", |
| 2605 | namespace); |
| 2606 | return (-1); |
| 2607 | } |
| 2608 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2609 | lib->namespace = xmlStrdup(namespace); |
| 2610 | lib->data = data; |
| 2611 | lib->have = have; |
| 2612 | lib->comp = comp; |
| 2613 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2614 | lib->facet = facet; |
| 2615 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2616 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2617 | if (ret < 0) { |
| 2618 | xmlGenericError(xmlGenericErrorContext, |
| 2619 | "Relax-NG types library failed to register '%s'\n", |
| 2620 | namespace); |
| 2621 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2622 | return(-1); |
| 2623 | } |
| 2624 | return(0); |
| 2625 | } |
| 2626 | |
| 2627 | /** |
| 2628 | * xmlRelaxNGInitTypes: |
| 2629 | * |
| 2630 | * Initilize the default type libraries. |
| 2631 | * |
| 2632 | * Returns 0 in case of success and -1 in case of error. |
| 2633 | */ |
| 2634 | static int |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2635 | xmlRelaxNGInitTypes(void) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2636 | if (xmlRelaxNGTypeInitialized != 0) |
| 2637 | return(0); |
| 2638 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2639 | if (xmlRelaxNGRegisteredTypes == NULL) { |
| 2640 | xmlGenericError(xmlGenericErrorContext, |
| 2641 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2642 | return(-1); |
| 2643 | } |
| 2644 | xmlRelaxNGRegisterTypeLibrary( |
| 2645 | BAD_CAST "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2646 | NULL, |
| 2647 | xmlRelaxNGSchemaTypeHave, |
| 2648 | xmlRelaxNGSchemaTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2649 | xmlRelaxNGSchemaTypeCompare, |
| 2650 | xmlRelaxNGSchemaFacetCheck, |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2651 | xmlRelaxNGSchemaFreeValue); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2652 | xmlRelaxNGRegisterTypeLibrary( |
| 2653 | xmlRelaxNGNs, |
| 2654 | NULL, |
| 2655 | xmlRelaxNGDefaultTypeHave, |
| 2656 | xmlRelaxNGDefaultTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2657 | xmlRelaxNGDefaultTypeCompare, |
| 2658 | NULL, |
| 2659 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2660 | xmlRelaxNGTypeInitialized = 1; |
| 2661 | return(0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | /** |
| 2665 | * xmlRelaxNGCleanupTypes: |
| 2666 | * |
| 2667 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2668 | */ |
| 2669 | void |
| 2670 | xmlRelaxNGCleanupTypes(void) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2671 | if (xmlRelaxNGTypeInitialized == 0) |
| 2672 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2673 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2674 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
| 2675 | xmlRelaxNGFreeTypeLibrary); |
| 2676 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | /************************************************************************ |
| 2680 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2681 | * Compiling element content into regexp * |
| 2682 | * * |
| 2683 | * Sometime the element content can be compiled into a pure regexp, * |
| 2684 | * This allows a faster execution and streamability at that level * |
| 2685 | * * |
| 2686 | ************************************************************************/ |
| 2687 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2688 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2689 | xmlRelaxNGDefinePtr def); |
| 2690 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2691 | /** |
| 2692 | * xmlRelaxNGIsCompileable: |
| 2693 | * @define: the definition to check |
| 2694 | * |
| 2695 | * Check if a definition is nullable. |
| 2696 | * |
| 2697 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2698 | */ |
| 2699 | static int |
| 2700 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2701 | int ret = -1; |
| 2702 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2703 | if (def == NULL) { |
| 2704 | return(-1); |
| 2705 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2706 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2707 | (def->dflags & IS_COMPILABLE)) |
| 2708 | return(1); |
| 2709 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2710 | (def->dflags & IS_NOT_COMPILABLE)) |
| 2711 | return(0); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2712 | switch(def->type) { |
| 2713 | case XML_RELAXNG_REF: |
| 2714 | case XML_RELAXNG_EXTERNALREF: |
| 2715 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2716 | if (def->depth == -20) { |
| 2717 | return(1); |
| 2718 | } else { |
| 2719 | def->depth = -20; |
| 2720 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2721 | } |
| 2722 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2723 | case XML_RELAXNG_NOOP: |
| 2724 | case XML_RELAXNG_START: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2725 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2726 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2727 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2728 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2729 | ret = 1; |
| 2730 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2731 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2732 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2733 | ((def->dflags & IS_COMPILABLE) == 0)) { |
| 2734 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2735 | if (ret == 0) def->dflags |= IS_NOT_COMPILABLE; |
| 2736 | if (ret == 1) def->dflags |= IS_COMPILABLE; |
| 2737 | } |
| 2738 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2739 | return(0); |
| 2740 | else |
| 2741 | return(1); |
| 2742 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2743 | case XML_RELAXNG_OPTIONAL: |
| 2744 | case XML_RELAXNG_ZEROORMORE: |
| 2745 | case XML_RELAXNG_ONEORMORE: |
| 2746 | case XML_RELAXNG_CHOICE: |
| 2747 | case XML_RELAXNG_GROUP: |
| 2748 | case XML_RELAXNG_DEF: { |
| 2749 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2750 | |
| 2751 | list = def->content; |
| 2752 | while (list != NULL) { |
| 2753 | ret = xmlRelaxNGIsCompileable(list); |
| 2754 | if (ret != 1) |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2755 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2756 | list = list->next; |
| 2757 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2758 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2759 | } |
| 2760 | case XML_RELAXNG_EXCEPT: |
| 2761 | case XML_RELAXNG_ATTRIBUTE: |
| 2762 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2763 | case XML_RELAXNG_DATATYPE: |
| 2764 | case XML_RELAXNG_LIST: |
| 2765 | case XML_RELAXNG_PARAM: |
| 2766 | case XML_RELAXNG_VALUE: |
| 2767 | ret = 0; |
| 2768 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2769 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2770 | ret = -1; |
| 2771 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2772 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2773 | if (ret == 0) def->dflags |= IS_NOT_COMPILABLE; |
| 2774 | if (ret == 1) def->dflags |= IS_COMPILABLE; |
| 2775 | return(ret); |
| 2776 | } |
| 2777 | |
| 2778 | /** |
| 2779 | * xmlRelaxNGCompile: |
| 2780 | * ctxt: the RelaxNG parser context |
| 2781 | * @define: the definition tree to compile |
| 2782 | * |
| 2783 | * Compile the set of definitions, it works recursively, till the |
| 2784 | * element boundaries, where it tries to compile the content if possible |
| 2785 | * |
| 2786 | * Returns 0 if success and -1 in case of error |
| 2787 | */ |
| 2788 | static int |
| 2789 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) { |
| 2790 | int ret = 0; |
| 2791 | xmlRelaxNGDefinePtr list; |
| 2792 | |
| 2793 | if ((ctxt == NULL) || (def == NULL)) return(-1); |
| 2794 | |
| 2795 | switch(def->type) { |
| 2796 | case XML_RELAXNG_START: |
| 2797 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
| 2798 | xmlAutomataPtr oldam = ctxt->am; |
| 2799 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2800 | |
| 2801 | def->depth = -25; |
| 2802 | |
| 2803 | list = def->content; |
| 2804 | ctxt->am = xmlNewAutomata(); |
| 2805 | if (ctxt->am == NULL) |
| 2806 | return(-1); |
| 2807 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2808 | while (list != NULL) { |
| 2809 | xmlRelaxNGCompile(ctxt, list); |
| 2810 | list = list->next; |
| 2811 | } |
| 2812 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2813 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 2814 | xmlRegexpIsDeterminist(def->contModel); |
| 2815 | |
| 2816 | xmlFreeAutomata(ctxt->am); |
| 2817 | ctxt->state = oldstate; |
| 2818 | ctxt->am = oldam; |
| 2819 | } |
| 2820 | break; |
| 2821 | case XML_RELAXNG_ELEMENT: |
| 2822 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 2823 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2824 | ctxt->state, NULL, def->name, def->ns, def); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2825 | } |
| 2826 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 2827 | xmlAutomataPtr oldam = ctxt->am; |
| 2828 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2829 | |
| 2830 | def->depth = -25; |
| 2831 | |
| 2832 | list = def->content; |
| 2833 | ctxt->am = xmlNewAutomata(); |
| 2834 | if (ctxt->am == NULL) |
| 2835 | return(-1); |
| 2836 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2837 | while (list != NULL) { |
| 2838 | xmlRelaxNGCompile(ctxt, list); |
| 2839 | list = list->next; |
| 2840 | } |
| 2841 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2842 | def->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2843 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
| 2844 | /* |
| 2845 | * we can only use the automata if it is determinist |
| 2846 | */ |
| 2847 | xmlRegFreeRegexp(def->contModel); |
| 2848 | def->contModel = NULL; |
| 2849 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2850 | xmlFreeAutomata(ctxt->am); |
| 2851 | ctxt->state = oldstate; |
| 2852 | ctxt->am = oldam; |
| 2853 | } else { |
| 2854 | xmlAutomataPtr oldam = ctxt->am; |
| 2855 | |
| 2856 | /* |
| 2857 | * we can't build the content model for this element content |
| 2858 | * but it still might be possible to build it for some of its |
| 2859 | * children, recurse. |
| 2860 | */ |
| 2861 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 2862 | ctxt->am = oldam; |
| 2863 | } |
| 2864 | break; |
| 2865 | case XML_RELAXNG_REF: |
| 2866 | case XML_RELAXNG_EXTERNALREF: |
| 2867 | case XML_RELAXNG_PARENTREF: |
| 2868 | case XML_RELAXNG_NOOP: |
| 2869 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 2870 | break; |
| 2871 | case XML_RELAXNG_OPTIONAL: { |
| 2872 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2873 | |
| 2874 | xmlRelaxNGCompile(ctxt, def->content); |
| 2875 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 2876 | break; |
| 2877 | } |
| 2878 | case XML_RELAXNG_ZEROORMORE: { |
| 2879 | xmlAutomataStatePtr oldstate; |
| 2880 | |
| 2881 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 2882 | oldstate = ctxt->state; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2883 | list = def->content; |
| 2884 | while (list != NULL) { |
| 2885 | xmlRelaxNGCompile(ctxt, list); |
| 2886 | list = list->next; |
| 2887 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2888 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 2889 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2890 | break; |
| 2891 | } |
| 2892 | case XML_RELAXNG_ONEORMORE: { |
| 2893 | xmlAutomataStatePtr oldstate; |
| 2894 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2895 | list = def->content; |
| 2896 | while (list != NULL) { |
| 2897 | xmlRelaxNGCompile(ctxt, list); |
| 2898 | list = list->next; |
| 2899 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2900 | oldstate = ctxt->state; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2901 | list = def->content; |
| 2902 | while (list != NULL) { |
| 2903 | xmlRelaxNGCompile(ctxt, list); |
| 2904 | list = list->next; |
| 2905 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2906 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 2907 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2908 | break; |
| 2909 | } |
| 2910 | case XML_RELAXNG_CHOICE: { |
| 2911 | xmlAutomataStatePtr target = NULL; |
| 2912 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2913 | |
| 2914 | list = def->content; |
| 2915 | while (list != NULL) { |
| 2916 | ctxt->state = oldstate; |
| 2917 | xmlRelaxNGCompile(ctxt, list); |
| 2918 | if (target == NULL) |
| 2919 | target = ctxt->state; |
| 2920 | else { |
| 2921 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, target); |
| 2922 | } |
| 2923 | list = list->next; |
| 2924 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2925 | ctxt->state = target; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2926 | |
| 2927 | break; |
| 2928 | } |
| 2929 | case XML_RELAXNG_GROUP: |
| 2930 | case XML_RELAXNG_DEF: |
| 2931 | list = def->content; |
| 2932 | while (list != NULL) { |
| 2933 | xmlRelaxNGCompile(ctxt, list); |
| 2934 | list = list->next; |
| 2935 | } |
| 2936 | break; |
| 2937 | case XML_RELAXNG_TEXT: { |
| 2938 | xmlAutomataStatePtr oldstate; |
| 2939 | |
| 2940 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 2941 | oldstate = ctxt->state; |
| 2942 | xmlRelaxNGCompile(ctxt, def->content); |
| 2943 | xmlAutomataNewTransition(ctxt->am, ctxt->state, ctxt->state, |
| 2944 | BAD_CAST "#text", NULL); |
| 2945 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2946 | break; |
| 2947 | } |
| 2948 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2949 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2950 | break; |
| 2951 | case XML_RELAXNG_EXCEPT: |
| 2952 | case XML_RELAXNG_ATTRIBUTE: |
| 2953 | case XML_RELAXNG_INTERLEAVE: |
| 2954 | case XML_RELAXNG_NOT_ALLOWED: |
| 2955 | case XML_RELAXNG_DATATYPE: |
| 2956 | case XML_RELAXNG_LIST: |
| 2957 | case XML_RELAXNG_PARAM: |
| 2958 | case XML_RELAXNG_VALUE: |
| 2959 | TODO /* This should not happen and generate an internal error */ |
| 2960 | printf("trying to compile %s\n", xmlRelaxNGDefName(def)); |
| 2961 | |
| 2962 | break; |
| 2963 | } |
| 2964 | return(ret); |
| 2965 | } |
| 2966 | |
| 2967 | /** |
| 2968 | * xmlRelaxNGTryCompile: |
| 2969 | * ctxt: the RelaxNG parser context |
| 2970 | * @define: the definition tree to compile |
| 2971 | * |
| 2972 | * Try to compile the set of definitions, it works recursively, |
| 2973 | * possibly ignoring parts which cannot be compiled. |
| 2974 | * |
| 2975 | * Returns 0 if success and -1 in case of error |
| 2976 | */ |
| 2977 | static int |
| 2978 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) { |
| 2979 | int ret = 0; |
| 2980 | xmlRelaxNGDefinePtr list; |
| 2981 | |
| 2982 | if ((ctxt == NULL) || (def == NULL)) return(-1); |
| 2983 | |
| 2984 | if ((def->type == XML_RELAXNG_START) || |
| 2985 | (def->type == XML_RELAXNG_ELEMENT)) { |
| 2986 | ret = xmlRelaxNGIsCompileable(def); |
| 2987 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 2988 | ctxt->am = NULL; |
| 2989 | ret = xmlRelaxNGCompile(ctxt, def); |
| 2990 | return(ret); |
| 2991 | } |
| 2992 | } |
| 2993 | switch(def->type) { |
| 2994 | case XML_RELAXNG_REF: |
| 2995 | case XML_RELAXNG_EXTERNALREF: |
| 2996 | case XML_RELAXNG_PARENTREF: |
| 2997 | case XML_RELAXNG_NOOP: |
| 2998 | case XML_RELAXNG_START: |
| 2999 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 3000 | break; |
| 3001 | case XML_RELAXNG_TEXT: |
| 3002 | case XML_RELAXNG_DATATYPE: |
| 3003 | case XML_RELAXNG_LIST: |
| 3004 | case XML_RELAXNG_PARAM: |
| 3005 | case XML_RELAXNG_VALUE: |
| 3006 | case XML_RELAXNG_EMPTY: |
| 3007 | case XML_RELAXNG_ELEMENT: |
| 3008 | ret = 0; |
| 3009 | break; |
| 3010 | case XML_RELAXNG_OPTIONAL: |
| 3011 | case XML_RELAXNG_ZEROORMORE: |
| 3012 | case XML_RELAXNG_ONEORMORE: |
| 3013 | case XML_RELAXNG_CHOICE: |
| 3014 | case XML_RELAXNG_GROUP: |
| 3015 | case XML_RELAXNG_DEF: |
| 3016 | list = def->content; |
| 3017 | while (list != NULL) { |
| 3018 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3019 | if (ret != 0) |
| 3020 | break; |
| 3021 | list = list->next; |
| 3022 | } |
| 3023 | break; |
| 3024 | case XML_RELAXNG_EXCEPT: |
| 3025 | case XML_RELAXNG_ATTRIBUTE: |
| 3026 | case XML_RELAXNG_INTERLEAVE: |
| 3027 | case XML_RELAXNG_NOT_ALLOWED: |
| 3028 | ret = 0; |
| 3029 | break; |
| 3030 | } |
| 3031 | return(ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | /************************************************************************ |
| 3035 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3036 | * Parsing functions * |
| 3037 | * * |
| 3038 | ************************************************************************/ |
| 3039 | |
| 3040 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute( |
| 3041 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 3042 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement( |
| 3043 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 3044 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns( |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3045 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, int group); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3046 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern( |
| 3047 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 3048 | static xmlRelaxNGPtr xmlRelaxNGParseDocument( |
| 3049 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 3050 | static int xmlRelaxNGParseGrammarContent( |
| 3051 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 3052 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass( |
| 3053 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 3054 | xmlRelaxNGDefinePtr def); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 3055 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar( |
| 3056 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3057 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3058 | xmlRelaxNGDefinePtr define, xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3059 | |
| 3060 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3061 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3062 | |
| 3063 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3064 | * xmlRelaxNGIsNullable: |
| 3065 | * @define: the definition to verify |
| 3066 | * |
| 3067 | * Check if a definition is nullable. |
| 3068 | * |
| 3069 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3070 | */ |
| 3071 | static int |
| 3072 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) { |
| 3073 | int ret; |
| 3074 | if (define == NULL) |
| 3075 | return(-1); |
| 3076 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3077 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3078 | return(1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3079 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3080 | return(0); |
| 3081 | switch (define->type) { |
| 3082 | case XML_RELAXNG_EMPTY: |
| 3083 | case XML_RELAXNG_TEXT: |
| 3084 | ret = 1; break; |
| 3085 | case XML_RELAXNG_NOOP: |
| 3086 | case XML_RELAXNG_DEF: |
| 3087 | case XML_RELAXNG_REF: |
| 3088 | case XML_RELAXNG_EXTERNALREF: |
| 3089 | case XML_RELAXNG_PARENTREF: |
| 3090 | case XML_RELAXNG_ONEORMORE: |
| 3091 | ret = xmlRelaxNGIsNullable(define->content); |
| 3092 | break; |
| 3093 | case XML_RELAXNG_EXCEPT: |
| 3094 | case XML_RELAXNG_NOT_ALLOWED: |
| 3095 | case XML_RELAXNG_ELEMENT: |
| 3096 | case XML_RELAXNG_DATATYPE: |
| 3097 | case XML_RELAXNG_PARAM: |
| 3098 | case XML_RELAXNG_VALUE: |
| 3099 | case XML_RELAXNG_LIST: |
| 3100 | case XML_RELAXNG_ATTRIBUTE: |
| 3101 | ret = 0; break; |
| 3102 | case XML_RELAXNG_CHOICE: { |
| 3103 | xmlRelaxNGDefinePtr list = define->content; |
| 3104 | |
| 3105 | while (list != NULL) { |
| 3106 | ret = xmlRelaxNGIsNullable(list); |
| 3107 | if (ret != 0) |
| 3108 | goto done; |
| 3109 | list = list->next; |
| 3110 | } |
| 3111 | ret = 0; break; |
| 3112 | } |
| 3113 | case XML_RELAXNG_START: |
| 3114 | case XML_RELAXNG_INTERLEAVE: |
| 3115 | case XML_RELAXNG_GROUP: { |
| 3116 | xmlRelaxNGDefinePtr list = define->content; |
| 3117 | |
| 3118 | while (list != NULL) { |
| 3119 | ret = xmlRelaxNGIsNullable(list); |
| 3120 | if (ret != 1) |
| 3121 | goto done; |
| 3122 | list = list->next; |
| 3123 | } |
| 3124 | return(1); |
| 3125 | } |
| 3126 | default: |
| 3127 | return(-1); |
| 3128 | } |
| 3129 | done: |
| 3130 | if (ret == 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3131 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3132 | if (ret == 1) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3133 | define->dflags |= IS_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3134 | return(ret); |
| 3135 | } |
| 3136 | |
| 3137 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3138 | * xmlRelaxNGIsBlank: |
| 3139 | * @str: a string |
| 3140 | * |
| 3141 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3142 | * |
| 3143 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3144 | */ |
| 3145 | static int |
| 3146 | xmlRelaxNGIsBlank(xmlChar *str) { |
| 3147 | if (str == NULL) |
| 3148 | return(1); |
| 3149 | while (*str != 0) { |
| 3150 | if (!(IS_BLANK(*str))) return(0); |
| 3151 | str++; |
| 3152 | } |
| 3153 | return(1); |
| 3154 | } |
| 3155 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3156 | /** |
| 3157 | * xmlRelaxNGGetDataTypeLibrary: |
| 3158 | * @ctxt: a Relax-NG parser context |
| 3159 | * @node: the current data or value element |
| 3160 | * |
| 3161 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3162 | * |
| 3163 | * Returns the datatypeLibary value or NULL if not found |
| 3164 | */ |
| 3165 | static xmlChar * |
| 3166 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3167 | xmlNodePtr node) { |
| 3168 | xmlChar *ret, *escape; |
| 3169 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3170 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
| 3171 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3172 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3173 | if (ret[0] == 0) { |
| 3174 | xmlFree(ret); |
| 3175 | return(NULL); |
| 3176 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3177 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3178 | if (escape == NULL) { |
| 3179 | return(ret); |
| 3180 | } |
| 3181 | xmlFree(ret); |
| 3182 | return(escape); |
| 3183 | } |
| 3184 | } |
| 3185 | node = node->parent; |
| 3186 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3187 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3188 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3189 | if (ret[0] == 0) { |
| 3190 | xmlFree(ret); |
| 3191 | return(NULL); |
| 3192 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3193 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3194 | if (escape == NULL) { |
| 3195 | return(ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3196 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3197 | xmlFree(ret); |
| 3198 | return(escape); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3199 | } |
| 3200 | node = node->parent; |
| 3201 | } |
| 3202 | return(NULL); |
| 3203 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3204 | |
| 3205 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3206 | * xmlRelaxNGParseValue: |
| 3207 | * @ctxt: a Relax-NG parser context |
| 3208 | * @node: the data node. |
| 3209 | * |
| 3210 | * parse the content of a RelaxNG value node. |
| 3211 | * |
| 3212 | * Returns the definition pointer or NULL in case of error |
| 3213 | */ |
| 3214 | static xmlRelaxNGDefinePtr |
| 3215 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 3216 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3217 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3218 | xmlChar *type; |
| 3219 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3220 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3221 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3222 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3223 | if (def == NULL) |
| 3224 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3225 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3226 | |
| 3227 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3228 | if (type != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3229 | xmlRelaxNGNormExtSpace(type); |
| 3230 | if (xmlValidateNCName(type, 0)) { |
| 3231 | if (ctxt->error != NULL) |
| 3232 | ctxt->error(ctxt->userData, |
| 3233 | "value type '%s' is not an NCName\n", |
| 3234 | type); |
| 3235 | ctxt->nbErrors++; |
| 3236 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3237 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3238 | if (library == NULL) |
| 3239 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 3240 | |
| 3241 | def->name = type; |
| 3242 | def->ns = library; |
| 3243 | |
| 3244 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3245 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3246 | if (lib == NULL) { |
| 3247 | if (ctxt->error != NULL) |
| 3248 | ctxt->error(ctxt->userData, |
| 3249 | "Use of unregistered type library '%s'\n", |
| 3250 | library); |
| 3251 | ctxt->nbErrors++; |
| 3252 | def->data = NULL; |
| 3253 | } else { |
| 3254 | def->data = lib; |
| 3255 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3256 | if (ctxt->error != NULL) |
| 3257 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3258 | "Internal error with type library '%s': no 'have'\n", |
| 3259 | library); |
| 3260 | ctxt->nbErrors++; |
| 3261 | } else { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3262 | success = lib->have(lib->data, def->name); |
| 3263 | if (success != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3264 | if (ctxt->error != NULL) |
| 3265 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3266 | "Error type '%s' is not exported by type library '%s'\n", |
| 3267 | def->name, library); |
| 3268 | ctxt->nbErrors++; |
| 3269 | } |
| 3270 | } |
| 3271 | } |
| 3272 | } |
| 3273 | if (node->children == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 3274 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3275 | } else if (((node->children->type != XML_TEXT_NODE) && |
| 3276 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3277 | (node->children->next != NULL)) { |
| 3278 | if (ctxt->error != NULL) |
| 3279 | ctxt->error(ctxt->userData, |
| 3280 | "Expecting a single text value for <value>content\n"); |
| 3281 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3282 | } else if (def != NULL) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3283 | def->value = xmlNodeGetContent(node); |
| 3284 | if (def->value == NULL) { |
| 3285 | if (ctxt->error != NULL) |
| 3286 | ctxt->error(ctxt->userData, |
| 3287 | "Element <value> has no content\n"); |
| 3288 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3289 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3290 | void *val = NULL; |
| 3291 | |
| 3292 | success = lib->check(lib->data, def->name, def->value, &val, node); |
| 3293 | if (success != 1) { |
| 3294 | if (ctxt->error != NULL) |
| 3295 | ctxt->error(ctxt->userData, |
| 3296 | "Value '%s' is not acceptable for type '%s'\n", |
| 3297 | def->value, def->name); |
| 3298 | ctxt->nbErrors++; |
| 3299 | } else { |
| 3300 | if (val != NULL) |
| 3301 | def->attrs = val; |
| 3302 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3303 | } |
| 3304 | } |
| 3305 | /* TODO check ahead of time that the value is okay per the type */ |
| 3306 | return(def); |
| 3307 | } |
| 3308 | |
| 3309 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3310 | * xmlRelaxNGParseData: |
| 3311 | * @ctxt: a Relax-NG parser context |
| 3312 | * @node: the data node. |
| 3313 | * |
| 3314 | * parse the content of a RelaxNG data node. |
| 3315 | * |
| 3316 | * Returns the definition pointer or NULL in case of error |
| 3317 | */ |
| 3318 | static xmlRelaxNGDefinePtr |
| 3319 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3320 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3321 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3322 | xmlRelaxNGTypeLibraryPtr lib; |
| 3323 | xmlChar *type; |
| 3324 | xmlChar *library; |
| 3325 | xmlNodePtr content; |
| 3326 | int tmp; |
| 3327 | |
| 3328 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3329 | if (type == NULL) { |
| 3330 | if (ctxt->error != NULL) |
| 3331 | ctxt->error(ctxt->userData, |
| 3332 | "data has no type\n"); |
| 3333 | ctxt->nbErrors++; |
| 3334 | return(NULL); |
| 3335 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3336 | xmlRelaxNGNormExtSpace(type); |
| 3337 | if (xmlValidateNCName(type, 0)) { |
| 3338 | if (ctxt->error != NULL) |
| 3339 | ctxt->error(ctxt->userData, |
| 3340 | "data type '%s' is not an NCName\n", |
| 3341 | type); |
| 3342 | ctxt->nbErrors++; |
| 3343 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3344 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3345 | if (library == NULL) |
| 3346 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 3347 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3348 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3349 | if (def == NULL) { |
| 3350 | xmlFree(type); |
| 3351 | return(NULL); |
| 3352 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3353 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3354 | def->name = type; |
| 3355 | def->ns = library; |
| 3356 | |
| 3357 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3358 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3359 | if (lib == NULL) { |
| 3360 | if (ctxt->error != NULL) |
| 3361 | ctxt->error(ctxt->userData, |
| 3362 | "Use of unregistered type library '%s'\n", |
| 3363 | library); |
| 3364 | ctxt->nbErrors++; |
| 3365 | def->data = NULL; |
| 3366 | } else { |
| 3367 | def->data = lib; |
| 3368 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3369 | if (ctxt->error != NULL) |
| 3370 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3371 | "Internal error with type library '%s': no 'have'\n", |
| 3372 | library); |
| 3373 | ctxt->nbErrors++; |
| 3374 | } else { |
| 3375 | tmp = lib->have(lib->data, def->name); |
| 3376 | if (tmp != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3377 | if (ctxt->error != NULL) |
| 3378 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3379 | "Error type '%s' is not exported by type library '%s'\n", |
| 3380 | def->name, library); |
| 3381 | ctxt->nbErrors++; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3382 | } else if ((xmlStrEqual(library, BAD_CAST |
| 3383 | "http://www.w3.org/2001/XMLSchema-datatypes")) && |
| 3384 | ((xmlStrEqual(def->name, BAD_CAST "IDREF")) || |
| 3385 | (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3386 | ctxt->idref = 1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3387 | } |
| 3388 | } |
| 3389 | } |
| 3390 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3391 | |
| 3392 | /* |
| 3393 | * Handle optional params |
| 3394 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3395 | while (content != NULL) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3396 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3397 | break; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3398 | if (xmlStrEqual(library, |
| 3399 | BAD_CAST"http://relaxng.org/ns/structure/1.0")) { |
| 3400 | if (ctxt->error != NULL) |
| 3401 | ctxt->error(ctxt->userData, |
| 3402 | "Type library '%s' does not allow type parameters\n", |
| 3403 | library); |
| 3404 | ctxt->nbErrors++; |
| 3405 | content = content->next; |
| 3406 | while ((content != NULL) && |
| 3407 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3408 | content = content->next; |
| 3409 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3410 | param = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3411 | if (param != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3412 | param->type = XML_RELAXNG_PARAM; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3413 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3414 | if (param->name == NULL) { |
| 3415 | if (ctxt->error != NULL) |
| 3416 | ctxt->error(ctxt->userData, |
| 3417 | "param has no name\n"); |
| 3418 | ctxt->nbErrors++; |
| 3419 | } |
| 3420 | param->value = xmlNodeGetContent(content); |
| 3421 | if (lastparam == NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3422 | def->attrs = lastparam = param; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3423 | } else { |
| 3424 | lastparam->next = param; |
| 3425 | lastparam = param; |
| 3426 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 3427 | if (lib != NULL) { |
| 3428 | } |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3429 | } |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3430 | content = content->next; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3431 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3432 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3433 | /* |
| 3434 | * Handle optional except |
| 3435 | */ |
| 3436 | if ((content != NULL) && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3437 | xmlNodePtr child; |
| 3438 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
| 3439 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3440 | except = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3441 | if (except == NULL) { |
| 3442 | return(def); |
| 3443 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3444 | except->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3445 | child = content->children; |
| 3446 | if (last == NULL) { |
| 3447 | def->content = except; |
| 3448 | } else { |
| 3449 | last->next = except; |
| 3450 | } |
| 3451 | if (child == NULL) { |
| 3452 | if (ctxt->error != NULL) |
| 3453 | ctxt->error(ctxt->userData, |
| 3454 | "except has no content\n"); |
| 3455 | ctxt->nbErrors++; |
| 3456 | } |
| 3457 | while (child != NULL) { |
| 3458 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3459 | if (tmp2 != NULL) { |
| 3460 | if (last2 == NULL) { |
| 3461 | except->content = last2 = tmp2; |
| 3462 | } else { |
| 3463 | last2->next = tmp2; |
| 3464 | last2 = tmp2; |
| 3465 | } |
| 3466 | } |
| 3467 | child = child->next; |
| 3468 | } |
| 3469 | content = content->next; |
| 3470 | } |
| 3471 | /* |
| 3472 | * Check there is no unhandled data |
| 3473 | */ |
| 3474 | if (content != NULL) { |
| 3475 | if (ctxt->error != NULL) |
| 3476 | ctxt->error(ctxt->userData, |
| 3477 | "Element data has unexpected content %s\n", content->name); |
| 3478 | ctxt->nbErrors++; |
| 3479 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3480 | |
| 3481 | return(def); |
| 3482 | } |
| 3483 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3484 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3485 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3486 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3487 | * xmlRelaxNGCompareNameClasses: |
| 3488 | * @defs1: the first element/attribute defs |
| 3489 | * @defs2: the second element/attribute defs |
| 3490 | * @name: the restriction on the name |
| 3491 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3492 | * |
| 3493 | * Compare the 2 lists of element definitions. The comparison is |
| 3494 | * that if both lists do not accept the same QNames, it returns 1 |
| 3495 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3496 | * |
| 3497 | * Returns 1 disttinct, 0 if equal |
| 3498 | */ |
| 3499 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3500 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
| 3501 | xmlRelaxNGDefinePtr def2) { |
| 3502 | int ret = 1; |
| 3503 | xmlNode node; |
| 3504 | xmlNs ns; |
| 3505 | xmlRelaxNGValidCtxt ctxt; |
| 3506 | ctxt.flags = FLAGS_IGNORABLE; |
| 3507 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3508 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3509 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3510 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
| 3511 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3512 | if (def2->type == XML_RELAXNG_TEXT) |
| 3513 | return(1); |
| 3514 | if (def1->name != NULL) { |
| 3515 | node.name = def1->name; |
| 3516 | } else { |
| 3517 | node.name = invalidName; |
| 3518 | } |
| 3519 | node.ns = &ns; |
| 3520 | if (def1->ns != NULL) { |
| 3521 | if (def1->ns[0] == 0) { |
| 3522 | node.ns = NULL; |
| 3523 | } else { |
| 3524 | ns.href = def1->ns; |
| 3525 | } |
| 3526 | } else { |
| 3527 | ns.href = invalidName; |
| 3528 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3529 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3530 | if (def1->nameClass != NULL) { |
| 3531 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3532 | } else { |
| 3533 | ret = 0; |
| 3534 | } |
| 3535 | } else { |
| 3536 | ret = 1; |
| 3537 | } |
| 3538 | } else if (def1->type == XML_RELAXNG_TEXT) { |
| 3539 | if (def2->type == XML_RELAXNG_TEXT) |
| 3540 | return(0); |
| 3541 | return(1); |
| 3542 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3543 | TODO |
| 3544 | ret = 0; |
| 3545 | } else { |
| 3546 | TODO |
| 3547 | ret = 0; |
| 3548 | } |
| 3549 | if (ret == 0) |
| 3550 | return(ret); |
| 3551 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
| 3552 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3553 | if (def2->name != NULL) { |
| 3554 | node.name = def2->name; |
| 3555 | } else { |
| 3556 | node.name = invalidName; |
| 3557 | } |
| 3558 | node.ns = &ns; |
| 3559 | if (def2->ns != NULL) { |
| 3560 | if (def2->ns[0] == 0) { |
| 3561 | node.ns = NULL; |
| 3562 | } else { |
| 3563 | ns.href = def2->ns; |
| 3564 | } |
| 3565 | } else { |
| 3566 | ns.href = invalidName; |
| 3567 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3568 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3569 | if (def2->nameClass != NULL) { |
| 3570 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3571 | } else { |
| 3572 | ret = 0; |
| 3573 | } |
| 3574 | } else { |
| 3575 | ret = 1; |
| 3576 | } |
| 3577 | } else { |
| 3578 | TODO |
| 3579 | ret = 0; |
| 3580 | } |
| 3581 | |
| 3582 | return(ret); |
| 3583 | } |
| 3584 | |
| 3585 | /** |
| 3586 | * xmlRelaxNGCompareElemDefLists: |
| 3587 | * @ctxt: a Relax-NG parser context |
| 3588 | * @defs1: the first list of element/attribute defs |
| 3589 | * @defs2: the second list of element/attribute defs |
| 3590 | * |
| 3591 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3592 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3593 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3594 | * |
| 3595 | * Returns 1 disttinct, 0 if equal |
| 3596 | */ |
| 3597 | static int |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3598 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3599 | xmlRelaxNGDefinePtr *def1, |
| 3600 | xmlRelaxNGDefinePtr *def2) { |
| 3601 | xmlRelaxNGDefinePtr *basedef2 = def2; |
| 3602 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3603 | if ((def1 == NULL) || (def2 == NULL)) |
| 3604 | return(1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3605 | if ((*def1 == NULL) || (*def2 == NULL)) |
| 3606 | return(1); |
| 3607 | while (*def1 != NULL) { |
| 3608 | while ((*def2) != NULL) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3609 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3610 | return(0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3611 | def2++; |
| 3612 | } |
| 3613 | def2 = basedef2; |
| 3614 | def1++; |
| 3615 | } |
| 3616 | return(1); |
| 3617 | } |
| 3618 | |
| 3619 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3620 | * xmlRelaxNGGenerateAttributes: |
| 3621 | * @ctxt: a Relax-NG parser context |
| 3622 | * @def: the definition definition |
| 3623 | * |
| 3624 | * Check if the definition can only generate attributes |
| 3625 | * |
| 3626 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3627 | */ |
| 3628 | static int |
| 3629 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
| 3630 | xmlRelaxNGDefinePtr def) { |
| 3631 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3632 | |
| 3633 | /* |
| 3634 | * Don't run that check in case of error. Infinite recursion |
| 3635 | * becomes possible. |
| 3636 | */ |
| 3637 | if (ctxt->nbErrors != 0) |
| 3638 | return(-1); |
| 3639 | |
| 3640 | parent = NULL; |
| 3641 | cur = def; |
| 3642 | while (cur != NULL) { |
| 3643 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3644 | (cur->type == XML_RELAXNG_TEXT) || |
| 3645 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3646 | (cur->type == XML_RELAXNG_PARAM) || |
| 3647 | (cur->type == XML_RELAXNG_LIST) || |
| 3648 | (cur->type == XML_RELAXNG_VALUE) || |
| 3649 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3650 | return(0); |
| 3651 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3652 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3653 | (cur->type == XML_RELAXNG_GROUP) || |
| 3654 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3655 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3656 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3657 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3658 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3659 | (cur->type == XML_RELAXNG_REF) || |
| 3660 | (cur->type == XML_RELAXNG_DEF)) { |
| 3661 | if (cur->content != NULL) { |
| 3662 | parent = cur; |
| 3663 | cur = cur->content; |
| 3664 | tmp = cur; |
| 3665 | while (tmp != NULL) { |
| 3666 | tmp->parent = parent; |
| 3667 | tmp = tmp->next; |
| 3668 | } |
| 3669 | continue; |
| 3670 | } |
| 3671 | } |
| 3672 | if (cur == def) |
| 3673 | break; |
| 3674 | if (cur->next != NULL) { |
| 3675 | cur = cur->next; |
| 3676 | continue; |
| 3677 | } |
| 3678 | do { |
| 3679 | cur = cur->parent; |
| 3680 | if (cur == NULL) break; |
| 3681 | if (cur == def) return(1); |
| 3682 | if (cur->next != NULL) { |
| 3683 | cur = cur->next; |
| 3684 | break; |
| 3685 | } |
| 3686 | } while (cur != NULL); |
| 3687 | } |
| 3688 | return(1); |
| 3689 | } |
| 3690 | |
| 3691 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3692 | * xmlRelaxNGGetElements: |
| 3693 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3694 | * @def: the definition definition |
| 3695 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3696 | * |
| 3697 | * Compute the list of top elements a definition can generate |
| 3698 | * |
| 3699 | * Returns a list of elements or NULL if none was found. |
| 3700 | */ |
| 3701 | static xmlRelaxNGDefinePtr * |
| 3702 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3703 | xmlRelaxNGDefinePtr def, |
| 3704 | int eora) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3705 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3706 | int len = 0; |
| 3707 | int max = 0; |
| 3708 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3709 | /* |
| 3710 | * Don't run that check in case of error. Infinite recursion |
| 3711 | * becomes possible. |
| 3712 | */ |
| 3713 | if (ctxt->nbErrors != 0) |
| 3714 | return(NULL); |
| 3715 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3716 | parent = NULL; |
| 3717 | cur = def; |
| 3718 | while (cur != NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3719 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3720 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3721 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3722 | if (ret == NULL) { |
| 3723 | max = 10; |
| 3724 | ret = (xmlRelaxNGDefinePtr *) |
| 3725 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3726 | if (ret == NULL) { |
| 3727 | if (ctxt->error != NULL) |
| 3728 | ctxt->error(ctxt->userData, |
| 3729 | "Out of memory in element search\n"); |
| 3730 | ctxt->nbErrors++; |
| 3731 | return(NULL); |
| 3732 | } |
| 3733 | } else if (max <= len) { |
| 3734 | max *= 2; |
| 3735 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3736 | if (ret == NULL) { |
| 3737 | if (ctxt->error != NULL) |
| 3738 | ctxt->error(ctxt->userData, |
| 3739 | "Out of memory in element search\n"); |
| 3740 | ctxt->nbErrors++; |
| 3741 | return(NULL); |
| 3742 | } |
| 3743 | } |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3744 | ret[len++] = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3745 | ret[len] = NULL; |
| 3746 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3747 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3748 | (cur->type == XML_RELAXNG_GROUP) || |
| 3749 | (cur->type == XML_RELAXNG_ONEORMORE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3750 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3751 | (cur->type == XML_RELAXNG_OPTIONAL) || |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3752 | (cur->type == XML_RELAXNG_PARENTREF) || |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3753 | (cur->type == XML_RELAXNG_REF) || |
| 3754 | (cur->type == XML_RELAXNG_DEF)) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3755 | /* |
| 3756 | * Don't go within elements or attributes or string values. |
| 3757 | * Just gather the element top list |
| 3758 | */ |
| 3759 | if (cur->content != NULL) { |
| 3760 | parent = cur; |
| 3761 | cur = cur->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3762 | tmp = cur; |
| 3763 | while (tmp != NULL) { |
| 3764 | tmp->parent = parent; |
| 3765 | tmp = tmp->next; |
| 3766 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3767 | continue; |
| 3768 | } |
| 3769 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3770 | if (cur == def) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3771 | break; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3772 | if (cur->next != NULL) { |
| 3773 | cur = cur->next; |
| 3774 | continue; |
| 3775 | } |
| 3776 | do { |
| 3777 | cur = cur->parent; |
| 3778 | if (cur == NULL) break; |
| 3779 | if (cur == def) return(ret); |
| 3780 | if (cur->next != NULL) { |
| 3781 | cur = cur->next; |
| 3782 | break; |
| 3783 | } |
| 3784 | } while (cur != NULL); |
| 3785 | } |
| 3786 | return(ret); |
| 3787 | } |
| 3788 | |
| 3789 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3790 | * xmlRelaxNGCheckChoiceDeterminism: |
| 3791 | * @ctxt: a Relax-NG parser context |
| 3792 | * @def: the choice definition |
| 3793 | * |
| 3794 | * Also used to find indeterministic pattern in choice |
| 3795 | */ |
| 3796 | static void |
| 3797 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
| 3798 | xmlRelaxNGDefinePtr def) { |
| 3799 | xmlRelaxNGDefinePtr **list; |
| 3800 | xmlRelaxNGDefinePtr cur; |
| 3801 | int nbchild = 0, i, j, ret; |
| 3802 | int is_nullable = 0; |
| 3803 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3804 | xmlHashTablePtr triage = NULL; |
| 3805 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3806 | |
| 3807 | if ((def == NULL) || |
| 3808 | (def->type != XML_RELAXNG_CHOICE)) |
| 3809 | return; |
| 3810 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3811 | if (def->dflags & IS_PROCESSED) |
| 3812 | return; |
| 3813 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3814 | /* |
| 3815 | * Don't run that check in case of error. Infinite recursion |
| 3816 | * becomes possible. |
| 3817 | */ |
| 3818 | if (ctxt->nbErrors != 0) |
| 3819 | return; |
| 3820 | |
| 3821 | is_nullable = xmlRelaxNGIsNullable(def); |
| 3822 | |
| 3823 | cur = def->content; |
| 3824 | while (cur != NULL) { |
| 3825 | nbchild++; |
| 3826 | cur = cur->next; |
| 3827 | } |
| 3828 | |
| 3829 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 3830 | sizeof(xmlRelaxNGDefinePtr *)); |
| 3831 | if (list == NULL) { |
| 3832 | if (ctxt->error != NULL) |
| 3833 | ctxt->error(ctxt->userData, |
| 3834 | "Out of memory in choice computation\n"); |
| 3835 | ctxt->nbErrors++; |
| 3836 | return; |
| 3837 | } |
| 3838 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3839 | /* |
| 3840 | * a bit strong but safe |
| 3841 | */ |
| 3842 | if (is_nullable == 0) { |
| 3843 | triage = xmlHashCreate(10); |
| 3844 | } else { |
| 3845 | is_triable = 0; |
| 3846 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3847 | cur = def->content; |
| 3848 | while (cur != NULL) { |
| 3849 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3850 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 3851 | is_triable = 0; |
| 3852 | } else if (is_triable == 1) { |
| 3853 | xmlRelaxNGDefinePtr *tmp; |
| 3854 | int res; |
| 3855 | |
| 3856 | tmp = list[i]; |
| 3857 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 3858 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 3859 | res = xmlHashAddEntry2(triage, |
| 3860 | BAD_CAST "#text", NULL, |
| 3861 | (void *)cur); |
| 3862 | if (res != 0) |
| 3863 | is_triable = -1; |
| 3864 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 3865 | ((*tmp)->name != NULL)) { |
| 3866 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3867 | res = xmlHashAddEntry2(triage, |
| 3868 | (*tmp)->name, NULL, |
| 3869 | (void *)cur); |
| 3870 | else |
| 3871 | res = xmlHashAddEntry2(triage, |
| 3872 | (*tmp)->name, (*tmp)->ns, |
| 3873 | (void *)cur); |
| 3874 | if (res != 0) |
| 3875 | is_triable = -1; |
| 3876 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 3877 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3878 | res = xmlHashAddEntry2(triage, |
| 3879 | BAD_CAST "#any", NULL, |
| 3880 | (void *)cur); |
| 3881 | else |
| 3882 | res = xmlHashAddEntry2(triage, |
| 3883 | BAD_CAST "#any", (*tmp)->ns, |
| 3884 | (void *)cur); |
| 3885 | if (res != 0) |
| 3886 | is_triable = -1; |
| 3887 | } else { |
| 3888 | is_triable = -1; |
| 3889 | } |
| 3890 | tmp++; |
| 3891 | } |
| 3892 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3893 | i++; |
| 3894 | cur = cur->next; |
| 3895 | } |
| 3896 | |
| 3897 | for (i = 0;i < nbchild;i++) { |
| 3898 | if (list[i] == NULL) |
| 3899 | continue; |
| 3900 | for (j = 0;j < i;j++) { |
| 3901 | if (list[j] == NULL) |
| 3902 | continue; |
| 3903 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 3904 | if (ret == 0) { |
| 3905 | is_indeterminist = 1; |
| 3906 | } |
| 3907 | } |
| 3908 | } |
| 3909 | for (i = 0;i < nbchild;i++) { |
| 3910 | if (list[i] != NULL) |
| 3911 | xmlFree(list[i]); |
| 3912 | } |
| 3913 | |
| 3914 | xmlFree(list); |
| 3915 | if (is_indeterminist) { |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3916 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3917 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3918 | if (is_triable == 1) { |
| 3919 | def->dflags |= IS_TRIABLE; |
| 3920 | def->data = triage; |
| 3921 | } else if (triage != NULL) { |
| 3922 | xmlHashFree(triage, NULL); |
| 3923 | } |
| 3924 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3928 | * xmlRelaxNGCheckGroupAttrs: |
| 3929 | * @ctxt: a Relax-NG parser context |
| 3930 | * @def: the group definition |
| 3931 | * |
| 3932 | * Detects violations of rule 7.3 |
| 3933 | */ |
| 3934 | static void |
| 3935 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
| 3936 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3937 | xmlRelaxNGDefinePtr **list; |
| 3938 | xmlRelaxNGDefinePtr cur; |
| 3939 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3940 | |
| 3941 | if ((def == NULL) || |
| 3942 | ((def->type != XML_RELAXNG_GROUP) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3943 | (def->type != XML_RELAXNG_ELEMENT))) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3944 | return; |
| 3945 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3946 | if (def->dflags & IS_PROCESSED) |
| 3947 | return; |
| 3948 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3949 | /* |
| 3950 | * Don't run that check in case of error. Infinite recursion |
| 3951 | * becomes possible. |
| 3952 | */ |
| 3953 | if (ctxt->nbErrors != 0) |
| 3954 | return; |
| 3955 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3956 | cur = def->attrs; |
| 3957 | while (cur != NULL) { |
| 3958 | nbchild++; |
| 3959 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3960 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3961 | cur = def->content; |
| 3962 | while (cur != NULL) { |
| 3963 | nbchild++; |
| 3964 | cur = cur->next; |
| 3965 | } |
| 3966 | |
| 3967 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 3968 | sizeof(xmlRelaxNGDefinePtr *)); |
| 3969 | if (list == NULL) { |
| 3970 | if (ctxt->error != NULL) |
| 3971 | ctxt->error(ctxt->userData, |
| 3972 | "Out of memory in group computation\n"); |
| 3973 | ctxt->nbErrors++; |
| 3974 | return; |
| 3975 | } |
| 3976 | i = 0; |
| 3977 | cur = def->attrs; |
| 3978 | while (cur != NULL) { |
| 3979 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 3980 | i++; |
| 3981 | cur = cur->next; |
| 3982 | } |
| 3983 | cur = def->content; |
| 3984 | while (cur != NULL) { |
| 3985 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 3986 | i++; |
| 3987 | cur = cur->next; |
| 3988 | } |
| 3989 | |
| 3990 | for (i = 0;i < nbchild;i++) { |
| 3991 | if (list[i] == NULL) |
| 3992 | continue; |
| 3993 | for (j = 0;j < i;j++) { |
| 3994 | if (list[j] == NULL) |
| 3995 | continue; |
| 3996 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 3997 | if (ret == 0) { |
| 3998 | if (ctxt->error != NULL) |
| 3999 | ctxt->error(ctxt->userData, |
| 4000 | "Attributes conflicts in group\n"); |
| 4001 | ctxt->nbErrors++; |
| 4002 | } |
| 4003 | } |
| 4004 | } |
| 4005 | for (i = 0;i < nbchild;i++) { |
| 4006 | if (list[i] != NULL) |
| 4007 | xmlFree(list[i]); |
| 4008 | } |
| 4009 | |
| 4010 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4011 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
| 4014 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4015 | * xmlRelaxNGComputeInterleaves: |
| 4016 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4017 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4018 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4019 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4020 | * A lot of work for preprocessing interleave definitions |
| 4021 | * is potentially needed to get a decent execution speed at runtime |
| 4022 | * - trying to get a total order on the element nodes generated |
| 4023 | * by the interleaves, order the list of interleave definitions |
| 4024 | * following that order. |
| 4025 | * - if <text/> is used to handle mixed content, it is better to |
| 4026 | * flag this in the define and simplify the runtime checking |
| 4027 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4028 | */ |
| 4029 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4030 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
| 4031 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4032 | xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4033 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4034 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4035 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4036 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4037 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4038 | int i,j,ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4039 | int nbgroups = 0; |
| 4040 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4041 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4042 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4043 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4044 | /* |
| 4045 | * Don't run that check in case of error. Infinite recursion |
| 4046 | * becomes possible. |
| 4047 | */ |
| 4048 | if (ctxt->nbErrors != 0) |
| 4049 | return; |
| 4050 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4051 | #ifdef DEBUG_INTERLEAVE |
| 4052 | xmlGenericError(xmlGenericErrorContext, |
| 4053 | "xmlRelaxNGComputeInterleaves(%s)\n", |
| 4054 | name); |
| 4055 | #endif |
| 4056 | cur = def->content; |
| 4057 | while (cur != NULL) { |
| 4058 | nbchild++; |
| 4059 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4060 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4061 | |
| 4062 | #ifdef DEBUG_INTERLEAVE |
| 4063 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4064 | #endif |
| 4065 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
| 4066 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
| 4067 | if (groups == NULL) |
| 4068 | goto error; |
| 4069 | cur = def->content; |
| 4070 | while (cur != NULL) { |
| 4071 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4072 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4073 | if (groups[nbgroups] == NULL) |
| 4074 | goto error; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4075 | if (cur->type == XML_RELAXNG_TEXT) |
| 4076 | is_mixed++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4077 | groups[nbgroups]->rule = cur; |
| 4078 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4079 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4080 | nbgroups++; |
| 4081 | cur = cur->next; |
| 4082 | } |
| 4083 | #ifdef DEBUG_INTERLEAVE |
| 4084 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4085 | #endif |
| 4086 | |
| 4087 | /* |
| 4088 | * Let's check that all rules makes a partitions according to 7.4 |
| 4089 | */ |
| 4090 | partitions = (xmlRelaxNGPartitionPtr) |
| 4091 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
| 4092 | if (partitions == NULL) |
| 4093 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4094 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4095 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4096 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4097 | for (i = 0;i < nbgroups;i++) { |
| 4098 | group = groups[i]; |
| 4099 | for (j = i+1;j < nbgroups;j++) { |
| 4100 | if (groups[j] == NULL) |
| 4101 | continue; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4102 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4103 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4104 | groups[j]->defs); |
| 4105 | if (ret == 0) { |
| 4106 | if (ctxt->error != NULL) |
| 4107 | ctxt->error(ctxt->userData, |
| 4108 | "Element or text conflicts in interleave\n"); |
| 4109 | ctxt->nbErrors++; |
| 4110 | } |
| 4111 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4112 | groups[j]->attrs); |
| 4113 | if (ret == 0) { |
| 4114 | if (ctxt->error != NULL) |
| 4115 | ctxt->error(ctxt->userData, |
| 4116 | "Attributes conflicts in interleave\n"); |
| 4117 | ctxt->nbErrors++; |
| 4118 | } |
| 4119 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4120 | tmp = group->defs; |
| 4121 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4122 | while (*tmp != NULL) { |
| 4123 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4124 | res = xmlHashAddEntry2(partitions->triage, |
| 4125 | BAD_CAST "#text", NULL, |
| 4126 | (void *)(i + 1)); |
| 4127 | if (res != 0) |
| 4128 | is_determinist = -1; |
| 4129 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4130 | ((*tmp)->name != NULL)) { |
| 4131 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4132 | res = xmlHashAddEntry2(partitions->triage, |
| 4133 | (*tmp)->name, NULL, |
| 4134 | (void *)(i + 1)); |
| 4135 | else |
| 4136 | res = xmlHashAddEntry2(partitions->triage, |
| 4137 | (*tmp)->name, (*tmp)->ns, |
| 4138 | (void *)(i + 1)); |
| 4139 | if (res != 0) |
| 4140 | is_determinist = -1; |
| 4141 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4142 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4143 | res = xmlHashAddEntry2(partitions->triage, |
| 4144 | BAD_CAST "#any", NULL, |
| 4145 | (void *)(i + 1)); |
| 4146 | else |
| 4147 | res = xmlHashAddEntry2(partitions->triage, |
| 4148 | BAD_CAST "#any", (*tmp)->ns, |
| 4149 | (void *)(i + 1)); |
| 4150 | if ((*tmp)->nameClass != NULL) |
| 4151 | is_determinist = 2; |
| 4152 | if (res != 0) |
| 4153 | is_determinist = -1; |
| 4154 | } else { |
| 4155 | is_determinist = -1; |
| 4156 | } |
| 4157 | tmp++; |
| 4158 | } |
| 4159 | } else { |
| 4160 | is_determinist = 0; |
| 4161 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4162 | } |
| 4163 | partitions->groups = groups; |
| 4164 | |
| 4165 | /* |
| 4166 | * and save the partition list back in the def |
| 4167 | */ |
| 4168 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4169 | if (is_mixed != 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4170 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4171 | if (is_determinist == 1) |
| 4172 | partitions->flags = IS_DETERMINIST; |
| 4173 | if (is_determinist == 2) |
| 4174 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4175 | return; |
| 4176 | |
| 4177 | error: |
| 4178 | if (ctxt->error != NULL) |
| 4179 | ctxt->error(ctxt->userData, |
| 4180 | "Out of memory in interleave computation\n"); |
| 4181 | ctxt->nbErrors++; |
| 4182 | if (groups != NULL) { |
| 4183 | for (i = 0;i < nbgroups;i++) |
| 4184 | if (groups[i] != NULL) { |
| 4185 | if (groups[i]->defs != NULL) |
| 4186 | xmlFree(groups[i]->defs); |
| 4187 | xmlFree(groups[i]); |
| 4188 | } |
| 4189 | xmlFree(groups); |
| 4190 | } |
| 4191 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | /** |
| 4195 | * xmlRelaxNGParseInterleave: |
| 4196 | * @ctxt: a Relax-NG parser context |
| 4197 | * @node: the data node. |
| 4198 | * |
| 4199 | * parse the content of a RelaxNG interleave node. |
| 4200 | * |
| 4201 | * Returns the definition pointer or NULL in case of error |
| 4202 | */ |
| 4203 | static xmlRelaxNGDefinePtr |
| 4204 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4205 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4206 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4207 | xmlNodePtr child; |
| 4208 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4209 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4210 | if (def == NULL) { |
| 4211 | return(NULL); |
| 4212 | } |
| 4213 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4214 | |
| 4215 | if (ctxt->interleaves == NULL) |
| 4216 | ctxt->interleaves = xmlHashCreate(10); |
| 4217 | if (ctxt->interleaves == NULL) { |
| 4218 | if (ctxt->error != NULL) |
| 4219 | ctxt->error(ctxt->userData, |
| 4220 | "Failed to create interleaves hash table\n"); |
| 4221 | ctxt->nbErrors++; |
| 4222 | } else { |
| 4223 | char name[32]; |
| 4224 | |
| 4225 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4226 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4227 | if (ctxt->error != NULL) |
| 4228 | ctxt->error(ctxt->userData, |
| 4229 | "Failed to add %s to hash table\n", name); |
| 4230 | ctxt->nbErrors++; |
| 4231 | } |
| 4232 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4233 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4234 | if (child == NULL) { |
| 4235 | if (ctxt->error != NULL) |
| 4236 | ctxt->error(ctxt->userData, "Element interleave is empty\n"); |
| 4237 | ctxt->nbErrors++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4238 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4239 | while (child != NULL) { |
| 4240 | if (IS_RELAXNG(child, "element")) { |
| 4241 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4242 | } else { |
| 4243 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4244 | } |
| 4245 | if (cur != NULL) { |
| 4246 | cur->parent = def; |
| 4247 | if (last == NULL) { |
| 4248 | def->content = last = cur; |
| 4249 | } else { |
| 4250 | last->next = cur; |
| 4251 | last = cur; |
| 4252 | } |
| 4253 | } |
| 4254 | child = child->next; |
| 4255 | } |
| 4256 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4257 | return(def); |
| 4258 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4259 | |
| 4260 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4261 | * xmlRelaxNGParseInclude: |
| 4262 | * @ctxt: a Relax-NG parser context |
| 4263 | * @node: the include node |
| 4264 | * |
| 4265 | * Integrate the content of an include node in the current grammar |
| 4266 | * |
| 4267 | * Returns 0 in case of success or -1 in case of error |
| 4268 | */ |
| 4269 | static int |
| 4270 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4271 | xmlRelaxNGIncludePtr incl; |
| 4272 | xmlNodePtr root; |
| 4273 | int ret = 0, tmp; |
| 4274 | |
| 4275 | incl = node->_private; |
| 4276 | if (incl == NULL) { |
| 4277 | if (ctxt->error != NULL) |
| 4278 | ctxt->error(ctxt->userData, |
| 4279 | "Include node has no data\n"); |
| 4280 | ctxt->nbErrors++; |
| 4281 | return(-1); |
| 4282 | } |
| 4283 | root = xmlDocGetRootElement(incl->doc); |
| 4284 | if (root == NULL) { |
| 4285 | if (ctxt->error != NULL) |
| 4286 | ctxt->error(ctxt->userData, |
| 4287 | "Include document is empty\n"); |
| 4288 | ctxt->nbErrors++; |
| 4289 | return(-1); |
| 4290 | } |
| 4291 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
| 4292 | if (ctxt->error != NULL) |
| 4293 | ctxt->error(ctxt->userData, |
| 4294 | "Include document root is not a grammar\n"); |
| 4295 | ctxt->nbErrors++; |
| 4296 | return(-1); |
| 4297 | } |
| 4298 | |
| 4299 | /* |
| 4300 | * Merge the definition from both the include and the internal list |
| 4301 | */ |
| 4302 | if (root->children != NULL) { |
| 4303 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4304 | if (tmp != 0) |
| 4305 | ret = -1; |
| 4306 | } |
| 4307 | if (node->children != NULL) { |
| 4308 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4309 | if (tmp != 0) |
| 4310 | ret = -1; |
| 4311 | } |
| 4312 | return(ret); |
| 4313 | } |
| 4314 | |
| 4315 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4316 | * xmlRelaxNGParseDefine: |
| 4317 | * @ctxt: a Relax-NG parser context |
| 4318 | * @node: the define node |
| 4319 | * |
| 4320 | * parse the content of a RelaxNG define element node. |
| 4321 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4322 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4323 | */ |
| 4324 | static int |
| 4325 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4326 | xmlChar *name; |
| 4327 | int ret = 0, tmp; |
| 4328 | xmlRelaxNGDefinePtr def; |
| 4329 | const xmlChar *olddefine; |
| 4330 | |
| 4331 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4332 | if (name == NULL) { |
| 4333 | if (ctxt->error != NULL) |
| 4334 | ctxt->error(ctxt->userData, |
| 4335 | "define has no name\n"); |
| 4336 | ctxt->nbErrors++; |
| 4337 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4338 | xmlRelaxNGNormExtSpace(name); |
| 4339 | if (xmlValidateNCName(name, 0)) { |
| 4340 | if (ctxt->error != NULL) |
| 4341 | ctxt->error(ctxt->userData, |
| 4342 | "define name '%s' is not an NCName\n", |
| 4343 | name); |
| 4344 | ctxt->nbErrors++; |
| 4345 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4346 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4347 | if (def == NULL) { |
| 4348 | xmlFree(name); |
| 4349 | return(-1); |
| 4350 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4351 | def->type = XML_RELAXNG_DEF; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4352 | def->name = name; |
| 4353 | if (node->children == NULL) { |
| 4354 | if (ctxt->error != NULL) |
| 4355 | ctxt->error(ctxt->userData, |
| 4356 | "define has no children\n"); |
| 4357 | ctxt->nbErrors++; |
| 4358 | } else { |
| 4359 | olddefine = ctxt->define; |
| 4360 | ctxt->define = name; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4361 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4362 | ctxt->define = olddefine; |
| 4363 | } |
| 4364 | if (ctxt->grammar->defs == NULL) |
| 4365 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4366 | if (ctxt->grammar->defs == NULL) { |
| 4367 | if (ctxt->error != NULL) |
| 4368 | ctxt->error(ctxt->userData, |
| 4369 | "Could not create definition hash\n"); |
| 4370 | ctxt->nbErrors++; |
| 4371 | ret = -1; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4372 | } else { |
| 4373 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4374 | if (tmp < 0) { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4375 | xmlRelaxNGDefinePtr prev; |
| 4376 | |
| 4377 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4378 | if (prev == NULL) { |
| 4379 | if (ctxt->error != NULL) |
| 4380 | ctxt->error(ctxt->userData, |
| 4381 | "Internal error on define aggregation of %s\n", |
| 4382 | name); |
| 4383 | ctxt->nbErrors++; |
| 4384 | ret = -1; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4385 | } else { |
| 4386 | while (prev->nextHash != NULL) |
| 4387 | prev = prev->nextHash; |
| 4388 | prev->nextHash = def; |
| 4389 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4390 | } |
| 4391 | } |
| 4392 | } |
| 4393 | return(ret); |
| 4394 | } |
| 4395 | |
| 4396 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4397 | * xmlRelaxNGProcessExternalRef: |
| 4398 | * @ctxt: the parser context |
| 4399 | * @node: the externlRef node |
| 4400 | * |
| 4401 | * Process and compile an externlRef node |
| 4402 | * |
| 4403 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4404 | */ |
| 4405 | static xmlRelaxNGDefinePtr |
| 4406 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4407 | xmlRelaxNGDocumentPtr docu; |
| 4408 | xmlNodePtr root, tmp; |
| 4409 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4410 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4411 | xmlRelaxNGDefinePtr def; |
| 4412 | |
| 4413 | docu = node->_private; |
| 4414 | if (docu != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4415 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4416 | if (def == NULL) |
| 4417 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4418 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4419 | |
| 4420 | if (docu->content == NULL) { |
| 4421 | /* |
| 4422 | * Then do the parsing for good |
| 4423 | */ |
| 4424 | root = xmlDocGetRootElement(docu->doc); |
| 4425 | if (root == NULL) { |
| 4426 | if (ctxt->error != NULL) |
| 4427 | ctxt->error(ctxt->userData, |
| 4428 | "xmlRelaxNGParse: %s is empty\n", |
| 4429 | ctxt->URL); |
| 4430 | ctxt->nbErrors++; |
| 4431 | return (NULL); |
| 4432 | } |
| 4433 | /* |
| 4434 | * ns transmission rules |
| 4435 | */ |
| 4436 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4437 | if (ns == NULL) { |
| 4438 | tmp = node; |
| 4439 | while ((tmp != NULL) && |
| 4440 | (tmp->type == XML_ELEMENT_NODE)) { |
| 4441 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4442 | if (ns != NULL) { |
| 4443 | break; |
| 4444 | } |
| 4445 | tmp = tmp->parent; |
| 4446 | } |
| 4447 | if (ns != NULL) { |
| 4448 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4449 | newNs = 1; |
| 4450 | xmlFree(ns); |
| 4451 | } |
| 4452 | } else { |
| 4453 | xmlFree(ns); |
| 4454 | } |
| 4455 | |
| 4456 | /* |
| 4457 | * Parsing to get a precompiled schemas. |
| 4458 | */ |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4459 | oldflags = ctxt->flags; |
| 4460 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4461 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4462 | ctxt->flags = oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4463 | if ((docu->schema != NULL) && |
| 4464 | (docu->schema->topgrammar != NULL)) { |
| 4465 | docu->content = docu->schema->topgrammar->start; |
| 4466 | } |
| 4467 | |
| 4468 | /* |
| 4469 | * the externalRef may be reused in a different ns context |
| 4470 | */ |
| 4471 | if (newNs == 1) { |
| 4472 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4473 | } |
| 4474 | } |
| 4475 | def->content = docu->content; |
| 4476 | } else { |
| 4477 | def = NULL; |
| 4478 | } |
| 4479 | return(def); |
| 4480 | } |
| 4481 | |
| 4482 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4483 | * xmlRelaxNGParsePattern: |
| 4484 | * @ctxt: a Relax-NG parser context |
| 4485 | * @node: the pattern node. |
| 4486 | * |
| 4487 | * parse the content of a RelaxNG pattern node. |
| 4488 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4489 | * Returns the definition pointer or NULL in case of error or if no |
| 4490 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4491 | */ |
| 4492 | static xmlRelaxNGDefinePtr |
| 4493 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4494 | xmlRelaxNGDefinePtr def = NULL; |
| 4495 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4496 | if (node == NULL) { |
| 4497 | return(NULL); |
| 4498 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4499 | if (IS_RELAXNG(node, "element")) { |
| 4500 | def = xmlRelaxNGParseElement(ctxt, node); |
| 4501 | } else if (IS_RELAXNG(node, "attribute")) { |
| 4502 | def = xmlRelaxNGParseAttribute(ctxt, node); |
| 4503 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4504 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4505 | if (def == NULL) |
| 4506 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4507 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4508 | if (node->children != NULL) { |
| 4509 | if (ctxt->error != NULL) |
| 4510 | ctxt->error(ctxt->userData, "empty: had a child node\n"); |
| 4511 | ctxt->nbErrors++; |
| 4512 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4513 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4514 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4515 | if (def == NULL) |
| 4516 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4517 | def->type = XML_RELAXNG_TEXT; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4518 | if (node->children != NULL) { |
| 4519 | if (ctxt->error != NULL) |
| 4520 | ctxt->error(ctxt->userData, "text: had a child node\n"); |
| 4521 | ctxt->nbErrors++; |
| 4522 | } |
| 4523 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4524 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4525 | if (def == NULL) |
| 4526 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4527 | def->type = XML_RELAXNG_ZEROORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4528 | if (node->children == NULL) { |
| 4529 | if (ctxt->error != NULL) |
| 4530 | ctxt->error(ctxt->userData, |
| 4531 | "Element %s is empty\n", node->name); |
| 4532 | ctxt->nbErrors++; |
| 4533 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4534 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4535 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4536 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4537 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4538 | if (def == NULL) |
| 4539 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4540 | def->type = XML_RELAXNG_ONEORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4541 | if (node->children == NULL) { |
| 4542 | if (ctxt->error != NULL) |
| 4543 | ctxt->error(ctxt->userData, |
| 4544 | "Element %s is empty\n", node->name); |
| 4545 | ctxt->nbErrors++; |
| 4546 | } else { |
| 4547 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4548 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4549 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4550 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4551 | if (def == NULL) |
| 4552 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4553 | def->type = XML_RELAXNG_OPTIONAL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4554 | if (node->children == NULL) { |
| 4555 | if (ctxt->error != NULL) |
| 4556 | ctxt->error(ctxt->userData, |
| 4557 | "Element %s is empty\n", node->name); |
| 4558 | ctxt->nbErrors++; |
| 4559 | } else { |
| 4560 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4561 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4562 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4563 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4564 | if (def == NULL) |
| 4565 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4566 | def->type = XML_RELAXNG_CHOICE; |
| 4567 | if (node->children == NULL) { |
| 4568 | if (ctxt->error != NULL) |
| 4569 | ctxt->error(ctxt->userData, |
| 4570 | "Element %s is empty\n", node->name); |
| 4571 | ctxt->nbErrors++; |
| 4572 | } else { |
| 4573 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4574 | } |
| 4575 | } else if (IS_RELAXNG(node, "group")) { |
| 4576 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4577 | if (def == NULL) |
| 4578 | return(NULL); |
| 4579 | def->type = XML_RELAXNG_GROUP; |
| 4580 | if (node->children == NULL) { |
| 4581 | if (ctxt->error != NULL) |
| 4582 | ctxt->error(ctxt->userData, |
| 4583 | "Element %s is empty\n", node->name); |
| 4584 | ctxt->nbErrors++; |
| 4585 | } else { |
| 4586 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4587 | } |
| 4588 | } else if (IS_RELAXNG(node, "ref")) { |
| 4589 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4590 | if (def == NULL) |
| 4591 | return(NULL); |
| 4592 | def->type = XML_RELAXNG_REF; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4593 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4594 | if (def->name == NULL) { |
| 4595 | if (ctxt->error != NULL) |
| 4596 | ctxt->error(ctxt->userData, |
| 4597 | "ref has no name\n"); |
| 4598 | ctxt->nbErrors++; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4599 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4600 | xmlRelaxNGNormExtSpace(def->name); |
| 4601 | if (xmlValidateNCName(def->name, 0)) { |
| 4602 | if (ctxt->error != NULL) |
| 4603 | ctxt->error(ctxt->userData, |
| 4604 | "ref name '%s' is not an NCName\n", |
| 4605 | def->name); |
| 4606 | ctxt->nbErrors++; |
| 4607 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4608 | } |
| 4609 | if (node->children != NULL) { |
| 4610 | if (ctxt->error != NULL) |
| 4611 | ctxt->error(ctxt->userData, |
| 4612 | "ref is not empty\n"); |
| 4613 | ctxt->nbErrors++; |
| 4614 | } |
| 4615 | if (ctxt->grammar->refs == NULL) |
| 4616 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4617 | if (ctxt->grammar->refs == NULL) { |
| 4618 | if (ctxt->error != NULL) |
| 4619 | ctxt->error(ctxt->userData, |
| 4620 | "Could not create references hash\n"); |
| 4621 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4622 | def = NULL; |
| 4623 | } else { |
| 4624 | int tmp; |
| 4625 | |
| 4626 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4627 | if (tmp < 0) { |
| 4628 | xmlRelaxNGDefinePtr prev; |
| 4629 | |
| 4630 | prev = (xmlRelaxNGDefinePtr) |
| 4631 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4632 | if (prev == NULL) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4633 | if (def->name != NULL) { |
| 4634 | if (ctxt->error != NULL) |
| 4635 | ctxt->error(ctxt->userData, |
| 4636 | "Error refs definitions '%s'\n", |
| 4637 | def->name); |
| 4638 | } else { |
| 4639 | if (ctxt->error != NULL) |
| 4640 | ctxt->error(ctxt->userData, |
| 4641 | "Error refs definitions\n"); |
| 4642 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4643 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4644 | def = NULL; |
| 4645 | } else { |
| 4646 | def->nextHash = prev->nextHash; |
| 4647 | prev->nextHash = def; |
| 4648 | } |
| 4649 | } |
| 4650 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4651 | } else if (IS_RELAXNG(node, "data")) { |
| 4652 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4653 | } else if (IS_RELAXNG(node, "value")) { |
| 4654 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4655 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4656 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4657 | if (def == NULL) |
| 4658 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4659 | def->type = XML_RELAXNG_LIST; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4660 | if (node->children == NULL) { |
| 4661 | if (ctxt->error != NULL) |
| 4662 | ctxt->error(ctxt->userData, |
| 4663 | "Element %s is empty\n", node->name); |
| 4664 | ctxt->nbErrors++; |
| 4665 | } else { |
| 4666 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4667 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4668 | } else if (IS_RELAXNG(node, "interleave")) { |
| 4669 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4670 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4671 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4672 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4673 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4674 | if (def == NULL) |
| 4675 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4676 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4677 | if (node->children != NULL) { |
| 4678 | if (ctxt->error != NULL) |
| 4679 | ctxt->error(ctxt->userData, |
| 4680 | "xmlRelaxNGParse: notAllowed element is not empty\n"); |
| 4681 | ctxt->nbErrors++; |
| 4682 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4683 | } else if (IS_RELAXNG(node, "grammar")) { |
| 4684 | xmlRelaxNGGrammarPtr grammar, old; |
| 4685 | xmlRelaxNGGrammarPtr oldparent; |
| 4686 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4687 | #ifdef DEBUG_GRAMMAR |
| 4688 | xmlGenericError(xmlGenericErrorContext, "Found <grammar> pattern\n"); |
| 4689 | #endif |
| 4690 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4691 | oldparent = ctxt->parentgrammar; |
| 4692 | old = ctxt->grammar; |
| 4693 | ctxt->parentgrammar = old; |
| 4694 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4695 | if (old != NULL) { |
| 4696 | ctxt->grammar = old; |
| 4697 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4698 | #if 0 |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4699 | if (grammar != NULL) { |
| 4700 | grammar->next = old->next; |
| 4701 | old->next = grammar; |
| 4702 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4703 | #endif |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4704 | } |
| 4705 | if (grammar != NULL) |
| 4706 | def = grammar->start; |
| 4707 | else |
| 4708 | def = NULL; |
| 4709 | } else if (IS_RELAXNG(node, "parentRef")) { |
| 4710 | if (ctxt->parentgrammar == NULL) { |
| 4711 | if (ctxt->error != NULL) |
| 4712 | ctxt->error(ctxt->userData, |
| 4713 | "Use of parentRef without a parent grammar\n"); |
| 4714 | ctxt->nbErrors++; |
| 4715 | return(NULL); |
| 4716 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4717 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4718 | if (def == NULL) |
| 4719 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4720 | def->type = XML_RELAXNG_PARENTREF; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4721 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4722 | if (def->name == NULL) { |
| 4723 | if (ctxt->error != NULL) |
| 4724 | ctxt->error(ctxt->userData, |
| 4725 | "parentRef has no name\n"); |
| 4726 | ctxt->nbErrors++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4727 | } else { |
| 4728 | xmlRelaxNGNormExtSpace(def->name); |
| 4729 | if (xmlValidateNCName(def->name, 0)) { |
| 4730 | if (ctxt->error != NULL) |
| 4731 | ctxt->error(ctxt->userData, |
| 4732 | "parentRef name '%s' is not an NCName\n", |
| 4733 | def->name); |
| 4734 | ctxt->nbErrors++; |
| 4735 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4736 | } |
| 4737 | if (node->children != NULL) { |
| 4738 | if (ctxt->error != NULL) |
| 4739 | ctxt->error(ctxt->userData, |
| 4740 | "parentRef is not empty\n"); |
| 4741 | ctxt->nbErrors++; |
| 4742 | } |
| 4743 | if (ctxt->parentgrammar->refs == NULL) |
| 4744 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4745 | if (ctxt->parentgrammar->refs == NULL) { |
| 4746 | if (ctxt->error != NULL) |
| 4747 | ctxt->error(ctxt->userData, |
| 4748 | "Could not create references hash\n"); |
| 4749 | ctxt->nbErrors++; |
| 4750 | def = NULL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4751 | } else if (def->name != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4752 | int tmp; |
| 4753 | |
| 4754 | tmp = xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4755 | if (tmp < 0) { |
| 4756 | xmlRelaxNGDefinePtr prev; |
| 4757 | |
| 4758 | prev = (xmlRelaxNGDefinePtr) |
| 4759 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4760 | if (prev == NULL) { |
| 4761 | if (ctxt->error != NULL) |
| 4762 | ctxt->error(ctxt->userData, |
| 4763 | "Internal error parentRef definitions '%s'\n", |
| 4764 | def->name); |
| 4765 | ctxt->nbErrors++; |
| 4766 | def = NULL; |
| 4767 | } else { |
| 4768 | def->nextHash = prev->nextHash; |
| 4769 | prev->nextHash = def; |
| 4770 | } |
| 4771 | } |
| 4772 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4773 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4774 | if (node->children == NULL) { |
| 4775 | if (ctxt->error != NULL) |
| 4776 | ctxt->error(ctxt->userData, |
| 4777 | "Mixed is empty\n"); |
| 4778 | ctxt->nbErrors++; |
| 4779 | def = NULL; |
| 4780 | } else { |
| 4781 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4782 | if (def != NULL) { |
| 4783 | xmlRelaxNGDefinePtr tmp; |
| 4784 | |
| 4785 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4786 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4787 | if (tmp != NULL) { |
| 4788 | tmp->type = XML_RELAXNG_GROUP; |
| 4789 | tmp->content = def->content; |
| 4790 | def->content = tmp; |
| 4791 | } |
| 4792 | } |
| 4793 | |
| 4794 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4795 | if (tmp == NULL) |
| 4796 | return(def); |
| 4797 | tmp->type = XML_RELAXNG_TEXT; |
| 4798 | tmp->next = def->content; |
| 4799 | def->content = tmp; |
| 4800 | } |
| 4801 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4802 | } else { |
| 4803 | if (ctxt->error != NULL) |
| 4804 | ctxt->error(ctxt->userData, |
| 4805 | "Unexpected node %s is not a pattern\n", |
| 4806 | node->name); |
| 4807 | ctxt->nbErrors++; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4808 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4809 | } |
| 4810 | return(def); |
| 4811 | } |
| 4812 | |
| 4813 | /** |
| 4814 | * xmlRelaxNGParseAttribute: |
| 4815 | * @ctxt: a Relax-NG parser context |
| 4816 | * @node: the element node |
| 4817 | * |
| 4818 | * parse the content of a RelaxNG attribute node. |
| 4819 | * |
| 4820 | * Returns the definition pointer or NULL in case of error. |
| 4821 | */ |
| 4822 | static xmlRelaxNGDefinePtr |
| 4823 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4824 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4825 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4826 | int old_flags; |
| 4827 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4828 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4829 | if (ret == NULL) |
| 4830 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4831 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4832 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4833 | child = node->children; |
| 4834 | if (child == NULL) { |
| 4835 | if (ctxt->error != NULL) |
| 4836 | ctxt->error(ctxt->userData, |
| 4837 | "xmlRelaxNGParseattribute: attribute has no children\n"); |
| 4838 | ctxt->nbErrors++; |
| 4839 | return(ret); |
| 4840 | } |
| 4841 | old_flags = ctxt->flags; |
| 4842 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4843 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4844 | if (cur != NULL) |
| 4845 | child = child->next; |
| 4846 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4847 | if (child != NULL) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4848 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4849 | if (cur != NULL) { |
| 4850 | switch (cur->type) { |
| 4851 | case XML_RELAXNG_EMPTY: |
| 4852 | case XML_RELAXNG_NOT_ALLOWED: |
| 4853 | case XML_RELAXNG_TEXT: |
| 4854 | case XML_RELAXNG_ELEMENT: |
| 4855 | case XML_RELAXNG_DATATYPE: |
| 4856 | case XML_RELAXNG_VALUE: |
| 4857 | case XML_RELAXNG_LIST: |
| 4858 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4859 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4860 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4861 | case XML_RELAXNG_DEF: |
| 4862 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4863 | case XML_RELAXNG_ZEROORMORE: |
| 4864 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4865 | case XML_RELAXNG_CHOICE: |
| 4866 | case XML_RELAXNG_GROUP: |
| 4867 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4868 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4869 | ret->content = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4870 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4871 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4872 | case XML_RELAXNG_START: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 4873 | case XML_RELAXNG_PARAM: |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4874 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4875 | if (ctxt->error != NULL) |
| 4876 | ctxt->error(ctxt->userData, |
| 4877 | "attribute has invalid content\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 4878 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4879 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4880 | case XML_RELAXNG_NOOP: |
| 4881 | TODO |
| 4882 | if (ctxt->error != NULL) |
| 4883 | ctxt->error(ctxt->userData, |
| 4884 | "Internal error, noop found\n"); |
| 4885 | ctxt->nbErrors++; |
| 4886 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4887 | } |
| 4888 | } |
| 4889 | child = child->next; |
| 4890 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4891 | if (child != NULL) { |
| 4892 | if (ctxt->error != NULL) |
| 4893 | ctxt->error(ctxt->userData, "attribute has multiple children\n"); |
| 4894 | ctxt->nbErrors++; |
| 4895 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4896 | ctxt->flags = old_flags; |
| 4897 | return(ret); |
| 4898 | } |
| 4899 | |
| 4900 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4901 | * xmlRelaxNGParseExceptNameClass: |
| 4902 | * @ctxt: a Relax-NG parser context |
| 4903 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4904 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4905 | * |
| 4906 | * parse the content of a RelaxNG nameClass node. |
| 4907 | * |
| 4908 | * Returns the definition pointer or NULL in case of error. |
| 4909 | */ |
| 4910 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4911 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
| 4912 | xmlNodePtr node, int attr) { |
| 4913 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 4914 | xmlNodePtr child; |
| 4915 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4916 | if (!IS_RELAXNG(node, "except")) { |
| 4917 | if (ctxt->error != NULL) |
| 4918 | ctxt->error(ctxt->userData, |
| 4919 | "Expecting an except node\n"); |
| 4920 | ctxt->nbErrors++; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4921 | return(NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4922 | } |
| 4923 | if (node->next != NULL) { |
| 4924 | if (ctxt->error != NULL) |
| 4925 | ctxt->error(ctxt->userData, |
| 4926 | "exceptNameClass allows only a single except node\n"); |
| 4927 | ctxt->nbErrors++; |
| 4928 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4929 | if (node->children == NULL) { |
| 4930 | if (ctxt->error != NULL) |
| 4931 | ctxt->error(ctxt->userData, |
| 4932 | "except has no content\n"); |
| 4933 | ctxt->nbErrors++; |
| 4934 | return(NULL); |
| 4935 | } |
| 4936 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4937 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4938 | if (ret == NULL) |
| 4939 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4940 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4941 | child = node->children; |
| 4942 | while (child != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4943 | cur = xmlRelaxNGNewDefine(ctxt, child); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4944 | if (cur == NULL) |
| 4945 | break; |
| 4946 | if (attr) |
| 4947 | cur->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4948 | else |
| 4949 | cur->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4950 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4951 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4952 | if (last == NULL) { |
| 4953 | ret->content = cur; |
| 4954 | } else { |
| 4955 | last->next = cur; |
| 4956 | } |
| 4957 | last = cur; |
| 4958 | } |
| 4959 | child = child->next; |
| 4960 | } |
| 4961 | |
| 4962 | return(ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | /** |
| 4966 | * xmlRelaxNGParseNameClass: |
| 4967 | * @ctxt: a Relax-NG parser context |
| 4968 | * @node: the nameClass node |
| 4969 | * @def: the current definition |
| 4970 | * |
| 4971 | * parse the content of a RelaxNG nameClass node. |
| 4972 | * |
| 4973 | * Returns the definition pointer or NULL in case of error. |
| 4974 | */ |
| 4975 | static xmlRelaxNGDefinePtr |
| 4976 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 4977 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4978 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4979 | xmlChar *val; |
| 4980 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4981 | ret = def; |
| 4982 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
| 4983 | (IS_RELAXNG(node, "nsName"))) { |
| 4984 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 4985 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4986 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4987 | if (ret == NULL) |
| 4988 | return(NULL); |
| 4989 | ret->parent = def; |
| 4990 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 4991 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4992 | else |
| 4993 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4994 | } |
| 4995 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4996 | if (IS_RELAXNG(node, "name")) { |
| 4997 | val = xmlNodeGetContent(node); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4998 | xmlRelaxNGNormExtSpace(val); |
| 4999 | if (xmlValidateNCName(val, 0)) { |
| 5000 | if (ctxt->error != NULL) { |
| 5001 | if (node->parent != NULL) |
| 5002 | ctxt->error(ctxt->userData, |
| 5003 | "Element %s name '%s' is not an NCName\n", |
| 5004 | node->parent->name, val); |
| 5005 | else |
| 5006 | ctxt->error(ctxt->userData, |
| 5007 | "name '%s' is not an NCName\n", |
| 5008 | val); |
| 5009 | } |
| 5010 | ctxt->nbErrors++; |
| 5011 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5012 | ret->name = val; |
| 5013 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5014 | ret->ns = val; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 5015 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5016 | (val != NULL) && |
| 5017 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5018 | ctxt->error(ctxt->userData, |
| 5019 | "Attribute with namespace '%s' is not allowed\n", |
| 5020 | val); |
| 5021 | ctxt->nbErrors++; |
| 5022 | } |
| 5023 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5024 | (val != NULL) && |
| 5025 | (val[0] == 0) && |
| 5026 | (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
| 5027 | ctxt->error(ctxt->userData, |
| 5028 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5029 | val); |
| 5030 | ctxt->nbErrors++; |
| 5031 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5032 | } else if (IS_RELAXNG(node, "anyName")) { |
| 5033 | ret->name = NULL; |
| 5034 | ret->ns = NULL; |
| 5035 | if (node->children != NULL) { |
| 5036 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5037 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5038 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5039 | } |
| 5040 | } else if (IS_RELAXNG(node, "nsName")) { |
| 5041 | ret->name = NULL; |
| 5042 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5043 | if (ret->ns == NULL) { |
| 5044 | if (ctxt->error != NULL) |
| 5045 | ctxt->error(ctxt->userData, |
| 5046 | "nsName has no ns attribute\n"); |
| 5047 | ctxt->nbErrors++; |
| 5048 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 5049 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5050 | (ret->ns != NULL) && |
| 5051 | (xmlStrEqual(ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5052 | ctxt->error(ctxt->userData, |
| 5053 | "Attribute with namespace '%s' is not allowed\n", |
| 5054 | ret->ns); |
| 5055 | ctxt->nbErrors++; |
| 5056 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5057 | if (node->children != NULL) { |
| 5058 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5059 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5060 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5061 | } |
| 5062 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5063 | xmlNodePtr child; |
| 5064 | xmlRelaxNGDefinePtr last = NULL; |
| 5065 | |
| 5066 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5067 | if (ret == NULL) |
| 5068 | return(NULL); |
| 5069 | ret->parent = def; |
| 5070 | ret->type = XML_RELAXNG_CHOICE; |
| 5071 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5072 | if (node->children == NULL) { |
| 5073 | if (ctxt->error != NULL) |
| 5074 | ctxt->error(ctxt->userData, |
| 5075 | "Element choice is empty\n"); |
| 5076 | ctxt->nbErrors++; |
| 5077 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5078 | |
| 5079 | child = node->children; |
| 5080 | while (child != NULL) { |
| 5081 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5082 | if (tmp != NULL) { |
| 5083 | if (last == NULL) { |
| 5084 | last = ret->nameClass = tmp; |
| 5085 | } else { |
| 5086 | last->next = tmp; |
| 5087 | last = tmp; |
| 5088 | } |
| 5089 | } |
| 5090 | child = child->next; |
| 5091 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5092 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5093 | } else { |
| 5094 | if (ctxt->error != NULL) |
| 5095 | ctxt->error(ctxt->userData, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5096 | "expecting name, anyName, nsName or choice : got %s\n", |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5097 | node->name); |
| 5098 | ctxt->nbErrors++; |
| 5099 | return(NULL); |
| 5100 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5101 | if (ret != def) { |
| 5102 | if (def->nameClass == NULL) { |
| 5103 | def->nameClass = ret; |
| 5104 | } else { |
| 5105 | tmp = def->nameClass; |
| 5106 | while (tmp->next != NULL) { |
| 5107 | tmp = tmp->next; |
| 5108 | } |
| 5109 | tmp->next = ret; |
| 5110 | } |
| 5111 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5112 | return(ret); |
| 5113 | } |
| 5114 | |
| 5115 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5116 | * xmlRelaxNGParseElement: |
| 5117 | * @ctxt: a Relax-NG parser context |
| 5118 | * @node: the element node |
| 5119 | * |
| 5120 | * parse the content of a RelaxNG element node. |
| 5121 | * |
| 5122 | * Returns the definition pointer or NULL in case of error. |
| 5123 | */ |
| 5124 | static xmlRelaxNGDefinePtr |
| 5125 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 5126 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5127 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5128 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5129 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5130 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5131 | if (ret == NULL) |
| 5132 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5133 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5134 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5135 | child = node->children; |
| 5136 | if (child == NULL) { |
| 5137 | if (ctxt->error != NULL) |
| 5138 | ctxt->error(ctxt->userData, |
| 5139 | "xmlRelaxNGParseElement: element has no children\n"); |
| 5140 | ctxt->nbErrors++; |
| 5141 | return(ret); |
| 5142 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5143 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5144 | if (cur != NULL) |
| 5145 | child = child->next; |
| 5146 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5147 | if (child == NULL) { |
| 5148 | if (ctxt->error != NULL) |
| 5149 | ctxt->error(ctxt->userData, |
| 5150 | "xmlRelaxNGParseElement: element has no content\n"); |
| 5151 | ctxt->nbErrors++; |
| 5152 | return(ret); |
| 5153 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5154 | olddefine = ctxt->define; |
| 5155 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5156 | last = NULL; |
| 5157 | while (child != NULL) { |
| 5158 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5159 | if (cur != NULL) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5160 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5161 | switch (cur->type) { |
| 5162 | case XML_RELAXNG_EMPTY: |
| 5163 | case XML_RELAXNG_NOT_ALLOWED: |
| 5164 | case XML_RELAXNG_TEXT: |
| 5165 | case XML_RELAXNG_ELEMENT: |
| 5166 | case XML_RELAXNG_DATATYPE: |
| 5167 | case XML_RELAXNG_VALUE: |
| 5168 | case XML_RELAXNG_LIST: |
| 5169 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5170 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5171 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5172 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5173 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5174 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5175 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5176 | case XML_RELAXNG_CHOICE: |
| 5177 | case XML_RELAXNG_GROUP: |
| 5178 | case XML_RELAXNG_INTERLEAVE: |
| 5179 | if (last == NULL) { |
| 5180 | ret->content = last = cur; |
| 5181 | } else { |
| 5182 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5183 | (ret->content == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5184 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5185 | if (ret->content != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5186 | ret->content->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5187 | ret->content->content = last; |
| 5188 | } else { |
| 5189 | ret->content = last; |
| 5190 | } |
| 5191 | } |
| 5192 | last->next = cur; |
| 5193 | last = cur; |
| 5194 | } |
| 5195 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5196 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5197 | /* HERE !!! */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5198 | cur->next = ret->attrs; |
| 5199 | ret->attrs = cur; |
| 5200 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5201 | case XML_RELAXNG_START: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 5202 | case XML_RELAXNG_PARAM: |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5203 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5204 | TODO |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5205 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5206 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5207 | case XML_RELAXNG_NOOP: |
| 5208 | TODO |
| 5209 | if (ctxt->error != NULL) |
| 5210 | ctxt->error(ctxt->userData, |
| 5211 | "Internal error, noop found\n"); |
| 5212 | ctxt->nbErrors++; |
| 5213 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5214 | } |
| 5215 | } |
| 5216 | child = child->next; |
| 5217 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5218 | ctxt->define = olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5219 | return(ret); |
| 5220 | } |
| 5221 | |
| 5222 | /** |
| 5223 | * xmlRelaxNGParsePatterns: |
| 5224 | * @ctxt: a Relax-NG parser context |
| 5225 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5226 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5227 | * |
| 5228 | * parse the content of a RelaxNG start node. |
| 5229 | * |
| 5230 | * Returns the definition pointer or NULL in case of error. |
| 5231 | */ |
| 5232 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5233 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
| 5234 | int group) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5235 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5236 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5237 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5238 | while (nodes != NULL) { |
| 5239 | if (IS_RELAXNG(nodes, "element")) { |
| 5240 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5241 | if (def == NULL) { |
| 5242 | def = last = cur; |
| 5243 | } else { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5244 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5245 | (def == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5246 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5247 | def->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5248 | def->content = last; |
| 5249 | } |
| 5250 | last->next = cur; |
| 5251 | last = cur; |
| 5252 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5253 | cur->parent = parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5254 | } else { |
| 5255 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5256 | if (cur != NULL) { |
| 5257 | if (def == NULL) { |
| 5258 | def = last = cur; |
| 5259 | } else { |
| 5260 | last->next = cur; |
| 5261 | last = cur; |
| 5262 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5263 | } |
| 5264 | } |
| 5265 | nodes = nodes->next; |
| 5266 | } |
| 5267 | return(def); |
| 5268 | } |
| 5269 | |
| 5270 | /** |
| 5271 | * xmlRelaxNGParseStart: |
| 5272 | * @ctxt: a Relax-NG parser context |
| 5273 | * @nodes: start children nodes |
| 5274 | * |
| 5275 | * parse the content of a RelaxNG start node. |
| 5276 | * |
| 5277 | * Returns 0 in case of success, -1 in case of error |
| 5278 | */ |
| 5279 | static int |
| 5280 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 5281 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5282 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5283 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5284 | if (nodes == NULL) { |
| 5285 | if (ctxt->error != NULL) |
| 5286 | ctxt->error(ctxt->userData, |
| 5287 | "start has no children\n"); |
| 5288 | ctxt->nbErrors++; |
| 5289 | return(-1); |
| 5290 | } |
| 5291 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5292 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5293 | if (def == NULL) |
| 5294 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5295 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5296 | if (nodes->children != NULL) { |
| 5297 | if (ctxt->error != NULL) |
| 5298 | ctxt->error(ctxt->userData, "element empty is not empty\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5299 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5300 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5301 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5302 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5303 | if (def == NULL) |
| 5304 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5305 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5306 | if (nodes->children != NULL) { |
| 5307 | if (ctxt->error != NULL) |
| 5308 | ctxt->error(ctxt->userData, |
| 5309 | "element notAllowed is not empty\n"); |
| 5310 | ctxt->nbErrors++; |
| 5311 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5312 | } else { |
| 5313 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5314 | } |
| 5315 | if (ctxt->grammar->start != NULL) { |
| 5316 | last = ctxt->grammar->start; |
| 5317 | while (last->next != NULL) |
| 5318 | last = last->next; |
| 5319 | last->next = def; |
| 5320 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5321 | ctxt->grammar->start = def; |
| 5322 | } |
| 5323 | nodes = nodes->next; |
| 5324 | if (nodes != NULL) { |
| 5325 | if (ctxt->error != NULL) |
| 5326 | ctxt->error(ctxt->userData, |
| 5327 | "start more than one children\n"); |
| 5328 | ctxt->nbErrors++; |
| 5329 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5330 | } |
| 5331 | return(ret); |
| 5332 | } |
| 5333 | |
| 5334 | /** |
| 5335 | * xmlRelaxNGParseGrammarContent: |
| 5336 | * @ctxt: a Relax-NG parser context |
| 5337 | * @nodes: grammar children nodes |
| 5338 | * |
| 5339 | * parse the content of a RelaxNG grammar node. |
| 5340 | * |
| 5341 | * Returns 0 in case of success, -1 in case of error |
| 5342 | */ |
| 5343 | static int |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5344 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5345 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5346 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5347 | |
| 5348 | if (nodes == NULL) { |
| 5349 | if (ctxt->error != NULL) |
| 5350 | ctxt->error(ctxt->userData, |
| 5351 | "grammar has no children\n"); |
| 5352 | ctxt->nbErrors++; |
| 5353 | return(-1); |
| 5354 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5355 | while (nodes != NULL) { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5356 | if (IS_RELAXNG(nodes, "start")) { |
| 5357 | if (nodes->children == NULL) { |
| 5358 | if (ctxt->error != NULL) |
| 5359 | ctxt->error(ctxt->userData, |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5360 | "start has no children\n"); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5361 | ctxt->nbErrors++; |
| 5362 | } else { |
| 5363 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5364 | if (tmp != 0) |
| 5365 | ret = -1; |
| 5366 | } |
| 5367 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5368 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5369 | if (tmp != 0) |
| 5370 | ret = -1; |
| 5371 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5372 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5373 | if (tmp != 0) |
| 5374 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5375 | } else { |
| 5376 | if (ctxt->error != NULL) |
| 5377 | ctxt->error(ctxt->userData, |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5378 | "grammar has unexpected child %s\n", nodes->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5379 | ctxt->nbErrors++; |
| 5380 | ret = -1; |
| 5381 | } |
| 5382 | nodes = nodes->next; |
| 5383 | } |
| 5384 | return (ret); |
| 5385 | } |
| 5386 | |
| 5387 | /** |
| 5388 | * xmlRelaxNGCheckReference: |
| 5389 | * @ref: the ref |
| 5390 | * @ctxt: a Relax-NG parser context |
| 5391 | * @name: the name associated to the defines |
| 5392 | * |
| 5393 | * Applies the 4.17. combine attribute rule for all the define |
| 5394 | * element of a given grammar using the same name. |
| 5395 | */ |
| 5396 | static void |
| 5397 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
| 5398 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5399 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5400 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5401 | |
| 5402 | grammar = ctxt->grammar; |
| 5403 | if (grammar == NULL) { |
| 5404 | if (ctxt->error != NULL) |
| 5405 | ctxt->error(ctxt->userData, |
| 5406 | "Internal error: no grammar in CheckReference %s\n", |
| 5407 | name); |
| 5408 | ctxt->nbErrors++; |
| 5409 | return; |
| 5410 | } |
| 5411 | if (ref->content != NULL) { |
| 5412 | if (ctxt->error != NULL) |
| 5413 | ctxt->error(ctxt->userData, |
| 5414 | "Internal error: reference has content in CheckReference %s\n", |
| 5415 | name); |
| 5416 | ctxt->nbErrors++; |
| 5417 | return; |
| 5418 | } |
| 5419 | if (grammar->defs != NULL) { |
| 5420 | def = xmlHashLookup(grammar->defs, name); |
| 5421 | if (def != NULL) { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5422 | cur = ref; |
| 5423 | while (cur != NULL) { |
| 5424 | cur->content = def; |
| 5425 | cur = cur->nextHash; |
| 5426 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5427 | } else { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5428 | if (ctxt->error != NULL) |
| 5429 | ctxt->error(ctxt->userData, |
| 5430 | "Reference %s has no matching definition\n", |
| 5431 | name); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5432 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5433 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5434 | } else { |
| 5435 | if (ctxt->error != NULL) |
| 5436 | ctxt->error(ctxt->userData, |
| 5437 | "Reference %s has no matching definition\n", |
| 5438 | name); |
| 5439 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5440 | } |
| 5441 | /* |
| 5442 | * TODO: make a closure and verify there is no loop ! |
| 5443 | */ |
| 5444 | } |
| 5445 | |
| 5446 | /** |
| 5447 | * xmlRelaxNGCheckCombine: |
| 5448 | * @define: the define(s) list |
| 5449 | * @ctxt: a Relax-NG parser context |
| 5450 | * @name: the name associated to the defines |
| 5451 | * |
| 5452 | * Applies the 4.17. combine attribute rule for all the define |
| 5453 | * element of a given grammar using the same name. |
| 5454 | */ |
| 5455 | static void |
| 5456 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
| 5457 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5458 | xmlChar *combine; |
| 5459 | int choiceOrInterleave = -1; |
| 5460 | int missing = 0; |
| 5461 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5462 | |
| 5463 | if (define->nextHash == NULL) |
| 5464 | return; |
| 5465 | cur = define; |
| 5466 | while (cur != NULL) { |
| 5467 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5468 | if (combine != NULL) { |
| 5469 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5470 | if (choiceOrInterleave == -1) |
| 5471 | choiceOrInterleave = 1; |
| 5472 | else if (choiceOrInterleave == 0) { |
| 5473 | if (ctxt->error != NULL) |
| 5474 | ctxt->error(ctxt->userData, |
| 5475 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5476 | name); |
| 5477 | ctxt->nbErrors++; |
| 5478 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5479 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5480 | if (choiceOrInterleave == -1) |
| 5481 | choiceOrInterleave = 0; |
| 5482 | else if (choiceOrInterleave == 1) { |
| 5483 | if (ctxt->error != NULL) |
| 5484 | ctxt->error(ctxt->userData, |
| 5485 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5486 | name); |
| 5487 | ctxt->nbErrors++; |
| 5488 | } |
| 5489 | } else { |
| 5490 | if (ctxt->error != NULL) |
| 5491 | ctxt->error(ctxt->userData, |
| 5492 | "Defines for %s use unknown combine value '%s''\n", |
| 5493 | name, combine); |
| 5494 | ctxt->nbErrors++; |
| 5495 | } |
| 5496 | xmlFree(combine); |
| 5497 | } else { |
| 5498 | if (missing == 0) |
| 5499 | missing = 1; |
| 5500 | else { |
| 5501 | if (ctxt->error != NULL) |
| 5502 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5503 | "Some defines for %s needs the combine attribute\n", |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5504 | name); |
| 5505 | ctxt->nbErrors++; |
| 5506 | } |
| 5507 | } |
| 5508 | |
| 5509 | cur = cur->nextHash; |
| 5510 | } |
| 5511 | #ifdef DEBUG |
| 5512 | xmlGenericError(xmlGenericErrorContext, |
| 5513 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5514 | name, choiceOrInterleave); |
| 5515 | #endif |
| 5516 | if (choiceOrInterleave == -1) |
| 5517 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5518 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5519 | if (cur == NULL) |
| 5520 | return; |
| 5521 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5522 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5523 | else |
| 5524 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5525 | tmp = define; |
| 5526 | last = NULL; |
| 5527 | while (tmp != NULL) { |
| 5528 | if (tmp->content != NULL) { |
| 5529 | if (tmp->content->next != NULL) { |
| 5530 | /* |
| 5531 | * we need first to create a wrapper. |
| 5532 | */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5533 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5534 | if (tmp2 == NULL) |
| 5535 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5536 | tmp2->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5537 | tmp2->content = tmp->content; |
| 5538 | } else { |
| 5539 | tmp2 = tmp->content; |
| 5540 | } |
| 5541 | if (last == NULL) { |
| 5542 | cur->content = tmp2; |
| 5543 | } else { |
| 5544 | last->next = tmp2; |
| 5545 | } |
| 5546 | last = tmp2; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5547 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 5548 | tmp->content = cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5549 | tmp = tmp->nextHash; |
| 5550 | } |
| 5551 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5552 | if (choiceOrInterleave == 0) { |
| 5553 | if (ctxt->interleaves == NULL) |
| 5554 | ctxt->interleaves = xmlHashCreate(10); |
| 5555 | if (ctxt->interleaves == NULL) { |
| 5556 | if (ctxt->error != NULL) |
| 5557 | ctxt->error(ctxt->userData, |
| 5558 | "Failed to create interleaves hash table\n"); |
| 5559 | ctxt->nbErrors++; |
| 5560 | } else { |
| 5561 | char tmpname[32]; |
| 5562 | |
| 5563 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5564 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5565 | if (ctxt->error != NULL) |
| 5566 | ctxt->error(ctxt->userData, |
| 5567 | "Failed to add %s to hash table\n", tmpname); |
| 5568 | ctxt->nbErrors++; |
| 5569 | } |
| 5570 | } |
| 5571 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5572 | } |
| 5573 | |
| 5574 | /** |
| 5575 | * xmlRelaxNGCombineStart: |
| 5576 | * @ctxt: a Relax-NG parser context |
| 5577 | * @grammar: the grammar |
| 5578 | * |
| 5579 | * Applies the 4.17. combine rule for all the start |
| 5580 | * element of a given grammar. |
| 5581 | */ |
| 5582 | static void |
| 5583 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
| 5584 | xmlRelaxNGGrammarPtr grammar) { |
| 5585 | xmlRelaxNGDefinePtr starts; |
| 5586 | xmlChar *combine; |
| 5587 | int choiceOrInterleave = -1; |
| 5588 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5589 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5590 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5591 | starts = grammar->start; |
| 5592 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5593 | return; |
| 5594 | cur = starts; |
| 5595 | while (cur != NULL) { |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5596 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5597 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5598 | combine = NULL; |
| 5599 | if (ctxt->error != NULL) |
| 5600 | ctxt->error(ctxt->userData, |
| 5601 | "Internal error: start element not found\n"); |
| 5602 | ctxt->nbErrors++; |
| 5603 | } else { |
| 5604 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5605 | } |
| 5606 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5607 | if (combine != NULL) { |
| 5608 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5609 | if (choiceOrInterleave == -1) |
| 5610 | choiceOrInterleave = 1; |
| 5611 | else if (choiceOrInterleave == 0) { |
| 5612 | if (ctxt->error != NULL) |
| 5613 | ctxt->error(ctxt->userData, |
| 5614 | "<start> use both 'choice' and 'interleave'\n"); |
| 5615 | ctxt->nbErrors++; |
| 5616 | } |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5617 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5618 | if (choiceOrInterleave == -1) |
| 5619 | choiceOrInterleave = 0; |
| 5620 | else if (choiceOrInterleave == 1) { |
| 5621 | if (ctxt->error != NULL) |
| 5622 | ctxt->error(ctxt->userData, |
| 5623 | "<start> use both 'choice' and 'interleave'\n"); |
| 5624 | ctxt->nbErrors++; |
| 5625 | } |
| 5626 | } else { |
| 5627 | if (ctxt->error != NULL) |
| 5628 | ctxt->error(ctxt->userData, |
| 5629 | "<start> uses unknown combine value '%s''\n", combine); |
| 5630 | ctxt->nbErrors++; |
| 5631 | } |
| 5632 | xmlFree(combine); |
| 5633 | } else { |
| 5634 | if (missing == 0) |
| 5635 | missing = 1; |
| 5636 | else { |
| 5637 | if (ctxt->error != NULL) |
| 5638 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5639 | "Some <start> element miss the combine attribute\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5640 | ctxt->nbErrors++; |
| 5641 | } |
| 5642 | } |
| 5643 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5644 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5645 | } |
| 5646 | #ifdef DEBUG |
| 5647 | xmlGenericError(xmlGenericErrorContext, |
| 5648 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5649 | choiceOrInterleave); |
| 5650 | #endif |
| 5651 | if (choiceOrInterleave == -1) |
| 5652 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5653 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5654 | if (cur == NULL) |
| 5655 | return; |
| 5656 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5657 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5658 | else |
| 5659 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5660 | cur->content = grammar->start; |
| 5661 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5662 | if (choiceOrInterleave == 0) { |
| 5663 | if (ctxt->interleaves == NULL) |
| 5664 | ctxt->interleaves = xmlHashCreate(10); |
| 5665 | if (ctxt->interleaves == NULL) { |
| 5666 | if (ctxt->error != NULL) |
| 5667 | ctxt->error(ctxt->userData, |
| 5668 | "Failed to create interleaves hash table\n"); |
| 5669 | ctxt->nbErrors++; |
| 5670 | } else { |
| 5671 | char tmpname[32]; |
| 5672 | |
| 5673 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5674 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5675 | if (ctxt->error != NULL) |
| 5676 | ctxt->error(ctxt->userData, |
| 5677 | "Failed to add %s to hash table\n", tmpname); |
| 5678 | ctxt->nbErrors++; |
| 5679 | } |
| 5680 | } |
| 5681 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5682 | } |
| 5683 | |
| 5684 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5685 | * xmlRelaxNGCheckCycles: |
| 5686 | * @ctxt: a Relax-NG parser context |
| 5687 | * @nodes: grammar children nodes |
| 5688 | * @depth: the counter |
| 5689 | * |
| 5690 | * Check for cycles. |
| 5691 | * |
| 5692 | * Returns 0 if check passed, and -1 in case of error |
| 5693 | */ |
| 5694 | static int |
| 5695 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5696 | xmlRelaxNGDefinePtr cur, int depth) { |
| 5697 | int ret = 0; |
| 5698 | |
| 5699 | while ((ret == 0) && (cur != NULL)) { |
| 5700 | if ((cur->type == XML_RELAXNG_REF) || |
| 5701 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5702 | if (cur->depth == -1) { |
| 5703 | cur->depth = depth; |
| 5704 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5705 | cur->depth = -2; |
| 5706 | } else if (depth == cur->depth) { |
| 5707 | if (ctxt->error != NULL) |
| 5708 | ctxt->error(ctxt->userData, |
| 5709 | "Detected a cycle in %s references\n", cur->name); |
| 5710 | ctxt->nbErrors++; |
| 5711 | return(-1); |
| 5712 | } |
| 5713 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5714 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5715 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5716 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5717 | } |
| 5718 | cur = cur->next; |
| 5719 | } |
| 5720 | return(ret); |
| 5721 | } |
| 5722 | |
| 5723 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5724 | * xmlRelaxNGTryUnlink: |
| 5725 | * @ctxt: a Relax-NG parser context |
| 5726 | * @cur: the definition to unlink |
| 5727 | * @parent: the parent definition |
| 5728 | * @prev: the previous sibling definition |
| 5729 | * |
| 5730 | * Try to unlink a definition. If not possble make it a NOOP |
| 5731 | * |
| 5732 | * Returns the new prev definition |
| 5733 | */ |
| 5734 | static xmlRelaxNGDefinePtr |
| 5735 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5736 | xmlRelaxNGDefinePtr cur, |
| 5737 | xmlRelaxNGDefinePtr parent, |
| 5738 | xmlRelaxNGDefinePtr prev) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5739 | if (prev != NULL) { |
| 5740 | prev->next = cur->next; |
| 5741 | } else { |
| 5742 | if (parent != NULL) { |
| 5743 | if (parent->content == cur) |
| 5744 | parent->content = cur->next; |
| 5745 | else if (parent->attrs == cur) |
| 5746 | parent->attrs = cur->next; |
| 5747 | else if (parent->nameClass == cur) |
| 5748 | parent->nameClass = cur->next; |
| 5749 | } else { |
| 5750 | cur->type = XML_RELAXNG_NOOP; |
| 5751 | prev = cur; |
| 5752 | } |
| 5753 | } |
| 5754 | return(prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5755 | } |
| 5756 | |
| 5757 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5758 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5759 | * @ctxt: a Relax-NG parser context |
| 5760 | * @nodes: grammar children nodes |
| 5761 | * |
| 5762 | * Check for simplification of empty and notAllowed |
| 5763 | */ |
| 5764 | static void |
| 5765 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5766 | xmlRelaxNGDefinePtr cur, |
| 5767 | xmlRelaxNGDefinePtr parent) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5768 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5769 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5770 | while (cur != NULL) { |
| 5771 | if ((cur->type == XML_RELAXNG_REF) || |
| 5772 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5773 | if (cur->depth != -3) { |
| 5774 | cur->depth = -3; |
| 5775 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5776 | } |
| 5777 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5778 | cur->parent = parent; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5779 | if ((parent != NULL) && |
| 5780 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5781 | (parent->type == XML_RELAXNG_LIST) || |
| 5782 | (parent->type == XML_RELAXNG_GROUP) || |
| 5783 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5784 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5785 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5786 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5787 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5788 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5789 | if ((parent != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5790 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5791 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5792 | } else |
| 5793 | prev = cur; |
| 5794 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5795 | cur->parent = parent; |
| 5796 | if ((parent != NULL) && |
| 5797 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5798 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5799 | parent->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5800 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5801 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5802 | if ((parent != NULL) && |
| 5803 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5804 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5805 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5806 | } else |
| 5807 | prev = cur; |
| 5808 | } else { |
| 5809 | cur->parent = parent; |
| 5810 | if (cur->content != NULL) |
| 5811 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5812 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5813 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5814 | if (cur->nameClass != NULL) |
| 5815 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5816 | /* |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5817 | * On Elements, try to move attribute only generating rules on |
| 5818 | * the attrs rules. |
| 5819 | */ |
| 5820 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5821 | int attronly; |
| 5822 | xmlRelaxNGDefinePtr tmp, pre; |
| 5823 | |
| 5824 | while (cur->content != NULL) { |
| 5825 | attronly = xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 5826 | if (attronly == 1) { |
| 5827 | /* |
| 5828 | * migrate cur->content to attrs |
| 5829 | */ |
| 5830 | tmp = cur->content; |
| 5831 | cur->content = tmp->next; |
| 5832 | tmp->next = cur->attrs; |
| 5833 | cur->attrs = tmp; |
| 5834 | } else { |
| 5835 | /* |
| 5836 | * cur->content can generate elements or text |
| 5837 | */ |
| 5838 | break; |
| 5839 | } |
| 5840 | } |
| 5841 | pre = cur->content; |
| 5842 | while ((pre != NULL) && (pre->next != NULL)) { |
| 5843 | tmp = pre->next; |
| 5844 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 5845 | if (attronly == 1) { |
| 5846 | /* |
| 5847 | * migrate tmp to attrs |
| 5848 | */ |
| 5849 | pre->next = tmp->next; |
| 5850 | tmp->next = cur->attrs; |
| 5851 | cur->attrs = tmp; |
| 5852 | } else { |
| 5853 | pre = tmp; |
| 5854 | } |
| 5855 | } |
| 5856 | } |
| 5857 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5858 | * This may result in a simplification |
| 5859 | */ |
| 5860 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 5861 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 5862 | if (cur->content == NULL) |
| 5863 | cur->type = XML_RELAXNG_EMPTY; |
| 5864 | else if (cur->content->next == NULL) { |
| 5865 | if ((parent == NULL) && (prev == NULL)) { |
| 5866 | cur->type = XML_RELAXNG_NOOP; |
| 5867 | } else if (prev == NULL) { |
| 5868 | parent->content = cur->content; |
| 5869 | cur->content->next = cur->next; |
| 5870 | cur = cur->content; |
| 5871 | } else { |
| 5872 | cur->content->next = cur->next; |
| 5873 | prev->next = cur->content; |
| 5874 | cur = cur->content; |
| 5875 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5876 | } |
| 5877 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5878 | /* |
| 5879 | * the current node may have been transformed back |
| 5880 | */ |
| 5881 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 5882 | (cur->content != NULL) && |
| 5883 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 5884 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5885 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5886 | if ((parent != NULL) && |
| 5887 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5888 | (parent->type == XML_RELAXNG_LIST) || |
| 5889 | (parent->type == XML_RELAXNG_GROUP) || |
| 5890 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5891 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5892 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5893 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5894 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5895 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5896 | if ((parent != NULL) && |
| 5897 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5898 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5899 | } else |
| 5900 | prev = cur; |
| 5901 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5902 | if ((parent != NULL) && |
| 5903 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5904 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5905 | parent->type = XML_RELAXNG_EMPTY; |
| 5906 | break; |
| 5907 | } |
| 5908 | if ((parent != NULL) && |
| 5909 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5910 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5911 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 5912 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5913 | } else |
| 5914 | prev = cur; |
| 5915 | } else { |
| 5916 | prev = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5917 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5918 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5919 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5920 | } |
| 5921 | } |
| 5922 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5923 | /** |
| 5924 | * xmlRelaxNGGroupContentType: |
| 5925 | * @ct1: the first content type |
| 5926 | * @ct2: the second content type |
| 5927 | * |
| 5928 | * Try to group 2 content types |
| 5929 | * |
| 5930 | * Returns the content type |
| 5931 | */ |
| 5932 | static xmlRelaxNGContentType |
| 5933 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
| 5934 | xmlRelaxNGContentType ct2) { |
| 5935 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 5936 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 5937 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5938 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
| 5939 | return(ct2); |
| 5940 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
| 5941 | return(ct1); |
| 5942 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
| 5943 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 5944 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 5945 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5946 | } |
| 5947 | |
| 5948 | /** |
| 5949 | * xmlRelaxNGMaxContentType: |
| 5950 | * @ct1: the first content type |
| 5951 | * @ct2: the second content type |
| 5952 | * |
| 5953 | * Compute the max content-type |
| 5954 | * |
| 5955 | * Returns the content type |
| 5956 | */ |
| 5957 | static xmlRelaxNGContentType |
| 5958 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
| 5959 | xmlRelaxNGContentType ct2) { |
| 5960 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 5961 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 5962 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5963 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
| 5964 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 5965 | return(XML_RELAXNG_CONTENT_SIMPLE); |
| 5966 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
| 5967 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 5968 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 5969 | return(XML_RELAXNG_CONTENT_EMPTY); |
| 5970 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5971 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5972 | /** |
| 5973 | * xmlRelaxNGCheckRules: |
| 5974 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5975 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5976 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5977 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5978 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5979 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5980 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5981 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5982 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5983 | static xmlRelaxNGContentType |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5984 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 5985 | xmlRelaxNGDefinePtr cur, int flags, |
| 5986 | xmlRelaxNGType ptype) { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5987 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5988 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5989 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5990 | while (cur != NULL) { |
| 5991 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 5992 | if ((cur->type == XML_RELAXNG_REF) || |
| 5993 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5994 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5995 | if (ctxt->error != NULL) |
| 5996 | ctxt->error(ctxt->userData, |
| 5997 | "Found forbidden pattern list//ref\n"); |
| 5998 | ctxt->nbErrors++; |
| 5999 | } |
| 6000 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6001 | if (ctxt->error != NULL) |
| 6002 | ctxt->error(ctxt->userData, |
| 6003 | "Found forbidden pattern data/except//ref\n"); |
| 6004 | ctxt->nbErrors++; |
| 6005 | } |
| 6006 | if (cur->depth > -4) { |
| 6007 | cur->depth = -4; |
| 6008 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6009 | flags, cur->type); |
| 6010 | cur->depth = ret - 15 ; |
| 6011 | } else if (cur->depth == -4) { |
| 6012 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6013 | } else { |
| 6014 | ret = (xmlRelaxNGContentType) cur->depth + 15; |
| 6015 | } |
| 6016 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6017 | /* |
| 6018 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6019 | */ |
| 6020 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6021 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6022 | if (ctxt->error != NULL) |
| 6023 | ctxt->error(ctxt->userData, |
| 6024 | "Found forbidden pattern data/except//element(ref)\n"); |
| 6025 | ctxt->nbErrors++; |
| 6026 | } |
| 6027 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6028 | if (ctxt->error != NULL) |
| 6029 | ctxt->error(ctxt->userData, |
| 6030 | "Found forbidden pattern list//element(ref)\n"); |
| 6031 | ctxt->nbErrors++; |
| 6032 | } |
| 6033 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6034 | if (ctxt->error != NULL) |
| 6035 | ctxt->error(ctxt->userData, |
| 6036 | "Found forbidden pattern attribute//element(ref)\n"); |
| 6037 | ctxt->nbErrors++; |
| 6038 | } |
| 6039 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6040 | if (ctxt->error != NULL) |
| 6041 | ctxt->error(ctxt->userData, |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 6042 | "Found forbidden pattern attribute//element(ref)\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6043 | ctxt->nbErrors++; |
| 6044 | } |
| 6045 | /* |
| 6046 | * reset since in the simple form elements are only child |
| 6047 | * of grammar/define |
| 6048 | */ |
| 6049 | nflags = 0; |
| 6050 | ret = xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6051 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6052 | if (ctxt->error != NULL) |
| 6053 | ctxt->error(ctxt->userData, |
| 6054 | "Element %s attributes have a content type error\n", |
| 6055 | cur->name); |
| 6056 | ctxt->nbErrors++; |
| 6057 | } |
| 6058 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6059 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6060 | if (ctxt->error != NULL) |
| 6061 | ctxt->error(ctxt->userData, |
| 6062 | "Element %s has a content type error\n", |
| 6063 | cur->name); |
| 6064 | ctxt->nbErrors++; |
| 6065 | } else { |
| 6066 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6067 | } |
| 6068 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6069 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6070 | if (ctxt->error != NULL) |
| 6071 | ctxt->error(ctxt->userData, |
| 6072 | "Found forbidden pattern attribute//attribute\n"); |
| 6073 | ctxt->nbErrors++; |
| 6074 | } |
| 6075 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6076 | if (ctxt->error != NULL) |
| 6077 | ctxt->error(ctxt->userData, |
| 6078 | "Found forbidden pattern list//attribute\n"); |
| 6079 | ctxt->nbErrors++; |
| 6080 | } |
| 6081 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6082 | if (ctxt->error != NULL) |
| 6083 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6084 | "Found forbidden pattern oneOrMore//group//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6085 | ctxt->nbErrors++; |
| 6086 | } |
| 6087 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6088 | if (ctxt->error != NULL) |
| 6089 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6090 | "Found forbidden pattern oneOrMore//interleave//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6091 | ctxt->nbErrors++; |
| 6092 | } |
| 6093 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6094 | if (ctxt->error != NULL) |
| 6095 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6096 | "Found forbidden pattern data/except//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6097 | ctxt->nbErrors++; |
| 6098 | } |
| 6099 | if (flags & XML_RELAXNG_IN_START) { |
| 6100 | if (ctxt->error != NULL) |
| 6101 | ctxt->error(ctxt->userData, |
| 6102 | "Found forbidden pattern start//attribute\n"); |
| 6103 | ctxt->nbErrors++; |
| 6104 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 6105 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) && (cur->name == NULL)) { |
| 6106 | if (cur->ns == NULL) { |
| 6107 | if (ctxt->error != NULL) |
| 6108 | ctxt->error(ctxt->userData, |
| 6109 | "Found anyName attribute without oneOrMore ancestor\n"); |
| 6110 | ctxt->nbErrors++; |
| 6111 | } else { |
| 6112 | if (ctxt->error != NULL) |
| 6113 | ctxt->error(ctxt->userData, |
| 6114 | "Found nsName attribute without oneOrMore ancestor\n"); |
| 6115 | ctxt->nbErrors++; |
| 6116 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6117 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6118 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6119 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6120 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6121 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6122 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6123 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6124 | if (ctxt->error != NULL) |
| 6125 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6126 | "Found forbidden pattern data/except//oneOrMore\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6127 | ctxt->nbErrors++; |
| 6128 | } |
| 6129 | if (flags & XML_RELAXNG_IN_START) { |
| 6130 | if (ctxt->error != NULL) |
| 6131 | ctxt->error(ctxt->userData, |
| 6132 | "Found forbidden pattern start//oneOrMore\n"); |
| 6133 | ctxt->nbErrors++; |
| 6134 | } |
| 6135 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6136 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6137 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6138 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6139 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6140 | if (ctxt->error != NULL) |
| 6141 | ctxt->error(ctxt->userData, |
| 6142 | "Found forbidden pattern list//list\n"); |
| 6143 | ctxt->nbErrors++; |
| 6144 | } |
| 6145 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6146 | if (ctxt->error != NULL) |
| 6147 | ctxt->error(ctxt->userData, |
| 6148 | "Found forbidden pattern data/except//list\n"); |
| 6149 | ctxt->nbErrors++; |
| 6150 | } |
| 6151 | if (flags & XML_RELAXNG_IN_START) { |
| 6152 | if (ctxt->error != NULL) |
| 6153 | ctxt->error(ctxt->userData, |
| 6154 | "Found forbidden pattern start//list\n"); |
| 6155 | ctxt->nbErrors++; |
| 6156 | } |
| 6157 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6158 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6159 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6160 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6161 | if (ctxt->error != NULL) |
| 6162 | ctxt->error(ctxt->userData, |
| 6163 | "Found forbidden pattern data/except//group\n"); |
| 6164 | ctxt->nbErrors++; |
| 6165 | } |
| 6166 | if (flags & XML_RELAXNG_IN_START) { |
| 6167 | if (ctxt->error != NULL) |
| 6168 | ctxt->error(ctxt->userData, |
| 6169 | "Found forbidden pattern start//group\n"); |
| 6170 | ctxt->nbErrors++; |
| 6171 | } |
| 6172 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6173 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6174 | else |
| 6175 | nflags = flags; |
| 6176 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6177 | /* |
| 6178 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6179 | */ |
| 6180 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6181 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6182 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6183 | if (ctxt->error != NULL) |
| 6184 | ctxt->error(ctxt->userData, |
| 6185 | "Found forbidden pattern list//interleave\n"); |
| 6186 | ctxt->nbErrors++; |
| 6187 | } |
| 6188 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6189 | if (ctxt->error != NULL) |
| 6190 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6191 | "Found forbidden pattern data/except//interleave\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6192 | ctxt->nbErrors++; |
| 6193 | } |
| 6194 | if (flags & XML_RELAXNG_IN_START) { |
| 6195 | if (ctxt->error != NULL) |
| 6196 | ctxt->error(ctxt->userData, |
| 6197 | "Found forbidden pattern start//interleave\n"); |
| 6198 | ctxt->nbErrors++; |
| 6199 | } |
| 6200 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6201 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6202 | else |
| 6203 | nflags = flags; |
| 6204 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6205 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6206 | if ((cur->parent != NULL) && |
| 6207 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6208 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6209 | else |
| 6210 | nflags = flags; |
| 6211 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6212 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6213 | if (flags & XML_RELAXNG_IN_START) { |
| 6214 | if (ctxt->error != NULL) |
| 6215 | ctxt->error(ctxt->userData, |
| 6216 | "Found forbidden pattern start//data\n"); |
| 6217 | ctxt->nbErrors++; |
| 6218 | } |
| 6219 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6220 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6221 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6222 | if (flags & XML_RELAXNG_IN_START) { |
| 6223 | if (ctxt->error != NULL) |
| 6224 | ctxt->error(ctxt->userData, |
| 6225 | "Found forbidden pattern start//value\n"); |
| 6226 | ctxt->nbErrors++; |
| 6227 | } |
| 6228 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6229 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6230 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6231 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6232 | if (ctxt->error != NULL) |
| 6233 | ctxt->error(ctxt->userData, |
| 6234 | "Found forbidden pattern list//text\n"); |
| 6235 | ctxt->nbErrors++; |
| 6236 | } |
| 6237 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6238 | if (ctxt->error != NULL) |
| 6239 | ctxt->error(ctxt->userData, |
| 6240 | "Found forbidden pattern data/except//text\n"); |
| 6241 | ctxt->nbErrors++; |
| 6242 | } |
| 6243 | if (flags & XML_RELAXNG_IN_START) { |
| 6244 | if (ctxt->error != NULL) |
| 6245 | ctxt->error(ctxt->userData, |
| 6246 | "Found forbidden pattern start//text\n"); |
| 6247 | ctxt->nbErrors++; |
| 6248 | } |
| 6249 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6250 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6251 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6252 | if (ctxt->error != NULL) |
| 6253 | ctxt->error(ctxt->userData, |
| 6254 | "Found forbidden pattern data/except//empty\n"); |
| 6255 | ctxt->nbErrors++; |
| 6256 | } |
| 6257 | if (flags & XML_RELAXNG_IN_START) { |
| 6258 | if (ctxt->error != NULL) |
| 6259 | ctxt->error(ctxt->userData, |
| 6260 | "Found forbidden pattern start//empty\n"); |
| 6261 | ctxt->nbErrors++; |
| 6262 | } |
| 6263 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6264 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6265 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6266 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6267 | } else { |
| 6268 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6269 | } |
| 6270 | cur = cur->next; |
| 6271 | if (ptype == XML_RELAXNG_GROUP) { |
| 6272 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6273 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6274 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6275 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6276 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6277 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6278 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6279 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6280 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6281 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6282 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6283 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6284 | else |
| 6285 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6286 | } else { |
| 6287 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6288 | } |
| 6289 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6290 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6291 | return(val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6292 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6293 | |
| 6294 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6295 | * xmlRelaxNGParseGrammar: |
| 6296 | * @ctxt: a Relax-NG parser context |
| 6297 | * @nodes: grammar children nodes |
| 6298 | * |
| 6299 | * parse a Relax-NG <grammar> node |
| 6300 | * |
| 6301 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6302 | * NULL in case of error |
| 6303 | */ |
| 6304 | static xmlRelaxNGGrammarPtr |
| 6305 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 6306 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6307 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6308 | #ifdef DEBUG_GRAMMAR |
| 6309 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6310 | #endif |
| 6311 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6312 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6313 | if (ret == NULL) |
| 6314 | return(NULL); |
| 6315 | |
| 6316 | /* |
| 6317 | * Link the new grammar in the tree |
| 6318 | */ |
| 6319 | ret->parent = ctxt->grammar; |
| 6320 | if (ctxt->grammar != NULL) { |
| 6321 | tmp = ctxt->grammar->children; |
| 6322 | if (tmp == NULL) { |
| 6323 | ctxt->grammar->children = ret; |
| 6324 | } else { |
| 6325 | while (tmp->next != NULL) |
| 6326 | tmp = tmp->next; |
| 6327 | tmp->next = ret; |
| 6328 | } |
| 6329 | } |
| 6330 | |
| 6331 | old = ctxt->grammar; |
| 6332 | ctxt->grammar = ret; |
| 6333 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6334 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6335 | if (ctxt->grammar == NULL) { |
| 6336 | if (ctxt->error != NULL) |
| 6337 | ctxt->error(ctxt->userData, |
| 6338 | "Failed to parse <grammar> content\n"); |
| 6339 | ctxt->nbErrors++; |
| 6340 | } else if (ctxt->grammar->start == NULL) { |
| 6341 | if (ctxt->error != NULL) |
| 6342 | ctxt->error(ctxt->userData, |
| 6343 | "Element <grammar> has no <start>\n"); |
| 6344 | ctxt->nbErrors++; |
| 6345 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6346 | |
| 6347 | /* |
| 6348 | * Apply 4.17 mergingd rules to defines and starts |
| 6349 | */ |
| 6350 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6351 | if (ret->defs != NULL) { |
| 6352 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6353 | ctxt); |
| 6354 | } |
| 6355 | |
| 6356 | /* |
| 6357 | * link together defines and refs in this grammar |
| 6358 | */ |
| 6359 | if (ret->refs != NULL) { |
| 6360 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6361 | ctxt); |
| 6362 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6363 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6364 | ctxt->grammar = old; |
| 6365 | return(ret); |
| 6366 | } |
| 6367 | |
| 6368 | /** |
| 6369 | * xmlRelaxNGParseDocument: |
| 6370 | * @ctxt: a Relax-NG parser context |
| 6371 | * @node: the root node of the RelaxNG schema |
| 6372 | * |
| 6373 | * parse a Relax-NG definition resource and build an internal |
| 6374 | * xmlRelaxNG struture which can be used to validate instances. |
| 6375 | * |
| 6376 | * Returns the internal XML RelaxNG structure built or |
| 6377 | * NULL in case of error |
| 6378 | */ |
| 6379 | static xmlRelaxNGPtr |
| 6380 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6381 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6382 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6383 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6384 | |
| 6385 | if ((ctxt == NULL) || (node == NULL)) |
| 6386 | return (NULL); |
| 6387 | |
| 6388 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6389 | if (schema == NULL) |
| 6390 | return(NULL); |
| 6391 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6392 | olddefine = ctxt->define; |
| 6393 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6394 | if (IS_RELAXNG(node, "grammar")) { |
| 6395 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 6396 | } else { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6397 | xmlRelaxNGGrammarPtr tmp, ret; |
| 6398 | |
| 6399 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6400 | if (schema->topgrammar == NULL) { |
| 6401 | return(schema); |
| 6402 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6403 | /* |
| 6404 | * Link the new grammar in the tree |
| 6405 | */ |
| 6406 | ret->parent = ctxt->grammar; |
| 6407 | if (ctxt->grammar != NULL) { |
| 6408 | tmp = ctxt->grammar->children; |
| 6409 | if (tmp == NULL) { |
| 6410 | ctxt->grammar->children = ret; |
| 6411 | } else { |
| 6412 | while (tmp->next != NULL) |
| 6413 | tmp = tmp->next; |
| 6414 | tmp->next = ret; |
| 6415 | } |
| 6416 | } |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6417 | old = ctxt->grammar; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6418 | ctxt->grammar = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6419 | xmlRelaxNGParseStart(ctxt, node); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6420 | if (old != NULL) |
| 6421 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6422 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6423 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6424 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6425 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6426 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6427 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6428 | while ((schema->topgrammar->start != NULL) && |
| 6429 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6430 | (schema->topgrammar->start->next != NULL)) |
| 6431 | schema->topgrammar->start = schema->topgrammar->start->content; |
| 6432 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6433 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6434 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6435 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6436 | |
| 6437 | #ifdef DEBUG |
| 6438 | if (schema == NULL) |
| 6439 | xmlGenericError(xmlGenericErrorContext, |
| 6440 | "xmlRelaxNGParseDocument() failed\n"); |
| 6441 | #endif |
| 6442 | |
| 6443 | return (schema); |
| 6444 | } |
| 6445 | |
| 6446 | /************************************************************************ |
| 6447 | * * |
| 6448 | * Reading RelaxNGs * |
| 6449 | * * |
| 6450 | ************************************************************************/ |
| 6451 | |
| 6452 | /** |
| 6453 | * xmlRelaxNGNewParserCtxt: |
| 6454 | * @URL: the location of the schema |
| 6455 | * |
| 6456 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6457 | * to contain an XML RelaxNGs file. |
| 6458 | * |
| 6459 | * Returns the parser context or NULL in case of error |
| 6460 | */ |
| 6461 | xmlRelaxNGParserCtxtPtr |
| 6462 | xmlRelaxNGNewParserCtxt(const char *URL) { |
| 6463 | xmlRelaxNGParserCtxtPtr ret; |
| 6464 | |
| 6465 | if (URL == NULL) |
| 6466 | return(NULL); |
| 6467 | |
| 6468 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6469 | if (ret == NULL) { |
| 6470 | xmlGenericError(xmlGenericErrorContext, |
| 6471 | "Failed to allocate new schama parser context for %s\n", URL); |
| 6472 | return (NULL); |
| 6473 | } |
| 6474 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6475 | ret->URL = xmlStrdup((const xmlChar *)URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6476 | ret->error = xmlGenericError; |
| 6477 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6478 | return (ret); |
| 6479 | } |
| 6480 | |
| 6481 | /** |
| 6482 | * xmlRelaxNGNewMemParserCtxt: |
| 6483 | * @buffer: a pointer to a char array containing the schemas |
| 6484 | * @size: the size of the array |
| 6485 | * |
| 6486 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6487 | * to contain an XML RelaxNGs file. |
| 6488 | * |
| 6489 | * Returns the parser context or NULL in case of error |
| 6490 | */ |
| 6491 | xmlRelaxNGParserCtxtPtr |
| 6492 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) { |
| 6493 | xmlRelaxNGParserCtxtPtr ret; |
| 6494 | |
| 6495 | if ((buffer == NULL) || (size <= 0)) |
| 6496 | return(NULL); |
| 6497 | |
| 6498 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6499 | if (ret == NULL) { |
| 6500 | xmlGenericError(xmlGenericErrorContext, |
| 6501 | "Failed to allocate new schama parser context\n"); |
| 6502 | return (NULL); |
| 6503 | } |
| 6504 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6505 | ret->buffer = buffer; |
| 6506 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6507 | ret->error = xmlGenericError; |
| 6508 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6509 | return (ret); |
| 6510 | } |
| 6511 | |
| 6512 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame^] | 6513 | * xmlRelaxNGNewDocParserCtxt: |
| 6514 | * @doc: a preparsed document tree |
| 6515 | * |
| 6516 | * Create an XML RelaxNGs parser context for that document. |
| 6517 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6518 | * document, the @doc parameter is duplicated internally. |
| 6519 | * |
| 6520 | * Returns the parser context or NULL in case of error |
| 6521 | */ |
| 6522 | xmlRelaxNGParserCtxtPtr |
| 6523 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) { |
| 6524 | xmlRelaxNGParserCtxtPtr ret; |
| 6525 | xmlDocPtr copy; |
| 6526 | |
| 6527 | if (doc == NULL) |
| 6528 | return(NULL); |
| 6529 | copy = xmlCopyDoc(doc, 1); |
| 6530 | if (copy == NULL) |
| 6531 | return(NULL); |
| 6532 | |
| 6533 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6534 | if (ret == NULL) { |
| 6535 | xmlGenericError(xmlGenericErrorContext, |
| 6536 | "Failed to allocate new schama parser context\n"); |
| 6537 | return (NULL); |
| 6538 | } |
| 6539 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6540 | ret->document = copy; |
| 6541 | ret->userData = xmlGenericErrorContext; |
| 6542 | return (ret); |
| 6543 | } |
| 6544 | |
| 6545 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6546 | * xmlRelaxNGFreeParserCtxt: |
| 6547 | * @ctxt: the schema parser context |
| 6548 | * |
| 6549 | * Free the resources associated to the schema parser context |
| 6550 | */ |
| 6551 | void |
| 6552 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) { |
| 6553 | if (ctxt == NULL) |
| 6554 | return; |
| 6555 | if (ctxt->URL != NULL) |
| 6556 | xmlFree(ctxt->URL); |
| 6557 | if (ctxt->doc != NULL) |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame^] | 6558 | xmlFreeDoc(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6559 | if (ctxt->interleaves != NULL) |
| 6560 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6561 | if (ctxt->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6562 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6563 | if (ctxt->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6564 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6565 | if (ctxt->docTab != NULL) |
| 6566 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6567 | if (ctxt->incTab != NULL) |
| 6568 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6569 | if (ctxt->defTab != NULL) { |
| 6570 | int i; |
| 6571 | |
| 6572 | for (i = 0;i < ctxt->defNr;i++) |
| 6573 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6574 | xmlFree(ctxt->defTab); |
| 6575 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6576 | xmlFree(ctxt); |
| 6577 | } |
| 6578 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6579 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6580 | * xmlRelaxNGNormExtSpace: |
| 6581 | * @value: a value |
| 6582 | * |
| 6583 | * Removes the leading and ending spaces of the value |
| 6584 | * The string is modified "in situ" |
| 6585 | */ |
| 6586 | static void |
| 6587 | xmlRelaxNGNormExtSpace(xmlChar *value) { |
| 6588 | xmlChar *start = value; |
| 6589 | xmlChar *cur = value; |
| 6590 | if (value == NULL) |
| 6591 | return; |
| 6592 | |
| 6593 | while (IS_BLANK(*cur)) cur++; |
| 6594 | if (cur == start) { |
| 6595 | do { |
| 6596 | while ((*cur != 0) && (!IS_BLANK(*cur))) cur++; |
| 6597 | if (*cur == 0) |
| 6598 | return; |
| 6599 | start = cur; |
| 6600 | while (IS_BLANK(*cur)) cur++; |
| 6601 | if (*cur == 0) { |
| 6602 | *start = 0; |
| 6603 | return; |
| 6604 | } |
| 6605 | } while (1); |
| 6606 | } else { |
| 6607 | do { |
| 6608 | while ((*cur != 0) && (!IS_BLANK(*cur))) |
| 6609 | *start++ = *cur++; |
| 6610 | if (*cur == 0) { |
| 6611 | *start = 0; |
| 6612 | return; |
| 6613 | } |
| 6614 | /* don't try to normalize the inner spaces */ |
| 6615 | while (IS_BLANK(*cur)) cur++; |
| 6616 | *start++ = *cur++; |
| 6617 | if (*cur == 0) { |
| 6618 | *start = 0; |
| 6619 | return; |
| 6620 | } |
| 6621 | } while (1); |
| 6622 | } |
| 6623 | } |
| 6624 | |
| 6625 | /** |
| 6626 | * xmlRelaxNGCheckAttributes: |
| 6627 | * @ctxt: a Relax-NG parser context |
| 6628 | * @node: a Relax-NG node |
| 6629 | * |
| 6630 | * Check all the attributes on the given node |
| 6631 | */ |
| 6632 | static void |
| 6633 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6634 | xmlAttrPtr cur, next; |
| 6635 | |
| 6636 | cur = node->properties; |
| 6637 | while (cur != NULL) { |
| 6638 | next = cur->next; |
| 6639 | if ((cur->ns == NULL) || |
| 6640 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6641 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6642 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6643 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6644 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6645 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6646 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6647 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6648 | if (ctxt->error != NULL) |
| 6649 | ctxt->error(ctxt->userData, |
| 6650 | "Attribute %s is not allowed on %s\n", |
| 6651 | cur->name, node->name); |
| 6652 | ctxt->nbErrors++; |
| 6653 | } |
| 6654 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6655 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6656 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6657 | if (ctxt->error != NULL) |
| 6658 | ctxt->error(ctxt->userData, |
| 6659 | "Attribute %s is not allowed on %s\n", |
| 6660 | cur->name, node->name); |
| 6661 | ctxt->nbErrors++; |
| 6662 | } |
| 6663 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6664 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6665 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6666 | if (ctxt->error != NULL) |
| 6667 | ctxt->error(ctxt->userData, |
| 6668 | "Attribute %s is not allowed on %s\n", |
| 6669 | cur->name, node->name); |
| 6670 | ctxt->nbErrors++; |
| 6671 | } |
| 6672 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6673 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6674 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6675 | if (ctxt->error != NULL) |
| 6676 | ctxt->error(ctxt->userData, |
| 6677 | "Attribute %s is not allowed on %s\n", |
| 6678 | cur->name, node->name); |
| 6679 | ctxt->nbErrors++; |
| 6680 | } |
| 6681 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6682 | xmlChar *val; |
| 6683 | xmlURIPtr uri; |
| 6684 | |
| 6685 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6686 | if (val != NULL) { |
| 6687 | if (val[0] != 0) { |
| 6688 | uri = xmlParseURI((const char *) val); |
| 6689 | if (uri == NULL) { |
| 6690 | if (ctxt->error != NULL) |
| 6691 | ctxt->error(ctxt->userData, |
| 6692 | "Attribute %s contains invalid URI %s\n", |
| 6693 | cur->name, val); |
| 6694 | ctxt->nbErrors++; |
| 6695 | } else { |
| 6696 | if (uri->scheme == NULL) { |
| 6697 | if (ctxt->error != NULL) |
| 6698 | ctxt->error(ctxt->userData, |
| 6699 | "Attribute %s URI %s is not absolute\n", |
| 6700 | cur->name, val); |
| 6701 | ctxt->nbErrors++; |
| 6702 | } |
| 6703 | if (uri->fragment != NULL) { |
| 6704 | if (ctxt->error != NULL) |
| 6705 | ctxt->error(ctxt->userData, |
| 6706 | "Attribute %s URI %s has a fragment ID\n", |
| 6707 | cur->name, val); |
| 6708 | ctxt->nbErrors++; |
| 6709 | } |
| 6710 | xmlFreeURI(uri); |
| 6711 | } |
| 6712 | } |
| 6713 | xmlFree(val); |
| 6714 | } |
| 6715 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6716 | if (ctxt->error != NULL) |
| 6717 | ctxt->error(ctxt->userData, |
| 6718 | "Unknown attribute %s on %s\n", |
| 6719 | cur->name, node->name); |
| 6720 | ctxt->nbErrors++; |
| 6721 | } |
| 6722 | } |
| 6723 | cur = next; |
| 6724 | } |
| 6725 | } |
| 6726 | |
| 6727 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6728 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6729 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6730 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6731 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6732 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6733 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6734 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6735 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6736 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6737 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6738 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6739 | delete = NULL; |
| 6740 | cur = root; |
| 6741 | while (cur != NULL) { |
| 6742 | if (delete != NULL) { |
| 6743 | xmlUnlinkNode(delete); |
| 6744 | xmlFreeNode(delete); |
| 6745 | delete = NULL; |
| 6746 | } |
| 6747 | if (cur->type == XML_ELEMENT_NODE) { |
| 6748 | /* |
| 6749 | * Simplification 4.1. Annotations |
| 6750 | */ |
| 6751 | if ((cur->ns == NULL) || |
| 6752 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6753 | if ((cur->parent != NULL) && |
| 6754 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6755 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6756 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6757 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6758 | if (ctxt->error != NULL) |
| 6759 | ctxt->error(ctxt->userData, |
| 6760 | "element %s doesn't allow foreign elements\n", |
| 6761 | cur->parent->name); |
| 6762 | ctxt->nbErrors++; |
| 6763 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6764 | delete = cur; |
| 6765 | goto skip_children; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6766 | } else { |
| 6767 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6768 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6769 | xmlChar *href, *ns, *base, *URL; |
| 6770 | xmlRelaxNGDocumentPtr docu; |
| 6771 | xmlNodePtr tmp; |
| 6772 | |
| 6773 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6774 | if (ns == NULL) { |
| 6775 | tmp = cur->parent; |
| 6776 | while ((tmp != NULL) && |
| 6777 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6778 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6779 | if (ns != NULL) |
| 6780 | break; |
| 6781 | tmp = tmp->parent; |
| 6782 | } |
| 6783 | } |
| 6784 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6785 | if (href == NULL) { |
| 6786 | if (ctxt->error != NULL) |
| 6787 | ctxt->error(ctxt->userData, |
| 6788 | "xmlRelaxNGParse: externalRef has no href attribute\n"); |
| 6789 | ctxt->nbErrors++; |
| 6790 | delete = cur; |
| 6791 | goto skip_children; |
| 6792 | } |
| 6793 | base = xmlNodeGetBase(cur->doc, cur); |
| 6794 | URL = xmlBuildURI(href, base); |
| 6795 | if (URL == NULL) { |
| 6796 | if (ctxt->error != NULL) |
| 6797 | ctxt->error(ctxt->userData, |
| 6798 | "Failed to compute URL for externalRef %s\n", href); |
| 6799 | ctxt->nbErrors++; |
| 6800 | if (href != NULL) |
| 6801 | xmlFree(href); |
| 6802 | if (base != NULL) |
| 6803 | xmlFree(base); |
| 6804 | delete = cur; |
| 6805 | goto skip_children; |
| 6806 | } |
| 6807 | if (href != NULL) |
| 6808 | xmlFree(href); |
| 6809 | if (base != NULL) |
| 6810 | xmlFree(base); |
| 6811 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6812 | if (docu == NULL) { |
| 6813 | if (ctxt->error != NULL) |
| 6814 | ctxt->error(ctxt->userData, |
| 6815 | "Failed to load externalRef %s\n", URL); |
| 6816 | ctxt->nbErrors++; |
| 6817 | xmlFree(URL); |
| 6818 | delete = cur; |
| 6819 | goto skip_children; |
| 6820 | } |
| 6821 | if (ns != NULL) |
| 6822 | xmlFree(ns); |
| 6823 | xmlFree(URL); |
| 6824 | cur->_private = docu; |
| 6825 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6826 | xmlChar *href, *ns, *base, *URL; |
| 6827 | xmlRelaxNGIncludePtr incl; |
| 6828 | xmlNodePtr tmp; |
| 6829 | |
| 6830 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6831 | if (href == NULL) { |
| 6832 | if (ctxt->error != NULL) |
| 6833 | ctxt->error(ctxt->userData, |
| 6834 | "xmlRelaxNGParse: include has no href attribute\n"); |
| 6835 | ctxt->nbErrors++; |
| 6836 | delete = cur; |
| 6837 | goto skip_children; |
| 6838 | } |
| 6839 | base = xmlNodeGetBase(cur->doc, cur); |
| 6840 | URL = xmlBuildURI(href, base); |
| 6841 | if (URL == NULL) { |
| 6842 | if (ctxt->error != NULL) |
| 6843 | ctxt->error(ctxt->userData, |
| 6844 | "Failed to compute URL for include %s\n", href); |
| 6845 | ctxt->nbErrors++; |
| 6846 | if (href != NULL) |
| 6847 | xmlFree(href); |
| 6848 | if (base != NULL) |
| 6849 | xmlFree(base); |
| 6850 | delete = cur; |
| 6851 | goto skip_children; |
| 6852 | } |
| 6853 | if (href != NULL) |
| 6854 | xmlFree(href); |
| 6855 | if (base != NULL) |
| 6856 | xmlFree(base); |
| 6857 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6858 | if (ns == NULL) { |
| 6859 | tmp = cur->parent; |
| 6860 | while ((tmp != NULL) && |
| 6861 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6862 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6863 | if (ns != NULL) |
| 6864 | break; |
| 6865 | tmp = tmp->parent; |
| 6866 | } |
| 6867 | } |
| 6868 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 6869 | if (ns != NULL) |
| 6870 | xmlFree(ns); |
| 6871 | if (incl == NULL) { |
| 6872 | if (ctxt->error != NULL) |
| 6873 | ctxt->error(ctxt->userData, |
| 6874 | "Failed to load include %s\n", URL); |
| 6875 | ctxt->nbErrors++; |
| 6876 | xmlFree(URL); |
| 6877 | delete = cur; |
| 6878 | goto skip_children; |
| 6879 | } |
| 6880 | xmlFree(URL); |
| 6881 | cur->_private = incl; |
| 6882 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 6883 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) { |
| 6884 | xmlChar *name, *ns; |
| 6885 | xmlNodePtr text = NULL; |
| 6886 | |
| 6887 | /* |
| 6888 | * Simplification 4.8. name attribute of element |
| 6889 | * and attribute elements |
| 6890 | */ |
| 6891 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 6892 | if (name != NULL) { |
| 6893 | if (cur->children == NULL) { |
| 6894 | text = xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 6895 | name); |
| 6896 | } else { |
| 6897 | xmlNodePtr node; |
| 6898 | node = xmlNewNode(cur->ns, BAD_CAST "name"); |
| 6899 | if (node != NULL) { |
| 6900 | xmlAddPrevSibling(cur->children, node); |
| 6901 | text = xmlNewText(name); |
| 6902 | xmlAddChild(node, text); |
| 6903 | text = node; |
| 6904 | } |
| 6905 | } |
| 6906 | if (text == NULL) { |
| 6907 | if (ctxt->error != NULL) |
| 6908 | ctxt->error(ctxt->userData, |
| 6909 | "Failed to create a name %s element\n", name); |
| 6910 | ctxt->nbErrors++; |
| 6911 | } |
| 6912 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 6913 | xmlFree(name); |
| 6914 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6915 | if (ns != NULL) { |
| 6916 | if (text != NULL) { |
| 6917 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 6918 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 6919 | } |
| 6920 | xmlFree(ns); |
| 6921 | } else if (xmlStrEqual(cur->name, |
| 6922 | BAD_CAST "attribute")) { |
| 6923 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 6924 | } |
| 6925 | } |
| 6926 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 6927 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 6928 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 6929 | /* |
| 6930 | * Simplification 4.8. name attribute of element |
| 6931 | * and attribute elements |
| 6932 | */ |
| 6933 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 6934 | xmlNodePtr node; |
| 6935 | xmlChar *ns = NULL; |
| 6936 | |
| 6937 | node = cur->parent; |
| 6938 | while ((node != NULL) && |
| 6939 | (node->type == XML_ELEMENT_NODE)) { |
| 6940 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 6941 | if (ns != NULL) { |
| 6942 | break; |
| 6943 | } |
| 6944 | node = node->parent; |
| 6945 | } |
| 6946 | if (ns == NULL) { |
| 6947 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 6948 | } else { |
| 6949 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 6950 | xmlFree(ns); |
| 6951 | } |
| 6952 | } |
| 6953 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6954 | xmlChar *name, *local, *prefix; |
| 6955 | |
| 6956 | /* |
| 6957 | * Simplification: 4.10. QNames |
| 6958 | */ |
| 6959 | name = xmlNodeGetContent(cur); |
| 6960 | if (name != NULL) { |
| 6961 | local = xmlSplitQName2(name, &prefix); |
| 6962 | if (local != NULL) { |
| 6963 | xmlNsPtr ns; |
| 6964 | |
| 6965 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 6966 | if (ns == NULL) { |
| 6967 | if (ctxt->error != NULL) |
| 6968 | ctxt->error(ctxt->userData, |
| 6969 | "xmlRelaxNGParse: no namespace for prefix %s\n", prefix); |
| 6970 | ctxt->nbErrors++; |
| 6971 | } else { |
| 6972 | xmlSetProp(cur, BAD_CAST "ns", ns->href); |
| 6973 | xmlNodeSetContent(cur, local); |
| 6974 | } |
| 6975 | xmlFree(local); |
| 6976 | xmlFree(prefix); |
| 6977 | } |
| 6978 | xmlFree(name); |
| 6979 | } |
| 6980 | } |
| 6981 | /* |
| 6982 | * 4.16 |
| 6983 | */ |
| 6984 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 6985 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 6986 | if (ctxt->error != NULL) |
| 6987 | ctxt->error(ctxt->userData, |
| 6988 | "Found nsName/except//nsName forbidden construct\n"); |
| 6989 | ctxt->nbErrors++; |
| 6990 | } |
| 6991 | } |
| 6992 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 6993 | (cur != root)) { |
| 6994 | int oldflags = ctxt->flags; |
| 6995 | |
| 6996 | /* |
| 6997 | * 4.16 |
| 6998 | */ |
| 6999 | if ((cur->parent != NULL) && |
| 7000 | (xmlStrEqual(cur->parent->name, BAD_CAST "anyName"))) { |
| 7001 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7002 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7003 | ctxt->flags = oldflags; |
| 7004 | goto skip_children; |
| 7005 | } else if ((cur->parent != NULL) && |
| 7006 | (xmlStrEqual(cur->parent->name, BAD_CAST "nsName"))) { |
| 7007 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7008 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7009 | ctxt->flags = oldflags; |
| 7010 | goto skip_children; |
| 7011 | } |
| 7012 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7013 | /* |
| 7014 | * 4.16 |
| 7015 | */ |
| 7016 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7017 | if (ctxt->error != NULL) |
| 7018 | ctxt->error(ctxt->userData, |
| 7019 | "Found anyName/except//anyName forbidden construct\n"); |
| 7020 | ctxt->nbErrors++; |
| 7021 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7022 | if (ctxt->error != NULL) |
| 7023 | ctxt->error(ctxt->userData, |
| 7024 | "Found nsName/except//anyName forbidden construct\n"); |
| 7025 | ctxt->nbErrors++; |
| 7026 | } |
| 7027 | } |
| 7028 | /* |
| 7029 | * Thisd is not an else since "include" is transformed |
| 7030 | * into a div |
| 7031 | */ |
| 7032 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7033 | xmlChar *ns; |
| 7034 | xmlNodePtr child, ins, tmp; |
| 7035 | |
| 7036 | /* |
| 7037 | * implements rule 4.11 |
| 7038 | */ |
| 7039 | |
| 7040 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7041 | |
| 7042 | child = cur->children; |
| 7043 | ins = cur; |
| 7044 | while (child != NULL) { |
| 7045 | if (ns != NULL) { |
| 7046 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7047 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7048 | } |
| 7049 | } |
| 7050 | tmp = child->next; |
| 7051 | xmlUnlinkNode(child); |
| 7052 | ins = xmlAddNextSibling(ins, child); |
| 7053 | child = tmp; |
| 7054 | } |
| 7055 | if (ns != NULL) |
| 7056 | xmlFree(ns); |
| 7057 | delete = cur; |
| 7058 | goto skip_children; |
| 7059 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7060 | } |
| 7061 | } |
| 7062 | /* |
| 7063 | * Simplification 4.2 whitespaces |
| 7064 | */ |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7065 | else if ((cur->type == XML_TEXT_NODE) || |
| 7066 | (cur->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7067 | if (IS_BLANK_NODE(cur)) { |
| 7068 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 7069 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) && |
| 7070 | (!xmlStrEqual(cur->parent->name, BAD_CAST "param"))) |
| 7071 | delete = cur; |
| 7072 | } else { |
| 7073 | delete = cur; |
| 7074 | goto skip_children; |
| 7075 | } |
| 7076 | } |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7077 | } else { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7078 | delete = cur; |
| 7079 | goto skip_children; |
| 7080 | } |
| 7081 | |
| 7082 | /* |
| 7083 | * Skip to next node |
| 7084 | */ |
| 7085 | if (cur->children != NULL) { |
| 7086 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7087 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7088 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7089 | cur = cur->children; |
| 7090 | continue; |
| 7091 | } |
| 7092 | } |
| 7093 | skip_children: |
| 7094 | if (cur->next != NULL) { |
| 7095 | cur = cur->next; |
| 7096 | continue; |
| 7097 | } |
| 7098 | |
| 7099 | do { |
| 7100 | cur = cur->parent; |
| 7101 | if (cur == NULL) |
| 7102 | break; |
| 7103 | if (cur == root) { |
| 7104 | cur = NULL; |
| 7105 | break; |
| 7106 | } |
| 7107 | if (cur->next != NULL) { |
| 7108 | cur = cur->next; |
| 7109 | break; |
| 7110 | } |
| 7111 | } while (cur != NULL); |
| 7112 | } |
| 7113 | if (delete != NULL) { |
| 7114 | xmlUnlinkNode(delete); |
| 7115 | xmlFreeNode(delete); |
| 7116 | delete = NULL; |
| 7117 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7118 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7119 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7120 | /** |
| 7121 | * xmlRelaxNGCleanupDoc: |
| 7122 | * @ctxt: a Relax-NG parser context |
| 7123 | * @doc: an xmldocPtr document pointer |
| 7124 | * |
| 7125 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7126 | * Include and externalRef lookups. |
| 7127 | * |
| 7128 | * Returns the cleaned up document or NULL in case of error |
| 7129 | */ |
| 7130 | static xmlDocPtr |
| 7131 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) { |
| 7132 | xmlNodePtr root; |
| 7133 | |
| 7134 | /* |
| 7135 | * Extract the root |
| 7136 | */ |
| 7137 | root = xmlDocGetRootElement(doc); |
| 7138 | if (root == NULL) { |
| 7139 | if (ctxt->error != NULL) |
| 7140 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 7141 | ctxt->URL); |
| 7142 | ctxt->nbErrors++; |
| 7143 | return (NULL); |
| 7144 | } |
| 7145 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7146 | return(doc); |
| 7147 | } |
| 7148 | |
| 7149 | /** |
| 7150 | * xmlRelaxNGParse: |
| 7151 | * @ctxt: a Relax-NG parser context |
| 7152 | * |
| 7153 | * parse a schema definition resource and build an internal |
| 7154 | * XML Shema struture which can be used to validate instances. |
| 7155 | * *WARNING* this interface is highly subject to change |
| 7156 | * |
| 7157 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7158 | * NULL in case of error |
| 7159 | */ |
| 7160 | xmlRelaxNGPtr |
| 7161 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7162 | { |
| 7163 | xmlRelaxNGPtr ret = NULL; |
| 7164 | xmlDocPtr doc; |
| 7165 | xmlNodePtr root; |
| 7166 | |
| 7167 | xmlRelaxNGInitTypes(); |
| 7168 | |
| 7169 | if (ctxt == NULL) |
| 7170 | return (NULL); |
| 7171 | |
| 7172 | /* |
| 7173 | * First step is to parse the input document into an DOM/Infoset |
| 7174 | */ |
| 7175 | if (ctxt->URL != NULL) { |
| 7176 | doc = xmlParseFile((const char *) ctxt->URL); |
| 7177 | if (doc == NULL) { |
| 7178 | if (ctxt->error != NULL) |
| 7179 | ctxt->error(ctxt->userData, |
| 7180 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL); |
| 7181 | ctxt->nbErrors++; |
| 7182 | return (NULL); |
| 7183 | } |
| 7184 | } else if (ctxt->buffer != NULL) { |
| 7185 | doc = xmlParseMemory(ctxt->buffer, ctxt->size); |
| 7186 | if (doc == NULL) { |
| 7187 | if (ctxt->error != NULL) |
| 7188 | ctxt->error(ctxt->userData, |
| 7189 | "xmlRelaxNGParse: could not parse schemas\n"); |
| 7190 | ctxt->nbErrors++; |
| 7191 | return (NULL); |
| 7192 | } |
| 7193 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7194 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame^] | 7195 | } else if (ctxt->document != NULL) { |
| 7196 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7197 | } else { |
| 7198 | if (ctxt->error != NULL) |
| 7199 | ctxt->error(ctxt->userData, |
| 7200 | "xmlRelaxNGParse: nothing to parse\n"); |
| 7201 | ctxt->nbErrors++; |
| 7202 | return (NULL); |
| 7203 | } |
| 7204 | ctxt->document = doc; |
| 7205 | |
| 7206 | /* |
| 7207 | * Some preprocessing of the document content |
| 7208 | */ |
| 7209 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7210 | if (doc == NULL) { |
| 7211 | xmlFreeDoc(ctxt->document); |
| 7212 | ctxt->document = NULL; |
| 7213 | return(NULL); |
| 7214 | } |
| 7215 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7216 | /* |
| 7217 | * Then do the parsing for good |
| 7218 | */ |
| 7219 | root = xmlDocGetRootElement(doc); |
| 7220 | if (root == NULL) { |
| 7221 | if (ctxt->error != NULL) |
| 7222 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 7223 | ctxt->URL); |
| 7224 | ctxt->nbErrors++; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7225 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7226 | return (NULL); |
| 7227 | } |
| 7228 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7229 | if (ret == NULL) { |
| 7230 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7231 | return(NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7232 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7233 | |
| 7234 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7235 | * Check the ref/defines links |
| 7236 | */ |
| 7237 | /* |
| 7238 | * try to preprocess interleaves |
| 7239 | */ |
| 7240 | if (ctxt->interleaves != NULL) { |
| 7241 | xmlHashScan(ctxt->interleaves, |
| 7242 | (xmlHashScanner)xmlRelaxNGComputeInterleaves, ctxt); |
| 7243 | } |
| 7244 | |
| 7245 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7246 | * if there was a parsing error return NULL |
| 7247 | */ |
| 7248 | if (ctxt->nbErrors > 0) { |
| 7249 | xmlRelaxNGFree(ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7250 | ctxt->document = NULL; |
| 7251 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7252 | return(NULL); |
| 7253 | } |
| 7254 | |
| 7255 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7256 | * try to compile (parts of) the schemas |
| 7257 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7258 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7259 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7260 | xmlRelaxNGDefinePtr def; |
| 7261 | |
| 7262 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7263 | if (def != NULL) { |
| 7264 | def->type = XML_RELAXNG_START; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7265 | def->content = ret->topgrammar->start; |
| 7266 | ret->topgrammar->start = def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7267 | } |
| 7268 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7269 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7270 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7271 | |
| 7272 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7273 | * Transfer the pointer for cleanup at the schema level. |
| 7274 | */ |
| 7275 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7276 | ctxt->document = NULL; |
| 7277 | ret->documents = ctxt->documents; |
| 7278 | ctxt->documents = NULL; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7279 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7280 | ret->includes = ctxt->includes; |
| 7281 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7282 | ret->defNr = ctxt->defNr; |
| 7283 | ret->defTab = ctxt->defTab; |
| 7284 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7285 | if (ctxt->idref == 1) |
| 7286 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7287 | |
| 7288 | return (ret); |
| 7289 | } |
| 7290 | |
| 7291 | /** |
| 7292 | * xmlRelaxNGSetParserErrors: |
| 7293 | * @ctxt: a Relax-NG validation context |
| 7294 | * @err: the error callback |
| 7295 | * @warn: the warning callback |
| 7296 | * @ctx: contextual data for the callbacks |
| 7297 | * |
| 7298 | * Set the callback functions used to handle errors for a validation context |
| 7299 | */ |
| 7300 | void |
| 7301 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
| 7302 | xmlRelaxNGValidityErrorFunc err, |
| 7303 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 7304 | if (ctxt == NULL) |
| 7305 | return; |
| 7306 | ctxt->error = err; |
| 7307 | ctxt->warning = warn; |
| 7308 | ctxt->userData = ctx; |
| 7309 | } |
| 7310 | /************************************************************************ |
| 7311 | * * |
| 7312 | * Dump back a compiled form * |
| 7313 | * * |
| 7314 | ************************************************************************/ |
| 7315 | static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define); |
| 7316 | |
| 7317 | /** |
| 7318 | * xmlRelaxNGDumpDefines: |
| 7319 | * @output: the file output |
| 7320 | * @defines: a list of define structures |
| 7321 | * |
| 7322 | * Dump a RelaxNG structure back |
| 7323 | */ |
| 7324 | static void |
| 7325 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) { |
| 7326 | while (defines != NULL) { |
| 7327 | xmlRelaxNGDumpDefine(output, defines); |
| 7328 | defines = defines->next; |
| 7329 | } |
| 7330 | } |
| 7331 | |
| 7332 | /** |
| 7333 | * xmlRelaxNGDumpDefine: |
| 7334 | * @output: the file output |
| 7335 | * @define: a define structure |
| 7336 | * |
| 7337 | * Dump a RelaxNG structure back |
| 7338 | */ |
| 7339 | static void |
| 7340 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) { |
| 7341 | if (define == NULL) |
| 7342 | return; |
| 7343 | switch(define->type) { |
| 7344 | case XML_RELAXNG_EMPTY: |
| 7345 | fprintf(output, "<empty/>\n"); |
| 7346 | break; |
| 7347 | case XML_RELAXNG_NOT_ALLOWED: |
| 7348 | fprintf(output, "<notAllowed/>\n"); |
| 7349 | break; |
| 7350 | case XML_RELAXNG_TEXT: |
| 7351 | fprintf(output, "<text/>\n"); |
| 7352 | break; |
| 7353 | case XML_RELAXNG_ELEMENT: |
| 7354 | fprintf(output, "<element>\n"); |
| 7355 | if (define->name != NULL) { |
| 7356 | fprintf(output, "<name"); |
| 7357 | if (define->ns != NULL) |
| 7358 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7359 | fprintf(output, ">%s</name>\n", define->name); |
| 7360 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7361 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7362 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7363 | fprintf(output, "</element>\n"); |
| 7364 | break; |
| 7365 | case XML_RELAXNG_LIST: |
| 7366 | fprintf(output, "<list>\n"); |
| 7367 | xmlRelaxNGDumpDefines(output, define->content); |
| 7368 | fprintf(output, "</list>\n"); |
| 7369 | break; |
| 7370 | case XML_RELAXNG_ONEORMORE: |
| 7371 | fprintf(output, "<oneOrMore>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7372 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7373 | fprintf(output, "</oneOrMore>\n"); |
| 7374 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7375 | case XML_RELAXNG_ZEROORMORE: |
| 7376 | fprintf(output, "<zeroOrMore>\n"); |
| 7377 | xmlRelaxNGDumpDefines(output, define->content); |
| 7378 | fprintf(output, "</zeroOrMore>\n"); |
| 7379 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7380 | case XML_RELAXNG_CHOICE: |
| 7381 | fprintf(output, "<choice>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7382 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7383 | fprintf(output, "</choice>\n"); |
| 7384 | break; |
| 7385 | case XML_RELAXNG_GROUP: |
| 7386 | fprintf(output, "<group>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7387 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7388 | fprintf(output, "</group>\n"); |
| 7389 | break; |
| 7390 | case XML_RELAXNG_INTERLEAVE: |
| 7391 | fprintf(output, "<interleave>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7392 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7393 | fprintf(output, "</interleave>\n"); |
| 7394 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7395 | case XML_RELAXNG_OPTIONAL: |
| 7396 | fprintf(output, "<optional>\n"); |
| 7397 | xmlRelaxNGDumpDefines(output, define->content); |
| 7398 | fprintf(output, "</optional>\n"); |
| 7399 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7400 | case XML_RELAXNG_ATTRIBUTE: |
| 7401 | fprintf(output, "<attribute>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7402 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7403 | fprintf(output, "</attribute>\n"); |
| 7404 | break; |
| 7405 | case XML_RELAXNG_DEF: |
| 7406 | fprintf(output, "<define"); |
| 7407 | if (define->name != NULL) |
| 7408 | fprintf(output, " name=\"%s\"", define->name); |
| 7409 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7410 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7411 | fprintf(output, "</define>\n"); |
| 7412 | break; |
| 7413 | case XML_RELAXNG_REF: |
| 7414 | fprintf(output, "<ref"); |
| 7415 | if (define->name != NULL) |
| 7416 | fprintf(output, " name=\"%s\"", define->name); |
| 7417 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7418 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7419 | fprintf(output, "</ref>\n"); |
| 7420 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7421 | case XML_RELAXNG_PARENTREF: |
| 7422 | fprintf(output, "<parentRef"); |
| 7423 | if (define->name != NULL) |
| 7424 | fprintf(output, " name=\"%s\"", define->name); |
| 7425 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7426 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7427 | fprintf(output, "</parentRef>\n"); |
| 7428 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7429 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7430 | fprintf(output, "<externalRef>"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7431 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7432 | fprintf(output, "</externalRef>\n"); |
| 7433 | break; |
| 7434 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7435 | case XML_RELAXNG_VALUE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7436 | TODO |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7437 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7438 | case XML_RELAXNG_START: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7439 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 7440 | case XML_RELAXNG_PARAM: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7441 | TODO |
| 7442 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 7443 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7444 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 7445 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7446 | } |
| 7447 | } |
| 7448 | |
| 7449 | /** |
| 7450 | * xmlRelaxNGDumpGrammar: |
| 7451 | * @output: the file output |
| 7452 | * @grammar: a grammar structure |
| 7453 | * @top: is this a top grammar |
| 7454 | * |
| 7455 | * Dump a RelaxNG structure back |
| 7456 | */ |
| 7457 | static void |
| 7458 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7459 | { |
| 7460 | if (grammar == NULL) |
| 7461 | return; |
| 7462 | |
| 7463 | fprintf(output, "<grammar"); |
| 7464 | if (top) |
| 7465 | fprintf(output, |
| 7466 | " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7467 | switch(grammar->combine) { |
| 7468 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7469 | break; |
| 7470 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7471 | fprintf(output, " combine=\"choice\""); |
| 7472 | break; |
| 7473 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7474 | fprintf(output, " combine=\"interleave\""); |
| 7475 | break; |
| 7476 | default: |
| 7477 | fprintf(output, " <!-- invalid combine value -->"); |
| 7478 | } |
| 7479 | fprintf(output, ">\n"); |
| 7480 | if (grammar->start == NULL) { |
| 7481 | fprintf(output, " <!-- grammar had no start -->"); |
| 7482 | } else { |
| 7483 | fprintf(output, "<start>\n"); |
| 7484 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7485 | fprintf(output, "</start>\n"); |
| 7486 | } |
| 7487 | /* TODO ? Dump the defines ? */ |
| 7488 | fprintf(output, "</grammar>\n"); |
| 7489 | } |
| 7490 | |
| 7491 | /** |
| 7492 | * xmlRelaxNGDump: |
| 7493 | * @output: the file output |
| 7494 | * @schema: a schema structure |
| 7495 | * |
| 7496 | * Dump a RelaxNG structure back |
| 7497 | */ |
| 7498 | void |
| 7499 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7500 | { |
| 7501 | if (schema == NULL) { |
| 7502 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7503 | return; |
| 7504 | } |
| 7505 | fprintf(output, "RelaxNG: "); |
| 7506 | if (schema->doc == NULL) { |
| 7507 | fprintf(output, "no document\n"); |
| 7508 | } else if (schema->doc->URL != NULL) { |
| 7509 | fprintf(output, "%s\n", schema->doc->URL); |
| 7510 | } else { |
| 7511 | fprintf(output, "\n"); |
| 7512 | } |
| 7513 | if (schema->topgrammar == NULL) { |
| 7514 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7515 | return; |
| 7516 | } |
| 7517 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7518 | } |
| 7519 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7520 | /** |
| 7521 | * xmlRelaxNGDumpTree: |
| 7522 | * @output: the file output |
| 7523 | * @schema: a schema structure |
| 7524 | * |
| 7525 | * Dump the transformed RelaxNG tree. |
| 7526 | */ |
| 7527 | void |
| 7528 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7529 | { |
| 7530 | if (schema == NULL) { |
| 7531 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7532 | return; |
| 7533 | } |
| 7534 | if (schema->doc == NULL) { |
| 7535 | fprintf(output, "no document\n"); |
| 7536 | } else { |
| 7537 | xmlDocDump(output, schema->doc); |
| 7538 | } |
| 7539 | } |
| 7540 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7541 | /************************************************************************ |
| 7542 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7543 | * Validation of compiled content * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7544 | * * |
| 7545 | ************************************************************************/ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7546 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7547 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7548 | |
| 7549 | /** |
| 7550 | * xmlRelaxNGValidateCompiledCallback: |
| 7551 | * @exec: the regular expression instance |
| 7552 | * @token: the token which matched |
| 7553 | * @transdata: callback data, the define for the subelement if available |
| 7554 | @ @inputdata: callback data, the Relax NG validation context |
| 7555 | * |
| 7556 | * Handle the callback and if needed validate the element children. |
| 7557 | */ |
| 7558 | static void |
| 7559 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
| 7560 | const xmlChar *token, |
| 7561 | void *transdata, |
| 7562 | void *inputdata) { |
| 7563 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7564 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7565 | int ret; |
| 7566 | |
| 7567 | #ifdef DEBUG_COMPILE |
| 7568 | xmlGenericError(xmlGenericErrorContext, |
| 7569 | "Compiled callback for: '%s'\n", token); |
| 7570 | #endif |
| 7571 | if (ctxt == NULL) { |
| 7572 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7573 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7574 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7575 | return; |
| 7576 | } |
| 7577 | if (define == NULL) { |
| 7578 | if (token[0] == '#') |
| 7579 | return; |
| 7580 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7581 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7582 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7583 | return; |
| 7584 | } |
| 7585 | if ((ctxt == NULL) || (define == NULL)) { |
| 7586 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7587 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7588 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7589 | return; |
| 7590 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
| 7591 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7592 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7593 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7594 | return; |
| 7595 | } |
| 7596 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
| 7597 | } |
| 7598 | |
| 7599 | /** |
| 7600 | * xmlRelaxNGValidateCompiledContent: |
| 7601 | * @ctxt: the RelaxNG validation context |
| 7602 | * @regexp: the regular expression as compiled |
| 7603 | * @content: list of children to test against the regexp |
| 7604 | * |
| 7605 | * Validate the content model of an element or start using the regexp |
| 7606 | * |
| 7607 | * Returns 0 in case of success, -1 in case of error. |
| 7608 | */ |
| 7609 | static int |
| 7610 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 7611 | xmlRegexpPtr regexp, xmlNodePtr content) { |
| 7612 | xmlRegExecCtxtPtr exec; |
| 7613 | xmlNodePtr cur; |
| 7614 | int ret; |
| 7615 | |
| 7616 | if ((ctxt == NULL) || (regexp == NULL)) |
| 7617 | return(-1); |
| 7618 | exec = xmlRegNewExecCtxt(regexp, |
| 7619 | xmlRelaxNGValidateCompiledCallback, ctxt); |
| 7620 | cur = content; |
| 7621 | while (cur != NULL) { |
| 7622 | ctxt->state->seq = cur; |
| 7623 | switch (cur->type) { |
| 7624 | case XML_TEXT_NODE: |
| 7625 | case XML_CDATA_SECTION_NODE: |
| 7626 | if (xmlIsBlankNode(cur)) |
| 7627 | break; |
| 7628 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7629 | if (ret < 0) { |
| 7630 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, cur->parent->name); |
| 7631 | } |
| 7632 | break; |
| 7633 | case XML_ELEMENT_NODE: |
| 7634 | if (cur->ns != NULL) { |
| 7635 | ret = xmlRegExecPushString2(exec, cur->name, |
| 7636 | cur->ns->href, ctxt); |
| 7637 | } else { |
| 7638 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 7639 | } |
| 7640 | if (ret < 0) { |
| 7641 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 7642 | } |
| 7643 | break; |
| 7644 | default: |
| 7645 | break; |
| 7646 | } |
| 7647 | if (ret < 0) break; |
| 7648 | /* |
| 7649 | * Switch to next element |
| 7650 | */ |
| 7651 | cur = cur->next; |
| 7652 | } |
| 7653 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 7654 | if (ret == 1) { |
| 7655 | ret = 0; |
| 7656 | ctxt->state->seq = NULL; |
| 7657 | } else if (ret == 0) { |
| 7658 | /* |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7659 | * TODO: get some of the names needed to exit the current state of exec |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7660 | */ |
| 7661 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 7662 | ret = -1; |
| 7663 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7664 | xmlRelaxNGDumpValidError(ctxt); |
| 7665 | } else { |
| 7666 | ret = -1; |
| 7667 | } |
| 7668 | xmlRegFreeExecCtxt(exec); |
| 7669 | return(ret); |
| 7670 | } |
| 7671 | |
| 7672 | /************************************************************************ |
| 7673 | * * |
| 7674 | * Progressive validation of when possible * |
| 7675 | * * |
| 7676 | ************************************************************************/ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7677 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7678 | xmlRelaxNGDefinePtr defines); |
| 7679 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt); |
| 7680 | |
| 7681 | /** |
| 7682 | * xmlRelaxNGElemPush: |
| 7683 | * @ctxt: the validation context |
| 7684 | * @exec: the regexp runtime for the new content model |
| 7685 | * |
| 7686 | * Push a new regexp for the current node content model on the stack |
| 7687 | * |
| 7688 | * Returns 0 in case of success and -1 in case of error. |
| 7689 | */ |
| 7690 | static int |
| 7691 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) { |
| 7692 | if (ctxt->elemTab == NULL) { |
| 7693 | ctxt->elemMax = 10; |
| 7694 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 7695 | sizeof(xmlRegExecCtxtPtr)); |
| 7696 | if (ctxt->elemTab == NULL) { |
| 7697 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7698 | return(-1); |
| 7699 | } |
| 7700 | } |
| 7701 | if (ctxt->elemNr >= ctxt->elemMax) { |
| 7702 | ctxt->elemMax *= 2; |
| 7703 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
| 7704 | ctxt->elemMax * sizeof(xmlRegExecCtxtPtr)); |
| 7705 | if (ctxt->elemTab == NULL) { |
| 7706 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7707 | return(-1); |
| 7708 | } |
| 7709 | } |
| 7710 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 7711 | ctxt->elem = exec; |
| 7712 | return(0); |
| 7713 | } |
| 7714 | |
| 7715 | /** |
| 7716 | * xmlRelaxNGElemPop: |
| 7717 | * @ctxt: the validation context |
| 7718 | * |
| 7719 | * Pop the regexp of the current node content model from the stack |
| 7720 | * |
| 7721 | * Returns the exec or NULL if empty |
| 7722 | */ |
| 7723 | static xmlRegExecCtxtPtr |
| 7724 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) { |
| 7725 | xmlRegExecCtxtPtr ret; |
| 7726 | |
| 7727 | if (ctxt->elemNr <= 0) return(NULL); |
| 7728 | ctxt->elemNr--; |
| 7729 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 7730 | ctxt->elemTab[ctxt->elemNr] = NULL; |
| 7731 | if (ctxt->elemNr > 0) |
| 7732 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 7733 | else |
| 7734 | ctxt->elem = NULL; |
| 7735 | return(ret); |
| 7736 | } |
| 7737 | |
| 7738 | /** |
| 7739 | * xmlRelaxNGValidateProgressiveCallback: |
| 7740 | * @exec: the regular expression instance |
| 7741 | * @token: the token which matched |
| 7742 | * @transdata: callback data, the define for the subelement if available |
| 7743 | @ @inputdata: callback data, the Relax NG validation context |
| 7744 | * |
| 7745 | * Handle the callback and if needed validate the element children. |
| 7746 | * some of the in/out informations are passed via the context in @inputdata. |
| 7747 | */ |
| 7748 | static void |
| 7749 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
| 7750 | const xmlChar *token, |
| 7751 | void *transdata, |
| 7752 | void *inputdata) { |
| 7753 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7754 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7755 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7756 | xmlNodePtr node = ctxt->pnode; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7757 | int ret, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7758 | |
| 7759 | #ifdef DEBUG_PROGRESSIVE |
| 7760 | xmlGenericError(xmlGenericErrorContext, |
| 7761 | "Progressive callback for: '%s'\n", token); |
| 7762 | #endif |
| 7763 | if (ctxt == NULL) { |
| 7764 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7765 | return; |
| 7766 | } |
| 7767 | ctxt->pstate = 1; |
| 7768 | if (define == NULL) { |
| 7769 | if (token[0] == '#') |
| 7770 | return; |
| 7771 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7772 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7773 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7774 | ctxt->pstate = -1; |
| 7775 | return; |
| 7776 | } |
| 7777 | if ((ctxt == NULL) || (define == NULL)) { |
| 7778 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7779 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7780 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7781 | ctxt->pstate = -1; |
| 7782 | return; |
| 7783 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
| 7784 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7785 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7786 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7787 | ctxt->pstate = -1; |
| 7788 | return; |
| 7789 | } |
| 7790 | if (node->type != XML_ELEMENT_NODE) { |
| 7791 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 7792 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7793 | xmlRelaxNGDumpValidError(ctxt); |
| 7794 | ctxt->pstate = -1; |
| 7795 | return; |
| 7796 | } |
| 7797 | if (define->contModel == NULL) { |
| 7798 | /* |
| 7799 | * this node cannot be validated in a streamable fashion |
| 7800 | */ |
| 7801 | #ifdef DEBUG_PROGRESSIVE |
| 7802 | xmlGenericError(xmlGenericErrorContext, |
| 7803 | "Element '%s' validation is not streamable\n", token); |
| 7804 | #endif |
| 7805 | ctxt->pstate = 0; |
| 7806 | ctxt->pdef = define; |
| 7807 | return; |
| 7808 | } |
| 7809 | exec = xmlRegNewExecCtxt(define->contModel, |
| 7810 | xmlRelaxNGValidateProgressiveCallback, |
| 7811 | ctxt); |
| 7812 | if (exec == NULL) { |
| 7813 | ctxt->pstate = -1; |
| 7814 | return; |
| 7815 | } |
| 7816 | xmlRelaxNGElemPush(ctxt, exec); |
| 7817 | |
| 7818 | /* |
| 7819 | * Validate the attributes part of the content. |
| 7820 | */ |
| 7821 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 7822 | if (state == NULL) { |
| 7823 | ctxt->pstate = -1; |
| 7824 | return; |
| 7825 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7826 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7827 | ctxt->state = state; |
| 7828 | if (define->attrs != NULL) { |
| 7829 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 7830 | if (ret != 0) { |
| 7831 | ctxt->pstate = -1; |
| 7832 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 7833 | } |
| 7834 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7835 | if (ctxt->state != NULL) { |
| 7836 | ctxt->state->seq = NULL; |
| 7837 | ret = xmlRelaxNGValidateElementEnd(ctxt); |
| 7838 | if (ret != 0) { |
| 7839 | ctxt->pstate = -1; |
| 7840 | } |
| 7841 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 7842 | } else if (ctxt->states != NULL) { |
| 7843 | int tmp = -1, i; |
| 7844 | |
| 7845 | oldflags = ctxt->flags; |
| 7846 | ctxt->flags |= FLAGS_IGNORABLE; |
| 7847 | |
| 7848 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 7849 | state = ctxt->states->tabState[i]; |
| 7850 | ctxt->state = state; |
| 7851 | ctxt->state->seq = NULL; |
| 7852 | |
| 7853 | if (xmlRelaxNGValidateElementEnd(ctxt) == 0) |
| 7854 | tmp = 0; |
| 7855 | xmlRelaxNGFreeValidState(ctxt, state); |
| 7856 | } |
| 7857 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 7858 | ctxt->states = NULL; |
| 7859 | if ((ret == 0) && (tmp == -1)) |
| 7860 | ctxt->pstate = -1; |
| 7861 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7862 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7863 | if (ctxt->pstate == -1) { |
| 7864 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 7865 | xmlRelaxNGDumpValidError(ctxt); |
| 7866 | } |
| 7867 | } |
| 7868 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7869 | } |
| 7870 | |
| 7871 | /** |
| 7872 | * xmlRelaxNGValidatePushElement: |
| 7873 | * @ctxt: the validation context |
| 7874 | * @doc: a document instance |
| 7875 | * @elem: an element instance |
| 7876 | * |
| 7877 | * Push a new element start on the RelaxNG validation stack. |
| 7878 | * |
| 7879 | * returns 1 if no validation problem was found or 0 if validating the |
| 7880 | * element requires a full node, and -1 in case of error. |
| 7881 | */ |
| 7882 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame^] | 7883 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 7884 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7885 | xmlNodePtr elem) |
| 7886 | { |
| 7887 | int ret = 1; |
| 7888 | |
| 7889 | if ((ctxt == NULL) || (elem == NULL)) |
| 7890 | return (-1); |
| 7891 | |
| 7892 | #ifdef DEBUG_PROGRESSIVE |
| 7893 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 7894 | #endif |
| 7895 | if (ctxt->elem == 0) { |
| 7896 | xmlRelaxNGPtr schema; |
| 7897 | xmlRelaxNGGrammarPtr grammar; |
| 7898 | xmlRegExecCtxtPtr exec; |
| 7899 | xmlRelaxNGDefinePtr define; |
| 7900 | |
| 7901 | schema = ctxt->schema; |
| 7902 | if (schema == NULL) { |
| 7903 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 7904 | return (-1); |
| 7905 | } |
| 7906 | grammar = schema->topgrammar; |
| 7907 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 7908 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 7909 | return (-1); |
| 7910 | } |
| 7911 | define = grammar->start; |
| 7912 | if (define->contModel == NULL) { |
| 7913 | ctxt->pdef = define; |
| 7914 | return (0); |
| 7915 | } |
| 7916 | exec = xmlRegNewExecCtxt(define->contModel, |
| 7917 | xmlRelaxNGValidateProgressiveCallback, |
| 7918 | ctxt); |
| 7919 | if (exec == NULL) { |
| 7920 | return (-1); |
| 7921 | } |
| 7922 | xmlRelaxNGElemPush(ctxt, exec); |
| 7923 | } |
| 7924 | ctxt->pnode = elem; |
| 7925 | ctxt->pstate = 0; |
| 7926 | if (elem->ns != NULL) { |
| 7927 | ret = |
| 7928 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 7929 | ctxt); |
| 7930 | } else { |
| 7931 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 7932 | } |
| 7933 | if (ret < 0) { |
| 7934 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 7935 | } else { |
| 7936 | if (ctxt->pstate == 0) |
| 7937 | ret = 0; |
| 7938 | else if (ctxt->pstate < 0) |
| 7939 | ret = -1; |
| 7940 | else |
| 7941 | ret = 1; |
| 7942 | } |
| 7943 | #ifdef DEBUG_PROGRESSIVE |
| 7944 | if (ret < 0) |
| 7945 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 7946 | elem->name); |
| 7947 | #endif |
| 7948 | return (ret); |
| 7949 | } |
| 7950 | |
| 7951 | /** |
| 7952 | * xmlRelaxNGValidatePushCData: |
| 7953 | * @ctxt: the RelaxNG validation context |
| 7954 | * @data: some character data read |
| 7955 | * @len: the lenght of the data |
| 7956 | * |
| 7957 | * check the CData parsed for validation in the current stack |
| 7958 | * |
| 7959 | * returns 1 if no validation problem was found or -1 otherwise |
| 7960 | */ |
| 7961 | int |
| 7962 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame^] | 7963 | const xmlChar * data, |
| 7964 | int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7965 | { |
| 7966 | int ret = 1; |
| 7967 | |
| 7968 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 7969 | return (-1); |
| 7970 | |
| 7971 | #ifdef DEBUG_PROGRESSIVE |
| 7972 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 7973 | #endif |
| 7974 | |
| 7975 | while (*data != 0) { |
| 7976 | if (!IS_BLANK(*data)) |
| 7977 | break; |
| 7978 | data++; |
| 7979 | } |
| 7980 | if (*data == 0) |
| 7981 | return(1); |
| 7982 | |
| 7983 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 7984 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame^] | 7985 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7986 | #ifdef DEBUG_PROGRESSIVE |
| 7987 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
| 7988 | #endif |
| 7989 | |
| 7990 | return(-1); |
| 7991 | } |
| 7992 | return(1); |
| 7993 | } |
| 7994 | |
| 7995 | /** |
| 7996 | * xmlRelaxNGValidatePopElement: |
| 7997 | * @ctxt: the RelaxNG validation context |
| 7998 | * @doc: a document instance |
| 7999 | * @elem: an element instance |
| 8000 | * |
| 8001 | * Pop the element end from the RelaxNG validation stack. |
| 8002 | * |
| 8003 | * returns 1 if no validation problem was found or 0 otherwise |
| 8004 | */ |
| 8005 | int |
| 8006 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8007 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
| 8008 | xmlNodePtr elem) { |
| 8009 | int ret; |
| 8010 | xmlRegExecCtxtPtr exec; |
| 8011 | |
| 8012 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) return(-1); |
| 8013 | #ifdef DEBUG_PROGRESSIVE |
| 8014 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8015 | #endif |
| 8016 | /* |
| 8017 | * verify that we reached a terminal state of the content model. |
| 8018 | */ |
| 8019 | exec = xmlRelaxNGElemPop(ctxt); |
| 8020 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8021 | if (ret == 0) { |
| 8022 | /* |
| 8023 | * TODO: get some of the names needed to exit the current state of exec |
| 8024 | */ |
| 8025 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8026 | ret = -1; |
| 8027 | } else if (ret < 0) { |
| 8028 | ret = -1; |
| 8029 | } else { |
| 8030 | ret = 1; |
| 8031 | } |
| 8032 | xmlRegFreeExecCtxt(exec); |
| 8033 | #ifdef DEBUG_PROGRESSIVE |
| 8034 | if (ret < 0) |
| 8035 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8036 | elem->name); |
| 8037 | #endif |
| 8038 | return(ret); |
| 8039 | } |
| 8040 | |
| 8041 | /** |
| 8042 | * xmlRelaxNGValidateFullElement: |
| 8043 | * @ctxt: the validation context |
| 8044 | * @doc: a document instance |
| 8045 | * @elem: an element instance |
| 8046 | * |
| 8047 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8048 | * 0 and the content of the node has been expanded. |
| 8049 | * |
| 8050 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8051 | */ |
| 8052 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame^] | 8053 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8054 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8055 | xmlNodePtr elem) { |
| 8056 | int ret; |
| 8057 | xmlRelaxNGValidStatePtr state; |
| 8058 | |
| 8059 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) return(-1); |
| 8060 | #ifdef DEBUG_PROGRESSIVE |
| 8061 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8062 | #endif |
| 8063 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8064 | if (state == NULL) { |
| 8065 | return(-1); |
| 8066 | } |
| 8067 | state->seq = elem; |
| 8068 | ctxt->state = state; |
| 8069 | ctxt->errNo = XML_RELAXNG_OK; |
| 8070 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
| 8071 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) ret = -1; |
| 8072 | else ret = 1; |
| 8073 | xmlRelaxNGFreeValidState(ctxt, state); |
| 8074 | ctxt->state = NULL; |
| 8075 | #ifdef DEBUG_PROGRESSIVE |
| 8076 | if (ret < 0) |
| 8077 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8078 | elem->name); |
| 8079 | #endif |
| 8080 | return(ret); |
| 8081 | } |
| 8082 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8083 | /************************************************************************ |
| 8084 | * * |
| 8085 | * Generic interpreted validation implementation * |
| 8086 | * * |
| 8087 | ************************************************************************/ |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8088 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8089 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8090 | |
| 8091 | /** |
| 8092 | * xmlRelaxNGSkipIgnored: |
| 8093 | * @ctxt: a schema validation context |
| 8094 | * @node: the top node. |
| 8095 | * |
| 8096 | * Skip ignorable nodes in that context |
| 8097 | * |
| 8098 | * Returns the new sibling or NULL in case of error. |
| 8099 | */ |
| 8100 | static xmlNodePtr |
| 8101 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 8102 | xmlNodePtr node) { |
| 8103 | /* |
| 8104 | * TODO complete and handle entities |
| 8105 | */ |
| 8106 | while ((node != NULL) && |
| 8107 | ((node->type == XML_COMMENT_NODE) || |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8108 | (node->type == XML_PI_NODE) || |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 8109 | (((node->type == XML_TEXT_NODE) || |
| 8110 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8111 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8112 | (IS_BLANK_NODE(node)))))) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8113 | node = node->next; |
| 8114 | } |
| 8115 | return(node); |
| 8116 | } |
| 8117 | |
| 8118 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8119 | * xmlRelaxNGNormalize: |
| 8120 | * @ctxt: a schema validation context |
| 8121 | * @str: the string to normalize |
| 8122 | * |
| 8123 | * Implements the normalizeWhiteSpace( s ) function from |
| 8124 | * section 6.2.9 of the spec |
| 8125 | * |
| 8126 | * Returns the new string or NULL in case of error. |
| 8127 | */ |
| 8128 | static xmlChar * |
| 8129 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) { |
| 8130 | xmlChar *ret, *p; |
| 8131 | const xmlChar *tmp; |
| 8132 | int len; |
| 8133 | |
| 8134 | if (str == NULL) |
| 8135 | return(NULL); |
| 8136 | tmp = str; |
| 8137 | while (*tmp != 0) tmp++; |
| 8138 | len = tmp - str; |
| 8139 | |
| 8140 | ret = (xmlChar *) xmlMalloc((len + 1) * sizeof(xmlChar)); |
| 8141 | if (ret == NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8142 | if (ctxt != NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8143 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8144 | } else { |
| 8145 | xmlGenericError(xmlGenericErrorContext, |
| 8146 | "xmlRelaxNGNormalize: out of memory\n"); |
| 8147 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8148 | return(NULL); |
| 8149 | } |
| 8150 | p = ret; |
| 8151 | while (IS_BLANK(*str)) str++; |
| 8152 | while (*str != 0) { |
| 8153 | if (IS_BLANK(*str)) { |
| 8154 | while (IS_BLANK(*str)) str++; |
| 8155 | if (*str == 0) |
| 8156 | break; |
| 8157 | *p++ = ' '; |
| 8158 | } else |
| 8159 | *p++ = *str++; |
| 8160 | } |
| 8161 | *p = 0; |
| 8162 | return(ret); |
| 8163 | } |
| 8164 | |
| 8165 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8166 | * xmlRelaxNGValidateDatatype: |
| 8167 | * @ctxt: a Relax-NG validation context |
| 8168 | * @value: the string value |
| 8169 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8170 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8171 | * |
| 8172 | * Validate the given value against the dataype |
| 8173 | * |
| 8174 | * Returns 0 if the validation succeeded or an error code. |
| 8175 | */ |
| 8176 | static int |
| 8177 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8178 | xmlRelaxNGDefinePtr define, xmlNodePtr node) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8179 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8180 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8181 | void *result = NULL; |
| 8182 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8183 | |
| 8184 | if ((define == NULL) || (define->data == NULL)) { |
| 8185 | return(-1); |
| 8186 | } |
| 8187 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8188 | if (lib->check != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8189 | if ((define->attrs != NULL) && |
| 8190 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8191 | ret = lib->check(lib->data, define->name, value, &result, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8192 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8193 | ret = lib->check(lib->data, define->name, value, NULL, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8194 | } |
| 8195 | } else |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8196 | ret = -1; |
| 8197 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8198 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8199 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8200 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8201 | return(-1); |
| 8202 | } else if (ret == 1) { |
| 8203 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8204 | } else if (ret == 2) { |
| 8205 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8206 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8207 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8208 | ret = -1; |
| 8209 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8210 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8211 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
| 8212 | if (lib->facet != NULL) { |
| 8213 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8214 | cur->value, value, result); |
| 8215 | if (tmp != 0) |
| 8216 | ret = -1; |
| 8217 | } |
| 8218 | cur = cur->next; |
| 8219 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8220 | if ((ret == 0) && (define->content != NULL)) { |
| 8221 | const xmlChar *oldvalue, *oldendvalue; |
| 8222 | |
| 8223 | oldvalue = ctxt->state->value; |
| 8224 | oldendvalue = ctxt->state->endvalue; |
| 8225 | ctxt->state->value = (xmlChar *) value; |
| 8226 | ctxt->state->endvalue = NULL; |
| 8227 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8228 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8229 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
| 8230 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8231 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8232 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8233 | return(ret); |
| 8234 | } |
| 8235 | |
| 8236 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8237 | * xmlRelaxNGNextValue: |
| 8238 | * @ctxt: a Relax-NG validation context |
| 8239 | * |
| 8240 | * Skip to the next value when validating within a list |
| 8241 | * |
| 8242 | * Returns 0 if the operation succeeded or an error code. |
| 8243 | */ |
| 8244 | static int |
| 8245 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) { |
| 8246 | xmlChar *cur; |
| 8247 | |
| 8248 | cur = ctxt->state->value; |
| 8249 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
| 8250 | ctxt->state->value = NULL; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 8251 | ctxt->state->endvalue = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8252 | return(0); |
| 8253 | } |
| 8254 | while (*cur != 0) cur++; |
| 8255 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++; |
| 8256 | if (cur == ctxt->state->endvalue) |
| 8257 | ctxt->state->value = NULL; |
| 8258 | else |
| 8259 | ctxt->state->value = cur; |
| 8260 | return(0); |
| 8261 | } |
| 8262 | |
| 8263 | /** |
| 8264 | * xmlRelaxNGValidateValueList: |
| 8265 | * @ctxt: a Relax-NG validation context |
| 8266 | * @defines: the list of definitions to verify |
| 8267 | * |
| 8268 | * Validate the given set of definitions for the current value |
| 8269 | * |
| 8270 | * Returns 0 if the validation succeeded or an error code. |
| 8271 | */ |
| 8272 | static int |
| 8273 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8274 | xmlRelaxNGDefinePtr defines) { |
| 8275 | int ret = 0; |
| 8276 | |
| 8277 | while (defines != NULL) { |
| 8278 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8279 | if (ret != 0) |
| 8280 | break; |
| 8281 | defines = defines->next; |
| 8282 | } |
| 8283 | return(ret); |
| 8284 | } |
| 8285 | |
| 8286 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8287 | * xmlRelaxNGValidateValue: |
| 8288 | * @ctxt: a Relax-NG validation context |
| 8289 | * @define: the definition to verify |
| 8290 | * |
| 8291 | * Validate the given definition for the current value |
| 8292 | * |
| 8293 | * Returns 0 if the validation succeeded or an error code. |
| 8294 | */ |
| 8295 | static int |
| 8296 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8297 | xmlRelaxNGDefinePtr define) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8298 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8299 | xmlChar *value; |
| 8300 | |
| 8301 | value = ctxt->state->value; |
| 8302 | switch (define->type) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8303 | case XML_RELAXNG_EMPTY: { |
| 8304 | if ((value != NULL) && (value[0] != 0)) { |
| 8305 | int idx = 0; |
| 8306 | |
| 8307 | while (IS_BLANK(value[idx])) |
| 8308 | idx++; |
| 8309 | if (value[idx] != 0) |
| 8310 | ret = -1; |
| 8311 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8312 | break; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8313 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8314 | case XML_RELAXNG_TEXT: |
| 8315 | break; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8316 | case XML_RELAXNG_VALUE: { |
| 8317 | if (!xmlStrEqual(value, define->value)) { |
| 8318 | if (define->name != NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8319 | xmlRelaxNGTypeLibraryPtr lib; |
| 8320 | |
| 8321 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 8322 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8323 | ret = lib->comp(lib->data, define->name, |
| 8324 | define->value, define->node, |
| 8325 | (void *) define->attrs, |
| 8326 | value, ctxt->state->node); |
| 8327 | } else |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8328 | ret = -1; |
| 8329 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8330 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, define->name); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8331 | return(-1); |
| 8332 | } else if (ret == 1) { |
| 8333 | ret = 0; |
| 8334 | } else { |
| 8335 | ret = -1; |
| 8336 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8337 | } else { |
| 8338 | xmlChar *nval, *nvalue; |
| 8339 | |
| 8340 | /* |
| 8341 | * TODO: trivial optimizations are possible by |
| 8342 | * computing at compile-time |
| 8343 | */ |
| 8344 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8345 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
| 8346 | |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8347 | if ((nval == NULL) || (nvalue == NULL) || |
| 8348 | (!xmlStrEqual(nval, nvalue))) |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8349 | ret = -1; |
| 8350 | if (nval != NULL) |
| 8351 | xmlFree(nval); |
| 8352 | if (nvalue != NULL) |
| 8353 | xmlFree(nvalue); |
| 8354 | } |
| 8355 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8356 | if (ret == 0) |
| 8357 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8358 | break; |
| 8359 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8360 | case XML_RELAXNG_DATATYPE: { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8361 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8362 | ctxt->state->seq); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8363 | if (ret == 0) |
| 8364 | xmlRelaxNGNextValue(ctxt); |
| 8365 | |
| 8366 | break; |
| 8367 | } |
| 8368 | case XML_RELAXNG_CHOICE: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8369 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8370 | xmlChar *oldvalue; |
| 8371 | |
| 8372 | oldflags = ctxt->flags; |
| 8373 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8374 | |
| 8375 | oldvalue = ctxt->state->value; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8376 | while (list != NULL) { |
| 8377 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8378 | if (ret == 0) { |
| 8379 | break; |
| 8380 | } |
| 8381 | ctxt->state->value = oldvalue; |
| 8382 | list = list->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8383 | } |
| 8384 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8385 | if (ret != 0) { |
| 8386 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8387 | xmlRelaxNGDumpValidError(ctxt); |
| 8388 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8389 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8390 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8391 | if (ret == 0) |
| 8392 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8393 | break; |
| 8394 | } |
| 8395 | case XML_RELAXNG_LIST: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8396 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8397 | xmlChar *oldvalue, *oldend, *val, *cur; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8398 | #ifdef DEBUG_LIST |
| 8399 | int nb_values = 0; |
| 8400 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8401 | |
| 8402 | oldvalue = ctxt->state->value; |
| 8403 | oldend = ctxt->state->endvalue; |
| 8404 | |
| 8405 | val = xmlStrdup(oldvalue); |
| 8406 | if (val == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8407 | val = xmlStrdup(BAD_CAST ""); |
| 8408 | } |
| 8409 | if (val == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8410 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8411 | return(-1); |
| 8412 | } |
| 8413 | cur = val; |
| 8414 | while (*cur != 0) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8415 | if (IS_BLANK(*cur)) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8416 | *cur = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8417 | cur++; |
| 8418 | #ifdef DEBUG_LIST |
| 8419 | nb_values++; |
| 8420 | #endif |
| 8421 | while (IS_BLANK(*cur)) |
| 8422 | *cur++ = 0; |
| 8423 | } else |
| 8424 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8425 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8426 | #ifdef DEBUG_LIST |
| 8427 | xmlGenericError(xmlGenericErrorContext, |
| 8428 | "list value: '%s' found %d items\n", oldvalue, nb_values); |
| 8429 | nb_values = 0; |
| 8430 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8431 | ctxt->state->endvalue = cur; |
| 8432 | cur = val; |
| 8433 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8434 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8435 | ctxt->state->value = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8436 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8437 | while (list != NULL) { |
| 8438 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8439 | ctxt->state->value = NULL; |
| 8440 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8441 | if (ret != 0) { |
| 8442 | #ifdef DEBUG_LIST |
| 8443 | xmlGenericError(xmlGenericErrorContext, |
| 8444 | "Failed to validate value: '%s' with %d rule\n", |
| 8445 | ctxt->state->value, nb_values); |
| 8446 | #endif |
| 8447 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8448 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8449 | #ifdef DEBUG_LIST |
| 8450 | nb_values++; |
| 8451 | #endif |
| 8452 | list = list->next; |
| 8453 | } |
| 8454 | |
| 8455 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8456 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8457 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, ctxt->state->value); |
| 8458 | ret = -1; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8459 | } |
| 8460 | xmlFree(val); |
| 8461 | ctxt->state->value = oldvalue; |
| 8462 | ctxt->state->endvalue = oldend; |
| 8463 | break; |
| 8464 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8465 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8466 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8467 | if (ret != 0) { |
| 8468 | break; |
| 8469 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8470 | /* no break on purpose */ |
| 8471 | case XML_RELAXNG_ZEROORMORE: { |
| 8472 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8473 | |
| 8474 | oldflags = ctxt->flags; |
| 8475 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8476 | cur = ctxt->state->value; |
| 8477 | temp = NULL; |
| 8478 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8479 | (temp != cur)) { |
| 8480 | temp = cur; |
| 8481 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8482 | if (ret != 0) { |
| 8483 | ctxt->state->value = temp; |
| 8484 | ret = 0; |
| 8485 | break; |
| 8486 | } |
| 8487 | cur = ctxt->state->value; |
| 8488 | } |
| 8489 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8490 | if (ret != 0) { |
| 8491 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8492 | xmlRelaxNGDumpValidError(ctxt); |
| 8493 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8494 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8495 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8496 | break; |
| 8497 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8498 | case XML_RELAXNG_EXCEPT: { |
| 8499 | xmlRelaxNGDefinePtr list; |
| 8500 | |
| 8501 | list = define->content; |
| 8502 | while (list != NULL) { |
| 8503 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8504 | if (ret == 0) { |
| 8505 | ret = -1; |
| 8506 | break; |
| 8507 | } else |
| 8508 | ret = 0; |
| 8509 | list = list->next; |
| 8510 | } |
| 8511 | break; |
| 8512 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8513 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8514 | case XML_RELAXNG_GROUP: { |
| 8515 | xmlRelaxNGDefinePtr list; |
| 8516 | |
| 8517 | list = define->content; |
| 8518 | while (list != NULL) { |
| 8519 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8520 | if (ret != 0) { |
| 8521 | ret = -1; |
| 8522 | break; |
| 8523 | } else |
| 8524 | ret = 0; |
| 8525 | list = list->next; |
| 8526 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8527 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8528 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8529 | case XML_RELAXNG_REF: |
| 8530 | case XML_RELAXNG_PARENTREF: |
| 8531 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8532 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8533 | default: |
| 8534 | TODO |
| 8535 | ret = -1; |
| 8536 | } |
| 8537 | return(ret); |
| 8538 | } |
| 8539 | |
| 8540 | /** |
| 8541 | * xmlRelaxNGValidateValueContent: |
| 8542 | * @ctxt: a Relax-NG validation context |
| 8543 | * @defines: the list of definitions to verify |
| 8544 | * |
| 8545 | * Validate the given definitions for the current value |
| 8546 | * |
| 8547 | * Returns 0 if the validation succeeded or an error code. |
| 8548 | */ |
| 8549 | static int |
| 8550 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8551 | xmlRelaxNGDefinePtr defines) { |
| 8552 | int ret = 0; |
| 8553 | |
| 8554 | while (defines != NULL) { |
| 8555 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8556 | if (ret != 0) |
| 8557 | break; |
| 8558 | defines = defines->next; |
| 8559 | } |
| 8560 | return(ret); |
| 8561 | } |
| 8562 | |
| 8563 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8564 | * xmlRelaxNGAttributeMatch: |
| 8565 | * @ctxt: a Relax-NG validation context |
| 8566 | * @define: the definition to check |
| 8567 | * @prop: the attribute |
| 8568 | * |
| 8569 | * Check if the attribute matches the definition nameClass |
| 8570 | * |
| 8571 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 8572 | */ |
| 8573 | static int |
| 8574 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8575 | xmlRelaxNGDefinePtr define, |
| 8576 | xmlAttrPtr prop) { |
| 8577 | int ret; |
| 8578 | |
| 8579 | if (define->name != NULL) { |
| 8580 | if (!xmlStrEqual(define->name, prop->name)) |
| 8581 | return(0); |
| 8582 | } |
| 8583 | if (define->ns != NULL) { |
| 8584 | if (define->ns[0] == 0) { |
| 8585 | if (prop->ns != NULL) |
| 8586 | return(0); |
| 8587 | } else { |
| 8588 | if ((prop->ns == NULL) || |
| 8589 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 8590 | return(0); |
| 8591 | } |
| 8592 | } |
| 8593 | if (define->nameClass == NULL) |
| 8594 | return(1); |
| 8595 | define = define->nameClass; |
| 8596 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 8597 | xmlRelaxNGDefinePtr list; |
| 8598 | |
| 8599 | list = define->content; |
| 8600 | while (list != NULL) { |
| 8601 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 8602 | if (ret == 1) |
| 8603 | return(0); |
| 8604 | if (ret < 0) |
| 8605 | return(ret); |
| 8606 | list = list->next; |
| 8607 | } |
| 8608 | } else { |
| 8609 | TODO |
| 8610 | } |
| 8611 | return(1); |
| 8612 | } |
| 8613 | |
| 8614 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8615 | * xmlRelaxNGValidateAttribute: |
| 8616 | * @ctxt: a Relax-NG validation context |
| 8617 | * @define: the definition to verify |
| 8618 | * |
| 8619 | * Validate the given attribute definition for that node |
| 8620 | * |
| 8621 | * Returns 0 if the validation succeeded or an error code. |
| 8622 | */ |
| 8623 | static int |
| 8624 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 8625 | xmlRelaxNGDefinePtr define) { |
| 8626 | int ret = 0, i; |
| 8627 | xmlChar *value, *oldvalue; |
| 8628 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8629 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8630 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8631 | if (ctxt->state->nbAttrLeft <= 0) |
| 8632 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8633 | if (define->name != NULL) { |
| 8634 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 8635 | tmp = ctxt->state->attrs[i]; |
| 8636 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 8637 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 8638 | (tmp->ns == NULL)) || |
| 8639 | ((tmp->ns != NULL) && |
| 8640 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 8641 | prop = tmp; |
| 8642 | break; |
| 8643 | } |
| 8644 | } |
| 8645 | } |
| 8646 | if (prop != NULL) { |
| 8647 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8648 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8649 | oldseq = ctxt->state->seq; |
| 8650 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8651 | ctxt->state->value = value; |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8652 | ctxt->state->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8653 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8654 | if (ctxt->state->value != NULL) |
| 8655 | value = ctxt->state->value; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8656 | if (value != NULL) |
| 8657 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8658 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8659 | ctxt->state->seq = oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8660 | if (ret == 0) { |
| 8661 | /* |
| 8662 | * flag the attribute as processed |
| 8663 | */ |
| 8664 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8665 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8666 | } |
| 8667 | } else { |
| 8668 | ret = -1; |
| 8669 | } |
| 8670 | #ifdef DEBUG |
| 8671 | xmlGenericError(xmlGenericErrorContext, |
| 8672 | "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret); |
| 8673 | #endif |
| 8674 | } else { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8675 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 8676 | tmp = ctxt->state->attrs[i]; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 8677 | if ((tmp != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8678 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8679 | prop = tmp; |
| 8680 | break; |
| 8681 | } |
| 8682 | } |
| 8683 | if (prop != NULL) { |
| 8684 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8685 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8686 | oldseq = ctxt->state->seq; |
| 8687 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8688 | ctxt->state->value = value; |
| 8689 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8690 | if (ctxt->state->value != NULL) |
| 8691 | value = ctxt->state->value; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8692 | if (value != NULL) |
| 8693 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8694 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8695 | ctxt->state->seq = oldseq; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8696 | if (ret == 0) { |
| 8697 | /* |
| 8698 | * flag the attribute as processed |
| 8699 | */ |
| 8700 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8701 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8702 | } |
| 8703 | } else { |
| 8704 | ret = -1; |
| 8705 | } |
| 8706 | #ifdef DEBUG |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 8707 | if (define->ns != NULL) { |
| 8708 | xmlGenericError(xmlGenericErrorContext, |
| 8709 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 8710 | define->ns, ret); |
| 8711 | } else { |
| 8712 | xmlGenericError(xmlGenericErrorContext, |
| 8713 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 8714 | ret); |
| 8715 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8716 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8717 | } |
| 8718 | |
| 8719 | return(ret); |
| 8720 | } |
| 8721 | |
| 8722 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8723 | * xmlRelaxNGValidateAttributeList: |
| 8724 | * @ctxt: a Relax-NG validation context |
| 8725 | * @define: the list of definition to verify |
| 8726 | * |
| 8727 | * Validate the given node against the list of attribute definitions |
| 8728 | * |
| 8729 | * Returns 0 if the validation succeeded or an error code. |
| 8730 | */ |
| 8731 | static int |
| 8732 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8733 | xmlRelaxNGDefinePtr defines) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8734 | int ret = 0, res; |
| 8735 | int needmore = 0; |
| 8736 | xmlRelaxNGDefinePtr cur; |
| 8737 | |
| 8738 | cur = defines; |
| 8739 | while (cur != NULL) { |
| 8740 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 8741 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 8742 | ret = -1; |
| 8743 | } else |
| 8744 | needmore = 1; |
| 8745 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8746 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8747 | if (!needmore) |
| 8748 | return(ret); |
| 8749 | cur = defines; |
| 8750 | while (cur != NULL) { |
| 8751 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
| 8752 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 8753 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 8754 | if (res < 0) |
| 8755 | ret = -1; |
| 8756 | } else { |
| 8757 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8758 | return(-1); |
| 8759 | } |
| 8760 | if (res == -1) /* continues on -2 */ |
| 8761 | break; |
| 8762 | } |
| 8763 | cur = cur->next; |
| 8764 | } |
| 8765 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8766 | return(ret); |
| 8767 | } |
| 8768 | |
| 8769 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8770 | * xmlRelaxNGNodeMatchesList: |
| 8771 | * @node: the node |
| 8772 | * @list: a NULL terminated array of definitions |
| 8773 | * |
| 8774 | * Check if a node can be matched by one of the definitions |
| 8775 | * |
| 8776 | * Returns 1 if matches 0 otherwise |
| 8777 | */ |
| 8778 | static int |
| 8779 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) { |
| 8780 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8781 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8782 | |
| 8783 | if ((node == NULL) || (list == NULL)) |
| 8784 | return(0); |
| 8785 | |
| 8786 | cur = list[i++]; |
| 8787 | while (cur != NULL) { |
| 8788 | if ((node->type == XML_ELEMENT_NODE) && |
| 8789 | (cur->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8790 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 8791 | if (tmp == 1) |
| 8792 | return(1); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 8793 | } else if (((node->type == XML_TEXT_NODE) || |
| 8794 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8795 | (cur->type == XML_RELAXNG_TEXT)) { |
| 8796 | return(1); |
| 8797 | } |
| 8798 | cur = list[i++]; |
| 8799 | } |
| 8800 | return(0); |
| 8801 | } |
| 8802 | |
| 8803 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8804 | * xmlRelaxNGValidateInterleave: |
| 8805 | * @ctxt: a Relax-NG validation context |
| 8806 | * @define: the definition to verify |
| 8807 | * |
| 8808 | * Validate an interleave definition for a node. |
| 8809 | * |
| 8810 | * Returns 0 if the validation succeeded or an error code. |
| 8811 | */ |
| 8812 | static int |
| 8813 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 8814 | xmlRelaxNGDefinePtr define) { |
| 8815 | int ret = 0, i, nbgroups, left; |
| 8816 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8817 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8818 | |
| 8819 | xmlRelaxNGValidStatePtr oldstate; |
| 8820 | xmlRelaxNGPartitionPtr partitions; |
| 8821 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 8822 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 8823 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 8824 | |
| 8825 | if (define->data != NULL) { |
| 8826 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 8827 | nbgroups = partitions->nbgroups; |
| 8828 | left = nbgroups; |
| 8829 | } else { |
| 8830 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 8831 | return(-1); |
| 8832 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8833 | /* |
| 8834 | * Optimizations for MIXED |
| 8835 | */ |
| 8836 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8837 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8838 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 8839 | if (nbgroups == 2) { |
| 8840 | /* |
| 8841 | * this is a pure <mixed> case |
| 8842 | */ |
| 8843 | if (ctxt->state != NULL) |
| 8844 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8845 | ctxt->state->seq); |
| 8846 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 8847 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8848 | partitions->groups[1]->rule); |
| 8849 | else |
| 8850 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8851 | partitions->groups[0]->rule); |
| 8852 | if (ret == 0) { |
| 8853 | if (ctxt->state != NULL) |
| 8854 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8855 | ctxt->state->seq); |
| 8856 | } |
| 8857 | ctxt->flags = oldflags; |
| 8858 | return(ret); |
| 8859 | } |
| 8860 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8861 | |
| 8862 | /* |
| 8863 | * Build arrays to store the first and last node of the chain |
| 8864 | * pertaining to each group |
| 8865 | */ |
| 8866 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 8867 | if (list == NULL) { |
| 8868 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8869 | return(-1); |
| 8870 | } |
| 8871 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 8872 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 8873 | if (lasts == NULL) { |
| 8874 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8875 | return(-1); |
| 8876 | } |
| 8877 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 8878 | |
| 8879 | /* |
| 8880 | * Walk the sequence of children finding the right group and |
| 8881 | * sorting them in sequences. |
| 8882 | */ |
| 8883 | cur = ctxt->state->seq; |
| 8884 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 8885 | start = cur; |
| 8886 | while (cur != NULL) { |
| 8887 | ctxt->state->seq = cur; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 8888 | if ((partitions->triage != NULL) && |
| 8889 | (partitions->flags & IS_DETERMINIST)) { |
| 8890 | void *tmp = NULL; |
| 8891 | |
| 8892 | if ((cur->type == XML_TEXT_NODE) || |
| 8893 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 8894 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 8895 | NULL); |
| 8896 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 8897 | if (cur->ns != NULL) { |
| 8898 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 8899 | cur->ns->href); |
| 8900 | if (tmp == NULL) |
| 8901 | tmp = xmlHashLookup2(partitions->triage, |
| 8902 | BAD_CAST "#any", cur->ns->href); |
| 8903 | } else |
| 8904 | tmp = xmlHashLookup2(partitions->triage, cur->name, NULL); |
| 8905 | if (tmp == NULL) |
| 8906 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 8907 | NULL); |
| 8908 | } |
| 8909 | |
| 8910 | if (tmp == NULL) { |
| 8911 | i = nbgroups; |
| 8912 | } else { |
| 8913 | i = ((long) tmp) - 1; |
| 8914 | if (partitions->flags & IS_NEEDCHECK) { |
| 8915 | group = partitions->groups[i]; |
| 8916 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 8917 | i = nbgroups; |
| 8918 | } |
| 8919 | } |
| 8920 | } else { |
| 8921 | for (i = 0;i < nbgroups;i++) { |
| 8922 | group = partitions->groups[i]; |
| 8923 | if (group == NULL) |
| 8924 | continue; |
| 8925 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 8926 | break; |
| 8927 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8928 | } |
| 8929 | /* |
| 8930 | * We break as soon as an element not matched is found |
| 8931 | */ |
| 8932 | if (i >= nbgroups) { |
| 8933 | break; |
| 8934 | } |
| 8935 | if (lasts[i] != NULL) { |
| 8936 | lasts[i]->next = cur; |
| 8937 | lasts[i] = cur; |
| 8938 | } else { |
| 8939 | list[i] = cur; |
| 8940 | lasts[i] = cur; |
| 8941 | } |
| 8942 | if (cur->next != NULL) |
| 8943 | lastchg = cur->next; |
| 8944 | else |
| 8945 | lastchg = cur; |
| 8946 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
| 8947 | } |
| 8948 | if (ret != 0) { |
| 8949 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 8950 | ret = -1; |
| 8951 | goto done; |
| 8952 | } |
| 8953 | lastelem = cur; |
| 8954 | oldstate = ctxt->state; |
| 8955 | for (i = 0;i < nbgroups;i++) { |
| 8956 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 8957 | group = partitions->groups[i]; |
| 8958 | if (lasts[i] != NULL) { |
| 8959 | last = lasts[i]->next; |
| 8960 | lasts[i]->next = NULL; |
| 8961 | } |
| 8962 | ctxt->state->seq = list[i]; |
| 8963 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 8964 | if (ret != 0) |
| 8965 | break; |
| 8966 | if (ctxt->state != NULL) { |
| 8967 | cur = ctxt->state->seq; |
| 8968 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8969 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8970 | oldstate = ctxt->state; |
| 8971 | ctxt->state = NULL; |
| 8972 | if (cur != NULL) { |
| 8973 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 8974 | ret = -1; |
| 8975 | ctxt->state = oldstate; |
| 8976 | goto done; |
| 8977 | } |
| 8978 | } else if (ctxt->states != NULL) { |
| 8979 | int j; |
| 8980 | int found = 0; |
| 8981 | |
| 8982 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 8983 | cur = ctxt->states->tabState[j]->seq; |
| 8984 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 8985 | if (cur == NULL) { |
| 8986 | found = 1; |
| 8987 | break; |
| 8988 | } |
| 8989 | } |
| 8990 | if (ctxt->states->nbState > 0) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8991 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8992 | oldstate = ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 8993 | } |
| 8994 | for (j = 0;j < ctxt->states->nbState - 1;j++) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8995 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[j]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8996 | } |
| 8997 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8998 | ctxt->states = NULL; |
| 8999 | if (found == 0) { |
| 9000 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9001 | ret = -1; |
| 9002 | ctxt->state = oldstate; |
| 9003 | goto done; |
| 9004 | } |
| 9005 | } else { |
| 9006 | ret = -1; |
| 9007 | break; |
| 9008 | } |
| 9009 | if (lasts[i] != NULL) { |
| 9010 | lasts[i]->next = last; |
| 9011 | } |
| 9012 | } |
| 9013 | if (ctxt->state != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9014 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9015 | ctxt->state = oldstate; |
| 9016 | ctxt->state->seq = lastelem; |
| 9017 | if (ret != 0) { |
| 9018 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9019 | ret = -1; |
| 9020 | goto done; |
| 9021 | } |
| 9022 | |
| 9023 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9024 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9025 | /* |
| 9026 | * builds the next links chain from the prev one |
| 9027 | */ |
| 9028 | cur = lastchg; |
| 9029 | while (cur != NULL) { |
| 9030 | if ((cur == start) || (cur->prev == NULL)) |
| 9031 | break; |
| 9032 | cur->prev->next = cur; |
| 9033 | cur = cur->prev; |
| 9034 | } |
| 9035 | if (ret == 0) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 9036 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9037 | } |
| 9038 | |
| 9039 | xmlFree(list); |
| 9040 | xmlFree(lasts); |
| 9041 | return(ret); |
| 9042 | } |
| 9043 | |
| 9044 | /** |
| 9045 | * xmlRelaxNGValidateDefinitionList: |
| 9046 | * @ctxt: a Relax-NG validation context |
| 9047 | * @define: the list of definition to verify |
| 9048 | * |
| 9049 | * Validate the given node content against the (list) of definitions |
| 9050 | * |
| 9051 | * Returns 0 if the validation succeeded or an error code. |
| 9052 | */ |
| 9053 | static int |
| 9054 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9055 | xmlRelaxNGDefinePtr defines) { |
| 9056 | int ret = 0, res; |
| 9057 | |
| 9058 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9059 | if (defines == NULL) { |
| 9060 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, BAD_CAST "NULL definition list"); |
| 9061 | return(-1); |
| 9062 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9063 | while (defines != NULL) { |
| 9064 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9065 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9066 | if (res < 0) |
| 9067 | ret = -1; |
| 9068 | } else { |
| 9069 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9070 | return(-1); |
| 9071 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 9072 | if (res == -1) /* continues on -2 */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9073 | break; |
| 9074 | defines = defines->next; |
| 9075 | } |
| 9076 | |
| 9077 | return(ret); |
| 9078 | } |
| 9079 | |
| 9080 | /** |
| 9081 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9082 | * @ctxt: a Relax-NG validation context |
| 9083 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9084 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9085 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9086 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9087 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9088 | * Returns 1 if the element matches, 0 if no, or -1 in case of error |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9089 | */ |
| 9090 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9091 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9092 | xmlRelaxNGDefinePtr define, |
| 9093 | xmlNodePtr elem) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9094 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9095 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9096 | if (define->name != NULL) { |
| 9097 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9098 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9099 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9100 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9101 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9102 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
| 9103 | if (elem->ns == NULL) { |
| 9104 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, |
| 9105 | elem->name); |
| 9106 | return(0); |
| 9107 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9108 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9109 | elem->name, define->ns); |
| 9110 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9111 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9112 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
| 9113 | (define->name == NULL)) { |
| 9114 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 9115 | elem->name); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9116 | return(0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9117 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
| 9118 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 9119 | define->name); |
| 9120 | return(0); |
| 9121 | } |
| 9122 | |
| 9123 | if (define->nameClass == NULL) |
| 9124 | return(1); |
| 9125 | |
| 9126 | define = define->nameClass; |
| 9127 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 9128 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9129 | if (ctxt != NULL) { |
| 9130 | oldflags = ctxt->flags; |
| 9131 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9132 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9133 | |
| 9134 | list = define->content; |
| 9135 | while (list != NULL) { |
| 9136 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9137 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9138 | if (ctxt != NULL) |
| 9139 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9140 | return(0); |
| 9141 | } |
| 9142 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9143 | if (ctxt != NULL) |
| 9144 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9145 | return(ret); |
| 9146 | } |
| 9147 | list = list->next; |
| 9148 | } |
| 9149 | ret = 1; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9150 | if (ctxt != NULL) { |
| 9151 | ctxt->flags = oldflags; |
| 9152 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9153 | } else if (define->type == XML_RELAXNG_CHOICE) { |
| 9154 | xmlRelaxNGDefinePtr list; |
| 9155 | |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9156 | if (ctxt != NULL) { |
| 9157 | oldflags = ctxt->flags; |
| 9158 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9159 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9160 | |
| 9161 | list = define->nameClass; |
| 9162 | while (list != NULL) { |
| 9163 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9164 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9165 | if (ctxt != NULL) |
| 9166 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9167 | return(1); |
| 9168 | } |
| 9169 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9170 | if (ctxt != NULL) |
| 9171 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9172 | return(ret); |
| 9173 | } |
| 9174 | list = list->next; |
| 9175 | } |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9176 | if (ctxt != NULL) { |
| 9177 | if (ret != 0) { |
| 9178 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9179 | xmlRelaxNGDumpValidError(ctxt); |
| 9180 | } else { |
| 9181 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
| 9182 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9183 | } |
| 9184 | ret = 0; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9185 | if (ctxt != NULL) { |
| 9186 | ctxt->flags = oldflags; |
| 9187 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9188 | } else { |
| 9189 | TODO |
| 9190 | ret = -1; |
| 9191 | } |
| 9192 | return(ret); |
| 9193 | } |
| 9194 | |
| 9195 | /** |
| 9196 | * xmlRelaxNGValidateElementEnd: |
| 9197 | * @ctxt: a Relax-NG validation context |
| 9198 | * |
| 9199 | * Validate the end of the element, implements check that |
| 9200 | * there is nothing left not consumed in the element content |
| 9201 | * or in the attribute list. |
| 9202 | * |
| 9203 | * Returns 0 if the validation succeeded or an error code. |
| 9204 | */ |
| 9205 | static int |
| 9206 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt) { |
| 9207 | int ret = 0, i; |
| 9208 | xmlRelaxNGValidStatePtr state; |
| 9209 | |
| 9210 | state = ctxt->state; |
| 9211 | if (state->seq != NULL) { |
| 9212 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9213 | if (state->seq != NULL) { |
| 9214 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9215 | state->node->name, state->seq->name); |
| 9216 | ret = -1; |
| 9217 | } |
| 9218 | } |
| 9219 | for (i = 0;i < state->nbAttrs;i++) { |
| 9220 | if (state->attrs[i] != NULL) { |
| 9221 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9222 | state->attrs[i]->name, state->node->name); |
| 9223 | ret = -1; |
| 9224 | } |
| 9225 | } |
| 9226 | return(ret); |
| 9227 | } |
| 9228 | |
| 9229 | /** |
| 9230 | * xmlRelaxNGValidateState: |
| 9231 | * @ctxt: a Relax-NG validation context |
| 9232 | * @define: the definition to verify |
| 9233 | * |
| 9234 | * Validate the current state against the definition |
| 9235 | * |
| 9236 | * Returns 0 if the validation succeeded or an error code. |
| 9237 | */ |
| 9238 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9239 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9240 | xmlRelaxNGDefinePtr define) |
| 9241 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9242 | xmlNodePtr node; |
| 9243 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9244 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9245 | |
| 9246 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9247 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9248 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9249 | } |
| 9250 | |
| 9251 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9252 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9253 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9254 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9255 | } |
| 9256 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9257 | for (i = 0; i < ctxt->depth; i++) |
| 9258 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9259 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9260 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9261 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9262 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9263 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9264 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9265 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9266 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9267 | #endif |
| 9268 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9269 | switch (define->type) { |
| 9270 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9271 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9272 | ret = 0; |
| 9273 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9274 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9275 | ret = -1; |
| 9276 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9277 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9278 | while ((node != NULL) && |
| 9279 | ((node->type == XML_TEXT_NODE) || |
| 9280 | (node->type == XML_COMMENT_NODE) || |
| 9281 | (node->type == XML_PI_NODE) || |
| 9282 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9283 | node = node->next; |
| 9284 | ctxt->state->seq = node; |
| 9285 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9286 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9287 | errNr = ctxt->errNr; |
| 9288 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9289 | if (node == NULL) { |
| 9290 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9291 | ret = -1; |
| 9292 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9293 | xmlRelaxNGDumpValidError(ctxt); |
| 9294 | break; |
| 9295 | } |
| 9296 | if (node->type != XML_ELEMENT_NODE) { |
| 9297 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9298 | ret = -1; |
| 9299 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9300 | xmlRelaxNGDumpValidError(ctxt); |
| 9301 | break; |
| 9302 | } |
| 9303 | /* |
| 9304 | * This node was already validated successfully against |
| 9305 | * this definition. |
| 9306 | */ |
| 9307 | if (node->_private == define) { |
| 9308 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9309 | if (ctxt->errNr > errNr) |
| 9310 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9311 | if (ctxt->errNr != 0) { |
| 9312 | while ((ctxt->err != NULL) && |
| 9313 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9314 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9315 | || |
| 9316 | ((ctxt->err->err == |
| 9317 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9318 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9319 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9320 | || (ctxt->err->err == |
| 9321 | XML_RELAXNG_ERR_NOTELEM))) |
| 9322 | xmlRelaxNGValidErrorPop(ctxt); |
| 9323 | } |
| 9324 | break; |
| 9325 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9326 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9327 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9328 | if (ret <= 0) { |
| 9329 | ret = -1; |
| 9330 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9331 | xmlRelaxNGDumpValidError(ctxt); |
| 9332 | break; |
| 9333 | } |
| 9334 | ret = 0; |
| 9335 | if (ctxt->errNr != 0) { |
| 9336 | if (ctxt->errNr > errNr) |
| 9337 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9338 | while ((ctxt->err != NULL) && |
| 9339 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9340 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9341 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9342 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9343 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9344 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9345 | xmlRelaxNGValidErrorPop(ctxt); |
| 9346 | } |
| 9347 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9348 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9349 | oldflags = ctxt->flags; |
| 9350 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9351 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9352 | } |
| 9353 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9354 | if (state == NULL) { |
| 9355 | ret = -1; |
| 9356 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9357 | xmlRelaxNGDumpValidError(ctxt); |
| 9358 | break; |
| 9359 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9360 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9361 | oldstate = ctxt->state; |
| 9362 | ctxt->state = state; |
| 9363 | if (define->attrs != NULL) { |
| 9364 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9365 | if (tmp != 0) { |
| 9366 | ret = -1; |
| 9367 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9368 | } |
| 9369 | } |
| 9370 | if (define->contModel != NULL) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9371 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9372 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9373 | xmlNodePtr nseq; |
| 9374 | |
| 9375 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9376 | ctxt->state = nstate; |
| 9377 | ctxt->states = NULL; |
| 9378 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9379 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9380 | define->contModel, |
| 9381 | ctxt->state->seq); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9382 | nseq = ctxt->state->seq; |
| 9383 | ctxt->state = tmpstate; |
| 9384 | ctxt->states = tmpstates; |
| 9385 | xmlRelaxNGFreeValidState(ctxt, nstate); |
| 9386 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9387 | #ifdef DEBUG_COMPILE |
| 9388 | xmlGenericError(xmlGenericErrorContext, |
| 9389 | "Validating content of '%s' : %d\n", define->name, tmp); |
| 9390 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9391 | if (tmp != 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9392 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9393 | |
| 9394 | if (ctxt->states != NULL) { |
| 9395 | tmp = -1; |
| 9396 | |
| 9397 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9398 | |
| 9399 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9400 | state = ctxt->states->tabState[i]; |
| 9401 | ctxt->state = state; |
| 9402 | ctxt->state->seq = nseq; |
| 9403 | |
| 9404 | if (xmlRelaxNGValidateElementEnd(ctxt) == 0) |
| 9405 | tmp = 0; |
| 9406 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9407 | } |
| 9408 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9409 | ctxt->flags = oldflags; |
| 9410 | ctxt->states = NULL; |
| 9411 | if ((ret == 0) && (tmp == -1)) |
| 9412 | ret = -1; |
| 9413 | } else { |
| 9414 | state = ctxt->state; |
| 9415 | ctxt->state->seq = nseq; |
| 9416 | if (ret == 0) |
| 9417 | ret = xmlRelaxNGValidateElementEnd(ctxt); |
| 9418 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9419 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9420 | } else { |
| 9421 | if (define->content != NULL) { |
| 9422 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9423 | define->content); |
| 9424 | if (tmp != 0) { |
| 9425 | ret = -1; |
| 9426 | if (ctxt->state == NULL) { |
| 9427 | ctxt->state = oldstate; |
| 9428 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9429 | node->name); |
| 9430 | ctxt->state = NULL; |
| 9431 | } else { |
| 9432 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9433 | node->name); |
| 9434 | } |
| 9435 | |
| 9436 | } |
| 9437 | } |
| 9438 | if (ctxt->states != NULL) { |
| 9439 | tmp = -1; |
| 9440 | |
| 9441 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9442 | |
| 9443 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9444 | state = ctxt->states->tabState[i]; |
| 9445 | ctxt->state = state; |
| 9446 | |
| 9447 | if (xmlRelaxNGValidateElementEnd(ctxt) == 0) |
| 9448 | tmp = 0; |
| 9449 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9450 | } |
| 9451 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9452 | ctxt->flags = oldflags; |
| 9453 | ctxt->states = NULL; |
| 9454 | if ((ret == 0) && (tmp == -1)) |
| 9455 | ret = -1; |
| 9456 | } else { |
| 9457 | state = ctxt->state; |
| 9458 | if (ret == 0) |
| 9459 | ret = xmlRelaxNGValidateElementEnd(ctxt); |
| 9460 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9461 | } |
| 9462 | } |
| 9463 | if (ret == 0) { |
| 9464 | node->_private = define; |
| 9465 | } |
| 9466 | ctxt->flags = oldflags; |
| 9467 | ctxt->state = oldstate; |
| 9468 | if (oldstate != NULL) |
| 9469 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9470 | if (ret != 0) { |
| 9471 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9472 | xmlRelaxNGDumpValidError(ctxt); |
| 9473 | ret = 0; |
| 9474 | } else { |
| 9475 | ret = -2; |
| 9476 | } |
| 9477 | } else { |
| 9478 | if (ctxt->errNr > errNr) |
| 9479 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9480 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9481 | |
| 9482 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9483 | xmlGenericError(xmlGenericErrorContext, |
| 9484 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 9485 | node->name, ret); |
| 9486 | if (oldstate == NULL) |
| 9487 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 9488 | else if (oldstate->seq == NULL) |
| 9489 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 9490 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 9491 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 9492 | oldstate->seq->name); |
| 9493 | else |
| 9494 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 9495 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9496 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9497 | break; |
| 9498 | case XML_RELAXNG_OPTIONAL:{ |
| 9499 | errNr = ctxt->errNr; |
| 9500 | oldflags = ctxt->flags; |
| 9501 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9502 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9503 | ret = |
| 9504 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 9505 | define->content); |
| 9506 | if (ret != 0) { |
| 9507 | if (ctxt->state != NULL) |
| 9508 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9509 | ctxt->state = oldstate; |
| 9510 | ctxt->flags = oldflags; |
| 9511 | ret = 0; |
| 9512 | if (ctxt->errNr > errNr) |
| 9513 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9514 | break; |
| 9515 | } |
| 9516 | if (ctxt->states != NULL) { |
| 9517 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9518 | } else { |
| 9519 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 9520 | if (ctxt->states == NULL) { |
| 9521 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9522 | ctxt->flags = oldflags; |
| 9523 | ret = -1; |
| 9524 | if (ctxt->errNr > errNr) |
| 9525 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9526 | break; |
| 9527 | } |
| 9528 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9529 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 9530 | ctxt->state = NULL; |
| 9531 | } |
| 9532 | ctxt->flags = oldflags; |
| 9533 | ret = 0; |
| 9534 | if (ctxt->errNr > errNr) |
| 9535 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9536 | break; |
| 9537 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9538 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9539 | errNr = ctxt->errNr; |
| 9540 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9541 | if (ret != 0) { |
| 9542 | break; |
| 9543 | } |
| 9544 | if (ctxt->errNr > errNr) |
| 9545 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9546 | /* no break on purpose */ |
| 9547 | case XML_RELAXNG_ZEROORMORE:{ |
| 9548 | int progress; |
| 9549 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 9550 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9551 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9552 | errNr = ctxt->errNr; |
| 9553 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 9554 | if (res == NULL) { |
| 9555 | ret = -1; |
| 9556 | break; |
| 9557 | } |
| 9558 | /* |
| 9559 | * All the input states are also exit states |
| 9560 | */ |
| 9561 | if (ctxt->state != NULL) { |
| 9562 | xmlRelaxNGAddStates(ctxt, res, |
| 9563 | xmlRelaxNGCopyValidState(ctxt, |
| 9564 | ctxt-> |
| 9565 | state)); |
| 9566 | } else { |
| 9567 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9568 | xmlRelaxNGAddStates(ctxt, res, |
| 9569 | xmlRelaxNGCopyValidState(ctxt, |
| 9570 | ctxt-> |
| 9571 | states-> |
| 9572 | tabState |
| 9573 | [j])); |
| 9574 | } |
| 9575 | } |
| 9576 | oldflags = ctxt->flags; |
| 9577 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9578 | do { |
| 9579 | progress = 0; |
| 9580 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9581 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9582 | if (ctxt->states != NULL) { |
| 9583 | states = ctxt->states; |
| 9584 | for (i = 0; i < states->nbState; i++) { |
| 9585 | ctxt->state = states->tabState[i]; |
| 9586 | ctxt->states = NULL; |
| 9587 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9588 | define-> |
| 9589 | content); |
| 9590 | if (ret == 0) { |
| 9591 | if (ctxt->state != NULL) { |
| 9592 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9593 | ctxt->state); |
| 9594 | ctxt->state = NULL; |
| 9595 | if (tmp == 1) |
| 9596 | progress = 1; |
| 9597 | } else if (ctxt->states != NULL) { |
| 9598 | for (j = 0; j < ctxt->states->nbState; |
| 9599 | j++) { |
| 9600 | tmp = |
| 9601 | xmlRelaxNGAddStates(ctxt, res, |
| 9602 | ctxt-> |
| 9603 | states-> |
| 9604 | tabState |
| 9605 | [j]); |
| 9606 | if (tmp == 1) |
| 9607 | progress = 1; |
| 9608 | } |
| 9609 | xmlRelaxNGFreeStates(ctxt, |
| 9610 | ctxt->states); |
| 9611 | ctxt->states = NULL; |
| 9612 | } |
| 9613 | } else { |
| 9614 | if (ctxt->state != NULL) { |
| 9615 | xmlRelaxNGFreeValidState(ctxt, |
| 9616 | ctxt->state); |
| 9617 | ctxt->state = NULL; |
| 9618 | } |
| 9619 | } |
| 9620 | } |
| 9621 | } else { |
| 9622 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9623 | define-> |
| 9624 | content); |
| 9625 | if (ret != 0) { |
| 9626 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9627 | ctxt->state = NULL; |
| 9628 | } else { |
| 9629 | base = res->nbState; |
| 9630 | if (ctxt->state != NULL) { |
| 9631 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9632 | ctxt->state); |
| 9633 | ctxt->state = NULL; |
| 9634 | if (tmp == 1) |
| 9635 | progress = 1; |
| 9636 | } else if (ctxt->states != NULL) { |
| 9637 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9638 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9639 | ctxt-> |
| 9640 | states-> |
| 9641 | tabState[j]); |
| 9642 | if (tmp == 1) |
| 9643 | progress = 1; |
| 9644 | } |
| 9645 | if (states == NULL) { |
| 9646 | states = ctxt->states; |
| 9647 | } else { |
| 9648 | xmlRelaxNGFreeStates(ctxt, |
| 9649 | ctxt->states); |
| 9650 | } |
| 9651 | ctxt->states = NULL; |
| 9652 | } |
| 9653 | } |
| 9654 | } |
| 9655 | if (progress) { |
| 9656 | /* |
| 9657 | * Collect all the new nodes added at that step |
| 9658 | * and make them the new node set |
| 9659 | */ |
| 9660 | if (res->nbState - base == 1) { |
| 9661 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 9662 | res-> |
| 9663 | tabState |
| 9664 | [base]); |
| 9665 | } else { |
| 9666 | if (states == NULL) { |
| 9667 | xmlRelaxNGNewStates(ctxt, |
| 9668 | res->nbState - base); |
| 9669 | } |
| 9670 | states->nbState = 0; |
| 9671 | for (i = base; i < res->nbState; i++) |
| 9672 | xmlRelaxNGAddStates(ctxt, states, |
| 9673 | xmlRelaxNGCopyValidState |
| 9674 | (ctxt, |
| 9675 | res->tabState[i])); |
| 9676 | ctxt->states = states; |
| 9677 | } |
| 9678 | } |
| 9679 | } while (progress == 1); |
| 9680 | if (states != NULL) { |
| 9681 | xmlRelaxNGFreeStates(ctxt, states); |
| 9682 | } |
| 9683 | ctxt->states = res; |
| 9684 | ctxt->flags = oldflags; |
| 9685 | if (ctxt->errNr > errNr) |
| 9686 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9687 | ret = 0; |
| 9688 | break; |
| 9689 | } |
| 9690 | case XML_RELAXNG_CHOICE:{ |
| 9691 | xmlRelaxNGDefinePtr list = NULL; |
| 9692 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9693 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9694 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9695 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9696 | errNr = ctxt->errNr; |
| 9697 | if ((define->dflags & IS_TRIABLE) |
| 9698 | && (define->data != NULL)) { |
| 9699 | xmlHashTablePtr triage = |
| 9700 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9701 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9702 | /* |
| 9703 | * Something we can optimize cleanly there is only one |
| 9704 | * possble branch out ! |
| 9705 | */ |
| 9706 | if (node == NULL) { |
| 9707 | ret = -1; |
| 9708 | break; |
| 9709 | } |
| 9710 | if ((node->type == XML_TEXT_NODE) || |
| 9711 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 9712 | list = |
| 9713 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 9714 | } else if (node->type == XML_ELEMENT_NODE) { |
| 9715 | if (node->ns != NULL) { |
| 9716 | list = xmlHashLookup2(triage, node->name, |
| 9717 | node->ns->href); |
| 9718 | if (list == NULL) |
| 9719 | list = |
| 9720 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9721 | node->ns->href); |
| 9722 | } else |
| 9723 | list = |
| 9724 | xmlHashLookup2(triage, node->name, NULL); |
| 9725 | if (list == NULL) |
| 9726 | list = |
| 9727 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9728 | NULL); |
| 9729 | } |
| 9730 | if (list == NULL) { |
| 9731 | ret = -1; |
| 9732 | break; |
| 9733 | } |
| 9734 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 9735 | if (ret == 0) { |
| 9736 | } |
| 9737 | break; |
| 9738 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9739 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9740 | list = define->content; |
| 9741 | oldflags = ctxt->flags; |
| 9742 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9743 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9744 | while (list != NULL) { |
| 9745 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9746 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 9747 | if (ret == 0) { |
| 9748 | if (states == NULL) { |
| 9749 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 9750 | } |
| 9751 | if (ctxt->state != NULL) { |
| 9752 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 9753 | } else if (ctxt->states != NULL) { |
| 9754 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9755 | xmlRelaxNGAddStates(ctxt, states, |
| 9756 | ctxt->states-> |
| 9757 | tabState[i]); |
| 9758 | } |
| 9759 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9760 | ctxt->states = NULL; |
| 9761 | } |
| 9762 | } else { |
| 9763 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9764 | } |
| 9765 | ctxt->state = oldstate; |
| 9766 | list = list->next; |
| 9767 | } |
| 9768 | if (states != NULL) { |
| 9769 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9770 | ctxt->states = states; |
| 9771 | ctxt->state = NULL; |
| 9772 | ret = 0; |
| 9773 | } else { |
| 9774 | ctxt->states = NULL; |
| 9775 | } |
| 9776 | ctxt->flags = oldflags; |
| 9777 | if (ret != 0) { |
| 9778 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9779 | xmlRelaxNGDumpValidError(ctxt); |
| 9780 | } |
| 9781 | } else { |
| 9782 | if (ctxt->errNr > errNr) |
| 9783 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9784 | } |
| 9785 | break; |
| 9786 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9787 | case XML_RELAXNG_DEF: |
| 9788 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9789 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9790 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9791 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9792 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 9793 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9794 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9795 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 9796 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 9797 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9798 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9799 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9800 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9801 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9802 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 9803 | break; |
| 9804 | case XML_RELAXNG_DATATYPE:{ |
| 9805 | xmlNodePtr child; |
| 9806 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9807 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9808 | child = node; |
| 9809 | while (child != NULL) { |
| 9810 | if (child->type == XML_ELEMENT_NODE) { |
| 9811 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 9812 | node->parent->name); |
| 9813 | ret = -1; |
| 9814 | break; |
| 9815 | } else if ((child->type == XML_TEXT_NODE) || |
| 9816 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 9817 | content = xmlStrcat(content, child->content); |
| 9818 | } |
| 9819 | /* TODO: handle entities ... */ |
| 9820 | child = child->next; |
| 9821 | } |
| 9822 | if (ret == -1) { |
| 9823 | if (content != NULL) |
| 9824 | xmlFree(content); |
| 9825 | break; |
| 9826 | } |
| 9827 | if (content == NULL) { |
| 9828 | content = xmlStrdup(BAD_CAST ""); |
| 9829 | if (content == NULL) { |
| 9830 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 9831 | ret = -1; |
| 9832 | break; |
| 9833 | } |
| 9834 | } |
| 9835 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 9836 | ctxt->state->seq); |
| 9837 | if (ret == -1) { |
| 9838 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 9839 | } else if (ret == 0) { |
| 9840 | ctxt->state->seq = NULL; |
| 9841 | } |
| 9842 | if (content != NULL) |
| 9843 | xmlFree(content); |
| 9844 | break; |
| 9845 | } |
| 9846 | case XML_RELAXNG_VALUE:{ |
| 9847 | xmlChar *content = NULL; |
| 9848 | xmlChar *oldvalue; |
| 9849 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9850 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9851 | child = node; |
| 9852 | while (child != NULL) { |
| 9853 | if (child->type == XML_ELEMENT_NODE) { |
| 9854 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 9855 | node->parent->name); |
| 9856 | ret = -1; |
| 9857 | break; |
| 9858 | } else if ((child->type == XML_TEXT_NODE) || |
| 9859 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 9860 | content = xmlStrcat(content, child->content); |
| 9861 | } |
| 9862 | /* TODO: handle entities ... */ |
| 9863 | child = child->next; |
| 9864 | } |
| 9865 | if (ret == -1) { |
| 9866 | if (content != NULL) |
| 9867 | xmlFree(content); |
| 9868 | break; |
| 9869 | } |
| 9870 | if (content == NULL) { |
| 9871 | content = xmlStrdup(BAD_CAST ""); |
| 9872 | if (content == NULL) { |
| 9873 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 9874 | ret = -1; |
| 9875 | break; |
| 9876 | } |
| 9877 | } |
| 9878 | oldvalue = ctxt->state->value; |
| 9879 | ctxt->state->value = content; |
| 9880 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 9881 | ctxt->state->value = oldvalue; |
| 9882 | if (ret == -1) { |
| 9883 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 9884 | } else if (ret == 0) { |
| 9885 | ctxt->state->seq = NULL; |
| 9886 | } |
| 9887 | if (content != NULL) |
| 9888 | xmlFree(content); |
| 9889 | break; |
| 9890 | } |
| 9891 | case XML_RELAXNG_LIST:{ |
| 9892 | xmlChar *content; |
| 9893 | xmlNodePtr child; |
| 9894 | xmlChar *oldvalue, *oldendvalue; |
| 9895 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9896 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9897 | /* |
| 9898 | * Make sure it's only text nodes |
| 9899 | */ |
| 9900 | |
| 9901 | content = NULL; |
| 9902 | child = node; |
| 9903 | while (child != NULL) { |
| 9904 | if (child->type == XML_ELEMENT_NODE) { |
| 9905 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 9906 | node->parent->name); |
| 9907 | ret = -1; |
| 9908 | break; |
| 9909 | } else if ((child->type == XML_TEXT_NODE) || |
| 9910 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 9911 | content = xmlStrcat(content, child->content); |
| 9912 | } |
| 9913 | /* TODO: handle entities ... */ |
| 9914 | child = child->next; |
| 9915 | } |
| 9916 | if (ret == -1) { |
| 9917 | if (content != NULL) |
| 9918 | xmlFree(content); |
| 9919 | break; |
| 9920 | } |
| 9921 | if (content == NULL) { |
| 9922 | content = xmlStrdup(BAD_CAST ""); |
| 9923 | if (content == NULL) { |
| 9924 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 9925 | ret = -1; |
| 9926 | break; |
| 9927 | } |
| 9928 | } |
| 9929 | len = xmlStrlen(content); |
| 9930 | oldvalue = ctxt->state->value; |
| 9931 | oldendvalue = ctxt->state->endvalue; |
| 9932 | ctxt->state->value = content; |
| 9933 | ctxt->state->endvalue = content + len; |
| 9934 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 9935 | ctxt->state->value = oldvalue; |
| 9936 | ctxt->state->endvalue = oldendvalue; |
| 9937 | if (ret == -1) { |
| 9938 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 9939 | } else if ((ret == 0) && (node != NULL)) { |
| 9940 | ctxt->state->seq = node->next; |
| 9941 | } |
| 9942 | if (content != NULL) |
| 9943 | xmlFree(content); |
| 9944 | break; |
| 9945 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9946 | case XML_RELAXNG_EXCEPT: |
| 9947 | case XML_RELAXNG_PARAM: |
| 9948 | TODO ret = -1; |
| 9949 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9950 | } |
| 9951 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9952 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9953 | for (i = 0; i < ctxt->depth; i++) |
| 9954 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9955 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9956 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9957 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9958 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9959 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9960 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9961 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9962 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9963 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9964 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9965 | } |
| 9966 | |
| 9967 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9968 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9969 | * @ctxt: a Relax-NG validation context |
| 9970 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9971 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9972 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9973 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9974 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9975 | */ |
| 9976 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9977 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 9978 | xmlRelaxNGDefinePtr define) { |
| 9979 | xmlRelaxNGStatesPtr states, res; |
| 9980 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9981 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9982 | /* |
| 9983 | * We should NOT have both ctxt->state and ctxt->states |
| 9984 | */ |
| 9985 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 9986 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9987 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9988 | ctxt->state = NULL; |
| 9989 | } |
| 9990 | |
| 9991 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
| 9992 | if (ctxt->states != NULL) { |
| 9993 | ctxt->state = ctxt->states->tabState[0]; |
| 9994 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9995 | ctxt->states = NULL; |
| 9996 | } |
| 9997 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 9998 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 9999 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10000 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10001 | ctxt->state = NULL; |
| 10002 | } |
| 10003 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10004 | ctxt->state = ctxt->states->tabState[0]; |
| 10005 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10006 | ctxt->states = NULL; |
| 10007 | } |
| 10008 | return(ret); |
| 10009 | } |
| 10010 | |
| 10011 | states = ctxt->states; |
| 10012 | ctxt->states = NULL; |
| 10013 | res = NULL; |
| 10014 | j = 0; |
| 10015 | oldflags = ctxt->flags; |
| 10016 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10017 | for (i = 0;i < states->nbState;i++) { |
| 10018 | ctxt->state = states->tabState[i]; |
| 10019 | ctxt->states = NULL; |
| 10020 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10021 | /* |
| 10022 | * We should NOT have both ctxt->state and ctxt->states |
| 10023 | */ |
| 10024 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10025 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10026 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10027 | ctxt->state = NULL; |
| 10028 | } |
| 10029 | if (ret == 0) { |
| 10030 | if (ctxt->states == NULL) { |
| 10031 | if (res != NULL) { |
| 10032 | /* add the state to the container */ |
| 10033 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10034 | ctxt->state = NULL; |
| 10035 | } else { |
| 10036 | /* add the state directly in states */ |
| 10037 | states->tabState[j++] = ctxt->state; |
| 10038 | ctxt->state = NULL; |
| 10039 | } |
| 10040 | } else { |
| 10041 | if (res == NULL) { |
| 10042 | /* make it the new container and copy other results */ |
| 10043 | res = ctxt->states; |
| 10044 | ctxt->states = NULL; |
| 10045 | for (k = 0;k < j;k++) |
| 10046 | xmlRelaxNGAddStates(ctxt, res, states->tabState[k]); |
| 10047 | } else { |
| 10048 | /* add all the new results to res and reff the container */ |
| 10049 | for (k = 0;k < ctxt->states->nbState;k++) |
| 10050 | xmlRelaxNGAddStates(ctxt, res, |
| 10051 | ctxt->states->tabState[k]); |
| 10052 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10053 | ctxt->states = NULL; |
| 10054 | } |
| 10055 | } |
| 10056 | } else { |
| 10057 | if (ctxt->state != NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10058 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10059 | ctxt->state = NULL; |
| 10060 | } else if (ctxt->states != NULL) { |
| 10061 | for (k = 0;k < ctxt->states->nbState;k++) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10062 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[k]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10063 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10064 | ctxt->states = NULL; |
| 10065 | } |
| 10066 | } |
| 10067 | } |
| 10068 | ctxt->flags = oldflags; |
| 10069 | if (res != NULL) { |
| 10070 | xmlRelaxNGFreeStates(ctxt, states); |
| 10071 | ctxt->states = res; |
| 10072 | ret = 0; |
| 10073 | } else if (j > 1) { |
| 10074 | states->nbState = j; |
| 10075 | ctxt->states = states; |
| 10076 | ret =0; |
| 10077 | } else if (j == 1) { |
| 10078 | ctxt->state = states->tabState[0]; |
| 10079 | xmlRelaxNGFreeStates(ctxt, states); |
| 10080 | ret = 0; |
| 10081 | } else { |
| 10082 | ret = -1; |
| 10083 | xmlRelaxNGFreeStates(ctxt, states); |
| 10084 | if (ctxt->states != NULL) { |
| 10085 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10086 | ctxt->states = NULL; |
| 10087 | } |
| 10088 | } |
| 10089 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10090 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10091 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10092 | ctxt->state = NULL; |
| 10093 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10094 | return(ret); |
| 10095 | } |
| 10096 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10097 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10098 | * xmlRelaxNGValidateDocument: |
| 10099 | * @ctxt: a Relax-NG validation context |
| 10100 | * @doc: the document |
| 10101 | * |
| 10102 | * Validate the given document |
| 10103 | * |
| 10104 | * Returns 0 if the validation succeeded or an error code. |
| 10105 | */ |
| 10106 | static int |
| 10107 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 10108 | int ret; |
| 10109 | xmlRelaxNGPtr schema; |
| 10110 | xmlRelaxNGGrammarPtr grammar; |
| 10111 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10112 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10113 | |
| 10114 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
| 10115 | return(-1); |
| 10116 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10117 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10118 | schema = ctxt->schema; |
| 10119 | grammar = schema->topgrammar; |
| 10120 | if (grammar == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10121 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10122 | return(-1); |
| 10123 | } |
| 10124 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10125 | ctxt->state = state; |
| 10126 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10127 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
| 10128 | state = ctxt->state; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10129 | node = state->seq; |
| 10130 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10131 | if (node != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10132 | if (ret != -1) { |
| 10133 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10134 | ret = -1; |
| 10135 | } |
| 10136 | } |
| 10137 | } else if (ctxt->states != NULL) { |
| 10138 | int i; |
| 10139 | int tmp = -1; |
| 10140 | |
| 10141 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 10142 | state = ctxt->states->tabState[i]; |
| 10143 | node = state->seq; |
| 10144 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10145 | if (node == NULL) |
| 10146 | tmp = 0; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10147 | xmlRelaxNGFreeValidState(ctxt,state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10148 | } |
| 10149 | if (tmp == -1) { |
| 10150 | if (ret != -1) { |
| 10151 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10152 | ret = -1; |
| 10153 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10154 | } |
| 10155 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10156 | if (ctxt->state != NULL) { |
| 10157 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10158 | ctxt->state = NULL; |
| 10159 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10160 | if (ret != 0) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10161 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10162 | #ifdef DEBUG |
| 10163 | else if (ctxt->errNr != 0) { |
| 10164 | ctxt->error(ctxt->userData, "%d Extra error messages left on stack !\n", |
| 10165 | ctxt->errNr); |
| 10166 | xmlRelaxNGDumpValidError(ctxt); |
| 10167 | } |
| 10168 | #endif |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10169 | if (ctxt->idref == 1) { |
| 10170 | xmlValidCtxt vctxt; |
| 10171 | |
| 10172 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10173 | vctxt.valid = 1; |
| 10174 | vctxt.error = ctxt->error; |
| 10175 | vctxt.warning = ctxt->warning; |
| 10176 | vctxt.userData = ctxt->userData; |
| 10177 | |
| 10178 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10179 | ret = -1; |
| 10180 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10181 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
| 10182 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10183 | |
| 10184 | return(ret); |
| 10185 | } |
| 10186 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10187 | /************************************************************************ |
| 10188 | * * |
| 10189 | * Validation interfaces * |
| 10190 | * * |
| 10191 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10192 | /** |
| 10193 | * xmlRelaxNGNewValidCtxt: |
| 10194 | * @schema: a precompiled XML RelaxNGs |
| 10195 | * |
| 10196 | * Create an XML RelaxNGs validation context based on the given schema |
| 10197 | * |
| 10198 | * Returns the validation context or NULL in case of error |
| 10199 | */ |
| 10200 | xmlRelaxNGValidCtxtPtr |
| 10201 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) { |
| 10202 | xmlRelaxNGValidCtxtPtr ret; |
| 10203 | |
| 10204 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10205 | if (ret == NULL) { |
| 10206 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10207 | "Failed to allocate new schema validation context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10208 | return (NULL); |
| 10209 | } |
| 10210 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10211 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10212 | ret->error = xmlGenericError; |
| 10213 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10214 | ret->errNr = 0; |
| 10215 | ret->errMax = 0; |
| 10216 | ret->err = NULL; |
| 10217 | ret->errTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10218 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10219 | ret->states = NULL; |
| 10220 | ret->freeState = NULL; |
| 10221 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10222 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10223 | return (ret); |
| 10224 | } |
| 10225 | |
| 10226 | /** |
| 10227 | * xmlRelaxNGFreeValidCtxt: |
| 10228 | * @ctxt: the schema validation context |
| 10229 | * |
| 10230 | * Free the resources associated to the schema validation context |
| 10231 | */ |
| 10232 | void |
| 10233 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10234 | int k; |
| 10235 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10236 | if (ctxt == NULL) |
| 10237 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10238 | if (ctxt->states != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10239 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
| 10240 | if (ctxt->freeState != NULL) { |
| 10241 | for (k = 0;k < ctxt->freeState->nbState;k++) { |
| 10242 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10243 | } |
| 10244 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
| 10245 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10246 | if (ctxt->freeStates != NULL) { |
| 10247 | for (k = 0;k < ctxt->freeStatesNr;k++) { |
| 10248 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10249 | } |
| 10250 | xmlFree(ctxt->freeStates); |
| 10251 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10252 | if (ctxt->errTab != NULL) |
| 10253 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10254 | if (ctxt->elemTab != NULL) { |
| 10255 | xmlRegExecCtxtPtr exec; |
| 10256 | |
| 10257 | exec = xmlRelaxNGElemPop(ctxt); |
| 10258 | while (exec != NULL) { |
| 10259 | xmlRegFreeExecCtxt(exec); |
| 10260 | exec = xmlRelaxNGElemPop(ctxt); |
| 10261 | } |
| 10262 | xmlFree(ctxt->elemTab); |
| 10263 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10264 | xmlFree(ctxt); |
| 10265 | } |
| 10266 | |
| 10267 | /** |
| 10268 | * xmlRelaxNGSetValidErrors: |
| 10269 | * @ctxt: a Relax-NG validation context |
| 10270 | * @err: the error function |
| 10271 | * @warn: the warning function |
| 10272 | * @ctx: the functions context |
| 10273 | * |
| 10274 | * Set the error and warning callback informations |
| 10275 | */ |
| 10276 | void |
| 10277 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
| 10278 | xmlRelaxNGValidityErrorFunc err, |
| 10279 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 10280 | if (ctxt == NULL) |
| 10281 | return; |
| 10282 | ctxt->error = err; |
| 10283 | ctxt->warning = warn; |
| 10284 | ctxt->userData = ctx; |
| 10285 | } |
| 10286 | |
| 10287 | /** |
| 10288 | * xmlRelaxNGValidateDoc: |
| 10289 | * @ctxt: a Relax-NG validation context |
| 10290 | * @doc: a parsed document tree |
| 10291 | * |
| 10292 | * Validate a document tree in memory. |
| 10293 | * |
| 10294 | * Returns 0 if the document is valid, a positive error code |
| 10295 | * number otherwise and -1 in case of internal or API error. |
| 10296 | */ |
| 10297 | int |
| 10298 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 10299 | int ret; |
| 10300 | |
| 10301 | if ((ctxt == NULL) || (doc == NULL)) |
| 10302 | return(-1); |
| 10303 | |
| 10304 | ctxt->doc = doc; |
| 10305 | |
| 10306 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10307 | /* |
| 10308 | * TODO: build error codes |
| 10309 | */ |
| 10310 | if (ret == -1) |
| 10311 | return(1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10312 | return(ret); |
| 10313 | } |
| 10314 | |
| 10315 | #endif /* LIBXML_SCHEMAS_ENABLED */ |
| 10316 | |