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 | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 58 | |
| 59 | #define UNBOUNDED (1 << 30) |
| 60 | #define TODO \ |
| 61 | xmlGenericError(xmlGenericErrorContext, \ |
| 62 | "Unimplemented block at %s:%d\n", \ |
| 63 | __FILE__, __LINE__); |
| 64 | |
| 65 | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
| 66 | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
| 67 | |
| 68 | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
| 69 | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
| 70 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 71 | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
| 72 | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
| 73 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 74 | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
| 75 | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
| 76 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 77 | typedef enum { |
| 78 | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
| 79 | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
| 80 | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
| 81 | } xmlRelaxNGCombine; |
| 82 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 83 | typedef enum { |
| 84 | XML_RELAXNG_CONTENT_ERROR = -1, |
| 85 | XML_RELAXNG_CONTENT_EMPTY = 0, |
| 86 | XML_RELAXNG_CONTENT_SIMPLE, |
| 87 | XML_RELAXNG_CONTENT_COMPLEX |
| 88 | } xmlRelaxNGContentType; |
| 89 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 90 | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
| 91 | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
| 92 | |
| 93 | struct _xmlRelaxNGGrammar { |
| 94 | xmlRelaxNGGrammarPtr parent;/* the parent grammar if any */ |
| 95 | xmlRelaxNGGrammarPtr children;/* the children grammar if any */ |
| 96 | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
| 97 | xmlRelaxNGDefinePtr start; /* <start> content */ |
| 98 | xmlRelaxNGCombine combine; /* the default combine value */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 99 | xmlRelaxNGDefinePtr startList;/* list of <start> definitions */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 100 | xmlHashTablePtr defs; /* define* */ |
| 101 | xmlHashTablePtr refs; /* references */ |
| 102 | }; |
| 103 | |
| 104 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 105 | typedef enum { |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 106 | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 107 | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
| 108 | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 109 | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 110 | XML_RELAXNG_TEXT, /* textual content */ |
| 111 | XML_RELAXNG_ELEMENT, /* an element */ |
| 112 | XML_RELAXNG_DATATYPE, /* extenal data type definition */ |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 113 | XML_RELAXNG_PARAM, /* extenal data type parameter */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 114 | XML_RELAXNG_VALUE, /* value from an extenal data type definition */ |
| 115 | XML_RELAXNG_LIST, /* a list of patterns */ |
| 116 | XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */ |
| 117 | XML_RELAXNG_DEF, /* a definition */ |
| 118 | XML_RELAXNG_REF, /* reference to a definition */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 119 | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 120 | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 121 | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
| 122 | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 123 | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
| 124 | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
| 125 | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 126 | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 127 | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 128 | } xmlRelaxNGType; |
| 129 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 130 | #define IS_NULLABLE 1 |
| 131 | #define IS_NOT_NULLABLE 2 |
| 132 | #define IS_INDETERMINIST 4 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 133 | #define IS_MIXED 8 |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 134 | #define IS_TRIABLE 16 |
| 135 | #define IS_PROCESSED 32 |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 136 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 137 | struct _xmlRelaxNGDefine { |
| 138 | xmlRelaxNGType type; /* the type of definition */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 139 | xmlNodePtr node; /* the node in the source */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 140 | xmlChar *name; /* the element local name if present */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 141 | xmlChar *ns; /* the namespace local name if present */ |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 142 | xmlChar *value; /* value when available */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 143 | void *data; /* data lib or specific pointer */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 144 | xmlRelaxNGDefinePtr content;/* the expected content */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 145 | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 146 | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 147 | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 148 | xmlRelaxNGDefinePtr nameClass;/* the nameClass definition if any */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 149 | xmlRelaxNGDefinePtr nextHash;/* next define in defs/refs hash tables */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 150 | short depth; /* used for the cycle detection */ |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 151 | short dflags; /* define related flags */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | /** |
| 155 | * _xmlRelaxNG: |
| 156 | * |
| 157 | * A RelaxNGs definition |
| 158 | */ |
| 159 | struct _xmlRelaxNG { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 160 | void *_private; /* unused by the library for users or bindings */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 161 | xmlRelaxNGGrammarPtr topgrammar; |
| 162 | xmlDocPtr doc; |
| 163 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 164 | int idref; /* requires idref checking */ |
| 165 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 166 | xmlHashTablePtr defs; /* define */ |
| 167 | xmlHashTablePtr refs; /* references */ |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 168 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 169 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 170 | int defNr; /* number of defines used */ |
| 171 | xmlRelaxNGDefinePtr *defTab;/* pointer to the allocated definitions */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 172 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 175 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
| 176 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
| 177 | #define XML_RELAXNG_IN_LIST (1 << 2) |
| 178 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
| 179 | #define XML_RELAXNG_IN_START (1 << 4) |
| 180 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
| 181 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
| 182 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 183 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
| 184 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 185 | |
| 186 | struct _xmlRelaxNGParserCtxt { |
| 187 | void *userData; /* user specific data block */ |
| 188 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 189 | xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 190 | xmlRelaxNGValidErr err; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 191 | |
| 192 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 193 | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 194 | xmlRelaxNGGrammarPtr parentgrammar;/* the parent grammar */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 195 | int flags; /* parser flags */ |
| 196 | int nbErrors; /* number of errors at parse time */ |
| 197 | int nbWarnings; /* number of warnings at parse time */ |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 198 | const xmlChar *define; /* the current define scope */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 199 | xmlRelaxNGDefinePtr def; /* the current define */ |
| 200 | |
| 201 | int nbInterleaves; |
| 202 | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 203 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 204 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 205 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 206 | xmlChar *URL; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 207 | xmlDocPtr document; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 208 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 209 | int defNr; /* number of defines used */ |
| 210 | int defMax; /* number of defines aloocated */ |
| 211 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
| 212 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 213 | const char *buffer; |
| 214 | int size; |
| 215 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 216 | /* the document stack */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 217 | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 218 | int docNr; /* Depth of the parsing stack */ |
| 219 | int docMax; /* Max depth of the parsing stack */ |
| 220 | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 221 | |
| 222 | /* the include stack */ |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 223 | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 224 | int incNr; /* Depth of the include parsing stack */ |
| 225 | int incMax; /* Max depth of the parsing stack */ |
| 226 | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 227 | |
| 228 | int idref; /* requires idref checking */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | #define FLAGS_IGNORABLE 1 |
| 232 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 233 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 234 | |
| 235 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 236 | * xmlRelaxNGInterleaveGroup: |
| 237 | * |
| 238 | * A RelaxNGs partition set associated to lists of definitions |
| 239 | */ |
| 240 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 241 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 242 | struct _xmlRelaxNGInterleaveGroup { |
| 243 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 244 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 245 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 246 | }; |
| 247 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 248 | #define IS_DETERMINIST 1 |
| 249 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 250 | /** |
| 251 | * xmlRelaxNGPartitions: |
| 252 | * |
| 253 | * A RelaxNGs partition associated to an interleave group |
| 254 | */ |
| 255 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 256 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 257 | struct _xmlRelaxNGPartition { |
| 258 | int nbgroups; /* number of groups in the partitions */ |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 259 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 260 | right group when possible */ |
| 261 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 262 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 263 | }; |
| 264 | |
| 265 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 266 | * xmlRelaxNGValidState: |
| 267 | * |
| 268 | * A RelaxNGs validation state |
| 269 | */ |
| 270 | #define MAX_ATTR 20 |
| 271 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 272 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 273 | struct _xmlRelaxNGValidState { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 274 | xmlNodePtr node; /* the current node */ |
| 275 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 276 | int nbAttrs; /* the number of attributes */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 277 | int maxAttrs; /* the size of attrs */ |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 278 | int nbAttrLeft; /* the number of attributes left to validate */ |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 279 | xmlChar *value; /* the value when operating on string */ |
| 280 | xmlChar *endvalue; /* the end value when operating on string */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 281 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 282 | }; |
| 283 | |
| 284 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 285 | * xmlRelaxNGStates: |
| 286 | * |
| 287 | * A RelaxNGs container for validation state |
| 288 | */ |
| 289 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 290 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 291 | struct _xmlRelaxNGStates { |
| 292 | int nbState; /* the number of states */ |
| 293 | int maxState; /* the size of the array */ |
| 294 | xmlRelaxNGValidStatePtr *tabState; |
| 295 | }; |
| 296 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 297 | #define ERROR_IS_DUP 1 |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 298 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 299 | * xmlRelaxNGValidError: |
| 300 | * |
| 301 | * A RelaxNGs validation error |
| 302 | */ |
| 303 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 304 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 305 | struct _xmlRelaxNGValidError { |
| 306 | xmlRelaxNGValidErr err; /* the error number */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 307 | int flags; /* flags */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 308 | xmlNodePtr node; /* the current node */ |
| 309 | xmlNodePtr seq; /* the current child */ |
| 310 | const xmlChar * arg1; /* first arg */ |
| 311 | const xmlChar * arg2; /* second arg */ |
| 312 | }; |
| 313 | |
| 314 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 315 | * xmlRelaxNGValidCtxt: |
| 316 | * |
| 317 | * A RelaxNGs validation context |
| 318 | */ |
| 319 | |
| 320 | struct _xmlRelaxNGValidCtxt { |
| 321 | void *userData; /* user specific data block */ |
| 322 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 323 | xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */ |
| 324 | |
| 325 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 326 | xmlDocPtr doc; /* the document being validated */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 327 | int flags; /* validation flags */ |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 328 | int depth; /* validation depth */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 329 | int idref; /* requires idref checking */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 330 | |
| 331 | /* |
| 332 | * Errors accumulated in branches may have to be stacked to be |
| 333 | * provided back when it's sure they affect validation. |
| 334 | */ |
| 335 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
| 336 | int errNr; /* Depth of the error stack */ |
| 337 | int errMax; /* Max depth of the error stack */ |
| 338 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 339 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 340 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 341 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 342 | |
| 343 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 344 | int freeStatesNr; |
| 345 | int freeStatesMax; |
| 346 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 347 | }; |
| 348 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 349 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 350 | * xmlRelaxNGInclude: |
| 351 | * |
| 352 | * Structure associated to a RelaxNGs document element |
| 353 | */ |
| 354 | struct _xmlRelaxNGInclude { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 355 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 356 | xmlChar *href; /* the normalized href value */ |
| 357 | xmlDocPtr doc; /* the associated XML document */ |
| 358 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 359 | xmlRelaxNGPtr schema; /* the schema */ |
| 360 | }; |
| 361 | |
| 362 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 363 | * xmlRelaxNGDocument: |
| 364 | * |
| 365 | * Structure associated to a RelaxNGs document element |
| 366 | */ |
| 367 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 368 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 369 | xmlChar *href; /* the normalized href value */ |
| 370 | xmlDocPtr doc; /* the associated XML document */ |
| 371 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 372 | xmlRelaxNGPtr schema; /* the schema */ |
| 373 | }; |
| 374 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 375 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 376 | /************************************************************************ |
| 377 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 378 | * Preliminary type checking interfaces * |
| 379 | * * |
| 380 | ************************************************************************/ |
| 381 | /** |
| 382 | * xmlRelaxNGTypeHave: |
| 383 | * @data: data needed for the library |
| 384 | * @type: the type name |
| 385 | * @value: the value to check |
| 386 | * |
| 387 | * Function provided by a type library to check if a type is exported |
| 388 | * |
| 389 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 390 | */ |
| 391 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar *type); |
| 392 | |
| 393 | /** |
| 394 | * xmlRelaxNGTypeCheck: |
| 395 | * @data: data needed for the library |
| 396 | * @type: the type name |
| 397 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 398 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 399 | * |
| 400 | * Function provided by a type library to check if a value match a type |
| 401 | * |
| 402 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 403 | */ |
| 404 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar *type, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 405 | const xmlChar *value, void **result, |
| 406 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 407 | |
| 408 | /** |
| 409 | * xmlRelaxNGFacetCheck: |
| 410 | * @data: data needed for the library |
| 411 | * @type: the type name |
| 412 | * @facet: the facet name |
| 413 | * @val: the facet value |
| 414 | * @strval: the string value |
| 415 | * @value: the value to check |
| 416 | * |
| 417 | * Function provided by a type library to check a value facet |
| 418 | * |
| 419 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 420 | */ |
| 421 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar *type, |
| 422 | const xmlChar *facet, const xmlChar *val, |
| 423 | const xmlChar *strval, void *value); |
| 424 | |
| 425 | /** |
| 426 | * xmlRelaxNGTypeFree: |
| 427 | * @data: data needed for the library |
| 428 | * @result: the value to free |
| 429 | * |
| 430 | * Function provided by a type library to free a returned result |
| 431 | */ |
| 432 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 433 | |
| 434 | /** |
| 435 | * xmlRelaxNGTypeCompare: |
| 436 | * @data: data needed for the library |
| 437 | * @type: the type name |
| 438 | * @value1: the first value |
| 439 | * @value2: the second value |
| 440 | * |
| 441 | * Function provided by a type library to compare two values accordingly |
| 442 | * to a type. |
| 443 | * |
| 444 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 445 | */ |
| 446 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar *type, |
| 447 | const xmlChar *value1, |
| 448 | const xmlChar *value2); |
| 449 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 450 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 451 | struct _xmlRelaxNGTypeLibrary { |
| 452 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 453 | void *data; /* data needed for the library */ |
| 454 | xmlRelaxNGTypeHave have; /* the export function */ |
| 455 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 456 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 457 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 458 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 459 | }; |
| 460 | |
| 461 | /************************************************************************ |
| 462 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 463 | * Allocation functions * |
| 464 | * * |
| 465 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 466 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 467 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 468 | static void xmlRelaxNGNormExtSpace(xmlChar *value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 469 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 470 | static int xmlRelaxNGEqualValidState( |
| 471 | xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 472 | xmlRelaxNGValidStatePtr state1, |
| 473 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 474 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 475 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 476 | |
| 477 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 478 | * xmlRelaxNGFreeDocument: |
| 479 | * @docu: a document structure |
| 480 | * |
| 481 | * Deallocate a RelaxNG document structure. |
| 482 | */ |
| 483 | static void |
| 484 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 485 | { |
| 486 | if (docu == NULL) |
| 487 | return; |
| 488 | |
| 489 | if (docu->href != NULL) |
| 490 | xmlFree(docu->href); |
| 491 | if (docu->doc != NULL) |
| 492 | xmlFreeDoc(docu->doc); |
| 493 | if (docu->schema != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 494 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 495 | xmlFree(docu); |
| 496 | } |
| 497 | |
| 498 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 499 | * xmlRelaxNGFreeDocumentList: |
| 500 | * @docu: a list of document structure |
| 501 | * |
| 502 | * Deallocate a RelaxNG document structures. |
| 503 | */ |
| 504 | static void |
| 505 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 506 | { |
| 507 | xmlRelaxNGDocumentPtr next; |
| 508 | while (docu != NULL) { |
| 509 | next = docu->next; |
| 510 | xmlRelaxNGFreeDocument(docu); |
| 511 | docu = next; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 516 | * xmlRelaxNGFreeInclude: |
| 517 | * @incl: a include structure |
| 518 | * |
| 519 | * Deallocate a RelaxNG include structure. |
| 520 | */ |
| 521 | static void |
| 522 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 523 | { |
| 524 | if (incl == NULL) |
| 525 | return; |
| 526 | |
| 527 | if (incl->href != NULL) |
| 528 | xmlFree(incl->href); |
| 529 | if (incl->doc != NULL) |
| 530 | xmlFreeDoc(incl->doc); |
| 531 | if (incl->schema != NULL) |
| 532 | xmlRelaxNGFree(incl->schema); |
| 533 | xmlFree(incl); |
| 534 | } |
| 535 | |
| 536 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 537 | * xmlRelaxNGFreeIncludeList: |
| 538 | * @incl: a include structure list |
| 539 | * |
| 540 | * Deallocate a RelaxNG include structure. |
| 541 | */ |
| 542 | static void |
| 543 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 544 | { |
| 545 | xmlRelaxNGIncludePtr next; |
| 546 | while (incl != NULL) { |
| 547 | next = incl->next; |
| 548 | xmlRelaxNGFreeInclude(incl); |
| 549 | incl = next; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 554 | * xmlRelaxNGNewRelaxNG: |
| 555 | * @ctxt: a Relax-NG validation context (optional) |
| 556 | * |
| 557 | * Allocate a new RelaxNG structure. |
| 558 | * |
| 559 | * Returns the newly allocated structure or NULL in case or error |
| 560 | */ |
| 561 | static xmlRelaxNGPtr |
| 562 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 563 | { |
| 564 | xmlRelaxNGPtr ret; |
| 565 | |
| 566 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 567 | if (ret == NULL) { |
| 568 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 569 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 570 | ctxt->nbErrors++; |
| 571 | return (NULL); |
| 572 | } |
| 573 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 574 | |
| 575 | return (ret); |
| 576 | } |
| 577 | |
| 578 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 579 | * xmlRelaxNGFreeInnerSchema: |
| 580 | * @schema: a schema structure |
| 581 | * |
| 582 | * Deallocate a RelaxNG schema structure. |
| 583 | */ |
| 584 | static void |
| 585 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 586 | { |
| 587 | if (schema == NULL) |
| 588 | return; |
| 589 | |
| 590 | if (schema->doc != NULL) |
| 591 | xmlFreeDoc(schema->doc); |
| 592 | if (schema->defTab != NULL) { |
| 593 | int i; |
| 594 | |
| 595 | for (i = 0;i < schema->defNr;i++) |
| 596 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 597 | xmlFree(schema->defTab); |
| 598 | } |
| 599 | |
| 600 | xmlFree(schema); |
| 601 | } |
| 602 | |
| 603 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 604 | * xmlRelaxNGFree: |
| 605 | * @schema: a schema structure |
| 606 | * |
| 607 | * Deallocate a RelaxNG structure. |
| 608 | */ |
| 609 | void |
| 610 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 611 | { |
| 612 | if (schema == NULL) |
| 613 | return; |
| 614 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 615 | if (schema->topgrammar != NULL) |
| 616 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
| 617 | if (schema->doc != NULL) |
| 618 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 619 | if (schema->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 620 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 621 | if (schema->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 622 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 623 | if (schema->defTab != NULL) { |
| 624 | int i; |
| 625 | |
| 626 | for (i = 0;i < schema->defNr;i++) |
| 627 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 628 | xmlFree(schema->defTab); |
| 629 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 630 | |
| 631 | xmlFree(schema); |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * xmlRelaxNGNewGrammar: |
| 636 | * @ctxt: a Relax-NG validation context (optional) |
| 637 | * |
| 638 | * Allocate a new RelaxNG grammar. |
| 639 | * |
| 640 | * Returns the newly allocated structure or NULL in case or error |
| 641 | */ |
| 642 | static xmlRelaxNGGrammarPtr |
| 643 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 644 | { |
| 645 | xmlRelaxNGGrammarPtr ret; |
| 646 | |
| 647 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 648 | if (ret == NULL) { |
| 649 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 650 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 651 | ctxt->nbErrors++; |
| 652 | return (NULL); |
| 653 | } |
| 654 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 655 | |
| 656 | return (ret); |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * xmlRelaxNGFreeGrammar: |
| 661 | * @grammar: a grammar structure |
| 662 | * |
| 663 | * Deallocate a RelaxNG grammar structure. |
| 664 | */ |
| 665 | static void |
| 666 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 667 | { |
| 668 | if (grammar == NULL) |
| 669 | return; |
| 670 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 671 | if (grammar->children != NULL) { |
| 672 | xmlRelaxNGFreeGrammar(grammar->children); |
| 673 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 674 | if (grammar->next != NULL) { |
| 675 | xmlRelaxNGFreeGrammar(grammar->next); |
| 676 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 677 | if (grammar->refs != NULL) { |
| 678 | xmlHashFree(grammar->refs, NULL); |
| 679 | } |
| 680 | if (grammar->defs != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 681 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | xmlFree(grammar); |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * xmlRelaxNGNewDefine: |
| 689 | * @ctxt: a Relax-NG validation context |
| 690 | * @node: the node in the input document. |
| 691 | * |
| 692 | * Allocate a new RelaxNG define. |
| 693 | * |
| 694 | * Returns the newly allocated structure or NULL in case or error |
| 695 | */ |
| 696 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 697 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 698 | { |
| 699 | xmlRelaxNGDefinePtr ret; |
| 700 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 701 | if (ctxt->defMax == 0) { |
| 702 | ctxt->defMax = 16; |
| 703 | ctxt->defNr = 0; |
| 704 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 705 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 706 | if (ctxt->defTab == NULL) { |
| 707 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 708 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 709 | ctxt->nbErrors++; |
| 710 | return (NULL); |
| 711 | } |
| 712 | } else if (ctxt->defMax <= ctxt->defNr) { |
| 713 | xmlRelaxNGDefinePtr *tmp; |
| 714 | ctxt->defMax *= 2; |
| 715 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 716 | ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 717 | if (tmp == NULL) { |
| 718 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 719 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 720 | ctxt->nbErrors++; |
| 721 | return (NULL); |
| 722 | } |
| 723 | ctxt->defTab = tmp; |
| 724 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 725 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 726 | if (ret == NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 727 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 728 | ctxt->error(ctxt->userData, "Out of memory\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 729 | ctxt->nbErrors++; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 730 | return(NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 731 | } |
| 732 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 733 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 734 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 735 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 736 | return (ret); |
| 737 | } |
| 738 | |
| 739 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 740 | * xmlRelaxNGFreePartition: |
| 741 | * @partitions: a partition set structure |
| 742 | * |
| 743 | * Deallocate RelaxNG partition set structures. |
| 744 | */ |
| 745 | static void |
| 746 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) { |
| 747 | xmlRelaxNGInterleaveGroupPtr group; |
| 748 | int j; |
| 749 | |
| 750 | if (partitions != NULL) { |
| 751 | if (partitions->groups != NULL) { |
| 752 | for (j = 0;j < partitions->nbgroups;j++) { |
| 753 | group = partitions->groups[j]; |
| 754 | if (group != NULL) { |
| 755 | if (group->defs != NULL) |
| 756 | xmlFree(group->defs); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 757 | if (group->attrs != NULL) |
| 758 | xmlFree(group->attrs); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 759 | xmlFree(group); |
| 760 | } |
| 761 | } |
| 762 | xmlFree(partitions->groups); |
| 763 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 764 | if (partitions->triage != NULL) { |
| 765 | xmlHashFree(partitions->triage, NULL); |
| 766 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 767 | xmlFree(partitions); |
| 768 | } |
| 769 | } |
| 770 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 771 | * xmlRelaxNGFreeDefine: |
| 772 | * @define: a define structure |
| 773 | * |
| 774 | * Deallocate a RelaxNG define structure. |
| 775 | */ |
| 776 | static void |
| 777 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 778 | { |
| 779 | if (define == NULL) |
| 780 | return; |
| 781 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 782 | if ((define->data != NULL) && |
| 783 | (define->type == XML_RELAXNG_INTERLEAVE)) |
| 784 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 785 | if ((define->data != NULL) && |
| 786 | (define->type == XML_RELAXNG_CHOICE)) |
| 787 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 788 | if (define->name != NULL) |
| 789 | xmlFree(define->name); |
| 790 | if (define->ns != NULL) |
| 791 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 792 | if (define->value != NULL) |
| 793 | xmlFree(define->value); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 794 | xmlFree(define); |
| 795 | } |
| 796 | |
| 797 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 798 | * xmlRelaxNGNewStates: |
| 799 | * @ctxt: a Relax-NG validation context |
| 800 | * @size: the default size for the container |
| 801 | * |
| 802 | * Allocate a new RelaxNG validation state container |
| 803 | * TODO: keep a pool in the ctxt |
| 804 | * |
| 805 | * Returns the newly allocated structure or NULL in case or error |
| 806 | */ |
| 807 | static xmlRelaxNGStatesPtr |
| 808 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 809 | { |
| 810 | xmlRelaxNGStatesPtr ret; |
| 811 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 812 | if ((ctxt != NULL) && |
| 813 | (ctxt->freeState != NULL) && |
| 814 | (ctxt->freeStatesNr > 0)) { |
| 815 | ctxt->freeStatesNr--; |
| 816 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 817 | ret->nbState = 0; |
| 818 | return(ret); |
| 819 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 820 | if (size < 16) size = 16; |
| 821 | |
| 822 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
| 823 | (size - 1) * sizeof(xmlRelaxNGValidStatePtr)); |
| 824 | if (ret == NULL) { |
| 825 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 826 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 827 | return (NULL); |
| 828 | } |
| 829 | ret->nbState = 0; |
| 830 | ret->maxState = size; |
| 831 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc( |
| 832 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 833 | if (ret->tabState == NULL) { |
| 834 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 835 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 836 | xmlFree(ret->tabState); |
| 837 | return (NULL); |
| 838 | } |
| 839 | return(ret); |
| 840 | } |
| 841 | |
| 842 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 843 | * xmlRelaxNGAddStateUniq: |
| 844 | * @ctxt: a Relax-NG validation context |
| 845 | * @states: the states container |
| 846 | * @state: the validation state |
| 847 | * |
| 848 | * Add a RelaxNG validation state to the container without checking |
| 849 | * for unicity. |
| 850 | * |
| 851 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 852 | */ |
| 853 | static int |
| 854 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
| 855 | xmlRelaxNGStatesPtr states, |
| 856 | xmlRelaxNGValidStatePtr state) |
| 857 | { |
| 858 | if (state == NULL) { |
| 859 | return(-1); |
| 860 | } |
| 861 | if (states->nbState >= states->maxState) { |
| 862 | xmlRelaxNGValidStatePtr *tmp; |
| 863 | int size; |
| 864 | |
| 865 | size = states->maxState * 2; |
| 866 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 867 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 868 | if (tmp == NULL) { |
| 869 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 870 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 871 | return(-1); |
| 872 | } |
| 873 | states->tabState = tmp; |
| 874 | states->maxState = size; |
| 875 | } |
| 876 | states->tabState[states->nbState++] = state; |
| 877 | return(1); |
| 878 | } |
| 879 | |
| 880 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 881 | * xmlRelaxNGAddState: |
| 882 | * @ctxt: a Relax-NG validation context |
| 883 | * @states: the states container |
| 884 | * @state: the validation state |
| 885 | * |
| 886 | * Add a RelaxNG validation state to the container |
| 887 | * |
| 888 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 889 | */ |
| 890 | static int |
| 891 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGStatesPtr states, |
| 892 | xmlRelaxNGValidStatePtr state) |
| 893 | { |
| 894 | int i; |
| 895 | |
| 896 | if (state == NULL) { |
| 897 | return(-1); |
| 898 | } |
| 899 | if (states->nbState >= states->maxState) { |
| 900 | xmlRelaxNGValidStatePtr *tmp; |
| 901 | int size; |
| 902 | |
| 903 | size = states->maxState * 2; |
| 904 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 905 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 906 | if (tmp == NULL) { |
| 907 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 908 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 909 | return(-1); |
| 910 | } |
| 911 | states->tabState = tmp; |
| 912 | states->maxState = size; |
| 913 | } |
| 914 | for (i = 0;i < states->nbState;i++) { |
| 915 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 916 | xmlRelaxNGFreeValidState(ctxt, state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 917 | return(0); |
| 918 | } |
| 919 | } |
| 920 | states->tabState[states->nbState++] = state; |
| 921 | return(1); |
| 922 | } |
| 923 | |
| 924 | /** |
| 925 | * xmlRelaxNGFreeStates: |
| 926 | * @ctxt: a Relax-NG validation context |
| 927 | * @states: teh container |
| 928 | * |
| 929 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 930 | */ |
| 931 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 932 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 933 | xmlRelaxNGStatesPtr states) |
| 934 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 935 | if (states == NULL) |
| 936 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 937 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
| 938 | ctxt->freeStatesMax = 40; |
| 939 | ctxt->freeStatesNr = 0; |
| 940 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 941 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 942 | if (ctxt->freeStates == NULL) { |
| 943 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 944 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 945 | } |
| 946 | } else if ((ctxt != NULL) && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 947 | xmlRelaxNGStatesPtr *tmp; |
| 948 | |
| 949 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 950 | 2 * ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 951 | if (tmp == NULL) { |
| 952 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 953 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 954 | xmlFree(states->tabState); |
| 955 | xmlFree(states); |
| 956 | return; |
| 957 | } |
| 958 | ctxt->freeStates = tmp; |
| 959 | ctxt->freeStatesMax *= 2; |
| 960 | } |
| 961 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 962 | xmlFree(states->tabState); |
| 963 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 964 | } else { |
| 965 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
| 969 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 970 | * xmlRelaxNGNewValidState: |
| 971 | * @ctxt: a Relax-NG validation context |
| 972 | * @node: the current node or NULL for the document |
| 973 | * |
| 974 | * Allocate a new RelaxNG validation state |
| 975 | * |
| 976 | * Returns the newly allocated structure or NULL in case or error |
| 977 | */ |
| 978 | static xmlRelaxNGValidStatePtr |
| 979 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 980 | { |
| 981 | xmlRelaxNGValidStatePtr ret; |
| 982 | xmlAttrPtr attr; |
| 983 | xmlAttrPtr attrs[MAX_ATTR]; |
| 984 | int nbAttrs = 0; |
| 985 | xmlNodePtr root = NULL; |
| 986 | |
| 987 | if (node == NULL) { |
| 988 | root = xmlDocGetRootElement(ctxt->doc); |
| 989 | if (root == NULL) |
| 990 | return(NULL); |
| 991 | } else { |
| 992 | attr = node->properties; |
| 993 | while (attr != NULL) { |
| 994 | if (nbAttrs < MAX_ATTR) |
| 995 | attrs[nbAttrs++] = attr; |
| 996 | else |
| 997 | nbAttrs++; |
| 998 | attr = attr->next; |
| 999 | } |
| 1000 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1001 | if ((ctxt->freeState != NULL) && |
| 1002 | (ctxt->freeState->nbState > 0)) { |
| 1003 | ctxt->freeState->nbState--; |
| 1004 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1005 | } else { |
| 1006 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1007 | if (ret == NULL) { |
| 1008 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1009 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1010 | return (NULL); |
| 1011 | } |
| 1012 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1013 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1014 | ret->value = NULL; |
| 1015 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1016 | if (node == NULL) { |
| 1017 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1018 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1019 | } else { |
| 1020 | ret->node = node; |
| 1021 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1022 | } |
| 1023 | ret->nbAttrs = 0; |
| 1024 | if (nbAttrs > 0) { |
| 1025 | if (ret->attrs == NULL) { |
| 1026 | if (nbAttrs < 4) ret->maxAttrs = 4; |
| 1027 | else ret->maxAttrs = nbAttrs; |
| 1028 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1029 | sizeof(xmlAttrPtr)); |
| 1030 | if (ret->attrs == NULL) { |
| 1031 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1032 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1033 | return (ret); |
| 1034 | } |
| 1035 | } else if (ret->maxAttrs < nbAttrs) { |
| 1036 | xmlAttrPtr *tmp; |
| 1037 | |
| 1038 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1039 | sizeof(xmlAttrPtr)); |
| 1040 | if (tmp == NULL) { |
| 1041 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1042 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1043 | return (ret); |
| 1044 | } |
| 1045 | ret->attrs = tmp; |
| 1046 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1047 | ret->nbAttrs = nbAttrs; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1048 | if (nbAttrs < MAX_ATTR) { |
| 1049 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1050 | } else { |
| 1051 | attr = node->properties; |
| 1052 | nbAttrs = 0; |
| 1053 | while (attr != NULL) { |
| 1054 | ret->attrs[nbAttrs++] = attr; |
| 1055 | attr = attr->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1056 | } |
| 1057 | } |
| 1058 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1059 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1060 | return (ret); |
| 1061 | } |
| 1062 | |
| 1063 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1064 | * xmlRelaxNGCopyValidState: |
| 1065 | * @ctxt: a Relax-NG validation context |
| 1066 | * @state: a validation state |
| 1067 | * |
| 1068 | * Copy the validation state |
| 1069 | * |
| 1070 | * Returns the newly allocated structure or NULL in case or error |
| 1071 | */ |
| 1072 | static xmlRelaxNGValidStatePtr |
| 1073 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1074 | xmlRelaxNGValidStatePtr state) |
| 1075 | { |
| 1076 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1077 | unsigned int maxAttrs; |
| 1078 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1079 | |
| 1080 | if (state == NULL) |
| 1081 | return(NULL); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1082 | if ((ctxt->freeState != NULL) && |
| 1083 | (ctxt->freeState->nbState > 0)) { |
| 1084 | ctxt->freeState->nbState--; |
| 1085 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1086 | } else { |
| 1087 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1088 | if (ret == NULL) { |
| 1089 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1090 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1091 | return (NULL); |
| 1092 | } |
| 1093 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1094 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1095 | attrs = ret->attrs; |
| 1096 | maxAttrs = ret->maxAttrs; |
| 1097 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1098 | ret->attrs = attrs; |
| 1099 | ret->maxAttrs = maxAttrs; |
| 1100 | if (state->nbAttrs > 0) { |
| 1101 | if (ret->attrs == NULL) { |
| 1102 | ret->maxAttrs = state->maxAttrs; |
| 1103 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1104 | sizeof(xmlAttrPtr)); |
| 1105 | if (ret->attrs == NULL) { |
| 1106 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1107 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1108 | ret->nbAttrs = 0; |
| 1109 | return (ret); |
| 1110 | } |
| 1111 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1112 | xmlAttrPtr *tmp; |
| 1113 | |
| 1114 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1115 | sizeof(xmlAttrPtr)); |
| 1116 | if (tmp == NULL) { |
| 1117 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1118 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1119 | ret->nbAttrs = 0; |
| 1120 | return (ret); |
| 1121 | } |
| 1122 | ret->maxAttrs = state->maxAttrs; |
| 1123 | } |
| 1124 | memcpy(ret->attrs, state->attrs, state->nbAttrs * sizeof(xmlAttrPtr)); |
| 1125 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1126 | return(ret); |
| 1127 | } |
| 1128 | |
| 1129 | /** |
| 1130 | * xmlRelaxNGEqualValidState: |
| 1131 | * @ctxt: a Relax-NG validation context |
| 1132 | * @state1: a validation state |
| 1133 | * @state2: a validation state |
| 1134 | * |
| 1135 | * Compare the validation states for equality |
| 1136 | * |
| 1137 | * Returns 1 if equald, 0 otherwise |
| 1138 | */ |
| 1139 | static int |
| 1140 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1141 | xmlRelaxNGValidStatePtr state1, |
| 1142 | xmlRelaxNGValidStatePtr state2) |
| 1143 | { |
| 1144 | int i; |
| 1145 | |
| 1146 | if ((state1 == NULL) || (state2 == NULL)) |
| 1147 | return(0); |
| 1148 | if (state1 == state2) |
| 1149 | return(1); |
| 1150 | if (state1->node != state2->node) |
| 1151 | return(0); |
| 1152 | if (state1->seq != state2->seq) |
| 1153 | return(0); |
| 1154 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
| 1155 | return(0); |
| 1156 | if (state1->nbAttrs != state2->nbAttrs) |
| 1157 | return(0); |
| 1158 | if (state1->endvalue != state2->endvalue) |
| 1159 | return(0); |
| 1160 | if ((state1->value != state2->value) && |
| 1161 | (!xmlStrEqual(state1->value, state2->value))) |
| 1162 | return(0); |
| 1163 | for (i = 0;i < state1->nbAttrs;i++) { |
| 1164 | if (state1->attrs[i] != state2->attrs[i]) |
| 1165 | return(0); |
| 1166 | } |
| 1167 | return(1); |
| 1168 | } |
| 1169 | |
| 1170 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1171 | * xmlRelaxNGFreeValidState: |
| 1172 | * @state: a validation state structure |
| 1173 | * |
| 1174 | * Deallocate a RelaxNG validation state structure. |
| 1175 | */ |
| 1176 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1177 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1178 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1179 | { |
| 1180 | if (state == NULL) |
| 1181 | return; |
| 1182 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1183 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
| 1184 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
| 1185 | } |
| 1186 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
| 1187 | if (state->attrs != NULL) |
| 1188 | xmlFree(state->attrs); |
| 1189 | xmlFree(state); |
| 1190 | } else { |
| 1191 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
| 1192 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | /************************************************************************ |
| 1196 | * * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1197 | * Document functions * |
| 1198 | * * |
| 1199 | ************************************************************************/ |
| 1200 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
| 1201 | xmlDocPtr doc); |
| 1202 | |
| 1203 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1204 | * xmlRelaxNGIncludePush: |
| 1205 | * @ctxt: the parser context |
| 1206 | * @value: the element doc |
| 1207 | * |
| 1208 | * Pushes a new include on top of the include stack |
| 1209 | * |
| 1210 | * Returns 0 in case of error, the index in the stack otherwise |
| 1211 | */ |
| 1212 | static int |
| 1213 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1214 | xmlRelaxNGIncludePtr value) |
| 1215 | { |
| 1216 | if (ctxt->incTab == NULL) { |
| 1217 | ctxt->incMax = 4; |
| 1218 | ctxt->incNr = 0; |
| 1219 | ctxt->incTab = (xmlRelaxNGIncludePtr *) xmlMalloc( |
| 1220 | ctxt->incMax * sizeof(ctxt->incTab[0])); |
| 1221 | if (ctxt->incTab == NULL) { |
| 1222 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1223 | return (0); |
| 1224 | } |
| 1225 | } |
| 1226 | if (ctxt->incNr >= ctxt->incMax) { |
| 1227 | ctxt->incMax *= 2; |
| 1228 | ctxt->incTab = |
| 1229 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
| 1230 | ctxt->incMax * |
| 1231 | sizeof(ctxt->incTab[0])); |
| 1232 | if (ctxt->incTab == NULL) { |
| 1233 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1234 | return (0); |
| 1235 | } |
| 1236 | } |
| 1237 | ctxt->incTab[ctxt->incNr] = value; |
| 1238 | ctxt->inc = value; |
| 1239 | return (ctxt->incNr++); |
| 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * xmlRelaxNGIncludePop: |
| 1244 | * @ctxt: the parser context |
| 1245 | * |
| 1246 | * Pops the top include from the include stack |
| 1247 | * |
| 1248 | * Returns the include just removed |
| 1249 | */ |
| 1250 | static xmlRelaxNGIncludePtr |
| 1251 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1252 | { |
| 1253 | xmlRelaxNGIncludePtr ret; |
| 1254 | |
| 1255 | if (ctxt->incNr <= 0) |
| 1256 | return (0); |
| 1257 | ctxt->incNr--; |
| 1258 | if (ctxt->incNr > 0) |
| 1259 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1260 | else |
| 1261 | ctxt->inc = NULL; |
| 1262 | ret = ctxt->incTab[ctxt->incNr]; |
| 1263 | ctxt->incTab[ctxt->incNr] = 0; |
| 1264 | return (ret); |
| 1265 | } |
| 1266 | |
| 1267 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1268 | * xmlRelaxNGRemoveRedefine: |
| 1269 | * @ctxt: the parser context |
| 1270 | * @URL: the normalized URL |
| 1271 | * @target: the included target |
| 1272 | * @name: the define name to eliminate |
| 1273 | * |
| 1274 | * Applies the elimination algorithm of 4.7 |
| 1275 | * |
| 1276 | * Returns 0 in case of error, 1 in case of success. |
| 1277 | */ |
| 1278 | static int |
| 1279 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
| 1280 | const xmlChar *URL ATTRIBUTE_UNUSED, |
| 1281 | xmlNodePtr target, const xmlChar *name) { |
| 1282 | int found = 0; |
| 1283 | xmlNodePtr tmp, tmp2; |
| 1284 | xmlChar *name2; |
| 1285 | |
| 1286 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1287 | if (name == NULL) |
| 1288 | xmlGenericError(xmlGenericErrorContext, |
| 1289 | "Elimination of <include> start from %s\n", URL); |
| 1290 | else |
| 1291 | xmlGenericError(xmlGenericErrorContext, |
| 1292 | "Elimination of <include> define %s from %s\n", name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1293 | #endif |
| 1294 | tmp = target; |
| 1295 | while (tmp != NULL) { |
| 1296 | tmp2 = tmp->next; |
| 1297 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1298 | found = 1; |
| 1299 | xmlUnlinkNode(tmp); |
| 1300 | xmlFreeNode(tmp); |
| 1301 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1302 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1303 | xmlRelaxNGNormExtSpace(name2); |
| 1304 | if (name2 != NULL) { |
| 1305 | if (xmlStrEqual(name, name2)) { |
| 1306 | found = 1; |
| 1307 | xmlUnlinkNode(tmp); |
| 1308 | xmlFreeNode(tmp); |
| 1309 | } |
| 1310 | xmlFree(name2); |
| 1311 | } |
| 1312 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1313 | xmlChar *href = NULL; |
| 1314 | xmlRelaxNGDocumentPtr inc = tmp->_private; |
| 1315 | |
| 1316 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1317 | (inc->doc->children != NULL)) { |
| 1318 | |
| 1319 | if (xmlStrEqual(inc->doc->children->name, BAD_CAST "grammar")) { |
| 1320 | #ifdef DEBUG_INCLUDE |
| 1321 | href = xmlGetProp(tmp, BAD_CAST "href"); |
| 1322 | #endif |
| 1323 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1324 | inc->doc->children->children, name) == 1) { |
| 1325 | found = 1; |
| 1326 | } |
| 1327 | if (href != NULL) |
| 1328 | xmlFree(href); |
| 1329 | } |
| 1330 | } |
| 1331 | } |
| 1332 | tmp = tmp2; |
| 1333 | } |
| 1334 | return(found); |
| 1335 | } |
| 1336 | |
| 1337 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1338 | * xmlRelaxNGLoadInclude: |
| 1339 | * @ctxt: the parser context |
| 1340 | * @URL: the normalized URL |
| 1341 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1342 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1343 | * |
| 1344 | * First lookup if the document is already loaded into the parser context, |
| 1345 | * check against recursion. If not found the resource is loaded and |
| 1346 | * the content is preprocessed before being returned back to the caller. |
| 1347 | * |
| 1348 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1349 | */ |
| 1350 | static xmlRelaxNGIncludePtr |
| 1351 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1352 | xmlNodePtr node, const xmlChar *ns) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1353 | xmlRelaxNGIncludePtr ret = NULL; |
| 1354 | xmlDocPtr doc; |
| 1355 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1356 | xmlNodePtr root, cur; |
| 1357 | |
| 1358 | #ifdef DEBUG_INCLUDE |
| 1359 | xmlGenericError(xmlGenericErrorContext, |
| 1360 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
| 1361 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1362 | |
| 1363 | /* |
| 1364 | * check against recursion in the stack |
| 1365 | */ |
| 1366 | for (i = 0;i < ctxt->incNr;i++) { |
| 1367 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1368 | if (ctxt->error != NULL) |
| 1369 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1370 | "Detected an Include recursion for %s\n", |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1371 | URL); |
| 1372 | ctxt->nbErrors++; |
| 1373 | return(NULL); |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1378 | * load the document |
| 1379 | */ |
| 1380 | doc = xmlParseFile((const char *) URL); |
| 1381 | if (doc == NULL) { |
| 1382 | if (ctxt->error != NULL) |
| 1383 | ctxt->error(ctxt->userData, |
| 1384 | "xmlRelaxNG: could not load %s\n", URL); |
| 1385 | ctxt->nbErrors++; |
| 1386 | return (NULL); |
| 1387 | } |
| 1388 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1389 | #ifdef DEBUG_INCLUDE |
| 1390 | xmlGenericError(xmlGenericErrorContext, |
| 1391 | "Parsed %s Okay\n", URL); |
| 1392 | #endif |
| 1393 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1394 | /* |
| 1395 | * Allocate the document structures and register it first. |
| 1396 | */ |
| 1397 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1398 | if (ret == NULL) { |
| 1399 | if (ctxt->error != NULL) |
| 1400 | ctxt->error(ctxt->userData, |
| 1401 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1402 | ctxt->nbErrors++; |
| 1403 | xmlFreeDoc(doc); |
| 1404 | return (NULL); |
| 1405 | } |
| 1406 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1407 | ret->doc = doc; |
| 1408 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1409 | ret->next = ctxt->includes; |
| 1410 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1411 | |
| 1412 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1413 | * transmit the ns if needed |
| 1414 | */ |
| 1415 | if (ns != NULL) { |
| 1416 | root = xmlDocGetRootElement(doc); |
| 1417 | if (root != NULL) { |
| 1418 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1419 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1425 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1426 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1427 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1428 | |
| 1429 | /* |
| 1430 | * Some preprocessing of the document content, this include recursing |
| 1431 | * in the include stack. |
| 1432 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1433 | #ifdef DEBUG_INCLUDE |
| 1434 | xmlGenericError(xmlGenericErrorContext, |
| 1435 | "cleanup of %s\n", URL); |
| 1436 | #endif |
| 1437 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1438 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1439 | if (doc == NULL) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1440 | ctxt->inc = NULL; |
| 1441 | return(NULL); |
| 1442 | } |
| 1443 | |
| 1444 | /* |
| 1445 | * Pop up the include from the stack |
| 1446 | */ |
| 1447 | xmlRelaxNGIncludePop(ctxt); |
| 1448 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1449 | #ifdef DEBUG_INCLUDE |
| 1450 | xmlGenericError(xmlGenericErrorContext, |
| 1451 | "Checking of %s\n", URL); |
| 1452 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1453 | /* |
| 1454 | * Check that the top element is a grammar |
| 1455 | */ |
| 1456 | root = xmlDocGetRootElement(doc); |
| 1457 | if (root == NULL) { |
| 1458 | if (ctxt->error != NULL) |
| 1459 | ctxt->error(ctxt->userData, |
| 1460 | "xmlRelaxNG: included document is empty %s\n", URL); |
| 1461 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1462 | return (NULL); |
| 1463 | } |
| 1464 | if (!IS_RELAXNG(root, "grammar")) { |
| 1465 | if (ctxt->error != NULL) |
| 1466 | ctxt->error(ctxt->userData, |
| 1467 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1468 | URL); |
| 1469 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1470 | return (NULL); |
| 1471 | } |
| 1472 | |
| 1473 | /* |
| 1474 | * Elimination of redefined rules in the include. |
| 1475 | */ |
| 1476 | cur = node->children; |
| 1477 | while (cur != NULL) { |
| 1478 | if (IS_RELAXNG(cur, "start")) { |
| 1479 | int found = 0; |
| 1480 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1481 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1482 | if (!found) { |
| 1483 | if (ctxt->error != NULL) |
| 1484 | ctxt->error(ctxt->userData, |
| 1485 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1486 | URL); |
| 1487 | ctxt->nbErrors++; |
| 1488 | } |
| 1489 | } else if (IS_RELAXNG(cur, "define")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1490 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1491 | |
| 1492 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1493 | if (name == NULL) { |
| 1494 | if (ctxt->error != NULL) |
| 1495 | ctxt->error(ctxt->userData, |
| 1496 | "xmlRelaxNG: include %s has define without name\n", |
| 1497 | URL); |
| 1498 | ctxt->nbErrors++; |
| 1499 | } else { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1500 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1501 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1502 | xmlRelaxNGNormExtSpace(name); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1503 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1504 | root->children, name); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1505 | if (!found) { |
| 1506 | if (ctxt->error != NULL) |
| 1507 | ctxt->error(ctxt->userData, |
| 1508 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1509 | URL, name); |
| 1510 | ctxt->nbErrors++; |
| 1511 | } |
| 1512 | xmlFree(name); |
| 1513 | } |
| 1514 | } |
| 1515 | cur = cur->next; |
| 1516 | } |
| 1517 | |
| 1518 | |
| 1519 | return(ret); |
| 1520 | } |
| 1521 | |
| 1522 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1523 | * xmlRelaxNGValidErrorPush: |
| 1524 | * @ctxt: the validation context |
| 1525 | * @err: the error code |
| 1526 | * @arg1: the first string argument |
| 1527 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1528 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1529 | * |
| 1530 | * Pushes a new error on top of the error stack |
| 1531 | * |
| 1532 | * Returns 0 in case of error, the index in the stack otherwise |
| 1533 | */ |
| 1534 | static int |
| 1535 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1536 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1537 | { |
| 1538 | xmlRelaxNGValidErrorPtr cur; |
| 1539 | if (ctxt->errTab == NULL) { |
| 1540 | ctxt->errMax = 8; |
| 1541 | ctxt->errNr = 0; |
| 1542 | ctxt->errTab = (xmlRelaxNGValidErrorPtr) xmlMalloc( |
| 1543 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
| 1544 | if (ctxt->errTab == NULL) { |
| 1545 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1546 | return (0); |
| 1547 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1548 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1549 | } |
| 1550 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1551 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1552 | ctxt->errTab = |
| 1553 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1554 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1555 | if (ctxt->errTab == NULL) { |
| 1556 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1557 | return (0); |
| 1558 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1559 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1560 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1561 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1562 | (ctxt->err->node == ctxt->state->node) && |
| 1563 | (ctxt->err->err == err)) |
| 1564 | return(ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1565 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1566 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1567 | if (dup) { |
| 1568 | cur->arg1 = xmlStrdup(arg1); |
| 1569 | cur->arg2 = xmlStrdup(arg2); |
| 1570 | cur->flags = ERROR_IS_DUP; |
| 1571 | } else { |
| 1572 | cur->arg1 = arg1; |
| 1573 | cur->arg2 = arg2; |
| 1574 | cur->flags = 0; |
| 1575 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1576 | if (ctxt->state != NULL) { |
| 1577 | cur->node = ctxt->state->node; |
| 1578 | cur->seq = ctxt->state->seq; |
| 1579 | } else { |
| 1580 | cur->node = NULL; |
| 1581 | cur->seq = NULL; |
| 1582 | } |
| 1583 | ctxt->err = cur; |
| 1584 | return (ctxt->errNr++); |
| 1585 | } |
| 1586 | |
| 1587 | /** |
| 1588 | * xmlRelaxNGValidErrorPop: |
| 1589 | * @ctxt: the validation context |
| 1590 | * |
| 1591 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1592 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1593 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1594 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1595 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1596 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1597 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1598 | if (ctxt->errNr <= 0) { |
| 1599 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1600 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1601 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1602 | ctxt->errNr--; |
| 1603 | if (ctxt->errNr > 0) |
| 1604 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1605 | else |
| 1606 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1607 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1608 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1609 | if (cur->arg1 != NULL) |
| 1610 | xmlFree((xmlChar *)cur->arg1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1611 | cur->arg1 = NULL; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1612 | if (cur->arg2 != NULL) |
| 1613 | xmlFree((xmlChar *)cur->arg2); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1614 | cur->arg2 = NULL; |
| 1615 | cur->flags = 0; |
| 1616 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1619 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1620 | * xmlRelaxNGDocumentPush: |
| 1621 | * @ctxt: the parser context |
| 1622 | * @value: the element doc |
| 1623 | * |
| 1624 | * Pushes a new doc on top of the doc stack |
| 1625 | * |
| 1626 | * Returns 0 in case of error, the index in the stack otherwise |
| 1627 | */ |
| 1628 | static int |
| 1629 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1630 | xmlRelaxNGDocumentPtr value) |
| 1631 | { |
| 1632 | if (ctxt->docTab == NULL) { |
| 1633 | ctxt->docMax = 4; |
| 1634 | ctxt->docNr = 0; |
| 1635 | ctxt->docTab = (xmlRelaxNGDocumentPtr *) xmlMalloc( |
| 1636 | ctxt->docMax * sizeof(ctxt->docTab[0])); |
| 1637 | if (ctxt->docTab == NULL) { |
| 1638 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1639 | return (0); |
| 1640 | } |
| 1641 | } |
| 1642 | if (ctxt->docNr >= ctxt->docMax) { |
| 1643 | ctxt->docMax *= 2; |
| 1644 | ctxt->docTab = |
| 1645 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
| 1646 | ctxt->docMax * |
| 1647 | sizeof(ctxt->docTab[0])); |
| 1648 | if (ctxt->docTab == NULL) { |
| 1649 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1650 | return (0); |
| 1651 | } |
| 1652 | } |
| 1653 | ctxt->docTab[ctxt->docNr] = value; |
| 1654 | ctxt->doc = value; |
| 1655 | return (ctxt->docNr++); |
| 1656 | } |
| 1657 | |
| 1658 | /** |
| 1659 | * xmlRelaxNGDocumentPop: |
| 1660 | * @ctxt: the parser context |
| 1661 | * |
| 1662 | * Pops the top doc from the doc stack |
| 1663 | * |
| 1664 | * Returns the doc just removed |
| 1665 | */ |
| 1666 | static xmlRelaxNGDocumentPtr |
| 1667 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1668 | { |
| 1669 | xmlRelaxNGDocumentPtr ret; |
| 1670 | |
| 1671 | if (ctxt->docNr <= 0) |
| 1672 | return (0); |
| 1673 | ctxt->docNr--; |
| 1674 | if (ctxt->docNr > 0) |
| 1675 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1676 | else |
| 1677 | ctxt->doc = NULL; |
| 1678 | ret = ctxt->docTab[ctxt->docNr]; |
| 1679 | ctxt->docTab[ctxt->docNr] = 0; |
| 1680 | return (ret); |
| 1681 | } |
| 1682 | |
| 1683 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1684 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1685 | * @ctxt: the parser context |
| 1686 | * @URL: the normalized URL |
| 1687 | * @ns: the inherited ns if any |
| 1688 | * |
| 1689 | * First lookup if the document is already loaded into the parser context, |
| 1690 | * check against recursion. If not found the resource is loaded and |
| 1691 | * the content is preprocessed before being returned back to the caller. |
| 1692 | * |
| 1693 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1694 | */ |
| 1695 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1696 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1697 | const xmlChar *ns) { |
| 1698 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1699 | xmlDocPtr doc; |
| 1700 | xmlNodePtr root; |
| 1701 | int i; |
| 1702 | |
| 1703 | /* |
| 1704 | * check against recursion in the stack |
| 1705 | */ |
| 1706 | for (i = 0;i < ctxt->docNr;i++) { |
| 1707 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1708 | if (ctxt->error != NULL) |
| 1709 | ctxt->error(ctxt->userData, |
| 1710 | "Detected an externalRef recursion for %s\n", |
| 1711 | URL); |
| 1712 | ctxt->nbErrors++; |
| 1713 | return(NULL); |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1718 | * load the document |
| 1719 | */ |
| 1720 | doc = xmlParseFile((const char *) URL); |
| 1721 | if (doc == NULL) { |
| 1722 | if (ctxt->error != NULL) |
| 1723 | ctxt->error(ctxt->userData, |
| 1724 | "xmlRelaxNG: could not load %s\n", URL); |
| 1725 | ctxt->nbErrors++; |
| 1726 | return (NULL); |
| 1727 | } |
| 1728 | |
| 1729 | /* |
| 1730 | * Allocate the document structures and register it first. |
| 1731 | */ |
| 1732 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1733 | if (ret == NULL) { |
| 1734 | if (ctxt->error != NULL) |
| 1735 | ctxt->error(ctxt->userData, |
| 1736 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1737 | ctxt->nbErrors++; |
| 1738 | xmlFreeDoc(doc); |
| 1739 | return (NULL); |
| 1740 | } |
| 1741 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1742 | ret->doc = doc; |
| 1743 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1744 | ret->next = ctxt->documents; |
| 1745 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1746 | |
| 1747 | /* |
| 1748 | * transmit the ns if needed |
| 1749 | */ |
| 1750 | if (ns != NULL) { |
| 1751 | root = xmlDocGetRootElement(doc); |
| 1752 | if (root != NULL) { |
| 1753 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1754 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | /* |
| 1760 | * push it on the stack and register it in the hash table |
| 1761 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1762 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1763 | |
| 1764 | /* |
| 1765 | * Some preprocessing of the document content |
| 1766 | */ |
| 1767 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1768 | if (doc == NULL) { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1769 | ctxt->doc = NULL; |
| 1770 | return(NULL); |
| 1771 | } |
| 1772 | |
| 1773 | xmlRelaxNGDocumentPop(ctxt); |
| 1774 | |
| 1775 | return(ret); |
| 1776 | } |
| 1777 | |
| 1778 | /************************************************************************ |
| 1779 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1780 | * Error functions * |
| 1781 | * * |
| 1782 | ************************************************************************/ |
| 1783 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1784 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 1785 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 1786 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 1787 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 1788 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1789 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1790 | #ifdef DEBUG |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1791 | static const char * |
| 1792 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) { |
| 1793 | if (def == NULL) |
| 1794 | return("none"); |
| 1795 | switch(def->type) { |
| 1796 | case XML_RELAXNG_EMPTY: return("empty"); |
| 1797 | case XML_RELAXNG_NOT_ALLOWED: return("notAllowed"); |
| 1798 | case XML_RELAXNG_EXCEPT: return("except"); |
| 1799 | case XML_RELAXNG_TEXT: return("text"); |
| 1800 | case XML_RELAXNG_ELEMENT: return("element"); |
| 1801 | case XML_RELAXNG_DATATYPE: return("datatype"); |
| 1802 | case XML_RELAXNG_VALUE: return("value"); |
| 1803 | case XML_RELAXNG_LIST: return("list"); |
| 1804 | case XML_RELAXNG_ATTRIBUTE: return("attribute"); |
| 1805 | case XML_RELAXNG_DEF: return("def"); |
| 1806 | case XML_RELAXNG_REF: return("ref"); |
| 1807 | case XML_RELAXNG_EXTERNALREF: return("externalRef"); |
| 1808 | case XML_RELAXNG_PARENTREF: return("parentRef"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1809 | case XML_RELAXNG_OPTIONAL: return("optional"); |
| 1810 | case XML_RELAXNG_ZEROORMORE: return("zeroOrMore"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1811 | case XML_RELAXNG_ONEORMORE: return("oneOrMore"); |
| 1812 | case XML_RELAXNG_CHOICE: return("choice"); |
| 1813 | case XML_RELAXNG_GROUP: return("group"); |
| 1814 | case XML_RELAXNG_INTERLEAVE: return("interleave"); |
| 1815 | case XML_RELAXNG_START: return("start"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1816 | case XML_RELAXNG_NOOP: return("noop"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1817 | case XML_RELAXNG_PARAM: return("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1818 | } |
| 1819 | return("unknown"); |
| 1820 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1821 | #endif |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1822 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1823 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1824 | * xmlRelaxNGGetErrorString: |
| 1825 | * @err: the error code |
| 1826 | * @arg1: the first string argument |
| 1827 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1828 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1829 | * computes a formatted error string for the given error code and args |
| 1830 | * |
| 1831 | * Returns the error string, it must be deallocated by the caller |
| 1832 | */ |
| 1833 | static xmlChar * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1834 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar *arg1, |
| 1835 | const xmlChar *arg2) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1836 | char msg[1000]; |
| 1837 | |
| 1838 | if (arg1 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1839 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1840 | if (arg2 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1841 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1842 | |
| 1843 | msg[0] = 0; |
| 1844 | switch (err) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1845 | case XML_RELAXNG_OK: |
| 1846 | return(NULL); |
| 1847 | case XML_RELAXNG_ERR_MEMORY: |
| 1848 | return(xmlCharStrdup("out of memory")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1849 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1850 | snprintf(msg, 1000, "failed to validate type %s", arg1); |
| 1851 | break; |
| 1852 | case XML_RELAXNG_ERR_TYPEVAL: |
| 1853 | snprintf(msg, 1000, "Type %s doesn't allow value %s", arg1, arg2); |
| 1854 | break; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1855 | case XML_RELAXNG_ERR_DUPID: |
| 1856 | snprintf(msg, 1000, "ID %s redefined", arg1); |
| 1857 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1858 | case XML_RELAXNG_ERR_TYPECMP: |
| 1859 | snprintf(msg, 1000, "failed to compare type %s", arg1); |
| 1860 | break; |
| 1861 | case XML_RELAXNG_ERR_NOSTATE: |
| 1862 | return(xmlCharStrdup("Internal error: no state")); |
| 1863 | case XML_RELAXNG_ERR_NODEFINE: |
| 1864 | return(xmlCharStrdup("Internal error: no define")); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1865 | case XML_RELAXNG_ERR_INTERNAL: |
| 1866 | snprintf(msg, 1000, "Internal error: %s", arg1); |
| 1867 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1868 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 1869 | snprintf(msg, 1000, "Extra data in list: %s", arg1); |
| 1870 | break; |
| 1871 | case XML_RELAXNG_ERR_INTERNODATA: |
| 1872 | return(xmlCharStrdup("Internal: interleave block has no data")); |
| 1873 | case XML_RELAXNG_ERR_INTERSEQ: |
| 1874 | return(xmlCharStrdup("Invalid sequence in interleave")); |
| 1875 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 1876 | snprintf(msg, 1000, "Extra element %s in interleave", arg1); |
| 1877 | break; |
| 1878 | case XML_RELAXNG_ERR_ELEMNAME: |
| 1879 | snprintf(msg, 1000, "Expecting element %s, got %s", arg1, arg2); |
| 1880 | break; |
| 1881 | case XML_RELAXNG_ERR_ELEMNONS: |
| 1882 | snprintf(msg, 1000, "Expecting a namespace for element %s", arg1); |
| 1883 | break; |
| 1884 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 1885 | snprintf(msg, 1000, "Element %s has wrong namespace: expecting %s", |
| 1886 | arg1, arg2); |
| 1887 | break; |
| 1888 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 1889 | snprintf(msg, 1000, "Expecting no namespace for element %s", arg1); |
| 1890 | break; |
| 1891 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 1892 | snprintf(msg, 1000, "Expecting element %s to be empty", arg1); |
| 1893 | break; |
| 1894 | case XML_RELAXNG_ERR_NOELEM: |
| 1895 | snprintf(msg, 1000, "Expecting an element %s, got nothing", arg1); |
| 1896 | break; |
| 1897 | case XML_RELAXNG_ERR_NOTELEM: |
| 1898 | return(xmlCharStrdup("Expecting an element got text")); |
| 1899 | case XML_RELAXNG_ERR_ATTRVALID: |
| 1900 | snprintf(msg, 1000, "Element %s failed to validate attributes", |
| 1901 | arg1); |
| 1902 | break; |
| 1903 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 1904 | snprintf(msg, 1000, "Element %s failed to validate content", |
| 1905 | arg1); |
| 1906 | break; |
| 1907 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 1908 | snprintf(msg, 1000, "Element %s has extra content: %s", |
| 1909 | arg1, arg2); |
| 1910 | break; |
| 1911 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 1912 | snprintf(msg, 1000, "Invalid attribute %s for element %s", |
| 1913 | arg1, arg2); |
| 1914 | break; |
| 1915 | case XML_RELAXNG_ERR_DATAELEM: |
| 1916 | snprintf(msg, 1000, "Datatype element %s has child elements", |
| 1917 | arg1); |
| 1918 | break; |
| 1919 | case XML_RELAXNG_ERR_VALELEM: |
| 1920 | snprintf(msg, 1000, "Value element %s has child elements", |
| 1921 | arg1); |
| 1922 | break; |
| 1923 | case XML_RELAXNG_ERR_LISTELEM: |
| 1924 | snprintf(msg, 1000, "List element %s has child elements", |
| 1925 | arg1); |
| 1926 | break; |
| 1927 | case XML_RELAXNG_ERR_DATATYPE: |
| 1928 | snprintf(msg, 1000, "Error validating datatype %s", |
| 1929 | arg1); |
| 1930 | break; |
| 1931 | case XML_RELAXNG_ERR_VALUE: |
| 1932 | snprintf(msg, 1000, "Error validating value %s", |
| 1933 | arg1); |
| 1934 | break; |
| 1935 | case XML_RELAXNG_ERR_LIST: |
| 1936 | return(xmlCharStrdup("Error validating list")); |
| 1937 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 1938 | return(xmlCharStrdup("No top grammar defined")); |
| 1939 | case XML_RELAXNG_ERR_EXTRADATA: |
| 1940 | return(xmlCharStrdup("Extra data in the document")); |
| 1941 | default: |
| 1942 | TODO |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1943 | } |
| 1944 | if (msg[0] == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1945 | snprintf(msg, 1000, "Unknown error code %d", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1946 | } |
| 1947 | msg[1000] = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1948 | return(xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | /** |
| 1952 | * xmlRelaxNGValidErrorContext: |
| 1953 | * @ctxt: the validation context |
| 1954 | * @node: the node |
| 1955 | * @child: the node child generating the problem. |
| 1956 | * |
| 1957 | * Dump informations about the kocation of the error in the instance |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1958 | */ |
| 1959 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1960 | xmlRelaxNGValidErrorContext(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, |
| 1961 | xmlNodePtr child) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1962 | { |
| 1963 | int line = 0; |
| 1964 | const xmlChar *file = NULL; |
| 1965 | const xmlChar *name = NULL; |
| 1966 | const char *type = "error"; |
| 1967 | |
| 1968 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 1969 | return; |
| 1970 | |
| 1971 | if (child != NULL) |
| 1972 | node = child; |
| 1973 | |
| 1974 | if (node != NULL) { |
| 1975 | if ((node->type == XML_DOCUMENT_NODE) || |
| 1976 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
| 1977 | xmlDocPtr doc = (xmlDocPtr) node; |
| 1978 | |
| 1979 | file = doc->URL; |
| 1980 | } else { |
| 1981 | /* |
| 1982 | * Try to find contextual informations to report |
| 1983 | */ |
| 1984 | if (node->type == XML_ELEMENT_NODE) { |
| 1985 | line = (int) node->content; |
| 1986 | } else if ((node->prev != NULL) && |
| 1987 | (node->prev->type == XML_ELEMENT_NODE)) { |
| 1988 | line = (int) node->prev->content; |
| 1989 | } else if ((node->parent != NULL) && |
| 1990 | (node->parent->type == XML_ELEMENT_NODE)) { |
| 1991 | line = (int) node->parent->content; |
| 1992 | } |
| 1993 | if ((node->doc != NULL) && (node->doc->URL != NULL)) |
| 1994 | file = node->doc->URL; |
| 1995 | if (node->name != NULL) |
| 1996 | name = node->name; |
| 1997 | } |
| 1998 | } |
| 1999 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2000 | type = "RNG validity error"; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2001 | |
| 2002 | if ((file != NULL) && (line != 0) && (name != NULL)) |
| 2003 | ctxt->error(ctxt->userData, "%s: file %s line %d element %s\n", |
| 2004 | type, file, line, name); |
| 2005 | else if ((file != NULL) && (name != NULL)) |
| 2006 | ctxt->error(ctxt->userData, "%s: file %s element %s\n", |
| 2007 | type, file, name); |
| 2008 | else if ((file != NULL) && (line != 0)) |
| 2009 | ctxt->error(ctxt->userData, "%s: file %s line %d\n", type, file, line); |
| 2010 | else if (file != NULL) |
| 2011 | ctxt->error(ctxt->userData, "%s: file %s\n", type, file); |
| 2012 | else if (name != NULL) |
| 2013 | ctxt->error(ctxt->userData, "%s: element %s\n", type, name); |
| 2014 | else |
| 2015 | ctxt->error(ctxt->userData, "%s\n", type); |
| 2016 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2017 | |
| 2018 | /** |
| 2019 | * xmlRelaxNGShowValidError: |
| 2020 | * @ctxt: the validation context |
| 2021 | * @err: the error number |
| 2022 | * @node: the node |
| 2023 | * @child: the node child generating the problem. |
| 2024 | * @arg1: the first argument |
| 2025 | * @arg2: the second argument |
| 2026 | * |
| 2027 | * Show a validation error. |
| 2028 | */ |
| 2029 | static void |
| 2030 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
| 2031 | xmlNodePtr node, xmlNodePtr child, |
| 2032 | const xmlChar *arg1, const xmlChar *arg2) |
| 2033 | { |
| 2034 | xmlChar *msg; |
| 2035 | |
| 2036 | if (ctxt->error == NULL) |
| 2037 | return; |
| 2038 | |
| 2039 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2040 | if (msg == NULL) |
| 2041 | return; |
| 2042 | |
| 2043 | xmlRelaxNGValidErrorContext(ctxt, node, child); |
| 2044 | ctxt->error(ctxt->userData, "%s\n", msg); |
| 2045 | xmlFree(msg); |
| 2046 | } |
| 2047 | |
| 2048 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2049 | * xmlRelaxNGPopErrors: |
| 2050 | * @ctxt: the validation context |
| 2051 | * @level: the error level in the stack |
| 2052 | * |
| 2053 | * pop and discard all errors until the given level is reached |
| 2054 | */ |
| 2055 | static void |
| 2056 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) { |
| 2057 | int i; |
| 2058 | xmlRelaxNGValidErrorPtr err; |
| 2059 | |
| 2060 | for (i = level;i < ctxt->errNr;i++) { |
| 2061 | err = &ctxt->errTab[i]; |
| 2062 | if (err->flags & ERROR_IS_DUP) { |
| 2063 | if (err->arg1 != NULL) |
| 2064 | xmlFree((xmlChar *)err->arg1); |
| 2065 | err->arg1 = NULL; |
| 2066 | if (err->arg2 != NULL) |
| 2067 | xmlFree((xmlChar *)err->arg2); |
| 2068 | err->arg2 = NULL; |
| 2069 | err->flags = 0; |
| 2070 | } |
| 2071 | } |
| 2072 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2073 | if (ctxt->errNr <= 0) |
| 2074 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2075 | } |
| 2076 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2077 | * xmlRelaxNGDumpValidError: |
| 2078 | * @ctxt: the validation context |
| 2079 | * |
| 2080 | * Show all validation error over a given index. |
| 2081 | */ |
| 2082 | static void |
| 2083 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2084 | int i, j; |
| 2085 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2086 | |
| 2087 | for (i = 0;i < ctxt->errNr;i++) { |
| 2088 | err = &ctxt->errTab[i]; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2089 | for (j = 0;j < i;j++) { |
| 2090 | dup = &ctxt->errTab[j]; |
| 2091 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2092 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2093 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2094 | goto skip; |
| 2095 | } |
| 2096 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2097 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2098 | err->arg1, err->arg2); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2099 | skip: |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2100 | if (err->flags & ERROR_IS_DUP) { |
| 2101 | if (err->arg1 != NULL) |
| 2102 | xmlFree((xmlChar *)err->arg1); |
| 2103 | err->arg1 = NULL; |
| 2104 | if (err->arg2 != NULL) |
| 2105 | xmlFree((xmlChar *)err->arg2); |
| 2106 | err->arg2 = NULL; |
| 2107 | err->flags = 0; |
| 2108 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2109 | } |
| 2110 | ctxt->errNr = 0; |
| 2111 | } |
| 2112 | /** |
| 2113 | * xmlRelaxNGAddValidError: |
| 2114 | * @ctxt: the validation context |
| 2115 | * @err: the error number |
| 2116 | * @arg1: the first argument |
| 2117 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2118 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2119 | * |
| 2120 | * Register a validation error, either generating it if it's sure |
| 2121 | * or stacking it for later handling if unsure. |
| 2122 | */ |
| 2123 | static void |
| 2124 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2125 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2126 | { |
| 2127 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 2128 | return; |
| 2129 | |
| 2130 | /* |
| 2131 | * generate the error directly |
| 2132 | */ |
| 2133 | if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) { |
| 2134 | xmlNodePtr node, seq; |
| 2135 | /* |
| 2136 | * Flush first any stacked error which might be the |
| 2137 | * real cause of the problem. |
| 2138 | */ |
| 2139 | if (ctxt->errNr != 0) |
| 2140 | xmlRelaxNGDumpValidError(ctxt); |
| 2141 | if (ctxt->state != NULL) { |
| 2142 | node = ctxt->state->node; |
| 2143 | seq = ctxt->state->seq; |
| 2144 | } else { |
| 2145 | node = seq = NULL; |
| 2146 | } |
| 2147 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
| 2148 | } |
| 2149 | /* |
| 2150 | * Stack the error for later processing if needed |
| 2151 | */ |
| 2152 | else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2153 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2154 | } |
| 2155 | } |
| 2156 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2157 | |
| 2158 | /************************************************************************ |
| 2159 | * * |
| 2160 | * Type library hooks * |
| 2161 | * * |
| 2162 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2163 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
| 2164 | const xmlChar *str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2165 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2166 | /** |
| 2167 | * xmlRelaxNGSchemaTypeHave: |
| 2168 | * @data: data needed for the library |
| 2169 | * @type: the type name |
| 2170 | * |
| 2171 | * Check if the given type is provided by |
| 2172 | * the W3C XMLSchema Datatype library. |
| 2173 | * |
| 2174 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2175 | */ |
| 2176 | static int |
| 2177 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2178 | const xmlChar *type) { |
| 2179 | xmlSchemaTypePtr typ; |
| 2180 | |
| 2181 | if (type == NULL) |
| 2182 | return(-1); |
| 2183 | typ = xmlSchemaGetPredefinedType(type, |
| 2184 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2185 | if (typ == NULL) |
| 2186 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2187 | return(1); |
| 2188 | } |
| 2189 | |
| 2190 | /** |
| 2191 | * xmlRelaxNGSchemaTypeCheck: |
| 2192 | * @data: data needed for the library |
| 2193 | * @type: the type name |
| 2194 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2195 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2196 | * |
| 2197 | * Check if the given type and value are validated by |
| 2198 | * the W3C XMLSchema Datatype library. |
| 2199 | * |
| 2200 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2201 | */ |
| 2202 | static int |
| 2203 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2204 | const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2205 | const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2206 | void **result, |
| 2207 | xmlNodePtr node) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2208 | xmlSchemaTypePtr typ; |
| 2209 | int ret; |
| 2210 | |
| 2211 | /* |
| 2212 | * TODO: the type should be cached ab provided back, interface subject |
| 2213 | * to changes. |
| 2214 | * TODO: handle facets, may require an additional interface and keep |
| 2215 | * the value returned from the validation. |
| 2216 | */ |
| 2217 | if ((type == NULL) || (value == NULL)) |
| 2218 | return(-1); |
| 2219 | typ = xmlSchemaGetPredefinedType(type, |
| 2220 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2221 | if (typ == NULL) |
| 2222 | return(-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2223 | ret = xmlSchemaValPredefTypeNode(typ, value, |
| 2224 | (xmlSchemaValPtr *) result, node); |
| 2225 | if (ret == 2) /* special ID error code */ |
| 2226 | return(2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2227 | if (ret == 0) |
| 2228 | return(1); |
| 2229 | if (ret > 0) |
| 2230 | return(0); |
| 2231 | return(-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2235 | * xmlRelaxNGSchemaFacetCheck: |
| 2236 | * @data: data needed for the library |
| 2237 | * @type: the type name |
| 2238 | * @facet: the facet name |
| 2239 | * @val: the facet value |
| 2240 | * @strval: the string value |
| 2241 | * @value: the value to check |
| 2242 | * |
| 2243 | * Function provided by a type library to check a value facet |
| 2244 | * |
| 2245 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2246 | */ |
| 2247 | static int |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2248 | xmlRelaxNGSchemaFacetCheck (void *data ATTRIBUTE_UNUSED, const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2249 | const xmlChar *facetname, const xmlChar *val, |
| 2250 | const xmlChar *strval, void *value) { |
| 2251 | xmlSchemaFacetPtr facet; |
| 2252 | xmlSchemaTypePtr typ; |
| 2253 | int ret; |
| 2254 | |
| 2255 | if ((type == NULL) || (strval == NULL)) |
| 2256 | return(-1); |
| 2257 | typ = xmlSchemaGetPredefinedType(type, |
| 2258 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2259 | if (typ == NULL) |
| 2260 | return(-1); |
| 2261 | |
| 2262 | facet = xmlSchemaNewFacet(); |
| 2263 | if (facet == NULL) |
| 2264 | return(-1); |
| 2265 | |
| 2266 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
| 2267 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
| 2268 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
| 2269 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
| 2270 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
| 2271 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
| 2272 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
| 2273 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
| 2274 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
| 2275 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
| 2276 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
| 2277 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
| 2278 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
| 2279 | facet->type = XML_SCHEMA_FACET_PATTERN; |
| 2280 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
| 2281 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
| 2282 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
| 2283 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
| 2284 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
| 2285 | facet->type = XML_SCHEMA_FACET_LENGTH; |
| 2286 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
| 2287 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2288 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2289 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2290 | } else { |
| 2291 | xmlSchemaFreeFacet(facet); |
| 2292 | return(-1); |
| 2293 | } |
| 2294 | facet->value = xmlStrdup(val); |
| 2295 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2296 | if (ret != 0) { |
| 2297 | xmlSchemaFreeFacet(facet); |
| 2298 | return(-1); |
| 2299 | } |
| 2300 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2301 | xmlSchemaFreeFacet(facet); |
| 2302 | if (ret != 0) |
| 2303 | return(-1); |
| 2304 | return(0); |
| 2305 | } |
| 2306 | |
| 2307 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame^] | 2308 | * xmlRelaxNGSchemaFreeValue: |
| 2309 | * @data: data needed for the library |
| 2310 | * @value: the value to free |
| 2311 | * |
| 2312 | * Function provided by a type library to free a Schemas value |
| 2313 | * |
| 2314 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2315 | */ |
| 2316 | static void |
| 2317 | xmlRelaxNGSchemaFreeValue (void *data ATTRIBUTE_UNUSED, void *value) { |
| 2318 | xmlSchemaFreeValue(value); |
| 2319 | } |
| 2320 | |
| 2321 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2322 | * xmlRelaxNGSchemaTypeCompare: |
| 2323 | * @data: data needed for the library |
| 2324 | * @type: the type name |
| 2325 | * @value1: the first value |
| 2326 | * @value2: the second value |
| 2327 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame^] | 2328 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2329 | * Datatype library. |
| 2330 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame^] | 2331 | * 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] | 2332 | */ |
| 2333 | static int |
| 2334 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
| 2335 | const xmlChar *type ATTRIBUTE_UNUSED, |
| 2336 | const xmlChar *value1 ATTRIBUTE_UNUSED, |
| 2337 | const xmlChar *value2 ATTRIBUTE_UNUSED) { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame^] | 2338 | int ret; |
| 2339 | xmlSchemaTypePtr typ; |
| 2340 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2341 | |
| 2342 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
| 2343 | return(-1); |
| 2344 | typ = xmlSchemaGetPredefinedType(type, |
| 2345 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2346 | if (typ == NULL) |
| 2347 | return(-1); |
| 2348 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, NULL); |
| 2349 | if (ret != 0) |
| 2350 | return(-1); |
| 2351 | if (res1 == NULL) |
| 2352 | return(-1); |
| 2353 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, NULL); |
| 2354 | if (ret != 0) { |
| 2355 | xmlSchemaFreeValue(res1); |
| 2356 | return(-1); |
| 2357 | } |
| 2358 | if (res1 == NULL) { |
| 2359 | xmlSchemaFreeValue(res1); |
| 2360 | return(-1); |
| 2361 | } |
| 2362 | ret = xmlSchemaCompareValues(res1, res2); |
| 2363 | xmlSchemaFreeValue(res1); |
| 2364 | xmlSchemaFreeValue(res2); |
| 2365 | if (ret == -2) |
| 2366 | return(-1); |
| 2367 | if (ret == 0) |
| 2368 | return(1); |
| 2369 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | /** |
| 2373 | * xmlRelaxNGDefaultTypeHave: |
| 2374 | * @data: data needed for the library |
| 2375 | * @type: the type name |
| 2376 | * |
| 2377 | * Check if the given type is provided by |
| 2378 | * the default datatype library. |
| 2379 | * |
| 2380 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2381 | */ |
| 2382 | static int |
| 2383 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar *type) { |
| 2384 | if (type == NULL) |
| 2385 | return(-1); |
| 2386 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2387 | return(1); |
| 2388 | if (xmlStrEqual(type, BAD_CAST "token")) |
| 2389 | return(1); |
| 2390 | return(0); |
| 2391 | } |
| 2392 | |
| 2393 | /** |
| 2394 | * xmlRelaxNGDefaultTypeCheck: |
| 2395 | * @data: data needed for the library |
| 2396 | * @type: the type name |
| 2397 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2398 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2399 | * |
| 2400 | * Check if the given type and value are validated by |
| 2401 | * the default datatype library. |
| 2402 | * |
| 2403 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2404 | */ |
| 2405 | static int |
| 2406 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
| 2407 | const xmlChar *type ATTRIBUTE_UNUSED, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2408 | const xmlChar *value ATTRIBUTE_UNUSED, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2409 | void **result ATTRIBUTE_UNUSED, |
| 2410 | xmlNodePtr node ATTRIBUTE_UNUSED) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2411 | if (value == NULL) |
| 2412 | return(-1); |
| 2413 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2414 | return(1); |
| 2415 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2416 | return(1); |
| 2417 | } |
| 2418 | |
| 2419 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | /** |
| 2423 | * xmlRelaxNGDefaultTypeCompare: |
| 2424 | * @data: data needed for the library |
| 2425 | * @type: the type name |
| 2426 | * @value1: the first value |
| 2427 | * @value2: the second value |
| 2428 | * |
| 2429 | * Compare two values accordingly a type from the default |
| 2430 | * datatype library. |
| 2431 | * |
| 2432 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2433 | */ |
| 2434 | static int |
| 2435 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
| 2436 | const xmlChar *type ATTRIBUTE_UNUSED, |
| 2437 | const xmlChar *value1 ATTRIBUTE_UNUSED, |
| 2438 | const xmlChar *value2 ATTRIBUTE_UNUSED) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2439 | int ret = -1; |
| 2440 | |
| 2441 | if (xmlStrEqual(type, BAD_CAST "string")) { |
| 2442 | ret = xmlStrEqual(value1, value2); |
| 2443 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
| 2444 | if (!xmlStrEqual(value1, value2)) { |
| 2445 | xmlChar *nval, *nvalue; |
| 2446 | |
| 2447 | /* |
| 2448 | * TODO: trivial optimizations are possible by |
| 2449 | * computing at compile-time |
| 2450 | */ |
| 2451 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2452 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
| 2453 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2454 | if ((nval == NULL) || (nvalue == NULL)) |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2455 | ret = -1; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2456 | else if (xmlStrEqual(nval, nvalue)) |
| 2457 | ret = 1; |
| 2458 | else |
| 2459 | ret = 0; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2460 | if (nval != NULL) |
| 2461 | xmlFree(nval); |
| 2462 | if (nvalue != NULL) |
| 2463 | xmlFree(nvalue); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2464 | } else |
| 2465 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2466 | } |
| 2467 | return(ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | static int xmlRelaxNGTypeInitialized = 0; |
| 2471 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2472 | |
| 2473 | /** |
| 2474 | * xmlRelaxNGFreeTypeLibrary: |
| 2475 | * @lib: the type library structure |
| 2476 | * @namespace: the URI bound to the library |
| 2477 | * |
| 2478 | * Free the structure associated to the type library |
| 2479 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2480 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2481 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
| 2482 | const xmlChar *namespace ATTRIBUTE_UNUSED) { |
| 2483 | if (lib == NULL) |
| 2484 | return; |
| 2485 | if (lib->namespace != NULL) |
| 2486 | xmlFree((xmlChar *)lib->namespace); |
| 2487 | xmlFree(lib); |
| 2488 | } |
| 2489 | |
| 2490 | /** |
| 2491 | * xmlRelaxNGRegisterTypeLibrary: |
| 2492 | * @namespace: the URI bound to the library |
| 2493 | * @data: data associated to the library |
| 2494 | * @have: the provide function |
| 2495 | * @check: the checking function |
| 2496 | * @comp: the comparison function |
| 2497 | * |
| 2498 | * Register a new type library |
| 2499 | * |
| 2500 | * Returns 0 in case of success and -1 in case of error. |
| 2501 | */ |
| 2502 | static int |
| 2503 | xmlRelaxNGRegisterTypeLibrary(const xmlChar *namespace, void *data, |
| 2504 | xmlRelaxNGTypeHave have, xmlRelaxNGTypeCheck check, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2505 | xmlRelaxNGTypeCompare comp, xmlRelaxNGFacetCheck facet, |
| 2506 | xmlRelaxNGTypeFree freef) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2507 | xmlRelaxNGTypeLibraryPtr lib; |
| 2508 | int ret; |
| 2509 | |
| 2510 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
| 2511 | (check == NULL) || (comp == NULL)) |
| 2512 | return(-1); |
| 2513 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
| 2514 | xmlGenericError(xmlGenericErrorContext, |
| 2515 | "Relax-NG types library '%s' already registered\n", |
| 2516 | namespace); |
| 2517 | return(-1); |
| 2518 | } |
| 2519 | lib = (xmlRelaxNGTypeLibraryPtr) xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
| 2520 | if (lib == NULL) { |
| 2521 | xmlGenericError(xmlGenericErrorContext, |
| 2522 | "Relax-NG types library '%s' malloc() failed\n", |
| 2523 | namespace); |
| 2524 | return (-1); |
| 2525 | } |
| 2526 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2527 | lib->namespace = xmlStrdup(namespace); |
| 2528 | lib->data = data; |
| 2529 | lib->have = have; |
| 2530 | lib->comp = comp; |
| 2531 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2532 | lib->facet = facet; |
| 2533 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2534 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2535 | if (ret < 0) { |
| 2536 | xmlGenericError(xmlGenericErrorContext, |
| 2537 | "Relax-NG types library failed to register '%s'\n", |
| 2538 | namespace); |
| 2539 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2540 | return(-1); |
| 2541 | } |
| 2542 | return(0); |
| 2543 | } |
| 2544 | |
| 2545 | /** |
| 2546 | * xmlRelaxNGInitTypes: |
| 2547 | * |
| 2548 | * Initilize the default type libraries. |
| 2549 | * |
| 2550 | * Returns 0 in case of success and -1 in case of error. |
| 2551 | */ |
| 2552 | static int |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2553 | xmlRelaxNGInitTypes(void) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2554 | if (xmlRelaxNGTypeInitialized != 0) |
| 2555 | return(0); |
| 2556 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2557 | if (xmlRelaxNGRegisteredTypes == NULL) { |
| 2558 | xmlGenericError(xmlGenericErrorContext, |
| 2559 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2560 | return(-1); |
| 2561 | } |
| 2562 | xmlRelaxNGRegisterTypeLibrary( |
| 2563 | BAD_CAST "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2564 | NULL, |
| 2565 | xmlRelaxNGSchemaTypeHave, |
| 2566 | xmlRelaxNGSchemaTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2567 | xmlRelaxNGSchemaTypeCompare, |
| 2568 | xmlRelaxNGSchemaFacetCheck, |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame^] | 2569 | xmlRelaxNGSchemaFreeValue); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2570 | xmlRelaxNGRegisterTypeLibrary( |
| 2571 | xmlRelaxNGNs, |
| 2572 | NULL, |
| 2573 | xmlRelaxNGDefaultTypeHave, |
| 2574 | xmlRelaxNGDefaultTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2575 | xmlRelaxNGDefaultTypeCompare, |
| 2576 | NULL, |
| 2577 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2578 | xmlRelaxNGTypeInitialized = 1; |
| 2579 | return(0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | /** |
| 2583 | * xmlRelaxNGCleanupTypes: |
| 2584 | * |
| 2585 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2586 | */ |
| 2587 | void |
| 2588 | xmlRelaxNGCleanupTypes(void) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2589 | if (xmlRelaxNGTypeInitialized == 0) |
| 2590 | return; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2591 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2592 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
| 2593 | xmlRelaxNGFreeTypeLibrary); |
| 2594 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | /************************************************************************ |
| 2598 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2599 | * Compiling element content into regexp * |
| 2600 | * * |
| 2601 | * Sometime the element content can be compiled into a pure regexp, * |
| 2602 | * This allows a faster execution and streamability at that level * |
| 2603 | * * |
| 2604 | ************************************************************************/ |
| 2605 | |
| 2606 | /** |
| 2607 | * xmlRelaxNGIsCompileable: |
| 2608 | * @define: the definition to check |
| 2609 | * |
| 2610 | * Check if a definition is nullable. |
| 2611 | * |
| 2612 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2613 | */ |
| 2614 | static int |
| 2615 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) { |
| 2616 | if (def == NULL) { |
| 2617 | return(-1); |
| 2618 | } |
| 2619 | switch(def->type) { |
| 2620 | case XML_RELAXNG_REF: |
| 2621 | case XML_RELAXNG_EXTERNALREF: |
| 2622 | case XML_RELAXNG_PARENTREF: |
| 2623 | case XML_RELAXNG_NOOP: |
| 2624 | case XML_RELAXNG_START: |
| 2625 | return(xmlRelaxNGIsCompileable(def->content)); |
| 2626 | case XML_RELAXNG_TEXT: |
| 2627 | case XML_RELAXNG_DATATYPE: |
| 2628 | case XML_RELAXNG_LIST: |
| 2629 | case XML_RELAXNG_PARAM: |
| 2630 | case XML_RELAXNG_VALUE: |
| 2631 | |
| 2632 | case XML_RELAXNG_EMPTY: |
| 2633 | case XML_RELAXNG_ELEMENT: |
| 2634 | return(1); |
| 2635 | case XML_RELAXNG_OPTIONAL: |
| 2636 | case XML_RELAXNG_ZEROORMORE: |
| 2637 | case XML_RELAXNG_ONEORMORE: |
| 2638 | case XML_RELAXNG_CHOICE: |
| 2639 | case XML_RELAXNG_GROUP: |
| 2640 | case XML_RELAXNG_DEF: { |
| 2641 | xmlRelaxNGDefinePtr list; |
| 2642 | int ret; |
| 2643 | |
| 2644 | list = def->content; |
| 2645 | while (list != NULL) { |
| 2646 | ret = xmlRelaxNGIsCompileable(list); |
| 2647 | if (ret != 1) |
| 2648 | return(ret); |
| 2649 | list = list->next; |
| 2650 | } |
| 2651 | return(1); |
| 2652 | } |
| 2653 | case XML_RELAXNG_EXCEPT: |
| 2654 | case XML_RELAXNG_ATTRIBUTE: |
| 2655 | case XML_RELAXNG_INTERLEAVE: |
| 2656 | return(0); |
| 2657 | case XML_RELAXNG_NOT_ALLOWED: |
| 2658 | return(-1); |
| 2659 | } |
| 2660 | return(-1); |
| 2661 | } |
| 2662 | |
| 2663 | /************************************************************************ |
| 2664 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2665 | * Parsing functions * |
| 2666 | * * |
| 2667 | ************************************************************************/ |
| 2668 | |
| 2669 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute( |
| 2670 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 2671 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement( |
| 2672 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 2673 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns( |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 2674 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, int group); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 2675 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern( |
| 2676 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 2677 | static xmlRelaxNGPtr xmlRelaxNGParseDocument( |
| 2678 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 2679 | static int xmlRelaxNGParseGrammarContent( |
| 2680 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 2681 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass( |
| 2682 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 2683 | xmlRelaxNGDefinePtr def); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 2684 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar( |
| 2685 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2686 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 2687 | xmlRelaxNGDefinePtr define, xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2688 | |
| 2689 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 2690 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2691 | |
| 2692 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2693 | * xmlRelaxNGIsNullable: |
| 2694 | * @define: the definition to verify |
| 2695 | * |
| 2696 | * Check if a definition is nullable. |
| 2697 | * |
| 2698 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2699 | */ |
| 2700 | static int |
| 2701 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) { |
| 2702 | int ret; |
| 2703 | if (define == NULL) |
| 2704 | return(-1); |
| 2705 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 2706 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2707 | return(1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 2708 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2709 | return(0); |
| 2710 | switch (define->type) { |
| 2711 | case XML_RELAXNG_EMPTY: |
| 2712 | case XML_RELAXNG_TEXT: |
| 2713 | ret = 1; break; |
| 2714 | case XML_RELAXNG_NOOP: |
| 2715 | case XML_RELAXNG_DEF: |
| 2716 | case XML_RELAXNG_REF: |
| 2717 | case XML_RELAXNG_EXTERNALREF: |
| 2718 | case XML_RELAXNG_PARENTREF: |
| 2719 | case XML_RELAXNG_ONEORMORE: |
| 2720 | ret = xmlRelaxNGIsNullable(define->content); |
| 2721 | break; |
| 2722 | case XML_RELAXNG_EXCEPT: |
| 2723 | case XML_RELAXNG_NOT_ALLOWED: |
| 2724 | case XML_RELAXNG_ELEMENT: |
| 2725 | case XML_RELAXNG_DATATYPE: |
| 2726 | case XML_RELAXNG_PARAM: |
| 2727 | case XML_RELAXNG_VALUE: |
| 2728 | case XML_RELAXNG_LIST: |
| 2729 | case XML_RELAXNG_ATTRIBUTE: |
| 2730 | ret = 0; break; |
| 2731 | case XML_RELAXNG_CHOICE: { |
| 2732 | xmlRelaxNGDefinePtr list = define->content; |
| 2733 | |
| 2734 | while (list != NULL) { |
| 2735 | ret = xmlRelaxNGIsNullable(list); |
| 2736 | if (ret != 0) |
| 2737 | goto done; |
| 2738 | list = list->next; |
| 2739 | } |
| 2740 | ret = 0; break; |
| 2741 | } |
| 2742 | case XML_RELAXNG_START: |
| 2743 | case XML_RELAXNG_INTERLEAVE: |
| 2744 | case XML_RELAXNG_GROUP: { |
| 2745 | xmlRelaxNGDefinePtr list = define->content; |
| 2746 | |
| 2747 | while (list != NULL) { |
| 2748 | ret = xmlRelaxNGIsNullable(list); |
| 2749 | if (ret != 1) |
| 2750 | goto done; |
| 2751 | list = list->next; |
| 2752 | } |
| 2753 | return(1); |
| 2754 | } |
| 2755 | default: |
| 2756 | return(-1); |
| 2757 | } |
| 2758 | done: |
| 2759 | if (ret == 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 2760 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2761 | if (ret == 1) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 2762 | define->dflags |= IS_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2763 | return(ret); |
| 2764 | } |
| 2765 | |
| 2766 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2767 | * xmlRelaxNGIsBlank: |
| 2768 | * @str: a string |
| 2769 | * |
| 2770 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 2771 | * |
| 2772 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 2773 | */ |
| 2774 | static int |
| 2775 | xmlRelaxNGIsBlank(xmlChar *str) { |
| 2776 | if (str == NULL) |
| 2777 | return(1); |
| 2778 | while (*str != 0) { |
| 2779 | if (!(IS_BLANK(*str))) return(0); |
| 2780 | str++; |
| 2781 | } |
| 2782 | return(1); |
| 2783 | } |
| 2784 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2785 | /** |
| 2786 | * xmlRelaxNGGetDataTypeLibrary: |
| 2787 | * @ctxt: a Relax-NG parser context |
| 2788 | * @node: the current data or value element |
| 2789 | * |
| 2790 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 2791 | * |
| 2792 | * Returns the datatypeLibary value or NULL if not found |
| 2793 | */ |
| 2794 | static xmlChar * |
| 2795 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 2796 | xmlNodePtr node) { |
| 2797 | xmlChar *ret, *escape; |
| 2798 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2799 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
| 2800 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 2801 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2802 | if (ret[0] == 0) { |
| 2803 | xmlFree(ret); |
| 2804 | return(NULL); |
| 2805 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2806 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2807 | if (escape == NULL) { |
| 2808 | return(ret); |
| 2809 | } |
| 2810 | xmlFree(ret); |
| 2811 | return(escape); |
| 2812 | } |
| 2813 | } |
| 2814 | node = node->parent; |
| 2815 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 2816 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 2817 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2818 | if (ret[0] == 0) { |
| 2819 | xmlFree(ret); |
| 2820 | return(NULL); |
| 2821 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 2822 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 2823 | if (escape == NULL) { |
| 2824 | return(ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2825 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 2826 | xmlFree(ret); |
| 2827 | return(escape); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2828 | } |
| 2829 | node = node->parent; |
| 2830 | } |
| 2831 | return(NULL); |
| 2832 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2833 | |
| 2834 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2835 | * xmlRelaxNGParseValue: |
| 2836 | * @ctxt: a Relax-NG parser context |
| 2837 | * @node: the data node. |
| 2838 | * |
| 2839 | * parse the content of a RelaxNG value node. |
| 2840 | * |
| 2841 | * Returns the definition pointer or NULL in case of error |
| 2842 | */ |
| 2843 | static xmlRelaxNGDefinePtr |
| 2844 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 2845 | xmlRelaxNGDefinePtr def = NULL; |
| 2846 | xmlRelaxNGTypeLibraryPtr lib; |
| 2847 | xmlChar *type; |
| 2848 | xmlChar *library; |
| 2849 | int tmp; |
| 2850 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2851 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2852 | if (def == NULL) |
| 2853 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2854 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2855 | |
| 2856 | type = xmlGetProp(node, BAD_CAST "type"); |
| 2857 | if (type != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2858 | xmlRelaxNGNormExtSpace(type); |
| 2859 | if (xmlValidateNCName(type, 0)) { |
| 2860 | if (ctxt->error != NULL) |
| 2861 | ctxt->error(ctxt->userData, |
| 2862 | "value type '%s' is not an NCName\n", |
| 2863 | type); |
| 2864 | ctxt->nbErrors++; |
| 2865 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2866 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 2867 | if (library == NULL) |
| 2868 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 2869 | |
| 2870 | def->name = type; |
| 2871 | def->ns = library; |
| 2872 | |
| 2873 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 2874 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 2875 | if (lib == NULL) { |
| 2876 | if (ctxt->error != NULL) |
| 2877 | ctxt->error(ctxt->userData, |
| 2878 | "Use of unregistered type library '%s'\n", |
| 2879 | library); |
| 2880 | ctxt->nbErrors++; |
| 2881 | def->data = NULL; |
| 2882 | } else { |
| 2883 | def->data = lib; |
| 2884 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 2885 | if (ctxt->error != NULL) |
| 2886 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2887 | "Internal error with type library '%s': no 'have'\n", |
| 2888 | library); |
| 2889 | ctxt->nbErrors++; |
| 2890 | } else { |
| 2891 | tmp = lib->have(lib->data, def->name); |
| 2892 | if (tmp != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 2893 | if (ctxt->error != NULL) |
| 2894 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2895 | "Error type '%s' is not exported by type library '%s'\n", |
| 2896 | def->name, library); |
| 2897 | ctxt->nbErrors++; |
| 2898 | } |
| 2899 | } |
| 2900 | } |
| 2901 | } |
| 2902 | if (node->children == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2903 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 2904 | } else if (((node->children->type != XML_TEXT_NODE) && |
| 2905 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 2906 | (node->children->next != NULL)) { |
| 2907 | if (ctxt->error != NULL) |
| 2908 | ctxt->error(ctxt->userData, |
| 2909 | "Expecting a single text value for <value>content\n"); |
| 2910 | ctxt->nbErrors++; |
| 2911 | } else { |
| 2912 | def->value = xmlNodeGetContent(node); |
| 2913 | if (def->value == NULL) { |
| 2914 | if (ctxt->error != NULL) |
| 2915 | ctxt->error(ctxt->userData, |
| 2916 | "Element <value> has no content\n"); |
| 2917 | ctxt->nbErrors++; |
| 2918 | } |
| 2919 | } |
| 2920 | /* TODO check ahead of time that the value is okay per the type */ |
| 2921 | return(def); |
| 2922 | } |
| 2923 | |
| 2924 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2925 | * xmlRelaxNGParseData: |
| 2926 | * @ctxt: a Relax-NG parser context |
| 2927 | * @node: the data node. |
| 2928 | * |
| 2929 | * parse the content of a RelaxNG data node. |
| 2930 | * |
| 2931 | * Returns the definition pointer or NULL in case of error |
| 2932 | */ |
| 2933 | static xmlRelaxNGDefinePtr |
| 2934 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 2935 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 2936 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2937 | xmlRelaxNGTypeLibraryPtr lib; |
| 2938 | xmlChar *type; |
| 2939 | xmlChar *library; |
| 2940 | xmlNodePtr content; |
| 2941 | int tmp; |
| 2942 | |
| 2943 | type = xmlGetProp(node, BAD_CAST "type"); |
| 2944 | if (type == NULL) { |
| 2945 | if (ctxt->error != NULL) |
| 2946 | ctxt->error(ctxt->userData, |
| 2947 | "data has no type\n"); |
| 2948 | ctxt->nbErrors++; |
| 2949 | return(NULL); |
| 2950 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 2951 | xmlRelaxNGNormExtSpace(type); |
| 2952 | if (xmlValidateNCName(type, 0)) { |
| 2953 | if (ctxt->error != NULL) |
| 2954 | ctxt->error(ctxt->userData, |
| 2955 | "data type '%s' is not an NCName\n", |
| 2956 | type); |
| 2957 | ctxt->nbErrors++; |
| 2958 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2959 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 2960 | if (library == NULL) |
| 2961 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 2962 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2963 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2964 | if (def == NULL) { |
| 2965 | xmlFree(type); |
| 2966 | return(NULL); |
| 2967 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 2968 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2969 | def->name = type; |
| 2970 | def->ns = library; |
| 2971 | |
| 2972 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 2973 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 2974 | if (lib == NULL) { |
| 2975 | if (ctxt->error != NULL) |
| 2976 | ctxt->error(ctxt->userData, |
| 2977 | "Use of unregistered type library '%s'\n", |
| 2978 | library); |
| 2979 | ctxt->nbErrors++; |
| 2980 | def->data = NULL; |
| 2981 | } else { |
| 2982 | def->data = lib; |
| 2983 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 2984 | if (ctxt->error != NULL) |
| 2985 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2986 | "Internal error with type library '%s': no 'have'\n", |
| 2987 | library); |
| 2988 | ctxt->nbErrors++; |
| 2989 | } else { |
| 2990 | tmp = lib->have(lib->data, def->name); |
| 2991 | if (tmp != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 2992 | if (ctxt->error != NULL) |
| 2993 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2994 | "Error type '%s' is not exported by type library '%s'\n", |
| 2995 | def->name, library); |
| 2996 | ctxt->nbErrors++; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2997 | } else if ((xmlStrEqual(library, BAD_CAST |
| 2998 | "http://www.w3.org/2001/XMLSchema-datatypes")) && |
| 2999 | ((xmlStrEqual(def->name, BAD_CAST "IDREF")) || |
| 3000 | (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3001 | ctxt->idref = 1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3002 | } |
| 3003 | } |
| 3004 | } |
| 3005 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3006 | |
| 3007 | /* |
| 3008 | * Handle optional params |
| 3009 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3010 | while (content != NULL) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3011 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3012 | break; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3013 | if (xmlStrEqual(library, |
| 3014 | BAD_CAST"http://relaxng.org/ns/structure/1.0")) { |
| 3015 | if (ctxt->error != NULL) |
| 3016 | ctxt->error(ctxt->userData, |
| 3017 | "Type library '%s' does not allow type parameters\n", |
| 3018 | library); |
| 3019 | ctxt->nbErrors++; |
| 3020 | content = content->next; |
| 3021 | while ((content != NULL) && |
| 3022 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3023 | content = content->next; |
| 3024 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3025 | param = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3026 | if (param != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3027 | param->type = XML_RELAXNG_PARAM; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3028 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3029 | if (param->name == NULL) { |
| 3030 | if (ctxt->error != NULL) |
| 3031 | ctxt->error(ctxt->userData, |
| 3032 | "param has no name\n"); |
| 3033 | ctxt->nbErrors++; |
| 3034 | } |
| 3035 | param->value = xmlNodeGetContent(content); |
| 3036 | if (lastparam == NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3037 | def->attrs = lastparam = param; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3038 | } else { |
| 3039 | lastparam->next = param; |
| 3040 | lastparam = param; |
| 3041 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 3042 | if (lib != NULL) { |
| 3043 | } |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3044 | } |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3045 | content = content->next; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3046 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3047 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3048 | /* |
| 3049 | * Handle optional except |
| 3050 | */ |
| 3051 | if ((content != NULL) && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3052 | xmlNodePtr child; |
| 3053 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
| 3054 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3055 | except = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3056 | if (except == NULL) { |
| 3057 | return(def); |
| 3058 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3059 | except->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3060 | child = content->children; |
| 3061 | if (last == NULL) { |
| 3062 | def->content = except; |
| 3063 | } else { |
| 3064 | last->next = except; |
| 3065 | } |
| 3066 | if (child == NULL) { |
| 3067 | if (ctxt->error != NULL) |
| 3068 | ctxt->error(ctxt->userData, |
| 3069 | "except has no content\n"); |
| 3070 | ctxt->nbErrors++; |
| 3071 | } |
| 3072 | while (child != NULL) { |
| 3073 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3074 | if (tmp2 != NULL) { |
| 3075 | if (last2 == NULL) { |
| 3076 | except->content = last2 = tmp2; |
| 3077 | } else { |
| 3078 | last2->next = tmp2; |
| 3079 | last2 = tmp2; |
| 3080 | } |
| 3081 | } |
| 3082 | child = child->next; |
| 3083 | } |
| 3084 | content = content->next; |
| 3085 | } |
| 3086 | /* |
| 3087 | * Check there is no unhandled data |
| 3088 | */ |
| 3089 | if (content != NULL) { |
| 3090 | if (ctxt->error != NULL) |
| 3091 | ctxt->error(ctxt->userData, |
| 3092 | "Element data has unexpected content %s\n", content->name); |
| 3093 | ctxt->nbErrors++; |
| 3094 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3095 | |
| 3096 | return(def); |
| 3097 | } |
| 3098 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3099 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3100 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3101 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3102 | * xmlRelaxNGCompareNameClasses: |
| 3103 | * @defs1: the first element/attribute defs |
| 3104 | * @defs2: the second element/attribute defs |
| 3105 | * @name: the restriction on the name |
| 3106 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3107 | * |
| 3108 | * Compare the 2 lists of element definitions. The comparison is |
| 3109 | * that if both lists do not accept the same QNames, it returns 1 |
| 3110 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3111 | * |
| 3112 | * Returns 1 disttinct, 0 if equal |
| 3113 | */ |
| 3114 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3115 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
| 3116 | xmlRelaxNGDefinePtr def2) { |
| 3117 | int ret = 1; |
| 3118 | xmlNode node; |
| 3119 | xmlNs ns; |
| 3120 | xmlRelaxNGValidCtxt ctxt; |
| 3121 | ctxt.flags = FLAGS_IGNORABLE; |
| 3122 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3123 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3124 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3125 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
| 3126 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3127 | if (def2->type == XML_RELAXNG_TEXT) |
| 3128 | return(1); |
| 3129 | if (def1->name != NULL) { |
| 3130 | node.name = def1->name; |
| 3131 | } else { |
| 3132 | node.name = invalidName; |
| 3133 | } |
| 3134 | node.ns = &ns; |
| 3135 | if (def1->ns != NULL) { |
| 3136 | if (def1->ns[0] == 0) { |
| 3137 | node.ns = NULL; |
| 3138 | } else { |
| 3139 | ns.href = def1->ns; |
| 3140 | } |
| 3141 | } else { |
| 3142 | ns.href = invalidName; |
| 3143 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3144 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3145 | if (def1->nameClass != NULL) { |
| 3146 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3147 | } else { |
| 3148 | ret = 0; |
| 3149 | } |
| 3150 | } else { |
| 3151 | ret = 1; |
| 3152 | } |
| 3153 | } else if (def1->type == XML_RELAXNG_TEXT) { |
| 3154 | if (def2->type == XML_RELAXNG_TEXT) |
| 3155 | return(0); |
| 3156 | return(1); |
| 3157 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3158 | TODO |
| 3159 | ret = 0; |
| 3160 | } else { |
| 3161 | TODO |
| 3162 | ret = 0; |
| 3163 | } |
| 3164 | if (ret == 0) |
| 3165 | return(ret); |
| 3166 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
| 3167 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3168 | if (def2->name != NULL) { |
| 3169 | node.name = def2->name; |
| 3170 | } else { |
| 3171 | node.name = invalidName; |
| 3172 | } |
| 3173 | node.ns = &ns; |
| 3174 | if (def2->ns != NULL) { |
| 3175 | if (def2->ns[0] == 0) { |
| 3176 | node.ns = NULL; |
| 3177 | } else { |
| 3178 | ns.href = def2->ns; |
| 3179 | } |
| 3180 | } else { |
| 3181 | ns.href = invalidName; |
| 3182 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3183 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3184 | if (def2->nameClass != NULL) { |
| 3185 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3186 | } else { |
| 3187 | ret = 0; |
| 3188 | } |
| 3189 | } else { |
| 3190 | ret = 1; |
| 3191 | } |
| 3192 | } else { |
| 3193 | TODO |
| 3194 | ret = 0; |
| 3195 | } |
| 3196 | |
| 3197 | return(ret); |
| 3198 | } |
| 3199 | |
| 3200 | /** |
| 3201 | * xmlRelaxNGCompareElemDefLists: |
| 3202 | * @ctxt: a Relax-NG parser context |
| 3203 | * @defs1: the first list of element/attribute defs |
| 3204 | * @defs2: the second list of element/attribute defs |
| 3205 | * |
| 3206 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3207 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3208 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3209 | * |
| 3210 | * Returns 1 disttinct, 0 if equal |
| 3211 | */ |
| 3212 | static int |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3213 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3214 | xmlRelaxNGDefinePtr *def1, |
| 3215 | xmlRelaxNGDefinePtr *def2) { |
| 3216 | xmlRelaxNGDefinePtr *basedef2 = def2; |
| 3217 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3218 | if ((def1 == NULL) || (def2 == NULL)) |
| 3219 | return(1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3220 | if ((*def1 == NULL) || (*def2 == NULL)) |
| 3221 | return(1); |
| 3222 | while (*def1 != NULL) { |
| 3223 | while ((*def2) != NULL) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3224 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3225 | return(0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3226 | def2++; |
| 3227 | } |
| 3228 | def2 = basedef2; |
| 3229 | def1++; |
| 3230 | } |
| 3231 | return(1); |
| 3232 | } |
| 3233 | |
| 3234 | /** |
| 3235 | * xmlRelaxNGGetElements: |
| 3236 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3237 | * @def: the definition definition |
| 3238 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3239 | * |
| 3240 | * Compute the list of top elements a definition can generate |
| 3241 | * |
| 3242 | * Returns a list of elements or NULL if none was found. |
| 3243 | */ |
| 3244 | static xmlRelaxNGDefinePtr * |
| 3245 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3246 | xmlRelaxNGDefinePtr def, |
| 3247 | int eora) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3248 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3249 | int len = 0; |
| 3250 | int max = 0; |
| 3251 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3252 | /* |
| 3253 | * Don't run that check in case of error. Infinite recursion |
| 3254 | * becomes possible. |
| 3255 | */ |
| 3256 | if (ctxt->nbErrors != 0) |
| 3257 | return(NULL); |
| 3258 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3259 | parent = NULL; |
| 3260 | cur = def; |
| 3261 | while (cur != NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3262 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3263 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3264 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3265 | if (ret == NULL) { |
| 3266 | max = 10; |
| 3267 | ret = (xmlRelaxNGDefinePtr *) |
| 3268 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3269 | if (ret == NULL) { |
| 3270 | if (ctxt->error != NULL) |
| 3271 | ctxt->error(ctxt->userData, |
| 3272 | "Out of memory in element search\n"); |
| 3273 | ctxt->nbErrors++; |
| 3274 | return(NULL); |
| 3275 | } |
| 3276 | } else if (max <= len) { |
| 3277 | max *= 2; |
| 3278 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3279 | if (ret == NULL) { |
| 3280 | if (ctxt->error != NULL) |
| 3281 | ctxt->error(ctxt->userData, |
| 3282 | "Out of memory in element search\n"); |
| 3283 | ctxt->nbErrors++; |
| 3284 | return(NULL); |
| 3285 | } |
| 3286 | } |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3287 | ret[len++] = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3288 | ret[len] = NULL; |
| 3289 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3290 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3291 | (cur->type == XML_RELAXNG_GROUP) || |
| 3292 | (cur->type == XML_RELAXNG_ONEORMORE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3293 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3294 | (cur->type == XML_RELAXNG_OPTIONAL) || |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3295 | (cur->type == XML_RELAXNG_PARENTREF) || |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3296 | (cur->type == XML_RELAXNG_REF) || |
| 3297 | (cur->type == XML_RELAXNG_DEF)) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3298 | /* |
| 3299 | * Don't go within elements or attributes or string values. |
| 3300 | * Just gather the element top list |
| 3301 | */ |
| 3302 | if (cur->content != NULL) { |
| 3303 | parent = cur; |
| 3304 | cur = cur->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3305 | tmp = cur; |
| 3306 | while (tmp != NULL) { |
| 3307 | tmp->parent = parent; |
| 3308 | tmp = tmp->next; |
| 3309 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3310 | continue; |
| 3311 | } |
| 3312 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3313 | if (cur == def) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3314 | break; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3315 | if (cur->next != NULL) { |
| 3316 | cur = cur->next; |
| 3317 | continue; |
| 3318 | } |
| 3319 | do { |
| 3320 | cur = cur->parent; |
| 3321 | if (cur == NULL) break; |
| 3322 | if (cur == def) return(ret); |
| 3323 | if (cur->next != NULL) { |
| 3324 | cur = cur->next; |
| 3325 | break; |
| 3326 | } |
| 3327 | } while (cur != NULL); |
| 3328 | } |
| 3329 | return(ret); |
| 3330 | } |
| 3331 | |
| 3332 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3333 | * xmlRelaxNGCheckChoiceDeterminism: |
| 3334 | * @ctxt: a Relax-NG parser context |
| 3335 | * @def: the choice definition |
| 3336 | * |
| 3337 | * Also used to find indeterministic pattern in choice |
| 3338 | */ |
| 3339 | static void |
| 3340 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
| 3341 | xmlRelaxNGDefinePtr def) { |
| 3342 | xmlRelaxNGDefinePtr **list; |
| 3343 | xmlRelaxNGDefinePtr cur; |
| 3344 | int nbchild = 0, i, j, ret; |
| 3345 | int is_nullable = 0; |
| 3346 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3347 | xmlHashTablePtr triage = NULL; |
| 3348 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3349 | |
| 3350 | if ((def == NULL) || |
| 3351 | (def->type != XML_RELAXNG_CHOICE)) |
| 3352 | return; |
| 3353 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3354 | if (def->dflags & IS_PROCESSED) |
| 3355 | return; |
| 3356 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3357 | /* |
| 3358 | * Don't run that check in case of error. Infinite recursion |
| 3359 | * becomes possible. |
| 3360 | */ |
| 3361 | if (ctxt->nbErrors != 0) |
| 3362 | return; |
| 3363 | |
| 3364 | is_nullable = xmlRelaxNGIsNullable(def); |
| 3365 | |
| 3366 | cur = def->content; |
| 3367 | while (cur != NULL) { |
| 3368 | nbchild++; |
| 3369 | cur = cur->next; |
| 3370 | } |
| 3371 | |
| 3372 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 3373 | sizeof(xmlRelaxNGDefinePtr *)); |
| 3374 | if (list == NULL) { |
| 3375 | if (ctxt->error != NULL) |
| 3376 | ctxt->error(ctxt->userData, |
| 3377 | "Out of memory in choice computation\n"); |
| 3378 | ctxt->nbErrors++; |
| 3379 | return; |
| 3380 | } |
| 3381 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3382 | /* |
| 3383 | * a bit strong but safe |
| 3384 | */ |
| 3385 | if (is_nullable == 0) { |
| 3386 | triage = xmlHashCreate(10); |
| 3387 | } else { |
| 3388 | is_triable = 0; |
| 3389 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3390 | cur = def->content; |
| 3391 | while (cur != NULL) { |
| 3392 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3393 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 3394 | is_triable = 0; |
| 3395 | } else if (is_triable == 1) { |
| 3396 | xmlRelaxNGDefinePtr *tmp; |
| 3397 | int res; |
| 3398 | |
| 3399 | tmp = list[i]; |
| 3400 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 3401 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 3402 | res = xmlHashAddEntry2(triage, |
| 3403 | BAD_CAST "#text", NULL, |
| 3404 | (void *)cur); |
| 3405 | if (res != 0) |
| 3406 | is_triable = -1; |
| 3407 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 3408 | ((*tmp)->name != NULL)) { |
| 3409 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3410 | res = xmlHashAddEntry2(triage, |
| 3411 | (*tmp)->name, NULL, |
| 3412 | (void *)cur); |
| 3413 | else |
| 3414 | res = xmlHashAddEntry2(triage, |
| 3415 | (*tmp)->name, (*tmp)->ns, |
| 3416 | (void *)cur); |
| 3417 | if (res != 0) |
| 3418 | is_triable = -1; |
| 3419 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 3420 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3421 | res = xmlHashAddEntry2(triage, |
| 3422 | BAD_CAST "#any", NULL, |
| 3423 | (void *)cur); |
| 3424 | else |
| 3425 | res = xmlHashAddEntry2(triage, |
| 3426 | BAD_CAST "#any", (*tmp)->ns, |
| 3427 | (void *)cur); |
| 3428 | if (res != 0) |
| 3429 | is_triable = -1; |
| 3430 | } else { |
| 3431 | is_triable = -1; |
| 3432 | } |
| 3433 | tmp++; |
| 3434 | } |
| 3435 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3436 | i++; |
| 3437 | cur = cur->next; |
| 3438 | } |
| 3439 | |
| 3440 | for (i = 0;i < nbchild;i++) { |
| 3441 | if (list[i] == NULL) |
| 3442 | continue; |
| 3443 | for (j = 0;j < i;j++) { |
| 3444 | if (list[j] == NULL) |
| 3445 | continue; |
| 3446 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 3447 | if (ret == 0) { |
| 3448 | is_indeterminist = 1; |
| 3449 | } |
| 3450 | } |
| 3451 | } |
| 3452 | for (i = 0;i < nbchild;i++) { |
| 3453 | if (list[i] != NULL) |
| 3454 | xmlFree(list[i]); |
| 3455 | } |
| 3456 | |
| 3457 | xmlFree(list); |
| 3458 | if (is_indeterminist) { |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3459 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3460 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3461 | if (is_triable == 1) { |
| 3462 | def->dflags |= IS_TRIABLE; |
| 3463 | def->data = triage; |
| 3464 | } else if (triage != NULL) { |
| 3465 | xmlHashFree(triage, NULL); |
| 3466 | } |
| 3467 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3471 | * xmlRelaxNGCheckGroupAttrs: |
| 3472 | * @ctxt: a Relax-NG parser context |
| 3473 | * @def: the group definition |
| 3474 | * |
| 3475 | * Detects violations of rule 7.3 |
| 3476 | */ |
| 3477 | static void |
| 3478 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
| 3479 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3480 | xmlRelaxNGDefinePtr **list; |
| 3481 | xmlRelaxNGDefinePtr cur; |
| 3482 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3483 | |
| 3484 | if ((def == NULL) || |
| 3485 | ((def->type != XML_RELAXNG_GROUP) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3486 | (def->type != XML_RELAXNG_ELEMENT))) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3487 | return; |
| 3488 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3489 | if (def->dflags & IS_PROCESSED) |
| 3490 | return; |
| 3491 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3492 | /* |
| 3493 | * Don't run that check in case of error. Infinite recursion |
| 3494 | * becomes possible. |
| 3495 | */ |
| 3496 | if (ctxt->nbErrors != 0) |
| 3497 | return; |
| 3498 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3499 | cur = def->attrs; |
| 3500 | while (cur != NULL) { |
| 3501 | nbchild++; |
| 3502 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3503 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3504 | cur = def->content; |
| 3505 | while (cur != NULL) { |
| 3506 | nbchild++; |
| 3507 | cur = cur->next; |
| 3508 | } |
| 3509 | |
| 3510 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 3511 | sizeof(xmlRelaxNGDefinePtr *)); |
| 3512 | if (list == NULL) { |
| 3513 | if (ctxt->error != NULL) |
| 3514 | ctxt->error(ctxt->userData, |
| 3515 | "Out of memory in group computation\n"); |
| 3516 | ctxt->nbErrors++; |
| 3517 | return; |
| 3518 | } |
| 3519 | i = 0; |
| 3520 | cur = def->attrs; |
| 3521 | while (cur != NULL) { |
| 3522 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 3523 | i++; |
| 3524 | cur = cur->next; |
| 3525 | } |
| 3526 | cur = def->content; |
| 3527 | while (cur != NULL) { |
| 3528 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 3529 | i++; |
| 3530 | cur = cur->next; |
| 3531 | } |
| 3532 | |
| 3533 | for (i = 0;i < nbchild;i++) { |
| 3534 | if (list[i] == NULL) |
| 3535 | continue; |
| 3536 | for (j = 0;j < i;j++) { |
| 3537 | if (list[j] == NULL) |
| 3538 | continue; |
| 3539 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 3540 | if (ret == 0) { |
| 3541 | if (ctxt->error != NULL) |
| 3542 | ctxt->error(ctxt->userData, |
| 3543 | "Attributes conflicts in group\n"); |
| 3544 | ctxt->nbErrors++; |
| 3545 | } |
| 3546 | } |
| 3547 | } |
| 3548 | for (i = 0;i < nbchild;i++) { |
| 3549 | if (list[i] != NULL) |
| 3550 | xmlFree(list[i]); |
| 3551 | } |
| 3552 | |
| 3553 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3554 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3558 | * xmlRelaxNGComputeInterleaves: |
| 3559 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3560 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3561 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3562 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3563 | * A lot of work for preprocessing interleave definitions |
| 3564 | * is potentially needed to get a decent execution speed at runtime |
| 3565 | * - trying to get a total order on the element nodes generated |
| 3566 | * by the interleaves, order the list of interleave definitions |
| 3567 | * following that order. |
| 3568 | * - if <text/> is used to handle mixed content, it is better to |
| 3569 | * flag this in the define and simplify the runtime checking |
| 3570 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3571 | */ |
| 3572 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3573 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
| 3574 | xmlRelaxNGParserCtxtPtr ctxt, |
| 3575 | xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3576 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3577 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3578 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 3579 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 3580 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3581 | int i,j,ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3582 | int nbgroups = 0; |
| 3583 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3584 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3585 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3586 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3587 | /* |
| 3588 | * Don't run that check in case of error. Infinite recursion |
| 3589 | * becomes possible. |
| 3590 | */ |
| 3591 | if (ctxt->nbErrors != 0) |
| 3592 | return; |
| 3593 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3594 | #ifdef DEBUG_INTERLEAVE |
| 3595 | xmlGenericError(xmlGenericErrorContext, |
| 3596 | "xmlRelaxNGComputeInterleaves(%s)\n", |
| 3597 | name); |
| 3598 | #endif |
| 3599 | cur = def->content; |
| 3600 | while (cur != NULL) { |
| 3601 | nbchild++; |
| 3602 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3603 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3604 | |
| 3605 | #ifdef DEBUG_INTERLEAVE |
| 3606 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 3607 | #endif |
| 3608 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
| 3609 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
| 3610 | if (groups == NULL) |
| 3611 | goto error; |
| 3612 | cur = def->content; |
| 3613 | while (cur != NULL) { |
| 3614 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 3615 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 3616 | if (groups[nbgroups] == NULL) |
| 3617 | goto error; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3618 | if (cur->type == XML_RELAXNG_TEXT) |
| 3619 | is_mixed++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3620 | groups[nbgroups]->rule = cur; |
| 3621 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 3622 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 3623 | nbgroups++; |
| 3624 | cur = cur->next; |
| 3625 | } |
| 3626 | #ifdef DEBUG_INTERLEAVE |
| 3627 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 3628 | #endif |
| 3629 | |
| 3630 | /* |
| 3631 | * Let's check that all rules makes a partitions according to 7.4 |
| 3632 | */ |
| 3633 | partitions = (xmlRelaxNGPartitionPtr) |
| 3634 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
| 3635 | if (partitions == NULL) |
| 3636 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 3637 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3638 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3639 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3640 | for (i = 0;i < nbgroups;i++) { |
| 3641 | group = groups[i]; |
| 3642 | for (j = i+1;j < nbgroups;j++) { |
| 3643 | if (groups[j] == NULL) |
| 3644 | continue; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3645 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3646 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 3647 | groups[j]->defs); |
| 3648 | if (ret == 0) { |
| 3649 | if (ctxt->error != NULL) |
| 3650 | ctxt->error(ctxt->userData, |
| 3651 | "Element or text conflicts in interleave\n"); |
| 3652 | ctxt->nbErrors++; |
| 3653 | } |
| 3654 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 3655 | groups[j]->attrs); |
| 3656 | if (ret == 0) { |
| 3657 | if (ctxt->error != NULL) |
| 3658 | ctxt->error(ctxt->userData, |
| 3659 | "Attributes conflicts in interleave\n"); |
| 3660 | ctxt->nbErrors++; |
| 3661 | } |
| 3662 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3663 | tmp = group->defs; |
| 3664 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 3665 | while (*tmp != NULL) { |
| 3666 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 3667 | res = xmlHashAddEntry2(partitions->triage, |
| 3668 | BAD_CAST "#text", NULL, |
| 3669 | (void *)(i + 1)); |
| 3670 | if (res != 0) |
| 3671 | is_determinist = -1; |
| 3672 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 3673 | ((*tmp)->name != NULL)) { |
| 3674 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3675 | res = xmlHashAddEntry2(partitions->triage, |
| 3676 | (*tmp)->name, NULL, |
| 3677 | (void *)(i + 1)); |
| 3678 | else |
| 3679 | res = xmlHashAddEntry2(partitions->triage, |
| 3680 | (*tmp)->name, (*tmp)->ns, |
| 3681 | (void *)(i + 1)); |
| 3682 | if (res != 0) |
| 3683 | is_determinist = -1; |
| 3684 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 3685 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3686 | res = xmlHashAddEntry2(partitions->triage, |
| 3687 | BAD_CAST "#any", NULL, |
| 3688 | (void *)(i + 1)); |
| 3689 | else |
| 3690 | res = xmlHashAddEntry2(partitions->triage, |
| 3691 | BAD_CAST "#any", (*tmp)->ns, |
| 3692 | (void *)(i + 1)); |
| 3693 | if ((*tmp)->nameClass != NULL) |
| 3694 | is_determinist = 2; |
| 3695 | if (res != 0) |
| 3696 | is_determinist = -1; |
| 3697 | } else { |
| 3698 | is_determinist = -1; |
| 3699 | } |
| 3700 | tmp++; |
| 3701 | } |
| 3702 | } else { |
| 3703 | is_determinist = 0; |
| 3704 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3705 | } |
| 3706 | partitions->groups = groups; |
| 3707 | |
| 3708 | /* |
| 3709 | * and save the partition list back in the def |
| 3710 | */ |
| 3711 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3712 | if (is_mixed != 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3713 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 3714 | if (is_determinist == 1) |
| 3715 | partitions->flags = IS_DETERMINIST; |
| 3716 | if (is_determinist == 2) |
| 3717 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3718 | return; |
| 3719 | |
| 3720 | error: |
| 3721 | if (ctxt->error != NULL) |
| 3722 | ctxt->error(ctxt->userData, |
| 3723 | "Out of memory in interleave computation\n"); |
| 3724 | ctxt->nbErrors++; |
| 3725 | if (groups != NULL) { |
| 3726 | for (i = 0;i < nbgroups;i++) |
| 3727 | if (groups[i] != NULL) { |
| 3728 | if (groups[i]->defs != NULL) |
| 3729 | xmlFree(groups[i]->defs); |
| 3730 | xmlFree(groups[i]); |
| 3731 | } |
| 3732 | xmlFree(groups); |
| 3733 | } |
| 3734 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
| 3737 | /** |
| 3738 | * xmlRelaxNGParseInterleave: |
| 3739 | * @ctxt: a Relax-NG parser context |
| 3740 | * @node: the data node. |
| 3741 | * |
| 3742 | * parse the content of a RelaxNG interleave node. |
| 3743 | * |
| 3744 | * Returns the definition pointer or NULL in case of error |
| 3745 | */ |
| 3746 | static xmlRelaxNGDefinePtr |
| 3747 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 3748 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3749 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3750 | xmlNodePtr child; |
| 3751 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3752 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 3753 | if (def == NULL) { |
| 3754 | return(NULL); |
| 3755 | } |
| 3756 | def->type = XML_RELAXNG_INTERLEAVE; |
| 3757 | |
| 3758 | if (ctxt->interleaves == NULL) |
| 3759 | ctxt->interleaves = xmlHashCreate(10); |
| 3760 | if (ctxt->interleaves == NULL) { |
| 3761 | if (ctxt->error != NULL) |
| 3762 | ctxt->error(ctxt->userData, |
| 3763 | "Failed to create interleaves hash table\n"); |
| 3764 | ctxt->nbErrors++; |
| 3765 | } else { |
| 3766 | char name[32]; |
| 3767 | |
| 3768 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 3769 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 3770 | if (ctxt->error != NULL) |
| 3771 | ctxt->error(ctxt->userData, |
| 3772 | "Failed to add %s to hash table\n", name); |
| 3773 | ctxt->nbErrors++; |
| 3774 | } |
| 3775 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3776 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3777 | if (child == NULL) { |
| 3778 | if (ctxt->error != NULL) |
| 3779 | ctxt->error(ctxt->userData, "Element interleave is empty\n"); |
| 3780 | ctxt->nbErrors++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 3781 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3782 | while (child != NULL) { |
| 3783 | if (IS_RELAXNG(child, "element")) { |
| 3784 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 3785 | } else { |
| 3786 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 3787 | } |
| 3788 | if (cur != NULL) { |
| 3789 | cur->parent = def; |
| 3790 | if (last == NULL) { |
| 3791 | def->content = last = cur; |
| 3792 | } else { |
| 3793 | last->next = cur; |
| 3794 | last = cur; |
| 3795 | } |
| 3796 | } |
| 3797 | child = child->next; |
| 3798 | } |
| 3799 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3800 | return(def); |
| 3801 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3802 | |
| 3803 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 3804 | * xmlRelaxNGParseInclude: |
| 3805 | * @ctxt: a Relax-NG parser context |
| 3806 | * @node: the include node |
| 3807 | * |
| 3808 | * Integrate the content of an include node in the current grammar |
| 3809 | * |
| 3810 | * Returns 0 in case of success or -1 in case of error |
| 3811 | */ |
| 3812 | static int |
| 3813 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 3814 | xmlRelaxNGIncludePtr incl; |
| 3815 | xmlNodePtr root; |
| 3816 | int ret = 0, tmp; |
| 3817 | |
| 3818 | incl = node->_private; |
| 3819 | if (incl == NULL) { |
| 3820 | if (ctxt->error != NULL) |
| 3821 | ctxt->error(ctxt->userData, |
| 3822 | "Include node has no data\n"); |
| 3823 | ctxt->nbErrors++; |
| 3824 | return(-1); |
| 3825 | } |
| 3826 | root = xmlDocGetRootElement(incl->doc); |
| 3827 | if (root == NULL) { |
| 3828 | if (ctxt->error != NULL) |
| 3829 | ctxt->error(ctxt->userData, |
| 3830 | "Include document is empty\n"); |
| 3831 | ctxt->nbErrors++; |
| 3832 | return(-1); |
| 3833 | } |
| 3834 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
| 3835 | if (ctxt->error != NULL) |
| 3836 | ctxt->error(ctxt->userData, |
| 3837 | "Include document root is not a grammar\n"); |
| 3838 | ctxt->nbErrors++; |
| 3839 | return(-1); |
| 3840 | } |
| 3841 | |
| 3842 | /* |
| 3843 | * Merge the definition from both the include and the internal list |
| 3844 | */ |
| 3845 | if (root->children != NULL) { |
| 3846 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 3847 | if (tmp != 0) |
| 3848 | ret = -1; |
| 3849 | } |
| 3850 | if (node->children != NULL) { |
| 3851 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 3852 | if (tmp != 0) |
| 3853 | ret = -1; |
| 3854 | } |
| 3855 | return(ret); |
| 3856 | } |
| 3857 | |
| 3858 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 3859 | * xmlRelaxNGParseDefine: |
| 3860 | * @ctxt: a Relax-NG parser context |
| 3861 | * @node: the define node |
| 3862 | * |
| 3863 | * parse the content of a RelaxNG define element node. |
| 3864 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 3865 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 3866 | */ |
| 3867 | static int |
| 3868 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 3869 | xmlChar *name; |
| 3870 | int ret = 0, tmp; |
| 3871 | xmlRelaxNGDefinePtr def; |
| 3872 | const xmlChar *olddefine; |
| 3873 | |
| 3874 | name = xmlGetProp(node, BAD_CAST "name"); |
| 3875 | if (name == NULL) { |
| 3876 | if (ctxt->error != NULL) |
| 3877 | ctxt->error(ctxt->userData, |
| 3878 | "define has no name\n"); |
| 3879 | ctxt->nbErrors++; |
| 3880 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3881 | xmlRelaxNGNormExtSpace(name); |
| 3882 | if (xmlValidateNCName(name, 0)) { |
| 3883 | if (ctxt->error != NULL) |
| 3884 | ctxt->error(ctxt->userData, |
| 3885 | "define name '%s' is not an NCName\n", |
| 3886 | name); |
| 3887 | ctxt->nbErrors++; |
| 3888 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3889 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 3890 | if (def == NULL) { |
| 3891 | xmlFree(name); |
| 3892 | return(-1); |
| 3893 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3894 | def->type = XML_RELAXNG_DEF; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 3895 | def->name = name; |
| 3896 | if (node->children == NULL) { |
| 3897 | if (ctxt->error != NULL) |
| 3898 | ctxt->error(ctxt->userData, |
| 3899 | "define has no children\n"); |
| 3900 | ctxt->nbErrors++; |
| 3901 | } else { |
| 3902 | olddefine = ctxt->define; |
| 3903 | ctxt->define = name; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3904 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 3905 | ctxt->define = olddefine; |
| 3906 | } |
| 3907 | if (ctxt->grammar->defs == NULL) |
| 3908 | ctxt->grammar->defs = xmlHashCreate(10); |
| 3909 | if (ctxt->grammar->defs == NULL) { |
| 3910 | if (ctxt->error != NULL) |
| 3911 | ctxt->error(ctxt->userData, |
| 3912 | "Could not create definition hash\n"); |
| 3913 | ctxt->nbErrors++; |
| 3914 | ret = -1; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 3915 | } else { |
| 3916 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 3917 | if (tmp < 0) { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3918 | xmlRelaxNGDefinePtr prev; |
| 3919 | |
| 3920 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 3921 | if (prev == NULL) { |
| 3922 | if (ctxt->error != NULL) |
| 3923 | ctxt->error(ctxt->userData, |
| 3924 | "Internal error on define aggregation of %s\n", |
| 3925 | name); |
| 3926 | ctxt->nbErrors++; |
| 3927 | ret = -1; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3928 | } else { |
| 3929 | while (prev->nextHash != NULL) |
| 3930 | prev = prev->nextHash; |
| 3931 | prev->nextHash = def; |
| 3932 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 3933 | } |
| 3934 | } |
| 3935 | } |
| 3936 | return(ret); |
| 3937 | } |
| 3938 | |
| 3939 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 3940 | * xmlRelaxNGProcessExternalRef: |
| 3941 | * @ctxt: the parser context |
| 3942 | * @node: the externlRef node |
| 3943 | * |
| 3944 | * Process and compile an externlRef node |
| 3945 | * |
| 3946 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 3947 | */ |
| 3948 | static xmlRelaxNGDefinePtr |
| 3949 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 3950 | xmlRelaxNGDocumentPtr docu; |
| 3951 | xmlNodePtr root, tmp; |
| 3952 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 3953 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 3954 | xmlRelaxNGDefinePtr def; |
| 3955 | |
| 3956 | docu = node->_private; |
| 3957 | if (docu != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3958 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 3959 | if (def == NULL) |
| 3960 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3961 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 3962 | |
| 3963 | if (docu->content == NULL) { |
| 3964 | /* |
| 3965 | * Then do the parsing for good |
| 3966 | */ |
| 3967 | root = xmlDocGetRootElement(docu->doc); |
| 3968 | if (root == NULL) { |
| 3969 | if (ctxt->error != NULL) |
| 3970 | ctxt->error(ctxt->userData, |
| 3971 | "xmlRelaxNGParse: %s is empty\n", |
| 3972 | ctxt->URL); |
| 3973 | ctxt->nbErrors++; |
| 3974 | return (NULL); |
| 3975 | } |
| 3976 | /* |
| 3977 | * ns transmission rules |
| 3978 | */ |
| 3979 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 3980 | if (ns == NULL) { |
| 3981 | tmp = node; |
| 3982 | while ((tmp != NULL) && |
| 3983 | (tmp->type == XML_ELEMENT_NODE)) { |
| 3984 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 3985 | if (ns != NULL) { |
| 3986 | break; |
| 3987 | } |
| 3988 | tmp = tmp->parent; |
| 3989 | } |
| 3990 | if (ns != NULL) { |
| 3991 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 3992 | newNs = 1; |
| 3993 | xmlFree(ns); |
| 3994 | } |
| 3995 | } else { |
| 3996 | xmlFree(ns); |
| 3997 | } |
| 3998 | |
| 3999 | /* |
| 4000 | * Parsing to get a precompiled schemas. |
| 4001 | */ |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4002 | oldflags = ctxt->flags; |
| 4003 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4004 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4005 | ctxt->flags = oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4006 | if ((docu->schema != NULL) && |
| 4007 | (docu->schema->topgrammar != NULL)) { |
| 4008 | docu->content = docu->schema->topgrammar->start; |
| 4009 | } |
| 4010 | |
| 4011 | /* |
| 4012 | * the externalRef may be reused in a different ns context |
| 4013 | */ |
| 4014 | if (newNs == 1) { |
| 4015 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4016 | } |
| 4017 | } |
| 4018 | def->content = docu->content; |
| 4019 | } else { |
| 4020 | def = NULL; |
| 4021 | } |
| 4022 | return(def); |
| 4023 | } |
| 4024 | |
| 4025 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4026 | * xmlRelaxNGParsePattern: |
| 4027 | * @ctxt: a Relax-NG parser context |
| 4028 | * @node: the pattern node. |
| 4029 | * |
| 4030 | * parse the content of a RelaxNG pattern node. |
| 4031 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4032 | * Returns the definition pointer or NULL in case of error or if no |
| 4033 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4034 | */ |
| 4035 | static xmlRelaxNGDefinePtr |
| 4036 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4037 | xmlRelaxNGDefinePtr def = NULL; |
| 4038 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4039 | if (node == NULL) { |
| 4040 | return(NULL); |
| 4041 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4042 | if (IS_RELAXNG(node, "element")) { |
| 4043 | def = xmlRelaxNGParseElement(ctxt, node); |
| 4044 | } else if (IS_RELAXNG(node, "attribute")) { |
| 4045 | def = xmlRelaxNGParseAttribute(ctxt, node); |
| 4046 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4047 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4048 | if (def == NULL) |
| 4049 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4050 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4051 | if (node->children != NULL) { |
| 4052 | if (ctxt->error != NULL) |
| 4053 | ctxt->error(ctxt->userData, "empty: had a child node\n"); |
| 4054 | ctxt->nbErrors++; |
| 4055 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4056 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4057 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4058 | if (def == NULL) |
| 4059 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4060 | def->type = XML_RELAXNG_TEXT; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4061 | if (node->children != NULL) { |
| 4062 | if (ctxt->error != NULL) |
| 4063 | ctxt->error(ctxt->userData, "text: had a child node\n"); |
| 4064 | ctxt->nbErrors++; |
| 4065 | } |
| 4066 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4067 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4068 | if (def == NULL) |
| 4069 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4070 | def->type = XML_RELAXNG_ZEROORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4071 | if (node->children == NULL) { |
| 4072 | if (ctxt->error != NULL) |
| 4073 | ctxt->error(ctxt->userData, |
| 4074 | "Element %s is empty\n", node->name); |
| 4075 | ctxt->nbErrors++; |
| 4076 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4077 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4078 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4079 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4080 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4081 | if (def == NULL) |
| 4082 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4083 | def->type = XML_RELAXNG_ONEORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4084 | if (node->children == NULL) { |
| 4085 | if (ctxt->error != NULL) |
| 4086 | ctxt->error(ctxt->userData, |
| 4087 | "Element %s is empty\n", node->name); |
| 4088 | ctxt->nbErrors++; |
| 4089 | } else { |
| 4090 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4091 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4092 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4093 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4094 | if (def == NULL) |
| 4095 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4096 | def->type = XML_RELAXNG_OPTIONAL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4097 | if (node->children == NULL) { |
| 4098 | if (ctxt->error != NULL) |
| 4099 | ctxt->error(ctxt->userData, |
| 4100 | "Element %s is empty\n", node->name); |
| 4101 | ctxt->nbErrors++; |
| 4102 | } else { |
| 4103 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4104 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4105 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4106 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4107 | if (def == NULL) |
| 4108 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4109 | def->type = XML_RELAXNG_CHOICE; |
| 4110 | if (node->children == NULL) { |
| 4111 | if (ctxt->error != NULL) |
| 4112 | ctxt->error(ctxt->userData, |
| 4113 | "Element %s is empty\n", node->name); |
| 4114 | ctxt->nbErrors++; |
| 4115 | } else { |
| 4116 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4117 | } |
| 4118 | } else if (IS_RELAXNG(node, "group")) { |
| 4119 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4120 | if (def == NULL) |
| 4121 | return(NULL); |
| 4122 | def->type = XML_RELAXNG_GROUP; |
| 4123 | if (node->children == NULL) { |
| 4124 | if (ctxt->error != NULL) |
| 4125 | ctxt->error(ctxt->userData, |
| 4126 | "Element %s is empty\n", node->name); |
| 4127 | ctxt->nbErrors++; |
| 4128 | } else { |
| 4129 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4130 | } |
| 4131 | } else if (IS_RELAXNG(node, "ref")) { |
| 4132 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4133 | if (def == NULL) |
| 4134 | return(NULL); |
| 4135 | def->type = XML_RELAXNG_REF; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4136 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4137 | if (def->name == NULL) { |
| 4138 | if (ctxt->error != NULL) |
| 4139 | ctxt->error(ctxt->userData, |
| 4140 | "ref has no name\n"); |
| 4141 | ctxt->nbErrors++; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4142 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4143 | xmlRelaxNGNormExtSpace(def->name); |
| 4144 | if (xmlValidateNCName(def->name, 0)) { |
| 4145 | if (ctxt->error != NULL) |
| 4146 | ctxt->error(ctxt->userData, |
| 4147 | "ref name '%s' is not an NCName\n", |
| 4148 | def->name); |
| 4149 | ctxt->nbErrors++; |
| 4150 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4151 | } |
| 4152 | if (node->children != NULL) { |
| 4153 | if (ctxt->error != NULL) |
| 4154 | ctxt->error(ctxt->userData, |
| 4155 | "ref is not empty\n"); |
| 4156 | ctxt->nbErrors++; |
| 4157 | } |
| 4158 | if (ctxt->grammar->refs == NULL) |
| 4159 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4160 | if (ctxt->grammar->refs == NULL) { |
| 4161 | if (ctxt->error != NULL) |
| 4162 | ctxt->error(ctxt->userData, |
| 4163 | "Could not create references hash\n"); |
| 4164 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4165 | def = NULL; |
| 4166 | } else { |
| 4167 | int tmp; |
| 4168 | |
| 4169 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4170 | if (tmp < 0) { |
| 4171 | xmlRelaxNGDefinePtr prev; |
| 4172 | |
| 4173 | prev = (xmlRelaxNGDefinePtr) |
| 4174 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4175 | if (prev == NULL) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4176 | if (def->name != NULL) { |
| 4177 | if (ctxt->error != NULL) |
| 4178 | ctxt->error(ctxt->userData, |
| 4179 | "Error refs definitions '%s'\n", |
| 4180 | def->name); |
| 4181 | } else { |
| 4182 | if (ctxt->error != NULL) |
| 4183 | ctxt->error(ctxt->userData, |
| 4184 | "Error refs definitions\n"); |
| 4185 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4186 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4187 | def = NULL; |
| 4188 | } else { |
| 4189 | def->nextHash = prev->nextHash; |
| 4190 | prev->nextHash = def; |
| 4191 | } |
| 4192 | } |
| 4193 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4194 | } else if (IS_RELAXNG(node, "data")) { |
| 4195 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4196 | } else if (IS_RELAXNG(node, "value")) { |
| 4197 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4198 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4199 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4200 | if (def == NULL) |
| 4201 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4202 | def->type = XML_RELAXNG_LIST; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4203 | if (node->children == NULL) { |
| 4204 | if (ctxt->error != NULL) |
| 4205 | ctxt->error(ctxt->userData, |
| 4206 | "Element %s is empty\n", node->name); |
| 4207 | ctxt->nbErrors++; |
| 4208 | } else { |
| 4209 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4210 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4211 | } else if (IS_RELAXNG(node, "interleave")) { |
| 4212 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4213 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4214 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4215 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4216 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4217 | if (def == NULL) |
| 4218 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4219 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4220 | if (node->children != NULL) { |
| 4221 | if (ctxt->error != NULL) |
| 4222 | ctxt->error(ctxt->userData, |
| 4223 | "xmlRelaxNGParse: notAllowed element is not empty\n"); |
| 4224 | ctxt->nbErrors++; |
| 4225 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4226 | } else if (IS_RELAXNG(node, "grammar")) { |
| 4227 | xmlRelaxNGGrammarPtr grammar, old; |
| 4228 | xmlRelaxNGGrammarPtr oldparent; |
| 4229 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4230 | #ifdef DEBUG_GRAMMAR |
| 4231 | xmlGenericError(xmlGenericErrorContext, "Found <grammar> pattern\n"); |
| 4232 | #endif |
| 4233 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4234 | oldparent = ctxt->parentgrammar; |
| 4235 | old = ctxt->grammar; |
| 4236 | ctxt->parentgrammar = old; |
| 4237 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4238 | if (old != NULL) { |
| 4239 | ctxt->grammar = old; |
| 4240 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4241 | #if 0 |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4242 | if (grammar != NULL) { |
| 4243 | grammar->next = old->next; |
| 4244 | old->next = grammar; |
| 4245 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4246 | #endif |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4247 | } |
| 4248 | if (grammar != NULL) |
| 4249 | def = grammar->start; |
| 4250 | else |
| 4251 | def = NULL; |
| 4252 | } else if (IS_RELAXNG(node, "parentRef")) { |
| 4253 | if (ctxt->parentgrammar == NULL) { |
| 4254 | if (ctxt->error != NULL) |
| 4255 | ctxt->error(ctxt->userData, |
| 4256 | "Use of parentRef without a parent grammar\n"); |
| 4257 | ctxt->nbErrors++; |
| 4258 | return(NULL); |
| 4259 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4260 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4261 | if (def == NULL) |
| 4262 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4263 | def->type = XML_RELAXNG_PARENTREF; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4264 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4265 | if (def->name == NULL) { |
| 4266 | if (ctxt->error != NULL) |
| 4267 | ctxt->error(ctxt->userData, |
| 4268 | "parentRef has no name\n"); |
| 4269 | ctxt->nbErrors++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4270 | } else { |
| 4271 | xmlRelaxNGNormExtSpace(def->name); |
| 4272 | if (xmlValidateNCName(def->name, 0)) { |
| 4273 | if (ctxt->error != NULL) |
| 4274 | ctxt->error(ctxt->userData, |
| 4275 | "parentRef name '%s' is not an NCName\n", |
| 4276 | def->name); |
| 4277 | ctxt->nbErrors++; |
| 4278 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4279 | } |
| 4280 | if (node->children != NULL) { |
| 4281 | if (ctxt->error != NULL) |
| 4282 | ctxt->error(ctxt->userData, |
| 4283 | "parentRef is not empty\n"); |
| 4284 | ctxt->nbErrors++; |
| 4285 | } |
| 4286 | if (ctxt->parentgrammar->refs == NULL) |
| 4287 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4288 | if (ctxt->parentgrammar->refs == NULL) { |
| 4289 | if (ctxt->error != NULL) |
| 4290 | ctxt->error(ctxt->userData, |
| 4291 | "Could not create references hash\n"); |
| 4292 | ctxt->nbErrors++; |
| 4293 | def = NULL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4294 | } else if (def->name != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4295 | int tmp; |
| 4296 | |
| 4297 | tmp = xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4298 | if (tmp < 0) { |
| 4299 | xmlRelaxNGDefinePtr prev; |
| 4300 | |
| 4301 | prev = (xmlRelaxNGDefinePtr) |
| 4302 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4303 | if (prev == NULL) { |
| 4304 | if (ctxt->error != NULL) |
| 4305 | ctxt->error(ctxt->userData, |
| 4306 | "Internal error parentRef definitions '%s'\n", |
| 4307 | def->name); |
| 4308 | ctxt->nbErrors++; |
| 4309 | def = NULL; |
| 4310 | } else { |
| 4311 | def->nextHash = prev->nextHash; |
| 4312 | prev->nextHash = def; |
| 4313 | } |
| 4314 | } |
| 4315 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4316 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4317 | if (node->children == NULL) { |
| 4318 | if (ctxt->error != NULL) |
| 4319 | ctxt->error(ctxt->userData, |
| 4320 | "Mixed is empty\n"); |
| 4321 | ctxt->nbErrors++; |
| 4322 | def = NULL; |
| 4323 | } else { |
| 4324 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4325 | if (def != NULL) { |
| 4326 | xmlRelaxNGDefinePtr tmp; |
| 4327 | |
| 4328 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4329 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4330 | if (tmp != NULL) { |
| 4331 | tmp->type = XML_RELAXNG_GROUP; |
| 4332 | tmp->content = def->content; |
| 4333 | def->content = tmp; |
| 4334 | } |
| 4335 | } |
| 4336 | |
| 4337 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4338 | if (tmp == NULL) |
| 4339 | return(def); |
| 4340 | tmp->type = XML_RELAXNG_TEXT; |
| 4341 | tmp->next = def->content; |
| 4342 | def->content = tmp; |
| 4343 | } |
| 4344 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4345 | } else { |
| 4346 | if (ctxt->error != NULL) |
| 4347 | ctxt->error(ctxt->userData, |
| 4348 | "Unexpected node %s is not a pattern\n", |
| 4349 | node->name); |
| 4350 | ctxt->nbErrors++; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4351 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4352 | } |
| 4353 | return(def); |
| 4354 | } |
| 4355 | |
| 4356 | /** |
| 4357 | * xmlRelaxNGParseAttribute: |
| 4358 | * @ctxt: a Relax-NG parser context |
| 4359 | * @node: the element node |
| 4360 | * |
| 4361 | * parse the content of a RelaxNG attribute node. |
| 4362 | * |
| 4363 | * Returns the definition pointer or NULL in case of error. |
| 4364 | */ |
| 4365 | static xmlRelaxNGDefinePtr |
| 4366 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4367 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4368 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4369 | int old_flags; |
| 4370 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4371 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4372 | if (ret == NULL) |
| 4373 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4374 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4375 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4376 | child = node->children; |
| 4377 | if (child == NULL) { |
| 4378 | if (ctxt->error != NULL) |
| 4379 | ctxt->error(ctxt->userData, |
| 4380 | "xmlRelaxNGParseattribute: attribute has no children\n"); |
| 4381 | ctxt->nbErrors++; |
| 4382 | return(ret); |
| 4383 | } |
| 4384 | old_flags = ctxt->flags; |
| 4385 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4386 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4387 | if (cur != NULL) |
| 4388 | child = child->next; |
| 4389 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4390 | if (child != NULL) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4391 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4392 | if (cur != NULL) { |
| 4393 | switch (cur->type) { |
| 4394 | case XML_RELAXNG_EMPTY: |
| 4395 | case XML_RELAXNG_NOT_ALLOWED: |
| 4396 | case XML_RELAXNG_TEXT: |
| 4397 | case XML_RELAXNG_ELEMENT: |
| 4398 | case XML_RELAXNG_DATATYPE: |
| 4399 | case XML_RELAXNG_VALUE: |
| 4400 | case XML_RELAXNG_LIST: |
| 4401 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4402 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4403 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4404 | case XML_RELAXNG_DEF: |
| 4405 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4406 | case XML_RELAXNG_ZEROORMORE: |
| 4407 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4408 | case XML_RELAXNG_CHOICE: |
| 4409 | case XML_RELAXNG_GROUP: |
| 4410 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4411 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4412 | ret->content = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4413 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4414 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4415 | case XML_RELAXNG_START: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 4416 | case XML_RELAXNG_PARAM: |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4417 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4418 | if (ctxt->error != NULL) |
| 4419 | ctxt->error(ctxt->userData, |
| 4420 | "attribute has invalid content\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 4421 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4422 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4423 | case XML_RELAXNG_NOOP: |
| 4424 | TODO |
| 4425 | if (ctxt->error != NULL) |
| 4426 | ctxt->error(ctxt->userData, |
| 4427 | "Internal error, noop found\n"); |
| 4428 | ctxt->nbErrors++; |
| 4429 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4430 | } |
| 4431 | } |
| 4432 | child = child->next; |
| 4433 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4434 | if (child != NULL) { |
| 4435 | if (ctxt->error != NULL) |
| 4436 | ctxt->error(ctxt->userData, "attribute has multiple children\n"); |
| 4437 | ctxt->nbErrors++; |
| 4438 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4439 | ctxt->flags = old_flags; |
| 4440 | return(ret); |
| 4441 | } |
| 4442 | |
| 4443 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4444 | * xmlRelaxNGParseExceptNameClass: |
| 4445 | * @ctxt: a Relax-NG parser context |
| 4446 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4447 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4448 | * |
| 4449 | * parse the content of a RelaxNG nameClass node. |
| 4450 | * |
| 4451 | * Returns the definition pointer or NULL in case of error. |
| 4452 | */ |
| 4453 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4454 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
| 4455 | xmlNodePtr node, int attr) { |
| 4456 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 4457 | xmlNodePtr child; |
| 4458 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4459 | if (!IS_RELAXNG(node, "except")) { |
| 4460 | if (ctxt->error != NULL) |
| 4461 | ctxt->error(ctxt->userData, |
| 4462 | "Expecting an except node\n"); |
| 4463 | ctxt->nbErrors++; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4464 | return(NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4465 | } |
| 4466 | if (node->next != NULL) { |
| 4467 | if (ctxt->error != NULL) |
| 4468 | ctxt->error(ctxt->userData, |
| 4469 | "exceptNameClass allows only a single except node\n"); |
| 4470 | ctxt->nbErrors++; |
| 4471 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4472 | if (node->children == NULL) { |
| 4473 | if (ctxt->error != NULL) |
| 4474 | ctxt->error(ctxt->userData, |
| 4475 | "except has no content\n"); |
| 4476 | ctxt->nbErrors++; |
| 4477 | return(NULL); |
| 4478 | } |
| 4479 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4480 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4481 | if (ret == NULL) |
| 4482 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4483 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4484 | child = node->children; |
| 4485 | while (child != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4486 | cur = xmlRelaxNGNewDefine(ctxt, child); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4487 | if (cur == NULL) |
| 4488 | break; |
| 4489 | if (attr) |
| 4490 | cur->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4491 | else |
| 4492 | cur->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4493 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4494 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4495 | if (last == NULL) { |
| 4496 | ret->content = cur; |
| 4497 | } else { |
| 4498 | last->next = cur; |
| 4499 | } |
| 4500 | last = cur; |
| 4501 | } |
| 4502 | child = child->next; |
| 4503 | } |
| 4504 | |
| 4505 | return(ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4506 | } |
| 4507 | |
| 4508 | /** |
| 4509 | * xmlRelaxNGParseNameClass: |
| 4510 | * @ctxt: a Relax-NG parser context |
| 4511 | * @node: the nameClass node |
| 4512 | * @def: the current definition |
| 4513 | * |
| 4514 | * parse the content of a RelaxNG nameClass node. |
| 4515 | * |
| 4516 | * Returns the definition pointer or NULL in case of error. |
| 4517 | */ |
| 4518 | static xmlRelaxNGDefinePtr |
| 4519 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 4520 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4521 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4522 | xmlChar *val; |
| 4523 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4524 | ret = def; |
| 4525 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
| 4526 | (IS_RELAXNG(node, "nsName"))) { |
| 4527 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 4528 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4529 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4530 | if (ret == NULL) |
| 4531 | return(NULL); |
| 4532 | ret->parent = def; |
| 4533 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 4534 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4535 | else |
| 4536 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4537 | } |
| 4538 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4539 | if (IS_RELAXNG(node, "name")) { |
| 4540 | val = xmlNodeGetContent(node); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4541 | xmlRelaxNGNormExtSpace(val); |
| 4542 | if (xmlValidateNCName(val, 0)) { |
| 4543 | if (ctxt->error != NULL) { |
| 4544 | if (node->parent != NULL) |
| 4545 | ctxt->error(ctxt->userData, |
| 4546 | "Element %s name '%s' is not an NCName\n", |
| 4547 | node->parent->name, val); |
| 4548 | else |
| 4549 | ctxt->error(ctxt->userData, |
| 4550 | "name '%s' is not an NCName\n", |
| 4551 | val); |
| 4552 | } |
| 4553 | ctxt->nbErrors++; |
| 4554 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4555 | ret->name = val; |
| 4556 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 4557 | ret->ns = val; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 4558 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 4559 | (val != NULL) && |
| 4560 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 4561 | ctxt->error(ctxt->userData, |
| 4562 | "Attribute with namespace '%s' is not allowed\n", |
| 4563 | val); |
| 4564 | ctxt->nbErrors++; |
| 4565 | } |
| 4566 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 4567 | (val != NULL) && |
| 4568 | (val[0] == 0) && |
| 4569 | (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
| 4570 | ctxt->error(ctxt->userData, |
| 4571 | "Attribute with QName 'xmlns' is not allowed\n", |
| 4572 | val); |
| 4573 | ctxt->nbErrors++; |
| 4574 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4575 | } else if (IS_RELAXNG(node, "anyName")) { |
| 4576 | ret->name = NULL; |
| 4577 | ret->ns = NULL; |
| 4578 | if (node->children != NULL) { |
| 4579 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4580 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 4581 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4582 | } |
| 4583 | } else if (IS_RELAXNG(node, "nsName")) { |
| 4584 | ret->name = NULL; |
| 4585 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 4586 | if (ret->ns == NULL) { |
| 4587 | if (ctxt->error != NULL) |
| 4588 | ctxt->error(ctxt->userData, |
| 4589 | "nsName has no ns attribute\n"); |
| 4590 | ctxt->nbErrors++; |
| 4591 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 4592 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 4593 | (ret->ns != NULL) && |
| 4594 | (xmlStrEqual(ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 4595 | ctxt->error(ctxt->userData, |
| 4596 | "Attribute with namespace '%s' is not allowed\n", |
| 4597 | ret->ns); |
| 4598 | ctxt->nbErrors++; |
| 4599 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4600 | if (node->children != NULL) { |
| 4601 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4602 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 4603 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4604 | } |
| 4605 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4606 | xmlNodePtr child; |
| 4607 | xmlRelaxNGDefinePtr last = NULL; |
| 4608 | |
| 4609 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 4610 | if (ret == NULL) |
| 4611 | return(NULL); |
| 4612 | ret->parent = def; |
| 4613 | ret->type = XML_RELAXNG_CHOICE; |
| 4614 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4615 | if (node->children == NULL) { |
| 4616 | if (ctxt->error != NULL) |
| 4617 | ctxt->error(ctxt->userData, |
| 4618 | "Element choice is empty\n"); |
| 4619 | ctxt->nbErrors++; |
| 4620 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4621 | |
| 4622 | child = node->children; |
| 4623 | while (child != NULL) { |
| 4624 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4625 | if (tmp != NULL) { |
| 4626 | if (last == NULL) { |
| 4627 | last = ret->nameClass = tmp; |
| 4628 | } else { |
| 4629 | last->next = tmp; |
| 4630 | last = tmp; |
| 4631 | } |
| 4632 | } |
| 4633 | child = child->next; |
| 4634 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4635 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4636 | } else { |
| 4637 | if (ctxt->error != NULL) |
| 4638 | ctxt->error(ctxt->userData, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4639 | "expecting name, anyName, nsName or choice : got %s\n", |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4640 | node->name); |
| 4641 | ctxt->nbErrors++; |
| 4642 | return(NULL); |
| 4643 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4644 | if (ret != def) { |
| 4645 | if (def->nameClass == NULL) { |
| 4646 | def->nameClass = ret; |
| 4647 | } else { |
| 4648 | tmp = def->nameClass; |
| 4649 | while (tmp->next != NULL) { |
| 4650 | tmp = tmp->next; |
| 4651 | } |
| 4652 | tmp->next = ret; |
| 4653 | } |
| 4654 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4655 | return(ret); |
| 4656 | } |
| 4657 | |
| 4658 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4659 | * xmlRelaxNGParseElement: |
| 4660 | * @ctxt: a Relax-NG parser context |
| 4661 | * @node: the element node |
| 4662 | * |
| 4663 | * parse the content of a RelaxNG element node. |
| 4664 | * |
| 4665 | * Returns the definition pointer or NULL in case of error. |
| 4666 | */ |
| 4667 | static xmlRelaxNGDefinePtr |
| 4668 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4669 | xmlRelaxNGDefinePtr ret, cur, last; |
| 4670 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4671 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4672 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4673 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4674 | if (ret == NULL) |
| 4675 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4676 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4677 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4678 | child = node->children; |
| 4679 | if (child == NULL) { |
| 4680 | if (ctxt->error != NULL) |
| 4681 | ctxt->error(ctxt->userData, |
| 4682 | "xmlRelaxNGParseElement: element has no children\n"); |
| 4683 | ctxt->nbErrors++; |
| 4684 | return(ret); |
| 4685 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4686 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4687 | if (cur != NULL) |
| 4688 | child = child->next; |
| 4689 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4690 | if (child == NULL) { |
| 4691 | if (ctxt->error != NULL) |
| 4692 | ctxt->error(ctxt->userData, |
| 4693 | "xmlRelaxNGParseElement: element has no content\n"); |
| 4694 | ctxt->nbErrors++; |
| 4695 | return(ret); |
| 4696 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4697 | olddefine = ctxt->define; |
| 4698 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4699 | last = NULL; |
| 4700 | while (child != NULL) { |
| 4701 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4702 | if (cur != NULL) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4703 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4704 | switch (cur->type) { |
| 4705 | case XML_RELAXNG_EMPTY: |
| 4706 | case XML_RELAXNG_NOT_ALLOWED: |
| 4707 | case XML_RELAXNG_TEXT: |
| 4708 | case XML_RELAXNG_ELEMENT: |
| 4709 | case XML_RELAXNG_DATATYPE: |
| 4710 | case XML_RELAXNG_VALUE: |
| 4711 | case XML_RELAXNG_LIST: |
| 4712 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4713 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4714 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4715 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4716 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4717 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4718 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4719 | case XML_RELAXNG_CHOICE: |
| 4720 | case XML_RELAXNG_GROUP: |
| 4721 | case XML_RELAXNG_INTERLEAVE: |
| 4722 | if (last == NULL) { |
| 4723 | ret->content = last = cur; |
| 4724 | } else { |
| 4725 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 4726 | (ret->content == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4727 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4728 | if (ret->content != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4729 | ret->content->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4730 | ret->content->content = last; |
| 4731 | } else { |
| 4732 | ret->content = last; |
| 4733 | } |
| 4734 | } |
| 4735 | last->next = cur; |
| 4736 | last = cur; |
| 4737 | } |
| 4738 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4739 | case XML_RELAXNG_ATTRIBUTE: |
| 4740 | cur->next = ret->attrs; |
| 4741 | ret->attrs = cur; |
| 4742 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4743 | case XML_RELAXNG_START: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 4744 | case XML_RELAXNG_PARAM: |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4745 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4746 | TODO |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 4747 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4748 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4749 | case XML_RELAXNG_NOOP: |
| 4750 | TODO |
| 4751 | if (ctxt->error != NULL) |
| 4752 | ctxt->error(ctxt->userData, |
| 4753 | "Internal error, noop found\n"); |
| 4754 | ctxt->nbErrors++; |
| 4755 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4756 | } |
| 4757 | } |
| 4758 | child = child->next; |
| 4759 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4760 | ctxt->define = olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4761 | return(ret); |
| 4762 | } |
| 4763 | |
| 4764 | /** |
| 4765 | * xmlRelaxNGParsePatterns: |
| 4766 | * @ctxt: a Relax-NG parser context |
| 4767 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4768 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4769 | * |
| 4770 | * parse the content of a RelaxNG start node. |
| 4771 | * |
| 4772 | * Returns the definition pointer or NULL in case of error. |
| 4773 | */ |
| 4774 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4775 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
| 4776 | int group) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4777 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4778 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4779 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4780 | while (nodes != NULL) { |
| 4781 | if (IS_RELAXNG(nodes, "element")) { |
| 4782 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 4783 | if (def == NULL) { |
| 4784 | def = last = cur; |
| 4785 | } else { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4786 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 4787 | (def == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4788 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 4789 | def->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4790 | def->content = last; |
| 4791 | } |
| 4792 | last->next = cur; |
| 4793 | last = cur; |
| 4794 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4795 | cur->parent = parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4796 | } else { |
| 4797 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4798 | if (cur != NULL) { |
| 4799 | if (def == NULL) { |
| 4800 | def = last = cur; |
| 4801 | } else { |
| 4802 | last->next = cur; |
| 4803 | last = cur; |
| 4804 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4805 | } |
| 4806 | } |
| 4807 | nodes = nodes->next; |
| 4808 | } |
| 4809 | return(def); |
| 4810 | } |
| 4811 | |
| 4812 | /** |
| 4813 | * xmlRelaxNGParseStart: |
| 4814 | * @ctxt: a Relax-NG parser context |
| 4815 | * @nodes: start children nodes |
| 4816 | * |
| 4817 | * parse the content of a RelaxNG start node. |
| 4818 | * |
| 4819 | * Returns 0 in case of success, -1 in case of error |
| 4820 | */ |
| 4821 | static int |
| 4822 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 4823 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 4824 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4825 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4826 | if (nodes == NULL) { |
| 4827 | if (ctxt->error != NULL) |
| 4828 | ctxt->error(ctxt->userData, |
| 4829 | "start has no children\n"); |
| 4830 | ctxt->nbErrors++; |
| 4831 | return(-1); |
| 4832 | } |
| 4833 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4834 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4835 | if (def == NULL) |
| 4836 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4837 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4838 | if (nodes->children != NULL) { |
| 4839 | if (ctxt->error != NULL) |
| 4840 | ctxt->error(ctxt->userData, "element empty is not empty\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 4841 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4842 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4843 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4844 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4845 | if (def == NULL) |
| 4846 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4847 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4848 | if (nodes->children != NULL) { |
| 4849 | if (ctxt->error != NULL) |
| 4850 | ctxt->error(ctxt->userData, |
| 4851 | "element notAllowed is not empty\n"); |
| 4852 | ctxt->nbErrors++; |
| 4853 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4854 | } else { |
| 4855 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 4856 | } |
| 4857 | if (ctxt->grammar->start != NULL) { |
| 4858 | last = ctxt->grammar->start; |
| 4859 | while (last->next != NULL) |
| 4860 | last = last->next; |
| 4861 | last->next = def; |
| 4862 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4863 | ctxt->grammar->start = def; |
| 4864 | } |
| 4865 | nodes = nodes->next; |
| 4866 | if (nodes != NULL) { |
| 4867 | if (ctxt->error != NULL) |
| 4868 | ctxt->error(ctxt->userData, |
| 4869 | "start more than one children\n"); |
| 4870 | ctxt->nbErrors++; |
| 4871 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4872 | } |
| 4873 | return(ret); |
| 4874 | } |
| 4875 | |
| 4876 | /** |
| 4877 | * xmlRelaxNGParseGrammarContent: |
| 4878 | * @ctxt: a Relax-NG parser context |
| 4879 | * @nodes: grammar children nodes |
| 4880 | * |
| 4881 | * parse the content of a RelaxNG grammar node. |
| 4882 | * |
| 4883 | * Returns 0 in case of success, -1 in case of error |
| 4884 | */ |
| 4885 | static int |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4886 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4887 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4888 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4889 | |
| 4890 | if (nodes == NULL) { |
| 4891 | if (ctxt->error != NULL) |
| 4892 | ctxt->error(ctxt->userData, |
| 4893 | "grammar has no children\n"); |
| 4894 | ctxt->nbErrors++; |
| 4895 | return(-1); |
| 4896 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4897 | while (nodes != NULL) { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4898 | if (IS_RELAXNG(nodes, "start")) { |
| 4899 | if (nodes->children == NULL) { |
| 4900 | if (ctxt->error != NULL) |
| 4901 | ctxt->error(ctxt->userData, |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4902 | "start has no children\n"); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4903 | ctxt->nbErrors++; |
| 4904 | } else { |
| 4905 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 4906 | if (tmp != 0) |
| 4907 | ret = -1; |
| 4908 | } |
| 4909 | } else if (IS_RELAXNG(nodes, "define")) { |
| 4910 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 4911 | if (tmp != 0) |
| 4912 | ret = -1; |
| 4913 | } else if (IS_RELAXNG(nodes, "include")) { |
| 4914 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 4915 | if (tmp != 0) |
| 4916 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4917 | } else { |
| 4918 | if (ctxt->error != NULL) |
| 4919 | ctxt->error(ctxt->userData, |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4920 | "grammar has unexpected child %s\n", nodes->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4921 | ctxt->nbErrors++; |
| 4922 | ret = -1; |
| 4923 | } |
| 4924 | nodes = nodes->next; |
| 4925 | } |
| 4926 | return (ret); |
| 4927 | } |
| 4928 | |
| 4929 | /** |
| 4930 | * xmlRelaxNGCheckReference: |
| 4931 | * @ref: the ref |
| 4932 | * @ctxt: a Relax-NG parser context |
| 4933 | * @name: the name associated to the defines |
| 4934 | * |
| 4935 | * Applies the 4.17. combine attribute rule for all the define |
| 4936 | * element of a given grammar using the same name. |
| 4937 | */ |
| 4938 | static void |
| 4939 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
| 4940 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 4941 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4942 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4943 | |
| 4944 | grammar = ctxt->grammar; |
| 4945 | if (grammar == NULL) { |
| 4946 | if (ctxt->error != NULL) |
| 4947 | ctxt->error(ctxt->userData, |
| 4948 | "Internal error: no grammar in CheckReference %s\n", |
| 4949 | name); |
| 4950 | ctxt->nbErrors++; |
| 4951 | return; |
| 4952 | } |
| 4953 | if (ref->content != NULL) { |
| 4954 | if (ctxt->error != NULL) |
| 4955 | ctxt->error(ctxt->userData, |
| 4956 | "Internal error: reference has content in CheckReference %s\n", |
| 4957 | name); |
| 4958 | ctxt->nbErrors++; |
| 4959 | return; |
| 4960 | } |
| 4961 | if (grammar->defs != NULL) { |
| 4962 | def = xmlHashLookup(grammar->defs, name); |
| 4963 | if (def != NULL) { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4964 | cur = ref; |
| 4965 | while (cur != NULL) { |
| 4966 | cur->content = def; |
| 4967 | cur = cur->nextHash; |
| 4968 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4969 | } else { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 4970 | if (ctxt->error != NULL) |
| 4971 | ctxt->error(ctxt->userData, |
| 4972 | "Reference %s has no matching definition\n", |
| 4973 | name); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 4974 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4975 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 4976 | } else { |
| 4977 | if (ctxt->error != NULL) |
| 4978 | ctxt->error(ctxt->userData, |
| 4979 | "Reference %s has no matching definition\n", |
| 4980 | name); |
| 4981 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4982 | } |
| 4983 | /* |
| 4984 | * TODO: make a closure and verify there is no loop ! |
| 4985 | */ |
| 4986 | } |
| 4987 | |
| 4988 | /** |
| 4989 | * xmlRelaxNGCheckCombine: |
| 4990 | * @define: the define(s) list |
| 4991 | * @ctxt: a Relax-NG parser context |
| 4992 | * @name: the name associated to the defines |
| 4993 | * |
| 4994 | * Applies the 4.17. combine attribute rule for all the define |
| 4995 | * element of a given grammar using the same name. |
| 4996 | */ |
| 4997 | static void |
| 4998 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
| 4999 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5000 | xmlChar *combine; |
| 5001 | int choiceOrInterleave = -1; |
| 5002 | int missing = 0; |
| 5003 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5004 | |
| 5005 | if (define->nextHash == NULL) |
| 5006 | return; |
| 5007 | cur = define; |
| 5008 | while (cur != NULL) { |
| 5009 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5010 | if (combine != NULL) { |
| 5011 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5012 | if (choiceOrInterleave == -1) |
| 5013 | choiceOrInterleave = 1; |
| 5014 | else if (choiceOrInterleave == 0) { |
| 5015 | if (ctxt->error != NULL) |
| 5016 | ctxt->error(ctxt->userData, |
| 5017 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5018 | name); |
| 5019 | ctxt->nbErrors++; |
| 5020 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5021 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5022 | if (choiceOrInterleave == -1) |
| 5023 | choiceOrInterleave = 0; |
| 5024 | else if (choiceOrInterleave == 1) { |
| 5025 | if (ctxt->error != NULL) |
| 5026 | ctxt->error(ctxt->userData, |
| 5027 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5028 | name); |
| 5029 | ctxt->nbErrors++; |
| 5030 | } |
| 5031 | } else { |
| 5032 | if (ctxt->error != NULL) |
| 5033 | ctxt->error(ctxt->userData, |
| 5034 | "Defines for %s use unknown combine value '%s''\n", |
| 5035 | name, combine); |
| 5036 | ctxt->nbErrors++; |
| 5037 | } |
| 5038 | xmlFree(combine); |
| 5039 | } else { |
| 5040 | if (missing == 0) |
| 5041 | missing = 1; |
| 5042 | else { |
| 5043 | if (ctxt->error != NULL) |
| 5044 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5045 | "Some defines for %s needs the combine attribute\n", |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5046 | name); |
| 5047 | ctxt->nbErrors++; |
| 5048 | } |
| 5049 | } |
| 5050 | |
| 5051 | cur = cur->nextHash; |
| 5052 | } |
| 5053 | #ifdef DEBUG |
| 5054 | xmlGenericError(xmlGenericErrorContext, |
| 5055 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5056 | name, choiceOrInterleave); |
| 5057 | #endif |
| 5058 | if (choiceOrInterleave == -1) |
| 5059 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5060 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5061 | if (cur == NULL) |
| 5062 | return; |
| 5063 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5064 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5065 | else |
| 5066 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5067 | tmp = define; |
| 5068 | last = NULL; |
| 5069 | while (tmp != NULL) { |
| 5070 | if (tmp->content != NULL) { |
| 5071 | if (tmp->content->next != NULL) { |
| 5072 | /* |
| 5073 | * we need first to create a wrapper. |
| 5074 | */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5075 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5076 | if (tmp2 == NULL) |
| 5077 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5078 | tmp2->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5079 | tmp2->content = tmp->content; |
| 5080 | } else { |
| 5081 | tmp2 = tmp->content; |
| 5082 | } |
| 5083 | if (last == NULL) { |
| 5084 | cur->content = tmp2; |
| 5085 | } else { |
| 5086 | last->next = tmp2; |
| 5087 | } |
| 5088 | last = tmp2; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5089 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 5090 | tmp->content = cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5091 | tmp = tmp->nextHash; |
| 5092 | } |
| 5093 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5094 | if (choiceOrInterleave == 0) { |
| 5095 | if (ctxt->interleaves == NULL) |
| 5096 | ctxt->interleaves = xmlHashCreate(10); |
| 5097 | if (ctxt->interleaves == NULL) { |
| 5098 | if (ctxt->error != NULL) |
| 5099 | ctxt->error(ctxt->userData, |
| 5100 | "Failed to create interleaves hash table\n"); |
| 5101 | ctxt->nbErrors++; |
| 5102 | } else { |
| 5103 | char tmpname[32]; |
| 5104 | |
| 5105 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5106 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5107 | if (ctxt->error != NULL) |
| 5108 | ctxt->error(ctxt->userData, |
| 5109 | "Failed to add %s to hash table\n", tmpname); |
| 5110 | ctxt->nbErrors++; |
| 5111 | } |
| 5112 | } |
| 5113 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5114 | } |
| 5115 | |
| 5116 | /** |
| 5117 | * xmlRelaxNGCombineStart: |
| 5118 | * @ctxt: a Relax-NG parser context |
| 5119 | * @grammar: the grammar |
| 5120 | * |
| 5121 | * Applies the 4.17. combine rule for all the start |
| 5122 | * element of a given grammar. |
| 5123 | */ |
| 5124 | static void |
| 5125 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
| 5126 | xmlRelaxNGGrammarPtr grammar) { |
| 5127 | xmlRelaxNGDefinePtr starts; |
| 5128 | xmlChar *combine; |
| 5129 | int choiceOrInterleave = -1; |
| 5130 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5131 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5132 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5133 | starts = grammar->start; |
| 5134 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5135 | return; |
| 5136 | cur = starts; |
| 5137 | while (cur != NULL) { |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5138 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5139 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5140 | combine = NULL; |
| 5141 | if (ctxt->error != NULL) |
| 5142 | ctxt->error(ctxt->userData, |
| 5143 | "Internal error: start element not found\n"); |
| 5144 | ctxt->nbErrors++; |
| 5145 | } else { |
| 5146 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5147 | } |
| 5148 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5149 | if (combine != NULL) { |
| 5150 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5151 | if (choiceOrInterleave == -1) |
| 5152 | choiceOrInterleave = 1; |
| 5153 | else if (choiceOrInterleave == 0) { |
| 5154 | if (ctxt->error != NULL) |
| 5155 | ctxt->error(ctxt->userData, |
| 5156 | "<start> use both 'choice' and 'interleave'\n"); |
| 5157 | ctxt->nbErrors++; |
| 5158 | } |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5159 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5160 | if (choiceOrInterleave == -1) |
| 5161 | choiceOrInterleave = 0; |
| 5162 | else if (choiceOrInterleave == 1) { |
| 5163 | if (ctxt->error != NULL) |
| 5164 | ctxt->error(ctxt->userData, |
| 5165 | "<start> use both 'choice' and 'interleave'\n"); |
| 5166 | ctxt->nbErrors++; |
| 5167 | } |
| 5168 | } else { |
| 5169 | if (ctxt->error != NULL) |
| 5170 | ctxt->error(ctxt->userData, |
| 5171 | "<start> uses unknown combine value '%s''\n", combine); |
| 5172 | ctxt->nbErrors++; |
| 5173 | } |
| 5174 | xmlFree(combine); |
| 5175 | } else { |
| 5176 | if (missing == 0) |
| 5177 | missing = 1; |
| 5178 | else { |
| 5179 | if (ctxt->error != NULL) |
| 5180 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5181 | "Some <start> element miss the combine attribute\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5182 | ctxt->nbErrors++; |
| 5183 | } |
| 5184 | } |
| 5185 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5186 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5187 | } |
| 5188 | #ifdef DEBUG |
| 5189 | xmlGenericError(xmlGenericErrorContext, |
| 5190 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5191 | choiceOrInterleave); |
| 5192 | #endif |
| 5193 | if (choiceOrInterleave == -1) |
| 5194 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5195 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5196 | if (cur == NULL) |
| 5197 | return; |
| 5198 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5199 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5200 | else |
| 5201 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5202 | cur->content = grammar->start; |
| 5203 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5204 | if (choiceOrInterleave == 0) { |
| 5205 | if (ctxt->interleaves == NULL) |
| 5206 | ctxt->interleaves = xmlHashCreate(10); |
| 5207 | if (ctxt->interleaves == NULL) { |
| 5208 | if (ctxt->error != NULL) |
| 5209 | ctxt->error(ctxt->userData, |
| 5210 | "Failed to create interleaves hash table\n"); |
| 5211 | ctxt->nbErrors++; |
| 5212 | } else { |
| 5213 | char tmpname[32]; |
| 5214 | |
| 5215 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5216 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5217 | if (ctxt->error != NULL) |
| 5218 | ctxt->error(ctxt->userData, |
| 5219 | "Failed to add %s to hash table\n", tmpname); |
| 5220 | ctxt->nbErrors++; |
| 5221 | } |
| 5222 | } |
| 5223 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
| 5226 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5227 | * xmlRelaxNGCheckCycles: |
| 5228 | * @ctxt: a Relax-NG parser context |
| 5229 | * @nodes: grammar children nodes |
| 5230 | * @depth: the counter |
| 5231 | * |
| 5232 | * Check for cycles. |
| 5233 | * |
| 5234 | * Returns 0 if check passed, and -1 in case of error |
| 5235 | */ |
| 5236 | static int |
| 5237 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5238 | xmlRelaxNGDefinePtr cur, int depth) { |
| 5239 | int ret = 0; |
| 5240 | |
| 5241 | while ((ret == 0) && (cur != NULL)) { |
| 5242 | if ((cur->type == XML_RELAXNG_REF) || |
| 5243 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5244 | if (cur->depth == -1) { |
| 5245 | cur->depth = depth; |
| 5246 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5247 | cur->depth = -2; |
| 5248 | } else if (depth == cur->depth) { |
| 5249 | if (ctxt->error != NULL) |
| 5250 | ctxt->error(ctxt->userData, |
| 5251 | "Detected a cycle in %s references\n", cur->name); |
| 5252 | ctxt->nbErrors++; |
| 5253 | return(-1); |
| 5254 | } |
| 5255 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5256 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5257 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5258 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5259 | } |
| 5260 | cur = cur->next; |
| 5261 | } |
| 5262 | return(ret); |
| 5263 | } |
| 5264 | |
| 5265 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5266 | * xmlRelaxNGTryUnlink: |
| 5267 | * @ctxt: a Relax-NG parser context |
| 5268 | * @cur: the definition to unlink |
| 5269 | * @parent: the parent definition |
| 5270 | * @prev: the previous sibling definition |
| 5271 | * |
| 5272 | * Try to unlink a definition. If not possble make it a NOOP |
| 5273 | * |
| 5274 | * Returns the new prev definition |
| 5275 | */ |
| 5276 | static xmlRelaxNGDefinePtr |
| 5277 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5278 | xmlRelaxNGDefinePtr cur, |
| 5279 | xmlRelaxNGDefinePtr parent, |
| 5280 | xmlRelaxNGDefinePtr prev) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5281 | if (prev != NULL) { |
| 5282 | prev->next = cur->next; |
| 5283 | } else { |
| 5284 | if (parent != NULL) { |
| 5285 | if (parent->content == cur) |
| 5286 | parent->content = cur->next; |
| 5287 | else if (parent->attrs == cur) |
| 5288 | parent->attrs = cur->next; |
| 5289 | else if (parent->nameClass == cur) |
| 5290 | parent->nameClass = cur->next; |
| 5291 | } else { |
| 5292 | cur->type = XML_RELAXNG_NOOP; |
| 5293 | prev = cur; |
| 5294 | } |
| 5295 | } |
| 5296 | return(prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5297 | } |
| 5298 | |
| 5299 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5300 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5301 | * @ctxt: a Relax-NG parser context |
| 5302 | * @nodes: grammar children nodes |
| 5303 | * |
| 5304 | * Check for simplification of empty and notAllowed |
| 5305 | */ |
| 5306 | static void |
| 5307 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5308 | xmlRelaxNGDefinePtr cur, |
| 5309 | xmlRelaxNGDefinePtr parent) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5310 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5311 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5312 | while (cur != NULL) { |
| 5313 | if ((cur->type == XML_RELAXNG_REF) || |
| 5314 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5315 | if (cur->depth != -3) { |
| 5316 | cur->depth = -3; |
| 5317 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5318 | } |
| 5319 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5320 | cur->parent = parent; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5321 | if ((parent != NULL) && |
| 5322 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5323 | (parent->type == XML_RELAXNG_LIST) || |
| 5324 | (parent->type == XML_RELAXNG_GROUP) || |
| 5325 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5326 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5327 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5328 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5329 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5330 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5331 | if ((parent != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5332 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5333 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5334 | } else |
| 5335 | prev = cur; |
| 5336 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5337 | cur->parent = parent; |
| 5338 | if ((parent != NULL) && |
| 5339 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5340 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5341 | parent->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5342 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5343 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5344 | if ((parent != NULL) && |
| 5345 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5346 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5347 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5348 | } else |
| 5349 | prev = cur; |
| 5350 | } else { |
| 5351 | cur->parent = parent; |
| 5352 | if (cur->content != NULL) |
| 5353 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
| 5354 | if (cur->attrs != NULL) |
| 5355 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5356 | if (cur->nameClass != NULL) |
| 5357 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5358 | /* |
| 5359 | * This may result in a simplification |
| 5360 | */ |
| 5361 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 5362 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 5363 | if (cur->content == NULL) |
| 5364 | cur->type = XML_RELAXNG_EMPTY; |
| 5365 | else if (cur->content->next == NULL) { |
| 5366 | if ((parent == NULL) && (prev == NULL)) { |
| 5367 | cur->type = XML_RELAXNG_NOOP; |
| 5368 | } else if (prev == NULL) { |
| 5369 | parent->content = cur->content; |
| 5370 | cur->content->next = cur->next; |
| 5371 | cur = cur->content; |
| 5372 | } else { |
| 5373 | cur->content->next = cur->next; |
| 5374 | prev->next = cur->content; |
| 5375 | cur = cur->content; |
| 5376 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5377 | } |
| 5378 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5379 | /* |
| 5380 | * the current node may have been transformed back |
| 5381 | */ |
| 5382 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 5383 | (cur->content != NULL) && |
| 5384 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 5385 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5386 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5387 | if ((parent != NULL) && |
| 5388 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5389 | (parent->type == XML_RELAXNG_LIST) || |
| 5390 | (parent->type == XML_RELAXNG_GROUP) || |
| 5391 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5392 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5393 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5394 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5395 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5396 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5397 | if ((parent != NULL) && |
| 5398 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5399 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5400 | } else |
| 5401 | prev = cur; |
| 5402 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5403 | if ((parent != NULL) && |
| 5404 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5405 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5406 | parent->type = XML_RELAXNG_EMPTY; |
| 5407 | break; |
| 5408 | } |
| 5409 | if ((parent != NULL) && |
| 5410 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5411 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5412 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 5413 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5414 | } else |
| 5415 | prev = cur; |
| 5416 | } else { |
| 5417 | prev = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5418 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5419 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5420 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5421 | } |
| 5422 | } |
| 5423 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5424 | /** |
| 5425 | * xmlRelaxNGGroupContentType: |
| 5426 | * @ct1: the first content type |
| 5427 | * @ct2: the second content type |
| 5428 | * |
| 5429 | * Try to group 2 content types |
| 5430 | * |
| 5431 | * Returns the content type |
| 5432 | */ |
| 5433 | static xmlRelaxNGContentType |
| 5434 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
| 5435 | xmlRelaxNGContentType ct2) { |
| 5436 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 5437 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 5438 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5439 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
| 5440 | return(ct2); |
| 5441 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
| 5442 | return(ct1); |
| 5443 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
| 5444 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 5445 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 5446 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5447 | } |
| 5448 | |
| 5449 | /** |
| 5450 | * xmlRelaxNGMaxContentType: |
| 5451 | * @ct1: the first content type |
| 5452 | * @ct2: the second content type |
| 5453 | * |
| 5454 | * Compute the max content-type |
| 5455 | * |
| 5456 | * Returns the content type |
| 5457 | */ |
| 5458 | static xmlRelaxNGContentType |
| 5459 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
| 5460 | xmlRelaxNGContentType ct2) { |
| 5461 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 5462 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 5463 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5464 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
| 5465 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 5466 | return(XML_RELAXNG_CONTENT_SIMPLE); |
| 5467 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
| 5468 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 5469 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 5470 | return(XML_RELAXNG_CONTENT_EMPTY); |
| 5471 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5472 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5473 | /** |
| 5474 | * xmlRelaxNGCheckRules: |
| 5475 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5476 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5477 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5478 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5479 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5480 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5481 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5482 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5483 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5484 | static xmlRelaxNGContentType |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5485 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 5486 | xmlRelaxNGDefinePtr cur, int flags, |
| 5487 | xmlRelaxNGType ptype) { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5488 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5489 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5490 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5491 | while (cur != NULL) { |
| 5492 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 5493 | if ((cur->type == XML_RELAXNG_REF) || |
| 5494 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5495 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5496 | if (ctxt->error != NULL) |
| 5497 | ctxt->error(ctxt->userData, |
| 5498 | "Found forbidden pattern list//ref\n"); |
| 5499 | ctxt->nbErrors++; |
| 5500 | } |
| 5501 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5502 | if (ctxt->error != NULL) |
| 5503 | ctxt->error(ctxt->userData, |
| 5504 | "Found forbidden pattern data/except//ref\n"); |
| 5505 | ctxt->nbErrors++; |
| 5506 | } |
| 5507 | if (cur->depth > -4) { |
| 5508 | cur->depth = -4; |
| 5509 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 5510 | flags, cur->type); |
| 5511 | cur->depth = ret - 15 ; |
| 5512 | } else if (cur->depth == -4) { |
| 5513 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 5514 | } else { |
| 5515 | ret = (xmlRelaxNGContentType) cur->depth + 15; |
| 5516 | } |
| 5517 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5518 | /* |
| 5519 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 5520 | */ |
| 5521 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 5522 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5523 | if (ctxt->error != NULL) |
| 5524 | ctxt->error(ctxt->userData, |
| 5525 | "Found forbidden pattern data/except//element(ref)\n"); |
| 5526 | ctxt->nbErrors++; |
| 5527 | } |
| 5528 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5529 | if (ctxt->error != NULL) |
| 5530 | ctxt->error(ctxt->userData, |
| 5531 | "Found forbidden pattern list//element(ref)\n"); |
| 5532 | ctxt->nbErrors++; |
| 5533 | } |
| 5534 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 5535 | if (ctxt->error != NULL) |
| 5536 | ctxt->error(ctxt->userData, |
| 5537 | "Found forbidden pattern attribute//element(ref)\n"); |
| 5538 | ctxt->nbErrors++; |
| 5539 | } |
| 5540 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 5541 | if (ctxt->error != NULL) |
| 5542 | ctxt->error(ctxt->userData, |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 5543 | "Found forbidden pattern attribute//element(ref)\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5544 | ctxt->nbErrors++; |
| 5545 | } |
| 5546 | /* |
| 5547 | * reset since in the simple form elements are only child |
| 5548 | * of grammar/define |
| 5549 | */ |
| 5550 | nflags = 0; |
| 5551 | ret = xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 5552 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 5553 | if (ctxt->error != NULL) |
| 5554 | ctxt->error(ctxt->userData, |
| 5555 | "Element %s attributes have a content type error\n", |
| 5556 | cur->name); |
| 5557 | ctxt->nbErrors++; |
| 5558 | } |
| 5559 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5560 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 5561 | if (ctxt->error != NULL) |
| 5562 | ctxt->error(ctxt->userData, |
| 5563 | "Element %s has a content type error\n", |
| 5564 | cur->name); |
| 5565 | ctxt->nbErrors++; |
| 5566 | } else { |
| 5567 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 5568 | } |
| 5569 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 5570 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 5571 | if (ctxt->error != NULL) |
| 5572 | ctxt->error(ctxt->userData, |
| 5573 | "Found forbidden pattern attribute//attribute\n"); |
| 5574 | ctxt->nbErrors++; |
| 5575 | } |
| 5576 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5577 | if (ctxt->error != NULL) |
| 5578 | ctxt->error(ctxt->userData, |
| 5579 | "Found forbidden pattern list//attribute\n"); |
| 5580 | ctxt->nbErrors++; |
| 5581 | } |
| 5582 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 5583 | if (ctxt->error != NULL) |
| 5584 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5585 | "Found forbidden pattern oneOrMore//group//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5586 | ctxt->nbErrors++; |
| 5587 | } |
| 5588 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 5589 | if (ctxt->error != NULL) |
| 5590 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5591 | "Found forbidden pattern oneOrMore//interleave//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5592 | ctxt->nbErrors++; |
| 5593 | } |
| 5594 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5595 | if (ctxt->error != NULL) |
| 5596 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5597 | "Found forbidden pattern data/except//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5598 | ctxt->nbErrors++; |
| 5599 | } |
| 5600 | if (flags & XML_RELAXNG_IN_START) { |
| 5601 | if (ctxt->error != NULL) |
| 5602 | ctxt->error(ctxt->userData, |
| 5603 | "Found forbidden pattern start//attribute\n"); |
| 5604 | ctxt->nbErrors++; |
| 5605 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5606 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) && (cur->name == NULL)) { |
| 5607 | if (cur->ns == NULL) { |
| 5608 | if (ctxt->error != NULL) |
| 5609 | ctxt->error(ctxt->userData, |
| 5610 | "Found anyName attribute without oneOrMore ancestor\n"); |
| 5611 | ctxt->nbErrors++; |
| 5612 | } else { |
| 5613 | if (ctxt->error != NULL) |
| 5614 | ctxt->error(ctxt->userData, |
| 5615 | "Found nsName attribute without oneOrMore ancestor\n"); |
| 5616 | ctxt->nbErrors++; |
| 5617 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5618 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5619 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 5620 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5621 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 5622 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 5623 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 5624 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5625 | if (ctxt->error != NULL) |
| 5626 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5627 | "Found forbidden pattern data/except//oneOrMore\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5628 | ctxt->nbErrors++; |
| 5629 | } |
| 5630 | if (flags & XML_RELAXNG_IN_START) { |
| 5631 | if (ctxt->error != NULL) |
| 5632 | ctxt->error(ctxt->userData, |
| 5633 | "Found forbidden pattern start//oneOrMore\n"); |
| 5634 | ctxt->nbErrors++; |
| 5635 | } |
| 5636 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 5637 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5638 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 5639 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 5640 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5641 | if (ctxt->error != NULL) |
| 5642 | ctxt->error(ctxt->userData, |
| 5643 | "Found forbidden pattern list//list\n"); |
| 5644 | ctxt->nbErrors++; |
| 5645 | } |
| 5646 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5647 | if (ctxt->error != NULL) |
| 5648 | ctxt->error(ctxt->userData, |
| 5649 | "Found forbidden pattern data/except//list\n"); |
| 5650 | ctxt->nbErrors++; |
| 5651 | } |
| 5652 | if (flags & XML_RELAXNG_IN_START) { |
| 5653 | if (ctxt->error != NULL) |
| 5654 | ctxt->error(ctxt->userData, |
| 5655 | "Found forbidden pattern start//list\n"); |
| 5656 | ctxt->nbErrors++; |
| 5657 | } |
| 5658 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 5659 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5660 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 5661 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5662 | if (ctxt->error != NULL) |
| 5663 | ctxt->error(ctxt->userData, |
| 5664 | "Found forbidden pattern data/except//group\n"); |
| 5665 | ctxt->nbErrors++; |
| 5666 | } |
| 5667 | if (flags & XML_RELAXNG_IN_START) { |
| 5668 | if (ctxt->error != NULL) |
| 5669 | ctxt->error(ctxt->userData, |
| 5670 | "Found forbidden pattern start//group\n"); |
| 5671 | ctxt->nbErrors++; |
| 5672 | } |
| 5673 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 5674 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 5675 | else |
| 5676 | nflags = flags; |
| 5677 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5678 | /* |
| 5679 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 5680 | */ |
| 5681 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 5682 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 5683 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5684 | if (ctxt->error != NULL) |
| 5685 | ctxt->error(ctxt->userData, |
| 5686 | "Found forbidden pattern list//interleave\n"); |
| 5687 | ctxt->nbErrors++; |
| 5688 | } |
| 5689 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5690 | if (ctxt->error != NULL) |
| 5691 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5692 | "Found forbidden pattern data/except//interleave\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5693 | ctxt->nbErrors++; |
| 5694 | } |
| 5695 | if (flags & XML_RELAXNG_IN_START) { |
| 5696 | if (ctxt->error != NULL) |
| 5697 | ctxt->error(ctxt->userData, |
| 5698 | "Found forbidden pattern start//interleave\n"); |
| 5699 | ctxt->nbErrors++; |
| 5700 | } |
| 5701 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 5702 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 5703 | else |
| 5704 | nflags = flags; |
| 5705 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5706 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 5707 | if ((cur->parent != NULL) && |
| 5708 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 5709 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 5710 | else |
| 5711 | nflags = flags; |
| 5712 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 5713 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 5714 | if (flags & XML_RELAXNG_IN_START) { |
| 5715 | if (ctxt->error != NULL) |
| 5716 | ctxt->error(ctxt->userData, |
| 5717 | "Found forbidden pattern start//data\n"); |
| 5718 | ctxt->nbErrors++; |
| 5719 | } |
| 5720 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 5721 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 5722 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 5723 | if (flags & XML_RELAXNG_IN_START) { |
| 5724 | if (ctxt->error != NULL) |
| 5725 | ctxt->error(ctxt->userData, |
| 5726 | "Found forbidden pattern start//value\n"); |
| 5727 | ctxt->nbErrors++; |
| 5728 | } |
| 5729 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 5730 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 5731 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 5732 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5733 | if (ctxt->error != NULL) |
| 5734 | ctxt->error(ctxt->userData, |
| 5735 | "Found forbidden pattern list//text\n"); |
| 5736 | ctxt->nbErrors++; |
| 5737 | } |
| 5738 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5739 | if (ctxt->error != NULL) |
| 5740 | ctxt->error(ctxt->userData, |
| 5741 | "Found forbidden pattern data/except//text\n"); |
| 5742 | ctxt->nbErrors++; |
| 5743 | } |
| 5744 | if (flags & XML_RELAXNG_IN_START) { |
| 5745 | if (ctxt->error != NULL) |
| 5746 | ctxt->error(ctxt->userData, |
| 5747 | "Found forbidden pattern start//text\n"); |
| 5748 | ctxt->nbErrors++; |
| 5749 | } |
| 5750 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 5751 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 5752 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5753 | if (ctxt->error != NULL) |
| 5754 | ctxt->error(ctxt->userData, |
| 5755 | "Found forbidden pattern data/except//empty\n"); |
| 5756 | ctxt->nbErrors++; |
| 5757 | } |
| 5758 | if (flags & XML_RELAXNG_IN_START) { |
| 5759 | if (ctxt->error != NULL) |
| 5760 | ctxt->error(ctxt->userData, |
| 5761 | "Found forbidden pattern start//empty\n"); |
| 5762 | ctxt->nbErrors++; |
| 5763 | } |
| 5764 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 5765 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 5766 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 5767 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 5768 | } else { |
| 5769 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 5770 | } |
| 5771 | cur = cur->next; |
| 5772 | if (ptype == XML_RELAXNG_GROUP) { |
| 5773 | val = xmlRelaxNGGroupContentType(val, ret); |
| 5774 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 5775 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 5776 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 5777 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 5778 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 5779 | val = xmlRelaxNGMaxContentType(val, ret); |
| 5780 | } else if (ptype == XML_RELAXNG_LIST) { |
| 5781 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 5782 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 5783 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 5784 | val = XML_RELAXNG_CONTENT_ERROR; |
| 5785 | else |
| 5786 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 5787 | } else { |
| 5788 | val = xmlRelaxNGGroupContentType(val, ret); |
| 5789 | } |
| 5790 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5791 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5792 | return(val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5793 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5794 | |
| 5795 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5796 | * xmlRelaxNGParseGrammar: |
| 5797 | * @ctxt: a Relax-NG parser context |
| 5798 | * @nodes: grammar children nodes |
| 5799 | * |
| 5800 | * parse a Relax-NG <grammar> node |
| 5801 | * |
| 5802 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 5803 | * NULL in case of error |
| 5804 | */ |
| 5805 | static xmlRelaxNGGrammarPtr |
| 5806 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 5807 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 5808 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5809 | #ifdef DEBUG_GRAMMAR |
| 5810 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 5811 | #endif |
| 5812 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5813 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 5814 | if (ret == NULL) |
| 5815 | return(NULL); |
| 5816 | |
| 5817 | /* |
| 5818 | * Link the new grammar in the tree |
| 5819 | */ |
| 5820 | ret->parent = ctxt->grammar; |
| 5821 | if (ctxt->grammar != NULL) { |
| 5822 | tmp = ctxt->grammar->children; |
| 5823 | if (tmp == NULL) { |
| 5824 | ctxt->grammar->children = ret; |
| 5825 | } else { |
| 5826 | while (tmp->next != NULL) |
| 5827 | tmp = tmp->next; |
| 5828 | tmp->next = ret; |
| 5829 | } |
| 5830 | } |
| 5831 | |
| 5832 | old = ctxt->grammar; |
| 5833 | ctxt->grammar = ret; |
| 5834 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 5835 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5836 | if (ctxt->grammar == NULL) { |
| 5837 | if (ctxt->error != NULL) |
| 5838 | ctxt->error(ctxt->userData, |
| 5839 | "Failed to parse <grammar> content\n"); |
| 5840 | ctxt->nbErrors++; |
| 5841 | } else if (ctxt->grammar->start == NULL) { |
| 5842 | if (ctxt->error != NULL) |
| 5843 | ctxt->error(ctxt->userData, |
| 5844 | "Element <grammar> has no <start>\n"); |
| 5845 | ctxt->nbErrors++; |
| 5846 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5847 | |
| 5848 | /* |
| 5849 | * Apply 4.17 mergingd rules to defines and starts |
| 5850 | */ |
| 5851 | xmlRelaxNGCombineStart(ctxt, ret); |
| 5852 | if (ret->defs != NULL) { |
| 5853 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 5854 | ctxt); |
| 5855 | } |
| 5856 | |
| 5857 | /* |
| 5858 | * link together defines and refs in this grammar |
| 5859 | */ |
| 5860 | if (ret->refs != NULL) { |
| 5861 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 5862 | ctxt); |
| 5863 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 5864 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5865 | ctxt->grammar = old; |
| 5866 | return(ret); |
| 5867 | } |
| 5868 | |
| 5869 | /** |
| 5870 | * xmlRelaxNGParseDocument: |
| 5871 | * @ctxt: a Relax-NG parser context |
| 5872 | * @node: the root node of the RelaxNG schema |
| 5873 | * |
| 5874 | * parse a Relax-NG definition resource and build an internal |
| 5875 | * xmlRelaxNG struture which can be used to validate instances. |
| 5876 | * |
| 5877 | * Returns the internal XML RelaxNG structure built or |
| 5878 | * NULL in case of error |
| 5879 | */ |
| 5880 | static xmlRelaxNGPtr |
| 5881 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 5882 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5883 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 5884 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5885 | |
| 5886 | if ((ctxt == NULL) || (node == NULL)) |
| 5887 | return (NULL); |
| 5888 | |
| 5889 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 5890 | if (schema == NULL) |
| 5891 | return(NULL); |
| 5892 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5893 | olddefine = ctxt->define; |
| 5894 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5895 | if (IS_RELAXNG(node, "grammar")) { |
| 5896 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 5897 | } else { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5898 | xmlRelaxNGGrammarPtr tmp, ret; |
| 5899 | |
| 5900 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5901 | if (schema->topgrammar == NULL) { |
| 5902 | return(schema); |
| 5903 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5904 | /* |
| 5905 | * Link the new grammar in the tree |
| 5906 | */ |
| 5907 | ret->parent = ctxt->grammar; |
| 5908 | if (ctxt->grammar != NULL) { |
| 5909 | tmp = ctxt->grammar->children; |
| 5910 | if (tmp == NULL) { |
| 5911 | ctxt->grammar->children = ret; |
| 5912 | } else { |
| 5913 | while (tmp->next != NULL) |
| 5914 | tmp = tmp->next; |
| 5915 | tmp->next = ret; |
| 5916 | } |
| 5917 | } |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 5918 | old = ctxt->grammar; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5919 | ctxt->grammar = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5920 | xmlRelaxNGParseStart(ctxt, node); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 5921 | if (old != NULL) |
| 5922 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5923 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5924 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5925 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5926 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5927 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5928 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 5929 | while ((schema->topgrammar->start != NULL) && |
| 5930 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 5931 | (schema->topgrammar->start->next != NULL)) |
| 5932 | schema->topgrammar->start = schema->topgrammar->start->content; |
| 5933 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 5934 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5935 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5936 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5937 | |
| 5938 | #ifdef DEBUG |
| 5939 | if (schema == NULL) |
| 5940 | xmlGenericError(xmlGenericErrorContext, |
| 5941 | "xmlRelaxNGParseDocument() failed\n"); |
| 5942 | #endif |
| 5943 | |
| 5944 | return (schema); |
| 5945 | } |
| 5946 | |
| 5947 | /************************************************************************ |
| 5948 | * * |
| 5949 | * Reading RelaxNGs * |
| 5950 | * * |
| 5951 | ************************************************************************/ |
| 5952 | |
| 5953 | /** |
| 5954 | * xmlRelaxNGNewParserCtxt: |
| 5955 | * @URL: the location of the schema |
| 5956 | * |
| 5957 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 5958 | * to contain an XML RelaxNGs file. |
| 5959 | * |
| 5960 | * Returns the parser context or NULL in case of error |
| 5961 | */ |
| 5962 | xmlRelaxNGParserCtxtPtr |
| 5963 | xmlRelaxNGNewParserCtxt(const char *URL) { |
| 5964 | xmlRelaxNGParserCtxtPtr ret; |
| 5965 | |
| 5966 | if (URL == NULL) |
| 5967 | return(NULL); |
| 5968 | |
| 5969 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 5970 | if (ret == NULL) { |
| 5971 | xmlGenericError(xmlGenericErrorContext, |
| 5972 | "Failed to allocate new schama parser context for %s\n", URL); |
| 5973 | return (NULL); |
| 5974 | } |
| 5975 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 5976 | ret->URL = xmlStrdup((const xmlChar *)URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5977 | ret->error = xmlGenericError; |
| 5978 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5979 | return (ret); |
| 5980 | } |
| 5981 | |
| 5982 | /** |
| 5983 | * xmlRelaxNGNewMemParserCtxt: |
| 5984 | * @buffer: a pointer to a char array containing the schemas |
| 5985 | * @size: the size of the array |
| 5986 | * |
| 5987 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 5988 | * to contain an XML RelaxNGs file. |
| 5989 | * |
| 5990 | * Returns the parser context or NULL in case of error |
| 5991 | */ |
| 5992 | xmlRelaxNGParserCtxtPtr |
| 5993 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) { |
| 5994 | xmlRelaxNGParserCtxtPtr ret; |
| 5995 | |
| 5996 | if ((buffer == NULL) || (size <= 0)) |
| 5997 | return(NULL); |
| 5998 | |
| 5999 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6000 | if (ret == NULL) { |
| 6001 | xmlGenericError(xmlGenericErrorContext, |
| 6002 | "Failed to allocate new schama parser context\n"); |
| 6003 | return (NULL); |
| 6004 | } |
| 6005 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6006 | ret->buffer = buffer; |
| 6007 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6008 | ret->error = xmlGenericError; |
| 6009 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6010 | return (ret); |
| 6011 | } |
| 6012 | |
| 6013 | /** |
| 6014 | * xmlRelaxNGFreeParserCtxt: |
| 6015 | * @ctxt: the schema parser context |
| 6016 | * |
| 6017 | * Free the resources associated to the schema parser context |
| 6018 | */ |
| 6019 | void |
| 6020 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) { |
| 6021 | if (ctxt == NULL) |
| 6022 | return; |
| 6023 | if (ctxt->URL != NULL) |
| 6024 | xmlFree(ctxt->URL); |
| 6025 | if (ctxt->doc != NULL) |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6026 | xmlFreeDoc(ctxt->document); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6027 | if (ctxt->interleaves != NULL) |
| 6028 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6029 | if (ctxt->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6030 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6031 | if (ctxt->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6032 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6033 | if (ctxt->docTab != NULL) |
| 6034 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6035 | if (ctxt->incTab != NULL) |
| 6036 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6037 | if (ctxt->defTab != NULL) { |
| 6038 | int i; |
| 6039 | |
| 6040 | for (i = 0;i < ctxt->defNr;i++) |
| 6041 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6042 | xmlFree(ctxt->defTab); |
| 6043 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6044 | xmlFree(ctxt); |
| 6045 | } |
| 6046 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6047 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6048 | * xmlRelaxNGNormExtSpace: |
| 6049 | * @value: a value |
| 6050 | * |
| 6051 | * Removes the leading and ending spaces of the value |
| 6052 | * The string is modified "in situ" |
| 6053 | */ |
| 6054 | static void |
| 6055 | xmlRelaxNGNormExtSpace(xmlChar *value) { |
| 6056 | xmlChar *start = value; |
| 6057 | xmlChar *cur = value; |
| 6058 | if (value == NULL) |
| 6059 | return; |
| 6060 | |
| 6061 | while (IS_BLANK(*cur)) cur++; |
| 6062 | if (cur == start) { |
| 6063 | do { |
| 6064 | while ((*cur != 0) && (!IS_BLANK(*cur))) cur++; |
| 6065 | if (*cur == 0) |
| 6066 | return; |
| 6067 | start = cur; |
| 6068 | while (IS_BLANK(*cur)) cur++; |
| 6069 | if (*cur == 0) { |
| 6070 | *start = 0; |
| 6071 | return; |
| 6072 | } |
| 6073 | } while (1); |
| 6074 | } else { |
| 6075 | do { |
| 6076 | while ((*cur != 0) && (!IS_BLANK(*cur))) |
| 6077 | *start++ = *cur++; |
| 6078 | if (*cur == 0) { |
| 6079 | *start = 0; |
| 6080 | return; |
| 6081 | } |
| 6082 | /* don't try to normalize the inner spaces */ |
| 6083 | while (IS_BLANK(*cur)) cur++; |
| 6084 | *start++ = *cur++; |
| 6085 | if (*cur == 0) { |
| 6086 | *start = 0; |
| 6087 | return; |
| 6088 | } |
| 6089 | } while (1); |
| 6090 | } |
| 6091 | } |
| 6092 | |
| 6093 | /** |
| 6094 | * xmlRelaxNGCheckAttributes: |
| 6095 | * @ctxt: a Relax-NG parser context |
| 6096 | * @node: a Relax-NG node |
| 6097 | * |
| 6098 | * Check all the attributes on the given node |
| 6099 | */ |
| 6100 | static void |
| 6101 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6102 | xmlAttrPtr cur, next; |
| 6103 | |
| 6104 | cur = node->properties; |
| 6105 | while (cur != NULL) { |
| 6106 | next = cur->next; |
| 6107 | if ((cur->ns == NULL) || |
| 6108 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6109 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6110 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6111 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6112 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6113 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6114 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6115 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6116 | if (ctxt->error != NULL) |
| 6117 | ctxt->error(ctxt->userData, |
| 6118 | "Attribute %s is not allowed on %s\n", |
| 6119 | cur->name, node->name); |
| 6120 | ctxt->nbErrors++; |
| 6121 | } |
| 6122 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6123 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6124 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6125 | if (ctxt->error != NULL) |
| 6126 | ctxt->error(ctxt->userData, |
| 6127 | "Attribute %s is not allowed on %s\n", |
| 6128 | cur->name, node->name); |
| 6129 | ctxt->nbErrors++; |
| 6130 | } |
| 6131 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6132 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6133 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6134 | if (ctxt->error != NULL) |
| 6135 | ctxt->error(ctxt->userData, |
| 6136 | "Attribute %s is not allowed on %s\n", |
| 6137 | cur->name, node->name); |
| 6138 | ctxt->nbErrors++; |
| 6139 | } |
| 6140 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6141 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6142 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6143 | if (ctxt->error != NULL) |
| 6144 | ctxt->error(ctxt->userData, |
| 6145 | "Attribute %s is not allowed on %s\n", |
| 6146 | cur->name, node->name); |
| 6147 | ctxt->nbErrors++; |
| 6148 | } |
| 6149 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6150 | xmlChar *val; |
| 6151 | xmlURIPtr uri; |
| 6152 | |
| 6153 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6154 | if (val != NULL) { |
| 6155 | if (val[0] != 0) { |
| 6156 | uri = xmlParseURI((const char *) val); |
| 6157 | if (uri == NULL) { |
| 6158 | if (ctxt->error != NULL) |
| 6159 | ctxt->error(ctxt->userData, |
| 6160 | "Attribute %s contains invalid URI %s\n", |
| 6161 | cur->name, val); |
| 6162 | ctxt->nbErrors++; |
| 6163 | } else { |
| 6164 | if (uri->scheme == NULL) { |
| 6165 | if (ctxt->error != NULL) |
| 6166 | ctxt->error(ctxt->userData, |
| 6167 | "Attribute %s URI %s is not absolute\n", |
| 6168 | cur->name, val); |
| 6169 | ctxt->nbErrors++; |
| 6170 | } |
| 6171 | if (uri->fragment != NULL) { |
| 6172 | if (ctxt->error != NULL) |
| 6173 | ctxt->error(ctxt->userData, |
| 6174 | "Attribute %s URI %s has a fragment ID\n", |
| 6175 | cur->name, val); |
| 6176 | ctxt->nbErrors++; |
| 6177 | } |
| 6178 | xmlFreeURI(uri); |
| 6179 | } |
| 6180 | } |
| 6181 | xmlFree(val); |
| 6182 | } |
| 6183 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6184 | if (ctxt->error != NULL) |
| 6185 | ctxt->error(ctxt->userData, |
| 6186 | "Unknown attribute %s on %s\n", |
| 6187 | cur->name, node->name); |
| 6188 | ctxt->nbErrors++; |
| 6189 | } |
| 6190 | } |
| 6191 | cur = next; |
| 6192 | } |
| 6193 | } |
| 6194 | |
| 6195 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6196 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6197 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6198 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6199 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6200 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6201 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6202 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6203 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6204 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6205 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6206 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6207 | delete = NULL; |
| 6208 | cur = root; |
| 6209 | while (cur != NULL) { |
| 6210 | if (delete != NULL) { |
| 6211 | xmlUnlinkNode(delete); |
| 6212 | xmlFreeNode(delete); |
| 6213 | delete = NULL; |
| 6214 | } |
| 6215 | if (cur->type == XML_ELEMENT_NODE) { |
| 6216 | /* |
| 6217 | * Simplification 4.1. Annotations |
| 6218 | */ |
| 6219 | if ((cur->ns == NULL) || |
| 6220 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6221 | if ((cur->parent != NULL) && |
| 6222 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6223 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6224 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6225 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6226 | if (ctxt->error != NULL) |
| 6227 | ctxt->error(ctxt->userData, |
| 6228 | "element %s doesn't allow foreign elements\n", |
| 6229 | cur->parent->name); |
| 6230 | ctxt->nbErrors++; |
| 6231 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6232 | delete = cur; |
| 6233 | goto skip_children; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6234 | } else { |
| 6235 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6236 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6237 | xmlChar *href, *ns, *base, *URL; |
| 6238 | xmlRelaxNGDocumentPtr docu; |
| 6239 | xmlNodePtr tmp; |
| 6240 | |
| 6241 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6242 | if (ns == NULL) { |
| 6243 | tmp = cur->parent; |
| 6244 | while ((tmp != NULL) && |
| 6245 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6246 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6247 | if (ns != NULL) |
| 6248 | break; |
| 6249 | tmp = tmp->parent; |
| 6250 | } |
| 6251 | } |
| 6252 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6253 | if (href == NULL) { |
| 6254 | if (ctxt->error != NULL) |
| 6255 | ctxt->error(ctxt->userData, |
| 6256 | "xmlRelaxNGParse: externalRef has no href attribute\n"); |
| 6257 | ctxt->nbErrors++; |
| 6258 | delete = cur; |
| 6259 | goto skip_children; |
| 6260 | } |
| 6261 | base = xmlNodeGetBase(cur->doc, cur); |
| 6262 | URL = xmlBuildURI(href, base); |
| 6263 | if (URL == NULL) { |
| 6264 | if (ctxt->error != NULL) |
| 6265 | ctxt->error(ctxt->userData, |
| 6266 | "Failed to compute URL for externalRef %s\n", href); |
| 6267 | ctxt->nbErrors++; |
| 6268 | if (href != NULL) |
| 6269 | xmlFree(href); |
| 6270 | if (base != NULL) |
| 6271 | xmlFree(base); |
| 6272 | delete = cur; |
| 6273 | goto skip_children; |
| 6274 | } |
| 6275 | if (href != NULL) |
| 6276 | xmlFree(href); |
| 6277 | if (base != NULL) |
| 6278 | xmlFree(base); |
| 6279 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6280 | if (docu == NULL) { |
| 6281 | if (ctxt->error != NULL) |
| 6282 | ctxt->error(ctxt->userData, |
| 6283 | "Failed to load externalRef %s\n", URL); |
| 6284 | ctxt->nbErrors++; |
| 6285 | xmlFree(URL); |
| 6286 | delete = cur; |
| 6287 | goto skip_children; |
| 6288 | } |
| 6289 | if (ns != NULL) |
| 6290 | xmlFree(ns); |
| 6291 | xmlFree(URL); |
| 6292 | cur->_private = docu; |
| 6293 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6294 | xmlChar *href, *ns, *base, *URL; |
| 6295 | xmlRelaxNGIncludePtr incl; |
| 6296 | xmlNodePtr tmp; |
| 6297 | |
| 6298 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6299 | if (href == NULL) { |
| 6300 | if (ctxt->error != NULL) |
| 6301 | ctxt->error(ctxt->userData, |
| 6302 | "xmlRelaxNGParse: include has no href attribute\n"); |
| 6303 | ctxt->nbErrors++; |
| 6304 | delete = cur; |
| 6305 | goto skip_children; |
| 6306 | } |
| 6307 | base = xmlNodeGetBase(cur->doc, cur); |
| 6308 | URL = xmlBuildURI(href, base); |
| 6309 | if (URL == NULL) { |
| 6310 | if (ctxt->error != NULL) |
| 6311 | ctxt->error(ctxt->userData, |
| 6312 | "Failed to compute URL for include %s\n", href); |
| 6313 | ctxt->nbErrors++; |
| 6314 | if (href != NULL) |
| 6315 | xmlFree(href); |
| 6316 | if (base != NULL) |
| 6317 | xmlFree(base); |
| 6318 | delete = cur; |
| 6319 | goto skip_children; |
| 6320 | } |
| 6321 | if (href != NULL) |
| 6322 | xmlFree(href); |
| 6323 | if (base != NULL) |
| 6324 | xmlFree(base); |
| 6325 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6326 | if (ns == NULL) { |
| 6327 | tmp = cur->parent; |
| 6328 | while ((tmp != NULL) && |
| 6329 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6330 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6331 | if (ns != NULL) |
| 6332 | break; |
| 6333 | tmp = tmp->parent; |
| 6334 | } |
| 6335 | } |
| 6336 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 6337 | if (ns != NULL) |
| 6338 | xmlFree(ns); |
| 6339 | if (incl == NULL) { |
| 6340 | if (ctxt->error != NULL) |
| 6341 | ctxt->error(ctxt->userData, |
| 6342 | "Failed to load include %s\n", URL); |
| 6343 | ctxt->nbErrors++; |
| 6344 | xmlFree(URL); |
| 6345 | delete = cur; |
| 6346 | goto skip_children; |
| 6347 | } |
| 6348 | xmlFree(URL); |
| 6349 | cur->_private = incl; |
| 6350 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 6351 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) { |
| 6352 | xmlChar *name, *ns; |
| 6353 | xmlNodePtr text = NULL; |
| 6354 | |
| 6355 | /* |
| 6356 | * Simplification 4.8. name attribute of element |
| 6357 | * and attribute elements |
| 6358 | */ |
| 6359 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 6360 | if (name != NULL) { |
| 6361 | if (cur->children == NULL) { |
| 6362 | text = xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 6363 | name); |
| 6364 | } else { |
| 6365 | xmlNodePtr node; |
| 6366 | node = xmlNewNode(cur->ns, BAD_CAST "name"); |
| 6367 | if (node != NULL) { |
| 6368 | xmlAddPrevSibling(cur->children, node); |
| 6369 | text = xmlNewText(name); |
| 6370 | xmlAddChild(node, text); |
| 6371 | text = node; |
| 6372 | } |
| 6373 | } |
| 6374 | if (text == NULL) { |
| 6375 | if (ctxt->error != NULL) |
| 6376 | ctxt->error(ctxt->userData, |
| 6377 | "Failed to create a name %s element\n", name); |
| 6378 | ctxt->nbErrors++; |
| 6379 | } |
| 6380 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 6381 | xmlFree(name); |
| 6382 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6383 | if (ns != NULL) { |
| 6384 | if (text != NULL) { |
| 6385 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 6386 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 6387 | } |
| 6388 | xmlFree(ns); |
| 6389 | } else if (xmlStrEqual(cur->name, |
| 6390 | BAD_CAST "attribute")) { |
| 6391 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 6392 | } |
| 6393 | } |
| 6394 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 6395 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 6396 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 6397 | /* |
| 6398 | * Simplification 4.8. name attribute of element |
| 6399 | * and attribute elements |
| 6400 | */ |
| 6401 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 6402 | xmlNodePtr node; |
| 6403 | xmlChar *ns = NULL; |
| 6404 | |
| 6405 | node = cur->parent; |
| 6406 | while ((node != NULL) && |
| 6407 | (node->type == XML_ELEMENT_NODE)) { |
| 6408 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 6409 | if (ns != NULL) { |
| 6410 | break; |
| 6411 | } |
| 6412 | node = node->parent; |
| 6413 | } |
| 6414 | if (ns == NULL) { |
| 6415 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 6416 | } else { |
| 6417 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 6418 | xmlFree(ns); |
| 6419 | } |
| 6420 | } |
| 6421 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6422 | xmlChar *name, *local, *prefix; |
| 6423 | |
| 6424 | /* |
| 6425 | * Simplification: 4.10. QNames |
| 6426 | */ |
| 6427 | name = xmlNodeGetContent(cur); |
| 6428 | if (name != NULL) { |
| 6429 | local = xmlSplitQName2(name, &prefix); |
| 6430 | if (local != NULL) { |
| 6431 | xmlNsPtr ns; |
| 6432 | |
| 6433 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 6434 | if (ns == NULL) { |
| 6435 | if (ctxt->error != NULL) |
| 6436 | ctxt->error(ctxt->userData, |
| 6437 | "xmlRelaxNGParse: no namespace for prefix %s\n", prefix); |
| 6438 | ctxt->nbErrors++; |
| 6439 | } else { |
| 6440 | xmlSetProp(cur, BAD_CAST "ns", ns->href); |
| 6441 | xmlNodeSetContent(cur, local); |
| 6442 | } |
| 6443 | xmlFree(local); |
| 6444 | xmlFree(prefix); |
| 6445 | } |
| 6446 | xmlFree(name); |
| 6447 | } |
| 6448 | } |
| 6449 | /* |
| 6450 | * 4.16 |
| 6451 | */ |
| 6452 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 6453 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 6454 | if (ctxt->error != NULL) |
| 6455 | ctxt->error(ctxt->userData, |
| 6456 | "Found nsName/except//nsName forbidden construct\n"); |
| 6457 | ctxt->nbErrors++; |
| 6458 | } |
| 6459 | } |
| 6460 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 6461 | (cur != root)) { |
| 6462 | int oldflags = ctxt->flags; |
| 6463 | |
| 6464 | /* |
| 6465 | * 4.16 |
| 6466 | */ |
| 6467 | if ((cur->parent != NULL) && |
| 6468 | (xmlStrEqual(cur->parent->name, BAD_CAST "anyName"))) { |
| 6469 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 6470 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 6471 | ctxt->flags = oldflags; |
| 6472 | goto skip_children; |
| 6473 | } else if ((cur->parent != NULL) && |
| 6474 | (xmlStrEqual(cur->parent->name, BAD_CAST "nsName"))) { |
| 6475 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 6476 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 6477 | ctxt->flags = oldflags; |
| 6478 | goto skip_children; |
| 6479 | } |
| 6480 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 6481 | /* |
| 6482 | * 4.16 |
| 6483 | */ |
| 6484 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 6485 | if (ctxt->error != NULL) |
| 6486 | ctxt->error(ctxt->userData, |
| 6487 | "Found anyName/except//anyName forbidden construct\n"); |
| 6488 | ctxt->nbErrors++; |
| 6489 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 6490 | if (ctxt->error != NULL) |
| 6491 | ctxt->error(ctxt->userData, |
| 6492 | "Found nsName/except//anyName forbidden construct\n"); |
| 6493 | ctxt->nbErrors++; |
| 6494 | } |
| 6495 | } |
| 6496 | /* |
| 6497 | * Thisd is not an else since "include" is transformed |
| 6498 | * into a div |
| 6499 | */ |
| 6500 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 6501 | xmlChar *ns; |
| 6502 | xmlNodePtr child, ins, tmp; |
| 6503 | |
| 6504 | /* |
| 6505 | * implements rule 4.11 |
| 6506 | */ |
| 6507 | |
| 6508 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6509 | |
| 6510 | child = cur->children; |
| 6511 | ins = cur; |
| 6512 | while (child != NULL) { |
| 6513 | if (ns != NULL) { |
| 6514 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 6515 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 6516 | } |
| 6517 | } |
| 6518 | tmp = child->next; |
| 6519 | xmlUnlinkNode(child); |
| 6520 | ins = xmlAddNextSibling(ins, child); |
| 6521 | child = tmp; |
| 6522 | } |
| 6523 | if (ns != NULL) |
| 6524 | xmlFree(ns); |
| 6525 | delete = cur; |
| 6526 | goto skip_children; |
| 6527 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6528 | } |
| 6529 | } |
| 6530 | /* |
| 6531 | * Simplification 4.2 whitespaces |
| 6532 | */ |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 6533 | else if ((cur->type == XML_TEXT_NODE) || |
| 6534 | (cur->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6535 | if (IS_BLANK_NODE(cur)) { |
| 6536 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 6537 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) && |
| 6538 | (!xmlStrEqual(cur->parent->name, BAD_CAST "param"))) |
| 6539 | delete = cur; |
| 6540 | } else { |
| 6541 | delete = cur; |
| 6542 | goto skip_children; |
| 6543 | } |
| 6544 | } |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 6545 | } else { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6546 | delete = cur; |
| 6547 | goto skip_children; |
| 6548 | } |
| 6549 | |
| 6550 | /* |
| 6551 | * Skip to next node |
| 6552 | */ |
| 6553 | if (cur->children != NULL) { |
| 6554 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 6555 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 6556 | (cur->children->type != XML_ENTITY_NODE)) { |
| 6557 | cur = cur->children; |
| 6558 | continue; |
| 6559 | } |
| 6560 | } |
| 6561 | skip_children: |
| 6562 | if (cur->next != NULL) { |
| 6563 | cur = cur->next; |
| 6564 | continue; |
| 6565 | } |
| 6566 | |
| 6567 | do { |
| 6568 | cur = cur->parent; |
| 6569 | if (cur == NULL) |
| 6570 | break; |
| 6571 | if (cur == root) { |
| 6572 | cur = NULL; |
| 6573 | break; |
| 6574 | } |
| 6575 | if (cur->next != NULL) { |
| 6576 | cur = cur->next; |
| 6577 | break; |
| 6578 | } |
| 6579 | } while (cur != NULL); |
| 6580 | } |
| 6581 | if (delete != NULL) { |
| 6582 | xmlUnlinkNode(delete); |
| 6583 | xmlFreeNode(delete); |
| 6584 | delete = NULL; |
| 6585 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6586 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6587 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6588 | /** |
| 6589 | * xmlRelaxNGCleanupDoc: |
| 6590 | * @ctxt: a Relax-NG parser context |
| 6591 | * @doc: an xmldocPtr document pointer |
| 6592 | * |
| 6593 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 6594 | * Include and externalRef lookups. |
| 6595 | * |
| 6596 | * Returns the cleaned up document or NULL in case of error |
| 6597 | */ |
| 6598 | static xmlDocPtr |
| 6599 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) { |
| 6600 | xmlNodePtr root; |
| 6601 | |
| 6602 | /* |
| 6603 | * Extract the root |
| 6604 | */ |
| 6605 | root = xmlDocGetRootElement(doc); |
| 6606 | if (root == NULL) { |
| 6607 | if (ctxt->error != NULL) |
| 6608 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 6609 | ctxt->URL); |
| 6610 | ctxt->nbErrors++; |
| 6611 | return (NULL); |
| 6612 | } |
| 6613 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6614 | return(doc); |
| 6615 | } |
| 6616 | |
| 6617 | /** |
| 6618 | * xmlRelaxNGParse: |
| 6619 | * @ctxt: a Relax-NG parser context |
| 6620 | * |
| 6621 | * parse a schema definition resource and build an internal |
| 6622 | * XML Shema struture which can be used to validate instances. |
| 6623 | * *WARNING* this interface is highly subject to change |
| 6624 | * |
| 6625 | * Returns the internal XML RelaxNG structure built from the resource or |
| 6626 | * NULL in case of error |
| 6627 | */ |
| 6628 | xmlRelaxNGPtr |
| 6629 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 6630 | { |
| 6631 | xmlRelaxNGPtr ret = NULL; |
| 6632 | xmlDocPtr doc; |
| 6633 | xmlNodePtr root; |
| 6634 | |
| 6635 | xmlRelaxNGInitTypes(); |
| 6636 | |
| 6637 | if (ctxt == NULL) |
| 6638 | return (NULL); |
| 6639 | |
| 6640 | /* |
| 6641 | * First step is to parse the input document into an DOM/Infoset |
| 6642 | */ |
| 6643 | if (ctxt->URL != NULL) { |
| 6644 | doc = xmlParseFile((const char *) ctxt->URL); |
| 6645 | if (doc == NULL) { |
| 6646 | if (ctxt->error != NULL) |
| 6647 | ctxt->error(ctxt->userData, |
| 6648 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL); |
| 6649 | ctxt->nbErrors++; |
| 6650 | return (NULL); |
| 6651 | } |
| 6652 | } else if (ctxt->buffer != NULL) { |
| 6653 | doc = xmlParseMemory(ctxt->buffer, ctxt->size); |
| 6654 | if (doc == NULL) { |
| 6655 | if (ctxt->error != NULL) |
| 6656 | ctxt->error(ctxt->userData, |
| 6657 | "xmlRelaxNGParse: could not parse schemas\n"); |
| 6658 | ctxt->nbErrors++; |
| 6659 | return (NULL); |
| 6660 | } |
| 6661 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 6662 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 6663 | } else { |
| 6664 | if (ctxt->error != NULL) |
| 6665 | ctxt->error(ctxt->userData, |
| 6666 | "xmlRelaxNGParse: nothing to parse\n"); |
| 6667 | ctxt->nbErrors++; |
| 6668 | return (NULL); |
| 6669 | } |
| 6670 | ctxt->document = doc; |
| 6671 | |
| 6672 | /* |
| 6673 | * Some preprocessing of the document content |
| 6674 | */ |
| 6675 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 6676 | if (doc == NULL) { |
| 6677 | xmlFreeDoc(ctxt->document); |
| 6678 | ctxt->document = NULL; |
| 6679 | return(NULL); |
| 6680 | } |
| 6681 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6682 | /* |
| 6683 | * Then do the parsing for good |
| 6684 | */ |
| 6685 | root = xmlDocGetRootElement(doc); |
| 6686 | if (root == NULL) { |
| 6687 | if (ctxt->error != NULL) |
| 6688 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 6689 | ctxt->URL); |
| 6690 | ctxt->nbErrors++; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6691 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6692 | return (NULL); |
| 6693 | } |
| 6694 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6695 | if (ret == NULL) { |
| 6696 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6697 | return(NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6698 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6699 | |
| 6700 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6701 | * Check the ref/defines links |
| 6702 | */ |
| 6703 | /* |
| 6704 | * try to preprocess interleaves |
| 6705 | */ |
| 6706 | if (ctxt->interleaves != NULL) { |
| 6707 | xmlHashScan(ctxt->interleaves, |
| 6708 | (xmlHashScanner)xmlRelaxNGComputeInterleaves, ctxt); |
| 6709 | } |
| 6710 | |
| 6711 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6712 | * if there was a parsing error return NULL |
| 6713 | */ |
| 6714 | if (ctxt->nbErrors > 0) { |
| 6715 | xmlRelaxNGFree(ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6716 | ctxt->document = NULL; |
| 6717 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6718 | return(NULL); |
| 6719 | } |
| 6720 | |
| 6721 | /* |
| 6722 | * Transfer the pointer for cleanup at the schema level. |
| 6723 | */ |
| 6724 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6725 | ctxt->document = NULL; |
| 6726 | ret->documents = ctxt->documents; |
| 6727 | ctxt->documents = NULL; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6728 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 6729 | ret->includes = ctxt->includes; |
| 6730 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6731 | ret->defNr = ctxt->defNr; |
| 6732 | ret->defTab = ctxt->defTab; |
| 6733 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 6734 | if (ctxt->idref == 1) |
| 6735 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6736 | |
| 6737 | return (ret); |
| 6738 | } |
| 6739 | |
| 6740 | /** |
| 6741 | * xmlRelaxNGSetParserErrors: |
| 6742 | * @ctxt: a Relax-NG validation context |
| 6743 | * @err: the error callback |
| 6744 | * @warn: the warning callback |
| 6745 | * @ctx: contextual data for the callbacks |
| 6746 | * |
| 6747 | * Set the callback functions used to handle errors for a validation context |
| 6748 | */ |
| 6749 | void |
| 6750 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
| 6751 | xmlRelaxNGValidityErrorFunc err, |
| 6752 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 6753 | if (ctxt == NULL) |
| 6754 | return; |
| 6755 | ctxt->error = err; |
| 6756 | ctxt->warning = warn; |
| 6757 | ctxt->userData = ctx; |
| 6758 | } |
| 6759 | /************************************************************************ |
| 6760 | * * |
| 6761 | * Dump back a compiled form * |
| 6762 | * * |
| 6763 | ************************************************************************/ |
| 6764 | static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define); |
| 6765 | |
| 6766 | /** |
| 6767 | * xmlRelaxNGDumpDefines: |
| 6768 | * @output: the file output |
| 6769 | * @defines: a list of define structures |
| 6770 | * |
| 6771 | * Dump a RelaxNG structure back |
| 6772 | */ |
| 6773 | static void |
| 6774 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) { |
| 6775 | while (defines != NULL) { |
| 6776 | xmlRelaxNGDumpDefine(output, defines); |
| 6777 | defines = defines->next; |
| 6778 | } |
| 6779 | } |
| 6780 | |
| 6781 | /** |
| 6782 | * xmlRelaxNGDumpDefine: |
| 6783 | * @output: the file output |
| 6784 | * @define: a define structure |
| 6785 | * |
| 6786 | * Dump a RelaxNG structure back |
| 6787 | */ |
| 6788 | static void |
| 6789 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) { |
| 6790 | if (define == NULL) |
| 6791 | return; |
| 6792 | switch(define->type) { |
| 6793 | case XML_RELAXNG_EMPTY: |
| 6794 | fprintf(output, "<empty/>\n"); |
| 6795 | break; |
| 6796 | case XML_RELAXNG_NOT_ALLOWED: |
| 6797 | fprintf(output, "<notAllowed/>\n"); |
| 6798 | break; |
| 6799 | case XML_RELAXNG_TEXT: |
| 6800 | fprintf(output, "<text/>\n"); |
| 6801 | break; |
| 6802 | case XML_RELAXNG_ELEMENT: |
| 6803 | fprintf(output, "<element>\n"); |
| 6804 | if (define->name != NULL) { |
| 6805 | fprintf(output, "<name"); |
| 6806 | if (define->ns != NULL) |
| 6807 | fprintf(output, " ns=\"%s\"", define->ns); |
| 6808 | fprintf(output, ">%s</name>\n", define->name); |
| 6809 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6810 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 6811 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6812 | fprintf(output, "</element>\n"); |
| 6813 | break; |
| 6814 | case XML_RELAXNG_LIST: |
| 6815 | fprintf(output, "<list>\n"); |
| 6816 | xmlRelaxNGDumpDefines(output, define->content); |
| 6817 | fprintf(output, "</list>\n"); |
| 6818 | break; |
| 6819 | case XML_RELAXNG_ONEORMORE: |
| 6820 | fprintf(output, "<oneOrMore>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6821 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6822 | fprintf(output, "</oneOrMore>\n"); |
| 6823 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6824 | case XML_RELAXNG_ZEROORMORE: |
| 6825 | fprintf(output, "<zeroOrMore>\n"); |
| 6826 | xmlRelaxNGDumpDefines(output, define->content); |
| 6827 | fprintf(output, "</zeroOrMore>\n"); |
| 6828 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6829 | case XML_RELAXNG_CHOICE: |
| 6830 | fprintf(output, "<choice>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6831 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6832 | fprintf(output, "</choice>\n"); |
| 6833 | break; |
| 6834 | case XML_RELAXNG_GROUP: |
| 6835 | fprintf(output, "<group>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6836 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6837 | fprintf(output, "</group>\n"); |
| 6838 | break; |
| 6839 | case XML_RELAXNG_INTERLEAVE: |
| 6840 | fprintf(output, "<interleave>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6841 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6842 | fprintf(output, "</interleave>\n"); |
| 6843 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6844 | case XML_RELAXNG_OPTIONAL: |
| 6845 | fprintf(output, "<optional>\n"); |
| 6846 | xmlRelaxNGDumpDefines(output, define->content); |
| 6847 | fprintf(output, "</optional>\n"); |
| 6848 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6849 | case XML_RELAXNG_ATTRIBUTE: |
| 6850 | fprintf(output, "<attribute>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6851 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6852 | fprintf(output, "</attribute>\n"); |
| 6853 | break; |
| 6854 | case XML_RELAXNG_DEF: |
| 6855 | fprintf(output, "<define"); |
| 6856 | if (define->name != NULL) |
| 6857 | fprintf(output, " name=\"%s\"", define->name); |
| 6858 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6859 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6860 | fprintf(output, "</define>\n"); |
| 6861 | break; |
| 6862 | case XML_RELAXNG_REF: |
| 6863 | fprintf(output, "<ref"); |
| 6864 | if (define->name != NULL) |
| 6865 | fprintf(output, " name=\"%s\"", define->name); |
| 6866 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6867 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6868 | fprintf(output, "</ref>\n"); |
| 6869 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6870 | case XML_RELAXNG_PARENTREF: |
| 6871 | fprintf(output, "<parentRef"); |
| 6872 | if (define->name != NULL) |
| 6873 | fprintf(output, " name=\"%s\"", define->name); |
| 6874 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6875 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6876 | fprintf(output, "</parentRef>\n"); |
| 6877 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6878 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 6879 | fprintf(output, "<externalRef>"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6880 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6881 | fprintf(output, "</externalRef>\n"); |
| 6882 | break; |
| 6883 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6884 | case XML_RELAXNG_VALUE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6885 | TODO |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6886 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6887 | case XML_RELAXNG_START: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6888 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 6889 | case XML_RELAXNG_PARAM: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6890 | TODO |
| 6891 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6892 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6893 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6894 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6895 | } |
| 6896 | } |
| 6897 | |
| 6898 | /** |
| 6899 | * xmlRelaxNGDumpGrammar: |
| 6900 | * @output: the file output |
| 6901 | * @grammar: a grammar structure |
| 6902 | * @top: is this a top grammar |
| 6903 | * |
| 6904 | * Dump a RelaxNG structure back |
| 6905 | */ |
| 6906 | static void |
| 6907 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 6908 | { |
| 6909 | if (grammar == NULL) |
| 6910 | return; |
| 6911 | |
| 6912 | fprintf(output, "<grammar"); |
| 6913 | if (top) |
| 6914 | fprintf(output, |
| 6915 | " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 6916 | switch(grammar->combine) { |
| 6917 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 6918 | break; |
| 6919 | case XML_RELAXNG_COMBINE_CHOICE: |
| 6920 | fprintf(output, " combine=\"choice\""); |
| 6921 | break; |
| 6922 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 6923 | fprintf(output, " combine=\"interleave\""); |
| 6924 | break; |
| 6925 | default: |
| 6926 | fprintf(output, " <!-- invalid combine value -->"); |
| 6927 | } |
| 6928 | fprintf(output, ">\n"); |
| 6929 | if (grammar->start == NULL) { |
| 6930 | fprintf(output, " <!-- grammar had no start -->"); |
| 6931 | } else { |
| 6932 | fprintf(output, "<start>\n"); |
| 6933 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 6934 | fprintf(output, "</start>\n"); |
| 6935 | } |
| 6936 | /* TODO ? Dump the defines ? */ |
| 6937 | fprintf(output, "</grammar>\n"); |
| 6938 | } |
| 6939 | |
| 6940 | /** |
| 6941 | * xmlRelaxNGDump: |
| 6942 | * @output: the file output |
| 6943 | * @schema: a schema structure |
| 6944 | * |
| 6945 | * Dump a RelaxNG structure back |
| 6946 | */ |
| 6947 | void |
| 6948 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 6949 | { |
| 6950 | if (schema == NULL) { |
| 6951 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 6952 | return; |
| 6953 | } |
| 6954 | fprintf(output, "RelaxNG: "); |
| 6955 | if (schema->doc == NULL) { |
| 6956 | fprintf(output, "no document\n"); |
| 6957 | } else if (schema->doc->URL != NULL) { |
| 6958 | fprintf(output, "%s\n", schema->doc->URL); |
| 6959 | } else { |
| 6960 | fprintf(output, "\n"); |
| 6961 | } |
| 6962 | if (schema->topgrammar == NULL) { |
| 6963 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 6964 | return; |
| 6965 | } |
| 6966 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 6967 | } |
| 6968 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 6969 | /** |
| 6970 | * xmlRelaxNGDumpTree: |
| 6971 | * @output: the file output |
| 6972 | * @schema: a schema structure |
| 6973 | * |
| 6974 | * Dump the transformed RelaxNG tree. |
| 6975 | */ |
| 6976 | void |
| 6977 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 6978 | { |
| 6979 | if (schema == NULL) { |
| 6980 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 6981 | return; |
| 6982 | } |
| 6983 | if (schema->doc == NULL) { |
| 6984 | fprintf(output, "no document\n"); |
| 6985 | } else { |
| 6986 | xmlDocDump(output, schema->doc); |
| 6987 | } |
| 6988 | } |
| 6989 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6990 | /************************************************************************ |
| 6991 | * * |
| 6992 | * Validation implementation * |
| 6993 | * * |
| 6994 | ************************************************************************/ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6995 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 6996 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 6997 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 6998 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6999 | |
| 7000 | /** |
| 7001 | * xmlRelaxNGSkipIgnored: |
| 7002 | * @ctxt: a schema validation context |
| 7003 | * @node: the top node. |
| 7004 | * |
| 7005 | * Skip ignorable nodes in that context |
| 7006 | * |
| 7007 | * Returns the new sibling or NULL in case of error. |
| 7008 | */ |
| 7009 | static xmlNodePtr |
| 7010 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 7011 | xmlNodePtr node) { |
| 7012 | /* |
| 7013 | * TODO complete and handle entities |
| 7014 | */ |
| 7015 | while ((node != NULL) && |
| 7016 | ((node->type == XML_COMMENT_NODE) || |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 7017 | (node->type == XML_PI_NODE) || |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7018 | (((node->type == XML_TEXT_NODE) || |
| 7019 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 7020 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 7021 | (IS_BLANK_NODE(node)))))) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7022 | node = node->next; |
| 7023 | } |
| 7024 | return(node); |
| 7025 | } |
| 7026 | |
| 7027 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7028 | * xmlRelaxNGNormalize: |
| 7029 | * @ctxt: a schema validation context |
| 7030 | * @str: the string to normalize |
| 7031 | * |
| 7032 | * Implements the normalizeWhiteSpace( s ) function from |
| 7033 | * section 6.2.9 of the spec |
| 7034 | * |
| 7035 | * Returns the new string or NULL in case of error. |
| 7036 | */ |
| 7037 | static xmlChar * |
| 7038 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) { |
| 7039 | xmlChar *ret, *p; |
| 7040 | const xmlChar *tmp; |
| 7041 | int len; |
| 7042 | |
| 7043 | if (str == NULL) |
| 7044 | return(NULL); |
| 7045 | tmp = str; |
| 7046 | while (*tmp != 0) tmp++; |
| 7047 | len = tmp - str; |
| 7048 | |
| 7049 | ret = (xmlChar *) xmlMalloc((len + 1) * sizeof(xmlChar)); |
| 7050 | if (ret == NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7051 | if (ctxt != NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7052 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7053 | } else { |
| 7054 | xmlGenericError(xmlGenericErrorContext, |
| 7055 | "xmlRelaxNGNormalize: out of memory\n"); |
| 7056 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7057 | return(NULL); |
| 7058 | } |
| 7059 | p = ret; |
| 7060 | while (IS_BLANK(*str)) str++; |
| 7061 | while (*str != 0) { |
| 7062 | if (IS_BLANK(*str)) { |
| 7063 | while (IS_BLANK(*str)) str++; |
| 7064 | if (*str == 0) |
| 7065 | break; |
| 7066 | *p++ = ' '; |
| 7067 | } else |
| 7068 | *p++ = *str++; |
| 7069 | } |
| 7070 | *p = 0; |
| 7071 | return(ret); |
| 7072 | } |
| 7073 | |
| 7074 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7075 | * xmlRelaxNGValidateDatatype: |
| 7076 | * @ctxt: a Relax-NG validation context |
| 7077 | * @value: the string value |
| 7078 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7079 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7080 | * |
| 7081 | * Validate the given value against the dataype |
| 7082 | * |
| 7083 | * Returns 0 if the validation succeeded or an error code. |
| 7084 | */ |
| 7085 | static int |
| 7086 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7087 | xmlRelaxNGDefinePtr define, xmlNodePtr node) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7088 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7089 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7090 | void *result = NULL; |
| 7091 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7092 | |
| 7093 | if ((define == NULL) || (define->data == NULL)) { |
| 7094 | return(-1); |
| 7095 | } |
| 7096 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7097 | if (lib->check != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7098 | if ((define->attrs != NULL) && |
| 7099 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7100 | ret = lib->check(lib->data, define->name, value, &result, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7101 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7102 | ret = lib->check(lib->data, define->name, value, NULL, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7103 | } |
| 7104 | } else |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7105 | ret = -1; |
| 7106 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7107 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7108 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 7109 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7110 | return(-1); |
| 7111 | } else if (ret == 1) { |
| 7112 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7113 | } else if (ret == 2) { |
| 7114 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7115 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7116 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7117 | ret = -1; |
| 7118 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7119 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7120 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
| 7121 | if (lib->facet != NULL) { |
| 7122 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 7123 | cur->value, value, result); |
| 7124 | if (tmp != 0) |
| 7125 | ret = -1; |
| 7126 | } |
| 7127 | cur = cur->next; |
| 7128 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7129 | if ((ret == 0) && (define->content != NULL)) { |
| 7130 | const xmlChar *oldvalue, *oldendvalue; |
| 7131 | |
| 7132 | oldvalue = ctxt->state->value; |
| 7133 | oldendvalue = ctxt->state->endvalue; |
| 7134 | ctxt->state->value = (xmlChar *) value; |
| 7135 | ctxt->state->endvalue = NULL; |
| 7136 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 7137 | ctxt->state->value = (xmlChar *) oldvalue; |
| 7138 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
| 7139 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 7140 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 7141 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 7142 | return(ret); |
| 7143 | } |
| 7144 | |
| 7145 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7146 | * xmlRelaxNGNextValue: |
| 7147 | * @ctxt: a Relax-NG validation context |
| 7148 | * |
| 7149 | * Skip to the next value when validating within a list |
| 7150 | * |
| 7151 | * Returns 0 if the operation succeeded or an error code. |
| 7152 | */ |
| 7153 | static int |
| 7154 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) { |
| 7155 | xmlChar *cur; |
| 7156 | |
| 7157 | cur = ctxt->state->value; |
| 7158 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
| 7159 | ctxt->state->value = NULL; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 7160 | ctxt->state->endvalue = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7161 | return(0); |
| 7162 | } |
| 7163 | while (*cur != 0) cur++; |
| 7164 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++; |
| 7165 | if (cur == ctxt->state->endvalue) |
| 7166 | ctxt->state->value = NULL; |
| 7167 | else |
| 7168 | ctxt->state->value = cur; |
| 7169 | return(0); |
| 7170 | } |
| 7171 | |
| 7172 | /** |
| 7173 | * xmlRelaxNGValidateValueList: |
| 7174 | * @ctxt: a Relax-NG validation context |
| 7175 | * @defines: the list of definitions to verify |
| 7176 | * |
| 7177 | * Validate the given set of definitions for the current value |
| 7178 | * |
| 7179 | * Returns 0 if the validation succeeded or an error code. |
| 7180 | */ |
| 7181 | static int |
| 7182 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7183 | xmlRelaxNGDefinePtr defines) { |
| 7184 | int ret = 0; |
| 7185 | |
| 7186 | while (defines != NULL) { |
| 7187 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 7188 | if (ret != 0) |
| 7189 | break; |
| 7190 | defines = defines->next; |
| 7191 | } |
| 7192 | return(ret); |
| 7193 | } |
| 7194 | |
| 7195 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7196 | * xmlRelaxNGValidateValue: |
| 7197 | * @ctxt: a Relax-NG validation context |
| 7198 | * @define: the definition to verify |
| 7199 | * |
| 7200 | * Validate the given definition for the current value |
| 7201 | * |
| 7202 | * Returns 0 if the validation succeeded or an error code. |
| 7203 | */ |
| 7204 | static int |
| 7205 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 7206 | xmlRelaxNGDefinePtr define) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7207 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7208 | xmlChar *value; |
| 7209 | |
| 7210 | value = ctxt->state->value; |
| 7211 | switch (define->type) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 7212 | case XML_RELAXNG_EMPTY: { |
| 7213 | if ((value != NULL) && (value[0] != 0)) { |
| 7214 | int idx = 0; |
| 7215 | |
| 7216 | while (IS_BLANK(value[idx])) |
| 7217 | idx++; |
| 7218 | if (value[idx] != 0) |
| 7219 | ret = -1; |
| 7220 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7221 | break; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 7222 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7223 | case XML_RELAXNG_TEXT: |
| 7224 | break; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7225 | case XML_RELAXNG_VALUE: { |
| 7226 | if (!xmlStrEqual(value, define->value)) { |
| 7227 | if (define->name != NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7228 | xmlRelaxNGTypeLibraryPtr lib; |
| 7229 | |
| 7230 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 7231 | if ((lib != NULL) && (lib->comp != NULL)) |
| 7232 | ret = lib->comp(lib->data, define->name, value, |
| 7233 | define->value); |
| 7234 | else |
| 7235 | ret = -1; |
| 7236 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7237 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, define->name); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7238 | return(-1); |
| 7239 | } else if (ret == 1) { |
| 7240 | ret = 0; |
| 7241 | } else { |
| 7242 | ret = -1; |
| 7243 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7244 | } else { |
| 7245 | xmlChar *nval, *nvalue; |
| 7246 | |
| 7247 | /* |
| 7248 | * TODO: trivial optimizations are possible by |
| 7249 | * computing at compile-time |
| 7250 | */ |
| 7251 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 7252 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
| 7253 | |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 7254 | if ((nval == NULL) || (nvalue == NULL) || |
| 7255 | (!xmlStrEqual(nval, nvalue))) |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7256 | ret = -1; |
| 7257 | if (nval != NULL) |
| 7258 | xmlFree(nval); |
| 7259 | if (nvalue != NULL) |
| 7260 | xmlFree(nvalue); |
| 7261 | } |
| 7262 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7263 | if (ret == 0) |
| 7264 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 7265 | break; |
| 7266 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7267 | case XML_RELAXNG_DATATYPE: { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7268 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 7269 | ctxt->state->seq); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7270 | if (ret == 0) |
| 7271 | xmlRelaxNGNextValue(ctxt); |
| 7272 | |
| 7273 | break; |
| 7274 | } |
| 7275 | case XML_RELAXNG_CHOICE: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7276 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7277 | xmlChar *oldvalue; |
| 7278 | |
| 7279 | oldflags = ctxt->flags; |
| 7280 | ctxt->flags |= FLAGS_IGNORABLE; |
| 7281 | |
| 7282 | oldvalue = ctxt->state->value; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7283 | while (list != NULL) { |
| 7284 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 7285 | if (ret == 0) { |
| 7286 | break; |
| 7287 | } |
| 7288 | ctxt->state->value = oldvalue; |
| 7289 | list = list->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7290 | } |
| 7291 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7292 | if (ret != 0) { |
| 7293 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7294 | xmlRelaxNGDumpValidError(ctxt); |
| 7295 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 7296 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7297 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7298 | if (ret == 0) |
| 7299 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7300 | break; |
| 7301 | } |
| 7302 | case XML_RELAXNG_LIST: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7303 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7304 | xmlChar *oldvalue, *oldend, *val, *cur; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7305 | #ifdef DEBUG_LIST |
| 7306 | int nb_values = 0; |
| 7307 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7308 | |
| 7309 | oldvalue = ctxt->state->value; |
| 7310 | oldend = ctxt->state->endvalue; |
| 7311 | |
| 7312 | val = xmlStrdup(oldvalue); |
| 7313 | if (val == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 7314 | val = xmlStrdup(BAD_CAST ""); |
| 7315 | } |
| 7316 | if (val == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7317 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7318 | return(-1); |
| 7319 | } |
| 7320 | cur = val; |
| 7321 | while (*cur != 0) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7322 | if (IS_BLANK(*cur)) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7323 | *cur = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7324 | cur++; |
| 7325 | #ifdef DEBUG_LIST |
| 7326 | nb_values++; |
| 7327 | #endif |
| 7328 | while (IS_BLANK(*cur)) |
| 7329 | *cur++ = 0; |
| 7330 | } else |
| 7331 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7332 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7333 | #ifdef DEBUG_LIST |
| 7334 | xmlGenericError(xmlGenericErrorContext, |
| 7335 | "list value: '%s' found %d items\n", oldvalue, nb_values); |
| 7336 | nb_values = 0; |
| 7337 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7338 | ctxt->state->endvalue = cur; |
| 7339 | cur = val; |
| 7340 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 7341 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7342 | ctxt->state->value = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 7343 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7344 | while (list != NULL) { |
| 7345 | if (ctxt->state->value == ctxt->state->endvalue) |
| 7346 | ctxt->state->value = NULL; |
| 7347 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 7348 | if (ret != 0) { |
| 7349 | #ifdef DEBUG_LIST |
| 7350 | xmlGenericError(xmlGenericErrorContext, |
| 7351 | "Failed to validate value: '%s' with %d rule\n", |
| 7352 | ctxt->state->value, nb_values); |
| 7353 | #endif |
| 7354 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 7355 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7356 | #ifdef DEBUG_LIST |
| 7357 | nb_values++; |
| 7358 | #endif |
| 7359 | list = list->next; |
| 7360 | } |
| 7361 | |
| 7362 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 7363 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 7364 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, ctxt->state->value); |
| 7365 | ret = -1; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7366 | } |
| 7367 | xmlFree(val); |
| 7368 | ctxt->state->value = oldvalue; |
| 7369 | ctxt->state->endvalue = oldend; |
| 7370 | break; |
| 7371 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7372 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7373 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 7374 | if (ret != 0) { |
| 7375 | break; |
| 7376 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7377 | /* no break on purpose */ |
| 7378 | case XML_RELAXNG_ZEROORMORE: { |
| 7379 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7380 | |
| 7381 | oldflags = ctxt->flags; |
| 7382 | ctxt->flags |= FLAGS_IGNORABLE; |
| 7383 | cur = ctxt->state->value; |
| 7384 | temp = NULL; |
| 7385 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 7386 | (temp != cur)) { |
| 7387 | temp = cur; |
| 7388 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 7389 | if (ret != 0) { |
| 7390 | ctxt->state->value = temp; |
| 7391 | ret = 0; |
| 7392 | break; |
| 7393 | } |
| 7394 | cur = ctxt->state->value; |
| 7395 | } |
| 7396 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7397 | if (ret != 0) { |
| 7398 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7399 | xmlRelaxNGDumpValidError(ctxt); |
| 7400 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 7401 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 7402 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 7403 | break; |
| 7404 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7405 | case XML_RELAXNG_EXCEPT: { |
| 7406 | xmlRelaxNGDefinePtr list; |
| 7407 | |
| 7408 | list = define->content; |
| 7409 | while (list != NULL) { |
| 7410 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 7411 | if (ret == 0) { |
| 7412 | ret = -1; |
| 7413 | break; |
| 7414 | } else |
| 7415 | ret = 0; |
| 7416 | list = list->next; |
| 7417 | } |
| 7418 | break; |
| 7419 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 7420 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7421 | case XML_RELAXNG_GROUP: { |
| 7422 | xmlRelaxNGDefinePtr list; |
| 7423 | |
| 7424 | list = define->content; |
| 7425 | while (list != NULL) { |
| 7426 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 7427 | if (ret != 0) { |
| 7428 | ret = -1; |
| 7429 | break; |
| 7430 | } else |
| 7431 | ret = 0; |
| 7432 | list = list->next; |
| 7433 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 7434 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7435 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 7436 | case XML_RELAXNG_REF: |
| 7437 | case XML_RELAXNG_PARENTREF: |
| 7438 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 7439 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7440 | default: |
| 7441 | TODO |
| 7442 | ret = -1; |
| 7443 | } |
| 7444 | return(ret); |
| 7445 | } |
| 7446 | |
| 7447 | /** |
| 7448 | * xmlRelaxNGValidateValueContent: |
| 7449 | * @ctxt: a Relax-NG validation context |
| 7450 | * @defines: the list of definitions to verify |
| 7451 | * |
| 7452 | * Validate the given definitions for the current value |
| 7453 | * |
| 7454 | * Returns 0 if the validation succeeded or an error code. |
| 7455 | */ |
| 7456 | static int |
| 7457 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 7458 | xmlRelaxNGDefinePtr defines) { |
| 7459 | int ret = 0; |
| 7460 | |
| 7461 | while (defines != NULL) { |
| 7462 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 7463 | if (ret != 0) |
| 7464 | break; |
| 7465 | defines = defines->next; |
| 7466 | } |
| 7467 | return(ret); |
| 7468 | } |
| 7469 | |
| 7470 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7471 | * xmlRelaxNGAttributeMatch: |
| 7472 | * @ctxt: a Relax-NG validation context |
| 7473 | * @define: the definition to check |
| 7474 | * @prop: the attribute |
| 7475 | * |
| 7476 | * Check if the attribute matches the definition nameClass |
| 7477 | * |
| 7478 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 7479 | */ |
| 7480 | static int |
| 7481 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 7482 | xmlRelaxNGDefinePtr define, |
| 7483 | xmlAttrPtr prop) { |
| 7484 | int ret; |
| 7485 | |
| 7486 | if (define->name != NULL) { |
| 7487 | if (!xmlStrEqual(define->name, prop->name)) |
| 7488 | return(0); |
| 7489 | } |
| 7490 | if (define->ns != NULL) { |
| 7491 | if (define->ns[0] == 0) { |
| 7492 | if (prop->ns != NULL) |
| 7493 | return(0); |
| 7494 | } else { |
| 7495 | if ((prop->ns == NULL) || |
| 7496 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 7497 | return(0); |
| 7498 | } |
| 7499 | } |
| 7500 | if (define->nameClass == NULL) |
| 7501 | return(1); |
| 7502 | define = define->nameClass; |
| 7503 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 7504 | xmlRelaxNGDefinePtr list; |
| 7505 | |
| 7506 | list = define->content; |
| 7507 | while (list != NULL) { |
| 7508 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 7509 | if (ret == 1) |
| 7510 | return(0); |
| 7511 | if (ret < 0) |
| 7512 | return(ret); |
| 7513 | list = list->next; |
| 7514 | } |
| 7515 | } else { |
| 7516 | TODO |
| 7517 | } |
| 7518 | return(1); |
| 7519 | } |
| 7520 | |
| 7521 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7522 | * xmlRelaxNGValidateAttribute: |
| 7523 | * @ctxt: a Relax-NG validation context |
| 7524 | * @define: the definition to verify |
| 7525 | * |
| 7526 | * Validate the given attribute definition for that node |
| 7527 | * |
| 7528 | * Returns 0 if the validation succeeded or an error code. |
| 7529 | */ |
| 7530 | static int |
| 7531 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 7532 | xmlRelaxNGDefinePtr define) { |
| 7533 | int ret = 0, i; |
| 7534 | xmlChar *value, *oldvalue; |
| 7535 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7536 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7537 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 7538 | if (ctxt->state->nbAttrLeft <= 0) |
| 7539 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7540 | if (define->name != NULL) { |
| 7541 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 7542 | tmp = ctxt->state->attrs[i]; |
| 7543 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 7544 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 7545 | (tmp->ns == NULL)) || |
| 7546 | ((tmp->ns != NULL) && |
| 7547 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 7548 | prop = tmp; |
| 7549 | break; |
| 7550 | } |
| 7551 | } |
| 7552 | } |
| 7553 | if (prop != NULL) { |
| 7554 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 7555 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7556 | oldseq = ctxt->state->seq; |
| 7557 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7558 | ctxt->state->value = value; |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 7559 | ctxt->state->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7560 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 7561 | if (ctxt->state->value != NULL) |
| 7562 | value = ctxt->state->value; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7563 | if (value != NULL) |
| 7564 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 7565 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7566 | ctxt->state->seq = oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7567 | if (ret == 0) { |
| 7568 | /* |
| 7569 | * flag the attribute as processed |
| 7570 | */ |
| 7571 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 7572 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7573 | } |
| 7574 | } else { |
| 7575 | ret = -1; |
| 7576 | } |
| 7577 | #ifdef DEBUG |
| 7578 | xmlGenericError(xmlGenericErrorContext, |
| 7579 | "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret); |
| 7580 | #endif |
| 7581 | } else { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7582 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 7583 | tmp = ctxt->state->attrs[i]; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 7584 | if ((tmp != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7585 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7586 | prop = tmp; |
| 7587 | break; |
| 7588 | } |
| 7589 | } |
| 7590 | if (prop != NULL) { |
| 7591 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 7592 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7593 | oldseq = ctxt->state->seq; |
| 7594 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7595 | ctxt->state->value = value; |
| 7596 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 7597 | if (ctxt->state->value != NULL) |
| 7598 | value = ctxt->state->value; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7599 | if (value != NULL) |
| 7600 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 7601 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7602 | ctxt->state->seq = oldseq; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7603 | if (ret == 0) { |
| 7604 | /* |
| 7605 | * flag the attribute as processed |
| 7606 | */ |
| 7607 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 7608 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7609 | } |
| 7610 | } else { |
| 7611 | ret = -1; |
| 7612 | } |
| 7613 | #ifdef DEBUG |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 7614 | if (define->ns != NULL) { |
| 7615 | xmlGenericError(xmlGenericErrorContext, |
| 7616 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 7617 | define->ns, ret); |
| 7618 | } else { |
| 7619 | xmlGenericError(xmlGenericErrorContext, |
| 7620 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 7621 | ret); |
| 7622 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 7623 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7624 | } |
| 7625 | |
| 7626 | return(ret); |
| 7627 | } |
| 7628 | |
| 7629 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7630 | * xmlRelaxNGValidateAttributeList: |
| 7631 | * @ctxt: a Relax-NG validation context |
| 7632 | * @define: the list of definition to verify |
| 7633 | * |
| 7634 | * Validate the given node against the list of attribute definitions |
| 7635 | * |
| 7636 | * Returns 0 if the validation succeeded or an error code. |
| 7637 | */ |
| 7638 | static int |
| 7639 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7640 | xmlRelaxNGDefinePtr defines) { |
| 7641 | int ret = 0; |
| 7642 | while (defines != NULL) { |
| 7643 | if (xmlRelaxNGValidateAttribute(ctxt, defines) != 0) |
| 7644 | ret = -1; |
| 7645 | defines = defines->next; |
| 7646 | } |
| 7647 | return(ret); |
| 7648 | } |
| 7649 | |
| 7650 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 7651 | * xmlRelaxNGNodeMatchesList: |
| 7652 | * @node: the node |
| 7653 | * @list: a NULL terminated array of definitions |
| 7654 | * |
| 7655 | * Check if a node can be matched by one of the definitions |
| 7656 | * |
| 7657 | * Returns 1 if matches 0 otherwise |
| 7658 | */ |
| 7659 | static int |
| 7660 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) { |
| 7661 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 7662 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 7663 | |
| 7664 | if ((node == NULL) || (list == NULL)) |
| 7665 | return(0); |
| 7666 | |
| 7667 | cur = list[i++]; |
| 7668 | while (cur != NULL) { |
| 7669 | if ((node->type == XML_ELEMENT_NODE) && |
| 7670 | (cur->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 7671 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 7672 | if (tmp == 1) |
| 7673 | return(1); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7674 | } else if (((node->type == XML_TEXT_NODE) || |
| 7675 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 7676 | (cur->type == XML_RELAXNG_TEXT)) { |
| 7677 | return(1); |
| 7678 | } |
| 7679 | cur = list[i++]; |
| 7680 | } |
| 7681 | return(0); |
| 7682 | } |
| 7683 | |
| 7684 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7685 | * xmlRelaxNGValidateInterleave: |
| 7686 | * @ctxt: a Relax-NG validation context |
| 7687 | * @define: the definition to verify |
| 7688 | * |
| 7689 | * Validate an interleave definition for a node. |
| 7690 | * |
| 7691 | * Returns 0 if the validation succeeded or an error code. |
| 7692 | */ |
| 7693 | static int |
| 7694 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 7695 | xmlRelaxNGDefinePtr define) { |
| 7696 | int ret = 0, i, nbgroups, left; |
| 7697 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 7698 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7699 | |
| 7700 | xmlRelaxNGValidStatePtr oldstate; |
| 7701 | xmlRelaxNGPartitionPtr partitions; |
| 7702 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 7703 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 7704 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 7705 | |
| 7706 | if (define->data != NULL) { |
| 7707 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 7708 | nbgroups = partitions->nbgroups; |
| 7709 | left = nbgroups; |
| 7710 | } else { |
| 7711 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 7712 | return(-1); |
| 7713 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 7714 | /* |
| 7715 | * Optimizations for MIXED |
| 7716 | */ |
| 7717 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 7718 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 7719 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 7720 | if (nbgroups == 2) { |
| 7721 | /* |
| 7722 | * this is a pure <mixed> case |
| 7723 | */ |
| 7724 | if (ctxt->state != NULL) |
| 7725 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 7726 | ctxt->state->seq); |
| 7727 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 7728 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 7729 | partitions->groups[1]->rule); |
| 7730 | else |
| 7731 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 7732 | partitions->groups[0]->rule); |
| 7733 | if (ret == 0) { |
| 7734 | if (ctxt->state != NULL) |
| 7735 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 7736 | ctxt->state->seq); |
| 7737 | } |
| 7738 | ctxt->flags = oldflags; |
| 7739 | return(ret); |
| 7740 | } |
| 7741 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7742 | |
| 7743 | /* |
| 7744 | * Build arrays to store the first and last node of the chain |
| 7745 | * pertaining to each group |
| 7746 | */ |
| 7747 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 7748 | if (list == NULL) { |
| 7749 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7750 | return(-1); |
| 7751 | } |
| 7752 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 7753 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 7754 | if (lasts == NULL) { |
| 7755 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7756 | return(-1); |
| 7757 | } |
| 7758 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 7759 | |
| 7760 | /* |
| 7761 | * Walk the sequence of children finding the right group and |
| 7762 | * sorting them in sequences. |
| 7763 | */ |
| 7764 | cur = ctxt->state->seq; |
| 7765 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 7766 | start = cur; |
| 7767 | while (cur != NULL) { |
| 7768 | ctxt->state->seq = cur; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 7769 | if ((partitions->triage != NULL) && |
| 7770 | (partitions->flags & IS_DETERMINIST)) { |
| 7771 | void *tmp = NULL; |
| 7772 | |
| 7773 | if ((cur->type == XML_TEXT_NODE) || |
| 7774 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 7775 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 7776 | NULL); |
| 7777 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 7778 | if (cur->ns != NULL) { |
| 7779 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 7780 | cur->ns->href); |
| 7781 | if (tmp == NULL) |
| 7782 | tmp = xmlHashLookup2(partitions->triage, |
| 7783 | BAD_CAST "#any", cur->ns->href); |
| 7784 | } else |
| 7785 | tmp = xmlHashLookup2(partitions->triage, cur->name, NULL); |
| 7786 | if (tmp == NULL) |
| 7787 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 7788 | NULL); |
| 7789 | } |
| 7790 | |
| 7791 | if (tmp == NULL) { |
| 7792 | i = nbgroups; |
| 7793 | } else { |
| 7794 | i = ((long) tmp) - 1; |
| 7795 | if (partitions->flags & IS_NEEDCHECK) { |
| 7796 | group = partitions->groups[i]; |
| 7797 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 7798 | i = nbgroups; |
| 7799 | } |
| 7800 | } |
| 7801 | } else { |
| 7802 | for (i = 0;i < nbgroups;i++) { |
| 7803 | group = partitions->groups[i]; |
| 7804 | if (group == NULL) |
| 7805 | continue; |
| 7806 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 7807 | break; |
| 7808 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7809 | } |
| 7810 | /* |
| 7811 | * We break as soon as an element not matched is found |
| 7812 | */ |
| 7813 | if (i >= nbgroups) { |
| 7814 | break; |
| 7815 | } |
| 7816 | if (lasts[i] != NULL) { |
| 7817 | lasts[i]->next = cur; |
| 7818 | lasts[i] = cur; |
| 7819 | } else { |
| 7820 | list[i] = cur; |
| 7821 | lasts[i] = cur; |
| 7822 | } |
| 7823 | if (cur->next != NULL) |
| 7824 | lastchg = cur->next; |
| 7825 | else |
| 7826 | lastchg = cur; |
| 7827 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
| 7828 | } |
| 7829 | if (ret != 0) { |
| 7830 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 7831 | ret = -1; |
| 7832 | goto done; |
| 7833 | } |
| 7834 | lastelem = cur; |
| 7835 | oldstate = ctxt->state; |
| 7836 | for (i = 0;i < nbgroups;i++) { |
| 7837 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 7838 | group = partitions->groups[i]; |
| 7839 | if (lasts[i] != NULL) { |
| 7840 | last = lasts[i]->next; |
| 7841 | lasts[i]->next = NULL; |
| 7842 | } |
| 7843 | ctxt->state->seq = list[i]; |
| 7844 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 7845 | if (ret != 0) |
| 7846 | break; |
| 7847 | if (ctxt->state != NULL) { |
| 7848 | cur = ctxt->state->seq; |
| 7849 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 7850 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7851 | oldstate = ctxt->state; |
| 7852 | ctxt->state = NULL; |
| 7853 | if (cur != NULL) { |
| 7854 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 7855 | ret = -1; |
| 7856 | ctxt->state = oldstate; |
| 7857 | goto done; |
| 7858 | } |
| 7859 | } else if (ctxt->states != NULL) { |
| 7860 | int j; |
| 7861 | int found = 0; |
| 7862 | |
| 7863 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 7864 | cur = ctxt->states->tabState[j]->seq; |
| 7865 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 7866 | if (cur == NULL) { |
| 7867 | found = 1; |
| 7868 | break; |
| 7869 | } |
| 7870 | } |
| 7871 | if (ctxt->states->nbState > 0) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 7872 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7873 | oldstate = ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 7874 | } |
| 7875 | for (j = 0;j < ctxt->states->nbState - 1;j++) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 7876 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[j]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7877 | } |
| 7878 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 7879 | ctxt->states = NULL; |
| 7880 | if (found == 0) { |
| 7881 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 7882 | ret = -1; |
| 7883 | ctxt->state = oldstate; |
| 7884 | goto done; |
| 7885 | } |
| 7886 | } else { |
| 7887 | ret = -1; |
| 7888 | break; |
| 7889 | } |
| 7890 | if (lasts[i] != NULL) { |
| 7891 | lasts[i]->next = last; |
| 7892 | } |
| 7893 | } |
| 7894 | if (ctxt->state != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 7895 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7896 | ctxt->state = oldstate; |
| 7897 | ctxt->state->seq = lastelem; |
| 7898 | if (ret != 0) { |
| 7899 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 7900 | ret = -1; |
| 7901 | goto done; |
| 7902 | } |
| 7903 | |
| 7904 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 7905 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7906 | /* |
| 7907 | * builds the next links chain from the prev one |
| 7908 | */ |
| 7909 | cur = lastchg; |
| 7910 | while (cur != NULL) { |
| 7911 | if ((cur == start) || (cur->prev == NULL)) |
| 7912 | break; |
| 7913 | cur->prev->next = cur; |
| 7914 | cur = cur->prev; |
| 7915 | } |
| 7916 | if (ret == 0) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 7917 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7918 | } |
| 7919 | |
| 7920 | xmlFree(list); |
| 7921 | xmlFree(lasts); |
| 7922 | return(ret); |
| 7923 | } |
| 7924 | |
| 7925 | /** |
| 7926 | * xmlRelaxNGValidateDefinitionList: |
| 7927 | * @ctxt: a Relax-NG validation context |
| 7928 | * @define: the list of definition to verify |
| 7929 | * |
| 7930 | * Validate the given node content against the (list) of definitions |
| 7931 | * |
| 7932 | * Returns 0 if the validation succeeded or an error code. |
| 7933 | */ |
| 7934 | static int |
| 7935 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7936 | xmlRelaxNGDefinePtr defines) { |
| 7937 | int ret = 0, res; |
| 7938 | |
| 7939 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 7940 | if (defines == NULL) { |
| 7941 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, BAD_CAST "NULL definition list"); |
| 7942 | return(-1); |
| 7943 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7944 | while (defines != NULL) { |
| 7945 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 7946 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 7947 | if (res < 0) |
| 7948 | ret = -1; |
| 7949 | } else { |
| 7950 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 7951 | return(-1); |
| 7952 | } |
| 7953 | if (ret < 0) |
| 7954 | break; |
| 7955 | defines = defines->next; |
| 7956 | } |
| 7957 | |
| 7958 | return(ret); |
| 7959 | } |
| 7960 | |
| 7961 | /** |
| 7962 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7963 | * @ctxt: a Relax-NG validation context |
| 7964 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7965 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7966 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7967 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7968 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7969 | * 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] | 7970 | */ |
| 7971 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7972 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 7973 | xmlRelaxNGDefinePtr define, |
| 7974 | xmlNodePtr elem) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 7975 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7976 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7977 | if (define->name != NULL) { |
| 7978 | if (!xmlStrEqual(elem->name, define->name)) { |
| 7979 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 7980 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 7981 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7982 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7983 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
| 7984 | if (elem->ns == NULL) { |
| 7985 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, |
| 7986 | elem->name); |
| 7987 | return(0); |
| 7988 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 7989 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 7990 | elem->name, define->ns); |
| 7991 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 7992 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7993 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
| 7994 | (define->name == NULL)) { |
| 7995 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 7996 | elem->name); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 7997 | return(0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7998 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
| 7999 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 8000 | define->name); |
| 8001 | return(0); |
| 8002 | } |
| 8003 | |
| 8004 | if (define->nameClass == NULL) |
| 8005 | return(1); |
| 8006 | |
| 8007 | define = define->nameClass; |
| 8008 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 8009 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8010 | if (ctxt != NULL) { |
| 8011 | oldflags = ctxt->flags; |
| 8012 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8013 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8014 | |
| 8015 | list = define->content; |
| 8016 | while (list != NULL) { |
| 8017 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 8018 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8019 | if (ctxt != NULL) |
| 8020 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8021 | return(0); |
| 8022 | } |
| 8023 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8024 | if (ctxt != NULL) |
| 8025 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8026 | return(ret); |
| 8027 | } |
| 8028 | list = list->next; |
| 8029 | } |
| 8030 | ret = 1; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8031 | if (ctxt != NULL) { |
| 8032 | ctxt->flags = oldflags; |
| 8033 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8034 | } else if (define->type == XML_RELAXNG_CHOICE) { |
| 8035 | xmlRelaxNGDefinePtr list; |
| 8036 | |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8037 | if (ctxt != NULL) { |
| 8038 | oldflags = ctxt->flags; |
| 8039 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8040 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8041 | |
| 8042 | list = define->nameClass; |
| 8043 | while (list != NULL) { |
| 8044 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 8045 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8046 | if (ctxt != NULL) |
| 8047 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8048 | return(1); |
| 8049 | } |
| 8050 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8051 | if (ctxt != NULL) |
| 8052 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8053 | return(ret); |
| 8054 | } |
| 8055 | list = list->next; |
| 8056 | } |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8057 | if (ctxt != NULL) { |
| 8058 | if (ret != 0) { |
| 8059 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8060 | xmlRelaxNGDumpValidError(ctxt); |
| 8061 | } else { |
| 8062 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
| 8063 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8064 | } |
| 8065 | ret = 0; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8066 | if (ctxt != NULL) { |
| 8067 | ctxt->flags = oldflags; |
| 8068 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8069 | } else { |
| 8070 | TODO |
| 8071 | ret = -1; |
| 8072 | } |
| 8073 | return(ret); |
| 8074 | } |
| 8075 | |
| 8076 | /** |
| 8077 | * xmlRelaxNGValidateElementEnd: |
| 8078 | * @ctxt: a Relax-NG validation context |
| 8079 | * |
| 8080 | * Validate the end of the element, implements check that |
| 8081 | * there is nothing left not consumed in the element content |
| 8082 | * or in the attribute list. |
| 8083 | * |
| 8084 | * Returns 0 if the validation succeeded or an error code. |
| 8085 | */ |
| 8086 | static int |
| 8087 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt) { |
| 8088 | int ret = 0, i; |
| 8089 | xmlRelaxNGValidStatePtr state; |
| 8090 | |
| 8091 | state = ctxt->state; |
| 8092 | if (state->seq != NULL) { |
| 8093 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 8094 | if (state->seq != NULL) { |
| 8095 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 8096 | state->node->name, state->seq->name); |
| 8097 | ret = -1; |
| 8098 | } |
| 8099 | } |
| 8100 | for (i = 0;i < state->nbAttrs;i++) { |
| 8101 | if (state->attrs[i] != NULL) { |
| 8102 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 8103 | state->attrs[i]->name, state->node->name); |
| 8104 | ret = -1; |
| 8105 | } |
| 8106 | } |
| 8107 | return(ret); |
| 8108 | } |
| 8109 | |
| 8110 | /** |
| 8111 | * xmlRelaxNGValidateState: |
| 8112 | * @ctxt: a Relax-NG validation context |
| 8113 | * @define: the definition to verify |
| 8114 | * |
| 8115 | * Validate the current state against the definition |
| 8116 | * |
| 8117 | * Returns 0 if the validation succeeded or an error code. |
| 8118 | */ |
| 8119 | static int |
| 8120 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 8121 | xmlRelaxNGDefinePtr define) { |
| 8122 | xmlNodePtr node; |
| 8123 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 8124 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8125 | |
| 8126 | if (define == NULL) { |
| 8127 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 8128 | return(-1); |
| 8129 | } |
| 8130 | |
| 8131 | if (ctxt->state != NULL) { |
| 8132 | node = ctxt->state->seq; |
| 8133 | } else { |
| 8134 | node = NULL; |
| 8135 | } |
| 8136 | #ifdef DEBUG |
| 8137 | for (i = 0;i < ctxt->depth;i++) |
| 8138 | xmlGenericError(xmlGenericErrorContext, " "); |
| 8139 | xmlGenericError(xmlGenericErrorContext, |
| 8140 | "Start validating %s ", xmlRelaxNGDefName(define)); |
| 8141 | if (define->name != NULL) |
| 8142 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
| 8143 | if ((node != NULL) && (node->name != NULL)) |
| 8144 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
| 8145 | else |
| 8146 | xmlGenericError(xmlGenericErrorContext, "\n"); |
| 8147 | #endif |
| 8148 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8149 | switch (define->type) { |
| 8150 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8151 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 8152 | ret = 0; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8153 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8154 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8155 | ret = -1; |
| 8156 | break; |
| 8157 | case XML_RELAXNG_TEXT: |
| 8158 | while ((node != NULL) && |
| 8159 | ((node->type == XML_TEXT_NODE) || |
| 8160 | (node->type == XML_COMMENT_NODE) || |
| 8161 | (node->type == XML_PI_NODE) || |
| 8162 | (node->type == XML_CDATA_SECTION_NODE))) |
| 8163 | node = node->next; |
| 8164 | ctxt->state->seq = node; |
| 8165 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8166 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8167 | errNr = ctxt->errNr; |
| 8168 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 8169 | if (node == NULL) { |
| 8170 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 8171 | ret = -1; |
| 8172 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8173 | xmlRelaxNGDumpValidError(ctxt); |
| 8174 | break; |
| 8175 | } |
| 8176 | if (node->type != XML_ELEMENT_NODE) { |
| 8177 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 8178 | ret = -1; |
| 8179 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8180 | xmlRelaxNGDumpValidError(ctxt); |
| 8181 | break; |
| 8182 | } |
| 8183 | /* |
| 8184 | * This node was already validated successfully against |
| 8185 | * this definition. |
| 8186 | */ |
| 8187 | if (node->_private == define) { |
| 8188 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8189 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8190 | if (ctxt->errNr != 0) { |
| 8191 | while ((ctxt->err != NULL) && |
| 8192 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 8193 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8194 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 8195 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8196 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 8197 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 8198 | xmlRelaxNGValidErrorPop(ctxt); |
| 8199 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8200 | break; |
| 8201 | } |
| 8202 | |
| 8203 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 8204 | if (ret <= 0) { |
| 8205 | ret = -1; |
| 8206 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8207 | xmlRelaxNGDumpValidError(ctxt); |
| 8208 | break; |
| 8209 | } |
| 8210 | ret = 0; |
| 8211 | if (ctxt->errNr != 0) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8212 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8213 | while ((ctxt->err != NULL) && |
| 8214 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 8215 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8216 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 8217 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8218 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 8219 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 8220 | xmlRelaxNGValidErrorPop(ctxt); |
| 8221 | } |
| 8222 | errNr = ctxt->errNr; |
| 8223 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8224 | oldflags = ctxt->flags; |
| 8225 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 8226 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 8227 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8228 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 8229 | if (state == NULL) { |
| 8230 | ret = -1; |
| 8231 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8232 | xmlRelaxNGDumpValidError(ctxt); |
| 8233 | break; |
| 8234 | } |
| 8235 | |
| 8236 | oldstate = ctxt->state; |
| 8237 | ctxt->state = state; |
| 8238 | if (define->attrs != NULL) { |
| 8239 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 8240 | if (tmp != 0) { |
| 8241 | ret = -1; |
| 8242 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 8243 | } |
| 8244 | } |
| 8245 | if (define->content != NULL) { |
| 8246 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 8247 | if (tmp != 0) { |
| 8248 | ret = -1; |
| 8249 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, node->name); |
| 8250 | } |
| 8251 | } |
| 8252 | if (ctxt->states != NULL) { |
| 8253 | tmp = -1; |
| 8254 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8255 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8256 | |
| 8257 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 8258 | state = ctxt->states->tabState[i]; |
| 8259 | ctxt->state = state; |
| 8260 | |
| 8261 | if (xmlRelaxNGValidateElementEnd(ctxt) == 0) |
| 8262 | tmp = 0; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8263 | xmlRelaxNGFreeValidState(ctxt,state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8264 | } |
| 8265 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8266 | ctxt->flags = oldflags; |
| 8267 | ctxt->states = NULL; |
| 8268 | if ((ret == 0) && (tmp == -1)) |
| 8269 | ret = -1; |
| 8270 | } else { |
| 8271 | state = ctxt->state; |
| 8272 | if (ret == 0) |
| 8273 | ret = xmlRelaxNGValidateElementEnd(ctxt); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8274 | xmlRelaxNGFreeValidState(ctxt,state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8275 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8276 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8277 | ctxt->state = oldstate; |
| 8278 | if (oldstate != NULL) |
| 8279 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 8280 | if (ret == 0) { |
| 8281 | node->_private = define; |
| 8282 | } |
| 8283 | if (ret != 0) { |
| 8284 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8285 | xmlRelaxNGDumpValidError(ctxt); |
| 8286 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8287 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8288 | } |
| 8289 | |
| 8290 | #ifdef DEBUG |
| 8291 | xmlGenericError(xmlGenericErrorContext, |
| 8292 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 8293 | node->name, ret); |
| 8294 | if (oldstate == NULL) |
| 8295 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 8296 | else if (oldstate->seq == NULL) |
| 8297 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 8298 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 8299 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 8300 | oldstate->seq->name); |
| 8301 | else |
| 8302 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 8303 | oldstate->seq->name, oldstate->seq->type); |
| 8304 | #endif |
| 8305 | break; |
| 8306 | case XML_RELAXNG_OPTIONAL: { |
| 8307 | oldflags = ctxt->flags; |
| 8308 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8309 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 8310 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 8311 | if (ret != 0) { |
| 8312 | if (ctxt->state != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8313 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8314 | ctxt->state = oldstate; |
| 8315 | ctxt->flags = oldflags; |
| 8316 | ret = 0; |
| 8317 | break; |
| 8318 | } |
| 8319 | if (ctxt->states != NULL) { |
| 8320 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 8321 | } else { |
| 8322 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 8323 | if (ctxt->states == NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8324 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8325 | ctxt->flags = oldflags; |
| 8326 | ret = -1; |
| 8327 | break; |
| 8328 | } |
| 8329 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 8330 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 8331 | ctxt->state = NULL; |
| 8332 | } |
| 8333 | ctxt->flags = oldflags; |
| 8334 | ret = 0; |
| 8335 | break; |
| 8336 | } |
| 8337 | case XML_RELAXNG_ONEORMORE: |
| 8338 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 8339 | if (ret != 0) { |
| 8340 | break; |
| 8341 | } |
| 8342 | /* no break on purpose */ |
| 8343 | case XML_RELAXNG_ZEROORMORE: { |
| 8344 | int progress; |
| 8345 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 8346 | int base, j; |
| 8347 | |
| 8348 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 8349 | if (res == NULL) { |
| 8350 | ret = -1; |
| 8351 | break; |
| 8352 | } |
| 8353 | /* |
| 8354 | * All the input states are also exit states |
| 8355 | */ |
| 8356 | if (ctxt->state != NULL) { |
| 8357 | xmlRelaxNGAddStates(ctxt, res, |
| 8358 | xmlRelaxNGCopyValidState(ctxt, ctxt->state)); |
| 8359 | } else { |
| 8360 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 8361 | xmlRelaxNGAddStates(ctxt, res, |
| 8362 | xmlRelaxNGCopyValidState(ctxt, |
| 8363 | ctxt->states->tabState[j])); |
| 8364 | } |
| 8365 | } |
| 8366 | oldflags = ctxt->flags; |
| 8367 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8368 | do { |
| 8369 | progress = 0; |
| 8370 | base = res->nbState; |
| 8371 | |
| 8372 | if (ctxt->states != NULL) { |
| 8373 | states = ctxt->states; |
| 8374 | for (i = 0;i < states->nbState;i++) { |
| 8375 | ctxt->state = states->tabState[i]; |
| 8376 | ctxt->states = NULL; |
| 8377 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 8378 | define->content); |
| 8379 | if (ret == 0) { |
| 8380 | if (ctxt->state != NULL) { |
| 8381 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 8382 | ctxt->state); |
| 8383 | ctxt->state = NULL; |
| 8384 | if (tmp == 1) |
| 8385 | progress = 1; |
| 8386 | } else if (ctxt->states != NULL) { |
| 8387 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 8388 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 8389 | ctxt->states->tabState[j]); |
| 8390 | if (tmp == 1) |
| 8391 | progress = 1; |
| 8392 | } |
| 8393 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8394 | ctxt->states = NULL; |
| 8395 | } |
| 8396 | } else { |
| 8397 | if (ctxt->state != NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8398 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8399 | ctxt->state = NULL; |
| 8400 | } |
| 8401 | } |
| 8402 | } |
| 8403 | } else { |
| 8404 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 8405 | define->content); |
| 8406 | if (ret != 0) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8407 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8408 | ctxt->state = NULL; |
| 8409 | } else { |
| 8410 | base = res->nbState; |
| 8411 | if (ctxt->state != NULL) { |
| 8412 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 8413 | ctxt->state); |
| 8414 | ctxt->state = NULL; |
| 8415 | if (tmp == 1) |
| 8416 | progress = 1; |
| 8417 | } else if (ctxt->states != NULL) { |
| 8418 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 8419 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 8420 | ctxt->states->tabState[j]); |
| 8421 | if (tmp == 1) |
| 8422 | progress = 1; |
| 8423 | } |
| 8424 | if (states == NULL) { |
| 8425 | states = ctxt->states; |
| 8426 | } else { |
| 8427 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8428 | } |
| 8429 | ctxt->states = NULL; |
| 8430 | } |
| 8431 | } |
| 8432 | } |
| 8433 | if (progress) { |
| 8434 | /* |
| 8435 | * Collect all the new nodes added at that step |
| 8436 | * and make them the new node set |
| 8437 | */ |
| 8438 | if (res->nbState - base == 1) { |
| 8439 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 8440 | res->tabState[base]); |
| 8441 | } else { |
| 8442 | if (states == NULL) { |
| 8443 | xmlRelaxNGNewStates(ctxt, res->nbState - base); |
| 8444 | } |
| 8445 | states->nbState = 0; |
| 8446 | for (i = base;i < res->nbState;i++) |
| 8447 | xmlRelaxNGAddStates(ctxt, states, |
| 8448 | xmlRelaxNGCopyValidState(ctxt, |
| 8449 | res->tabState[i])); |
| 8450 | ctxt->states = states; |
| 8451 | } |
| 8452 | } |
| 8453 | } while (progress == 1); |
| 8454 | if (states != NULL) { |
| 8455 | xmlRelaxNGFreeStates(ctxt, states); |
| 8456 | } |
| 8457 | ctxt->states = res; |
| 8458 | ctxt->flags = oldflags; |
| 8459 | ret = 0; |
| 8460 | break; |
| 8461 | } |
| 8462 | case XML_RELAXNG_CHOICE: { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8463 | xmlRelaxNGDefinePtr list = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8464 | xmlRelaxNGStatesPtr states = NULL; |
| 8465 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8466 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8467 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8468 | if ((define->dflags & IS_TRIABLE) && (define->data != NULL)) { |
| 8469 | xmlHashTablePtr triage = (xmlHashTablePtr) define->data; |
| 8470 | |
| 8471 | /* |
| 8472 | * Something we can optimize cleanly there is only one |
| 8473 | * possble branch out ! |
| 8474 | */ |
| 8475 | if (node == NULL) { |
| 8476 | ret = -1; |
| 8477 | break; |
| 8478 | } |
| 8479 | if ((node->type == XML_TEXT_NODE) || |
| 8480 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 8481 | list = xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 8482 | } else if (node->type == XML_ELEMENT_NODE) { |
| 8483 | if (node->ns != NULL) { |
| 8484 | list = xmlHashLookup2(triage, node->name, |
| 8485 | node->ns->href); |
| 8486 | if (list == NULL) |
| 8487 | list = xmlHashLookup2(triage, BAD_CAST "#any", |
| 8488 | node->ns->href); |
| 8489 | } else |
| 8490 | list = xmlHashLookup2(triage, node->name, NULL); |
| 8491 | if (list == NULL) |
| 8492 | list = xmlHashLookup2(triage, BAD_CAST "#any", NULL); |
| 8493 | } |
| 8494 | if (list == NULL) { |
| 8495 | ret = -1; |
| 8496 | break; |
| 8497 | } |
| 8498 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 8499 | break; |
| 8500 | } |
| 8501 | |
| 8502 | list = define->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8503 | oldflags = ctxt->flags; |
| 8504 | errNr = ctxt->errNr; |
| 8505 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8506 | |
| 8507 | while (list != NULL) { |
| 8508 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 8509 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 8510 | if (ret == 0) { |
| 8511 | if (states == NULL) { |
| 8512 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 8513 | } |
| 8514 | if (ctxt->state != NULL) { |
| 8515 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 8516 | } else if (ctxt->states != NULL) { |
| 8517 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 8518 | xmlRelaxNGAddStates(ctxt, states, |
| 8519 | ctxt->states->tabState[i]); |
| 8520 | } |
| 8521 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8522 | ctxt->states = NULL; |
| 8523 | } |
| 8524 | } else { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8525 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8526 | } |
| 8527 | ctxt->state = oldstate; |
| 8528 | list = list->next; |
| 8529 | } |
| 8530 | if (states != NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8531 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8532 | ctxt->states = states; |
| 8533 | ctxt->state = NULL; |
| 8534 | ret = 0; |
| 8535 | } else { |
| 8536 | ctxt->states = NULL; |
| 8537 | } |
| 8538 | ctxt->flags = oldflags; |
| 8539 | if (ret != 0) { |
| 8540 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8541 | xmlRelaxNGDumpValidError(ctxt); |
| 8542 | } else if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8543 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8544 | } |
| 8545 | break; |
| 8546 | } |
| 8547 | case XML_RELAXNG_DEF: |
| 8548 | case XML_RELAXNG_GROUP: |
| 8549 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8550 | break; |
| 8551 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8552 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8553 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8554 | case XML_RELAXNG_ATTRIBUTE: |
| 8555 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 8556 | break; |
| 8557 | case XML_RELAXNG_NOOP: |
| 8558 | case XML_RELAXNG_REF: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8559 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8560 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 8561 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 8562 | case XML_RELAXNG_PARENTREF: |
| 8563 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 8564 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8565 | case XML_RELAXNG_DATATYPE: { |
| 8566 | xmlNodePtr child; |
| 8567 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8568 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8569 | child = node; |
| 8570 | while (child != NULL) { |
| 8571 | if (child->type == XML_ELEMENT_NODE) { |
| 8572 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 8573 | node->parent->name); |
| 8574 | ret = -1; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8575 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8576 | } else if ((child->type == XML_TEXT_NODE) || |
| 8577 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 8578 | content = xmlStrcat(content, child->content); |
| 8579 | } |
| 8580 | /* TODO: handle entities ... */ |
| 8581 | child = child->next; |
| 8582 | } |
| 8583 | if (ret == -1) { |
| 8584 | if (content != NULL) |
| 8585 | xmlFree(content); |
| 8586 | break; |
| 8587 | } |
| 8588 | if (content == NULL) { |
| 8589 | content = xmlStrdup(BAD_CAST ""); |
| 8590 | if (content == NULL) { |
| 8591 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8592 | ret = -1; |
| 8593 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8594 | } |
| 8595 | } |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8596 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 8597 | ctxt->state->seq); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8598 | if (ret == -1) { |
| 8599 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 8600 | } else if (ret == 0) { |
| 8601 | ctxt->state->seq = NULL; |
| 8602 | } |
| 8603 | if (content != NULL) |
| 8604 | xmlFree(content); |
| 8605 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8606 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8607 | case XML_RELAXNG_VALUE: { |
| 8608 | xmlChar *content = NULL; |
| 8609 | xmlChar *oldvalue; |
| 8610 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8611 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8612 | child = node; |
| 8613 | while (child != NULL) { |
| 8614 | if (child->type == XML_ELEMENT_NODE) { |
| 8615 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 8616 | node->parent->name); |
| 8617 | ret = -1; |
| 8618 | break; |
| 8619 | } else if ((child->type == XML_TEXT_NODE) || |
| 8620 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 8621 | content = xmlStrcat(content, child->content); |
| 8622 | } |
| 8623 | /* TODO: handle entities ... */ |
| 8624 | child = child->next; |
| 8625 | } |
| 8626 | if (ret == -1) { |
| 8627 | if (content != NULL) |
| 8628 | xmlFree(content); |
| 8629 | break; |
| 8630 | } |
| 8631 | if (content == NULL) { |
| 8632 | content = xmlStrdup(BAD_CAST ""); |
| 8633 | if (content == NULL) { |
| 8634 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8635 | ret = -1; |
| 8636 | break; |
| 8637 | } |
| 8638 | } |
| 8639 | oldvalue = ctxt->state->value; |
| 8640 | ctxt->state->value = content; |
| 8641 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 8642 | ctxt->state->value = oldvalue; |
| 8643 | if (ret == -1) { |
| 8644 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 8645 | } else if (ret == 0) { |
| 8646 | ctxt->state->seq = NULL; |
| 8647 | } |
| 8648 | if (content != NULL) |
| 8649 | xmlFree(content); |
| 8650 | break; |
| 8651 | } |
| 8652 | case XML_RELAXNG_LIST: { |
| 8653 | xmlChar *content; |
| 8654 | xmlNodePtr child; |
| 8655 | xmlChar *oldvalue, *oldendvalue; |
| 8656 | int len; |
| 8657 | |
| 8658 | /* |
| 8659 | * Make sure it's only text nodes |
| 8660 | */ |
| 8661 | |
| 8662 | content = NULL; |
| 8663 | child = node; |
| 8664 | while (child != NULL) { |
| 8665 | if (child->type == XML_ELEMENT_NODE) { |
| 8666 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 8667 | node->parent->name); |
| 8668 | ret = -1; |
| 8669 | break; |
| 8670 | } else if ((child->type == XML_TEXT_NODE) || |
| 8671 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 8672 | content = xmlStrcat(content, child->content); |
| 8673 | } |
| 8674 | /* TODO: handle entities ... */ |
| 8675 | child = child->next; |
| 8676 | } |
| 8677 | if (ret == -1) { |
| 8678 | if (content != NULL) |
| 8679 | xmlFree(content); |
| 8680 | break; |
| 8681 | } |
| 8682 | if (content == NULL) { |
| 8683 | content = xmlStrdup(BAD_CAST ""); |
| 8684 | if (content == NULL) { |
| 8685 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8686 | ret = -1; |
| 8687 | break; |
| 8688 | } |
| 8689 | } |
| 8690 | len = xmlStrlen(content); |
| 8691 | oldvalue = ctxt->state->value; |
| 8692 | oldendvalue = ctxt->state->endvalue; |
| 8693 | ctxt->state->value = content; |
| 8694 | ctxt->state->endvalue = content + len; |
| 8695 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 8696 | ctxt->state->value = oldvalue; |
| 8697 | ctxt->state->endvalue = oldendvalue; |
| 8698 | if (ret == -1) { |
| 8699 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 8700 | } else if ((ret == 0) && (node != NULL)) { |
| 8701 | ctxt->state->seq = node->next; |
| 8702 | } |
| 8703 | if (content != NULL) |
| 8704 | xmlFree(content); |
| 8705 | break; |
| 8706 | } |
| 8707 | case XML_RELAXNG_START: |
| 8708 | case XML_RELAXNG_EXCEPT: |
| 8709 | case XML_RELAXNG_PARAM: |
| 8710 | TODO |
| 8711 | ret = -1; |
| 8712 | break; |
| 8713 | } |
| 8714 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8715 | #ifdef DEBUG |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8716 | for (i = 0;i < ctxt->depth;i++) |
| 8717 | xmlGenericError(xmlGenericErrorContext, " "); |
| 8718 | xmlGenericError(xmlGenericErrorContext, |
| 8719 | "Validating %s ", xmlRelaxNGDefName(define)); |
| 8720 | if (define->name != NULL) |
| 8721 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
| 8722 | if (ret == 0) |
| 8723 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
| 8724 | else |
| 8725 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8726 | #endif |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8727 | return(ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8728 | } |
| 8729 | |
| 8730 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8731 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8732 | * @ctxt: a Relax-NG validation context |
| 8733 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8734 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8735 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8736 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8737 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8738 | */ |
| 8739 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8740 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 8741 | xmlRelaxNGDefinePtr define) { |
| 8742 | xmlRelaxNGStatesPtr states, res; |
| 8743 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8744 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8745 | /* |
| 8746 | * We should NOT have both ctxt->state and ctxt->states |
| 8747 | */ |
| 8748 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 8749 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8750 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8751 | ctxt->state = NULL; |
| 8752 | } |
| 8753 | |
| 8754 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
| 8755 | if (ctxt->states != NULL) { |
| 8756 | ctxt->state = ctxt->states->tabState[0]; |
| 8757 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8758 | ctxt->states = NULL; |
| 8759 | } |
| 8760 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 8761 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 8762 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8763 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8764 | ctxt->state = NULL; |
| 8765 | } |
| 8766 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 8767 | ctxt->state = ctxt->states->tabState[0]; |
| 8768 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8769 | ctxt->states = NULL; |
| 8770 | } |
| 8771 | return(ret); |
| 8772 | } |
| 8773 | |
| 8774 | states = ctxt->states; |
| 8775 | ctxt->states = NULL; |
| 8776 | res = NULL; |
| 8777 | j = 0; |
| 8778 | oldflags = ctxt->flags; |
| 8779 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8780 | for (i = 0;i < states->nbState;i++) { |
| 8781 | ctxt->state = states->tabState[i]; |
| 8782 | ctxt->states = NULL; |
| 8783 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 8784 | /* |
| 8785 | * We should NOT have both ctxt->state and ctxt->states |
| 8786 | */ |
| 8787 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 8788 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8789 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8790 | ctxt->state = NULL; |
| 8791 | } |
| 8792 | if (ret == 0) { |
| 8793 | if (ctxt->states == NULL) { |
| 8794 | if (res != NULL) { |
| 8795 | /* add the state to the container */ |
| 8796 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 8797 | ctxt->state = NULL; |
| 8798 | } else { |
| 8799 | /* add the state directly in states */ |
| 8800 | states->tabState[j++] = ctxt->state; |
| 8801 | ctxt->state = NULL; |
| 8802 | } |
| 8803 | } else { |
| 8804 | if (res == NULL) { |
| 8805 | /* make it the new container and copy other results */ |
| 8806 | res = ctxt->states; |
| 8807 | ctxt->states = NULL; |
| 8808 | for (k = 0;k < j;k++) |
| 8809 | xmlRelaxNGAddStates(ctxt, res, states->tabState[k]); |
| 8810 | } else { |
| 8811 | /* add all the new results to res and reff the container */ |
| 8812 | for (k = 0;k < ctxt->states->nbState;k++) |
| 8813 | xmlRelaxNGAddStates(ctxt, res, |
| 8814 | ctxt->states->tabState[k]); |
| 8815 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8816 | ctxt->states = NULL; |
| 8817 | } |
| 8818 | } |
| 8819 | } else { |
| 8820 | if (ctxt->state != NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8821 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8822 | ctxt->state = NULL; |
| 8823 | } else if (ctxt->states != NULL) { |
| 8824 | for (k = 0;k < ctxt->states->nbState;k++) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8825 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[k]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8826 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8827 | ctxt->states = NULL; |
| 8828 | } |
| 8829 | } |
| 8830 | } |
| 8831 | ctxt->flags = oldflags; |
| 8832 | if (res != NULL) { |
| 8833 | xmlRelaxNGFreeStates(ctxt, states); |
| 8834 | ctxt->states = res; |
| 8835 | ret = 0; |
| 8836 | } else if (j > 1) { |
| 8837 | states->nbState = j; |
| 8838 | ctxt->states = states; |
| 8839 | ret =0; |
| 8840 | } else if (j == 1) { |
| 8841 | ctxt->state = states->tabState[0]; |
| 8842 | xmlRelaxNGFreeStates(ctxt, states); |
| 8843 | ret = 0; |
| 8844 | } else { |
| 8845 | ret = -1; |
| 8846 | xmlRelaxNGFreeStates(ctxt, states); |
| 8847 | if (ctxt->states != NULL) { |
| 8848 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 8849 | ctxt->states = NULL; |
| 8850 | } |
| 8851 | } |
| 8852 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 8853 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8854 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8855 | ctxt->state = NULL; |
| 8856 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8857 | return(ret); |
| 8858 | } |
| 8859 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8860 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8861 | * xmlRelaxNGValidateDocument: |
| 8862 | * @ctxt: a Relax-NG validation context |
| 8863 | * @doc: the document |
| 8864 | * |
| 8865 | * Validate the given document |
| 8866 | * |
| 8867 | * Returns 0 if the validation succeeded or an error code. |
| 8868 | */ |
| 8869 | static int |
| 8870 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 8871 | int ret; |
| 8872 | xmlRelaxNGPtr schema; |
| 8873 | xmlRelaxNGGrammarPtr grammar; |
| 8874 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8875 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8876 | |
| 8877 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
| 8878 | return(-1); |
| 8879 | |
| 8880 | schema = ctxt->schema; |
| 8881 | grammar = schema->topgrammar; |
| 8882 | if (grammar == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8883 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8884 | return(-1); |
| 8885 | } |
| 8886 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 8887 | ctxt->state = state; |
| 8888 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8889 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
| 8890 | state = ctxt->state; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8891 | node = state->seq; |
| 8892 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 8893 | if (node != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8894 | if (ret != -1) { |
| 8895 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 8896 | ret = -1; |
| 8897 | } |
| 8898 | } |
| 8899 | } else if (ctxt->states != NULL) { |
| 8900 | int i; |
| 8901 | int tmp = -1; |
| 8902 | |
| 8903 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 8904 | state = ctxt->states->tabState[i]; |
| 8905 | node = state->seq; |
| 8906 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 8907 | if (node == NULL) |
| 8908 | tmp = 0; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8909 | xmlRelaxNGFreeValidState(ctxt,state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8910 | } |
| 8911 | if (tmp == -1) { |
| 8912 | if (ret != -1) { |
| 8913 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 8914 | ret = -1; |
| 8915 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8916 | } |
| 8917 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 8918 | if (ctxt->state != NULL) { |
| 8919 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 8920 | ctxt->state = NULL; |
| 8921 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8922 | if (ret != 0) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8923 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 8924 | #ifdef DEBUG |
| 8925 | else if (ctxt->errNr != 0) { |
| 8926 | ctxt->error(ctxt->userData, "%d Extra error messages left on stack !\n", |
| 8927 | ctxt->errNr); |
| 8928 | xmlRelaxNGDumpValidError(ctxt); |
| 8929 | } |
| 8930 | #endif |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8931 | if (ctxt->idref == 1) { |
| 8932 | xmlValidCtxt vctxt; |
| 8933 | |
| 8934 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 8935 | vctxt.valid = 1; |
| 8936 | vctxt.error = ctxt->error; |
| 8937 | vctxt.warning = ctxt->warning; |
| 8938 | vctxt.userData = ctxt->userData; |
| 8939 | |
| 8940 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 8941 | ret = -1; |
| 8942 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8943 | |
| 8944 | return(ret); |
| 8945 | } |
| 8946 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8947 | /************************************************************************ |
| 8948 | * * |
| 8949 | * Validation interfaces * |
| 8950 | * * |
| 8951 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8952 | /** |
| 8953 | * xmlRelaxNGNewValidCtxt: |
| 8954 | * @schema: a precompiled XML RelaxNGs |
| 8955 | * |
| 8956 | * Create an XML RelaxNGs validation context based on the given schema |
| 8957 | * |
| 8958 | * Returns the validation context or NULL in case of error |
| 8959 | */ |
| 8960 | xmlRelaxNGValidCtxtPtr |
| 8961 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) { |
| 8962 | xmlRelaxNGValidCtxtPtr ret; |
| 8963 | |
| 8964 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 8965 | if (ret == NULL) { |
| 8966 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8967 | "Failed to allocate new schema validation context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8968 | return (NULL); |
| 8969 | } |
| 8970 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 8971 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 8972 | ret->error = xmlGenericError; |
| 8973 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8974 | ret->errNr = 0; |
| 8975 | ret->errMax = 0; |
| 8976 | ret->err = NULL; |
| 8977 | ret->errTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8978 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8979 | ret->states = NULL; |
| 8980 | ret->freeState = NULL; |
| 8981 | ret->freeStates = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8982 | return (ret); |
| 8983 | } |
| 8984 | |
| 8985 | /** |
| 8986 | * xmlRelaxNGFreeValidCtxt: |
| 8987 | * @ctxt: the schema validation context |
| 8988 | * |
| 8989 | * Free the resources associated to the schema validation context |
| 8990 | */ |
| 8991 | void |
| 8992 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8993 | int k; |
| 8994 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8995 | if (ctxt == NULL) |
| 8996 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8997 | if (ctxt->states != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8998 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
| 8999 | if (ctxt->freeState != NULL) { |
| 9000 | for (k = 0;k < ctxt->freeState->nbState;k++) { |
| 9001 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 9002 | } |
| 9003 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
| 9004 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9005 | if (ctxt->freeStates != NULL) { |
| 9006 | for (k = 0;k < ctxt->freeStatesNr;k++) { |
| 9007 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 9008 | } |
| 9009 | xmlFree(ctxt->freeStates); |
| 9010 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 9011 | if (ctxt->errTab != NULL) |
| 9012 | xmlFree(ctxt->errTab); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9013 | xmlFree(ctxt); |
| 9014 | } |
| 9015 | |
| 9016 | /** |
| 9017 | * xmlRelaxNGSetValidErrors: |
| 9018 | * @ctxt: a Relax-NG validation context |
| 9019 | * @err: the error function |
| 9020 | * @warn: the warning function |
| 9021 | * @ctx: the functions context |
| 9022 | * |
| 9023 | * Set the error and warning callback informations |
| 9024 | */ |
| 9025 | void |
| 9026 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
| 9027 | xmlRelaxNGValidityErrorFunc err, |
| 9028 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 9029 | if (ctxt == NULL) |
| 9030 | return; |
| 9031 | ctxt->error = err; |
| 9032 | ctxt->warning = warn; |
| 9033 | ctxt->userData = ctx; |
| 9034 | } |
| 9035 | |
| 9036 | /** |
| 9037 | * xmlRelaxNGValidateDoc: |
| 9038 | * @ctxt: a Relax-NG validation context |
| 9039 | * @doc: a parsed document tree |
| 9040 | * |
| 9041 | * Validate a document tree in memory. |
| 9042 | * |
| 9043 | * Returns 0 if the document is valid, a positive error code |
| 9044 | * number otherwise and -1 in case of internal or API error. |
| 9045 | */ |
| 9046 | int |
| 9047 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 9048 | int ret; |
| 9049 | |
| 9050 | if ((ctxt == NULL) || (doc == NULL)) |
| 9051 | return(-1); |
| 9052 | |
| 9053 | ctxt->doc = doc; |
| 9054 | |
| 9055 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 9056 | /* |
| 9057 | * TODO: build error codes |
| 9058 | */ |
| 9059 | if (ret == -1) |
| 9060 | return(1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9061 | return(ret); |
| 9062 | } |
| 9063 | |
| 9064 | #endif /* LIBXML_SCHEMAS_ENABLED */ |
| 9065 | |