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 | f4b4f98 | 2003-02-13 11:02:08 +0000 | [diff] [blame] | 11 | * - add support for DTD compatibility spec |
| 12 | * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 13 | * - report better mem allocations pbms at runtime and abort immediately. |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 16 | #define IN_LIBXML |
| 17 | #include "libxml.h" |
| 18 | |
| 19 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdio.h> |
| 23 | #include <libxml/xmlmemory.h> |
| 24 | #include <libxml/parser.h> |
| 25 | #include <libxml/parserInternals.h> |
| 26 | #include <libxml/hash.h> |
| 27 | #include <libxml/uri.h> |
| 28 | |
| 29 | #include <libxml/relaxng.h> |
| 30 | |
| 31 | #include <libxml/xmlschemastypes.h> |
| 32 | #include <libxml/xmlautomata.h> |
| 33 | #include <libxml/xmlregexp.h> |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 34 | #include <libxml/xmlschemastypes.h> |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * The Relax-NG namespace |
| 38 | */ |
| 39 | static const xmlChar *xmlRelaxNGNs = (const xmlChar *) |
| 40 | "http://relaxng.org/ns/structure/1.0"; |
| 41 | |
| 42 | #define IS_RELAXNG(node, type) \ |
| 43 | ((node != NULL) && (node->ns != NULL) && \ |
| 44 | (xmlStrEqual(node->name, (const xmlChar *) type)) && \ |
| 45 | (xmlStrEqual(node->ns->href, xmlRelaxNGNs))) |
| 46 | |
| 47 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 48 | /* #define DEBUG 1 */ |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 49 | /* #define DEBUG_GRAMMAR 1 */ |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 50 | /* #define DEBUG_CONTENT 1 */ |
| 51 | /* #define DEBUG_TYPE 1 */ |
| 52 | /* #define DEBUG_VALID 1 */ |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 53 | /* #define DEBUG_INTERLEAVE 1 */ |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 54 | /* #define DEBUG_LIST 1 */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 55 | /* #define DEBUG_INCLUDE */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 56 | /* #define DEBUG_ERROR 1 */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 57 | /* #define DEBUG_COMPILE 1 */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 58 | /* #define DEBUG_PROGRESSIVE 1 */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 59 | |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 60 | #define MAX_ERROR 5 |
| 61 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 62 | #define TODO \ |
| 63 | xmlGenericError(xmlGenericErrorContext, \ |
| 64 | "Unimplemented block at %s:%d\n", \ |
| 65 | __FILE__, __LINE__); |
| 66 | |
| 67 | typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema; |
| 68 | typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr; |
| 69 | |
| 70 | typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine; |
| 71 | typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr; |
| 72 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 73 | typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument; |
| 74 | typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr; |
| 75 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 76 | typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude; |
| 77 | typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr; |
| 78 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 79 | typedef enum { |
| 80 | XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */ |
| 81 | XML_RELAXNG_COMBINE_CHOICE, /* choice */ |
| 82 | XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */ |
| 83 | } xmlRelaxNGCombine; |
| 84 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 85 | typedef enum { |
| 86 | XML_RELAXNG_CONTENT_ERROR = -1, |
| 87 | XML_RELAXNG_CONTENT_EMPTY = 0, |
| 88 | XML_RELAXNG_CONTENT_SIMPLE, |
| 89 | XML_RELAXNG_CONTENT_COMPLEX |
| 90 | } xmlRelaxNGContentType; |
| 91 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 92 | typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar; |
| 93 | typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr; |
| 94 | |
| 95 | struct _xmlRelaxNGGrammar { |
| 96 | xmlRelaxNGGrammarPtr parent;/* the parent grammar if any */ |
| 97 | xmlRelaxNGGrammarPtr children;/* the children grammar if any */ |
| 98 | xmlRelaxNGGrammarPtr next; /* the next grammar if any */ |
| 99 | xmlRelaxNGDefinePtr start; /* <start> content */ |
| 100 | xmlRelaxNGCombine combine; /* the default combine value */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 101 | xmlRelaxNGDefinePtr startList;/* list of <start> definitions */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 102 | xmlHashTablePtr defs; /* define* */ |
| 103 | xmlHashTablePtr refs; /* references */ |
| 104 | }; |
| 105 | |
| 106 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 107 | typedef enum { |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 108 | XML_RELAXNG_NOOP = -1, /* a no operation from simplification */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 109 | XML_RELAXNG_EMPTY = 0, /* an empty pattern */ |
| 110 | XML_RELAXNG_NOT_ALLOWED, /* not allowed top */ |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 111 | XML_RELAXNG_EXCEPT, /* except present in nameclass defs */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 112 | XML_RELAXNG_TEXT, /* textual content */ |
| 113 | XML_RELAXNG_ELEMENT, /* an element */ |
| 114 | XML_RELAXNG_DATATYPE, /* extenal data type definition */ |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 115 | XML_RELAXNG_PARAM, /* extenal data type parameter */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 116 | XML_RELAXNG_VALUE, /* value from an extenal data type definition */ |
| 117 | XML_RELAXNG_LIST, /* a list of patterns */ |
| 118 | XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */ |
| 119 | XML_RELAXNG_DEF, /* a definition */ |
| 120 | XML_RELAXNG_REF, /* reference to a definition */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 121 | XML_RELAXNG_EXTERNALREF, /* reference to an external def */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 122 | XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 123 | XML_RELAXNG_OPTIONAL, /* optional patterns */ |
| 124 | XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 125 | XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */ |
| 126 | XML_RELAXNG_CHOICE, /* a choice between non empty patterns */ |
| 127 | XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 128 | XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 129 | XML_RELAXNG_START /* Used to keep track of starts on grammars */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 130 | } xmlRelaxNGType; |
| 131 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 132 | #define IS_NULLABLE (1 << 0) |
| 133 | #define IS_NOT_NULLABLE (1 << 1) |
| 134 | #define IS_INDETERMINIST (1 << 2) |
| 135 | #define IS_MIXED (1 << 3) |
| 136 | #define IS_TRIABLE (1 << 4) |
| 137 | #define IS_PROCESSED (1 << 5) |
| 138 | #define IS_COMPILABLE (1 << 6) |
| 139 | #define IS_NOT_COMPILABLE (1 << 7) |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 140 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 141 | struct _xmlRelaxNGDefine { |
| 142 | xmlRelaxNGType type; /* the type of definition */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 143 | xmlNodePtr node; /* the node in the source */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 144 | xmlChar *name; /* the element local name if present */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 145 | xmlChar *ns; /* the namespace local name if present */ |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 146 | xmlChar *value; /* value when available */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 147 | void *data; /* data lib or specific pointer */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 148 | xmlRelaxNGDefinePtr content;/* the expected content */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 149 | xmlRelaxNGDefinePtr parent; /* the parent definition, if any */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 150 | xmlRelaxNGDefinePtr next; /* list within grouping sequences */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 151 | xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */ |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 152 | xmlRelaxNGDefinePtr nameClass;/* the nameClass definition if any */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 153 | xmlRelaxNGDefinePtr nextHash;/* next define in defs/refs hash tables */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 154 | short depth; /* used for the cycle detection */ |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 155 | short dflags; /* define related flags */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 156 | xmlRegexpPtr contModel; /* a compiled content model if available */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | /** |
| 160 | * _xmlRelaxNG: |
| 161 | * |
| 162 | * A RelaxNGs definition |
| 163 | */ |
| 164 | struct _xmlRelaxNG { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 165 | void *_private; /* unused by the library for users or bindings */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 166 | xmlRelaxNGGrammarPtr topgrammar; |
| 167 | xmlDocPtr doc; |
| 168 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 169 | int idref; /* requires idref checking */ |
| 170 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 171 | xmlHashTablePtr defs; /* define */ |
| 172 | xmlHashTablePtr refs; /* references */ |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 173 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 174 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 175 | int defNr; /* number of defines used */ |
| 176 | xmlRelaxNGDefinePtr *defTab;/* pointer to the allocated definitions */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 177 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 180 | #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0) |
| 181 | #define XML_RELAXNG_IN_ONEORMORE (1 << 1) |
| 182 | #define XML_RELAXNG_IN_LIST (1 << 2) |
| 183 | #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3) |
| 184 | #define XML_RELAXNG_IN_START (1 << 4) |
| 185 | #define XML_RELAXNG_IN_OOMGROUP (1 << 5) |
| 186 | #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6) |
| 187 | #define XML_RELAXNG_IN_EXTERNALREF (1 << 7) |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 188 | #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8) |
| 189 | #define XML_RELAXNG_IN_NSEXCEPT (1 << 9) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 190 | |
| 191 | struct _xmlRelaxNGParserCtxt { |
| 192 | void *userData; /* user specific data block */ |
| 193 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 194 | xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 195 | xmlRelaxNGValidErr err; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 196 | |
| 197 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 198 | xmlRelaxNGGrammarPtr grammar; /* the current grammar */ |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 199 | xmlRelaxNGGrammarPtr parentgrammar;/* the parent grammar */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 200 | int flags; /* parser flags */ |
| 201 | int nbErrors; /* number of errors at parse time */ |
| 202 | int nbWarnings; /* number of warnings at parse time */ |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 203 | const xmlChar *define; /* the current define scope */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 204 | xmlRelaxNGDefinePtr def; /* the current define */ |
| 205 | |
| 206 | int nbInterleaves; |
| 207 | xmlHashTablePtr interleaves; /* keep track of all the interleaves */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 208 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 209 | xmlRelaxNGDocumentPtr documents; /* all the documents loaded */ |
| 210 | xmlRelaxNGIncludePtr includes; /* all the includes loaded */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 211 | xmlChar *URL; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 212 | xmlDocPtr document; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 213 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 214 | int defNr; /* number of defines used */ |
| 215 | int defMax; /* number of defines aloocated */ |
| 216 | xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */ |
| 217 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 218 | const char *buffer; |
| 219 | int size; |
| 220 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 221 | /* the document stack */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 222 | xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 223 | int docNr; /* Depth of the parsing stack */ |
| 224 | int docMax; /* Max depth of the parsing stack */ |
| 225 | xmlRelaxNGDocumentPtr *docTab; /* array of docs */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 226 | |
| 227 | /* the include stack */ |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 228 | xmlRelaxNGIncludePtr inc; /* Current parsed include */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 229 | int incNr; /* Depth of the include parsing stack */ |
| 230 | int incMax; /* Max depth of the parsing stack */ |
| 231 | xmlRelaxNGIncludePtr *incTab; /* array of incs */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 232 | |
| 233 | int idref; /* requires idref checking */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 234 | |
| 235 | /* used to compile content models */ |
| 236 | xmlAutomataPtr am; /* the automata */ |
| 237 | xmlAutomataStatePtr state; /* used to build the automata */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | #define FLAGS_IGNORABLE 1 |
| 241 | #define FLAGS_NEGATIVE 2 |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 242 | #define FLAGS_MIXED_CONTENT 4 |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 243 | |
| 244 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 245 | * xmlRelaxNGInterleaveGroup: |
| 246 | * |
| 247 | * A RelaxNGs partition set associated to lists of definitions |
| 248 | */ |
| 249 | typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup; |
| 250 | typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr; |
| 251 | struct _xmlRelaxNGInterleaveGroup { |
| 252 | xmlRelaxNGDefinePtr rule; /* the rule to satisfy */ |
| 253 | xmlRelaxNGDefinePtr *defs; /* the array of element definitions */ |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 254 | xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 255 | }; |
| 256 | |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 257 | #define IS_DETERMINIST 1 |
| 258 | #define IS_NEEDCHECK 2 |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 259 | /** |
| 260 | * xmlRelaxNGPartitions: |
| 261 | * |
| 262 | * A RelaxNGs partition associated to an interleave group |
| 263 | */ |
| 264 | typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition; |
| 265 | typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr; |
| 266 | struct _xmlRelaxNGPartition { |
| 267 | int nbgroups; /* number of groups in the partitions */ |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 268 | xmlHashTablePtr triage; /* hash table used to direct nodes to the |
| 269 | right group when possible */ |
| 270 | int flags; /* determinist ? */ |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 271 | xmlRelaxNGInterleaveGroupPtr *groups; |
| 272 | }; |
| 273 | |
| 274 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 275 | * xmlRelaxNGValidState: |
| 276 | * |
| 277 | * A RelaxNGs validation state |
| 278 | */ |
| 279 | #define MAX_ATTR 20 |
| 280 | typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState; |
| 281 | typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr; |
| 282 | struct _xmlRelaxNGValidState { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 283 | xmlNodePtr node; /* the current node */ |
| 284 | xmlNodePtr seq; /* the sequence of children left to validate */ |
| 285 | int nbAttrs; /* the number of attributes */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 286 | int maxAttrs; /* the size of attrs */ |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 287 | int nbAttrLeft; /* the number of attributes left to validate */ |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 288 | xmlChar *value; /* the value when operating on string */ |
| 289 | xmlChar *endvalue; /* the end value when operating on string */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 290 | xmlAttrPtr *attrs; /* the array of attributes */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 294 | * xmlRelaxNGStates: |
| 295 | * |
| 296 | * A RelaxNGs container for validation state |
| 297 | */ |
| 298 | typedef struct _xmlRelaxNGStates xmlRelaxNGStates; |
| 299 | typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr; |
| 300 | struct _xmlRelaxNGStates { |
| 301 | int nbState; /* the number of states */ |
| 302 | int maxState; /* the size of the array */ |
| 303 | xmlRelaxNGValidStatePtr *tabState; |
| 304 | }; |
| 305 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 306 | #define ERROR_IS_DUP 1 |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 307 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 308 | * xmlRelaxNGValidError: |
| 309 | * |
| 310 | * A RelaxNGs validation error |
| 311 | */ |
| 312 | typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError; |
| 313 | typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr; |
| 314 | struct _xmlRelaxNGValidError { |
| 315 | xmlRelaxNGValidErr err; /* the error number */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 316 | int flags; /* flags */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 317 | xmlNodePtr node; /* the current node */ |
| 318 | xmlNodePtr seq; /* the current child */ |
| 319 | const xmlChar * arg1; /* first arg */ |
| 320 | const xmlChar * arg2; /* second arg */ |
| 321 | }; |
| 322 | |
| 323 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 324 | * xmlRelaxNGValidCtxt: |
| 325 | * |
| 326 | * A RelaxNGs validation context |
| 327 | */ |
| 328 | |
| 329 | struct _xmlRelaxNGValidCtxt { |
| 330 | void *userData; /* user specific data block */ |
| 331 | xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */ |
| 332 | xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */ |
| 333 | |
| 334 | xmlRelaxNGPtr schema; /* The schema in use */ |
| 335 | xmlDocPtr doc; /* the document being validated */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 336 | int flags; /* validation flags */ |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 337 | int depth; /* validation depth */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 338 | int idref; /* requires idref checking */ |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 339 | int errNo; /* the first error found */ |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 340 | |
| 341 | /* |
| 342 | * Errors accumulated in branches may have to be stacked to be |
| 343 | * provided back when it's sure they affect validation. |
| 344 | */ |
| 345 | xmlRelaxNGValidErrorPtr err; /* Last error */ |
| 346 | int errNr; /* Depth of the error stack */ |
| 347 | int errMax; /* Max depth of the error stack */ |
| 348 | xmlRelaxNGValidErrorPtr errTab; /* stack of errors */ |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 349 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 350 | xmlRelaxNGValidStatePtr state; /* the current validation state */ |
| 351 | xmlRelaxNGStatesPtr states; /* the accumulated state list */ |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 352 | |
| 353 | xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */ |
| 354 | int freeStatesNr; |
| 355 | int freeStatesMax; |
| 356 | xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 357 | |
| 358 | /* |
| 359 | * This is used for "progressive" validation |
| 360 | */ |
| 361 | xmlRegExecCtxtPtr elem; /* the current element regexp */ |
| 362 | int elemNr; /* the number of element validated */ |
| 363 | int elemMax; /* the max depth of elements */ |
| 364 | xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */ |
| 365 | int pstate; /* progressive state */ |
| 366 | xmlNodePtr pnode; /* the current node */ |
| 367 | xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */ |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 368 | int perr; /* signal error in content model |
| 369 | outside the regexp */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 370 | }; |
| 371 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 372 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 373 | * xmlRelaxNGInclude: |
| 374 | * |
| 375 | * Structure associated to a RelaxNGs document element |
| 376 | */ |
| 377 | struct _xmlRelaxNGInclude { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 378 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 379 | xmlChar *href; /* the normalized href value */ |
| 380 | xmlDocPtr doc; /* the associated XML document */ |
| 381 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 382 | xmlRelaxNGPtr schema; /* the schema */ |
| 383 | }; |
| 384 | |
| 385 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 386 | * xmlRelaxNGDocument: |
| 387 | * |
| 388 | * Structure associated to a RelaxNGs document element |
| 389 | */ |
| 390 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 391 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 392 | xmlChar *href; /* the normalized href value */ |
| 393 | xmlDocPtr doc; /* the associated XML document */ |
| 394 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 395 | xmlRelaxNGPtr schema; /* the schema */ |
| 396 | }; |
| 397 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 398 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 399 | /************************************************************************ |
| 400 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 401 | * Preliminary type checking interfaces * |
| 402 | * * |
| 403 | ************************************************************************/ |
| 404 | /** |
| 405 | * xmlRelaxNGTypeHave: |
| 406 | * @data: data needed for the library |
| 407 | * @type: the type name |
| 408 | * @value: the value to check |
| 409 | * |
| 410 | * Function provided by a type library to check if a type is exported |
| 411 | * |
| 412 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 413 | */ |
| 414 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar *type); |
| 415 | |
| 416 | /** |
| 417 | * xmlRelaxNGTypeCheck: |
| 418 | * @data: data needed for the library |
| 419 | * @type: the type name |
| 420 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 421 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 422 | * |
| 423 | * Function provided by a type library to check if a value match a type |
| 424 | * |
| 425 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 426 | */ |
| 427 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar *type, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 428 | const xmlChar *value, void **result, |
| 429 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 430 | |
| 431 | /** |
| 432 | * xmlRelaxNGFacetCheck: |
| 433 | * @data: data needed for the library |
| 434 | * @type: the type name |
| 435 | * @facet: the facet name |
| 436 | * @val: the facet value |
| 437 | * @strval: the string value |
| 438 | * @value: the value to check |
| 439 | * |
| 440 | * Function provided by a type library to check a value facet |
| 441 | * |
| 442 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 443 | */ |
| 444 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar *type, |
| 445 | const xmlChar *facet, const xmlChar *val, |
| 446 | const xmlChar *strval, void *value); |
| 447 | |
| 448 | /** |
| 449 | * xmlRelaxNGTypeFree: |
| 450 | * @data: data needed for the library |
| 451 | * @result: the value to free |
| 452 | * |
| 453 | * Function provided by a type library to free a returned result |
| 454 | */ |
| 455 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 456 | |
| 457 | /** |
| 458 | * xmlRelaxNGTypeCompare: |
| 459 | * @data: data needed for the library |
| 460 | * @type: the type name |
| 461 | * @value1: the first value |
| 462 | * @value2: the second value |
| 463 | * |
| 464 | * Function provided by a type library to compare two values accordingly |
| 465 | * to a type. |
| 466 | * |
| 467 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 468 | */ |
| 469 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar *type, |
| 470 | const xmlChar *value1, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 471 | xmlNodePtr ctxt1, |
| 472 | void *comp1, |
| 473 | const xmlChar *value2, |
| 474 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 475 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 476 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 477 | struct _xmlRelaxNGTypeLibrary { |
| 478 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 479 | void *data; /* data needed for the library */ |
| 480 | xmlRelaxNGTypeHave have; /* the export function */ |
| 481 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 482 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 483 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 484 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 485 | }; |
| 486 | |
| 487 | /************************************************************************ |
| 488 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 489 | * Allocation functions * |
| 490 | * * |
| 491 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 492 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 493 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 494 | static void xmlRelaxNGNormExtSpace(xmlChar *value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 495 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 496 | static int xmlRelaxNGEqualValidState( |
| 497 | xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 498 | xmlRelaxNGValidStatePtr state1, |
| 499 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 500 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 501 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 502 | |
| 503 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 504 | * xmlRelaxNGFreeDocument: |
| 505 | * @docu: a document structure |
| 506 | * |
| 507 | * Deallocate a RelaxNG document structure. |
| 508 | */ |
| 509 | static void |
| 510 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 511 | { |
| 512 | if (docu == NULL) |
| 513 | return; |
| 514 | |
| 515 | if (docu->href != NULL) |
| 516 | xmlFree(docu->href); |
| 517 | if (docu->doc != NULL) |
| 518 | xmlFreeDoc(docu->doc); |
| 519 | if (docu->schema != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 520 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 521 | xmlFree(docu); |
| 522 | } |
| 523 | |
| 524 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 525 | * xmlRelaxNGFreeDocumentList: |
| 526 | * @docu: a list of document structure |
| 527 | * |
| 528 | * Deallocate a RelaxNG document structures. |
| 529 | */ |
| 530 | static void |
| 531 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 532 | { |
| 533 | xmlRelaxNGDocumentPtr next; |
| 534 | while (docu != NULL) { |
| 535 | next = docu->next; |
| 536 | xmlRelaxNGFreeDocument(docu); |
| 537 | docu = next; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 542 | * xmlRelaxNGFreeInclude: |
| 543 | * @incl: a include structure |
| 544 | * |
| 545 | * Deallocate a RelaxNG include structure. |
| 546 | */ |
| 547 | static void |
| 548 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 549 | { |
| 550 | if (incl == NULL) |
| 551 | return; |
| 552 | |
| 553 | if (incl->href != NULL) |
| 554 | xmlFree(incl->href); |
| 555 | if (incl->doc != NULL) |
| 556 | xmlFreeDoc(incl->doc); |
| 557 | if (incl->schema != NULL) |
| 558 | xmlRelaxNGFree(incl->schema); |
| 559 | xmlFree(incl); |
| 560 | } |
| 561 | |
| 562 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 563 | * xmlRelaxNGFreeIncludeList: |
| 564 | * @incl: a include structure list |
| 565 | * |
| 566 | * Deallocate a RelaxNG include structure. |
| 567 | */ |
| 568 | static void |
| 569 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 570 | { |
| 571 | xmlRelaxNGIncludePtr next; |
| 572 | while (incl != NULL) { |
| 573 | next = incl->next; |
| 574 | xmlRelaxNGFreeInclude(incl); |
| 575 | incl = next; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 580 | * xmlRelaxNGNewRelaxNG: |
| 581 | * @ctxt: a Relax-NG validation context (optional) |
| 582 | * |
| 583 | * Allocate a new RelaxNG structure. |
| 584 | * |
| 585 | * Returns the newly allocated structure or NULL in case or error |
| 586 | */ |
| 587 | static xmlRelaxNGPtr |
| 588 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 589 | { |
| 590 | xmlRelaxNGPtr ret; |
| 591 | |
| 592 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 593 | if (ret == NULL) { |
| 594 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 595 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 596 | ctxt->nbErrors++; |
| 597 | return (NULL); |
| 598 | } |
| 599 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 600 | |
| 601 | return (ret); |
| 602 | } |
| 603 | |
| 604 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 605 | * xmlRelaxNGFreeInnerSchema: |
| 606 | * @schema: a schema structure |
| 607 | * |
| 608 | * Deallocate a RelaxNG schema structure. |
| 609 | */ |
| 610 | static void |
| 611 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 612 | { |
| 613 | if (schema == NULL) |
| 614 | return; |
| 615 | |
| 616 | if (schema->doc != NULL) |
| 617 | xmlFreeDoc(schema->doc); |
| 618 | if (schema->defTab != NULL) { |
| 619 | int i; |
| 620 | |
| 621 | for (i = 0;i < schema->defNr;i++) |
| 622 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 623 | xmlFree(schema->defTab); |
| 624 | } |
| 625 | |
| 626 | xmlFree(schema); |
| 627 | } |
| 628 | |
| 629 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 630 | * xmlRelaxNGFree: |
| 631 | * @schema: a schema structure |
| 632 | * |
| 633 | * Deallocate a RelaxNG structure. |
| 634 | */ |
| 635 | void |
| 636 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 637 | { |
| 638 | if (schema == NULL) |
| 639 | return; |
| 640 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 641 | if (schema->topgrammar != NULL) |
| 642 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
| 643 | if (schema->doc != NULL) |
| 644 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 645 | if (schema->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 646 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 647 | if (schema->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 648 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 649 | if (schema->defTab != NULL) { |
| 650 | int i; |
| 651 | |
| 652 | for (i = 0;i < schema->defNr;i++) |
| 653 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 654 | xmlFree(schema->defTab); |
| 655 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 656 | |
| 657 | xmlFree(schema); |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * xmlRelaxNGNewGrammar: |
| 662 | * @ctxt: a Relax-NG validation context (optional) |
| 663 | * |
| 664 | * Allocate a new RelaxNG grammar. |
| 665 | * |
| 666 | * Returns the newly allocated structure or NULL in case or error |
| 667 | */ |
| 668 | static xmlRelaxNGGrammarPtr |
| 669 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 670 | { |
| 671 | xmlRelaxNGGrammarPtr ret; |
| 672 | |
| 673 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 674 | if (ret == NULL) { |
| 675 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 676 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 677 | ctxt->nbErrors++; |
| 678 | return (NULL); |
| 679 | } |
| 680 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 681 | |
| 682 | return (ret); |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * xmlRelaxNGFreeGrammar: |
| 687 | * @grammar: a grammar structure |
| 688 | * |
| 689 | * Deallocate a RelaxNG grammar structure. |
| 690 | */ |
| 691 | static void |
| 692 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 693 | { |
| 694 | if (grammar == NULL) |
| 695 | return; |
| 696 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 697 | if (grammar->children != NULL) { |
| 698 | xmlRelaxNGFreeGrammar(grammar->children); |
| 699 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 700 | if (grammar->next != NULL) { |
| 701 | xmlRelaxNGFreeGrammar(grammar->next); |
| 702 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 703 | if (grammar->refs != NULL) { |
| 704 | xmlHashFree(grammar->refs, NULL); |
| 705 | } |
| 706 | if (grammar->defs != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 707 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | xmlFree(grammar); |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * xmlRelaxNGNewDefine: |
| 715 | * @ctxt: a Relax-NG validation context |
| 716 | * @node: the node in the input document. |
| 717 | * |
| 718 | * Allocate a new RelaxNG define. |
| 719 | * |
| 720 | * Returns the newly allocated structure or NULL in case or error |
| 721 | */ |
| 722 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 723 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 724 | { |
| 725 | xmlRelaxNGDefinePtr ret; |
| 726 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 727 | if (ctxt->defMax == 0) { |
| 728 | ctxt->defMax = 16; |
| 729 | ctxt->defNr = 0; |
| 730 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 731 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 732 | if (ctxt->defTab == NULL) { |
| 733 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 734 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 735 | ctxt->nbErrors++; |
| 736 | return (NULL); |
| 737 | } |
| 738 | } else if (ctxt->defMax <= ctxt->defNr) { |
| 739 | xmlRelaxNGDefinePtr *tmp; |
| 740 | ctxt->defMax *= 2; |
| 741 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 742 | ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 743 | if (tmp == NULL) { |
| 744 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 745 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 746 | ctxt->nbErrors++; |
| 747 | return (NULL); |
| 748 | } |
| 749 | ctxt->defTab = tmp; |
| 750 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 751 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 752 | if (ret == NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 753 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 754 | ctxt->error(ctxt->userData, "Out of memory\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 755 | ctxt->nbErrors++; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 756 | return(NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 757 | } |
| 758 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 759 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 760 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 761 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 762 | return (ret); |
| 763 | } |
| 764 | |
| 765 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 766 | * xmlRelaxNGFreePartition: |
| 767 | * @partitions: a partition set structure |
| 768 | * |
| 769 | * Deallocate RelaxNG partition set structures. |
| 770 | */ |
| 771 | static void |
| 772 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) { |
| 773 | xmlRelaxNGInterleaveGroupPtr group; |
| 774 | int j; |
| 775 | |
| 776 | if (partitions != NULL) { |
| 777 | if (partitions->groups != NULL) { |
| 778 | for (j = 0;j < partitions->nbgroups;j++) { |
| 779 | group = partitions->groups[j]; |
| 780 | if (group != NULL) { |
| 781 | if (group->defs != NULL) |
| 782 | xmlFree(group->defs); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 783 | if (group->attrs != NULL) |
| 784 | xmlFree(group->attrs); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 785 | xmlFree(group); |
| 786 | } |
| 787 | } |
| 788 | xmlFree(partitions->groups); |
| 789 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 790 | if (partitions->triage != NULL) { |
| 791 | xmlHashFree(partitions->triage, NULL); |
| 792 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 793 | xmlFree(partitions); |
| 794 | } |
| 795 | } |
| 796 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 797 | * xmlRelaxNGFreeDefine: |
| 798 | * @define: a define structure |
| 799 | * |
| 800 | * Deallocate a RelaxNG define structure. |
| 801 | */ |
| 802 | static void |
| 803 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 804 | { |
| 805 | if (define == NULL) |
| 806 | return; |
| 807 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 808 | if ((define->type == XML_RELAXNG_VALUE) && |
| 809 | (define->attrs != NULL)) { |
| 810 | xmlRelaxNGTypeLibraryPtr lib; |
| 811 | |
| 812 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 813 | if ((lib != NULL) && (lib->freef != NULL)) |
| 814 | lib->freef(lib->data, (void *) define->attrs); |
| 815 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 816 | if ((define->data != NULL) && |
| 817 | (define->type == XML_RELAXNG_INTERLEAVE)) |
| 818 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 819 | if ((define->data != NULL) && |
| 820 | (define->type == XML_RELAXNG_CHOICE)) |
| 821 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 822 | if (define->name != NULL) |
| 823 | xmlFree(define->name); |
| 824 | if (define->ns != NULL) |
| 825 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 826 | if (define->value != NULL) |
| 827 | xmlFree(define->value); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 828 | if (define->contModel != NULL) |
| 829 | xmlRegFreeRegexp(define->contModel); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 830 | xmlFree(define); |
| 831 | } |
| 832 | |
| 833 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 834 | * xmlRelaxNGNewStates: |
| 835 | * @ctxt: a Relax-NG validation context |
| 836 | * @size: the default size for the container |
| 837 | * |
| 838 | * Allocate a new RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 839 | * |
| 840 | * Returns the newly allocated structure or NULL in case or error |
| 841 | */ |
| 842 | static xmlRelaxNGStatesPtr |
| 843 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 844 | { |
| 845 | xmlRelaxNGStatesPtr ret; |
| 846 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 847 | if ((ctxt != NULL) && |
| 848 | (ctxt->freeState != NULL) && |
| 849 | (ctxt->freeStatesNr > 0)) { |
| 850 | ctxt->freeStatesNr--; |
| 851 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 852 | ret->nbState = 0; |
| 853 | return(ret); |
| 854 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 855 | if (size < 16) size = 16; |
| 856 | |
| 857 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
| 858 | (size - 1) * sizeof(xmlRelaxNGValidStatePtr)); |
| 859 | if (ret == NULL) { |
| 860 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 861 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 862 | return (NULL); |
| 863 | } |
| 864 | ret->nbState = 0; |
| 865 | ret->maxState = size; |
| 866 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc( |
| 867 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 868 | if (ret->tabState == NULL) { |
| 869 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 870 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 871 | xmlFree(ret->tabState); |
| 872 | return (NULL); |
| 873 | } |
| 874 | return(ret); |
| 875 | } |
| 876 | |
| 877 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 878 | * xmlRelaxNGAddStateUniq: |
| 879 | * @ctxt: a Relax-NG validation context |
| 880 | * @states: the states container |
| 881 | * @state: the validation state |
| 882 | * |
| 883 | * Add a RelaxNG validation state to the container without checking |
| 884 | * for unicity. |
| 885 | * |
| 886 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 887 | */ |
| 888 | static int |
| 889 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
| 890 | xmlRelaxNGStatesPtr states, |
| 891 | xmlRelaxNGValidStatePtr state) |
| 892 | { |
| 893 | if (state == NULL) { |
| 894 | return(-1); |
| 895 | } |
| 896 | if (states->nbState >= states->maxState) { |
| 897 | xmlRelaxNGValidStatePtr *tmp; |
| 898 | int size; |
| 899 | |
| 900 | size = states->maxState * 2; |
| 901 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 902 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 903 | if (tmp == NULL) { |
| 904 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 905 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 906 | return(-1); |
| 907 | } |
| 908 | states->tabState = tmp; |
| 909 | states->maxState = size; |
| 910 | } |
| 911 | states->tabState[states->nbState++] = state; |
| 912 | return(1); |
| 913 | } |
| 914 | |
| 915 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 916 | * xmlRelaxNGAddState: |
| 917 | * @ctxt: a Relax-NG validation context |
| 918 | * @states: the states container |
| 919 | * @state: the validation state |
| 920 | * |
| 921 | * Add a RelaxNG validation state to the container |
| 922 | * |
| 923 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 924 | */ |
| 925 | static int |
| 926 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGStatesPtr states, |
| 927 | xmlRelaxNGValidStatePtr state) |
| 928 | { |
| 929 | int i; |
| 930 | |
| 931 | if (state == NULL) { |
| 932 | return(-1); |
| 933 | } |
| 934 | if (states->nbState >= states->maxState) { |
| 935 | xmlRelaxNGValidStatePtr *tmp; |
| 936 | int size; |
| 937 | |
| 938 | size = states->maxState * 2; |
| 939 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 940 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 941 | if (tmp == NULL) { |
| 942 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 943 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 944 | return(-1); |
| 945 | } |
| 946 | states->tabState = tmp; |
| 947 | states->maxState = size; |
| 948 | } |
| 949 | for (i = 0;i < states->nbState;i++) { |
| 950 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 951 | xmlRelaxNGFreeValidState(ctxt, state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 952 | return(0); |
| 953 | } |
| 954 | } |
| 955 | states->tabState[states->nbState++] = state; |
| 956 | return(1); |
| 957 | } |
| 958 | |
| 959 | /** |
| 960 | * xmlRelaxNGFreeStates: |
| 961 | * @ctxt: a Relax-NG validation context |
| 962 | * @states: teh container |
| 963 | * |
| 964 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 965 | */ |
| 966 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 967 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 968 | xmlRelaxNGStatesPtr states) |
| 969 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 970 | if (states == NULL) |
| 971 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 972 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
| 973 | ctxt->freeStatesMax = 40; |
| 974 | ctxt->freeStatesNr = 0; |
| 975 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 976 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 977 | if (ctxt->freeStates == NULL) { |
| 978 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 979 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 980 | } |
| 981 | } else if ((ctxt != NULL) && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 982 | xmlRelaxNGStatesPtr *tmp; |
| 983 | |
| 984 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 985 | 2 * ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 986 | if (tmp == NULL) { |
| 987 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 988 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 989 | xmlFree(states->tabState); |
| 990 | xmlFree(states); |
| 991 | return; |
| 992 | } |
| 993 | ctxt->freeStates = tmp; |
| 994 | ctxt->freeStatesMax *= 2; |
| 995 | } |
| 996 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 997 | xmlFree(states->tabState); |
| 998 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 999 | } else { |
| 1000 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1005 | * xmlRelaxNGNewValidState: |
| 1006 | * @ctxt: a Relax-NG validation context |
| 1007 | * @node: the current node or NULL for the document |
| 1008 | * |
| 1009 | * Allocate a new RelaxNG validation state |
| 1010 | * |
| 1011 | * Returns the newly allocated structure or NULL in case or error |
| 1012 | */ |
| 1013 | static xmlRelaxNGValidStatePtr |
| 1014 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 1015 | { |
| 1016 | xmlRelaxNGValidStatePtr ret; |
| 1017 | xmlAttrPtr attr; |
| 1018 | xmlAttrPtr attrs[MAX_ATTR]; |
| 1019 | int nbAttrs = 0; |
| 1020 | xmlNodePtr root = NULL; |
| 1021 | |
| 1022 | if (node == NULL) { |
| 1023 | root = xmlDocGetRootElement(ctxt->doc); |
| 1024 | if (root == NULL) |
| 1025 | return(NULL); |
| 1026 | } else { |
| 1027 | attr = node->properties; |
| 1028 | while (attr != NULL) { |
| 1029 | if (nbAttrs < MAX_ATTR) |
| 1030 | attrs[nbAttrs++] = attr; |
| 1031 | else |
| 1032 | nbAttrs++; |
| 1033 | attr = attr->next; |
| 1034 | } |
| 1035 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1036 | if ((ctxt->freeState != NULL) && |
| 1037 | (ctxt->freeState->nbState > 0)) { |
| 1038 | ctxt->freeState->nbState--; |
| 1039 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1040 | } else { |
| 1041 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1042 | if (ret == NULL) { |
| 1043 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1044 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1045 | return (NULL); |
| 1046 | } |
| 1047 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1048 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1049 | ret->value = NULL; |
| 1050 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1051 | if (node == NULL) { |
| 1052 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1053 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1054 | } else { |
| 1055 | ret->node = node; |
| 1056 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1057 | } |
| 1058 | ret->nbAttrs = 0; |
| 1059 | if (nbAttrs > 0) { |
| 1060 | if (ret->attrs == NULL) { |
| 1061 | if (nbAttrs < 4) ret->maxAttrs = 4; |
| 1062 | else ret->maxAttrs = nbAttrs; |
| 1063 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1064 | sizeof(xmlAttrPtr)); |
| 1065 | if (ret->attrs == NULL) { |
| 1066 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1067 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1068 | return (ret); |
| 1069 | } |
| 1070 | } else if (ret->maxAttrs < nbAttrs) { |
| 1071 | xmlAttrPtr *tmp; |
| 1072 | |
| 1073 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1074 | sizeof(xmlAttrPtr)); |
| 1075 | if (tmp == NULL) { |
| 1076 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1077 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1078 | return (ret); |
| 1079 | } |
| 1080 | ret->attrs = tmp; |
Daniel Veillard | 73827cb | 2003-08-25 10:57:27 +0000 | [diff] [blame] | 1081 | ret->maxAttrs = nbAttrs; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1082 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1083 | ret->nbAttrs = nbAttrs; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1084 | if (nbAttrs < MAX_ATTR) { |
| 1085 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1086 | } else { |
| 1087 | attr = node->properties; |
| 1088 | nbAttrs = 0; |
| 1089 | while (attr != NULL) { |
| 1090 | ret->attrs[nbAttrs++] = attr; |
| 1091 | attr = attr->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1095 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1096 | return (ret); |
| 1097 | } |
| 1098 | |
| 1099 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1100 | * xmlRelaxNGCopyValidState: |
| 1101 | * @ctxt: a Relax-NG validation context |
| 1102 | * @state: a validation state |
| 1103 | * |
| 1104 | * Copy the validation state |
| 1105 | * |
| 1106 | * Returns the newly allocated structure or NULL in case or error |
| 1107 | */ |
| 1108 | static xmlRelaxNGValidStatePtr |
| 1109 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1110 | xmlRelaxNGValidStatePtr state) |
| 1111 | { |
| 1112 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1113 | unsigned int maxAttrs; |
| 1114 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1115 | |
| 1116 | if (state == NULL) |
| 1117 | return(NULL); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1118 | if ((ctxt->freeState != NULL) && |
| 1119 | (ctxt->freeState->nbState > 0)) { |
| 1120 | ctxt->freeState->nbState--; |
| 1121 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1122 | } else { |
| 1123 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1124 | if (ret == NULL) { |
| 1125 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1126 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1127 | return (NULL); |
| 1128 | } |
| 1129 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1130 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1131 | attrs = ret->attrs; |
| 1132 | maxAttrs = ret->maxAttrs; |
| 1133 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1134 | ret->attrs = attrs; |
| 1135 | ret->maxAttrs = maxAttrs; |
| 1136 | if (state->nbAttrs > 0) { |
| 1137 | if (ret->attrs == NULL) { |
| 1138 | ret->maxAttrs = state->maxAttrs; |
| 1139 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1140 | sizeof(xmlAttrPtr)); |
| 1141 | if (ret->attrs == NULL) { |
| 1142 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1143 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1144 | ret->nbAttrs = 0; |
| 1145 | return (ret); |
| 1146 | } |
| 1147 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1148 | xmlAttrPtr *tmp; |
| 1149 | |
| 1150 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1151 | sizeof(xmlAttrPtr)); |
| 1152 | if (tmp == NULL) { |
| 1153 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1154 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1155 | ret->nbAttrs = 0; |
| 1156 | return (ret); |
| 1157 | } |
| 1158 | ret->maxAttrs = state->maxAttrs; |
Daniel Veillard | 73827cb | 2003-08-25 10:57:27 +0000 | [diff] [blame] | 1159 | ret->attrs = tmp; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1160 | } |
| 1161 | memcpy(ret->attrs, state->attrs, state->nbAttrs * sizeof(xmlAttrPtr)); |
| 1162 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1163 | return(ret); |
| 1164 | } |
| 1165 | |
| 1166 | /** |
| 1167 | * xmlRelaxNGEqualValidState: |
| 1168 | * @ctxt: a Relax-NG validation context |
| 1169 | * @state1: a validation state |
| 1170 | * @state2: a validation state |
| 1171 | * |
| 1172 | * Compare the validation states for equality |
| 1173 | * |
| 1174 | * Returns 1 if equald, 0 otherwise |
| 1175 | */ |
| 1176 | static int |
| 1177 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1178 | xmlRelaxNGValidStatePtr state1, |
| 1179 | xmlRelaxNGValidStatePtr state2) |
| 1180 | { |
| 1181 | int i; |
| 1182 | |
| 1183 | if ((state1 == NULL) || (state2 == NULL)) |
| 1184 | return(0); |
| 1185 | if (state1 == state2) |
| 1186 | return(1); |
| 1187 | if (state1->node != state2->node) |
| 1188 | return(0); |
| 1189 | if (state1->seq != state2->seq) |
| 1190 | return(0); |
| 1191 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
| 1192 | return(0); |
| 1193 | if (state1->nbAttrs != state2->nbAttrs) |
| 1194 | return(0); |
| 1195 | if (state1->endvalue != state2->endvalue) |
| 1196 | return(0); |
| 1197 | if ((state1->value != state2->value) && |
| 1198 | (!xmlStrEqual(state1->value, state2->value))) |
| 1199 | return(0); |
| 1200 | for (i = 0;i < state1->nbAttrs;i++) { |
| 1201 | if (state1->attrs[i] != state2->attrs[i]) |
| 1202 | return(0); |
| 1203 | } |
| 1204 | return(1); |
| 1205 | } |
| 1206 | |
| 1207 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1208 | * xmlRelaxNGFreeValidState: |
| 1209 | * @state: a validation state structure |
| 1210 | * |
| 1211 | * Deallocate a RelaxNG validation state structure. |
| 1212 | */ |
| 1213 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1214 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1215 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1216 | { |
| 1217 | if (state == NULL) |
| 1218 | return; |
| 1219 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1220 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
| 1221 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
| 1222 | } |
| 1223 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
| 1224 | if (state->attrs != NULL) |
| 1225 | xmlFree(state->attrs); |
| 1226 | xmlFree(state); |
| 1227 | } else { |
| 1228 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
| 1229 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /************************************************************************ |
| 1233 | * * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1234 | * Document functions * |
| 1235 | * * |
| 1236 | ************************************************************************/ |
| 1237 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
| 1238 | xmlDocPtr doc); |
| 1239 | |
| 1240 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1241 | * xmlRelaxNGIncludePush: |
| 1242 | * @ctxt: the parser context |
| 1243 | * @value: the element doc |
| 1244 | * |
| 1245 | * Pushes a new include on top of the include stack |
| 1246 | * |
| 1247 | * Returns 0 in case of error, the index in the stack otherwise |
| 1248 | */ |
| 1249 | static int |
| 1250 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1251 | xmlRelaxNGIncludePtr value) |
| 1252 | { |
| 1253 | if (ctxt->incTab == NULL) { |
| 1254 | ctxt->incMax = 4; |
| 1255 | ctxt->incNr = 0; |
| 1256 | ctxt->incTab = (xmlRelaxNGIncludePtr *) xmlMalloc( |
| 1257 | ctxt->incMax * sizeof(ctxt->incTab[0])); |
| 1258 | if (ctxt->incTab == NULL) { |
| 1259 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1260 | return (0); |
| 1261 | } |
| 1262 | } |
| 1263 | if (ctxt->incNr >= ctxt->incMax) { |
| 1264 | ctxt->incMax *= 2; |
| 1265 | ctxt->incTab = |
| 1266 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
| 1267 | ctxt->incMax * |
| 1268 | sizeof(ctxt->incTab[0])); |
| 1269 | if (ctxt->incTab == NULL) { |
| 1270 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1271 | return (0); |
| 1272 | } |
| 1273 | } |
| 1274 | ctxt->incTab[ctxt->incNr] = value; |
| 1275 | ctxt->inc = value; |
| 1276 | return (ctxt->incNr++); |
| 1277 | } |
| 1278 | |
| 1279 | /** |
| 1280 | * xmlRelaxNGIncludePop: |
| 1281 | * @ctxt: the parser context |
| 1282 | * |
| 1283 | * Pops the top include from the include stack |
| 1284 | * |
| 1285 | * Returns the include just removed |
| 1286 | */ |
| 1287 | static xmlRelaxNGIncludePtr |
| 1288 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1289 | { |
| 1290 | xmlRelaxNGIncludePtr ret; |
| 1291 | |
| 1292 | if (ctxt->incNr <= 0) |
| 1293 | return (0); |
| 1294 | ctxt->incNr--; |
| 1295 | if (ctxt->incNr > 0) |
| 1296 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1297 | else |
| 1298 | ctxt->inc = NULL; |
| 1299 | ret = ctxt->incTab[ctxt->incNr]; |
| 1300 | ctxt->incTab[ctxt->incNr] = 0; |
| 1301 | return (ret); |
| 1302 | } |
| 1303 | |
| 1304 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1305 | * xmlRelaxNGRemoveRedefine: |
| 1306 | * @ctxt: the parser context |
| 1307 | * @URL: the normalized URL |
| 1308 | * @target: the included target |
| 1309 | * @name: the define name to eliminate |
| 1310 | * |
| 1311 | * Applies the elimination algorithm of 4.7 |
| 1312 | * |
| 1313 | * Returns 0 in case of error, 1 in case of success. |
| 1314 | */ |
| 1315 | static int |
| 1316 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
| 1317 | const xmlChar *URL ATTRIBUTE_UNUSED, |
| 1318 | xmlNodePtr target, const xmlChar *name) { |
| 1319 | int found = 0; |
| 1320 | xmlNodePtr tmp, tmp2; |
| 1321 | xmlChar *name2; |
| 1322 | |
| 1323 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1324 | if (name == NULL) |
| 1325 | xmlGenericError(xmlGenericErrorContext, |
| 1326 | "Elimination of <include> start from %s\n", URL); |
| 1327 | else |
| 1328 | xmlGenericError(xmlGenericErrorContext, |
| 1329 | "Elimination of <include> define %s from %s\n", name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1330 | #endif |
| 1331 | tmp = target; |
| 1332 | while (tmp != NULL) { |
| 1333 | tmp2 = tmp->next; |
| 1334 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1335 | found = 1; |
| 1336 | xmlUnlinkNode(tmp); |
| 1337 | xmlFreeNode(tmp); |
| 1338 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1339 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1340 | xmlRelaxNGNormExtSpace(name2); |
| 1341 | if (name2 != NULL) { |
| 1342 | if (xmlStrEqual(name, name2)) { |
| 1343 | found = 1; |
| 1344 | xmlUnlinkNode(tmp); |
| 1345 | xmlFreeNode(tmp); |
| 1346 | } |
| 1347 | xmlFree(name2); |
| 1348 | } |
| 1349 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1350 | xmlChar *href = NULL; |
| 1351 | xmlRelaxNGDocumentPtr inc = tmp->_private; |
| 1352 | |
| 1353 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1354 | (inc->doc->children != NULL)) { |
| 1355 | |
| 1356 | if (xmlStrEqual(inc->doc->children->name, BAD_CAST "grammar")) { |
| 1357 | #ifdef DEBUG_INCLUDE |
| 1358 | href = xmlGetProp(tmp, BAD_CAST "href"); |
| 1359 | #endif |
| 1360 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1361 | inc->doc->children->children, name) == 1) { |
| 1362 | found = 1; |
| 1363 | } |
| 1364 | if (href != NULL) |
| 1365 | xmlFree(href); |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | tmp = tmp2; |
| 1370 | } |
| 1371 | return(found); |
| 1372 | } |
| 1373 | |
| 1374 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1375 | * xmlRelaxNGLoadInclude: |
| 1376 | * @ctxt: the parser context |
| 1377 | * @URL: the normalized URL |
| 1378 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1379 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1380 | * |
| 1381 | * First lookup if the document is already loaded into the parser context, |
| 1382 | * check against recursion. If not found the resource is loaded and |
| 1383 | * the content is preprocessed before being returned back to the caller. |
| 1384 | * |
| 1385 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1386 | */ |
| 1387 | static xmlRelaxNGIncludePtr |
| 1388 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1389 | xmlNodePtr node, const xmlChar *ns) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1390 | xmlRelaxNGIncludePtr ret = NULL; |
| 1391 | xmlDocPtr doc; |
| 1392 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1393 | xmlNodePtr root, cur; |
| 1394 | |
| 1395 | #ifdef DEBUG_INCLUDE |
| 1396 | xmlGenericError(xmlGenericErrorContext, |
| 1397 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
| 1398 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1399 | |
| 1400 | /* |
| 1401 | * check against recursion in the stack |
| 1402 | */ |
| 1403 | for (i = 0;i < ctxt->incNr;i++) { |
| 1404 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1405 | if (ctxt->error != NULL) |
| 1406 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1407 | "Detected an Include recursion for %s\n", |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1408 | URL); |
| 1409 | ctxt->nbErrors++; |
| 1410 | return(NULL); |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1415 | * load the document |
| 1416 | */ |
| 1417 | doc = xmlParseFile((const char *) URL); |
| 1418 | if (doc == NULL) { |
| 1419 | if (ctxt->error != NULL) |
| 1420 | ctxt->error(ctxt->userData, |
| 1421 | "xmlRelaxNG: could not load %s\n", URL); |
| 1422 | ctxt->nbErrors++; |
| 1423 | return (NULL); |
| 1424 | } |
| 1425 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1426 | #ifdef DEBUG_INCLUDE |
| 1427 | xmlGenericError(xmlGenericErrorContext, |
| 1428 | "Parsed %s Okay\n", URL); |
| 1429 | #endif |
| 1430 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1431 | /* |
| 1432 | * Allocate the document structures and register it first. |
| 1433 | */ |
| 1434 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1435 | if (ret == NULL) { |
| 1436 | if (ctxt->error != NULL) |
| 1437 | ctxt->error(ctxt->userData, |
| 1438 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1439 | ctxt->nbErrors++; |
| 1440 | xmlFreeDoc(doc); |
| 1441 | return (NULL); |
| 1442 | } |
| 1443 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1444 | ret->doc = doc; |
| 1445 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1446 | ret->next = ctxt->includes; |
| 1447 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1448 | |
| 1449 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1450 | * transmit the ns if needed |
| 1451 | */ |
| 1452 | if (ns != NULL) { |
| 1453 | root = xmlDocGetRootElement(doc); |
| 1454 | if (root != NULL) { |
| 1455 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1456 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1462 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1463 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1464 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1465 | |
| 1466 | /* |
| 1467 | * Some preprocessing of the document content, this include recursing |
| 1468 | * in the include stack. |
| 1469 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1470 | #ifdef DEBUG_INCLUDE |
| 1471 | xmlGenericError(xmlGenericErrorContext, |
| 1472 | "cleanup of %s\n", URL); |
| 1473 | #endif |
| 1474 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1475 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1476 | if (doc == NULL) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1477 | ctxt->inc = NULL; |
| 1478 | return(NULL); |
| 1479 | } |
| 1480 | |
| 1481 | /* |
| 1482 | * Pop up the include from the stack |
| 1483 | */ |
| 1484 | xmlRelaxNGIncludePop(ctxt); |
| 1485 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1486 | #ifdef DEBUG_INCLUDE |
| 1487 | xmlGenericError(xmlGenericErrorContext, |
| 1488 | "Checking of %s\n", URL); |
| 1489 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1490 | /* |
| 1491 | * Check that the top element is a grammar |
| 1492 | */ |
| 1493 | root = xmlDocGetRootElement(doc); |
| 1494 | if (root == NULL) { |
| 1495 | if (ctxt->error != NULL) |
| 1496 | ctxt->error(ctxt->userData, |
| 1497 | "xmlRelaxNG: included document is empty %s\n", URL); |
| 1498 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1499 | return (NULL); |
| 1500 | } |
| 1501 | if (!IS_RELAXNG(root, "grammar")) { |
| 1502 | if (ctxt->error != NULL) |
| 1503 | ctxt->error(ctxt->userData, |
| 1504 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1505 | URL); |
| 1506 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1507 | return (NULL); |
| 1508 | } |
| 1509 | |
| 1510 | /* |
| 1511 | * Elimination of redefined rules in the include. |
| 1512 | */ |
| 1513 | cur = node->children; |
| 1514 | while (cur != NULL) { |
| 1515 | if (IS_RELAXNG(cur, "start")) { |
| 1516 | int found = 0; |
| 1517 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1518 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1519 | if (!found) { |
| 1520 | if (ctxt->error != NULL) |
| 1521 | ctxt->error(ctxt->userData, |
| 1522 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1523 | URL); |
| 1524 | ctxt->nbErrors++; |
| 1525 | } |
| 1526 | } else if (IS_RELAXNG(cur, "define")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1527 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1528 | |
| 1529 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1530 | if (name == NULL) { |
| 1531 | if (ctxt->error != NULL) |
| 1532 | ctxt->error(ctxt->userData, |
| 1533 | "xmlRelaxNG: include %s has define without name\n", |
| 1534 | URL); |
| 1535 | ctxt->nbErrors++; |
| 1536 | } else { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1537 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1538 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1539 | xmlRelaxNGNormExtSpace(name); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1540 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1541 | root->children, name); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1542 | if (!found) { |
| 1543 | if (ctxt->error != NULL) |
| 1544 | ctxt->error(ctxt->userData, |
| 1545 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1546 | URL, name); |
| 1547 | ctxt->nbErrors++; |
| 1548 | } |
| 1549 | xmlFree(name); |
| 1550 | } |
| 1551 | } |
| 1552 | cur = cur->next; |
| 1553 | } |
| 1554 | |
| 1555 | |
| 1556 | return(ret); |
| 1557 | } |
| 1558 | |
| 1559 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1560 | * xmlRelaxNGValidErrorPush: |
| 1561 | * @ctxt: the validation context |
| 1562 | * @err: the error code |
| 1563 | * @arg1: the first string argument |
| 1564 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1565 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1566 | * |
| 1567 | * Pushes a new error on top of the error stack |
| 1568 | * |
| 1569 | * Returns 0 in case of error, the index in the stack otherwise |
| 1570 | */ |
| 1571 | static int |
| 1572 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1573 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1574 | { |
| 1575 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1576 | #ifdef DEBUG_ERROR |
| 1577 | xmlGenericError(xmlGenericErrorContext, |
| 1578 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
| 1579 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1580 | if (ctxt->errTab == NULL) { |
| 1581 | ctxt->errMax = 8; |
| 1582 | ctxt->errNr = 0; |
| 1583 | ctxt->errTab = (xmlRelaxNGValidErrorPtr) xmlMalloc( |
| 1584 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
| 1585 | if (ctxt->errTab == NULL) { |
| 1586 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1587 | return (0); |
| 1588 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1589 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1590 | } |
| 1591 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1592 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1593 | ctxt->errTab = |
| 1594 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1595 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1596 | if (ctxt->errTab == NULL) { |
| 1597 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1598 | return (0); |
| 1599 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1600 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1601 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1602 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1603 | (ctxt->err->node == ctxt->state->node) && |
| 1604 | (ctxt->err->err == err)) |
| 1605 | return(ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1606 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1607 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1608 | if (dup) { |
| 1609 | cur->arg1 = xmlStrdup(arg1); |
| 1610 | cur->arg2 = xmlStrdup(arg2); |
| 1611 | cur->flags = ERROR_IS_DUP; |
| 1612 | } else { |
| 1613 | cur->arg1 = arg1; |
| 1614 | cur->arg2 = arg2; |
| 1615 | cur->flags = 0; |
| 1616 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1617 | if (ctxt->state != NULL) { |
| 1618 | cur->node = ctxt->state->node; |
| 1619 | cur->seq = ctxt->state->seq; |
| 1620 | } else { |
| 1621 | cur->node = NULL; |
| 1622 | cur->seq = NULL; |
| 1623 | } |
| 1624 | ctxt->err = cur; |
| 1625 | return (ctxt->errNr++); |
| 1626 | } |
| 1627 | |
| 1628 | /** |
| 1629 | * xmlRelaxNGValidErrorPop: |
| 1630 | * @ctxt: the validation context |
| 1631 | * |
| 1632 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1633 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1634 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1635 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1636 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1637 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1638 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1639 | if (ctxt->errNr <= 0) { |
| 1640 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1641 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1642 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1643 | ctxt->errNr--; |
| 1644 | if (ctxt->errNr > 0) |
| 1645 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1646 | else |
| 1647 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1648 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1649 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1650 | if (cur->arg1 != NULL) |
| 1651 | xmlFree((xmlChar *)cur->arg1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1652 | cur->arg1 = NULL; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1653 | if (cur->arg2 != NULL) |
| 1654 | xmlFree((xmlChar *)cur->arg2); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1655 | cur->arg2 = NULL; |
| 1656 | cur->flags = 0; |
| 1657 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1660 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1661 | * xmlRelaxNGDocumentPush: |
| 1662 | * @ctxt: the parser context |
| 1663 | * @value: the element doc |
| 1664 | * |
| 1665 | * Pushes a new doc on top of the doc stack |
| 1666 | * |
| 1667 | * Returns 0 in case of error, the index in the stack otherwise |
| 1668 | */ |
| 1669 | static int |
| 1670 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1671 | xmlRelaxNGDocumentPtr value) |
| 1672 | { |
| 1673 | if (ctxt->docTab == NULL) { |
| 1674 | ctxt->docMax = 4; |
| 1675 | ctxt->docNr = 0; |
| 1676 | ctxt->docTab = (xmlRelaxNGDocumentPtr *) xmlMalloc( |
| 1677 | ctxt->docMax * sizeof(ctxt->docTab[0])); |
| 1678 | if (ctxt->docTab == NULL) { |
| 1679 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1680 | return (0); |
| 1681 | } |
| 1682 | } |
| 1683 | if (ctxt->docNr >= ctxt->docMax) { |
| 1684 | ctxt->docMax *= 2; |
| 1685 | ctxt->docTab = |
| 1686 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
| 1687 | ctxt->docMax * |
| 1688 | sizeof(ctxt->docTab[0])); |
| 1689 | if (ctxt->docTab == NULL) { |
| 1690 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1691 | return (0); |
| 1692 | } |
| 1693 | } |
| 1694 | ctxt->docTab[ctxt->docNr] = value; |
| 1695 | ctxt->doc = value; |
| 1696 | return (ctxt->docNr++); |
| 1697 | } |
| 1698 | |
| 1699 | /** |
| 1700 | * xmlRelaxNGDocumentPop: |
| 1701 | * @ctxt: the parser context |
| 1702 | * |
| 1703 | * Pops the top doc from the doc stack |
| 1704 | * |
| 1705 | * Returns the doc just removed |
| 1706 | */ |
| 1707 | static xmlRelaxNGDocumentPtr |
| 1708 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1709 | { |
| 1710 | xmlRelaxNGDocumentPtr ret; |
| 1711 | |
| 1712 | if (ctxt->docNr <= 0) |
| 1713 | return (0); |
| 1714 | ctxt->docNr--; |
| 1715 | if (ctxt->docNr > 0) |
| 1716 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1717 | else |
| 1718 | ctxt->doc = NULL; |
| 1719 | ret = ctxt->docTab[ctxt->docNr]; |
| 1720 | ctxt->docTab[ctxt->docNr] = 0; |
| 1721 | return (ret); |
| 1722 | } |
| 1723 | |
| 1724 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1725 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1726 | * @ctxt: the parser context |
| 1727 | * @URL: the normalized URL |
| 1728 | * @ns: the inherited ns if any |
| 1729 | * |
| 1730 | * First lookup if the document is already loaded into the parser context, |
| 1731 | * check against recursion. If not found the resource is loaded and |
| 1732 | * the content is preprocessed before being returned back to the caller. |
| 1733 | * |
| 1734 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1735 | */ |
| 1736 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1737 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1738 | const xmlChar *ns) { |
| 1739 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1740 | xmlDocPtr doc; |
| 1741 | xmlNodePtr root; |
| 1742 | int i; |
| 1743 | |
| 1744 | /* |
| 1745 | * check against recursion in the stack |
| 1746 | */ |
| 1747 | for (i = 0;i < ctxt->docNr;i++) { |
| 1748 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1749 | if (ctxt->error != NULL) |
| 1750 | ctxt->error(ctxt->userData, |
| 1751 | "Detected an externalRef recursion for %s\n", |
| 1752 | URL); |
| 1753 | ctxt->nbErrors++; |
| 1754 | return(NULL); |
| 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1759 | * load the document |
| 1760 | */ |
| 1761 | doc = xmlParseFile((const char *) URL); |
| 1762 | if (doc == NULL) { |
| 1763 | if (ctxt->error != NULL) |
| 1764 | ctxt->error(ctxt->userData, |
| 1765 | "xmlRelaxNG: could not load %s\n", URL); |
| 1766 | ctxt->nbErrors++; |
| 1767 | return (NULL); |
| 1768 | } |
| 1769 | |
| 1770 | /* |
| 1771 | * Allocate the document structures and register it first. |
| 1772 | */ |
| 1773 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1774 | if (ret == NULL) { |
| 1775 | if (ctxt->error != NULL) |
| 1776 | ctxt->error(ctxt->userData, |
| 1777 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1778 | ctxt->nbErrors++; |
| 1779 | xmlFreeDoc(doc); |
| 1780 | return (NULL); |
| 1781 | } |
| 1782 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1783 | ret->doc = doc; |
| 1784 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1785 | ret->next = ctxt->documents; |
| 1786 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1787 | |
| 1788 | /* |
| 1789 | * transmit the ns if needed |
| 1790 | */ |
| 1791 | if (ns != NULL) { |
| 1792 | root = xmlDocGetRootElement(doc); |
| 1793 | if (root != NULL) { |
| 1794 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1795 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1796 | } |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | /* |
| 1801 | * push it on the stack and register it in the hash table |
| 1802 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1803 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1804 | |
| 1805 | /* |
| 1806 | * Some preprocessing of the document content |
| 1807 | */ |
| 1808 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1809 | if (doc == NULL) { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1810 | ctxt->doc = NULL; |
| 1811 | return(NULL); |
| 1812 | } |
| 1813 | |
| 1814 | xmlRelaxNGDocumentPop(ctxt); |
| 1815 | |
| 1816 | return(ret); |
| 1817 | } |
| 1818 | |
| 1819 | /************************************************************************ |
| 1820 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1821 | * Error functions * |
| 1822 | * * |
| 1823 | ************************************************************************/ |
| 1824 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1825 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 1826 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 1827 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 1828 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 1829 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1830 | |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1831 | static const char * |
| 1832 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) { |
| 1833 | if (def == NULL) |
| 1834 | return("none"); |
| 1835 | switch(def->type) { |
| 1836 | case XML_RELAXNG_EMPTY: return("empty"); |
| 1837 | case XML_RELAXNG_NOT_ALLOWED: return("notAllowed"); |
| 1838 | case XML_RELAXNG_EXCEPT: return("except"); |
| 1839 | case XML_RELAXNG_TEXT: return("text"); |
| 1840 | case XML_RELAXNG_ELEMENT: return("element"); |
| 1841 | case XML_RELAXNG_DATATYPE: return("datatype"); |
| 1842 | case XML_RELAXNG_VALUE: return("value"); |
| 1843 | case XML_RELAXNG_LIST: return("list"); |
| 1844 | case XML_RELAXNG_ATTRIBUTE: return("attribute"); |
| 1845 | case XML_RELAXNG_DEF: return("def"); |
| 1846 | case XML_RELAXNG_REF: return("ref"); |
| 1847 | case XML_RELAXNG_EXTERNALREF: return("externalRef"); |
| 1848 | case XML_RELAXNG_PARENTREF: return("parentRef"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1849 | case XML_RELAXNG_OPTIONAL: return("optional"); |
| 1850 | case XML_RELAXNG_ZEROORMORE: return("zeroOrMore"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1851 | case XML_RELAXNG_ONEORMORE: return("oneOrMore"); |
| 1852 | case XML_RELAXNG_CHOICE: return("choice"); |
| 1853 | case XML_RELAXNG_GROUP: return("group"); |
| 1854 | case XML_RELAXNG_INTERLEAVE: return("interleave"); |
| 1855 | case XML_RELAXNG_START: return("start"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1856 | case XML_RELAXNG_NOOP: return("noop"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1857 | case XML_RELAXNG_PARAM: return("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1858 | } |
| 1859 | return("unknown"); |
| 1860 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1861 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1862 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1863 | * xmlRelaxNGGetErrorString: |
| 1864 | * @err: the error code |
| 1865 | * @arg1: the first string argument |
| 1866 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1867 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1868 | * computes a formatted error string for the given error code and args |
| 1869 | * |
| 1870 | * Returns the error string, it must be deallocated by the caller |
| 1871 | */ |
| 1872 | static xmlChar * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1873 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar *arg1, |
| 1874 | const xmlChar *arg2) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1875 | char msg[1000]; |
| 1876 | |
| 1877 | if (arg1 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1878 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1879 | if (arg2 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1880 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1881 | |
| 1882 | msg[0] = 0; |
| 1883 | switch (err) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1884 | case XML_RELAXNG_OK: |
| 1885 | return(NULL); |
| 1886 | case XML_RELAXNG_ERR_MEMORY: |
| 1887 | return(xmlCharStrdup("out of memory")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1888 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1889 | snprintf(msg, 1000, "failed to validate type %s", arg1); |
| 1890 | break; |
| 1891 | case XML_RELAXNG_ERR_TYPEVAL: |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 1892 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'", arg1, arg2); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1893 | break; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1894 | case XML_RELAXNG_ERR_DUPID: |
| 1895 | snprintf(msg, 1000, "ID %s redefined", arg1); |
| 1896 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1897 | case XML_RELAXNG_ERR_TYPECMP: |
| 1898 | snprintf(msg, 1000, "failed to compare type %s", arg1); |
| 1899 | break; |
| 1900 | case XML_RELAXNG_ERR_NOSTATE: |
| 1901 | return(xmlCharStrdup("Internal error: no state")); |
| 1902 | case XML_RELAXNG_ERR_NODEFINE: |
| 1903 | return(xmlCharStrdup("Internal error: no define")); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1904 | case XML_RELAXNG_ERR_INTERNAL: |
| 1905 | snprintf(msg, 1000, "Internal error: %s", arg1); |
| 1906 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1907 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 1908 | snprintf(msg, 1000, "Extra data in list: %s", arg1); |
| 1909 | break; |
| 1910 | case XML_RELAXNG_ERR_INTERNODATA: |
| 1911 | return(xmlCharStrdup("Internal: interleave block has no data")); |
| 1912 | case XML_RELAXNG_ERR_INTERSEQ: |
| 1913 | return(xmlCharStrdup("Invalid sequence in interleave")); |
| 1914 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 1915 | snprintf(msg, 1000, "Extra element %s in interleave", arg1); |
| 1916 | break; |
| 1917 | case XML_RELAXNG_ERR_ELEMNAME: |
| 1918 | snprintf(msg, 1000, "Expecting element %s, got %s", arg1, arg2); |
| 1919 | break; |
| 1920 | case XML_RELAXNG_ERR_ELEMNONS: |
| 1921 | snprintf(msg, 1000, "Expecting a namespace for element %s", arg1); |
| 1922 | break; |
| 1923 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 1924 | snprintf(msg, 1000, "Element %s has wrong namespace: expecting %s", |
| 1925 | arg1, arg2); |
| 1926 | break; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 1927 | case XML_RELAXNG_ERR_ELEMWRONG: |
| 1928 | snprintf(msg, 1000, "Did not expect element %s there", |
| 1929 | arg1); |
| 1930 | break; |
| 1931 | case XML_RELAXNG_ERR_TEXTWRONG: |
| 1932 | snprintf(msg, 1000, "Did not expect text in element %s content", |
| 1933 | arg1); |
| 1934 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1935 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 1936 | snprintf(msg, 1000, "Expecting no namespace for element %s", arg1); |
| 1937 | break; |
| 1938 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 1939 | snprintf(msg, 1000, "Expecting element %s to be empty", arg1); |
| 1940 | break; |
| 1941 | case XML_RELAXNG_ERR_NOELEM: |
| 1942 | snprintf(msg, 1000, "Expecting an element %s, got nothing", arg1); |
| 1943 | break; |
| 1944 | case XML_RELAXNG_ERR_NOTELEM: |
| 1945 | return(xmlCharStrdup("Expecting an element got text")); |
| 1946 | case XML_RELAXNG_ERR_ATTRVALID: |
| 1947 | snprintf(msg, 1000, "Element %s failed to validate attributes", |
| 1948 | arg1); |
| 1949 | break; |
| 1950 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 1951 | snprintf(msg, 1000, "Element %s failed to validate content", |
| 1952 | arg1); |
| 1953 | break; |
| 1954 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 1955 | snprintf(msg, 1000, "Element %s has extra content: %s", |
| 1956 | arg1, arg2); |
| 1957 | break; |
| 1958 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 1959 | snprintf(msg, 1000, "Invalid attribute %s for element %s", |
| 1960 | arg1, arg2); |
| 1961 | break; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 1962 | case XML_RELAXNG_ERR_LACKDATA: |
| 1963 | snprintf(msg, 1000, "Datatype element %s contains no data", |
| 1964 | arg1); |
| 1965 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1966 | case XML_RELAXNG_ERR_DATAELEM: |
| 1967 | snprintf(msg, 1000, "Datatype element %s has child elements", |
| 1968 | arg1); |
| 1969 | break; |
| 1970 | case XML_RELAXNG_ERR_VALELEM: |
| 1971 | snprintf(msg, 1000, "Value element %s has child elements", |
| 1972 | arg1); |
| 1973 | break; |
| 1974 | case XML_RELAXNG_ERR_LISTELEM: |
| 1975 | snprintf(msg, 1000, "List element %s has child elements", |
| 1976 | arg1); |
| 1977 | break; |
| 1978 | case XML_RELAXNG_ERR_DATATYPE: |
| 1979 | snprintf(msg, 1000, "Error validating datatype %s", |
| 1980 | arg1); |
| 1981 | break; |
| 1982 | case XML_RELAXNG_ERR_VALUE: |
| 1983 | snprintf(msg, 1000, "Error validating value %s", |
| 1984 | arg1); |
| 1985 | break; |
| 1986 | case XML_RELAXNG_ERR_LIST: |
| 1987 | return(xmlCharStrdup("Error validating list")); |
| 1988 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 1989 | return(xmlCharStrdup("No top grammar defined")); |
| 1990 | case XML_RELAXNG_ERR_EXTRADATA: |
| 1991 | return(xmlCharStrdup("Extra data in the document")); |
| 1992 | default: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 1993 | return(xmlCharStrdup("Unknown error !")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1994 | } |
| 1995 | if (msg[0] == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1996 | snprintf(msg, 1000, "Unknown error code %d", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1997 | } |
Daniel Veillard | adbb0e6 | 2003-05-10 20:02:45 +0000 | [diff] [blame] | 1998 | msg[1000 - 1] = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1999 | return(xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | /** |
| 2003 | * xmlRelaxNGValidErrorContext: |
| 2004 | * @ctxt: the validation context |
| 2005 | * @node: the node |
| 2006 | * @child: the node child generating the problem. |
| 2007 | * |
| 2008 | * Dump informations about the kocation of the error in the instance |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2009 | */ |
| 2010 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2011 | xmlRelaxNGValidErrorContext(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, |
| 2012 | xmlNodePtr child) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2013 | { |
| 2014 | int line = 0; |
| 2015 | const xmlChar *file = NULL; |
| 2016 | const xmlChar *name = NULL; |
| 2017 | const char *type = "error"; |
| 2018 | |
| 2019 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 2020 | return; |
| 2021 | |
| 2022 | if (child != NULL) |
| 2023 | node = child; |
| 2024 | |
| 2025 | if (node != NULL) { |
| 2026 | if ((node->type == XML_DOCUMENT_NODE) || |
| 2027 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
| 2028 | xmlDocPtr doc = (xmlDocPtr) node; |
| 2029 | |
| 2030 | file = doc->URL; |
| 2031 | } else { |
| 2032 | /* |
| 2033 | * Try to find contextual informations to report |
| 2034 | */ |
| 2035 | if (node->type == XML_ELEMENT_NODE) { |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 2036 | line = (long) node->content; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2037 | } else if ((node->prev != NULL) && |
| 2038 | (node->prev->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 2039 | line = (long) node->prev->content; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2040 | } else if ((node->parent != NULL) && |
| 2041 | (node->parent->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 2042 | line = (long) node->parent->content; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2043 | } |
| 2044 | if ((node->doc != NULL) && (node->doc->URL != NULL)) |
| 2045 | file = node->doc->URL; |
| 2046 | if (node->name != NULL) |
| 2047 | name = node->name; |
| 2048 | } |
| 2049 | } |
| 2050 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2051 | type = "RNG validity error"; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2052 | |
| 2053 | if ((file != NULL) && (line != 0) && (name != NULL)) |
| 2054 | ctxt->error(ctxt->userData, "%s: file %s line %d element %s\n", |
| 2055 | type, file, line, name); |
| 2056 | else if ((file != NULL) && (name != NULL)) |
| 2057 | ctxt->error(ctxt->userData, "%s: file %s element %s\n", |
| 2058 | type, file, name); |
| 2059 | else if ((file != NULL) && (line != 0)) |
| 2060 | ctxt->error(ctxt->userData, "%s: file %s line %d\n", type, file, line); |
| 2061 | else if (file != NULL) |
| 2062 | ctxt->error(ctxt->userData, "%s: file %s\n", type, file); |
| 2063 | else if (name != NULL) |
| 2064 | ctxt->error(ctxt->userData, "%s: element %s\n", type, name); |
| 2065 | else |
| 2066 | ctxt->error(ctxt->userData, "%s\n", type); |
| 2067 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2068 | |
| 2069 | /** |
| 2070 | * xmlRelaxNGShowValidError: |
| 2071 | * @ctxt: the validation context |
| 2072 | * @err: the error number |
| 2073 | * @node: the node |
| 2074 | * @child: the node child generating the problem. |
| 2075 | * @arg1: the first argument |
| 2076 | * @arg2: the second argument |
| 2077 | * |
| 2078 | * Show a validation error. |
| 2079 | */ |
| 2080 | static void |
| 2081 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
| 2082 | xmlNodePtr node, xmlNodePtr child, |
| 2083 | const xmlChar *arg1, const xmlChar *arg2) |
| 2084 | { |
| 2085 | xmlChar *msg; |
| 2086 | |
| 2087 | if (ctxt->error == NULL) |
| 2088 | return; |
| 2089 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2090 | #ifdef DEBUG_ERROR |
| 2091 | xmlGenericError(xmlGenericErrorContext, |
| 2092 | "Show error %d\n", err); |
| 2093 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2094 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2095 | if (msg == NULL) |
| 2096 | return; |
| 2097 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2098 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 2099 | ctxt->errNo = err; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2100 | xmlRelaxNGValidErrorContext(ctxt, node, child); |
| 2101 | ctxt->error(ctxt->userData, "%s\n", msg); |
| 2102 | xmlFree(msg); |
| 2103 | } |
| 2104 | |
| 2105 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2106 | * xmlRelaxNGPopErrors: |
| 2107 | * @ctxt: the validation context |
| 2108 | * @level: the error level in the stack |
| 2109 | * |
| 2110 | * pop and discard all errors until the given level is reached |
| 2111 | */ |
| 2112 | static void |
| 2113 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) { |
| 2114 | int i; |
| 2115 | xmlRelaxNGValidErrorPtr err; |
| 2116 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2117 | #ifdef DEBUG_ERROR |
| 2118 | xmlGenericError(xmlGenericErrorContext, |
| 2119 | "Pop errors till level %d\n", level); |
| 2120 | #endif |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2121 | for (i = level;i < ctxt->errNr;i++) { |
| 2122 | err = &ctxt->errTab[i]; |
| 2123 | if (err->flags & ERROR_IS_DUP) { |
| 2124 | if (err->arg1 != NULL) |
| 2125 | xmlFree((xmlChar *)err->arg1); |
| 2126 | err->arg1 = NULL; |
| 2127 | if (err->arg2 != NULL) |
| 2128 | xmlFree((xmlChar *)err->arg2); |
| 2129 | err->arg2 = NULL; |
| 2130 | err->flags = 0; |
| 2131 | } |
| 2132 | } |
| 2133 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2134 | if (ctxt->errNr <= 0) |
| 2135 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2136 | } |
| 2137 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2138 | * xmlRelaxNGDumpValidError: |
| 2139 | * @ctxt: the validation context |
| 2140 | * |
| 2141 | * Show all validation error over a given index. |
| 2142 | */ |
| 2143 | static void |
| 2144 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2145 | int i, j, k; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2146 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2147 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2148 | #ifdef DEBUG_ERROR |
| 2149 | xmlGenericError(xmlGenericErrorContext, |
| 2150 | "Dumping error stack %d errors\n", ctxt->errNr); |
| 2151 | #endif |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2152 | for (i = 0, k = 0;i < ctxt->errNr;i++) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2153 | err = &ctxt->errTab[i]; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2154 | if (k < MAX_ERROR) { |
| 2155 | for (j = 0;j < i;j++) { |
| 2156 | dup = &ctxt->errTab[j]; |
| 2157 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2158 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2159 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2160 | goto skip; |
| 2161 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2162 | } |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2163 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2164 | err->arg1, err->arg2); |
| 2165 | k++; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2166 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2167 | skip: |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2168 | if (err->flags & ERROR_IS_DUP) { |
| 2169 | if (err->arg1 != NULL) |
| 2170 | xmlFree((xmlChar *)err->arg1); |
| 2171 | err->arg1 = NULL; |
| 2172 | if (err->arg2 != NULL) |
| 2173 | xmlFree((xmlChar *)err->arg2); |
| 2174 | err->arg2 = NULL; |
| 2175 | err->flags = 0; |
| 2176 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2177 | } |
| 2178 | ctxt->errNr = 0; |
| 2179 | } |
| 2180 | /** |
| 2181 | * xmlRelaxNGAddValidError: |
| 2182 | * @ctxt: the validation context |
| 2183 | * @err: the error number |
| 2184 | * @arg1: the first argument |
| 2185 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2186 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2187 | * |
| 2188 | * Register a validation error, either generating it if it's sure |
| 2189 | * or stacking it for later handling if unsure. |
| 2190 | */ |
| 2191 | static void |
| 2192 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2193 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2194 | { |
| 2195 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 2196 | return; |
| 2197 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2198 | #ifdef DEBUG_ERROR |
| 2199 | xmlGenericError(xmlGenericErrorContext, |
| 2200 | "Adding error %d\n", err); |
| 2201 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2202 | /* |
| 2203 | * generate the error directly |
| 2204 | */ |
| 2205 | if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) { |
| 2206 | xmlNodePtr node, seq; |
| 2207 | /* |
| 2208 | * Flush first any stacked error which might be the |
| 2209 | * real cause of the problem. |
| 2210 | */ |
| 2211 | if (ctxt->errNr != 0) |
| 2212 | xmlRelaxNGDumpValidError(ctxt); |
| 2213 | if (ctxt->state != NULL) { |
| 2214 | node = ctxt->state->node; |
| 2215 | seq = ctxt->state->seq; |
| 2216 | } else { |
| 2217 | node = seq = NULL; |
| 2218 | } |
| 2219 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
| 2220 | } |
| 2221 | /* |
| 2222 | * Stack the error for later processing if needed |
| 2223 | */ |
| 2224 | else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2225 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2226 | } |
| 2227 | } |
| 2228 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2229 | |
| 2230 | /************************************************************************ |
| 2231 | * * |
| 2232 | * Type library hooks * |
| 2233 | * * |
| 2234 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2235 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
| 2236 | const xmlChar *str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2237 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2238 | /** |
| 2239 | * xmlRelaxNGSchemaTypeHave: |
| 2240 | * @data: data needed for the library |
| 2241 | * @type: the type name |
| 2242 | * |
| 2243 | * Check if the given type is provided by |
| 2244 | * the W3C XMLSchema Datatype library. |
| 2245 | * |
| 2246 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2247 | */ |
| 2248 | static int |
| 2249 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2250 | const xmlChar *type) { |
| 2251 | xmlSchemaTypePtr typ; |
| 2252 | |
| 2253 | if (type == NULL) |
| 2254 | return(-1); |
| 2255 | typ = xmlSchemaGetPredefinedType(type, |
| 2256 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2257 | if (typ == NULL) |
| 2258 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2259 | return(1); |
| 2260 | } |
| 2261 | |
| 2262 | /** |
| 2263 | * xmlRelaxNGSchemaTypeCheck: |
| 2264 | * @data: data needed for the library |
| 2265 | * @type: the type name |
| 2266 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2267 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2268 | * |
| 2269 | * Check if the given type and value are validated by |
| 2270 | * the W3C XMLSchema Datatype library. |
| 2271 | * |
| 2272 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2273 | */ |
| 2274 | static int |
| 2275 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2276 | const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2277 | const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2278 | void **result, |
| 2279 | xmlNodePtr node) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2280 | xmlSchemaTypePtr typ; |
| 2281 | int ret; |
| 2282 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2283 | if ((type == NULL) || (value == NULL)) |
| 2284 | return(-1); |
| 2285 | typ = xmlSchemaGetPredefinedType(type, |
| 2286 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2287 | if (typ == NULL) |
| 2288 | return(-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2289 | ret = xmlSchemaValPredefTypeNode(typ, value, |
| 2290 | (xmlSchemaValPtr *) result, node); |
| 2291 | if (ret == 2) /* special ID error code */ |
| 2292 | return(2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2293 | if (ret == 0) |
| 2294 | return(1); |
| 2295 | if (ret > 0) |
| 2296 | return(0); |
| 2297 | return(-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2301 | * xmlRelaxNGSchemaFacetCheck: |
| 2302 | * @data: data needed for the library |
| 2303 | * @type: the type name |
| 2304 | * @facet: the facet name |
| 2305 | * @val: the facet value |
| 2306 | * @strval: the string value |
| 2307 | * @value: the value to check |
| 2308 | * |
| 2309 | * Function provided by a type library to check a value facet |
| 2310 | * |
| 2311 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2312 | */ |
| 2313 | static int |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2314 | xmlRelaxNGSchemaFacetCheck (void *data ATTRIBUTE_UNUSED, const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2315 | const xmlChar *facetname, const xmlChar *val, |
| 2316 | const xmlChar *strval, void *value) { |
| 2317 | xmlSchemaFacetPtr facet; |
| 2318 | xmlSchemaTypePtr typ; |
| 2319 | int ret; |
| 2320 | |
| 2321 | if ((type == NULL) || (strval == NULL)) |
| 2322 | return(-1); |
| 2323 | typ = xmlSchemaGetPredefinedType(type, |
| 2324 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2325 | if (typ == NULL) |
| 2326 | return(-1); |
| 2327 | |
| 2328 | facet = xmlSchemaNewFacet(); |
| 2329 | if (facet == NULL) |
| 2330 | return(-1); |
| 2331 | |
| 2332 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
| 2333 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
| 2334 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
| 2335 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
| 2336 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
| 2337 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
| 2338 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
| 2339 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
| 2340 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
| 2341 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
| 2342 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
| 2343 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
| 2344 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
| 2345 | facet->type = XML_SCHEMA_FACET_PATTERN; |
| 2346 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
| 2347 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
| 2348 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
| 2349 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
| 2350 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
| 2351 | facet->type = XML_SCHEMA_FACET_LENGTH; |
| 2352 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
| 2353 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2354 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2355 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2356 | } else { |
| 2357 | xmlSchemaFreeFacet(facet); |
| 2358 | return(-1); |
| 2359 | } |
| 2360 | facet->value = xmlStrdup(val); |
| 2361 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2362 | if (ret != 0) { |
| 2363 | xmlSchemaFreeFacet(facet); |
| 2364 | return(-1); |
| 2365 | } |
| 2366 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2367 | xmlSchemaFreeFacet(facet); |
| 2368 | if (ret != 0) |
| 2369 | return(-1); |
| 2370 | return(0); |
| 2371 | } |
| 2372 | |
| 2373 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2374 | * xmlRelaxNGSchemaFreeValue: |
| 2375 | * @data: data needed for the library |
| 2376 | * @value: the value to free |
| 2377 | * |
| 2378 | * Function provided by a type library to free a Schemas value |
| 2379 | * |
| 2380 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2381 | */ |
| 2382 | static void |
| 2383 | xmlRelaxNGSchemaFreeValue (void *data ATTRIBUTE_UNUSED, void *value) { |
| 2384 | xmlSchemaFreeValue(value); |
| 2385 | } |
| 2386 | |
| 2387 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2388 | * xmlRelaxNGSchemaTypeCompare: |
| 2389 | * @data: data needed for the library |
| 2390 | * @type: the type name |
| 2391 | * @value1: the first value |
| 2392 | * @value2: the second value |
| 2393 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2394 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2395 | * Datatype library. |
| 2396 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2397 | * 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] | 2398 | */ |
| 2399 | static int |
| 2400 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2401 | const xmlChar *type, |
| 2402 | const xmlChar *value1, |
| 2403 | xmlNodePtr ctxt1, |
| 2404 | void *comp1, |
| 2405 | const xmlChar *value2, |
| 2406 | xmlNodePtr ctxt2) { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2407 | int ret; |
| 2408 | xmlSchemaTypePtr typ; |
| 2409 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2410 | |
| 2411 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
| 2412 | return(-1); |
| 2413 | typ = xmlSchemaGetPredefinedType(type, |
| 2414 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2415 | if (typ == NULL) |
| 2416 | return(-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2417 | if (comp1 == NULL) { |
| 2418 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2419 | if (ret != 0) |
| 2420 | return(-1); |
| 2421 | if (res1 == NULL) |
| 2422 | return(-1); |
| 2423 | } else { |
| 2424 | res1 = (xmlSchemaValPtr) comp1; |
| 2425 | } |
| 2426 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2427 | if (ret != 0) { |
| 2428 | xmlSchemaFreeValue(res1); |
| 2429 | return(-1); |
| 2430 | } |
| 2431 | if (res1 == NULL) { |
| 2432 | xmlSchemaFreeValue(res1); |
| 2433 | return(-1); |
| 2434 | } |
| 2435 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2436 | if (res1 != (xmlSchemaValPtr) comp1) |
| 2437 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2438 | xmlSchemaFreeValue(res2); |
| 2439 | if (ret == -2) |
| 2440 | return(-1); |
| 2441 | if (ret == 0) |
| 2442 | return(1); |
| 2443 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | /** |
| 2447 | * xmlRelaxNGDefaultTypeHave: |
| 2448 | * @data: data needed for the library |
| 2449 | * @type: the type name |
| 2450 | * |
| 2451 | * Check if the given type is provided by |
| 2452 | * the default datatype library. |
| 2453 | * |
| 2454 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2455 | */ |
| 2456 | static int |
| 2457 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar *type) { |
| 2458 | if (type == NULL) |
| 2459 | return(-1); |
| 2460 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2461 | return(1); |
| 2462 | if (xmlStrEqual(type, BAD_CAST "token")) |
| 2463 | return(1); |
| 2464 | return(0); |
| 2465 | } |
| 2466 | |
| 2467 | /** |
| 2468 | * xmlRelaxNGDefaultTypeCheck: |
| 2469 | * @data: data needed for the library |
| 2470 | * @type: the type name |
| 2471 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2472 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2473 | * |
| 2474 | * Check if the given type and value are validated by |
| 2475 | * the default datatype library. |
| 2476 | * |
| 2477 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2478 | */ |
| 2479 | static int |
| 2480 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
| 2481 | const xmlChar *type ATTRIBUTE_UNUSED, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2482 | const xmlChar *value ATTRIBUTE_UNUSED, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2483 | void **result ATTRIBUTE_UNUSED, |
| 2484 | xmlNodePtr node ATTRIBUTE_UNUSED) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2485 | if (value == NULL) |
| 2486 | return(-1); |
| 2487 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2488 | return(1); |
| 2489 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2490 | return(1); |
| 2491 | } |
| 2492 | |
| 2493 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
| 2496 | /** |
| 2497 | * xmlRelaxNGDefaultTypeCompare: |
| 2498 | * @data: data needed for the library |
| 2499 | * @type: the type name |
| 2500 | * @value1: the first value |
| 2501 | * @value2: the second value |
| 2502 | * |
| 2503 | * Compare two values accordingly a type from the default |
| 2504 | * datatype library. |
| 2505 | * |
| 2506 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2507 | */ |
| 2508 | static int |
| 2509 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2510 | const xmlChar *type, |
| 2511 | const xmlChar *value1, |
| 2512 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2513 | void *comp1 ATTRIBUTE_UNUSED, |
| 2514 | const xmlChar *value2, |
| 2515 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2516 | int ret = -1; |
| 2517 | |
| 2518 | if (xmlStrEqual(type, BAD_CAST "string")) { |
| 2519 | ret = xmlStrEqual(value1, value2); |
| 2520 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
| 2521 | if (!xmlStrEqual(value1, value2)) { |
| 2522 | xmlChar *nval, *nvalue; |
| 2523 | |
| 2524 | /* |
| 2525 | * TODO: trivial optimizations are possible by |
| 2526 | * computing at compile-time |
| 2527 | */ |
| 2528 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2529 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
| 2530 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2531 | if ((nval == NULL) || (nvalue == NULL)) |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2532 | ret = -1; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2533 | else if (xmlStrEqual(nval, nvalue)) |
| 2534 | ret = 1; |
| 2535 | else |
| 2536 | ret = 0; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2537 | if (nval != NULL) |
| 2538 | xmlFree(nval); |
| 2539 | if (nvalue != NULL) |
| 2540 | xmlFree(nvalue); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2541 | } else |
| 2542 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2543 | } |
| 2544 | return(ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | static int xmlRelaxNGTypeInitialized = 0; |
| 2548 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2549 | |
| 2550 | /** |
| 2551 | * xmlRelaxNGFreeTypeLibrary: |
| 2552 | * @lib: the type library structure |
| 2553 | * @namespace: the URI bound to the library |
| 2554 | * |
| 2555 | * Free the structure associated to the type library |
| 2556 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2557 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2558 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
| 2559 | const xmlChar *namespace ATTRIBUTE_UNUSED) { |
| 2560 | if (lib == NULL) |
| 2561 | return; |
| 2562 | if (lib->namespace != NULL) |
| 2563 | xmlFree((xmlChar *)lib->namespace); |
| 2564 | xmlFree(lib); |
| 2565 | } |
| 2566 | |
| 2567 | /** |
| 2568 | * xmlRelaxNGRegisterTypeLibrary: |
| 2569 | * @namespace: the URI bound to the library |
| 2570 | * @data: data associated to the library |
| 2571 | * @have: the provide function |
| 2572 | * @check: the checking function |
| 2573 | * @comp: the comparison function |
| 2574 | * |
| 2575 | * Register a new type library |
| 2576 | * |
| 2577 | * Returns 0 in case of success and -1 in case of error. |
| 2578 | */ |
| 2579 | static int |
| 2580 | xmlRelaxNGRegisterTypeLibrary(const xmlChar *namespace, void *data, |
| 2581 | xmlRelaxNGTypeHave have, xmlRelaxNGTypeCheck check, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2582 | xmlRelaxNGTypeCompare comp, xmlRelaxNGFacetCheck facet, |
| 2583 | xmlRelaxNGTypeFree freef) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2584 | xmlRelaxNGTypeLibraryPtr lib; |
| 2585 | int ret; |
| 2586 | |
| 2587 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
| 2588 | (check == NULL) || (comp == NULL)) |
| 2589 | return(-1); |
| 2590 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
| 2591 | xmlGenericError(xmlGenericErrorContext, |
| 2592 | "Relax-NG types library '%s' already registered\n", |
| 2593 | namespace); |
| 2594 | return(-1); |
| 2595 | } |
| 2596 | lib = (xmlRelaxNGTypeLibraryPtr) xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
| 2597 | if (lib == NULL) { |
| 2598 | xmlGenericError(xmlGenericErrorContext, |
| 2599 | "Relax-NG types library '%s' malloc() failed\n", |
| 2600 | namespace); |
| 2601 | return (-1); |
| 2602 | } |
| 2603 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2604 | lib->namespace = xmlStrdup(namespace); |
| 2605 | lib->data = data; |
| 2606 | lib->have = have; |
| 2607 | lib->comp = comp; |
| 2608 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2609 | lib->facet = facet; |
| 2610 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2611 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2612 | if (ret < 0) { |
| 2613 | xmlGenericError(xmlGenericErrorContext, |
| 2614 | "Relax-NG types library failed to register '%s'\n", |
| 2615 | namespace); |
| 2616 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2617 | return(-1); |
| 2618 | } |
| 2619 | return(0); |
| 2620 | } |
| 2621 | |
| 2622 | /** |
| 2623 | * xmlRelaxNGInitTypes: |
| 2624 | * |
| 2625 | * Initilize the default type libraries. |
| 2626 | * |
| 2627 | * Returns 0 in case of success and -1 in case of error. |
| 2628 | */ |
| 2629 | static int |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2630 | xmlRelaxNGInitTypes(void) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2631 | if (xmlRelaxNGTypeInitialized != 0) |
| 2632 | return(0); |
| 2633 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2634 | if (xmlRelaxNGRegisteredTypes == NULL) { |
| 2635 | xmlGenericError(xmlGenericErrorContext, |
| 2636 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2637 | return(-1); |
| 2638 | } |
| 2639 | xmlRelaxNGRegisterTypeLibrary( |
| 2640 | BAD_CAST "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2641 | NULL, |
| 2642 | xmlRelaxNGSchemaTypeHave, |
| 2643 | xmlRelaxNGSchemaTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2644 | xmlRelaxNGSchemaTypeCompare, |
| 2645 | xmlRelaxNGSchemaFacetCheck, |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2646 | xmlRelaxNGSchemaFreeValue); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2647 | xmlRelaxNGRegisterTypeLibrary( |
| 2648 | xmlRelaxNGNs, |
| 2649 | NULL, |
| 2650 | xmlRelaxNGDefaultTypeHave, |
| 2651 | xmlRelaxNGDefaultTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2652 | xmlRelaxNGDefaultTypeCompare, |
| 2653 | NULL, |
| 2654 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2655 | xmlRelaxNGTypeInitialized = 1; |
| 2656 | return(0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | /** |
| 2660 | * xmlRelaxNGCleanupTypes: |
| 2661 | * |
| 2662 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2663 | */ |
| 2664 | void |
| 2665 | xmlRelaxNGCleanupTypes(void) { |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2666 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2667 | if (xmlRelaxNGTypeInitialized == 0) |
| 2668 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2669 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
| 2670 | xmlRelaxNGFreeTypeLibrary); |
| 2671 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | /************************************************************************ |
| 2675 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2676 | * Compiling element content into regexp * |
| 2677 | * * |
| 2678 | * Sometime the element content can be compiled into a pure regexp, * |
| 2679 | * This allows a faster execution and streamability at that level * |
| 2680 | * * |
| 2681 | ************************************************************************/ |
| 2682 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2683 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2684 | xmlRelaxNGDefinePtr def); |
| 2685 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2686 | /** |
| 2687 | * xmlRelaxNGIsCompileable: |
| 2688 | * @define: the definition to check |
| 2689 | * |
| 2690 | * Check if a definition is nullable. |
| 2691 | * |
| 2692 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2693 | */ |
| 2694 | static int |
| 2695 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2696 | int ret = -1; |
| 2697 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2698 | if (def == NULL) { |
| 2699 | return(-1); |
| 2700 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2701 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2702 | (def->dflags & IS_COMPILABLE)) |
| 2703 | return(1); |
| 2704 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2705 | (def->dflags & IS_NOT_COMPILABLE)) |
| 2706 | return(0); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2707 | switch(def->type) { |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2708 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2709 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2710 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2711 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2712 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2713 | ret = 1; |
| 2714 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2715 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2716 | /* |
| 2717 | * Check if the element content is compileable |
| 2718 | */ |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2719 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2720 | ((def->dflags & IS_COMPILABLE) == 0)) { |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2721 | xmlRelaxNGDefinePtr list; |
| 2722 | list = def->content; |
| 2723 | while (list != NULL) { |
| 2724 | ret = xmlRelaxNGIsCompileable(list); |
| 2725 | if (ret != 1) |
| 2726 | break; |
| 2727 | list = list->next; |
| 2728 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2729 | if (ret == 0) def->dflags |= IS_NOT_COMPILABLE; |
| 2730 | if (ret == 1) def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2731 | #ifdef DEBUG_COMPILE |
| 2732 | if (ret == 1) { |
| 2733 | xmlGenericError(xmlGenericErrorContext, |
| 2734 | "element content for %s is compilable\n", |
| 2735 | def->name); |
| 2736 | } else if (ret == 0) { |
| 2737 | xmlGenericError(xmlGenericErrorContext, |
| 2738 | "element content for %s is not compilable\n", |
| 2739 | def->name); |
| 2740 | } else { |
| 2741 | xmlGenericError(xmlGenericErrorContext, |
| 2742 | "Problem in RelaxNGIsCompileable for element %s\n", |
| 2743 | def->name); |
| 2744 | } |
| 2745 | #endif |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2746 | } |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2747 | /* |
| 2748 | * All elements return a compileable status unless they |
| 2749 | * are generic like anyName |
| 2750 | */ |
| 2751 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2752 | ret = 0; |
| 2753 | else |
| 2754 | ret = 1; |
| 2755 | return(ret); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2756 | case XML_RELAXNG_REF: |
| 2757 | case XML_RELAXNG_EXTERNALREF: |
| 2758 | case XML_RELAXNG_PARENTREF: |
| 2759 | if (def->depth == -20) { |
| 2760 | return(1); |
| 2761 | } else { |
| 2762 | xmlRelaxNGDefinePtr list; |
| 2763 | |
| 2764 | def->depth = -20; |
| 2765 | list = def->content; |
| 2766 | while (list != NULL) { |
| 2767 | ret = xmlRelaxNGIsCompileable(list); |
| 2768 | if (ret != 1) |
| 2769 | break; |
| 2770 | list = list->next; |
| 2771 | } |
| 2772 | } |
| 2773 | break; |
| 2774 | case XML_RELAXNG_START: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2775 | case XML_RELAXNG_OPTIONAL: |
| 2776 | case XML_RELAXNG_ZEROORMORE: |
| 2777 | case XML_RELAXNG_ONEORMORE: |
| 2778 | case XML_RELAXNG_CHOICE: |
| 2779 | case XML_RELAXNG_GROUP: |
| 2780 | case XML_RELAXNG_DEF: { |
| 2781 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2782 | |
| 2783 | list = def->content; |
| 2784 | while (list != NULL) { |
| 2785 | ret = xmlRelaxNGIsCompileable(list); |
| 2786 | if (ret != 1) |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2787 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2788 | list = list->next; |
| 2789 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2790 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2791 | } |
| 2792 | case XML_RELAXNG_EXCEPT: |
| 2793 | case XML_RELAXNG_ATTRIBUTE: |
| 2794 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2795 | case XML_RELAXNG_DATATYPE: |
| 2796 | case XML_RELAXNG_LIST: |
| 2797 | case XML_RELAXNG_PARAM: |
| 2798 | case XML_RELAXNG_VALUE: |
| 2799 | ret = 0; |
| 2800 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2801 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2802 | ret = -1; |
| 2803 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2804 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2805 | if (ret == 0) def->dflags |= IS_NOT_COMPILABLE; |
| 2806 | if (ret == 1) def->dflags |= IS_COMPILABLE; |
Daniel Veillard | d94849b | 2003-07-28 13:02:24 +0000 | [diff] [blame] | 2807 | #ifdef DEBUG_COMPILE |
| 2808 | if (ret == 1) { |
| 2809 | xmlGenericError(xmlGenericErrorContext, |
| 2810 | "RelaxNGIsCompileable %s : true\n", |
| 2811 | xmlRelaxNGDefName(def)); |
| 2812 | } else if (ret == 0) { |
| 2813 | xmlGenericError(xmlGenericErrorContext, |
| 2814 | "RelaxNGIsCompileable %s : false\n", |
| 2815 | xmlRelaxNGDefName(def)); |
| 2816 | } else { |
| 2817 | xmlGenericError(xmlGenericErrorContext, |
| 2818 | "Problem in RelaxNGIsCompileable %s\n", |
| 2819 | xmlRelaxNGDefName(def)); |
| 2820 | } |
| 2821 | #endif |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2822 | return(ret); |
| 2823 | } |
| 2824 | |
| 2825 | /** |
| 2826 | * xmlRelaxNGCompile: |
| 2827 | * ctxt: the RelaxNG parser context |
| 2828 | * @define: the definition tree to compile |
| 2829 | * |
| 2830 | * Compile the set of definitions, it works recursively, till the |
| 2831 | * element boundaries, where it tries to compile the content if possible |
| 2832 | * |
| 2833 | * Returns 0 if success and -1 in case of error |
| 2834 | */ |
| 2835 | static int |
| 2836 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) { |
| 2837 | int ret = 0; |
| 2838 | xmlRelaxNGDefinePtr list; |
| 2839 | |
| 2840 | if ((ctxt == NULL) || (def == NULL)) return(-1); |
| 2841 | |
| 2842 | switch(def->type) { |
| 2843 | case XML_RELAXNG_START: |
| 2844 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
| 2845 | xmlAutomataPtr oldam = ctxt->am; |
| 2846 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2847 | |
| 2848 | def->depth = -25; |
| 2849 | |
| 2850 | list = def->content; |
| 2851 | ctxt->am = xmlNewAutomata(); |
| 2852 | if (ctxt->am == NULL) |
| 2853 | return(-1); |
| 2854 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2855 | while (list != NULL) { |
| 2856 | xmlRelaxNGCompile(ctxt, list); |
| 2857 | list = list->next; |
| 2858 | } |
| 2859 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2860 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 2861 | xmlRegexpIsDeterminist(def->contModel); |
| 2862 | |
| 2863 | xmlFreeAutomata(ctxt->am); |
| 2864 | ctxt->state = oldstate; |
| 2865 | ctxt->am = oldam; |
| 2866 | } |
| 2867 | break; |
| 2868 | case XML_RELAXNG_ELEMENT: |
| 2869 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 2870 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2871 | ctxt->state, NULL, def->name, def->ns, def); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2872 | } |
| 2873 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 2874 | xmlAutomataPtr oldam = ctxt->am; |
| 2875 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2876 | |
| 2877 | def->depth = -25; |
| 2878 | |
| 2879 | list = def->content; |
| 2880 | ctxt->am = xmlNewAutomata(); |
| 2881 | if (ctxt->am == NULL) |
| 2882 | return(-1); |
| 2883 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2884 | while (list != NULL) { |
| 2885 | xmlRelaxNGCompile(ctxt, list); |
| 2886 | list = list->next; |
| 2887 | } |
| 2888 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2889 | def->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2890 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
| 2891 | /* |
| 2892 | * we can only use the automata if it is determinist |
| 2893 | */ |
| 2894 | xmlRegFreeRegexp(def->contModel); |
| 2895 | def->contModel = NULL; |
| 2896 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2897 | xmlFreeAutomata(ctxt->am); |
| 2898 | ctxt->state = oldstate; |
| 2899 | ctxt->am = oldam; |
| 2900 | } else { |
| 2901 | xmlAutomataPtr oldam = ctxt->am; |
| 2902 | |
| 2903 | /* |
| 2904 | * we can't build the content model for this element content |
| 2905 | * but it still might be possible to build it for some of its |
| 2906 | * children, recurse. |
| 2907 | */ |
| 2908 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 2909 | ctxt->am = oldam; |
| 2910 | } |
| 2911 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2912 | case XML_RELAXNG_NOOP: |
| 2913 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 2914 | break; |
| 2915 | case XML_RELAXNG_OPTIONAL: { |
| 2916 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2917 | |
| 2918 | xmlRelaxNGCompile(ctxt, def->content); |
| 2919 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 2920 | break; |
| 2921 | } |
| 2922 | case XML_RELAXNG_ZEROORMORE: { |
| 2923 | xmlAutomataStatePtr oldstate; |
| 2924 | |
| 2925 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 2926 | oldstate = ctxt->state; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2927 | list = def->content; |
| 2928 | while (list != NULL) { |
| 2929 | xmlRelaxNGCompile(ctxt, list); |
| 2930 | list = list->next; |
| 2931 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2932 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 2933 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2934 | break; |
| 2935 | } |
| 2936 | case XML_RELAXNG_ONEORMORE: { |
| 2937 | xmlAutomataStatePtr oldstate; |
| 2938 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2939 | list = def->content; |
| 2940 | while (list != NULL) { |
| 2941 | xmlRelaxNGCompile(ctxt, list); |
| 2942 | list = list->next; |
| 2943 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2944 | oldstate = ctxt->state; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2945 | list = def->content; |
| 2946 | while (list != NULL) { |
| 2947 | xmlRelaxNGCompile(ctxt, list); |
| 2948 | list = list->next; |
| 2949 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2950 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 2951 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2952 | break; |
| 2953 | } |
| 2954 | case XML_RELAXNG_CHOICE: { |
| 2955 | xmlAutomataStatePtr target = NULL; |
| 2956 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2957 | |
| 2958 | list = def->content; |
| 2959 | while (list != NULL) { |
| 2960 | ctxt->state = oldstate; |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2961 | ret = xmlRelaxNGCompile(ctxt, list); |
| 2962 | if (ret != 0) |
| 2963 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2964 | if (target == NULL) |
| 2965 | target = ctxt->state; |
| 2966 | else { |
| 2967 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, target); |
| 2968 | } |
| 2969 | list = list->next; |
| 2970 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2971 | ctxt->state = target; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2972 | |
| 2973 | break; |
| 2974 | } |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2975 | case XML_RELAXNG_REF: |
| 2976 | case XML_RELAXNG_EXTERNALREF: |
| 2977 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2978 | case XML_RELAXNG_GROUP: |
| 2979 | case XML_RELAXNG_DEF: |
| 2980 | list = def->content; |
| 2981 | while (list != NULL) { |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 2982 | ret = xmlRelaxNGCompile(ctxt, list); |
| 2983 | if (ret != 0) |
| 2984 | break; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2985 | list = list->next; |
| 2986 | } |
| 2987 | break; |
| 2988 | case XML_RELAXNG_TEXT: { |
| 2989 | xmlAutomataStatePtr oldstate; |
| 2990 | |
| 2991 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 2992 | oldstate = ctxt->state; |
| 2993 | xmlRelaxNGCompile(ctxt, def->content); |
| 2994 | xmlAutomataNewTransition(ctxt->am, ctxt->state, ctxt->state, |
| 2995 | BAD_CAST "#text", NULL); |
| 2996 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2997 | break; |
| 2998 | } |
| 2999 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 3000 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3001 | break; |
| 3002 | case XML_RELAXNG_EXCEPT: |
| 3003 | case XML_RELAXNG_ATTRIBUTE: |
| 3004 | case XML_RELAXNG_INTERLEAVE: |
| 3005 | case XML_RELAXNG_NOT_ALLOWED: |
| 3006 | case XML_RELAXNG_DATATYPE: |
| 3007 | case XML_RELAXNG_LIST: |
| 3008 | case XML_RELAXNG_PARAM: |
| 3009 | case XML_RELAXNG_VALUE: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 3010 | /* This should not happen and generate an internal error */ |
| 3011 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
| 3012 | xmlRelaxNGDefName(def)); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3013 | break; |
| 3014 | } |
| 3015 | return(ret); |
| 3016 | } |
| 3017 | |
| 3018 | /** |
| 3019 | * xmlRelaxNGTryCompile: |
| 3020 | * ctxt: the RelaxNG parser context |
| 3021 | * @define: the definition tree to compile |
| 3022 | * |
| 3023 | * Try to compile the set of definitions, it works recursively, |
| 3024 | * possibly ignoring parts which cannot be compiled. |
| 3025 | * |
| 3026 | * Returns 0 if success and -1 in case of error |
| 3027 | */ |
| 3028 | static int |
| 3029 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) { |
| 3030 | int ret = 0; |
| 3031 | xmlRelaxNGDefinePtr list; |
| 3032 | |
| 3033 | if ((ctxt == NULL) || (def == NULL)) return(-1); |
| 3034 | |
| 3035 | if ((def->type == XML_RELAXNG_START) || |
| 3036 | (def->type == XML_RELAXNG_ELEMENT)) { |
| 3037 | ret = xmlRelaxNGIsCompileable(def); |
| 3038 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 3039 | ctxt->am = NULL; |
| 3040 | ret = xmlRelaxNGCompile(ctxt, def); |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3041 | #ifdef DEBUG_PROGRESSIVE |
| 3042 | if (ret == 0) { |
| 3043 | if (def->type == XML_RELAXNG_START) |
| 3044 | xmlGenericError(xmlGenericErrorContext, |
| 3045 | "compiled the start\n"); |
| 3046 | else |
| 3047 | xmlGenericError(xmlGenericErrorContext, |
| 3048 | "compiled element %s\n", def->name); |
| 3049 | } else { |
| 3050 | if (def->type == XML_RELAXNG_START) |
| 3051 | xmlGenericError(xmlGenericErrorContext, |
| 3052 | "failed to compile the start\n"); |
| 3053 | else |
| 3054 | xmlGenericError(xmlGenericErrorContext, |
| 3055 | "failed to compile element %s\n", def->name); |
| 3056 | } |
| 3057 | #endif |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3058 | return(ret); |
| 3059 | } |
| 3060 | } |
| 3061 | switch(def->type) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3062 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3063 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 3064 | break; |
| 3065 | case XML_RELAXNG_TEXT: |
| 3066 | case XML_RELAXNG_DATATYPE: |
| 3067 | case XML_RELAXNG_LIST: |
| 3068 | case XML_RELAXNG_PARAM: |
| 3069 | case XML_RELAXNG_VALUE: |
| 3070 | case XML_RELAXNG_EMPTY: |
| 3071 | case XML_RELAXNG_ELEMENT: |
| 3072 | ret = 0; |
| 3073 | break; |
| 3074 | case XML_RELAXNG_OPTIONAL: |
| 3075 | case XML_RELAXNG_ZEROORMORE: |
| 3076 | case XML_RELAXNG_ONEORMORE: |
| 3077 | case XML_RELAXNG_CHOICE: |
| 3078 | case XML_RELAXNG_GROUP: |
| 3079 | case XML_RELAXNG_DEF: |
Daniel Veillard | 2134ab1 | 2003-07-23 19:56:29 +0000 | [diff] [blame] | 3080 | case XML_RELAXNG_START: |
| 3081 | case XML_RELAXNG_REF: |
| 3082 | case XML_RELAXNG_EXTERNALREF: |
| 3083 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3084 | list = def->content; |
| 3085 | while (list != NULL) { |
| 3086 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3087 | if (ret != 0) |
| 3088 | break; |
| 3089 | list = list->next; |
| 3090 | } |
| 3091 | break; |
| 3092 | case XML_RELAXNG_EXCEPT: |
| 3093 | case XML_RELAXNG_ATTRIBUTE: |
| 3094 | case XML_RELAXNG_INTERLEAVE: |
| 3095 | case XML_RELAXNG_NOT_ALLOWED: |
| 3096 | ret = 0; |
| 3097 | break; |
| 3098 | } |
| 3099 | return(ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | /************************************************************************ |
| 3103 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3104 | * Parsing functions * |
| 3105 | * * |
| 3106 | ************************************************************************/ |
| 3107 | |
| 3108 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute( |
| 3109 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 3110 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement( |
| 3111 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 3112 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns( |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3113 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, int group); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3114 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern( |
| 3115 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 3116 | static xmlRelaxNGPtr xmlRelaxNGParseDocument( |
| 3117 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 3118 | static int xmlRelaxNGParseGrammarContent( |
| 3119 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 3120 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass( |
| 3121 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 3122 | xmlRelaxNGDefinePtr def); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 3123 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar( |
| 3124 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3125 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3126 | xmlRelaxNGDefinePtr define, xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3127 | |
| 3128 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3129 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3130 | |
| 3131 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3132 | * xmlRelaxNGIsNullable: |
| 3133 | * @define: the definition to verify |
| 3134 | * |
| 3135 | * Check if a definition is nullable. |
| 3136 | * |
| 3137 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3138 | */ |
| 3139 | static int |
| 3140 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) { |
| 3141 | int ret; |
| 3142 | if (define == NULL) |
| 3143 | return(-1); |
| 3144 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3145 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3146 | return(1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3147 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3148 | return(0); |
| 3149 | switch (define->type) { |
| 3150 | case XML_RELAXNG_EMPTY: |
| 3151 | case XML_RELAXNG_TEXT: |
| 3152 | ret = 1; break; |
| 3153 | case XML_RELAXNG_NOOP: |
| 3154 | case XML_RELAXNG_DEF: |
| 3155 | case XML_RELAXNG_REF: |
| 3156 | case XML_RELAXNG_EXTERNALREF: |
| 3157 | case XML_RELAXNG_PARENTREF: |
| 3158 | case XML_RELAXNG_ONEORMORE: |
| 3159 | ret = xmlRelaxNGIsNullable(define->content); |
| 3160 | break; |
| 3161 | case XML_RELAXNG_EXCEPT: |
| 3162 | case XML_RELAXNG_NOT_ALLOWED: |
| 3163 | case XML_RELAXNG_ELEMENT: |
| 3164 | case XML_RELAXNG_DATATYPE: |
| 3165 | case XML_RELAXNG_PARAM: |
| 3166 | case XML_RELAXNG_VALUE: |
| 3167 | case XML_RELAXNG_LIST: |
| 3168 | case XML_RELAXNG_ATTRIBUTE: |
| 3169 | ret = 0; break; |
| 3170 | case XML_RELAXNG_CHOICE: { |
| 3171 | xmlRelaxNGDefinePtr list = define->content; |
| 3172 | |
| 3173 | while (list != NULL) { |
| 3174 | ret = xmlRelaxNGIsNullable(list); |
| 3175 | if (ret != 0) |
| 3176 | goto done; |
| 3177 | list = list->next; |
| 3178 | } |
| 3179 | ret = 0; break; |
| 3180 | } |
| 3181 | case XML_RELAXNG_START: |
| 3182 | case XML_RELAXNG_INTERLEAVE: |
| 3183 | case XML_RELAXNG_GROUP: { |
| 3184 | xmlRelaxNGDefinePtr list = define->content; |
| 3185 | |
| 3186 | while (list != NULL) { |
| 3187 | ret = xmlRelaxNGIsNullable(list); |
| 3188 | if (ret != 1) |
| 3189 | goto done; |
| 3190 | list = list->next; |
| 3191 | } |
| 3192 | return(1); |
| 3193 | } |
| 3194 | default: |
| 3195 | return(-1); |
| 3196 | } |
| 3197 | done: |
| 3198 | if (ret == 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3199 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3200 | if (ret == 1) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3201 | define->dflags |= IS_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3202 | return(ret); |
| 3203 | } |
| 3204 | |
| 3205 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3206 | * xmlRelaxNGIsBlank: |
| 3207 | * @str: a string |
| 3208 | * |
| 3209 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3210 | * |
| 3211 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3212 | */ |
| 3213 | static int |
| 3214 | xmlRelaxNGIsBlank(xmlChar *str) { |
| 3215 | if (str == NULL) |
| 3216 | return(1); |
| 3217 | while (*str != 0) { |
| 3218 | if (!(IS_BLANK(*str))) return(0); |
| 3219 | str++; |
| 3220 | } |
| 3221 | return(1); |
| 3222 | } |
| 3223 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3224 | /** |
| 3225 | * xmlRelaxNGGetDataTypeLibrary: |
| 3226 | * @ctxt: a Relax-NG parser context |
| 3227 | * @node: the current data or value element |
| 3228 | * |
| 3229 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3230 | * |
| 3231 | * Returns the datatypeLibary value or NULL if not found |
| 3232 | */ |
| 3233 | static xmlChar * |
| 3234 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3235 | xmlNodePtr node) { |
| 3236 | xmlChar *ret, *escape; |
| 3237 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3238 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
| 3239 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3240 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3241 | if (ret[0] == 0) { |
| 3242 | xmlFree(ret); |
| 3243 | return(NULL); |
| 3244 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3245 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3246 | if (escape == NULL) { |
| 3247 | return(ret); |
| 3248 | } |
| 3249 | xmlFree(ret); |
| 3250 | return(escape); |
| 3251 | } |
| 3252 | } |
| 3253 | node = node->parent; |
| 3254 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3255 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3256 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3257 | if (ret[0] == 0) { |
| 3258 | xmlFree(ret); |
| 3259 | return(NULL); |
| 3260 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3261 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3262 | if (escape == NULL) { |
| 3263 | return(ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3264 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3265 | xmlFree(ret); |
| 3266 | return(escape); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3267 | } |
| 3268 | node = node->parent; |
| 3269 | } |
| 3270 | return(NULL); |
| 3271 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3272 | |
| 3273 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3274 | * xmlRelaxNGParseValue: |
| 3275 | * @ctxt: a Relax-NG parser context |
| 3276 | * @node: the data node. |
| 3277 | * |
| 3278 | * parse the content of a RelaxNG value node. |
| 3279 | * |
| 3280 | * Returns the definition pointer or NULL in case of error |
| 3281 | */ |
| 3282 | static xmlRelaxNGDefinePtr |
| 3283 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 3284 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3285 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3286 | xmlChar *type; |
| 3287 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3288 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3289 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3290 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3291 | if (def == NULL) |
| 3292 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3293 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3294 | |
| 3295 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3296 | if (type != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3297 | xmlRelaxNGNormExtSpace(type); |
| 3298 | if (xmlValidateNCName(type, 0)) { |
| 3299 | if (ctxt->error != NULL) |
| 3300 | ctxt->error(ctxt->userData, |
| 3301 | "value type '%s' is not an NCName\n", |
| 3302 | type); |
| 3303 | ctxt->nbErrors++; |
| 3304 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3305 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3306 | if (library == NULL) |
| 3307 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 3308 | |
| 3309 | def->name = type; |
| 3310 | def->ns = library; |
| 3311 | |
| 3312 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3313 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3314 | if (lib == NULL) { |
| 3315 | if (ctxt->error != NULL) |
| 3316 | ctxt->error(ctxt->userData, |
| 3317 | "Use of unregistered type library '%s'\n", |
| 3318 | library); |
| 3319 | ctxt->nbErrors++; |
| 3320 | def->data = NULL; |
| 3321 | } else { |
| 3322 | def->data = lib; |
| 3323 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3324 | if (ctxt->error != NULL) |
| 3325 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3326 | "Internal error with type library '%s': no 'have'\n", |
| 3327 | library); |
| 3328 | ctxt->nbErrors++; |
| 3329 | } else { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3330 | success = lib->have(lib->data, def->name); |
| 3331 | if (success != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3332 | if (ctxt->error != NULL) |
| 3333 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3334 | "Error type '%s' is not exported by type library '%s'\n", |
| 3335 | def->name, library); |
| 3336 | ctxt->nbErrors++; |
| 3337 | } |
| 3338 | } |
| 3339 | } |
| 3340 | } |
| 3341 | if (node->children == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 3342 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3343 | } else if (((node->children->type != XML_TEXT_NODE) && |
| 3344 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3345 | (node->children->next != NULL)) { |
| 3346 | if (ctxt->error != NULL) |
| 3347 | ctxt->error(ctxt->userData, |
| 3348 | "Expecting a single text value for <value>content\n"); |
| 3349 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3350 | } else if (def != NULL) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3351 | def->value = xmlNodeGetContent(node); |
| 3352 | if (def->value == NULL) { |
| 3353 | if (ctxt->error != NULL) |
| 3354 | ctxt->error(ctxt->userData, |
| 3355 | "Element <value> has no content\n"); |
| 3356 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3357 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3358 | void *val = NULL; |
| 3359 | |
| 3360 | success = lib->check(lib->data, def->name, def->value, &val, node); |
| 3361 | if (success != 1) { |
| 3362 | if (ctxt->error != NULL) |
| 3363 | ctxt->error(ctxt->userData, |
| 3364 | "Value '%s' is not acceptable for type '%s'\n", |
| 3365 | def->value, def->name); |
| 3366 | ctxt->nbErrors++; |
| 3367 | } else { |
| 3368 | if (val != NULL) |
| 3369 | def->attrs = val; |
| 3370 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3371 | } |
| 3372 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3373 | return(def); |
| 3374 | } |
| 3375 | |
| 3376 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3377 | * xmlRelaxNGParseData: |
| 3378 | * @ctxt: a Relax-NG parser context |
| 3379 | * @node: the data node. |
| 3380 | * |
| 3381 | * parse the content of a RelaxNG data node. |
| 3382 | * |
| 3383 | * Returns the definition pointer or NULL in case of error |
| 3384 | */ |
| 3385 | static xmlRelaxNGDefinePtr |
| 3386 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3387 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3388 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3389 | xmlRelaxNGTypeLibraryPtr lib; |
| 3390 | xmlChar *type; |
| 3391 | xmlChar *library; |
| 3392 | xmlNodePtr content; |
| 3393 | int tmp; |
| 3394 | |
| 3395 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3396 | if (type == NULL) { |
| 3397 | if (ctxt->error != NULL) |
| 3398 | ctxt->error(ctxt->userData, |
| 3399 | "data has no type\n"); |
| 3400 | ctxt->nbErrors++; |
| 3401 | return(NULL); |
| 3402 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3403 | xmlRelaxNGNormExtSpace(type); |
| 3404 | if (xmlValidateNCName(type, 0)) { |
| 3405 | if (ctxt->error != NULL) |
| 3406 | ctxt->error(ctxt->userData, |
| 3407 | "data type '%s' is not an NCName\n", |
| 3408 | type); |
| 3409 | ctxt->nbErrors++; |
| 3410 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3411 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3412 | if (library == NULL) |
| 3413 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 3414 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3415 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3416 | if (def == NULL) { |
| 3417 | xmlFree(type); |
| 3418 | return(NULL); |
| 3419 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3420 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3421 | def->name = type; |
| 3422 | def->ns = library; |
| 3423 | |
| 3424 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3425 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3426 | if (lib == NULL) { |
| 3427 | if (ctxt->error != NULL) |
| 3428 | ctxt->error(ctxt->userData, |
| 3429 | "Use of unregistered type library '%s'\n", |
| 3430 | library); |
| 3431 | ctxt->nbErrors++; |
| 3432 | def->data = NULL; |
| 3433 | } else { |
| 3434 | def->data = lib; |
| 3435 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3436 | if (ctxt->error != NULL) |
| 3437 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3438 | "Internal error with type library '%s': no 'have'\n", |
| 3439 | library); |
| 3440 | ctxt->nbErrors++; |
| 3441 | } else { |
| 3442 | tmp = lib->have(lib->data, def->name); |
| 3443 | if (tmp != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3444 | if (ctxt->error != NULL) |
| 3445 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3446 | "Error type '%s' is not exported by type library '%s'\n", |
| 3447 | def->name, library); |
| 3448 | ctxt->nbErrors++; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3449 | } else if ((xmlStrEqual(library, BAD_CAST |
| 3450 | "http://www.w3.org/2001/XMLSchema-datatypes")) && |
| 3451 | ((xmlStrEqual(def->name, BAD_CAST "IDREF")) || |
| 3452 | (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3453 | ctxt->idref = 1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3454 | } |
| 3455 | } |
| 3456 | } |
| 3457 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3458 | |
| 3459 | /* |
| 3460 | * Handle optional params |
| 3461 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3462 | while (content != NULL) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3463 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3464 | break; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3465 | if (xmlStrEqual(library, |
| 3466 | BAD_CAST"http://relaxng.org/ns/structure/1.0")) { |
| 3467 | if (ctxt->error != NULL) |
| 3468 | ctxt->error(ctxt->userData, |
| 3469 | "Type library '%s' does not allow type parameters\n", |
| 3470 | library); |
| 3471 | ctxt->nbErrors++; |
| 3472 | content = content->next; |
| 3473 | while ((content != NULL) && |
| 3474 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3475 | content = content->next; |
| 3476 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3477 | param = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3478 | if (param != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3479 | param->type = XML_RELAXNG_PARAM; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3480 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3481 | if (param->name == NULL) { |
| 3482 | if (ctxt->error != NULL) |
| 3483 | ctxt->error(ctxt->userData, |
| 3484 | "param has no name\n"); |
| 3485 | ctxt->nbErrors++; |
| 3486 | } |
| 3487 | param->value = xmlNodeGetContent(content); |
| 3488 | if (lastparam == NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3489 | def->attrs = lastparam = param; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3490 | } else { |
| 3491 | lastparam->next = param; |
| 3492 | lastparam = param; |
| 3493 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 3494 | if (lib != NULL) { |
| 3495 | } |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3496 | } |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3497 | content = content->next; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3498 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3499 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3500 | /* |
| 3501 | * Handle optional except |
| 3502 | */ |
| 3503 | if ((content != NULL) && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3504 | xmlNodePtr child; |
| 3505 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
| 3506 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3507 | except = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3508 | if (except == NULL) { |
| 3509 | return(def); |
| 3510 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3511 | except->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3512 | child = content->children; |
| 3513 | if (last == NULL) { |
| 3514 | def->content = except; |
| 3515 | } else { |
| 3516 | last->next = except; |
| 3517 | } |
| 3518 | if (child == NULL) { |
| 3519 | if (ctxt->error != NULL) |
| 3520 | ctxt->error(ctxt->userData, |
| 3521 | "except has no content\n"); |
| 3522 | ctxt->nbErrors++; |
| 3523 | } |
| 3524 | while (child != NULL) { |
| 3525 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3526 | if (tmp2 != NULL) { |
| 3527 | if (last2 == NULL) { |
| 3528 | except->content = last2 = tmp2; |
| 3529 | } else { |
| 3530 | last2->next = tmp2; |
| 3531 | last2 = tmp2; |
| 3532 | } |
| 3533 | } |
| 3534 | child = child->next; |
| 3535 | } |
| 3536 | content = content->next; |
| 3537 | } |
| 3538 | /* |
| 3539 | * Check there is no unhandled data |
| 3540 | */ |
| 3541 | if (content != NULL) { |
| 3542 | if (ctxt->error != NULL) |
| 3543 | ctxt->error(ctxt->userData, |
| 3544 | "Element data has unexpected content %s\n", content->name); |
| 3545 | ctxt->nbErrors++; |
| 3546 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3547 | |
| 3548 | return(def); |
| 3549 | } |
| 3550 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3551 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3552 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3553 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3554 | * xmlRelaxNGCompareNameClasses: |
| 3555 | * @defs1: the first element/attribute defs |
| 3556 | * @defs2: the second element/attribute defs |
| 3557 | * @name: the restriction on the name |
| 3558 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3559 | * |
| 3560 | * Compare the 2 lists of element definitions. The comparison is |
| 3561 | * that if both lists do not accept the same QNames, it returns 1 |
| 3562 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3563 | * |
| 3564 | * Returns 1 disttinct, 0 if equal |
| 3565 | */ |
| 3566 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3567 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
| 3568 | xmlRelaxNGDefinePtr def2) { |
| 3569 | int ret = 1; |
| 3570 | xmlNode node; |
| 3571 | xmlNs ns; |
| 3572 | xmlRelaxNGValidCtxt ctxt; |
| 3573 | ctxt.flags = FLAGS_IGNORABLE; |
| 3574 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3575 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3576 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3577 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
| 3578 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3579 | if (def2->type == XML_RELAXNG_TEXT) |
| 3580 | return(1); |
| 3581 | if (def1->name != NULL) { |
| 3582 | node.name = def1->name; |
| 3583 | } else { |
| 3584 | node.name = invalidName; |
| 3585 | } |
| 3586 | node.ns = &ns; |
| 3587 | if (def1->ns != NULL) { |
| 3588 | if (def1->ns[0] == 0) { |
| 3589 | node.ns = NULL; |
| 3590 | } else { |
| 3591 | ns.href = def1->ns; |
| 3592 | } |
| 3593 | } else { |
| 3594 | ns.href = invalidName; |
| 3595 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3596 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3597 | if (def1->nameClass != NULL) { |
| 3598 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3599 | } else { |
| 3600 | ret = 0; |
| 3601 | } |
| 3602 | } else { |
| 3603 | ret = 1; |
| 3604 | } |
| 3605 | } else if (def1->type == XML_RELAXNG_TEXT) { |
| 3606 | if (def2->type == XML_RELAXNG_TEXT) |
| 3607 | return(0); |
| 3608 | return(1); |
| 3609 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3610 | TODO |
| 3611 | ret = 0; |
| 3612 | } else { |
| 3613 | TODO |
| 3614 | ret = 0; |
| 3615 | } |
| 3616 | if (ret == 0) |
| 3617 | return(ret); |
| 3618 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
| 3619 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3620 | if (def2->name != NULL) { |
| 3621 | node.name = def2->name; |
| 3622 | } else { |
| 3623 | node.name = invalidName; |
| 3624 | } |
| 3625 | node.ns = &ns; |
| 3626 | if (def2->ns != NULL) { |
| 3627 | if (def2->ns[0] == 0) { |
| 3628 | node.ns = NULL; |
| 3629 | } else { |
| 3630 | ns.href = def2->ns; |
| 3631 | } |
| 3632 | } else { |
| 3633 | ns.href = invalidName; |
| 3634 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3635 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3636 | if (def2->nameClass != NULL) { |
| 3637 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3638 | } else { |
| 3639 | ret = 0; |
| 3640 | } |
| 3641 | } else { |
| 3642 | ret = 1; |
| 3643 | } |
| 3644 | } else { |
| 3645 | TODO |
| 3646 | ret = 0; |
| 3647 | } |
| 3648 | |
| 3649 | return(ret); |
| 3650 | } |
| 3651 | |
| 3652 | /** |
| 3653 | * xmlRelaxNGCompareElemDefLists: |
| 3654 | * @ctxt: a Relax-NG parser context |
| 3655 | * @defs1: the first list of element/attribute defs |
| 3656 | * @defs2: the second list of element/attribute defs |
| 3657 | * |
| 3658 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3659 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3660 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3661 | * |
| 3662 | * Returns 1 disttinct, 0 if equal |
| 3663 | */ |
| 3664 | static int |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3665 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3666 | xmlRelaxNGDefinePtr *def1, |
| 3667 | xmlRelaxNGDefinePtr *def2) { |
| 3668 | xmlRelaxNGDefinePtr *basedef2 = def2; |
| 3669 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3670 | if ((def1 == NULL) || (def2 == NULL)) |
| 3671 | return(1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3672 | if ((*def1 == NULL) || (*def2 == NULL)) |
| 3673 | return(1); |
| 3674 | while (*def1 != NULL) { |
| 3675 | while ((*def2) != NULL) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3676 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3677 | return(0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3678 | def2++; |
| 3679 | } |
| 3680 | def2 = basedef2; |
| 3681 | def1++; |
| 3682 | } |
| 3683 | return(1); |
| 3684 | } |
| 3685 | |
| 3686 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3687 | * xmlRelaxNGGenerateAttributes: |
| 3688 | * @ctxt: a Relax-NG parser context |
| 3689 | * @def: the definition definition |
| 3690 | * |
| 3691 | * Check if the definition can only generate attributes |
| 3692 | * |
| 3693 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3694 | */ |
| 3695 | static int |
| 3696 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
| 3697 | xmlRelaxNGDefinePtr def) { |
| 3698 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3699 | |
| 3700 | /* |
| 3701 | * Don't run that check in case of error. Infinite recursion |
| 3702 | * becomes possible. |
| 3703 | */ |
| 3704 | if (ctxt->nbErrors != 0) |
| 3705 | return(-1); |
| 3706 | |
| 3707 | parent = NULL; |
| 3708 | cur = def; |
| 3709 | while (cur != NULL) { |
| 3710 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3711 | (cur->type == XML_RELAXNG_TEXT) || |
| 3712 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3713 | (cur->type == XML_RELAXNG_PARAM) || |
| 3714 | (cur->type == XML_RELAXNG_LIST) || |
| 3715 | (cur->type == XML_RELAXNG_VALUE) || |
| 3716 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3717 | return(0); |
| 3718 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3719 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3720 | (cur->type == XML_RELAXNG_GROUP) || |
| 3721 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3722 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3723 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3724 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3725 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3726 | (cur->type == XML_RELAXNG_REF) || |
| 3727 | (cur->type == XML_RELAXNG_DEF)) { |
| 3728 | if (cur->content != NULL) { |
| 3729 | parent = cur; |
| 3730 | cur = cur->content; |
| 3731 | tmp = cur; |
| 3732 | while (tmp != NULL) { |
| 3733 | tmp->parent = parent; |
| 3734 | tmp = tmp->next; |
| 3735 | } |
| 3736 | continue; |
| 3737 | } |
| 3738 | } |
| 3739 | if (cur == def) |
| 3740 | break; |
| 3741 | if (cur->next != NULL) { |
| 3742 | cur = cur->next; |
| 3743 | continue; |
| 3744 | } |
| 3745 | do { |
| 3746 | cur = cur->parent; |
| 3747 | if (cur == NULL) break; |
| 3748 | if (cur == def) return(1); |
| 3749 | if (cur->next != NULL) { |
| 3750 | cur = cur->next; |
| 3751 | break; |
| 3752 | } |
| 3753 | } while (cur != NULL); |
| 3754 | } |
| 3755 | return(1); |
| 3756 | } |
| 3757 | |
| 3758 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3759 | * xmlRelaxNGGetElements: |
| 3760 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3761 | * @def: the definition definition |
| 3762 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3763 | * |
| 3764 | * Compute the list of top elements a definition can generate |
| 3765 | * |
| 3766 | * Returns a list of elements or NULL if none was found. |
| 3767 | */ |
| 3768 | static xmlRelaxNGDefinePtr * |
| 3769 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3770 | xmlRelaxNGDefinePtr def, |
| 3771 | int eora) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3772 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3773 | int len = 0; |
| 3774 | int max = 0; |
| 3775 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3776 | /* |
| 3777 | * Don't run that check in case of error. Infinite recursion |
| 3778 | * becomes possible. |
| 3779 | */ |
| 3780 | if (ctxt->nbErrors != 0) |
| 3781 | return(NULL); |
| 3782 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3783 | parent = NULL; |
| 3784 | cur = def; |
| 3785 | while (cur != NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3786 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3787 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3788 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3789 | if (ret == NULL) { |
| 3790 | max = 10; |
| 3791 | ret = (xmlRelaxNGDefinePtr *) |
| 3792 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3793 | if (ret == NULL) { |
| 3794 | if (ctxt->error != NULL) |
| 3795 | ctxt->error(ctxt->userData, |
| 3796 | "Out of memory in element search\n"); |
| 3797 | ctxt->nbErrors++; |
| 3798 | return(NULL); |
| 3799 | } |
| 3800 | } else if (max <= len) { |
| 3801 | max *= 2; |
| 3802 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3803 | if (ret == NULL) { |
| 3804 | if (ctxt->error != NULL) |
| 3805 | ctxt->error(ctxt->userData, |
| 3806 | "Out of memory in element search\n"); |
| 3807 | ctxt->nbErrors++; |
| 3808 | return(NULL); |
| 3809 | } |
| 3810 | } |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3811 | ret[len++] = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3812 | ret[len] = NULL; |
| 3813 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3814 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3815 | (cur->type == XML_RELAXNG_GROUP) || |
| 3816 | (cur->type == XML_RELAXNG_ONEORMORE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3817 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3818 | (cur->type == XML_RELAXNG_OPTIONAL) || |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3819 | (cur->type == XML_RELAXNG_PARENTREF) || |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3820 | (cur->type == XML_RELAXNG_REF) || |
| 3821 | (cur->type == XML_RELAXNG_DEF)) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3822 | /* |
| 3823 | * Don't go within elements or attributes or string values. |
| 3824 | * Just gather the element top list |
| 3825 | */ |
| 3826 | if (cur->content != NULL) { |
| 3827 | parent = cur; |
| 3828 | cur = cur->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3829 | tmp = cur; |
| 3830 | while (tmp != NULL) { |
| 3831 | tmp->parent = parent; |
| 3832 | tmp = tmp->next; |
| 3833 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3834 | continue; |
| 3835 | } |
| 3836 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3837 | if (cur == def) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3838 | break; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3839 | if (cur->next != NULL) { |
| 3840 | cur = cur->next; |
| 3841 | continue; |
| 3842 | } |
| 3843 | do { |
| 3844 | cur = cur->parent; |
| 3845 | if (cur == NULL) break; |
| 3846 | if (cur == def) return(ret); |
| 3847 | if (cur->next != NULL) { |
| 3848 | cur = cur->next; |
| 3849 | break; |
| 3850 | } |
| 3851 | } while (cur != NULL); |
| 3852 | } |
| 3853 | return(ret); |
| 3854 | } |
| 3855 | |
| 3856 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3857 | * xmlRelaxNGCheckChoiceDeterminism: |
| 3858 | * @ctxt: a Relax-NG parser context |
| 3859 | * @def: the choice definition |
| 3860 | * |
| 3861 | * Also used to find indeterministic pattern in choice |
| 3862 | */ |
| 3863 | static void |
| 3864 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
| 3865 | xmlRelaxNGDefinePtr def) { |
| 3866 | xmlRelaxNGDefinePtr **list; |
| 3867 | xmlRelaxNGDefinePtr cur; |
| 3868 | int nbchild = 0, i, j, ret; |
| 3869 | int is_nullable = 0; |
| 3870 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3871 | xmlHashTablePtr triage = NULL; |
| 3872 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3873 | |
| 3874 | if ((def == NULL) || |
| 3875 | (def->type != XML_RELAXNG_CHOICE)) |
| 3876 | return; |
| 3877 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3878 | if (def->dflags & IS_PROCESSED) |
| 3879 | return; |
| 3880 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3881 | /* |
| 3882 | * Don't run that check in case of error. Infinite recursion |
| 3883 | * becomes possible. |
| 3884 | */ |
| 3885 | if (ctxt->nbErrors != 0) |
| 3886 | return; |
| 3887 | |
| 3888 | is_nullable = xmlRelaxNGIsNullable(def); |
| 3889 | |
| 3890 | cur = def->content; |
| 3891 | while (cur != NULL) { |
| 3892 | nbchild++; |
| 3893 | cur = cur->next; |
| 3894 | } |
| 3895 | |
| 3896 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 3897 | sizeof(xmlRelaxNGDefinePtr *)); |
| 3898 | if (list == NULL) { |
| 3899 | if (ctxt->error != NULL) |
| 3900 | ctxt->error(ctxt->userData, |
| 3901 | "Out of memory in choice computation\n"); |
| 3902 | ctxt->nbErrors++; |
| 3903 | return; |
| 3904 | } |
| 3905 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3906 | /* |
| 3907 | * a bit strong but safe |
| 3908 | */ |
| 3909 | if (is_nullable == 0) { |
| 3910 | triage = xmlHashCreate(10); |
| 3911 | } else { |
| 3912 | is_triable = 0; |
| 3913 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3914 | cur = def->content; |
| 3915 | while (cur != NULL) { |
| 3916 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3917 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 3918 | is_triable = 0; |
| 3919 | } else if (is_triable == 1) { |
| 3920 | xmlRelaxNGDefinePtr *tmp; |
| 3921 | int res; |
| 3922 | |
| 3923 | tmp = list[i]; |
| 3924 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 3925 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 3926 | res = xmlHashAddEntry2(triage, |
| 3927 | BAD_CAST "#text", NULL, |
| 3928 | (void *)cur); |
| 3929 | if (res != 0) |
| 3930 | is_triable = -1; |
| 3931 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 3932 | ((*tmp)->name != NULL)) { |
| 3933 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3934 | res = xmlHashAddEntry2(triage, |
| 3935 | (*tmp)->name, NULL, |
| 3936 | (void *)cur); |
| 3937 | else |
| 3938 | res = xmlHashAddEntry2(triage, |
| 3939 | (*tmp)->name, (*tmp)->ns, |
| 3940 | (void *)cur); |
| 3941 | if (res != 0) |
| 3942 | is_triable = -1; |
| 3943 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 3944 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3945 | res = xmlHashAddEntry2(triage, |
| 3946 | BAD_CAST "#any", NULL, |
| 3947 | (void *)cur); |
| 3948 | else |
| 3949 | res = xmlHashAddEntry2(triage, |
| 3950 | BAD_CAST "#any", (*tmp)->ns, |
| 3951 | (void *)cur); |
| 3952 | if (res != 0) |
| 3953 | is_triable = -1; |
| 3954 | } else { |
| 3955 | is_triable = -1; |
| 3956 | } |
| 3957 | tmp++; |
| 3958 | } |
| 3959 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3960 | i++; |
| 3961 | cur = cur->next; |
| 3962 | } |
| 3963 | |
| 3964 | for (i = 0;i < nbchild;i++) { |
| 3965 | if (list[i] == NULL) |
| 3966 | continue; |
| 3967 | for (j = 0;j < i;j++) { |
| 3968 | if (list[j] == NULL) |
| 3969 | continue; |
| 3970 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 3971 | if (ret == 0) { |
| 3972 | is_indeterminist = 1; |
| 3973 | } |
| 3974 | } |
| 3975 | } |
| 3976 | for (i = 0;i < nbchild;i++) { |
| 3977 | if (list[i] != NULL) |
| 3978 | xmlFree(list[i]); |
| 3979 | } |
| 3980 | |
| 3981 | xmlFree(list); |
| 3982 | if (is_indeterminist) { |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3983 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3984 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3985 | if (is_triable == 1) { |
| 3986 | def->dflags |= IS_TRIABLE; |
| 3987 | def->data = triage; |
| 3988 | } else if (triage != NULL) { |
| 3989 | xmlHashFree(triage, NULL); |
| 3990 | } |
| 3991 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
| 3994 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3995 | * xmlRelaxNGCheckGroupAttrs: |
| 3996 | * @ctxt: a Relax-NG parser context |
| 3997 | * @def: the group definition |
| 3998 | * |
| 3999 | * Detects violations of rule 7.3 |
| 4000 | */ |
| 4001 | static void |
| 4002 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
| 4003 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4004 | xmlRelaxNGDefinePtr **list; |
| 4005 | xmlRelaxNGDefinePtr cur; |
| 4006 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4007 | |
| 4008 | if ((def == NULL) || |
| 4009 | ((def->type != XML_RELAXNG_GROUP) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4010 | (def->type != XML_RELAXNG_ELEMENT))) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4011 | return; |
| 4012 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4013 | if (def->dflags & IS_PROCESSED) |
| 4014 | return; |
| 4015 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4016 | /* |
| 4017 | * Don't run that check in case of error. Infinite recursion |
| 4018 | * becomes possible. |
| 4019 | */ |
| 4020 | if (ctxt->nbErrors != 0) |
| 4021 | return; |
| 4022 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4023 | cur = def->attrs; |
| 4024 | while (cur != NULL) { |
| 4025 | nbchild++; |
| 4026 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4027 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4028 | cur = def->content; |
| 4029 | while (cur != NULL) { |
| 4030 | nbchild++; |
| 4031 | cur = cur->next; |
| 4032 | } |
| 4033 | |
| 4034 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 4035 | sizeof(xmlRelaxNGDefinePtr *)); |
| 4036 | if (list == NULL) { |
| 4037 | if (ctxt->error != NULL) |
| 4038 | ctxt->error(ctxt->userData, |
| 4039 | "Out of memory in group computation\n"); |
| 4040 | ctxt->nbErrors++; |
| 4041 | return; |
| 4042 | } |
| 4043 | i = 0; |
| 4044 | cur = def->attrs; |
| 4045 | while (cur != NULL) { |
| 4046 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4047 | i++; |
| 4048 | cur = cur->next; |
| 4049 | } |
| 4050 | cur = def->content; |
| 4051 | while (cur != NULL) { |
| 4052 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4053 | i++; |
| 4054 | cur = cur->next; |
| 4055 | } |
| 4056 | |
| 4057 | for (i = 0;i < nbchild;i++) { |
| 4058 | if (list[i] == NULL) |
| 4059 | continue; |
| 4060 | for (j = 0;j < i;j++) { |
| 4061 | if (list[j] == NULL) |
| 4062 | continue; |
| 4063 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 4064 | if (ret == 0) { |
| 4065 | if (ctxt->error != NULL) |
| 4066 | ctxt->error(ctxt->userData, |
| 4067 | "Attributes conflicts in group\n"); |
| 4068 | ctxt->nbErrors++; |
| 4069 | } |
| 4070 | } |
| 4071 | } |
| 4072 | for (i = 0;i < nbchild;i++) { |
| 4073 | if (list[i] != NULL) |
| 4074 | xmlFree(list[i]); |
| 4075 | } |
| 4076 | |
| 4077 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4078 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
| 4081 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4082 | * xmlRelaxNGComputeInterleaves: |
| 4083 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4084 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4085 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4086 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4087 | * A lot of work for preprocessing interleave definitions |
| 4088 | * is potentially needed to get a decent execution speed at runtime |
| 4089 | * - trying to get a total order on the element nodes generated |
| 4090 | * by the interleaves, order the list of interleave definitions |
| 4091 | * following that order. |
| 4092 | * - if <text/> is used to handle mixed content, it is better to |
| 4093 | * flag this in the define and simplify the runtime checking |
| 4094 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4095 | */ |
| 4096 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4097 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
| 4098 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4099 | xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4100 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4101 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4102 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4103 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4104 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4105 | int i,j,ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4106 | int nbgroups = 0; |
| 4107 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4108 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4109 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4110 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4111 | /* |
| 4112 | * Don't run that check in case of error. Infinite recursion |
| 4113 | * becomes possible. |
| 4114 | */ |
| 4115 | if (ctxt->nbErrors != 0) |
| 4116 | return; |
| 4117 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4118 | #ifdef DEBUG_INTERLEAVE |
| 4119 | xmlGenericError(xmlGenericErrorContext, |
| 4120 | "xmlRelaxNGComputeInterleaves(%s)\n", |
| 4121 | name); |
| 4122 | #endif |
| 4123 | cur = def->content; |
| 4124 | while (cur != NULL) { |
| 4125 | nbchild++; |
| 4126 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4127 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4128 | |
| 4129 | #ifdef DEBUG_INTERLEAVE |
| 4130 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4131 | #endif |
| 4132 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
| 4133 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
| 4134 | if (groups == NULL) |
| 4135 | goto error; |
| 4136 | cur = def->content; |
| 4137 | while (cur != NULL) { |
| 4138 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4139 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4140 | if (groups[nbgroups] == NULL) |
| 4141 | goto error; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4142 | if (cur->type == XML_RELAXNG_TEXT) |
| 4143 | is_mixed++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4144 | groups[nbgroups]->rule = cur; |
| 4145 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4146 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4147 | nbgroups++; |
| 4148 | cur = cur->next; |
| 4149 | } |
| 4150 | #ifdef DEBUG_INTERLEAVE |
| 4151 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4152 | #endif |
| 4153 | |
| 4154 | /* |
| 4155 | * Let's check that all rules makes a partitions according to 7.4 |
| 4156 | */ |
| 4157 | partitions = (xmlRelaxNGPartitionPtr) |
| 4158 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
| 4159 | if (partitions == NULL) |
| 4160 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4161 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4162 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4163 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4164 | for (i = 0;i < nbgroups;i++) { |
| 4165 | group = groups[i]; |
| 4166 | for (j = i+1;j < nbgroups;j++) { |
| 4167 | if (groups[j] == NULL) |
| 4168 | continue; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4169 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4170 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4171 | groups[j]->defs); |
| 4172 | if (ret == 0) { |
| 4173 | if (ctxt->error != NULL) |
| 4174 | ctxt->error(ctxt->userData, |
| 4175 | "Element or text conflicts in interleave\n"); |
| 4176 | ctxt->nbErrors++; |
| 4177 | } |
| 4178 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4179 | groups[j]->attrs); |
| 4180 | if (ret == 0) { |
| 4181 | if (ctxt->error != NULL) |
| 4182 | ctxt->error(ctxt->userData, |
| 4183 | "Attributes conflicts in interleave\n"); |
| 4184 | ctxt->nbErrors++; |
| 4185 | } |
| 4186 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4187 | tmp = group->defs; |
| 4188 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4189 | while (*tmp != NULL) { |
| 4190 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4191 | res = xmlHashAddEntry2(partitions->triage, |
| 4192 | BAD_CAST "#text", NULL, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4193 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4194 | if (res != 0) |
| 4195 | is_determinist = -1; |
| 4196 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4197 | ((*tmp)->name != NULL)) { |
| 4198 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4199 | res = xmlHashAddEntry2(partitions->triage, |
| 4200 | (*tmp)->name, NULL, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4201 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4202 | else |
| 4203 | res = xmlHashAddEntry2(partitions->triage, |
| 4204 | (*tmp)->name, (*tmp)->ns, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4205 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4206 | if (res != 0) |
| 4207 | is_determinist = -1; |
| 4208 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4209 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4210 | res = xmlHashAddEntry2(partitions->triage, |
| 4211 | BAD_CAST "#any", NULL, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4212 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4213 | else |
| 4214 | res = xmlHashAddEntry2(partitions->triage, |
| 4215 | BAD_CAST "#any", (*tmp)->ns, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4216 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4217 | if ((*tmp)->nameClass != NULL) |
| 4218 | is_determinist = 2; |
| 4219 | if (res != 0) |
| 4220 | is_determinist = -1; |
| 4221 | } else { |
| 4222 | is_determinist = -1; |
| 4223 | } |
| 4224 | tmp++; |
| 4225 | } |
| 4226 | } else { |
| 4227 | is_determinist = 0; |
| 4228 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4229 | } |
| 4230 | partitions->groups = groups; |
| 4231 | |
| 4232 | /* |
| 4233 | * and save the partition list back in the def |
| 4234 | */ |
| 4235 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4236 | if (is_mixed != 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4237 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4238 | if (is_determinist == 1) |
| 4239 | partitions->flags = IS_DETERMINIST; |
| 4240 | if (is_determinist == 2) |
| 4241 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4242 | return; |
| 4243 | |
| 4244 | error: |
| 4245 | if (ctxt->error != NULL) |
| 4246 | ctxt->error(ctxt->userData, |
| 4247 | "Out of memory in interleave computation\n"); |
| 4248 | ctxt->nbErrors++; |
| 4249 | if (groups != NULL) { |
| 4250 | for (i = 0;i < nbgroups;i++) |
| 4251 | if (groups[i] != NULL) { |
| 4252 | if (groups[i]->defs != NULL) |
| 4253 | xmlFree(groups[i]->defs); |
| 4254 | xmlFree(groups[i]); |
| 4255 | } |
| 4256 | xmlFree(groups); |
| 4257 | } |
| 4258 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | /** |
| 4262 | * xmlRelaxNGParseInterleave: |
| 4263 | * @ctxt: a Relax-NG parser context |
| 4264 | * @node: the data node. |
| 4265 | * |
| 4266 | * parse the content of a RelaxNG interleave node. |
| 4267 | * |
| 4268 | * Returns the definition pointer or NULL in case of error |
| 4269 | */ |
| 4270 | static xmlRelaxNGDefinePtr |
| 4271 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4272 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4273 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4274 | xmlNodePtr child; |
| 4275 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4276 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4277 | if (def == NULL) { |
| 4278 | return(NULL); |
| 4279 | } |
| 4280 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4281 | |
| 4282 | if (ctxt->interleaves == NULL) |
| 4283 | ctxt->interleaves = xmlHashCreate(10); |
| 4284 | if (ctxt->interleaves == NULL) { |
| 4285 | if (ctxt->error != NULL) |
| 4286 | ctxt->error(ctxt->userData, |
| 4287 | "Failed to create interleaves hash table\n"); |
| 4288 | ctxt->nbErrors++; |
| 4289 | } else { |
| 4290 | char name[32]; |
| 4291 | |
| 4292 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4293 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4294 | if (ctxt->error != NULL) |
| 4295 | ctxt->error(ctxt->userData, |
| 4296 | "Failed to add %s to hash table\n", name); |
| 4297 | ctxt->nbErrors++; |
| 4298 | } |
| 4299 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4300 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4301 | if (child == NULL) { |
| 4302 | if (ctxt->error != NULL) |
| 4303 | ctxt->error(ctxt->userData, "Element interleave is empty\n"); |
| 4304 | ctxt->nbErrors++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4305 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4306 | while (child != NULL) { |
| 4307 | if (IS_RELAXNG(child, "element")) { |
| 4308 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4309 | } else { |
| 4310 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4311 | } |
| 4312 | if (cur != NULL) { |
| 4313 | cur->parent = def; |
| 4314 | if (last == NULL) { |
| 4315 | def->content = last = cur; |
| 4316 | } else { |
| 4317 | last->next = cur; |
| 4318 | last = cur; |
| 4319 | } |
| 4320 | } |
| 4321 | child = child->next; |
| 4322 | } |
| 4323 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4324 | return(def); |
| 4325 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4326 | |
| 4327 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4328 | * xmlRelaxNGParseInclude: |
| 4329 | * @ctxt: a Relax-NG parser context |
| 4330 | * @node: the include node |
| 4331 | * |
| 4332 | * Integrate the content of an include node in the current grammar |
| 4333 | * |
| 4334 | * Returns 0 in case of success or -1 in case of error |
| 4335 | */ |
| 4336 | static int |
| 4337 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4338 | xmlRelaxNGIncludePtr incl; |
| 4339 | xmlNodePtr root; |
| 4340 | int ret = 0, tmp; |
| 4341 | |
| 4342 | incl = node->_private; |
| 4343 | if (incl == NULL) { |
| 4344 | if (ctxt->error != NULL) |
| 4345 | ctxt->error(ctxt->userData, |
| 4346 | "Include node has no data\n"); |
| 4347 | ctxt->nbErrors++; |
| 4348 | return(-1); |
| 4349 | } |
| 4350 | root = xmlDocGetRootElement(incl->doc); |
| 4351 | if (root == NULL) { |
| 4352 | if (ctxt->error != NULL) |
| 4353 | ctxt->error(ctxt->userData, |
| 4354 | "Include document is empty\n"); |
| 4355 | ctxt->nbErrors++; |
| 4356 | return(-1); |
| 4357 | } |
| 4358 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
| 4359 | if (ctxt->error != NULL) |
| 4360 | ctxt->error(ctxt->userData, |
| 4361 | "Include document root is not a grammar\n"); |
| 4362 | ctxt->nbErrors++; |
| 4363 | return(-1); |
| 4364 | } |
| 4365 | |
| 4366 | /* |
| 4367 | * Merge the definition from both the include and the internal list |
| 4368 | */ |
| 4369 | if (root->children != NULL) { |
| 4370 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4371 | if (tmp != 0) |
| 4372 | ret = -1; |
| 4373 | } |
| 4374 | if (node->children != NULL) { |
| 4375 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4376 | if (tmp != 0) |
| 4377 | ret = -1; |
| 4378 | } |
| 4379 | return(ret); |
| 4380 | } |
| 4381 | |
| 4382 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4383 | * xmlRelaxNGParseDefine: |
| 4384 | * @ctxt: a Relax-NG parser context |
| 4385 | * @node: the define node |
| 4386 | * |
| 4387 | * parse the content of a RelaxNG define element node. |
| 4388 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4389 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4390 | */ |
| 4391 | static int |
| 4392 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4393 | xmlChar *name; |
| 4394 | int ret = 0, tmp; |
| 4395 | xmlRelaxNGDefinePtr def; |
| 4396 | const xmlChar *olddefine; |
| 4397 | |
| 4398 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4399 | if (name == NULL) { |
| 4400 | if (ctxt->error != NULL) |
| 4401 | ctxt->error(ctxt->userData, |
| 4402 | "define has no name\n"); |
| 4403 | ctxt->nbErrors++; |
| 4404 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4405 | xmlRelaxNGNormExtSpace(name); |
| 4406 | if (xmlValidateNCName(name, 0)) { |
| 4407 | if (ctxt->error != NULL) |
| 4408 | ctxt->error(ctxt->userData, |
| 4409 | "define name '%s' is not an NCName\n", |
| 4410 | name); |
| 4411 | ctxt->nbErrors++; |
| 4412 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4413 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4414 | if (def == NULL) { |
| 4415 | xmlFree(name); |
| 4416 | return(-1); |
| 4417 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4418 | def->type = XML_RELAXNG_DEF; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4419 | def->name = name; |
| 4420 | if (node->children == NULL) { |
| 4421 | if (ctxt->error != NULL) |
| 4422 | ctxt->error(ctxt->userData, |
| 4423 | "define has no children\n"); |
| 4424 | ctxt->nbErrors++; |
| 4425 | } else { |
| 4426 | olddefine = ctxt->define; |
| 4427 | ctxt->define = name; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4428 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4429 | ctxt->define = olddefine; |
| 4430 | } |
| 4431 | if (ctxt->grammar->defs == NULL) |
| 4432 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4433 | if (ctxt->grammar->defs == NULL) { |
| 4434 | if (ctxt->error != NULL) |
| 4435 | ctxt->error(ctxt->userData, |
| 4436 | "Could not create definition hash\n"); |
| 4437 | ctxt->nbErrors++; |
| 4438 | ret = -1; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4439 | } else { |
| 4440 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4441 | if (tmp < 0) { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4442 | xmlRelaxNGDefinePtr prev; |
| 4443 | |
| 4444 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4445 | if (prev == NULL) { |
| 4446 | if (ctxt->error != NULL) |
| 4447 | ctxt->error(ctxt->userData, |
| 4448 | "Internal error on define aggregation of %s\n", |
| 4449 | name); |
| 4450 | ctxt->nbErrors++; |
| 4451 | ret = -1; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4452 | } else { |
| 4453 | while (prev->nextHash != NULL) |
| 4454 | prev = prev->nextHash; |
| 4455 | prev->nextHash = def; |
| 4456 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4457 | } |
| 4458 | } |
| 4459 | } |
| 4460 | return(ret); |
| 4461 | } |
| 4462 | |
| 4463 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4464 | * xmlRelaxNGProcessExternalRef: |
| 4465 | * @ctxt: the parser context |
| 4466 | * @node: the externlRef node |
| 4467 | * |
| 4468 | * Process and compile an externlRef node |
| 4469 | * |
| 4470 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4471 | */ |
| 4472 | static xmlRelaxNGDefinePtr |
| 4473 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4474 | xmlRelaxNGDocumentPtr docu; |
| 4475 | xmlNodePtr root, tmp; |
| 4476 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4477 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4478 | xmlRelaxNGDefinePtr def; |
| 4479 | |
| 4480 | docu = node->_private; |
| 4481 | if (docu != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4482 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4483 | if (def == NULL) |
| 4484 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4485 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4486 | |
| 4487 | if (docu->content == NULL) { |
| 4488 | /* |
| 4489 | * Then do the parsing for good |
| 4490 | */ |
| 4491 | root = xmlDocGetRootElement(docu->doc); |
| 4492 | if (root == NULL) { |
| 4493 | if (ctxt->error != NULL) |
| 4494 | ctxt->error(ctxt->userData, |
| 4495 | "xmlRelaxNGParse: %s is empty\n", |
| 4496 | ctxt->URL); |
| 4497 | ctxt->nbErrors++; |
| 4498 | return (NULL); |
| 4499 | } |
| 4500 | /* |
| 4501 | * ns transmission rules |
| 4502 | */ |
| 4503 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4504 | if (ns == NULL) { |
| 4505 | tmp = node; |
| 4506 | while ((tmp != NULL) && |
| 4507 | (tmp->type == XML_ELEMENT_NODE)) { |
| 4508 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4509 | if (ns != NULL) { |
| 4510 | break; |
| 4511 | } |
| 4512 | tmp = tmp->parent; |
| 4513 | } |
| 4514 | if (ns != NULL) { |
| 4515 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4516 | newNs = 1; |
| 4517 | xmlFree(ns); |
| 4518 | } |
| 4519 | } else { |
| 4520 | xmlFree(ns); |
| 4521 | } |
| 4522 | |
| 4523 | /* |
| 4524 | * Parsing to get a precompiled schemas. |
| 4525 | */ |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4526 | oldflags = ctxt->flags; |
| 4527 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4528 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4529 | ctxt->flags = oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4530 | if ((docu->schema != NULL) && |
| 4531 | (docu->schema->topgrammar != NULL)) { |
| 4532 | docu->content = docu->schema->topgrammar->start; |
| 4533 | } |
| 4534 | |
| 4535 | /* |
| 4536 | * the externalRef may be reused in a different ns context |
| 4537 | */ |
| 4538 | if (newNs == 1) { |
| 4539 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4540 | } |
| 4541 | } |
| 4542 | def->content = docu->content; |
| 4543 | } else { |
| 4544 | def = NULL; |
| 4545 | } |
| 4546 | return(def); |
| 4547 | } |
| 4548 | |
| 4549 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4550 | * xmlRelaxNGParsePattern: |
| 4551 | * @ctxt: a Relax-NG parser context |
| 4552 | * @node: the pattern node. |
| 4553 | * |
| 4554 | * parse the content of a RelaxNG pattern node. |
| 4555 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4556 | * Returns the definition pointer or NULL in case of error or if no |
| 4557 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4558 | */ |
| 4559 | static xmlRelaxNGDefinePtr |
| 4560 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4561 | xmlRelaxNGDefinePtr def = NULL; |
| 4562 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4563 | if (node == NULL) { |
| 4564 | return(NULL); |
| 4565 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4566 | if (IS_RELAXNG(node, "element")) { |
| 4567 | def = xmlRelaxNGParseElement(ctxt, node); |
| 4568 | } else if (IS_RELAXNG(node, "attribute")) { |
| 4569 | def = xmlRelaxNGParseAttribute(ctxt, node); |
| 4570 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4571 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4572 | if (def == NULL) |
| 4573 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4574 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4575 | if (node->children != NULL) { |
| 4576 | if (ctxt->error != NULL) |
| 4577 | ctxt->error(ctxt->userData, "empty: had a child node\n"); |
| 4578 | ctxt->nbErrors++; |
| 4579 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4580 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4581 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4582 | if (def == NULL) |
| 4583 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4584 | def->type = XML_RELAXNG_TEXT; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4585 | if (node->children != NULL) { |
| 4586 | if (ctxt->error != NULL) |
| 4587 | ctxt->error(ctxt->userData, "text: had a child node\n"); |
| 4588 | ctxt->nbErrors++; |
| 4589 | } |
| 4590 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4591 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4592 | if (def == NULL) |
| 4593 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4594 | def->type = XML_RELAXNG_ZEROORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4595 | if (node->children == NULL) { |
| 4596 | if (ctxt->error != NULL) |
| 4597 | ctxt->error(ctxt->userData, |
| 4598 | "Element %s is empty\n", node->name); |
| 4599 | ctxt->nbErrors++; |
| 4600 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4601 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4602 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4603 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4604 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4605 | if (def == NULL) |
| 4606 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4607 | def->type = XML_RELAXNG_ONEORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4608 | if (node->children == NULL) { |
| 4609 | if (ctxt->error != NULL) |
| 4610 | ctxt->error(ctxt->userData, |
| 4611 | "Element %s is empty\n", node->name); |
| 4612 | ctxt->nbErrors++; |
| 4613 | } else { |
| 4614 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4615 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4616 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4617 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4618 | if (def == NULL) |
| 4619 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4620 | def->type = XML_RELAXNG_OPTIONAL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4621 | if (node->children == NULL) { |
| 4622 | if (ctxt->error != NULL) |
| 4623 | ctxt->error(ctxt->userData, |
| 4624 | "Element %s is empty\n", node->name); |
| 4625 | ctxt->nbErrors++; |
| 4626 | } else { |
| 4627 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4628 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4629 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4630 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4631 | if (def == NULL) |
| 4632 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4633 | def->type = XML_RELAXNG_CHOICE; |
| 4634 | if (node->children == NULL) { |
| 4635 | if (ctxt->error != NULL) |
| 4636 | ctxt->error(ctxt->userData, |
| 4637 | "Element %s is empty\n", node->name); |
| 4638 | ctxt->nbErrors++; |
| 4639 | } else { |
| 4640 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4641 | } |
| 4642 | } else if (IS_RELAXNG(node, "group")) { |
| 4643 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4644 | if (def == NULL) |
| 4645 | return(NULL); |
| 4646 | def->type = XML_RELAXNG_GROUP; |
| 4647 | if (node->children == NULL) { |
| 4648 | if (ctxt->error != NULL) |
| 4649 | ctxt->error(ctxt->userData, |
| 4650 | "Element %s is empty\n", node->name); |
| 4651 | ctxt->nbErrors++; |
| 4652 | } else { |
| 4653 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4654 | } |
| 4655 | } else if (IS_RELAXNG(node, "ref")) { |
| 4656 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4657 | if (def == NULL) |
| 4658 | return(NULL); |
| 4659 | def->type = XML_RELAXNG_REF; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4660 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4661 | if (def->name == NULL) { |
| 4662 | if (ctxt->error != NULL) |
| 4663 | ctxt->error(ctxt->userData, |
| 4664 | "ref has no name\n"); |
| 4665 | ctxt->nbErrors++; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4666 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4667 | xmlRelaxNGNormExtSpace(def->name); |
| 4668 | if (xmlValidateNCName(def->name, 0)) { |
| 4669 | if (ctxt->error != NULL) |
| 4670 | ctxt->error(ctxt->userData, |
| 4671 | "ref name '%s' is not an NCName\n", |
| 4672 | def->name); |
| 4673 | ctxt->nbErrors++; |
| 4674 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4675 | } |
| 4676 | if (node->children != NULL) { |
| 4677 | if (ctxt->error != NULL) |
| 4678 | ctxt->error(ctxt->userData, |
| 4679 | "ref is not empty\n"); |
| 4680 | ctxt->nbErrors++; |
| 4681 | } |
| 4682 | if (ctxt->grammar->refs == NULL) |
| 4683 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4684 | if (ctxt->grammar->refs == NULL) { |
| 4685 | if (ctxt->error != NULL) |
| 4686 | ctxt->error(ctxt->userData, |
| 4687 | "Could not create references hash\n"); |
| 4688 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4689 | def = NULL; |
| 4690 | } else { |
| 4691 | int tmp; |
| 4692 | |
| 4693 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4694 | if (tmp < 0) { |
| 4695 | xmlRelaxNGDefinePtr prev; |
| 4696 | |
| 4697 | prev = (xmlRelaxNGDefinePtr) |
| 4698 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4699 | if (prev == NULL) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4700 | if (def->name != NULL) { |
| 4701 | if (ctxt->error != NULL) |
| 4702 | ctxt->error(ctxt->userData, |
| 4703 | "Error refs definitions '%s'\n", |
| 4704 | def->name); |
| 4705 | } else { |
| 4706 | if (ctxt->error != NULL) |
| 4707 | ctxt->error(ctxt->userData, |
| 4708 | "Error refs definitions\n"); |
| 4709 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4710 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4711 | def = NULL; |
| 4712 | } else { |
| 4713 | def->nextHash = prev->nextHash; |
| 4714 | prev->nextHash = def; |
| 4715 | } |
| 4716 | } |
| 4717 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4718 | } else if (IS_RELAXNG(node, "data")) { |
| 4719 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4720 | } else if (IS_RELAXNG(node, "value")) { |
| 4721 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4722 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4723 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4724 | if (def == NULL) |
| 4725 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4726 | def->type = XML_RELAXNG_LIST; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4727 | if (node->children == NULL) { |
| 4728 | if (ctxt->error != NULL) |
| 4729 | ctxt->error(ctxt->userData, |
| 4730 | "Element %s is empty\n", node->name); |
| 4731 | ctxt->nbErrors++; |
| 4732 | } else { |
| 4733 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4734 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4735 | } else if (IS_RELAXNG(node, "interleave")) { |
| 4736 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4737 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4738 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4739 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4740 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4741 | if (def == NULL) |
| 4742 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4743 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4744 | if (node->children != NULL) { |
| 4745 | if (ctxt->error != NULL) |
| 4746 | ctxt->error(ctxt->userData, |
| 4747 | "xmlRelaxNGParse: notAllowed element is not empty\n"); |
| 4748 | ctxt->nbErrors++; |
| 4749 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4750 | } else if (IS_RELAXNG(node, "grammar")) { |
| 4751 | xmlRelaxNGGrammarPtr grammar, old; |
| 4752 | xmlRelaxNGGrammarPtr oldparent; |
| 4753 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4754 | #ifdef DEBUG_GRAMMAR |
| 4755 | xmlGenericError(xmlGenericErrorContext, "Found <grammar> pattern\n"); |
| 4756 | #endif |
| 4757 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4758 | oldparent = ctxt->parentgrammar; |
| 4759 | old = ctxt->grammar; |
| 4760 | ctxt->parentgrammar = old; |
| 4761 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4762 | if (old != NULL) { |
| 4763 | ctxt->grammar = old; |
| 4764 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4765 | #if 0 |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4766 | if (grammar != NULL) { |
| 4767 | grammar->next = old->next; |
| 4768 | old->next = grammar; |
| 4769 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4770 | #endif |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4771 | } |
| 4772 | if (grammar != NULL) |
| 4773 | def = grammar->start; |
| 4774 | else |
| 4775 | def = NULL; |
| 4776 | } else if (IS_RELAXNG(node, "parentRef")) { |
| 4777 | if (ctxt->parentgrammar == NULL) { |
| 4778 | if (ctxt->error != NULL) |
| 4779 | ctxt->error(ctxt->userData, |
| 4780 | "Use of parentRef without a parent grammar\n"); |
| 4781 | ctxt->nbErrors++; |
| 4782 | return(NULL); |
| 4783 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4784 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4785 | if (def == NULL) |
| 4786 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4787 | def->type = XML_RELAXNG_PARENTREF; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4788 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4789 | if (def->name == NULL) { |
| 4790 | if (ctxt->error != NULL) |
| 4791 | ctxt->error(ctxt->userData, |
| 4792 | "parentRef has no name\n"); |
| 4793 | ctxt->nbErrors++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4794 | } else { |
| 4795 | xmlRelaxNGNormExtSpace(def->name); |
| 4796 | if (xmlValidateNCName(def->name, 0)) { |
| 4797 | if (ctxt->error != NULL) |
| 4798 | ctxt->error(ctxt->userData, |
| 4799 | "parentRef name '%s' is not an NCName\n", |
| 4800 | def->name); |
| 4801 | ctxt->nbErrors++; |
| 4802 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4803 | } |
| 4804 | if (node->children != NULL) { |
| 4805 | if (ctxt->error != NULL) |
| 4806 | ctxt->error(ctxt->userData, |
| 4807 | "parentRef is not empty\n"); |
| 4808 | ctxt->nbErrors++; |
| 4809 | } |
| 4810 | if (ctxt->parentgrammar->refs == NULL) |
| 4811 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4812 | if (ctxt->parentgrammar->refs == NULL) { |
| 4813 | if (ctxt->error != NULL) |
| 4814 | ctxt->error(ctxt->userData, |
| 4815 | "Could not create references hash\n"); |
| 4816 | ctxt->nbErrors++; |
| 4817 | def = NULL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4818 | } else if (def->name != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4819 | int tmp; |
| 4820 | |
| 4821 | tmp = xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4822 | if (tmp < 0) { |
| 4823 | xmlRelaxNGDefinePtr prev; |
| 4824 | |
| 4825 | prev = (xmlRelaxNGDefinePtr) |
| 4826 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4827 | if (prev == NULL) { |
| 4828 | if (ctxt->error != NULL) |
| 4829 | ctxt->error(ctxt->userData, |
| 4830 | "Internal error parentRef definitions '%s'\n", |
| 4831 | def->name); |
| 4832 | ctxt->nbErrors++; |
| 4833 | def = NULL; |
| 4834 | } else { |
| 4835 | def->nextHash = prev->nextHash; |
| 4836 | prev->nextHash = def; |
| 4837 | } |
| 4838 | } |
| 4839 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4840 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4841 | if (node->children == NULL) { |
| 4842 | if (ctxt->error != NULL) |
| 4843 | ctxt->error(ctxt->userData, |
| 4844 | "Mixed is empty\n"); |
| 4845 | ctxt->nbErrors++; |
| 4846 | def = NULL; |
| 4847 | } else { |
| 4848 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4849 | if (def != NULL) { |
| 4850 | xmlRelaxNGDefinePtr tmp; |
| 4851 | |
| 4852 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4853 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4854 | if (tmp != NULL) { |
| 4855 | tmp->type = XML_RELAXNG_GROUP; |
| 4856 | tmp->content = def->content; |
| 4857 | def->content = tmp; |
| 4858 | } |
| 4859 | } |
| 4860 | |
| 4861 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4862 | if (tmp == NULL) |
| 4863 | return(def); |
| 4864 | tmp->type = XML_RELAXNG_TEXT; |
| 4865 | tmp->next = def->content; |
| 4866 | def->content = tmp; |
| 4867 | } |
| 4868 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4869 | } else { |
| 4870 | if (ctxt->error != NULL) |
| 4871 | ctxt->error(ctxt->userData, |
| 4872 | "Unexpected node %s is not a pattern\n", |
| 4873 | node->name); |
| 4874 | ctxt->nbErrors++; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4875 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4876 | } |
| 4877 | return(def); |
| 4878 | } |
| 4879 | |
| 4880 | /** |
| 4881 | * xmlRelaxNGParseAttribute: |
| 4882 | * @ctxt: a Relax-NG parser context |
| 4883 | * @node: the element node |
| 4884 | * |
| 4885 | * parse the content of a RelaxNG attribute node. |
| 4886 | * |
| 4887 | * Returns the definition pointer or NULL in case of error. |
| 4888 | */ |
| 4889 | static xmlRelaxNGDefinePtr |
| 4890 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4891 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4892 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4893 | int old_flags; |
| 4894 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4895 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4896 | if (ret == NULL) |
| 4897 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4898 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4899 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4900 | child = node->children; |
| 4901 | if (child == NULL) { |
| 4902 | if (ctxt->error != NULL) |
| 4903 | ctxt->error(ctxt->userData, |
| 4904 | "xmlRelaxNGParseattribute: attribute has no children\n"); |
| 4905 | ctxt->nbErrors++; |
| 4906 | return(ret); |
| 4907 | } |
| 4908 | old_flags = ctxt->flags; |
| 4909 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4910 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4911 | if (cur != NULL) |
| 4912 | child = child->next; |
| 4913 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4914 | if (child != NULL) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4915 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4916 | if (cur != NULL) { |
| 4917 | switch (cur->type) { |
| 4918 | case XML_RELAXNG_EMPTY: |
| 4919 | case XML_RELAXNG_NOT_ALLOWED: |
| 4920 | case XML_RELAXNG_TEXT: |
| 4921 | case XML_RELAXNG_ELEMENT: |
| 4922 | case XML_RELAXNG_DATATYPE: |
| 4923 | case XML_RELAXNG_VALUE: |
| 4924 | case XML_RELAXNG_LIST: |
| 4925 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4926 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4927 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4928 | case XML_RELAXNG_DEF: |
| 4929 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4930 | case XML_RELAXNG_ZEROORMORE: |
| 4931 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4932 | case XML_RELAXNG_CHOICE: |
| 4933 | case XML_RELAXNG_GROUP: |
| 4934 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4935 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4936 | ret->content = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4937 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4938 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4939 | case XML_RELAXNG_START: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 4940 | case XML_RELAXNG_PARAM: |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4941 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4942 | if (ctxt->error != NULL) |
| 4943 | ctxt->error(ctxt->userData, |
| 4944 | "attribute has invalid content\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 4945 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4946 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4947 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4948 | if (ctxt->error != NULL) |
| 4949 | ctxt->error(ctxt->userData, |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 4950 | "RNG Internal error, noop found in attribute\n"); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4951 | ctxt->nbErrors++; |
| 4952 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4953 | } |
| 4954 | } |
| 4955 | child = child->next; |
| 4956 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4957 | if (child != NULL) { |
| 4958 | if (ctxt->error != NULL) |
| 4959 | ctxt->error(ctxt->userData, "attribute has multiple children\n"); |
| 4960 | ctxt->nbErrors++; |
| 4961 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4962 | ctxt->flags = old_flags; |
| 4963 | return(ret); |
| 4964 | } |
| 4965 | |
| 4966 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4967 | * xmlRelaxNGParseExceptNameClass: |
| 4968 | * @ctxt: a Relax-NG parser context |
| 4969 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4970 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4971 | * |
| 4972 | * parse the content of a RelaxNG nameClass node. |
| 4973 | * |
| 4974 | * Returns the definition pointer or NULL in case of error. |
| 4975 | */ |
| 4976 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4977 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
| 4978 | xmlNodePtr node, int attr) { |
| 4979 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 4980 | xmlNodePtr child; |
| 4981 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4982 | if (!IS_RELAXNG(node, "except")) { |
| 4983 | if (ctxt->error != NULL) |
| 4984 | ctxt->error(ctxt->userData, |
| 4985 | "Expecting an except node\n"); |
| 4986 | ctxt->nbErrors++; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4987 | return(NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4988 | } |
| 4989 | if (node->next != NULL) { |
| 4990 | if (ctxt->error != NULL) |
| 4991 | ctxt->error(ctxt->userData, |
| 4992 | "exceptNameClass allows only a single except node\n"); |
| 4993 | ctxt->nbErrors++; |
| 4994 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4995 | if (node->children == NULL) { |
| 4996 | if (ctxt->error != NULL) |
| 4997 | ctxt->error(ctxt->userData, |
| 4998 | "except has no content\n"); |
| 4999 | ctxt->nbErrors++; |
| 5000 | return(NULL); |
| 5001 | } |
| 5002 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5003 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5004 | if (ret == NULL) |
| 5005 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5006 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5007 | child = node->children; |
| 5008 | while (child != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5009 | cur = xmlRelaxNGNewDefine(ctxt, child); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5010 | if (cur == NULL) |
| 5011 | break; |
| 5012 | if (attr) |
| 5013 | cur->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5014 | else |
| 5015 | cur->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5016 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5017 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5018 | if (last == NULL) { |
| 5019 | ret->content = cur; |
| 5020 | } else { |
| 5021 | last->next = cur; |
| 5022 | } |
| 5023 | last = cur; |
| 5024 | } |
| 5025 | child = child->next; |
| 5026 | } |
| 5027 | |
| 5028 | return(ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5029 | } |
| 5030 | |
| 5031 | /** |
| 5032 | * xmlRelaxNGParseNameClass: |
| 5033 | * @ctxt: a Relax-NG parser context |
| 5034 | * @node: the nameClass node |
| 5035 | * @def: the current definition |
| 5036 | * |
| 5037 | * parse the content of a RelaxNG nameClass node. |
| 5038 | * |
| 5039 | * Returns the definition pointer or NULL in case of error. |
| 5040 | */ |
| 5041 | static xmlRelaxNGDefinePtr |
| 5042 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 5043 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5044 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5045 | xmlChar *val; |
| 5046 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5047 | ret = def; |
| 5048 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
| 5049 | (IS_RELAXNG(node, "nsName"))) { |
| 5050 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 5051 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5052 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5053 | if (ret == NULL) |
| 5054 | return(NULL); |
| 5055 | ret->parent = def; |
| 5056 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 5057 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5058 | else |
| 5059 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5060 | } |
| 5061 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5062 | if (IS_RELAXNG(node, "name")) { |
| 5063 | val = xmlNodeGetContent(node); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5064 | xmlRelaxNGNormExtSpace(val); |
| 5065 | if (xmlValidateNCName(val, 0)) { |
| 5066 | if (ctxt->error != NULL) { |
| 5067 | if (node->parent != NULL) |
| 5068 | ctxt->error(ctxt->userData, |
| 5069 | "Element %s name '%s' is not an NCName\n", |
| 5070 | node->parent->name, val); |
| 5071 | else |
| 5072 | ctxt->error(ctxt->userData, |
| 5073 | "name '%s' is not an NCName\n", |
| 5074 | val); |
| 5075 | } |
| 5076 | ctxt->nbErrors++; |
| 5077 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5078 | ret->name = val; |
| 5079 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5080 | ret->ns = val; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 5081 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5082 | (val != NULL) && |
| 5083 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5084 | ctxt->error(ctxt->userData, |
| 5085 | "Attribute with namespace '%s' is not allowed\n", |
| 5086 | val); |
| 5087 | ctxt->nbErrors++; |
| 5088 | } |
| 5089 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5090 | (val != NULL) && |
| 5091 | (val[0] == 0) && |
| 5092 | (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
| 5093 | ctxt->error(ctxt->userData, |
| 5094 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5095 | val); |
| 5096 | ctxt->nbErrors++; |
| 5097 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5098 | } else if (IS_RELAXNG(node, "anyName")) { |
| 5099 | ret->name = NULL; |
| 5100 | ret->ns = NULL; |
| 5101 | if (node->children != NULL) { |
| 5102 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5103 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5104 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5105 | } |
| 5106 | } else if (IS_RELAXNG(node, "nsName")) { |
| 5107 | ret->name = NULL; |
| 5108 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5109 | if (ret->ns == NULL) { |
| 5110 | if (ctxt->error != NULL) |
| 5111 | ctxt->error(ctxt->userData, |
| 5112 | "nsName has no ns attribute\n"); |
| 5113 | ctxt->nbErrors++; |
| 5114 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 5115 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5116 | (ret->ns != NULL) && |
| 5117 | (xmlStrEqual(ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5118 | ctxt->error(ctxt->userData, |
| 5119 | "Attribute with namespace '%s' is not allowed\n", |
| 5120 | ret->ns); |
| 5121 | ctxt->nbErrors++; |
| 5122 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5123 | if (node->children != NULL) { |
| 5124 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5125 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5126 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5127 | } |
| 5128 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5129 | xmlNodePtr child; |
| 5130 | xmlRelaxNGDefinePtr last = NULL; |
| 5131 | |
| 5132 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5133 | if (ret == NULL) |
| 5134 | return(NULL); |
| 5135 | ret->parent = def; |
| 5136 | ret->type = XML_RELAXNG_CHOICE; |
| 5137 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5138 | if (node->children == NULL) { |
| 5139 | if (ctxt->error != NULL) |
| 5140 | ctxt->error(ctxt->userData, |
| 5141 | "Element choice is empty\n"); |
| 5142 | ctxt->nbErrors++; |
| 5143 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5144 | |
| 5145 | child = node->children; |
| 5146 | while (child != NULL) { |
| 5147 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5148 | if (tmp != NULL) { |
| 5149 | if (last == NULL) { |
| 5150 | last = ret->nameClass = tmp; |
| 5151 | } else { |
| 5152 | last->next = tmp; |
| 5153 | last = tmp; |
| 5154 | } |
| 5155 | } |
| 5156 | child = child->next; |
| 5157 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5158 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5159 | } else { |
| 5160 | if (ctxt->error != NULL) |
| 5161 | ctxt->error(ctxt->userData, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5162 | "expecting name, anyName, nsName or choice : got %s\n", |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5163 | node->name); |
| 5164 | ctxt->nbErrors++; |
| 5165 | return(NULL); |
| 5166 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5167 | if (ret != def) { |
| 5168 | if (def->nameClass == NULL) { |
| 5169 | def->nameClass = ret; |
| 5170 | } else { |
| 5171 | tmp = def->nameClass; |
| 5172 | while (tmp->next != NULL) { |
| 5173 | tmp = tmp->next; |
| 5174 | } |
| 5175 | tmp->next = ret; |
| 5176 | } |
| 5177 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5178 | return(ret); |
| 5179 | } |
| 5180 | |
| 5181 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5182 | * xmlRelaxNGParseElement: |
| 5183 | * @ctxt: a Relax-NG parser context |
| 5184 | * @node: the element node |
| 5185 | * |
| 5186 | * parse the content of a RelaxNG element node. |
| 5187 | * |
| 5188 | * Returns the definition pointer or NULL in case of error. |
| 5189 | */ |
| 5190 | static xmlRelaxNGDefinePtr |
| 5191 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 5192 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5193 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5194 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5195 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5196 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5197 | if (ret == NULL) |
| 5198 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5199 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5200 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5201 | child = node->children; |
| 5202 | if (child == NULL) { |
| 5203 | if (ctxt->error != NULL) |
| 5204 | ctxt->error(ctxt->userData, |
| 5205 | "xmlRelaxNGParseElement: element has no children\n"); |
| 5206 | ctxt->nbErrors++; |
| 5207 | return(ret); |
| 5208 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5209 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5210 | if (cur != NULL) |
| 5211 | child = child->next; |
| 5212 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5213 | if (child == NULL) { |
| 5214 | if (ctxt->error != NULL) |
| 5215 | ctxt->error(ctxt->userData, |
| 5216 | "xmlRelaxNGParseElement: element has no content\n"); |
| 5217 | ctxt->nbErrors++; |
| 5218 | return(ret); |
| 5219 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5220 | olddefine = ctxt->define; |
| 5221 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5222 | last = NULL; |
| 5223 | while (child != NULL) { |
| 5224 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5225 | if (cur != NULL) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5226 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5227 | switch (cur->type) { |
| 5228 | case XML_RELAXNG_EMPTY: |
| 5229 | case XML_RELAXNG_NOT_ALLOWED: |
| 5230 | case XML_RELAXNG_TEXT: |
| 5231 | case XML_RELAXNG_ELEMENT: |
| 5232 | case XML_RELAXNG_DATATYPE: |
| 5233 | case XML_RELAXNG_VALUE: |
| 5234 | case XML_RELAXNG_LIST: |
| 5235 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5236 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5237 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5238 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5239 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5240 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5241 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5242 | case XML_RELAXNG_CHOICE: |
| 5243 | case XML_RELAXNG_GROUP: |
| 5244 | case XML_RELAXNG_INTERLEAVE: |
| 5245 | if (last == NULL) { |
| 5246 | ret->content = last = cur; |
| 5247 | } else { |
| 5248 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5249 | (ret->content == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5250 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5251 | if (ret->content != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5252 | ret->content->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5253 | ret->content->content = last; |
| 5254 | } else { |
| 5255 | ret->content = last; |
| 5256 | } |
| 5257 | } |
| 5258 | last->next = cur; |
| 5259 | last = cur; |
| 5260 | } |
| 5261 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5262 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5263 | /* HERE !!! */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5264 | cur->next = ret->attrs; |
| 5265 | ret->attrs = cur; |
| 5266 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5267 | case XML_RELAXNG_START: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5268 | if (ctxt->error != NULL) |
| 5269 | ctxt->error(ctxt->userData, |
| 5270 | "RNG Internal error, start found in element\n"); |
| 5271 | ctxt->nbErrors++; |
| 5272 | break; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 5273 | case XML_RELAXNG_PARAM: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5274 | if (ctxt->error != NULL) |
| 5275 | ctxt->error(ctxt->userData, |
| 5276 | "RNG Internal error, param found in element\n"); |
| 5277 | ctxt->nbErrors++; |
| 5278 | break; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5279 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5280 | if (ctxt->error != NULL) |
| 5281 | ctxt->error(ctxt->userData, |
| 5282 | "RNG Internal error, except found in element\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5283 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5284 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5285 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5286 | if (ctxt->error != NULL) |
| 5287 | ctxt->error(ctxt->userData, |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5288 | "RNG Internal error, noop found in element\n"); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5289 | ctxt->nbErrors++; |
| 5290 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5291 | } |
| 5292 | } |
| 5293 | child = child->next; |
| 5294 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5295 | ctxt->define = olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5296 | return(ret); |
| 5297 | } |
| 5298 | |
| 5299 | /** |
| 5300 | * xmlRelaxNGParsePatterns: |
| 5301 | * @ctxt: a Relax-NG parser context |
| 5302 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5303 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5304 | * |
| 5305 | * parse the content of a RelaxNG start node. |
| 5306 | * |
| 5307 | * Returns the definition pointer or NULL in case of error. |
| 5308 | */ |
| 5309 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5310 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
| 5311 | int group) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5312 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5313 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5314 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5315 | while (nodes != NULL) { |
| 5316 | if (IS_RELAXNG(nodes, "element")) { |
| 5317 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5318 | if (def == NULL) { |
| 5319 | def = last = cur; |
| 5320 | } else { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5321 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5322 | (def == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5323 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5324 | def->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5325 | def->content = last; |
| 5326 | } |
| 5327 | last->next = cur; |
| 5328 | last = cur; |
| 5329 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5330 | cur->parent = parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5331 | } else { |
| 5332 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5333 | if (cur != NULL) { |
| 5334 | if (def == NULL) { |
| 5335 | def = last = cur; |
| 5336 | } else { |
| 5337 | last->next = cur; |
| 5338 | last = cur; |
| 5339 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5340 | } |
| 5341 | } |
| 5342 | nodes = nodes->next; |
| 5343 | } |
| 5344 | return(def); |
| 5345 | } |
| 5346 | |
| 5347 | /** |
| 5348 | * xmlRelaxNGParseStart: |
| 5349 | * @ctxt: a Relax-NG parser context |
| 5350 | * @nodes: start children nodes |
| 5351 | * |
| 5352 | * parse the content of a RelaxNG start node. |
| 5353 | * |
| 5354 | * Returns 0 in case of success, -1 in case of error |
| 5355 | */ |
| 5356 | static int |
| 5357 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 5358 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5359 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5360 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5361 | if (nodes == NULL) { |
| 5362 | if (ctxt->error != NULL) |
| 5363 | ctxt->error(ctxt->userData, |
| 5364 | "start has no children\n"); |
| 5365 | ctxt->nbErrors++; |
| 5366 | return(-1); |
| 5367 | } |
| 5368 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5369 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5370 | if (def == NULL) |
| 5371 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5372 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5373 | if (nodes->children != NULL) { |
| 5374 | if (ctxt->error != NULL) |
| 5375 | ctxt->error(ctxt->userData, "element empty is not empty\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5376 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5377 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5378 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5379 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5380 | if (def == NULL) |
| 5381 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5382 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5383 | if (nodes->children != NULL) { |
| 5384 | if (ctxt->error != NULL) |
| 5385 | ctxt->error(ctxt->userData, |
| 5386 | "element notAllowed is not empty\n"); |
| 5387 | ctxt->nbErrors++; |
| 5388 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5389 | } else { |
| 5390 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5391 | } |
| 5392 | if (ctxt->grammar->start != NULL) { |
| 5393 | last = ctxt->grammar->start; |
| 5394 | while (last->next != NULL) |
| 5395 | last = last->next; |
| 5396 | last->next = def; |
| 5397 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5398 | ctxt->grammar->start = def; |
| 5399 | } |
| 5400 | nodes = nodes->next; |
| 5401 | if (nodes != NULL) { |
| 5402 | if (ctxt->error != NULL) |
| 5403 | ctxt->error(ctxt->userData, |
| 5404 | "start more than one children\n"); |
| 5405 | ctxt->nbErrors++; |
| 5406 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5407 | } |
| 5408 | return(ret); |
| 5409 | } |
| 5410 | |
| 5411 | /** |
| 5412 | * xmlRelaxNGParseGrammarContent: |
| 5413 | * @ctxt: a Relax-NG parser context |
| 5414 | * @nodes: grammar children nodes |
| 5415 | * |
| 5416 | * parse the content of a RelaxNG grammar node. |
| 5417 | * |
| 5418 | * Returns 0 in case of success, -1 in case of error |
| 5419 | */ |
| 5420 | static int |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5421 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5422 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5423 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5424 | |
| 5425 | if (nodes == NULL) { |
| 5426 | if (ctxt->error != NULL) |
| 5427 | ctxt->error(ctxt->userData, |
| 5428 | "grammar has no children\n"); |
| 5429 | ctxt->nbErrors++; |
| 5430 | return(-1); |
| 5431 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5432 | while (nodes != NULL) { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5433 | if (IS_RELAXNG(nodes, "start")) { |
| 5434 | if (nodes->children == NULL) { |
| 5435 | if (ctxt->error != NULL) |
| 5436 | ctxt->error(ctxt->userData, |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5437 | "start has no children\n"); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5438 | ctxt->nbErrors++; |
| 5439 | } else { |
| 5440 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5441 | if (tmp != 0) |
| 5442 | ret = -1; |
| 5443 | } |
| 5444 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5445 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5446 | if (tmp != 0) |
| 5447 | ret = -1; |
| 5448 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5449 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5450 | if (tmp != 0) |
| 5451 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5452 | } else { |
| 5453 | if (ctxt->error != NULL) |
| 5454 | ctxt->error(ctxt->userData, |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5455 | "grammar has unexpected child %s\n", nodes->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5456 | ctxt->nbErrors++; |
| 5457 | ret = -1; |
| 5458 | } |
| 5459 | nodes = nodes->next; |
| 5460 | } |
| 5461 | return (ret); |
| 5462 | } |
| 5463 | |
| 5464 | /** |
| 5465 | * xmlRelaxNGCheckReference: |
| 5466 | * @ref: the ref |
| 5467 | * @ctxt: a Relax-NG parser context |
| 5468 | * @name: the name associated to the defines |
| 5469 | * |
| 5470 | * Applies the 4.17. combine attribute rule for all the define |
| 5471 | * element of a given grammar using the same name. |
| 5472 | */ |
| 5473 | static void |
| 5474 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
| 5475 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5476 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5477 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5478 | |
| 5479 | grammar = ctxt->grammar; |
| 5480 | if (grammar == NULL) { |
| 5481 | if (ctxt->error != NULL) |
| 5482 | ctxt->error(ctxt->userData, |
| 5483 | "Internal error: no grammar in CheckReference %s\n", |
| 5484 | name); |
| 5485 | ctxt->nbErrors++; |
| 5486 | return; |
| 5487 | } |
| 5488 | if (ref->content != NULL) { |
| 5489 | if (ctxt->error != NULL) |
| 5490 | ctxt->error(ctxt->userData, |
| 5491 | "Internal error: reference has content in CheckReference %s\n", |
| 5492 | name); |
| 5493 | ctxt->nbErrors++; |
| 5494 | return; |
| 5495 | } |
| 5496 | if (grammar->defs != NULL) { |
| 5497 | def = xmlHashLookup(grammar->defs, name); |
| 5498 | if (def != NULL) { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5499 | cur = ref; |
| 5500 | while (cur != NULL) { |
| 5501 | cur->content = def; |
| 5502 | cur = cur->nextHash; |
| 5503 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5504 | } else { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5505 | if (ctxt->error != NULL) |
| 5506 | ctxt->error(ctxt->userData, |
| 5507 | "Reference %s has no matching definition\n", |
| 5508 | name); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5509 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5510 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5511 | } else { |
| 5512 | if (ctxt->error != NULL) |
| 5513 | ctxt->error(ctxt->userData, |
| 5514 | "Reference %s has no matching definition\n", |
| 5515 | name); |
| 5516 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5517 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5518 | } |
| 5519 | |
| 5520 | /** |
| 5521 | * xmlRelaxNGCheckCombine: |
| 5522 | * @define: the define(s) list |
| 5523 | * @ctxt: a Relax-NG parser context |
| 5524 | * @name: the name associated to the defines |
| 5525 | * |
| 5526 | * Applies the 4.17. combine attribute rule for all the define |
| 5527 | * element of a given grammar using the same name. |
| 5528 | */ |
| 5529 | static void |
| 5530 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
| 5531 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5532 | xmlChar *combine; |
| 5533 | int choiceOrInterleave = -1; |
| 5534 | int missing = 0; |
| 5535 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5536 | |
| 5537 | if (define->nextHash == NULL) |
| 5538 | return; |
| 5539 | cur = define; |
| 5540 | while (cur != NULL) { |
| 5541 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5542 | if (combine != NULL) { |
| 5543 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5544 | if (choiceOrInterleave == -1) |
| 5545 | choiceOrInterleave = 1; |
| 5546 | else if (choiceOrInterleave == 0) { |
| 5547 | if (ctxt->error != NULL) |
| 5548 | ctxt->error(ctxt->userData, |
| 5549 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5550 | name); |
| 5551 | ctxt->nbErrors++; |
| 5552 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5553 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5554 | if (choiceOrInterleave == -1) |
| 5555 | choiceOrInterleave = 0; |
| 5556 | else if (choiceOrInterleave == 1) { |
| 5557 | if (ctxt->error != NULL) |
| 5558 | ctxt->error(ctxt->userData, |
| 5559 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5560 | name); |
| 5561 | ctxt->nbErrors++; |
| 5562 | } |
| 5563 | } else { |
| 5564 | if (ctxt->error != NULL) |
| 5565 | ctxt->error(ctxt->userData, |
| 5566 | "Defines for %s use unknown combine value '%s''\n", |
| 5567 | name, combine); |
| 5568 | ctxt->nbErrors++; |
| 5569 | } |
| 5570 | xmlFree(combine); |
| 5571 | } else { |
| 5572 | if (missing == 0) |
| 5573 | missing = 1; |
| 5574 | else { |
| 5575 | if (ctxt->error != NULL) |
| 5576 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5577 | "Some defines for %s needs the combine attribute\n", |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5578 | name); |
| 5579 | ctxt->nbErrors++; |
| 5580 | } |
| 5581 | } |
| 5582 | |
| 5583 | cur = cur->nextHash; |
| 5584 | } |
| 5585 | #ifdef DEBUG |
| 5586 | xmlGenericError(xmlGenericErrorContext, |
| 5587 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5588 | name, choiceOrInterleave); |
| 5589 | #endif |
| 5590 | if (choiceOrInterleave == -1) |
| 5591 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5592 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5593 | if (cur == NULL) |
| 5594 | return; |
| 5595 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5596 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5597 | else |
| 5598 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5599 | tmp = define; |
| 5600 | last = NULL; |
| 5601 | while (tmp != NULL) { |
| 5602 | if (tmp->content != NULL) { |
| 5603 | if (tmp->content->next != NULL) { |
| 5604 | /* |
| 5605 | * we need first to create a wrapper. |
| 5606 | */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5607 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5608 | if (tmp2 == NULL) |
| 5609 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5610 | tmp2->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5611 | tmp2->content = tmp->content; |
| 5612 | } else { |
| 5613 | tmp2 = tmp->content; |
| 5614 | } |
| 5615 | if (last == NULL) { |
| 5616 | cur->content = tmp2; |
| 5617 | } else { |
| 5618 | last->next = tmp2; |
| 5619 | } |
| 5620 | last = tmp2; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5621 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 5622 | tmp->content = cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5623 | tmp = tmp->nextHash; |
| 5624 | } |
| 5625 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5626 | if (choiceOrInterleave == 0) { |
| 5627 | if (ctxt->interleaves == NULL) |
| 5628 | ctxt->interleaves = xmlHashCreate(10); |
| 5629 | if (ctxt->interleaves == NULL) { |
| 5630 | if (ctxt->error != NULL) |
| 5631 | ctxt->error(ctxt->userData, |
| 5632 | "Failed to create interleaves hash table\n"); |
| 5633 | ctxt->nbErrors++; |
| 5634 | } else { |
| 5635 | char tmpname[32]; |
| 5636 | |
| 5637 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5638 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5639 | if (ctxt->error != NULL) |
| 5640 | ctxt->error(ctxt->userData, |
| 5641 | "Failed to add %s to hash table\n", tmpname); |
| 5642 | ctxt->nbErrors++; |
| 5643 | } |
| 5644 | } |
| 5645 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5646 | } |
| 5647 | |
| 5648 | /** |
| 5649 | * xmlRelaxNGCombineStart: |
| 5650 | * @ctxt: a Relax-NG parser context |
| 5651 | * @grammar: the grammar |
| 5652 | * |
| 5653 | * Applies the 4.17. combine rule for all the start |
| 5654 | * element of a given grammar. |
| 5655 | */ |
| 5656 | static void |
| 5657 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
| 5658 | xmlRelaxNGGrammarPtr grammar) { |
| 5659 | xmlRelaxNGDefinePtr starts; |
| 5660 | xmlChar *combine; |
| 5661 | int choiceOrInterleave = -1; |
| 5662 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5663 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5664 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5665 | starts = grammar->start; |
| 5666 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5667 | return; |
| 5668 | cur = starts; |
| 5669 | while (cur != NULL) { |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5670 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5671 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5672 | combine = NULL; |
| 5673 | if (ctxt->error != NULL) |
| 5674 | ctxt->error(ctxt->userData, |
| 5675 | "Internal error: start element not found\n"); |
| 5676 | ctxt->nbErrors++; |
| 5677 | } else { |
| 5678 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5679 | } |
| 5680 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5681 | if (combine != NULL) { |
| 5682 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5683 | if (choiceOrInterleave == -1) |
| 5684 | choiceOrInterleave = 1; |
| 5685 | else if (choiceOrInterleave == 0) { |
| 5686 | if (ctxt->error != NULL) |
| 5687 | ctxt->error(ctxt->userData, |
| 5688 | "<start> use both 'choice' and 'interleave'\n"); |
| 5689 | ctxt->nbErrors++; |
| 5690 | } |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5691 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5692 | if (choiceOrInterleave == -1) |
| 5693 | choiceOrInterleave = 0; |
| 5694 | else if (choiceOrInterleave == 1) { |
| 5695 | if (ctxt->error != NULL) |
| 5696 | ctxt->error(ctxt->userData, |
| 5697 | "<start> use both 'choice' and 'interleave'\n"); |
| 5698 | ctxt->nbErrors++; |
| 5699 | } |
| 5700 | } else { |
| 5701 | if (ctxt->error != NULL) |
| 5702 | ctxt->error(ctxt->userData, |
| 5703 | "<start> uses unknown combine value '%s''\n", combine); |
| 5704 | ctxt->nbErrors++; |
| 5705 | } |
| 5706 | xmlFree(combine); |
| 5707 | } else { |
| 5708 | if (missing == 0) |
| 5709 | missing = 1; |
| 5710 | else { |
| 5711 | if (ctxt->error != NULL) |
| 5712 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5713 | "Some <start> element miss the combine attribute\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5714 | ctxt->nbErrors++; |
| 5715 | } |
| 5716 | } |
| 5717 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5718 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5719 | } |
| 5720 | #ifdef DEBUG |
| 5721 | xmlGenericError(xmlGenericErrorContext, |
| 5722 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5723 | choiceOrInterleave); |
| 5724 | #endif |
| 5725 | if (choiceOrInterleave == -1) |
| 5726 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5727 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5728 | if (cur == NULL) |
| 5729 | return; |
| 5730 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5731 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5732 | else |
| 5733 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5734 | cur->content = grammar->start; |
| 5735 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5736 | if (choiceOrInterleave == 0) { |
| 5737 | if (ctxt->interleaves == NULL) |
| 5738 | ctxt->interleaves = xmlHashCreate(10); |
| 5739 | if (ctxt->interleaves == NULL) { |
| 5740 | if (ctxt->error != NULL) |
| 5741 | ctxt->error(ctxt->userData, |
| 5742 | "Failed to create interleaves hash table\n"); |
| 5743 | ctxt->nbErrors++; |
| 5744 | } else { |
| 5745 | char tmpname[32]; |
| 5746 | |
| 5747 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5748 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5749 | if (ctxt->error != NULL) |
| 5750 | ctxt->error(ctxt->userData, |
| 5751 | "Failed to add %s to hash table\n", tmpname); |
| 5752 | ctxt->nbErrors++; |
| 5753 | } |
| 5754 | } |
| 5755 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5756 | } |
| 5757 | |
| 5758 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5759 | * xmlRelaxNGCheckCycles: |
| 5760 | * @ctxt: a Relax-NG parser context |
| 5761 | * @nodes: grammar children nodes |
| 5762 | * @depth: the counter |
| 5763 | * |
| 5764 | * Check for cycles. |
| 5765 | * |
| 5766 | * Returns 0 if check passed, and -1 in case of error |
| 5767 | */ |
| 5768 | static int |
| 5769 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5770 | xmlRelaxNGDefinePtr cur, int depth) { |
| 5771 | int ret = 0; |
| 5772 | |
| 5773 | while ((ret == 0) && (cur != NULL)) { |
| 5774 | if ((cur->type == XML_RELAXNG_REF) || |
| 5775 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5776 | if (cur->depth == -1) { |
| 5777 | cur->depth = depth; |
| 5778 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5779 | cur->depth = -2; |
| 5780 | } else if (depth == cur->depth) { |
| 5781 | if (ctxt->error != NULL) |
| 5782 | ctxt->error(ctxt->userData, |
| 5783 | "Detected a cycle in %s references\n", cur->name); |
| 5784 | ctxt->nbErrors++; |
| 5785 | return(-1); |
| 5786 | } |
| 5787 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5788 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5789 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5790 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5791 | } |
| 5792 | cur = cur->next; |
| 5793 | } |
| 5794 | return(ret); |
| 5795 | } |
| 5796 | |
| 5797 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5798 | * xmlRelaxNGTryUnlink: |
| 5799 | * @ctxt: a Relax-NG parser context |
| 5800 | * @cur: the definition to unlink |
| 5801 | * @parent: the parent definition |
| 5802 | * @prev: the previous sibling definition |
| 5803 | * |
| 5804 | * Try to unlink a definition. If not possble make it a NOOP |
| 5805 | * |
| 5806 | * Returns the new prev definition |
| 5807 | */ |
| 5808 | static xmlRelaxNGDefinePtr |
| 5809 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5810 | xmlRelaxNGDefinePtr cur, |
| 5811 | xmlRelaxNGDefinePtr parent, |
| 5812 | xmlRelaxNGDefinePtr prev) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5813 | if (prev != NULL) { |
| 5814 | prev->next = cur->next; |
| 5815 | } else { |
| 5816 | if (parent != NULL) { |
| 5817 | if (parent->content == cur) |
| 5818 | parent->content = cur->next; |
| 5819 | else if (parent->attrs == cur) |
| 5820 | parent->attrs = cur->next; |
| 5821 | else if (parent->nameClass == cur) |
| 5822 | parent->nameClass = cur->next; |
| 5823 | } else { |
| 5824 | cur->type = XML_RELAXNG_NOOP; |
| 5825 | prev = cur; |
| 5826 | } |
| 5827 | } |
| 5828 | return(prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5829 | } |
| 5830 | |
| 5831 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5832 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5833 | * @ctxt: a Relax-NG parser context |
| 5834 | * @nodes: grammar children nodes |
| 5835 | * |
| 5836 | * Check for simplification of empty and notAllowed |
| 5837 | */ |
| 5838 | static void |
| 5839 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5840 | xmlRelaxNGDefinePtr cur, |
| 5841 | xmlRelaxNGDefinePtr parent) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5842 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5843 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5844 | while (cur != NULL) { |
| 5845 | if ((cur->type == XML_RELAXNG_REF) || |
| 5846 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5847 | if (cur->depth != -3) { |
| 5848 | cur->depth = -3; |
| 5849 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5850 | } |
| 5851 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5852 | cur->parent = parent; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5853 | if ((parent != NULL) && |
| 5854 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5855 | (parent->type == XML_RELAXNG_LIST) || |
| 5856 | (parent->type == XML_RELAXNG_GROUP) || |
| 5857 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5858 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5859 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5860 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5861 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5862 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5863 | if ((parent != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5864 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5865 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5866 | } else |
| 5867 | prev = cur; |
| 5868 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5869 | cur->parent = parent; |
| 5870 | if ((parent != NULL) && |
| 5871 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5872 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5873 | parent->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5874 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5875 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5876 | if ((parent != NULL) && |
| 5877 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5878 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5879 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5880 | } else |
| 5881 | prev = cur; |
| 5882 | } else { |
| 5883 | cur->parent = parent; |
| 5884 | if (cur->content != NULL) |
| 5885 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5886 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5887 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5888 | if (cur->nameClass != NULL) |
| 5889 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5890 | /* |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5891 | * On Elements, try to move attribute only generating rules on |
| 5892 | * the attrs rules. |
| 5893 | */ |
| 5894 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5895 | int attronly; |
| 5896 | xmlRelaxNGDefinePtr tmp, pre; |
| 5897 | |
| 5898 | while (cur->content != NULL) { |
| 5899 | attronly = xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 5900 | if (attronly == 1) { |
| 5901 | /* |
| 5902 | * migrate cur->content to attrs |
| 5903 | */ |
| 5904 | tmp = cur->content; |
| 5905 | cur->content = tmp->next; |
| 5906 | tmp->next = cur->attrs; |
| 5907 | cur->attrs = tmp; |
| 5908 | } else { |
| 5909 | /* |
| 5910 | * cur->content can generate elements or text |
| 5911 | */ |
| 5912 | break; |
| 5913 | } |
| 5914 | } |
| 5915 | pre = cur->content; |
| 5916 | while ((pre != NULL) && (pre->next != NULL)) { |
| 5917 | tmp = pre->next; |
| 5918 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 5919 | if (attronly == 1) { |
| 5920 | /* |
| 5921 | * migrate tmp to attrs |
| 5922 | */ |
| 5923 | pre->next = tmp->next; |
| 5924 | tmp->next = cur->attrs; |
| 5925 | cur->attrs = tmp; |
| 5926 | } else { |
| 5927 | pre = tmp; |
| 5928 | } |
| 5929 | } |
| 5930 | } |
| 5931 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5932 | * This may result in a simplification |
| 5933 | */ |
| 5934 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 5935 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 5936 | if (cur->content == NULL) |
| 5937 | cur->type = XML_RELAXNG_EMPTY; |
| 5938 | else if (cur->content->next == NULL) { |
| 5939 | if ((parent == NULL) && (prev == NULL)) { |
| 5940 | cur->type = XML_RELAXNG_NOOP; |
| 5941 | } else if (prev == NULL) { |
| 5942 | parent->content = cur->content; |
| 5943 | cur->content->next = cur->next; |
| 5944 | cur = cur->content; |
| 5945 | } else { |
| 5946 | cur->content->next = cur->next; |
| 5947 | prev->next = cur->content; |
| 5948 | cur = cur->content; |
| 5949 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5950 | } |
| 5951 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5952 | /* |
| 5953 | * the current node may have been transformed back |
| 5954 | */ |
| 5955 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 5956 | (cur->content != NULL) && |
| 5957 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 5958 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5959 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5960 | if ((parent != NULL) && |
| 5961 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5962 | (parent->type == XML_RELAXNG_LIST) || |
| 5963 | (parent->type == XML_RELAXNG_GROUP) || |
| 5964 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5965 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5966 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5967 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5968 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5969 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5970 | if ((parent != NULL) && |
| 5971 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5972 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5973 | } else |
| 5974 | prev = cur; |
| 5975 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5976 | if ((parent != NULL) && |
| 5977 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5978 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5979 | parent->type = XML_RELAXNG_EMPTY; |
| 5980 | break; |
| 5981 | } |
| 5982 | if ((parent != NULL) && |
| 5983 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5984 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5985 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 5986 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5987 | } else |
| 5988 | prev = cur; |
| 5989 | } else { |
| 5990 | prev = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5991 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5992 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5993 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5994 | } |
| 5995 | } |
| 5996 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5997 | /** |
| 5998 | * xmlRelaxNGGroupContentType: |
| 5999 | * @ct1: the first content type |
| 6000 | * @ct2: the second content type |
| 6001 | * |
| 6002 | * Try to group 2 content types |
| 6003 | * |
| 6004 | * Returns the content type |
| 6005 | */ |
| 6006 | static xmlRelaxNGContentType |
| 6007 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
| 6008 | xmlRelaxNGContentType ct2) { |
| 6009 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 6010 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6011 | return(XML_RELAXNG_CONTENT_ERROR); |
| 6012 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
| 6013 | return(ct2); |
| 6014 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
| 6015 | return(ct1); |
| 6016 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
| 6017 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6018 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 6019 | return(XML_RELAXNG_CONTENT_ERROR); |
| 6020 | } |
| 6021 | |
| 6022 | /** |
| 6023 | * xmlRelaxNGMaxContentType: |
| 6024 | * @ct1: the first content type |
| 6025 | * @ct2: the second content type |
| 6026 | * |
| 6027 | * Compute the max content-type |
| 6028 | * |
| 6029 | * Returns the content type |
| 6030 | */ |
| 6031 | static xmlRelaxNGContentType |
| 6032 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
| 6033 | xmlRelaxNGContentType ct2) { |
| 6034 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 6035 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 6036 | return(XML_RELAXNG_CONTENT_ERROR); |
| 6037 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
| 6038 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 6039 | return(XML_RELAXNG_CONTENT_SIMPLE); |
| 6040 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
| 6041 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 6042 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 6043 | return(XML_RELAXNG_CONTENT_EMPTY); |
| 6044 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6045 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6046 | /** |
| 6047 | * xmlRelaxNGCheckRules: |
| 6048 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6049 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6050 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6051 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6052 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6053 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6054 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6055 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6056 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6057 | static xmlRelaxNGContentType |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6058 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 6059 | xmlRelaxNGDefinePtr cur, int flags, |
| 6060 | xmlRelaxNGType ptype) { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 6061 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6062 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6063 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6064 | while (cur != NULL) { |
| 6065 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6066 | if ((cur->type == XML_RELAXNG_REF) || |
| 6067 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 6068 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6069 | if (ctxt->error != NULL) |
| 6070 | ctxt->error(ctxt->userData, |
| 6071 | "Found forbidden pattern list//ref\n"); |
| 6072 | ctxt->nbErrors++; |
| 6073 | } |
| 6074 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6075 | if (ctxt->error != NULL) |
| 6076 | ctxt->error(ctxt->userData, |
| 6077 | "Found forbidden pattern data/except//ref\n"); |
| 6078 | ctxt->nbErrors++; |
| 6079 | } |
| 6080 | if (cur->depth > -4) { |
| 6081 | cur->depth = -4; |
| 6082 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6083 | flags, cur->type); |
| 6084 | cur->depth = ret - 15 ; |
| 6085 | } else if (cur->depth == -4) { |
| 6086 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6087 | } else { |
| 6088 | ret = (xmlRelaxNGContentType) cur->depth + 15; |
| 6089 | } |
| 6090 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6091 | /* |
| 6092 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6093 | */ |
| 6094 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6095 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6096 | if (ctxt->error != NULL) |
| 6097 | ctxt->error(ctxt->userData, |
| 6098 | "Found forbidden pattern data/except//element(ref)\n"); |
| 6099 | ctxt->nbErrors++; |
| 6100 | } |
| 6101 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6102 | if (ctxt->error != NULL) |
| 6103 | ctxt->error(ctxt->userData, |
| 6104 | "Found forbidden pattern list//element(ref)\n"); |
| 6105 | ctxt->nbErrors++; |
| 6106 | } |
| 6107 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6108 | if (ctxt->error != NULL) |
| 6109 | ctxt->error(ctxt->userData, |
| 6110 | "Found forbidden pattern attribute//element(ref)\n"); |
| 6111 | ctxt->nbErrors++; |
| 6112 | } |
| 6113 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6114 | if (ctxt->error != NULL) |
| 6115 | ctxt->error(ctxt->userData, |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 6116 | "Found forbidden pattern attribute//element(ref)\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6117 | ctxt->nbErrors++; |
| 6118 | } |
| 6119 | /* |
| 6120 | * reset since in the simple form elements are only child |
| 6121 | * of grammar/define |
| 6122 | */ |
| 6123 | nflags = 0; |
| 6124 | ret = xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6125 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6126 | if (ctxt->error != NULL) |
| 6127 | ctxt->error(ctxt->userData, |
| 6128 | "Element %s attributes have a content type error\n", |
| 6129 | cur->name); |
| 6130 | ctxt->nbErrors++; |
| 6131 | } |
| 6132 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6133 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6134 | if (ctxt->error != NULL) |
| 6135 | ctxt->error(ctxt->userData, |
| 6136 | "Element %s has a content type error\n", |
| 6137 | cur->name); |
| 6138 | ctxt->nbErrors++; |
| 6139 | } else { |
| 6140 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6141 | } |
| 6142 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6143 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6144 | if (ctxt->error != NULL) |
| 6145 | ctxt->error(ctxt->userData, |
| 6146 | "Found forbidden pattern attribute//attribute\n"); |
| 6147 | ctxt->nbErrors++; |
| 6148 | } |
| 6149 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6150 | if (ctxt->error != NULL) |
| 6151 | ctxt->error(ctxt->userData, |
| 6152 | "Found forbidden pattern list//attribute\n"); |
| 6153 | ctxt->nbErrors++; |
| 6154 | } |
| 6155 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6156 | if (ctxt->error != NULL) |
| 6157 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6158 | "Found forbidden pattern oneOrMore//group//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6159 | ctxt->nbErrors++; |
| 6160 | } |
| 6161 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6162 | if (ctxt->error != NULL) |
| 6163 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6164 | "Found forbidden pattern oneOrMore//interleave//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6165 | ctxt->nbErrors++; |
| 6166 | } |
| 6167 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6168 | if (ctxt->error != NULL) |
| 6169 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6170 | "Found forbidden pattern data/except//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6171 | ctxt->nbErrors++; |
| 6172 | } |
| 6173 | if (flags & XML_RELAXNG_IN_START) { |
| 6174 | if (ctxt->error != NULL) |
| 6175 | ctxt->error(ctxt->userData, |
| 6176 | "Found forbidden pattern start//attribute\n"); |
| 6177 | ctxt->nbErrors++; |
| 6178 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 6179 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) && (cur->name == NULL)) { |
| 6180 | if (cur->ns == NULL) { |
| 6181 | if (ctxt->error != NULL) |
| 6182 | ctxt->error(ctxt->userData, |
| 6183 | "Found anyName attribute without oneOrMore ancestor\n"); |
| 6184 | ctxt->nbErrors++; |
| 6185 | } else { |
| 6186 | if (ctxt->error != NULL) |
| 6187 | ctxt->error(ctxt->userData, |
| 6188 | "Found nsName attribute without oneOrMore ancestor\n"); |
| 6189 | ctxt->nbErrors++; |
| 6190 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6191 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6192 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6193 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6194 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6195 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6196 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6197 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6198 | if (ctxt->error != NULL) |
| 6199 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6200 | "Found forbidden pattern data/except//oneOrMore\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6201 | ctxt->nbErrors++; |
| 6202 | } |
| 6203 | if (flags & XML_RELAXNG_IN_START) { |
| 6204 | if (ctxt->error != NULL) |
| 6205 | ctxt->error(ctxt->userData, |
| 6206 | "Found forbidden pattern start//oneOrMore\n"); |
| 6207 | ctxt->nbErrors++; |
| 6208 | } |
| 6209 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6210 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6211 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6212 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6213 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6214 | if (ctxt->error != NULL) |
| 6215 | ctxt->error(ctxt->userData, |
| 6216 | "Found forbidden pattern list//list\n"); |
| 6217 | ctxt->nbErrors++; |
| 6218 | } |
| 6219 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6220 | if (ctxt->error != NULL) |
| 6221 | ctxt->error(ctxt->userData, |
| 6222 | "Found forbidden pattern data/except//list\n"); |
| 6223 | ctxt->nbErrors++; |
| 6224 | } |
| 6225 | if (flags & XML_RELAXNG_IN_START) { |
| 6226 | if (ctxt->error != NULL) |
| 6227 | ctxt->error(ctxt->userData, |
| 6228 | "Found forbidden pattern start//list\n"); |
| 6229 | ctxt->nbErrors++; |
| 6230 | } |
| 6231 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6232 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6233 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6234 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6235 | if (ctxt->error != NULL) |
| 6236 | ctxt->error(ctxt->userData, |
| 6237 | "Found forbidden pattern data/except//group\n"); |
| 6238 | ctxt->nbErrors++; |
| 6239 | } |
| 6240 | if (flags & XML_RELAXNG_IN_START) { |
| 6241 | if (ctxt->error != NULL) |
| 6242 | ctxt->error(ctxt->userData, |
| 6243 | "Found forbidden pattern start//group\n"); |
| 6244 | ctxt->nbErrors++; |
| 6245 | } |
| 6246 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6247 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6248 | else |
| 6249 | nflags = flags; |
| 6250 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6251 | /* |
| 6252 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6253 | */ |
| 6254 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6255 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6256 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6257 | if (ctxt->error != NULL) |
| 6258 | ctxt->error(ctxt->userData, |
| 6259 | "Found forbidden pattern list//interleave\n"); |
| 6260 | ctxt->nbErrors++; |
| 6261 | } |
| 6262 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6263 | if (ctxt->error != NULL) |
| 6264 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6265 | "Found forbidden pattern data/except//interleave\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6266 | ctxt->nbErrors++; |
| 6267 | } |
| 6268 | if (flags & XML_RELAXNG_IN_START) { |
| 6269 | if (ctxt->error != NULL) |
| 6270 | ctxt->error(ctxt->userData, |
| 6271 | "Found forbidden pattern start//interleave\n"); |
| 6272 | ctxt->nbErrors++; |
| 6273 | } |
| 6274 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6275 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6276 | else |
| 6277 | nflags = flags; |
| 6278 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6279 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6280 | if ((cur->parent != NULL) && |
| 6281 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6282 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6283 | else |
| 6284 | nflags = flags; |
| 6285 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6286 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6287 | if (flags & XML_RELAXNG_IN_START) { |
| 6288 | if (ctxt->error != NULL) |
| 6289 | ctxt->error(ctxt->userData, |
| 6290 | "Found forbidden pattern start//data\n"); |
| 6291 | ctxt->nbErrors++; |
| 6292 | } |
| 6293 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6294 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6295 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6296 | if (flags & XML_RELAXNG_IN_START) { |
| 6297 | if (ctxt->error != NULL) |
| 6298 | ctxt->error(ctxt->userData, |
| 6299 | "Found forbidden pattern start//value\n"); |
| 6300 | ctxt->nbErrors++; |
| 6301 | } |
| 6302 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6303 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6304 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6305 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6306 | if (ctxt->error != NULL) |
| 6307 | ctxt->error(ctxt->userData, |
| 6308 | "Found forbidden pattern list//text\n"); |
| 6309 | ctxt->nbErrors++; |
| 6310 | } |
| 6311 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6312 | if (ctxt->error != NULL) |
| 6313 | ctxt->error(ctxt->userData, |
| 6314 | "Found forbidden pattern data/except//text\n"); |
| 6315 | ctxt->nbErrors++; |
| 6316 | } |
| 6317 | if (flags & XML_RELAXNG_IN_START) { |
| 6318 | if (ctxt->error != NULL) |
| 6319 | ctxt->error(ctxt->userData, |
| 6320 | "Found forbidden pattern start//text\n"); |
| 6321 | ctxt->nbErrors++; |
| 6322 | } |
| 6323 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6324 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6325 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6326 | if (ctxt->error != NULL) |
| 6327 | ctxt->error(ctxt->userData, |
| 6328 | "Found forbidden pattern data/except//empty\n"); |
| 6329 | ctxt->nbErrors++; |
| 6330 | } |
| 6331 | if (flags & XML_RELAXNG_IN_START) { |
| 6332 | if (ctxt->error != NULL) |
| 6333 | ctxt->error(ctxt->userData, |
| 6334 | "Found forbidden pattern start//empty\n"); |
| 6335 | ctxt->nbErrors++; |
| 6336 | } |
| 6337 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6338 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6339 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6340 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6341 | } else { |
| 6342 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6343 | } |
| 6344 | cur = cur->next; |
| 6345 | if (ptype == XML_RELAXNG_GROUP) { |
| 6346 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6347 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6348 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6349 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6350 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6351 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6352 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6353 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6354 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6355 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6356 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6357 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6358 | else |
| 6359 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6360 | } else { |
| 6361 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6362 | } |
| 6363 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6364 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6365 | return(val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6366 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6367 | |
| 6368 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6369 | * xmlRelaxNGParseGrammar: |
| 6370 | * @ctxt: a Relax-NG parser context |
| 6371 | * @nodes: grammar children nodes |
| 6372 | * |
| 6373 | * parse a Relax-NG <grammar> node |
| 6374 | * |
| 6375 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6376 | * NULL in case of error |
| 6377 | */ |
| 6378 | static xmlRelaxNGGrammarPtr |
| 6379 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 6380 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6381 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6382 | #ifdef DEBUG_GRAMMAR |
| 6383 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6384 | #endif |
| 6385 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6386 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6387 | if (ret == NULL) |
| 6388 | return(NULL); |
| 6389 | |
| 6390 | /* |
| 6391 | * Link the new grammar in the tree |
| 6392 | */ |
| 6393 | ret->parent = ctxt->grammar; |
| 6394 | if (ctxt->grammar != NULL) { |
| 6395 | tmp = ctxt->grammar->children; |
| 6396 | if (tmp == NULL) { |
| 6397 | ctxt->grammar->children = ret; |
| 6398 | } else { |
| 6399 | while (tmp->next != NULL) |
| 6400 | tmp = tmp->next; |
| 6401 | tmp->next = ret; |
| 6402 | } |
| 6403 | } |
| 6404 | |
| 6405 | old = ctxt->grammar; |
| 6406 | ctxt->grammar = ret; |
| 6407 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6408 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6409 | if (ctxt->grammar == NULL) { |
| 6410 | if (ctxt->error != NULL) |
| 6411 | ctxt->error(ctxt->userData, |
| 6412 | "Failed to parse <grammar> content\n"); |
| 6413 | ctxt->nbErrors++; |
| 6414 | } else if (ctxt->grammar->start == NULL) { |
| 6415 | if (ctxt->error != NULL) |
| 6416 | ctxt->error(ctxt->userData, |
| 6417 | "Element <grammar> has no <start>\n"); |
| 6418 | ctxt->nbErrors++; |
| 6419 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6420 | |
| 6421 | /* |
| 6422 | * Apply 4.17 mergingd rules to defines and starts |
| 6423 | */ |
| 6424 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6425 | if (ret->defs != NULL) { |
| 6426 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6427 | ctxt); |
| 6428 | } |
| 6429 | |
| 6430 | /* |
| 6431 | * link together defines and refs in this grammar |
| 6432 | */ |
| 6433 | if (ret->refs != NULL) { |
| 6434 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6435 | ctxt); |
| 6436 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6437 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6438 | ctxt->grammar = old; |
| 6439 | return(ret); |
| 6440 | } |
| 6441 | |
| 6442 | /** |
| 6443 | * xmlRelaxNGParseDocument: |
| 6444 | * @ctxt: a Relax-NG parser context |
| 6445 | * @node: the root node of the RelaxNG schema |
| 6446 | * |
| 6447 | * parse a Relax-NG definition resource and build an internal |
| 6448 | * xmlRelaxNG struture which can be used to validate instances. |
| 6449 | * |
| 6450 | * Returns the internal XML RelaxNG structure built or |
| 6451 | * NULL in case of error |
| 6452 | */ |
| 6453 | static xmlRelaxNGPtr |
| 6454 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6455 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6456 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6457 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6458 | |
| 6459 | if ((ctxt == NULL) || (node == NULL)) |
| 6460 | return (NULL); |
| 6461 | |
| 6462 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6463 | if (schema == NULL) |
| 6464 | return(NULL); |
| 6465 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6466 | olddefine = ctxt->define; |
| 6467 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6468 | if (IS_RELAXNG(node, "grammar")) { |
| 6469 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 6470 | } else { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6471 | xmlRelaxNGGrammarPtr tmp, ret; |
| 6472 | |
| 6473 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6474 | if (schema->topgrammar == NULL) { |
| 6475 | return(schema); |
| 6476 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6477 | /* |
| 6478 | * Link the new grammar in the tree |
| 6479 | */ |
| 6480 | ret->parent = ctxt->grammar; |
| 6481 | if (ctxt->grammar != NULL) { |
| 6482 | tmp = ctxt->grammar->children; |
| 6483 | if (tmp == NULL) { |
| 6484 | ctxt->grammar->children = ret; |
| 6485 | } else { |
| 6486 | while (tmp->next != NULL) |
| 6487 | tmp = tmp->next; |
| 6488 | tmp->next = ret; |
| 6489 | } |
| 6490 | } |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6491 | old = ctxt->grammar; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6492 | ctxt->grammar = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6493 | xmlRelaxNGParseStart(ctxt, node); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6494 | if (old != NULL) |
| 6495 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6496 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6497 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6498 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6499 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6500 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6501 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6502 | while ((schema->topgrammar->start != NULL) && |
| 6503 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6504 | (schema->topgrammar->start->next != NULL)) |
| 6505 | schema->topgrammar->start = schema->topgrammar->start->content; |
| 6506 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6507 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6508 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6509 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6510 | |
| 6511 | #ifdef DEBUG |
| 6512 | if (schema == NULL) |
| 6513 | xmlGenericError(xmlGenericErrorContext, |
| 6514 | "xmlRelaxNGParseDocument() failed\n"); |
| 6515 | #endif |
| 6516 | |
| 6517 | return (schema); |
| 6518 | } |
| 6519 | |
| 6520 | /************************************************************************ |
| 6521 | * * |
| 6522 | * Reading RelaxNGs * |
| 6523 | * * |
| 6524 | ************************************************************************/ |
| 6525 | |
| 6526 | /** |
| 6527 | * xmlRelaxNGNewParserCtxt: |
| 6528 | * @URL: the location of the schema |
| 6529 | * |
| 6530 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6531 | * to contain an XML RelaxNGs file. |
| 6532 | * |
| 6533 | * Returns the parser context or NULL in case of error |
| 6534 | */ |
| 6535 | xmlRelaxNGParserCtxtPtr |
| 6536 | xmlRelaxNGNewParserCtxt(const char *URL) { |
| 6537 | xmlRelaxNGParserCtxtPtr ret; |
| 6538 | |
| 6539 | if (URL == NULL) |
| 6540 | return(NULL); |
| 6541 | |
| 6542 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6543 | if (ret == NULL) { |
| 6544 | xmlGenericError(xmlGenericErrorContext, |
| 6545 | "Failed to allocate new schama parser context for %s\n", URL); |
| 6546 | return (NULL); |
| 6547 | } |
| 6548 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6549 | ret->URL = xmlStrdup((const xmlChar *)URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6550 | ret->error = xmlGenericError; |
| 6551 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6552 | return (ret); |
| 6553 | } |
| 6554 | |
| 6555 | /** |
| 6556 | * xmlRelaxNGNewMemParserCtxt: |
| 6557 | * @buffer: a pointer to a char array containing the schemas |
| 6558 | * @size: the size of the array |
| 6559 | * |
| 6560 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6561 | * to contain an XML RelaxNGs file. |
| 6562 | * |
| 6563 | * Returns the parser context or NULL in case of error |
| 6564 | */ |
| 6565 | xmlRelaxNGParserCtxtPtr |
| 6566 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) { |
| 6567 | xmlRelaxNGParserCtxtPtr ret; |
| 6568 | |
| 6569 | if ((buffer == NULL) || (size <= 0)) |
| 6570 | return(NULL); |
| 6571 | |
| 6572 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6573 | if (ret == NULL) { |
| 6574 | xmlGenericError(xmlGenericErrorContext, |
| 6575 | "Failed to allocate new schama parser context\n"); |
| 6576 | return (NULL); |
| 6577 | } |
| 6578 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6579 | ret->buffer = buffer; |
| 6580 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6581 | ret->error = xmlGenericError; |
| 6582 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6583 | return (ret); |
| 6584 | } |
| 6585 | |
| 6586 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6587 | * xmlRelaxNGNewDocParserCtxt: |
| 6588 | * @doc: a preparsed document tree |
| 6589 | * |
| 6590 | * Create an XML RelaxNGs parser context for that document. |
| 6591 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6592 | * document, the @doc parameter is duplicated internally. |
| 6593 | * |
| 6594 | * Returns the parser context or NULL in case of error |
| 6595 | */ |
| 6596 | xmlRelaxNGParserCtxtPtr |
| 6597 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) { |
| 6598 | xmlRelaxNGParserCtxtPtr ret; |
| 6599 | xmlDocPtr copy; |
| 6600 | |
| 6601 | if (doc == NULL) |
| 6602 | return(NULL); |
| 6603 | copy = xmlCopyDoc(doc, 1); |
| 6604 | if (copy == NULL) |
| 6605 | return(NULL); |
| 6606 | |
| 6607 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6608 | if (ret == NULL) { |
| 6609 | xmlGenericError(xmlGenericErrorContext, |
| 6610 | "Failed to allocate new schama parser context\n"); |
| 6611 | return (NULL); |
| 6612 | } |
| 6613 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6614 | ret->document = copy; |
| 6615 | ret->userData = xmlGenericErrorContext; |
| 6616 | return (ret); |
| 6617 | } |
| 6618 | |
| 6619 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6620 | * xmlRelaxNGFreeParserCtxt: |
| 6621 | * @ctxt: the schema parser context |
| 6622 | * |
| 6623 | * Free the resources associated to the schema parser context |
| 6624 | */ |
| 6625 | void |
| 6626 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) { |
| 6627 | if (ctxt == NULL) |
| 6628 | return; |
| 6629 | if (ctxt->URL != NULL) |
| 6630 | xmlFree(ctxt->URL); |
| 6631 | if (ctxt->doc != NULL) |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 6632 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6633 | if (ctxt->interleaves != NULL) |
| 6634 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6635 | if (ctxt->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6636 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6637 | if (ctxt->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6638 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6639 | if (ctxt->docTab != NULL) |
| 6640 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6641 | if (ctxt->incTab != NULL) |
| 6642 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6643 | if (ctxt->defTab != NULL) { |
| 6644 | int i; |
| 6645 | |
| 6646 | for (i = 0;i < ctxt->defNr;i++) |
| 6647 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6648 | xmlFree(ctxt->defTab); |
| 6649 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6650 | xmlFree(ctxt); |
| 6651 | } |
| 6652 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6653 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6654 | * xmlRelaxNGNormExtSpace: |
| 6655 | * @value: a value |
| 6656 | * |
| 6657 | * Removes the leading and ending spaces of the value |
| 6658 | * The string is modified "in situ" |
| 6659 | */ |
| 6660 | static void |
| 6661 | xmlRelaxNGNormExtSpace(xmlChar *value) { |
| 6662 | xmlChar *start = value; |
| 6663 | xmlChar *cur = value; |
| 6664 | if (value == NULL) |
| 6665 | return; |
| 6666 | |
| 6667 | while (IS_BLANK(*cur)) cur++; |
| 6668 | if (cur == start) { |
| 6669 | do { |
| 6670 | while ((*cur != 0) && (!IS_BLANK(*cur))) cur++; |
| 6671 | if (*cur == 0) |
| 6672 | return; |
| 6673 | start = cur; |
| 6674 | while (IS_BLANK(*cur)) cur++; |
| 6675 | if (*cur == 0) { |
| 6676 | *start = 0; |
| 6677 | return; |
| 6678 | } |
| 6679 | } while (1); |
| 6680 | } else { |
| 6681 | do { |
| 6682 | while ((*cur != 0) && (!IS_BLANK(*cur))) |
| 6683 | *start++ = *cur++; |
| 6684 | if (*cur == 0) { |
| 6685 | *start = 0; |
| 6686 | return; |
| 6687 | } |
| 6688 | /* don't try to normalize the inner spaces */ |
| 6689 | while (IS_BLANK(*cur)) cur++; |
| 6690 | *start++ = *cur++; |
| 6691 | if (*cur == 0) { |
| 6692 | *start = 0; |
| 6693 | return; |
| 6694 | } |
| 6695 | } while (1); |
| 6696 | } |
| 6697 | } |
| 6698 | |
| 6699 | /** |
| 6700 | * xmlRelaxNGCheckAttributes: |
| 6701 | * @ctxt: a Relax-NG parser context |
| 6702 | * @node: a Relax-NG node |
| 6703 | * |
| 6704 | * Check all the attributes on the given node |
| 6705 | */ |
| 6706 | static void |
| 6707 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6708 | xmlAttrPtr cur, next; |
| 6709 | |
| 6710 | cur = node->properties; |
| 6711 | while (cur != NULL) { |
| 6712 | next = cur->next; |
| 6713 | if ((cur->ns == NULL) || |
| 6714 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6715 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6716 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6717 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6718 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6719 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6720 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6721 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6722 | if (ctxt->error != NULL) |
| 6723 | ctxt->error(ctxt->userData, |
| 6724 | "Attribute %s is not allowed on %s\n", |
| 6725 | cur->name, node->name); |
| 6726 | ctxt->nbErrors++; |
| 6727 | } |
| 6728 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6729 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6730 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6731 | if (ctxt->error != NULL) |
| 6732 | ctxt->error(ctxt->userData, |
| 6733 | "Attribute %s is not allowed on %s\n", |
| 6734 | cur->name, node->name); |
| 6735 | ctxt->nbErrors++; |
| 6736 | } |
| 6737 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6738 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6739 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6740 | if (ctxt->error != NULL) |
| 6741 | ctxt->error(ctxt->userData, |
| 6742 | "Attribute %s is not allowed on %s\n", |
| 6743 | cur->name, node->name); |
| 6744 | ctxt->nbErrors++; |
| 6745 | } |
| 6746 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6747 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6748 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6749 | if (ctxt->error != NULL) |
| 6750 | ctxt->error(ctxt->userData, |
| 6751 | "Attribute %s is not allowed on %s\n", |
| 6752 | cur->name, node->name); |
| 6753 | ctxt->nbErrors++; |
| 6754 | } |
| 6755 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6756 | xmlChar *val; |
| 6757 | xmlURIPtr uri; |
| 6758 | |
| 6759 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6760 | if (val != NULL) { |
| 6761 | if (val[0] != 0) { |
| 6762 | uri = xmlParseURI((const char *) val); |
| 6763 | if (uri == NULL) { |
| 6764 | if (ctxt->error != NULL) |
| 6765 | ctxt->error(ctxt->userData, |
| 6766 | "Attribute %s contains invalid URI %s\n", |
| 6767 | cur->name, val); |
| 6768 | ctxt->nbErrors++; |
| 6769 | } else { |
| 6770 | if (uri->scheme == NULL) { |
| 6771 | if (ctxt->error != NULL) |
| 6772 | ctxt->error(ctxt->userData, |
| 6773 | "Attribute %s URI %s is not absolute\n", |
| 6774 | cur->name, val); |
| 6775 | ctxt->nbErrors++; |
| 6776 | } |
| 6777 | if (uri->fragment != NULL) { |
| 6778 | if (ctxt->error != NULL) |
| 6779 | ctxt->error(ctxt->userData, |
| 6780 | "Attribute %s URI %s has a fragment ID\n", |
| 6781 | cur->name, val); |
| 6782 | ctxt->nbErrors++; |
| 6783 | } |
| 6784 | xmlFreeURI(uri); |
| 6785 | } |
| 6786 | } |
| 6787 | xmlFree(val); |
| 6788 | } |
| 6789 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6790 | if (ctxt->error != NULL) |
| 6791 | ctxt->error(ctxt->userData, |
| 6792 | "Unknown attribute %s on %s\n", |
| 6793 | cur->name, node->name); |
| 6794 | ctxt->nbErrors++; |
| 6795 | } |
| 6796 | } |
| 6797 | cur = next; |
| 6798 | } |
| 6799 | } |
| 6800 | |
| 6801 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6802 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6803 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6804 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6805 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6806 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6807 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6808 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6809 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6810 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6811 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6812 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6813 | delete = NULL; |
| 6814 | cur = root; |
| 6815 | while (cur != NULL) { |
| 6816 | if (delete != NULL) { |
| 6817 | xmlUnlinkNode(delete); |
| 6818 | xmlFreeNode(delete); |
| 6819 | delete = NULL; |
| 6820 | } |
| 6821 | if (cur->type == XML_ELEMENT_NODE) { |
| 6822 | /* |
| 6823 | * Simplification 4.1. Annotations |
| 6824 | */ |
| 6825 | if ((cur->ns == NULL) || |
| 6826 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6827 | if ((cur->parent != NULL) && |
| 6828 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6829 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6830 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6831 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6832 | if (ctxt->error != NULL) |
| 6833 | ctxt->error(ctxt->userData, |
| 6834 | "element %s doesn't allow foreign elements\n", |
| 6835 | cur->parent->name); |
| 6836 | ctxt->nbErrors++; |
| 6837 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6838 | delete = cur; |
| 6839 | goto skip_children; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6840 | } else { |
| 6841 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6842 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6843 | xmlChar *href, *ns, *base, *URL; |
| 6844 | xmlRelaxNGDocumentPtr docu; |
| 6845 | xmlNodePtr tmp; |
| 6846 | |
| 6847 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6848 | if (ns == NULL) { |
| 6849 | tmp = cur->parent; |
| 6850 | while ((tmp != NULL) && |
| 6851 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6852 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6853 | if (ns != NULL) |
| 6854 | break; |
| 6855 | tmp = tmp->parent; |
| 6856 | } |
| 6857 | } |
| 6858 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6859 | if (href == NULL) { |
| 6860 | if (ctxt->error != NULL) |
| 6861 | ctxt->error(ctxt->userData, |
| 6862 | "xmlRelaxNGParse: externalRef has no href attribute\n"); |
| 6863 | ctxt->nbErrors++; |
| 6864 | delete = cur; |
| 6865 | goto skip_children; |
| 6866 | } |
| 6867 | base = xmlNodeGetBase(cur->doc, cur); |
| 6868 | URL = xmlBuildURI(href, base); |
| 6869 | if (URL == NULL) { |
| 6870 | if (ctxt->error != NULL) |
| 6871 | ctxt->error(ctxt->userData, |
| 6872 | "Failed to compute URL for externalRef %s\n", href); |
| 6873 | ctxt->nbErrors++; |
| 6874 | if (href != NULL) |
| 6875 | xmlFree(href); |
| 6876 | if (base != NULL) |
| 6877 | xmlFree(base); |
| 6878 | delete = cur; |
| 6879 | goto skip_children; |
| 6880 | } |
| 6881 | if (href != NULL) |
| 6882 | xmlFree(href); |
| 6883 | if (base != NULL) |
| 6884 | xmlFree(base); |
| 6885 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6886 | if (docu == NULL) { |
| 6887 | if (ctxt->error != NULL) |
| 6888 | ctxt->error(ctxt->userData, |
| 6889 | "Failed to load externalRef %s\n", URL); |
| 6890 | ctxt->nbErrors++; |
| 6891 | xmlFree(URL); |
| 6892 | delete = cur; |
| 6893 | goto skip_children; |
| 6894 | } |
| 6895 | if (ns != NULL) |
| 6896 | xmlFree(ns); |
| 6897 | xmlFree(URL); |
| 6898 | cur->_private = docu; |
| 6899 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6900 | xmlChar *href, *ns, *base, *URL; |
| 6901 | xmlRelaxNGIncludePtr incl; |
| 6902 | xmlNodePtr tmp; |
| 6903 | |
| 6904 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6905 | if (href == NULL) { |
| 6906 | if (ctxt->error != NULL) |
| 6907 | ctxt->error(ctxt->userData, |
| 6908 | "xmlRelaxNGParse: include has no href attribute\n"); |
| 6909 | ctxt->nbErrors++; |
| 6910 | delete = cur; |
| 6911 | goto skip_children; |
| 6912 | } |
| 6913 | base = xmlNodeGetBase(cur->doc, cur); |
| 6914 | URL = xmlBuildURI(href, base); |
| 6915 | if (URL == NULL) { |
| 6916 | if (ctxt->error != NULL) |
| 6917 | ctxt->error(ctxt->userData, |
| 6918 | "Failed to compute URL for include %s\n", href); |
| 6919 | ctxt->nbErrors++; |
| 6920 | if (href != NULL) |
| 6921 | xmlFree(href); |
| 6922 | if (base != NULL) |
| 6923 | xmlFree(base); |
| 6924 | delete = cur; |
| 6925 | goto skip_children; |
| 6926 | } |
| 6927 | if (href != NULL) |
| 6928 | xmlFree(href); |
| 6929 | if (base != NULL) |
| 6930 | xmlFree(base); |
| 6931 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6932 | if (ns == NULL) { |
| 6933 | tmp = cur->parent; |
| 6934 | while ((tmp != NULL) && |
| 6935 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6936 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6937 | if (ns != NULL) |
| 6938 | break; |
| 6939 | tmp = tmp->parent; |
| 6940 | } |
| 6941 | } |
| 6942 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 6943 | if (ns != NULL) |
| 6944 | xmlFree(ns); |
| 6945 | if (incl == NULL) { |
| 6946 | if (ctxt->error != NULL) |
| 6947 | ctxt->error(ctxt->userData, |
| 6948 | "Failed to load include %s\n", URL); |
| 6949 | ctxt->nbErrors++; |
| 6950 | xmlFree(URL); |
| 6951 | delete = cur; |
| 6952 | goto skip_children; |
| 6953 | } |
| 6954 | xmlFree(URL); |
| 6955 | cur->_private = incl; |
| 6956 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 6957 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) { |
| 6958 | xmlChar *name, *ns; |
| 6959 | xmlNodePtr text = NULL; |
| 6960 | |
| 6961 | /* |
| 6962 | * Simplification 4.8. name attribute of element |
| 6963 | * and attribute elements |
| 6964 | */ |
| 6965 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 6966 | if (name != NULL) { |
| 6967 | if (cur->children == NULL) { |
| 6968 | text = xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 6969 | name); |
| 6970 | } else { |
| 6971 | xmlNodePtr node; |
| 6972 | node = xmlNewNode(cur->ns, BAD_CAST "name"); |
| 6973 | if (node != NULL) { |
| 6974 | xmlAddPrevSibling(cur->children, node); |
| 6975 | text = xmlNewText(name); |
| 6976 | xmlAddChild(node, text); |
| 6977 | text = node; |
| 6978 | } |
| 6979 | } |
| 6980 | if (text == NULL) { |
| 6981 | if (ctxt->error != NULL) |
| 6982 | ctxt->error(ctxt->userData, |
| 6983 | "Failed to create a name %s element\n", name); |
| 6984 | ctxt->nbErrors++; |
| 6985 | } |
| 6986 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 6987 | xmlFree(name); |
| 6988 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6989 | if (ns != NULL) { |
| 6990 | if (text != NULL) { |
| 6991 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 6992 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 6993 | } |
| 6994 | xmlFree(ns); |
| 6995 | } else if (xmlStrEqual(cur->name, |
| 6996 | BAD_CAST "attribute")) { |
| 6997 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 6998 | } |
| 6999 | } |
| 7000 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 7001 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 7002 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 7003 | /* |
| 7004 | * Simplification 4.8. name attribute of element |
| 7005 | * and attribute elements |
| 7006 | */ |
| 7007 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 7008 | xmlNodePtr node; |
| 7009 | xmlChar *ns = NULL; |
| 7010 | |
| 7011 | node = cur->parent; |
| 7012 | while ((node != NULL) && |
| 7013 | (node->type == XML_ELEMENT_NODE)) { |
| 7014 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 7015 | if (ns != NULL) { |
| 7016 | break; |
| 7017 | } |
| 7018 | node = node->parent; |
| 7019 | } |
| 7020 | if (ns == NULL) { |
| 7021 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 7022 | } else { |
| 7023 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 7024 | xmlFree(ns); |
| 7025 | } |
| 7026 | } |
| 7027 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 7028 | xmlChar *name, *local, *prefix; |
| 7029 | |
| 7030 | /* |
| 7031 | * Simplification: 4.10. QNames |
| 7032 | */ |
| 7033 | name = xmlNodeGetContent(cur); |
| 7034 | if (name != NULL) { |
| 7035 | local = xmlSplitQName2(name, &prefix); |
| 7036 | if (local != NULL) { |
| 7037 | xmlNsPtr ns; |
| 7038 | |
| 7039 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 7040 | if (ns == NULL) { |
| 7041 | if (ctxt->error != NULL) |
| 7042 | ctxt->error(ctxt->userData, |
| 7043 | "xmlRelaxNGParse: no namespace for prefix %s\n", prefix); |
| 7044 | ctxt->nbErrors++; |
| 7045 | } else { |
| 7046 | xmlSetProp(cur, BAD_CAST "ns", ns->href); |
| 7047 | xmlNodeSetContent(cur, local); |
| 7048 | } |
| 7049 | xmlFree(local); |
| 7050 | xmlFree(prefix); |
| 7051 | } |
| 7052 | xmlFree(name); |
| 7053 | } |
| 7054 | } |
| 7055 | /* |
| 7056 | * 4.16 |
| 7057 | */ |
| 7058 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 7059 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7060 | if (ctxt->error != NULL) |
| 7061 | ctxt->error(ctxt->userData, |
| 7062 | "Found nsName/except//nsName forbidden construct\n"); |
| 7063 | ctxt->nbErrors++; |
| 7064 | } |
| 7065 | } |
| 7066 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 7067 | (cur != root)) { |
| 7068 | int oldflags = ctxt->flags; |
| 7069 | |
| 7070 | /* |
| 7071 | * 4.16 |
| 7072 | */ |
| 7073 | if ((cur->parent != NULL) && |
| 7074 | (xmlStrEqual(cur->parent->name, BAD_CAST "anyName"))) { |
| 7075 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 7076 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7077 | ctxt->flags = oldflags; |
| 7078 | goto skip_children; |
| 7079 | } else if ((cur->parent != NULL) && |
| 7080 | (xmlStrEqual(cur->parent->name, BAD_CAST "nsName"))) { |
| 7081 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7082 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7083 | ctxt->flags = oldflags; |
| 7084 | goto skip_children; |
| 7085 | } |
| 7086 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7087 | /* |
| 7088 | * 4.16 |
| 7089 | */ |
| 7090 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7091 | if (ctxt->error != NULL) |
| 7092 | ctxt->error(ctxt->userData, |
| 7093 | "Found anyName/except//anyName forbidden construct\n"); |
| 7094 | ctxt->nbErrors++; |
| 7095 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7096 | if (ctxt->error != NULL) |
| 7097 | ctxt->error(ctxt->userData, |
| 7098 | "Found nsName/except//anyName forbidden construct\n"); |
| 7099 | ctxt->nbErrors++; |
| 7100 | } |
| 7101 | } |
| 7102 | /* |
| 7103 | * Thisd is not an else since "include" is transformed |
| 7104 | * into a div |
| 7105 | */ |
| 7106 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7107 | xmlChar *ns; |
| 7108 | xmlNodePtr child, ins, tmp; |
| 7109 | |
| 7110 | /* |
| 7111 | * implements rule 4.11 |
| 7112 | */ |
| 7113 | |
| 7114 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7115 | |
| 7116 | child = cur->children; |
| 7117 | ins = cur; |
| 7118 | while (child != NULL) { |
| 7119 | if (ns != NULL) { |
| 7120 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7121 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7122 | } |
| 7123 | } |
| 7124 | tmp = child->next; |
| 7125 | xmlUnlinkNode(child); |
| 7126 | ins = xmlAddNextSibling(ins, child); |
| 7127 | child = tmp; |
| 7128 | } |
| 7129 | if (ns != NULL) |
| 7130 | xmlFree(ns); |
| 7131 | delete = cur; |
| 7132 | goto skip_children; |
| 7133 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7134 | } |
| 7135 | } |
| 7136 | /* |
| 7137 | * Simplification 4.2 whitespaces |
| 7138 | */ |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7139 | else if ((cur->type == XML_TEXT_NODE) || |
| 7140 | (cur->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7141 | if (IS_BLANK_NODE(cur)) { |
| 7142 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 7143 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) && |
| 7144 | (!xmlStrEqual(cur->parent->name, BAD_CAST "param"))) |
| 7145 | delete = cur; |
| 7146 | } else { |
| 7147 | delete = cur; |
| 7148 | goto skip_children; |
| 7149 | } |
| 7150 | } |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7151 | } else { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7152 | delete = cur; |
| 7153 | goto skip_children; |
| 7154 | } |
| 7155 | |
| 7156 | /* |
| 7157 | * Skip to next node |
| 7158 | */ |
| 7159 | if (cur->children != NULL) { |
| 7160 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7161 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7162 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7163 | cur = cur->children; |
| 7164 | continue; |
| 7165 | } |
| 7166 | } |
| 7167 | skip_children: |
| 7168 | if (cur->next != NULL) { |
| 7169 | cur = cur->next; |
| 7170 | continue; |
| 7171 | } |
| 7172 | |
| 7173 | do { |
| 7174 | cur = cur->parent; |
| 7175 | if (cur == NULL) |
| 7176 | break; |
| 7177 | if (cur == root) { |
| 7178 | cur = NULL; |
| 7179 | break; |
| 7180 | } |
| 7181 | if (cur->next != NULL) { |
| 7182 | cur = cur->next; |
| 7183 | break; |
| 7184 | } |
| 7185 | } while (cur != NULL); |
| 7186 | } |
| 7187 | if (delete != NULL) { |
| 7188 | xmlUnlinkNode(delete); |
| 7189 | xmlFreeNode(delete); |
| 7190 | delete = NULL; |
| 7191 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7192 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7193 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7194 | /** |
| 7195 | * xmlRelaxNGCleanupDoc: |
| 7196 | * @ctxt: a Relax-NG parser context |
| 7197 | * @doc: an xmldocPtr document pointer |
| 7198 | * |
| 7199 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7200 | * Include and externalRef lookups. |
| 7201 | * |
| 7202 | * Returns the cleaned up document or NULL in case of error |
| 7203 | */ |
| 7204 | static xmlDocPtr |
| 7205 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) { |
| 7206 | xmlNodePtr root; |
| 7207 | |
| 7208 | /* |
| 7209 | * Extract the root |
| 7210 | */ |
| 7211 | root = xmlDocGetRootElement(doc); |
| 7212 | if (root == NULL) { |
| 7213 | if (ctxt->error != NULL) |
| 7214 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 7215 | ctxt->URL); |
| 7216 | ctxt->nbErrors++; |
| 7217 | return (NULL); |
| 7218 | } |
| 7219 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7220 | return(doc); |
| 7221 | } |
| 7222 | |
| 7223 | /** |
| 7224 | * xmlRelaxNGParse: |
| 7225 | * @ctxt: a Relax-NG parser context |
| 7226 | * |
| 7227 | * parse a schema definition resource and build an internal |
| 7228 | * XML Shema struture which can be used to validate instances. |
| 7229 | * *WARNING* this interface is highly subject to change |
| 7230 | * |
| 7231 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7232 | * NULL in case of error |
| 7233 | */ |
| 7234 | xmlRelaxNGPtr |
| 7235 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7236 | { |
| 7237 | xmlRelaxNGPtr ret = NULL; |
| 7238 | xmlDocPtr doc; |
| 7239 | xmlNodePtr root; |
| 7240 | |
| 7241 | xmlRelaxNGInitTypes(); |
| 7242 | |
| 7243 | if (ctxt == NULL) |
| 7244 | return (NULL); |
| 7245 | |
| 7246 | /* |
| 7247 | * First step is to parse the input document into an DOM/Infoset |
| 7248 | */ |
| 7249 | if (ctxt->URL != NULL) { |
| 7250 | doc = xmlParseFile((const char *) ctxt->URL); |
| 7251 | if (doc == NULL) { |
| 7252 | if (ctxt->error != NULL) |
| 7253 | ctxt->error(ctxt->userData, |
| 7254 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL); |
| 7255 | ctxt->nbErrors++; |
| 7256 | return (NULL); |
| 7257 | } |
| 7258 | } else if (ctxt->buffer != NULL) { |
| 7259 | doc = xmlParseMemory(ctxt->buffer, ctxt->size); |
| 7260 | if (doc == NULL) { |
| 7261 | if (ctxt->error != NULL) |
| 7262 | ctxt->error(ctxt->userData, |
| 7263 | "xmlRelaxNGParse: could not parse schemas\n"); |
| 7264 | ctxt->nbErrors++; |
| 7265 | return (NULL); |
| 7266 | } |
| 7267 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7268 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7269 | } else if (ctxt->document != NULL) { |
| 7270 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7271 | } else { |
| 7272 | if (ctxt->error != NULL) |
| 7273 | ctxt->error(ctxt->userData, |
| 7274 | "xmlRelaxNGParse: nothing to parse\n"); |
| 7275 | ctxt->nbErrors++; |
| 7276 | return (NULL); |
| 7277 | } |
| 7278 | ctxt->document = doc; |
| 7279 | |
| 7280 | /* |
| 7281 | * Some preprocessing of the document content |
| 7282 | */ |
| 7283 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7284 | if (doc == NULL) { |
| 7285 | xmlFreeDoc(ctxt->document); |
| 7286 | ctxt->document = NULL; |
| 7287 | return(NULL); |
| 7288 | } |
| 7289 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7290 | /* |
| 7291 | * Then do the parsing for good |
| 7292 | */ |
| 7293 | root = xmlDocGetRootElement(doc); |
| 7294 | if (root == NULL) { |
| 7295 | if (ctxt->error != NULL) |
| 7296 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 7297 | ctxt->URL); |
| 7298 | ctxt->nbErrors++; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7299 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7300 | return (NULL); |
| 7301 | } |
| 7302 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7303 | if (ret == NULL) { |
| 7304 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7305 | return(NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7306 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7307 | |
| 7308 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7309 | * Check the ref/defines links |
| 7310 | */ |
| 7311 | /* |
| 7312 | * try to preprocess interleaves |
| 7313 | */ |
| 7314 | if (ctxt->interleaves != NULL) { |
| 7315 | xmlHashScan(ctxt->interleaves, |
| 7316 | (xmlHashScanner)xmlRelaxNGComputeInterleaves, ctxt); |
| 7317 | } |
| 7318 | |
| 7319 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7320 | * if there was a parsing error return NULL |
| 7321 | */ |
| 7322 | if (ctxt->nbErrors > 0) { |
| 7323 | xmlRelaxNGFree(ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7324 | ctxt->document = NULL; |
| 7325 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7326 | return(NULL); |
| 7327 | } |
| 7328 | |
| 7329 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7330 | * try to compile (parts of) the schemas |
| 7331 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7332 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7333 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7334 | xmlRelaxNGDefinePtr def; |
| 7335 | |
| 7336 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7337 | if (def != NULL) { |
| 7338 | def->type = XML_RELAXNG_START; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7339 | def->content = ret->topgrammar->start; |
| 7340 | ret->topgrammar->start = def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7341 | } |
| 7342 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7343 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7344 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7345 | |
| 7346 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7347 | * Transfer the pointer for cleanup at the schema level. |
| 7348 | */ |
| 7349 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7350 | ctxt->document = NULL; |
| 7351 | ret->documents = ctxt->documents; |
| 7352 | ctxt->documents = NULL; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7353 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7354 | ret->includes = ctxt->includes; |
| 7355 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7356 | ret->defNr = ctxt->defNr; |
| 7357 | ret->defTab = ctxt->defTab; |
| 7358 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7359 | if (ctxt->idref == 1) |
| 7360 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7361 | |
| 7362 | return (ret); |
| 7363 | } |
| 7364 | |
| 7365 | /** |
| 7366 | * xmlRelaxNGSetParserErrors: |
| 7367 | * @ctxt: a Relax-NG validation context |
| 7368 | * @err: the error callback |
| 7369 | * @warn: the warning callback |
| 7370 | * @ctx: contextual data for the callbacks |
| 7371 | * |
| 7372 | * Set the callback functions used to handle errors for a validation context |
| 7373 | */ |
| 7374 | void |
| 7375 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
| 7376 | xmlRelaxNGValidityErrorFunc err, |
| 7377 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 7378 | if (ctxt == NULL) |
| 7379 | return; |
| 7380 | ctxt->error = err; |
| 7381 | ctxt->warning = warn; |
| 7382 | ctxt->userData = ctx; |
| 7383 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7384 | |
| 7385 | /** |
| 7386 | * xmlRelaxNGGetParserErrors: |
| 7387 | * @ctxt: a Relax-NG validation context |
| 7388 | * @err: the error callback result |
| 7389 | * @warn: the warning callback result |
| 7390 | * @ctx: contextual data for the callbacks result |
| 7391 | * |
| 7392 | * Get the callback information used to handle errors for a validation context |
| 7393 | * |
| 7394 | * Returns -1 in case of failure, 0 otherwise. |
| 7395 | */ |
| 7396 | int |
| 7397 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
| 7398 | xmlRelaxNGValidityErrorFunc *err, |
| 7399 | xmlRelaxNGValidityWarningFunc *warn, void **ctx) { |
| 7400 | if (ctxt == NULL) |
| 7401 | return(-1); |
| 7402 | if (err != NULL) *err = ctxt->error; |
| 7403 | if (warn != NULL) *warn = ctxt->warning; |
| 7404 | if (ctx != NULL) *ctx = ctxt->userData; |
| 7405 | return(0); |
| 7406 | } |
| 7407 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7408 | /************************************************************************ |
| 7409 | * * |
| 7410 | * Dump back a compiled form * |
| 7411 | * * |
| 7412 | ************************************************************************/ |
| 7413 | static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define); |
| 7414 | |
| 7415 | /** |
| 7416 | * xmlRelaxNGDumpDefines: |
| 7417 | * @output: the file output |
| 7418 | * @defines: a list of define structures |
| 7419 | * |
| 7420 | * Dump a RelaxNG structure back |
| 7421 | */ |
| 7422 | static void |
| 7423 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) { |
| 7424 | while (defines != NULL) { |
| 7425 | xmlRelaxNGDumpDefine(output, defines); |
| 7426 | defines = defines->next; |
| 7427 | } |
| 7428 | } |
| 7429 | |
| 7430 | /** |
| 7431 | * xmlRelaxNGDumpDefine: |
| 7432 | * @output: the file output |
| 7433 | * @define: a define structure |
| 7434 | * |
| 7435 | * Dump a RelaxNG structure back |
| 7436 | */ |
| 7437 | static void |
| 7438 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) { |
| 7439 | if (define == NULL) |
| 7440 | return; |
| 7441 | switch(define->type) { |
| 7442 | case XML_RELAXNG_EMPTY: |
| 7443 | fprintf(output, "<empty/>\n"); |
| 7444 | break; |
| 7445 | case XML_RELAXNG_NOT_ALLOWED: |
| 7446 | fprintf(output, "<notAllowed/>\n"); |
| 7447 | break; |
| 7448 | case XML_RELAXNG_TEXT: |
| 7449 | fprintf(output, "<text/>\n"); |
| 7450 | break; |
| 7451 | case XML_RELAXNG_ELEMENT: |
| 7452 | fprintf(output, "<element>\n"); |
| 7453 | if (define->name != NULL) { |
| 7454 | fprintf(output, "<name"); |
| 7455 | if (define->ns != NULL) |
| 7456 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7457 | fprintf(output, ">%s</name>\n", define->name); |
| 7458 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7459 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7460 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7461 | fprintf(output, "</element>\n"); |
| 7462 | break; |
| 7463 | case XML_RELAXNG_LIST: |
| 7464 | fprintf(output, "<list>\n"); |
| 7465 | xmlRelaxNGDumpDefines(output, define->content); |
| 7466 | fprintf(output, "</list>\n"); |
| 7467 | break; |
| 7468 | case XML_RELAXNG_ONEORMORE: |
| 7469 | fprintf(output, "<oneOrMore>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7470 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7471 | fprintf(output, "</oneOrMore>\n"); |
| 7472 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7473 | case XML_RELAXNG_ZEROORMORE: |
| 7474 | fprintf(output, "<zeroOrMore>\n"); |
| 7475 | xmlRelaxNGDumpDefines(output, define->content); |
| 7476 | fprintf(output, "</zeroOrMore>\n"); |
| 7477 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7478 | case XML_RELAXNG_CHOICE: |
| 7479 | fprintf(output, "<choice>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7480 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7481 | fprintf(output, "</choice>\n"); |
| 7482 | break; |
| 7483 | case XML_RELAXNG_GROUP: |
| 7484 | fprintf(output, "<group>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7485 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7486 | fprintf(output, "</group>\n"); |
| 7487 | break; |
| 7488 | case XML_RELAXNG_INTERLEAVE: |
| 7489 | fprintf(output, "<interleave>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7490 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7491 | fprintf(output, "</interleave>\n"); |
| 7492 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7493 | case XML_RELAXNG_OPTIONAL: |
| 7494 | fprintf(output, "<optional>\n"); |
| 7495 | xmlRelaxNGDumpDefines(output, define->content); |
| 7496 | fprintf(output, "</optional>\n"); |
| 7497 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7498 | case XML_RELAXNG_ATTRIBUTE: |
| 7499 | fprintf(output, "<attribute>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7500 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7501 | fprintf(output, "</attribute>\n"); |
| 7502 | break; |
| 7503 | case XML_RELAXNG_DEF: |
| 7504 | fprintf(output, "<define"); |
| 7505 | if (define->name != NULL) |
| 7506 | fprintf(output, " name=\"%s\"", define->name); |
| 7507 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7508 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7509 | fprintf(output, "</define>\n"); |
| 7510 | break; |
| 7511 | case XML_RELAXNG_REF: |
| 7512 | fprintf(output, "<ref"); |
| 7513 | if (define->name != NULL) |
| 7514 | fprintf(output, " name=\"%s\"", define->name); |
| 7515 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7516 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7517 | fprintf(output, "</ref>\n"); |
| 7518 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7519 | case XML_RELAXNG_PARENTREF: |
| 7520 | fprintf(output, "<parentRef"); |
| 7521 | if (define->name != NULL) |
| 7522 | fprintf(output, " name=\"%s\"", define->name); |
| 7523 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7524 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7525 | fprintf(output, "</parentRef>\n"); |
| 7526 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7527 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7528 | fprintf(output, "<externalRef>"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7529 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7530 | fprintf(output, "</externalRef>\n"); |
| 7531 | break; |
| 7532 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7533 | case XML_RELAXNG_VALUE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7534 | TODO |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7535 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7536 | case XML_RELAXNG_START: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7537 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 7538 | case XML_RELAXNG_PARAM: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7539 | TODO |
| 7540 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 7541 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7542 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 7543 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7544 | } |
| 7545 | } |
| 7546 | |
| 7547 | /** |
| 7548 | * xmlRelaxNGDumpGrammar: |
| 7549 | * @output: the file output |
| 7550 | * @grammar: a grammar structure |
| 7551 | * @top: is this a top grammar |
| 7552 | * |
| 7553 | * Dump a RelaxNG structure back |
| 7554 | */ |
| 7555 | static void |
| 7556 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7557 | { |
| 7558 | if (grammar == NULL) |
| 7559 | return; |
| 7560 | |
| 7561 | fprintf(output, "<grammar"); |
| 7562 | if (top) |
| 7563 | fprintf(output, |
| 7564 | " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7565 | switch(grammar->combine) { |
| 7566 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7567 | break; |
| 7568 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7569 | fprintf(output, " combine=\"choice\""); |
| 7570 | break; |
| 7571 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7572 | fprintf(output, " combine=\"interleave\""); |
| 7573 | break; |
| 7574 | default: |
| 7575 | fprintf(output, " <!-- invalid combine value -->"); |
| 7576 | } |
| 7577 | fprintf(output, ">\n"); |
| 7578 | if (grammar->start == NULL) { |
| 7579 | fprintf(output, " <!-- grammar had no start -->"); |
| 7580 | } else { |
| 7581 | fprintf(output, "<start>\n"); |
| 7582 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7583 | fprintf(output, "</start>\n"); |
| 7584 | } |
| 7585 | /* TODO ? Dump the defines ? */ |
| 7586 | fprintf(output, "</grammar>\n"); |
| 7587 | } |
| 7588 | |
| 7589 | /** |
| 7590 | * xmlRelaxNGDump: |
| 7591 | * @output: the file output |
| 7592 | * @schema: a schema structure |
| 7593 | * |
| 7594 | * Dump a RelaxNG structure back |
| 7595 | */ |
| 7596 | void |
| 7597 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7598 | { |
| 7599 | if (schema == NULL) { |
| 7600 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7601 | return; |
| 7602 | } |
| 7603 | fprintf(output, "RelaxNG: "); |
| 7604 | if (schema->doc == NULL) { |
| 7605 | fprintf(output, "no document\n"); |
| 7606 | } else if (schema->doc->URL != NULL) { |
| 7607 | fprintf(output, "%s\n", schema->doc->URL); |
| 7608 | } else { |
| 7609 | fprintf(output, "\n"); |
| 7610 | } |
| 7611 | if (schema->topgrammar == NULL) { |
| 7612 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7613 | return; |
| 7614 | } |
| 7615 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7616 | } |
| 7617 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7618 | /** |
| 7619 | * xmlRelaxNGDumpTree: |
| 7620 | * @output: the file output |
| 7621 | * @schema: a schema structure |
| 7622 | * |
| 7623 | * Dump the transformed RelaxNG tree. |
| 7624 | */ |
| 7625 | void |
| 7626 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7627 | { |
| 7628 | if (schema == NULL) { |
| 7629 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7630 | return; |
| 7631 | } |
| 7632 | if (schema->doc == NULL) { |
| 7633 | fprintf(output, "no document\n"); |
| 7634 | } else { |
| 7635 | xmlDocDump(output, schema->doc); |
| 7636 | } |
| 7637 | } |
| 7638 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7639 | /************************************************************************ |
| 7640 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7641 | * Validation of compiled content * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7642 | * * |
| 7643 | ************************************************************************/ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7644 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7645 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7646 | |
| 7647 | /** |
| 7648 | * xmlRelaxNGValidateCompiledCallback: |
| 7649 | * @exec: the regular expression instance |
| 7650 | * @token: the token which matched |
| 7651 | * @transdata: callback data, the define for the subelement if available |
| 7652 | @ @inputdata: callback data, the Relax NG validation context |
| 7653 | * |
| 7654 | * Handle the callback and if needed validate the element children. |
| 7655 | */ |
| 7656 | static void |
| 7657 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
| 7658 | const xmlChar *token, |
| 7659 | void *transdata, |
| 7660 | void *inputdata) { |
| 7661 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7662 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7663 | int ret; |
| 7664 | |
| 7665 | #ifdef DEBUG_COMPILE |
| 7666 | xmlGenericError(xmlGenericErrorContext, |
| 7667 | "Compiled callback for: '%s'\n", token); |
| 7668 | #endif |
| 7669 | if (ctxt == NULL) { |
| 7670 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7671 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7672 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7673 | return; |
| 7674 | } |
| 7675 | if (define == NULL) { |
| 7676 | if (token[0] == '#') |
| 7677 | return; |
| 7678 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7679 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7680 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7681 | return; |
| 7682 | } |
| 7683 | if ((ctxt == NULL) || (define == NULL)) { |
| 7684 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7685 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7686 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7687 | return; |
| 7688 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
| 7689 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7690 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7691 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7692 | return; |
| 7693 | } |
| 7694 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7695 | if (ret != 0) |
| 7696 | ctxt->perr = ret; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7697 | } |
| 7698 | |
| 7699 | /** |
| 7700 | * xmlRelaxNGValidateCompiledContent: |
| 7701 | * @ctxt: the RelaxNG validation context |
| 7702 | * @regexp: the regular expression as compiled |
| 7703 | * @content: list of children to test against the regexp |
| 7704 | * |
| 7705 | * Validate the content model of an element or start using the regexp |
| 7706 | * |
| 7707 | * Returns 0 in case of success, -1 in case of error. |
| 7708 | */ |
| 7709 | static int |
| 7710 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 7711 | xmlRegexpPtr regexp, xmlNodePtr content) { |
| 7712 | xmlRegExecCtxtPtr exec; |
| 7713 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7714 | int ret = 0; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7715 | int oldperr = ctxt->perr; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7716 | |
| 7717 | if ((ctxt == NULL) || (regexp == NULL)) |
| 7718 | return(-1); |
| 7719 | exec = xmlRegNewExecCtxt(regexp, |
| 7720 | xmlRelaxNGValidateCompiledCallback, ctxt); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7721 | ctxt->perr = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7722 | cur = content; |
| 7723 | while (cur != NULL) { |
| 7724 | ctxt->state->seq = cur; |
| 7725 | switch (cur->type) { |
| 7726 | case XML_TEXT_NODE: |
| 7727 | case XML_CDATA_SECTION_NODE: |
| 7728 | if (xmlIsBlankNode(cur)) |
| 7729 | break; |
| 7730 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7731 | if (ret < 0) { |
| 7732 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, cur->parent->name); |
| 7733 | } |
| 7734 | break; |
| 7735 | case XML_ELEMENT_NODE: |
| 7736 | if (cur->ns != NULL) { |
| 7737 | ret = xmlRegExecPushString2(exec, cur->name, |
| 7738 | cur->ns->href, ctxt); |
| 7739 | } else { |
| 7740 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 7741 | } |
| 7742 | if (ret < 0) { |
| 7743 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 7744 | } |
| 7745 | break; |
| 7746 | default: |
| 7747 | break; |
| 7748 | } |
| 7749 | if (ret < 0) break; |
| 7750 | /* |
| 7751 | * Switch to next element |
| 7752 | */ |
| 7753 | cur = cur->next; |
| 7754 | } |
| 7755 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 7756 | if (ret == 1) { |
| 7757 | ret = 0; |
| 7758 | ctxt->state->seq = NULL; |
| 7759 | } else if (ret == 0) { |
| 7760 | /* |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7761 | * TODO: get some of the names needed to exit the current state of exec |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7762 | */ |
| 7763 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 7764 | ret = -1; |
| 7765 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7766 | xmlRelaxNGDumpValidError(ctxt); |
| 7767 | } else { |
| 7768 | ret = -1; |
| 7769 | } |
| 7770 | xmlRegFreeExecCtxt(exec); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 7771 | /* |
| 7772 | * There might be content model errors outside of the pure |
| 7773 | * regexp validation, e.g. for attribute values. |
| 7774 | */ |
| 7775 | if ((ret == 0) && (ctxt->perr != 0)) { |
| 7776 | ret = ctxt->perr; |
| 7777 | } |
| 7778 | ctxt->perr = oldperr; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7779 | return(ret); |
| 7780 | } |
| 7781 | |
| 7782 | /************************************************************************ |
| 7783 | * * |
| 7784 | * Progressive validation of when possible * |
| 7785 | * * |
| 7786 | ************************************************************************/ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7787 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7788 | xmlRelaxNGDefinePtr defines); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 7789 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int log); |
| 7790 | static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7791 | |
| 7792 | /** |
| 7793 | * xmlRelaxNGElemPush: |
| 7794 | * @ctxt: the validation context |
| 7795 | * @exec: the regexp runtime for the new content model |
| 7796 | * |
| 7797 | * Push a new regexp for the current node content model on the stack |
| 7798 | * |
| 7799 | * Returns 0 in case of success and -1 in case of error. |
| 7800 | */ |
| 7801 | static int |
| 7802 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) { |
| 7803 | if (ctxt->elemTab == NULL) { |
| 7804 | ctxt->elemMax = 10; |
| 7805 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 7806 | sizeof(xmlRegExecCtxtPtr)); |
| 7807 | if (ctxt->elemTab == NULL) { |
| 7808 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7809 | return(-1); |
| 7810 | } |
| 7811 | } |
| 7812 | if (ctxt->elemNr >= ctxt->elemMax) { |
| 7813 | ctxt->elemMax *= 2; |
| 7814 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
| 7815 | ctxt->elemMax * sizeof(xmlRegExecCtxtPtr)); |
| 7816 | if (ctxt->elemTab == NULL) { |
| 7817 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7818 | return(-1); |
| 7819 | } |
| 7820 | } |
| 7821 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 7822 | ctxt->elem = exec; |
| 7823 | return(0); |
| 7824 | } |
| 7825 | |
| 7826 | /** |
| 7827 | * xmlRelaxNGElemPop: |
| 7828 | * @ctxt: the validation context |
| 7829 | * |
| 7830 | * Pop the regexp of the current node content model from the stack |
| 7831 | * |
| 7832 | * Returns the exec or NULL if empty |
| 7833 | */ |
| 7834 | static xmlRegExecCtxtPtr |
| 7835 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) { |
| 7836 | xmlRegExecCtxtPtr ret; |
| 7837 | |
| 7838 | if (ctxt->elemNr <= 0) return(NULL); |
| 7839 | ctxt->elemNr--; |
| 7840 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 7841 | ctxt->elemTab[ctxt->elemNr] = NULL; |
| 7842 | if (ctxt->elemNr > 0) |
| 7843 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 7844 | else |
| 7845 | ctxt->elem = NULL; |
| 7846 | return(ret); |
| 7847 | } |
| 7848 | |
| 7849 | /** |
| 7850 | * xmlRelaxNGValidateProgressiveCallback: |
| 7851 | * @exec: the regular expression instance |
| 7852 | * @token: the token which matched |
| 7853 | * @transdata: callback data, the define for the subelement if available |
| 7854 | @ @inputdata: callback data, the Relax NG validation context |
| 7855 | * |
| 7856 | * Handle the callback and if needed validate the element children. |
| 7857 | * some of the in/out informations are passed via the context in @inputdata. |
| 7858 | */ |
| 7859 | static void |
| 7860 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
| 7861 | const xmlChar *token, |
| 7862 | void *transdata, |
| 7863 | void *inputdata) { |
| 7864 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7865 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7866 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7867 | xmlNodePtr node = ctxt->pnode; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 7868 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7869 | |
| 7870 | #ifdef DEBUG_PROGRESSIVE |
| 7871 | xmlGenericError(xmlGenericErrorContext, |
| 7872 | "Progressive callback for: '%s'\n", token); |
| 7873 | #endif |
| 7874 | if (ctxt == NULL) { |
| 7875 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7876 | return; |
| 7877 | } |
| 7878 | ctxt->pstate = 1; |
| 7879 | if (define == NULL) { |
| 7880 | if (token[0] == '#') |
| 7881 | return; |
| 7882 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7883 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7884 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7885 | ctxt->pstate = -1; |
| 7886 | return; |
| 7887 | } |
| 7888 | if ((ctxt == NULL) || (define == NULL)) { |
| 7889 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7890 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7891 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7892 | ctxt->pstate = -1; |
| 7893 | return; |
| 7894 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
| 7895 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7896 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7897 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7898 | ctxt->pstate = -1; |
| 7899 | return; |
| 7900 | } |
| 7901 | if (node->type != XML_ELEMENT_NODE) { |
| 7902 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 7903 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7904 | xmlRelaxNGDumpValidError(ctxt); |
| 7905 | ctxt->pstate = -1; |
| 7906 | return; |
| 7907 | } |
| 7908 | if (define->contModel == NULL) { |
| 7909 | /* |
| 7910 | * this node cannot be validated in a streamable fashion |
| 7911 | */ |
| 7912 | #ifdef DEBUG_PROGRESSIVE |
| 7913 | xmlGenericError(xmlGenericErrorContext, |
| 7914 | "Element '%s' validation is not streamable\n", token); |
| 7915 | #endif |
| 7916 | ctxt->pstate = 0; |
| 7917 | ctxt->pdef = define; |
| 7918 | return; |
| 7919 | } |
| 7920 | exec = xmlRegNewExecCtxt(define->contModel, |
| 7921 | xmlRelaxNGValidateProgressiveCallback, |
| 7922 | ctxt); |
| 7923 | if (exec == NULL) { |
| 7924 | ctxt->pstate = -1; |
| 7925 | return; |
| 7926 | } |
| 7927 | xmlRelaxNGElemPush(ctxt, exec); |
| 7928 | |
| 7929 | /* |
| 7930 | * Validate the attributes part of the content. |
| 7931 | */ |
| 7932 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 7933 | if (state == NULL) { |
| 7934 | ctxt->pstate = -1; |
| 7935 | return; |
| 7936 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7937 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7938 | ctxt->state = state; |
| 7939 | if (define->attrs != NULL) { |
| 7940 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 7941 | if (ret != 0) { |
| 7942 | ctxt->pstate = -1; |
| 7943 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 7944 | } |
| 7945 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7946 | if (ctxt->state != NULL) { |
| 7947 | ctxt->state->seq = NULL; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 7948 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7949 | if (ret != 0) { |
| 7950 | ctxt->pstate = -1; |
| 7951 | } |
| 7952 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 7953 | } else if (ctxt->states != NULL) { |
| 7954 | int tmp = -1, i; |
| 7955 | |
| 7956 | oldflags = ctxt->flags; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7957 | |
| 7958 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 7959 | state = ctxt->states->tabState[i]; |
| 7960 | ctxt->state = state; |
| 7961 | ctxt->state->seq = NULL; |
| 7962 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 7963 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7964 | tmp = 0; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 7965 | break; |
| 7966 | } |
| 7967 | } |
| 7968 | if (tmp != 0) { |
| 7969 | /* |
| 7970 | * validation error, log the message for the "best" one |
| 7971 | */ |
| 7972 | ctxt->flags |= FLAGS_IGNORABLE; |
| 7973 | xmlRelaxNGLogBestError(ctxt); |
| 7974 | } |
| 7975 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 7976 | xmlRelaxNGFreeValidState(ctxt, |
| 7977 | ctxt->states->tabState[i]); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7978 | } |
| 7979 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 7980 | ctxt->states = NULL; |
| 7981 | if ((ret == 0) && (tmp == -1)) |
| 7982 | ctxt->pstate = -1; |
| 7983 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7984 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7985 | if (ctxt->pstate == -1) { |
| 7986 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 7987 | xmlRelaxNGDumpValidError(ctxt); |
| 7988 | } |
| 7989 | } |
| 7990 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7991 | } |
| 7992 | |
| 7993 | /** |
| 7994 | * xmlRelaxNGValidatePushElement: |
| 7995 | * @ctxt: the validation context |
| 7996 | * @doc: a document instance |
| 7997 | * @elem: an element instance |
| 7998 | * |
| 7999 | * Push a new element start on the RelaxNG validation stack. |
| 8000 | * |
| 8001 | * returns 1 if no validation problem was found or 0 if validating the |
| 8002 | * element requires a full node, and -1 in case of error. |
| 8003 | */ |
| 8004 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8005 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8006 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8007 | xmlNodePtr elem) |
| 8008 | { |
| 8009 | int ret = 1; |
| 8010 | |
| 8011 | if ((ctxt == NULL) || (elem == NULL)) |
| 8012 | return (-1); |
| 8013 | |
| 8014 | #ifdef DEBUG_PROGRESSIVE |
| 8015 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 8016 | #endif |
| 8017 | if (ctxt->elem == 0) { |
| 8018 | xmlRelaxNGPtr schema; |
| 8019 | xmlRelaxNGGrammarPtr grammar; |
| 8020 | xmlRegExecCtxtPtr exec; |
| 8021 | xmlRelaxNGDefinePtr define; |
| 8022 | |
| 8023 | schema = ctxt->schema; |
| 8024 | if (schema == NULL) { |
| 8025 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8026 | return (-1); |
| 8027 | } |
| 8028 | grammar = schema->topgrammar; |
| 8029 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 8030 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 8031 | return (-1); |
| 8032 | } |
| 8033 | define = grammar->start; |
| 8034 | if (define->contModel == NULL) { |
| 8035 | ctxt->pdef = define; |
| 8036 | return (0); |
| 8037 | } |
| 8038 | exec = xmlRegNewExecCtxt(define->contModel, |
| 8039 | xmlRelaxNGValidateProgressiveCallback, |
| 8040 | ctxt); |
| 8041 | if (exec == NULL) { |
| 8042 | return (-1); |
| 8043 | } |
| 8044 | xmlRelaxNGElemPush(ctxt, exec); |
| 8045 | } |
| 8046 | ctxt->pnode = elem; |
| 8047 | ctxt->pstate = 0; |
| 8048 | if (elem->ns != NULL) { |
| 8049 | ret = |
| 8050 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 8051 | ctxt); |
| 8052 | } else { |
| 8053 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 8054 | } |
| 8055 | if (ret < 0) { |
| 8056 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 8057 | } else { |
| 8058 | if (ctxt->pstate == 0) |
| 8059 | ret = 0; |
| 8060 | else if (ctxt->pstate < 0) |
| 8061 | ret = -1; |
| 8062 | else |
| 8063 | ret = 1; |
| 8064 | } |
| 8065 | #ifdef DEBUG_PROGRESSIVE |
| 8066 | if (ret < 0) |
| 8067 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 8068 | elem->name); |
| 8069 | #endif |
| 8070 | return (ret); |
| 8071 | } |
| 8072 | |
| 8073 | /** |
| 8074 | * xmlRelaxNGValidatePushCData: |
| 8075 | * @ctxt: the RelaxNG validation context |
| 8076 | * @data: some character data read |
| 8077 | * @len: the lenght of the data |
| 8078 | * |
| 8079 | * check the CData parsed for validation in the current stack |
| 8080 | * |
| 8081 | * returns 1 if no validation problem was found or -1 otherwise |
| 8082 | */ |
| 8083 | int |
| 8084 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8085 | const xmlChar * data, |
| 8086 | int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8087 | { |
| 8088 | int ret = 1; |
| 8089 | |
| 8090 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 8091 | return (-1); |
| 8092 | |
| 8093 | #ifdef DEBUG_PROGRESSIVE |
| 8094 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 8095 | #endif |
| 8096 | |
| 8097 | while (*data != 0) { |
| 8098 | if (!IS_BLANK(*data)) |
| 8099 | break; |
| 8100 | data++; |
| 8101 | } |
| 8102 | if (*data == 0) |
| 8103 | return(1); |
| 8104 | |
| 8105 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8106 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8107 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8108 | #ifdef DEBUG_PROGRESSIVE |
| 8109 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
| 8110 | #endif |
| 8111 | |
| 8112 | return(-1); |
| 8113 | } |
| 8114 | return(1); |
| 8115 | } |
| 8116 | |
| 8117 | /** |
| 8118 | * xmlRelaxNGValidatePopElement: |
| 8119 | * @ctxt: the RelaxNG validation context |
| 8120 | * @doc: a document instance |
| 8121 | * @elem: an element instance |
| 8122 | * |
| 8123 | * Pop the element end from the RelaxNG validation stack. |
| 8124 | * |
| 8125 | * returns 1 if no validation problem was found or 0 otherwise |
| 8126 | */ |
| 8127 | int |
| 8128 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8129 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
| 8130 | xmlNodePtr elem) { |
| 8131 | int ret; |
| 8132 | xmlRegExecCtxtPtr exec; |
| 8133 | |
| 8134 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) return(-1); |
| 8135 | #ifdef DEBUG_PROGRESSIVE |
| 8136 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8137 | #endif |
| 8138 | /* |
| 8139 | * verify that we reached a terminal state of the content model. |
| 8140 | */ |
| 8141 | exec = xmlRelaxNGElemPop(ctxt); |
| 8142 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8143 | if (ret == 0) { |
| 8144 | /* |
| 8145 | * TODO: get some of the names needed to exit the current state of exec |
| 8146 | */ |
| 8147 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8148 | ret = -1; |
| 8149 | } else if (ret < 0) { |
| 8150 | ret = -1; |
| 8151 | } else { |
| 8152 | ret = 1; |
| 8153 | } |
| 8154 | xmlRegFreeExecCtxt(exec); |
| 8155 | #ifdef DEBUG_PROGRESSIVE |
| 8156 | if (ret < 0) |
| 8157 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8158 | elem->name); |
| 8159 | #endif |
| 8160 | return(ret); |
| 8161 | } |
| 8162 | |
| 8163 | /** |
| 8164 | * xmlRelaxNGValidateFullElement: |
| 8165 | * @ctxt: the validation context |
| 8166 | * @doc: a document instance |
| 8167 | * @elem: an element instance |
| 8168 | * |
| 8169 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8170 | * 0 and the content of the node has been expanded. |
| 8171 | * |
| 8172 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8173 | */ |
| 8174 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8175 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8176 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8177 | xmlNodePtr elem) { |
| 8178 | int ret; |
| 8179 | xmlRelaxNGValidStatePtr state; |
| 8180 | |
| 8181 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) return(-1); |
| 8182 | #ifdef DEBUG_PROGRESSIVE |
| 8183 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8184 | #endif |
| 8185 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8186 | if (state == NULL) { |
| 8187 | return(-1); |
| 8188 | } |
| 8189 | state->seq = elem; |
| 8190 | ctxt->state = state; |
| 8191 | ctxt->errNo = XML_RELAXNG_OK; |
| 8192 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
| 8193 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) ret = -1; |
| 8194 | else ret = 1; |
| 8195 | xmlRelaxNGFreeValidState(ctxt, state); |
| 8196 | ctxt->state = NULL; |
| 8197 | #ifdef DEBUG_PROGRESSIVE |
| 8198 | if (ret < 0) |
| 8199 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8200 | elem->name); |
| 8201 | #endif |
| 8202 | return(ret); |
| 8203 | } |
| 8204 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8205 | /************************************************************************ |
| 8206 | * * |
| 8207 | * Generic interpreted validation implementation * |
| 8208 | * * |
| 8209 | ************************************************************************/ |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8210 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8211 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8212 | |
| 8213 | /** |
| 8214 | * xmlRelaxNGSkipIgnored: |
| 8215 | * @ctxt: a schema validation context |
| 8216 | * @node: the top node. |
| 8217 | * |
| 8218 | * Skip ignorable nodes in that context |
| 8219 | * |
| 8220 | * Returns the new sibling or NULL in case of error. |
| 8221 | */ |
| 8222 | static xmlNodePtr |
| 8223 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 8224 | xmlNodePtr node) { |
| 8225 | /* |
| 8226 | * TODO complete and handle entities |
| 8227 | */ |
| 8228 | while ((node != NULL) && |
| 8229 | ((node->type == XML_COMMENT_NODE) || |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8230 | (node->type == XML_PI_NODE) || |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 8231 | (((node->type == XML_TEXT_NODE) || |
| 8232 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8233 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8234 | (IS_BLANK_NODE(node)))))) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8235 | node = node->next; |
| 8236 | } |
| 8237 | return(node); |
| 8238 | } |
| 8239 | |
| 8240 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8241 | * xmlRelaxNGNormalize: |
| 8242 | * @ctxt: a schema validation context |
| 8243 | * @str: the string to normalize |
| 8244 | * |
| 8245 | * Implements the normalizeWhiteSpace( s ) function from |
| 8246 | * section 6.2.9 of the spec |
| 8247 | * |
| 8248 | * Returns the new string or NULL in case of error. |
| 8249 | */ |
| 8250 | static xmlChar * |
| 8251 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) { |
| 8252 | xmlChar *ret, *p; |
| 8253 | const xmlChar *tmp; |
| 8254 | int len; |
| 8255 | |
| 8256 | if (str == NULL) |
| 8257 | return(NULL); |
| 8258 | tmp = str; |
| 8259 | while (*tmp != 0) tmp++; |
| 8260 | len = tmp - str; |
| 8261 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8262 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8263 | if (ret == NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8264 | if (ctxt != NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8265 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8266 | } else { |
| 8267 | xmlGenericError(xmlGenericErrorContext, |
| 8268 | "xmlRelaxNGNormalize: out of memory\n"); |
| 8269 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8270 | return(NULL); |
| 8271 | } |
| 8272 | p = ret; |
| 8273 | while (IS_BLANK(*str)) str++; |
| 8274 | while (*str != 0) { |
| 8275 | if (IS_BLANK(*str)) { |
| 8276 | while (IS_BLANK(*str)) str++; |
| 8277 | if (*str == 0) |
| 8278 | break; |
| 8279 | *p++ = ' '; |
| 8280 | } else |
| 8281 | *p++ = *str++; |
| 8282 | } |
| 8283 | *p = 0; |
| 8284 | return(ret); |
| 8285 | } |
| 8286 | |
| 8287 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8288 | * xmlRelaxNGValidateDatatype: |
| 8289 | * @ctxt: a Relax-NG validation context |
| 8290 | * @value: the string value |
| 8291 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8292 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8293 | * |
| 8294 | * Validate the given value against the dataype |
| 8295 | * |
| 8296 | * Returns 0 if the validation succeeded or an error code. |
| 8297 | */ |
| 8298 | static int |
| 8299 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8300 | xmlRelaxNGDefinePtr define, xmlNodePtr node) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8301 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8302 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8303 | void *result = NULL; |
| 8304 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8305 | |
| 8306 | if ((define == NULL) || (define->data == NULL)) { |
| 8307 | return(-1); |
| 8308 | } |
| 8309 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8310 | if (lib->check != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8311 | if ((define->attrs != NULL) && |
| 8312 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8313 | ret = lib->check(lib->data, define->name, value, &result, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8314 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8315 | ret = lib->check(lib->data, define->name, value, NULL, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8316 | } |
| 8317 | } else |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8318 | ret = -1; |
| 8319 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8320 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8321 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8322 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8323 | return(-1); |
| 8324 | } else if (ret == 1) { |
| 8325 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8326 | } else if (ret == 2) { |
| 8327 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8328 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8329 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8330 | ret = -1; |
| 8331 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8332 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8333 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
| 8334 | if (lib->facet != NULL) { |
| 8335 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8336 | cur->value, value, result); |
| 8337 | if (tmp != 0) |
| 8338 | ret = -1; |
| 8339 | } |
| 8340 | cur = cur->next; |
| 8341 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8342 | if ((ret == 0) && (define->content != NULL)) { |
| 8343 | const xmlChar *oldvalue, *oldendvalue; |
| 8344 | |
| 8345 | oldvalue = ctxt->state->value; |
| 8346 | oldendvalue = ctxt->state->endvalue; |
| 8347 | ctxt->state->value = (xmlChar *) value; |
| 8348 | ctxt->state->endvalue = NULL; |
| 8349 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8350 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8351 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
| 8352 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8353 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8354 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8355 | return(ret); |
| 8356 | } |
| 8357 | |
| 8358 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8359 | * xmlRelaxNGNextValue: |
| 8360 | * @ctxt: a Relax-NG validation context |
| 8361 | * |
| 8362 | * Skip to the next value when validating within a list |
| 8363 | * |
| 8364 | * Returns 0 if the operation succeeded or an error code. |
| 8365 | */ |
| 8366 | static int |
| 8367 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) { |
| 8368 | xmlChar *cur; |
| 8369 | |
| 8370 | cur = ctxt->state->value; |
| 8371 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
| 8372 | ctxt->state->value = NULL; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 8373 | ctxt->state->endvalue = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8374 | return(0); |
| 8375 | } |
| 8376 | while (*cur != 0) cur++; |
| 8377 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++; |
| 8378 | if (cur == ctxt->state->endvalue) |
| 8379 | ctxt->state->value = NULL; |
| 8380 | else |
| 8381 | ctxt->state->value = cur; |
| 8382 | return(0); |
| 8383 | } |
| 8384 | |
| 8385 | /** |
| 8386 | * xmlRelaxNGValidateValueList: |
| 8387 | * @ctxt: a Relax-NG validation context |
| 8388 | * @defines: the list of definitions to verify |
| 8389 | * |
| 8390 | * Validate the given set of definitions for the current value |
| 8391 | * |
| 8392 | * Returns 0 if the validation succeeded or an error code. |
| 8393 | */ |
| 8394 | static int |
| 8395 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8396 | xmlRelaxNGDefinePtr defines) { |
| 8397 | int ret = 0; |
| 8398 | |
| 8399 | while (defines != NULL) { |
| 8400 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8401 | if (ret != 0) |
| 8402 | break; |
| 8403 | defines = defines->next; |
| 8404 | } |
| 8405 | return(ret); |
| 8406 | } |
| 8407 | |
| 8408 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8409 | * xmlRelaxNGValidateValue: |
| 8410 | * @ctxt: a Relax-NG validation context |
| 8411 | * @define: the definition to verify |
| 8412 | * |
| 8413 | * Validate the given definition for the current value |
| 8414 | * |
| 8415 | * Returns 0 if the validation succeeded or an error code. |
| 8416 | */ |
| 8417 | static int |
| 8418 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8419 | xmlRelaxNGDefinePtr define) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8420 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8421 | xmlChar *value; |
| 8422 | |
| 8423 | value = ctxt->state->value; |
| 8424 | switch (define->type) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8425 | case XML_RELAXNG_EMPTY: { |
| 8426 | if ((value != NULL) && (value[0] != 0)) { |
| 8427 | int idx = 0; |
| 8428 | |
| 8429 | while (IS_BLANK(value[idx])) |
| 8430 | idx++; |
| 8431 | if (value[idx] != 0) |
| 8432 | ret = -1; |
| 8433 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8434 | break; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8435 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8436 | case XML_RELAXNG_TEXT: |
| 8437 | break; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8438 | case XML_RELAXNG_VALUE: { |
| 8439 | if (!xmlStrEqual(value, define->value)) { |
| 8440 | if (define->name != NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8441 | xmlRelaxNGTypeLibraryPtr lib; |
| 8442 | |
| 8443 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 8444 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8445 | ret = lib->comp(lib->data, define->name, |
| 8446 | define->value, define->node, |
| 8447 | (void *) define->attrs, |
| 8448 | value, ctxt->state->node); |
| 8449 | } else |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8450 | ret = -1; |
| 8451 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8452 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, define->name); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8453 | return(-1); |
| 8454 | } else if (ret == 1) { |
| 8455 | ret = 0; |
| 8456 | } else { |
| 8457 | ret = -1; |
| 8458 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8459 | } else { |
| 8460 | xmlChar *nval, *nvalue; |
| 8461 | |
| 8462 | /* |
| 8463 | * TODO: trivial optimizations are possible by |
| 8464 | * computing at compile-time |
| 8465 | */ |
| 8466 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8467 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
| 8468 | |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8469 | if ((nval == NULL) || (nvalue == NULL) || |
| 8470 | (!xmlStrEqual(nval, nvalue))) |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8471 | ret = -1; |
| 8472 | if (nval != NULL) |
| 8473 | xmlFree(nval); |
| 8474 | if (nvalue != NULL) |
| 8475 | xmlFree(nvalue); |
| 8476 | } |
| 8477 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8478 | if (ret == 0) |
| 8479 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8480 | break; |
| 8481 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8482 | case XML_RELAXNG_DATATYPE: { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8483 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8484 | ctxt->state->seq); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8485 | if (ret == 0) |
| 8486 | xmlRelaxNGNextValue(ctxt); |
| 8487 | |
| 8488 | break; |
| 8489 | } |
| 8490 | case XML_RELAXNG_CHOICE: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8491 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8492 | xmlChar *oldvalue; |
| 8493 | |
| 8494 | oldflags = ctxt->flags; |
| 8495 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8496 | |
| 8497 | oldvalue = ctxt->state->value; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8498 | while (list != NULL) { |
| 8499 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8500 | if (ret == 0) { |
| 8501 | break; |
| 8502 | } |
| 8503 | ctxt->state->value = oldvalue; |
| 8504 | list = list->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8505 | } |
| 8506 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8507 | if (ret != 0) { |
| 8508 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8509 | xmlRelaxNGDumpValidError(ctxt); |
| 8510 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8511 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8512 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8513 | if (ret == 0) |
| 8514 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8515 | break; |
| 8516 | } |
| 8517 | case XML_RELAXNG_LIST: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8518 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8519 | xmlChar *oldvalue, *oldend, *val, *cur; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8520 | #ifdef DEBUG_LIST |
| 8521 | int nb_values = 0; |
| 8522 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8523 | |
| 8524 | oldvalue = ctxt->state->value; |
| 8525 | oldend = ctxt->state->endvalue; |
| 8526 | |
| 8527 | val = xmlStrdup(oldvalue); |
| 8528 | if (val == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8529 | val = xmlStrdup(BAD_CAST ""); |
| 8530 | } |
| 8531 | if (val == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8532 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8533 | return(-1); |
| 8534 | } |
| 8535 | cur = val; |
| 8536 | while (*cur != 0) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8537 | if (IS_BLANK(*cur)) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8538 | *cur = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8539 | cur++; |
| 8540 | #ifdef DEBUG_LIST |
| 8541 | nb_values++; |
| 8542 | #endif |
| 8543 | while (IS_BLANK(*cur)) |
| 8544 | *cur++ = 0; |
| 8545 | } else |
| 8546 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8547 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8548 | #ifdef DEBUG_LIST |
| 8549 | xmlGenericError(xmlGenericErrorContext, |
| 8550 | "list value: '%s' found %d items\n", oldvalue, nb_values); |
| 8551 | nb_values = 0; |
| 8552 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8553 | ctxt->state->endvalue = cur; |
| 8554 | cur = val; |
| 8555 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8556 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8557 | ctxt->state->value = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8558 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8559 | while (list != NULL) { |
| 8560 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8561 | ctxt->state->value = NULL; |
| 8562 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8563 | if (ret != 0) { |
| 8564 | #ifdef DEBUG_LIST |
| 8565 | xmlGenericError(xmlGenericErrorContext, |
| 8566 | "Failed to validate value: '%s' with %d rule\n", |
| 8567 | ctxt->state->value, nb_values); |
| 8568 | #endif |
| 8569 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8570 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8571 | #ifdef DEBUG_LIST |
| 8572 | nb_values++; |
| 8573 | #endif |
| 8574 | list = list->next; |
| 8575 | } |
| 8576 | |
| 8577 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8578 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8579 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, ctxt->state->value); |
| 8580 | ret = -1; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8581 | } |
| 8582 | xmlFree(val); |
| 8583 | ctxt->state->value = oldvalue; |
| 8584 | ctxt->state->endvalue = oldend; |
| 8585 | break; |
| 8586 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8587 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8588 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8589 | if (ret != 0) { |
| 8590 | break; |
| 8591 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8592 | /* no break on purpose */ |
| 8593 | case XML_RELAXNG_ZEROORMORE: { |
| 8594 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8595 | |
| 8596 | oldflags = ctxt->flags; |
| 8597 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8598 | cur = ctxt->state->value; |
| 8599 | temp = NULL; |
| 8600 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8601 | (temp != cur)) { |
| 8602 | temp = cur; |
| 8603 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8604 | if (ret != 0) { |
| 8605 | ctxt->state->value = temp; |
| 8606 | ret = 0; |
| 8607 | break; |
| 8608 | } |
| 8609 | cur = ctxt->state->value; |
| 8610 | } |
| 8611 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8612 | if (ret != 0) { |
| 8613 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8614 | xmlRelaxNGDumpValidError(ctxt); |
| 8615 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8616 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8617 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8618 | break; |
| 8619 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8620 | case XML_RELAXNG_EXCEPT: { |
| 8621 | xmlRelaxNGDefinePtr list; |
| 8622 | |
| 8623 | list = define->content; |
| 8624 | while (list != NULL) { |
| 8625 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8626 | if (ret == 0) { |
| 8627 | ret = -1; |
| 8628 | break; |
| 8629 | } else |
| 8630 | ret = 0; |
| 8631 | list = list->next; |
| 8632 | } |
| 8633 | break; |
| 8634 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8635 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8636 | case XML_RELAXNG_GROUP: { |
| 8637 | xmlRelaxNGDefinePtr list; |
| 8638 | |
| 8639 | list = define->content; |
| 8640 | while (list != NULL) { |
| 8641 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8642 | if (ret != 0) { |
| 8643 | ret = -1; |
| 8644 | break; |
| 8645 | } else |
| 8646 | ret = 0; |
| 8647 | list = list->next; |
| 8648 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8649 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8650 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8651 | case XML_RELAXNG_REF: |
| 8652 | case XML_RELAXNG_PARENTREF: |
| 8653 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8654 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8655 | default: |
| 8656 | TODO |
| 8657 | ret = -1; |
| 8658 | } |
| 8659 | return(ret); |
| 8660 | } |
| 8661 | |
| 8662 | /** |
| 8663 | * xmlRelaxNGValidateValueContent: |
| 8664 | * @ctxt: a Relax-NG validation context |
| 8665 | * @defines: the list of definitions to verify |
| 8666 | * |
| 8667 | * Validate the given definitions for the current value |
| 8668 | * |
| 8669 | * Returns 0 if the validation succeeded or an error code. |
| 8670 | */ |
| 8671 | static int |
| 8672 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8673 | xmlRelaxNGDefinePtr defines) { |
| 8674 | int ret = 0; |
| 8675 | |
| 8676 | while (defines != NULL) { |
| 8677 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8678 | if (ret != 0) |
| 8679 | break; |
| 8680 | defines = defines->next; |
| 8681 | } |
| 8682 | return(ret); |
| 8683 | } |
| 8684 | |
| 8685 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8686 | * xmlRelaxNGAttributeMatch: |
| 8687 | * @ctxt: a Relax-NG validation context |
| 8688 | * @define: the definition to check |
| 8689 | * @prop: the attribute |
| 8690 | * |
| 8691 | * Check if the attribute matches the definition nameClass |
| 8692 | * |
| 8693 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 8694 | */ |
| 8695 | static int |
| 8696 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8697 | xmlRelaxNGDefinePtr define, |
| 8698 | xmlAttrPtr prop) { |
| 8699 | int ret; |
| 8700 | |
| 8701 | if (define->name != NULL) { |
| 8702 | if (!xmlStrEqual(define->name, prop->name)) |
| 8703 | return(0); |
| 8704 | } |
| 8705 | if (define->ns != NULL) { |
| 8706 | if (define->ns[0] == 0) { |
| 8707 | if (prop->ns != NULL) |
| 8708 | return(0); |
| 8709 | } else { |
| 8710 | if ((prop->ns == NULL) || |
| 8711 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 8712 | return(0); |
| 8713 | } |
| 8714 | } |
| 8715 | if (define->nameClass == NULL) |
| 8716 | return(1); |
| 8717 | define = define->nameClass; |
| 8718 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 8719 | xmlRelaxNGDefinePtr list; |
| 8720 | |
| 8721 | list = define->content; |
| 8722 | while (list != NULL) { |
| 8723 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 8724 | if (ret == 1) |
| 8725 | return(0); |
| 8726 | if (ret < 0) |
| 8727 | return(ret); |
| 8728 | list = list->next; |
| 8729 | } |
| 8730 | } else { |
| 8731 | TODO |
| 8732 | } |
| 8733 | return(1); |
| 8734 | } |
| 8735 | |
| 8736 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8737 | * xmlRelaxNGValidateAttribute: |
| 8738 | * @ctxt: a Relax-NG validation context |
| 8739 | * @define: the definition to verify |
| 8740 | * |
| 8741 | * Validate the given attribute definition for that node |
| 8742 | * |
| 8743 | * Returns 0 if the validation succeeded or an error code. |
| 8744 | */ |
| 8745 | static int |
| 8746 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 8747 | xmlRelaxNGDefinePtr define) { |
| 8748 | int ret = 0, i; |
| 8749 | xmlChar *value, *oldvalue; |
| 8750 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8751 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8752 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8753 | if (ctxt->state->nbAttrLeft <= 0) |
| 8754 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8755 | if (define->name != NULL) { |
| 8756 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 8757 | tmp = ctxt->state->attrs[i]; |
| 8758 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 8759 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 8760 | (tmp->ns == NULL)) || |
| 8761 | ((tmp->ns != NULL) && |
| 8762 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 8763 | prop = tmp; |
| 8764 | break; |
| 8765 | } |
| 8766 | } |
| 8767 | } |
| 8768 | if (prop != NULL) { |
| 8769 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8770 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8771 | oldseq = ctxt->state->seq; |
| 8772 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8773 | ctxt->state->value = value; |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8774 | ctxt->state->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8775 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8776 | if (ctxt->state->value != NULL) |
| 8777 | value = ctxt->state->value; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8778 | if (value != NULL) |
| 8779 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8780 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8781 | ctxt->state->seq = oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8782 | if (ret == 0) { |
| 8783 | /* |
| 8784 | * flag the attribute as processed |
| 8785 | */ |
| 8786 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8787 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8788 | } |
| 8789 | } else { |
| 8790 | ret = -1; |
| 8791 | } |
| 8792 | #ifdef DEBUG |
| 8793 | xmlGenericError(xmlGenericErrorContext, |
| 8794 | "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret); |
| 8795 | #endif |
| 8796 | } else { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8797 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 8798 | tmp = ctxt->state->attrs[i]; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 8799 | if ((tmp != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8800 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8801 | prop = tmp; |
| 8802 | break; |
| 8803 | } |
| 8804 | } |
| 8805 | if (prop != NULL) { |
| 8806 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8807 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8808 | oldseq = ctxt->state->seq; |
| 8809 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8810 | ctxt->state->value = value; |
| 8811 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8812 | if (ctxt->state->value != NULL) |
| 8813 | value = ctxt->state->value; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8814 | if (value != NULL) |
| 8815 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8816 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8817 | ctxt->state->seq = oldseq; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8818 | if (ret == 0) { |
| 8819 | /* |
| 8820 | * flag the attribute as processed |
| 8821 | */ |
| 8822 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8823 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8824 | } |
| 8825 | } else { |
| 8826 | ret = -1; |
| 8827 | } |
| 8828 | #ifdef DEBUG |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 8829 | if (define->ns != NULL) { |
| 8830 | xmlGenericError(xmlGenericErrorContext, |
| 8831 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 8832 | define->ns, ret); |
| 8833 | } else { |
| 8834 | xmlGenericError(xmlGenericErrorContext, |
| 8835 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 8836 | ret); |
| 8837 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8838 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8839 | } |
| 8840 | |
| 8841 | return(ret); |
| 8842 | } |
| 8843 | |
| 8844 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8845 | * xmlRelaxNGValidateAttributeList: |
| 8846 | * @ctxt: a Relax-NG validation context |
| 8847 | * @define: the list of definition to verify |
| 8848 | * |
| 8849 | * Validate the given node against the list of attribute definitions |
| 8850 | * |
| 8851 | * Returns 0 if the validation succeeded or an error code. |
| 8852 | */ |
| 8853 | static int |
| 8854 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8855 | xmlRelaxNGDefinePtr defines) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8856 | int ret = 0, res; |
| 8857 | int needmore = 0; |
| 8858 | xmlRelaxNGDefinePtr cur; |
| 8859 | |
| 8860 | cur = defines; |
| 8861 | while (cur != NULL) { |
| 8862 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 8863 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 8864 | ret = -1; |
| 8865 | } else |
| 8866 | needmore = 1; |
| 8867 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8868 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8869 | if (!needmore) |
| 8870 | return(ret); |
| 8871 | cur = defines; |
| 8872 | while (cur != NULL) { |
| 8873 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
| 8874 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 8875 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 8876 | if (res < 0) |
| 8877 | ret = -1; |
| 8878 | } else { |
| 8879 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8880 | return(-1); |
| 8881 | } |
| 8882 | if (res == -1) /* continues on -2 */ |
| 8883 | break; |
| 8884 | } |
| 8885 | cur = cur->next; |
| 8886 | } |
| 8887 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8888 | return(ret); |
| 8889 | } |
| 8890 | |
| 8891 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8892 | * xmlRelaxNGNodeMatchesList: |
| 8893 | * @node: the node |
| 8894 | * @list: a NULL terminated array of definitions |
| 8895 | * |
| 8896 | * Check if a node can be matched by one of the definitions |
| 8897 | * |
| 8898 | * Returns 1 if matches 0 otherwise |
| 8899 | */ |
| 8900 | static int |
| 8901 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) { |
| 8902 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8903 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8904 | |
| 8905 | if ((node == NULL) || (list == NULL)) |
| 8906 | return(0); |
| 8907 | |
| 8908 | cur = list[i++]; |
| 8909 | while (cur != NULL) { |
| 8910 | if ((node->type == XML_ELEMENT_NODE) && |
| 8911 | (cur->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8912 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 8913 | if (tmp == 1) |
| 8914 | return(1); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 8915 | } else if (((node->type == XML_TEXT_NODE) || |
| 8916 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8917 | (cur->type == XML_RELAXNG_TEXT)) { |
| 8918 | return(1); |
| 8919 | } |
| 8920 | cur = list[i++]; |
| 8921 | } |
| 8922 | return(0); |
| 8923 | } |
| 8924 | |
| 8925 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8926 | * xmlRelaxNGValidateInterleave: |
| 8927 | * @ctxt: a Relax-NG validation context |
| 8928 | * @define: the definition to verify |
| 8929 | * |
| 8930 | * Validate an interleave definition for a node. |
| 8931 | * |
| 8932 | * Returns 0 if the validation succeeded or an error code. |
| 8933 | */ |
| 8934 | static int |
| 8935 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 8936 | xmlRelaxNGDefinePtr define) { |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 8937 | int ret = 0, i, nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8938 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8939 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8940 | |
| 8941 | xmlRelaxNGValidStatePtr oldstate; |
| 8942 | xmlRelaxNGPartitionPtr partitions; |
| 8943 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 8944 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 8945 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 8946 | |
| 8947 | if (define->data != NULL) { |
| 8948 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 8949 | nbgroups = partitions->nbgroups; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8950 | } else { |
| 8951 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 8952 | return(-1); |
| 8953 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8954 | /* |
| 8955 | * Optimizations for MIXED |
| 8956 | */ |
| 8957 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8958 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8959 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 8960 | if (nbgroups == 2) { |
| 8961 | /* |
| 8962 | * this is a pure <mixed> case |
| 8963 | */ |
| 8964 | if (ctxt->state != NULL) |
| 8965 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8966 | ctxt->state->seq); |
| 8967 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 8968 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8969 | partitions->groups[1]->rule); |
| 8970 | else |
| 8971 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8972 | partitions->groups[0]->rule); |
| 8973 | if (ret == 0) { |
| 8974 | if (ctxt->state != NULL) |
| 8975 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8976 | ctxt->state->seq); |
| 8977 | } |
| 8978 | ctxt->flags = oldflags; |
| 8979 | return(ret); |
| 8980 | } |
| 8981 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8982 | |
| 8983 | /* |
| 8984 | * Build arrays to store the first and last node of the chain |
| 8985 | * pertaining to each group |
| 8986 | */ |
| 8987 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 8988 | if (list == NULL) { |
| 8989 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8990 | return(-1); |
| 8991 | } |
| 8992 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 8993 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 8994 | if (lasts == NULL) { |
| 8995 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8996 | return(-1); |
| 8997 | } |
| 8998 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 8999 | |
| 9000 | /* |
| 9001 | * Walk the sequence of children finding the right group and |
| 9002 | * sorting them in sequences. |
| 9003 | */ |
| 9004 | cur = ctxt->state->seq; |
| 9005 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9006 | start = cur; |
| 9007 | while (cur != NULL) { |
| 9008 | ctxt->state->seq = cur; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9009 | if ((partitions->triage != NULL) && |
| 9010 | (partitions->flags & IS_DETERMINIST)) { |
| 9011 | void *tmp = NULL; |
| 9012 | |
| 9013 | if ((cur->type == XML_TEXT_NODE) || |
| 9014 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 9015 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 9016 | NULL); |
| 9017 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 9018 | if (cur->ns != NULL) { |
| 9019 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 9020 | cur->ns->href); |
| 9021 | if (tmp == NULL) |
| 9022 | tmp = xmlHashLookup2(partitions->triage, |
| 9023 | BAD_CAST "#any", cur->ns->href); |
| 9024 | } else |
| 9025 | tmp = xmlHashLookup2(partitions->triage, cur->name, NULL); |
| 9026 | if (tmp == NULL) |
| 9027 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 9028 | NULL); |
| 9029 | } |
| 9030 | |
| 9031 | if (tmp == NULL) { |
| 9032 | i = nbgroups; |
| 9033 | } else { |
| 9034 | i = ((long) tmp) - 1; |
| 9035 | if (partitions->flags & IS_NEEDCHECK) { |
| 9036 | group = partitions->groups[i]; |
| 9037 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9038 | i = nbgroups; |
| 9039 | } |
| 9040 | } |
| 9041 | } else { |
| 9042 | for (i = 0;i < nbgroups;i++) { |
| 9043 | group = partitions->groups[i]; |
| 9044 | if (group == NULL) |
| 9045 | continue; |
| 9046 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 9047 | break; |
| 9048 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9049 | } |
| 9050 | /* |
| 9051 | * We break as soon as an element not matched is found |
| 9052 | */ |
| 9053 | if (i >= nbgroups) { |
| 9054 | break; |
| 9055 | } |
| 9056 | if (lasts[i] != NULL) { |
| 9057 | lasts[i]->next = cur; |
| 9058 | lasts[i] = cur; |
| 9059 | } else { |
| 9060 | list[i] = cur; |
| 9061 | lasts[i] = cur; |
| 9062 | } |
| 9063 | if (cur->next != NULL) |
| 9064 | lastchg = cur->next; |
| 9065 | else |
| 9066 | lastchg = cur; |
| 9067 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
| 9068 | } |
| 9069 | if (ret != 0) { |
| 9070 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9071 | ret = -1; |
| 9072 | goto done; |
| 9073 | } |
| 9074 | lastelem = cur; |
| 9075 | oldstate = ctxt->state; |
| 9076 | for (i = 0;i < nbgroups;i++) { |
| 9077 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 9078 | group = partitions->groups[i]; |
| 9079 | if (lasts[i] != NULL) { |
| 9080 | last = lasts[i]->next; |
| 9081 | lasts[i]->next = NULL; |
| 9082 | } |
| 9083 | ctxt->state->seq = list[i]; |
| 9084 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 9085 | if (ret != 0) |
| 9086 | break; |
| 9087 | if (ctxt->state != NULL) { |
| 9088 | cur = ctxt->state->seq; |
| 9089 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9090 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9091 | oldstate = ctxt->state; |
| 9092 | ctxt->state = NULL; |
| 9093 | if (cur != NULL) { |
| 9094 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9095 | ret = -1; |
| 9096 | ctxt->state = oldstate; |
| 9097 | goto done; |
| 9098 | } |
| 9099 | } else if (ctxt->states != NULL) { |
| 9100 | int j; |
| 9101 | int found = 0; |
| 9102 | |
| 9103 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 9104 | cur = ctxt->states->tabState[j]->seq; |
| 9105 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9106 | if (cur == NULL) { |
| 9107 | found = 1; |
| 9108 | break; |
| 9109 | } |
| 9110 | } |
| 9111 | if (ctxt->states->nbState > 0) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9112 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9113 | oldstate = ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9114 | } |
| 9115 | for (j = 0;j < ctxt->states->nbState - 1;j++) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9116 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[j]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9117 | } |
| 9118 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9119 | ctxt->states = NULL; |
| 9120 | if (found == 0) { |
| 9121 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9122 | ret = -1; |
| 9123 | ctxt->state = oldstate; |
| 9124 | goto done; |
| 9125 | } |
| 9126 | } else { |
| 9127 | ret = -1; |
| 9128 | break; |
| 9129 | } |
| 9130 | if (lasts[i] != NULL) { |
| 9131 | lasts[i]->next = last; |
| 9132 | } |
| 9133 | } |
| 9134 | if (ctxt->state != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9135 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9136 | ctxt->state = oldstate; |
| 9137 | ctxt->state->seq = lastelem; |
| 9138 | if (ret != 0) { |
| 9139 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9140 | ret = -1; |
| 9141 | goto done; |
| 9142 | } |
| 9143 | |
| 9144 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9145 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9146 | /* |
| 9147 | * builds the next links chain from the prev one |
| 9148 | */ |
| 9149 | cur = lastchg; |
| 9150 | while (cur != NULL) { |
| 9151 | if ((cur == start) || (cur->prev == NULL)) |
| 9152 | break; |
| 9153 | cur->prev->next = cur; |
| 9154 | cur = cur->prev; |
| 9155 | } |
| 9156 | if (ret == 0) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 9157 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9158 | } |
| 9159 | |
| 9160 | xmlFree(list); |
| 9161 | xmlFree(lasts); |
| 9162 | return(ret); |
| 9163 | } |
| 9164 | |
| 9165 | /** |
| 9166 | * xmlRelaxNGValidateDefinitionList: |
| 9167 | * @ctxt: a Relax-NG validation context |
| 9168 | * @define: the list of definition to verify |
| 9169 | * |
| 9170 | * Validate the given node content against the (list) of definitions |
| 9171 | * |
| 9172 | * Returns 0 if the validation succeeded or an error code. |
| 9173 | */ |
| 9174 | static int |
| 9175 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9176 | xmlRelaxNGDefinePtr defines) { |
| 9177 | int ret = 0, res; |
| 9178 | |
| 9179 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9180 | if (defines == NULL) { |
| 9181 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, BAD_CAST "NULL definition list"); |
| 9182 | return(-1); |
| 9183 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9184 | while (defines != NULL) { |
| 9185 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9186 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9187 | if (res < 0) |
| 9188 | ret = -1; |
| 9189 | } else { |
| 9190 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9191 | return(-1); |
| 9192 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 9193 | if (res == -1) /* continues on -2 */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9194 | break; |
| 9195 | defines = defines->next; |
| 9196 | } |
| 9197 | |
| 9198 | return(ret); |
| 9199 | } |
| 9200 | |
| 9201 | /** |
| 9202 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9203 | * @ctxt: a Relax-NG validation context |
| 9204 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9205 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9206 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9207 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9208 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9209 | * 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] | 9210 | */ |
| 9211 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9212 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9213 | xmlRelaxNGDefinePtr define, |
| 9214 | xmlNodePtr elem) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9215 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9216 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9217 | if (define->name != NULL) { |
| 9218 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9219 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9220 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9221 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9222 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9223 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
| 9224 | if (elem->ns == NULL) { |
| 9225 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, |
| 9226 | elem->name); |
| 9227 | return(0); |
| 9228 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9229 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9230 | elem->name, define->ns); |
| 9231 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9232 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9233 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
| 9234 | (define->name == NULL)) { |
| 9235 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 9236 | elem->name); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9237 | return(0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9238 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
| 9239 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 9240 | define->name); |
| 9241 | return(0); |
| 9242 | } |
| 9243 | |
| 9244 | if (define->nameClass == NULL) |
| 9245 | return(1); |
| 9246 | |
| 9247 | define = define->nameClass; |
| 9248 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 9249 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9250 | if (ctxt != NULL) { |
| 9251 | oldflags = ctxt->flags; |
| 9252 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9253 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9254 | |
| 9255 | list = define->content; |
| 9256 | while (list != NULL) { |
| 9257 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9258 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9259 | if (ctxt != NULL) |
| 9260 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9261 | return(0); |
| 9262 | } |
| 9263 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9264 | if (ctxt != NULL) |
| 9265 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9266 | return(ret); |
| 9267 | } |
| 9268 | list = list->next; |
| 9269 | } |
| 9270 | ret = 1; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9271 | if (ctxt != NULL) { |
| 9272 | ctxt->flags = oldflags; |
| 9273 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9274 | } else if (define->type == XML_RELAXNG_CHOICE) { |
| 9275 | xmlRelaxNGDefinePtr list; |
| 9276 | |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9277 | if (ctxt != NULL) { |
| 9278 | oldflags = ctxt->flags; |
| 9279 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9280 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9281 | |
| 9282 | list = define->nameClass; |
| 9283 | while (list != NULL) { |
| 9284 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9285 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9286 | if (ctxt != NULL) |
| 9287 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9288 | return(1); |
| 9289 | } |
| 9290 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9291 | if (ctxt != NULL) |
| 9292 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9293 | return(ret); |
| 9294 | } |
| 9295 | list = list->next; |
| 9296 | } |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9297 | if (ctxt != NULL) { |
| 9298 | if (ret != 0) { |
| 9299 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9300 | xmlRelaxNGDumpValidError(ctxt); |
| 9301 | } else { |
| 9302 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
| 9303 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9304 | } |
| 9305 | ret = 0; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9306 | if (ctxt != NULL) { |
| 9307 | ctxt->flags = oldflags; |
| 9308 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9309 | } else { |
| 9310 | TODO |
| 9311 | ret = -1; |
| 9312 | } |
| 9313 | return(ret); |
| 9314 | } |
| 9315 | |
| 9316 | /** |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9317 | * xmlRelaxNGBestState: |
| 9318 | * @ctxt: a Relax-NG validation context |
| 9319 | * |
| 9320 | * Find the "best" state in the ctxt->states list of states to report |
| 9321 | * errors about. I.e. a state with no element left in the child list |
| 9322 | * or the one with the less attributes left. |
| 9323 | * This is called only if a falidation error was detected |
| 9324 | * |
| 9325 | * Returns the index of the "best" state or -1 in case of error |
| 9326 | */ |
| 9327 | static int |
| 9328 | xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) { |
| 9329 | xmlRelaxNGValidStatePtr state; |
| 9330 | int i, tmp; |
| 9331 | int best = -1; |
| 9332 | int value = 1000000; |
| 9333 | |
| 9334 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9335 | (ctxt->states->nbState <= 0)) |
| 9336 | return(-1); |
| 9337 | |
| 9338 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 9339 | state = ctxt->states->tabState[i]; |
| 9340 | if (state == NULL) |
| 9341 | continue; |
| 9342 | if (state->seq != NULL) { |
| 9343 | if ((best == -1) || (value > 100000)) { |
| 9344 | value = 100000; |
| 9345 | best = i; |
| 9346 | } |
| 9347 | } else { |
| 9348 | tmp = state->nbAttrLeft; |
| 9349 | if ((best == -1) || (value > tmp)) { |
| 9350 | value = tmp; |
| 9351 | best = i; |
| 9352 | } |
| 9353 | } |
| 9354 | } |
| 9355 | return(best); |
| 9356 | } |
| 9357 | |
| 9358 | /** |
| 9359 | * xmlRelaxNGLogBestError: |
| 9360 | * @ctxt: a Relax-NG validation context |
| 9361 | * |
| 9362 | * Find the "best" state in the ctxt->states list of states to report |
| 9363 | * errors about and log it. |
| 9364 | */ |
| 9365 | static void |
| 9366 | xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) { |
| 9367 | int best; |
| 9368 | |
| 9369 | if ((ctxt == NULL) || (ctxt->states == NULL) || |
| 9370 | (ctxt->states->nbState <= 0)) |
| 9371 | return; |
| 9372 | |
| 9373 | best = xmlRelaxNGBestState(ctxt); |
| 9374 | if ((best >= 0) && (best < ctxt->states->nbState)) { |
| 9375 | ctxt->state = ctxt->states->tabState[best]; |
| 9376 | |
| 9377 | xmlRelaxNGValidateElementEnd(ctxt, 1); |
| 9378 | } |
| 9379 | } |
| 9380 | |
| 9381 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9382 | * xmlRelaxNGValidateElementEnd: |
| 9383 | * @ctxt: a Relax-NG validation context |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9384 | * @log: indicate that error logging should be done |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9385 | * |
| 9386 | * Validate the end of the element, implements check that |
| 9387 | * there is nothing left not consumed in the element content |
| 9388 | * or in the attribute list. |
| 9389 | * |
| 9390 | * Returns 0 if the validation succeeded or an error code. |
| 9391 | */ |
| 9392 | static int |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9393 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int log) { |
| 9394 | int i; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9395 | xmlRelaxNGValidStatePtr state; |
| 9396 | |
| 9397 | state = ctxt->state; |
| 9398 | if (state->seq != NULL) { |
| 9399 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9400 | if (state->seq != NULL) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9401 | if (log) { |
| 9402 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9403 | state->node->name, state->seq->name); |
| 9404 | } |
| 9405 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9406 | } |
| 9407 | } |
| 9408 | for (i = 0;i < state->nbAttrs;i++) { |
| 9409 | if (state->attrs[i] != NULL) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9410 | if (log) { |
| 9411 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9412 | state->attrs[i]->name, state->node->name); |
| 9413 | } |
| 9414 | return(-1 -i); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9415 | } |
| 9416 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9417 | return(0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9418 | } |
| 9419 | |
| 9420 | /** |
| 9421 | * xmlRelaxNGValidateState: |
| 9422 | * @ctxt: a Relax-NG validation context |
| 9423 | * @define: the definition to verify |
| 9424 | * |
| 9425 | * Validate the current state against the definition |
| 9426 | * |
| 9427 | * Returns 0 if the validation succeeded or an error code. |
| 9428 | */ |
| 9429 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9430 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9431 | xmlRelaxNGDefinePtr define) |
| 9432 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9433 | xmlNodePtr node; |
| 9434 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9435 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9436 | |
| 9437 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9438 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9439 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9440 | } |
| 9441 | |
| 9442 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9443 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9444 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9445 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9446 | } |
| 9447 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9448 | for (i = 0; i < ctxt->depth; i++) |
| 9449 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9450 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9451 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9452 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9453 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9454 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9455 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9456 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9457 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9458 | #endif |
| 9459 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9460 | switch (define->type) { |
| 9461 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9462 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9463 | ret = 0; |
| 9464 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9465 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9466 | ret = -1; |
| 9467 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9468 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9469 | while ((node != NULL) && |
| 9470 | ((node->type == XML_TEXT_NODE) || |
| 9471 | (node->type == XML_COMMENT_NODE) || |
| 9472 | (node->type == XML_PI_NODE) || |
| 9473 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9474 | node = node->next; |
| 9475 | ctxt->state->seq = node; |
| 9476 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9477 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9478 | errNr = ctxt->errNr; |
| 9479 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9480 | if (node == NULL) { |
| 9481 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9482 | ret = -1; |
| 9483 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9484 | xmlRelaxNGDumpValidError(ctxt); |
| 9485 | break; |
| 9486 | } |
| 9487 | if (node->type != XML_ELEMENT_NODE) { |
| 9488 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9489 | ret = -1; |
| 9490 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9491 | xmlRelaxNGDumpValidError(ctxt); |
| 9492 | break; |
| 9493 | } |
| 9494 | /* |
| 9495 | * This node was already validated successfully against |
| 9496 | * this definition. |
| 9497 | */ |
| 9498 | if (node->_private == define) { |
| 9499 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9500 | if (ctxt->errNr > errNr) |
| 9501 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9502 | if (ctxt->errNr != 0) { |
| 9503 | while ((ctxt->err != NULL) && |
| 9504 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9505 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9506 | || |
| 9507 | ((ctxt->err->err == |
| 9508 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9509 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9510 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9511 | || (ctxt->err->err == |
| 9512 | XML_RELAXNG_ERR_NOTELEM))) |
| 9513 | xmlRelaxNGValidErrorPop(ctxt); |
| 9514 | } |
| 9515 | break; |
| 9516 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9517 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9518 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9519 | if (ret <= 0) { |
| 9520 | ret = -1; |
| 9521 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9522 | xmlRelaxNGDumpValidError(ctxt); |
| 9523 | break; |
| 9524 | } |
| 9525 | ret = 0; |
| 9526 | if (ctxt->errNr != 0) { |
| 9527 | if (ctxt->errNr > errNr) |
| 9528 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9529 | while ((ctxt->err != NULL) && |
| 9530 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9531 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9532 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9533 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9534 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9535 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9536 | xmlRelaxNGValidErrorPop(ctxt); |
| 9537 | } |
| 9538 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9539 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9540 | oldflags = ctxt->flags; |
| 9541 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9542 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9543 | } |
| 9544 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9545 | if (state == NULL) { |
| 9546 | ret = -1; |
| 9547 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9548 | xmlRelaxNGDumpValidError(ctxt); |
| 9549 | break; |
| 9550 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9551 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9552 | oldstate = ctxt->state; |
| 9553 | ctxt->state = state; |
| 9554 | if (define->attrs != NULL) { |
| 9555 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9556 | if (tmp != 0) { |
| 9557 | ret = -1; |
| 9558 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9559 | } |
| 9560 | } |
| 9561 | if (define->contModel != NULL) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9562 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9563 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9564 | xmlNodePtr nseq; |
| 9565 | |
| 9566 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9567 | ctxt->state = nstate; |
| 9568 | ctxt->states = NULL; |
| 9569 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9570 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9571 | define->contModel, |
| 9572 | ctxt->state->seq); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9573 | nseq = ctxt->state->seq; |
| 9574 | ctxt->state = tmpstate; |
| 9575 | ctxt->states = tmpstates; |
| 9576 | xmlRelaxNGFreeValidState(ctxt, nstate); |
| 9577 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9578 | #ifdef DEBUG_COMPILE |
| 9579 | xmlGenericError(xmlGenericErrorContext, |
| 9580 | "Validating content of '%s' : %d\n", define->name, tmp); |
| 9581 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9582 | if (tmp != 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9583 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9584 | |
| 9585 | if (ctxt->states != NULL) { |
| 9586 | tmp = -1; |
| 9587 | |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9588 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9589 | state = ctxt->states->tabState[i]; |
| 9590 | ctxt->state = state; |
| 9591 | ctxt->state->seq = nseq; |
| 9592 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9593 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9594 | tmp = 0; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9595 | break; |
| 9596 | } |
| 9597 | } |
| 9598 | if (tmp != 0) { |
| 9599 | /* |
| 9600 | * validation error, log the message for the "best" one |
| 9601 | */ |
| 9602 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9603 | xmlRelaxNGLogBestError(ctxt); |
| 9604 | } |
| 9605 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9606 | xmlRelaxNGFreeValidState(ctxt, |
| 9607 | ctxt->states->tabState[i]); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9608 | } |
| 9609 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9610 | ctxt->flags = oldflags; |
| 9611 | ctxt->states = NULL; |
| 9612 | if ((ret == 0) && (tmp == -1)) |
| 9613 | ret = -1; |
| 9614 | } else { |
| 9615 | state = ctxt->state; |
| 9616 | ctxt->state->seq = nseq; |
| 9617 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9618 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9619 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9620 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9621 | } else { |
| 9622 | if (define->content != NULL) { |
| 9623 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9624 | define->content); |
| 9625 | if (tmp != 0) { |
| 9626 | ret = -1; |
| 9627 | if (ctxt->state == NULL) { |
| 9628 | ctxt->state = oldstate; |
| 9629 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9630 | node->name); |
| 9631 | ctxt->state = NULL; |
| 9632 | } else { |
| 9633 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9634 | node->name); |
| 9635 | } |
| 9636 | |
| 9637 | } |
| 9638 | } |
| 9639 | if (ctxt->states != NULL) { |
| 9640 | tmp = -1; |
| 9641 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9642 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9643 | state = ctxt->states->tabState[i]; |
| 9644 | ctxt->state = state; |
| 9645 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9646 | if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9647 | tmp = 0; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9648 | break; |
| 9649 | } |
| 9650 | } |
| 9651 | if (tmp != 0) { |
| 9652 | /* |
| 9653 | * validation error, log the message for the "best" one |
| 9654 | */ |
| 9655 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9656 | xmlRelaxNGLogBestError(ctxt); |
| 9657 | } |
| 9658 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9659 | xmlRelaxNGFreeValidState(ctxt, |
| 9660 | ctxt->states->tabState[i]); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9661 | } |
| 9662 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9663 | ctxt->flags = oldflags; |
| 9664 | ctxt->states = NULL; |
| 9665 | if ((ret == 0) && (tmp == -1)) |
| 9666 | ret = -1; |
| 9667 | } else { |
| 9668 | state = ctxt->state; |
| 9669 | if (ret == 0) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 9670 | ret = xmlRelaxNGValidateElementEnd(ctxt, 1); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9671 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9672 | } |
| 9673 | } |
| 9674 | if (ret == 0) { |
| 9675 | node->_private = define; |
| 9676 | } |
| 9677 | ctxt->flags = oldflags; |
| 9678 | ctxt->state = oldstate; |
| 9679 | if (oldstate != NULL) |
| 9680 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9681 | if (ret != 0) { |
| 9682 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9683 | xmlRelaxNGDumpValidError(ctxt); |
| 9684 | ret = 0; |
| 9685 | } else { |
| 9686 | ret = -2; |
| 9687 | } |
| 9688 | } else { |
| 9689 | if (ctxt->errNr > errNr) |
| 9690 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9691 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9692 | |
| 9693 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9694 | xmlGenericError(xmlGenericErrorContext, |
| 9695 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 9696 | node->name, ret); |
| 9697 | if (oldstate == NULL) |
| 9698 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 9699 | else if (oldstate->seq == NULL) |
| 9700 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 9701 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 9702 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 9703 | oldstate->seq->name); |
| 9704 | else |
| 9705 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 9706 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9707 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9708 | break; |
| 9709 | case XML_RELAXNG_OPTIONAL:{ |
| 9710 | errNr = ctxt->errNr; |
| 9711 | oldflags = ctxt->flags; |
| 9712 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9713 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9714 | ret = |
| 9715 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 9716 | define->content); |
| 9717 | if (ret != 0) { |
| 9718 | if (ctxt->state != NULL) |
| 9719 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9720 | ctxt->state = oldstate; |
| 9721 | ctxt->flags = oldflags; |
| 9722 | ret = 0; |
| 9723 | if (ctxt->errNr > errNr) |
| 9724 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9725 | break; |
| 9726 | } |
| 9727 | if (ctxt->states != NULL) { |
| 9728 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9729 | } else { |
| 9730 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 9731 | if (ctxt->states == NULL) { |
| 9732 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9733 | ctxt->flags = oldflags; |
| 9734 | ret = -1; |
| 9735 | if (ctxt->errNr > errNr) |
| 9736 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9737 | break; |
| 9738 | } |
| 9739 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9740 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 9741 | ctxt->state = NULL; |
| 9742 | } |
| 9743 | ctxt->flags = oldflags; |
| 9744 | ret = 0; |
| 9745 | if (ctxt->errNr > errNr) |
| 9746 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9747 | break; |
| 9748 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9749 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9750 | errNr = ctxt->errNr; |
| 9751 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9752 | if (ret != 0) { |
| 9753 | break; |
| 9754 | } |
| 9755 | if (ctxt->errNr > errNr) |
| 9756 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9757 | /* no break on purpose */ |
| 9758 | case XML_RELAXNG_ZEROORMORE:{ |
| 9759 | int progress; |
| 9760 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 9761 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9762 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9763 | errNr = ctxt->errNr; |
| 9764 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 9765 | if (res == NULL) { |
| 9766 | ret = -1; |
| 9767 | break; |
| 9768 | } |
| 9769 | /* |
| 9770 | * All the input states are also exit states |
| 9771 | */ |
| 9772 | if (ctxt->state != NULL) { |
| 9773 | xmlRelaxNGAddStates(ctxt, res, |
| 9774 | xmlRelaxNGCopyValidState(ctxt, |
| 9775 | ctxt-> |
| 9776 | state)); |
| 9777 | } else { |
| 9778 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9779 | xmlRelaxNGAddStates(ctxt, res, |
| 9780 | xmlRelaxNGCopyValidState(ctxt, |
| 9781 | ctxt-> |
| 9782 | states-> |
| 9783 | tabState |
| 9784 | [j])); |
| 9785 | } |
| 9786 | } |
| 9787 | oldflags = ctxt->flags; |
| 9788 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9789 | do { |
| 9790 | progress = 0; |
| 9791 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9792 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9793 | if (ctxt->states != NULL) { |
| 9794 | states = ctxt->states; |
| 9795 | for (i = 0; i < states->nbState; i++) { |
| 9796 | ctxt->state = states->tabState[i]; |
| 9797 | ctxt->states = NULL; |
| 9798 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9799 | define-> |
| 9800 | content); |
| 9801 | if (ret == 0) { |
| 9802 | if (ctxt->state != NULL) { |
| 9803 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9804 | ctxt->state); |
| 9805 | ctxt->state = NULL; |
| 9806 | if (tmp == 1) |
| 9807 | progress = 1; |
| 9808 | } else if (ctxt->states != NULL) { |
| 9809 | for (j = 0; j < ctxt->states->nbState; |
| 9810 | j++) { |
| 9811 | tmp = |
| 9812 | xmlRelaxNGAddStates(ctxt, res, |
| 9813 | ctxt-> |
| 9814 | states-> |
| 9815 | tabState |
| 9816 | [j]); |
| 9817 | if (tmp == 1) |
| 9818 | progress = 1; |
| 9819 | } |
| 9820 | xmlRelaxNGFreeStates(ctxt, |
| 9821 | ctxt->states); |
| 9822 | ctxt->states = NULL; |
| 9823 | } |
| 9824 | } else { |
| 9825 | if (ctxt->state != NULL) { |
| 9826 | xmlRelaxNGFreeValidState(ctxt, |
| 9827 | ctxt->state); |
| 9828 | ctxt->state = NULL; |
| 9829 | } |
| 9830 | } |
| 9831 | } |
| 9832 | } else { |
| 9833 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9834 | define-> |
| 9835 | content); |
| 9836 | if (ret != 0) { |
| 9837 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9838 | ctxt->state = NULL; |
| 9839 | } else { |
| 9840 | base = res->nbState; |
| 9841 | if (ctxt->state != NULL) { |
| 9842 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9843 | ctxt->state); |
| 9844 | ctxt->state = NULL; |
| 9845 | if (tmp == 1) |
| 9846 | progress = 1; |
| 9847 | } else if (ctxt->states != NULL) { |
| 9848 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9849 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9850 | ctxt-> |
| 9851 | states-> |
| 9852 | tabState[j]); |
| 9853 | if (tmp == 1) |
| 9854 | progress = 1; |
| 9855 | } |
| 9856 | if (states == NULL) { |
| 9857 | states = ctxt->states; |
| 9858 | } else { |
| 9859 | xmlRelaxNGFreeStates(ctxt, |
| 9860 | ctxt->states); |
| 9861 | } |
| 9862 | ctxt->states = NULL; |
| 9863 | } |
| 9864 | } |
| 9865 | } |
| 9866 | if (progress) { |
| 9867 | /* |
| 9868 | * Collect all the new nodes added at that step |
| 9869 | * and make them the new node set |
| 9870 | */ |
| 9871 | if (res->nbState - base == 1) { |
| 9872 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 9873 | res-> |
| 9874 | tabState |
| 9875 | [base]); |
| 9876 | } else { |
| 9877 | if (states == NULL) { |
| 9878 | xmlRelaxNGNewStates(ctxt, |
| 9879 | res->nbState - base); |
| 9880 | } |
| 9881 | states->nbState = 0; |
| 9882 | for (i = base; i < res->nbState; i++) |
| 9883 | xmlRelaxNGAddStates(ctxt, states, |
| 9884 | xmlRelaxNGCopyValidState |
| 9885 | (ctxt, |
| 9886 | res->tabState[i])); |
| 9887 | ctxt->states = states; |
| 9888 | } |
| 9889 | } |
| 9890 | } while (progress == 1); |
| 9891 | if (states != NULL) { |
| 9892 | xmlRelaxNGFreeStates(ctxt, states); |
| 9893 | } |
| 9894 | ctxt->states = res; |
| 9895 | ctxt->flags = oldflags; |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 9896 | #if 0 |
| 9897 | /* |
| 9898 | * errors may have to be propagated back... |
| 9899 | */ |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9900 | if (ctxt->errNr > errNr) |
| 9901 | xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | c1ffa0a | 2003-08-26 13:56:48 +0000 | [diff] [blame] | 9902 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9903 | ret = 0; |
| 9904 | break; |
| 9905 | } |
| 9906 | case XML_RELAXNG_CHOICE:{ |
| 9907 | xmlRelaxNGDefinePtr list = NULL; |
| 9908 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9909 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9910 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9911 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9912 | errNr = ctxt->errNr; |
| 9913 | if ((define->dflags & IS_TRIABLE) |
| 9914 | && (define->data != NULL)) { |
| 9915 | xmlHashTablePtr triage = |
| 9916 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9917 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9918 | /* |
| 9919 | * Something we can optimize cleanly there is only one |
| 9920 | * possble branch out ! |
| 9921 | */ |
| 9922 | if (node == NULL) { |
| 9923 | ret = -1; |
| 9924 | break; |
| 9925 | } |
| 9926 | if ((node->type == XML_TEXT_NODE) || |
| 9927 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 9928 | list = |
| 9929 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 9930 | } else if (node->type == XML_ELEMENT_NODE) { |
| 9931 | if (node->ns != NULL) { |
| 9932 | list = xmlHashLookup2(triage, node->name, |
| 9933 | node->ns->href); |
| 9934 | if (list == NULL) |
| 9935 | list = |
| 9936 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9937 | node->ns->href); |
| 9938 | } else |
| 9939 | list = |
| 9940 | xmlHashLookup2(triage, node->name, NULL); |
| 9941 | if (list == NULL) |
| 9942 | list = |
| 9943 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9944 | NULL); |
| 9945 | } |
| 9946 | if (list == NULL) { |
| 9947 | ret = -1; |
| 9948 | break; |
| 9949 | } |
| 9950 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 9951 | if (ret == 0) { |
| 9952 | } |
| 9953 | break; |
| 9954 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9955 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9956 | list = define->content; |
| 9957 | oldflags = ctxt->flags; |
| 9958 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9959 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9960 | while (list != NULL) { |
| 9961 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9962 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 9963 | if (ret == 0) { |
| 9964 | if (states == NULL) { |
| 9965 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 9966 | } |
| 9967 | if (ctxt->state != NULL) { |
| 9968 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 9969 | } else if (ctxt->states != NULL) { |
| 9970 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9971 | xmlRelaxNGAddStates(ctxt, states, |
| 9972 | ctxt->states-> |
| 9973 | tabState[i]); |
| 9974 | } |
| 9975 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9976 | ctxt->states = NULL; |
| 9977 | } |
| 9978 | } else { |
| 9979 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9980 | } |
| 9981 | ctxt->state = oldstate; |
| 9982 | list = list->next; |
| 9983 | } |
| 9984 | if (states != NULL) { |
| 9985 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9986 | ctxt->states = states; |
| 9987 | ctxt->state = NULL; |
| 9988 | ret = 0; |
| 9989 | } else { |
| 9990 | ctxt->states = NULL; |
| 9991 | } |
| 9992 | ctxt->flags = oldflags; |
| 9993 | if (ret != 0) { |
| 9994 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9995 | xmlRelaxNGDumpValidError(ctxt); |
| 9996 | } |
| 9997 | } else { |
| 9998 | if (ctxt->errNr > errNr) |
| 9999 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 10000 | } |
| 10001 | break; |
| 10002 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10003 | case XML_RELAXNG_DEF: |
| 10004 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10005 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 10006 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10007 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10008 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 10009 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10010 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10011 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 10012 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10013 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10014 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10015 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10016 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 10017 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10018 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 10019 | break; |
| 10020 | case XML_RELAXNG_DATATYPE:{ |
| 10021 | xmlNodePtr child; |
| 10022 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10023 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10024 | child = node; |
| 10025 | while (child != NULL) { |
| 10026 | if (child->type == XML_ELEMENT_NODE) { |
| 10027 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 10028 | node->parent->name); |
| 10029 | ret = -1; |
| 10030 | break; |
| 10031 | } else if ((child->type == XML_TEXT_NODE) || |
| 10032 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10033 | content = xmlStrcat(content, child->content); |
| 10034 | } |
| 10035 | /* TODO: handle entities ... */ |
| 10036 | child = child->next; |
| 10037 | } |
| 10038 | if (ret == -1) { |
| 10039 | if (content != NULL) |
| 10040 | xmlFree(content); |
| 10041 | break; |
| 10042 | } |
| 10043 | if (content == NULL) { |
| 10044 | content = xmlStrdup(BAD_CAST ""); |
| 10045 | if (content == NULL) { |
| 10046 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 10047 | ret = -1; |
| 10048 | break; |
| 10049 | } |
| 10050 | } |
| 10051 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 10052 | ctxt->state->seq); |
| 10053 | if (ret == -1) { |
| 10054 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 10055 | } else if (ret == 0) { |
| 10056 | ctxt->state->seq = NULL; |
| 10057 | } |
| 10058 | if (content != NULL) |
| 10059 | xmlFree(content); |
| 10060 | break; |
| 10061 | } |
| 10062 | case XML_RELAXNG_VALUE:{ |
| 10063 | xmlChar *content = NULL; |
| 10064 | xmlChar *oldvalue; |
| 10065 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10066 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10067 | child = node; |
| 10068 | while (child != NULL) { |
| 10069 | if (child->type == XML_ELEMENT_NODE) { |
| 10070 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 10071 | node->parent->name); |
| 10072 | ret = -1; |
| 10073 | break; |
| 10074 | } else if ((child->type == XML_TEXT_NODE) || |
| 10075 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10076 | content = xmlStrcat(content, child->content); |
| 10077 | } |
| 10078 | /* TODO: handle entities ... */ |
| 10079 | child = child->next; |
| 10080 | } |
| 10081 | if (ret == -1) { |
| 10082 | if (content != NULL) |
| 10083 | xmlFree(content); |
| 10084 | break; |
| 10085 | } |
| 10086 | if (content == NULL) { |
| 10087 | content = xmlStrdup(BAD_CAST ""); |
| 10088 | if (content == NULL) { |
| 10089 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 10090 | ret = -1; |
| 10091 | break; |
| 10092 | } |
| 10093 | } |
| 10094 | oldvalue = ctxt->state->value; |
| 10095 | ctxt->state->value = content; |
| 10096 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10097 | ctxt->state->value = oldvalue; |
| 10098 | if (ret == -1) { |
| 10099 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 10100 | } else if (ret == 0) { |
| 10101 | ctxt->state->seq = NULL; |
| 10102 | } |
| 10103 | if (content != NULL) |
| 10104 | xmlFree(content); |
| 10105 | break; |
| 10106 | } |
| 10107 | case XML_RELAXNG_LIST:{ |
| 10108 | xmlChar *content; |
| 10109 | xmlNodePtr child; |
| 10110 | xmlChar *oldvalue, *oldendvalue; |
| 10111 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10112 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10113 | /* |
| 10114 | * Make sure it's only text nodes |
| 10115 | */ |
| 10116 | |
| 10117 | content = NULL; |
| 10118 | child = node; |
| 10119 | while (child != NULL) { |
| 10120 | if (child->type == XML_ELEMENT_NODE) { |
| 10121 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 10122 | node->parent->name); |
| 10123 | ret = -1; |
| 10124 | break; |
| 10125 | } else if ((child->type == XML_TEXT_NODE) || |
| 10126 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 10127 | content = xmlStrcat(content, child->content); |
| 10128 | } |
| 10129 | /* TODO: handle entities ... */ |
| 10130 | child = child->next; |
| 10131 | } |
| 10132 | if (ret == -1) { |
| 10133 | if (content != NULL) |
| 10134 | xmlFree(content); |
| 10135 | break; |
| 10136 | } |
| 10137 | if (content == NULL) { |
| 10138 | content = xmlStrdup(BAD_CAST ""); |
| 10139 | if (content == NULL) { |
| 10140 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 10141 | ret = -1; |
| 10142 | break; |
| 10143 | } |
| 10144 | } |
| 10145 | len = xmlStrlen(content); |
| 10146 | oldvalue = ctxt->state->value; |
| 10147 | oldendvalue = ctxt->state->endvalue; |
| 10148 | ctxt->state->value = content; |
| 10149 | ctxt->state->endvalue = content + len; |
| 10150 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 10151 | ctxt->state->value = oldvalue; |
| 10152 | ctxt->state->endvalue = oldendvalue; |
| 10153 | if (ret == -1) { |
| 10154 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 10155 | } else if ((ret == 0) && (node != NULL)) { |
| 10156 | ctxt->state->seq = node->next; |
| 10157 | } |
| 10158 | if (content != NULL) |
| 10159 | xmlFree(content); |
| 10160 | break; |
| 10161 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10162 | case XML_RELAXNG_EXCEPT: |
| 10163 | case XML_RELAXNG_PARAM: |
| 10164 | TODO ret = -1; |
| 10165 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10166 | } |
| 10167 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10168 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10169 | for (i = 0; i < ctxt->depth; i++) |
| 10170 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10171 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10172 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10173 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10174 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10175 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10176 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10177 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10178 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10179 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 10180 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10181 | } |
| 10182 | |
| 10183 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10184 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10185 | * @ctxt: a Relax-NG validation context |
| 10186 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10187 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10188 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10189 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10190 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10191 | */ |
| 10192 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10193 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 10194 | xmlRelaxNGDefinePtr define) { |
| 10195 | xmlRelaxNGStatesPtr states, res; |
| 10196 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10197 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10198 | /* |
| 10199 | * We should NOT have both ctxt->state and ctxt->states |
| 10200 | */ |
| 10201 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10202 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10203 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10204 | ctxt->state = NULL; |
| 10205 | } |
| 10206 | |
| 10207 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
| 10208 | if (ctxt->states != NULL) { |
| 10209 | ctxt->state = ctxt->states->tabState[0]; |
| 10210 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10211 | ctxt->states = NULL; |
| 10212 | } |
| 10213 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10214 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10215 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10216 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10217 | ctxt->state = NULL; |
| 10218 | } |
| 10219 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10220 | ctxt->state = ctxt->states->tabState[0]; |
| 10221 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10222 | ctxt->states = NULL; |
| 10223 | } |
| 10224 | return(ret); |
| 10225 | } |
| 10226 | |
| 10227 | states = ctxt->states; |
| 10228 | ctxt->states = NULL; |
| 10229 | res = NULL; |
| 10230 | j = 0; |
| 10231 | oldflags = ctxt->flags; |
| 10232 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10233 | for (i = 0;i < states->nbState;i++) { |
| 10234 | ctxt->state = states->tabState[i]; |
| 10235 | ctxt->states = NULL; |
| 10236 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10237 | /* |
| 10238 | * We should NOT have both ctxt->state and ctxt->states |
| 10239 | */ |
| 10240 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10241 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10242 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10243 | ctxt->state = NULL; |
| 10244 | } |
| 10245 | if (ret == 0) { |
| 10246 | if (ctxt->states == NULL) { |
| 10247 | if (res != NULL) { |
| 10248 | /* add the state to the container */ |
| 10249 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10250 | ctxt->state = NULL; |
| 10251 | } else { |
| 10252 | /* add the state directly in states */ |
| 10253 | states->tabState[j++] = ctxt->state; |
| 10254 | ctxt->state = NULL; |
| 10255 | } |
| 10256 | } else { |
| 10257 | if (res == NULL) { |
| 10258 | /* make it the new container and copy other results */ |
| 10259 | res = ctxt->states; |
| 10260 | ctxt->states = NULL; |
| 10261 | for (k = 0;k < j;k++) |
| 10262 | xmlRelaxNGAddStates(ctxt, res, states->tabState[k]); |
| 10263 | } else { |
| 10264 | /* add all the new results to res and reff the container */ |
| 10265 | for (k = 0;k < ctxt->states->nbState;k++) |
| 10266 | xmlRelaxNGAddStates(ctxt, res, |
| 10267 | ctxt->states->tabState[k]); |
| 10268 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10269 | ctxt->states = NULL; |
| 10270 | } |
| 10271 | } |
| 10272 | } else { |
| 10273 | if (ctxt->state != NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10274 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10275 | ctxt->state = NULL; |
| 10276 | } else if (ctxt->states != NULL) { |
| 10277 | for (k = 0;k < ctxt->states->nbState;k++) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10278 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[k]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10279 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10280 | ctxt->states = NULL; |
| 10281 | } |
| 10282 | } |
| 10283 | } |
| 10284 | ctxt->flags = oldflags; |
| 10285 | if (res != NULL) { |
| 10286 | xmlRelaxNGFreeStates(ctxt, states); |
| 10287 | ctxt->states = res; |
| 10288 | ret = 0; |
| 10289 | } else if (j > 1) { |
| 10290 | states->nbState = j; |
| 10291 | ctxt->states = states; |
| 10292 | ret =0; |
| 10293 | } else if (j == 1) { |
| 10294 | ctxt->state = states->tabState[0]; |
| 10295 | xmlRelaxNGFreeStates(ctxt, states); |
| 10296 | ret = 0; |
| 10297 | } else { |
| 10298 | ret = -1; |
| 10299 | xmlRelaxNGFreeStates(ctxt, states); |
| 10300 | if (ctxt->states != NULL) { |
| 10301 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10302 | ctxt->states = NULL; |
| 10303 | } |
| 10304 | } |
| 10305 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10306 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10307 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10308 | ctxt->state = NULL; |
| 10309 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10310 | return(ret); |
| 10311 | } |
| 10312 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10313 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10314 | * xmlRelaxNGValidateDocument: |
| 10315 | * @ctxt: a Relax-NG validation context |
| 10316 | * @doc: the document |
| 10317 | * |
| 10318 | * Validate the given document |
| 10319 | * |
| 10320 | * Returns 0 if the validation succeeded or an error code. |
| 10321 | */ |
| 10322 | static int |
| 10323 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 10324 | int ret; |
| 10325 | xmlRelaxNGPtr schema; |
| 10326 | xmlRelaxNGGrammarPtr grammar; |
| 10327 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10328 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10329 | |
| 10330 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
| 10331 | return(-1); |
| 10332 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10333 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10334 | schema = ctxt->schema; |
| 10335 | grammar = schema->topgrammar; |
| 10336 | if (grammar == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10337 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10338 | return(-1); |
| 10339 | } |
| 10340 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10341 | ctxt->state = state; |
| 10342 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10343 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
| 10344 | state = ctxt->state; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10345 | node = state->seq; |
| 10346 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10347 | if (node != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10348 | if (ret != -1) { |
| 10349 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10350 | ret = -1; |
| 10351 | } |
| 10352 | } |
| 10353 | } else if (ctxt->states != NULL) { |
| 10354 | int i; |
| 10355 | int tmp = -1; |
| 10356 | |
| 10357 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 10358 | state = ctxt->states->tabState[i]; |
| 10359 | node = state->seq; |
| 10360 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10361 | if (node == NULL) |
| 10362 | tmp = 0; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10363 | xmlRelaxNGFreeValidState(ctxt,state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10364 | } |
| 10365 | if (tmp == -1) { |
| 10366 | if (ret != -1) { |
| 10367 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10368 | ret = -1; |
| 10369 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10370 | } |
| 10371 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10372 | if (ctxt->state != NULL) { |
| 10373 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10374 | ctxt->state = NULL; |
| 10375 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10376 | if (ret != 0) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10377 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10378 | #ifdef DEBUG |
| 10379 | else if (ctxt->errNr != 0) { |
| 10380 | ctxt->error(ctxt->userData, "%d Extra error messages left on stack !\n", |
| 10381 | ctxt->errNr); |
| 10382 | xmlRelaxNGDumpValidError(ctxt); |
| 10383 | } |
| 10384 | #endif |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10385 | if (ctxt->idref == 1) { |
| 10386 | xmlValidCtxt vctxt; |
| 10387 | |
| 10388 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10389 | vctxt.valid = 1; |
| 10390 | vctxt.error = ctxt->error; |
| 10391 | vctxt.warning = ctxt->warning; |
| 10392 | vctxt.userData = ctxt->userData; |
| 10393 | |
| 10394 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10395 | ret = -1; |
| 10396 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10397 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
| 10398 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10399 | |
| 10400 | return(ret); |
| 10401 | } |
| 10402 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10403 | /************************************************************************ |
| 10404 | * * |
| 10405 | * Validation interfaces * |
| 10406 | * * |
| 10407 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10408 | /** |
| 10409 | * xmlRelaxNGNewValidCtxt: |
| 10410 | * @schema: a precompiled XML RelaxNGs |
| 10411 | * |
| 10412 | * Create an XML RelaxNGs validation context based on the given schema |
| 10413 | * |
| 10414 | * Returns the validation context or NULL in case of error |
| 10415 | */ |
| 10416 | xmlRelaxNGValidCtxtPtr |
| 10417 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) { |
| 10418 | xmlRelaxNGValidCtxtPtr ret; |
| 10419 | |
| 10420 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10421 | if (ret == NULL) { |
| 10422 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10423 | "Failed to allocate new schema validation context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10424 | return (NULL); |
| 10425 | } |
| 10426 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10427 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10428 | ret->error = xmlGenericError; |
| 10429 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10430 | ret->errNr = 0; |
| 10431 | ret->errMax = 0; |
| 10432 | ret->err = NULL; |
| 10433 | ret->errTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10434 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10435 | ret->states = NULL; |
| 10436 | ret->freeState = NULL; |
| 10437 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10438 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10439 | return (ret); |
| 10440 | } |
| 10441 | |
| 10442 | /** |
| 10443 | * xmlRelaxNGFreeValidCtxt: |
| 10444 | * @ctxt: the schema validation context |
| 10445 | * |
| 10446 | * Free the resources associated to the schema validation context |
| 10447 | */ |
| 10448 | void |
| 10449 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10450 | int k; |
| 10451 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10452 | if (ctxt == NULL) |
| 10453 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10454 | if (ctxt->states != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10455 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
| 10456 | if (ctxt->freeState != NULL) { |
| 10457 | for (k = 0;k < ctxt->freeState->nbState;k++) { |
| 10458 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10459 | } |
| 10460 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
| 10461 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10462 | if (ctxt->freeStates != NULL) { |
| 10463 | for (k = 0;k < ctxt->freeStatesNr;k++) { |
| 10464 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10465 | } |
| 10466 | xmlFree(ctxt->freeStates); |
| 10467 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10468 | if (ctxt->errTab != NULL) |
| 10469 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10470 | if (ctxt->elemTab != NULL) { |
| 10471 | xmlRegExecCtxtPtr exec; |
| 10472 | |
| 10473 | exec = xmlRelaxNGElemPop(ctxt); |
| 10474 | while (exec != NULL) { |
| 10475 | xmlRegFreeExecCtxt(exec); |
| 10476 | exec = xmlRelaxNGElemPop(ctxt); |
| 10477 | } |
| 10478 | xmlFree(ctxt->elemTab); |
| 10479 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10480 | xmlFree(ctxt); |
| 10481 | } |
| 10482 | |
| 10483 | /** |
| 10484 | * xmlRelaxNGSetValidErrors: |
| 10485 | * @ctxt: a Relax-NG validation context |
| 10486 | * @err: the error function |
| 10487 | * @warn: the warning function |
| 10488 | * @ctx: the functions context |
| 10489 | * |
| 10490 | * Set the error and warning callback informations |
| 10491 | */ |
| 10492 | void |
| 10493 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
| 10494 | xmlRelaxNGValidityErrorFunc err, |
| 10495 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 10496 | if (ctxt == NULL) |
| 10497 | return; |
| 10498 | ctxt->error = err; |
| 10499 | ctxt->warning = warn; |
| 10500 | ctxt->userData = ctx; |
| 10501 | } |
| 10502 | |
| 10503 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10504 | * xmlRelaxNGGetValidErrors: |
| 10505 | * @ctxt: a Relax-NG validation context |
| 10506 | * @err: the error function result |
| 10507 | * @warn: the warning function result |
| 10508 | * @ctx: the functions context result |
| 10509 | * |
| 10510 | * Get the error and warning callback informations |
| 10511 | * |
| 10512 | * Returns -1 in case of error and 0 otherwise |
| 10513 | */ |
| 10514 | int |
| 10515 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
| 10516 | xmlRelaxNGValidityErrorFunc *err, |
| 10517 | xmlRelaxNGValidityWarningFunc *warn, void **ctx) { |
| 10518 | if (ctxt == NULL) |
| 10519 | return(-1); |
| 10520 | if (err != NULL) *err = ctxt->error; |
| 10521 | if (warn != NULL) *warn = ctxt->warning; |
| 10522 | if (ctx != NULL) *ctx = ctxt->userData; |
| 10523 | return(0); |
| 10524 | } |
| 10525 | |
| 10526 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10527 | * xmlRelaxNGValidateDoc: |
| 10528 | * @ctxt: a Relax-NG validation context |
| 10529 | * @doc: a parsed document tree |
| 10530 | * |
| 10531 | * Validate a document tree in memory. |
| 10532 | * |
| 10533 | * Returns 0 if the document is valid, a positive error code |
| 10534 | * number otherwise and -1 in case of internal or API error. |
| 10535 | */ |
| 10536 | int |
| 10537 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 10538 | int ret; |
| 10539 | |
| 10540 | if ((ctxt == NULL) || (doc == NULL)) |
| 10541 | return(-1); |
| 10542 | |
| 10543 | ctxt->doc = doc; |
| 10544 | |
| 10545 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10546 | /* |
| 10547 | * TODO: build error codes |
| 10548 | */ |
| 10549 | if (ret == -1) |
| 10550 | return(1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10551 | return(ret); |
| 10552 | } |
| 10553 | |
| 10554 | #endif /* LIBXML_SCHEMAS_ENABLED */ |
| 10555 | |