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 | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 59 | |
| 60 | #define UNBOUNDED (1 << 30) |
| 61 | #define TODO \ |
| 62 | xmlGenericError(xmlGenericErrorContext, \ |
| 63 | "Unimplemented block at %s:%d\n", \ |
| 64 | __FILE__, __LINE__); |
| 65 | |
| 66 | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
| 67 | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
| 68 | |
| 69 | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
| 70 | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
| 71 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 72 | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
| 73 | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
| 74 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 75 | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
| 76 | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
| 77 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 78 | typedef enum { |
| 79 | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
| 80 | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
| 81 | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
| 82 | } xmlRelaxNGCombine; |
| 83 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 84 | typedef enum { |
| 85 | XML_RELAXNG_CONTENT_ERROR = -1, |
| 86 | XML_RELAXNG_CONTENT_EMPTY = 0, |
| 87 | XML_RELAXNG_CONTENT_SIMPLE, |
| 88 | XML_RELAXNG_CONTENT_COMPLEX |
| 89 | } xmlRelaxNGContentType; |
| 90 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 91 | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
| 92 | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
| 93 | |
| 94 | struct _xmlRelaxNGGrammar { |
| 95 | xmlRelaxNGGrammarPtr parent;/* the parent grammar if any */ |
| 96 | xmlRelaxNGGrammarPtr children;/* the children grammar if any */ |
| 97 | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
| 98 | xmlRelaxNGDefinePtr start; /* <start> content */ |
| 99 | xmlRelaxNGCombine combine; /* the default combine value */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 100 | xmlRelaxNGDefinePtr startList;/* list of <start> definitions */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 101 | xmlHashTablePtr defs; /* define* */ |
| 102 | xmlHashTablePtr refs; /* references */ |
| 103 | }; |
| 104 | |
| 105 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 106 | typedef enum { |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 107 | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 108 | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
| 109 | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 110 | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 111 | XML_RELAXNG_TEXT, /* textual content */ |
| 112 | XML_RELAXNG_ELEMENT, /* an element */ |
| 113 | XML_RELAXNG_DATATYPE, /* extenal data type definition */ |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 114 | XML_RELAXNG_PARAM, /* extenal data type parameter */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 115 | XML_RELAXNG_VALUE, /* value from an extenal data type definition */ |
| 116 | XML_RELAXNG_LIST, /* a list of patterns */ |
| 117 | XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */ |
| 118 | XML_RELAXNG_DEF, /* a definition */ |
| 119 | XML_RELAXNG_REF, /* reference to a definition */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 120 | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 121 | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 122 | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
| 123 | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 124 | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
| 125 | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
| 126 | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 127 | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 128 | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 129 | } xmlRelaxNGType; |
| 130 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 131 | #define IS_NULLABLE 1 |
| 132 | #define IS_NOT_NULLABLE 2 |
| 133 | #define IS_INDETERMINIST 4 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 134 | #define IS_MIXED 8 |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 135 | #define IS_TRIABLE 16 |
| 136 | #define IS_PROCESSED 32 |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 137 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 138 | struct _xmlRelaxNGDefine { |
| 139 | xmlRelaxNGType type; /* the type of definition */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 140 | xmlNodePtr node; /* the node in the source */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 141 | xmlChar *name; /* the element local name if present */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 142 | xmlChar *ns; /* the namespace local name if present */ |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 143 | xmlChar *value; /* value when available */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 144 | void *data; /* data lib or specific pointer */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 145 | xmlRelaxNGDefinePtr content;/* the expected content */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 146 | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 147 | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 148 | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 149 | xmlRelaxNGDefinePtr nameClass;/* the nameClass definition if any */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 150 | xmlRelaxNGDefinePtr nextHash;/* next define in defs/refs hash tables */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 151 | short depth; /* used for the cycle detection */ |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 152 | short dflags; /* define related flags */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | /** |
| 156 | * _xmlRelaxNG: |
| 157 | * |
| 158 | * A RelaxNGs definition |
| 159 | */ |
| 160 | struct _xmlRelaxNG { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 161 | void *_private; /* unused by the library for users or bindings */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 162 | xmlRelaxNGGrammarPtr topgrammar; |
| 163 | xmlDocPtr doc; |
| 164 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 165 | int idref; /* requires idref checking */ |
| 166 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 167 | xmlHashTablePtr defs; /* define */ |
| 168 | xmlHashTablePtr refs; /* references */ |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 169 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 170 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 171 | int defNr; /* number of defines used */ |
| 172 | xmlRelaxNGDefinePtr *defTab;/* pointer to the allocated definitions */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 173 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 176 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
| 177 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
| 178 | #define XML_RELAXNG_IN_LIST (1 << 2) |
| 179 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
| 180 | #define XML_RELAXNG_IN_START (1 << 4) |
| 181 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
| 182 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
| 183 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 184 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
| 185 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 186 | |
| 187 | struct _xmlRelaxNGParserCtxt { |
| 188 | void *userData; /* user specific data block */ |
| 189 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 190 | xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 191 | xmlRelaxNGValidErr err; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 192 | |
| 193 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 194 | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 195 | xmlRelaxNGGrammarPtr parentgrammar;/* the parent grammar */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 196 | int flags; /* parser flags */ |
| 197 | int nbErrors; /* number of errors at parse time */ |
| 198 | int nbWarnings; /* number of warnings at parse time */ |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 199 | const xmlChar *define; /* the current define scope */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 200 | xmlRelaxNGDefinePtr def; /* the current define */ |
| 201 | |
| 202 | int nbInterleaves; |
| 203 | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 204 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 205 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 206 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 207 | xmlChar *URL; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 208 | xmlDocPtr document; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 209 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 210 | int defNr; /* number of defines used */ |
| 211 | int defMax; /* number of defines aloocated */ |
| 212 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
| 213 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 214 | const char *buffer; |
| 215 | int size; |
| 216 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 217 | /* the document stack */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 218 | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 219 | int docNr; /* Depth of the parsing stack */ |
| 220 | int docMax; /* Max depth of the parsing stack */ |
| 221 | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 222 | |
| 223 | /* the include stack */ |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 224 | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 225 | int incNr; /* Depth of the include parsing stack */ |
| 226 | int incMax; /* Max depth of the parsing stack */ |
| 227 | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 228 | |
| 229 | int idref; /* requires idref checking */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | #define FLAGS_IGNORABLE 1 |
| 233 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 234 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 235 | |
| 236 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 237 | * xmlRelaxNGInterleaveGroup: |
| 238 | * |
| 239 | * A RelaxNGs partition set associated to lists of definitions |
| 240 | */ |
| 241 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 242 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 243 | struct _xmlRelaxNGInterleaveGroup { |
| 244 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 245 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 246 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 247 | }; |
| 248 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 249 | #define IS_DETERMINIST 1 |
| 250 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 251 | /** |
| 252 | * xmlRelaxNGPartitions: |
| 253 | * |
| 254 | * A RelaxNGs partition associated to an interleave group |
| 255 | */ |
| 256 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 257 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 258 | struct _xmlRelaxNGPartition { |
| 259 | int nbgroups; /* number of groups in the partitions */ |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 260 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 261 | right group when possible */ |
| 262 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 263 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 264 | }; |
| 265 | |
| 266 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 267 | * xmlRelaxNGValidState: |
| 268 | * |
| 269 | * A RelaxNGs validation state |
| 270 | */ |
| 271 | #define MAX_ATTR 20 |
| 272 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 273 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 274 | struct _xmlRelaxNGValidState { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 275 | xmlNodePtr node; /* the current node */ |
| 276 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 277 | int nbAttrs; /* the number of attributes */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 278 | int maxAttrs; /* the size of attrs */ |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 279 | int nbAttrLeft; /* the number of attributes left to validate */ |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 280 | xmlChar *value; /* the value when operating on string */ |
| 281 | xmlChar *endvalue; /* the end value when operating on string */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 282 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 286 | * xmlRelaxNGStates: |
| 287 | * |
| 288 | * A RelaxNGs container for validation state |
| 289 | */ |
| 290 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 291 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 292 | struct _xmlRelaxNGStates { |
| 293 | int nbState; /* the number of states */ |
| 294 | int maxState; /* the size of the array */ |
| 295 | xmlRelaxNGValidStatePtr *tabState; |
| 296 | }; |
| 297 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 298 | #define ERROR_IS_DUP 1 |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 299 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 300 | * xmlRelaxNGValidError: |
| 301 | * |
| 302 | * A RelaxNGs validation error |
| 303 | */ |
| 304 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 305 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 306 | struct _xmlRelaxNGValidError { |
| 307 | xmlRelaxNGValidErr err; /* the error number */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 308 | int flags; /* flags */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 309 | xmlNodePtr node; /* the current node */ |
| 310 | xmlNodePtr seq; /* the current child */ |
| 311 | const xmlChar * arg1; /* first arg */ |
| 312 | const xmlChar * arg2; /* second arg */ |
| 313 | }; |
| 314 | |
| 315 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 316 | * xmlRelaxNGValidCtxt: |
| 317 | * |
| 318 | * A RelaxNGs validation context |
| 319 | */ |
| 320 | |
| 321 | struct _xmlRelaxNGValidCtxt { |
| 322 | void *userData; /* user specific data block */ |
| 323 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 324 | xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */ |
| 325 | |
| 326 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 327 | xmlDocPtr doc; /* the document being validated */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 328 | int flags; /* validation flags */ |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 329 | int depth; /* validation depth */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 330 | int idref; /* requires idref checking */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 331 | int errNo; /* the first error found */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 332 | |
| 333 | /* |
| 334 | * Errors accumulated in branches may have to be stacked to be |
| 335 | * provided back when it's sure they affect validation. |
| 336 | */ |
| 337 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
| 338 | int errNr; /* Depth of the error stack */ |
| 339 | int errMax; /* Max depth of the error stack */ |
| 340 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 341 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 342 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 343 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 344 | |
| 345 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 346 | int freeStatesNr; |
| 347 | int freeStatesMax; |
| 348 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 349 | }; |
| 350 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 351 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 352 | * xmlRelaxNGInclude: |
| 353 | * |
| 354 | * Structure associated to a RelaxNGs document element |
| 355 | */ |
| 356 | struct _xmlRelaxNGInclude { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 357 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 358 | xmlChar *href; /* the normalized href value */ |
| 359 | xmlDocPtr doc; /* the associated XML document */ |
| 360 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 361 | xmlRelaxNGPtr schema; /* the schema */ |
| 362 | }; |
| 363 | |
| 364 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 365 | * xmlRelaxNGDocument: |
| 366 | * |
| 367 | * Structure associated to a RelaxNGs document element |
| 368 | */ |
| 369 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 370 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 371 | xmlChar *href; /* the normalized href value */ |
| 372 | xmlDocPtr doc; /* the associated XML document */ |
| 373 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 374 | xmlRelaxNGPtr schema; /* the schema */ |
| 375 | }; |
| 376 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 377 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 378 | /************************************************************************ |
| 379 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 380 | * Preliminary type checking interfaces * |
| 381 | * * |
| 382 | ************************************************************************/ |
| 383 | /** |
| 384 | * xmlRelaxNGTypeHave: |
| 385 | * @data: data needed for the library |
| 386 | * @type: the type name |
| 387 | * @value: the value to check |
| 388 | * |
| 389 | * Function provided by a type library to check if a type is exported |
| 390 | * |
| 391 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 392 | */ |
| 393 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar *type); |
| 394 | |
| 395 | /** |
| 396 | * xmlRelaxNGTypeCheck: |
| 397 | * @data: data needed for the library |
| 398 | * @type: the type name |
| 399 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 400 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 401 | * |
| 402 | * Function provided by a type library to check if a value match a type |
| 403 | * |
| 404 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 405 | */ |
| 406 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar *type, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 407 | const xmlChar *value, void **result, |
| 408 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 409 | |
| 410 | /** |
| 411 | * xmlRelaxNGFacetCheck: |
| 412 | * @data: data needed for the library |
| 413 | * @type: the type name |
| 414 | * @facet: the facet name |
| 415 | * @val: the facet value |
| 416 | * @strval: the string value |
| 417 | * @value: the value to check |
| 418 | * |
| 419 | * Function provided by a type library to check a value facet |
| 420 | * |
| 421 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 422 | */ |
| 423 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar *type, |
| 424 | const xmlChar *facet, const xmlChar *val, |
| 425 | const xmlChar *strval, void *value); |
| 426 | |
| 427 | /** |
| 428 | * xmlRelaxNGTypeFree: |
| 429 | * @data: data needed for the library |
| 430 | * @result: the value to free |
| 431 | * |
| 432 | * Function provided by a type library to free a returned result |
| 433 | */ |
| 434 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 435 | |
| 436 | /** |
| 437 | * xmlRelaxNGTypeCompare: |
| 438 | * @data: data needed for the library |
| 439 | * @type: the type name |
| 440 | * @value1: the first value |
| 441 | * @value2: the second value |
| 442 | * |
| 443 | * Function provided by a type library to compare two values accordingly |
| 444 | * to a type. |
| 445 | * |
| 446 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 447 | */ |
| 448 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar *type, |
| 449 | const xmlChar *value1, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 450 | xmlNodePtr ctxt1, |
| 451 | void *comp1, |
| 452 | const xmlChar *value2, |
| 453 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 454 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 455 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 456 | struct _xmlRelaxNGTypeLibrary { |
| 457 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 458 | void *data; /* data needed for the library */ |
| 459 | xmlRelaxNGTypeHave have; /* the export function */ |
| 460 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 461 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 462 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 463 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 464 | }; |
| 465 | |
| 466 | /************************************************************************ |
| 467 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 468 | * Allocation functions * |
| 469 | * * |
| 470 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 471 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 472 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 473 | static void xmlRelaxNGNormExtSpace(xmlChar *value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 474 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 475 | static int xmlRelaxNGEqualValidState( |
| 476 | xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 477 | xmlRelaxNGValidStatePtr state1, |
| 478 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 479 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 480 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 481 | |
| 482 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 483 | * xmlRelaxNGFreeDocument: |
| 484 | * @docu: a document structure |
| 485 | * |
| 486 | * Deallocate a RelaxNG document structure. |
| 487 | */ |
| 488 | static void |
| 489 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 490 | { |
| 491 | if (docu == NULL) |
| 492 | return; |
| 493 | |
| 494 | if (docu->href != NULL) |
| 495 | xmlFree(docu->href); |
| 496 | if (docu->doc != NULL) |
| 497 | xmlFreeDoc(docu->doc); |
| 498 | if (docu->schema != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 499 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 500 | xmlFree(docu); |
| 501 | } |
| 502 | |
| 503 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 504 | * xmlRelaxNGFreeDocumentList: |
| 505 | * @docu: a list of document structure |
| 506 | * |
| 507 | * Deallocate a RelaxNG document structures. |
| 508 | */ |
| 509 | static void |
| 510 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 511 | { |
| 512 | xmlRelaxNGDocumentPtr next; |
| 513 | while (docu != NULL) { |
| 514 | next = docu->next; |
| 515 | xmlRelaxNGFreeDocument(docu); |
| 516 | docu = next; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 521 | * xmlRelaxNGFreeInclude: |
| 522 | * @incl: a include structure |
| 523 | * |
| 524 | * Deallocate a RelaxNG include structure. |
| 525 | */ |
| 526 | static void |
| 527 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 528 | { |
| 529 | if (incl == NULL) |
| 530 | return; |
| 531 | |
| 532 | if (incl->href != NULL) |
| 533 | xmlFree(incl->href); |
| 534 | if (incl->doc != NULL) |
| 535 | xmlFreeDoc(incl->doc); |
| 536 | if (incl->schema != NULL) |
| 537 | xmlRelaxNGFree(incl->schema); |
| 538 | xmlFree(incl); |
| 539 | } |
| 540 | |
| 541 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 542 | * xmlRelaxNGFreeIncludeList: |
| 543 | * @incl: a include structure list |
| 544 | * |
| 545 | * Deallocate a RelaxNG include structure. |
| 546 | */ |
| 547 | static void |
| 548 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 549 | { |
| 550 | xmlRelaxNGIncludePtr next; |
| 551 | while (incl != NULL) { |
| 552 | next = incl->next; |
| 553 | xmlRelaxNGFreeInclude(incl); |
| 554 | incl = next; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 559 | * xmlRelaxNGNewRelaxNG: |
| 560 | * @ctxt: a Relax-NG validation context (optional) |
| 561 | * |
| 562 | * Allocate a new RelaxNG structure. |
| 563 | * |
| 564 | * Returns the newly allocated structure or NULL in case or error |
| 565 | */ |
| 566 | static xmlRelaxNGPtr |
| 567 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 568 | { |
| 569 | xmlRelaxNGPtr ret; |
| 570 | |
| 571 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 572 | if (ret == NULL) { |
| 573 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 574 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 575 | ctxt->nbErrors++; |
| 576 | return (NULL); |
| 577 | } |
| 578 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 579 | |
| 580 | return (ret); |
| 581 | } |
| 582 | |
| 583 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 584 | * xmlRelaxNGFreeInnerSchema: |
| 585 | * @schema: a schema structure |
| 586 | * |
| 587 | * Deallocate a RelaxNG schema structure. |
| 588 | */ |
| 589 | static void |
| 590 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 591 | { |
| 592 | if (schema == NULL) |
| 593 | return; |
| 594 | |
| 595 | if (schema->doc != NULL) |
| 596 | xmlFreeDoc(schema->doc); |
| 597 | if (schema->defTab != NULL) { |
| 598 | int i; |
| 599 | |
| 600 | for (i = 0;i < schema->defNr;i++) |
| 601 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 602 | xmlFree(schema->defTab); |
| 603 | } |
| 604 | |
| 605 | xmlFree(schema); |
| 606 | } |
| 607 | |
| 608 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 609 | * xmlRelaxNGFree: |
| 610 | * @schema: a schema structure |
| 611 | * |
| 612 | * Deallocate a RelaxNG structure. |
| 613 | */ |
| 614 | void |
| 615 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 616 | { |
| 617 | if (schema == NULL) |
| 618 | return; |
| 619 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 620 | if (schema->topgrammar != NULL) |
| 621 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
| 622 | if (schema->doc != NULL) |
| 623 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 624 | if (schema->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 625 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 626 | if (schema->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 627 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 628 | if (schema->defTab != NULL) { |
| 629 | int i; |
| 630 | |
| 631 | for (i = 0;i < schema->defNr;i++) |
| 632 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 633 | xmlFree(schema->defTab); |
| 634 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 635 | |
| 636 | xmlFree(schema); |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * xmlRelaxNGNewGrammar: |
| 641 | * @ctxt: a Relax-NG validation context (optional) |
| 642 | * |
| 643 | * Allocate a new RelaxNG grammar. |
| 644 | * |
| 645 | * Returns the newly allocated structure or NULL in case or error |
| 646 | */ |
| 647 | static xmlRelaxNGGrammarPtr |
| 648 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 649 | { |
| 650 | xmlRelaxNGGrammarPtr ret; |
| 651 | |
| 652 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 653 | if (ret == NULL) { |
| 654 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 655 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 656 | ctxt->nbErrors++; |
| 657 | return (NULL); |
| 658 | } |
| 659 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 660 | |
| 661 | return (ret); |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * xmlRelaxNGFreeGrammar: |
| 666 | * @grammar: a grammar structure |
| 667 | * |
| 668 | * Deallocate a RelaxNG grammar structure. |
| 669 | */ |
| 670 | static void |
| 671 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 672 | { |
| 673 | if (grammar == NULL) |
| 674 | return; |
| 675 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 676 | if (grammar->children != NULL) { |
| 677 | xmlRelaxNGFreeGrammar(grammar->children); |
| 678 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 679 | if (grammar->next != NULL) { |
| 680 | xmlRelaxNGFreeGrammar(grammar->next); |
| 681 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 682 | if (grammar->refs != NULL) { |
| 683 | xmlHashFree(grammar->refs, NULL); |
| 684 | } |
| 685 | if (grammar->defs != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 686 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | xmlFree(grammar); |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * xmlRelaxNGNewDefine: |
| 694 | * @ctxt: a Relax-NG validation context |
| 695 | * @node: the node in the input document. |
| 696 | * |
| 697 | * Allocate a new RelaxNG define. |
| 698 | * |
| 699 | * Returns the newly allocated structure or NULL in case or error |
| 700 | */ |
| 701 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 702 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 703 | { |
| 704 | xmlRelaxNGDefinePtr ret; |
| 705 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 706 | if (ctxt->defMax == 0) { |
| 707 | ctxt->defMax = 16; |
| 708 | ctxt->defNr = 0; |
| 709 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 710 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 711 | if (ctxt->defTab == NULL) { |
| 712 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 713 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 714 | ctxt->nbErrors++; |
| 715 | return (NULL); |
| 716 | } |
| 717 | } else if (ctxt->defMax <= ctxt->defNr) { |
| 718 | xmlRelaxNGDefinePtr *tmp; |
| 719 | ctxt->defMax *= 2; |
| 720 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 721 | ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 722 | if (tmp == NULL) { |
| 723 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 724 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 725 | ctxt->nbErrors++; |
| 726 | return (NULL); |
| 727 | } |
| 728 | ctxt->defTab = tmp; |
| 729 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 730 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 731 | if (ret == NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 732 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 733 | ctxt->error(ctxt->userData, "Out of memory\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 734 | ctxt->nbErrors++; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 735 | return(NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 736 | } |
| 737 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 738 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 739 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 740 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 741 | return (ret); |
| 742 | } |
| 743 | |
| 744 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 745 | * xmlRelaxNGFreePartition: |
| 746 | * @partitions: a partition set structure |
| 747 | * |
| 748 | * Deallocate RelaxNG partition set structures. |
| 749 | */ |
| 750 | static void |
| 751 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) { |
| 752 | xmlRelaxNGInterleaveGroupPtr group; |
| 753 | int j; |
| 754 | |
| 755 | if (partitions != NULL) { |
| 756 | if (partitions->groups != NULL) { |
| 757 | for (j = 0;j < partitions->nbgroups;j++) { |
| 758 | group = partitions->groups[j]; |
| 759 | if (group != NULL) { |
| 760 | if (group->defs != NULL) |
| 761 | xmlFree(group->defs); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 762 | if (group->attrs != NULL) |
| 763 | xmlFree(group->attrs); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 764 | xmlFree(group); |
| 765 | } |
| 766 | } |
| 767 | xmlFree(partitions->groups); |
| 768 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 769 | if (partitions->triage != NULL) { |
| 770 | xmlHashFree(partitions->triage, NULL); |
| 771 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 772 | xmlFree(partitions); |
| 773 | } |
| 774 | } |
| 775 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 776 | * xmlRelaxNGFreeDefine: |
| 777 | * @define: a define structure |
| 778 | * |
| 779 | * Deallocate a RelaxNG define structure. |
| 780 | */ |
| 781 | static void |
| 782 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 783 | { |
| 784 | if (define == NULL) |
| 785 | return; |
| 786 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 787 | if ((define->type == XML_RELAXNG_VALUE) && |
| 788 | (define->attrs != NULL)) { |
| 789 | xmlRelaxNGTypeLibraryPtr lib; |
| 790 | |
| 791 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 792 | if ((lib != NULL) && (lib->freef != NULL)) |
| 793 | lib->freef(lib->data, (void *) define->attrs); |
| 794 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 795 | if ((define->data != NULL) && |
| 796 | (define->type == XML_RELAXNG_INTERLEAVE)) |
| 797 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 798 | if ((define->data != NULL) && |
| 799 | (define->type == XML_RELAXNG_CHOICE)) |
| 800 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 801 | if (define->name != NULL) |
| 802 | xmlFree(define->name); |
| 803 | if (define->ns != NULL) |
| 804 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 805 | if (define->value != NULL) |
| 806 | xmlFree(define->value); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 807 | xmlFree(define); |
| 808 | } |
| 809 | |
| 810 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 811 | * xmlRelaxNGNewStates: |
| 812 | * @ctxt: a Relax-NG validation context |
| 813 | * @size: the default size for the container |
| 814 | * |
| 815 | * Allocate a new RelaxNG validation state container |
| 816 | * TODO: keep a pool in the ctxt |
| 817 | * |
| 818 | * Returns the newly allocated structure or NULL in case or error |
| 819 | */ |
| 820 | static xmlRelaxNGStatesPtr |
| 821 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 822 | { |
| 823 | xmlRelaxNGStatesPtr ret; |
| 824 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 825 | if ((ctxt != NULL) && |
| 826 | (ctxt->freeState != NULL) && |
| 827 | (ctxt->freeStatesNr > 0)) { |
| 828 | ctxt->freeStatesNr--; |
| 829 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 830 | ret->nbState = 0; |
| 831 | return(ret); |
| 832 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 833 | if (size < 16) size = 16; |
| 834 | |
| 835 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
| 836 | (size - 1) * sizeof(xmlRelaxNGValidStatePtr)); |
| 837 | if (ret == NULL) { |
| 838 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 839 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 840 | return (NULL); |
| 841 | } |
| 842 | ret->nbState = 0; |
| 843 | ret->maxState = size; |
| 844 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc( |
| 845 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 846 | if (ret->tabState == NULL) { |
| 847 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 848 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 849 | xmlFree(ret->tabState); |
| 850 | return (NULL); |
| 851 | } |
| 852 | return(ret); |
| 853 | } |
| 854 | |
| 855 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 856 | * xmlRelaxNGAddStateUniq: |
| 857 | * @ctxt: a Relax-NG validation context |
| 858 | * @states: the states container |
| 859 | * @state: the validation state |
| 860 | * |
| 861 | * Add a RelaxNG validation state to the container without checking |
| 862 | * for unicity. |
| 863 | * |
| 864 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 865 | */ |
| 866 | static int |
| 867 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
| 868 | xmlRelaxNGStatesPtr states, |
| 869 | xmlRelaxNGValidStatePtr state) |
| 870 | { |
| 871 | if (state == NULL) { |
| 872 | return(-1); |
| 873 | } |
| 874 | if (states->nbState >= states->maxState) { |
| 875 | xmlRelaxNGValidStatePtr *tmp; |
| 876 | int size; |
| 877 | |
| 878 | size = states->maxState * 2; |
| 879 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 880 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 881 | if (tmp == NULL) { |
| 882 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 883 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 884 | return(-1); |
| 885 | } |
| 886 | states->tabState = tmp; |
| 887 | states->maxState = size; |
| 888 | } |
| 889 | states->tabState[states->nbState++] = state; |
| 890 | return(1); |
| 891 | } |
| 892 | |
| 893 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 894 | * xmlRelaxNGAddState: |
| 895 | * @ctxt: a Relax-NG validation context |
| 896 | * @states: the states container |
| 897 | * @state: the validation state |
| 898 | * |
| 899 | * Add a RelaxNG validation state to the container |
| 900 | * |
| 901 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 902 | */ |
| 903 | static int |
| 904 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGStatesPtr states, |
| 905 | xmlRelaxNGValidStatePtr state) |
| 906 | { |
| 907 | int i; |
| 908 | |
| 909 | if (state == NULL) { |
| 910 | return(-1); |
| 911 | } |
| 912 | if (states->nbState >= states->maxState) { |
| 913 | xmlRelaxNGValidStatePtr *tmp; |
| 914 | int size; |
| 915 | |
| 916 | size = states->maxState * 2; |
| 917 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 918 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 919 | if (tmp == NULL) { |
| 920 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 921 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 922 | return(-1); |
| 923 | } |
| 924 | states->tabState = tmp; |
| 925 | states->maxState = size; |
| 926 | } |
| 927 | for (i = 0;i < states->nbState;i++) { |
| 928 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 929 | xmlRelaxNGFreeValidState(ctxt, state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 930 | return(0); |
| 931 | } |
| 932 | } |
| 933 | states->tabState[states->nbState++] = state; |
| 934 | return(1); |
| 935 | } |
| 936 | |
| 937 | /** |
| 938 | * xmlRelaxNGFreeStates: |
| 939 | * @ctxt: a Relax-NG validation context |
| 940 | * @states: teh container |
| 941 | * |
| 942 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 943 | */ |
| 944 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 945 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 946 | xmlRelaxNGStatesPtr states) |
| 947 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 948 | if (states == NULL) |
| 949 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 950 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
| 951 | ctxt->freeStatesMax = 40; |
| 952 | ctxt->freeStatesNr = 0; |
| 953 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 954 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 955 | if (ctxt->freeStates == NULL) { |
| 956 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 957 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 958 | } |
| 959 | } else if ((ctxt != NULL) && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 960 | xmlRelaxNGStatesPtr *tmp; |
| 961 | |
| 962 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 963 | 2 * ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 964 | if (tmp == NULL) { |
| 965 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 966 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 967 | xmlFree(states->tabState); |
| 968 | xmlFree(states); |
| 969 | return; |
| 970 | } |
| 971 | ctxt->freeStates = tmp; |
| 972 | ctxt->freeStatesMax *= 2; |
| 973 | } |
| 974 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 975 | xmlFree(states->tabState); |
| 976 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 977 | } else { |
| 978 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | |
| 982 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 983 | * xmlRelaxNGNewValidState: |
| 984 | * @ctxt: a Relax-NG validation context |
| 985 | * @node: the current node or NULL for the document |
| 986 | * |
| 987 | * Allocate a new RelaxNG validation state |
| 988 | * |
| 989 | * Returns the newly allocated structure or NULL in case or error |
| 990 | */ |
| 991 | static xmlRelaxNGValidStatePtr |
| 992 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 993 | { |
| 994 | xmlRelaxNGValidStatePtr ret; |
| 995 | xmlAttrPtr attr; |
| 996 | xmlAttrPtr attrs[MAX_ATTR]; |
| 997 | int nbAttrs = 0; |
| 998 | xmlNodePtr root = NULL; |
| 999 | |
| 1000 | if (node == NULL) { |
| 1001 | root = xmlDocGetRootElement(ctxt->doc); |
| 1002 | if (root == NULL) |
| 1003 | return(NULL); |
| 1004 | } else { |
| 1005 | attr = node->properties; |
| 1006 | while (attr != NULL) { |
| 1007 | if (nbAttrs < MAX_ATTR) |
| 1008 | attrs[nbAttrs++] = attr; |
| 1009 | else |
| 1010 | nbAttrs++; |
| 1011 | attr = attr->next; |
| 1012 | } |
| 1013 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1014 | if ((ctxt->freeState != NULL) && |
| 1015 | (ctxt->freeState->nbState > 0)) { |
| 1016 | ctxt->freeState->nbState--; |
| 1017 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1018 | } else { |
| 1019 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1020 | if (ret == NULL) { |
| 1021 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1022 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1023 | return (NULL); |
| 1024 | } |
| 1025 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1026 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1027 | ret->value = NULL; |
| 1028 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1029 | if (node == NULL) { |
| 1030 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1031 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1032 | } else { |
| 1033 | ret->node = node; |
| 1034 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1035 | } |
| 1036 | ret->nbAttrs = 0; |
| 1037 | if (nbAttrs > 0) { |
| 1038 | if (ret->attrs == NULL) { |
| 1039 | if (nbAttrs < 4) ret->maxAttrs = 4; |
| 1040 | else ret->maxAttrs = nbAttrs; |
| 1041 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1042 | sizeof(xmlAttrPtr)); |
| 1043 | if (ret->attrs == NULL) { |
| 1044 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1045 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1046 | return (ret); |
| 1047 | } |
| 1048 | } else if (ret->maxAttrs < nbAttrs) { |
| 1049 | xmlAttrPtr *tmp; |
| 1050 | |
| 1051 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1052 | sizeof(xmlAttrPtr)); |
| 1053 | if (tmp == NULL) { |
| 1054 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1055 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1056 | return (ret); |
| 1057 | } |
| 1058 | ret->attrs = tmp; |
| 1059 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1060 | ret->nbAttrs = nbAttrs; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1061 | if (nbAttrs < MAX_ATTR) { |
| 1062 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1063 | } else { |
| 1064 | attr = node->properties; |
| 1065 | nbAttrs = 0; |
| 1066 | while (attr != NULL) { |
| 1067 | ret->attrs[nbAttrs++] = attr; |
| 1068 | attr = attr->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1072 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1073 | return (ret); |
| 1074 | } |
| 1075 | |
| 1076 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1077 | * xmlRelaxNGCopyValidState: |
| 1078 | * @ctxt: a Relax-NG validation context |
| 1079 | * @state: a validation state |
| 1080 | * |
| 1081 | * Copy the validation state |
| 1082 | * |
| 1083 | * Returns the newly allocated structure or NULL in case or error |
| 1084 | */ |
| 1085 | static xmlRelaxNGValidStatePtr |
| 1086 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1087 | xmlRelaxNGValidStatePtr state) |
| 1088 | { |
| 1089 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1090 | unsigned int maxAttrs; |
| 1091 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1092 | |
| 1093 | if (state == NULL) |
| 1094 | return(NULL); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1095 | if ((ctxt->freeState != NULL) && |
| 1096 | (ctxt->freeState->nbState > 0)) { |
| 1097 | ctxt->freeState->nbState--; |
| 1098 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1099 | } else { |
| 1100 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1101 | if (ret == NULL) { |
| 1102 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1103 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1104 | return (NULL); |
| 1105 | } |
| 1106 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1107 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1108 | attrs = ret->attrs; |
| 1109 | maxAttrs = ret->maxAttrs; |
| 1110 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1111 | ret->attrs = attrs; |
| 1112 | ret->maxAttrs = maxAttrs; |
| 1113 | if (state->nbAttrs > 0) { |
| 1114 | if (ret->attrs == NULL) { |
| 1115 | ret->maxAttrs = state->maxAttrs; |
| 1116 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1117 | sizeof(xmlAttrPtr)); |
| 1118 | if (ret->attrs == NULL) { |
| 1119 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1120 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1121 | ret->nbAttrs = 0; |
| 1122 | return (ret); |
| 1123 | } |
| 1124 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1125 | xmlAttrPtr *tmp; |
| 1126 | |
| 1127 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1128 | sizeof(xmlAttrPtr)); |
| 1129 | if (tmp == NULL) { |
| 1130 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1131 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1132 | ret->nbAttrs = 0; |
| 1133 | return (ret); |
| 1134 | } |
| 1135 | ret->maxAttrs = state->maxAttrs; |
| 1136 | } |
| 1137 | memcpy(ret->attrs, state->attrs, state->nbAttrs * sizeof(xmlAttrPtr)); |
| 1138 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1139 | return(ret); |
| 1140 | } |
| 1141 | |
| 1142 | /** |
| 1143 | * xmlRelaxNGEqualValidState: |
| 1144 | * @ctxt: a Relax-NG validation context |
| 1145 | * @state1: a validation state |
| 1146 | * @state2: a validation state |
| 1147 | * |
| 1148 | * Compare the validation states for equality |
| 1149 | * |
| 1150 | * Returns 1 if equald, 0 otherwise |
| 1151 | */ |
| 1152 | static int |
| 1153 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1154 | xmlRelaxNGValidStatePtr state1, |
| 1155 | xmlRelaxNGValidStatePtr state2) |
| 1156 | { |
| 1157 | int i; |
| 1158 | |
| 1159 | if ((state1 == NULL) || (state2 == NULL)) |
| 1160 | return(0); |
| 1161 | if (state1 == state2) |
| 1162 | return(1); |
| 1163 | if (state1->node != state2->node) |
| 1164 | return(0); |
| 1165 | if (state1->seq != state2->seq) |
| 1166 | return(0); |
| 1167 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
| 1168 | return(0); |
| 1169 | if (state1->nbAttrs != state2->nbAttrs) |
| 1170 | return(0); |
| 1171 | if (state1->endvalue != state2->endvalue) |
| 1172 | return(0); |
| 1173 | if ((state1->value != state2->value) && |
| 1174 | (!xmlStrEqual(state1->value, state2->value))) |
| 1175 | return(0); |
| 1176 | for (i = 0;i < state1->nbAttrs;i++) { |
| 1177 | if (state1->attrs[i] != state2->attrs[i]) |
| 1178 | return(0); |
| 1179 | } |
| 1180 | return(1); |
| 1181 | } |
| 1182 | |
| 1183 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1184 | * xmlRelaxNGFreeValidState: |
| 1185 | * @state: a validation state structure |
| 1186 | * |
| 1187 | * Deallocate a RelaxNG validation state structure. |
| 1188 | */ |
| 1189 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1190 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1191 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1192 | { |
| 1193 | if (state == NULL) |
| 1194 | return; |
| 1195 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1196 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
| 1197 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
| 1198 | } |
| 1199 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
| 1200 | if (state->attrs != NULL) |
| 1201 | xmlFree(state->attrs); |
| 1202 | xmlFree(state); |
| 1203 | } else { |
| 1204 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
| 1205 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | /************************************************************************ |
| 1209 | * * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1210 | * Document functions * |
| 1211 | * * |
| 1212 | ************************************************************************/ |
| 1213 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
| 1214 | xmlDocPtr doc); |
| 1215 | |
| 1216 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1217 | * xmlRelaxNGIncludePush: |
| 1218 | * @ctxt: the parser context |
| 1219 | * @value: the element doc |
| 1220 | * |
| 1221 | * Pushes a new include on top of the include stack |
| 1222 | * |
| 1223 | * Returns 0 in case of error, the index in the stack otherwise |
| 1224 | */ |
| 1225 | static int |
| 1226 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1227 | xmlRelaxNGIncludePtr value) |
| 1228 | { |
| 1229 | if (ctxt->incTab == NULL) { |
| 1230 | ctxt->incMax = 4; |
| 1231 | ctxt->incNr = 0; |
| 1232 | ctxt->incTab = (xmlRelaxNGIncludePtr *) xmlMalloc( |
| 1233 | ctxt->incMax * sizeof(ctxt->incTab[0])); |
| 1234 | if (ctxt->incTab == NULL) { |
| 1235 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1236 | return (0); |
| 1237 | } |
| 1238 | } |
| 1239 | if (ctxt->incNr >= ctxt->incMax) { |
| 1240 | ctxt->incMax *= 2; |
| 1241 | ctxt->incTab = |
| 1242 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
| 1243 | ctxt->incMax * |
| 1244 | sizeof(ctxt->incTab[0])); |
| 1245 | if (ctxt->incTab == NULL) { |
| 1246 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1247 | return (0); |
| 1248 | } |
| 1249 | } |
| 1250 | ctxt->incTab[ctxt->incNr] = value; |
| 1251 | ctxt->inc = value; |
| 1252 | return (ctxt->incNr++); |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * xmlRelaxNGIncludePop: |
| 1257 | * @ctxt: the parser context |
| 1258 | * |
| 1259 | * Pops the top include from the include stack |
| 1260 | * |
| 1261 | * Returns the include just removed |
| 1262 | */ |
| 1263 | static xmlRelaxNGIncludePtr |
| 1264 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1265 | { |
| 1266 | xmlRelaxNGIncludePtr ret; |
| 1267 | |
| 1268 | if (ctxt->incNr <= 0) |
| 1269 | return (0); |
| 1270 | ctxt->incNr--; |
| 1271 | if (ctxt->incNr > 0) |
| 1272 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1273 | else |
| 1274 | ctxt->inc = NULL; |
| 1275 | ret = ctxt->incTab[ctxt->incNr]; |
| 1276 | ctxt->incTab[ctxt->incNr] = 0; |
| 1277 | return (ret); |
| 1278 | } |
| 1279 | |
| 1280 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1281 | * xmlRelaxNGRemoveRedefine: |
| 1282 | * @ctxt: the parser context |
| 1283 | * @URL: the normalized URL |
| 1284 | * @target: the included target |
| 1285 | * @name: the define name to eliminate |
| 1286 | * |
| 1287 | * Applies the elimination algorithm of 4.7 |
| 1288 | * |
| 1289 | * Returns 0 in case of error, 1 in case of success. |
| 1290 | */ |
| 1291 | static int |
| 1292 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
| 1293 | const xmlChar *URL ATTRIBUTE_UNUSED, |
| 1294 | xmlNodePtr target, const xmlChar *name) { |
| 1295 | int found = 0; |
| 1296 | xmlNodePtr tmp, tmp2; |
| 1297 | xmlChar *name2; |
| 1298 | |
| 1299 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1300 | if (name == NULL) |
| 1301 | xmlGenericError(xmlGenericErrorContext, |
| 1302 | "Elimination of <include> start from %s\n", URL); |
| 1303 | else |
| 1304 | xmlGenericError(xmlGenericErrorContext, |
| 1305 | "Elimination of <include> define %s from %s\n", name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1306 | #endif |
| 1307 | tmp = target; |
| 1308 | while (tmp != NULL) { |
| 1309 | tmp2 = tmp->next; |
| 1310 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1311 | found = 1; |
| 1312 | xmlUnlinkNode(tmp); |
| 1313 | xmlFreeNode(tmp); |
| 1314 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1315 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1316 | xmlRelaxNGNormExtSpace(name2); |
| 1317 | if (name2 != NULL) { |
| 1318 | if (xmlStrEqual(name, name2)) { |
| 1319 | found = 1; |
| 1320 | xmlUnlinkNode(tmp); |
| 1321 | xmlFreeNode(tmp); |
| 1322 | } |
| 1323 | xmlFree(name2); |
| 1324 | } |
| 1325 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1326 | xmlChar *href = NULL; |
| 1327 | xmlRelaxNGDocumentPtr inc = tmp->_private; |
| 1328 | |
| 1329 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1330 | (inc->doc->children != NULL)) { |
| 1331 | |
| 1332 | if (xmlStrEqual(inc->doc->children->name, BAD_CAST "grammar")) { |
| 1333 | #ifdef DEBUG_INCLUDE |
| 1334 | href = xmlGetProp(tmp, BAD_CAST "href"); |
| 1335 | #endif |
| 1336 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1337 | inc->doc->children->children, name) == 1) { |
| 1338 | found = 1; |
| 1339 | } |
| 1340 | if (href != NULL) |
| 1341 | xmlFree(href); |
| 1342 | } |
| 1343 | } |
| 1344 | } |
| 1345 | tmp = tmp2; |
| 1346 | } |
| 1347 | return(found); |
| 1348 | } |
| 1349 | |
| 1350 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1351 | * xmlRelaxNGLoadInclude: |
| 1352 | * @ctxt: the parser context |
| 1353 | * @URL: the normalized URL |
| 1354 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1355 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1356 | * |
| 1357 | * First lookup if the document is already loaded into the parser context, |
| 1358 | * check against recursion. If not found the resource is loaded and |
| 1359 | * the content is preprocessed before being returned back to the caller. |
| 1360 | * |
| 1361 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1362 | */ |
| 1363 | static xmlRelaxNGIncludePtr |
| 1364 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1365 | xmlNodePtr node, const xmlChar *ns) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1366 | xmlRelaxNGIncludePtr ret = NULL; |
| 1367 | xmlDocPtr doc; |
| 1368 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1369 | xmlNodePtr root, cur; |
| 1370 | |
| 1371 | #ifdef DEBUG_INCLUDE |
| 1372 | xmlGenericError(xmlGenericErrorContext, |
| 1373 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
| 1374 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1375 | |
| 1376 | /* |
| 1377 | * check against recursion in the stack |
| 1378 | */ |
| 1379 | for (i = 0;i < ctxt->incNr;i++) { |
| 1380 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1381 | if (ctxt->error != NULL) |
| 1382 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1383 | "Detected an Include recursion for %s\n", |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1384 | URL); |
| 1385 | ctxt->nbErrors++; |
| 1386 | return(NULL); |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1391 | * load the document |
| 1392 | */ |
| 1393 | doc = xmlParseFile((const char *) URL); |
| 1394 | if (doc == NULL) { |
| 1395 | if (ctxt->error != NULL) |
| 1396 | ctxt->error(ctxt->userData, |
| 1397 | "xmlRelaxNG: could not load %s\n", URL); |
| 1398 | ctxt->nbErrors++; |
| 1399 | return (NULL); |
| 1400 | } |
| 1401 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1402 | #ifdef DEBUG_INCLUDE |
| 1403 | xmlGenericError(xmlGenericErrorContext, |
| 1404 | "Parsed %s Okay\n", URL); |
| 1405 | #endif |
| 1406 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1407 | /* |
| 1408 | * Allocate the document structures and register it first. |
| 1409 | */ |
| 1410 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1411 | if (ret == NULL) { |
| 1412 | if (ctxt->error != NULL) |
| 1413 | ctxt->error(ctxt->userData, |
| 1414 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1415 | ctxt->nbErrors++; |
| 1416 | xmlFreeDoc(doc); |
| 1417 | return (NULL); |
| 1418 | } |
| 1419 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1420 | ret->doc = doc; |
| 1421 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1422 | ret->next = ctxt->includes; |
| 1423 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1424 | |
| 1425 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1426 | * transmit the ns if needed |
| 1427 | */ |
| 1428 | if (ns != NULL) { |
| 1429 | root = xmlDocGetRootElement(doc); |
| 1430 | if (root != NULL) { |
| 1431 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1432 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1438 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1439 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1440 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1441 | |
| 1442 | /* |
| 1443 | * Some preprocessing of the document content, this include recursing |
| 1444 | * in the include stack. |
| 1445 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1446 | #ifdef DEBUG_INCLUDE |
| 1447 | xmlGenericError(xmlGenericErrorContext, |
| 1448 | "cleanup of %s\n", URL); |
| 1449 | #endif |
| 1450 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1451 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1452 | if (doc == NULL) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1453 | ctxt->inc = NULL; |
| 1454 | return(NULL); |
| 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * Pop up the include from the stack |
| 1459 | */ |
| 1460 | xmlRelaxNGIncludePop(ctxt); |
| 1461 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1462 | #ifdef DEBUG_INCLUDE |
| 1463 | xmlGenericError(xmlGenericErrorContext, |
| 1464 | "Checking of %s\n", URL); |
| 1465 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1466 | /* |
| 1467 | * Check that the top element is a grammar |
| 1468 | */ |
| 1469 | root = xmlDocGetRootElement(doc); |
| 1470 | if (root == NULL) { |
| 1471 | if (ctxt->error != NULL) |
| 1472 | ctxt->error(ctxt->userData, |
| 1473 | "xmlRelaxNG: included document is empty %s\n", URL); |
| 1474 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1475 | return (NULL); |
| 1476 | } |
| 1477 | if (!IS_RELAXNG(root, "grammar")) { |
| 1478 | if (ctxt->error != NULL) |
| 1479 | ctxt->error(ctxt->userData, |
| 1480 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1481 | URL); |
| 1482 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1483 | return (NULL); |
| 1484 | } |
| 1485 | |
| 1486 | /* |
| 1487 | * Elimination of redefined rules in the include. |
| 1488 | */ |
| 1489 | cur = node->children; |
| 1490 | while (cur != NULL) { |
| 1491 | if (IS_RELAXNG(cur, "start")) { |
| 1492 | int found = 0; |
| 1493 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1494 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1495 | if (!found) { |
| 1496 | if (ctxt->error != NULL) |
| 1497 | ctxt->error(ctxt->userData, |
| 1498 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1499 | URL); |
| 1500 | ctxt->nbErrors++; |
| 1501 | } |
| 1502 | } else if (IS_RELAXNG(cur, "define")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1503 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1504 | |
| 1505 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1506 | if (name == NULL) { |
| 1507 | if (ctxt->error != NULL) |
| 1508 | ctxt->error(ctxt->userData, |
| 1509 | "xmlRelaxNG: include %s has define without name\n", |
| 1510 | URL); |
| 1511 | ctxt->nbErrors++; |
| 1512 | } else { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1513 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1514 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1515 | xmlRelaxNGNormExtSpace(name); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1516 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1517 | root->children, name); |
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 define %s but not the included grammar\n", |
| 1522 | URL, name); |
| 1523 | ctxt->nbErrors++; |
| 1524 | } |
| 1525 | xmlFree(name); |
| 1526 | } |
| 1527 | } |
| 1528 | cur = cur->next; |
| 1529 | } |
| 1530 | |
| 1531 | |
| 1532 | return(ret); |
| 1533 | } |
| 1534 | |
| 1535 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1536 | * xmlRelaxNGValidErrorPush: |
| 1537 | * @ctxt: the validation context |
| 1538 | * @err: the error code |
| 1539 | * @arg1: the first string argument |
| 1540 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1541 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1542 | * |
| 1543 | * Pushes a new error on top of the error stack |
| 1544 | * |
| 1545 | * Returns 0 in case of error, the index in the stack otherwise |
| 1546 | */ |
| 1547 | static int |
| 1548 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1549 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1550 | { |
| 1551 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 1552 | #ifdef DEBUG_ERROR |
| 1553 | xmlGenericError(xmlGenericErrorContext, |
| 1554 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
| 1555 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1556 | if (ctxt->errTab == NULL) { |
| 1557 | ctxt->errMax = 8; |
| 1558 | ctxt->errNr = 0; |
| 1559 | ctxt->errTab = (xmlRelaxNGValidErrorPtr) xmlMalloc( |
| 1560 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
| 1561 | if (ctxt->errTab == NULL) { |
| 1562 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1563 | return (0); |
| 1564 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1565 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1566 | } |
| 1567 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1568 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1569 | ctxt->errTab = |
| 1570 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1571 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1572 | if (ctxt->errTab == NULL) { |
| 1573 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1574 | return (0); |
| 1575 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1576 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1577 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1578 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1579 | (ctxt->err->node == ctxt->state->node) && |
| 1580 | (ctxt->err->err == err)) |
| 1581 | return(ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1582 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1583 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1584 | if (dup) { |
| 1585 | cur->arg1 = xmlStrdup(arg1); |
| 1586 | cur->arg2 = xmlStrdup(arg2); |
| 1587 | cur->flags = ERROR_IS_DUP; |
| 1588 | } else { |
| 1589 | cur->arg1 = arg1; |
| 1590 | cur->arg2 = arg2; |
| 1591 | cur->flags = 0; |
| 1592 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1593 | if (ctxt->state != NULL) { |
| 1594 | cur->node = ctxt->state->node; |
| 1595 | cur->seq = ctxt->state->seq; |
| 1596 | } else { |
| 1597 | cur->node = NULL; |
| 1598 | cur->seq = NULL; |
| 1599 | } |
| 1600 | ctxt->err = cur; |
| 1601 | return (ctxt->errNr++); |
| 1602 | } |
| 1603 | |
| 1604 | /** |
| 1605 | * xmlRelaxNGValidErrorPop: |
| 1606 | * @ctxt: the validation context |
| 1607 | * |
| 1608 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1609 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1610 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1611 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1612 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1613 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1614 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1615 | if (ctxt->errNr <= 0) { |
| 1616 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1617 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1618 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1619 | ctxt->errNr--; |
| 1620 | if (ctxt->errNr > 0) |
| 1621 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1622 | else |
| 1623 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1624 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1625 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1626 | if (cur->arg1 != NULL) |
| 1627 | xmlFree((xmlChar *)cur->arg1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1628 | cur->arg1 = NULL; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1629 | if (cur->arg2 != NULL) |
| 1630 | xmlFree((xmlChar *)cur->arg2); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1631 | cur->arg2 = NULL; |
| 1632 | cur->flags = 0; |
| 1633 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1636 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1637 | * xmlRelaxNGDocumentPush: |
| 1638 | * @ctxt: the parser context |
| 1639 | * @value: the element doc |
| 1640 | * |
| 1641 | * Pushes a new doc on top of the doc stack |
| 1642 | * |
| 1643 | * Returns 0 in case of error, the index in the stack otherwise |
| 1644 | */ |
| 1645 | static int |
| 1646 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1647 | xmlRelaxNGDocumentPtr value) |
| 1648 | { |
| 1649 | if (ctxt->docTab == NULL) { |
| 1650 | ctxt->docMax = 4; |
| 1651 | ctxt->docNr = 0; |
| 1652 | ctxt->docTab = (xmlRelaxNGDocumentPtr *) xmlMalloc( |
| 1653 | ctxt->docMax * sizeof(ctxt->docTab[0])); |
| 1654 | if (ctxt->docTab == NULL) { |
| 1655 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1656 | return (0); |
| 1657 | } |
| 1658 | } |
| 1659 | if (ctxt->docNr >= ctxt->docMax) { |
| 1660 | ctxt->docMax *= 2; |
| 1661 | ctxt->docTab = |
| 1662 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
| 1663 | ctxt->docMax * |
| 1664 | sizeof(ctxt->docTab[0])); |
| 1665 | if (ctxt->docTab == NULL) { |
| 1666 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1667 | return (0); |
| 1668 | } |
| 1669 | } |
| 1670 | ctxt->docTab[ctxt->docNr] = value; |
| 1671 | ctxt->doc = value; |
| 1672 | return (ctxt->docNr++); |
| 1673 | } |
| 1674 | |
| 1675 | /** |
| 1676 | * xmlRelaxNGDocumentPop: |
| 1677 | * @ctxt: the parser context |
| 1678 | * |
| 1679 | * Pops the top doc from the doc stack |
| 1680 | * |
| 1681 | * Returns the doc just removed |
| 1682 | */ |
| 1683 | static xmlRelaxNGDocumentPtr |
| 1684 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1685 | { |
| 1686 | xmlRelaxNGDocumentPtr ret; |
| 1687 | |
| 1688 | if (ctxt->docNr <= 0) |
| 1689 | return (0); |
| 1690 | ctxt->docNr--; |
| 1691 | if (ctxt->docNr > 0) |
| 1692 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1693 | else |
| 1694 | ctxt->doc = NULL; |
| 1695 | ret = ctxt->docTab[ctxt->docNr]; |
| 1696 | ctxt->docTab[ctxt->docNr] = 0; |
| 1697 | return (ret); |
| 1698 | } |
| 1699 | |
| 1700 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1701 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1702 | * @ctxt: the parser context |
| 1703 | * @URL: the normalized URL |
| 1704 | * @ns: the inherited ns if any |
| 1705 | * |
| 1706 | * First lookup if the document is already loaded into the parser context, |
| 1707 | * check against recursion. If not found the resource is loaded and |
| 1708 | * the content is preprocessed before being returned back to the caller. |
| 1709 | * |
| 1710 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1711 | */ |
| 1712 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1713 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1714 | const xmlChar *ns) { |
| 1715 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1716 | xmlDocPtr doc; |
| 1717 | xmlNodePtr root; |
| 1718 | int i; |
| 1719 | |
| 1720 | /* |
| 1721 | * check against recursion in the stack |
| 1722 | */ |
| 1723 | for (i = 0;i < ctxt->docNr;i++) { |
| 1724 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1725 | if (ctxt->error != NULL) |
| 1726 | ctxt->error(ctxt->userData, |
| 1727 | "Detected an externalRef recursion for %s\n", |
| 1728 | URL); |
| 1729 | ctxt->nbErrors++; |
| 1730 | return(NULL); |
| 1731 | } |
| 1732 | } |
| 1733 | |
| 1734 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1735 | * load the document |
| 1736 | */ |
| 1737 | doc = xmlParseFile((const char *) URL); |
| 1738 | if (doc == NULL) { |
| 1739 | if (ctxt->error != NULL) |
| 1740 | ctxt->error(ctxt->userData, |
| 1741 | "xmlRelaxNG: could not load %s\n", URL); |
| 1742 | ctxt->nbErrors++; |
| 1743 | return (NULL); |
| 1744 | } |
| 1745 | |
| 1746 | /* |
| 1747 | * Allocate the document structures and register it first. |
| 1748 | */ |
| 1749 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1750 | if (ret == NULL) { |
| 1751 | if (ctxt->error != NULL) |
| 1752 | ctxt->error(ctxt->userData, |
| 1753 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1754 | ctxt->nbErrors++; |
| 1755 | xmlFreeDoc(doc); |
| 1756 | return (NULL); |
| 1757 | } |
| 1758 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1759 | ret->doc = doc; |
| 1760 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1761 | ret->next = ctxt->documents; |
| 1762 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1763 | |
| 1764 | /* |
| 1765 | * transmit the ns if needed |
| 1766 | */ |
| 1767 | if (ns != NULL) { |
| 1768 | root = xmlDocGetRootElement(doc); |
| 1769 | if (root != NULL) { |
| 1770 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1771 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1772 | } |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | /* |
| 1777 | * push it on the stack and register it in the hash table |
| 1778 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1779 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1780 | |
| 1781 | /* |
| 1782 | * Some preprocessing of the document content |
| 1783 | */ |
| 1784 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1785 | if (doc == NULL) { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1786 | ctxt->doc = NULL; |
| 1787 | return(NULL); |
| 1788 | } |
| 1789 | |
| 1790 | xmlRelaxNGDocumentPop(ctxt); |
| 1791 | |
| 1792 | return(ret); |
| 1793 | } |
| 1794 | |
| 1795 | /************************************************************************ |
| 1796 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1797 | * Error functions * |
| 1798 | * * |
| 1799 | ************************************************************************/ |
| 1800 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1801 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 1802 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 1803 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 1804 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 1805 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1806 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1807 | #ifdef DEBUG |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1808 | static const char * |
| 1809 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) { |
| 1810 | if (def == NULL) |
| 1811 | return("none"); |
| 1812 | switch(def->type) { |
| 1813 | case XML_RELAXNG_EMPTY: return("empty"); |
| 1814 | case XML_RELAXNG_NOT_ALLOWED: return("notAllowed"); |
| 1815 | case XML_RELAXNG_EXCEPT: return("except"); |
| 1816 | case XML_RELAXNG_TEXT: return("text"); |
| 1817 | case XML_RELAXNG_ELEMENT: return("element"); |
| 1818 | case XML_RELAXNG_DATATYPE: return("datatype"); |
| 1819 | case XML_RELAXNG_VALUE: return("value"); |
| 1820 | case XML_RELAXNG_LIST: return("list"); |
| 1821 | case XML_RELAXNG_ATTRIBUTE: return("attribute"); |
| 1822 | case XML_RELAXNG_DEF: return("def"); |
| 1823 | case XML_RELAXNG_REF: return("ref"); |
| 1824 | case XML_RELAXNG_EXTERNALREF: return("externalRef"); |
| 1825 | case XML_RELAXNG_PARENTREF: return("parentRef"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1826 | case XML_RELAXNG_OPTIONAL: return("optional"); |
| 1827 | case XML_RELAXNG_ZEROORMORE: return("zeroOrMore"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1828 | case XML_RELAXNG_ONEORMORE: return("oneOrMore"); |
| 1829 | case XML_RELAXNG_CHOICE: return("choice"); |
| 1830 | case XML_RELAXNG_GROUP: return("group"); |
| 1831 | case XML_RELAXNG_INTERLEAVE: return("interleave"); |
| 1832 | case XML_RELAXNG_START: return("start"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1833 | case XML_RELAXNG_NOOP: return("noop"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1834 | case XML_RELAXNG_PARAM: return("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1835 | } |
| 1836 | return("unknown"); |
| 1837 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1838 | #endif |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1839 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1840 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1841 | * xmlRelaxNGGetErrorString: |
| 1842 | * @err: the error code |
| 1843 | * @arg1: the first string argument |
| 1844 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1845 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1846 | * computes a formatted error string for the given error code and args |
| 1847 | * |
| 1848 | * Returns the error string, it must be deallocated by the caller |
| 1849 | */ |
| 1850 | static xmlChar * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1851 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar *arg1, |
| 1852 | const xmlChar *arg2) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1853 | char msg[1000]; |
| 1854 | |
| 1855 | if (arg1 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1856 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1857 | if (arg2 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1858 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1859 | |
| 1860 | msg[0] = 0; |
| 1861 | switch (err) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1862 | case XML_RELAXNG_OK: |
| 1863 | return(NULL); |
| 1864 | case XML_RELAXNG_ERR_MEMORY: |
| 1865 | return(xmlCharStrdup("out of memory")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1866 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1867 | snprintf(msg, 1000, "failed to validate type %s", arg1); |
| 1868 | break; |
| 1869 | case XML_RELAXNG_ERR_TYPEVAL: |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 1870 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'", arg1, arg2); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1871 | break; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1872 | case XML_RELAXNG_ERR_DUPID: |
| 1873 | snprintf(msg, 1000, "ID %s redefined", arg1); |
| 1874 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1875 | case XML_RELAXNG_ERR_TYPECMP: |
| 1876 | snprintf(msg, 1000, "failed to compare type %s", arg1); |
| 1877 | break; |
| 1878 | case XML_RELAXNG_ERR_NOSTATE: |
| 1879 | return(xmlCharStrdup("Internal error: no state")); |
| 1880 | case XML_RELAXNG_ERR_NODEFINE: |
| 1881 | return(xmlCharStrdup("Internal error: no define")); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1882 | case XML_RELAXNG_ERR_INTERNAL: |
| 1883 | snprintf(msg, 1000, "Internal error: %s", arg1); |
| 1884 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1885 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 1886 | snprintf(msg, 1000, "Extra data in list: %s", arg1); |
| 1887 | break; |
| 1888 | case XML_RELAXNG_ERR_INTERNODATA: |
| 1889 | return(xmlCharStrdup("Internal: interleave block has no data")); |
| 1890 | case XML_RELAXNG_ERR_INTERSEQ: |
| 1891 | return(xmlCharStrdup("Invalid sequence in interleave")); |
| 1892 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 1893 | snprintf(msg, 1000, "Extra element %s in interleave", arg1); |
| 1894 | break; |
| 1895 | case XML_RELAXNG_ERR_ELEMNAME: |
| 1896 | snprintf(msg, 1000, "Expecting element %s, got %s", arg1, arg2); |
| 1897 | break; |
| 1898 | case XML_RELAXNG_ERR_ELEMNONS: |
| 1899 | snprintf(msg, 1000, "Expecting a namespace for element %s", arg1); |
| 1900 | break; |
| 1901 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 1902 | snprintf(msg, 1000, "Element %s has wrong namespace: expecting %s", |
| 1903 | arg1, arg2); |
| 1904 | break; |
| 1905 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 1906 | snprintf(msg, 1000, "Expecting no namespace for element %s", arg1); |
| 1907 | break; |
| 1908 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 1909 | snprintf(msg, 1000, "Expecting element %s to be empty", arg1); |
| 1910 | break; |
| 1911 | case XML_RELAXNG_ERR_NOELEM: |
| 1912 | snprintf(msg, 1000, "Expecting an element %s, got nothing", arg1); |
| 1913 | break; |
| 1914 | case XML_RELAXNG_ERR_NOTELEM: |
| 1915 | return(xmlCharStrdup("Expecting an element got text")); |
| 1916 | case XML_RELAXNG_ERR_ATTRVALID: |
| 1917 | snprintf(msg, 1000, "Element %s failed to validate attributes", |
| 1918 | arg1); |
| 1919 | break; |
| 1920 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 1921 | snprintf(msg, 1000, "Element %s failed to validate content", |
| 1922 | arg1); |
| 1923 | break; |
| 1924 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 1925 | snprintf(msg, 1000, "Element %s has extra content: %s", |
| 1926 | arg1, arg2); |
| 1927 | break; |
| 1928 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 1929 | snprintf(msg, 1000, "Invalid attribute %s for element %s", |
| 1930 | arg1, arg2); |
| 1931 | break; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 1932 | case XML_RELAXNG_ERR_LACKDATA: |
| 1933 | snprintf(msg, 1000, "Datatype element %s contains no data", |
| 1934 | arg1); |
| 1935 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1936 | case XML_RELAXNG_ERR_DATAELEM: |
| 1937 | snprintf(msg, 1000, "Datatype element %s has child elements", |
| 1938 | arg1); |
| 1939 | break; |
| 1940 | case XML_RELAXNG_ERR_VALELEM: |
| 1941 | snprintf(msg, 1000, "Value element %s has child elements", |
| 1942 | arg1); |
| 1943 | break; |
| 1944 | case XML_RELAXNG_ERR_LISTELEM: |
| 1945 | snprintf(msg, 1000, "List element %s has child elements", |
| 1946 | arg1); |
| 1947 | break; |
| 1948 | case XML_RELAXNG_ERR_DATATYPE: |
| 1949 | snprintf(msg, 1000, "Error validating datatype %s", |
| 1950 | arg1); |
| 1951 | break; |
| 1952 | case XML_RELAXNG_ERR_VALUE: |
| 1953 | snprintf(msg, 1000, "Error validating value %s", |
| 1954 | arg1); |
| 1955 | break; |
| 1956 | case XML_RELAXNG_ERR_LIST: |
| 1957 | return(xmlCharStrdup("Error validating list")); |
| 1958 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 1959 | return(xmlCharStrdup("No top grammar defined")); |
| 1960 | case XML_RELAXNG_ERR_EXTRADATA: |
| 1961 | return(xmlCharStrdup("Extra data in the document")); |
| 1962 | default: |
| 1963 | TODO |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1964 | } |
| 1965 | if (msg[0] == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1966 | snprintf(msg, 1000, "Unknown error code %d", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1967 | } |
| 1968 | msg[1000] = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1969 | return(xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | /** |
| 1973 | * xmlRelaxNGValidErrorContext: |
| 1974 | * @ctxt: the validation context |
| 1975 | * @node: the node |
| 1976 | * @child: the node child generating the problem. |
| 1977 | * |
| 1978 | * Dump informations about the kocation of the error in the instance |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1979 | */ |
| 1980 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1981 | xmlRelaxNGValidErrorContext(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, |
| 1982 | xmlNodePtr child) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1983 | { |
| 1984 | int line = 0; |
| 1985 | const xmlChar *file = NULL; |
| 1986 | const xmlChar *name = NULL; |
| 1987 | const char *type = "error"; |
| 1988 | |
| 1989 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 1990 | return; |
| 1991 | |
| 1992 | if (child != NULL) |
| 1993 | node = child; |
| 1994 | |
| 1995 | if (node != NULL) { |
| 1996 | if ((node->type == XML_DOCUMENT_NODE) || |
| 1997 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
| 1998 | xmlDocPtr doc = (xmlDocPtr) node; |
| 1999 | |
| 2000 | file = doc->URL; |
| 2001 | } else { |
| 2002 | /* |
| 2003 | * Try to find contextual informations to report |
| 2004 | */ |
| 2005 | if (node->type == XML_ELEMENT_NODE) { |
| 2006 | line = (int) node->content; |
| 2007 | } else if ((node->prev != NULL) && |
| 2008 | (node->prev->type == XML_ELEMENT_NODE)) { |
| 2009 | line = (int) node->prev->content; |
| 2010 | } else if ((node->parent != NULL) && |
| 2011 | (node->parent->type == XML_ELEMENT_NODE)) { |
| 2012 | line = (int) node->parent->content; |
| 2013 | } |
| 2014 | if ((node->doc != NULL) && (node->doc->URL != NULL)) |
| 2015 | file = node->doc->URL; |
| 2016 | if (node->name != NULL) |
| 2017 | name = node->name; |
| 2018 | } |
| 2019 | } |
| 2020 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2021 | type = "RNG validity error"; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2022 | |
| 2023 | if ((file != NULL) && (line != 0) && (name != NULL)) |
| 2024 | ctxt->error(ctxt->userData, "%s: file %s line %d element %s\n", |
| 2025 | type, file, line, name); |
| 2026 | else if ((file != NULL) && (name != NULL)) |
| 2027 | ctxt->error(ctxt->userData, "%s: file %s element %s\n", |
| 2028 | type, file, name); |
| 2029 | else if ((file != NULL) && (line != 0)) |
| 2030 | ctxt->error(ctxt->userData, "%s: file %s line %d\n", type, file, line); |
| 2031 | else if (file != NULL) |
| 2032 | ctxt->error(ctxt->userData, "%s: file %s\n", type, file); |
| 2033 | else if (name != NULL) |
| 2034 | ctxt->error(ctxt->userData, "%s: element %s\n", type, name); |
| 2035 | else |
| 2036 | ctxt->error(ctxt->userData, "%s\n", type); |
| 2037 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2038 | |
| 2039 | /** |
| 2040 | * xmlRelaxNGShowValidError: |
| 2041 | * @ctxt: the validation context |
| 2042 | * @err: the error number |
| 2043 | * @node: the node |
| 2044 | * @child: the node child generating the problem. |
| 2045 | * @arg1: the first argument |
| 2046 | * @arg2: the second argument |
| 2047 | * |
| 2048 | * Show a validation error. |
| 2049 | */ |
| 2050 | static void |
| 2051 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
| 2052 | xmlNodePtr node, xmlNodePtr child, |
| 2053 | const xmlChar *arg1, const xmlChar *arg2) |
| 2054 | { |
| 2055 | xmlChar *msg; |
| 2056 | |
| 2057 | if (ctxt->error == NULL) |
| 2058 | return; |
| 2059 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 2060 | #ifdef DEBUG_ERROR |
| 2061 | xmlGenericError(xmlGenericErrorContext, |
| 2062 | "Show error %d\n", err); |
| 2063 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2064 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2065 | if (msg == NULL) |
| 2066 | return; |
| 2067 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 2068 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 2069 | ctxt->errNo = err; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2070 | xmlRelaxNGValidErrorContext(ctxt, node, child); |
| 2071 | ctxt->error(ctxt->userData, "%s\n", msg); |
| 2072 | xmlFree(msg); |
| 2073 | } |
| 2074 | |
| 2075 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2076 | * xmlRelaxNGPopErrors: |
| 2077 | * @ctxt: the validation context |
| 2078 | * @level: the error level in the stack |
| 2079 | * |
| 2080 | * pop and discard all errors until the given level is reached |
| 2081 | */ |
| 2082 | static void |
| 2083 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) { |
| 2084 | int i; |
| 2085 | xmlRelaxNGValidErrorPtr err; |
| 2086 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 2087 | #ifdef DEBUG_ERROR |
| 2088 | xmlGenericError(xmlGenericErrorContext, |
| 2089 | "Pop errors till level %d\n", level); |
| 2090 | #endif |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2091 | for (i = level;i < ctxt->errNr;i++) { |
| 2092 | err = &ctxt->errTab[i]; |
| 2093 | if (err->flags & ERROR_IS_DUP) { |
| 2094 | if (err->arg1 != NULL) |
| 2095 | xmlFree((xmlChar *)err->arg1); |
| 2096 | err->arg1 = NULL; |
| 2097 | if (err->arg2 != NULL) |
| 2098 | xmlFree((xmlChar *)err->arg2); |
| 2099 | err->arg2 = NULL; |
| 2100 | err->flags = 0; |
| 2101 | } |
| 2102 | } |
| 2103 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2104 | if (ctxt->errNr <= 0) |
| 2105 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2106 | } |
| 2107 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2108 | * xmlRelaxNGDumpValidError: |
| 2109 | * @ctxt: the validation context |
| 2110 | * |
| 2111 | * Show all validation error over a given index. |
| 2112 | */ |
| 2113 | static void |
| 2114 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2115 | int i, j; |
| 2116 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2117 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 2118 | #ifdef DEBUG_ERROR |
| 2119 | xmlGenericError(xmlGenericErrorContext, |
| 2120 | "Dumping error stack %d errors\n", ctxt->errNr); |
| 2121 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2122 | for (i = 0;i < ctxt->errNr;i++) { |
| 2123 | err = &ctxt->errTab[i]; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2124 | for (j = 0;j < i;j++) { |
| 2125 | dup = &ctxt->errTab[j]; |
| 2126 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2127 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2128 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2129 | goto skip; |
| 2130 | } |
| 2131 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2132 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2133 | err->arg1, err->arg2); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2134 | skip: |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2135 | if (err->flags & ERROR_IS_DUP) { |
| 2136 | if (err->arg1 != NULL) |
| 2137 | xmlFree((xmlChar *)err->arg1); |
| 2138 | err->arg1 = NULL; |
| 2139 | if (err->arg2 != NULL) |
| 2140 | xmlFree((xmlChar *)err->arg2); |
| 2141 | err->arg2 = NULL; |
| 2142 | err->flags = 0; |
| 2143 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2144 | } |
| 2145 | ctxt->errNr = 0; |
| 2146 | } |
| 2147 | /** |
| 2148 | * xmlRelaxNGAddValidError: |
| 2149 | * @ctxt: the validation context |
| 2150 | * @err: the error number |
| 2151 | * @arg1: the first argument |
| 2152 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2153 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2154 | * |
| 2155 | * Register a validation error, either generating it if it's sure |
| 2156 | * or stacking it for later handling if unsure. |
| 2157 | */ |
| 2158 | static void |
| 2159 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2160 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2161 | { |
| 2162 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 2163 | return; |
| 2164 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 2165 | #ifdef DEBUG_ERROR |
| 2166 | xmlGenericError(xmlGenericErrorContext, |
| 2167 | "Adding error %d\n", err); |
| 2168 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2169 | /* |
| 2170 | * generate the error directly |
| 2171 | */ |
| 2172 | if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) { |
| 2173 | xmlNodePtr node, seq; |
| 2174 | /* |
| 2175 | * Flush first any stacked error which might be the |
| 2176 | * real cause of the problem. |
| 2177 | */ |
| 2178 | if (ctxt->errNr != 0) |
| 2179 | xmlRelaxNGDumpValidError(ctxt); |
| 2180 | if (ctxt->state != NULL) { |
| 2181 | node = ctxt->state->node; |
| 2182 | seq = ctxt->state->seq; |
| 2183 | } else { |
| 2184 | node = seq = NULL; |
| 2185 | } |
| 2186 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
| 2187 | } |
| 2188 | /* |
| 2189 | * Stack the error for later processing if needed |
| 2190 | */ |
| 2191 | else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2192 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2193 | } |
| 2194 | } |
| 2195 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2196 | |
| 2197 | /************************************************************************ |
| 2198 | * * |
| 2199 | * Type library hooks * |
| 2200 | * * |
| 2201 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2202 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
| 2203 | const xmlChar *str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2204 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2205 | /** |
| 2206 | * xmlRelaxNGSchemaTypeHave: |
| 2207 | * @data: data needed for the library |
| 2208 | * @type: the type name |
| 2209 | * |
| 2210 | * Check if the given type is provided by |
| 2211 | * the W3C XMLSchema Datatype library. |
| 2212 | * |
| 2213 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2214 | */ |
| 2215 | static int |
| 2216 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2217 | const xmlChar *type) { |
| 2218 | xmlSchemaTypePtr typ; |
| 2219 | |
| 2220 | if (type == NULL) |
| 2221 | return(-1); |
| 2222 | typ = xmlSchemaGetPredefinedType(type, |
| 2223 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2224 | if (typ == NULL) |
| 2225 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2226 | return(1); |
| 2227 | } |
| 2228 | |
| 2229 | /** |
| 2230 | * xmlRelaxNGSchemaTypeCheck: |
| 2231 | * @data: data needed for the library |
| 2232 | * @type: the type name |
| 2233 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2234 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2235 | * |
| 2236 | * Check if the given type and value are validated by |
| 2237 | * the W3C XMLSchema Datatype library. |
| 2238 | * |
| 2239 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2240 | */ |
| 2241 | static int |
| 2242 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2243 | const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2244 | const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2245 | void **result, |
| 2246 | xmlNodePtr node) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2247 | xmlSchemaTypePtr typ; |
| 2248 | int ret; |
| 2249 | |
| 2250 | /* |
| 2251 | * TODO: the type should be cached ab provided back, interface subject |
| 2252 | * to changes. |
| 2253 | * TODO: handle facets, may require an additional interface and keep |
| 2254 | * the value returned from the validation. |
| 2255 | */ |
| 2256 | if ((type == NULL) || (value == NULL)) |
| 2257 | return(-1); |
| 2258 | typ = xmlSchemaGetPredefinedType(type, |
| 2259 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2260 | if (typ == NULL) |
| 2261 | return(-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2262 | ret = xmlSchemaValPredefTypeNode(typ, value, |
| 2263 | (xmlSchemaValPtr *) result, node); |
| 2264 | if (ret == 2) /* special ID error code */ |
| 2265 | return(2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2266 | if (ret == 0) |
| 2267 | return(1); |
| 2268 | if (ret > 0) |
| 2269 | return(0); |
| 2270 | return(-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2274 | * xmlRelaxNGSchemaFacetCheck: |
| 2275 | * @data: data needed for the library |
| 2276 | * @type: the type name |
| 2277 | * @facet: the facet name |
| 2278 | * @val: the facet value |
| 2279 | * @strval: the string value |
| 2280 | * @value: the value to check |
| 2281 | * |
| 2282 | * Function provided by a type library to check a value facet |
| 2283 | * |
| 2284 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2285 | */ |
| 2286 | static int |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2287 | xmlRelaxNGSchemaFacetCheck (void *data ATTRIBUTE_UNUSED, const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2288 | const xmlChar *facetname, const xmlChar *val, |
| 2289 | const xmlChar *strval, void *value) { |
| 2290 | xmlSchemaFacetPtr facet; |
| 2291 | xmlSchemaTypePtr typ; |
| 2292 | int ret; |
| 2293 | |
| 2294 | if ((type == NULL) || (strval == NULL)) |
| 2295 | return(-1); |
| 2296 | typ = xmlSchemaGetPredefinedType(type, |
| 2297 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2298 | if (typ == NULL) |
| 2299 | return(-1); |
| 2300 | |
| 2301 | facet = xmlSchemaNewFacet(); |
| 2302 | if (facet == NULL) |
| 2303 | return(-1); |
| 2304 | |
| 2305 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
| 2306 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
| 2307 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
| 2308 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
| 2309 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
| 2310 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
| 2311 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
| 2312 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
| 2313 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
| 2314 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
| 2315 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
| 2316 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
| 2317 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
| 2318 | facet->type = XML_SCHEMA_FACET_PATTERN; |
| 2319 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
| 2320 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
| 2321 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
| 2322 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
| 2323 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
| 2324 | facet->type = XML_SCHEMA_FACET_LENGTH; |
| 2325 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
| 2326 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2327 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2328 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2329 | } else { |
| 2330 | xmlSchemaFreeFacet(facet); |
| 2331 | return(-1); |
| 2332 | } |
| 2333 | facet->value = xmlStrdup(val); |
| 2334 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2335 | if (ret != 0) { |
| 2336 | xmlSchemaFreeFacet(facet); |
| 2337 | return(-1); |
| 2338 | } |
| 2339 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2340 | xmlSchemaFreeFacet(facet); |
| 2341 | if (ret != 0) |
| 2342 | return(-1); |
| 2343 | return(0); |
| 2344 | } |
| 2345 | |
| 2346 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2347 | * xmlRelaxNGSchemaFreeValue: |
| 2348 | * @data: data needed for the library |
| 2349 | * @value: the value to free |
| 2350 | * |
| 2351 | * Function provided by a type library to free a Schemas value |
| 2352 | * |
| 2353 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2354 | */ |
| 2355 | static void |
| 2356 | xmlRelaxNGSchemaFreeValue (void *data ATTRIBUTE_UNUSED, void *value) { |
| 2357 | xmlSchemaFreeValue(value); |
| 2358 | } |
| 2359 | |
| 2360 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2361 | * xmlRelaxNGSchemaTypeCompare: |
| 2362 | * @data: data needed for the library |
| 2363 | * @type: the type name |
| 2364 | * @value1: the first value |
| 2365 | * @value2: the second value |
| 2366 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2367 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2368 | * Datatype library. |
| 2369 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2370 | * 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] | 2371 | */ |
| 2372 | static int |
| 2373 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2374 | const xmlChar *type, |
| 2375 | const xmlChar *value1, |
| 2376 | xmlNodePtr ctxt1, |
| 2377 | void *comp1, |
| 2378 | const xmlChar *value2, |
| 2379 | xmlNodePtr ctxt2) { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2380 | int ret; |
| 2381 | xmlSchemaTypePtr typ; |
| 2382 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2383 | |
| 2384 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
| 2385 | return(-1); |
| 2386 | typ = xmlSchemaGetPredefinedType(type, |
| 2387 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2388 | if (typ == NULL) |
| 2389 | return(-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2390 | if (comp1 == NULL) { |
| 2391 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2392 | if (ret != 0) |
| 2393 | return(-1); |
| 2394 | if (res1 == NULL) |
| 2395 | return(-1); |
| 2396 | } else { |
| 2397 | res1 = (xmlSchemaValPtr) comp1; |
| 2398 | } |
| 2399 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2400 | if (ret != 0) { |
| 2401 | xmlSchemaFreeValue(res1); |
| 2402 | return(-1); |
| 2403 | } |
| 2404 | if (res1 == NULL) { |
| 2405 | xmlSchemaFreeValue(res1); |
| 2406 | return(-1); |
| 2407 | } |
| 2408 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2409 | if (res1 != (xmlSchemaValPtr) comp1) |
| 2410 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2411 | xmlSchemaFreeValue(res2); |
| 2412 | if (ret == -2) |
| 2413 | return(-1); |
| 2414 | if (ret == 0) |
| 2415 | return(1); |
| 2416 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | /** |
| 2420 | * xmlRelaxNGDefaultTypeHave: |
| 2421 | * @data: data needed for the library |
| 2422 | * @type: the type name |
| 2423 | * |
| 2424 | * Check if the given type is provided by |
| 2425 | * the default datatype library. |
| 2426 | * |
| 2427 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2428 | */ |
| 2429 | static int |
| 2430 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar *type) { |
| 2431 | if (type == NULL) |
| 2432 | return(-1); |
| 2433 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2434 | return(1); |
| 2435 | if (xmlStrEqual(type, BAD_CAST "token")) |
| 2436 | return(1); |
| 2437 | return(0); |
| 2438 | } |
| 2439 | |
| 2440 | /** |
| 2441 | * xmlRelaxNGDefaultTypeCheck: |
| 2442 | * @data: data needed for the library |
| 2443 | * @type: the type name |
| 2444 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2445 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2446 | * |
| 2447 | * Check if the given type and value are validated by |
| 2448 | * the default datatype library. |
| 2449 | * |
| 2450 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2451 | */ |
| 2452 | static int |
| 2453 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
| 2454 | const xmlChar *type ATTRIBUTE_UNUSED, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2455 | const xmlChar *value ATTRIBUTE_UNUSED, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2456 | void **result ATTRIBUTE_UNUSED, |
| 2457 | xmlNodePtr node ATTRIBUTE_UNUSED) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2458 | if (value == NULL) |
| 2459 | return(-1); |
| 2460 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2461 | return(1); |
| 2462 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2463 | return(1); |
| 2464 | } |
| 2465 | |
| 2466 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | /** |
| 2470 | * xmlRelaxNGDefaultTypeCompare: |
| 2471 | * @data: data needed for the library |
| 2472 | * @type: the type name |
| 2473 | * @value1: the first value |
| 2474 | * @value2: the second value |
| 2475 | * |
| 2476 | * Compare two values accordingly a type from the default |
| 2477 | * datatype library. |
| 2478 | * |
| 2479 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2480 | */ |
| 2481 | static int |
| 2482 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2483 | const xmlChar *type, |
| 2484 | const xmlChar *value1, |
| 2485 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2486 | void *comp1 ATTRIBUTE_UNUSED, |
| 2487 | const xmlChar *value2, |
| 2488 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2489 | int ret = -1; |
| 2490 | |
| 2491 | if (xmlStrEqual(type, BAD_CAST "string")) { |
| 2492 | ret = xmlStrEqual(value1, value2); |
| 2493 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
| 2494 | if (!xmlStrEqual(value1, value2)) { |
| 2495 | xmlChar *nval, *nvalue; |
| 2496 | |
| 2497 | /* |
| 2498 | * TODO: trivial optimizations are possible by |
| 2499 | * computing at compile-time |
| 2500 | */ |
| 2501 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2502 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
| 2503 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2504 | if ((nval == NULL) || (nvalue == NULL)) |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2505 | ret = -1; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2506 | else if (xmlStrEqual(nval, nvalue)) |
| 2507 | ret = 1; |
| 2508 | else |
| 2509 | ret = 0; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2510 | if (nval != NULL) |
| 2511 | xmlFree(nval); |
| 2512 | if (nvalue != NULL) |
| 2513 | xmlFree(nvalue); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2514 | } else |
| 2515 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2516 | } |
| 2517 | return(ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | static int xmlRelaxNGTypeInitialized = 0; |
| 2521 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2522 | |
| 2523 | /** |
| 2524 | * xmlRelaxNGFreeTypeLibrary: |
| 2525 | * @lib: the type library structure |
| 2526 | * @namespace: the URI bound to the library |
| 2527 | * |
| 2528 | * Free the structure associated to the type library |
| 2529 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2530 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2531 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
| 2532 | const xmlChar *namespace ATTRIBUTE_UNUSED) { |
| 2533 | if (lib == NULL) |
| 2534 | return; |
| 2535 | if (lib->namespace != NULL) |
| 2536 | xmlFree((xmlChar *)lib->namespace); |
| 2537 | xmlFree(lib); |
| 2538 | } |
| 2539 | |
| 2540 | /** |
| 2541 | * xmlRelaxNGRegisterTypeLibrary: |
| 2542 | * @namespace: the URI bound to the library |
| 2543 | * @data: data associated to the library |
| 2544 | * @have: the provide function |
| 2545 | * @check: the checking function |
| 2546 | * @comp: the comparison function |
| 2547 | * |
| 2548 | * Register a new type library |
| 2549 | * |
| 2550 | * Returns 0 in case of success and -1 in case of error. |
| 2551 | */ |
| 2552 | static int |
| 2553 | xmlRelaxNGRegisterTypeLibrary(const xmlChar *namespace, void *data, |
| 2554 | xmlRelaxNGTypeHave have, xmlRelaxNGTypeCheck check, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2555 | xmlRelaxNGTypeCompare comp, xmlRelaxNGFacetCheck facet, |
| 2556 | xmlRelaxNGTypeFree freef) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2557 | xmlRelaxNGTypeLibraryPtr lib; |
| 2558 | int ret; |
| 2559 | |
| 2560 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
| 2561 | (check == NULL) || (comp == NULL)) |
| 2562 | return(-1); |
| 2563 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
| 2564 | xmlGenericError(xmlGenericErrorContext, |
| 2565 | "Relax-NG types library '%s' already registered\n", |
| 2566 | namespace); |
| 2567 | return(-1); |
| 2568 | } |
| 2569 | lib = (xmlRelaxNGTypeLibraryPtr) xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
| 2570 | if (lib == NULL) { |
| 2571 | xmlGenericError(xmlGenericErrorContext, |
| 2572 | "Relax-NG types library '%s' malloc() failed\n", |
| 2573 | namespace); |
| 2574 | return (-1); |
| 2575 | } |
| 2576 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2577 | lib->namespace = xmlStrdup(namespace); |
| 2578 | lib->data = data; |
| 2579 | lib->have = have; |
| 2580 | lib->comp = comp; |
| 2581 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2582 | lib->facet = facet; |
| 2583 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2584 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2585 | if (ret < 0) { |
| 2586 | xmlGenericError(xmlGenericErrorContext, |
| 2587 | "Relax-NG types library failed to register '%s'\n", |
| 2588 | namespace); |
| 2589 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2590 | return(-1); |
| 2591 | } |
| 2592 | return(0); |
| 2593 | } |
| 2594 | |
| 2595 | /** |
| 2596 | * xmlRelaxNGInitTypes: |
| 2597 | * |
| 2598 | * Initilize the default type libraries. |
| 2599 | * |
| 2600 | * Returns 0 in case of success and -1 in case of error. |
| 2601 | */ |
| 2602 | static int |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2603 | xmlRelaxNGInitTypes(void) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2604 | if (xmlRelaxNGTypeInitialized != 0) |
| 2605 | return(0); |
| 2606 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2607 | if (xmlRelaxNGRegisteredTypes == NULL) { |
| 2608 | xmlGenericError(xmlGenericErrorContext, |
| 2609 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2610 | return(-1); |
| 2611 | } |
| 2612 | xmlRelaxNGRegisterTypeLibrary( |
| 2613 | BAD_CAST "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2614 | NULL, |
| 2615 | xmlRelaxNGSchemaTypeHave, |
| 2616 | xmlRelaxNGSchemaTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2617 | xmlRelaxNGSchemaTypeCompare, |
| 2618 | xmlRelaxNGSchemaFacetCheck, |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2619 | xmlRelaxNGSchemaFreeValue); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2620 | xmlRelaxNGRegisterTypeLibrary( |
| 2621 | xmlRelaxNGNs, |
| 2622 | NULL, |
| 2623 | xmlRelaxNGDefaultTypeHave, |
| 2624 | xmlRelaxNGDefaultTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2625 | xmlRelaxNGDefaultTypeCompare, |
| 2626 | NULL, |
| 2627 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2628 | xmlRelaxNGTypeInitialized = 1; |
| 2629 | return(0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | /** |
| 2633 | * xmlRelaxNGCleanupTypes: |
| 2634 | * |
| 2635 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2636 | */ |
| 2637 | void |
| 2638 | xmlRelaxNGCleanupTypes(void) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2639 | if (xmlRelaxNGTypeInitialized == 0) |
| 2640 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2641 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2642 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
| 2643 | xmlRelaxNGFreeTypeLibrary); |
| 2644 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | /************************************************************************ |
| 2648 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2649 | * Compiling element content into regexp * |
| 2650 | * * |
| 2651 | * Sometime the element content can be compiled into a pure regexp, * |
| 2652 | * This allows a faster execution and streamability at that level * |
| 2653 | * * |
| 2654 | ************************************************************************/ |
| 2655 | |
| 2656 | /** |
| 2657 | * xmlRelaxNGIsCompileable: |
| 2658 | * @define: the definition to check |
| 2659 | * |
| 2660 | * Check if a definition is nullable. |
| 2661 | * |
| 2662 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2663 | */ |
| 2664 | static int |
| 2665 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) { |
| 2666 | if (def == NULL) { |
| 2667 | return(-1); |
| 2668 | } |
| 2669 | switch(def->type) { |
| 2670 | case XML_RELAXNG_REF: |
| 2671 | case XML_RELAXNG_EXTERNALREF: |
| 2672 | case XML_RELAXNG_PARENTREF: |
| 2673 | case XML_RELAXNG_NOOP: |
| 2674 | case XML_RELAXNG_START: |
| 2675 | return(xmlRelaxNGIsCompileable(def->content)); |
| 2676 | case XML_RELAXNG_TEXT: |
| 2677 | case XML_RELAXNG_DATATYPE: |
| 2678 | case XML_RELAXNG_LIST: |
| 2679 | case XML_RELAXNG_PARAM: |
| 2680 | case XML_RELAXNG_VALUE: |
| 2681 | |
| 2682 | case XML_RELAXNG_EMPTY: |
| 2683 | case XML_RELAXNG_ELEMENT: |
| 2684 | return(1); |
| 2685 | case XML_RELAXNG_OPTIONAL: |
| 2686 | case XML_RELAXNG_ZEROORMORE: |
| 2687 | case XML_RELAXNG_ONEORMORE: |
| 2688 | case XML_RELAXNG_CHOICE: |
| 2689 | case XML_RELAXNG_GROUP: |
| 2690 | case XML_RELAXNG_DEF: { |
| 2691 | xmlRelaxNGDefinePtr list; |
| 2692 | int ret; |
| 2693 | |
| 2694 | list = def->content; |
| 2695 | while (list != NULL) { |
| 2696 | ret = xmlRelaxNGIsCompileable(list); |
| 2697 | if (ret != 1) |
| 2698 | return(ret); |
| 2699 | list = list->next; |
| 2700 | } |
| 2701 | return(1); |
| 2702 | } |
| 2703 | case XML_RELAXNG_EXCEPT: |
| 2704 | case XML_RELAXNG_ATTRIBUTE: |
| 2705 | case XML_RELAXNG_INTERLEAVE: |
| 2706 | return(0); |
| 2707 | case XML_RELAXNG_NOT_ALLOWED: |
| 2708 | return(-1); |
| 2709 | } |
| 2710 | return(-1); |
| 2711 | } |
| 2712 | |
| 2713 | /************************************************************************ |
| 2714 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2715 | * Parsing functions * |
| 2716 | * * |
| 2717 | ************************************************************************/ |
| 2718 | |
| 2719 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute( |
| 2720 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 2721 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement( |
| 2722 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 2723 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns( |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 2724 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, int group); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 2725 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern( |
| 2726 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 2727 | static xmlRelaxNGPtr xmlRelaxNGParseDocument( |
| 2728 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 2729 | static int xmlRelaxNGParseGrammarContent( |
| 2730 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 2731 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass( |
| 2732 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 2733 | xmlRelaxNGDefinePtr def); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 2734 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar( |
| 2735 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2736 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 2737 | xmlRelaxNGDefinePtr define, xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2738 | |
| 2739 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 2740 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2741 | |
| 2742 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2743 | * xmlRelaxNGIsNullable: |
| 2744 | * @define: the definition to verify |
| 2745 | * |
| 2746 | * Check if a definition is nullable. |
| 2747 | * |
| 2748 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2749 | */ |
| 2750 | static int |
| 2751 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) { |
| 2752 | int ret; |
| 2753 | if (define == NULL) |
| 2754 | return(-1); |
| 2755 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 2756 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2757 | return(1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 2758 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2759 | return(0); |
| 2760 | switch (define->type) { |
| 2761 | case XML_RELAXNG_EMPTY: |
| 2762 | case XML_RELAXNG_TEXT: |
| 2763 | ret = 1; break; |
| 2764 | case XML_RELAXNG_NOOP: |
| 2765 | case XML_RELAXNG_DEF: |
| 2766 | case XML_RELAXNG_REF: |
| 2767 | case XML_RELAXNG_EXTERNALREF: |
| 2768 | case XML_RELAXNG_PARENTREF: |
| 2769 | case XML_RELAXNG_ONEORMORE: |
| 2770 | ret = xmlRelaxNGIsNullable(define->content); |
| 2771 | break; |
| 2772 | case XML_RELAXNG_EXCEPT: |
| 2773 | case XML_RELAXNG_NOT_ALLOWED: |
| 2774 | case XML_RELAXNG_ELEMENT: |
| 2775 | case XML_RELAXNG_DATATYPE: |
| 2776 | case XML_RELAXNG_PARAM: |
| 2777 | case XML_RELAXNG_VALUE: |
| 2778 | case XML_RELAXNG_LIST: |
| 2779 | case XML_RELAXNG_ATTRIBUTE: |
| 2780 | ret = 0; break; |
| 2781 | case XML_RELAXNG_CHOICE: { |
| 2782 | xmlRelaxNGDefinePtr list = define->content; |
| 2783 | |
| 2784 | while (list != NULL) { |
| 2785 | ret = xmlRelaxNGIsNullable(list); |
| 2786 | if (ret != 0) |
| 2787 | goto done; |
| 2788 | list = list->next; |
| 2789 | } |
| 2790 | ret = 0; break; |
| 2791 | } |
| 2792 | case XML_RELAXNG_START: |
| 2793 | case XML_RELAXNG_INTERLEAVE: |
| 2794 | case XML_RELAXNG_GROUP: { |
| 2795 | xmlRelaxNGDefinePtr list = define->content; |
| 2796 | |
| 2797 | while (list != NULL) { |
| 2798 | ret = xmlRelaxNGIsNullable(list); |
| 2799 | if (ret != 1) |
| 2800 | goto done; |
| 2801 | list = list->next; |
| 2802 | } |
| 2803 | return(1); |
| 2804 | } |
| 2805 | default: |
| 2806 | return(-1); |
| 2807 | } |
| 2808 | done: |
| 2809 | if (ret == 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 2810 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2811 | if (ret == 1) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 2812 | define->dflags |= IS_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2813 | return(ret); |
| 2814 | } |
| 2815 | |
| 2816 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2817 | * xmlRelaxNGIsBlank: |
| 2818 | * @str: a string |
| 2819 | * |
| 2820 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 2821 | * |
| 2822 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 2823 | */ |
| 2824 | static int |
| 2825 | xmlRelaxNGIsBlank(xmlChar *str) { |
| 2826 | if (str == NULL) |
| 2827 | return(1); |
| 2828 | while (*str != 0) { |
| 2829 | if (!(IS_BLANK(*str))) return(0); |
| 2830 | str++; |
| 2831 | } |
| 2832 | return(1); |
| 2833 | } |
| 2834 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2835 | /** |
| 2836 | * xmlRelaxNGGetDataTypeLibrary: |
| 2837 | * @ctxt: a Relax-NG parser context |
| 2838 | * @node: the current data or value element |
| 2839 | * |
| 2840 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 2841 | * |
| 2842 | * Returns the datatypeLibary value or NULL if not found |
| 2843 | */ |
| 2844 | static xmlChar * |
| 2845 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 2846 | xmlNodePtr node) { |
| 2847 | xmlChar *ret, *escape; |
| 2848 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2849 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
| 2850 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 2851 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2852 | if (ret[0] == 0) { |
| 2853 | xmlFree(ret); |
| 2854 | return(NULL); |
| 2855 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2856 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2857 | if (escape == NULL) { |
| 2858 | return(ret); |
| 2859 | } |
| 2860 | xmlFree(ret); |
| 2861 | return(escape); |
| 2862 | } |
| 2863 | } |
| 2864 | node = node->parent; |
| 2865 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 2866 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 2867 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2868 | if (ret[0] == 0) { |
| 2869 | xmlFree(ret); |
| 2870 | return(NULL); |
| 2871 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 2872 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 2873 | if (escape == NULL) { |
| 2874 | return(ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2875 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 2876 | xmlFree(ret); |
| 2877 | return(escape); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2878 | } |
| 2879 | node = node->parent; |
| 2880 | } |
| 2881 | return(NULL); |
| 2882 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2883 | |
| 2884 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2885 | * xmlRelaxNGParseValue: |
| 2886 | * @ctxt: a Relax-NG parser context |
| 2887 | * @node: the data node. |
| 2888 | * |
| 2889 | * parse the content of a RelaxNG value node. |
| 2890 | * |
| 2891 | * Returns the definition pointer or NULL in case of error |
| 2892 | */ |
| 2893 | static xmlRelaxNGDefinePtr |
| 2894 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 2895 | xmlRelaxNGDefinePtr def = NULL; |
| 2896 | xmlRelaxNGTypeLibraryPtr lib; |
| 2897 | xmlChar *type; |
| 2898 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2899 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2900 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2901 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2902 | if (def == NULL) |
| 2903 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2904 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2905 | |
| 2906 | type = xmlGetProp(node, BAD_CAST "type"); |
| 2907 | if (type != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2908 | xmlRelaxNGNormExtSpace(type); |
| 2909 | if (xmlValidateNCName(type, 0)) { |
| 2910 | if (ctxt->error != NULL) |
| 2911 | ctxt->error(ctxt->userData, |
| 2912 | "value type '%s' is not an NCName\n", |
| 2913 | type); |
| 2914 | ctxt->nbErrors++; |
| 2915 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2916 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 2917 | if (library == NULL) |
| 2918 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 2919 | |
| 2920 | def->name = type; |
| 2921 | def->ns = library; |
| 2922 | |
| 2923 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 2924 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 2925 | if (lib == NULL) { |
| 2926 | if (ctxt->error != NULL) |
| 2927 | ctxt->error(ctxt->userData, |
| 2928 | "Use of unregistered type library '%s'\n", |
| 2929 | library); |
| 2930 | ctxt->nbErrors++; |
| 2931 | def->data = NULL; |
| 2932 | } else { |
| 2933 | def->data = lib; |
| 2934 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 2935 | if (ctxt->error != NULL) |
| 2936 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2937 | "Internal error with type library '%s': no 'have'\n", |
| 2938 | library); |
| 2939 | ctxt->nbErrors++; |
| 2940 | } else { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2941 | success = lib->have(lib->data, def->name); |
| 2942 | if (success != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 2943 | if (ctxt->error != NULL) |
| 2944 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2945 | "Error type '%s' is not exported by type library '%s'\n", |
| 2946 | def->name, library); |
| 2947 | ctxt->nbErrors++; |
| 2948 | } |
| 2949 | } |
| 2950 | } |
| 2951 | } |
| 2952 | if (node->children == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2953 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 2954 | } else if (((node->children->type != XML_TEXT_NODE) && |
| 2955 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2956 | (node->children->next != NULL)) { |
| 2957 | if (ctxt->error != NULL) |
| 2958 | ctxt->error(ctxt->userData, |
| 2959 | "Expecting a single text value for <value>content\n"); |
| 2960 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2961 | } else if (def != NULL) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2962 | def->value = xmlNodeGetContent(node); |
| 2963 | if (def->value == NULL) { |
| 2964 | if (ctxt->error != NULL) |
| 2965 | ctxt->error(ctxt->userData, |
| 2966 | "Element <value> has no content\n"); |
| 2967 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2968 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 2969 | void *val = NULL; |
| 2970 | |
| 2971 | success = lib->check(lib->data, def->name, def->value, &val, node); |
| 2972 | if (success != 1) { |
| 2973 | if (ctxt->error != NULL) |
| 2974 | ctxt->error(ctxt->userData, |
| 2975 | "Value '%s' is not acceptable for type '%s'\n", |
| 2976 | def->value, def->name); |
| 2977 | ctxt->nbErrors++; |
| 2978 | } else { |
| 2979 | if (val != NULL) |
| 2980 | def->attrs = val; |
| 2981 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2982 | } |
| 2983 | } |
| 2984 | /* TODO check ahead of time that the value is okay per the type */ |
| 2985 | return(def); |
| 2986 | } |
| 2987 | |
| 2988 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2989 | * xmlRelaxNGParseData: |
| 2990 | * @ctxt: a Relax-NG parser context |
| 2991 | * @node: the data node. |
| 2992 | * |
| 2993 | * parse the content of a RelaxNG data node. |
| 2994 | * |
| 2995 | * Returns the definition pointer or NULL in case of error |
| 2996 | */ |
| 2997 | static xmlRelaxNGDefinePtr |
| 2998 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 2999 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3000 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3001 | xmlRelaxNGTypeLibraryPtr lib; |
| 3002 | xmlChar *type; |
| 3003 | xmlChar *library; |
| 3004 | xmlNodePtr content; |
| 3005 | int tmp; |
| 3006 | |
| 3007 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3008 | if (type == NULL) { |
| 3009 | if (ctxt->error != NULL) |
| 3010 | ctxt->error(ctxt->userData, |
| 3011 | "data has no type\n"); |
| 3012 | ctxt->nbErrors++; |
| 3013 | return(NULL); |
| 3014 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3015 | xmlRelaxNGNormExtSpace(type); |
| 3016 | if (xmlValidateNCName(type, 0)) { |
| 3017 | if (ctxt->error != NULL) |
| 3018 | ctxt->error(ctxt->userData, |
| 3019 | "data type '%s' is not an NCName\n", |
| 3020 | type); |
| 3021 | ctxt->nbErrors++; |
| 3022 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3023 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3024 | if (library == NULL) |
| 3025 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 3026 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3027 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3028 | if (def == NULL) { |
| 3029 | xmlFree(type); |
| 3030 | return(NULL); |
| 3031 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3032 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3033 | def->name = type; |
| 3034 | def->ns = library; |
| 3035 | |
| 3036 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3037 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3038 | if (lib == NULL) { |
| 3039 | if (ctxt->error != NULL) |
| 3040 | ctxt->error(ctxt->userData, |
| 3041 | "Use of unregistered type library '%s'\n", |
| 3042 | library); |
| 3043 | ctxt->nbErrors++; |
| 3044 | def->data = NULL; |
| 3045 | } else { |
| 3046 | def->data = lib; |
| 3047 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3048 | if (ctxt->error != NULL) |
| 3049 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3050 | "Internal error with type library '%s': no 'have'\n", |
| 3051 | library); |
| 3052 | ctxt->nbErrors++; |
| 3053 | } else { |
| 3054 | tmp = lib->have(lib->data, def->name); |
| 3055 | if (tmp != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3056 | if (ctxt->error != NULL) |
| 3057 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3058 | "Error type '%s' is not exported by type library '%s'\n", |
| 3059 | def->name, library); |
| 3060 | ctxt->nbErrors++; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3061 | } else if ((xmlStrEqual(library, BAD_CAST |
| 3062 | "http://www.w3.org/2001/XMLSchema-datatypes")) && |
| 3063 | ((xmlStrEqual(def->name, BAD_CAST "IDREF")) || |
| 3064 | (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3065 | ctxt->idref = 1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3066 | } |
| 3067 | } |
| 3068 | } |
| 3069 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3070 | |
| 3071 | /* |
| 3072 | * Handle optional params |
| 3073 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3074 | while (content != NULL) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3075 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3076 | break; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3077 | if (xmlStrEqual(library, |
| 3078 | BAD_CAST"http://relaxng.org/ns/structure/1.0")) { |
| 3079 | if (ctxt->error != NULL) |
| 3080 | ctxt->error(ctxt->userData, |
| 3081 | "Type library '%s' does not allow type parameters\n", |
| 3082 | library); |
| 3083 | ctxt->nbErrors++; |
| 3084 | content = content->next; |
| 3085 | while ((content != NULL) && |
| 3086 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3087 | content = content->next; |
| 3088 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3089 | param = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3090 | if (param != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3091 | param->type = XML_RELAXNG_PARAM; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3092 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3093 | if (param->name == NULL) { |
| 3094 | if (ctxt->error != NULL) |
| 3095 | ctxt->error(ctxt->userData, |
| 3096 | "param has no name\n"); |
| 3097 | ctxt->nbErrors++; |
| 3098 | } |
| 3099 | param->value = xmlNodeGetContent(content); |
| 3100 | if (lastparam == NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3101 | def->attrs = lastparam = param; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3102 | } else { |
| 3103 | lastparam->next = param; |
| 3104 | lastparam = param; |
| 3105 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 3106 | if (lib != NULL) { |
| 3107 | } |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3108 | } |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3109 | content = content->next; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3110 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3111 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3112 | /* |
| 3113 | * Handle optional except |
| 3114 | */ |
| 3115 | if ((content != NULL) && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3116 | xmlNodePtr child; |
| 3117 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
| 3118 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3119 | except = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3120 | if (except == NULL) { |
| 3121 | return(def); |
| 3122 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3123 | except->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3124 | child = content->children; |
| 3125 | if (last == NULL) { |
| 3126 | def->content = except; |
| 3127 | } else { |
| 3128 | last->next = except; |
| 3129 | } |
| 3130 | if (child == NULL) { |
| 3131 | if (ctxt->error != NULL) |
| 3132 | ctxt->error(ctxt->userData, |
| 3133 | "except has no content\n"); |
| 3134 | ctxt->nbErrors++; |
| 3135 | } |
| 3136 | while (child != NULL) { |
| 3137 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3138 | if (tmp2 != NULL) { |
| 3139 | if (last2 == NULL) { |
| 3140 | except->content = last2 = tmp2; |
| 3141 | } else { |
| 3142 | last2->next = tmp2; |
| 3143 | last2 = tmp2; |
| 3144 | } |
| 3145 | } |
| 3146 | child = child->next; |
| 3147 | } |
| 3148 | content = content->next; |
| 3149 | } |
| 3150 | /* |
| 3151 | * Check there is no unhandled data |
| 3152 | */ |
| 3153 | if (content != NULL) { |
| 3154 | if (ctxt->error != NULL) |
| 3155 | ctxt->error(ctxt->userData, |
| 3156 | "Element data has unexpected content %s\n", content->name); |
| 3157 | ctxt->nbErrors++; |
| 3158 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3159 | |
| 3160 | return(def); |
| 3161 | } |
| 3162 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3163 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3164 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3165 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3166 | * xmlRelaxNGCompareNameClasses: |
| 3167 | * @defs1: the first element/attribute defs |
| 3168 | * @defs2: the second element/attribute defs |
| 3169 | * @name: the restriction on the name |
| 3170 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3171 | * |
| 3172 | * Compare the 2 lists of element definitions. The comparison is |
| 3173 | * that if both lists do not accept the same QNames, it returns 1 |
| 3174 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3175 | * |
| 3176 | * Returns 1 disttinct, 0 if equal |
| 3177 | */ |
| 3178 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3179 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
| 3180 | xmlRelaxNGDefinePtr def2) { |
| 3181 | int ret = 1; |
| 3182 | xmlNode node; |
| 3183 | xmlNs ns; |
| 3184 | xmlRelaxNGValidCtxt ctxt; |
| 3185 | ctxt.flags = FLAGS_IGNORABLE; |
| 3186 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3187 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3188 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3189 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
| 3190 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3191 | if (def2->type == XML_RELAXNG_TEXT) |
| 3192 | return(1); |
| 3193 | if (def1->name != NULL) { |
| 3194 | node.name = def1->name; |
| 3195 | } else { |
| 3196 | node.name = invalidName; |
| 3197 | } |
| 3198 | node.ns = &ns; |
| 3199 | if (def1->ns != NULL) { |
| 3200 | if (def1->ns[0] == 0) { |
| 3201 | node.ns = NULL; |
| 3202 | } else { |
| 3203 | ns.href = def1->ns; |
| 3204 | } |
| 3205 | } else { |
| 3206 | ns.href = invalidName; |
| 3207 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3208 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3209 | if (def1->nameClass != NULL) { |
| 3210 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3211 | } else { |
| 3212 | ret = 0; |
| 3213 | } |
| 3214 | } else { |
| 3215 | ret = 1; |
| 3216 | } |
| 3217 | } else if (def1->type == XML_RELAXNG_TEXT) { |
| 3218 | if (def2->type == XML_RELAXNG_TEXT) |
| 3219 | return(0); |
| 3220 | return(1); |
| 3221 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3222 | TODO |
| 3223 | ret = 0; |
| 3224 | } else { |
| 3225 | TODO |
| 3226 | ret = 0; |
| 3227 | } |
| 3228 | if (ret == 0) |
| 3229 | return(ret); |
| 3230 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
| 3231 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3232 | if (def2->name != NULL) { |
| 3233 | node.name = def2->name; |
| 3234 | } else { |
| 3235 | node.name = invalidName; |
| 3236 | } |
| 3237 | node.ns = &ns; |
| 3238 | if (def2->ns != NULL) { |
| 3239 | if (def2->ns[0] == 0) { |
| 3240 | node.ns = NULL; |
| 3241 | } else { |
| 3242 | ns.href = def2->ns; |
| 3243 | } |
| 3244 | } else { |
| 3245 | ns.href = invalidName; |
| 3246 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3247 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3248 | if (def2->nameClass != NULL) { |
| 3249 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3250 | } else { |
| 3251 | ret = 0; |
| 3252 | } |
| 3253 | } else { |
| 3254 | ret = 1; |
| 3255 | } |
| 3256 | } else { |
| 3257 | TODO |
| 3258 | ret = 0; |
| 3259 | } |
| 3260 | |
| 3261 | return(ret); |
| 3262 | } |
| 3263 | |
| 3264 | /** |
| 3265 | * xmlRelaxNGCompareElemDefLists: |
| 3266 | * @ctxt: a Relax-NG parser context |
| 3267 | * @defs1: the first list of element/attribute defs |
| 3268 | * @defs2: the second list of element/attribute defs |
| 3269 | * |
| 3270 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3271 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3272 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3273 | * |
| 3274 | * Returns 1 disttinct, 0 if equal |
| 3275 | */ |
| 3276 | static int |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3277 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3278 | xmlRelaxNGDefinePtr *def1, |
| 3279 | xmlRelaxNGDefinePtr *def2) { |
| 3280 | xmlRelaxNGDefinePtr *basedef2 = def2; |
| 3281 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3282 | if ((def1 == NULL) || (def2 == NULL)) |
| 3283 | return(1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3284 | if ((*def1 == NULL) || (*def2 == NULL)) |
| 3285 | return(1); |
| 3286 | while (*def1 != NULL) { |
| 3287 | while ((*def2) != NULL) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3288 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3289 | return(0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3290 | def2++; |
| 3291 | } |
| 3292 | def2 = basedef2; |
| 3293 | def1++; |
| 3294 | } |
| 3295 | return(1); |
| 3296 | } |
| 3297 | |
| 3298 | /** |
| 3299 | * xmlRelaxNGGetElements: |
| 3300 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3301 | * @def: the definition definition |
| 3302 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3303 | * |
| 3304 | * Compute the list of top elements a definition can generate |
| 3305 | * |
| 3306 | * Returns a list of elements or NULL if none was found. |
| 3307 | */ |
| 3308 | static xmlRelaxNGDefinePtr * |
| 3309 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3310 | xmlRelaxNGDefinePtr def, |
| 3311 | int eora) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3312 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3313 | int len = 0; |
| 3314 | int max = 0; |
| 3315 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3316 | /* |
| 3317 | * Don't run that check in case of error. Infinite recursion |
| 3318 | * becomes possible. |
| 3319 | */ |
| 3320 | if (ctxt->nbErrors != 0) |
| 3321 | return(NULL); |
| 3322 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3323 | parent = NULL; |
| 3324 | cur = def; |
| 3325 | while (cur != NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3326 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3327 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3328 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3329 | if (ret == NULL) { |
| 3330 | max = 10; |
| 3331 | ret = (xmlRelaxNGDefinePtr *) |
| 3332 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3333 | if (ret == NULL) { |
| 3334 | if (ctxt->error != NULL) |
| 3335 | ctxt->error(ctxt->userData, |
| 3336 | "Out of memory in element search\n"); |
| 3337 | ctxt->nbErrors++; |
| 3338 | return(NULL); |
| 3339 | } |
| 3340 | } else if (max <= len) { |
| 3341 | max *= 2; |
| 3342 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3343 | if (ret == NULL) { |
| 3344 | if (ctxt->error != NULL) |
| 3345 | ctxt->error(ctxt->userData, |
| 3346 | "Out of memory in element search\n"); |
| 3347 | ctxt->nbErrors++; |
| 3348 | return(NULL); |
| 3349 | } |
| 3350 | } |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3351 | ret[len++] = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3352 | ret[len] = NULL; |
| 3353 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3354 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3355 | (cur->type == XML_RELAXNG_GROUP) || |
| 3356 | (cur->type == XML_RELAXNG_ONEORMORE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3357 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3358 | (cur->type == XML_RELAXNG_OPTIONAL) || |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3359 | (cur->type == XML_RELAXNG_PARENTREF) || |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3360 | (cur->type == XML_RELAXNG_REF) || |
| 3361 | (cur->type == XML_RELAXNG_DEF)) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3362 | /* |
| 3363 | * Don't go within elements or attributes or string values. |
| 3364 | * Just gather the element top list |
| 3365 | */ |
| 3366 | if (cur->content != NULL) { |
| 3367 | parent = cur; |
| 3368 | cur = cur->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3369 | tmp = cur; |
| 3370 | while (tmp != NULL) { |
| 3371 | tmp->parent = parent; |
| 3372 | tmp = tmp->next; |
| 3373 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3374 | continue; |
| 3375 | } |
| 3376 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3377 | if (cur == def) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3378 | break; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3379 | if (cur->next != NULL) { |
| 3380 | cur = cur->next; |
| 3381 | continue; |
| 3382 | } |
| 3383 | do { |
| 3384 | cur = cur->parent; |
| 3385 | if (cur == NULL) break; |
| 3386 | if (cur == def) return(ret); |
| 3387 | if (cur->next != NULL) { |
| 3388 | cur = cur->next; |
| 3389 | break; |
| 3390 | } |
| 3391 | } while (cur != NULL); |
| 3392 | } |
| 3393 | return(ret); |
| 3394 | } |
| 3395 | |
| 3396 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3397 | * xmlRelaxNGCheckChoiceDeterminism: |
| 3398 | * @ctxt: a Relax-NG parser context |
| 3399 | * @def: the choice definition |
| 3400 | * |
| 3401 | * Also used to find indeterministic pattern in choice |
| 3402 | */ |
| 3403 | static void |
| 3404 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
| 3405 | xmlRelaxNGDefinePtr def) { |
| 3406 | xmlRelaxNGDefinePtr **list; |
| 3407 | xmlRelaxNGDefinePtr cur; |
| 3408 | int nbchild = 0, i, j, ret; |
| 3409 | int is_nullable = 0; |
| 3410 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3411 | xmlHashTablePtr triage = NULL; |
| 3412 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3413 | |
| 3414 | if ((def == NULL) || |
| 3415 | (def->type != XML_RELAXNG_CHOICE)) |
| 3416 | return; |
| 3417 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3418 | if (def->dflags & IS_PROCESSED) |
| 3419 | return; |
| 3420 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3421 | /* |
| 3422 | * Don't run that check in case of error. Infinite recursion |
| 3423 | * becomes possible. |
| 3424 | */ |
| 3425 | if (ctxt->nbErrors != 0) |
| 3426 | return; |
| 3427 | |
| 3428 | is_nullable = xmlRelaxNGIsNullable(def); |
| 3429 | |
| 3430 | cur = def->content; |
| 3431 | while (cur != NULL) { |
| 3432 | nbchild++; |
| 3433 | cur = cur->next; |
| 3434 | } |
| 3435 | |
| 3436 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 3437 | sizeof(xmlRelaxNGDefinePtr *)); |
| 3438 | if (list == NULL) { |
| 3439 | if (ctxt->error != NULL) |
| 3440 | ctxt->error(ctxt->userData, |
| 3441 | "Out of memory in choice computation\n"); |
| 3442 | ctxt->nbErrors++; |
| 3443 | return; |
| 3444 | } |
| 3445 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3446 | /* |
| 3447 | * a bit strong but safe |
| 3448 | */ |
| 3449 | if (is_nullable == 0) { |
| 3450 | triage = xmlHashCreate(10); |
| 3451 | } else { |
| 3452 | is_triable = 0; |
| 3453 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3454 | cur = def->content; |
| 3455 | while (cur != NULL) { |
| 3456 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3457 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 3458 | is_triable = 0; |
| 3459 | } else if (is_triable == 1) { |
| 3460 | xmlRelaxNGDefinePtr *tmp; |
| 3461 | int res; |
| 3462 | |
| 3463 | tmp = list[i]; |
| 3464 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 3465 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 3466 | res = xmlHashAddEntry2(triage, |
| 3467 | BAD_CAST "#text", NULL, |
| 3468 | (void *)cur); |
| 3469 | if (res != 0) |
| 3470 | is_triable = -1; |
| 3471 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 3472 | ((*tmp)->name != NULL)) { |
| 3473 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3474 | res = xmlHashAddEntry2(triage, |
| 3475 | (*tmp)->name, NULL, |
| 3476 | (void *)cur); |
| 3477 | else |
| 3478 | res = xmlHashAddEntry2(triage, |
| 3479 | (*tmp)->name, (*tmp)->ns, |
| 3480 | (void *)cur); |
| 3481 | if (res != 0) |
| 3482 | is_triable = -1; |
| 3483 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 3484 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3485 | res = xmlHashAddEntry2(triage, |
| 3486 | BAD_CAST "#any", NULL, |
| 3487 | (void *)cur); |
| 3488 | else |
| 3489 | res = xmlHashAddEntry2(triage, |
| 3490 | BAD_CAST "#any", (*tmp)->ns, |
| 3491 | (void *)cur); |
| 3492 | if (res != 0) |
| 3493 | is_triable = -1; |
| 3494 | } else { |
| 3495 | is_triable = -1; |
| 3496 | } |
| 3497 | tmp++; |
| 3498 | } |
| 3499 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3500 | i++; |
| 3501 | cur = cur->next; |
| 3502 | } |
| 3503 | |
| 3504 | for (i = 0;i < nbchild;i++) { |
| 3505 | if (list[i] == NULL) |
| 3506 | continue; |
| 3507 | for (j = 0;j < i;j++) { |
| 3508 | if (list[j] == NULL) |
| 3509 | continue; |
| 3510 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 3511 | if (ret == 0) { |
| 3512 | is_indeterminist = 1; |
| 3513 | } |
| 3514 | } |
| 3515 | } |
| 3516 | for (i = 0;i < nbchild;i++) { |
| 3517 | if (list[i] != NULL) |
| 3518 | xmlFree(list[i]); |
| 3519 | } |
| 3520 | |
| 3521 | xmlFree(list); |
| 3522 | if (is_indeterminist) { |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3523 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3524 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3525 | if (is_triable == 1) { |
| 3526 | def->dflags |= IS_TRIABLE; |
| 3527 | def->data = triage; |
| 3528 | } else if (triage != NULL) { |
| 3529 | xmlHashFree(triage, NULL); |
| 3530 | } |
| 3531 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
| 3534 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3535 | * xmlRelaxNGCheckGroupAttrs: |
| 3536 | * @ctxt: a Relax-NG parser context |
| 3537 | * @def: the group definition |
| 3538 | * |
| 3539 | * Detects violations of rule 7.3 |
| 3540 | */ |
| 3541 | static void |
| 3542 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
| 3543 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3544 | xmlRelaxNGDefinePtr **list; |
| 3545 | xmlRelaxNGDefinePtr cur; |
| 3546 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3547 | |
| 3548 | if ((def == NULL) || |
| 3549 | ((def->type != XML_RELAXNG_GROUP) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3550 | (def->type != XML_RELAXNG_ELEMENT))) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3551 | return; |
| 3552 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3553 | if (def->dflags & IS_PROCESSED) |
| 3554 | return; |
| 3555 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3556 | /* |
| 3557 | * Don't run that check in case of error. Infinite recursion |
| 3558 | * becomes possible. |
| 3559 | */ |
| 3560 | if (ctxt->nbErrors != 0) |
| 3561 | return; |
| 3562 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3563 | cur = def->attrs; |
| 3564 | while (cur != NULL) { |
| 3565 | nbchild++; |
| 3566 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3567 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3568 | cur = def->content; |
| 3569 | while (cur != NULL) { |
| 3570 | nbchild++; |
| 3571 | cur = cur->next; |
| 3572 | } |
| 3573 | |
| 3574 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 3575 | sizeof(xmlRelaxNGDefinePtr *)); |
| 3576 | if (list == NULL) { |
| 3577 | if (ctxt->error != NULL) |
| 3578 | ctxt->error(ctxt->userData, |
| 3579 | "Out of memory in group computation\n"); |
| 3580 | ctxt->nbErrors++; |
| 3581 | return; |
| 3582 | } |
| 3583 | i = 0; |
| 3584 | cur = def->attrs; |
| 3585 | while (cur != NULL) { |
| 3586 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 3587 | i++; |
| 3588 | cur = cur->next; |
| 3589 | } |
| 3590 | cur = def->content; |
| 3591 | while (cur != NULL) { |
| 3592 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 3593 | i++; |
| 3594 | cur = cur->next; |
| 3595 | } |
| 3596 | |
| 3597 | for (i = 0;i < nbchild;i++) { |
| 3598 | if (list[i] == NULL) |
| 3599 | continue; |
| 3600 | for (j = 0;j < i;j++) { |
| 3601 | if (list[j] == NULL) |
| 3602 | continue; |
| 3603 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 3604 | if (ret == 0) { |
| 3605 | if (ctxt->error != NULL) |
| 3606 | ctxt->error(ctxt->userData, |
| 3607 | "Attributes conflicts in group\n"); |
| 3608 | ctxt->nbErrors++; |
| 3609 | } |
| 3610 | } |
| 3611 | } |
| 3612 | for (i = 0;i < nbchild;i++) { |
| 3613 | if (list[i] != NULL) |
| 3614 | xmlFree(list[i]); |
| 3615 | } |
| 3616 | |
| 3617 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3618 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3622 | * xmlRelaxNGComputeInterleaves: |
| 3623 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3624 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3625 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3626 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3627 | * A lot of work for preprocessing interleave definitions |
| 3628 | * is potentially needed to get a decent execution speed at runtime |
| 3629 | * - trying to get a total order on the element nodes generated |
| 3630 | * by the interleaves, order the list of interleave definitions |
| 3631 | * following that order. |
| 3632 | * - if <text/> is used to handle mixed content, it is better to |
| 3633 | * flag this in the define and simplify the runtime checking |
| 3634 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3635 | */ |
| 3636 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3637 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
| 3638 | xmlRelaxNGParserCtxtPtr ctxt, |
| 3639 | xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3640 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3641 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3642 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 3643 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 3644 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3645 | int i,j,ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3646 | int nbgroups = 0; |
| 3647 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3648 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3649 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3650 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3651 | /* |
| 3652 | * Don't run that check in case of error. Infinite recursion |
| 3653 | * becomes possible. |
| 3654 | */ |
| 3655 | if (ctxt->nbErrors != 0) |
| 3656 | return; |
| 3657 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3658 | #ifdef DEBUG_INTERLEAVE |
| 3659 | xmlGenericError(xmlGenericErrorContext, |
| 3660 | "xmlRelaxNGComputeInterleaves(%s)\n", |
| 3661 | name); |
| 3662 | #endif |
| 3663 | cur = def->content; |
| 3664 | while (cur != NULL) { |
| 3665 | nbchild++; |
| 3666 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3667 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3668 | |
| 3669 | #ifdef DEBUG_INTERLEAVE |
| 3670 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 3671 | #endif |
| 3672 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
| 3673 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
| 3674 | if (groups == NULL) |
| 3675 | goto error; |
| 3676 | cur = def->content; |
| 3677 | while (cur != NULL) { |
| 3678 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 3679 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 3680 | if (groups[nbgroups] == NULL) |
| 3681 | goto error; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3682 | if (cur->type == XML_RELAXNG_TEXT) |
| 3683 | is_mixed++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3684 | groups[nbgroups]->rule = cur; |
| 3685 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 3686 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 3687 | nbgroups++; |
| 3688 | cur = cur->next; |
| 3689 | } |
| 3690 | #ifdef DEBUG_INTERLEAVE |
| 3691 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 3692 | #endif |
| 3693 | |
| 3694 | /* |
| 3695 | * Let's check that all rules makes a partitions according to 7.4 |
| 3696 | */ |
| 3697 | partitions = (xmlRelaxNGPartitionPtr) |
| 3698 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
| 3699 | if (partitions == NULL) |
| 3700 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 3701 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3702 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3703 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3704 | for (i = 0;i < nbgroups;i++) { |
| 3705 | group = groups[i]; |
| 3706 | for (j = i+1;j < nbgroups;j++) { |
| 3707 | if (groups[j] == NULL) |
| 3708 | continue; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3709 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3710 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 3711 | groups[j]->defs); |
| 3712 | if (ret == 0) { |
| 3713 | if (ctxt->error != NULL) |
| 3714 | ctxt->error(ctxt->userData, |
| 3715 | "Element or text conflicts in interleave\n"); |
| 3716 | ctxt->nbErrors++; |
| 3717 | } |
| 3718 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 3719 | groups[j]->attrs); |
| 3720 | if (ret == 0) { |
| 3721 | if (ctxt->error != NULL) |
| 3722 | ctxt->error(ctxt->userData, |
| 3723 | "Attributes conflicts in interleave\n"); |
| 3724 | ctxt->nbErrors++; |
| 3725 | } |
| 3726 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3727 | tmp = group->defs; |
| 3728 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 3729 | while (*tmp != NULL) { |
| 3730 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 3731 | res = xmlHashAddEntry2(partitions->triage, |
| 3732 | BAD_CAST "#text", NULL, |
| 3733 | (void *)(i + 1)); |
| 3734 | if (res != 0) |
| 3735 | is_determinist = -1; |
| 3736 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 3737 | ((*tmp)->name != NULL)) { |
| 3738 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3739 | res = xmlHashAddEntry2(partitions->triage, |
| 3740 | (*tmp)->name, NULL, |
| 3741 | (void *)(i + 1)); |
| 3742 | else |
| 3743 | res = xmlHashAddEntry2(partitions->triage, |
| 3744 | (*tmp)->name, (*tmp)->ns, |
| 3745 | (void *)(i + 1)); |
| 3746 | if (res != 0) |
| 3747 | is_determinist = -1; |
| 3748 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 3749 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3750 | res = xmlHashAddEntry2(partitions->triage, |
| 3751 | BAD_CAST "#any", NULL, |
| 3752 | (void *)(i + 1)); |
| 3753 | else |
| 3754 | res = xmlHashAddEntry2(partitions->triage, |
| 3755 | BAD_CAST "#any", (*tmp)->ns, |
| 3756 | (void *)(i + 1)); |
| 3757 | if ((*tmp)->nameClass != NULL) |
| 3758 | is_determinist = 2; |
| 3759 | if (res != 0) |
| 3760 | is_determinist = -1; |
| 3761 | } else { |
| 3762 | is_determinist = -1; |
| 3763 | } |
| 3764 | tmp++; |
| 3765 | } |
| 3766 | } else { |
| 3767 | is_determinist = 0; |
| 3768 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3769 | } |
| 3770 | partitions->groups = groups; |
| 3771 | |
| 3772 | /* |
| 3773 | * and save the partition list back in the def |
| 3774 | */ |
| 3775 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3776 | if (is_mixed != 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3777 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3778 | if (is_determinist == 1) |
| 3779 | partitions->flags = IS_DETERMINIST; |
| 3780 | if (is_determinist == 2) |
| 3781 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3782 | return; |
| 3783 | |
| 3784 | error: |
| 3785 | if (ctxt->error != NULL) |
| 3786 | ctxt->error(ctxt->userData, |
| 3787 | "Out of memory in interleave computation\n"); |
| 3788 | ctxt->nbErrors++; |
| 3789 | if (groups != NULL) { |
| 3790 | for (i = 0;i < nbgroups;i++) |
| 3791 | if (groups[i] != NULL) { |
| 3792 | if (groups[i]->defs != NULL) |
| 3793 | xmlFree(groups[i]->defs); |
| 3794 | xmlFree(groups[i]); |
| 3795 | } |
| 3796 | xmlFree(groups); |
| 3797 | } |
| 3798 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
| 3801 | /** |
| 3802 | * xmlRelaxNGParseInterleave: |
| 3803 | * @ctxt: a Relax-NG parser context |
| 3804 | * @node: the data node. |
| 3805 | * |
| 3806 | * parse the content of a RelaxNG interleave node. |
| 3807 | * |
| 3808 | * Returns the definition pointer or NULL in case of error |
| 3809 | */ |
| 3810 | static xmlRelaxNGDefinePtr |
| 3811 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 3812 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3813 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3814 | xmlNodePtr child; |
| 3815 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3816 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 3817 | if (def == NULL) { |
| 3818 | return(NULL); |
| 3819 | } |
| 3820 | def->type = XML_RELAXNG_INTERLEAVE; |
| 3821 | |
| 3822 | if (ctxt->interleaves == NULL) |
| 3823 | ctxt->interleaves = xmlHashCreate(10); |
| 3824 | if (ctxt->interleaves == NULL) { |
| 3825 | if (ctxt->error != NULL) |
| 3826 | ctxt->error(ctxt->userData, |
| 3827 | "Failed to create interleaves hash table\n"); |
| 3828 | ctxt->nbErrors++; |
| 3829 | } else { |
| 3830 | char name[32]; |
| 3831 | |
| 3832 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 3833 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 3834 | if (ctxt->error != NULL) |
| 3835 | ctxt->error(ctxt->userData, |
| 3836 | "Failed to add %s to hash table\n", name); |
| 3837 | ctxt->nbErrors++; |
| 3838 | } |
| 3839 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3840 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3841 | if (child == NULL) { |
| 3842 | if (ctxt->error != NULL) |
| 3843 | ctxt->error(ctxt->userData, "Element interleave is empty\n"); |
| 3844 | ctxt->nbErrors++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 3845 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3846 | while (child != NULL) { |
| 3847 | if (IS_RELAXNG(child, "element")) { |
| 3848 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 3849 | } else { |
| 3850 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 3851 | } |
| 3852 | if (cur != NULL) { |
| 3853 | cur->parent = def; |
| 3854 | if (last == NULL) { |
| 3855 | def->content = last = cur; |
| 3856 | } else { |
| 3857 | last->next = cur; |
| 3858 | last = cur; |
| 3859 | } |
| 3860 | } |
| 3861 | child = child->next; |
| 3862 | } |
| 3863 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3864 | return(def); |
| 3865 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3866 | |
| 3867 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 3868 | * xmlRelaxNGParseInclude: |
| 3869 | * @ctxt: a Relax-NG parser context |
| 3870 | * @node: the include node |
| 3871 | * |
| 3872 | * Integrate the content of an include node in the current grammar |
| 3873 | * |
| 3874 | * Returns 0 in case of success or -1 in case of error |
| 3875 | */ |
| 3876 | static int |
| 3877 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 3878 | xmlRelaxNGIncludePtr incl; |
| 3879 | xmlNodePtr root; |
| 3880 | int ret = 0, tmp; |
| 3881 | |
| 3882 | incl = node->_private; |
| 3883 | if (incl == NULL) { |
| 3884 | if (ctxt->error != NULL) |
| 3885 | ctxt->error(ctxt->userData, |
| 3886 | "Include node has no data\n"); |
| 3887 | ctxt->nbErrors++; |
| 3888 | return(-1); |
| 3889 | } |
| 3890 | root = xmlDocGetRootElement(incl->doc); |
| 3891 | if (root == NULL) { |
| 3892 | if (ctxt->error != NULL) |
| 3893 | ctxt->error(ctxt->userData, |
| 3894 | "Include document is empty\n"); |
| 3895 | ctxt->nbErrors++; |
| 3896 | return(-1); |
| 3897 | } |
| 3898 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
| 3899 | if (ctxt->error != NULL) |
| 3900 | ctxt->error(ctxt->userData, |
| 3901 | "Include document root is not a grammar\n"); |
| 3902 | ctxt->nbErrors++; |
| 3903 | return(-1); |
| 3904 | } |
| 3905 | |
| 3906 | /* |
| 3907 | * Merge the definition from both the include and the internal list |
| 3908 | */ |
| 3909 | if (root->children != NULL) { |
| 3910 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 3911 | if (tmp != 0) |
| 3912 | ret = -1; |
| 3913 | } |
| 3914 | if (node->children != NULL) { |
| 3915 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 3916 | if (tmp != 0) |
| 3917 | ret = -1; |
| 3918 | } |
| 3919 | return(ret); |
| 3920 | } |
| 3921 | |
| 3922 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 3923 | * xmlRelaxNGParseDefine: |
| 3924 | * @ctxt: a Relax-NG parser context |
| 3925 | * @node: the define node |
| 3926 | * |
| 3927 | * parse the content of a RelaxNG define element node. |
| 3928 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 3929 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 3930 | */ |
| 3931 | static int |
| 3932 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 3933 | xmlChar *name; |
| 3934 | int ret = 0, tmp; |
| 3935 | xmlRelaxNGDefinePtr def; |
| 3936 | const xmlChar *olddefine; |
| 3937 | |
| 3938 | name = xmlGetProp(node, BAD_CAST "name"); |
| 3939 | if (name == NULL) { |
| 3940 | if (ctxt->error != NULL) |
| 3941 | ctxt->error(ctxt->userData, |
| 3942 | "define has no name\n"); |
| 3943 | ctxt->nbErrors++; |
| 3944 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3945 | xmlRelaxNGNormExtSpace(name); |
| 3946 | if (xmlValidateNCName(name, 0)) { |
| 3947 | if (ctxt->error != NULL) |
| 3948 | ctxt->error(ctxt->userData, |
| 3949 | "define name '%s' is not an NCName\n", |
| 3950 | name); |
| 3951 | ctxt->nbErrors++; |
| 3952 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3953 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 3954 | if (def == NULL) { |
| 3955 | xmlFree(name); |
| 3956 | return(-1); |
| 3957 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3958 | def->type = XML_RELAXNG_DEF; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 3959 | def->name = name; |
| 3960 | if (node->children == NULL) { |
| 3961 | if (ctxt->error != NULL) |
| 3962 | ctxt->error(ctxt->userData, |
| 3963 | "define has no children\n"); |
| 3964 | ctxt->nbErrors++; |
| 3965 | } else { |
| 3966 | olddefine = ctxt->define; |
| 3967 | ctxt->define = name; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3968 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 3969 | ctxt->define = olddefine; |
| 3970 | } |
| 3971 | if (ctxt->grammar->defs == NULL) |
| 3972 | ctxt->grammar->defs = xmlHashCreate(10); |
| 3973 | if (ctxt->grammar->defs == NULL) { |
| 3974 | if (ctxt->error != NULL) |
| 3975 | ctxt->error(ctxt->userData, |
| 3976 | "Could not create definition hash\n"); |
| 3977 | ctxt->nbErrors++; |
| 3978 | ret = -1; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 3979 | } else { |
| 3980 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 3981 | if (tmp < 0) { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3982 | xmlRelaxNGDefinePtr prev; |
| 3983 | |
| 3984 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 3985 | if (prev == NULL) { |
| 3986 | if (ctxt->error != NULL) |
| 3987 | ctxt->error(ctxt->userData, |
| 3988 | "Internal error on define aggregation of %s\n", |
| 3989 | name); |
| 3990 | ctxt->nbErrors++; |
| 3991 | ret = -1; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3992 | } else { |
| 3993 | while (prev->nextHash != NULL) |
| 3994 | prev = prev->nextHash; |
| 3995 | prev->nextHash = def; |
| 3996 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 3997 | } |
| 3998 | } |
| 3999 | } |
| 4000 | return(ret); |
| 4001 | } |
| 4002 | |
| 4003 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4004 | * xmlRelaxNGProcessExternalRef: |
| 4005 | * @ctxt: the parser context |
| 4006 | * @node: the externlRef node |
| 4007 | * |
| 4008 | * Process and compile an externlRef node |
| 4009 | * |
| 4010 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4011 | */ |
| 4012 | static xmlRelaxNGDefinePtr |
| 4013 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4014 | xmlRelaxNGDocumentPtr docu; |
| 4015 | xmlNodePtr root, tmp; |
| 4016 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4017 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4018 | xmlRelaxNGDefinePtr def; |
| 4019 | |
| 4020 | docu = node->_private; |
| 4021 | if (docu != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4022 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4023 | if (def == NULL) |
| 4024 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4025 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4026 | |
| 4027 | if (docu->content == NULL) { |
| 4028 | /* |
| 4029 | * Then do the parsing for good |
| 4030 | */ |
| 4031 | root = xmlDocGetRootElement(docu->doc); |
| 4032 | if (root == NULL) { |
| 4033 | if (ctxt->error != NULL) |
| 4034 | ctxt->error(ctxt->userData, |
| 4035 | "xmlRelaxNGParse: %s is empty\n", |
| 4036 | ctxt->URL); |
| 4037 | ctxt->nbErrors++; |
| 4038 | return (NULL); |
| 4039 | } |
| 4040 | /* |
| 4041 | * ns transmission rules |
| 4042 | */ |
| 4043 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4044 | if (ns == NULL) { |
| 4045 | tmp = node; |
| 4046 | while ((tmp != NULL) && |
| 4047 | (tmp->type == XML_ELEMENT_NODE)) { |
| 4048 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4049 | if (ns != NULL) { |
| 4050 | break; |
| 4051 | } |
| 4052 | tmp = tmp->parent; |
| 4053 | } |
| 4054 | if (ns != NULL) { |
| 4055 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4056 | newNs = 1; |
| 4057 | xmlFree(ns); |
| 4058 | } |
| 4059 | } else { |
| 4060 | xmlFree(ns); |
| 4061 | } |
| 4062 | |
| 4063 | /* |
| 4064 | * Parsing to get a precompiled schemas. |
| 4065 | */ |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4066 | oldflags = ctxt->flags; |
| 4067 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4068 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4069 | ctxt->flags = oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4070 | if ((docu->schema != NULL) && |
| 4071 | (docu->schema->topgrammar != NULL)) { |
| 4072 | docu->content = docu->schema->topgrammar->start; |
| 4073 | } |
| 4074 | |
| 4075 | /* |
| 4076 | * the externalRef may be reused in a different ns context |
| 4077 | */ |
| 4078 | if (newNs == 1) { |
| 4079 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4080 | } |
| 4081 | } |
| 4082 | def->content = docu->content; |
| 4083 | } else { |
| 4084 | def = NULL; |
| 4085 | } |
| 4086 | return(def); |
| 4087 | } |
| 4088 | |
| 4089 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4090 | * xmlRelaxNGParsePattern: |
| 4091 | * @ctxt: a Relax-NG parser context |
| 4092 | * @node: the pattern node. |
| 4093 | * |
| 4094 | * parse the content of a RelaxNG pattern node. |
| 4095 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4096 | * Returns the definition pointer or NULL in case of error or if no |
| 4097 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4098 | */ |
| 4099 | static xmlRelaxNGDefinePtr |
| 4100 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4101 | xmlRelaxNGDefinePtr def = NULL; |
| 4102 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4103 | if (node == NULL) { |
| 4104 | return(NULL); |
| 4105 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4106 | if (IS_RELAXNG(node, "element")) { |
| 4107 | def = xmlRelaxNGParseElement(ctxt, node); |
| 4108 | } else if (IS_RELAXNG(node, "attribute")) { |
| 4109 | def = xmlRelaxNGParseAttribute(ctxt, node); |
| 4110 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4111 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4112 | if (def == NULL) |
| 4113 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4114 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4115 | if (node->children != NULL) { |
| 4116 | if (ctxt->error != NULL) |
| 4117 | ctxt->error(ctxt->userData, "empty: had a child node\n"); |
| 4118 | ctxt->nbErrors++; |
| 4119 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4120 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4121 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4122 | if (def == NULL) |
| 4123 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4124 | def->type = XML_RELAXNG_TEXT; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4125 | if (node->children != NULL) { |
| 4126 | if (ctxt->error != NULL) |
| 4127 | ctxt->error(ctxt->userData, "text: had a child node\n"); |
| 4128 | ctxt->nbErrors++; |
| 4129 | } |
| 4130 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4131 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4132 | if (def == NULL) |
| 4133 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4134 | def->type = XML_RELAXNG_ZEROORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4135 | if (node->children == NULL) { |
| 4136 | if (ctxt->error != NULL) |
| 4137 | ctxt->error(ctxt->userData, |
| 4138 | "Element %s is empty\n", node->name); |
| 4139 | ctxt->nbErrors++; |
| 4140 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4141 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4142 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4143 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4144 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4145 | if (def == NULL) |
| 4146 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4147 | def->type = XML_RELAXNG_ONEORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4148 | if (node->children == NULL) { |
| 4149 | if (ctxt->error != NULL) |
| 4150 | ctxt->error(ctxt->userData, |
| 4151 | "Element %s is empty\n", node->name); |
| 4152 | ctxt->nbErrors++; |
| 4153 | } else { |
| 4154 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4155 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4156 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4157 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4158 | if (def == NULL) |
| 4159 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4160 | def->type = XML_RELAXNG_OPTIONAL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4161 | if (node->children == NULL) { |
| 4162 | if (ctxt->error != NULL) |
| 4163 | ctxt->error(ctxt->userData, |
| 4164 | "Element %s is empty\n", node->name); |
| 4165 | ctxt->nbErrors++; |
| 4166 | } else { |
| 4167 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4168 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4169 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4170 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4171 | if (def == NULL) |
| 4172 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4173 | def->type = XML_RELAXNG_CHOICE; |
| 4174 | if (node->children == NULL) { |
| 4175 | if (ctxt->error != NULL) |
| 4176 | ctxt->error(ctxt->userData, |
| 4177 | "Element %s is empty\n", node->name); |
| 4178 | ctxt->nbErrors++; |
| 4179 | } else { |
| 4180 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4181 | } |
| 4182 | } else if (IS_RELAXNG(node, "group")) { |
| 4183 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4184 | if (def == NULL) |
| 4185 | return(NULL); |
| 4186 | def->type = XML_RELAXNG_GROUP; |
| 4187 | if (node->children == NULL) { |
| 4188 | if (ctxt->error != NULL) |
| 4189 | ctxt->error(ctxt->userData, |
| 4190 | "Element %s is empty\n", node->name); |
| 4191 | ctxt->nbErrors++; |
| 4192 | } else { |
| 4193 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4194 | } |
| 4195 | } else if (IS_RELAXNG(node, "ref")) { |
| 4196 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4197 | if (def == NULL) |
| 4198 | return(NULL); |
| 4199 | def->type = XML_RELAXNG_REF; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4200 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4201 | if (def->name == NULL) { |
| 4202 | if (ctxt->error != NULL) |
| 4203 | ctxt->error(ctxt->userData, |
| 4204 | "ref has no name\n"); |
| 4205 | ctxt->nbErrors++; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4206 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4207 | xmlRelaxNGNormExtSpace(def->name); |
| 4208 | if (xmlValidateNCName(def->name, 0)) { |
| 4209 | if (ctxt->error != NULL) |
| 4210 | ctxt->error(ctxt->userData, |
| 4211 | "ref name '%s' is not an NCName\n", |
| 4212 | def->name); |
| 4213 | ctxt->nbErrors++; |
| 4214 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4215 | } |
| 4216 | if (node->children != NULL) { |
| 4217 | if (ctxt->error != NULL) |
| 4218 | ctxt->error(ctxt->userData, |
| 4219 | "ref is not empty\n"); |
| 4220 | ctxt->nbErrors++; |
| 4221 | } |
| 4222 | if (ctxt->grammar->refs == NULL) |
| 4223 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4224 | if (ctxt->grammar->refs == NULL) { |
| 4225 | if (ctxt->error != NULL) |
| 4226 | ctxt->error(ctxt->userData, |
| 4227 | "Could not create references hash\n"); |
| 4228 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4229 | def = NULL; |
| 4230 | } else { |
| 4231 | int tmp; |
| 4232 | |
| 4233 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4234 | if (tmp < 0) { |
| 4235 | xmlRelaxNGDefinePtr prev; |
| 4236 | |
| 4237 | prev = (xmlRelaxNGDefinePtr) |
| 4238 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4239 | if (prev == NULL) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4240 | if (def->name != NULL) { |
| 4241 | if (ctxt->error != NULL) |
| 4242 | ctxt->error(ctxt->userData, |
| 4243 | "Error refs definitions '%s'\n", |
| 4244 | def->name); |
| 4245 | } else { |
| 4246 | if (ctxt->error != NULL) |
| 4247 | ctxt->error(ctxt->userData, |
| 4248 | "Error refs definitions\n"); |
| 4249 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4250 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4251 | def = NULL; |
| 4252 | } else { |
| 4253 | def->nextHash = prev->nextHash; |
| 4254 | prev->nextHash = def; |
| 4255 | } |
| 4256 | } |
| 4257 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4258 | } else if (IS_RELAXNG(node, "data")) { |
| 4259 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4260 | } else if (IS_RELAXNG(node, "value")) { |
| 4261 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4262 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4263 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4264 | if (def == NULL) |
| 4265 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4266 | def->type = XML_RELAXNG_LIST; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4267 | if (node->children == NULL) { |
| 4268 | if (ctxt->error != NULL) |
| 4269 | ctxt->error(ctxt->userData, |
| 4270 | "Element %s is empty\n", node->name); |
| 4271 | ctxt->nbErrors++; |
| 4272 | } else { |
| 4273 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4274 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4275 | } else if (IS_RELAXNG(node, "interleave")) { |
| 4276 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4277 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4278 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4279 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4280 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4281 | if (def == NULL) |
| 4282 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4283 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4284 | if (node->children != NULL) { |
| 4285 | if (ctxt->error != NULL) |
| 4286 | ctxt->error(ctxt->userData, |
| 4287 | "xmlRelaxNGParse: notAllowed element is not empty\n"); |
| 4288 | ctxt->nbErrors++; |
| 4289 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4290 | } else if (IS_RELAXNG(node, "grammar")) { |
| 4291 | xmlRelaxNGGrammarPtr grammar, old; |
| 4292 | xmlRelaxNGGrammarPtr oldparent; |
| 4293 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4294 | #ifdef DEBUG_GRAMMAR |
| 4295 | xmlGenericError(xmlGenericErrorContext, "Found <grammar> pattern\n"); |
| 4296 | #endif |
| 4297 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4298 | oldparent = ctxt->parentgrammar; |
| 4299 | old = ctxt->grammar; |
| 4300 | ctxt->parentgrammar = old; |
| 4301 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4302 | if (old != NULL) { |
| 4303 | ctxt->grammar = old; |
| 4304 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4305 | #if 0 |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4306 | if (grammar != NULL) { |
| 4307 | grammar->next = old->next; |
| 4308 | old->next = grammar; |
| 4309 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4310 | #endif |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4311 | } |
| 4312 | if (grammar != NULL) |
| 4313 | def = grammar->start; |
| 4314 | else |
| 4315 | def = NULL; |
| 4316 | } else if (IS_RELAXNG(node, "parentRef")) { |
| 4317 | if (ctxt->parentgrammar == NULL) { |
| 4318 | if (ctxt->error != NULL) |
| 4319 | ctxt->error(ctxt->userData, |
| 4320 | "Use of parentRef without a parent grammar\n"); |
| 4321 | ctxt->nbErrors++; |
| 4322 | return(NULL); |
| 4323 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4324 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4325 | if (def == NULL) |
| 4326 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4327 | def->type = XML_RELAXNG_PARENTREF; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4328 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4329 | if (def->name == NULL) { |
| 4330 | if (ctxt->error != NULL) |
| 4331 | ctxt->error(ctxt->userData, |
| 4332 | "parentRef has no name\n"); |
| 4333 | ctxt->nbErrors++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4334 | } else { |
| 4335 | xmlRelaxNGNormExtSpace(def->name); |
| 4336 | if (xmlValidateNCName(def->name, 0)) { |
| 4337 | if (ctxt->error != NULL) |
| 4338 | ctxt->error(ctxt->userData, |
| 4339 | "parentRef name '%s' is not an NCName\n", |
| 4340 | def->name); |
| 4341 | ctxt->nbErrors++; |
| 4342 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4343 | } |
| 4344 | if (node->children != NULL) { |
| 4345 | if (ctxt->error != NULL) |
| 4346 | ctxt->error(ctxt->userData, |
| 4347 | "parentRef is not empty\n"); |
| 4348 | ctxt->nbErrors++; |
| 4349 | } |
| 4350 | if (ctxt->parentgrammar->refs == NULL) |
| 4351 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4352 | if (ctxt->parentgrammar->refs == NULL) { |
| 4353 | if (ctxt->error != NULL) |
| 4354 | ctxt->error(ctxt->userData, |
| 4355 | "Could not create references hash\n"); |
| 4356 | ctxt->nbErrors++; |
| 4357 | def = NULL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4358 | } else if (def->name != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4359 | int tmp; |
| 4360 | |
| 4361 | tmp = xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4362 | if (tmp < 0) { |
| 4363 | xmlRelaxNGDefinePtr prev; |
| 4364 | |
| 4365 | prev = (xmlRelaxNGDefinePtr) |
| 4366 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4367 | if (prev == NULL) { |
| 4368 | if (ctxt->error != NULL) |
| 4369 | ctxt->error(ctxt->userData, |
| 4370 | "Internal error parentRef definitions '%s'\n", |
| 4371 | def->name); |
| 4372 | ctxt->nbErrors++; |
| 4373 | def = NULL; |
| 4374 | } else { |
| 4375 | def->nextHash = prev->nextHash; |
| 4376 | prev->nextHash = def; |
| 4377 | } |
| 4378 | } |
| 4379 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4380 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4381 | if (node->children == NULL) { |
| 4382 | if (ctxt->error != NULL) |
| 4383 | ctxt->error(ctxt->userData, |
| 4384 | "Mixed is empty\n"); |
| 4385 | ctxt->nbErrors++; |
| 4386 | def = NULL; |
| 4387 | } else { |
| 4388 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4389 | if (def != NULL) { |
| 4390 | xmlRelaxNGDefinePtr tmp; |
| 4391 | |
| 4392 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4393 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4394 | if (tmp != NULL) { |
| 4395 | tmp->type = XML_RELAXNG_GROUP; |
| 4396 | tmp->content = def->content; |
| 4397 | def->content = tmp; |
| 4398 | } |
| 4399 | } |
| 4400 | |
| 4401 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4402 | if (tmp == NULL) |
| 4403 | return(def); |
| 4404 | tmp->type = XML_RELAXNG_TEXT; |
| 4405 | tmp->next = def->content; |
| 4406 | def->content = tmp; |
| 4407 | } |
| 4408 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4409 | } else { |
| 4410 | if (ctxt->error != NULL) |
| 4411 | ctxt->error(ctxt->userData, |
| 4412 | "Unexpected node %s is not a pattern\n", |
| 4413 | node->name); |
| 4414 | ctxt->nbErrors++; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4415 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4416 | } |
| 4417 | return(def); |
| 4418 | } |
| 4419 | |
| 4420 | /** |
| 4421 | * xmlRelaxNGParseAttribute: |
| 4422 | * @ctxt: a Relax-NG parser context |
| 4423 | * @node: the element node |
| 4424 | * |
| 4425 | * parse the content of a RelaxNG attribute node. |
| 4426 | * |
| 4427 | * Returns the definition pointer or NULL in case of error. |
| 4428 | */ |
| 4429 | static xmlRelaxNGDefinePtr |
| 4430 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4431 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4432 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4433 | int old_flags; |
| 4434 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4435 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4436 | if (ret == NULL) |
| 4437 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4438 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4439 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4440 | child = node->children; |
| 4441 | if (child == NULL) { |
| 4442 | if (ctxt->error != NULL) |
| 4443 | ctxt->error(ctxt->userData, |
| 4444 | "xmlRelaxNGParseattribute: attribute has no children\n"); |
| 4445 | ctxt->nbErrors++; |
| 4446 | return(ret); |
| 4447 | } |
| 4448 | old_flags = ctxt->flags; |
| 4449 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4450 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4451 | if (cur != NULL) |
| 4452 | child = child->next; |
| 4453 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4454 | if (child != NULL) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4455 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4456 | if (cur != NULL) { |
| 4457 | switch (cur->type) { |
| 4458 | case XML_RELAXNG_EMPTY: |
| 4459 | case XML_RELAXNG_NOT_ALLOWED: |
| 4460 | case XML_RELAXNG_TEXT: |
| 4461 | case XML_RELAXNG_ELEMENT: |
| 4462 | case XML_RELAXNG_DATATYPE: |
| 4463 | case XML_RELAXNG_VALUE: |
| 4464 | case XML_RELAXNG_LIST: |
| 4465 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4466 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4467 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4468 | case XML_RELAXNG_DEF: |
| 4469 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4470 | case XML_RELAXNG_ZEROORMORE: |
| 4471 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4472 | case XML_RELAXNG_CHOICE: |
| 4473 | case XML_RELAXNG_GROUP: |
| 4474 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4475 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4476 | ret->content = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4477 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4478 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4479 | case XML_RELAXNG_START: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 4480 | case XML_RELAXNG_PARAM: |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4481 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4482 | if (ctxt->error != NULL) |
| 4483 | ctxt->error(ctxt->userData, |
| 4484 | "attribute has invalid content\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 4485 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4486 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4487 | case XML_RELAXNG_NOOP: |
| 4488 | TODO |
| 4489 | if (ctxt->error != NULL) |
| 4490 | ctxt->error(ctxt->userData, |
| 4491 | "Internal error, noop found\n"); |
| 4492 | ctxt->nbErrors++; |
| 4493 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4494 | } |
| 4495 | } |
| 4496 | child = child->next; |
| 4497 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4498 | if (child != NULL) { |
| 4499 | if (ctxt->error != NULL) |
| 4500 | ctxt->error(ctxt->userData, "attribute has multiple children\n"); |
| 4501 | ctxt->nbErrors++; |
| 4502 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4503 | ctxt->flags = old_flags; |
| 4504 | return(ret); |
| 4505 | } |
| 4506 | |
| 4507 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4508 | * xmlRelaxNGParseExceptNameClass: |
| 4509 | * @ctxt: a Relax-NG parser context |
| 4510 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4511 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4512 | * |
| 4513 | * parse the content of a RelaxNG nameClass node. |
| 4514 | * |
| 4515 | * Returns the definition pointer or NULL in case of error. |
| 4516 | */ |
| 4517 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4518 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
| 4519 | xmlNodePtr node, int attr) { |
| 4520 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 4521 | xmlNodePtr child; |
| 4522 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4523 | if (!IS_RELAXNG(node, "except")) { |
| 4524 | if (ctxt->error != NULL) |
| 4525 | ctxt->error(ctxt->userData, |
| 4526 | "Expecting an except node\n"); |
| 4527 | ctxt->nbErrors++; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4528 | return(NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4529 | } |
| 4530 | if (node->next != NULL) { |
| 4531 | if (ctxt->error != NULL) |
| 4532 | ctxt->error(ctxt->userData, |
| 4533 | "exceptNameClass allows only a single except node\n"); |
| 4534 | ctxt->nbErrors++; |
| 4535 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4536 | if (node->children == NULL) { |
| 4537 | if (ctxt->error != NULL) |
| 4538 | ctxt->error(ctxt->userData, |
| 4539 | "except has no content\n"); |
| 4540 | ctxt->nbErrors++; |
| 4541 | return(NULL); |
| 4542 | } |
| 4543 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4544 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4545 | if (ret == NULL) |
| 4546 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4547 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4548 | child = node->children; |
| 4549 | while (child != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4550 | cur = xmlRelaxNGNewDefine(ctxt, child); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4551 | if (cur == NULL) |
| 4552 | break; |
| 4553 | if (attr) |
| 4554 | cur->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4555 | else |
| 4556 | cur->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4557 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4558 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4559 | if (last == NULL) { |
| 4560 | ret->content = cur; |
| 4561 | } else { |
| 4562 | last->next = cur; |
| 4563 | } |
| 4564 | last = cur; |
| 4565 | } |
| 4566 | child = child->next; |
| 4567 | } |
| 4568 | |
| 4569 | return(ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4570 | } |
| 4571 | |
| 4572 | /** |
| 4573 | * xmlRelaxNGParseNameClass: |
| 4574 | * @ctxt: a Relax-NG parser context |
| 4575 | * @node: the nameClass node |
| 4576 | * @def: the current definition |
| 4577 | * |
| 4578 | * parse the content of a RelaxNG nameClass node. |
| 4579 | * |
| 4580 | * Returns the definition pointer or NULL in case of error. |
| 4581 | */ |
| 4582 | static xmlRelaxNGDefinePtr |
| 4583 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 4584 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4585 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4586 | xmlChar *val; |
| 4587 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4588 | ret = def; |
| 4589 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
| 4590 | (IS_RELAXNG(node, "nsName"))) { |
| 4591 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 4592 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4593 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4594 | if (ret == NULL) |
| 4595 | return(NULL); |
| 4596 | ret->parent = def; |
| 4597 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 4598 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4599 | else |
| 4600 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4601 | } |
| 4602 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4603 | if (IS_RELAXNG(node, "name")) { |
| 4604 | val = xmlNodeGetContent(node); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4605 | xmlRelaxNGNormExtSpace(val); |
| 4606 | if (xmlValidateNCName(val, 0)) { |
| 4607 | if (ctxt->error != NULL) { |
| 4608 | if (node->parent != NULL) |
| 4609 | ctxt->error(ctxt->userData, |
| 4610 | "Element %s name '%s' is not an NCName\n", |
| 4611 | node->parent->name, val); |
| 4612 | else |
| 4613 | ctxt->error(ctxt->userData, |
| 4614 | "name '%s' is not an NCName\n", |
| 4615 | val); |
| 4616 | } |
| 4617 | ctxt->nbErrors++; |
| 4618 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4619 | ret->name = val; |
| 4620 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 4621 | ret->ns = val; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 4622 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 4623 | (val != NULL) && |
| 4624 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 4625 | ctxt->error(ctxt->userData, |
| 4626 | "Attribute with namespace '%s' is not allowed\n", |
| 4627 | val); |
| 4628 | ctxt->nbErrors++; |
| 4629 | } |
| 4630 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 4631 | (val != NULL) && |
| 4632 | (val[0] == 0) && |
| 4633 | (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
| 4634 | ctxt->error(ctxt->userData, |
| 4635 | "Attribute with QName 'xmlns' is not allowed\n", |
| 4636 | val); |
| 4637 | ctxt->nbErrors++; |
| 4638 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4639 | } else if (IS_RELAXNG(node, "anyName")) { |
| 4640 | ret->name = NULL; |
| 4641 | ret->ns = NULL; |
| 4642 | if (node->children != NULL) { |
| 4643 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4644 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 4645 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4646 | } |
| 4647 | } else if (IS_RELAXNG(node, "nsName")) { |
| 4648 | ret->name = NULL; |
| 4649 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 4650 | if (ret->ns == NULL) { |
| 4651 | if (ctxt->error != NULL) |
| 4652 | ctxt->error(ctxt->userData, |
| 4653 | "nsName has no ns attribute\n"); |
| 4654 | ctxt->nbErrors++; |
| 4655 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 4656 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 4657 | (ret->ns != NULL) && |
| 4658 | (xmlStrEqual(ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 4659 | ctxt->error(ctxt->userData, |
| 4660 | "Attribute with namespace '%s' is not allowed\n", |
| 4661 | ret->ns); |
| 4662 | ctxt->nbErrors++; |
| 4663 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4664 | if (node->children != NULL) { |
| 4665 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4666 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 4667 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4668 | } |
| 4669 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4670 | xmlNodePtr child; |
| 4671 | xmlRelaxNGDefinePtr last = NULL; |
| 4672 | |
| 4673 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 4674 | if (ret == NULL) |
| 4675 | return(NULL); |
| 4676 | ret->parent = def; |
| 4677 | ret->type = XML_RELAXNG_CHOICE; |
| 4678 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4679 | if (node->children == NULL) { |
| 4680 | if (ctxt->error != NULL) |
| 4681 | ctxt->error(ctxt->userData, |
| 4682 | "Element choice is empty\n"); |
| 4683 | ctxt->nbErrors++; |
| 4684 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4685 | |
| 4686 | child = node->children; |
| 4687 | while (child != NULL) { |
| 4688 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4689 | if (tmp != NULL) { |
| 4690 | if (last == NULL) { |
| 4691 | last = ret->nameClass = tmp; |
| 4692 | } else { |
| 4693 | last->next = tmp; |
| 4694 | last = tmp; |
| 4695 | } |
| 4696 | } |
| 4697 | child = child->next; |
| 4698 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4699 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4700 | } else { |
| 4701 | if (ctxt->error != NULL) |
| 4702 | ctxt->error(ctxt->userData, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4703 | "expecting name, anyName, nsName or choice : got %s\n", |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4704 | node->name); |
| 4705 | ctxt->nbErrors++; |
| 4706 | return(NULL); |
| 4707 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4708 | if (ret != def) { |
| 4709 | if (def->nameClass == NULL) { |
| 4710 | def->nameClass = ret; |
| 4711 | } else { |
| 4712 | tmp = def->nameClass; |
| 4713 | while (tmp->next != NULL) { |
| 4714 | tmp = tmp->next; |
| 4715 | } |
| 4716 | tmp->next = ret; |
| 4717 | } |
| 4718 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4719 | return(ret); |
| 4720 | } |
| 4721 | |
| 4722 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4723 | * xmlRelaxNGParseElement: |
| 4724 | * @ctxt: a Relax-NG parser context |
| 4725 | * @node: the element node |
| 4726 | * |
| 4727 | * parse the content of a RelaxNG element node. |
| 4728 | * |
| 4729 | * Returns the definition pointer or NULL in case of error. |
| 4730 | */ |
| 4731 | static xmlRelaxNGDefinePtr |
| 4732 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4733 | xmlRelaxNGDefinePtr ret, cur, last; |
| 4734 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4735 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4736 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4737 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4738 | if (ret == NULL) |
| 4739 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4740 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4741 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4742 | child = node->children; |
| 4743 | if (child == NULL) { |
| 4744 | if (ctxt->error != NULL) |
| 4745 | ctxt->error(ctxt->userData, |
| 4746 | "xmlRelaxNGParseElement: element has no children\n"); |
| 4747 | ctxt->nbErrors++; |
| 4748 | return(ret); |
| 4749 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4750 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4751 | if (cur != NULL) |
| 4752 | child = child->next; |
| 4753 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4754 | if (child == NULL) { |
| 4755 | if (ctxt->error != NULL) |
| 4756 | ctxt->error(ctxt->userData, |
| 4757 | "xmlRelaxNGParseElement: element has no content\n"); |
| 4758 | ctxt->nbErrors++; |
| 4759 | return(ret); |
| 4760 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4761 | olddefine = ctxt->define; |
| 4762 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4763 | last = NULL; |
| 4764 | while (child != NULL) { |
| 4765 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4766 | if (cur != NULL) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4767 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4768 | switch (cur->type) { |
| 4769 | case XML_RELAXNG_EMPTY: |
| 4770 | case XML_RELAXNG_NOT_ALLOWED: |
| 4771 | case XML_RELAXNG_TEXT: |
| 4772 | case XML_RELAXNG_ELEMENT: |
| 4773 | case XML_RELAXNG_DATATYPE: |
| 4774 | case XML_RELAXNG_VALUE: |
| 4775 | case XML_RELAXNG_LIST: |
| 4776 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4777 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4778 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4779 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4780 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4781 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4782 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4783 | case XML_RELAXNG_CHOICE: |
| 4784 | case XML_RELAXNG_GROUP: |
| 4785 | case XML_RELAXNG_INTERLEAVE: |
| 4786 | if (last == NULL) { |
| 4787 | ret->content = last = cur; |
| 4788 | } else { |
| 4789 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 4790 | (ret->content == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4791 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4792 | if (ret->content != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4793 | ret->content->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4794 | ret->content->content = last; |
| 4795 | } else { |
| 4796 | ret->content = last; |
| 4797 | } |
| 4798 | } |
| 4799 | last->next = cur; |
| 4800 | last = cur; |
| 4801 | } |
| 4802 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4803 | case XML_RELAXNG_ATTRIBUTE: |
| 4804 | cur->next = ret->attrs; |
| 4805 | ret->attrs = cur; |
| 4806 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4807 | case XML_RELAXNG_START: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 4808 | case XML_RELAXNG_PARAM: |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4809 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4810 | TODO |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 4811 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4812 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4813 | case XML_RELAXNG_NOOP: |
| 4814 | TODO |
| 4815 | if (ctxt->error != NULL) |
| 4816 | ctxt->error(ctxt->userData, |
| 4817 | "Internal error, noop found\n"); |
| 4818 | ctxt->nbErrors++; |
| 4819 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4820 | } |
| 4821 | } |
| 4822 | child = child->next; |
| 4823 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4824 | ctxt->define = olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4825 | return(ret); |
| 4826 | } |
| 4827 | |
| 4828 | /** |
| 4829 | * xmlRelaxNGParsePatterns: |
| 4830 | * @ctxt: a Relax-NG parser context |
| 4831 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4832 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4833 | * |
| 4834 | * parse the content of a RelaxNG start node. |
| 4835 | * |
| 4836 | * Returns the definition pointer or NULL in case of error. |
| 4837 | */ |
| 4838 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4839 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
| 4840 | int group) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4841 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4842 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4843 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4844 | while (nodes != NULL) { |
| 4845 | if (IS_RELAXNG(nodes, "element")) { |
| 4846 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 4847 | if (def == NULL) { |
| 4848 | def = last = cur; |
| 4849 | } else { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4850 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 4851 | (def == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4852 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 4853 | def->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4854 | def->content = last; |
| 4855 | } |
| 4856 | last->next = cur; |
| 4857 | last = cur; |
| 4858 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4859 | cur->parent = parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4860 | } else { |
| 4861 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4862 | if (cur != NULL) { |
| 4863 | if (def == NULL) { |
| 4864 | def = last = cur; |
| 4865 | } else { |
| 4866 | last->next = cur; |
| 4867 | last = cur; |
| 4868 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4869 | } |
| 4870 | } |
| 4871 | nodes = nodes->next; |
| 4872 | } |
| 4873 | return(def); |
| 4874 | } |
| 4875 | |
| 4876 | /** |
| 4877 | * xmlRelaxNGParseStart: |
| 4878 | * @ctxt: a Relax-NG parser context |
| 4879 | * @nodes: start children nodes |
| 4880 | * |
| 4881 | * parse the content of a RelaxNG start node. |
| 4882 | * |
| 4883 | * Returns 0 in case of success, -1 in case of error |
| 4884 | */ |
| 4885 | static int |
| 4886 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 4887 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 4888 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4889 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4890 | if (nodes == NULL) { |
| 4891 | if (ctxt->error != NULL) |
| 4892 | ctxt->error(ctxt->userData, |
| 4893 | "start has no children\n"); |
| 4894 | ctxt->nbErrors++; |
| 4895 | return(-1); |
| 4896 | } |
| 4897 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4898 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4899 | if (def == NULL) |
| 4900 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4901 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4902 | if (nodes->children != NULL) { |
| 4903 | if (ctxt->error != NULL) |
| 4904 | ctxt->error(ctxt->userData, "element empty is not empty\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 4905 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4906 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4907 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4908 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4909 | if (def == NULL) |
| 4910 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4911 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4912 | if (nodes->children != NULL) { |
| 4913 | if (ctxt->error != NULL) |
| 4914 | ctxt->error(ctxt->userData, |
| 4915 | "element notAllowed is not empty\n"); |
| 4916 | ctxt->nbErrors++; |
| 4917 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4918 | } else { |
| 4919 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 4920 | } |
| 4921 | if (ctxt->grammar->start != NULL) { |
| 4922 | last = ctxt->grammar->start; |
| 4923 | while (last->next != NULL) |
| 4924 | last = last->next; |
| 4925 | last->next = def; |
| 4926 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4927 | ctxt->grammar->start = def; |
| 4928 | } |
| 4929 | nodes = nodes->next; |
| 4930 | if (nodes != NULL) { |
| 4931 | if (ctxt->error != NULL) |
| 4932 | ctxt->error(ctxt->userData, |
| 4933 | "start more than one children\n"); |
| 4934 | ctxt->nbErrors++; |
| 4935 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4936 | } |
| 4937 | return(ret); |
| 4938 | } |
| 4939 | |
| 4940 | /** |
| 4941 | * xmlRelaxNGParseGrammarContent: |
| 4942 | * @ctxt: a Relax-NG parser context |
| 4943 | * @nodes: grammar children nodes |
| 4944 | * |
| 4945 | * parse the content of a RelaxNG grammar node. |
| 4946 | * |
| 4947 | * Returns 0 in case of success, -1 in case of error |
| 4948 | */ |
| 4949 | static int |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4950 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4951 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4952 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4953 | |
| 4954 | if (nodes == NULL) { |
| 4955 | if (ctxt->error != NULL) |
| 4956 | ctxt->error(ctxt->userData, |
| 4957 | "grammar has no children\n"); |
| 4958 | ctxt->nbErrors++; |
| 4959 | return(-1); |
| 4960 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4961 | while (nodes != NULL) { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4962 | if (IS_RELAXNG(nodes, "start")) { |
| 4963 | if (nodes->children == NULL) { |
| 4964 | if (ctxt->error != NULL) |
| 4965 | ctxt->error(ctxt->userData, |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4966 | "start has no children\n"); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4967 | ctxt->nbErrors++; |
| 4968 | } else { |
| 4969 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 4970 | if (tmp != 0) |
| 4971 | ret = -1; |
| 4972 | } |
| 4973 | } else if (IS_RELAXNG(nodes, "define")) { |
| 4974 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 4975 | if (tmp != 0) |
| 4976 | ret = -1; |
| 4977 | } else if (IS_RELAXNG(nodes, "include")) { |
| 4978 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 4979 | if (tmp != 0) |
| 4980 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4981 | } else { |
| 4982 | if (ctxt->error != NULL) |
| 4983 | ctxt->error(ctxt->userData, |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4984 | "grammar has unexpected child %s\n", nodes->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4985 | ctxt->nbErrors++; |
| 4986 | ret = -1; |
| 4987 | } |
| 4988 | nodes = nodes->next; |
| 4989 | } |
| 4990 | return (ret); |
| 4991 | } |
| 4992 | |
| 4993 | /** |
| 4994 | * xmlRelaxNGCheckReference: |
| 4995 | * @ref: the ref |
| 4996 | * @ctxt: a Relax-NG parser context |
| 4997 | * @name: the name associated to the defines |
| 4998 | * |
| 4999 | * Applies the 4.17. combine attribute rule for all the define |
| 5000 | * element of a given grammar using the same name. |
| 5001 | */ |
| 5002 | static void |
| 5003 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
| 5004 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5005 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5006 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5007 | |
| 5008 | grammar = ctxt->grammar; |
| 5009 | if (grammar == NULL) { |
| 5010 | if (ctxt->error != NULL) |
| 5011 | ctxt->error(ctxt->userData, |
| 5012 | "Internal error: no grammar in CheckReference %s\n", |
| 5013 | name); |
| 5014 | ctxt->nbErrors++; |
| 5015 | return; |
| 5016 | } |
| 5017 | if (ref->content != NULL) { |
| 5018 | if (ctxt->error != NULL) |
| 5019 | ctxt->error(ctxt->userData, |
| 5020 | "Internal error: reference has content in CheckReference %s\n", |
| 5021 | name); |
| 5022 | ctxt->nbErrors++; |
| 5023 | return; |
| 5024 | } |
| 5025 | if (grammar->defs != NULL) { |
| 5026 | def = xmlHashLookup(grammar->defs, name); |
| 5027 | if (def != NULL) { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5028 | cur = ref; |
| 5029 | while (cur != NULL) { |
| 5030 | cur->content = def; |
| 5031 | cur = cur->nextHash; |
| 5032 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5033 | } else { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5034 | if (ctxt->error != NULL) |
| 5035 | ctxt->error(ctxt->userData, |
| 5036 | "Reference %s has no matching definition\n", |
| 5037 | name); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5038 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5039 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5040 | } else { |
| 5041 | if (ctxt->error != NULL) |
| 5042 | ctxt->error(ctxt->userData, |
| 5043 | "Reference %s has no matching definition\n", |
| 5044 | name); |
| 5045 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5046 | } |
| 5047 | /* |
| 5048 | * TODO: make a closure and verify there is no loop ! |
| 5049 | */ |
| 5050 | } |
| 5051 | |
| 5052 | /** |
| 5053 | * xmlRelaxNGCheckCombine: |
| 5054 | * @define: the define(s) list |
| 5055 | * @ctxt: a Relax-NG parser context |
| 5056 | * @name: the name associated to the defines |
| 5057 | * |
| 5058 | * Applies the 4.17. combine attribute rule for all the define |
| 5059 | * element of a given grammar using the same name. |
| 5060 | */ |
| 5061 | static void |
| 5062 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
| 5063 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5064 | xmlChar *combine; |
| 5065 | int choiceOrInterleave = -1; |
| 5066 | int missing = 0; |
| 5067 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5068 | |
| 5069 | if (define->nextHash == NULL) |
| 5070 | return; |
| 5071 | cur = define; |
| 5072 | while (cur != NULL) { |
| 5073 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5074 | if (combine != NULL) { |
| 5075 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5076 | if (choiceOrInterleave == -1) |
| 5077 | choiceOrInterleave = 1; |
| 5078 | else if (choiceOrInterleave == 0) { |
| 5079 | if (ctxt->error != NULL) |
| 5080 | ctxt->error(ctxt->userData, |
| 5081 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5082 | name); |
| 5083 | ctxt->nbErrors++; |
| 5084 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5085 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5086 | if (choiceOrInterleave == -1) |
| 5087 | choiceOrInterleave = 0; |
| 5088 | else if (choiceOrInterleave == 1) { |
| 5089 | if (ctxt->error != NULL) |
| 5090 | ctxt->error(ctxt->userData, |
| 5091 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5092 | name); |
| 5093 | ctxt->nbErrors++; |
| 5094 | } |
| 5095 | } else { |
| 5096 | if (ctxt->error != NULL) |
| 5097 | ctxt->error(ctxt->userData, |
| 5098 | "Defines for %s use unknown combine value '%s''\n", |
| 5099 | name, combine); |
| 5100 | ctxt->nbErrors++; |
| 5101 | } |
| 5102 | xmlFree(combine); |
| 5103 | } else { |
| 5104 | if (missing == 0) |
| 5105 | missing = 1; |
| 5106 | else { |
| 5107 | if (ctxt->error != NULL) |
| 5108 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5109 | "Some defines for %s needs the combine attribute\n", |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5110 | name); |
| 5111 | ctxt->nbErrors++; |
| 5112 | } |
| 5113 | } |
| 5114 | |
| 5115 | cur = cur->nextHash; |
| 5116 | } |
| 5117 | #ifdef DEBUG |
| 5118 | xmlGenericError(xmlGenericErrorContext, |
| 5119 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5120 | name, choiceOrInterleave); |
| 5121 | #endif |
| 5122 | if (choiceOrInterleave == -1) |
| 5123 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5124 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5125 | if (cur == NULL) |
| 5126 | return; |
| 5127 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5128 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5129 | else |
| 5130 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5131 | tmp = define; |
| 5132 | last = NULL; |
| 5133 | while (tmp != NULL) { |
| 5134 | if (tmp->content != NULL) { |
| 5135 | if (tmp->content->next != NULL) { |
| 5136 | /* |
| 5137 | * we need first to create a wrapper. |
| 5138 | */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5139 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5140 | if (tmp2 == NULL) |
| 5141 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5142 | tmp2->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5143 | tmp2->content = tmp->content; |
| 5144 | } else { |
| 5145 | tmp2 = tmp->content; |
| 5146 | } |
| 5147 | if (last == NULL) { |
| 5148 | cur->content = tmp2; |
| 5149 | } else { |
| 5150 | last->next = tmp2; |
| 5151 | } |
| 5152 | last = tmp2; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5153 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 5154 | tmp->content = cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5155 | tmp = tmp->nextHash; |
| 5156 | } |
| 5157 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5158 | if (choiceOrInterleave == 0) { |
| 5159 | if (ctxt->interleaves == NULL) |
| 5160 | ctxt->interleaves = xmlHashCreate(10); |
| 5161 | if (ctxt->interleaves == NULL) { |
| 5162 | if (ctxt->error != NULL) |
| 5163 | ctxt->error(ctxt->userData, |
| 5164 | "Failed to create interleaves hash table\n"); |
| 5165 | ctxt->nbErrors++; |
| 5166 | } else { |
| 5167 | char tmpname[32]; |
| 5168 | |
| 5169 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5170 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5171 | if (ctxt->error != NULL) |
| 5172 | ctxt->error(ctxt->userData, |
| 5173 | "Failed to add %s to hash table\n", tmpname); |
| 5174 | ctxt->nbErrors++; |
| 5175 | } |
| 5176 | } |
| 5177 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5178 | } |
| 5179 | |
| 5180 | /** |
| 5181 | * xmlRelaxNGCombineStart: |
| 5182 | * @ctxt: a Relax-NG parser context |
| 5183 | * @grammar: the grammar |
| 5184 | * |
| 5185 | * Applies the 4.17. combine rule for all the start |
| 5186 | * element of a given grammar. |
| 5187 | */ |
| 5188 | static void |
| 5189 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
| 5190 | xmlRelaxNGGrammarPtr grammar) { |
| 5191 | xmlRelaxNGDefinePtr starts; |
| 5192 | xmlChar *combine; |
| 5193 | int choiceOrInterleave = -1; |
| 5194 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5195 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5196 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5197 | starts = grammar->start; |
| 5198 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5199 | return; |
| 5200 | cur = starts; |
| 5201 | while (cur != NULL) { |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5202 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5203 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5204 | combine = NULL; |
| 5205 | if (ctxt->error != NULL) |
| 5206 | ctxt->error(ctxt->userData, |
| 5207 | "Internal error: start element not found\n"); |
| 5208 | ctxt->nbErrors++; |
| 5209 | } else { |
| 5210 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5211 | } |
| 5212 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5213 | if (combine != NULL) { |
| 5214 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5215 | if (choiceOrInterleave == -1) |
| 5216 | choiceOrInterleave = 1; |
| 5217 | else if (choiceOrInterleave == 0) { |
| 5218 | if (ctxt->error != NULL) |
| 5219 | ctxt->error(ctxt->userData, |
| 5220 | "<start> use both 'choice' and 'interleave'\n"); |
| 5221 | ctxt->nbErrors++; |
| 5222 | } |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5223 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5224 | if (choiceOrInterleave == -1) |
| 5225 | choiceOrInterleave = 0; |
| 5226 | else if (choiceOrInterleave == 1) { |
| 5227 | if (ctxt->error != NULL) |
| 5228 | ctxt->error(ctxt->userData, |
| 5229 | "<start> use both 'choice' and 'interleave'\n"); |
| 5230 | ctxt->nbErrors++; |
| 5231 | } |
| 5232 | } else { |
| 5233 | if (ctxt->error != NULL) |
| 5234 | ctxt->error(ctxt->userData, |
| 5235 | "<start> uses unknown combine value '%s''\n", combine); |
| 5236 | ctxt->nbErrors++; |
| 5237 | } |
| 5238 | xmlFree(combine); |
| 5239 | } else { |
| 5240 | if (missing == 0) |
| 5241 | missing = 1; |
| 5242 | else { |
| 5243 | if (ctxt->error != NULL) |
| 5244 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5245 | "Some <start> element miss the combine attribute\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5246 | ctxt->nbErrors++; |
| 5247 | } |
| 5248 | } |
| 5249 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5250 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5251 | } |
| 5252 | #ifdef DEBUG |
| 5253 | xmlGenericError(xmlGenericErrorContext, |
| 5254 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5255 | choiceOrInterleave); |
| 5256 | #endif |
| 5257 | if (choiceOrInterleave == -1) |
| 5258 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5259 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5260 | if (cur == NULL) |
| 5261 | return; |
| 5262 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5263 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5264 | else |
| 5265 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5266 | cur->content = grammar->start; |
| 5267 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5268 | if (choiceOrInterleave == 0) { |
| 5269 | if (ctxt->interleaves == NULL) |
| 5270 | ctxt->interleaves = xmlHashCreate(10); |
| 5271 | if (ctxt->interleaves == NULL) { |
| 5272 | if (ctxt->error != NULL) |
| 5273 | ctxt->error(ctxt->userData, |
| 5274 | "Failed to create interleaves hash table\n"); |
| 5275 | ctxt->nbErrors++; |
| 5276 | } else { |
| 5277 | char tmpname[32]; |
| 5278 | |
| 5279 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5280 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5281 | if (ctxt->error != NULL) |
| 5282 | ctxt->error(ctxt->userData, |
| 5283 | "Failed to add %s to hash table\n", tmpname); |
| 5284 | ctxt->nbErrors++; |
| 5285 | } |
| 5286 | } |
| 5287 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5288 | } |
| 5289 | |
| 5290 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5291 | * xmlRelaxNGCheckCycles: |
| 5292 | * @ctxt: a Relax-NG parser context |
| 5293 | * @nodes: grammar children nodes |
| 5294 | * @depth: the counter |
| 5295 | * |
| 5296 | * Check for cycles. |
| 5297 | * |
| 5298 | * Returns 0 if check passed, and -1 in case of error |
| 5299 | */ |
| 5300 | static int |
| 5301 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5302 | xmlRelaxNGDefinePtr cur, int depth) { |
| 5303 | int ret = 0; |
| 5304 | |
| 5305 | while ((ret == 0) && (cur != NULL)) { |
| 5306 | if ((cur->type == XML_RELAXNG_REF) || |
| 5307 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5308 | if (cur->depth == -1) { |
| 5309 | cur->depth = depth; |
| 5310 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5311 | cur->depth = -2; |
| 5312 | } else if (depth == cur->depth) { |
| 5313 | if (ctxt->error != NULL) |
| 5314 | ctxt->error(ctxt->userData, |
| 5315 | "Detected a cycle in %s references\n", cur->name); |
| 5316 | ctxt->nbErrors++; |
| 5317 | return(-1); |
| 5318 | } |
| 5319 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5320 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5321 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5322 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5323 | } |
| 5324 | cur = cur->next; |
| 5325 | } |
| 5326 | return(ret); |
| 5327 | } |
| 5328 | |
| 5329 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5330 | * xmlRelaxNGTryUnlink: |
| 5331 | * @ctxt: a Relax-NG parser context |
| 5332 | * @cur: the definition to unlink |
| 5333 | * @parent: the parent definition |
| 5334 | * @prev: the previous sibling definition |
| 5335 | * |
| 5336 | * Try to unlink a definition. If not possble make it a NOOP |
| 5337 | * |
| 5338 | * Returns the new prev definition |
| 5339 | */ |
| 5340 | static xmlRelaxNGDefinePtr |
| 5341 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5342 | xmlRelaxNGDefinePtr cur, |
| 5343 | xmlRelaxNGDefinePtr parent, |
| 5344 | xmlRelaxNGDefinePtr prev) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5345 | if (prev != NULL) { |
| 5346 | prev->next = cur->next; |
| 5347 | } else { |
| 5348 | if (parent != NULL) { |
| 5349 | if (parent->content == cur) |
| 5350 | parent->content = cur->next; |
| 5351 | else if (parent->attrs == cur) |
| 5352 | parent->attrs = cur->next; |
| 5353 | else if (parent->nameClass == cur) |
| 5354 | parent->nameClass = cur->next; |
| 5355 | } else { |
| 5356 | cur->type = XML_RELAXNG_NOOP; |
| 5357 | prev = cur; |
| 5358 | } |
| 5359 | } |
| 5360 | return(prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5361 | } |
| 5362 | |
| 5363 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5364 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5365 | * @ctxt: a Relax-NG parser context |
| 5366 | * @nodes: grammar children nodes |
| 5367 | * |
| 5368 | * Check for simplification of empty and notAllowed |
| 5369 | */ |
| 5370 | static void |
| 5371 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5372 | xmlRelaxNGDefinePtr cur, |
| 5373 | xmlRelaxNGDefinePtr parent) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5374 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5375 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5376 | while (cur != NULL) { |
| 5377 | if ((cur->type == XML_RELAXNG_REF) || |
| 5378 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5379 | if (cur->depth != -3) { |
| 5380 | cur->depth = -3; |
| 5381 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5382 | } |
| 5383 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5384 | cur->parent = parent; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5385 | if ((parent != NULL) && |
| 5386 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5387 | (parent->type == XML_RELAXNG_LIST) || |
| 5388 | (parent->type == XML_RELAXNG_GROUP) || |
| 5389 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5390 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5391 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5392 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5393 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5394 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5395 | if ((parent != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5396 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5397 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5398 | } else |
| 5399 | prev = cur; |
| 5400 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5401 | cur->parent = parent; |
| 5402 | if ((parent != NULL) && |
| 5403 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5404 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5405 | parent->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5406 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5407 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5408 | if ((parent != NULL) && |
| 5409 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5410 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5411 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5412 | } else |
| 5413 | prev = cur; |
| 5414 | } else { |
| 5415 | cur->parent = parent; |
| 5416 | if (cur->content != NULL) |
| 5417 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5418 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5419 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5420 | if (cur->nameClass != NULL) |
| 5421 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5422 | /* |
| 5423 | * This may result in a simplification |
| 5424 | */ |
| 5425 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 5426 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 5427 | if (cur->content == NULL) |
| 5428 | cur->type = XML_RELAXNG_EMPTY; |
| 5429 | else if (cur->content->next == NULL) { |
| 5430 | if ((parent == NULL) && (prev == NULL)) { |
| 5431 | cur->type = XML_RELAXNG_NOOP; |
| 5432 | } else if (prev == NULL) { |
| 5433 | parent->content = cur->content; |
| 5434 | cur->content->next = cur->next; |
| 5435 | cur = cur->content; |
| 5436 | } else { |
| 5437 | cur->content->next = cur->next; |
| 5438 | prev->next = cur->content; |
| 5439 | cur = cur->content; |
| 5440 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5441 | } |
| 5442 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5443 | /* |
| 5444 | * the current node may have been transformed back |
| 5445 | */ |
| 5446 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 5447 | (cur->content != NULL) && |
| 5448 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 5449 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5450 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5451 | if ((parent != NULL) && |
| 5452 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5453 | (parent->type == XML_RELAXNG_LIST) || |
| 5454 | (parent->type == XML_RELAXNG_GROUP) || |
| 5455 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5456 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5457 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5458 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5459 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5460 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5461 | if ((parent != NULL) && |
| 5462 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5463 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5464 | } else |
| 5465 | prev = cur; |
| 5466 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5467 | if ((parent != NULL) && |
| 5468 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5469 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5470 | parent->type = XML_RELAXNG_EMPTY; |
| 5471 | break; |
| 5472 | } |
| 5473 | if ((parent != NULL) && |
| 5474 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5475 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5476 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 5477 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5478 | } else |
| 5479 | prev = cur; |
| 5480 | } else { |
| 5481 | prev = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5482 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5483 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5484 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5485 | } |
| 5486 | } |
| 5487 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5488 | /** |
| 5489 | * xmlRelaxNGGroupContentType: |
| 5490 | * @ct1: the first content type |
| 5491 | * @ct2: the second content type |
| 5492 | * |
| 5493 | * Try to group 2 content types |
| 5494 | * |
| 5495 | * Returns the content type |
| 5496 | */ |
| 5497 | static xmlRelaxNGContentType |
| 5498 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
| 5499 | xmlRelaxNGContentType ct2) { |
| 5500 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 5501 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 5502 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5503 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
| 5504 | return(ct2); |
| 5505 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
| 5506 | return(ct1); |
| 5507 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
| 5508 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 5509 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 5510 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5511 | } |
| 5512 | |
| 5513 | /** |
| 5514 | * xmlRelaxNGMaxContentType: |
| 5515 | * @ct1: the first content type |
| 5516 | * @ct2: the second content type |
| 5517 | * |
| 5518 | * Compute the max content-type |
| 5519 | * |
| 5520 | * Returns the content type |
| 5521 | */ |
| 5522 | static xmlRelaxNGContentType |
| 5523 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
| 5524 | xmlRelaxNGContentType ct2) { |
| 5525 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 5526 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 5527 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5528 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
| 5529 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 5530 | return(XML_RELAXNG_CONTENT_SIMPLE); |
| 5531 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
| 5532 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 5533 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 5534 | return(XML_RELAXNG_CONTENT_EMPTY); |
| 5535 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5536 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5537 | /** |
| 5538 | * xmlRelaxNGCheckRules: |
| 5539 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5540 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5541 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5542 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5543 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5544 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5545 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5546 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5547 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5548 | static xmlRelaxNGContentType |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5549 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 5550 | xmlRelaxNGDefinePtr cur, int flags, |
| 5551 | xmlRelaxNGType ptype) { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5552 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5553 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5554 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5555 | while (cur != NULL) { |
| 5556 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 5557 | if ((cur->type == XML_RELAXNG_REF) || |
| 5558 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5559 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5560 | if (ctxt->error != NULL) |
| 5561 | ctxt->error(ctxt->userData, |
| 5562 | "Found forbidden pattern list//ref\n"); |
| 5563 | ctxt->nbErrors++; |
| 5564 | } |
| 5565 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5566 | if (ctxt->error != NULL) |
| 5567 | ctxt->error(ctxt->userData, |
| 5568 | "Found forbidden pattern data/except//ref\n"); |
| 5569 | ctxt->nbErrors++; |
| 5570 | } |
| 5571 | if (cur->depth > -4) { |
| 5572 | cur->depth = -4; |
| 5573 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 5574 | flags, cur->type); |
| 5575 | cur->depth = ret - 15 ; |
| 5576 | } else if (cur->depth == -4) { |
| 5577 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 5578 | } else { |
| 5579 | ret = (xmlRelaxNGContentType) cur->depth + 15; |
| 5580 | } |
| 5581 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5582 | /* |
| 5583 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 5584 | */ |
| 5585 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 5586 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5587 | if (ctxt->error != NULL) |
| 5588 | ctxt->error(ctxt->userData, |
| 5589 | "Found forbidden pattern data/except//element(ref)\n"); |
| 5590 | ctxt->nbErrors++; |
| 5591 | } |
| 5592 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5593 | if (ctxt->error != NULL) |
| 5594 | ctxt->error(ctxt->userData, |
| 5595 | "Found forbidden pattern list//element(ref)\n"); |
| 5596 | ctxt->nbErrors++; |
| 5597 | } |
| 5598 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 5599 | if (ctxt->error != NULL) |
| 5600 | ctxt->error(ctxt->userData, |
| 5601 | "Found forbidden pattern attribute//element(ref)\n"); |
| 5602 | ctxt->nbErrors++; |
| 5603 | } |
| 5604 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 5605 | if (ctxt->error != NULL) |
| 5606 | ctxt->error(ctxt->userData, |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 5607 | "Found forbidden pattern attribute//element(ref)\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5608 | ctxt->nbErrors++; |
| 5609 | } |
| 5610 | /* |
| 5611 | * reset since in the simple form elements are only child |
| 5612 | * of grammar/define |
| 5613 | */ |
| 5614 | nflags = 0; |
| 5615 | ret = xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 5616 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 5617 | if (ctxt->error != NULL) |
| 5618 | ctxt->error(ctxt->userData, |
| 5619 | "Element %s attributes have a content type error\n", |
| 5620 | cur->name); |
| 5621 | ctxt->nbErrors++; |
| 5622 | } |
| 5623 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5624 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 5625 | if (ctxt->error != NULL) |
| 5626 | ctxt->error(ctxt->userData, |
| 5627 | "Element %s has a content type error\n", |
| 5628 | cur->name); |
| 5629 | ctxt->nbErrors++; |
| 5630 | } else { |
| 5631 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 5632 | } |
| 5633 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 5634 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 5635 | if (ctxt->error != NULL) |
| 5636 | ctxt->error(ctxt->userData, |
| 5637 | "Found forbidden pattern attribute//attribute\n"); |
| 5638 | ctxt->nbErrors++; |
| 5639 | } |
| 5640 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5641 | if (ctxt->error != NULL) |
| 5642 | ctxt->error(ctxt->userData, |
| 5643 | "Found forbidden pattern list//attribute\n"); |
| 5644 | ctxt->nbErrors++; |
| 5645 | } |
| 5646 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 5647 | if (ctxt->error != NULL) |
| 5648 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5649 | "Found forbidden pattern oneOrMore//group//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5650 | ctxt->nbErrors++; |
| 5651 | } |
| 5652 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 5653 | if (ctxt->error != NULL) |
| 5654 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5655 | "Found forbidden pattern oneOrMore//interleave//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5656 | ctxt->nbErrors++; |
| 5657 | } |
| 5658 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5659 | if (ctxt->error != NULL) |
| 5660 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5661 | "Found forbidden pattern data/except//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5662 | ctxt->nbErrors++; |
| 5663 | } |
| 5664 | if (flags & XML_RELAXNG_IN_START) { |
| 5665 | if (ctxt->error != NULL) |
| 5666 | ctxt->error(ctxt->userData, |
| 5667 | "Found forbidden pattern start//attribute\n"); |
| 5668 | ctxt->nbErrors++; |
| 5669 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5670 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) && (cur->name == NULL)) { |
| 5671 | if (cur->ns == NULL) { |
| 5672 | if (ctxt->error != NULL) |
| 5673 | ctxt->error(ctxt->userData, |
| 5674 | "Found anyName attribute without oneOrMore ancestor\n"); |
| 5675 | ctxt->nbErrors++; |
| 5676 | } else { |
| 5677 | if (ctxt->error != NULL) |
| 5678 | ctxt->error(ctxt->userData, |
| 5679 | "Found nsName attribute without oneOrMore ancestor\n"); |
| 5680 | ctxt->nbErrors++; |
| 5681 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5682 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5683 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 5684 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5685 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 5686 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 5687 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 5688 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5689 | if (ctxt->error != NULL) |
| 5690 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5691 | "Found forbidden pattern data/except//oneOrMore\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5692 | ctxt->nbErrors++; |
| 5693 | } |
| 5694 | if (flags & XML_RELAXNG_IN_START) { |
| 5695 | if (ctxt->error != NULL) |
| 5696 | ctxt->error(ctxt->userData, |
| 5697 | "Found forbidden pattern start//oneOrMore\n"); |
| 5698 | ctxt->nbErrors++; |
| 5699 | } |
| 5700 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 5701 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5702 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 5703 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 5704 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5705 | if (ctxt->error != NULL) |
| 5706 | ctxt->error(ctxt->userData, |
| 5707 | "Found forbidden pattern list//list\n"); |
| 5708 | ctxt->nbErrors++; |
| 5709 | } |
| 5710 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5711 | if (ctxt->error != NULL) |
| 5712 | ctxt->error(ctxt->userData, |
| 5713 | "Found forbidden pattern data/except//list\n"); |
| 5714 | ctxt->nbErrors++; |
| 5715 | } |
| 5716 | if (flags & XML_RELAXNG_IN_START) { |
| 5717 | if (ctxt->error != NULL) |
| 5718 | ctxt->error(ctxt->userData, |
| 5719 | "Found forbidden pattern start//list\n"); |
| 5720 | ctxt->nbErrors++; |
| 5721 | } |
| 5722 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 5723 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5724 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 5725 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5726 | if (ctxt->error != NULL) |
| 5727 | ctxt->error(ctxt->userData, |
| 5728 | "Found forbidden pattern data/except//group\n"); |
| 5729 | ctxt->nbErrors++; |
| 5730 | } |
| 5731 | if (flags & XML_RELAXNG_IN_START) { |
| 5732 | if (ctxt->error != NULL) |
| 5733 | ctxt->error(ctxt->userData, |
| 5734 | "Found forbidden pattern start//group\n"); |
| 5735 | ctxt->nbErrors++; |
| 5736 | } |
| 5737 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 5738 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 5739 | else |
| 5740 | nflags = flags; |
| 5741 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5742 | /* |
| 5743 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 5744 | */ |
| 5745 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 5746 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 5747 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5748 | if (ctxt->error != NULL) |
| 5749 | ctxt->error(ctxt->userData, |
| 5750 | "Found forbidden pattern list//interleave\n"); |
| 5751 | ctxt->nbErrors++; |
| 5752 | } |
| 5753 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5754 | if (ctxt->error != NULL) |
| 5755 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5756 | "Found forbidden pattern data/except//interleave\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5757 | ctxt->nbErrors++; |
| 5758 | } |
| 5759 | if (flags & XML_RELAXNG_IN_START) { |
| 5760 | if (ctxt->error != NULL) |
| 5761 | ctxt->error(ctxt->userData, |
| 5762 | "Found forbidden pattern start//interleave\n"); |
| 5763 | ctxt->nbErrors++; |
| 5764 | } |
| 5765 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 5766 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 5767 | else |
| 5768 | nflags = flags; |
| 5769 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5770 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 5771 | if ((cur->parent != NULL) && |
| 5772 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 5773 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 5774 | else |
| 5775 | nflags = flags; |
| 5776 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5777 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 5778 | if (flags & XML_RELAXNG_IN_START) { |
| 5779 | if (ctxt->error != NULL) |
| 5780 | ctxt->error(ctxt->userData, |
| 5781 | "Found forbidden pattern start//data\n"); |
| 5782 | ctxt->nbErrors++; |
| 5783 | } |
| 5784 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 5785 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 5786 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 5787 | if (flags & XML_RELAXNG_IN_START) { |
| 5788 | if (ctxt->error != NULL) |
| 5789 | ctxt->error(ctxt->userData, |
| 5790 | "Found forbidden pattern start//value\n"); |
| 5791 | ctxt->nbErrors++; |
| 5792 | } |
| 5793 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 5794 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 5795 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 5796 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5797 | if (ctxt->error != NULL) |
| 5798 | ctxt->error(ctxt->userData, |
| 5799 | "Found forbidden pattern list//text\n"); |
| 5800 | ctxt->nbErrors++; |
| 5801 | } |
| 5802 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5803 | if (ctxt->error != NULL) |
| 5804 | ctxt->error(ctxt->userData, |
| 5805 | "Found forbidden pattern data/except//text\n"); |
| 5806 | ctxt->nbErrors++; |
| 5807 | } |
| 5808 | if (flags & XML_RELAXNG_IN_START) { |
| 5809 | if (ctxt->error != NULL) |
| 5810 | ctxt->error(ctxt->userData, |
| 5811 | "Found forbidden pattern start//text\n"); |
| 5812 | ctxt->nbErrors++; |
| 5813 | } |
| 5814 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 5815 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 5816 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5817 | if (ctxt->error != NULL) |
| 5818 | ctxt->error(ctxt->userData, |
| 5819 | "Found forbidden pattern data/except//empty\n"); |
| 5820 | ctxt->nbErrors++; |
| 5821 | } |
| 5822 | if (flags & XML_RELAXNG_IN_START) { |
| 5823 | if (ctxt->error != NULL) |
| 5824 | ctxt->error(ctxt->userData, |
| 5825 | "Found forbidden pattern start//empty\n"); |
| 5826 | ctxt->nbErrors++; |
| 5827 | } |
| 5828 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 5829 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 5830 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 5831 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 5832 | } else { |
| 5833 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 5834 | } |
| 5835 | cur = cur->next; |
| 5836 | if (ptype == XML_RELAXNG_GROUP) { |
| 5837 | val = xmlRelaxNGGroupContentType(val, ret); |
| 5838 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 5839 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 5840 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 5841 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 5842 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 5843 | val = xmlRelaxNGMaxContentType(val, ret); |
| 5844 | } else if (ptype == XML_RELAXNG_LIST) { |
| 5845 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 5846 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 5847 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 5848 | val = XML_RELAXNG_CONTENT_ERROR; |
| 5849 | else |
| 5850 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 5851 | } else { |
| 5852 | val = xmlRelaxNGGroupContentType(val, ret); |
| 5853 | } |
| 5854 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5855 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5856 | return(val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5857 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5858 | |
| 5859 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5860 | * xmlRelaxNGParseGrammar: |
| 5861 | * @ctxt: a Relax-NG parser context |
| 5862 | * @nodes: grammar children nodes |
| 5863 | * |
| 5864 | * parse a Relax-NG <grammar> node |
| 5865 | * |
| 5866 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 5867 | * NULL in case of error |
| 5868 | */ |
| 5869 | static xmlRelaxNGGrammarPtr |
| 5870 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 5871 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 5872 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5873 | #ifdef DEBUG_GRAMMAR |
| 5874 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 5875 | #endif |
| 5876 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5877 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 5878 | if (ret == NULL) |
| 5879 | return(NULL); |
| 5880 | |
| 5881 | /* |
| 5882 | * Link the new grammar in the tree |
| 5883 | */ |
| 5884 | ret->parent = ctxt->grammar; |
| 5885 | if (ctxt->grammar != NULL) { |
| 5886 | tmp = ctxt->grammar->children; |
| 5887 | if (tmp == NULL) { |
| 5888 | ctxt->grammar->children = ret; |
| 5889 | } else { |
| 5890 | while (tmp->next != NULL) |
| 5891 | tmp = tmp->next; |
| 5892 | tmp->next = ret; |
| 5893 | } |
| 5894 | } |
| 5895 | |
| 5896 | old = ctxt->grammar; |
| 5897 | ctxt->grammar = ret; |
| 5898 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 5899 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5900 | if (ctxt->grammar == NULL) { |
| 5901 | if (ctxt->error != NULL) |
| 5902 | ctxt->error(ctxt->userData, |
| 5903 | "Failed to parse <grammar> content\n"); |
| 5904 | ctxt->nbErrors++; |
| 5905 | } else if (ctxt->grammar->start == NULL) { |
| 5906 | if (ctxt->error != NULL) |
| 5907 | ctxt->error(ctxt->userData, |
| 5908 | "Element <grammar> has no <start>\n"); |
| 5909 | ctxt->nbErrors++; |
| 5910 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5911 | |
| 5912 | /* |
| 5913 | * Apply 4.17 mergingd rules to defines and starts |
| 5914 | */ |
| 5915 | xmlRelaxNGCombineStart(ctxt, ret); |
| 5916 | if (ret->defs != NULL) { |
| 5917 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 5918 | ctxt); |
| 5919 | } |
| 5920 | |
| 5921 | /* |
| 5922 | * link together defines and refs in this grammar |
| 5923 | */ |
| 5924 | if (ret->refs != NULL) { |
| 5925 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 5926 | ctxt); |
| 5927 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 5928 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5929 | ctxt->grammar = old; |
| 5930 | return(ret); |
| 5931 | } |
| 5932 | |
| 5933 | /** |
| 5934 | * xmlRelaxNGParseDocument: |
| 5935 | * @ctxt: a Relax-NG parser context |
| 5936 | * @node: the root node of the RelaxNG schema |
| 5937 | * |
| 5938 | * parse a Relax-NG definition resource and build an internal |
| 5939 | * xmlRelaxNG struture which can be used to validate instances. |
| 5940 | * |
| 5941 | * Returns the internal XML RelaxNG structure built or |
| 5942 | * NULL in case of error |
| 5943 | */ |
| 5944 | static xmlRelaxNGPtr |
| 5945 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 5946 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5947 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 5948 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5949 | |
| 5950 | if ((ctxt == NULL) || (node == NULL)) |
| 5951 | return (NULL); |
| 5952 | |
| 5953 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 5954 | if (schema == NULL) |
| 5955 | return(NULL); |
| 5956 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5957 | olddefine = ctxt->define; |
| 5958 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5959 | if (IS_RELAXNG(node, "grammar")) { |
| 5960 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 5961 | } else { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5962 | xmlRelaxNGGrammarPtr tmp, ret; |
| 5963 | |
| 5964 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5965 | if (schema->topgrammar == NULL) { |
| 5966 | return(schema); |
| 5967 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5968 | /* |
| 5969 | * Link the new grammar in the tree |
| 5970 | */ |
| 5971 | ret->parent = ctxt->grammar; |
| 5972 | if (ctxt->grammar != NULL) { |
| 5973 | tmp = ctxt->grammar->children; |
| 5974 | if (tmp == NULL) { |
| 5975 | ctxt->grammar->children = ret; |
| 5976 | } else { |
| 5977 | while (tmp->next != NULL) |
| 5978 | tmp = tmp->next; |
| 5979 | tmp->next = ret; |
| 5980 | } |
| 5981 | } |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 5982 | old = ctxt->grammar; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5983 | ctxt->grammar = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5984 | xmlRelaxNGParseStart(ctxt, node); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 5985 | if (old != NULL) |
| 5986 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5987 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5988 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5989 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5990 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5991 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5992 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 5993 | while ((schema->topgrammar->start != NULL) && |
| 5994 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 5995 | (schema->topgrammar->start->next != NULL)) |
| 5996 | schema->topgrammar->start = schema->topgrammar->start->content; |
| 5997 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 5998 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5999 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6000 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6001 | |
| 6002 | #ifdef DEBUG |
| 6003 | if (schema == NULL) |
| 6004 | xmlGenericError(xmlGenericErrorContext, |
| 6005 | "xmlRelaxNGParseDocument() failed\n"); |
| 6006 | #endif |
| 6007 | |
| 6008 | return (schema); |
| 6009 | } |
| 6010 | |
| 6011 | /************************************************************************ |
| 6012 | * * |
| 6013 | * Reading RelaxNGs * |
| 6014 | * * |
| 6015 | ************************************************************************/ |
| 6016 | |
| 6017 | /** |
| 6018 | * xmlRelaxNGNewParserCtxt: |
| 6019 | * @URL: the location of the schema |
| 6020 | * |
| 6021 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6022 | * to contain an XML RelaxNGs file. |
| 6023 | * |
| 6024 | * Returns the parser context or NULL in case of error |
| 6025 | */ |
| 6026 | xmlRelaxNGParserCtxtPtr |
| 6027 | xmlRelaxNGNewParserCtxt(const char *URL) { |
| 6028 | xmlRelaxNGParserCtxtPtr ret; |
| 6029 | |
| 6030 | if (URL == NULL) |
| 6031 | return(NULL); |
| 6032 | |
| 6033 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6034 | if (ret == NULL) { |
| 6035 | xmlGenericError(xmlGenericErrorContext, |
| 6036 | "Failed to allocate new schama parser context for %s\n", URL); |
| 6037 | return (NULL); |
| 6038 | } |
| 6039 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6040 | ret->URL = xmlStrdup((const xmlChar *)URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6041 | ret->error = xmlGenericError; |
| 6042 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6043 | return (ret); |
| 6044 | } |
| 6045 | |
| 6046 | /** |
| 6047 | * xmlRelaxNGNewMemParserCtxt: |
| 6048 | * @buffer: a pointer to a char array containing the schemas |
| 6049 | * @size: the size of the array |
| 6050 | * |
| 6051 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6052 | * to contain an XML RelaxNGs file. |
| 6053 | * |
| 6054 | * Returns the parser context or NULL in case of error |
| 6055 | */ |
| 6056 | xmlRelaxNGParserCtxtPtr |
| 6057 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) { |
| 6058 | xmlRelaxNGParserCtxtPtr ret; |
| 6059 | |
| 6060 | if ((buffer == NULL) || (size <= 0)) |
| 6061 | return(NULL); |
| 6062 | |
| 6063 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6064 | if (ret == NULL) { |
| 6065 | xmlGenericError(xmlGenericErrorContext, |
| 6066 | "Failed to allocate new schama parser context\n"); |
| 6067 | return (NULL); |
| 6068 | } |
| 6069 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6070 | ret->buffer = buffer; |
| 6071 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6072 | ret->error = xmlGenericError; |
| 6073 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6074 | return (ret); |
| 6075 | } |
| 6076 | |
| 6077 | /** |
| 6078 | * xmlRelaxNGFreeParserCtxt: |
| 6079 | * @ctxt: the schema parser context |
| 6080 | * |
| 6081 | * Free the resources associated to the schema parser context |
| 6082 | */ |
| 6083 | void |
| 6084 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) { |
| 6085 | if (ctxt == NULL) |
| 6086 | return; |
| 6087 | if (ctxt->URL != NULL) |
| 6088 | xmlFree(ctxt->URL); |
| 6089 | if (ctxt->doc != NULL) |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6090 | xmlFreeDoc(ctxt->document); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6091 | if (ctxt->interleaves != NULL) |
| 6092 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6093 | if (ctxt->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6094 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6095 | if (ctxt->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6096 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6097 | if (ctxt->docTab != NULL) |
| 6098 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6099 | if (ctxt->incTab != NULL) |
| 6100 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6101 | if (ctxt->defTab != NULL) { |
| 6102 | int i; |
| 6103 | |
| 6104 | for (i = 0;i < ctxt->defNr;i++) |
| 6105 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6106 | xmlFree(ctxt->defTab); |
| 6107 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6108 | xmlFree(ctxt); |
| 6109 | } |
| 6110 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6111 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6112 | * xmlRelaxNGNormExtSpace: |
| 6113 | * @value: a value |
| 6114 | * |
| 6115 | * Removes the leading and ending spaces of the value |
| 6116 | * The string is modified "in situ" |
| 6117 | */ |
| 6118 | static void |
| 6119 | xmlRelaxNGNormExtSpace(xmlChar *value) { |
| 6120 | xmlChar *start = value; |
| 6121 | xmlChar *cur = value; |
| 6122 | if (value == NULL) |
| 6123 | return; |
| 6124 | |
| 6125 | while (IS_BLANK(*cur)) cur++; |
| 6126 | if (cur == start) { |
| 6127 | do { |
| 6128 | while ((*cur != 0) && (!IS_BLANK(*cur))) cur++; |
| 6129 | if (*cur == 0) |
| 6130 | return; |
| 6131 | start = cur; |
| 6132 | while (IS_BLANK(*cur)) cur++; |
| 6133 | if (*cur == 0) { |
| 6134 | *start = 0; |
| 6135 | return; |
| 6136 | } |
| 6137 | } while (1); |
| 6138 | } else { |
| 6139 | do { |
| 6140 | while ((*cur != 0) && (!IS_BLANK(*cur))) |
| 6141 | *start++ = *cur++; |
| 6142 | if (*cur == 0) { |
| 6143 | *start = 0; |
| 6144 | return; |
| 6145 | } |
| 6146 | /* don't try to normalize the inner spaces */ |
| 6147 | while (IS_BLANK(*cur)) cur++; |
| 6148 | *start++ = *cur++; |
| 6149 | if (*cur == 0) { |
| 6150 | *start = 0; |
| 6151 | return; |
| 6152 | } |
| 6153 | } while (1); |
| 6154 | } |
| 6155 | } |
| 6156 | |
| 6157 | /** |
| 6158 | * xmlRelaxNGCheckAttributes: |
| 6159 | * @ctxt: a Relax-NG parser context |
| 6160 | * @node: a Relax-NG node |
| 6161 | * |
| 6162 | * Check all the attributes on the given node |
| 6163 | */ |
| 6164 | static void |
| 6165 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6166 | xmlAttrPtr cur, next; |
| 6167 | |
| 6168 | cur = node->properties; |
| 6169 | while (cur != NULL) { |
| 6170 | next = cur->next; |
| 6171 | if ((cur->ns == NULL) || |
| 6172 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6173 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6174 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6175 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6176 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6177 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6178 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6179 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6180 | if (ctxt->error != NULL) |
| 6181 | ctxt->error(ctxt->userData, |
| 6182 | "Attribute %s is not allowed on %s\n", |
| 6183 | cur->name, node->name); |
| 6184 | ctxt->nbErrors++; |
| 6185 | } |
| 6186 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6187 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6188 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6189 | if (ctxt->error != NULL) |
| 6190 | ctxt->error(ctxt->userData, |
| 6191 | "Attribute %s is not allowed on %s\n", |
| 6192 | cur->name, node->name); |
| 6193 | ctxt->nbErrors++; |
| 6194 | } |
| 6195 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6196 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6197 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6198 | if (ctxt->error != NULL) |
| 6199 | ctxt->error(ctxt->userData, |
| 6200 | "Attribute %s is not allowed on %s\n", |
| 6201 | cur->name, node->name); |
| 6202 | ctxt->nbErrors++; |
| 6203 | } |
| 6204 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6205 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6206 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6207 | if (ctxt->error != NULL) |
| 6208 | ctxt->error(ctxt->userData, |
| 6209 | "Attribute %s is not allowed on %s\n", |
| 6210 | cur->name, node->name); |
| 6211 | ctxt->nbErrors++; |
| 6212 | } |
| 6213 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6214 | xmlChar *val; |
| 6215 | xmlURIPtr uri; |
| 6216 | |
| 6217 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6218 | if (val != NULL) { |
| 6219 | if (val[0] != 0) { |
| 6220 | uri = xmlParseURI((const char *) val); |
| 6221 | if (uri == NULL) { |
| 6222 | if (ctxt->error != NULL) |
| 6223 | ctxt->error(ctxt->userData, |
| 6224 | "Attribute %s contains invalid URI %s\n", |
| 6225 | cur->name, val); |
| 6226 | ctxt->nbErrors++; |
| 6227 | } else { |
| 6228 | if (uri->scheme == NULL) { |
| 6229 | if (ctxt->error != NULL) |
| 6230 | ctxt->error(ctxt->userData, |
| 6231 | "Attribute %s URI %s is not absolute\n", |
| 6232 | cur->name, val); |
| 6233 | ctxt->nbErrors++; |
| 6234 | } |
| 6235 | if (uri->fragment != NULL) { |
| 6236 | if (ctxt->error != NULL) |
| 6237 | ctxt->error(ctxt->userData, |
| 6238 | "Attribute %s URI %s has a fragment ID\n", |
| 6239 | cur->name, val); |
| 6240 | ctxt->nbErrors++; |
| 6241 | } |
| 6242 | xmlFreeURI(uri); |
| 6243 | } |
| 6244 | } |
| 6245 | xmlFree(val); |
| 6246 | } |
| 6247 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6248 | if (ctxt->error != NULL) |
| 6249 | ctxt->error(ctxt->userData, |
| 6250 | "Unknown attribute %s on %s\n", |
| 6251 | cur->name, node->name); |
| 6252 | ctxt->nbErrors++; |
| 6253 | } |
| 6254 | } |
| 6255 | cur = next; |
| 6256 | } |
| 6257 | } |
| 6258 | |
| 6259 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6260 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6261 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6262 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6263 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6264 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6265 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6266 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6267 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6268 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6269 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6270 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6271 | delete = NULL; |
| 6272 | cur = root; |
| 6273 | while (cur != NULL) { |
| 6274 | if (delete != NULL) { |
| 6275 | xmlUnlinkNode(delete); |
| 6276 | xmlFreeNode(delete); |
| 6277 | delete = NULL; |
| 6278 | } |
| 6279 | if (cur->type == XML_ELEMENT_NODE) { |
| 6280 | /* |
| 6281 | * Simplification 4.1. Annotations |
| 6282 | */ |
| 6283 | if ((cur->ns == NULL) || |
| 6284 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6285 | if ((cur->parent != NULL) && |
| 6286 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6287 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6288 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6289 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6290 | if (ctxt->error != NULL) |
| 6291 | ctxt->error(ctxt->userData, |
| 6292 | "element %s doesn't allow foreign elements\n", |
| 6293 | cur->parent->name); |
| 6294 | ctxt->nbErrors++; |
| 6295 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6296 | delete = cur; |
| 6297 | goto skip_children; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6298 | } else { |
| 6299 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6300 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6301 | xmlChar *href, *ns, *base, *URL; |
| 6302 | xmlRelaxNGDocumentPtr docu; |
| 6303 | xmlNodePtr tmp; |
| 6304 | |
| 6305 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6306 | if (ns == NULL) { |
| 6307 | tmp = cur->parent; |
| 6308 | while ((tmp != NULL) && |
| 6309 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6310 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6311 | if (ns != NULL) |
| 6312 | break; |
| 6313 | tmp = tmp->parent; |
| 6314 | } |
| 6315 | } |
| 6316 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6317 | if (href == NULL) { |
| 6318 | if (ctxt->error != NULL) |
| 6319 | ctxt->error(ctxt->userData, |
| 6320 | "xmlRelaxNGParse: externalRef has no href attribute\n"); |
| 6321 | ctxt->nbErrors++; |
| 6322 | delete = cur; |
| 6323 | goto skip_children; |
| 6324 | } |
| 6325 | base = xmlNodeGetBase(cur->doc, cur); |
| 6326 | URL = xmlBuildURI(href, base); |
| 6327 | if (URL == NULL) { |
| 6328 | if (ctxt->error != NULL) |
| 6329 | ctxt->error(ctxt->userData, |
| 6330 | "Failed to compute URL for externalRef %s\n", href); |
| 6331 | ctxt->nbErrors++; |
| 6332 | if (href != NULL) |
| 6333 | xmlFree(href); |
| 6334 | if (base != NULL) |
| 6335 | xmlFree(base); |
| 6336 | delete = cur; |
| 6337 | goto skip_children; |
| 6338 | } |
| 6339 | if (href != NULL) |
| 6340 | xmlFree(href); |
| 6341 | if (base != NULL) |
| 6342 | xmlFree(base); |
| 6343 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6344 | if (docu == NULL) { |
| 6345 | if (ctxt->error != NULL) |
| 6346 | ctxt->error(ctxt->userData, |
| 6347 | "Failed to load externalRef %s\n", URL); |
| 6348 | ctxt->nbErrors++; |
| 6349 | xmlFree(URL); |
| 6350 | delete = cur; |
| 6351 | goto skip_children; |
| 6352 | } |
| 6353 | if (ns != NULL) |
| 6354 | xmlFree(ns); |
| 6355 | xmlFree(URL); |
| 6356 | cur->_private = docu; |
| 6357 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6358 | xmlChar *href, *ns, *base, *URL; |
| 6359 | xmlRelaxNGIncludePtr incl; |
| 6360 | xmlNodePtr tmp; |
| 6361 | |
| 6362 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6363 | if (href == NULL) { |
| 6364 | if (ctxt->error != NULL) |
| 6365 | ctxt->error(ctxt->userData, |
| 6366 | "xmlRelaxNGParse: include has no href attribute\n"); |
| 6367 | ctxt->nbErrors++; |
| 6368 | delete = cur; |
| 6369 | goto skip_children; |
| 6370 | } |
| 6371 | base = xmlNodeGetBase(cur->doc, cur); |
| 6372 | URL = xmlBuildURI(href, base); |
| 6373 | if (URL == NULL) { |
| 6374 | if (ctxt->error != NULL) |
| 6375 | ctxt->error(ctxt->userData, |
| 6376 | "Failed to compute URL for include %s\n", href); |
| 6377 | ctxt->nbErrors++; |
| 6378 | if (href != NULL) |
| 6379 | xmlFree(href); |
| 6380 | if (base != NULL) |
| 6381 | xmlFree(base); |
| 6382 | delete = cur; |
| 6383 | goto skip_children; |
| 6384 | } |
| 6385 | if (href != NULL) |
| 6386 | xmlFree(href); |
| 6387 | if (base != NULL) |
| 6388 | xmlFree(base); |
| 6389 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6390 | if (ns == NULL) { |
| 6391 | tmp = cur->parent; |
| 6392 | while ((tmp != NULL) && |
| 6393 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6394 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6395 | if (ns != NULL) |
| 6396 | break; |
| 6397 | tmp = tmp->parent; |
| 6398 | } |
| 6399 | } |
| 6400 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 6401 | if (ns != NULL) |
| 6402 | xmlFree(ns); |
| 6403 | if (incl == NULL) { |
| 6404 | if (ctxt->error != NULL) |
| 6405 | ctxt->error(ctxt->userData, |
| 6406 | "Failed to load include %s\n", URL); |
| 6407 | ctxt->nbErrors++; |
| 6408 | xmlFree(URL); |
| 6409 | delete = cur; |
| 6410 | goto skip_children; |
| 6411 | } |
| 6412 | xmlFree(URL); |
| 6413 | cur->_private = incl; |
| 6414 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 6415 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) { |
| 6416 | xmlChar *name, *ns; |
| 6417 | xmlNodePtr text = NULL; |
| 6418 | |
| 6419 | /* |
| 6420 | * Simplification 4.8. name attribute of element |
| 6421 | * and attribute elements |
| 6422 | */ |
| 6423 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 6424 | if (name != NULL) { |
| 6425 | if (cur->children == NULL) { |
| 6426 | text = xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 6427 | name); |
| 6428 | } else { |
| 6429 | xmlNodePtr node; |
| 6430 | node = xmlNewNode(cur->ns, BAD_CAST "name"); |
| 6431 | if (node != NULL) { |
| 6432 | xmlAddPrevSibling(cur->children, node); |
| 6433 | text = xmlNewText(name); |
| 6434 | xmlAddChild(node, text); |
| 6435 | text = node; |
| 6436 | } |
| 6437 | } |
| 6438 | if (text == NULL) { |
| 6439 | if (ctxt->error != NULL) |
| 6440 | ctxt->error(ctxt->userData, |
| 6441 | "Failed to create a name %s element\n", name); |
| 6442 | ctxt->nbErrors++; |
| 6443 | } |
| 6444 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 6445 | xmlFree(name); |
| 6446 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6447 | if (ns != NULL) { |
| 6448 | if (text != NULL) { |
| 6449 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 6450 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 6451 | } |
| 6452 | xmlFree(ns); |
| 6453 | } else if (xmlStrEqual(cur->name, |
| 6454 | BAD_CAST "attribute")) { |
| 6455 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 6456 | } |
| 6457 | } |
| 6458 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 6459 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 6460 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 6461 | /* |
| 6462 | * Simplification 4.8. name attribute of element |
| 6463 | * and attribute elements |
| 6464 | */ |
| 6465 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 6466 | xmlNodePtr node; |
| 6467 | xmlChar *ns = NULL; |
| 6468 | |
| 6469 | node = cur->parent; |
| 6470 | while ((node != NULL) && |
| 6471 | (node->type == XML_ELEMENT_NODE)) { |
| 6472 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 6473 | if (ns != NULL) { |
| 6474 | break; |
| 6475 | } |
| 6476 | node = node->parent; |
| 6477 | } |
| 6478 | if (ns == NULL) { |
| 6479 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 6480 | } else { |
| 6481 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 6482 | xmlFree(ns); |
| 6483 | } |
| 6484 | } |
| 6485 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6486 | xmlChar *name, *local, *prefix; |
| 6487 | |
| 6488 | /* |
| 6489 | * Simplification: 4.10. QNames |
| 6490 | */ |
| 6491 | name = xmlNodeGetContent(cur); |
| 6492 | if (name != NULL) { |
| 6493 | local = xmlSplitQName2(name, &prefix); |
| 6494 | if (local != NULL) { |
| 6495 | xmlNsPtr ns; |
| 6496 | |
| 6497 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 6498 | if (ns == NULL) { |
| 6499 | if (ctxt->error != NULL) |
| 6500 | ctxt->error(ctxt->userData, |
| 6501 | "xmlRelaxNGParse: no namespace for prefix %s\n", prefix); |
| 6502 | ctxt->nbErrors++; |
| 6503 | } else { |
| 6504 | xmlSetProp(cur, BAD_CAST "ns", ns->href); |
| 6505 | xmlNodeSetContent(cur, local); |
| 6506 | } |
| 6507 | xmlFree(local); |
| 6508 | xmlFree(prefix); |
| 6509 | } |
| 6510 | xmlFree(name); |
| 6511 | } |
| 6512 | } |
| 6513 | /* |
| 6514 | * 4.16 |
| 6515 | */ |
| 6516 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 6517 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 6518 | if (ctxt->error != NULL) |
| 6519 | ctxt->error(ctxt->userData, |
| 6520 | "Found nsName/except//nsName forbidden construct\n"); |
| 6521 | ctxt->nbErrors++; |
| 6522 | } |
| 6523 | } |
| 6524 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 6525 | (cur != root)) { |
| 6526 | int oldflags = ctxt->flags; |
| 6527 | |
| 6528 | /* |
| 6529 | * 4.16 |
| 6530 | */ |
| 6531 | if ((cur->parent != NULL) && |
| 6532 | (xmlStrEqual(cur->parent->name, BAD_CAST "anyName"))) { |
| 6533 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 6534 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 6535 | ctxt->flags = oldflags; |
| 6536 | goto skip_children; |
| 6537 | } else if ((cur->parent != NULL) && |
| 6538 | (xmlStrEqual(cur->parent->name, BAD_CAST "nsName"))) { |
| 6539 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 6540 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 6541 | ctxt->flags = oldflags; |
| 6542 | goto skip_children; |
| 6543 | } |
| 6544 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 6545 | /* |
| 6546 | * 4.16 |
| 6547 | */ |
| 6548 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 6549 | if (ctxt->error != NULL) |
| 6550 | ctxt->error(ctxt->userData, |
| 6551 | "Found anyName/except//anyName forbidden construct\n"); |
| 6552 | ctxt->nbErrors++; |
| 6553 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 6554 | if (ctxt->error != NULL) |
| 6555 | ctxt->error(ctxt->userData, |
| 6556 | "Found nsName/except//anyName forbidden construct\n"); |
| 6557 | ctxt->nbErrors++; |
| 6558 | } |
| 6559 | } |
| 6560 | /* |
| 6561 | * Thisd is not an else since "include" is transformed |
| 6562 | * into a div |
| 6563 | */ |
| 6564 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 6565 | xmlChar *ns; |
| 6566 | xmlNodePtr child, ins, tmp; |
| 6567 | |
| 6568 | /* |
| 6569 | * implements rule 4.11 |
| 6570 | */ |
| 6571 | |
| 6572 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6573 | |
| 6574 | child = cur->children; |
| 6575 | ins = cur; |
| 6576 | while (child != NULL) { |
| 6577 | if (ns != NULL) { |
| 6578 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 6579 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 6580 | } |
| 6581 | } |
| 6582 | tmp = child->next; |
| 6583 | xmlUnlinkNode(child); |
| 6584 | ins = xmlAddNextSibling(ins, child); |
| 6585 | child = tmp; |
| 6586 | } |
| 6587 | if (ns != NULL) |
| 6588 | xmlFree(ns); |
| 6589 | delete = cur; |
| 6590 | goto skip_children; |
| 6591 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6592 | } |
| 6593 | } |
| 6594 | /* |
| 6595 | * Simplification 4.2 whitespaces |
| 6596 | */ |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 6597 | else if ((cur->type == XML_TEXT_NODE) || |
| 6598 | (cur->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6599 | if (IS_BLANK_NODE(cur)) { |
| 6600 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 6601 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) && |
| 6602 | (!xmlStrEqual(cur->parent->name, BAD_CAST "param"))) |
| 6603 | delete = cur; |
| 6604 | } else { |
| 6605 | delete = cur; |
| 6606 | goto skip_children; |
| 6607 | } |
| 6608 | } |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 6609 | } else { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6610 | delete = cur; |
| 6611 | goto skip_children; |
| 6612 | } |
| 6613 | |
| 6614 | /* |
| 6615 | * Skip to next node |
| 6616 | */ |
| 6617 | if (cur->children != NULL) { |
| 6618 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 6619 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 6620 | (cur->children->type != XML_ENTITY_NODE)) { |
| 6621 | cur = cur->children; |
| 6622 | continue; |
| 6623 | } |
| 6624 | } |
| 6625 | skip_children: |
| 6626 | if (cur->next != NULL) { |
| 6627 | cur = cur->next; |
| 6628 | continue; |
| 6629 | } |
| 6630 | |
| 6631 | do { |
| 6632 | cur = cur->parent; |
| 6633 | if (cur == NULL) |
| 6634 | break; |
| 6635 | if (cur == root) { |
| 6636 | cur = NULL; |
| 6637 | break; |
| 6638 | } |
| 6639 | if (cur->next != NULL) { |
| 6640 | cur = cur->next; |
| 6641 | break; |
| 6642 | } |
| 6643 | } while (cur != NULL); |
| 6644 | } |
| 6645 | if (delete != NULL) { |
| 6646 | xmlUnlinkNode(delete); |
| 6647 | xmlFreeNode(delete); |
| 6648 | delete = NULL; |
| 6649 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6650 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6651 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6652 | /** |
| 6653 | * xmlRelaxNGCleanupDoc: |
| 6654 | * @ctxt: a Relax-NG parser context |
| 6655 | * @doc: an xmldocPtr document pointer |
| 6656 | * |
| 6657 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 6658 | * Include and externalRef lookups. |
| 6659 | * |
| 6660 | * Returns the cleaned up document or NULL in case of error |
| 6661 | */ |
| 6662 | static xmlDocPtr |
| 6663 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) { |
| 6664 | xmlNodePtr root; |
| 6665 | |
| 6666 | /* |
| 6667 | * Extract the root |
| 6668 | */ |
| 6669 | root = xmlDocGetRootElement(doc); |
| 6670 | if (root == NULL) { |
| 6671 | if (ctxt->error != NULL) |
| 6672 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 6673 | ctxt->URL); |
| 6674 | ctxt->nbErrors++; |
| 6675 | return (NULL); |
| 6676 | } |
| 6677 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6678 | return(doc); |
| 6679 | } |
| 6680 | |
| 6681 | /** |
| 6682 | * xmlRelaxNGParse: |
| 6683 | * @ctxt: a Relax-NG parser context |
| 6684 | * |
| 6685 | * parse a schema definition resource and build an internal |
| 6686 | * XML Shema struture which can be used to validate instances. |
| 6687 | * *WARNING* this interface is highly subject to change |
| 6688 | * |
| 6689 | * Returns the internal XML RelaxNG structure built from the resource or |
| 6690 | * NULL in case of error |
| 6691 | */ |
| 6692 | xmlRelaxNGPtr |
| 6693 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 6694 | { |
| 6695 | xmlRelaxNGPtr ret = NULL; |
| 6696 | xmlDocPtr doc; |
| 6697 | xmlNodePtr root; |
| 6698 | |
| 6699 | xmlRelaxNGInitTypes(); |
| 6700 | |
| 6701 | if (ctxt == NULL) |
| 6702 | return (NULL); |
| 6703 | |
| 6704 | /* |
| 6705 | * First step is to parse the input document into an DOM/Infoset |
| 6706 | */ |
| 6707 | if (ctxt->URL != NULL) { |
| 6708 | doc = xmlParseFile((const char *) ctxt->URL); |
| 6709 | if (doc == NULL) { |
| 6710 | if (ctxt->error != NULL) |
| 6711 | ctxt->error(ctxt->userData, |
| 6712 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL); |
| 6713 | ctxt->nbErrors++; |
| 6714 | return (NULL); |
| 6715 | } |
| 6716 | } else if (ctxt->buffer != NULL) { |
| 6717 | doc = xmlParseMemory(ctxt->buffer, ctxt->size); |
| 6718 | if (doc == NULL) { |
| 6719 | if (ctxt->error != NULL) |
| 6720 | ctxt->error(ctxt->userData, |
| 6721 | "xmlRelaxNGParse: could not parse schemas\n"); |
| 6722 | ctxt->nbErrors++; |
| 6723 | return (NULL); |
| 6724 | } |
| 6725 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 6726 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 6727 | } else { |
| 6728 | if (ctxt->error != NULL) |
| 6729 | ctxt->error(ctxt->userData, |
| 6730 | "xmlRelaxNGParse: nothing to parse\n"); |
| 6731 | ctxt->nbErrors++; |
| 6732 | return (NULL); |
| 6733 | } |
| 6734 | ctxt->document = doc; |
| 6735 | |
| 6736 | /* |
| 6737 | * Some preprocessing of the document content |
| 6738 | */ |
| 6739 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 6740 | if (doc == NULL) { |
| 6741 | xmlFreeDoc(ctxt->document); |
| 6742 | ctxt->document = NULL; |
| 6743 | return(NULL); |
| 6744 | } |
| 6745 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6746 | /* |
| 6747 | * Then do the parsing for good |
| 6748 | */ |
| 6749 | root = xmlDocGetRootElement(doc); |
| 6750 | if (root == NULL) { |
| 6751 | if (ctxt->error != NULL) |
| 6752 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 6753 | ctxt->URL); |
| 6754 | ctxt->nbErrors++; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6755 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6756 | return (NULL); |
| 6757 | } |
| 6758 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6759 | if (ret == NULL) { |
| 6760 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6761 | return(NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6762 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6763 | |
| 6764 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6765 | * Check the ref/defines links |
| 6766 | */ |
| 6767 | /* |
| 6768 | * try to preprocess interleaves |
| 6769 | */ |
| 6770 | if (ctxt->interleaves != NULL) { |
| 6771 | xmlHashScan(ctxt->interleaves, |
| 6772 | (xmlHashScanner)xmlRelaxNGComputeInterleaves, ctxt); |
| 6773 | } |
| 6774 | |
| 6775 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6776 | * if there was a parsing error return NULL |
| 6777 | */ |
| 6778 | if (ctxt->nbErrors > 0) { |
| 6779 | xmlRelaxNGFree(ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6780 | ctxt->document = NULL; |
| 6781 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6782 | return(NULL); |
| 6783 | } |
| 6784 | |
| 6785 | /* |
| 6786 | * Transfer the pointer for cleanup at the schema level. |
| 6787 | */ |
| 6788 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6789 | ctxt->document = NULL; |
| 6790 | ret->documents = ctxt->documents; |
| 6791 | ctxt->documents = NULL; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6792 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 6793 | ret->includes = ctxt->includes; |
| 6794 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6795 | ret->defNr = ctxt->defNr; |
| 6796 | ret->defTab = ctxt->defTab; |
| 6797 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 6798 | if (ctxt->idref == 1) |
| 6799 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6800 | |
| 6801 | return (ret); |
| 6802 | } |
| 6803 | |
| 6804 | /** |
| 6805 | * xmlRelaxNGSetParserErrors: |
| 6806 | * @ctxt: a Relax-NG validation context |
| 6807 | * @err: the error callback |
| 6808 | * @warn: the warning callback |
| 6809 | * @ctx: contextual data for the callbacks |
| 6810 | * |
| 6811 | * Set the callback functions used to handle errors for a validation context |
| 6812 | */ |
| 6813 | void |
| 6814 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
| 6815 | xmlRelaxNGValidityErrorFunc err, |
| 6816 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 6817 | if (ctxt == NULL) |
| 6818 | return; |
| 6819 | ctxt->error = err; |
| 6820 | ctxt->warning = warn; |
| 6821 | ctxt->userData = ctx; |
| 6822 | } |
| 6823 | /************************************************************************ |
| 6824 | * * |
| 6825 | * Dump back a compiled form * |
| 6826 | * * |
| 6827 | ************************************************************************/ |
| 6828 | static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define); |
| 6829 | |
| 6830 | /** |
| 6831 | * xmlRelaxNGDumpDefines: |
| 6832 | * @output: the file output |
| 6833 | * @defines: a list of define structures |
| 6834 | * |
| 6835 | * Dump a RelaxNG structure back |
| 6836 | */ |
| 6837 | static void |
| 6838 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) { |
| 6839 | while (defines != NULL) { |
| 6840 | xmlRelaxNGDumpDefine(output, defines); |
| 6841 | defines = defines->next; |
| 6842 | } |
| 6843 | } |
| 6844 | |
| 6845 | /** |
| 6846 | * xmlRelaxNGDumpDefine: |
| 6847 | * @output: the file output |
| 6848 | * @define: a define structure |
| 6849 | * |
| 6850 | * Dump a RelaxNG structure back |
| 6851 | */ |
| 6852 | static void |
| 6853 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) { |
| 6854 | if (define == NULL) |
| 6855 | return; |
| 6856 | switch(define->type) { |
| 6857 | case XML_RELAXNG_EMPTY: |
| 6858 | fprintf(output, "<empty/>\n"); |
| 6859 | break; |
| 6860 | case XML_RELAXNG_NOT_ALLOWED: |
| 6861 | fprintf(output, "<notAllowed/>\n"); |
| 6862 | break; |
| 6863 | case XML_RELAXNG_TEXT: |
| 6864 | fprintf(output, "<text/>\n"); |
| 6865 | break; |
| 6866 | case XML_RELAXNG_ELEMENT: |
| 6867 | fprintf(output, "<element>\n"); |
| 6868 | if (define->name != NULL) { |
| 6869 | fprintf(output, "<name"); |
| 6870 | if (define->ns != NULL) |
| 6871 | fprintf(output, " ns=\"%s\"", define->ns); |
| 6872 | fprintf(output, ">%s</name>\n", define->name); |
| 6873 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6874 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 6875 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6876 | fprintf(output, "</element>\n"); |
| 6877 | break; |
| 6878 | case XML_RELAXNG_LIST: |
| 6879 | fprintf(output, "<list>\n"); |
| 6880 | xmlRelaxNGDumpDefines(output, define->content); |
| 6881 | fprintf(output, "</list>\n"); |
| 6882 | break; |
| 6883 | case XML_RELAXNG_ONEORMORE: |
| 6884 | fprintf(output, "<oneOrMore>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6885 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6886 | fprintf(output, "</oneOrMore>\n"); |
| 6887 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6888 | case XML_RELAXNG_ZEROORMORE: |
| 6889 | fprintf(output, "<zeroOrMore>\n"); |
| 6890 | xmlRelaxNGDumpDefines(output, define->content); |
| 6891 | fprintf(output, "</zeroOrMore>\n"); |
| 6892 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6893 | case XML_RELAXNG_CHOICE: |
| 6894 | fprintf(output, "<choice>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6895 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6896 | fprintf(output, "</choice>\n"); |
| 6897 | break; |
| 6898 | case XML_RELAXNG_GROUP: |
| 6899 | fprintf(output, "<group>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6900 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6901 | fprintf(output, "</group>\n"); |
| 6902 | break; |
| 6903 | case XML_RELAXNG_INTERLEAVE: |
| 6904 | fprintf(output, "<interleave>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6905 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6906 | fprintf(output, "</interleave>\n"); |
| 6907 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6908 | case XML_RELAXNG_OPTIONAL: |
| 6909 | fprintf(output, "<optional>\n"); |
| 6910 | xmlRelaxNGDumpDefines(output, define->content); |
| 6911 | fprintf(output, "</optional>\n"); |
| 6912 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6913 | case XML_RELAXNG_ATTRIBUTE: |
| 6914 | fprintf(output, "<attribute>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6915 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6916 | fprintf(output, "</attribute>\n"); |
| 6917 | break; |
| 6918 | case XML_RELAXNG_DEF: |
| 6919 | fprintf(output, "<define"); |
| 6920 | if (define->name != NULL) |
| 6921 | fprintf(output, " name=\"%s\"", define->name); |
| 6922 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6923 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6924 | fprintf(output, "</define>\n"); |
| 6925 | break; |
| 6926 | case XML_RELAXNG_REF: |
| 6927 | fprintf(output, "<ref"); |
| 6928 | if (define->name != NULL) |
| 6929 | fprintf(output, " name=\"%s\"", define->name); |
| 6930 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6931 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6932 | fprintf(output, "</ref>\n"); |
| 6933 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6934 | case XML_RELAXNG_PARENTREF: |
| 6935 | fprintf(output, "<parentRef"); |
| 6936 | if (define->name != NULL) |
| 6937 | fprintf(output, " name=\"%s\"", define->name); |
| 6938 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6939 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6940 | fprintf(output, "</parentRef>\n"); |
| 6941 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6942 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 6943 | fprintf(output, "<externalRef>"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6944 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6945 | fprintf(output, "</externalRef>\n"); |
| 6946 | break; |
| 6947 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6948 | case XML_RELAXNG_VALUE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6949 | TODO |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6950 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6951 | case XML_RELAXNG_START: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6952 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 6953 | case XML_RELAXNG_PARAM: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6954 | TODO |
| 6955 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6956 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6957 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6958 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6959 | } |
| 6960 | } |
| 6961 | |
| 6962 | /** |
| 6963 | * xmlRelaxNGDumpGrammar: |
| 6964 | * @output: the file output |
| 6965 | * @grammar: a grammar structure |
| 6966 | * @top: is this a top grammar |
| 6967 | * |
| 6968 | * Dump a RelaxNG structure back |
| 6969 | */ |
| 6970 | static void |
| 6971 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 6972 | { |
| 6973 | if (grammar == NULL) |
| 6974 | return; |
| 6975 | |
| 6976 | fprintf(output, "<grammar"); |
| 6977 | if (top) |
| 6978 | fprintf(output, |
| 6979 | " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 6980 | switch(grammar->combine) { |
| 6981 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 6982 | break; |
| 6983 | case XML_RELAXNG_COMBINE_CHOICE: |
| 6984 | fprintf(output, " combine=\"choice\""); |
| 6985 | break; |
| 6986 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 6987 | fprintf(output, " combine=\"interleave\""); |
| 6988 | break; |
| 6989 | default: |
| 6990 | fprintf(output, " <!-- invalid combine value -->"); |
| 6991 | } |
| 6992 | fprintf(output, ">\n"); |
| 6993 | if (grammar->start == NULL) { |
| 6994 | fprintf(output, " <!-- grammar had no start -->"); |
| 6995 | } else { |
| 6996 | fprintf(output, "<start>\n"); |
| 6997 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 6998 | fprintf(output, "</start>\n"); |
| 6999 | } |
| 7000 | /* TODO ? Dump the defines ? */ |
| 7001 | fprintf(output, "</grammar>\n"); |
| 7002 | } |
| 7003 | |
| 7004 | /** |
| 7005 | * xmlRelaxNGDump: |
| 7006 | * @output: the file output |
| 7007 | * @schema: a schema structure |
| 7008 | * |
| 7009 | * Dump a RelaxNG structure back |
| 7010 | */ |
| 7011 | void |
| 7012 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7013 | { |
| 7014 | if (schema == NULL) { |
| 7015 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7016 | return; |
| 7017 | } |
| 7018 | fprintf(output, "RelaxNG: "); |
| 7019 | if (schema->doc == NULL) { |
| 7020 | fprintf(output, "no document\n"); |
| 7021 | } else if (schema->doc->URL != NULL) { |
| 7022 | fprintf(output, "%s\n", schema->doc->URL); |
| 7023 | } else { |
| 7024 | fprintf(output, "\n"); |
| 7025 | } |
| 7026 | if (schema->topgrammar == NULL) { |
| 7027 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7028 | return; |
| 7029 | } |
| 7030 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7031 | } |
| 7032 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7033 | /** |
| 7034 | * xmlRelaxNGDumpTree: |
| 7035 | * @output: the file output |
| 7036 | * @schema: a schema structure |
| 7037 | * |
| 7038 | * Dump the transformed RelaxNG tree. |
| 7039 | */ |
| 7040 | void |
| 7041 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7042 | { |
| 7043 | if (schema == NULL) { |
| 7044 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7045 | return; |
| 7046 | } |
| 7047 | if (schema->doc == NULL) { |
| 7048 | fprintf(output, "no document\n"); |
| 7049 | } else { |
| 7050 | xmlDocDump(output, schema->doc); |
| 7051 | } |
| 7052 | } |
| 7053 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7054 | /************************************************************************ |
| 7055 | * * |
| 7056 | * Validation implementation * |
| 7057 | * * |
| 7058 | ************************************************************************/ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7059 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7060 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7061 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 7062 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7063 | |
| 7064 | /** |
| 7065 | * xmlRelaxNGSkipIgnored: |
| 7066 | * @ctxt: a schema validation context |
| 7067 | * @node: the top node. |
| 7068 | * |
| 7069 | * Skip ignorable nodes in that context |
| 7070 | * |
| 7071 | * Returns the new sibling or NULL in case of error. |
| 7072 | */ |
| 7073 | static xmlNodePtr |
| 7074 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 7075 | xmlNodePtr node) { |
| 7076 | /* |
| 7077 | * TODO complete and handle entities |
| 7078 | */ |
| 7079 | while ((node != NULL) && |
| 7080 | ((node->type == XML_COMMENT_NODE) || |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 7081 | (node->type == XML_PI_NODE) || |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7082 | (((node->type == XML_TEXT_NODE) || |
| 7083 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 7084 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 7085 | (IS_BLANK_NODE(node)))))) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7086 | node = node->next; |
| 7087 | } |
| 7088 | return(node); |
| 7089 | } |
| 7090 | |
| 7091 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7092 | * xmlRelaxNGNormalize: |
| 7093 | * @ctxt: a schema validation context |
| 7094 | * @str: the string to normalize |
| 7095 | * |
| 7096 | * Implements the normalizeWhiteSpace( s ) function from |
| 7097 | * section 6.2.9 of the spec |
| 7098 | * |
| 7099 | * Returns the new string or NULL in case of error. |
| 7100 | */ |
| 7101 | static xmlChar * |
| 7102 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) { |
| 7103 | xmlChar *ret, *p; |
| 7104 | const xmlChar *tmp; |
| 7105 | int len; |
| 7106 | |
| 7107 | if (str == NULL) |
| 7108 | return(NULL); |
| 7109 | tmp = str; |
| 7110 | while (*tmp != 0) tmp++; |
| 7111 | len = tmp - str; |
| 7112 | |
| 7113 | ret = (xmlChar *) xmlMalloc((len + 1) * sizeof(xmlChar)); |
| 7114 | if (ret == NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7115 | if (ctxt != NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7116 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7117 | } else { |
| 7118 | xmlGenericError(xmlGenericErrorContext, |
| 7119 | "xmlRelaxNGNormalize: out of memory\n"); |
| 7120 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7121 | return(NULL); |
| 7122 | } |
| 7123 | p = ret; |
| 7124 | while (IS_BLANK(*str)) str++; |
| 7125 | while (*str != 0) { |
| 7126 | if (IS_BLANK(*str)) { |
| 7127 | while (IS_BLANK(*str)) str++; |
| 7128 | if (*str == 0) |
| 7129 | break; |
| 7130 | *p++ = ' '; |
| 7131 | } else |
| 7132 | *p++ = *str++; |
| 7133 | } |
| 7134 | *p = 0; |
| 7135 | return(ret); |
| 7136 | } |
| 7137 | |
| 7138 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7139 | * xmlRelaxNGValidateDatatype: |
| 7140 | * @ctxt: a Relax-NG validation context |
| 7141 | * @value: the string value |
| 7142 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7143 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7144 | * |
| 7145 | * Validate the given value against the dataype |
| 7146 | * |
| 7147 | * Returns 0 if the validation succeeded or an error code. |
| 7148 | */ |
| 7149 | static int |
| 7150 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7151 | xmlRelaxNGDefinePtr define, xmlNodePtr node) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7152 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7153 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7154 | void *result = NULL; |
| 7155 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7156 | |
| 7157 | if ((define == NULL) || (define->data == NULL)) { |
| 7158 | return(-1); |
| 7159 | } |
| 7160 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7161 | if (lib->check != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7162 | if ((define->attrs != NULL) && |
| 7163 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7164 | ret = lib->check(lib->data, define->name, value, &result, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7165 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7166 | ret = lib->check(lib->data, define->name, value, NULL, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7167 | } |
| 7168 | } else |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7169 | ret = -1; |
| 7170 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7171 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7172 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 7173 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7174 | return(-1); |
| 7175 | } else if (ret == 1) { |
| 7176 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7177 | } else if (ret == 2) { |
| 7178 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7179 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7180 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7181 | ret = -1; |
| 7182 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7183 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7184 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
| 7185 | if (lib->facet != NULL) { |
| 7186 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 7187 | cur->value, value, result); |
| 7188 | if (tmp != 0) |
| 7189 | ret = -1; |
| 7190 | } |
| 7191 | cur = cur->next; |
| 7192 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7193 | if ((ret == 0) && (define->content != NULL)) { |
| 7194 | const xmlChar *oldvalue, *oldendvalue; |
| 7195 | |
| 7196 | oldvalue = ctxt->state->value; |
| 7197 | oldendvalue = ctxt->state->endvalue; |
| 7198 | ctxt->state->value = (xmlChar *) value; |
| 7199 | ctxt->state->endvalue = NULL; |
| 7200 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 7201 | ctxt->state->value = (xmlChar *) oldvalue; |
| 7202 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
| 7203 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7204 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 7205 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7206 | return(ret); |
| 7207 | } |
| 7208 | |
| 7209 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7210 | * xmlRelaxNGNextValue: |
| 7211 | * @ctxt: a Relax-NG validation context |
| 7212 | * |
| 7213 | * Skip to the next value when validating within a list |
| 7214 | * |
| 7215 | * Returns 0 if the operation succeeded or an error code. |
| 7216 | */ |
| 7217 | static int |
| 7218 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) { |
| 7219 | xmlChar *cur; |
| 7220 | |
| 7221 | cur = ctxt->state->value; |
| 7222 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
| 7223 | ctxt->state->value = NULL; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 7224 | ctxt->state->endvalue = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7225 | return(0); |
| 7226 | } |
| 7227 | while (*cur != 0) cur++; |
| 7228 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++; |
| 7229 | if (cur == ctxt->state->endvalue) |
| 7230 | ctxt->state->value = NULL; |
| 7231 | else |
| 7232 | ctxt->state->value = cur; |
| 7233 | return(0); |
| 7234 | } |
| 7235 | |
| 7236 | /** |
| 7237 | * xmlRelaxNGValidateValueList: |
| 7238 | * @ctxt: a Relax-NG validation context |
| 7239 | * @defines: the list of definitions to verify |
| 7240 | * |
| 7241 | * Validate the given set of definitions for the current value |
| 7242 | * |
| 7243 | * Returns 0 if the validation succeeded or an error code. |
| 7244 | */ |
| 7245 | static int |
| 7246 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7247 | xmlRelaxNGDefinePtr defines) { |
| 7248 | int ret = 0; |
| 7249 | |
| 7250 | while (defines != NULL) { |
| 7251 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 7252 | if (ret != 0) |
| 7253 | break; |
| 7254 | defines = defines->next; |
| 7255 | } |
| 7256 | return(ret); |
| 7257 | } |
| 7258 | |
| 7259 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7260 | * xmlRelaxNGValidateValue: |
| 7261 | * @ctxt: a Relax-NG validation context |
| 7262 | * @define: the definition to verify |
| 7263 | * |
| 7264 | * Validate the given definition for the current value |
| 7265 | * |
| 7266 | * Returns 0 if the validation succeeded or an error code. |
| 7267 | */ |
| 7268 | static int |
| 7269 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 7270 | xmlRelaxNGDefinePtr define) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7271 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7272 | xmlChar *value; |
| 7273 | |
| 7274 | value = ctxt->state->value; |
| 7275 | switch (define->type) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 7276 | case XML_RELAXNG_EMPTY: { |
| 7277 | if ((value != NULL) && (value[0] != 0)) { |
| 7278 | int idx = 0; |
| 7279 | |
| 7280 | while (IS_BLANK(value[idx])) |
| 7281 | idx++; |
| 7282 | if (value[idx] != 0) |
| 7283 | ret = -1; |
| 7284 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7285 | break; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 7286 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7287 | case XML_RELAXNG_TEXT: |
| 7288 | break; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7289 | case XML_RELAXNG_VALUE: { |
| 7290 | if (!xmlStrEqual(value, define->value)) { |
| 7291 | if (define->name != NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7292 | xmlRelaxNGTypeLibraryPtr lib; |
| 7293 | |
| 7294 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 7295 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 7296 | ret = lib->comp(lib->data, define->name, |
| 7297 | define->value, define->node, |
| 7298 | (void *) define->attrs, |
| 7299 | value, ctxt->state->node); |
| 7300 | } else |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7301 | ret = -1; |
| 7302 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7303 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, define->name); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7304 | return(-1); |
| 7305 | } else if (ret == 1) { |
| 7306 | ret = 0; |
| 7307 | } else { |
| 7308 | ret = -1; |
| 7309 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7310 | } else { |
| 7311 | xmlChar *nval, *nvalue; |
| 7312 | |
| 7313 | /* |
| 7314 | * TODO: trivial optimizations are possible by |
| 7315 | * computing at compile-time |
| 7316 | */ |
| 7317 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 7318 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
| 7319 | |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7320 | if ((nval == NULL) || (nvalue == NULL) || |
| 7321 | (!xmlStrEqual(nval, nvalue))) |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7322 | ret = -1; |
| 7323 | if (nval != NULL) |
| 7324 | xmlFree(nval); |
| 7325 | if (nvalue != NULL) |
| 7326 | xmlFree(nvalue); |
| 7327 | } |
| 7328 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7329 | if (ret == 0) |
| 7330 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7331 | break; |
| 7332 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7333 | case XML_RELAXNG_DATATYPE: { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7334 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 7335 | ctxt->state->seq); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7336 | if (ret == 0) |
| 7337 | xmlRelaxNGNextValue(ctxt); |
| 7338 | |
| 7339 | break; |
| 7340 | } |
| 7341 | case XML_RELAXNG_CHOICE: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7342 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7343 | xmlChar *oldvalue; |
| 7344 | |
| 7345 | oldflags = ctxt->flags; |
| 7346 | ctxt->flags |= FLAGS_IGNORABLE; |
| 7347 | |
| 7348 | oldvalue = ctxt->state->value; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7349 | while (list != NULL) { |
| 7350 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 7351 | if (ret == 0) { |
| 7352 | break; |
| 7353 | } |
| 7354 | ctxt->state->value = oldvalue; |
| 7355 | list = list->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7356 | } |
| 7357 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7358 | if (ret != 0) { |
| 7359 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7360 | xmlRelaxNGDumpValidError(ctxt); |
| 7361 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 7362 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7363 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7364 | if (ret == 0) |
| 7365 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7366 | break; |
| 7367 | } |
| 7368 | case XML_RELAXNG_LIST: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7369 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7370 | xmlChar *oldvalue, *oldend, *val, *cur; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7371 | #ifdef DEBUG_LIST |
| 7372 | int nb_values = 0; |
| 7373 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7374 | |
| 7375 | oldvalue = ctxt->state->value; |
| 7376 | oldend = ctxt->state->endvalue; |
| 7377 | |
| 7378 | val = xmlStrdup(oldvalue); |
| 7379 | if (val == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 7380 | val = xmlStrdup(BAD_CAST ""); |
| 7381 | } |
| 7382 | if (val == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7383 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7384 | return(-1); |
| 7385 | } |
| 7386 | cur = val; |
| 7387 | while (*cur != 0) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7388 | if (IS_BLANK(*cur)) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7389 | *cur = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7390 | cur++; |
| 7391 | #ifdef DEBUG_LIST |
| 7392 | nb_values++; |
| 7393 | #endif |
| 7394 | while (IS_BLANK(*cur)) |
| 7395 | *cur++ = 0; |
| 7396 | } else |
| 7397 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7398 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7399 | #ifdef DEBUG_LIST |
| 7400 | xmlGenericError(xmlGenericErrorContext, |
| 7401 | "list value: '%s' found %d items\n", oldvalue, nb_values); |
| 7402 | nb_values = 0; |
| 7403 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7404 | ctxt->state->endvalue = cur; |
| 7405 | cur = val; |
| 7406 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 7407 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7408 | ctxt->state->value = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 7409 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7410 | while (list != NULL) { |
| 7411 | if (ctxt->state->value == ctxt->state->endvalue) |
| 7412 | ctxt->state->value = NULL; |
| 7413 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 7414 | if (ret != 0) { |
| 7415 | #ifdef DEBUG_LIST |
| 7416 | xmlGenericError(xmlGenericErrorContext, |
| 7417 | "Failed to validate value: '%s' with %d rule\n", |
| 7418 | ctxt->state->value, nb_values); |
| 7419 | #endif |
| 7420 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 7421 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7422 | #ifdef DEBUG_LIST |
| 7423 | nb_values++; |
| 7424 | #endif |
| 7425 | list = list->next; |
| 7426 | } |
| 7427 | |
| 7428 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 7429 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 7430 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, ctxt->state->value); |
| 7431 | ret = -1; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7432 | } |
| 7433 | xmlFree(val); |
| 7434 | ctxt->state->value = oldvalue; |
| 7435 | ctxt->state->endvalue = oldend; |
| 7436 | break; |
| 7437 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7438 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7439 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 7440 | if (ret != 0) { |
| 7441 | break; |
| 7442 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7443 | /* no break on purpose */ |
| 7444 | case XML_RELAXNG_ZEROORMORE: { |
| 7445 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7446 | |
| 7447 | oldflags = ctxt->flags; |
| 7448 | ctxt->flags |= FLAGS_IGNORABLE; |
| 7449 | cur = ctxt->state->value; |
| 7450 | temp = NULL; |
| 7451 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 7452 | (temp != cur)) { |
| 7453 | temp = cur; |
| 7454 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 7455 | if (ret != 0) { |
| 7456 | ctxt->state->value = temp; |
| 7457 | ret = 0; |
| 7458 | break; |
| 7459 | } |
| 7460 | cur = ctxt->state->value; |
| 7461 | } |
| 7462 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7463 | if (ret != 0) { |
| 7464 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7465 | xmlRelaxNGDumpValidError(ctxt); |
| 7466 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 7467 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7468 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7469 | break; |
| 7470 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7471 | case XML_RELAXNG_EXCEPT: { |
| 7472 | xmlRelaxNGDefinePtr list; |
| 7473 | |
| 7474 | list = define->content; |
| 7475 | while (list != NULL) { |
| 7476 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 7477 | if (ret == 0) { |
| 7478 | ret = -1; |
| 7479 | break; |
| 7480 | } else |
| 7481 | ret = 0; |
| 7482 | list = list->next; |
| 7483 | } |
| 7484 | break; |
| 7485 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 7486 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7487 | case XML_RELAXNG_GROUP: { |
| 7488 | xmlRelaxNGDefinePtr list; |
| 7489 | |
| 7490 | list = define->content; |
| 7491 | while (list != NULL) { |
| 7492 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 7493 | if (ret != 0) { |
| 7494 | ret = -1; |
| 7495 | break; |
| 7496 | } else |
| 7497 | ret = 0; |
| 7498 | list = list->next; |
| 7499 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 7500 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7501 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 7502 | case XML_RELAXNG_REF: |
| 7503 | case XML_RELAXNG_PARENTREF: |
| 7504 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 7505 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7506 | default: |
| 7507 | TODO |
| 7508 | ret = -1; |
| 7509 | } |
| 7510 | return(ret); |
| 7511 | } |
| 7512 | |
| 7513 | /** |
| 7514 | * xmlRelaxNGValidateValueContent: |
| 7515 | * @ctxt: a Relax-NG validation context |
| 7516 | * @defines: the list of definitions to verify |
| 7517 | * |
| 7518 | * Validate the given definitions for the current value |
| 7519 | * |
| 7520 | * Returns 0 if the validation succeeded or an error code. |
| 7521 | */ |
| 7522 | static int |
| 7523 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 7524 | xmlRelaxNGDefinePtr defines) { |
| 7525 | int ret = 0; |
| 7526 | |
| 7527 | while (defines != NULL) { |
| 7528 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 7529 | if (ret != 0) |
| 7530 | break; |
| 7531 | defines = defines->next; |
| 7532 | } |
| 7533 | return(ret); |
| 7534 | } |
| 7535 | |
| 7536 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7537 | * xmlRelaxNGAttributeMatch: |
| 7538 | * @ctxt: a Relax-NG validation context |
| 7539 | * @define: the definition to check |
| 7540 | * @prop: the attribute |
| 7541 | * |
| 7542 | * Check if the attribute matches the definition nameClass |
| 7543 | * |
| 7544 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 7545 | */ |
| 7546 | static int |
| 7547 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 7548 | xmlRelaxNGDefinePtr define, |
| 7549 | xmlAttrPtr prop) { |
| 7550 | int ret; |
| 7551 | |
| 7552 | if (define->name != NULL) { |
| 7553 | if (!xmlStrEqual(define->name, prop->name)) |
| 7554 | return(0); |
| 7555 | } |
| 7556 | if (define->ns != NULL) { |
| 7557 | if (define->ns[0] == 0) { |
| 7558 | if (prop->ns != NULL) |
| 7559 | return(0); |
| 7560 | } else { |
| 7561 | if ((prop->ns == NULL) || |
| 7562 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 7563 | return(0); |
| 7564 | } |
| 7565 | } |
| 7566 | if (define->nameClass == NULL) |
| 7567 | return(1); |
| 7568 | define = define->nameClass; |
| 7569 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 7570 | xmlRelaxNGDefinePtr list; |
| 7571 | |
| 7572 | list = define->content; |
| 7573 | while (list != NULL) { |
| 7574 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 7575 | if (ret == 1) |
| 7576 | return(0); |
| 7577 | if (ret < 0) |
| 7578 | return(ret); |
| 7579 | list = list->next; |
| 7580 | } |
| 7581 | } else { |
| 7582 | TODO |
| 7583 | } |
| 7584 | return(1); |
| 7585 | } |
| 7586 | |
| 7587 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7588 | * xmlRelaxNGValidateAttribute: |
| 7589 | * @ctxt: a Relax-NG validation context |
| 7590 | * @define: the definition to verify |
| 7591 | * |
| 7592 | * Validate the given attribute definition for that node |
| 7593 | * |
| 7594 | * Returns 0 if the validation succeeded or an error code. |
| 7595 | */ |
| 7596 | static int |
| 7597 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 7598 | xmlRelaxNGDefinePtr define) { |
| 7599 | int ret = 0, i; |
| 7600 | xmlChar *value, *oldvalue; |
| 7601 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7602 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7603 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 7604 | if (ctxt->state->nbAttrLeft <= 0) |
| 7605 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7606 | if (define->name != NULL) { |
| 7607 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 7608 | tmp = ctxt->state->attrs[i]; |
| 7609 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 7610 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 7611 | (tmp->ns == NULL)) || |
| 7612 | ((tmp->ns != NULL) && |
| 7613 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 7614 | prop = tmp; |
| 7615 | break; |
| 7616 | } |
| 7617 | } |
| 7618 | } |
| 7619 | if (prop != NULL) { |
| 7620 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 7621 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7622 | oldseq = ctxt->state->seq; |
| 7623 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7624 | ctxt->state->value = value; |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 7625 | ctxt->state->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7626 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 7627 | if (ctxt->state->value != NULL) |
| 7628 | value = ctxt->state->value; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7629 | if (value != NULL) |
| 7630 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 7631 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7632 | ctxt->state->seq = oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7633 | if (ret == 0) { |
| 7634 | /* |
| 7635 | * flag the attribute as processed |
| 7636 | */ |
| 7637 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 7638 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7639 | } |
| 7640 | } else { |
| 7641 | ret = -1; |
| 7642 | } |
| 7643 | #ifdef DEBUG |
| 7644 | xmlGenericError(xmlGenericErrorContext, |
| 7645 | "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret); |
| 7646 | #endif |
| 7647 | } else { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7648 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 7649 | tmp = ctxt->state->attrs[i]; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 7650 | if ((tmp != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7651 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7652 | prop = tmp; |
| 7653 | break; |
| 7654 | } |
| 7655 | } |
| 7656 | if (prop != NULL) { |
| 7657 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 7658 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7659 | oldseq = ctxt->state->seq; |
| 7660 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7661 | ctxt->state->value = value; |
| 7662 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 7663 | if (ctxt->state->value != NULL) |
| 7664 | value = ctxt->state->value; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7665 | if (value != NULL) |
| 7666 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 7667 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7668 | ctxt->state->seq = oldseq; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7669 | if (ret == 0) { |
| 7670 | /* |
| 7671 | * flag the attribute as processed |
| 7672 | */ |
| 7673 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 7674 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7675 | } |
| 7676 | } else { |
| 7677 | ret = -1; |
| 7678 | } |
| 7679 | #ifdef DEBUG |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 7680 | if (define->ns != NULL) { |
| 7681 | xmlGenericError(xmlGenericErrorContext, |
| 7682 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 7683 | define->ns, ret); |
| 7684 | } else { |
| 7685 | xmlGenericError(xmlGenericErrorContext, |
| 7686 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 7687 | ret); |
| 7688 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7689 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7690 | } |
| 7691 | |
| 7692 | return(ret); |
| 7693 | } |
| 7694 | |
| 7695 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7696 | * xmlRelaxNGValidateAttributeList: |
| 7697 | * @ctxt: a Relax-NG validation context |
| 7698 | * @define: the list of definition to verify |
| 7699 | * |
| 7700 | * Validate the given node against the list of attribute definitions |
| 7701 | * |
| 7702 | * Returns 0 if the validation succeeded or an error code. |
| 7703 | */ |
| 7704 | static int |
| 7705 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7706 | xmlRelaxNGDefinePtr defines) { |
| 7707 | int ret = 0; |
| 7708 | while (defines != NULL) { |
| 7709 | if (xmlRelaxNGValidateAttribute(ctxt, defines) != 0) |
| 7710 | ret = -1; |
| 7711 | defines = defines->next; |
| 7712 | } |
| 7713 | return(ret); |
| 7714 | } |
| 7715 | |
| 7716 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 7717 | * xmlRelaxNGNodeMatchesList: |
| 7718 | * @node: the node |
| 7719 | * @list: a NULL terminated array of definitions |
| 7720 | * |
| 7721 | * Check if a node can be matched by one of the definitions |
| 7722 | * |
| 7723 | * Returns 1 if matches 0 otherwise |
| 7724 | */ |
| 7725 | static int |
| 7726 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) { |
| 7727 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 7728 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 7729 | |
| 7730 | if ((node == NULL) || (list == NULL)) |
| 7731 | return(0); |
| 7732 | |
| 7733 | cur = list[i++]; |
| 7734 | while (cur != NULL) { |
| 7735 | if ((node->type == XML_ELEMENT_NODE) && |
| 7736 | (cur->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 7737 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 7738 | if (tmp == 1) |
| 7739 | return(1); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7740 | } else if (((node->type == XML_TEXT_NODE) || |
| 7741 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 7742 | (cur->type == XML_RELAXNG_TEXT)) { |
| 7743 | return(1); |
| 7744 | } |
| 7745 | cur = list[i++]; |
| 7746 | } |
| 7747 | return(0); |
| 7748 | } |
| 7749 | |
| 7750 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7751 | * xmlRelaxNGValidateInterleave: |
| 7752 | * @ctxt: a Relax-NG validation context |
| 7753 | * @define: the definition to verify |
| 7754 | * |
| 7755 | * Validate an interleave definition for a node. |
| 7756 | * |
| 7757 | * Returns 0 if the validation succeeded or an error code. |
| 7758 | */ |
| 7759 | static int |
| 7760 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 7761 | xmlRelaxNGDefinePtr define) { |
| 7762 | int ret = 0, i, nbgroups, left; |
| 7763 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 7764 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7765 | |
| 7766 | xmlRelaxNGValidStatePtr oldstate; |
| 7767 | xmlRelaxNGPartitionPtr partitions; |
| 7768 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 7769 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 7770 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 7771 | |
| 7772 | if (define->data != NULL) { |
| 7773 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 7774 | nbgroups = partitions->nbgroups; |
| 7775 | left = nbgroups; |
| 7776 | } else { |
| 7777 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 7778 | return(-1); |
| 7779 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 7780 | /* |
| 7781 | * Optimizations for MIXED |
| 7782 | */ |
| 7783 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 7784 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 7785 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 7786 | if (nbgroups == 2) { |
| 7787 | /* |
| 7788 | * this is a pure <mixed> case |
| 7789 | */ |
| 7790 | if (ctxt->state != NULL) |
| 7791 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 7792 | ctxt->state->seq); |
| 7793 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 7794 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 7795 | partitions->groups[1]->rule); |
| 7796 | else |
| 7797 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 7798 | partitions->groups[0]->rule); |
| 7799 | if (ret == 0) { |
| 7800 | if (ctxt->state != NULL) |
| 7801 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 7802 | ctxt->state->seq); |
| 7803 | } |
| 7804 | ctxt->flags = oldflags; |
| 7805 | return(ret); |
| 7806 | } |
| 7807 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7808 | |
| 7809 | /* |
| 7810 | * Build arrays to store the first and last node of the chain |
| 7811 | * pertaining to each group |
| 7812 | */ |
| 7813 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 7814 | if (list == NULL) { |
| 7815 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7816 | return(-1); |
| 7817 | } |
| 7818 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 7819 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 7820 | if (lasts == NULL) { |
| 7821 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7822 | return(-1); |
| 7823 | } |
| 7824 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 7825 | |
| 7826 | /* |
| 7827 | * Walk the sequence of children finding the right group and |
| 7828 | * sorting them in sequences. |
| 7829 | */ |
| 7830 | cur = ctxt->state->seq; |
| 7831 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 7832 | start = cur; |
| 7833 | while (cur != NULL) { |
| 7834 | ctxt->state->seq = cur; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 7835 | if ((partitions->triage != NULL) && |
| 7836 | (partitions->flags & IS_DETERMINIST)) { |
| 7837 | void *tmp = NULL; |
| 7838 | |
| 7839 | if ((cur->type == XML_TEXT_NODE) || |
| 7840 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 7841 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 7842 | NULL); |
| 7843 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 7844 | if (cur->ns != NULL) { |
| 7845 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 7846 | cur->ns->href); |
| 7847 | if (tmp == NULL) |
| 7848 | tmp = xmlHashLookup2(partitions->triage, |
| 7849 | BAD_CAST "#any", cur->ns->href); |
| 7850 | } else |
| 7851 | tmp = xmlHashLookup2(partitions->triage, cur->name, NULL); |
| 7852 | if (tmp == NULL) |
| 7853 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 7854 | NULL); |
| 7855 | } |
| 7856 | |
| 7857 | if (tmp == NULL) { |
| 7858 | i = nbgroups; |
| 7859 | } else { |
| 7860 | i = ((long) tmp) - 1; |
| 7861 | if (partitions->flags & IS_NEEDCHECK) { |
| 7862 | group = partitions->groups[i]; |
| 7863 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 7864 | i = nbgroups; |
| 7865 | } |
| 7866 | } |
| 7867 | } else { |
| 7868 | for (i = 0;i < nbgroups;i++) { |
| 7869 | group = partitions->groups[i]; |
| 7870 | if (group == NULL) |
| 7871 | continue; |
| 7872 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 7873 | break; |
| 7874 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7875 | } |
| 7876 | /* |
| 7877 | * We break as soon as an element not matched is found |
| 7878 | */ |
| 7879 | if (i >= nbgroups) { |
| 7880 | break; |
| 7881 | } |
| 7882 | if (lasts[i] != NULL) { |
| 7883 | lasts[i]->next = cur; |
| 7884 | lasts[i] = cur; |
| 7885 | } else { |
| 7886 | list[i] = cur; |
| 7887 | lasts[i] = cur; |
| 7888 | } |
| 7889 | if (cur->next != NULL) |
| 7890 | lastchg = cur->next; |
| 7891 | else |
| 7892 | lastchg = cur; |
| 7893 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
| 7894 | } |
| 7895 | if (ret != 0) { |
| 7896 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 7897 | ret = -1; |
| 7898 | goto done; |
| 7899 | } |
| 7900 | lastelem = cur; |
| 7901 | oldstate = ctxt->state; |
| 7902 | for (i = 0;i < nbgroups;i++) { |
| 7903 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 7904 | group = partitions->groups[i]; |
| 7905 | if (lasts[i] != NULL) { |
| 7906 | last = lasts[i]->next; |
| 7907 | lasts[i]->next = NULL; |
| 7908 | } |
| 7909 | ctxt->state->seq = list[i]; |
| 7910 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 7911 | if (ret != 0) |
| 7912 | break; |
| 7913 | if (ctxt->state != NULL) { |
| 7914 | cur = ctxt->state->seq; |
| 7915 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 7916 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7917 | oldstate = ctxt->state; |
| 7918 | ctxt->state = NULL; |
| 7919 | if (cur != NULL) { |
| 7920 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 7921 | ret = -1; |
| 7922 | ctxt->state = oldstate; |
| 7923 | goto done; |
| 7924 | } |
| 7925 | } else if (ctxt->states != NULL) { |
| 7926 | int j; |
| 7927 | int found = 0; |
| 7928 | |
| 7929 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 7930 | cur = ctxt->states->tabState[j]->seq; |
| 7931 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 7932 | if (cur == NULL) { |
| 7933 | found = 1; |
| 7934 | break; |
| 7935 | } |
| 7936 | } |
| 7937 | if (ctxt->states->nbState > 0) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 7938 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7939 | oldstate = ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 7940 | } |
| 7941 | for (j = 0;j < ctxt->states->nbState - 1;j++) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 7942 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[j]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7943 | } |
| 7944 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 7945 | ctxt->states = NULL; |
| 7946 | if (found == 0) { |
| 7947 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 7948 | ret = -1; |
| 7949 | ctxt->state = oldstate; |
| 7950 | goto done; |
| 7951 | } |
| 7952 | } else { |
| 7953 | ret = -1; |
| 7954 | break; |
| 7955 | } |
| 7956 | if (lasts[i] != NULL) { |
| 7957 | lasts[i]->next = last; |
| 7958 | } |
| 7959 | } |
| 7960 | if (ctxt->state != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 7961 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7962 | ctxt->state = oldstate; |
| 7963 | ctxt->state->seq = lastelem; |
| 7964 | if (ret != 0) { |
| 7965 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 7966 | ret = -1; |
| 7967 | goto done; |
| 7968 | } |
| 7969 | |
| 7970 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 7971 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7972 | /* |
| 7973 | * builds the next links chain from the prev one |
| 7974 | */ |
| 7975 | cur = lastchg; |
| 7976 | while (cur != NULL) { |
| 7977 | if ((cur == start) || (cur->prev == NULL)) |
| 7978 | break; |
| 7979 | cur->prev->next = cur; |
| 7980 | cur = cur->prev; |
| 7981 | } |
| 7982 | if (ret == 0) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 7983 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7984 | } |
| 7985 | |
| 7986 | xmlFree(list); |
| 7987 | xmlFree(lasts); |
| 7988 | return(ret); |
| 7989 | } |
| 7990 | |
| 7991 | /** |
| 7992 | * xmlRelaxNGValidateDefinitionList: |
| 7993 | * @ctxt: a Relax-NG validation context |
| 7994 | * @define: the list of definition to verify |
| 7995 | * |
| 7996 | * Validate the given node content against the (list) of definitions |
| 7997 | * |
| 7998 | * Returns 0 if the validation succeeded or an error code. |
| 7999 | */ |
| 8000 | static int |
| 8001 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8002 | xmlRelaxNGDefinePtr defines) { |
| 8003 | int ret = 0, res; |
| 8004 | |
| 8005 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 8006 | if (defines == NULL) { |
| 8007 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, BAD_CAST "NULL definition list"); |
| 8008 | return(-1); |
| 8009 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8010 | while (defines != NULL) { |
| 8011 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 8012 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 8013 | if (res < 0) |
| 8014 | ret = -1; |
| 8015 | } else { |
| 8016 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8017 | return(-1); |
| 8018 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8019 | if (res == -1) /* continues on -2 */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8020 | break; |
| 8021 | defines = defines->next; |
| 8022 | } |
| 8023 | |
| 8024 | return(ret); |
| 8025 | } |
| 8026 | |
| 8027 | /** |
| 8028 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8029 | * @ctxt: a Relax-NG validation context |
| 8030 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8031 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8032 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8033 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8034 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8035 | * 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] | 8036 | */ |
| 8037 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8038 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8039 | xmlRelaxNGDefinePtr define, |
| 8040 | xmlNodePtr elem) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8041 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8042 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8043 | if (define->name != NULL) { |
| 8044 | if (!xmlStrEqual(elem->name, define->name)) { |
| 8045 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 8046 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8047 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8048 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8049 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
| 8050 | if (elem->ns == NULL) { |
| 8051 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, |
| 8052 | elem->name); |
| 8053 | return(0); |
| 8054 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 8055 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 8056 | elem->name, define->ns); |
| 8057 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8058 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8059 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
| 8060 | (define->name == NULL)) { |
| 8061 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 8062 | elem->name); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8063 | return(0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8064 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
| 8065 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 8066 | define->name); |
| 8067 | return(0); |
| 8068 | } |
| 8069 | |
| 8070 | if (define->nameClass == NULL) |
| 8071 | return(1); |
| 8072 | |
| 8073 | define = define->nameClass; |
| 8074 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 8075 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8076 | if (ctxt != NULL) { |
| 8077 | oldflags = ctxt->flags; |
| 8078 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8079 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8080 | |
| 8081 | list = define->content; |
| 8082 | while (list != NULL) { |
| 8083 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 8084 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8085 | if (ctxt != NULL) |
| 8086 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8087 | return(0); |
| 8088 | } |
| 8089 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8090 | if (ctxt != NULL) |
| 8091 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8092 | return(ret); |
| 8093 | } |
| 8094 | list = list->next; |
| 8095 | } |
| 8096 | ret = 1; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8097 | if (ctxt != NULL) { |
| 8098 | ctxt->flags = oldflags; |
| 8099 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8100 | } else if (define->type == XML_RELAXNG_CHOICE) { |
| 8101 | xmlRelaxNGDefinePtr list; |
| 8102 | |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8103 | if (ctxt != NULL) { |
| 8104 | oldflags = ctxt->flags; |
| 8105 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8106 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8107 | |
| 8108 | list = define->nameClass; |
| 8109 | while (list != NULL) { |
| 8110 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 8111 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8112 | if (ctxt != NULL) |
| 8113 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8114 | return(1); |
| 8115 | } |
| 8116 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8117 | if (ctxt != NULL) |
| 8118 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8119 | return(ret); |
| 8120 | } |
| 8121 | list = list->next; |
| 8122 | } |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8123 | if (ctxt != NULL) { |
| 8124 | if (ret != 0) { |
| 8125 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8126 | xmlRelaxNGDumpValidError(ctxt); |
| 8127 | } else { |
| 8128 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
| 8129 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8130 | } |
| 8131 | ret = 0; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8132 | if (ctxt != NULL) { |
| 8133 | ctxt->flags = oldflags; |
| 8134 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8135 | } else { |
| 8136 | TODO |
| 8137 | ret = -1; |
| 8138 | } |
| 8139 | return(ret); |
| 8140 | } |
| 8141 | |
| 8142 | /** |
| 8143 | * xmlRelaxNGValidateElementEnd: |
| 8144 | * @ctxt: a Relax-NG validation context |
| 8145 | * |
| 8146 | * Validate the end of the element, implements check that |
| 8147 | * there is nothing left not consumed in the element content |
| 8148 | * or in the attribute list. |
| 8149 | * |
| 8150 | * Returns 0 if the validation succeeded or an error code. |
| 8151 | */ |
| 8152 | static int |
| 8153 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt) { |
| 8154 | int ret = 0, i; |
| 8155 | xmlRelaxNGValidStatePtr state; |
| 8156 | |
| 8157 | state = ctxt->state; |
| 8158 | if (state->seq != NULL) { |
| 8159 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 8160 | if (state->seq != NULL) { |
| 8161 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 8162 | state->node->name, state->seq->name); |
| 8163 | ret = -1; |
| 8164 | } |
| 8165 | } |
| 8166 | for (i = 0;i < state->nbAttrs;i++) { |
| 8167 | if (state->attrs[i] != NULL) { |
| 8168 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 8169 | state->attrs[i]->name, state->node->name); |
| 8170 | ret = -1; |
| 8171 | } |
| 8172 | } |
| 8173 | return(ret); |
| 8174 | } |
| 8175 | |
| 8176 | /** |
| 8177 | * xmlRelaxNGValidateState: |
| 8178 | * @ctxt: a Relax-NG validation context |
| 8179 | * @define: the definition to verify |
| 8180 | * |
| 8181 | * Validate the current state against the definition |
| 8182 | * |
| 8183 | * Returns 0 if the validation succeeded or an error code. |
| 8184 | */ |
| 8185 | static int |
| 8186 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 8187 | xmlRelaxNGDefinePtr define) { |
| 8188 | xmlNodePtr node; |
| 8189 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 8190 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8191 | |
| 8192 | if (define == NULL) { |
| 8193 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 8194 | return(-1); |
| 8195 | } |
| 8196 | |
| 8197 | if (ctxt->state != NULL) { |
| 8198 | node = ctxt->state->seq; |
| 8199 | } else { |
| 8200 | node = NULL; |
| 8201 | } |
| 8202 | #ifdef DEBUG |
| 8203 | for (i = 0;i < ctxt->depth;i++) |
| 8204 | xmlGenericError(xmlGenericErrorContext, " "); |
| 8205 | xmlGenericError(xmlGenericErrorContext, |
| 8206 | "Start validating %s ", xmlRelaxNGDefName(define)); |
| 8207 | if (define->name != NULL) |
| 8208 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
| 8209 | if ((node != NULL) && (node->name != NULL)) |
| 8210 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
| 8211 | else |
| 8212 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 8213 | #endif |
| 8214 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8215 | switch (define->type) { |
| 8216 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8217 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 8218 | ret = 0; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8219 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8220 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8221 | ret = -1; |
| 8222 | break; |
| 8223 | case XML_RELAXNG_TEXT: |
| 8224 | while ((node != NULL) && |
| 8225 | ((node->type == XML_TEXT_NODE) || |
| 8226 | (node->type == XML_COMMENT_NODE) || |
| 8227 | (node->type == XML_PI_NODE) || |
| 8228 | (node->type == XML_CDATA_SECTION_NODE))) |
| 8229 | node = node->next; |
| 8230 | ctxt->state->seq = node; |
| 8231 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8232 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8233 | errNr = ctxt->errNr; |
| 8234 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 8235 | if (node == NULL) { |
| 8236 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 8237 | ret = -1; |
| 8238 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8239 | xmlRelaxNGDumpValidError(ctxt); |
| 8240 | break; |
| 8241 | } |
| 8242 | if (node->type != XML_ELEMENT_NODE) { |
| 8243 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 8244 | ret = -1; |
| 8245 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8246 | xmlRelaxNGDumpValidError(ctxt); |
| 8247 | break; |
| 8248 | } |
| 8249 | /* |
| 8250 | * This node was already validated successfully against |
| 8251 | * this definition. |
| 8252 | */ |
| 8253 | if (node->_private == define) { |
| 8254 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8255 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8256 | if (ctxt->errNr != 0) { |
| 8257 | while ((ctxt->err != NULL) && |
| 8258 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 8259 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8260 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 8261 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8262 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 8263 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 8264 | xmlRelaxNGValidErrorPop(ctxt); |
| 8265 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8266 | break; |
| 8267 | } |
| 8268 | |
| 8269 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 8270 | if (ret <= 0) { |
| 8271 | ret = -1; |
| 8272 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8273 | xmlRelaxNGDumpValidError(ctxt); |
| 8274 | break; |
| 8275 | } |
| 8276 | ret = 0; |
| 8277 | if (ctxt->errNr != 0) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8278 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8279 | while ((ctxt->err != NULL) && |
| 8280 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 8281 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8282 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 8283 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8284 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 8285 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 8286 | xmlRelaxNGValidErrorPop(ctxt); |
| 8287 | } |
| 8288 | errNr = ctxt->errNr; |
| 8289 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8290 | oldflags = ctxt->flags; |
| 8291 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 8292 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 8293 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8294 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 8295 | if (state == NULL) { |
| 8296 | ret = -1; |
| 8297 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8298 | xmlRelaxNGDumpValidError(ctxt); |
| 8299 | break; |
| 8300 | } |
| 8301 | |
| 8302 | oldstate = ctxt->state; |
| 8303 | ctxt->state = state; |
| 8304 | if (define->attrs != NULL) { |
| 8305 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 8306 | if (tmp != 0) { |
| 8307 | ret = -1; |
| 8308 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 8309 | } |
| 8310 | } |
| 8311 | if (define->content != NULL) { |
| 8312 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 8313 | if (tmp != 0) { |
| 8314 | ret = -1; |
| 8315 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, node->name); |
| 8316 | } |
| 8317 | } |
| 8318 | if (ctxt->states != NULL) { |
| 8319 | tmp = -1; |
| 8320 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8321 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8322 | |
| 8323 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 8324 | state = ctxt->states->tabState[i]; |
| 8325 | ctxt->state = state; |
| 8326 | |
| 8327 | if (xmlRelaxNGValidateElementEnd(ctxt) == 0) |
| 8328 | tmp = 0; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8329 | xmlRelaxNGFreeValidState(ctxt,state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8330 | } |
| 8331 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8332 | ctxt->flags = oldflags; |
| 8333 | ctxt->states = NULL; |
| 8334 | if ((ret == 0) && (tmp == -1)) |
| 8335 | ret = -1; |
| 8336 | } else { |
| 8337 | state = ctxt->state; |
| 8338 | if (ret == 0) |
| 8339 | ret = xmlRelaxNGValidateElementEnd(ctxt); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8340 | xmlRelaxNGFreeValidState(ctxt,state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8341 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8342 | if (ret == 0) { |
| 8343 | node->_private = define; |
| 8344 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8345 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8346 | ctxt->state = oldstate; |
| 8347 | if (oldstate != NULL) |
| 8348 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8349 | if (ret != 0) { |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8350 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8351 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8352 | ret = 0; |
| 8353 | } else { |
| 8354 | ret = -2; |
| 8355 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8356 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8357 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8358 | } |
| 8359 | |
| 8360 | #ifdef DEBUG |
| 8361 | xmlGenericError(xmlGenericErrorContext, |
| 8362 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 8363 | node->name, ret); |
| 8364 | if (oldstate == NULL) |
| 8365 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 8366 | else if (oldstate->seq == NULL) |
| 8367 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 8368 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 8369 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 8370 | oldstate->seq->name); |
| 8371 | else |
| 8372 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 8373 | oldstate->seq->name, oldstate->seq->type); |
| 8374 | #endif |
| 8375 | break; |
| 8376 | case XML_RELAXNG_OPTIONAL: { |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8377 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8378 | oldflags = ctxt->flags; |
| 8379 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8380 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 8381 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 8382 | if (ret != 0) { |
| 8383 | if (ctxt->state != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8384 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8385 | ctxt->state = oldstate; |
| 8386 | ctxt->flags = oldflags; |
| 8387 | ret = 0; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8388 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8389 | break; |
| 8390 | } |
| 8391 | if (ctxt->states != NULL) { |
| 8392 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 8393 | } else { |
| 8394 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 8395 | if (ctxt->states == NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8396 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8397 | ctxt->flags = oldflags; |
| 8398 | ret = -1; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8399 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8400 | break; |
| 8401 | } |
| 8402 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 8403 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 8404 | ctxt->state = NULL; |
| 8405 | } |
| 8406 | ctxt->flags = oldflags; |
| 8407 | ret = 0; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8408 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8409 | break; |
| 8410 | } |
| 8411 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8412 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8413 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 8414 | if (ret != 0) { |
| 8415 | break; |
| 8416 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8417 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8418 | /* no break on purpose */ |
| 8419 | case XML_RELAXNG_ZEROORMORE: { |
| 8420 | int progress; |
| 8421 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 8422 | int base, j; |
| 8423 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8424 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8425 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 8426 | if (res == NULL) { |
| 8427 | ret = -1; |
| 8428 | break; |
| 8429 | } |
| 8430 | /* |
| 8431 | * All the input states are also exit states |
| 8432 | */ |
| 8433 | if (ctxt->state != NULL) { |
| 8434 | xmlRelaxNGAddStates(ctxt, res, |
| 8435 | xmlRelaxNGCopyValidState(ctxt, ctxt->state)); |
| 8436 | } else { |
| 8437 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 8438 | xmlRelaxNGAddStates(ctxt, res, |
| 8439 | xmlRelaxNGCopyValidState(ctxt, |
| 8440 | ctxt->states->tabState[j])); |
| 8441 | } |
| 8442 | } |
| 8443 | oldflags = ctxt->flags; |
| 8444 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8445 | do { |
| 8446 | progress = 0; |
| 8447 | base = res->nbState; |
| 8448 | |
| 8449 | if (ctxt->states != NULL) { |
| 8450 | states = ctxt->states; |
| 8451 | for (i = 0;i < states->nbState;i++) { |
| 8452 | ctxt->state = states->tabState[i]; |
| 8453 | ctxt->states = NULL; |
| 8454 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 8455 | define->content); |
| 8456 | if (ret == 0) { |
| 8457 | if (ctxt->state != NULL) { |
| 8458 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 8459 | ctxt->state); |
| 8460 | ctxt->state = NULL; |
| 8461 | if (tmp == 1) |
| 8462 | progress = 1; |
| 8463 | } else if (ctxt->states != NULL) { |
| 8464 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 8465 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 8466 | ctxt->states->tabState[j]); |
| 8467 | if (tmp == 1) |
| 8468 | progress = 1; |
| 8469 | } |
| 8470 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8471 | ctxt->states = NULL; |
| 8472 | } |
| 8473 | } else { |
| 8474 | if (ctxt->state != NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8475 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8476 | ctxt->state = NULL; |
| 8477 | } |
| 8478 | } |
| 8479 | } |
| 8480 | } else { |
| 8481 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 8482 | define->content); |
| 8483 | if (ret != 0) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8484 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8485 | ctxt->state = NULL; |
| 8486 | } else { |
| 8487 | base = res->nbState; |
| 8488 | if (ctxt->state != NULL) { |
| 8489 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 8490 | ctxt->state); |
| 8491 | ctxt->state = NULL; |
| 8492 | if (tmp == 1) |
| 8493 | progress = 1; |
| 8494 | } else if (ctxt->states != NULL) { |
| 8495 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 8496 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 8497 | ctxt->states->tabState[j]); |
| 8498 | if (tmp == 1) |
| 8499 | progress = 1; |
| 8500 | } |
| 8501 | if (states == NULL) { |
| 8502 | states = ctxt->states; |
| 8503 | } else { |
| 8504 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8505 | } |
| 8506 | ctxt->states = NULL; |
| 8507 | } |
| 8508 | } |
| 8509 | } |
| 8510 | if (progress) { |
| 8511 | /* |
| 8512 | * Collect all the new nodes added at that step |
| 8513 | * and make them the new node set |
| 8514 | */ |
| 8515 | if (res->nbState - base == 1) { |
| 8516 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 8517 | res->tabState[base]); |
| 8518 | } else { |
| 8519 | if (states == NULL) { |
| 8520 | xmlRelaxNGNewStates(ctxt, res->nbState - base); |
| 8521 | } |
| 8522 | states->nbState = 0; |
| 8523 | for (i = base;i < res->nbState;i++) |
| 8524 | xmlRelaxNGAddStates(ctxt, states, |
| 8525 | xmlRelaxNGCopyValidState(ctxt, |
| 8526 | res->tabState[i])); |
| 8527 | ctxt->states = states; |
| 8528 | } |
| 8529 | } |
| 8530 | } while (progress == 1); |
| 8531 | if (states != NULL) { |
| 8532 | xmlRelaxNGFreeStates(ctxt, states); |
| 8533 | } |
| 8534 | ctxt->states = res; |
| 8535 | ctxt->flags = oldflags; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8536 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8537 | ret = 0; |
| 8538 | break; |
| 8539 | } |
| 8540 | case XML_RELAXNG_CHOICE: { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8541 | xmlRelaxNGDefinePtr list = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8542 | xmlRelaxNGStatesPtr states = NULL; |
| 8543 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8544 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8545 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8546 | errNr = ctxt->errNr; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8547 | if ((define->dflags & IS_TRIABLE) && (define->data != NULL)) { |
| 8548 | xmlHashTablePtr triage = (xmlHashTablePtr) define->data; |
| 8549 | |
| 8550 | /* |
| 8551 | * Something we can optimize cleanly there is only one |
| 8552 | * possble branch out ! |
| 8553 | */ |
| 8554 | if (node == NULL) { |
| 8555 | ret = -1; |
| 8556 | break; |
| 8557 | } |
| 8558 | if ((node->type == XML_TEXT_NODE) || |
| 8559 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 8560 | list = xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 8561 | } else if (node->type == XML_ELEMENT_NODE) { |
| 8562 | if (node->ns != NULL) { |
| 8563 | list = xmlHashLookup2(triage, node->name, |
| 8564 | node->ns->href); |
| 8565 | if (list == NULL) |
| 8566 | list = xmlHashLookup2(triage, BAD_CAST "#any", |
| 8567 | node->ns->href); |
| 8568 | } else |
| 8569 | list = xmlHashLookup2(triage, node->name, NULL); |
| 8570 | if (list == NULL) |
| 8571 | list = xmlHashLookup2(triage, BAD_CAST "#any", NULL); |
| 8572 | } |
| 8573 | if (list == NULL) { |
| 8574 | ret = -1; |
| 8575 | break; |
| 8576 | } |
| 8577 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8578 | if (ret == 0) { |
| 8579 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8580 | break; |
| 8581 | } |
| 8582 | |
| 8583 | list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8584 | oldflags = ctxt->flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8585 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8586 | |
| 8587 | while (list != NULL) { |
| 8588 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 8589 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 8590 | if (ret == 0) { |
| 8591 | if (states == NULL) { |
| 8592 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 8593 | } |
| 8594 | if (ctxt->state != NULL) { |
| 8595 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 8596 | } else if (ctxt->states != NULL) { |
| 8597 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 8598 | xmlRelaxNGAddStates(ctxt, states, |
| 8599 | ctxt->states->tabState[i]); |
| 8600 | } |
| 8601 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8602 | ctxt->states = NULL; |
| 8603 | } |
| 8604 | } else { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8605 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8606 | } |
| 8607 | ctxt->state = oldstate; |
| 8608 | list = list->next; |
| 8609 | } |
| 8610 | if (states != NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8611 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8612 | ctxt->states = states; |
| 8613 | ctxt->state = NULL; |
| 8614 | ret = 0; |
| 8615 | } else { |
| 8616 | ctxt->states = NULL; |
| 8617 | } |
| 8618 | ctxt->flags = oldflags; |
| 8619 | if (ret != 0) { |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8620 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8621 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8622 | } |
| 8623 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8624 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8625 | } |
| 8626 | break; |
| 8627 | } |
| 8628 | case XML_RELAXNG_DEF: |
| 8629 | case XML_RELAXNG_GROUP: |
| 8630 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8631 | break; |
| 8632 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8633 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8634 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8635 | case XML_RELAXNG_ATTRIBUTE: |
| 8636 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 8637 | break; |
| 8638 | case XML_RELAXNG_NOOP: |
| 8639 | case XML_RELAXNG_REF: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8640 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8641 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 8642 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 8643 | case XML_RELAXNG_PARENTREF: |
| 8644 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 8645 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8646 | case XML_RELAXNG_DATATYPE: { |
| 8647 | xmlNodePtr child; |
| 8648 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8649 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8650 | child = node; |
| 8651 | while (child != NULL) { |
| 8652 | if (child->type == XML_ELEMENT_NODE) { |
| 8653 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 8654 | node->parent->name); |
| 8655 | ret = -1; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8656 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8657 | } else if ((child->type == XML_TEXT_NODE) || |
| 8658 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 8659 | content = xmlStrcat(content, child->content); |
| 8660 | } |
| 8661 | /* TODO: handle entities ... */ |
| 8662 | child = child->next; |
| 8663 | } |
| 8664 | if (ret == -1) { |
| 8665 | if (content != NULL) |
| 8666 | xmlFree(content); |
| 8667 | break; |
| 8668 | } |
| 8669 | if (content == NULL) { |
| 8670 | content = xmlStrdup(BAD_CAST ""); |
| 8671 | if (content == NULL) { |
| 8672 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8673 | ret = -1; |
| 8674 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8675 | } |
| 8676 | } |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8677 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 8678 | ctxt->state->seq); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8679 | if (ret == -1) { |
| 8680 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 8681 | } else if (ret == 0) { |
| 8682 | ctxt->state->seq = NULL; |
| 8683 | } |
| 8684 | if (content != NULL) |
| 8685 | xmlFree(content); |
| 8686 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8687 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8688 | case XML_RELAXNG_VALUE: { |
| 8689 | xmlChar *content = NULL; |
| 8690 | xmlChar *oldvalue; |
| 8691 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8692 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8693 | child = node; |
| 8694 | while (child != NULL) { |
| 8695 | if (child->type == XML_ELEMENT_NODE) { |
| 8696 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 8697 | node->parent->name); |
| 8698 | ret = -1; |
| 8699 | break; |
| 8700 | } else if ((child->type == XML_TEXT_NODE) || |
| 8701 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 8702 | content = xmlStrcat(content, child->content); |
| 8703 | } |
| 8704 | /* TODO: handle entities ... */ |
| 8705 | child = child->next; |
| 8706 | } |
| 8707 | if (ret == -1) { |
| 8708 | if (content != NULL) |
| 8709 | xmlFree(content); |
| 8710 | break; |
| 8711 | } |
| 8712 | if (content == NULL) { |
| 8713 | content = xmlStrdup(BAD_CAST ""); |
| 8714 | if (content == NULL) { |
| 8715 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8716 | ret = -1; |
| 8717 | break; |
| 8718 | } |
| 8719 | } |
| 8720 | oldvalue = ctxt->state->value; |
| 8721 | ctxt->state->value = content; |
| 8722 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 8723 | ctxt->state->value = oldvalue; |
| 8724 | if (ret == -1) { |
| 8725 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 8726 | } else if (ret == 0) { |
| 8727 | ctxt->state->seq = NULL; |
| 8728 | } |
| 8729 | if (content != NULL) |
| 8730 | xmlFree(content); |
| 8731 | break; |
| 8732 | } |
| 8733 | case XML_RELAXNG_LIST: { |
| 8734 | xmlChar *content; |
| 8735 | xmlNodePtr child; |
| 8736 | xmlChar *oldvalue, *oldendvalue; |
| 8737 | int len; |
| 8738 | |
| 8739 | /* |
| 8740 | * Make sure it's only text nodes |
| 8741 | */ |
| 8742 | |
| 8743 | content = NULL; |
| 8744 | child = node; |
| 8745 | while (child != NULL) { |
| 8746 | if (child->type == XML_ELEMENT_NODE) { |
| 8747 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 8748 | node->parent->name); |
| 8749 | ret = -1; |
| 8750 | break; |
| 8751 | } else if ((child->type == XML_TEXT_NODE) || |
| 8752 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 8753 | content = xmlStrcat(content, child->content); |
| 8754 | } |
| 8755 | /* TODO: handle entities ... */ |
| 8756 | child = child->next; |
| 8757 | } |
| 8758 | if (ret == -1) { |
| 8759 | if (content != NULL) |
| 8760 | xmlFree(content); |
| 8761 | break; |
| 8762 | } |
| 8763 | if (content == NULL) { |
| 8764 | content = xmlStrdup(BAD_CAST ""); |
| 8765 | if (content == NULL) { |
| 8766 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8767 | ret = -1; |
| 8768 | break; |
| 8769 | } |
| 8770 | } |
| 8771 | len = xmlStrlen(content); |
| 8772 | oldvalue = ctxt->state->value; |
| 8773 | oldendvalue = ctxt->state->endvalue; |
| 8774 | ctxt->state->value = content; |
| 8775 | ctxt->state->endvalue = content + len; |
| 8776 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 8777 | ctxt->state->value = oldvalue; |
| 8778 | ctxt->state->endvalue = oldendvalue; |
| 8779 | if (ret == -1) { |
| 8780 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 8781 | } else if ((ret == 0) && (node != NULL)) { |
| 8782 | ctxt->state->seq = node->next; |
| 8783 | } |
| 8784 | if (content != NULL) |
| 8785 | xmlFree(content); |
| 8786 | break; |
| 8787 | } |
| 8788 | case XML_RELAXNG_START: |
| 8789 | case XML_RELAXNG_EXCEPT: |
| 8790 | case XML_RELAXNG_PARAM: |
| 8791 | TODO |
| 8792 | ret = -1; |
| 8793 | break; |
| 8794 | } |
| 8795 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8796 | #ifdef DEBUG |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8797 | for (i = 0;i < ctxt->depth;i++) |
| 8798 | xmlGenericError(xmlGenericErrorContext, " "); |
| 8799 | xmlGenericError(xmlGenericErrorContext, |
| 8800 | "Validating %s ", xmlRelaxNGDefName(define)); |
| 8801 | if (define->name != NULL) |
| 8802 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
| 8803 | if (ret == 0) |
| 8804 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
| 8805 | else |
| 8806 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8807 | #endif |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8808 | return(ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8809 | } |
| 8810 | |
| 8811 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8812 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8813 | * @ctxt: a Relax-NG validation context |
| 8814 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8815 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8816 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8817 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8818 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8819 | */ |
| 8820 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8821 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 8822 | xmlRelaxNGDefinePtr define) { |
| 8823 | xmlRelaxNGStatesPtr states, res; |
| 8824 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8825 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8826 | /* |
| 8827 | * We should NOT have both ctxt->state and ctxt->states |
| 8828 | */ |
| 8829 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 8830 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8831 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8832 | ctxt->state = NULL; |
| 8833 | } |
| 8834 | |
| 8835 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
| 8836 | if (ctxt->states != NULL) { |
| 8837 | ctxt->state = ctxt->states->tabState[0]; |
| 8838 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8839 | ctxt->states = NULL; |
| 8840 | } |
| 8841 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 8842 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 8843 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8844 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8845 | ctxt->state = NULL; |
| 8846 | } |
| 8847 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 8848 | ctxt->state = ctxt->states->tabState[0]; |
| 8849 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8850 | ctxt->states = NULL; |
| 8851 | } |
| 8852 | return(ret); |
| 8853 | } |
| 8854 | |
| 8855 | states = ctxt->states; |
| 8856 | ctxt->states = NULL; |
| 8857 | res = NULL; |
| 8858 | j = 0; |
| 8859 | oldflags = ctxt->flags; |
| 8860 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8861 | for (i = 0;i < states->nbState;i++) { |
| 8862 | ctxt->state = states->tabState[i]; |
| 8863 | ctxt->states = NULL; |
| 8864 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 8865 | /* |
| 8866 | * We should NOT have both ctxt->state and ctxt->states |
| 8867 | */ |
| 8868 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 8869 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8870 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8871 | ctxt->state = NULL; |
| 8872 | } |
| 8873 | if (ret == 0) { |
| 8874 | if (ctxt->states == NULL) { |
| 8875 | if (res != NULL) { |
| 8876 | /* add the state to the container */ |
| 8877 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 8878 | ctxt->state = NULL; |
| 8879 | } else { |
| 8880 | /* add the state directly in states */ |
| 8881 | states->tabState[j++] = ctxt->state; |
| 8882 | ctxt->state = NULL; |
| 8883 | } |
| 8884 | } else { |
| 8885 | if (res == NULL) { |
| 8886 | /* make it the new container and copy other results */ |
| 8887 | res = ctxt->states; |
| 8888 | ctxt->states = NULL; |
| 8889 | for (k = 0;k < j;k++) |
| 8890 | xmlRelaxNGAddStates(ctxt, res, states->tabState[k]); |
| 8891 | } else { |
| 8892 | /* add all the new results to res and reff the container */ |
| 8893 | for (k = 0;k < ctxt->states->nbState;k++) |
| 8894 | xmlRelaxNGAddStates(ctxt, res, |
| 8895 | ctxt->states->tabState[k]); |
| 8896 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8897 | ctxt->states = NULL; |
| 8898 | } |
| 8899 | } |
| 8900 | } else { |
| 8901 | if (ctxt->state != NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8902 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8903 | ctxt->state = NULL; |
| 8904 | } else if (ctxt->states != NULL) { |
| 8905 | for (k = 0;k < ctxt->states->nbState;k++) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8906 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[k]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8907 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8908 | ctxt->states = NULL; |
| 8909 | } |
| 8910 | } |
| 8911 | } |
| 8912 | ctxt->flags = oldflags; |
| 8913 | if (res != NULL) { |
| 8914 | xmlRelaxNGFreeStates(ctxt, states); |
| 8915 | ctxt->states = res; |
| 8916 | ret = 0; |
| 8917 | } else if (j > 1) { |
| 8918 | states->nbState = j; |
| 8919 | ctxt->states = states; |
| 8920 | ret =0; |
| 8921 | } else if (j == 1) { |
| 8922 | ctxt->state = states->tabState[0]; |
| 8923 | xmlRelaxNGFreeStates(ctxt, states); |
| 8924 | ret = 0; |
| 8925 | } else { |
| 8926 | ret = -1; |
| 8927 | xmlRelaxNGFreeStates(ctxt, states); |
| 8928 | if (ctxt->states != NULL) { |
| 8929 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8930 | ctxt->states = NULL; |
| 8931 | } |
| 8932 | } |
| 8933 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 8934 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8935 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8936 | ctxt->state = NULL; |
| 8937 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8938 | return(ret); |
| 8939 | } |
| 8940 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8941 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8942 | * xmlRelaxNGValidateDocument: |
| 8943 | * @ctxt: a Relax-NG validation context |
| 8944 | * @doc: the document |
| 8945 | * |
| 8946 | * Validate the given document |
| 8947 | * |
| 8948 | * Returns 0 if the validation succeeded or an error code. |
| 8949 | */ |
| 8950 | static int |
| 8951 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 8952 | int ret; |
| 8953 | xmlRelaxNGPtr schema; |
| 8954 | xmlRelaxNGGrammarPtr grammar; |
| 8955 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8956 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8957 | |
| 8958 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
| 8959 | return(-1); |
| 8960 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 8961 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8962 | schema = ctxt->schema; |
| 8963 | grammar = schema->topgrammar; |
| 8964 | if (grammar == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8965 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8966 | return(-1); |
| 8967 | } |
| 8968 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 8969 | ctxt->state = state; |
| 8970 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8971 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
| 8972 | state = ctxt->state; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8973 | node = state->seq; |
| 8974 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 8975 | if (node != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8976 | if (ret != -1) { |
| 8977 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 8978 | ret = -1; |
| 8979 | } |
| 8980 | } |
| 8981 | } else if (ctxt->states != NULL) { |
| 8982 | int i; |
| 8983 | int tmp = -1; |
| 8984 | |
| 8985 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 8986 | state = ctxt->states->tabState[i]; |
| 8987 | node = state->seq; |
| 8988 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 8989 | if (node == NULL) |
| 8990 | tmp = 0; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8991 | xmlRelaxNGFreeValidState(ctxt,state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8992 | } |
| 8993 | if (tmp == -1) { |
| 8994 | if (ret != -1) { |
| 8995 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 8996 | ret = -1; |
| 8997 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8998 | } |
| 8999 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9000 | if (ctxt->state != NULL) { |
| 9001 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9002 | ctxt->state = NULL; |
| 9003 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9004 | if (ret != 0) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9005 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9006 | #ifdef DEBUG |
| 9007 | else if (ctxt->errNr != 0) { |
| 9008 | ctxt->error(ctxt->userData, "%d Extra error messages left on stack !\n", |
| 9009 | ctxt->errNr); |
| 9010 | xmlRelaxNGDumpValidError(ctxt); |
| 9011 | } |
| 9012 | #endif |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 9013 | if (ctxt->idref == 1) { |
| 9014 | xmlValidCtxt vctxt; |
| 9015 | |
| 9016 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 9017 | vctxt.valid = 1; |
| 9018 | vctxt.error = ctxt->error; |
| 9019 | vctxt.warning = ctxt->warning; |
| 9020 | vctxt.userData = ctxt->userData; |
| 9021 | |
| 9022 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 9023 | ret = -1; |
| 9024 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 9025 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
| 9026 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9027 | |
| 9028 | return(ret); |
| 9029 | } |
| 9030 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9031 | /************************************************************************ |
| 9032 | * * |
| 9033 | * Validation interfaces * |
| 9034 | * * |
| 9035 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9036 | /** |
| 9037 | * xmlRelaxNGNewValidCtxt: |
| 9038 | * @schema: a precompiled XML RelaxNGs |
| 9039 | * |
| 9040 | * Create an XML RelaxNGs validation context based on the given schema |
| 9041 | * |
| 9042 | * Returns the validation context or NULL in case of error |
| 9043 | */ |
| 9044 | xmlRelaxNGValidCtxtPtr |
| 9045 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) { |
| 9046 | xmlRelaxNGValidCtxtPtr ret; |
| 9047 | |
| 9048 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 9049 | if (ret == NULL) { |
| 9050 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9051 | "Failed to allocate new schema validation context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9052 | return (NULL); |
| 9053 | } |
| 9054 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 9055 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 9056 | ret->error = xmlGenericError; |
| 9057 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 9058 | ret->errNr = 0; |
| 9059 | ret->errMax = 0; |
| 9060 | ret->err = NULL; |
| 9061 | ret->errTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 9062 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9063 | ret->states = NULL; |
| 9064 | ret->freeState = NULL; |
| 9065 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame^] | 9066 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9067 | return (ret); |
| 9068 | } |
| 9069 | |
| 9070 | /** |
| 9071 | * xmlRelaxNGFreeValidCtxt: |
| 9072 | * @ctxt: the schema validation context |
| 9073 | * |
| 9074 | * Free the resources associated to the schema validation context |
| 9075 | */ |
| 9076 | void |
| 9077 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9078 | int k; |
| 9079 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9080 | if (ctxt == NULL) |
| 9081 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9082 | if (ctxt->states != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9083 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
| 9084 | if (ctxt->freeState != NULL) { |
| 9085 | for (k = 0;k < ctxt->freeState->nbState;k++) { |
| 9086 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 9087 | } |
| 9088 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
| 9089 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9090 | if (ctxt->freeStates != NULL) { |
| 9091 | for (k = 0;k < ctxt->freeStatesNr;k++) { |
| 9092 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 9093 | } |
| 9094 | xmlFree(ctxt->freeStates); |
| 9095 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 9096 | if (ctxt->errTab != NULL) |
| 9097 | xmlFree(ctxt->errTab); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9098 | xmlFree(ctxt); |
| 9099 | } |
| 9100 | |
| 9101 | /** |
| 9102 | * xmlRelaxNGSetValidErrors: |
| 9103 | * @ctxt: a Relax-NG validation context |
| 9104 | * @err: the error function |
| 9105 | * @warn: the warning function |
| 9106 | * @ctx: the functions context |
| 9107 | * |
| 9108 | * Set the error and warning callback informations |
| 9109 | */ |
| 9110 | void |
| 9111 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
| 9112 | xmlRelaxNGValidityErrorFunc err, |
| 9113 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 9114 | if (ctxt == NULL) |
| 9115 | return; |
| 9116 | ctxt->error = err; |
| 9117 | ctxt->warning = warn; |
| 9118 | ctxt->userData = ctx; |
| 9119 | } |
| 9120 | |
| 9121 | /** |
| 9122 | * xmlRelaxNGValidateDoc: |
| 9123 | * @ctxt: a Relax-NG validation context |
| 9124 | * @doc: a parsed document tree |
| 9125 | * |
| 9126 | * Validate a document tree in memory. |
| 9127 | * |
| 9128 | * Returns 0 if the document is valid, a positive error code |
| 9129 | * number otherwise and -1 in case of internal or API error. |
| 9130 | */ |
| 9131 | int |
| 9132 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 9133 | int ret; |
| 9134 | |
| 9135 | if ((ctxt == NULL) || (doc == NULL)) |
| 9136 | return(-1); |
| 9137 | |
| 9138 | ctxt->doc = doc; |
| 9139 | |
| 9140 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 9141 | /* |
| 9142 | * TODO: build error codes |
| 9143 | */ |
| 9144 | if (ret == -1) |
| 9145 | return(1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9146 | return(ret); |
| 9147 | } |
| 9148 | |
| 9149 | #endif /* LIBXML_SCHEMAS_ENABLED */ |
| 9150 | |