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 | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 368 | }; |
| 369 | |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 370 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 371 | * xmlRelaxNGInclude: |
| 372 | * |
| 373 | * Structure associated to a RelaxNGs document element |
| 374 | */ |
| 375 | struct _xmlRelaxNGInclude { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 376 | xmlRelaxNGIncludePtr next; /* keep a chain of includes */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 377 | xmlChar *href; /* the normalized href value */ |
| 378 | xmlDocPtr doc; /* the associated XML document */ |
| 379 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 380 | xmlRelaxNGPtr schema; /* the schema */ |
| 381 | }; |
| 382 | |
| 383 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 384 | * xmlRelaxNGDocument: |
| 385 | * |
| 386 | * Structure associated to a RelaxNGs document element |
| 387 | */ |
| 388 | struct _xmlRelaxNGDocument { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 389 | xmlRelaxNGDocumentPtr next; /* keep a chain of documents */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 390 | xmlChar *href; /* the normalized href value */ |
| 391 | xmlDocPtr doc; /* the associated XML document */ |
| 392 | xmlRelaxNGDefinePtr content;/* the definitions */ |
| 393 | xmlRelaxNGPtr schema; /* the schema */ |
| 394 | }; |
| 395 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 396 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 397 | /************************************************************************ |
| 398 | * * |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 399 | * Preliminary type checking interfaces * |
| 400 | * * |
| 401 | ************************************************************************/ |
| 402 | /** |
| 403 | * xmlRelaxNGTypeHave: |
| 404 | * @data: data needed for the library |
| 405 | * @type: the type name |
| 406 | * @value: the value to check |
| 407 | * |
| 408 | * Function provided by a type library to check if a type is exported |
| 409 | * |
| 410 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 411 | */ |
| 412 | typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar *type); |
| 413 | |
| 414 | /** |
| 415 | * xmlRelaxNGTypeCheck: |
| 416 | * @data: data needed for the library |
| 417 | * @type: the type name |
| 418 | * @value: the value to check |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 419 | * @result: place to store the result if needed |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 420 | * |
| 421 | * Function provided by a type library to check if a value match a type |
| 422 | * |
| 423 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 424 | */ |
| 425 | typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar *type, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 426 | const xmlChar *value, void **result, |
| 427 | xmlNodePtr node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 428 | |
| 429 | /** |
| 430 | * xmlRelaxNGFacetCheck: |
| 431 | * @data: data needed for the library |
| 432 | * @type: the type name |
| 433 | * @facet: the facet name |
| 434 | * @val: the facet value |
| 435 | * @strval: the string value |
| 436 | * @value: the value to check |
| 437 | * |
| 438 | * Function provided by a type library to check a value facet |
| 439 | * |
| 440 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 441 | */ |
| 442 | typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar *type, |
| 443 | const xmlChar *facet, const xmlChar *val, |
| 444 | const xmlChar *strval, void *value); |
| 445 | |
| 446 | /** |
| 447 | * xmlRelaxNGTypeFree: |
| 448 | * @data: data needed for the library |
| 449 | * @result: the value to free |
| 450 | * |
| 451 | * Function provided by a type library to free a returned result |
| 452 | */ |
| 453 | typedef void (*xmlRelaxNGTypeFree) (void *data, void *result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 454 | |
| 455 | /** |
| 456 | * xmlRelaxNGTypeCompare: |
| 457 | * @data: data needed for the library |
| 458 | * @type: the type name |
| 459 | * @value1: the first value |
| 460 | * @value2: the second value |
| 461 | * |
| 462 | * Function provided by a type library to compare two values accordingly |
| 463 | * to a type. |
| 464 | * |
| 465 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 466 | */ |
| 467 | typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar *type, |
| 468 | const xmlChar *value1, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 469 | xmlNodePtr ctxt1, |
| 470 | void *comp1, |
| 471 | const xmlChar *value2, |
| 472 | xmlNodePtr ctxt2); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 473 | typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary; |
| 474 | typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr; |
| 475 | struct _xmlRelaxNGTypeLibrary { |
| 476 | const xmlChar *namespace; /* the datatypeLibrary value */ |
| 477 | void *data; /* data needed for the library */ |
| 478 | xmlRelaxNGTypeHave have; /* the export function */ |
| 479 | xmlRelaxNGTypeCheck check; /* the checking function */ |
| 480 | xmlRelaxNGTypeCompare comp; /* the compare function */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 481 | xmlRelaxNGFacetCheck facet; /* the facet check function */ |
| 482 | xmlRelaxNGTypeFree freef; /* the freeing function */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 483 | }; |
| 484 | |
| 485 | /************************************************************************ |
| 486 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 487 | * Allocation functions * |
| 488 | * * |
| 489 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 490 | static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar); |
| 491 | static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 492 | static void xmlRelaxNGNormExtSpace(xmlChar *value); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 493 | static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 494 | static int xmlRelaxNGEqualValidState( |
| 495 | xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 496 | xmlRelaxNGValidStatePtr state1, |
| 497 | xmlRelaxNGValidStatePtr state2); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 498 | static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 499 | xmlRelaxNGValidStatePtr state); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 500 | |
| 501 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 502 | * xmlRelaxNGFreeDocument: |
| 503 | * @docu: a document structure |
| 504 | * |
| 505 | * Deallocate a RelaxNG document structure. |
| 506 | */ |
| 507 | static void |
| 508 | xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu) |
| 509 | { |
| 510 | if (docu == NULL) |
| 511 | return; |
| 512 | |
| 513 | if (docu->href != NULL) |
| 514 | xmlFree(docu->href); |
| 515 | if (docu->doc != NULL) |
| 516 | xmlFreeDoc(docu->doc); |
| 517 | if (docu->schema != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 518 | xmlRelaxNGFreeInnerSchema(docu->schema); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 519 | xmlFree(docu); |
| 520 | } |
| 521 | |
| 522 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 523 | * xmlRelaxNGFreeDocumentList: |
| 524 | * @docu: a list of document structure |
| 525 | * |
| 526 | * Deallocate a RelaxNG document structures. |
| 527 | */ |
| 528 | static void |
| 529 | xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu) |
| 530 | { |
| 531 | xmlRelaxNGDocumentPtr next; |
| 532 | while (docu != NULL) { |
| 533 | next = docu->next; |
| 534 | xmlRelaxNGFreeDocument(docu); |
| 535 | docu = next; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 540 | * xmlRelaxNGFreeInclude: |
| 541 | * @incl: a include structure |
| 542 | * |
| 543 | * Deallocate a RelaxNG include structure. |
| 544 | */ |
| 545 | static void |
| 546 | xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl) |
| 547 | { |
| 548 | if (incl == NULL) |
| 549 | return; |
| 550 | |
| 551 | if (incl->href != NULL) |
| 552 | xmlFree(incl->href); |
| 553 | if (incl->doc != NULL) |
| 554 | xmlFreeDoc(incl->doc); |
| 555 | if (incl->schema != NULL) |
| 556 | xmlRelaxNGFree(incl->schema); |
| 557 | xmlFree(incl); |
| 558 | } |
| 559 | |
| 560 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 561 | * xmlRelaxNGFreeIncludeList: |
| 562 | * @incl: a include structure list |
| 563 | * |
| 564 | * Deallocate a RelaxNG include structure. |
| 565 | */ |
| 566 | static void |
| 567 | xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl) |
| 568 | { |
| 569 | xmlRelaxNGIncludePtr next; |
| 570 | while (incl != NULL) { |
| 571 | next = incl->next; |
| 572 | xmlRelaxNGFreeInclude(incl); |
| 573 | incl = next; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 578 | * xmlRelaxNGNewRelaxNG: |
| 579 | * @ctxt: a Relax-NG validation context (optional) |
| 580 | * |
| 581 | * Allocate a new RelaxNG structure. |
| 582 | * |
| 583 | * Returns the newly allocated structure or NULL in case or error |
| 584 | */ |
| 585 | static xmlRelaxNGPtr |
| 586 | xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt) |
| 587 | { |
| 588 | xmlRelaxNGPtr ret; |
| 589 | |
| 590 | ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG)); |
| 591 | if (ret == NULL) { |
| 592 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 593 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 594 | ctxt->nbErrors++; |
| 595 | return (NULL); |
| 596 | } |
| 597 | memset(ret, 0, sizeof(xmlRelaxNG)); |
| 598 | |
| 599 | return (ret); |
| 600 | } |
| 601 | |
| 602 | /** |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 603 | * xmlRelaxNGFreeInnerSchema: |
| 604 | * @schema: a schema structure |
| 605 | * |
| 606 | * Deallocate a RelaxNG schema structure. |
| 607 | */ |
| 608 | static void |
| 609 | xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema) |
| 610 | { |
| 611 | if (schema == NULL) |
| 612 | return; |
| 613 | |
| 614 | if (schema->doc != NULL) |
| 615 | xmlFreeDoc(schema->doc); |
| 616 | if (schema->defTab != NULL) { |
| 617 | int i; |
| 618 | |
| 619 | for (i = 0;i < schema->defNr;i++) |
| 620 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 621 | xmlFree(schema->defTab); |
| 622 | } |
| 623 | |
| 624 | xmlFree(schema); |
| 625 | } |
| 626 | |
| 627 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 628 | * xmlRelaxNGFree: |
| 629 | * @schema: a schema structure |
| 630 | * |
| 631 | * Deallocate a RelaxNG structure. |
| 632 | */ |
| 633 | void |
| 634 | xmlRelaxNGFree(xmlRelaxNGPtr schema) |
| 635 | { |
| 636 | if (schema == NULL) |
| 637 | return; |
| 638 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 639 | if (schema->topgrammar != NULL) |
| 640 | xmlRelaxNGFreeGrammar(schema->topgrammar); |
| 641 | if (schema->doc != NULL) |
| 642 | xmlFreeDoc(schema->doc); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 643 | if (schema->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 644 | xmlRelaxNGFreeDocumentList(schema->documents); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 645 | if (schema->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 646 | xmlRelaxNGFreeIncludeList(schema->includes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 647 | if (schema->defTab != NULL) { |
| 648 | int i; |
| 649 | |
| 650 | for (i = 0;i < schema->defNr;i++) |
| 651 | xmlRelaxNGFreeDefine(schema->defTab[i]); |
| 652 | xmlFree(schema->defTab); |
| 653 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 654 | |
| 655 | xmlFree(schema); |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * xmlRelaxNGNewGrammar: |
| 660 | * @ctxt: a Relax-NG validation context (optional) |
| 661 | * |
| 662 | * Allocate a new RelaxNG grammar. |
| 663 | * |
| 664 | * Returns the newly allocated structure or NULL in case or error |
| 665 | */ |
| 666 | static xmlRelaxNGGrammarPtr |
| 667 | xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt) |
| 668 | { |
| 669 | xmlRelaxNGGrammarPtr ret; |
| 670 | |
| 671 | ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar)); |
| 672 | if (ret == NULL) { |
| 673 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 674 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 675 | ctxt->nbErrors++; |
| 676 | return (NULL); |
| 677 | } |
| 678 | memset(ret, 0, sizeof(xmlRelaxNGGrammar)); |
| 679 | |
| 680 | return (ret); |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * xmlRelaxNGFreeGrammar: |
| 685 | * @grammar: a grammar structure |
| 686 | * |
| 687 | * Deallocate a RelaxNG grammar structure. |
| 688 | */ |
| 689 | static void |
| 690 | xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar) |
| 691 | { |
| 692 | if (grammar == NULL) |
| 693 | return; |
| 694 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 695 | if (grammar->children != NULL) { |
| 696 | xmlRelaxNGFreeGrammar(grammar->children); |
| 697 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 698 | if (grammar->next != NULL) { |
| 699 | xmlRelaxNGFreeGrammar(grammar->next); |
| 700 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 701 | if (grammar->refs != NULL) { |
| 702 | xmlHashFree(grammar->refs, NULL); |
| 703 | } |
| 704 | if (grammar->defs != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 705 | xmlHashFree(grammar->defs, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | xmlFree(grammar); |
| 709 | } |
| 710 | |
| 711 | /** |
| 712 | * xmlRelaxNGNewDefine: |
| 713 | * @ctxt: a Relax-NG validation context |
| 714 | * @node: the node in the input document. |
| 715 | * |
| 716 | * Allocate a new RelaxNG define. |
| 717 | * |
| 718 | * Returns the newly allocated structure or NULL in case or error |
| 719 | */ |
| 720 | static xmlRelaxNGDefinePtr |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 721 | xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 722 | { |
| 723 | xmlRelaxNGDefinePtr ret; |
| 724 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 725 | if (ctxt->defMax == 0) { |
| 726 | ctxt->defMax = 16; |
| 727 | ctxt->defNr = 0; |
| 728 | ctxt->defTab = (xmlRelaxNGDefinePtr *) |
| 729 | xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 730 | if (ctxt->defTab == NULL) { |
| 731 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 732 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 733 | ctxt->nbErrors++; |
| 734 | return (NULL); |
| 735 | } |
| 736 | } else if (ctxt->defMax <= ctxt->defNr) { |
| 737 | xmlRelaxNGDefinePtr *tmp; |
| 738 | ctxt->defMax *= 2; |
| 739 | tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab, |
| 740 | ctxt->defMax * sizeof(xmlRelaxNGDefinePtr)); |
| 741 | if (tmp == NULL) { |
| 742 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 743 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 744 | ctxt->nbErrors++; |
| 745 | return (NULL); |
| 746 | } |
| 747 | ctxt->defTab = tmp; |
| 748 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 749 | ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine)); |
| 750 | if (ret == NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 751 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 752 | ctxt->error(ctxt->userData, "Out of memory\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 753 | ctxt->nbErrors++; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 754 | return(NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 755 | } |
| 756 | memset(ret, 0, sizeof(xmlRelaxNGDefine)); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 757 | ctxt->defTab[ctxt->defNr++] = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 758 | ret->node = node; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 759 | ret->depth = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 760 | return (ret); |
| 761 | } |
| 762 | |
| 763 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 764 | * xmlRelaxNGFreePartition: |
| 765 | * @partitions: a partition set structure |
| 766 | * |
| 767 | * Deallocate RelaxNG partition set structures. |
| 768 | */ |
| 769 | static void |
| 770 | xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) { |
| 771 | xmlRelaxNGInterleaveGroupPtr group; |
| 772 | int j; |
| 773 | |
| 774 | if (partitions != NULL) { |
| 775 | if (partitions->groups != NULL) { |
| 776 | for (j = 0;j < partitions->nbgroups;j++) { |
| 777 | group = partitions->groups[j]; |
| 778 | if (group != NULL) { |
| 779 | if (group->defs != NULL) |
| 780 | xmlFree(group->defs); |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 781 | if (group->attrs != NULL) |
| 782 | xmlFree(group->attrs); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 783 | xmlFree(group); |
| 784 | } |
| 785 | } |
| 786 | xmlFree(partitions->groups); |
| 787 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 788 | if (partitions->triage != NULL) { |
| 789 | xmlHashFree(partitions->triage, NULL); |
| 790 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 791 | xmlFree(partitions); |
| 792 | } |
| 793 | } |
| 794 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 795 | * xmlRelaxNGFreeDefine: |
| 796 | * @define: a define structure |
| 797 | * |
| 798 | * Deallocate a RelaxNG define structure. |
| 799 | */ |
| 800 | static void |
| 801 | xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define) |
| 802 | { |
| 803 | if (define == NULL) |
| 804 | return; |
| 805 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 806 | if ((define->type == XML_RELAXNG_VALUE) && |
| 807 | (define->attrs != NULL)) { |
| 808 | xmlRelaxNGTypeLibraryPtr lib; |
| 809 | |
| 810 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
| 811 | if ((lib != NULL) && (lib->freef != NULL)) |
| 812 | lib->freef(lib->data, (void *) define->attrs); |
| 813 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 814 | if ((define->data != NULL) && |
| 815 | (define->type == XML_RELAXNG_INTERLEAVE)) |
| 816 | xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 817 | if ((define->data != NULL) && |
| 818 | (define->type == XML_RELAXNG_CHOICE)) |
| 819 | xmlHashFree((xmlHashTablePtr) define->data, NULL); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 820 | if (define->name != NULL) |
| 821 | xmlFree(define->name); |
| 822 | if (define->ns != NULL) |
| 823 | xmlFree(define->ns); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 824 | if (define->value != NULL) |
| 825 | xmlFree(define->value); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 826 | if (define->contModel != NULL) |
| 827 | xmlRegFreeRegexp(define->contModel); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 828 | xmlFree(define); |
| 829 | } |
| 830 | |
| 831 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 832 | * xmlRelaxNGNewStates: |
| 833 | * @ctxt: a Relax-NG validation context |
| 834 | * @size: the default size for the container |
| 835 | * |
| 836 | * Allocate a new RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 837 | * |
| 838 | * Returns the newly allocated structure or NULL in case or error |
| 839 | */ |
| 840 | static xmlRelaxNGStatesPtr |
| 841 | xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size) |
| 842 | { |
| 843 | xmlRelaxNGStatesPtr ret; |
| 844 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 845 | if ((ctxt != NULL) && |
| 846 | (ctxt->freeState != NULL) && |
| 847 | (ctxt->freeStatesNr > 0)) { |
| 848 | ctxt->freeStatesNr--; |
| 849 | ret = ctxt->freeStates[ctxt->freeStatesNr]; |
| 850 | ret->nbState = 0; |
| 851 | return(ret); |
| 852 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 853 | if (size < 16) size = 16; |
| 854 | |
| 855 | ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) + |
| 856 | (size - 1) * sizeof(xmlRelaxNGValidStatePtr)); |
| 857 | if (ret == NULL) { |
| 858 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 859 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 860 | return (NULL); |
| 861 | } |
| 862 | ret->nbState = 0; |
| 863 | ret->maxState = size; |
| 864 | ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc( |
| 865 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 866 | if (ret->tabState == NULL) { |
| 867 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 868 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 869 | xmlFree(ret->tabState); |
| 870 | return (NULL); |
| 871 | } |
| 872 | return(ret); |
| 873 | } |
| 874 | |
| 875 | /** |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 876 | * xmlRelaxNGAddStateUniq: |
| 877 | * @ctxt: a Relax-NG validation context |
| 878 | * @states: the states container |
| 879 | * @state: the validation state |
| 880 | * |
| 881 | * Add a RelaxNG validation state to the container without checking |
| 882 | * for unicity. |
| 883 | * |
| 884 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 885 | */ |
| 886 | static int |
| 887 | xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt, |
| 888 | xmlRelaxNGStatesPtr states, |
| 889 | xmlRelaxNGValidStatePtr state) |
| 890 | { |
| 891 | if (state == NULL) { |
| 892 | return(-1); |
| 893 | } |
| 894 | if (states->nbState >= states->maxState) { |
| 895 | xmlRelaxNGValidStatePtr *tmp; |
| 896 | int size; |
| 897 | |
| 898 | size = states->maxState * 2; |
| 899 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 900 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 901 | if (tmp == NULL) { |
| 902 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 903 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 904 | return(-1); |
| 905 | } |
| 906 | states->tabState = tmp; |
| 907 | states->maxState = size; |
| 908 | } |
| 909 | states->tabState[states->nbState++] = state; |
| 910 | return(1); |
| 911 | } |
| 912 | |
| 913 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 914 | * xmlRelaxNGAddState: |
| 915 | * @ctxt: a Relax-NG validation context |
| 916 | * @states: the states container |
| 917 | * @state: the validation state |
| 918 | * |
| 919 | * Add a RelaxNG validation state to the container |
| 920 | * |
| 921 | * Return 1 in case of success and 0 if this is a duplicate and -1 on error |
| 922 | */ |
| 923 | static int |
| 924 | xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGStatesPtr states, |
| 925 | xmlRelaxNGValidStatePtr state) |
| 926 | { |
| 927 | int i; |
| 928 | |
| 929 | if (state == NULL) { |
| 930 | return(-1); |
| 931 | } |
| 932 | if (states->nbState >= states->maxState) { |
| 933 | xmlRelaxNGValidStatePtr *tmp; |
| 934 | int size; |
| 935 | |
| 936 | size = states->maxState * 2; |
| 937 | tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState, |
| 938 | (size) * sizeof(xmlRelaxNGValidStatePtr)); |
| 939 | if (tmp == NULL) { |
| 940 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 941 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 942 | return(-1); |
| 943 | } |
| 944 | states->tabState = tmp; |
| 945 | states->maxState = size; |
| 946 | } |
| 947 | for (i = 0;i < states->nbState;i++) { |
| 948 | if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 949 | xmlRelaxNGFreeValidState(ctxt, state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 950 | return(0); |
| 951 | } |
| 952 | } |
| 953 | states->tabState[states->nbState++] = state; |
| 954 | return(1); |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * xmlRelaxNGFreeStates: |
| 959 | * @ctxt: a Relax-NG validation context |
| 960 | * @states: teh container |
| 961 | * |
| 962 | * Free a RelaxNG validation state container |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 963 | */ |
| 964 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 965 | xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 966 | xmlRelaxNGStatesPtr states) |
| 967 | { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 968 | if (states == NULL) |
| 969 | return; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 970 | if ((ctxt != NULL) && (ctxt->freeStates == NULL)) { |
| 971 | ctxt->freeStatesMax = 40; |
| 972 | ctxt->freeStatesNr = 0; |
| 973 | ctxt->freeStates = (xmlRelaxNGStatesPtr *) |
| 974 | xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 975 | if (ctxt->freeStates == NULL) { |
| 976 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 977 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 978 | } |
| 979 | } else if ((ctxt != NULL) && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) { |
| 980 | xmlRelaxNGStatesPtr *tmp; |
| 981 | |
| 982 | tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates, |
| 983 | 2 * ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr)); |
| 984 | if (tmp == NULL) { |
| 985 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 986 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 987 | xmlFree(states->tabState); |
| 988 | xmlFree(states); |
| 989 | return; |
| 990 | } |
| 991 | ctxt->freeStates = tmp; |
| 992 | ctxt->freeStatesMax *= 2; |
| 993 | } |
| 994 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 995 | xmlFree(states->tabState); |
| 996 | xmlFree(states); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 997 | } else { |
| 998 | ctxt->freeStates[ctxt->freeStatesNr++] = states; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1003 | * xmlRelaxNGNewValidState: |
| 1004 | * @ctxt: a Relax-NG validation context |
| 1005 | * @node: the current node or NULL for the document |
| 1006 | * |
| 1007 | * Allocate a new RelaxNG validation state |
| 1008 | * |
| 1009 | * Returns the newly allocated structure or NULL in case or error |
| 1010 | */ |
| 1011 | static xmlRelaxNGValidStatePtr |
| 1012 | xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node) |
| 1013 | { |
| 1014 | xmlRelaxNGValidStatePtr ret; |
| 1015 | xmlAttrPtr attr; |
| 1016 | xmlAttrPtr attrs[MAX_ATTR]; |
| 1017 | int nbAttrs = 0; |
| 1018 | xmlNodePtr root = NULL; |
| 1019 | |
| 1020 | if (node == NULL) { |
| 1021 | root = xmlDocGetRootElement(ctxt->doc); |
| 1022 | if (root == NULL) |
| 1023 | return(NULL); |
| 1024 | } else { |
| 1025 | attr = node->properties; |
| 1026 | while (attr != NULL) { |
| 1027 | if (nbAttrs < MAX_ATTR) |
| 1028 | attrs[nbAttrs++] = attr; |
| 1029 | else |
| 1030 | nbAttrs++; |
| 1031 | attr = attr->next; |
| 1032 | } |
| 1033 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1034 | if ((ctxt->freeState != NULL) && |
| 1035 | (ctxt->freeState->nbState > 0)) { |
| 1036 | ctxt->freeState->nbState--; |
| 1037 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1038 | } else { |
| 1039 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1040 | if (ret == NULL) { |
| 1041 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1042 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1043 | return (NULL); |
| 1044 | } |
| 1045 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1046 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 1047 | ret->value = NULL; |
| 1048 | ret->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1049 | if (node == NULL) { |
| 1050 | ret->node = (xmlNodePtr) ctxt->doc; |
| 1051 | ret->seq = root; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1052 | } else { |
| 1053 | ret->node = node; |
| 1054 | ret->seq = node->children; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1055 | } |
| 1056 | ret->nbAttrs = 0; |
| 1057 | if (nbAttrs > 0) { |
| 1058 | if (ret->attrs == NULL) { |
| 1059 | if (nbAttrs < 4) ret->maxAttrs = 4; |
| 1060 | else ret->maxAttrs = nbAttrs; |
| 1061 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1062 | sizeof(xmlAttrPtr)); |
| 1063 | if (ret->attrs == NULL) { |
| 1064 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1065 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1066 | return (ret); |
| 1067 | } |
| 1068 | } else if (ret->maxAttrs < nbAttrs) { |
| 1069 | xmlAttrPtr *tmp; |
| 1070 | |
| 1071 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs * |
| 1072 | sizeof(xmlAttrPtr)); |
| 1073 | if (tmp == NULL) { |
| 1074 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1075 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1076 | return (ret); |
| 1077 | } |
| 1078 | ret->attrs = tmp; |
| 1079 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1080 | ret->nbAttrs = nbAttrs; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1081 | if (nbAttrs < MAX_ATTR) { |
| 1082 | memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs); |
| 1083 | } else { |
| 1084 | attr = node->properties; |
| 1085 | nbAttrs = 0; |
| 1086 | while (attr != NULL) { |
| 1087 | ret->attrs[nbAttrs++] = attr; |
| 1088 | attr = attr->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | } |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 1092 | ret->nbAttrLeft = ret->nbAttrs; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1093 | return (ret); |
| 1094 | } |
| 1095 | |
| 1096 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1097 | * xmlRelaxNGCopyValidState: |
| 1098 | * @ctxt: a Relax-NG validation context |
| 1099 | * @state: a validation state |
| 1100 | * |
| 1101 | * Copy the validation state |
| 1102 | * |
| 1103 | * Returns the newly allocated structure or NULL in case or error |
| 1104 | */ |
| 1105 | static xmlRelaxNGValidStatePtr |
| 1106 | xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1107 | xmlRelaxNGValidStatePtr state) |
| 1108 | { |
| 1109 | xmlRelaxNGValidStatePtr ret; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1110 | unsigned int maxAttrs; |
| 1111 | xmlAttrPtr *attrs; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1112 | |
| 1113 | if (state == NULL) |
| 1114 | return(NULL); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1115 | if ((ctxt->freeState != NULL) && |
| 1116 | (ctxt->freeState->nbState > 0)) { |
| 1117 | ctxt->freeState->nbState--; |
| 1118 | ret = ctxt->freeState->tabState[ctxt->freeState->nbState]; |
| 1119 | } else { |
| 1120 | ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState)); |
| 1121 | if (ret == NULL) { |
| 1122 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1123 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1124 | return (NULL); |
| 1125 | } |
| 1126 | memset(ret, 0, sizeof(xmlRelaxNGValidState)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1127 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1128 | attrs = ret->attrs; |
| 1129 | maxAttrs = ret->maxAttrs; |
| 1130 | memcpy(ret, state, sizeof(xmlRelaxNGValidState)); |
| 1131 | ret->attrs = attrs; |
| 1132 | ret->maxAttrs = maxAttrs; |
| 1133 | if (state->nbAttrs > 0) { |
| 1134 | if (ret->attrs == NULL) { |
| 1135 | ret->maxAttrs = state->maxAttrs; |
| 1136 | ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs * |
| 1137 | sizeof(xmlAttrPtr)); |
| 1138 | if (ret->attrs == NULL) { |
| 1139 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1140 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1141 | ret->nbAttrs = 0; |
| 1142 | return (ret); |
| 1143 | } |
| 1144 | } else if (ret->maxAttrs < state->nbAttrs) { |
| 1145 | xmlAttrPtr *tmp; |
| 1146 | |
| 1147 | tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs * |
| 1148 | sizeof(xmlAttrPtr)); |
| 1149 | if (tmp == NULL) { |
| 1150 | if ((ctxt != NULL) && (ctxt->error != NULL)) |
| 1151 | ctxt->error(ctxt->userData, "Out of memory\n"); |
| 1152 | ret->nbAttrs = 0; |
| 1153 | return (ret); |
| 1154 | } |
| 1155 | ret->maxAttrs = state->maxAttrs; |
| 1156 | } |
| 1157 | memcpy(ret->attrs, state->attrs, state->nbAttrs * sizeof(xmlAttrPtr)); |
| 1158 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1159 | return(ret); |
| 1160 | } |
| 1161 | |
| 1162 | /** |
| 1163 | * xmlRelaxNGEqualValidState: |
| 1164 | * @ctxt: a Relax-NG validation context |
| 1165 | * @state1: a validation state |
| 1166 | * @state2: a validation state |
| 1167 | * |
| 1168 | * Compare the validation states for equality |
| 1169 | * |
| 1170 | * Returns 1 if equald, 0 otherwise |
| 1171 | */ |
| 1172 | static int |
| 1173 | xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 1174 | xmlRelaxNGValidStatePtr state1, |
| 1175 | xmlRelaxNGValidStatePtr state2) |
| 1176 | { |
| 1177 | int i; |
| 1178 | |
| 1179 | if ((state1 == NULL) || (state2 == NULL)) |
| 1180 | return(0); |
| 1181 | if (state1 == state2) |
| 1182 | return(1); |
| 1183 | if (state1->node != state2->node) |
| 1184 | return(0); |
| 1185 | if (state1->seq != state2->seq) |
| 1186 | return(0); |
| 1187 | if (state1->nbAttrLeft != state2->nbAttrLeft) |
| 1188 | return(0); |
| 1189 | if (state1->nbAttrs != state2->nbAttrs) |
| 1190 | return(0); |
| 1191 | if (state1->endvalue != state2->endvalue) |
| 1192 | return(0); |
| 1193 | if ((state1->value != state2->value) && |
| 1194 | (!xmlStrEqual(state1->value, state2->value))) |
| 1195 | return(0); |
| 1196 | for (i = 0;i < state1->nbAttrs;i++) { |
| 1197 | if (state1->attrs[i] != state2->attrs[i]) |
| 1198 | return(0); |
| 1199 | } |
| 1200 | return(1); |
| 1201 | } |
| 1202 | |
| 1203 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1204 | * xmlRelaxNGFreeValidState: |
| 1205 | * @state: a validation state structure |
| 1206 | * |
| 1207 | * Deallocate a RelaxNG validation state structure. |
| 1208 | */ |
| 1209 | static void |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1210 | xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt, |
| 1211 | xmlRelaxNGValidStatePtr state) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1212 | { |
| 1213 | if (state == NULL) |
| 1214 | return; |
| 1215 | |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 1216 | if ((ctxt != NULL) && (ctxt->freeState == NULL)) { |
| 1217 | ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40); |
| 1218 | } |
| 1219 | if ((ctxt == NULL) || (ctxt->freeState == NULL)) { |
| 1220 | if (state->attrs != NULL) |
| 1221 | xmlFree(state->attrs); |
| 1222 | xmlFree(state); |
| 1223 | } else { |
| 1224 | xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state); |
| 1225 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | /************************************************************************ |
| 1229 | * * |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1230 | * Document functions * |
| 1231 | * * |
| 1232 | ************************************************************************/ |
| 1233 | static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, |
| 1234 | xmlDocPtr doc); |
| 1235 | |
| 1236 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1237 | * xmlRelaxNGIncludePush: |
| 1238 | * @ctxt: the parser context |
| 1239 | * @value: the element doc |
| 1240 | * |
| 1241 | * Pushes a new include on top of the include stack |
| 1242 | * |
| 1243 | * Returns 0 in case of error, the index in the stack otherwise |
| 1244 | */ |
| 1245 | static int |
| 1246 | xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1247 | xmlRelaxNGIncludePtr value) |
| 1248 | { |
| 1249 | if (ctxt->incTab == NULL) { |
| 1250 | ctxt->incMax = 4; |
| 1251 | ctxt->incNr = 0; |
| 1252 | ctxt->incTab = (xmlRelaxNGIncludePtr *) xmlMalloc( |
| 1253 | ctxt->incMax * sizeof(ctxt->incTab[0])); |
| 1254 | if (ctxt->incTab == NULL) { |
| 1255 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1256 | return (0); |
| 1257 | } |
| 1258 | } |
| 1259 | if (ctxt->incNr >= ctxt->incMax) { |
| 1260 | ctxt->incMax *= 2; |
| 1261 | ctxt->incTab = |
| 1262 | (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab, |
| 1263 | ctxt->incMax * |
| 1264 | sizeof(ctxt->incTab[0])); |
| 1265 | if (ctxt->incTab == NULL) { |
| 1266 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1267 | return (0); |
| 1268 | } |
| 1269 | } |
| 1270 | ctxt->incTab[ctxt->incNr] = value; |
| 1271 | ctxt->inc = value; |
| 1272 | return (ctxt->incNr++); |
| 1273 | } |
| 1274 | |
| 1275 | /** |
| 1276 | * xmlRelaxNGIncludePop: |
| 1277 | * @ctxt: the parser context |
| 1278 | * |
| 1279 | * Pops the top include from the include stack |
| 1280 | * |
| 1281 | * Returns the include just removed |
| 1282 | */ |
| 1283 | static xmlRelaxNGIncludePtr |
| 1284 | xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1285 | { |
| 1286 | xmlRelaxNGIncludePtr ret; |
| 1287 | |
| 1288 | if (ctxt->incNr <= 0) |
| 1289 | return (0); |
| 1290 | ctxt->incNr--; |
| 1291 | if (ctxt->incNr > 0) |
| 1292 | ctxt->inc = ctxt->incTab[ctxt->incNr - 1]; |
| 1293 | else |
| 1294 | ctxt->inc = NULL; |
| 1295 | ret = ctxt->incTab[ctxt->incNr]; |
| 1296 | ctxt->incTab[ctxt->incNr] = 0; |
| 1297 | return (ret); |
| 1298 | } |
| 1299 | |
| 1300 | /** |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1301 | * xmlRelaxNGRemoveRedefine: |
| 1302 | * @ctxt: the parser context |
| 1303 | * @URL: the normalized URL |
| 1304 | * @target: the included target |
| 1305 | * @name: the define name to eliminate |
| 1306 | * |
| 1307 | * Applies the elimination algorithm of 4.7 |
| 1308 | * |
| 1309 | * Returns 0 in case of error, 1 in case of success. |
| 1310 | */ |
| 1311 | static int |
| 1312 | xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt, |
| 1313 | const xmlChar *URL ATTRIBUTE_UNUSED, |
| 1314 | xmlNodePtr target, const xmlChar *name) { |
| 1315 | int found = 0; |
| 1316 | xmlNodePtr tmp, tmp2; |
| 1317 | xmlChar *name2; |
| 1318 | |
| 1319 | #ifdef DEBUG_INCLUDE |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1320 | if (name == NULL) |
| 1321 | xmlGenericError(xmlGenericErrorContext, |
| 1322 | "Elimination of <include> start from %s\n", URL); |
| 1323 | else |
| 1324 | xmlGenericError(xmlGenericErrorContext, |
| 1325 | "Elimination of <include> define %s from %s\n", name, URL); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1326 | #endif |
| 1327 | tmp = target; |
| 1328 | while (tmp != NULL) { |
| 1329 | tmp2 = tmp->next; |
| 1330 | if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) { |
| 1331 | found = 1; |
| 1332 | xmlUnlinkNode(tmp); |
| 1333 | xmlFreeNode(tmp); |
| 1334 | } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) { |
| 1335 | name2 = xmlGetProp(tmp, BAD_CAST "name"); |
| 1336 | xmlRelaxNGNormExtSpace(name2); |
| 1337 | if (name2 != NULL) { |
| 1338 | if (xmlStrEqual(name, name2)) { |
| 1339 | found = 1; |
| 1340 | xmlUnlinkNode(tmp); |
| 1341 | xmlFreeNode(tmp); |
| 1342 | } |
| 1343 | xmlFree(name2); |
| 1344 | } |
| 1345 | } else if (IS_RELAXNG(tmp, "include")) { |
| 1346 | xmlChar *href = NULL; |
| 1347 | xmlRelaxNGDocumentPtr inc = tmp->_private; |
| 1348 | |
| 1349 | if ((inc != NULL) && (inc->doc != NULL) && |
| 1350 | (inc->doc->children != NULL)) { |
| 1351 | |
| 1352 | if (xmlStrEqual(inc->doc->children->name, BAD_CAST "grammar")) { |
| 1353 | #ifdef DEBUG_INCLUDE |
| 1354 | href = xmlGetProp(tmp, BAD_CAST "href"); |
| 1355 | #endif |
| 1356 | if (xmlRelaxNGRemoveRedefine(ctxt, href, |
| 1357 | inc->doc->children->children, name) == 1) { |
| 1358 | found = 1; |
| 1359 | } |
| 1360 | if (href != NULL) |
| 1361 | xmlFree(href); |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | tmp = tmp2; |
| 1366 | } |
| 1367 | return(found); |
| 1368 | } |
| 1369 | |
| 1370 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1371 | * xmlRelaxNGLoadInclude: |
| 1372 | * @ctxt: the parser context |
| 1373 | * @URL: the normalized URL |
| 1374 | * @node: the include node. |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1375 | * @ns: the namespace passed from the context. |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1376 | * |
| 1377 | * First lookup if the document is already loaded into the parser context, |
| 1378 | * check against recursion. If not found the resource is loaded and |
| 1379 | * the content is preprocessed before being returned back to the caller. |
| 1380 | * |
| 1381 | * Returns the xmlRelaxNGIncludePtr or NULL in case of error |
| 1382 | */ |
| 1383 | static xmlRelaxNGIncludePtr |
| 1384 | xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1385 | xmlNodePtr node, const xmlChar *ns) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1386 | xmlRelaxNGIncludePtr ret = NULL; |
| 1387 | xmlDocPtr doc; |
| 1388 | int i; |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1389 | xmlNodePtr root, cur; |
| 1390 | |
| 1391 | #ifdef DEBUG_INCLUDE |
| 1392 | xmlGenericError(xmlGenericErrorContext, |
| 1393 | "xmlRelaxNGLoadInclude(%s)\n", URL); |
| 1394 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1395 | |
| 1396 | /* |
| 1397 | * check against recursion in the stack |
| 1398 | */ |
| 1399 | for (i = 0;i < ctxt->incNr;i++) { |
| 1400 | if (xmlStrEqual(ctxt->incTab[i]->href, URL)) { |
| 1401 | if (ctxt->error != NULL) |
| 1402 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1403 | "Detected an Include recursion for %s\n", |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1404 | URL); |
| 1405 | ctxt->nbErrors++; |
| 1406 | return(NULL); |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | /* |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1411 | * load the document |
| 1412 | */ |
| 1413 | doc = xmlParseFile((const char *) URL); |
| 1414 | if (doc == NULL) { |
| 1415 | if (ctxt->error != NULL) |
| 1416 | ctxt->error(ctxt->userData, |
| 1417 | "xmlRelaxNG: could not load %s\n", URL); |
| 1418 | ctxt->nbErrors++; |
| 1419 | return (NULL); |
| 1420 | } |
| 1421 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1422 | #ifdef DEBUG_INCLUDE |
| 1423 | xmlGenericError(xmlGenericErrorContext, |
| 1424 | "Parsed %s Okay\n", URL); |
| 1425 | #endif |
| 1426 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1427 | /* |
| 1428 | * Allocate the document structures and register it first. |
| 1429 | */ |
| 1430 | ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude)); |
| 1431 | if (ret == NULL) { |
| 1432 | if (ctxt->error != NULL) |
| 1433 | ctxt->error(ctxt->userData, |
| 1434 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1435 | ctxt->nbErrors++; |
| 1436 | xmlFreeDoc(doc); |
| 1437 | return (NULL); |
| 1438 | } |
| 1439 | memset(ret, 0, sizeof(xmlRelaxNGInclude)); |
| 1440 | ret->doc = doc; |
| 1441 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1442 | ret->next = ctxt->includes; |
| 1443 | ctxt->includes = ret; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1444 | |
| 1445 | /* |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 1446 | * transmit the ns if needed |
| 1447 | */ |
| 1448 | if (ns != NULL) { |
| 1449 | root = xmlDocGetRootElement(doc); |
| 1450 | if (root != NULL) { |
| 1451 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1452 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | /* |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1458 | * push it on the stack |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1459 | */ |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1460 | xmlRelaxNGIncludePush(ctxt, ret); |
| 1461 | |
| 1462 | /* |
| 1463 | * Some preprocessing of the document content, this include recursing |
| 1464 | * in the include stack. |
| 1465 | */ |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1466 | #ifdef DEBUG_INCLUDE |
| 1467 | xmlGenericError(xmlGenericErrorContext, |
| 1468 | "cleanup of %s\n", URL); |
| 1469 | #endif |
| 1470 | |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1471 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1472 | if (doc == NULL) { |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1473 | ctxt->inc = NULL; |
| 1474 | return(NULL); |
| 1475 | } |
| 1476 | |
| 1477 | /* |
| 1478 | * Pop up the include from the stack |
| 1479 | */ |
| 1480 | xmlRelaxNGIncludePop(ctxt); |
| 1481 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1482 | #ifdef DEBUG_INCLUDE |
| 1483 | xmlGenericError(xmlGenericErrorContext, |
| 1484 | "Checking of %s\n", URL); |
| 1485 | #endif |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1486 | /* |
| 1487 | * Check that the top element is a grammar |
| 1488 | */ |
| 1489 | root = xmlDocGetRootElement(doc); |
| 1490 | if (root == NULL) { |
| 1491 | if (ctxt->error != NULL) |
| 1492 | ctxt->error(ctxt->userData, |
| 1493 | "xmlRelaxNG: included document is empty %s\n", URL); |
| 1494 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1495 | return (NULL); |
| 1496 | } |
| 1497 | if (!IS_RELAXNG(root, "grammar")) { |
| 1498 | if (ctxt->error != NULL) |
| 1499 | ctxt->error(ctxt->userData, |
| 1500 | "xmlRelaxNG: included document %s root is not a grammar\n", |
| 1501 | URL); |
| 1502 | ctxt->nbErrors++; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1503 | return (NULL); |
| 1504 | } |
| 1505 | |
| 1506 | /* |
| 1507 | * Elimination of redefined rules in the include. |
| 1508 | */ |
| 1509 | cur = node->children; |
| 1510 | while (cur != NULL) { |
| 1511 | if (IS_RELAXNG(cur, "start")) { |
| 1512 | int found = 0; |
| 1513 | |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1514 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1515 | if (!found) { |
| 1516 | if (ctxt->error != NULL) |
| 1517 | ctxt->error(ctxt->userData, |
| 1518 | "xmlRelaxNG: include %s has a start but not the included grammar\n", |
| 1519 | URL); |
| 1520 | ctxt->nbErrors++; |
| 1521 | } |
| 1522 | } else if (IS_RELAXNG(cur, "define")) { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1523 | xmlChar *name; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1524 | |
| 1525 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 1526 | if (name == NULL) { |
| 1527 | if (ctxt->error != NULL) |
| 1528 | ctxt->error(ctxt->userData, |
| 1529 | "xmlRelaxNG: include %s has define without name\n", |
| 1530 | URL); |
| 1531 | ctxt->nbErrors++; |
| 1532 | } else { |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1533 | int found; |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1534 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1535 | xmlRelaxNGNormExtSpace(name); |
Daniel Veillard | 5add868 | 2003-03-10 13:13:58 +0000 | [diff] [blame] | 1536 | found = xmlRelaxNGRemoveRedefine(ctxt, URL, |
| 1537 | root->children, name); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1538 | if (!found) { |
| 1539 | if (ctxt->error != NULL) |
| 1540 | ctxt->error(ctxt->userData, |
| 1541 | "xmlRelaxNG: include %s has a define %s but not the included grammar\n", |
| 1542 | URL, name); |
| 1543 | ctxt->nbErrors++; |
| 1544 | } |
| 1545 | xmlFree(name); |
| 1546 | } |
| 1547 | } |
| 1548 | cur = cur->next; |
| 1549 | } |
| 1550 | |
| 1551 | |
| 1552 | return(ret); |
| 1553 | } |
| 1554 | |
| 1555 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1556 | * xmlRelaxNGValidErrorPush: |
| 1557 | * @ctxt: the validation context |
| 1558 | * @err: the error code |
| 1559 | * @arg1: the first string argument |
| 1560 | * @arg2: the second string argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1561 | * @dup: arg need to be duplicated |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1562 | * |
| 1563 | * Pushes a new error on top of the error stack |
| 1564 | * |
| 1565 | * Returns 0 in case of error, the index in the stack otherwise |
| 1566 | */ |
| 1567 | static int |
| 1568 | xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1569 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1570 | { |
| 1571 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 1572 | #ifdef DEBUG_ERROR |
| 1573 | xmlGenericError(xmlGenericErrorContext, |
| 1574 | "Pushing error %d at %d on stack\n", err, ctxt->errNr); |
| 1575 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1576 | if (ctxt->errTab == NULL) { |
| 1577 | ctxt->errMax = 8; |
| 1578 | ctxt->errNr = 0; |
| 1579 | ctxt->errTab = (xmlRelaxNGValidErrorPtr) xmlMalloc( |
| 1580 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
| 1581 | if (ctxt->errTab == NULL) { |
| 1582 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1583 | return (0); |
| 1584 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1585 | ctxt->err = NULL; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1586 | } |
| 1587 | if (ctxt->errNr >= ctxt->errMax) { |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1588 | ctxt->errMax *= 2; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1589 | ctxt->errTab = |
| 1590 | (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab, |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1591 | ctxt->errMax * sizeof(xmlRelaxNGValidError)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1592 | if (ctxt->errTab == NULL) { |
| 1593 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1594 | return (0); |
| 1595 | } |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 1596 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1597 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1598 | if ((ctxt->err != NULL) && (ctxt->state != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1599 | (ctxt->err->node == ctxt->state->node) && |
| 1600 | (ctxt->err->err == err)) |
| 1601 | return(ctxt->errNr); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1602 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1603 | cur->err = err; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1604 | if (dup) { |
| 1605 | cur->arg1 = xmlStrdup(arg1); |
| 1606 | cur->arg2 = xmlStrdup(arg2); |
| 1607 | cur->flags = ERROR_IS_DUP; |
| 1608 | } else { |
| 1609 | cur->arg1 = arg1; |
| 1610 | cur->arg2 = arg2; |
| 1611 | cur->flags = 0; |
| 1612 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1613 | if (ctxt->state != NULL) { |
| 1614 | cur->node = ctxt->state->node; |
| 1615 | cur->seq = ctxt->state->seq; |
| 1616 | } else { |
| 1617 | cur->node = NULL; |
| 1618 | cur->seq = NULL; |
| 1619 | } |
| 1620 | ctxt->err = cur; |
| 1621 | return (ctxt->errNr++); |
| 1622 | } |
| 1623 | |
| 1624 | /** |
| 1625 | * xmlRelaxNGValidErrorPop: |
| 1626 | * @ctxt: the validation context |
| 1627 | * |
| 1628 | * Pops the top error from the error stack |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1629 | */ |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1630 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1631 | xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt) |
| 1632 | { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1633 | xmlRelaxNGValidErrorPtr cur; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1634 | |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1635 | if (ctxt->errNr <= 0) { |
| 1636 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1637 | return; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1638 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1639 | ctxt->errNr--; |
| 1640 | if (ctxt->errNr > 0) |
| 1641 | ctxt->err = &ctxt->errTab[ctxt->errNr - 1]; |
| 1642 | else |
| 1643 | ctxt->err = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1644 | cur = &ctxt->errTab[ctxt->errNr]; |
| 1645 | if (cur->flags & ERROR_IS_DUP) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1646 | if (cur->arg1 != NULL) |
| 1647 | xmlFree((xmlChar *)cur->arg1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1648 | cur->arg1 = NULL; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 1649 | if (cur->arg2 != NULL) |
| 1650 | xmlFree((xmlChar *)cur->arg2); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1651 | cur->arg2 = NULL; |
| 1652 | cur->flags = 0; |
| 1653 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1656 | /** |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1657 | * xmlRelaxNGDocumentPush: |
| 1658 | * @ctxt: the parser context |
| 1659 | * @value: the element doc |
| 1660 | * |
| 1661 | * Pushes a new doc on top of the doc stack |
| 1662 | * |
| 1663 | * Returns 0 in case of error, the index in the stack otherwise |
| 1664 | */ |
| 1665 | static int |
| 1666 | xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt, |
| 1667 | xmlRelaxNGDocumentPtr value) |
| 1668 | { |
| 1669 | if (ctxt->docTab == NULL) { |
| 1670 | ctxt->docMax = 4; |
| 1671 | ctxt->docNr = 0; |
| 1672 | ctxt->docTab = (xmlRelaxNGDocumentPtr *) xmlMalloc( |
| 1673 | ctxt->docMax * sizeof(ctxt->docTab[0])); |
| 1674 | if (ctxt->docTab == NULL) { |
| 1675 | xmlGenericError(xmlGenericErrorContext, "malloc failed !\n"); |
| 1676 | return (0); |
| 1677 | } |
| 1678 | } |
| 1679 | if (ctxt->docNr >= ctxt->docMax) { |
| 1680 | ctxt->docMax *= 2; |
| 1681 | ctxt->docTab = |
| 1682 | (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab, |
| 1683 | ctxt->docMax * |
| 1684 | sizeof(ctxt->docTab[0])); |
| 1685 | if (ctxt->docTab == NULL) { |
| 1686 | xmlGenericError(xmlGenericErrorContext, "realloc failed !\n"); |
| 1687 | return (0); |
| 1688 | } |
| 1689 | } |
| 1690 | ctxt->docTab[ctxt->docNr] = value; |
| 1691 | ctxt->doc = value; |
| 1692 | return (ctxt->docNr++); |
| 1693 | } |
| 1694 | |
| 1695 | /** |
| 1696 | * xmlRelaxNGDocumentPop: |
| 1697 | * @ctxt: the parser context |
| 1698 | * |
| 1699 | * Pops the top doc from the doc stack |
| 1700 | * |
| 1701 | * Returns the doc just removed |
| 1702 | */ |
| 1703 | static xmlRelaxNGDocumentPtr |
| 1704 | xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt) |
| 1705 | { |
| 1706 | xmlRelaxNGDocumentPtr ret; |
| 1707 | |
| 1708 | if (ctxt->docNr <= 0) |
| 1709 | return (0); |
| 1710 | ctxt->docNr--; |
| 1711 | if (ctxt->docNr > 0) |
| 1712 | ctxt->doc = ctxt->docTab[ctxt->docNr - 1]; |
| 1713 | else |
| 1714 | ctxt->doc = NULL; |
| 1715 | ret = ctxt->docTab[ctxt->docNr]; |
| 1716 | ctxt->docTab[ctxt->docNr] = 0; |
| 1717 | return (ret); |
| 1718 | } |
| 1719 | |
| 1720 | /** |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1721 | * xmlRelaxNGLoadExternalRef: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1722 | * @ctxt: the parser context |
| 1723 | * @URL: the normalized URL |
| 1724 | * @ns: the inherited ns if any |
| 1725 | * |
| 1726 | * First lookup if the document is already loaded into the parser context, |
| 1727 | * check against recursion. If not found the resource is loaded and |
| 1728 | * the content is preprocessed before being returned back to the caller. |
| 1729 | * |
| 1730 | * Returns the xmlRelaxNGDocumentPtr or NULL in case of error |
| 1731 | */ |
| 1732 | static xmlRelaxNGDocumentPtr |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 1733 | xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL, |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1734 | const xmlChar *ns) { |
| 1735 | xmlRelaxNGDocumentPtr ret = NULL; |
| 1736 | xmlDocPtr doc; |
| 1737 | xmlNodePtr root; |
| 1738 | int i; |
| 1739 | |
| 1740 | /* |
| 1741 | * check against recursion in the stack |
| 1742 | */ |
| 1743 | for (i = 0;i < ctxt->docNr;i++) { |
| 1744 | if (xmlStrEqual(ctxt->docTab[i]->href, URL)) { |
| 1745 | if (ctxt->error != NULL) |
| 1746 | ctxt->error(ctxt->userData, |
| 1747 | "Detected an externalRef recursion for %s\n", |
| 1748 | URL); |
| 1749 | ctxt->nbErrors++; |
| 1750 | return(NULL); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | /* |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1755 | * load the document |
| 1756 | */ |
| 1757 | doc = xmlParseFile((const char *) URL); |
| 1758 | if (doc == NULL) { |
| 1759 | if (ctxt->error != NULL) |
| 1760 | ctxt->error(ctxt->userData, |
| 1761 | "xmlRelaxNG: could not load %s\n", URL); |
| 1762 | ctxt->nbErrors++; |
| 1763 | return (NULL); |
| 1764 | } |
| 1765 | |
| 1766 | /* |
| 1767 | * Allocate the document structures and register it first. |
| 1768 | */ |
| 1769 | ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument)); |
| 1770 | if (ret == NULL) { |
| 1771 | if (ctxt->error != NULL) |
| 1772 | ctxt->error(ctxt->userData, |
| 1773 | "xmlRelaxNG: allocate memory for doc %s\n", URL); |
| 1774 | ctxt->nbErrors++; |
| 1775 | xmlFreeDoc(doc); |
| 1776 | return (NULL); |
| 1777 | } |
| 1778 | memset(ret, 0, sizeof(xmlRelaxNGDocument)); |
| 1779 | ret->doc = doc; |
| 1780 | ret->href = xmlStrdup(URL); |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 1781 | ret->next = ctxt->documents; |
| 1782 | ctxt->documents = ret; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1783 | |
| 1784 | /* |
| 1785 | * transmit the ns if needed |
| 1786 | */ |
| 1787 | if (ns != NULL) { |
| 1788 | root = xmlDocGetRootElement(doc); |
| 1789 | if (root != NULL) { |
| 1790 | if (xmlHasProp(root, BAD_CAST"ns") == NULL) { |
| 1791 | xmlSetProp(root, BAD_CAST"ns", ns); |
| 1792 | } |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | /* |
| 1797 | * push it on the stack and register it in the hash table |
| 1798 | */ |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1799 | xmlRelaxNGDocumentPush(ctxt, ret); |
| 1800 | |
| 1801 | /* |
| 1802 | * Some preprocessing of the document content |
| 1803 | */ |
| 1804 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 1805 | if (doc == NULL) { |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 1806 | ctxt->doc = NULL; |
| 1807 | return(NULL); |
| 1808 | } |
| 1809 | |
| 1810 | xmlRelaxNGDocumentPop(ctxt); |
| 1811 | |
| 1812 | return(ret); |
| 1813 | } |
| 1814 | |
| 1815 | /************************************************************************ |
| 1816 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1817 | * Error functions * |
| 1818 | * * |
| 1819 | ************************************************************************/ |
| 1820 | |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1821 | #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0); |
| 1822 | #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0); |
| 1823 | #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0); |
| 1824 | #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1); |
| 1825 | #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1826 | |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1827 | static const char * |
| 1828 | xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) { |
| 1829 | if (def == NULL) |
| 1830 | return("none"); |
| 1831 | switch(def->type) { |
| 1832 | case XML_RELAXNG_EMPTY: return("empty"); |
| 1833 | case XML_RELAXNG_NOT_ALLOWED: return("notAllowed"); |
| 1834 | case XML_RELAXNG_EXCEPT: return("except"); |
| 1835 | case XML_RELAXNG_TEXT: return("text"); |
| 1836 | case XML_RELAXNG_ELEMENT: return("element"); |
| 1837 | case XML_RELAXNG_DATATYPE: return("datatype"); |
| 1838 | case XML_RELAXNG_VALUE: return("value"); |
| 1839 | case XML_RELAXNG_LIST: return("list"); |
| 1840 | case XML_RELAXNG_ATTRIBUTE: return("attribute"); |
| 1841 | case XML_RELAXNG_DEF: return("def"); |
| 1842 | case XML_RELAXNG_REF: return("ref"); |
| 1843 | case XML_RELAXNG_EXTERNALREF: return("externalRef"); |
| 1844 | case XML_RELAXNG_PARENTREF: return("parentRef"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1845 | case XML_RELAXNG_OPTIONAL: return("optional"); |
| 1846 | case XML_RELAXNG_ZEROORMORE: return("zeroOrMore"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1847 | case XML_RELAXNG_ONEORMORE: return("oneOrMore"); |
| 1848 | case XML_RELAXNG_CHOICE: return("choice"); |
| 1849 | case XML_RELAXNG_GROUP: return("group"); |
| 1850 | case XML_RELAXNG_INTERLEAVE: return("interleave"); |
| 1851 | case XML_RELAXNG_START: return("start"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1852 | case XML_RELAXNG_NOOP: return("noop"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 1853 | case XML_RELAXNG_PARAM: return("param"); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 1854 | } |
| 1855 | return("unknown"); |
| 1856 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1857 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1858 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1859 | * xmlRelaxNGGetErrorString: |
| 1860 | * @err: the error code |
| 1861 | * @arg1: the first string argument |
| 1862 | * @arg2: the second string argument |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 1863 | * |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1864 | * computes a formatted error string for the given error code and args |
| 1865 | * |
| 1866 | * Returns the error string, it must be deallocated by the caller |
| 1867 | */ |
| 1868 | static xmlChar * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1869 | xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar *arg1, |
| 1870 | const xmlChar *arg2) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1871 | char msg[1000]; |
| 1872 | |
| 1873 | if (arg1 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1874 | arg1 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1875 | if (arg2 == NULL) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1876 | arg2 = BAD_CAST ""; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1877 | |
| 1878 | msg[0] = 0; |
| 1879 | switch (err) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1880 | case XML_RELAXNG_OK: |
| 1881 | return(NULL); |
| 1882 | case XML_RELAXNG_ERR_MEMORY: |
| 1883 | return(xmlCharStrdup("out of memory")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1884 | case XML_RELAXNG_ERR_TYPE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1885 | snprintf(msg, 1000, "failed to validate type %s", arg1); |
| 1886 | break; |
| 1887 | case XML_RELAXNG_ERR_TYPEVAL: |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 1888 | snprintf(msg, 1000, "Type %s doesn't allow value '%s'", arg1, arg2); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1889 | break; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 1890 | case XML_RELAXNG_ERR_DUPID: |
| 1891 | snprintf(msg, 1000, "ID %s redefined", arg1); |
| 1892 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1893 | case XML_RELAXNG_ERR_TYPECMP: |
| 1894 | snprintf(msg, 1000, "failed to compare type %s", arg1); |
| 1895 | break; |
| 1896 | case XML_RELAXNG_ERR_NOSTATE: |
| 1897 | return(xmlCharStrdup("Internal error: no state")); |
| 1898 | case XML_RELAXNG_ERR_NODEFINE: |
| 1899 | return(xmlCharStrdup("Internal error: no define")); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 1900 | case XML_RELAXNG_ERR_INTERNAL: |
| 1901 | snprintf(msg, 1000, "Internal error: %s", arg1); |
| 1902 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1903 | case XML_RELAXNG_ERR_LISTEXTRA: |
| 1904 | snprintf(msg, 1000, "Extra data in list: %s", arg1); |
| 1905 | break; |
| 1906 | case XML_RELAXNG_ERR_INTERNODATA: |
| 1907 | return(xmlCharStrdup("Internal: interleave block has no data")); |
| 1908 | case XML_RELAXNG_ERR_INTERSEQ: |
| 1909 | return(xmlCharStrdup("Invalid sequence in interleave")); |
| 1910 | case XML_RELAXNG_ERR_INTEREXTRA: |
| 1911 | snprintf(msg, 1000, "Extra element %s in interleave", arg1); |
| 1912 | break; |
| 1913 | case XML_RELAXNG_ERR_ELEMNAME: |
| 1914 | snprintf(msg, 1000, "Expecting element %s, got %s", arg1, arg2); |
| 1915 | break; |
| 1916 | case XML_RELAXNG_ERR_ELEMNONS: |
| 1917 | snprintf(msg, 1000, "Expecting a namespace for element %s", arg1); |
| 1918 | break; |
| 1919 | case XML_RELAXNG_ERR_ELEMWRONGNS: |
| 1920 | snprintf(msg, 1000, "Element %s has wrong namespace: expecting %s", |
| 1921 | arg1, arg2); |
| 1922 | break; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 1923 | case XML_RELAXNG_ERR_ELEMWRONG: |
| 1924 | snprintf(msg, 1000, "Did not expect element %s there", |
| 1925 | arg1); |
| 1926 | break; |
| 1927 | case XML_RELAXNG_ERR_TEXTWRONG: |
| 1928 | snprintf(msg, 1000, "Did not expect text in element %s content", |
| 1929 | arg1); |
| 1930 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1931 | case XML_RELAXNG_ERR_ELEMEXTRANS: |
| 1932 | snprintf(msg, 1000, "Expecting no namespace for element %s", arg1); |
| 1933 | break; |
| 1934 | case XML_RELAXNG_ERR_ELEMNOTEMPTY: |
| 1935 | snprintf(msg, 1000, "Expecting element %s to be empty", arg1); |
| 1936 | break; |
| 1937 | case XML_RELAXNG_ERR_NOELEM: |
| 1938 | snprintf(msg, 1000, "Expecting an element %s, got nothing", arg1); |
| 1939 | break; |
| 1940 | case XML_RELAXNG_ERR_NOTELEM: |
| 1941 | return(xmlCharStrdup("Expecting an element got text")); |
| 1942 | case XML_RELAXNG_ERR_ATTRVALID: |
| 1943 | snprintf(msg, 1000, "Element %s failed to validate attributes", |
| 1944 | arg1); |
| 1945 | break; |
| 1946 | case XML_RELAXNG_ERR_CONTENTVALID: |
| 1947 | snprintf(msg, 1000, "Element %s failed to validate content", |
| 1948 | arg1); |
| 1949 | break; |
| 1950 | case XML_RELAXNG_ERR_EXTRACONTENT: |
| 1951 | snprintf(msg, 1000, "Element %s has extra content: %s", |
| 1952 | arg1, arg2); |
| 1953 | break; |
| 1954 | case XML_RELAXNG_ERR_INVALIDATTR: |
| 1955 | snprintf(msg, 1000, "Invalid attribute %s for element %s", |
| 1956 | arg1, arg2); |
| 1957 | break; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 1958 | case XML_RELAXNG_ERR_LACKDATA: |
| 1959 | snprintf(msg, 1000, "Datatype element %s contains no data", |
| 1960 | arg1); |
| 1961 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1962 | case XML_RELAXNG_ERR_DATAELEM: |
| 1963 | snprintf(msg, 1000, "Datatype element %s has child elements", |
| 1964 | arg1); |
| 1965 | break; |
| 1966 | case XML_RELAXNG_ERR_VALELEM: |
| 1967 | snprintf(msg, 1000, "Value element %s has child elements", |
| 1968 | arg1); |
| 1969 | break; |
| 1970 | case XML_RELAXNG_ERR_LISTELEM: |
| 1971 | snprintf(msg, 1000, "List element %s has child elements", |
| 1972 | arg1); |
| 1973 | break; |
| 1974 | case XML_RELAXNG_ERR_DATATYPE: |
| 1975 | snprintf(msg, 1000, "Error validating datatype %s", |
| 1976 | arg1); |
| 1977 | break; |
| 1978 | case XML_RELAXNG_ERR_VALUE: |
| 1979 | snprintf(msg, 1000, "Error validating value %s", |
| 1980 | arg1); |
| 1981 | break; |
| 1982 | case XML_RELAXNG_ERR_LIST: |
| 1983 | return(xmlCharStrdup("Error validating list")); |
| 1984 | case XML_RELAXNG_ERR_NOGRAMMAR: |
| 1985 | return(xmlCharStrdup("No top grammar defined")); |
| 1986 | case XML_RELAXNG_ERR_EXTRADATA: |
| 1987 | return(xmlCharStrdup("Extra data in the document")); |
| 1988 | default: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 1989 | return(xmlCharStrdup("Unknown error !")); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1990 | } |
| 1991 | if (msg[0] == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1992 | snprintf(msg, 1000, "Unknown error code %d", err); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1993 | } |
Daniel Veillard | adbb0e6 | 2003-05-10 20:02:45 +0000 | [diff] [blame] | 1994 | msg[1000 - 1] = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 1995 | return(xmlStrdup((xmlChar *) msg)); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | /** |
| 1999 | * xmlRelaxNGValidErrorContext: |
| 2000 | * @ctxt: the validation context |
| 2001 | * @node: the node |
| 2002 | * @child: the node child generating the problem. |
| 2003 | * |
| 2004 | * Dump informations about the kocation of the error in the instance |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2005 | */ |
| 2006 | static void |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2007 | xmlRelaxNGValidErrorContext(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, |
| 2008 | xmlNodePtr child) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2009 | { |
| 2010 | int line = 0; |
| 2011 | const xmlChar *file = NULL; |
| 2012 | const xmlChar *name = NULL; |
| 2013 | const char *type = "error"; |
| 2014 | |
| 2015 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 2016 | return; |
| 2017 | |
| 2018 | if (child != NULL) |
| 2019 | node = child; |
| 2020 | |
| 2021 | if (node != NULL) { |
| 2022 | if ((node->type == XML_DOCUMENT_NODE) || |
| 2023 | (node->type == XML_HTML_DOCUMENT_NODE)) { |
| 2024 | xmlDocPtr doc = (xmlDocPtr) node; |
| 2025 | |
| 2026 | file = doc->URL; |
| 2027 | } else { |
| 2028 | /* |
| 2029 | * Try to find contextual informations to report |
| 2030 | */ |
| 2031 | if (node->type == XML_ELEMENT_NODE) { |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 2032 | line = (long) node->content; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2033 | } else if ((node->prev != NULL) && |
| 2034 | (node->prev->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 2035 | line = (long) node->prev->content; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2036 | } else if ((node->parent != NULL) && |
| 2037 | (node->parent->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 2038 | line = (long) node->parent->content; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2039 | } |
| 2040 | if ((node->doc != NULL) && (node->doc->URL != NULL)) |
| 2041 | file = node->doc->URL; |
| 2042 | if (node->name != NULL) |
| 2043 | name = node->name; |
| 2044 | } |
| 2045 | } |
| 2046 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2047 | type = "RNG validity error"; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2048 | |
| 2049 | if ((file != NULL) && (line != 0) && (name != NULL)) |
| 2050 | ctxt->error(ctxt->userData, "%s: file %s line %d element %s\n", |
| 2051 | type, file, line, name); |
| 2052 | else if ((file != NULL) && (name != NULL)) |
| 2053 | ctxt->error(ctxt->userData, "%s: file %s element %s\n", |
| 2054 | type, file, name); |
| 2055 | else if ((file != NULL) && (line != 0)) |
| 2056 | ctxt->error(ctxt->userData, "%s: file %s line %d\n", type, file, line); |
| 2057 | else if (file != NULL) |
| 2058 | ctxt->error(ctxt->userData, "%s: file %s\n", type, file); |
| 2059 | else if (name != NULL) |
| 2060 | ctxt->error(ctxt->userData, "%s: element %s\n", type, name); |
| 2061 | else |
| 2062 | ctxt->error(ctxt->userData, "%s\n", type); |
| 2063 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2064 | |
| 2065 | /** |
| 2066 | * xmlRelaxNGShowValidError: |
| 2067 | * @ctxt: the validation context |
| 2068 | * @err: the error number |
| 2069 | * @node: the node |
| 2070 | * @child: the node child generating the problem. |
| 2071 | * @arg1: the first argument |
| 2072 | * @arg2: the second argument |
| 2073 | * |
| 2074 | * Show a validation error. |
| 2075 | */ |
| 2076 | static void |
| 2077 | xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
| 2078 | xmlNodePtr node, xmlNodePtr child, |
| 2079 | const xmlChar *arg1, const xmlChar *arg2) |
| 2080 | { |
| 2081 | xmlChar *msg; |
| 2082 | |
| 2083 | if (ctxt->error == NULL) |
| 2084 | return; |
| 2085 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2086 | #ifdef DEBUG_ERROR |
| 2087 | xmlGenericError(xmlGenericErrorContext, |
| 2088 | "Show error %d\n", err); |
| 2089 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2090 | msg = xmlRelaxNGGetErrorString(err, arg1, arg2); |
| 2091 | if (msg == NULL) |
| 2092 | return; |
| 2093 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2094 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 2095 | ctxt->errNo = err; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2096 | xmlRelaxNGValidErrorContext(ctxt, node, child); |
| 2097 | ctxt->error(ctxt->userData, "%s\n", msg); |
| 2098 | xmlFree(msg); |
| 2099 | } |
| 2100 | |
| 2101 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2102 | * xmlRelaxNGPopErrors: |
| 2103 | * @ctxt: the validation context |
| 2104 | * @level: the error level in the stack |
| 2105 | * |
| 2106 | * pop and discard all errors until the given level is reached |
| 2107 | */ |
| 2108 | static void |
| 2109 | xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) { |
| 2110 | int i; |
| 2111 | xmlRelaxNGValidErrorPtr err; |
| 2112 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2113 | #ifdef DEBUG_ERROR |
| 2114 | xmlGenericError(xmlGenericErrorContext, |
| 2115 | "Pop errors till level %d\n", level); |
| 2116 | #endif |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2117 | for (i = level;i < ctxt->errNr;i++) { |
| 2118 | err = &ctxt->errTab[i]; |
| 2119 | if (err->flags & ERROR_IS_DUP) { |
| 2120 | if (err->arg1 != NULL) |
| 2121 | xmlFree((xmlChar *)err->arg1); |
| 2122 | err->arg1 = NULL; |
| 2123 | if (err->arg2 != NULL) |
| 2124 | xmlFree((xmlChar *)err->arg2); |
| 2125 | err->arg2 = NULL; |
| 2126 | err->flags = 0; |
| 2127 | } |
| 2128 | } |
| 2129 | ctxt->errNr = level; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2130 | if (ctxt->errNr <= 0) |
| 2131 | ctxt->err = NULL; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2132 | } |
| 2133 | /** |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2134 | * xmlRelaxNGDumpValidError: |
| 2135 | * @ctxt: the validation context |
| 2136 | * |
| 2137 | * Show all validation error over a given index. |
| 2138 | */ |
| 2139 | static void |
| 2140 | xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2141 | int i, j, k; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2142 | xmlRelaxNGValidErrorPtr err, dup; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2143 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2144 | #ifdef DEBUG_ERROR |
| 2145 | xmlGenericError(xmlGenericErrorContext, |
| 2146 | "Dumping error stack %d errors\n", ctxt->errNr); |
| 2147 | #endif |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2148 | for (i = 0, k = 0;i < ctxt->errNr;i++) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2149 | err = &ctxt->errTab[i]; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2150 | if (k < MAX_ERROR) { |
| 2151 | for (j = 0;j < i;j++) { |
| 2152 | dup = &ctxt->errTab[j]; |
| 2153 | if ((err->err == dup->err) && (err->node == dup->node) && |
| 2154 | (xmlStrEqual(err->arg1, dup->arg1)) && |
| 2155 | (xmlStrEqual(err->arg2, dup->arg2))) { |
| 2156 | goto skip; |
| 2157 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2158 | } |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 2159 | xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq, |
| 2160 | err->arg1, err->arg2); |
| 2161 | k++; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2162 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2163 | skip: |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2164 | if (err->flags & ERROR_IS_DUP) { |
| 2165 | if (err->arg1 != NULL) |
| 2166 | xmlFree((xmlChar *)err->arg1); |
| 2167 | err->arg1 = NULL; |
| 2168 | if (err->arg2 != NULL) |
| 2169 | xmlFree((xmlChar *)err->arg2); |
| 2170 | err->arg2 = NULL; |
| 2171 | err->flags = 0; |
| 2172 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2173 | } |
| 2174 | ctxt->errNr = 0; |
| 2175 | } |
| 2176 | /** |
| 2177 | * xmlRelaxNGAddValidError: |
| 2178 | * @ctxt: the validation context |
| 2179 | * @err: the error number |
| 2180 | * @arg1: the first argument |
| 2181 | * @arg2: the second argument |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2182 | * @dup: need to dup the args |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2183 | * |
| 2184 | * Register a validation error, either generating it if it's sure |
| 2185 | * or stacking it for later handling if unsure. |
| 2186 | */ |
| 2187 | static void |
| 2188 | xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2189 | const xmlChar *arg1, const xmlChar *arg2, int dup) |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2190 | { |
| 2191 | if ((ctxt == NULL) || (ctxt->error == NULL)) |
| 2192 | return; |
| 2193 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 2194 | #ifdef DEBUG_ERROR |
| 2195 | xmlGenericError(xmlGenericErrorContext, |
| 2196 | "Adding error %d\n", err); |
| 2197 | #endif |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2198 | /* |
| 2199 | * generate the error directly |
| 2200 | */ |
| 2201 | if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) { |
| 2202 | xmlNodePtr node, seq; |
| 2203 | /* |
| 2204 | * Flush first any stacked error which might be the |
| 2205 | * real cause of the problem. |
| 2206 | */ |
| 2207 | if (ctxt->errNr != 0) |
| 2208 | xmlRelaxNGDumpValidError(ctxt); |
| 2209 | if (ctxt->state != NULL) { |
| 2210 | node = ctxt->state->node; |
| 2211 | seq = ctxt->state->seq; |
| 2212 | } else { |
| 2213 | node = seq = NULL; |
| 2214 | } |
| 2215 | xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2); |
| 2216 | } |
| 2217 | /* |
| 2218 | * Stack the error for later processing if needed |
| 2219 | */ |
| 2220 | else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2221 | xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2222 | } |
| 2223 | } |
| 2224 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2225 | |
| 2226 | /************************************************************************ |
| 2227 | * * |
| 2228 | * Type library hooks * |
| 2229 | * * |
| 2230 | ************************************************************************/ |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2231 | static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, |
| 2232 | const xmlChar *str); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2233 | |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2234 | /** |
| 2235 | * xmlRelaxNGSchemaTypeHave: |
| 2236 | * @data: data needed for the library |
| 2237 | * @type: the type name |
| 2238 | * |
| 2239 | * Check if the given type is provided by |
| 2240 | * the W3C XMLSchema Datatype library. |
| 2241 | * |
| 2242 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2243 | */ |
| 2244 | static int |
| 2245 | xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2246 | const xmlChar *type) { |
| 2247 | xmlSchemaTypePtr typ; |
| 2248 | |
| 2249 | if (type == NULL) |
| 2250 | return(-1); |
| 2251 | typ = xmlSchemaGetPredefinedType(type, |
| 2252 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2253 | if (typ == NULL) |
| 2254 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2255 | return(1); |
| 2256 | } |
| 2257 | |
| 2258 | /** |
| 2259 | * xmlRelaxNGSchemaTypeCheck: |
| 2260 | * @data: data needed for the library |
| 2261 | * @type: the type name |
| 2262 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2263 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2264 | * |
| 2265 | * Check if the given type and value are validated by |
| 2266 | * the W3C XMLSchema Datatype library. |
| 2267 | * |
| 2268 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2269 | */ |
| 2270 | static int |
| 2271 | xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2272 | const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2273 | const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2274 | void **result, |
| 2275 | xmlNodePtr node) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2276 | xmlSchemaTypePtr typ; |
| 2277 | int ret; |
| 2278 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2279 | if ((type == NULL) || (value == NULL)) |
| 2280 | return(-1); |
| 2281 | typ = xmlSchemaGetPredefinedType(type, |
| 2282 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2283 | if (typ == NULL) |
| 2284 | return(-1); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2285 | ret = xmlSchemaValPredefTypeNode(typ, value, |
| 2286 | (xmlSchemaValPtr *) result, node); |
| 2287 | if (ret == 2) /* special ID error code */ |
| 2288 | return(2); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 2289 | if (ret == 0) |
| 2290 | return(1); |
| 2291 | if (ret > 0) |
| 2292 | return(0); |
| 2293 | return(-1); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | /** |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2297 | * xmlRelaxNGSchemaFacetCheck: |
| 2298 | * @data: data needed for the library |
| 2299 | * @type: the type name |
| 2300 | * @facet: the facet name |
| 2301 | * @val: the facet value |
| 2302 | * @strval: the string value |
| 2303 | * @value: the value to check |
| 2304 | * |
| 2305 | * Function provided by a type library to check a value facet |
| 2306 | * |
| 2307 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2308 | */ |
| 2309 | static int |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 2310 | xmlRelaxNGSchemaFacetCheck (void *data ATTRIBUTE_UNUSED, const xmlChar *type, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2311 | const xmlChar *facetname, const xmlChar *val, |
| 2312 | const xmlChar *strval, void *value) { |
| 2313 | xmlSchemaFacetPtr facet; |
| 2314 | xmlSchemaTypePtr typ; |
| 2315 | int ret; |
| 2316 | |
| 2317 | if ((type == NULL) || (strval == NULL)) |
| 2318 | return(-1); |
| 2319 | typ = xmlSchemaGetPredefinedType(type, |
| 2320 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2321 | if (typ == NULL) |
| 2322 | return(-1); |
| 2323 | |
| 2324 | facet = xmlSchemaNewFacet(); |
| 2325 | if (facet == NULL) |
| 2326 | return(-1); |
| 2327 | |
| 2328 | if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) { |
| 2329 | facet->type = XML_SCHEMA_FACET_MININCLUSIVE; |
| 2330 | } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) { |
| 2331 | facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE; |
| 2332 | } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) { |
| 2333 | facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE; |
| 2334 | } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) { |
| 2335 | facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE; |
| 2336 | } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) { |
| 2337 | facet->type = XML_SCHEMA_FACET_TOTALDIGITS; |
| 2338 | } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) { |
| 2339 | facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS; |
| 2340 | } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) { |
| 2341 | facet->type = XML_SCHEMA_FACET_PATTERN; |
| 2342 | } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) { |
| 2343 | facet->type = XML_SCHEMA_FACET_ENUMERATION; |
| 2344 | } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) { |
| 2345 | facet->type = XML_SCHEMA_FACET_WHITESPACE; |
| 2346 | } else if (xmlStrEqual(facetname, BAD_CAST "length")) { |
| 2347 | facet->type = XML_SCHEMA_FACET_LENGTH; |
| 2348 | } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) { |
| 2349 | facet->type = XML_SCHEMA_FACET_MAXLENGTH; |
| 2350 | } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) { |
| 2351 | facet->type = XML_SCHEMA_FACET_MINLENGTH; |
| 2352 | } else { |
| 2353 | xmlSchemaFreeFacet(facet); |
| 2354 | return(-1); |
| 2355 | } |
| 2356 | facet->value = xmlStrdup(val); |
| 2357 | ret = xmlSchemaCheckFacet(facet, typ, NULL, type); |
| 2358 | if (ret != 0) { |
| 2359 | xmlSchemaFreeFacet(facet); |
| 2360 | return(-1); |
| 2361 | } |
| 2362 | ret = xmlSchemaValidateFacet(typ, facet, strval, value); |
| 2363 | xmlSchemaFreeFacet(facet); |
| 2364 | if (ret != 0) |
| 2365 | return(-1); |
| 2366 | return(0); |
| 2367 | } |
| 2368 | |
| 2369 | /** |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2370 | * xmlRelaxNGSchemaFreeValue: |
| 2371 | * @data: data needed for the library |
| 2372 | * @value: the value to free |
| 2373 | * |
| 2374 | * Function provided by a type library to free a Schemas value |
| 2375 | * |
| 2376 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2377 | */ |
| 2378 | static void |
| 2379 | xmlRelaxNGSchemaFreeValue (void *data ATTRIBUTE_UNUSED, void *value) { |
| 2380 | xmlSchemaFreeValue(value); |
| 2381 | } |
| 2382 | |
| 2383 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2384 | * xmlRelaxNGSchemaTypeCompare: |
| 2385 | * @data: data needed for the library |
| 2386 | * @type: the type name |
| 2387 | * @value1: the first value |
| 2388 | * @value2: the second value |
| 2389 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2390 | * Compare two values for equality accordingly a type from the W3C XMLSchema |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2391 | * Datatype library. |
| 2392 | * |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2393 | * 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] | 2394 | */ |
| 2395 | static int |
| 2396 | xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2397 | const xmlChar *type, |
| 2398 | const xmlChar *value1, |
| 2399 | xmlNodePtr ctxt1, |
| 2400 | void *comp1, |
| 2401 | const xmlChar *value2, |
| 2402 | xmlNodePtr ctxt2) { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2403 | int ret; |
| 2404 | xmlSchemaTypePtr typ; |
| 2405 | xmlSchemaValPtr res1 = NULL, res2 = NULL; |
| 2406 | |
| 2407 | if ((type == NULL) || (value1 == NULL) || (value2 == NULL)) |
| 2408 | return(-1); |
| 2409 | typ = xmlSchemaGetPredefinedType(type, |
| 2410 | BAD_CAST "http://www.w3.org/2001/XMLSchema"); |
| 2411 | if (typ == NULL) |
| 2412 | return(-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2413 | if (comp1 == NULL) { |
| 2414 | ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1); |
| 2415 | if (ret != 0) |
| 2416 | return(-1); |
| 2417 | if (res1 == NULL) |
| 2418 | return(-1); |
| 2419 | } else { |
| 2420 | res1 = (xmlSchemaValPtr) comp1; |
| 2421 | } |
| 2422 | ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2423 | if (ret != 0) { |
| 2424 | xmlSchemaFreeValue(res1); |
| 2425 | return(-1); |
| 2426 | } |
| 2427 | if (res1 == NULL) { |
| 2428 | xmlSchemaFreeValue(res1); |
| 2429 | return(-1); |
| 2430 | } |
| 2431 | ret = xmlSchemaCompareValues(res1, res2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2432 | if (res1 != (xmlSchemaValPtr) comp1) |
| 2433 | xmlSchemaFreeValue(res1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2434 | xmlSchemaFreeValue(res2); |
| 2435 | if (ret == -2) |
| 2436 | return(-1); |
| 2437 | if (ret == 0) |
| 2438 | return(1); |
| 2439 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | /** |
| 2443 | * xmlRelaxNGDefaultTypeHave: |
| 2444 | * @data: data needed for the library |
| 2445 | * @type: the type name |
| 2446 | * |
| 2447 | * Check if the given type is provided by |
| 2448 | * the default datatype library. |
| 2449 | * |
| 2450 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2451 | */ |
| 2452 | static int |
| 2453 | xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar *type) { |
| 2454 | if (type == NULL) |
| 2455 | return(-1); |
| 2456 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2457 | return(1); |
| 2458 | if (xmlStrEqual(type, BAD_CAST "token")) |
| 2459 | return(1); |
| 2460 | return(0); |
| 2461 | } |
| 2462 | |
| 2463 | /** |
| 2464 | * xmlRelaxNGDefaultTypeCheck: |
| 2465 | * @data: data needed for the library |
| 2466 | * @type: the type name |
| 2467 | * @value: the value to check |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2468 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2469 | * |
| 2470 | * Check if the given type and value are validated by |
| 2471 | * the default datatype library. |
| 2472 | * |
| 2473 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2474 | */ |
| 2475 | static int |
| 2476 | xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED, |
| 2477 | const xmlChar *type ATTRIBUTE_UNUSED, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2478 | const xmlChar *value ATTRIBUTE_UNUSED, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2479 | void **result ATTRIBUTE_UNUSED, |
| 2480 | xmlNodePtr node ATTRIBUTE_UNUSED) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2481 | if (value == NULL) |
| 2482 | return(-1); |
| 2483 | if (xmlStrEqual(type, BAD_CAST "string")) |
| 2484 | return(1); |
| 2485 | if (xmlStrEqual(type, BAD_CAST "token")) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2486 | return(1); |
| 2487 | } |
| 2488 | |
| 2489 | return(0); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | /** |
| 2493 | * xmlRelaxNGDefaultTypeCompare: |
| 2494 | * @data: data needed for the library |
| 2495 | * @type: the type name |
| 2496 | * @value1: the first value |
| 2497 | * @value2: the second value |
| 2498 | * |
| 2499 | * Compare two values accordingly a type from the default |
| 2500 | * datatype library. |
| 2501 | * |
| 2502 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 2503 | */ |
| 2504 | static int |
| 2505 | xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED, |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2506 | const xmlChar *type, |
| 2507 | const xmlChar *value1, |
| 2508 | xmlNodePtr ctxt1 ATTRIBUTE_UNUSED, |
| 2509 | void *comp1 ATTRIBUTE_UNUSED, |
| 2510 | const xmlChar *value2, |
| 2511 | xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2512 | int ret = -1; |
| 2513 | |
| 2514 | if (xmlStrEqual(type, BAD_CAST "string")) { |
| 2515 | ret = xmlStrEqual(value1, value2); |
| 2516 | } else if (xmlStrEqual(type, BAD_CAST "token")) { |
| 2517 | if (!xmlStrEqual(value1, value2)) { |
| 2518 | xmlChar *nval, *nvalue; |
| 2519 | |
| 2520 | /* |
| 2521 | * TODO: trivial optimizations are possible by |
| 2522 | * computing at compile-time |
| 2523 | */ |
| 2524 | nval = xmlRelaxNGNormalize(NULL, value1); |
| 2525 | nvalue = xmlRelaxNGNormalize(NULL, value2); |
| 2526 | |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2527 | if ((nval == NULL) || (nvalue == NULL)) |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2528 | ret = -1; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2529 | else if (xmlStrEqual(nval, nvalue)) |
| 2530 | ret = 1; |
| 2531 | else |
| 2532 | ret = 0; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2533 | if (nval != NULL) |
| 2534 | xmlFree(nval); |
| 2535 | if (nvalue != NULL) |
| 2536 | xmlFree(nvalue); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 2537 | } else |
| 2538 | ret = 1; |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 2539 | } |
| 2540 | return(ret); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
| 2543 | static int xmlRelaxNGTypeInitialized = 0; |
| 2544 | static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL; |
| 2545 | |
| 2546 | /** |
| 2547 | * xmlRelaxNGFreeTypeLibrary: |
| 2548 | * @lib: the type library structure |
| 2549 | * @namespace: the URI bound to the library |
| 2550 | * |
| 2551 | * Free the structure associated to the type library |
| 2552 | */ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2553 | static void |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2554 | xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib, |
| 2555 | const xmlChar *namespace ATTRIBUTE_UNUSED) { |
| 2556 | if (lib == NULL) |
| 2557 | return; |
| 2558 | if (lib->namespace != NULL) |
| 2559 | xmlFree((xmlChar *)lib->namespace); |
| 2560 | xmlFree(lib); |
| 2561 | } |
| 2562 | |
| 2563 | /** |
| 2564 | * xmlRelaxNGRegisterTypeLibrary: |
| 2565 | * @namespace: the URI bound to the library |
| 2566 | * @data: data associated to the library |
| 2567 | * @have: the provide function |
| 2568 | * @check: the checking function |
| 2569 | * @comp: the comparison function |
| 2570 | * |
| 2571 | * Register a new type library |
| 2572 | * |
| 2573 | * Returns 0 in case of success and -1 in case of error. |
| 2574 | */ |
| 2575 | static int |
| 2576 | xmlRelaxNGRegisterTypeLibrary(const xmlChar *namespace, void *data, |
| 2577 | xmlRelaxNGTypeHave have, xmlRelaxNGTypeCheck check, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2578 | xmlRelaxNGTypeCompare comp, xmlRelaxNGFacetCheck facet, |
| 2579 | xmlRelaxNGTypeFree freef) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2580 | xmlRelaxNGTypeLibraryPtr lib; |
| 2581 | int ret; |
| 2582 | |
| 2583 | if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) || |
| 2584 | (check == NULL) || (comp == NULL)) |
| 2585 | return(-1); |
| 2586 | if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) { |
| 2587 | xmlGenericError(xmlGenericErrorContext, |
| 2588 | "Relax-NG types library '%s' already registered\n", |
| 2589 | namespace); |
| 2590 | return(-1); |
| 2591 | } |
| 2592 | lib = (xmlRelaxNGTypeLibraryPtr) xmlMalloc(sizeof(xmlRelaxNGTypeLibrary)); |
| 2593 | if (lib == NULL) { |
| 2594 | xmlGenericError(xmlGenericErrorContext, |
| 2595 | "Relax-NG types library '%s' malloc() failed\n", |
| 2596 | namespace); |
| 2597 | return (-1); |
| 2598 | } |
| 2599 | memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary)); |
| 2600 | lib->namespace = xmlStrdup(namespace); |
| 2601 | lib->data = data; |
| 2602 | lib->have = have; |
| 2603 | lib->comp = comp; |
| 2604 | lib->check = check; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2605 | lib->facet = facet; |
| 2606 | lib->freef = freef; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2607 | ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib); |
| 2608 | if (ret < 0) { |
| 2609 | xmlGenericError(xmlGenericErrorContext, |
| 2610 | "Relax-NG types library failed to register '%s'\n", |
| 2611 | namespace); |
| 2612 | xmlRelaxNGFreeTypeLibrary(lib, namespace); |
| 2613 | return(-1); |
| 2614 | } |
| 2615 | return(0); |
| 2616 | } |
| 2617 | |
| 2618 | /** |
| 2619 | * xmlRelaxNGInitTypes: |
| 2620 | * |
| 2621 | * Initilize the default type libraries. |
| 2622 | * |
| 2623 | * Returns 0 in case of success and -1 in case of error. |
| 2624 | */ |
| 2625 | static int |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2626 | xmlRelaxNGInitTypes(void) { |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2627 | if (xmlRelaxNGTypeInitialized != 0) |
| 2628 | return(0); |
| 2629 | xmlRelaxNGRegisteredTypes = xmlHashCreate(10); |
| 2630 | if (xmlRelaxNGRegisteredTypes == NULL) { |
| 2631 | xmlGenericError(xmlGenericErrorContext, |
| 2632 | "Failed to allocate sh table for Relax-NG types\n"); |
| 2633 | return(-1); |
| 2634 | } |
| 2635 | xmlRelaxNGRegisterTypeLibrary( |
| 2636 | BAD_CAST "http://www.w3.org/2001/XMLSchema-datatypes", |
| 2637 | NULL, |
| 2638 | xmlRelaxNGSchemaTypeHave, |
| 2639 | xmlRelaxNGSchemaTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2640 | xmlRelaxNGSchemaTypeCompare, |
| 2641 | xmlRelaxNGSchemaFacetCheck, |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2642 | xmlRelaxNGSchemaFreeValue); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2643 | xmlRelaxNGRegisterTypeLibrary( |
| 2644 | xmlRelaxNGNs, |
| 2645 | NULL, |
| 2646 | xmlRelaxNGDefaultTypeHave, |
| 2647 | xmlRelaxNGDefaultTypeCheck, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2648 | xmlRelaxNGDefaultTypeCompare, |
| 2649 | NULL, |
| 2650 | NULL); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2651 | xmlRelaxNGTypeInitialized = 1; |
| 2652 | return(0); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | /** |
| 2656 | * xmlRelaxNGCleanupTypes: |
| 2657 | * |
| 2658 | * Cleanup the default Schemas type library associated to RelaxNG |
| 2659 | */ |
| 2660 | void |
| 2661 | xmlRelaxNGCleanupTypes(void) { |
Daniel Veillard | a84c0b3 | 2003-06-02 16:58:46 +0000 | [diff] [blame] | 2662 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2663 | if (xmlRelaxNGTypeInitialized == 0) |
| 2664 | return; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 2665 | xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator) |
| 2666 | xmlRelaxNGFreeTypeLibrary); |
| 2667 | xmlRelaxNGTypeInitialized = 0; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | /************************************************************************ |
| 2671 | * * |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2672 | * Compiling element content into regexp * |
| 2673 | * * |
| 2674 | * Sometime the element content can be compiled into a pure regexp, * |
| 2675 | * This allows a faster execution and streamability at that level * |
| 2676 | * * |
| 2677 | ************************************************************************/ |
| 2678 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2679 | static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, |
| 2680 | xmlRelaxNGDefinePtr def); |
| 2681 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2682 | /** |
| 2683 | * xmlRelaxNGIsCompileable: |
| 2684 | * @define: the definition to check |
| 2685 | * |
| 2686 | * Check if a definition is nullable. |
| 2687 | * |
| 2688 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 2689 | */ |
| 2690 | static int |
| 2691 | xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) { |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2692 | int ret = -1; |
| 2693 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2694 | if (def == NULL) { |
| 2695 | return(-1); |
| 2696 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2697 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2698 | (def->dflags & IS_COMPILABLE)) |
| 2699 | return(1); |
| 2700 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 2701 | (def->dflags & IS_NOT_COMPILABLE)) |
| 2702 | return(0); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2703 | switch(def->type) { |
| 2704 | case XML_RELAXNG_REF: |
| 2705 | case XML_RELAXNG_EXTERNALREF: |
| 2706 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2707 | if (def->depth == -20) { |
| 2708 | return(1); |
| 2709 | } else { |
| 2710 | def->depth = -20; |
| 2711 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2712 | } |
| 2713 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2714 | case XML_RELAXNG_NOOP: |
| 2715 | case XML_RELAXNG_START: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2716 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2717 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2718 | case XML_RELAXNG_TEXT: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2719 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2720 | ret = 1; |
| 2721 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2722 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2723 | if (((def->dflags & IS_NOT_COMPILABLE) == 0) && |
| 2724 | ((def->dflags & IS_COMPILABLE) == 0)) { |
| 2725 | ret = xmlRelaxNGIsCompileable(def->content); |
| 2726 | if (ret == 0) def->dflags |= IS_NOT_COMPILABLE; |
| 2727 | if (ret == 1) def->dflags |= IS_COMPILABLE; |
| 2728 | } |
| 2729 | if ((def->nameClass != NULL) || (def->name == NULL)) |
| 2730 | return(0); |
| 2731 | else |
| 2732 | return(1); |
| 2733 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2734 | case XML_RELAXNG_OPTIONAL: |
| 2735 | case XML_RELAXNG_ZEROORMORE: |
| 2736 | case XML_RELAXNG_ONEORMORE: |
| 2737 | case XML_RELAXNG_CHOICE: |
| 2738 | case XML_RELAXNG_GROUP: |
| 2739 | case XML_RELAXNG_DEF: { |
| 2740 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2741 | |
| 2742 | list = def->content; |
| 2743 | while (list != NULL) { |
| 2744 | ret = xmlRelaxNGIsCompileable(list); |
| 2745 | if (ret != 1) |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2746 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2747 | list = list->next; |
| 2748 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2749 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2750 | } |
| 2751 | case XML_RELAXNG_EXCEPT: |
| 2752 | case XML_RELAXNG_ATTRIBUTE: |
| 2753 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2754 | case XML_RELAXNG_DATATYPE: |
| 2755 | case XML_RELAXNG_LIST: |
| 2756 | case XML_RELAXNG_PARAM: |
| 2757 | case XML_RELAXNG_VALUE: |
| 2758 | ret = 0; |
| 2759 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2760 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2761 | ret = -1; |
| 2762 | break; |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 2763 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2764 | if (ret == 0) def->dflags |= IS_NOT_COMPILABLE; |
| 2765 | if (ret == 1) def->dflags |= IS_COMPILABLE; |
| 2766 | return(ret); |
| 2767 | } |
| 2768 | |
| 2769 | /** |
| 2770 | * xmlRelaxNGCompile: |
| 2771 | * ctxt: the RelaxNG parser context |
| 2772 | * @define: the definition tree to compile |
| 2773 | * |
| 2774 | * Compile the set of definitions, it works recursively, till the |
| 2775 | * element boundaries, where it tries to compile the content if possible |
| 2776 | * |
| 2777 | * Returns 0 if success and -1 in case of error |
| 2778 | */ |
| 2779 | static int |
| 2780 | xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) { |
| 2781 | int ret = 0; |
| 2782 | xmlRelaxNGDefinePtr list; |
| 2783 | |
| 2784 | if ((ctxt == NULL) || (def == NULL)) return(-1); |
| 2785 | |
| 2786 | switch(def->type) { |
| 2787 | case XML_RELAXNG_START: |
| 2788 | if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) { |
| 2789 | xmlAutomataPtr oldam = ctxt->am; |
| 2790 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2791 | |
| 2792 | def->depth = -25; |
| 2793 | |
| 2794 | list = def->content; |
| 2795 | ctxt->am = xmlNewAutomata(); |
| 2796 | if (ctxt->am == NULL) |
| 2797 | return(-1); |
| 2798 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2799 | while (list != NULL) { |
| 2800 | xmlRelaxNGCompile(ctxt, list); |
| 2801 | list = list->next; |
| 2802 | } |
| 2803 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2804 | def->contModel = xmlAutomataCompile(ctxt->am); |
| 2805 | xmlRegexpIsDeterminist(def->contModel); |
| 2806 | |
| 2807 | xmlFreeAutomata(ctxt->am); |
| 2808 | ctxt->state = oldstate; |
| 2809 | ctxt->am = oldam; |
| 2810 | } |
| 2811 | break; |
| 2812 | case XML_RELAXNG_ELEMENT: |
| 2813 | if ((ctxt->am != NULL) && (def->name != NULL)) { |
| 2814 | ctxt->state = xmlAutomataNewTransition2(ctxt->am, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2815 | ctxt->state, NULL, def->name, def->ns, def); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2816 | } |
| 2817 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 2818 | xmlAutomataPtr oldam = ctxt->am; |
| 2819 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2820 | |
| 2821 | def->depth = -25; |
| 2822 | |
| 2823 | list = def->content; |
| 2824 | ctxt->am = xmlNewAutomata(); |
| 2825 | if (ctxt->am == NULL) |
| 2826 | return(-1); |
| 2827 | ctxt->state = xmlAutomataGetInitState(ctxt->am); |
| 2828 | while (list != NULL) { |
| 2829 | xmlRelaxNGCompile(ctxt, list); |
| 2830 | list = list->next; |
| 2831 | } |
| 2832 | xmlAutomataSetFinalState(ctxt->am, ctxt->state); |
| 2833 | def->contModel = xmlAutomataCompile(ctxt->am); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2834 | if (!xmlRegexpIsDeterminist(def->contModel)) { |
| 2835 | /* |
| 2836 | * we can only use the automata if it is determinist |
| 2837 | */ |
| 2838 | xmlRegFreeRegexp(def->contModel); |
| 2839 | def->contModel = NULL; |
| 2840 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2841 | xmlFreeAutomata(ctxt->am); |
| 2842 | ctxt->state = oldstate; |
| 2843 | ctxt->am = oldam; |
| 2844 | } else { |
| 2845 | xmlAutomataPtr oldam = ctxt->am; |
| 2846 | |
| 2847 | /* |
| 2848 | * we can't build the content model for this element content |
| 2849 | * but it still might be possible to build it for some of its |
| 2850 | * children, recurse. |
| 2851 | */ |
| 2852 | ret = xmlRelaxNGTryCompile(ctxt, def); |
| 2853 | ctxt->am = oldam; |
| 2854 | } |
| 2855 | break; |
| 2856 | case XML_RELAXNG_REF: |
| 2857 | case XML_RELAXNG_EXTERNALREF: |
| 2858 | case XML_RELAXNG_PARENTREF: |
| 2859 | case XML_RELAXNG_NOOP: |
| 2860 | ret = xmlRelaxNGCompile(ctxt, def->content); |
| 2861 | break; |
| 2862 | case XML_RELAXNG_OPTIONAL: { |
| 2863 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2864 | |
| 2865 | xmlRelaxNGCompile(ctxt, def->content); |
| 2866 | xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state); |
| 2867 | break; |
| 2868 | } |
| 2869 | case XML_RELAXNG_ZEROORMORE: { |
| 2870 | xmlAutomataStatePtr oldstate; |
| 2871 | |
| 2872 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 2873 | oldstate = ctxt->state; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2874 | list = def->content; |
| 2875 | while (list != NULL) { |
| 2876 | xmlRelaxNGCompile(ctxt, list); |
| 2877 | list = list->next; |
| 2878 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2879 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 2880 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2881 | break; |
| 2882 | } |
| 2883 | case XML_RELAXNG_ONEORMORE: { |
| 2884 | xmlAutomataStatePtr oldstate; |
| 2885 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2886 | list = def->content; |
| 2887 | while (list != NULL) { |
| 2888 | xmlRelaxNGCompile(ctxt, list); |
| 2889 | list = list->next; |
| 2890 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2891 | oldstate = ctxt->state; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2892 | list = def->content; |
| 2893 | while (list != NULL) { |
| 2894 | xmlRelaxNGCompile(ctxt, list); |
| 2895 | list = list->next; |
| 2896 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2897 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate); |
| 2898 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2899 | break; |
| 2900 | } |
| 2901 | case XML_RELAXNG_CHOICE: { |
| 2902 | xmlAutomataStatePtr target = NULL; |
| 2903 | xmlAutomataStatePtr oldstate = ctxt->state; |
| 2904 | |
| 2905 | list = def->content; |
| 2906 | while (list != NULL) { |
| 2907 | ctxt->state = oldstate; |
| 2908 | xmlRelaxNGCompile(ctxt, list); |
| 2909 | if (target == NULL) |
| 2910 | target = ctxt->state; |
| 2911 | else { |
| 2912 | xmlAutomataNewEpsilon(ctxt->am, ctxt->state, target); |
| 2913 | } |
| 2914 | list = list->next; |
| 2915 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2916 | ctxt->state = target; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2917 | |
| 2918 | break; |
| 2919 | } |
| 2920 | case XML_RELAXNG_GROUP: |
| 2921 | case XML_RELAXNG_DEF: |
| 2922 | list = def->content; |
| 2923 | while (list != NULL) { |
| 2924 | xmlRelaxNGCompile(ctxt, list); |
| 2925 | list = list->next; |
| 2926 | } |
| 2927 | break; |
| 2928 | case XML_RELAXNG_TEXT: { |
| 2929 | xmlAutomataStatePtr oldstate; |
| 2930 | |
| 2931 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
| 2932 | oldstate = ctxt->state; |
| 2933 | xmlRelaxNGCompile(ctxt, def->content); |
| 2934 | xmlAutomataNewTransition(ctxt->am, ctxt->state, ctxt->state, |
| 2935 | BAD_CAST "#text", NULL); |
| 2936 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL); |
| 2937 | break; |
| 2938 | } |
| 2939 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 2940 | ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2941 | break; |
| 2942 | case XML_RELAXNG_EXCEPT: |
| 2943 | case XML_RELAXNG_ATTRIBUTE: |
| 2944 | case XML_RELAXNG_INTERLEAVE: |
| 2945 | case XML_RELAXNG_NOT_ALLOWED: |
| 2946 | case XML_RELAXNG_DATATYPE: |
| 2947 | case XML_RELAXNG_LIST: |
| 2948 | case XML_RELAXNG_PARAM: |
| 2949 | case XML_RELAXNG_VALUE: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 2950 | /* This should not happen and generate an internal error */ |
| 2951 | fprintf(stderr, "RNG internal error trying to compile %s\n", |
| 2952 | xmlRelaxNGDefName(def)); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 2953 | break; |
| 2954 | } |
| 2955 | return(ret); |
| 2956 | } |
| 2957 | |
| 2958 | /** |
| 2959 | * xmlRelaxNGTryCompile: |
| 2960 | * ctxt: the RelaxNG parser context |
| 2961 | * @define: the definition tree to compile |
| 2962 | * |
| 2963 | * Try to compile the set of definitions, it works recursively, |
| 2964 | * possibly ignoring parts which cannot be compiled. |
| 2965 | * |
| 2966 | * Returns 0 if success and -1 in case of error |
| 2967 | */ |
| 2968 | static int |
| 2969 | xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) { |
| 2970 | int ret = 0; |
| 2971 | xmlRelaxNGDefinePtr list; |
| 2972 | |
| 2973 | if ((ctxt == NULL) || (def == NULL)) return(-1); |
| 2974 | |
| 2975 | if ((def->type == XML_RELAXNG_START) || |
| 2976 | (def->type == XML_RELAXNG_ELEMENT)) { |
| 2977 | ret = xmlRelaxNGIsCompileable(def); |
| 2978 | if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) { |
| 2979 | ctxt->am = NULL; |
| 2980 | ret = xmlRelaxNGCompile(ctxt, def); |
| 2981 | return(ret); |
| 2982 | } |
| 2983 | } |
| 2984 | switch(def->type) { |
| 2985 | case XML_RELAXNG_REF: |
| 2986 | case XML_RELAXNG_EXTERNALREF: |
| 2987 | case XML_RELAXNG_PARENTREF: |
| 2988 | case XML_RELAXNG_NOOP: |
| 2989 | case XML_RELAXNG_START: |
| 2990 | ret = xmlRelaxNGTryCompile(ctxt, def->content); |
| 2991 | break; |
| 2992 | case XML_RELAXNG_TEXT: |
| 2993 | case XML_RELAXNG_DATATYPE: |
| 2994 | case XML_RELAXNG_LIST: |
| 2995 | case XML_RELAXNG_PARAM: |
| 2996 | case XML_RELAXNG_VALUE: |
| 2997 | case XML_RELAXNG_EMPTY: |
| 2998 | case XML_RELAXNG_ELEMENT: |
| 2999 | ret = 0; |
| 3000 | break; |
| 3001 | case XML_RELAXNG_OPTIONAL: |
| 3002 | case XML_RELAXNG_ZEROORMORE: |
| 3003 | case XML_RELAXNG_ONEORMORE: |
| 3004 | case XML_RELAXNG_CHOICE: |
| 3005 | case XML_RELAXNG_GROUP: |
| 3006 | case XML_RELAXNG_DEF: |
| 3007 | list = def->content; |
| 3008 | while (list != NULL) { |
| 3009 | ret = xmlRelaxNGTryCompile(ctxt, list); |
| 3010 | if (ret != 0) |
| 3011 | break; |
| 3012 | list = list->next; |
| 3013 | } |
| 3014 | break; |
| 3015 | case XML_RELAXNG_EXCEPT: |
| 3016 | case XML_RELAXNG_ATTRIBUTE: |
| 3017 | case XML_RELAXNG_INTERLEAVE: |
| 3018 | case XML_RELAXNG_NOT_ALLOWED: |
| 3019 | ret = 0; |
| 3020 | break; |
| 3021 | } |
| 3022 | return(ret); |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | /************************************************************************ |
| 3026 | * * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3027 | * Parsing functions * |
| 3028 | * * |
| 3029 | ************************************************************************/ |
| 3030 | |
| 3031 | static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute( |
| 3032 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 3033 | static xmlRelaxNGDefinePtr xmlRelaxNGParseElement( |
| 3034 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
| 3035 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns( |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3036 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, int group); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3037 | static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern( |
| 3038 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 3039 | static xmlRelaxNGPtr xmlRelaxNGParseDocument( |
| 3040 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 3041 | static int xmlRelaxNGParseGrammarContent( |
| 3042 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 3043 | static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass( |
| 3044 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 3045 | xmlRelaxNGDefinePtr def); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 3046 | static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar( |
| 3047 | xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3048 | static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 3049 | xmlRelaxNGDefinePtr define, xmlNodePtr elem); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3050 | |
| 3051 | |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 3052 | #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3053 | |
| 3054 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3055 | * xmlRelaxNGIsNullable: |
| 3056 | * @define: the definition to verify |
| 3057 | * |
| 3058 | * Check if a definition is nullable. |
| 3059 | * |
| 3060 | * Returns 1 if yes, 0 if no and -1 in case of error |
| 3061 | */ |
| 3062 | static int |
| 3063 | xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) { |
| 3064 | int ret; |
| 3065 | if (define == NULL) |
| 3066 | return(-1); |
| 3067 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3068 | if (define->dflags & IS_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3069 | return(1); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3070 | if (define->dflags & IS_NOT_NULLABLE) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3071 | return(0); |
| 3072 | switch (define->type) { |
| 3073 | case XML_RELAXNG_EMPTY: |
| 3074 | case XML_RELAXNG_TEXT: |
| 3075 | ret = 1; break; |
| 3076 | case XML_RELAXNG_NOOP: |
| 3077 | case XML_RELAXNG_DEF: |
| 3078 | case XML_RELAXNG_REF: |
| 3079 | case XML_RELAXNG_EXTERNALREF: |
| 3080 | case XML_RELAXNG_PARENTREF: |
| 3081 | case XML_RELAXNG_ONEORMORE: |
| 3082 | ret = xmlRelaxNGIsNullable(define->content); |
| 3083 | break; |
| 3084 | case XML_RELAXNG_EXCEPT: |
| 3085 | case XML_RELAXNG_NOT_ALLOWED: |
| 3086 | case XML_RELAXNG_ELEMENT: |
| 3087 | case XML_RELAXNG_DATATYPE: |
| 3088 | case XML_RELAXNG_PARAM: |
| 3089 | case XML_RELAXNG_VALUE: |
| 3090 | case XML_RELAXNG_LIST: |
| 3091 | case XML_RELAXNG_ATTRIBUTE: |
| 3092 | ret = 0; break; |
| 3093 | case XML_RELAXNG_CHOICE: { |
| 3094 | xmlRelaxNGDefinePtr list = define->content; |
| 3095 | |
| 3096 | while (list != NULL) { |
| 3097 | ret = xmlRelaxNGIsNullable(list); |
| 3098 | if (ret != 0) |
| 3099 | goto done; |
| 3100 | list = list->next; |
| 3101 | } |
| 3102 | ret = 0; break; |
| 3103 | } |
| 3104 | case XML_RELAXNG_START: |
| 3105 | case XML_RELAXNG_INTERLEAVE: |
| 3106 | case XML_RELAXNG_GROUP: { |
| 3107 | xmlRelaxNGDefinePtr list = define->content; |
| 3108 | |
| 3109 | while (list != NULL) { |
| 3110 | ret = xmlRelaxNGIsNullable(list); |
| 3111 | if (ret != 1) |
| 3112 | goto done; |
| 3113 | list = list->next; |
| 3114 | } |
| 3115 | return(1); |
| 3116 | } |
| 3117 | default: |
| 3118 | return(-1); |
| 3119 | } |
| 3120 | done: |
| 3121 | if (ret == 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3122 | define->dflags |= IS_NOT_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3123 | if (ret == 1) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3124 | define->dflags |= IS_NULLABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3125 | return(ret); |
| 3126 | } |
| 3127 | |
| 3128 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3129 | * xmlRelaxNGIsBlank: |
| 3130 | * @str: a string |
| 3131 | * |
| 3132 | * Check if a string is ignorable c.f. 4.2. Whitespace |
| 3133 | * |
| 3134 | * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise |
| 3135 | */ |
| 3136 | static int |
| 3137 | xmlRelaxNGIsBlank(xmlChar *str) { |
| 3138 | if (str == NULL) |
| 3139 | return(1); |
| 3140 | while (*str != 0) { |
| 3141 | if (!(IS_BLANK(*str))) return(0); |
| 3142 | str++; |
| 3143 | } |
| 3144 | return(1); |
| 3145 | } |
| 3146 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3147 | /** |
| 3148 | * xmlRelaxNGGetDataTypeLibrary: |
| 3149 | * @ctxt: a Relax-NG parser context |
| 3150 | * @node: the current data or value element |
| 3151 | * |
| 3152 | * Applies algorithm from 4.3. datatypeLibrary attribute |
| 3153 | * |
| 3154 | * Returns the datatypeLibary value or NULL if not found |
| 3155 | */ |
| 3156 | static xmlChar * |
| 3157 | xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3158 | xmlNodePtr node) { |
| 3159 | xmlChar *ret, *escape; |
| 3160 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3161 | if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) { |
| 3162 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3163 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3164 | if (ret[0] == 0) { |
| 3165 | xmlFree(ret); |
| 3166 | return(NULL); |
| 3167 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3168 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3169 | if (escape == NULL) { |
| 3170 | return(ret); |
| 3171 | } |
| 3172 | xmlFree(ret); |
| 3173 | return(escape); |
| 3174 | } |
| 3175 | } |
| 3176 | node = node->parent; |
| 3177 | while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3178 | ret = xmlGetProp(node, BAD_CAST "datatypeLibrary"); |
| 3179 | if (ret != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3180 | if (ret[0] == 0) { |
| 3181 | xmlFree(ret); |
| 3182 | return(NULL); |
| 3183 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3184 | escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?"); |
| 3185 | if (escape == NULL) { |
| 3186 | return(ret); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3187 | } |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 3188 | xmlFree(ret); |
| 3189 | return(escape); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 3190 | } |
| 3191 | node = node->parent; |
| 3192 | } |
| 3193 | return(NULL); |
| 3194 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3195 | |
| 3196 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3197 | * xmlRelaxNGParseValue: |
| 3198 | * @ctxt: a Relax-NG parser context |
| 3199 | * @node: the data node. |
| 3200 | * |
| 3201 | * parse the content of a RelaxNG value node. |
| 3202 | * |
| 3203 | * Returns the definition pointer or NULL in case of error |
| 3204 | */ |
| 3205 | static xmlRelaxNGDefinePtr |
| 3206 | xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 3207 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | 5f1946a | 2003-03-31 16:38:16 +0000 | [diff] [blame] | 3208 | xmlRelaxNGTypeLibraryPtr lib = NULL; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3209 | xmlChar *type; |
| 3210 | xmlChar *library; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3211 | int success = 0; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3212 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3213 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3214 | if (def == NULL) |
| 3215 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3216 | def->type = XML_RELAXNG_VALUE; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3217 | |
| 3218 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3219 | if (type != NULL) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3220 | xmlRelaxNGNormExtSpace(type); |
| 3221 | if (xmlValidateNCName(type, 0)) { |
| 3222 | if (ctxt->error != NULL) |
| 3223 | ctxt->error(ctxt->userData, |
| 3224 | "value type '%s' is not an NCName\n", |
| 3225 | type); |
| 3226 | ctxt->nbErrors++; |
| 3227 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3228 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3229 | if (library == NULL) |
| 3230 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 3231 | |
| 3232 | def->name = type; |
| 3233 | def->ns = library; |
| 3234 | |
| 3235 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3236 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3237 | if (lib == NULL) { |
| 3238 | if (ctxt->error != NULL) |
| 3239 | ctxt->error(ctxt->userData, |
| 3240 | "Use of unregistered type library '%s'\n", |
| 3241 | library); |
| 3242 | ctxt->nbErrors++; |
| 3243 | def->data = NULL; |
| 3244 | } else { |
| 3245 | def->data = lib; |
| 3246 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3247 | if (ctxt->error != NULL) |
| 3248 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3249 | "Internal error with type library '%s': no 'have'\n", |
| 3250 | library); |
| 3251 | ctxt->nbErrors++; |
| 3252 | } else { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3253 | success = lib->have(lib->data, def->name); |
| 3254 | if (success != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3255 | if (ctxt->error != NULL) |
| 3256 | ctxt->error(ctxt->userData, |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3257 | "Error type '%s' is not exported by type library '%s'\n", |
| 3258 | def->name, library); |
| 3259 | ctxt->nbErrors++; |
| 3260 | } |
| 3261 | } |
| 3262 | } |
| 3263 | } |
| 3264 | if (node->children == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 3265 | def->value = xmlStrdup(BAD_CAST ""); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 3266 | } else if (((node->children->type != XML_TEXT_NODE) && |
| 3267 | (node->children->type != XML_CDATA_SECTION_NODE)) || |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3268 | (node->children->next != NULL)) { |
| 3269 | if (ctxt->error != NULL) |
| 3270 | ctxt->error(ctxt->userData, |
| 3271 | "Expecting a single text value for <value>content\n"); |
| 3272 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3273 | } else if (def != NULL) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3274 | def->value = xmlNodeGetContent(node); |
| 3275 | if (def->value == NULL) { |
| 3276 | if (ctxt->error != NULL) |
| 3277 | ctxt->error(ctxt->userData, |
| 3278 | "Element <value> has no content\n"); |
| 3279 | ctxt->nbErrors++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3280 | } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) { |
| 3281 | void *val = NULL; |
| 3282 | |
| 3283 | success = lib->check(lib->data, def->name, def->value, &val, node); |
| 3284 | if (success != 1) { |
| 3285 | if (ctxt->error != NULL) |
| 3286 | ctxt->error(ctxt->userData, |
| 3287 | "Value '%s' is not acceptable for type '%s'\n", |
| 3288 | def->value, def->name); |
| 3289 | ctxt->nbErrors++; |
| 3290 | } else { |
| 3291 | if (val != NULL) |
| 3292 | def->attrs = val; |
| 3293 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3294 | } |
| 3295 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 3296 | return(def); |
| 3297 | } |
| 3298 | |
| 3299 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3300 | * xmlRelaxNGParseData: |
| 3301 | * @ctxt: a Relax-NG parser context |
| 3302 | * @node: the data node. |
| 3303 | * |
| 3304 | * parse the content of a RelaxNG data node. |
| 3305 | * |
| 3306 | * Returns the definition pointer or NULL in case of error |
| 3307 | */ |
| 3308 | static xmlRelaxNGDefinePtr |
| 3309 | xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3310 | xmlRelaxNGDefinePtr def = NULL, except, last = NULL; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3311 | xmlRelaxNGDefinePtr param, lastparam = NULL; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3312 | xmlRelaxNGTypeLibraryPtr lib; |
| 3313 | xmlChar *type; |
| 3314 | xmlChar *library; |
| 3315 | xmlNodePtr content; |
| 3316 | int tmp; |
| 3317 | |
| 3318 | type = xmlGetProp(node, BAD_CAST "type"); |
| 3319 | if (type == NULL) { |
| 3320 | if (ctxt->error != NULL) |
| 3321 | ctxt->error(ctxt->userData, |
| 3322 | "data has no type\n"); |
| 3323 | ctxt->nbErrors++; |
| 3324 | return(NULL); |
| 3325 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 3326 | xmlRelaxNGNormExtSpace(type); |
| 3327 | if (xmlValidateNCName(type, 0)) { |
| 3328 | if (ctxt->error != NULL) |
| 3329 | ctxt->error(ctxt->userData, |
| 3330 | "data type '%s' is not an NCName\n", |
| 3331 | type); |
| 3332 | ctxt->nbErrors++; |
| 3333 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3334 | library = xmlRelaxNGGetDataTypeLibrary(ctxt, node); |
| 3335 | if (library == NULL) |
| 3336 | library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0"); |
| 3337 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3338 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3339 | if (def == NULL) { |
| 3340 | xmlFree(type); |
| 3341 | return(NULL); |
| 3342 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3343 | def->type = XML_RELAXNG_DATATYPE; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3344 | def->name = type; |
| 3345 | def->ns = library; |
| 3346 | |
| 3347 | lib = (xmlRelaxNGTypeLibraryPtr) |
| 3348 | xmlHashLookup(xmlRelaxNGRegisteredTypes, library); |
| 3349 | if (lib == NULL) { |
| 3350 | if (ctxt->error != NULL) |
| 3351 | ctxt->error(ctxt->userData, |
| 3352 | "Use of unregistered type library '%s'\n", |
| 3353 | library); |
| 3354 | ctxt->nbErrors++; |
| 3355 | def->data = NULL; |
| 3356 | } else { |
| 3357 | def->data = lib; |
| 3358 | if (lib->have == NULL) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3359 | if (ctxt->error != NULL) |
| 3360 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3361 | "Internal error with type library '%s': no 'have'\n", |
| 3362 | library); |
| 3363 | ctxt->nbErrors++; |
| 3364 | } else { |
| 3365 | tmp = lib->have(lib->data, def->name); |
| 3366 | if (tmp != 1) { |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 3367 | if (ctxt->error != NULL) |
| 3368 | ctxt->error(ctxt->userData, |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3369 | "Error type '%s' is not exported by type library '%s'\n", |
| 3370 | def->name, library); |
| 3371 | ctxt->nbErrors++; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3372 | } else if ((xmlStrEqual(library, BAD_CAST |
| 3373 | "http://www.w3.org/2001/XMLSchema-datatypes")) && |
| 3374 | ((xmlStrEqual(def->name, BAD_CAST "IDREF")) || |
| 3375 | (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) { |
| 3376 | ctxt->idref = 1; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3377 | } |
| 3378 | } |
| 3379 | } |
| 3380 | content = node->children; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3381 | |
| 3382 | /* |
| 3383 | * Handle optional params |
| 3384 | */ |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3385 | while (content != NULL) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3386 | if (!xmlStrEqual(content->name, BAD_CAST "param")) |
| 3387 | break; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3388 | if (xmlStrEqual(library, |
| 3389 | BAD_CAST"http://relaxng.org/ns/structure/1.0")) { |
| 3390 | if (ctxt->error != NULL) |
| 3391 | ctxt->error(ctxt->userData, |
| 3392 | "Type library '%s' does not allow type parameters\n", |
| 3393 | library); |
| 3394 | ctxt->nbErrors++; |
| 3395 | content = content->next; |
| 3396 | while ((content != NULL) && |
| 3397 | (xmlStrEqual(content->name, BAD_CAST "param"))) |
| 3398 | content = content->next; |
| 3399 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3400 | param = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3401 | if (param != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3402 | param->type = XML_RELAXNG_PARAM; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3403 | param->name = xmlGetProp(content, BAD_CAST "name"); |
| 3404 | if (param->name == NULL) { |
| 3405 | if (ctxt->error != NULL) |
| 3406 | ctxt->error(ctxt->userData, |
| 3407 | "param has no name\n"); |
| 3408 | ctxt->nbErrors++; |
| 3409 | } |
| 3410 | param->value = xmlNodeGetContent(content); |
| 3411 | if (lastparam == NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3412 | def->attrs = lastparam = param; |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3413 | } else { |
| 3414 | lastparam->next = param; |
| 3415 | lastparam = param; |
| 3416 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 3417 | if (lib != NULL) { |
| 3418 | } |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3419 | } |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 3420 | content = content->next; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 3421 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3422 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3423 | /* |
| 3424 | * Handle optional except |
| 3425 | */ |
| 3426 | if ((content != NULL) && (xmlStrEqual(content->name, BAD_CAST "except"))) { |
| 3427 | xmlNodePtr child; |
| 3428 | xmlRelaxNGDefinePtr tmp2, last2 = NULL; |
| 3429 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3430 | except = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3431 | if (except == NULL) { |
| 3432 | return(def); |
| 3433 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3434 | except->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 3435 | child = content->children; |
| 3436 | if (last == NULL) { |
| 3437 | def->content = except; |
| 3438 | } else { |
| 3439 | last->next = except; |
| 3440 | } |
| 3441 | if (child == NULL) { |
| 3442 | if (ctxt->error != NULL) |
| 3443 | ctxt->error(ctxt->userData, |
| 3444 | "except has no content\n"); |
| 3445 | ctxt->nbErrors++; |
| 3446 | } |
| 3447 | while (child != NULL) { |
| 3448 | tmp2 = xmlRelaxNGParsePattern(ctxt, child); |
| 3449 | if (tmp2 != NULL) { |
| 3450 | if (last2 == NULL) { |
| 3451 | except->content = last2 = tmp2; |
| 3452 | } else { |
| 3453 | last2->next = tmp2; |
| 3454 | last2 = tmp2; |
| 3455 | } |
| 3456 | } |
| 3457 | child = child->next; |
| 3458 | } |
| 3459 | content = content->next; |
| 3460 | } |
| 3461 | /* |
| 3462 | * Check there is no unhandled data |
| 3463 | */ |
| 3464 | if (content != NULL) { |
| 3465 | if (ctxt->error != NULL) |
| 3466 | ctxt->error(ctxt->userData, |
| 3467 | "Element data has unexpected content %s\n", content->name); |
| 3468 | ctxt->nbErrors++; |
| 3469 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 3470 | |
| 3471 | return(def); |
| 3472 | } |
| 3473 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3474 | static const xmlChar *invalidName = BAD_CAST "\1"; |
| 3475 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3476 | /** |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3477 | * xmlRelaxNGCompareNameClasses: |
| 3478 | * @defs1: the first element/attribute defs |
| 3479 | * @defs2: the second element/attribute defs |
| 3480 | * @name: the restriction on the name |
| 3481 | * @ns: the restriction on the namespace |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3482 | * |
| 3483 | * Compare the 2 lists of element definitions. The comparison is |
| 3484 | * that if both lists do not accept the same QNames, it returns 1 |
| 3485 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3486 | * |
| 3487 | * Returns 1 disttinct, 0 if equal |
| 3488 | */ |
| 3489 | static int |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3490 | xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1, |
| 3491 | xmlRelaxNGDefinePtr def2) { |
| 3492 | int ret = 1; |
| 3493 | xmlNode node; |
| 3494 | xmlNs ns; |
| 3495 | xmlRelaxNGValidCtxt ctxt; |
| 3496 | ctxt.flags = FLAGS_IGNORABLE; |
| 3497 | |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 3498 | memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 3499 | |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3500 | if ((def1->type == XML_RELAXNG_ELEMENT) || |
| 3501 | (def1->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3502 | if (def2->type == XML_RELAXNG_TEXT) |
| 3503 | return(1); |
| 3504 | if (def1->name != NULL) { |
| 3505 | node.name = def1->name; |
| 3506 | } else { |
| 3507 | node.name = invalidName; |
| 3508 | } |
| 3509 | node.ns = &ns; |
| 3510 | if (def1->ns != NULL) { |
| 3511 | if (def1->ns[0] == 0) { |
| 3512 | node.ns = NULL; |
| 3513 | } else { |
| 3514 | ns.href = def1->ns; |
| 3515 | } |
| 3516 | } else { |
| 3517 | ns.href = invalidName; |
| 3518 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3519 | if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3520 | if (def1->nameClass != NULL) { |
| 3521 | ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2); |
| 3522 | } else { |
| 3523 | ret = 0; |
| 3524 | } |
| 3525 | } else { |
| 3526 | ret = 1; |
| 3527 | } |
| 3528 | } else if (def1->type == XML_RELAXNG_TEXT) { |
| 3529 | if (def2->type == XML_RELAXNG_TEXT) |
| 3530 | return(0); |
| 3531 | return(1); |
| 3532 | } else if (def1->type == XML_RELAXNG_EXCEPT) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3533 | TODO |
| 3534 | ret = 0; |
| 3535 | } else { |
| 3536 | TODO |
| 3537 | ret = 0; |
| 3538 | } |
| 3539 | if (ret == 0) |
| 3540 | return(ret); |
| 3541 | if ((def2->type == XML_RELAXNG_ELEMENT) || |
| 3542 | (def2->type == XML_RELAXNG_ATTRIBUTE)) { |
| 3543 | if (def2->name != NULL) { |
| 3544 | node.name = def2->name; |
| 3545 | } else { |
| 3546 | node.name = invalidName; |
| 3547 | } |
| 3548 | node.ns = &ns; |
| 3549 | if (def2->ns != NULL) { |
| 3550 | if (def2->ns[0] == 0) { |
| 3551 | node.ns = NULL; |
| 3552 | } else { |
| 3553 | ns.href = def2->ns; |
| 3554 | } |
| 3555 | } else { |
| 3556 | ns.href = invalidName; |
| 3557 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3558 | if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3559 | if (def2->nameClass != NULL) { |
| 3560 | ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1); |
| 3561 | } else { |
| 3562 | ret = 0; |
| 3563 | } |
| 3564 | } else { |
| 3565 | ret = 1; |
| 3566 | } |
| 3567 | } else { |
| 3568 | TODO |
| 3569 | ret = 0; |
| 3570 | } |
| 3571 | |
| 3572 | return(ret); |
| 3573 | } |
| 3574 | |
| 3575 | /** |
| 3576 | * xmlRelaxNGCompareElemDefLists: |
| 3577 | * @ctxt: a Relax-NG parser context |
| 3578 | * @defs1: the first list of element/attribute defs |
| 3579 | * @defs2: the second list of element/attribute defs |
| 3580 | * |
| 3581 | * Compare the 2 lists of element or attribute definitions. The comparison |
| 3582 | * is that if both lists do not accept the same QNames, it returns 1 |
| 3583 | * If the 2 lists can accept the same QName the comparison returns 0 |
| 3584 | * |
| 3585 | * Returns 1 disttinct, 0 if equal |
| 3586 | */ |
| 3587 | static int |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3588 | xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 3589 | xmlRelaxNGDefinePtr *def1, |
| 3590 | xmlRelaxNGDefinePtr *def2) { |
| 3591 | xmlRelaxNGDefinePtr *basedef2 = def2; |
| 3592 | |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3593 | if ((def1 == NULL) || (def2 == NULL)) |
| 3594 | return(1); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3595 | if ((*def1 == NULL) || (*def2 == NULL)) |
| 3596 | return(1); |
| 3597 | while (*def1 != NULL) { |
| 3598 | while ((*def2) != NULL) { |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 3599 | if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0) |
| 3600 | return(0); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3601 | def2++; |
| 3602 | } |
| 3603 | def2 = basedef2; |
| 3604 | def1++; |
| 3605 | } |
| 3606 | return(1); |
| 3607 | } |
| 3608 | |
| 3609 | /** |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 3610 | * xmlRelaxNGGenerateAttributes: |
| 3611 | * @ctxt: a Relax-NG parser context |
| 3612 | * @def: the definition definition |
| 3613 | * |
| 3614 | * Check if the definition can only generate attributes |
| 3615 | * |
| 3616 | * Returns 1 if yes, 0 if no and -1 in case of error. |
| 3617 | */ |
| 3618 | static int |
| 3619 | xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt, |
| 3620 | xmlRelaxNGDefinePtr def) { |
| 3621 | xmlRelaxNGDefinePtr parent, cur, tmp; |
| 3622 | |
| 3623 | /* |
| 3624 | * Don't run that check in case of error. Infinite recursion |
| 3625 | * becomes possible. |
| 3626 | */ |
| 3627 | if (ctxt->nbErrors != 0) |
| 3628 | return(-1); |
| 3629 | |
| 3630 | parent = NULL; |
| 3631 | cur = def; |
| 3632 | while (cur != NULL) { |
| 3633 | if ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3634 | (cur->type == XML_RELAXNG_TEXT) || |
| 3635 | (cur->type == XML_RELAXNG_DATATYPE) || |
| 3636 | (cur->type == XML_RELAXNG_PARAM) || |
| 3637 | (cur->type == XML_RELAXNG_LIST) || |
| 3638 | (cur->type == XML_RELAXNG_VALUE) || |
| 3639 | (cur->type == XML_RELAXNG_EMPTY)) |
| 3640 | return(0); |
| 3641 | if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3642 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3643 | (cur->type == XML_RELAXNG_GROUP) || |
| 3644 | (cur->type == XML_RELAXNG_ONEORMORE) || |
| 3645 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3646 | (cur->type == XML_RELAXNG_OPTIONAL) || |
| 3647 | (cur->type == XML_RELAXNG_PARENTREF) || |
| 3648 | (cur->type == XML_RELAXNG_EXTERNALREF) || |
| 3649 | (cur->type == XML_RELAXNG_REF) || |
| 3650 | (cur->type == XML_RELAXNG_DEF)) { |
| 3651 | if (cur->content != NULL) { |
| 3652 | parent = cur; |
| 3653 | cur = cur->content; |
| 3654 | tmp = cur; |
| 3655 | while (tmp != NULL) { |
| 3656 | tmp->parent = parent; |
| 3657 | tmp = tmp->next; |
| 3658 | } |
| 3659 | continue; |
| 3660 | } |
| 3661 | } |
| 3662 | if (cur == def) |
| 3663 | break; |
| 3664 | if (cur->next != NULL) { |
| 3665 | cur = cur->next; |
| 3666 | continue; |
| 3667 | } |
| 3668 | do { |
| 3669 | cur = cur->parent; |
| 3670 | if (cur == NULL) break; |
| 3671 | if (cur == def) return(1); |
| 3672 | if (cur->next != NULL) { |
| 3673 | cur = cur->next; |
| 3674 | break; |
| 3675 | } |
| 3676 | } while (cur != NULL); |
| 3677 | } |
| 3678 | return(1); |
| 3679 | } |
| 3680 | |
| 3681 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3682 | * xmlRelaxNGGetElements: |
| 3683 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3684 | * @def: the definition definition |
| 3685 | * @eora: gather elements (0) or attributes (1) |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3686 | * |
| 3687 | * Compute the list of top elements a definition can generate |
| 3688 | * |
| 3689 | * Returns a list of elements or NULL if none was found. |
| 3690 | */ |
| 3691 | static xmlRelaxNGDefinePtr * |
| 3692 | xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt, |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3693 | xmlRelaxNGDefinePtr def, |
| 3694 | int eora) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3695 | xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3696 | int len = 0; |
| 3697 | int max = 0; |
| 3698 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3699 | /* |
| 3700 | * Don't run that check in case of error. Infinite recursion |
| 3701 | * becomes possible. |
| 3702 | */ |
| 3703 | if (ctxt->nbErrors != 0) |
| 3704 | return(NULL); |
| 3705 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3706 | parent = NULL; |
| 3707 | cur = def; |
| 3708 | while (cur != NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3709 | if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) || |
| 3710 | (cur->type == XML_RELAXNG_TEXT))) || |
| 3711 | ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3712 | if (ret == NULL) { |
| 3713 | max = 10; |
| 3714 | ret = (xmlRelaxNGDefinePtr *) |
| 3715 | xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3716 | if (ret == NULL) { |
| 3717 | if (ctxt->error != NULL) |
| 3718 | ctxt->error(ctxt->userData, |
| 3719 | "Out of memory in element search\n"); |
| 3720 | ctxt->nbErrors++; |
| 3721 | return(NULL); |
| 3722 | } |
| 3723 | } else if (max <= len) { |
| 3724 | max *= 2; |
| 3725 | ret = xmlRealloc(ret, (max + 1) * sizeof(xmlRelaxNGDefinePtr)); |
| 3726 | if (ret == NULL) { |
| 3727 | if (ctxt->error != NULL) |
| 3728 | ctxt->error(ctxt->userData, |
| 3729 | "Out of memory in element search\n"); |
| 3730 | ctxt->nbErrors++; |
| 3731 | return(NULL); |
| 3732 | } |
| 3733 | } |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3734 | ret[len++] = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3735 | ret[len] = NULL; |
| 3736 | } else if ((cur->type == XML_RELAXNG_CHOICE) || |
| 3737 | (cur->type == XML_RELAXNG_INTERLEAVE) || |
| 3738 | (cur->type == XML_RELAXNG_GROUP) || |
| 3739 | (cur->type == XML_RELAXNG_ONEORMORE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3740 | (cur->type == XML_RELAXNG_ZEROORMORE) || |
| 3741 | (cur->type == XML_RELAXNG_OPTIONAL) || |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 3742 | (cur->type == XML_RELAXNG_PARENTREF) || |
Daniel Veillard | b08c981 | 2003-01-28 23:09:49 +0000 | [diff] [blame] | 3743 | (cur->type == XML_RELAXNG_REF) || |
| 3744 | (cur->type == XML_RELAXNG_DEF)) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3745 | /* |
| 3746 | * Don't go within elements or attributes or string values. |
| 3747 | * Just gather the element top list |
| 3748 | */ |
| 3749 | if (cur->content != NULL) { |
| 3750 | parent = cur; |
| 3751 | cur = cur->content; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3752 | tmp = cur; |
| 3753 | while (tmp != NULL) { |
| 3754 | tmp->parent = parent; |
| 3755 | tmp = tmp->next; |
| 3756 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3757 | continue; |
| 3758 | } |
| 3759 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 3760 | if (cur == def) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3761 | break; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 3762 | if (cur->next != NULL) { |
| 3763 | cur = cur->next; |
| 3764 | continue; |
| 3765 | } |
| 3766 | do { |
| 3767 | cur = cur->parent; |
| 3768 | if (cur == NULL) break; |
| 3769 | if (cur == def) return(ret); |
| 3770 | if (cur->next != NULL) { |
| 3771 | cur = cur->next; |
| 3772 | break; |
| 3773 | } |
| 3774 | } while (cur != NULL); |
| 3775 | } |
| 3776 | return(ret); |
| 3777 | } |
| 3778 | |
| 3779 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3780 | * xmlRelaxNGCheckChoiceDeterminism: |
| 3781 | * @ctxt: a Relax-NG parser context |
| 3782 | * @def: the choice definition |
| 3783 | * |
| 3784 | * Also used to find indeterministic pattern in choice |
| 3785 | */ |
| 3786 | static void |
| 3787 | xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt, |
| 3788 | xmlRelaxNGDefinePtr def) { |
| 3789 | xmlRelaxNGDefinePtr **list; |
| 3790 | xmlRelaxNGDefinePtr cur; |
| 3791 | int nbchild = 0, i, j, ret; |
| 3792 | int is_nullable = 0; |
| 3793 | int is_indeterminist = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3794 | xmlHashTablePtr triage = NULL; |
| 3795 | int is_triable = 1; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3796 | |
| 3797 | if ((def == NULL) || |
| 3798 | (def->type != XML_RELAXNG_CHOICE)) |
| 3799 | return; |
| 3800 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3801 | if (def->dflags & IS_PROCESSED) |
| 3802 | return; |
| 3803 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3804 | /* |
| 3805 | * Don't run that check in case of error. Infinite recursion |
| 3806 | * becomes possible. |
| 3807 | */ |
| 3808 | if (ctxt->nbErrors != 0) |
| 3809 | return; |
| 3810 | |
| 3811 | is_nullable = xmlRelaxNGIsNullable(def); |
| 3812 | |
| 3813 | cur = def->content; |
| 3814 | while (cur != NULL) { |
| 3815 | nbchild++; |
| 3816 | cur = cur->next; |
| 3817 | } |
| 3818 | |
| 3819 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 3820 | sizeof(xmlRelaxNGDefinePtr *)); |
| 3821 | if (list == NULL) { |
| 3822 | if (ctxt->error != NULL) |
| 3823 | ctxt->error(ctxt->userData, |
| 3824 | "Out of memory in choice computation\n"); |
| 3825 | ctxt->nbErrors++; |
| 3826 | return; |
| 3827 | } |
| 3828 | i = 0; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3829 | /* |
| 3830 | * a bit strong but safe |
| 3831 | */ |
| 3832 | if (is_nullable == 0) { |
| 3833 | triage = xmlHashCreate(10); |
| 3834 | } else { |
| 3835 | is_triable = 0; |
| 3836 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3837 | cur = def->content; |
| 3838 | while (cur != NULL) { |
| 3839 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 0); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3840 | if ((list[i] == NULL) || (list[i][0] == NULL)) { |
| 3841 | is_triable = 0; |
| 3842 | } else if (is_triable == 1) { |
| 3843 | xmlRelaxNGDefinePtr *tmp; |
| 3844 | int res; |
| 3845 | |
| 3846 | tmp = list[i]; |
| 3847 | while ((*tmp != NULL) && (is_triable == 1)) { |
| 3848 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 3849 | res = xmlHashAddEntry2(triage, |
| 3850 | BAD_CAST "#text", NULL, |
| 3851 | (void *)cur); |
| 3852 | if (res != 0) |
| 3853 | is_triable = -1; |
| 3854 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 3855 | ((*tmp)->name != NULL)) { |
| 3856 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3857 | res = xmlHashAddEntry2(triage, |
| 3858 | (*tmp)->name, NULL, |
| 3859 | (void *)cur); |
| 3860 | else |
| 3861 | res = xmlHashAddEntry2(triage, |
| 3862 | (*tmp)->name, (*tmp)->ns, |
| 3863 | (void *)cur); |
| 3864 | if (res != 0) |
| 3865 | is_triable = -1; |
| 3866 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 3867 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 3868 | res = xmlHashAddEntry2(triage, |
| 3869 | BAD_CAST "#any", NULL, |
| 3870 | (void *)cur); |
| 3871 | else |
| 3872 | res = xmlHashAddEntry2(triage, |
| 3873 | BAD_CAST "#any", (*tmp)->ns, |
| 3874 | (void *)cur); |
| 3875 | if (res != 0) |
| 3876 | is_triable = -1; |
| 3877 | } else { |
| 3878 | is_triable = -1; |
| 3879 | } |
| 3880 | tmp++; |
| 3881 | } |
| 3882 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3883 | i++; |
| 3884 | cur = cur->next; |
| 3885 | } |
| 3886 | |
| 3887 | for (i = 0;i < nbchild;i++) { |
| 3888 | if (list[i] == NULL) |
| 3889 | continue; |
| 3890 | for (j = 0;j < i;j++) { |
| 3891 | if (list[j] == NULL) |
| 3892 | continue; |
| 3893 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 3894 | if (ret == 0) { |
| 3895 | is_indeterminist = 1; |
| 3896 | } |
| 3897 | } |
| 3898 | } |
| 3899 | for (i = 0;i < nbchild;i++) { |
| 3900 | if (list[i] != NULL) |
| 3901 | xmlFree(list[i]); |
| 3902 | } |
| 3903 | |
| 3904 | xmlFree(list); |
| 3905 | if (is_indeterminist) { |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3906 | def->dflags |= IS_INDETERMINIST; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3907 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3908 | if (is_triable == 1) { |
| 3909 | def->dflags |= IS_TRIABLE; |
| 3910 | def->data = triage; |
| 3911 | } else if (triage != NULL) { |
| 3912 | xmlHashFree(triage, NULL); |
| 3913 | } |
| 3914 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3915 | } |
| 3916 | |
| 3917 | /** |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3918 | * xmlRelaxNGCheckGroupAttrs: |
| 3919 | * @ctxt: a Relax-NG parser context |
| 3920 | * @def: the group definition |
| 3921 | * |
| 3922 | * Detects violations of rule 7.3 |
| 3923 | */ |
| 3924 | static void |
| 3925 | xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt, |
| 3926 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3927 | xmlRelaxNGDefinePtr **list; |
| 3928 | xmlRelaxNGDefinePtr cur; |
| 3929 | int nbchild = 0, i, j, ret; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3930 | |
| 3931 | if ((def == NULL) || |
| 3932 | ((def->type != XML_RELAXNG_GROUP) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3933 | (def->type != XML_RELAXNG_ELEMENT))) |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3934 | return; |
| 3935 | |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 3936 | if (def->dflags & IS_PROCESSED) |
| 3937 | return; |
| 3938 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3939 | /* |
| 3940 | * Don't run that check in case of error. Infinite recursion |
| 3941 | * becomes possible. |
| 3942 | */ |
| 3943 | if (ctxt->nbErrors != 0) |
| 3944 | return; |
| 3945 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3946 | cur = def->attrs; |
| 3947 | while (cur != NULL) { |
| 3948 | nbchild++; |
| 3949 | cur = cur->next; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 3950 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 3951 | cur = def->content; |
| 3952 | while (cur != NULL) { |
| 3953 | nbchild++; |
| 3954 | cur = cur->next; |
| 3955 | } |
| 3956 | |
| 3957 | list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild * |
| 3958 | sizeof(xmlRelaxNGDefinePtr *)); |
| 3959 | if (list == NULL) { |
| 3960 | if (ctxt->error != NULL) |
| 3961 | ctxt->error(ctxt->userData, |
| 3962 | "Out of memory in group computation\n"); |
| 3963 | ctxt->nbErrors++; |
| 3964 | return; |
| 3965 | } |
| 3966 | i = 0; |
| 3967 | cur = def->attrs; |
| 3968 | while (cur != NULL) { |
| 3969 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 3970 | i++; |
| 3971 | cur = cur->next; |
| 3972 | } |
| 3973 | cur = def->content; |
| 3974 | while (cur != NULL) { |
| 3975 | list[i] = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 3976 | i++; |
| 3977 | cur = cur->next; |
| 3978 | } |
| 3979 | |
| 3980 | for (i = 0;i < nbchild;i++) { |
| 3981 | if (list[i] == NULL) |
| 3982 | continue; |
| 3983 | for (j = 0;j < i;j++) { |
| 3984 | if (list[j] == NULL) |
| 3985 | continue; |
| 3986 | ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]); |
| 3987 | if (ret == 0) { |
| 3988 | if (ctxt->error != NULL) |
| 3989 | ctxt->error(ctxt->userData, |
| 3990 | "Attributes conflicts in group\n"); |
| 3991 | ctxt->nbErrors++; |
| 3992 | } |
| 3993 | } |
| 3994 | } |
| 3995 | for (i = 0;i < nbchild;i++) { |
| 3996 | if (list[i] != NULL) |
| 3997 | xmlFree(list[i]); |
| 3998 | } |
| 3999 | |
| 4000 | xmlFree(list); |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4001 | def->dflags |= IS_PROCESSED; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4005 | * xmlRelaxNGComputeInterleaves: |
| 4006 | * @def: the interleave definition |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4007 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4008 | * @name: the definition name |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4009 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4010 | * A lot of work for preprocessing interleave definitions |
| 4011 | * is potentially needed to get a decent execution speed at runtime |
| 4012 | * - trying to get a total order on the element nodes generated |
| 4013 | * by the interleaves, order the list of interleave definitions |
| 4014 | * following that order. |
| 4015 | * - if <text/> is used to handle mixed content, it is better to |
| 4016 | * flag this in the define and simplify the runtime checking |
| 4017 | * algorithm |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4018 | */ |
| 4019 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4020 | xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def, |
| 4021 | xmlRelaxNGParserCtxtPtr ctxt, |
| 4022 | xmlChar *name ATTRIBUTE_UNUSED) { |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4023 | xmlRelaxNGDefinePtr cur, *tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4024 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4025 | xmlRelaxNGPartitionPtr partitions = NULL; |
| 4026 | xmlRelaxNGInterleaveGroupPtr *groups = NULL; |
| 4027 | xmlRelaxNGInterleaveGroupPtr group; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4028 | int i,j,ret, res; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4029 | int nbgroups = 0; |
| 4030 | int nbchild = 0; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4031 | int is_mixed = 0; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4032 | int is_determinist = 1; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4033 | |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 4034 | /* |
| 4035 | * Don't run that check in case of error. Infinite recursion |
| 4036 | * becomes possible. |
| 4037 | */ |
| 4038 | if (ctxt->nbErrors != 0) |
| 4039 | return; |
| 4040 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4041 | #ifdef DEBUG_INTERLEAVE |
| 4042 | xmlGenericError(xmlGenericErrorContext, |
| 4043 | "xmlRelaxNGComputeInterleaves(%s)\n", |
| 4044 | name); |
| 4045 | #endif |
| 4046 | cur = def->content; |
| 4047 | while (cur != NULL) { |
| 4048 | nbchild++; |
| 4049 | cur = cur->next; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4050 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4051 | |
| 4052 | #ifdef DEBUG_INTERLEAVE |
| 4053 | xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild); |
| 4054 | #endif |
| 4055 | groups = (xmlRelaxNGInterleaveGroupPtr *) |
| 4056 | xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr)); |
| 4057 | if (groups == NULL) |
| 4058 | goto error; |
| 4059 | cur = def->content; |
| 4060 | while (cur != NULL) { |
| 4061 | groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr) |
| 4062 | xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup)); |
| 4063 | if (groups[nbgroups] == NULL) |
| 4064 | goto error; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4065 | if (cur->type == XML_RELAXNG_TEXT) |
| 4066 | is_mixed++; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4067 | groups[nbgroups]->rule = cur; |
| 4068 | groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0); |
| 4069 | groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1); |
| 4070 | nbgroups++; |
| 4071 | cur = cur->next; |
| 4072 | } |
| 4073 | #ifdef DEBUG_INTERLEAVE |
| 4074 | xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups); |
| 4075 | #endif |
| 4076 | |
| 4077 | /* |
| 4078 | * Let's check that all rules makes a partitions according to 7.4 |
| 4079 | */ |
| 4080 | partitions = (xmlRelaxNGPartitionPtr) |
| 4081 | xmlMalloc(sizeof(xmlRelaxNGPartition)); |
| 4082 | if (partitions == NULL) |
| 4083 | goto error; |
Daniel Veillard | 2086382 | 2003-03-22 17:51:47 +0000 | [diff] [blame] | 4084 | memset(partitions, 0, sizeof(xmlRelaxNGPartition)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4085 | partitions->nbgroups = nbgroups; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4086 | partitions->triage = xmlHashCreate(nbgroups); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4087 | for (i = 0;i < nbgroups;i++) { |
| 4088 | group = groups[i]; |
| 4089 | for (j = i+1;j < nbgroups;j++) { |
| 4090 | if (groups[j] == NULL) |
| 4091 | continue; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4092 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4093 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs, |
| 4094 | groups[j]->defs); |
| 4095 | if (ret == 0) { |
| 4096 | if (ctxt->error != NULL) |
| 4097 | ctxt->error(ctxt->userData, |
| 4098 | "Element or text conflicts in interleave\n"); |
| 4099 | ctxt->nbErrors++; |
| 4100 | } |
| 4101 | ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs, |
| 4102 | groups[j]->attrs); |
| 4103 | if (ret == 0) { |
| 4104 | if (ctxt->error != NULL) |
| 4105 | ctxt->error(ctxt->userData, |
| 4106 | "Attributes conflicts in interleave\n"); |
| 4107 | ctxt->nbErrors++; |
| 4108 | } |
| 4109 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4110 | tmp = group->defs; |
| 4111 | if ((tmp != NULL) && (*tmp != NULL)) { |
| 4112 | while (*tmp != NULL) { |
| 4113 | if ((*tmp)->type == XML_RELAXNG_TEXT) { |
| 4114 | res = xmlHashAddEntry2(partitions->triage, |
| 4115 | BAD_CAST "#text", NULL, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4116 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4117 | if (res != 0) |
| 4118 | is_determinist = -1; |
| 4119 | } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) && |
| 4120 | ((*tmp)->name != NULL)) { |
| 4121 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4122 | res = xmlHashAddEntry2(partitions->triage, |
| 4123 | (*tmp)->name, NULL, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4124 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4125 | else |
| 4126 | res = xmlHashAddEntry2(partitions->triage, |
| 4127 | (*tmp)->name, (*tmp)->ns, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4128 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4129 | if (res != 0) |
| 4130 | is_determinist = -1; |
| 4131 | } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) { |
| 4132 | if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0)) |
| 4133 | res = xmlHashAddEntry2(partitions->triage, |
| 4134 | BAD_CAST "#any", NULL, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4135 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4136 | else |
| 4137 | res = xmlHashAddEntry2(partitions->triage, |
| 4138 | BAD_CAST "#any", (*tmp)->ns, |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 4139 | (void *)(long)(i + 1)); |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4140 | if ((*tmp)->nameClass != NULL) |
| 4141 | is_determinist = 2; |
| 4142 | if (res != 0) |
| 4143 | is_determinist = -1; |
| 4144 | } else { |
| 4145 | is_determinist = -1; |
| 4146 | } |
| 4147 | tmp++; |
| 4148 | } |
| 4149 | } else { |
| 4150 | is_determinist = 0; |
| 4151 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4152 | } |
| 4153 | partitions->groups = groups; |
| 4154 | |
| 4155 | /* |
| 4156 | * and save the partition list back in the def |
| 4157 | */ |
| 4158 | def->data = partitions; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 4159 | if (is_mixed != 0) |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 4160 | def->dflags |= IS_MIXED; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 4161 | if (is_determinist == 1) |
| 4162 | partitions->flags = IS_DETERMINIST; |
| 4163 | if (is_determinist == 2) |
| 4164 | partitions->flags = IS_DETERMINIST | IS_NEEDCHECK; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4165 | return; |
| 4166 | |
| 4167 | error: |
| 4168 | if (ctxt->error != NULL) |
| 4169 | ctxt->error(ctxt->userData, |
| 4170 | "Out of memory in interleave computation\n"); |
| 4171 | ctxt->nbErrors++; |
| 4172 | if (groups != NULL) { |
| 4173 | for (i = 0;i < nbgroups;i++) |
| 4174 | if (groups[i] != NULL) { |
| 4175 | if (groups[i]->defs != NULL) |
| 4176 | xmlFree(groups[i]->defs); |
| 4177 | xmlFree(groups[i]); |
| 4178 | } |
| 4179 | xmlFree(groups); |
| 4180 | } |
| 4181 | xmlRelaxNGFreePartition(partitions); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4182 | } |
| 4183 | |
| 4184 | /** |
| 4185 | * xmlRelaxNGParseInterleave: |
| 4186 | * @ctxt: a Relax-NG parser context |
| 4187 | * @node: the data node. |
| 4188 | * |
| 4189 | * parse the content of a RelaxNG interleave node. |
| 4190 | * |
| 4191 | * Returns the definition pointer or NULL in case of error |
| 4192 | */ |
| 4193 | static xmlRelaxNGDefinePtr |
| 4194 | xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4195 | xmlRelaxNGDefinePtr def = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4196 | xmlRelaxNGDefinePtr last = NULL, cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4197 | xmlNodePtr child; |
| 4198 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4199 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4200 | if (def == NULL) { |
| 4201 | return(NULL); |
| 4202 | } |
| 4203 | def->type = XML_RELAXNG_INTERLEAVE; |
| 4204 | |
| 4205 | if (ctxt->interleaves == NULL) |
| 4206 | ctxt->interleaves = xmlHashCreate(10); |
| 4207 | if (ctxt->interleaves == NULL) { |
| 4208 | if (ctxt->error != NULL) |
| 4209 | ctxt->error(ctxt->userData, |
| 4210 | "Failed to create interleaves hash table\n"); |
| 4211 | ctxt->nbErrors++; |
| 4212 | } else { |
| 4213 | char name[32]; |
| 4214 | |
| 4215 | snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++); |
| 4216 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) { |
| 4217 | if (ctxt->error != NULL) |
| 4218 | ctxt->error(ctxt->userData, |
| 4219 | "Failed to add %s to hash table\n", name); |
| 4220 | ctxt->nbErrors++; |
| 4221 | } |
| 4222 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4223 | child = node->children; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4224 | if (child == NULL) { |
| 4225 | if (ctxt->error != NULL) |
| 4226 | ctxt->error(ctxt->userData, "Element interleave is empty\n"); |
| 4227 | ctxt->nbErrors++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 4228 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4229 | while (child != NULL) { |
| 4230 | if (IS_RELAXNG(child, "element")) { |
| 4231 | cur = xmlRelaxNGParseElement(ctxt, child); |
| 4232 | } else { |
| 4233 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4234 | } |
| 4235 | if (cur != NULL) { |
| 4236 | cur->parent = def; |
| 4237 | if (last == NULL) { |
| 4238 | def->content = last = cur; |
| 4239 | } else { |
| 4240 | last->next = cur; |
| 4241 | last = cur; |
| 4242 | } |
| 4243 | } |
| 4244 | child = child->next; |
| 4245 | } |
| 4246 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4247 | return(def); |
| 4248 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4249 | |
| 4250 | /** |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4251 | * xmlRelaxNGParseInclude: |
| 4252 | * @ctxt: a Relax-NG parser context |
| 4253 | * @node: the include node |
| 4254 | * |
| 4255 | * Integrate the content of an include node in the current grammar |
| 4256 | * |
| 4257 | * Returns 0 in case of success or -1 in case of error |
| 4258 | */ |
| 4259 | static int |
| 4260 | xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4261 | xmlRelaxNGIncludePtr incl; |
| 4262 | xmlNodePtr root; |
| 4263 | int ret = 0, tmp; |
| 4264 | |
| 4265 | incl = node->_private; |
| 4266 | if (incl == NULL) { |
| 4267 | if (ctxt->error != NULL) |
| 4268 | ctxt->error(ctxt->userData, |
| 4269 | "Include node has no data\n"); |
| 4270 | ctxt->nbErrors++; |
| 4271 | return(-1); |
| 4272 | } |
| 4273 | root = xmlDocGetRootElement(incl->doc); |
| 4274 | if (root == NULL) { |
| 4275 | if (ctxt->error != NULL) |
| 4276 | ctxt->error(ctxt->userData, |
| 4277 | "Include document is empty\n"); |
| 4278 | ctxt->nbErrors++; |
| 4279 | return(-1); |
| 4280 | } |
| 4281 | if (!xmlStrEqual(root->name, BAD_CAST "grammar")) { |
| 4282 | if (ctxt->error != NULL) |
| 4283 | ctxt->error(ctxt->userData, |
| 4284 | "Include document root is not a grammar\n"); |
| 4285 | ctxt->nbErrors++; |
| 4286 | return(-1); |
| 4287 | } |
| 4288 | |
| 4289 | /* |
| 4290 | * Merge the definition from both the include and the internal list |
| 4291 | */ |
| 4292 | if (root->children != NULL) { |
| 4293 | tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children); |
| 4294 | if (tmp != 0) |
| 4295 | ret = -1; |
| 4296 | } |
| 4297 | if (node->children != NULL) { |
| 4298 | tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children); |
| 4299 | if (tmp != 0) |
| 4300 | ret = -1; |
| 4301 | } |
| 4302 | return(ret); |
| 4303 | } |
| 4304 | |
| 4305 | /** |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4306 | * xmlRelaxNGParseDefine: |
| 4307 | * @ctxt: a Relax-NG parser context |
| 4308 | * @node: the define node |
| 4309 | * |
| 4310 | * parse the content of a RelaxNG define element node. |
| 4311 | * |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4312 | * Returns 0 in case of success or -1 in case of error |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4313 | */ |
| 4314 | static int |
| 4315 | xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4316 | xmlChar *name; |
| 4317 | int ret = 0, tmp; |
| 4318 | xmlRelaxNGDefinePtr def; |
| 4319 | const xmlChar *olddefine; |
| 4320 | |
| 4321 | name = xmlGetProp(node, BAD_CAST "name"); |
| 4322 | if (name == NULL) { |
| 4323 | if (ctxt->error != NULL) |
| 4324 | ctxt->error(ctxt->userData, |
| 4325 | "define has no name\n"); |
| 4326 | ctxt->nbErrors++; |
| 4327 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4328 | xmlRelaxNGNormExtSpace(name); |
| 4329 | if (xmlValidateNCName(name, 0)) { |
| 4330 | if (ctxt->error != NULL) |
| 4331 | ctxt->error(ctxt->userData, |
| 4332 | "define name '%s' is not an NCName\n", |
| 4333 | name); |
| 4334 | ctxt->nbErrors++; |
| 4335 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4336 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4337 | if (def == NULL) { |
| 4338 | xmlFree(name); |
| 4339 | return(-1); |
| 4340 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4341 | def->type = XML_RELAXNG_DEF; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4342 | def->name = name; |
| 4343 | if (node->children == NULL) { |
| 4344 | if (ctxt->error != NULL) |
| 4345 | ctxt->error(ctxt->userData, |
| 4346 | "define has no children\n"); |
| 4347 | ctxt->nbErrors++; |
| 4348 | } else { |
| 4349 | olddefine = ctxt->define; |
| 4350 | ctxt->define = name; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4351 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4352 | ctxt->define = olddefine; |
| 4353 | } |
| 4354 | if (ctxt->grammar->defs == NULL) |
| 4355 | ctxt->grammar->defs = xmlHashCreate(10); |
| 4356 | if (ctxt->grammar->defs == NULL) { |
| 4357 | if (ctxt->error != NULL) |
| 4358 | ctxt->error(ctxt->userData, |
| 4359 | "Could not create definition hash\n"); |
| 4360 | ctxt->nbErrors++; |
| 4361 | ret = -1; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4362 | } else { |
| 4363 | tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def); |
| 4364 | if (tmp < 0) { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4365 | xmlRelaxNGDefinePtr prev; |
| 4366 | |
| 4367 | prev = xmlHashLookup(ctxt->grammar->defs, name); |
| 4368 | if (prev == NULL) { |
| 4369 | if (ctxt->error != NULL) |
| 4370 | ctxt->error(ctxt->userData, |
| 4371 | "Internal error on define aggregation of %s\n", |
| 4372 | name); |
| 4373 | ctxt->nbErrors++; |
| 4374 | ret = -1; |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 4375 | } else { |
| 4376 | while (prev->nextHash != NULL) |
| 4377 | prev = prev->nextHash; |
| 4378 | prev->nextHash = def; |
| 4379 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4380 | } |
| 4381 | } |
| 4382 | } |
| 4383 | return(ret); |
| 4384 | } |
| 4385 | |
| 4386 | /** |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4387 | * xmlRelaxNGProcessExternalRef: |
| 4388 | * @ctxt: the parser context |
| 4389 | * @node: the externlRef node |
| 4390 | * |
| 4391 | * Process and compile an externlRef node |
| 4392 | * |
| 4393 | * Returns the xmlRelaxNGDefinePtr or NULL in case of error |
| 4394 | */ |
| 4395 | static xmlRelaxNGDefinePtr |
| 4396 | xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4397 | xmlRelaxNGDocumentPtr docu; |
| 4398 | xmlNodePtr root, tmp; |
| 4399 | xmlChar *ns; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4400 | int newNs = 0, oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4401 | xmlRelaxNGDefinePtr def; |
| 4402 | |
| 4403 | docu = node->_private; |
| 4404 | if (docu != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4405 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4406 | if (def == NULL) |
| 4407 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4408 | def->type = XML_RELAXNG_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4409 | |
| 4410 | if (docu->content == NULL) { |
| 4411 | /* |
| 4412 | * Then do the parsing for good |
| 4413 | */ |
| 4414 | root = xmlDocGetRootElement(docu->doc); |
| 4415 | if (root == NULL) { |
| 4416 | if (ctxt->error != NULL) |
| 4417 | ctxt->error(ctxt->userData, |
| 4418 | "xmlRelaxNGParse: %s is empty\n", |
| 4419 | ctxt->URL); |
| 4420 | ctxt->nbErrors++; |
| 4421 | return (NULL); |
| 4422 | } |
| 4423 | /* |
| 4424 | * ns transmission rules |
| 4425 | */ |
| 4426 | ns = xmlGetProp(root, BAD_CAST "ns"); |
| 4427 | if (ns == NULL) { |
| 4428 | tmp = node; |
| 4429 | while ((tmp != NULL) && |
| 4430 | (tmp->type == XML_ELEMENT_NODE)) { |
| 4431 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 4432 | if (ns != NULL) { |
| 4433 | break; |
| 4434 | } |
| 4435 | tmp = tmp->parent; |
| 4436 | } |
| 4437 | if (ns != NULL) { |
| 4438 | xmlSetProp(root, BAD_CAST "ns", ns); |
| 4439 | newNs = 1; |
| 4440 | xmlFree(ns); |
| 4441 | } |
| 4442 | } else { |
| 4443 | xmlFree(ns); |
| 4444 | } |
| 4445 | |
| 4446 | /* |
| 4447 | * Parsing to get a precompiled schemas. |
| 4448 | */ |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4449 | oldflags = ctxt->flags; |
| 4450 | ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4451 | docu->schema = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4452 | ctxt->flags = oldflags; |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4453 | if ((docu->schema != NULL) && |
| 4454 | (docu->schema->topgrammar != NULL)) { |
| 4455 | docu->content = docu->schema->topgrammar->start; |
| 4456 | } |
| 4457 | |
| 4458 | /* |
| 4459 | * the externalRef may be reused in a different ns context |
| 4460 | */ |
| 4461 | if (newNs == 1) { |
| 4462 | xmlUnsetProp(root, BAD_CAST "ns"); |
| 4463 | } |
| 4464 | } |
| 4465 | def->content = docu->content; |
| 4466 | } else { |
| 4467 | def = NULL; |
| 4468 | } |
| 4469 | return(def); |
| 4470 | } |
| 4471 | |
| 4472 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4473 | * xmlRelaxNGParsePattern: |
| 4474 | * @ctxt: a Relax-NG parser context |
| 4475 | * @node: the pattern node. |
| 4476 | * |
| 4477 | * parse the content of a RelaxNG pattern node. |
| 4478 | * |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4479 | * Returns the definition pointer or NULL in case of error or if no |
| 4480 | * pattern is generated. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4481 | */ |
| 4482 | static xmlRelaxNGDefinePtr |
| 4483 | xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 4484 | xmlRelaxNGDefinePtr def = NULL; |
| 4485 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4486 | if (node == NULL) { |
| 4487 | return(NULL); |
| 4488 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4489 | if (IS_RELAXNG(node, "element")) { |
| 4490 | def = xmlRelaxNGParseElement(ctxt, node); |
| 4491 | } else if (IS_RELAXNG(node, "attribute")) { |
| 4492 | def = xmlRelaxNGParseAttribute(ctxt, node); |
| 4493 | } else if (IS_RELAXNG(node, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4494 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4495 | if (def == NULL) |
| 4496 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4497 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4498 | if (node->children != NULL) { |
| 4499 | if (ctxt->error != NULL) |
| 4500 | ctxt->error(ctxt->userData, "empty: had a child node\n"); |
| 4501 | ctxt->nbErrors++; |
| 4502 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4503 | } else if (IS_RELAXNG(node, "text")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4504 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4505 | if (def == NULL) |
| 4506 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4507 | def->type = XML_RELAXNG_TEXT; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4508 | if (node->children != NULL) { |
| 4509 | if (ctxt->error != NULL) |
| 4510 | ctxt->error(ctxt->userData, "text: had a child node\n"); |
| 4511 | ctxt->nbErrors++; |
| 4512 | } |
| 4513 | } else if (IS_RELAXNG(node, "zeroOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4514 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4515 | if (def == NULL) |
| 4516 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4517 | def->type = XML_RELAXNG_ZEROORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4518 | if (node->children == NULL) { |
| 4519 | if (ctxt->error != NULL) |
| 4520 | ctxt->error(ctxt->userData, |
| 4521 | "Element %s is empty\n", node->name); |
| 4522 | ctxt->nbErrors++; |
| 4523 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4524 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4525 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4526 | } else if (IS_RELAXNG(node, "oneOrMore")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4527 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4528 | if (def == NULL) |
| 4529 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4530 | def->type = XML_RELAXNG_ONEORMORE; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4531 | if (node->children == NULL) { |
| 4532 | if (ctxt->error != NULL) |
| 4533 | ctxt->error(ctxt->userData, |
| 4534 | "Element %s is empty\n", node->name); |
| 4535 | ctxt->nbErrors++; |
| 4536 | } else { |
| 4537 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4538 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4539 | } else if (IS_RELAXNG(node, "optional")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4540 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4541 | if (def == NULL) |
| 4542 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4543 | def->type = XML_RELAXNG_OPTIONAL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4544 | if (node->children == NULL) { |
| 4545 | if (ctxt->error != NULL) |
| 4546 | ctxt->error(ctxt->userData, |
| 4547 | "Element %s is empty\n", node->name); |
| 4548 | ctxt->nbErrors++; |
| 4549 | } else { |
| 4550 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1); |
| 4551 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4552 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4553 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4554 | if (def == NULL) |
| 4555 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4556 | def->type = XML_RELAXNG_CHOICE; |
| 4557 | if (node->children == NULL) { |
| 4558 | if (ctxt->error != NULL) |
| 4559 | ctxt->error(ctxt->userData, |
| 4560 | "Element %s is empty\n", node->name); |
| 4561 | ctxt->nbErrors++; |
| 4562 | } else { |
| 4563 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4564 | } |
| 4565 | } else if (IS_RELAXNG(node, "group")) { |
| 4566 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4567 | if (def == NULL) |
| 4568 | return(NULL); |
| 4569 | def->type = XML_RELAXNG_GROUP; |
| 4570 | if (node->children == NULL) { |
| 4571 | if (ctxt->error != NULL) |
| 4572 | ctxt->error(ctxt->userData, |
| 4573 | "Element %s is empty\n", node->name); |
| 4574 | ctxt->nbErrors++; |
| 4575 | } else { |
| 4576 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4577 | } |
| 4578 | } else if (IS_RELAXNG(node, "ref")) { |
| 4579 | def = xmlRelaxNGNewDefine(ctxt, node); |
| 4580 | if (def == NULL) |
| 4581 | return(NULL); |
| 4582 | def->type = XML_RELAXNG_REF; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4583 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4584 | if (def->name == NULL) { |
| 4585 | if (ctxt->error != NULL) |
| 4586 | ctxt->error(ctxt->userData, |
| 4587 | "ref has no name\n"); |
| 4588 | ctxt->nbErrors++; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 4589 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4590 | xmlRelaxNGNormExtSpace(def->name); |
| 4591 | if (xmlValidateNCName(def->name, 0)) { |
| 4592 | if (ctxt->error != NULL) |
| 4593 | ctxt->error(ctxt->userData, |
| 4594 | "ref name '%s' is not an NCName\n", |
| 4595 | def->name); |
| 4596 | ctxt->nbErrors++; |
| 4597 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4598 | } |
| 4599 | if (node->children != NULL) { |
| 4600 | if (ctxt->error != NULL) |
| 4601 | ctxt->error(ctxt->userData, |
| 4602 | "ref is not empty\n"); |
| 4603 | ctxt->nbErrors++; |
| 4604 | } |
| 4605 | if (ctxt->grammar->refs == NULL) |
| 4606 | ctxt->grammar->refs = xmlHashCreate(10); |
| 4607 | if (ctxt->grammar->refs == NULL) { |
| 4608 | if (ctxt->error != NULL) |
| 4609 | ctxt->error(ctxt->userData, |
| 4610 | "Could not create references hash\n"); |
| 4611 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4612 | def = NULL; |
| 4613 | } else { |
| 4614 | int tmp; |
| 4615 | |
| 4616 | tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def); |
| 4617 | if (tmp < 0) { |
| 4618 | xmlRelaxNGDefinePtr prev; |
| 4619 | |
| 4620 | prev = (xmlRelaxNGDefinePtr) |
| 4621 | xmlHashLookup(ctxt->grammar->refs, def->name); |
| 4622 | if (prev == NULL) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4623 | if (def->name != NULL) { |
| 4624 | if (ctxt->error != NULL) |
| 4625 | ctxt->error(ctxt->userData, |
| 4626 | "Error refs definitions '%s'\n", |
| 4627 | def->name); |
| 4628 | } else { |
| 4629 | if (ctxt->error != NULL) |
| 4630 | ctxt->error(ctxt->userData, |
| 4631 | "Error refs definitions\n"); |
| 4632 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4633 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4634 | def = NULL; |
| 4635 | } else { |
| 4636 | def->nextHash = prev->nextHash; |
| 4637 | prev->nextHash = def; |
| 4638 | } |
| 4639 | } |
| 4640 | } |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 4641 | } else if (IS_RELAXNG(node, "data")) { |
| 4642 | def = xmlRelaxNGParseData(ctxt, node); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 4643 | } else if (IS_RELAXNG(node, "value")) { |
| 4644 | def = xmlRelaxNGParseValue(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4645 | } else if (IS_RELAXNG(node, "list")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4646 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 4647 | if (def == NULL) |
| 4648 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4649 | def->type = XML_RELAXNG_LIST; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4650 | if (node->children == NULL) { |
| 4651 | if (ctxt->error != NULL) |
| 4652 | ctxt->error(ctxt->userData, |
| 4653 | "Element %s is empty\n", node->name); |
| 4654 | ctxt->nbErrors++; |
| 4655 | } else { |
| 4656 | def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0); |
| 4657 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4658 | } else if (IS_RELAXNG(node, "interleave")) { |
| 4659 | def = xmlRelaxNGParseInterleave(ctxt, node); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4660 | } else if (IS_RELAXNG(node, "externalRef")) { |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 4661 | def = xmlRelaxNGProcessExternalRef(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4662 | } else if (IS_RELAXNG(node, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4663 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4664 | if (def == NULL) |
| 4665 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4666 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4667 | if (node->children != NULL) { |
| 4668 | if (ctxt->error != NULL) |
| 4669 | ctxt->error(ctxt->userData, |
| 4670 | "xmlRelaxNGParse: notAllowed element is not empty\n"); |
| 4671 | ctxt->nbErrors++; |
| 4672 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4673 | } else if (IS_RELAXNG(node, "grammar")) { |
| 4674 | xmlRelaxNGGrammarPtr grammar, old; |
| 4675 | xmlRelaxNGGrammarPtr oldparent; |
| 4676 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4677 | #ifdef DEBUG_GRAMMAR |
| 4678 | xmlGenericError(xmlGenericErrorContext, "Found <grammar> pattern\n"); |
| 4679 | #endif |
| 4680 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4681 | oldparent = ctxt->parentgrammar; |
| 4682 | old = ctxt->grammar; |
| 4683 | ctxt->parentgrammar = old; |
| 4684 | grammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 4685 | if (old != NULL) { |
| 4686 | ctxt->grammar = old; |
| 4687 | ctxt->parentgrammar = oldparent; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4688 | #if 0 |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4689 | if (grammar != NULL) { |
| 4690 | grammar->next = old->next; |
| 4691 | old->next = grammar; |
| 4692 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 4693 | #endif |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4694 | } |
| 4695 | if (grammar != NULL) |
| 4696 | def = grammar->start; |
| 4697 | else |
| 4698 | def = NULL; |
| 4699 | } else if (IS_RELAXNG(node, "parentRef")) { |
| 4700 | if (ctxt->parentgrammar == NULL) { |
| 4701 | if (ctxt->error != NULL) |
| 4702 | ctxt->error(ctxt->userData, |
| 4703 | "Use of parentRef without a parent grammar\n"); |
| 4704 | ctxt->nbErrors++; |
| 4705 | return(NULL); |
| 4706 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4707 | def = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4708 | if (def == NULL) |
| 4709 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4710 | def->type = XML_RELAXNG_PARENTREF; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4711 | def->name = xmlGetProp(node, BAD_CAST "name"); |
| 4712 | if (def->name == NULL) { |
| 4713 | if (ctxt->error != NULL) |
| 4714 | ctxt->error(ctxt->userData, |
| 4715 | "parentRef has no name\n"); |
| 4716 | ctxt->nbErrors++; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4717 | } else { |
| 4718 | xmlRelaxNGNormExtSpace(def->name); |
| 4719 | if (xmlValidateNCName(def->name, 0)) { |
| 4720 | if (ctxt->error != NULL) |
| 4721 | ctxt->error(ctxt->userData, |
| 4722 | "parentRef name '%s' is not an NCName\n", |
| 4723 | def->name); |
| 4724 | ctxt->nbErrors++; |
| 4725 | } |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4726 | } |
| 4727 | if (node->children != NULL) { |
| 4728 | if (ctxt->error != NULL) |
| 4729 | ctxt->error(ctxt->userData, |
| 4730 | "parentRef is not empty\n"); |
| 4731 | ctxt->nbErrors++; |
| 4732 | } |
| 4733 | if (ctxt->parentgrammar->refs == NULL) |
| 4734 | ctxt->parentgrammar->refs = xmlHashCreate(10); |
| 4735 | if (ctxt->parentgrammar->refs == NULL) { |
| 4736 | if (ctxt->error != NULL) |
| 4737 | ctxt->error(ctxt->userData, |
| 4738 | "Could not create references hash\n"); |
| 4739 | ctxt->nbErrors++; |
| 4740 | def = NULL; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4741 | } else if (def->name != NULL) { |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4742 | int tmp; |
| 4743 | |
| 4744 | tmp = xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def); |
| 4745 | if (tmp < 0) { |
| 4746 | xmlRelaxNGDefinePtr prev; |
| 4747 | |
| 4748 | prev = (xmlRelaxNGDefinePtr) |
| 4749 | xmlHashLookup(ctxt->parentgrammar->refs, def->name); |
| 4750 | if (prev == NULL) { |
| 4751 | if (ctxt->error != NULL) |
| 4752 | ctxt->error(ctxt->userData, |
| 4753 | "Internal error parentRef definitions '%s'\n", |
| 4754 | def->name); |
| 4755 | ctxt->nbErrors++; |
| 4756 | def = NULL; |
| 4757 | } else { |
| 4758 | def->nextHash = prev->nextHash; |
| 4759 | prev->nextHash = def; |
| 4760 | } |
| 4761 | } |
| 4762 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4763 | } else if (IS_RELAXNG(node, "mixed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4764 | if (node->children == NULL) { |
| 4765 | if (ctxt->error != NULL) |
| 4766 | ctxt->error(ctxt->userData, |
| 4767 | "Mixed is empty\n"); |
| 4768 | ctxt->nbErrors++; |
| 4769 | def = NULL; |
| 4770 | } else { |
| 4771 | def = xmlRelaxNGParseInterleave(ctxt, node); |
| 4772 | if (def != NULL) { |
| 4773 | xmlRelaxNGDefinePtr tmp; |
| 4774 | |
| 4775 | if ((def->content != NULL) && (def->content->next != NULL)) { |
| 4776 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4777 | if (tmp != NULL) { |
| 4778 | tmp->type = XML_RELAXNG_GROUP; |
| 4779 | tmp->content = def->content; |
| 4780 | def->content = tmp; |
| 4781 | } |
| 4782 | } |
| 4783 | |
| 4784 | tmp = xmlRelaxNGNewDefine(ctxt, node); |
| 4785 | if (tmp == NULL) |
| 4786 | return(def); |
| 4787 | tmp->type = XML_RELAXNG_TEXT; |
| 4788 | tmp->next = def->content; |
| 4789 | def->content = tmp; |
| 4790 | } |
| 4791 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4792 | } else { |
| 4793 | if (ctxt->error != NULL) |
| 4794 | ctxt->error(ctxt->userData, |
| 4795 | "Unexpected node %s is not a pattern\n", |
| 4796 | node->name); |
| 4797 | ctxt->nbErrors++; |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 4798 | def = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4799 | } |
| 4800 | return(def); |
| 4801 | } |
| 4802 | |
| 4803 | /** |
| 4804 | * xmlRelaxNGParseAttribute: |
| 4805 | * @ctxt: a Relax-NG parser context |
| 4806 | * @node: the element node |
| 4807 | * |
| 4808 | * parse the content of a RelaxNG attribute node. |
| 4809 | * |
| 4810 | * Returns the definition pointer or NULL in case of error. |
| 4811 | */ |
| 4812 | static xmlRelaxNGDefinePtr |
| 4813 | xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4814 | xmlRelaxNGDefinePtr ret, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4815 | xmlNodePtr child; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4816 | int old_flags; |
| 4817 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4818 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4819 | if (ret == NULL) |
| 4820 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4821 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4822 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4823 | child = node->children; |
| 4824 | if (child == NULL) { |
| 4825 | if (ctxt->error != NULL) |
| 4826 | ctxt->error(ctxt->userData, |
| 4827 | "xmlRelaxNGParseattribute: attribute has no children\n"); |
| 4828 | ctxt->nbErrors++; |
| 4829 | return(ret); |
| 4830 | } |
| 4831 | old_flags = ctxt->flags; |
| 4832 | ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4833 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 4834 | if (cur != NULL) |
| 4835 | child = child->next; |
| 4836 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4837 | if (child != NULL) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4838 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 4839 | if (cur != NULL) { |
| 4840 | switch (cur->type) { |
| 4841 | case XML_RELAXNG_EMPTY: |
| 4842 | case XML_RELAXNG_NOT_ALLOWED: |
| 4843 | case XML_RELAXNG_TEXT: |
| 4844 | case XML_RELAXNG_ELEMENT: |
| 4845 | case XML_RELAXNG_DATATYPE: |
| 4846 | case XML_RELAXNG_VALUE: |
| 4847 | case XML_RELAXNG_LIST: |
| 4848 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4849 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4850 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4851 | case XML_RELAXNG_DEF: |
| 4852 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4853 | case XML_RELAXNG_ZEROORMORE: |
| 4854 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4855 | case XML_RELAXNG_CHOICE: |
| 4856 | case XML_RELAXNG_GROUP: |
| 4857 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4858 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4859 | ret->content = cur; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 4860 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4861 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4862 | case XML_RELAXNG_START: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 4863 | case XML_RELAXNG_PARAM: |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4864 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4865 | if (ctxt->error != NULL) |
| 4866 | ctxt->error(ctxt->userData, |
| 4867 | "attribute has invalid content\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 4868 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 4869 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4870 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4871 | if (ctxt->error != NULL) |
| 4872 | ctxt->error(ctxt->userData, |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 4873 | "RNG Internal error, noop found in attribute\n"); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 4874 | ctxt->nbErrors++; |
| 4875 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4876 | } |
| 4877 | } |
| 4878 | child = child->next; |
| 4879 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4880 | if (child != NULL) { |
| 4881 | if (ctxt->error != NULL) |
| 4882 | ctxt->error(ctxt->userData, "attribute has multiple children\n"); |
| 4883 | ctxt->nbErrors++; |
| 4884 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 4885 | ctxt->flags = old_flags; |
| 4886 | return(ret); |
| 4887 | } |
| 4888 | |
| 4889 | /** |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4890 | * xmlRelaxNGParseExceptNameClass: |
| 4891 | * @ctxt: a Relax-NG parser context |
| 4892 | * @node: the except node |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4893 | * @attr: 1 if within an attribute, 0 if within an element |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4894 | * |
| 4895 | * parse the content of a RelaxNG nameClass node. |
| 4896 | * |
| 4897 | * Returns the definition pointer or NULL in case of error. |
| 4898 | */ |
| 4899 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4900 | xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt, |
| 4901 | xmlNodePtr node, int attr) { |
| 4902 | xmlRelaxNGDefinePtr ret, cur, last = NULL; |
| 4903 | xmlNodePtr child; |
| 4904 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4905 | if (!IS_RELAXNG(node, "except")) { |
| 4906 | if (ctxt->error != NULL) |
| 4907 | ctxt->error(ctxt->userData, |
| 4908 | "Expecting an except node\n"); |
| 4909 | ctxt->nbErrors++; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4910 | return(NULL); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4911 | } |
| 4912 | if (node->next != NULL) { |
| 4913 | if (ctxt->error != NULL) |
| 4914 | ctxt->error(ctxt->userData, |
| 4915 | "exceptNameClass allows only a single except node\n"); |
| 4916 | ctxt->nbErrors++; |
| 4917 | } |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4918 | if (node->children == NULL) { |
| 4919 | if (ctxt->error != NULL) |
| 4920 | ctxt->error(ctxt->userData, |
| 4921 | "except has no content\n"); |
| 4922 | ctxt->nbErrors++; |
| 4923 | return(NULL); |
| 4924 | } |
| 4925 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4926 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4927 | if (ret == NULL) |
| 4928 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4929 | ret->type = XML_RELAXNG_EXCEPT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4930 | child = node->children; |
| 4931 | while (child != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4932 | cur = xmlRelaxNGNewDefine(ctxt, child); |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4933 | if (cur == NULL) |
| 4934 | break; |
| 4935 | if (attr) |
| 4936 | cur->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4937 | else |
| 4938 | cur->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4939 | |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 4940 | if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) { |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 4941 | if (last == NULL) { |
| 4942 | ret->content = cur; |
| 4943 | } else { |
| 4944 | last->next = cur; |
| 4945 | } |
| 4946 | last = cur; |
| 4947 | } |
| 4948 | child = child->next; |
| 4949 | } |
| 4950 | |
| 4951 | return(ret); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | /** |
| 4955 | * xmlRelaxNGParseNameClass: |
| 4956 | * @ctxt: a Relax-NG parser context |
| 4957 | * @node: the nameClass node |
| 4958 | * @def: the current definition |
| 4959 | * |
| 4960 | * parse the content of a RelaxNG nameClass node. |
| 4961 | * |
| 4962 | * Returns the definition pointer or NULL in case of error. |
| 4963 | */ |
| 4964 | static xmlRelaxNGDefinePtr |
| 4965 | xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, |
| 4966 | xmlRelaxNGDefinePtr def) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4967 | xmlRelaxNGDefinePtr ret, tmp; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4968 | xmlChar *val; |
| 4969 | |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4970 | ret = def; |
| 4971 | if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) || |
| 4972 | (IS_RELAXNG(node, "nsName"))) { |
| 4973 | if ((def->type != XML_RELAXNG_ELEMENT) && |
| 4974 | (def->type != XML_RELAXNG_ATTRIBUTE)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4975 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4976 | if (ret == NULL) |
| 4977 | return(NULL); |
| 4978 | ret->parent = def; |
| 4979 | if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) |
| 4980 | ret->type = XML_RELAXNG_ATTRIBUTE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 4981 | else |
| 4982 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 4983 | } |
| 4984 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 4985 | if (IS_RELAXNG(node, "name")) { |
| 4986 | val = xmlNodeGetContent(node); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 4987 | xmlRelaxNGNormExtSpace(val); |
| 4988 | if (xmlValidateNCName(val, 0)) { |
| 4989 | if (ctxt->error != NULL) { |
| 4990 | if (node->parent != NULL) |
| 4991 | ctxt->error(ctxt->userData, |
| 4992 | "Element %s name '%s' is not an NCName\n", |
| 4993 | node->parent->name, val); |
| 4994 | else |
| 4995 | ctxt->error(ctxt->userData, |
| 4996 | "name '%s' is not an NCName\n", |
| 4997 | val); |
| 4998 | } |
| 4999 | ctxt->nbErrors++; |
| 5000 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5001 | ret->name = val; |
| 5002 | val = xmlGetProp(node, BAD_CAST "ns"); |
| 5003 | ret->ns = val; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 5004 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5005 | (val != NULL) && |
| 5006 | (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5007 | ctxt->error(ctxt->userData, |
| 5008 | "Attribute with namespace '%s' is not allowed\n", |
| 5009 | val); |
| 5010 | ctxt->nbErrors++; |
| 5011 | } |
| 5012 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5013 | (val != NULL) && |
| 5014 | (val[0] == 0) && |
| 5015 | (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) { |
| 5016 | ctxt->error(ctxt->userData, |
| 5017 | "Attribute with QName 'xmlns' is not allowed\n", |
| 5018 | val); |
| 5019 | ctxt->nbErrors++; |
| 5020 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5021 | } else if (IS_RELAXNG(node, "anyName")) { |
| 5022 | ret->name = NULL; |
| 5023 | ret->ns = NULL; |
| 5024 | if (node->children != NULL) { |
| 5025 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5026 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5027 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5028 | } |
| 5029 | } else if (IS_RELAXNG(node, "nsName")) { |
| 5030 | ret->name = NULL; |
| 5031 | ret->ns = xmlGetProp(node, BAD_CAST "ns"); |
| 5032 | if (ret->ns == NULL) { |
| 5033 | if (ctxt->error != NULL) |
| 5034 | ctxt->error(ctxt->userData, |
| 5035 | "nsName has no ns attribute\n"); |
| 5036 | ctxt->nbErrors++; |
| 5037 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 5038 | if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) && |
| 5039 | (ret->ns != NULL) && |
| 5040 | (xmlStrEqual(ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) { |
| 5041 | ctxt->error(ctxt->userData, |
| 5042 | "Attribute with namespace '%s' is not allowed\n", |
| 5043 | ret->ns); |
| 5044 | ctxt->nbErrors++; |
| 5045 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5046 | if (node->children != NULL) { |
| 5047 | ret->nameClass = |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5048 | xmlRelaxNGParseExceptNameClass(ctxt, node->children, |
| 5049 | (def->type == XML_RELAXNG_ATTRIBUTE)); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5050 | } |
| 5051 | } else if (IS_RELAXNG(node, "choice")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5052 | xmlNodePtr child; |
| 5053 | xmlRelaxNGDefinePtr last = NULL; |
| 5054 | |
| 5055 | ret = xmlRelaxNGNewDefine(ctxt, node); |
| 5056 | if (ret == NULL) |
| 5057 | return(NULL); |
| 5058 | ret->parent = def; |
| 5059 | ret->type = XML_RELAXNG_CHOICE; |
| 5060 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5061 | if (node->children == NULL) { |
| 5062 | if (ctxt->error != NULL) |
| 5063 | ctxt->error(ctxt->userData, |
| 5064 | "Element choice is empty\n"); |
| 5065 | ctxt->nbErrors++; |
| 5066 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5067 | |
| 5068 | child = node->children; |
| 5069 | while (child != NULL) { |
| 5070 | tmp = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5071 | if (tmp != NULL) { |
| 5072 | if (last == NULL) { |
| 5073 | last = ret->nameClass = tmp; |
| 5074 | } else { |
| 5075 | last->next = tmp; |
| 5076 | last = tmp; |
| 5077 | } |
| 5078 | } |
| 5079 | child = child->next; |
| 5080 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5081 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5082 | } else { |
| 5083 | if (ctxt->error != NULL) |
| 5084 | ctxt->error(ctxt->userData, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5085 | "expecting name, anyName, nsName or choice : got %s\n", |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5086 | node->name); |
| 5087 | ctxt->nbErrors++; |
| 5088 | return(NULL); |
| 5089 | } |
Daniel Veillard | 2e9b165 | 2003-02-19 13:29:45 +0000 | [diff] [blame] | 5090 | if (ret != def) { |
| 5091 | if (def->nameClass == NULL) { |
| 5092 | def->nameClass = ret; |
| 5093 | } else { |
| 5094 | tmp = def->nameClass; |
| 5095 | while (tmp->next != NULL) { |
| 5096 | tmp = tmp->next; |
| 5097 | } |
| 5098 | tmp->next = ret; |
| 5099 | } |
| 5100 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5101 | return(ret); |
| 5102 | } |
| 5103 | |
| 5104 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5105 | * xmlRelaxNGParseElement: |
| 5106 | * @ctxt: a Relax-NG parser context |
| 5107 | * @node: the element node |
| 5108 | * |
| 5109 | * parse the content of a RelaxNG element node. |
| 5110 | * |
| 5111 | * Returns the definition pointer or NULL in case of error. |
| 5112 | */ |
| 5113 | static xmlRelaxNGDefinePtr |
| 5114 | xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 5115 | xmlRelaxNGDefinePtr ret, cur, last; |
| 5116 | xmlNodePtr child; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5117 | const xmlChar *olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5118 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5119 | ret = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5120 | if (ret == NULL) |
| 5121 | return(NULL); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5122 | ret->type = XML_RELAXNG_ELEMENT; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5123 | ret->parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5124 | child = node->children; |
| 5125 | if (child == NULL) { |
| 5126 | if (ctxt->error != NULL) |
| 5127 | ctxt->error(ctxt->userData, |
| 5128 | "xmlRelaxNGParseElement: element has no children\n"); |
| 5129 | ctxt->nbErrors++; |
| 5130 | return(ret); |
| 5131 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 5132 | cur = xmlRelaxNGParseNameClass(ctxt, child, ret); |
| 5133 | if (cur != NULL) |
| 5134 | child = child->next; |
| 5135 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5136 | if (child == NULL) { |
| 5137 | if (ctxt->error != NULL) |
| 5138 | ctxt->error(ctxt->userData, |
| 5139 | "xmlRelaxNGParseElement: element has no content\n"); |
| 5140 | ctxt->nbErrors++; |
| 5141 | return(ret); |
| 5142 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5143 | olddefine = ctxt->define; |
| 5144 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5145 | last = NULL; |
| 5146 | while (child != NULL) { |
| 5147 | cur = xmlRelaxNGParsePattern(ctxt, child); |
| 5148 | if (cur != NULL) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5149 | cur->parent = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5150 | switch (cur->type) { |
| 5151 | case XML_RELAXNG_EMPTY: |
| 5152 | case XML_RELAXNG_NOT_ALLOWED: |
| 5153 | case XML_RELAXNG_TEXT: |
| 5154 | case XML_RELAXNG_ELEMENT: |
| 5155 | case XML_RELAXNG_DATATYPE: |
| 5156 | case XML_RELAXNG_VALUE: |
| 5157 | case XML_RELAXNG_LIST: |
| 5158 | case XML_RELAXNG_REF: |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5159 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5160 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5161 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5162 | case XML_RELAXNG_ZEROORMORE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5163 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5164 | case XML_RELAXNG_OPTIONAL: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5165 | case XML_RELAXNG_CHOICE: |
| 5166 | case XML_RELAXNG_GROUP: |
| 5167 | case XML_RELAXNG_INTERLEAVE: |
| 5168 | if (last == NULL) { |
| 5169 | ret->content = last = cur; |
| 5170 | } else { |
| 5171 | if ((last->type == XML_RELAXNG_ELEMENT) && |
| 5172 | (ret->content == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5173 | ret->content = xmlRelaxNGNewDefine(ctxt, node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5174 | if (ret->content != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5175 | ret->content->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5176 | ret->content->content = last; |
| 5177 | } else { |
| 5178 | ret->content = last; |
| 5179 | } |
| 5180 | } |
| 5181 | last->next = cur; |
| 5182 | last = cur; |
| 5183 | } |
| 5184 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5185 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5186 | /* HERE !!! */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5187 | cur->next = ret->attrs; |
| 5188 | ret->attrs = cur; |
| 5189 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5190 | case XML_RELAXNG_START: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5191 | if (ctxt->error != NULL) |
| 5192 | ctxt->error(ctxt->userData, |
| 5193 | "RNG Internal error, start found in element\n"); |
| 5194 | ctxt->nbErrors++; |
| 5195 | break; |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 5196 | case XML_RELAXNG_PARAM: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5197 | if (ctxt->error != NULL) |
| 5198 | ctxt->error(ctxt->userData, |
| 5199 | "RNG Internal error, param found in element\n"); |
| 5200 | ctxt->nbErrors++; |
| 5201 | break; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 5202 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5203 | if (ctxt->error != NULL) |
| 5204 | ctxt->error(ctxt->userData, |
| 5205 | "RNG Internal error, except found in element\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5206 | ctxt->nbErrors++; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 5207 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5208 | case XML_RELAXNG_NOOP: |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5209 | if (ctxt->error != NULL) |
| 5210 | ctxt->error(ctxt->userData, |
Daniel Veillard | ac29793 | 2003-04-17 12:55:35 +0000 | [diff] [blame] | 5211 | "RNG Internal error, noop found in element\n"); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5212 | ctxt->nbErrors++; |
| 5213 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5214 | } |
| 5215 | } |
| 5216 | child = child->next; |
| 5217 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5218 | ctxt->define = olddefine; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5219 | return(ret); |
| 5220 | } |
| 5221 | |
| 5222 | /** |
| 5223 | * xmlRelaxNGParsePatterns: |
| 5224 | * @ctxt: a Relax-NG parser context |
| 5225 | * @nodes: list of nodes |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5226 | * @group: use an implicit <group> for elements |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5227 | * |
| 5228 | * parse the content of a RelaxNG start node. |
| 5229 | * |
| 5230 | * Returns the definition pointer or NULL in case of error. |
| 5231 | */ |
| 5232 | static xmlRelaxNGDefinePtr |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5233 | xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, |
| 5234 | int group) { |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5235 | xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5236 | |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5237 | parent = ctxt->def; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5238 | while (nodes != NULL) { |
| 5239 | if (IS_RELAXNG(nodes, "element")) { |
| 5240 | cur = xmlRelaxNGParseElement(ctxt, nodes); |
| 5241 | if (def == NULL) { |
| 5242 | def = last = cur; |
| 5243 | } else { |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5244 | if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) && |
| 5245 | (def == last)) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5246 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
| 5247 | def->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5248 | def->content = last; |
| 5249 | } |
| 5250 | last->next = cur; |
| 5251 | last = cur; |
| 5252 | } |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 5253 | cur->parent = parent; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5254 | } else { |
| 5255 | cur = xmlRelaxNGParsePattern(ctxt, nodes); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 5256 | if (cur != NULL) { |
| 5257 | if (def == NULL) { |
| 5258 | def = last = cur; |
| 5259 | } else { |
| 5260 | last->next = cur; |
| 5261 | last = cur; |
| 5262 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5263 | } |
| 5264 | } |
| 5265 | nodes = nodes->next; |
| 5266 | } |
| 5267 | return(def); |
| 5268 | } |
| 5269 | |
| 5270 | /** |
| 5271 | * xmlRelaxNGParseStart: |
| 5272 | * @ctxt: a Relax-NG parser context |
| 5273 | * @nodes: start children nodes |
| 5274 | * |
| 5275 | * parse the content of a RelaxNG start node. |
| 5276 | * |
| 5277 | * Returns 0 in case of success, -1 in case of error |
| 5278 | */ |
| 5279 | static int |
| 5280 | xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 5281 | int ret = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5282 | xmlRelaxNGDefinePtr def = NULL, last; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5283 | |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5284 | if (nodes == NULL) { |
| 5285 | if (ctxt->error != NULL) |
| 5286 | ctxt->error(ctxt->userData, |
| 5287 | "start has no children\n"); |
| 5288 | ctxt->nbErrors++; |
| 5289 | return(-1); |
| 5290 | } |
| 5291 | if (IS_RELAXNG(nodes, "empty")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5292 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5293 | if (def == NULL) |
| 5294 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5295 | def->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5296 | if (nodes->children != NULL) { |
| 5297 | if (ctxt->error != NULL) |
| 5298 | ctxt->error(ctxt->userData, "element empty is not empty\n"); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5299 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5300 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5301 | } else if (IS_RELAXNG(nodes, "notAllowed")) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5302 | def = xmlRelaxNGNewDefine(ctxt, nodes); |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5303 | if (def == NULL) |
| 5304 | return(-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5305 | def->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5306 | if (nodes->children != NULL) { |
| 5307 | if (ctxt->error != NULL) |
| 5308 | ctxt->error(ctxt->userData, |
| 5309 | "element notAllowed is not empty\n"); |
| 5310 | ctxt->nbErrors++; |
| 5311 | } |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5312 | } else { |
| 5313 | def = xmlRelaxNGParsePatterns(ctxt, nodes, 1); |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5314 | } |
| 5315 | if (ctxt->grammar->start != NULL) { |
| 5316 | last = ctxt->grammar->start; |
| 5317 | while (last->next != NULL) |
| 5318 | last = last->next; |
| 5319 | last->next = def; |
| 5320 | } else { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5321 | ctxt->grammar->start = def; |
| 5322 | } |
| 5323 | nodes = nodes->next; |
| 5324 | if (nodes != NULL) { |
| 5325 | if (ctxt->error != NULL) |
| 5326 | ctxt->error(ctxt->userData, |
| 5327 | "start more than one children\n"); |
| 5328 | ctxt->nbErrors++; |
| 5329 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5330 | } |
| 5331 | return(ret); |
| 5332 | } |
| 5333 | |
| 5334 | /** |
| 5335 | * xmlRelaxNGParseGrammarContent: |
| 5336 | * @ctxt: a Relax-NG parser context |
| 5337 | * @nodes: grammar children nodes |
| 5338 | * |
| 5339 | * parse the content of a RelaxNG grammar node. |
| 5340 | * |
| 5341 | * Returns 0 in case of success, -1 in case of error |
| 5342 | */ |
| 5343 | static int |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5344 | xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5345 | { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5346 | int ret = 0, tmp; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5347 | |
| 5348 | if (nodes == NULL) { |
| 5349 | if (ctxt->error != NULL) |
| 5350 | ctxt->error(ctxt->userData, |
| 5351 | "grammar has no children\n"); |
| 5352 | ctxt->nbErrors++; |
| 5353 | return(-1); |
| 5354 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5355 | while (nodes != NULL) { |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5356 | if (IS_RELAXNG(nodes, "start")) { |
| 5357 | if (nodes->children == NULL) { |
| 5358 | if (ctxt->error != NULL) |
| 5359 | ctxt->error(ctxt->userData, |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 5360 | "start has no children\n"); |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5361 | ctxt->nbErrors++; |
| 5362 | } else { |
| 5363 | tmp = xmlRelaxNGParseStart(ctxt, nodes->children); |
| 5364 | if (tmp != 0) |
| 5365 | ret = -1; |
| 5366 | } |
| 5367 | } else if (IS_RELAXNG(nodes, "define")) { |
| 5368 | tmp = xmlRelaxNGParseDefine(ctxt, nodes); |
| 5369 | if (tmp != 0) |
| 5370 | ret = -1; |
| 5371 | } else if (IS_RELAXNG(nodes, "include")) { |
| 5372 | tmp = xmlRelaxNGParseInclude(ctxt, nodes); |
| 5373 | if (tmp != 0) |
| 5374 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5375 | } else { |
| 5376 | if (ctxt->error != NULL) |
| 5377 | ctxt->error(ctxt->userData, |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 5378 | "grammar has unexpected child %s\n", nodes->name); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5379 | ctxt->nbErrors++; |
| 5380 | ret = -1; |
| 5381 | } |
| 5382 | nodes = nodes->next; |
| 5383 | } |
| 5384 | return (ret); |
| 5385 | } |
| 5386 | |
| 5387 | /** |
| 5388 | * xmlRelaxNGCheckReference: |
| 5389 | * @ref: the ref |
| 5390 | * @ctxt: a Relax-NG parser context |
| 5391 | * @name: the name associated to the defines |
| 5392 | * |
| 5393 | * Applies the 4.17. combine attribute rule for all the define |
| 5394 | * element of a given grammar using the same name. |
| 5395 | */ |
| 5396 | static void |
| 5397 | xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref, |
| 5398 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5399 | xmlRelaxNGGrammarPtr grammar; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5400 | xmlRelaxNGDefinePtr def, cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5401 | |
| 5402 | grammar = ctxt->grammar; |
| 5403 | if (grammar == NULL) { |
| 5404 | if (ctxt->error != NULL) |
| 5405 | ctxt->error(ctxt->userData, |
| 5406 | "Internal error: no grammar in CheckReference %s\n", |
| 5407 | name); |
| 5408 | ctxt->nbErrors++; |
| 5409 | return; |
| 5410 | } |
| 5411 | if (ref->content != NULL) { |
| 5412 | if (ctxt->error != NULL) |
| 5413 | ctxt->error(ctxt->userData, |
| 5414 | "Internal error: reference has content in CheckReference %s\n", |
| 5415 | name); |
| 5416 | ctxt->nbErrors++; |
| 5417 | return; |
| 5418 | } |
| 5419 | if (grammar->defs != NULL) { |
| 5420 | def = xmlHashLookup(grammar->defs, name); |
| 5421 | if (def != NULL) { |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 5422 | cur = ref; |
| 5423 | while (cur != NULL) { |
| 5424 | cur->content = def; |
| 5425 | cur = cur->nextHash; |
| 5426 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5427 | } else { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5428 | if (ctxt->error != NULL) |
| 5429 | ctxt->error(ctxt->userData, |
| 5430 | "Reference %s has no matching definition\n", |
| 5431 | name); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 5432 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5433 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5434 | } else { |
| 5435 | if (ctxt->error != NULL) |
| 5436 | ctxt->error(ctxt->userData, |
| 5437 | "Reference %s has no matching definition\n", |
| 5438 | name); |
| 5439 | ctxt->nbErrors++; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5440 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5441 | } |
| 5442 | |
| 5443 | /** |
| 5444 | * xmlRelaxNGCheckCombine: |
| 5445 | * @define: the define(s) list |
| 5446 | * @ctxt: a Relax-NG parser context |
| 5447 | * @name: the name associated to the defines |
| 5448 | * |
| 5449 | * Applies the 4.17. combine attribute rule for all the define |
| 5450 | * element of a given grammar using the same name. |
| 5451 | */ |
| 5452 | static void |
| 5453 | xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define, |
| 5454 | xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) { |
| 5455 | xmlChar *combine; |
| 5456 | int choiceOrInterleave = -1; |
| 5457 | int missing = 0; |
| 5458 | xmlRelaxNGDefinePtr cur, last, tmp, tmp2; |
| 5459 | |
| 5460 | if (define->nextHash == NULL) |
| 5461 | return; |
| 5462 | cur = define; |
| 5463 | while (cur != NULL) { |
| 5464 | combine = xmlGetProp(cur->node, BAD_CAST "combine"); |
| 5465 | if (combine != NULL) { |
| 5466 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5467 | if (choiceOrInterleave == -1) |
| 5468 | choiceOrInterleave = 1; |
| 5469 | else if (choiceOrInterleave == 0) { |
| 5470 | if (ctxt->error != NULL) |
| 5471 | ctxt->error(ctxt->userData, |
| 5472 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5473 | name); |
| 5474 | ctxt->nbErrors++; |
| 5475 | } |
Daniel Veillard | 154877e | 2003-01-30 12:17:05 +0000 | [diff] [blame] | 5476 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5477 | if (choiceOrInterleave == -1) |
| 5478 | choiceOrInterleave = 0; |
| 5479 | else if (choiceOrInterleave == 1) { |
| 5480 | if (ctxt->error != NULL) |
| 5481 | ctxt->error(ctxt->userData, |
| 5482 | "Defines for %s use both 'choice' and 'interleave'\n", |
| 5483 | name); |
| 5484 | ctxt->nbErrors++; |
| 5485 | } |
| 5486 | } else { |
| 5487 | if (ctxt->error != NULL) |
| 5488 | ctxt->error(ctxt->userData, |
| 5489 | "Defines for %s use unknown combine value '%s''\n", |
| 5490 | name, combine); |
| 5491 | ctxt->nbErrors++; |
| 5492 | } |
| 5493 | xmlFree(combine); |
| 5494 | } else { |
| 5495 | if (missing == 0) |
| 5496 | missing = 1; |
| 5497 | else { |
| 5498 | if (ctxt->error != NULL) |
| 5499 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5500 | "Some defines for %s needs the combine attribute\n", |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5501 | name); |
| 5502 | ctxt->nbErrors++; |
| 5503 | } |
| 5504 | } |
| 5505 | |
| 5506 | cur = cur->nextHash; |
| 5507 | } |
| 5508 | #ifdef DEBUG |
| 5509 | xmlGenericError(xmlGenericErrorContext, |
| 5510 | "xmlRelaxNGCheckCombine(): merging %s defines: %d\n", |
| 5511 | name, choiceOrInterleave); |
| 5512 | #endif |
| 5513 | if (choiceOrInterleave == -1) |
| 5514 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5515 | cur = xmlRelaxNGNewDefine(ctxt, define->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5516 | if (cur == NULL) |
| 5517 | return; |
| 5518 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5519 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5520 | else |
| 5521 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5522 | tmp = define; |
| 5523 | last = NULL; |
| 5524 | while (tmp != NULL) { |
| 5525 | if (tmp->content != NULL) { |
| 5526 | if (tmp->content->next != NULL) { |
| 5527 | /* |
| 5528 | * we need first to create a wrapper. |
| 5529 | */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5530 | tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5531 | if (tmp2 == NULL) |
| 5532 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5533 | tmp2->type = XML_RELAXNG_GROUP; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5534 | tmp2->content = tmp->content; |
| 5535 | } else { |
| 5536 | tmp2 = tmp->content; |
| 5537 | } |
| 5538 | if (last == NULL) { |
| 5539 | cur->content = tmp2; |
| 5540 | } else { |
| 5541 | last->next = tmp2; |
| 5542 | } |
| 5543 | last = tmp2; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5544 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 5545 | tmp->content = cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5546 | tmp = tmp->nextHash; |
| 5547 | } |
| 5548 | define->content = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5549 | if (choiceOrInterleave == 0) { |
| 5550 | if (ctxt->interleaves == NULL) |
| 5551 | ctxt->interleaves = xmlHashCreate(10); |
| 5552 | if (ctxt->interleaves == NULL) { |
| 5553 | if (ctxt->error != NULL) |
| 5554 | ctxt->error(ctxt->userData, |
| 5555 | "Failed to create interleaves hash table\n"); |
| 5556 | ctxt->nbErrors++; |
| 5557 | } else { |
| 5558 | char tmpname[32]; |
| 5559 | |
| 5560 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5561 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5562 | if (ctxt->error != NULL) |
| 5563 | ctxt->error(ctxt->userData, |
| 5564 | "Failed to add %s to hash table\n", tmpname); |
| 5565 | ctxt->nbErrors++; |
| 5566 | } |
| 5567 | } |
| 5568 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5569 | } |
| 5570 | |
| 5571 | /** |
| 5572 | * xmlRelaxNGCombineStart: |
| 5573 | * @ctxt: a Relax-NG parser context |
| 5574 | * @grammar: the grammar |
| 5575 | * |
| 5576 | * Applies the 4.17. combine rule for all the start |
| 5577 | * element of a given grammar. |
| 5578 | */ |
| 5579 | static void |
| 5580 | xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt, |
| 5581 | xmlRelaxNGGrammarPtr grammar) { |
| 5582 | xmlRelaxNGDefinePtr starts; |
| 5583 | xmlChar *combine; |
| 5584 | int choiceOrInterleave = -1; |
| 5585 | int missing = 0; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5586 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5587 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5588 | starts = grammar->start; |
| 5589 | if ((starts == NULL) || (starts->next == NULL)) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5590 | return; |
| 5591 | cur = starts; |
| 5592 | while (cur != NULL) { |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5593 | if ((cur->node == NULL) || (cur->node->parent == NULL) || |
| 5594 | (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) { |
| 5595 | combine = NULL; |
| 5596 | if (ctxt->error != NULL) |
| 5597 | ctxt->error(ctxt->userData, |
| 5598 | "Internal error: start element not found\n"); |
| 5599 | ctxt->nbErrors++; |
| 5600 | } else { |
| 5601 | combine = xmlGetProp(cur->node->parent, BAD_CAST "combine"); |
| 5602 | } |
| 5603 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5604 | if (combine != NULL) { |
| 5605 | if (xmlStrEqual(combine, BAD_CAST "choice")) { |
| 5606 | if (choiceOrInterleave == -1) |
| 5607 | choiceOrInterleave = 1; |
| 5608 | else if (choiceOrInterleave == 0) { |
| 5609 | if (ctxt->error != NULL) |
| 5610 | ctxt->error(ctxt->userData, |
| 5611 | "<start> use both 'choice' and 'interleave'\n"); |
| 5612 | ctxt->nbErrors++; |
| 5613 | } |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5614 | } else if (xmlStrEqual(combine, BAD_CAST "interleave")) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5615 | if (choiceOrInterleave == -1) |
| 5616 | choiceOrInterleave = 0; |
| 5617 | else if (choiceOrInterleave == 1) { |
| 5618 | if (ctxt->error != NULL) |
| 5619 | ctxt->error(ctxt->userData, |
| 5620 | "<start> use both 'choice' and 'interleave'\n"); |
| 5621 | ctxt->nbErrors++; |
| 5622 | } |
| 5623 | } else { |
| 5624 | if (ctxt->error != NULL) |
| 5625 | ctxt->error(ctxt->userData, |
| 5626 | "<start> uses unknown combine value '%s''\n", combine); |
| 5627 | ctxt->nbErrors++; |
| 5628 | } |
| 5629 | xmlFree(combine); |
| 5630 | } else { |
| 5631 | if (missing == 0) |
| 5632 | missing = 1; |
| 5633 | else { |
| 5634 | if (ctxt->error != NULL) |
| 5635 | ctxt->error(ctxt->userData, |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 5636 | "Some <start> element miss the combine attribute\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5637 | ctxt->nbErrors++; |
| 5638 | } |
| 5639 | } |
| 5640 | |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5641 | cur = cur->next; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5642 | } |
| 5643 | #ifdef DEBUG |
| 5644 | xmlGenericError(xmlGenericErrorContext, |
| 5645 | "xmlRelaxNGCombineStart(): merging <start>: %d\n", |
| 5646 | choiceOrInterleave); |
| 5647 | #endif |
| 5648 | if (choiceOrInterleave == -1) |
| 5649 | choiceOrInterleave = 0; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5650 | cur = xmlRelaxNGNewDefine(ctxt, starts->node); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5651 | if (cur == NULL) |
| 5652 | return; |
| 5653 | if (choiceOrInterleave == 0) |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5654 | cur->type = XML_RELAXNG_INTERLEAVE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5655 | else |
| 5656 | cur->type = XML_RELAXNG_CHOICE; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 5657 | cur->content = grammar->start; |
| 5658 | grammar->start = cur; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5659 | if (choiceOrInterleave == 0) { |
| 5660 | if (ctxt->interleaves == NULL) |
| 5661 | ctxt->interleaves = xmlHashCreate(10); |
| 5662 | if (ctxt->interleaves == NULL) { |
| 5663 | if (ctxt->error != NULL) |
| 5664 | ctxt->error(ctxt->userData, |
| 5665 | "Failed to create interleaves hash table\n"); |
| 5666 | ctxt->nbErrors++; |
| 5667 | } else { |
| 5668 | char tmpname[32]; |
| 5669 | |
| 5670 | snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++); |
| 5671 | if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) { |
| 5672 | if (ctxt->error != NULL) |
| 5673 | ctxt->error(ctxt->userData, |
| 5674 | "Failed to add %s to hash table\n", tmpname); |
| 5675 | ctxt->nbErrors++; |
| 5676 | } |
| 5677 | } |
| 5678 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 5679 | } |
| 5680 | |
| 5681 | /** |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5682 | * xmlRelaxNGCheckCycles: |
| 5683 | * @ctxt: a Relax-NG parser context |
| 5684 | * @nodes: grammar children nodes |
| 5685 | * @depth: the counter |
| 5686 | * |
| 5687 | * Check for cycles. |
| 5688 | * |
| 5689 | * Returns 0 if check passed, and -1 in case of error |
| 5690 | */ |
| 5691 | static int |
| 5692 | xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt, |
| 5693 | xmlRelaxNGDefinePtr cur, int depth) { |
| 5694 | int ret = 0; |
| 5695 | |
| 5696 | while ((ret == 0) && (cur != NULL)) { |
| 5697 | if ((cur->type == XML_RELAXNG_REF) || |
| 5698 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5699 | if (cur->depth == -1) { |
| 5700 | cur->depth = depth; |
| 5701 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
| 5702 | cur->depth = -2; |
| 5703 | } else if (depth == cur->depth) { |
| 5704 | if (ctxt->error != NULL) |
| 5705 | ctxt->error(ctxt->userData, |
| 5706 | "Detected a cycle in %s references\n", cur->name); |
| 5707 | ctxt->nbErrors++; |
| 5708 | return(-1); |
| 5709 | } |
| 5710 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5711 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1); |
| 5712 | } else { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5713 | ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth); |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 5714 | } |
| 5715 | cur = cur->next; |
| 5716 | } |
| 5717 | return(ret); |
| 5718 | } |
| 5719 | |
| 5720 | /** |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5721 | * xmlRelaxNGTryUnlink: |
| 5722 | * @ctxt: a Relax-NG parser context |
| 5723 | * @cur: the definition to unlink |
| 5724 | * @parent: the parent definition |
| 5725 | * @prev: the previous sibling definition |
| 5726 | * |
| 5727 | * Try to unlink a definition. If not possble make it a NOOP |
| 5728 | * |
| 5729 | * Returns the new prev definition |
| 5730 | */ |
| 5731 | static xmlRelaxNGDefinePtr |
| 5732 | xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 5733 | xmlRelaxNGDefinePtr cur, |
| 5734 | xmlRelaxNGDefinePtr parent, |
| 5735 | xmlRelaxNGDefinePtr prev) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5736 | if (prev != NULL) { |
| 5737 | prev->next = cur->next; |
| 5738 | } else { |
| 5739 | if (parent != NULL) { |
| 5740 | if (parent->content == cur) |
| 5741 | parent->content = cur->next; |
| 5742 | else if (parent->attrs == cur) |
| 5743 | parent->attrs = cur->next; |
| 5744 | else if (parent->nameClass == cur) |
| 5745 | parent->nameClass = cur->next; |
| 5746 | } else { |
| 5747 | cur->type = XML_RELAXNG_NOOP; |
| 5748 | prev = cur; |
| 5749 | } |
| 5750 | } |
| 5751 | return(prev); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5752 | } |
| 5753 | |
| 5754 | /** |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5755 | * xmlRelaxNGSimplify: |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5756 | * @ctxt: a Relax-NG parser context |
| 5757 | * @nodes: grammar children nodes |
| 5758 | * |
| 5759 | * Check for simplification of empty and notAllowed |
| 5760 | */ |
| 5761 | static void |
| 5762 | xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt, |
| 5763 | xmlRelaxNGDefinePtr cur, |
| 5764 | xmlRelaxNGDefinePtr parent) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5765 | xmlRelaxNGDefinePtr prev = NULL; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5766 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5767 | while (cur != NULL) { |
| 5768 | if ((cur->type == XML_RELAXNG_REF) || |
| 5769 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5770 | if (cur->depth != -3) { |
| 5771 | cur->depth = -3; |
| 5772 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5773 | } |
| 5774 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5775 | cur->parent = parent; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5776 | if ((parent != NULL) && |
| 5777 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5778 | (parent->type == XML_RELAXNG_LIST) || |
| 5779 | (parent->type == XML_RELAXNG_GROUP) || |
| 5780 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5781 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5782 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5783 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5784 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5785 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5786 | if ((parent != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5787 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5788 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5789 | } else |
| 5790 | prev = cur; |
| 5791 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5792 | cur->parent = parent; |
| 5793 | if ((parent != NULL) && |
| 5794 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5795 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5796 | parent->type = XML_RELAXNG_EMPTY; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5797 | break; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5798 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5799 | if ((parent != NULL) && |
| 5800 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5801 | (parent->type == XML_RELAXNG_INTERLEAVE))) { |
| 5802 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5803 | } else |
| 5804 | prev = cur; |
| 5805 | } else { |
| 5806 | cur->parent = parent; |
| 5807 | if (cur->content != NULL) |
| 5808 | xmlRelaxNGSimplify(ctxt, cur->content, cur); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5809 | if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL)) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5810 | xmlRelaxNGSimplify(ctxt, cur->attrs, cur); |
| 5811 | if (cur->nameClass != NULL) |
| 5812 | xmlRelaxNGSimplify(ctxt, cur->nameClass, cur); |
| 5813 | /* |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 5814 | * On Elements, try to move attribute only generating rules on |
| 5815 | * the attrs rules. |
| 5816 | */ |
| 5817 | if (cur->type == XML_RELAXNG_ELEMENT) { |
| 5818 | int attronly; |
| 5819 | xmlRelaxNGDefinePtr tmp, pre; |
| 5820 | |
| 5821 | while (cur->content != NULL) { |
| 5822 | attronly = xmlRelaxNGGenerateAttributes(ctxt, cur->content); |
| 5823 | if (attronly == 1) { |
| 5824 | /* |
| 5825 | * migrate cur->content to attrs |
| 5826 | */ |
| 5827 | tmp = cur->content; |
| 5828 | cur->content = tmp->next; |
| 5829 | tmp->next = cur->attrs; |
| 5830 | cur->attrs = tmp; |
| 5831 | } else { |
| 5832 | /* |
| 5833 | * cur->content can generate elements or text |
| 5834 | */ |
| 5835 | break; |
| 5836 | } |
| 5837 | } |
| 5838 | pre = cur->content; |
| 5839 | while ((pre != NULL) && (pre->next != NULL)) { |
| 5840 | tmp = pre->next; |
| 5841 | attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp); |
| 5842 | if (attronly == 1) { |
| 5843 | /* |
| 5844 | * migrate tmp to attrs |
| 5845 | */ |
| 5846 | pre->next = tmp->next; |
| 5847 | tmp->next = cur->attrs; |
| 5848 | cur->attrs = tmp; |
| 5849 | } else { |
| 5850 | pre = tmp; |
| 5851 | } |
| 5852 | } |
| 5853 | } |
| 5854 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5855 | * This may result in a simplification |
| 5856 | */ |
| 5857 | if ((cur->type == XML_RELAXNG_GROUP) || |
| 5858 | (cur->type == XML_RELAXNG_INTERLEAVE)) { |
| 5859 | if (cur->content == NULL) |
| 5860 | cur->type = XML_RELAXNG_EMPTY; |
| 5861 | else if (cur->content->next == NULL) { |
| 5862 | if ((parent == NULL) && (prev == NULL)) { |
| 5863 | cur->type = XML_RELAXNG_NOOP; |
| 5864 | } else if (prev == NULL) { |
| 5865 | parent->content = cur->content; |
| 5866 | cur->content->next = cur->next; |
| 5867 | cur = cur->content; |
| 5868 | } else { |
| 5869 | cur->content->next = cur->next; |
| 5870 | prev->next = cur->content; |
| 5871 | cur = cur->content; |
| 5872 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5873 | } |
| 5874 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5875 | /* |
| 5876 | * the current node may have been transformed back |
| 5877 | */ |
| 5878 | if ((cur->type == XML_RELAXNG_EXCEPT) && |
| 5879 | (cur->content != NULL) && |
| 5880 | (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) { |
| 5881 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5882 | } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) { |
| 5883 | if ((parent != NULL) && |
| 5884 | ((parent->type == XML_RELAXNG_ATTRIBUTE) || |
| 5885 | (parent->type == XML_RELAXNG_LIST) || |
| 5886 | (parent->type == XML_RELAXNG_GROUP) || |
| 5887 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5888 | (parent->type == XML_RELAXNG_ONEORMORE) || |
| 5889 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5890 | parent->type = XML_RELAXNG_NOT_ALLOWED; |
| 5891 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5892 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5893 | if ((parent != NULL) && |
| 5894 | (parent->type == XML_RELAXNG_CHOICE)) { |
| 5895 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5896 | } else |
| 5897 | prev = cur; |
| 5898 | } else if (cur->type == XML_RELAXNG_EMPTY){ |
| 5899 | if ((parent != NULL) && |
| 5900 | ((parent->type == XML_RELAXNG_ONEORMORE) || |
| 5901 | (parent->type == XML_RELAXNG_ZEROORMORE))) { |
| 5902 | parent->type = XML_RELAXNG_EMPTY; |
| 5903 | break; |
| 5904 | } |
| 5905 | if ((parent != NULL) && |
| 5906 | ((parent->type == XML_RELAXNG_GROUP) || |
| 5907 | (parent->type == XML_RELAXNG_INTERLEAVE) || |
| 5908 | (parent->type == XML_RELAXNG_CHOICE))) { |
| 5909 | prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev); |
| 5910 | } else |
| 5911 | prev = cur; |
| 5912 | } else { |
| 5913 | prev = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5914 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 5915 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5916 | cur = cur->next; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5917 | } |
| 5918 | } |
| 5919 | |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5920 | /** |
| 5921 | * xmlRelaxNGGroupContentType: |
| 5922 | * @ct1: the first content type |
| 5923 | * @ct2: the second content type |
| 5924 | * |
| 5925 | * Try to group 2 content types |
| 5926 | * |
| 5927 | * Returns the content type |
| 5928 | */ |
| 5929 | static xmlRelaxNGContentType |
| 5930 | xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1, |
| 5931 | xmlRelaxNGContentType ct2) { |
| 5932 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 5933 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 5934 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5935 | if (ct1 == XML_RELAXNG_CONTENT_EMPTY) |
| 5936 | return(ct2); |
| 5937 | if (ct2 == XML_RELAXNG_CONTENT_EMPTY) |
| 5938 | return(ct1); |
| 5939 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) && |
| 5940 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 5941 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 5942 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5943 | } |
| 5944 | |
| 5945 | /** |
| 5946 | * xmlRelaxNGMaxContentType: |
| 5947 | * @ct1: the first content type |
| 5948 | * @ct2: the second content type |
| 5949 | * |
| 5950 | * Compute the max content-type |
| 5951 | * |
| 5952 | * Returns the content type |
| 5953 | */ |
| 5954 | static xmlRelaxNGContentType |
| 5955 | xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1, |
| 5956 | xmlRelaxNGContentType ct2) { |
| 5957 | if ((ct1 == XML_RELAXNG_CONTENT_ERROR) || |
| 5958 | (ct2 == XML_RELAXNG_CONTENT_ERROR)) |
| 5959 | return(XML_RELAXNG_CONTENT_ERROR); |
| 5960 | if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) || |
| 5961 | (ct2 == XML_RELAXNG_CONTENT_SIMPLE)) |
| 5962 | return(XML_RELAXNG_CONTENT_SIMPLE); |
| 5963 | if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) || |
| 5964 | (ct2 == XML_RELAXNG_CONTENT_COMPLEX)) |
| 5965 | return(XML_RELAXNG_CONTENT_COMPLEX); |
| 5966 | return(XML_RELAXNG_CONTENT_EMPTY); |
| 5967 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5968 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5969 | /** |
| 5970 | * xmlRelaxNGCheckRules: |
| 5971 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5972 | * @cur: the current definition |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 5973 | * @flags: some accumulated flags |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5974 | * @ptype: the parent type |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5975 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5976 | * Check for rules in section 7.1 and 7.2 |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5977 | * |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5978 | * Returns the content type of @cur |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5979 | */ |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5980 | static xmlRelaxNGContentType |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5981 | xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt, |
| 5982 | xmlRelaxNGDefinePtr cur, int flags, |
| 5983 | xmlRelaxNGType ptype) { |
Daniel Veillard | 4c5cf70 | 2003-02-21 15:40:34 +0000 | [diff] [blame] | 5984 | int nflags = flags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5985 | xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY; |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 5986 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 5987 | while (cur != NULL) { |
| 5988 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 5989 | if ((cur->type == XML_RELAXNG_REF) || |
| 5990 | (cur->type == XML_RELAXNG_PARENTREF)) { |
| 5991 | if (flags & XML_RELAXNG_IN_LIST) { |
| 5992 | if (ctxt->error != NULL) |
| 5993 | ctxt->error(ctxt->userData, |
| 5994 | "Found forbidden pattern list//ref\n"); |
| 5995 | ctxt->nbErrors++; |
| 5996 | } |
| 5997 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 5998 | if (ctxt->error != NULL) |
| 5999 | ctxt->error(ctxt->userData, |
| 6000 | "Found forbidden pattern data/except//ref\n"); |
| 6001 | ctxt->nbErrors++; |
| 6002 | } |
| 6003 | if (cur->depth > -4) { |
| 6004 | cur->depth = -4; |
| 6005 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, |
| 6006 | flags, cur->type); |
| 6007 | cur->depth = ret - 15 ; |
| 6008 | } else if (cur->depth == -4) { |
| 6009 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6010 | } else { |
| 6011 | ret = (xmlRelaxNGContentType) cur->depth + 15; |
| 6012 | } |
| 6013 | } else if (cur->type == XML_RELAXNG_ELEMENT) { |
| 6014 | /* |
| 6015 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6016 | */ |
| 6017 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6018 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6019 | if (ctxt->error != NULL) |
| 6020 | ctxt->error(ctxt->userData, |
| 6021 | "Found forbidden pattern data/except//element(ref)\n"); |
| 6022 | ctxt->nbErrors++; |
| 6023 | } |
| 6024 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6025 | if (ctxt->error != NULL) |
| 6026 | ctxt->error(ctxt->userData, |
| 6027 | "Found forbidden pattern list//element(ref)\n"); |
| 6028 | ctxt->nbErrors++; |
| 6029 | } |
| 6030 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6031 | if (ctxt->error != NULL) |
| 6032 | ctxt->error(ctxt->userData, |
| 6033 | "Found forbidden pattern attribute//element(ref)\n"); |
| 6034 | ctxt->nbErrors++; |
| 6035 | } |
| 6036 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6037 | if (ctxt->error != NULL) |
| 6038 | ctxt->error(ctxt->userData, |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 6039 | "Found forbidden pattern attribute//element(ref)\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6040 | ctxt->nbErrors++; |
| 6041 | } |
| 6042 | /* |
| 6043 | * reset since in the simple form elements are only child |
| 6044 | * of grammar/define |
| 6045 | */ |
| 6046 | nflags = 0; |
| 6047 | ret = xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type); |
| 6048 | if (ret != XML_RELAXNG_CONTENT_EMPTY) { |
| 6049 | if (ctxt->error != NULL) |
| 6050 | ctxt->error(ctxt->userData, |
| 6051 | "Element %s attributes have a content type error\n", |
| 6052 | cur->name); |
| 6053 | ctxt->nbErrors++; |
| 6054 | } |
| 6055 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6056 | if (ret == XML_RELAXNG_CONTENT_ERROR) { |
| 6057 | if (ctxt->error != NULL) |
| 6058 | ctxt->error(ctxt->userData, |
| 6059 | "Element %s has a content type error\n", |
| 6060 | cur->name); |
| 6061 | ctxt->nbErrors++; |
| 6062 | } else { |
| 6063 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6064 | } |
| 6065 | } else if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 6066 | if (flags & XML_RELAXNG_IN_ATTRIBUTE) { |
| 6067 | if (ctxt->error != NULL) |
| 6068 | ctxt->error(ctxt->userData, |
| 6069 | "Found forbidden pattern attribute//attribute\n"); |
| 6070 | ctxt->nbErrors++; |
| 6071 | } |
| 6072 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6073 | if (ctxt->error != NULL) |
| 6074 | ctxt->error(ctxt->userData, |
| 6075 | "Found forbidden pattern list//attribute\n"); |
| 6076 | ctxt->nbErrors++; |
| 6077 | } |
| 6078 | if (flags & XML_RELAXNG_IN_OOMGROUP) { |
| 6079 | if (ctxt->error != NULL) |
| 6080 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6081 | "Found forbidden pattern oneOrMore//group//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6082 | ctxt->nbErrors++; |
| 6083 | } |
| 6084 | if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) { |
| 6085 | if (ctxt->error != NULL) |
| 6086 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6087 | "Found forbidden pattern oneOrMore//interleave//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6088 | ctxt->nbErrors++; |
| 6089 | } |
| 6090 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6091 | if (ctxt->error != NULL) |
| 6092 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6093 | "Found forbidden pattern data/except//attribute\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6094 | ctxt->nbErrors++; |
| 6095 | } |
| 6096 | if (flags & XML_RELAXNG_IN_START) { |
| 6097 | if (ctxt->error != NULL) |
| 6098 | ctxt->error(ctxt->userData, |
| 6099 | "Found forbidden pattern start//attribute\n"); |
| 6100 | ctxt->nbErrors++; |
| 6101 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 6102 | if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) && (cur->name == NULL)) { |
| 6103 | if (cur->ns == NULL) { |
| 6104 | if (ctxt->error != NULL) |
| 6105 | ctxt->error(ctxt->userData, |
| 6106 | "Found anyName attribute without oneOrMore ancestor\n"); |
| 6107 | ctxt->nbErrors++; |
| 6108 | } else { |
| 6109 | if (ctxt->error != NULL) |
| 6110 | ctxt->error(ctxt->userData, |
| 6111 | "Found nsName attribute without oneOrMore ancestor\n"); |
| 6112 | ctxt->nbErrors++; |
| 6113 | } |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6114 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6115 | nflags = flags | XML_RELAXNG_IN_ATTRIBUTE; |
| 6116 | xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6117 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6118 | } else if ((cur->type == XML_RELAXNG_ONEORMORE) || |
| 6119 | (cur->type == XML_RELAXNG_ZEROORMORE)) { |
| 6120 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6121 | if (ctxt->error != NULL) |
| 6122 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6123 | "Found forbidden pattern data/except//oneOrMore\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6124 | ctxt->nbErrors++; |
| 6125 | } |
| 6126 | if (flags & XML_RELAXNG_IN_START) { |
| 6127 | if (ctxt->error != NULL) |
| 6128 | ctxt->error(ctxt->userData, |
| 6129 | "Found forbidden pattern start//oneOrMore\n"); |
| 6130 | ctxt->nbErrors++; |
| 6131 | } |
| 6132 | nflags = flags | XML_RELAXNG_IN_ONEORMORE; |
| 6133 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6134 | ret = xmlRelaxNGGroupContentType(ret, ret); |
| 6135 | } else if (cur->type == XML_RELAXNG_LIST) { |
| 6136 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6137 | if (ctxt->error != NULL) |
| 6138 | ctxt->error(ctxt->userData, |
| 6139 | "Found forbidden pattern list//list\n"); |
| 6140 | ctxt->nbErrors++; |
| 6141 | } |
| 6142 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6143 | if (ctxt->error != NULL) |
| 6144 | ctxt->error(ctxt->userData, |
| 6145 | "Found forbidden pattern data/except//list\n"); |
| 6146 | ctxt->nbErrors++; |
| 6147 | } |
| 6148 | if (flags & XML_RELAXNG_IN_START) { |
| 6149 | if (ctxt->error != NULL) |
| 6150 | ctxt->error(ctxt->userData, |
| 6151 | "Found forbidden pattern start//list\n"); |
| 6152 | ctxt->nbErrors++; |
| 6153 | } |
| 6154 | nflags = flags | XML_RELAXNG_IN_LIST; |
| 6155 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6156 | } else if (cur->type == XML_RELAXNG_GROUP) { |
| 6157 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6158 | if (ctxt->error != NULL) |
| 6159 | ctxt->error(ctxt->userData, |
| 6160 | "Found forbidden pattern data/except//group\n"); |
| 6161 | ctxt->nbErrors++; |
| 6162 | } |
| 6163 | if (flags & XML_RELAXNG_IN_START) { |
| 6164 | if (ctxt->error != NULL) |
| 6165 | ctxt->error(ctxt->userData, |
| 6166 | "Found forbidden pattern start//group\n"); |
| 6167 | ctxt->nbErrors++; |
| 6168 | } |
| 6169 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6170 | nflags = flags | XML_RELAXNG_IN_OOMGROUP; |
| 6171 | else |
| 6172 | nflags = flags; |
| 6173 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6174 | /* |
| 6175 | * The 7.3 Attribute derivation rule for groups is plugged there |
| 6176 | */ |
| 6177 | xmlRelaxNGCheckGroupAttrs(ctxt, cur); |
| 6178 | } else if (cur->type == XML_RELAXNG_INTERLEAVE) { |
| 6179 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6180 | if (ctxt->error != NULL) |
| 6181 | ctxt->error(ctxt->userData, |
| 6182 | "Found forbidden pattern list//interleave\n"); |
| 6183 | ctxt->nbErrors++; |
| 6184 | } |
| 6185 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6186 | if (ctxt->error != NULL) |
| 6187 | ctxt->error(ctxt->userData, |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6188 | "Found forbidden pattern data/except//interleave\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6189 | ctxt->nbErrors++; |
| 6190 | } |
| 6191 | if (flags & XML_RELAXNG_IN_START) { |
| 6192 | if (ctxt->error != NULL) |
| 6193 | ctxt->error(ctxt->userData, |
| 6194 | "Found forbidden pattern start//interleave\n"); |
| 6195 | ctxt->nbErrors++; |
| 6196 | } |
| 6197 | if (flags & XML_RELAXNG_IN_ONEORMORE) |
| 6198 | nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE; |
| 6199 | else |
| 6200 | nflags = flags; |
| 6201 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6202 | } else if (cur->type == XML_RELAXNG_EXCEPT) { |
| 6203 | if ((cur->parent != NULL) && |
| 6204 | (cur->parent->type == XML_RELAXNG_DATATYPE)) |
| 6205 | nflags = flags | XML_RELAXNG_IN_DATAEXCEPT; |
| 6206 | else |
| 6207 | nflags = flags; |
| 6208 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type); |
| 6209 | } else if (cur->type == XML_RELAXNG_DATATYPE) { |
| 6210 | if (flags & XML_RELAXNG_IN_START) { |
| 6211 | if (ctxt->error != NULL) |
| 6212 | ctxt->error(ctxt->userData, |
| 6213 | "Found forbidden pattern start//data\n"); |
| 6214 | ctxt->nbErrors++; |
| 6215 | } |
| 6216 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6217 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6218 | } else if (cur->type == XML_RELAXNG_VALUE) { |
| 6219 | if (flags & XML_RELAXNG_IN_START) { |
| 6220 | if (ctxt->error != NULL) |
| 6221 | ctxt->error(ctxt->userData, |
| 6222 | "Found forbidden pattern start//value\n"); |
| 6223 | ctxt->nbErrors++; |
| 6224 | } |
| 6225 | xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6226 | ret = XML_RELAXNG_CONTENT_SIMPLE; |
| 6227 | } else if (cur->type == XML_RELAXNG_TEXT) { |
| 6228 | if (flags & XML_RELAXNG_IN_LIST) { |
| 6229 | if (ctxt->error != NULL) |
| 6230 | ctxt->error(ctxt->userData, |
| 6231 | "Found forbidden pattern list//text\n"); |
| 6232 | ctxt->nbErrors++; |
| 6233 | } |
| 6234 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6235 | if (ctxt->error != NULL) |
| 6236 | ctxt->error(ctxt->userData, |
| 6237 | "Found forbidden pattern data/except//text\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//text\n"); |
| 6244 | ctxt->nbErrors++; |
| 6245 | } |
| 6246 | ret = XML_RELAXNG_CONTENT_COMPLEX; |
| 6247 | } else if (cur->type == XML_RELAXNG_EMPTY) { |
| 6248 | if (flags & XML_RELAXNG_IN_DATAEXCEPT) { |
| 6249 | if (ctxt->error != NULL) |
| 6250 | ctxt->error(ctxt->userData, |
| 6251 | "Found forbidden pattern data/except//empty\n"); |
| 6252 | ctxt->nbErrors++; |
| 6253 | } |
| 6254 | if (flags & XML_RELAXNG_IN_START) { |
| 6255 | if (ctxt->error != NULL) |
| 6256 | ctxt->error(ctxt->userData, |
| 6257 | "Found forbidden pattern start//empty\n"); |
| 6258 | ctxt->nbErrors++; |
| 6259 | } |
| 6260 | ret = XML_RELAXNG_CONTENT_EMPTY; |
| 6261 | } else if (cur->type == XML_RELAXNG_CHOICE) { |
| 6262 | xmlRelaxNGCheckChoiceDeterminism(ctxt, cur); |
| 6263 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6264 | } else { |
| 6265 | ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type); |
| 6266 | } |
| 6267 | cur = cur->next; |
| 6268 | if (ptype == XML_RELAXNG_GROUP) { |
| 6269 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6270 | } else if (ptype == XML_RELAXNG_INTERLEAVE) { |
| 6271 | tmp = xmlRelaxNGGroupContentType(val, ret); |
| 6272 | if (tmp != XML_RELAXNG_CONTENT_ERROR) |
| 6273 | tmp = xmlRelaxNGMaxContentType(val, ret); |
| 6274 | } else if (ptype == XML_RELAXNG_CHOICE) { |
| 6275 | val = xmlRelaxNGMaxContentType(val, ret); |
| 6276 | } else if (ptype == XML_RELAXNG_LIST) { |
| 6277 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6278 | } else if (ptype == XML_RELAXNG_EXCEPT) { |
| 6279 | if (ret == XML_RELAXNG_CONTENT_ERROR) |
| 6280 | val = XML_RELAXNG_CONTENT_ERROR; |
| 6281 | else |
| 6282 | val = XML_RELAXNG_CONTENT_SIMPLE; |
| 6283 | } else { |
| 6284 | val = xmlRelaxNGGroupContentType(val, ret); |
| 6285 | } |
| 6286 | |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6287 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6288 | return(val); |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6289 | } |
Daniel Veillard | 1c745ad | 2003-02-20 00:11:02 +0000 | [diff] [blame] | 6290 | |
| 6291 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6292 | * xmlRelaxNGParseGrammar: |
| 6293 | * @ctxt: a Relax-NG parser context |
| 6294 | * @nodes: grammar children nodes |
| 6295 | * |
| 6296 | * parse a Relax-NG <grammar> node |
| 6297 | * |
| 6298 | * Returns the internal xmlRelaxNGGrammarPtr built or |
| 6299 | * NULL in case of error |
| 6300 | */ |
| 6301 | static xmlRelaxNGGrammarPtr |
| 6302 | xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) { |
| 6303 | xmlRelaxNGGrammarPtr ret, tmp, old; |
| 6304 | |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6305 | #ifdef DEBUG_GRAMMAR |
| 6306 | xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n"); |
| 6307 | #endif |
| 6308 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6309 | ret = xmlRelaxNGNewGrammar(ctxt); |
| 6310 | if (ret == NULL) |
| 6311 | return(NULL); |
| 6312 | |
| 6313 | /* |
| 6314 | * Link the new grammar in the tree |
| 6315 | */ |
| 6316 | ret->parent = ctxt->grammar; |
| 6317 | if (ctxt->grammar != NULL) { |
| 6318 | tmp = ctxt->grammar->children; |
| 6319 | if (tmp == NULL) { |
| 6320 | ctxt->grammar->children = ret; |
| 6321 | } else { |
| 6322 | while (tmp->next != NULL) |
| 6323 | tmp = tmp->next; |
| 6324 | tmp->next = ret; |
| 6325 | } |
| 6326 | } |
| 6327 | |
| 6328 | old = ctxt->grammar; |
| 6329 | ctxt->grammar = ret; |
| 6330 | xmlRelaxNGParseGrammarContent(ctxt, nodes); |
| 6331 | ctxt->grammar = ret; |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6332 | if (ctxt->grammar == NULL) { |
| 6333 | if (ctxt->error != NULL) |
| 6334 | ctxt->error(ctxt->userData, |
| 6335 | "Failed to parse <grammar> content\n"); |
| 6336 | ctxt->nbErrors++; |
| 6337 | } else if (ctxt->grammar->start == NULL) { |
| 6338 | if (ctxt->error != NULL) |
| 6339 | ctxt->error(ctxt->userData, |
| 6340 | "Element <grammar> has no <start>\n"); |
| 6341 | ctxt->nbErrors++; |
| 6342 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6343 | |
| 6344 | /* |
| 6345 | * Apply 4.17 mergingd rules to defines and starts |
| 6346 | */ |
| 6347 | xmlRelaxNGCombineStart(ctxt, ret); |
| 6348 | if (ret->defs != NULL) { |
| 6349 | xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine, |
| 6350 | ctxt); |
| 6351 | } |
| 6352 | |
| 6353 | /* |
| 6354 | * link together defines and refs in this grammar |
| 6355 | */ |
| 6356 | if (ret->refs != NULL) { |
| 6357 | xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference, |
| 6358 | ctxt); |
| 6359 | } |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 6360 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6361 | ctxt->grammar = old; |
| 6362 | return(ret); |
| 6363 | } |
| 6364 | |
| 6365 | /** |
| 6366 | * xmlRelaxNGParseDocument: |
| 6367 | * @ctxt: a Relax-NG parser context |
| 6368 | * @node: the root node of the RelaxNG schema |
| 6369 | * |
| 6370 | * parse a Relax-NG definition resource and build an internal |
| 6371 | * xmlRelaxNG struture which can be used to validate instances. |
| 6372 | * |
| 6373 | * Returns the internal XML RelaxNG structure built or |
| 6374 | * NULL in case of error |
| 6375 | */ |
| 6376 | static xmlRelaxNGPtr |
| 6377 | xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6378 | xmlRelaxNGPtr schema = NULL; |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6379 | const xmlChar *olddefine; |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6380 | xmlRelaxNGGrammarPtr old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6381 | |
| 6382 | if ((ctxt == NULL) || (node == NULL)) |
| 6383 | return (NULL); |
| 6384 | |
| 6385 | schema = xmlRelaxNGNewRelaxNG(ctxt); |
| 6386 | if (schema == NULL) |
| 6387 | return(NULL); |
| 6388 | |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6389 | olddefine = ctxt->define; |
| 6390 | ctxt->define = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6391 | if (IS_RELAXNG(node, "grammar")) { |
| 6392 | schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children); |
| 6393 | } else { |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6394 | xmlRelaxNGGrammarPtr tmp, ret; |
| 6395 | |
| 6396 | schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6397 | if (schema->topgrammar == NULL) { |
| 6398 | return(schema); |
| 6399 | } |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6400 | /* |
| 6401 | * Link the new grammar in the tree |
| 6402 | */ |
| 6403 | ret->parent = ctxt->grammar; |
| 6404 | if (ctxt->grammar != NULL) { |
| 6405 | tmp = ctxt->grammar->children; |
| 6406 | if (tmp == NULL) { |
| 6407 | ctxt->grammar->children = ret; |
| 6408 | } else { |
| 6409 | while (tmp->next != NULL) |
| 6410 | tmp = tmp->next; |
| 6411 | tmp->next = ret; |
| 6412 | } |
| 6413 | } |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6414 | old = ctxt->grammar; |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6415 | ctxt->grammar = ret; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6416 | xmlRelaxNGParseStart(ctxt, node); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 6417 | if (old != NULL) |
| 6418 | ctxt->grammar = old; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6419 | } |
Daniel Veillard | 276be4a | 2003-01-24 01:03:34 +0000 | [diff] [blame] | 6420 | ctxt->define = olddefine; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6421 | if (schema->topgrammar->start != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6422 | xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6423 | if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6424 | xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL); |
| 6425 | while ((schema->topgrammar->start != NULL) && |
| 6426 | (schema->topgrammar->start->type == XML_RELAXNG_NOOP) && |
| 6427 | (schema->topgrammar->start->next != NULL)) |
| 6428 | schema->topgrammar->start = schema->topgrammar->start->content; |
| 6429 | xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start, |
| 6430 | XML_RELAXNG_IN_START, XML_RELAXNG_NOOP); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 6431 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 6432 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6433 | |
| 6434 | #ifdef DEBUG |
| 6435 | if (schema == NULL) |
| 6436 | xmlGenericError(xmlGenericErrorContext, |
| 6437 | "xmlRelaxNGParseDocument() failed\n"); |
| 6438 | #endif |
| 6439 | |
| 6440 | return (schema); |
| 6441 | } |
| 6442 | |
| 6443 | /************************************************************************ |
| 6444 | * * |
| 6445 | * Reading RelaxNGs * |
| 6446 | * * |
| 6447 | ************************************************************************/ |
| 6448 | |
| 6449 | /** |
| 6450 | * xmlRelaxNGNewParserCtxt: |
| 6451 | * @URL: the location of the schema |
| 6452 | * |
| 6453 | * Create an XML RelaxNGs parse context for that file/resource expected |
| 6454 | * to contain an XML RelaxNGs file. |
| 6455 | * |
| 6456 | * Returns the parser context or NULL in case of error |
| 6457 | */ |
| 6458 | xmlRelaxNGParserCtxtPtr |
| 6459 | xmlRelaxNGNewParserCtxt(const char *URL) { |
| 6460 | xmlRelaxNGParserCtxtPtr ret; |
| 6461 | |
| 6462 | if (URL == NULL) |
| 6463 | return(NULL); |
| 6464 | |
| 6465 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6466 | if (ret == NULL) { |
| 6467 | xmlGenericError(xmlGenericErrorContext, |
| 6468 | "Failed to allocate new schama parser context for %s\n", URL); |
| 6469 | return (NULL); |
| 6470 | } |
| 6471 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6472 | ret->URL = xmlStrdup((const xmlChar *)URL); |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6473 | ret->error = xmlGenericError; |
| 6474 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6475 | return (ret); |
| 6476 | } |
| 6477 | |
| 6478 | /** |
| 6479 | * xmlRelaxNGNewMemParserCtxt: |
| 6480 | * @buffer: a pointer to a char array containing the schemas |
| 6481 | * @size: the size of the array |
| 6482 | * |
| 6483 | * Create an XML RelaxNGs parse context for that memory buffer expected |
| 6484 | * to contain an XML RelaxNGs file. |
| 6485 | * |
| 6486 | * Returns the parser context or NULL in case of error |
| 6487 | */ |
| 6488 | xmlRelaxNGParserCtxtPtr |
| 6489 | xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) { |
| 6490 | xmlRelaxNGParserCtxtPtr ret; |
| 6491 | |
| 6492 | if ((buffer == NULL) || (size <= 0)) |
| 6493 | return(NULL); |
| 6494 | |
| 6495 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6496 | if (ret == NULL) { |
| 6497 | xmlGenericError(xmlGenericErrorContext, |
| 6498 | "Failed to allocate new schama parser context\n"); |
| 6499 | return (NULL); |
| 6500 | } |
| 6501 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6502 | ret->buffer = buffer; |
| 6503 | ret->size = size; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 6504 | ret->error = xmlGenericError; |
| 6505 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6506 | return (ret); |
| 6507 | } |
| 6508 | |
| 6509 | /** |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 6510 | * xmlRelaxNGNewDocParserCtxt: |
| 6511 | * @doc: a preparsed document tree |
| 6512 | * |
| 6513 | * Create an XML RelaxNGs parser context for that document. |
| 6514 | * Note: since the process of compiling a RelaxNG schemas modifies the |
| 6515 | * document, the @doc parameter is duplicated internally. |
| 6516 | * |
| 6517 | * Returns the parser context or NULL in case of error |
| 6518 | */ |
| 6519 | xmlRelaxNGParserCtxtPtr |
| 6520 | xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) { |
| 6521 | xmlRelaxNGParserCtxtPtr ret; |
| 6522 | xmlDocPtr copy; |
| 6523 | |
| 6524 | if (doc == NULL) |
| 6525 | return(NULL); |
| 6526 | copy = xmlCopyDoc(doc, 1); |
| 6527 | if (copy == NULL) |
| 6528 | return(NULL); |
| 6529 | |
| 6530 | ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt)); |
| 6531 | if (ret == NULL) { |
| 6532 | xmlGenericError(xmlGenericErrorContext, |
| 6533 | "Failed to allocate new schama parser context\n"); |
| 6534 | return (NULL); |
| 6535 | } |
| 6536 | memset(ret, 0, sizeof(xmlRelaxNGParserCtxt)); |
| 6537 | ret->document = copy; |
| 6538 | ret->userData = xmlGenericErrorContext; |
| 6539 | return (ret); |
| 6540 | } |
| 6541 | |
| 6542 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6543 | * xmlRelaxNGFreeParserCtxt: |
| 6544 | * @ctxt: the schema parser context |
| 6545 | * |
| 6546 | * Free the resources associated to the schema parser context |
| 6547 | */ |
| 6548 | void |
| 6549 | xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) { |
| 6550 | if (ctxt == NULL) |
| 6551 | return; |
| 6552 | if (ctxt->URL != NULL) |
| 6553 | xmlFree(ctxt->URL); |
| 6554 | if (ctxt->doc != NULL) |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 6555 | xmlRelaxNGFreeDocument(ctxt->doc); |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 6556 | if (ctxt->interleaves != NULL) |
| 6557 | xmlHashFree(ctxt->interleaves, NULL); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6558 | if (ctxt->documents != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6559 | xmlRelaxNGFreeDocumentList(ctxt->documents); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 6560 | if (ctxt->includes != NULL) |
Daniel Veillard | c482e26 | 2003-02-26 14:48:48 +0000 | [diff] [blame] | 6561 | xmlRelaxNGFreeIncludeList(ctxt->includes); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6562 | if (ctxt->docTab != NULL) |
| 6563 | xmlFree(ctxt->docTab); |
Daniel Veillard | a9d912d | 2003-02-01 17:43:10 +0000 | [diff] [blame] | 6564 | if (ctxt->incTab != NULL) |
| 6565 | xmlFree(ctxt->incTab); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 6566 | if (ctxt->defTab != NULL) { |
| 6567 | int i; |
| 6568 | |
| 6569 | for (i = 0;i < ctxt->defNr;i++) |
| 6570 | xmlRelaxNGFreeDefine(ctxt->defTab[i]); |
| 6571 | xmlFree(ctxt->defTab); |
| 6572 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6573 | xmlFree(ctxt); |
| 6574 | } |
| 6575 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6576 | /** |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6577 | * xmlRelaxNGNormExtSpace: |
| 6578 | * @value: a value |
| 6579 | * |
| 6580 | * Removes the leading and ending spaces of the value |
| 6581 | * The string is modified "in situ" |
| 6582 | */ |
| 6583 | static void |
| 6584 | xmlRelaxNGNormExtSpace(xmlChar *value) { |
| 6585 | xmlChar *start = value; |
| 6586 | xmlChar *cur = value; |
| 6587 | if (value == NULL) |
| 6588 | return; |
| 6589 | |
| 6590 | while (IS_BLANK(*cur)) cur++; |
| 6591 | if (cur == start) { |
| 6592 | do { |
| 6593 | while ((*cur != 0) && (!IS_BLANK(*cur))) cur++; |
| 6594 | if (*cur == 0) |
| 6595 | return; |
| 6596 | start = cur; |
| 6597 | while (IS_BLANK(*cur)) cur++; |
| 6598 | if (*cur == 0) { |
| 6599 | *start = 0; |
| 6600 | return; |
| 6601 | } |
| 6602 | } while (1); |
| 6603 | } else { |
| 6604 | do { |
| 6605 | while ((*cur != 0) && (!IS_BLANK(*cur))) |
| 6606 | *start++ = *cur++; |
| 6607 | if (*cur == 0) { |
| 6608 | *start = 0; |
| 6609 | return; |
| 6610 | } |
| 6611 | /* don't try to normalize the inner spaces */ |
| 6612 | while (IS_BLANK(*cur)) cur++; |
| 6613 | *start++ = *cur++; |
| 6614 | if (*cur == 0) { |
| 6615 | *start = 0; |
| 6616 | return; |
| 6617 | } |
| 6618 | } while (1); |
| 6619 | } |
| 6620 | } |
| 6621 | |
| 6622 | /** |
| 6623 | * xmlRelaxNGCheckAttributes: |
| 6624 | * @ctxt: a Relax-NG parser context |
| 6625 | * @node: a Relax-NG node |
| 6626 | * |
| 6627 | * Check all the attributes on the given node |
| 6628 | */ |
| 6629 | static void |
| 6630 | xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) { |
| 6631 | xmlAttrPtr cur, next; |
| 6632 | |
| 6633 | cur = node->properties; |
| 6634 | while (cur != NULL) { |
| 6635 | next = cur->next; |
| 6636 | if ((cur->ns == NULL) || |
| 6637 | (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
| 6638 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6639 | if ((!xmlStrEqual(node->name, BAD_CAST "element")) && |
| 6640 | (!xmlStrEqual(node->name, BAD_CAST "attribute")) && |
| 6641 | (!xmlStrEqual(node->name, BAD_CAST "ref")) && |
| 6642 | (!xmlStrEqual(node->name, BAD_CAST "parentRef")) && |
Daniel Veillard | 2df2de2 | 2003-02-17 23:34:33 +0000 | [diff] [blame] | 6643 | (!xmlStrEqual(node->name, BAD_CAST "param")) && |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6644 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6645 | if (ctxt->error != NULL) |
| 6646 | ctxt->error(ctxt->userData, |
| 6647 | "Attribute %s is not allowed on %s\n", |
| 6648 | cur->name, node->name); |
| 6649 | ctxt->nbErrors++; |
| 6650 | } |
| 6651 | } else if (xmlStrEqual(cur->name, BAD_CAST "type")) { |
| 6652 | if ((!xmlStrEqual(node->name, BAD_CAST "value")) && |
| 6653 | (!xmlStrEqual(node->name, BAD_CAST "data"))) { |
| 6654 | if (ctxt->error != NULL) |
| 6655 | ctxt->error(ctxt->userData, |
| 6656 | "Attribute %s is not allowed on %s\n", |
| 6657 | cur->name, node->name); |
| 6658 | ctxt->nbErrors++; |
| 6659 | } |
| 6660 | } else if (xmlStrEqual(cur->name, BAD_CAST "href")) { |
| 6661 | if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) && |
| 6662 | (!xmlStrEqual(node->name, BAD_CAST "include"))) { |
| 6663 | if (ctxt->error != NULL) |
| 6664 | ctxt->error(ctxt->userData, |
| 6665 | "Attribute %s is not allowed on %s\n", |
| 6666 | cur->name, node->name); |
| 6667 | ctxt->nbErrors++; |
| 6668 | } |
| 6669 | } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) { |
| 6670 | if ((!xmlStrEqual(node->name, BAD_CAST "start")) && |
| 6671 | (!xmlStrEqual(node->name, BAD_CAST "define"))) { |
| 6672 | if (ctxt->error != NULL) |
| 6673 | ctxt->error(ctxt->userData, |
| 6674 | "Attribute %s is not allowed on %s\n", |
| 6675 | cur->name, node->name); |
| 6676 | ctxt->nbErrors++; |
| 6677 | } |
| 6678 | } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) { |
| 6679 | xmlChar *val; |
| 6680 | xmlURIPtr uri; |
| 6681 | |
| 6682 | val = xmlNodeListGetString(node->doc, cur->children, 1); |
| 6683 | if (val != NULL) { |
| 6684 | if (val[0] != 0) { |
| 6685 | uri = xmlParseURI((const char *) val); |
| 6686 | if (uri == NULL) { |
| 6687 | if (ctxt->error != NULL) |
| 6688 | ctxt->error(ctxt->userData, |
| 6689 | "Attribute %s contains invalid URI %s\n", |
| 6690 | cur->name, val); |
| 6691 | ctxt->nbErrors++; |
| 6692 | } else { |
| 6693 | if (uri->scheme == NULL) { |
| 6694 | if (ctxt->error != NULL) |
| 6695 | ctxt->error(ctxt->userData, |
| 6696 | "Attribute %s URI %s is not absolute\n", |
| 6697 | cur->name, val); |
| 6698 | ctxt->nbErrors++; |
| 6699 | } |
| 6700 | if (uri->fragment != NULL) { |
| 6701 | if (ctxt->error != NULL) |
| 6702 | ctxt->error(ctxt->userData, |
| 6703 | "Attribute %s URI %s has a fragment ID\n", |
| 6704 | cur->name, val); |
| 6705 | ctxt->nbErrors++; |
| 6706 | } |
| 6707 | xmlFreeURI(uri); |
| 6708 | } |
| 6709 | } |
| 6710 | xmlFree(val); |
| 6711 | } |
| 6712 | } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) { |
| 6713 | if (ctxt->error != NULL) |
| 6714 | ctxt->error(ctxt->userData, |
| 6715 | "Unknown attribute %s on %s\n", |
| 6716 | cur->name, node->name); |
| 6717 | ctxt->nbErrors++; |
| 6718 | } |
| 6719 | } |
| 6720 | cur = next; |
| 6721 | } |
| 6722 | } |
| 6723 | |
| 6724 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6725 | * xmlRelaxNGCleanupTree: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 6726 | * @ctxt: a Relax-NG parser context |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6727 | * @root: an xmlNodePtr subtree |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6728 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6729 | * Cleanup the subtree from unwanted nodes for parsing, resolve |
| 6730 | * Include and externalRef lookups. |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6731 | */ |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6732 | static void |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6733 | xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) { |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 6734 | xmlNodePtr cur, delete; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6735 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6736 | delete = NULL; |
| 6737 | cur = root; |
| 6738 | while (cur != NULL) { |
| 6739 | if (delete != NULL) { |
| 6740 | xmlUnlinkNode(delete); |
| 6741 | xmlFreeNode(delete); |
| 6742 | delete = NULL; |
| 6743 | } |
| 6744 | if (cur->type == XML_ELEMENT_NODE) { |
| 6745 | /* |
| 6746 | * Simplification 4.1. Annotations |
| 6747 | */ |
| 6748 | if ((cur->ns == NULL) || |
| 6749 | (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 6750 | if ((cur->parent != NULL) && |
| 6751 | (cur->parent->type == XML_ELEMENT_NODE) && |
| 6752 | ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) || |
| 6753 | (xmlStrEqual(cur->parent->name, BAD_CAST "value")) || |
| 6754 | (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) { |
| 6755 | if (ctxt->error != NULL) |
| 6756 | ctxt->error(ctxt->userData, |
| 6757 | "element %s doesn't allow foreign elements\n", |
| 6758 | cur->parent->name); |
| 6759 | ctxt->nbErrors++; |
| 6760 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 6761 | delete = cur; |
| 6762 | goto skip_children; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 6763 | } else { |
| 6764 | xmlRelaxNGCleanupAttributes(ctxt, cur); |
| 6765 | if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) { |
| 6766 | xmlChar *href, *ns, *base, *URL; |
| 6767 | xmlRelaxNGDocumentPtr docu; |
| 6768 | xmlNodePtr tmp; |
| 6769 | |
| 6770 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6771 | if (ns == NULL) { |
| 6772 | tmp = cur->parent; |
| 6773 | while ((tmp != NULL) && |
| 6774 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6775 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6776 | if (ns != NULL) |
| 6777 | break; |
| 6778 | tmp = tmp->parent; |
| 6779 | } |
| 6780 | } |
| 6781 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6782 | if (href == NULL) { |
| 6783 | if (ctxt->error != NULL) |
| 6784 | ctxt->error(ctxt->userData, |
| 6785 | "xmlRelaxNGParse: externalRef has no href attribute\n"); |
| 6786 | ctxt->nbErrors++; |
| 6787 | delete = cur; |
| 6788 | goto skip_children; |
| 6789 | } |
| 6790 | base = xmlNodeGetBase(cur->doc, cur); |
| 6791 | URL = xmlBuildURI(href, base); |
| 6792 | if (URL == NULL) { |
| 6793 | if (ctxt->error != NULL) |
| 6794 | ctxt->error(ctxt->userData, |
| 6795 | "Failed to compute URL for externalRef %s\n", href); |
| 6796 | ctxt->nbErrors++; |
| 6797 | if (href != NULL) |
| 6798 | xmlFree(href); |
| 6799 | if (base != NULL) |
| 6800 | xmlFree(base); |
| 6801 | delete = cur; |
| 6802 | goto skip_children; |
| 6803 | } |
| 6804 | if (href != NULL) |
| 6805 | xmlFree(href); |
| 6806 | if (base != NULL) |
| 6807 | xmlFree(base); |
| 6808 | docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns); |
| 6809 | if (docu == NULL) { |
| 6810 | if (ctxt->error != NULL) |
| 6811 | ctxt->error(ctxt->userData, |
| 6812 | "Failed to load externalRef %s\n", URL); |
| 6813 | ctxt->nbErrors++; |
| 6814 | xmlFree(URL); |
| 6815 | delete = cur; |
| 6816 | goto skip_children; |
| 6817 | } |
| 6818 | if (ns != NULL) |
| 6819 | xmlFree(ns); |
| 6820 | xmlFree(URL); |
| 6821 | cur->_private = docu; |
| 6822 | } else if (xmlStrEqual(cur->name, BAD_CAST "include")) { |
| 6823 | xmlChar *href, *ns, *base, *URL; |
| 6824 | xmlRelaxNGIncludePtr incl; |
| 6825 | xmlNodePtr tmp; |
| 6826 | |
| 6827 | href = xmlGetProp(cur, BAD_CAST "href"); |
| 6828 | if (href == NULL) { |
| 6829 | if (ctxt->error != NULL) |
| 6830 | ctxt->error(ctxt->userData, |
| 6831 | "xmlRelaxNGParse: include has no href attribute\n"); |
| 6832 | ctxt->nbErrors++; |
| 6833 | delete = cur; |
| 6834 | goto skip_children; |
| 6835 | } |
| 6836 | base = xmlNodeGetBase(cur->doc, cur); |
| 6837 | URL = xmlBuildURI(href, base); |
| 6838 | if (URL == NULL) { |
| 6839 | if (ctxt->error != NULL) |
| 6840 | ctxt->error(ctxt->userData, |
| 6841 | "Failed to compute URL for include %s\n", href); |
| 6842 | ctxt->nbErrors++; |
| 6843 | if (href != NULL) |
| 6844 | xmlFree(href); |
| 6845 | if (base != NULL) |
| 6846 | xmlFree(base); |
| 6847 | delete = cur; |
| 6848 | goto skip_children; |
| 6849 | } |
| 6850 | if (href != NULL) |
| 6851 | xmlFree(href); |
| 6852 | if (base != NULL) |
| 6853 | xmlFree(base); |
| 6854 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6855 | if (ns == NULL) { |
| 6856 | tmp = cur->parent; |
| 6857 | while ((tmp != NULL) && |
| 6858 | (tmp->type == XML_ELEMENT_NODE)) { |
| 6859 | ns = xmlGetProp(tmp, BAD_CAST "ns"); |
| 6860 | if (ns != NULL) |
| 6861 | break; |
| 6862 | tmp = tmp->parent; |
| 6863 | } |
| 6864 | } |
| 6865 | incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns); |
| 6866 | if (ns != NULL) |
| 6867 | xmlFree(ns); |
| 6868 | if (incl == NULL) { |
| 6869 | if (ctxt->error != NULL) |
| 6870 | ctxt->error(ctxt->userData, |
| 6871 | "Failed to load include %s\n", URL); |
| 6872 | ctxt->nbErrors++; |
| 6873 | xmlFree(URL); |
| 6874 | delete = cur; |
| 6875 | goto skip_children; |
| 6876 | } |
| 6877 | xmlFree(URL); |
| 6878 | cur->_private = incl; |
| 6879 | } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) || |
| 6880 | (xmlStrEqual(cur->name, BAD_CAST "attribute"))) { |
| 6881 | xmlChar *name, *ns; |
| 6882 | xmlNodePtr text = NULL; |
| 6883 | |
| 6884 | /* |
| 6885 | * Simplification 4.8. name attribute of element |
| 6886 | * and attribute elements |
| 6887 | */ |
| 6888 | name = xmlGetProp(cur, BAD_CAST "name"); |
| 6889 | if (name != NULL) { |
| 6890 | if (cur->children == NULL) { |
| 6891 | text = xmlNewChild(cur, cur->ns, BAD_CAST "name", |
| 6892 | name); |
| 6893 | } else { |
| 6894 | xmlNodePtr node; |
| 6895 | node = xmlNewNode(cur->ns, BAD_CAST "name"); |
| 6896 | if (node != NULL) { |
| 6897 | xmlAddPrevSibling(cur->children, node); |
| 6898 | text = xmlNewText(name); |
| 6899 | xmlAddChild(node, text); |
| 6900 | text = node; |
| 6901 | } |
| 6902 | } |
| 6903 | if (text == NULL) { |
| 6904 | if (ctxt->error != NULL) |
| 6905 | ctxt->error(ctxt->userData, |
| 6906 | "Failed to create a name %s element\n", name); |
| 6907 | ctxt->nbErrors++; |
| 6908 | } |
| 6909 | xmlUnsetProp(cur, BAD_CAST "name"); |
| 6910 | xmlFree(name); |
| 6911 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 6912 | if (ns != NULL) { |
| 6913 | if (text != NULL) { |
| 6914 | xmlSetProp(text, BAD_CAST "ns", ns); |
| 6915 | /* xmlUnsetProp(cur, BAD_CAST "ns"); */ |
| 6916 | } |
| 6917 | xmlFree(ns); |
| 6918 | } else if (xmlStrEqual(cur->name, |
| 6919 | BAD_CAST "attribute")) { |
| 6920 | xmlSetProp(text, BAD_CAST "ns", BAD_CAST ""); |
| 6921 | } |
| 6922 | } |
| 6923 | } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) || |
| 6924 | (xmlStrEqual(cur->name, BAD_CAST "nsName")) || |
| 6925 | (xmlStrEqual(cur->name, BAD_CAST "value"))) { |
| 6926 | /* |
| 6927 | * Simplification 4.8. name attribute of element |
| 6928 | * and attribute elements |
| 6929 | */ |
| 6930 | if (xmlHasProp(cur, BAD_CAST "ns") == NULL) { |
| 6931 | xmlNodePtr node; |
| 6932 | xmlChar *ns = NULL; |
| 6933 | |
| 6934 | node = cur->parent; |
| 6935 | while ((node != NULL) && |
| 6936 | (node->type == XML_ELEMENT_NODE)) { |
| 6937 | ns = xmlGetProp(node, BAD_CAST "ns"); |
| 6938 | if (ns != NULL) { |
| 6939 | break; |
| 6940 | } |
| 6941 | node = node->parent; |
| 6942 | } |
| 6943 | if (ns == NULL) { |
| 6944 | xmlSetProp(cur, BAD_CAST "ns", BAD_CAST ""); |
| 6945 | } else { |
| 6946 | xmlSetProp(cur, BAD_CAST "ns", ns); |
| 6947 | xmlFree(ns); |
| 6948 | } |
| 6949 | } |
| 6950 | if (xmlStrEqual(cur->name, BAD_CAST "name")) { |
| 6951 | xmlChar *name, *local, *prefix; |
| 6952 | |
| 6953 | /* |
| 6954 | * Simplification: 4.10. QNames |
| 6955 | */ |
| 6956 | name = xmlNodeGetContent(cur); |
| 6957 | if (name != NULL) { |
| 6958 | local = xmlSplitQName2(name, &prefix); |
| 6959 | if (local != NULL) { |
| 6960 | xmlNsPtr ns; |
| 6961 | |
| 6962 | ns = xmlSearchNs(cur->doc, cur, prefix); |
| 6963 | if (ns == NULL) { |
| 6964 | if (ctxt->error != NULL) |
| 6965 | ctxt->error(ctxt->userData, |
| 6966 | "xmlRelaxNGParse: no namespace for prefix %s\n", prefix); |
| 6967 | ctxt->nbErrors++; |
| 6968 | } else { |
| 6969 | xmlSetProp(cur, BAD_CAST "ns", ns->href); |
| 6970 | xmlNodeSetContent(cur, local); |
| 6971 | } |
| 6972 | xmlFree(local); |
| 6973 | xmlFree(prefix); |
| 6974 | } |
| 6975 | xmlFree(name); |
| 6976 | } |
| 6977 | } |
| 6978 | /* |
| 6979 | * 4.16 |
| 6980 | */ |
| 6981 | if (xmlStrEqual(cur->name, BAD_CAST "nsName")) { |
| 6982 | if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 6983 | if (ctxt->error != NULL) |
| 6984 | ctxt->error(ctxt->userData, |
| 6985 | "Found nsName/except//nsName forbidden construct\n"); |
| 6986 | ctxt->nbErrors++; |
| 6987 | } |
| 6988 | } |
| 6989 | } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) && |
| 6990 | (cur != root)) { |
| 6991 | int oldflags = ctxt->flags; |
| 6992 | |
| 6993 | /* |
| 6994 | * 4.16 |
| 6995 | */ |
| 6996 | if ((cur->parent != NULL) && |
| 6997 | (xmlStrEqual(cur->parent->name, BAD_CAST "anyName"))) { |
| 6998 | ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT; |
| 6999 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7000 | ctxt->flags = oldflags; |
| 7001 | goto skip_children; |
| 7002 | } else if ((cur->parent != NULL) && |
| 7003 | (xmlStrEqual(cur->parent->name, BAD_CAST "nsName"))) { |
| 7004 | ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT; |
| 7005 | xmlRelaxNGCleanupTree(ctxt, cur); |
| 7006 | ctxt->flags = oldflags; |
| 7007 | goto skip_children; |
| 7008 | } |
| 7009 | } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) { |
| 7010 | /* |
| 7011 | * 4.16 |
| 7012 | */ |
| 7013 | if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) { |
| 7014 | if (ctxt->error != NULL) |
| 7015 | ctxt->error(ctxt->userData, |
| 7016 | "Found anyName/except//anyName forbidden construct\n"); |
| 7017 | ctxt->nbErrors++; |
| 7018 | } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) { |
| 7019 | if (ctxt->error != NULL) |
| 7020 | ctxt->error(ctxt->userData, |
| 7021 | "Found nsName/except//anyName forbidden construct\n"); |
| 7022 | ctxt->nbErrors++; |
| 7023 | } |
| 7024 | } |
| 7025 | /* |
| 7026 | * Thisd is not an else since "include" is transformed |
| 7027 | * into a div |
| 7028 | */ |
| 7029 | if (xmlStrEqual(cur->name, BAD_CAST "div")) { |
| 7030 | xmlChar *ns; |
| 7031 | xmlNodePtr child, ins, tmp; |
| 7032 | |
| 7033 | /* |
| 7034 | * implements rule 4.11 |
| 7035 | */ |
| 7036 | |
| 7037 | ns = xmlGetProp(cur, BAD_CAST "ns"); |
| 7038 | |
| 7039 | child = cur->children; |
| 7040 | ins = cur; |
| 7041 | while (child != NULL) { |
| 7042 | if (ns != NULL) { |
| 7043 | if (!xmlHasProp(child, BAD_CAST "ns")) { |
| 7044 | xmlSetProp(child, BAD_CAST "ns", ns); |
| 7045 | } |
| 7046 | } |
| 7047 | tmp = child->next; |
| 7048 | xmlUnlinkNode(child); |
| 7049 | ins = xmlAddNextSibling(ins, child); |
| 7050 | child = tmp; |
| 7051 | } |
| 7052 | if (ns != NULL) |
| 7053 | xmlFree(ns); |
| 7054 | delete = cur; |
| 7055 | goto skip_children; |
| 7056 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7057 | } |
| 7058 | } |
| 7059 | /* |
| 7060 | * Simplification 4.2 whitespaces |
| 7061 | */ |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7062 | else if ((cur->type == XML_TEXT_NODE) || |
| 7063 | (cur->type == XML_CDATA_SECTION_NODE)) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7064 | if (IS_BLANK_NODE(cur)) { |
| 7065 | if (cur->parent->type == XML_ELEMENT_NODE) { |
| 7066 | if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) && |
| 7067 | (!xmlStrEqual(cur->parent->name, BAD_CAST "param"))) |
| 7068 | delete = cur; |
| 7069 | } else { |
| 7070 | delete = cur; |
| 7071 | goto skip_children; |
| 7072 | } |
| 7073 | } |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 7074 | } else { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7075 | delete = cur; |
| 7076 | goto skip_children; |
| 7077 | } |
| 7078 | |
| 7079 | /* |
| 7080 | * Skip to next node |
| 7081 | */ |
| 7082 | if (cur->children != NULL) { |
| 7083 | if ((cur->children->type != XML_ENTITY_DECL) && |
| 7084 | (cur->children->type != XML_ENTITY_REF_NODE) && |
| 7085 | (cur->children->type != XML_ENTITY_NODE)) { |
| 7086 | cur = cur->children; |
| 7087 | continue; |
| 7088 | } |
| 7089 | } |
| 7090 | skip_children: |
| 7091 | if (cur->next != NULL) { |
| 7092 | cur = cur->next; |
| 7093 | continue; |
| 7094 | } |
| 7095 | |
| 7096 | do { |
| 7097 | cur = cur->parent; |
| 7098 | if (cur == NULL) |
| 7099 | break; |
| 7100 | if (cur == root) { |
| 7101 | cur = NULL; |
| 7102 | break; |
| 7103 | } |
| 7104 | if (cur->next != NULL) { |
| 7105 | cur = cur->next; |
| 7106 | break; |
| 7107 | } |
| 7108 | } while (cur != NULL); |
| 7109 | } |
| 7110 | if (delete != NULL) { |
| 7111 | xmlUnlinkNode(delete); |
| 7112 | xmlFreeNode(delete); |
| 7113 | delete = NULL; |
| 7114 | } |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7115 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7116 | |
Daniel Veillard | c5312d7 | 2003-02-21 17:14:10 +0000 | [diff] [blame] | 7117 | /** |
| 7118 | * xmlRelaxNGCleanupDoc: |
| 7119 | * @ctxt: a Relax-NG parser context |
| 7120 | * @doc: an xmldocPtr document pointer |
| 7121 | * |
| 7122 | * Cleanup the document from unwanted nodes for parsing, resolve |
| 7123 | * Include and externalRef lookups. |
| 7124 | * |
| 7125 | * Returns the cleaned up document or NULL in case of error |
| 7126 | */ |
| 7127 | static xmlDocPtr |
| 7128 | xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) { |
| 7129 | xmlNodePtr root; |
| 7130 | |
| 7131 | /* |
| 7132 | * Extract the root |
| 7133 | */ |
| 7134 | root = xmlDocGetRootElement(doc); |
| 7135 | if (root == NULL) { |
| 7136 | if (ctxt->error != NULL) |
| 7137 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 7138 | ctxt->URL); |
| 7139 | ctxt->nbErrors++; |
| 7140 | return (NULL); |
| 7141 | } |
| 7142 | xmlRelaxNGCleanupTree(ctxt, root); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7143 | return(doc); |
| 7144 | } |
| 7145 | |
| 7146 | /** |
| 7147 | * xmlRelaxNGParse: |
| 7148 | * @ctxt: a Relax-NG parser context |
| 7149 | * |
| 7150 | * parse a schema definition resource and build an internal |
| 7151 | * XML Shema struture which can be used to validate instances. |
| 7152 | * *WARNING* this interface is highly subject to change |
| 7153 | * |
| 7154 | * Returns the internal XML RelaxNG structure built from the resource or |
| 7155 | * NULL in case of error |
| 7156 | */ |
| 7157 | xmlRelaxNGPtr |
| 7158 | xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt) |
| 7159 | { |
| 7160 | xmlRelaxNGPtr ret = NULL; |
| 7161 | xmlDocPtr doc; |
| 7162 | xmlNodePtr root; |
| 7163 | |
| 7164 | xmlRelaxNGInitTypes(); |
| 7165 | |
| 7166 | if (ctxt == NULL) |
| 7167 | return (NULL); |
| 7168 | |
| 7169 | /* |
| 7170 | * First step is to parse the input document into an DOM/Infoset |
| 7171 | */ |
| 7172 | if (ctxt->URL != NULL) { |
| 7173 | doc = xmlParseFile((const char *) ctxt->URL); |
| 7174 | if (doc == NULL) { |
| 7175 | if (ctxt->error != NULL) |
| 7176 | ctxt->error(ctxt->userData, |
| 7177 | "xmlRelaxNGParse: could not load %s\n", ctxt->URL); |
| 7178 | ctxt->nbErrors++; |
| 7179 | return (NULL); |
| 7180 | } |
| 7181 | } else if (ctxt->buffer != NULL) { |
| 7182 | doc = xmlParseMemory(ctxt->buffer, ctxt->size); |
| 7183 | if (doc == NULL) { |
| 7184 | if (ctxt->error != NULL) |
| 7185 | ctxt->error(ctxt->userData, |
| 7186 | "xmlRelaxNGParse: could not parse schemas\n"); |
| 7187 | ctxt->nbErrors++; |
| 7188 | return (NULL); |
| 7189 | } |
| 7190 | doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
| 7191 | ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer"); |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7192 | } else if (ctxt->document != NULL) { |
| 7193 | doc = ctxt->document; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7194 | } else { |
| 7195 | if (ctxt->error != NULL) |
| 7196 | ctxt->error(ctxt->userData, |
| 7197 | "xmlRelaxNGParse: nothing to parse\n"); |
| 7198 | ctxt->nbErrors++; |
| 7199 | return (NULL); |
| 7200 | } |
| 7201 | ctxt->document = doc; |
| 7202 | |
| 7203 | /* |
| 7204 | * Some preprocessing of the document content |
| 7205 | */ |
| 7206 | doc = xmlRelaxNGCleanupDoc(ctxt, doc); |
| 7207 | if (doc == NULL) { |
| 7208 | xmlFreeDoc(ctxt->document); |
| 7209 | ctxt->document = NULL; |
| 7210 | return(NULL); |
| 7211 | } |
| 7212 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7213 | /* |
| 7214 | * Then do the parsing for good |
| 7215 | */ |
| 7216 | root = xmlDocGetRootElement(doc); |
| 7217 | if (root == NULL) { |
| 7218 | if (ctxt->error != NULL) |
| 7219 | ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n", |
| 7220 | ctxt->URL); |
| 7221 | ctxt->nbErrors++; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7222 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7223 | return (NULL); |
| 7224 | } |
| 7225 | ret = xmlRelaxNGParseDocument(ctxt, root); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7226 | if (ret == NULL) { |
| 7227 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7228 | return(NULL); |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7229 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7230 | |
| 7231 | /* |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7232 | * Check the ref/defines links |
| 7233 | */ |
| 7234 | /* |
| 7235 | * try to preprocess interleaves |
| 7236 | */ |
| 7237 | if (ctxt->interleaves != NULL) { |
| 7238 | xmlHashScan(ctxt->interleaves, |
| 7239 | (xmlHashScanner)xmlRelaxNGComputeInterleaves, ctxt); |
| 7240 | } |
| 7241 | |
| 7242 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7243 | * if there was a parsing error return NULL |
| 7244 | */ |
| 7245 | if (ctxt->nbErrors > 0) { |
| 7246 | xmlRelaxNGFree(ret); |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7247 | ctxt->document = NULL; |
| 7248 | xmlFreeDoc(doc); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7249 | return(NULL); |
| 7250 | } |
| 7251 | |
| 7252 | /* |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7253 | * try to compile (parts of) the schemas |
| 7254 | */ |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7255 | if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) { |
| 7256 | if (ret->topgrammar->start->type != XML_RELAXNG_START) { |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7257 | xmlRelaxNGDefinePtr def; |
| 7258 | |
| 7259 | def = xmlRelaxNGNewDefine(ctxt, NULL); |
| 7260 | if (def != NULL) { |
| 7261 | def->type = XML_RELAXNG_START; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7262 | def->content = ret->topgrammar->start; |
| 7263 | ret->topgrammar->start = def; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7264 | } |
| 7265 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7266 | xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7267 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 7268 | |
| 7269 | /* |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7270 | * Transfer the pointer for cleanup at the schema level. |
| 7271 | */ |
| 7272 | ret->doc = doc; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7273 | ctxt->document = NULL; |
| 7274 | ret->documents = ctxt->documents; |
| 7275 | ctxt->documents = NULL; |
Daniel Veillard | 3ebc7d4 | 2003-02-24 17:17:58 +0000 | [diff] [blame] | 7276 | |
Daniel Veillard | e2a5a08 | 2003-02-02 14:35:17 +0000 | [diff] [blame] | 7277 | ret->includes = ctxt->includes; |
| 7278 | ctxt->includes = NULL; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7279 | ret->defNr = ctxt->defNr; |
| 7280 | ret->defTab = ctxt->defTab; |
| 7281 | ctxt->defTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 7282 | if (ctxt->idref == 1) |
| 7283 | ret->idref = 1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7284 | |
| 7285 | return (ret); |
| 7286 | } |
| 7287 | |
| 7288 | /** |
| 7289 | * xmlRelaxNGSetParserErrors: |
| 7290 | * @ctxt: a Relax-NG validation context |
| 7291 | * @err: the error callback |
| 7292 | * @warn: the warning callback |
| 7293 | * @ctx: contextual data for the callbacks |
| 7294 | * |
| 7295 | * Set the callback functions used to handle errors for a validation context |
| 7296 | */ |
| 7297 | void |
| 7298 | xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
| 7299 | xmlRelaxNGValidityErrorFunc err, |
| 7300 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 7301 | if (ctxt == NULL) |
| 7302 | return; |
| 7303 | ctxt->error = err; |
| 7304 | ctxt->warning = warn; |
| 7305 | ctxt->userData = ctx; |
| 7306 | } |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 7307 | |
| 7308 | /** |
| 7309 | * xmlRelaxNGGetParserErrors: |
| 7310 | * @ctxt: a Relax-NG validation context |
| 7311 | * @err: the error callback result |
| 7312 | * @warn: the warning callback result |
| 7313 | * @ctx: contextual data for the callbacks result |
| 7314 | * |
| 7315 | * Get the callback information used to handle errors for a validation context |
| 7316 | * |
| 7317 | * Returns -1 in case of failure, 0 otherwise. |
| 7318 | */ |
| 7319 | int |
| 7320 | xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt, |
| 7321 | xmlRelaxNGValidityErrorFunc *err, |
| 7322 | xmlRelaxNGValidityWarningFunc *warn, void **ctx) { |
| 7323 | if (ctxt == NULL) |
| 7324 | return(-1); |
| 7325 | if (err != NULL) *err = ctxt->error; |
| 7326 | if (warn != NULL) *warn = ctxt->warning; |
| 7327 | if (ctx != NULL) *ctx = ctxt->userData; |
| 7328 | return(0); |
| 7329 | } |
| 7330 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7331 | /************************************************************************ |
| 7332 | * * |
| 7333 | * Dump back a compiled form * |
| 7334 | * * |
| 7335 | ************************************************************************/ |
| 7336 | static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define); |
| 7337 | |
| 7338 | /** |
| 7339 | * xmlRelaxNGDumpDefines: |
| 7340 | * @output: the file output |
| 7341 | * @defines: a list of define structures |
| 7342 | * |
| 7343 | * Dump a RelaxNG structure back |
| 7344 | */ |
| 7345 | static void |
| 7346 | xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) { |
| 7347 | while (defines != NULL) { |
| 7348 | xmlRelaxNGDumpDefine(output, defines); |
| 7349 | defines = defines->next; |
| 7350 | } |
| 7351 | } |
| 7352 | |
| 7353 | /** |
| 7354 | * xmlRelaxNGDumpDefine: |
| 7355 | * @output: the file output |
| 7356 | * @define: a define structure |
| 7357 | * |
| 7358 | * Dump a RelaxNG structure back |
| 7359 | */ |
| 7360 | static void |
| 7361 | xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) { |
| 7362 | if (define == NULL) |
| 7363 | return; |
| 7364 | switch(define->type) { |
| 7365 | case XML_RELAXNG_EMPTY: |
| 7366 | fprintf(output, "<empty/>\n"); |
| 7367 | break; |
| 7368 | case XML_RELAXNG_NOT_ALLOWED: |
| 7369 | fprintf(output, "<notAllowed/>\n"); |
| 7370 | break; |
| 7371 | case XML_RELAXNG_TEXT: |
| 7372 | fprintf(output, "<text/>\n"); |
| 7373 | break; |
| 7374 | case XML_RELAXNG_ELEMENT: |
| 7375 | fprintf(output, "<element>\n"); |
| 7376 | if (define->name != NULL) { |
| 7377 | fprintf(output, "<name"); |
| 7378 | if (define->ns != NULL) |
| 7379 | fprintf(output, " ns=\"%s\"", define->ns); |
| 7380 | fprintf(output, ">%s</name>\n", define->name); |
| 7381 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7382 | xmlRelaxNGDumpDefines(output, define->attrs); |
| 7383 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7384 | fprintf(output, "</element>\n"); |
| 7385 | break; |
| 7386 | case XML_RELAXNG_LIST: |
| 7387 | fprintf(output, "<list>\n"); |
| 7388 | xmlRelaxNGDumpDefines(output, define->content); |
| 7389 | fprintf(output, "</list>\n"); |
| 7390 | break; |
| 7391 | case XML_RELAXNG_ONEORMORE: |
| 7392 | fprintf(output, "<oneOrMore>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7393 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7394 | fprintf(output, "</oneOrMore>\n"); |
| 7395 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7396 | case XML_RELAXNG_ZEROORMORE: |
| 7397 | fprintf(output, "<zeroOrMore>\n"); |
| 7398 | xmlRelaxNGDumpDefines(output, define->content); |
| 7399 | fprintf(output, "</zeroOrMore>\n"); |
| 7400 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7401 | case XML_RELAXNG_CHOICE: |
| 7402 | fprintf(output, "<choice>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7403 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7404 | fprintf(output, "</choice>\n"); |
| 7405 | break; |
| 7406 | case XML_RELAXNG_GROUP: |
| 7407 | fprintf(output, "<group>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7408 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7409 | fprintf(output, "</group>\n"); |
| 7410 | break; |
| 7411 | case XML_RELAXNG_INTERLEAVE: |
| 7412 | fprintf(output, "<interleave>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7413 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7414 | fprintf(output, "</interleave>\n"); |
| 7415 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7416 | case XML_RELAXNG_OPTIONAL: |
| 7417 | fprintf(output, "<optional>\n"); |
| 7418 | xmlRelaxNGDumpDefines(output, define->content); |
| 7419 | fprintf(output, "</optional>\n"); |
| 7420 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7421 | case XML_RELAXNG_ATTRIBUTE: |
| 7422 | fprintf(output, "<attribute>\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7423 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7424 | fprintf(output, "</attribute>\n"); |
| 7425 | break; |
| 7426 | case XML_RELAXNG_DEF: |
| 7427 | fprintf(output, "<define"); |
| 7428 | if (define->name != NULL) |
| 7429 | fprintf(output, " name=\"%s\"", define->name); |
| 7430 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7431 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7432 | fprintf(output, "</define>\n"); |
| 7433 | break; |
| 7434 | case XML_RELAXNG_REF: |
| 7435 | fprintf(output, "<ref"); |
| 7436 | if (define->name != NULL) |
| 7437 | fprintf(output, " name=\"%s\"", define->name); |
| 7438 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7439 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7440 | fprintf(output, "</ref>\n"); |
| 7441 | break; |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7442 | case XML_RELAXNG_PARENTREF: |
| 7443 | fprintf(output, "<parentRef"); |
| 7444 | if (define->name != NULL) |
| 7445 | fprintf(output, " name=\"%s\"", define->name); |
| 7446 | fprintf(output, ">\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7447 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 419a768 | 2003-02-03 23:22:49 +0000 | [diff] [blame] | 7448 | fprintf(output, "</parentRef>\n"); |
| 7449 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7450 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 7451 | fprintf(output, "<externalRef>"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7452 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | e431a27 | 2003-01-29 23:02:33 +0000 | [diff] [blame] | 7453 | fprintf(output, "</externalRef>\n"); |
| 7454 | break; |
| 7455 | case XML_RELAXNG_DATATYPE: |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7456 | case XML_RELAXNG_VALUE: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7457 | TODO |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7458 | break; |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7459 | case XML_RELAXNG_START: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7460 | case XML_RELAXNG_EXCEPT: |
Daniel Veillard | 8fe9871 | 2003-02-19 00:19:14 +0000 | [diff] [blame] | 7461 | case XML_RELAXNG_PARAM: |
Daniel Veillard | d41f4f4 | 2003-01-29 21:07:52 +0000 | [diff] [blame] | 7462 | TODO |
| 7463 | break; |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 7464 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7465 | xmlRelaxNGDumpDefines(output, define->content); |
Daniel Veillard | 77648bb | 2003-02-20 15:03:22 +0000 | [diff] [blame] | 7466 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7467 | } |
| 7468 | } |
| 7469 | |
| 7470 | /** |
| 7471 | * xmlRelaxNGDumpGrammar: |
| 7472 | * @output: the file output |
| 7473 | * @grammar: a grammar structure |
| 7474 | * @top: is this a top grammar |
| 7475 | * |
| 7476 | * Dump a RelaxNG structure back |
| 7477 | */ |
| 7478 | static void |
| 7479 | xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top) |
| 7480 | { |
| 7481 | if (grammar == NULL) |
| 7482 | return; |
| 7483 | |
| 7484 | fprintf(output, "<grammar"); |
| 7485 | if (top) |
| 7486 | fprintf(output, |
| 7487 | " xmlns=\"http://relaxng.org/ns/structure/1.0\""); |
| 7488 | switch(grammar->combine) { |
| 7489 | case XML_RELAXNG_COMBINE_UNDEFINED: |
| 7490 | break; |
| 7491 | case XML_RELAXNG_COMBINE_CHOICE: |
| 7492 | fprintf(output, " combine=\"choice\""); |
| 7493 | break; |
| 7494 | case XML_RELAXNG_COMBINE_INTERLEAVE: |
| 7495 | fprintf(output, " combine=\"interleave\""); |
| 7496 | break; |
| 7497 | default: |
| 7498 | fprintf(output, " <!-- invalid combine value -->"); |
| 7499 | } |
| 7500 | fprintf(output, ">\n"); |
| 7501 | if (grammar->start == NULL) { |
| 7502 | fprintf(output, " <!-- grammar had no start -->"); |
| 7503 | } else { |
| 7504 | fprintf(output, "<start>\n"); |
| 7505 | xmlRelaxNGDumpDefine(output, grammar->start); |
| 7506 | fprintf(output, "</start>\n"); |
| 7507 | } |
| 7508 | /* TODO ? Dump the defines ? */ |
| 7509 | fprintf(output, "</grammar>\n"); |
| 7510 | } |
| 7511 | |
| 7512 | /** |
| 7513 | * xmlRelaxNGDump: |
| 7514 | * @output: the file output |
| 7515 | * @schema: a schema structure |
| 7516 | * |
| 7517 | * Dump a RelaxNG structure back |
| 7518 | */ |
| 7519 | void |
| 7520 | xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema) |
| 7521 | { |
| 7522 | if (schema == NULL) { |
| 7523 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7524 | return; |
| 7525 | } |
| 7526 | fprintf(output, "RelaxNG: "); |
| 7527 | if (schema->doc == NULL) { |
| 7528 | fprintf(output, "no document\n"); |
| 7529 | } else if (schema->doc->URL != NULL) { |
| 7530 | fprintf(output, "%s\n", schema->doc->URL); |
| 7531 | } else { |
| 7532 | fprintf(output, "\n"); |
| 7533 | } |
| 7534 | if (schema->topgrammar == NULL) { |
| 7535 | fprintf(output, "RelaxNG has no top grammar\n"); |
| 7536 | return; |
| 7537 | } |
| 7538 | xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1); |
| 7539 | } |
| 7540 | |
Daniel Veillard | febcca4 | 2003-02-16 15:44:18 +0000 | [diff] [blame] | 7541 | /** |
| 7542 | * xmlRelaxNGDumpTree: |
| 7543 | * @output: the file output |
| 7544 | * @schema: a schema structure |
| 7545 | * |
| 7546 | * Dump the transformed RelaxNG tree. |
| 7547 | */ |
| 7548 | void |
| 7549 | xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema) |
| 7550 | { |
| 7551 | if (schema == NULL) { |
| 7552 | fprintf(output, "RelaxNG empty or failed to compile\n"); |
| 7553 | return; |
| 7554 | } |
| 7555 | if (schema->doc == NULL) { |
| 7556 | fprintf(output, "no document\n"); |
| 7557 | } else { |
| 7558 | xmlDocDump(output, schema->doc); |
| 7559 | } |
| 7560 | } |
| 7561 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7562 | /************************************************************************ |
| 7563 | * * |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7564 | * Validation of compiled content * |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 7565 | * * |
| 7566 | ************************************************************************/ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 7567 | static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 7568 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7569 | |
| 7570 | /** |
| 7571 | * xmlRelaxNGValidateCompiledCallback: |
| 7572 | * @exec: the regular expression instance |
| 7573 | * @token: the token which matched |
| 7574 | * @transdata: callback data, the define for the subelement if available |
| 7575 | @ @inputdata: callback data, the Relax NG validation context |
| 7576 | * |
| 7577 | * Handle the callback and if needed validate the element children. |
| 7578 | */ |
| 7579 | static void |
| 7580 | xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
| 7581 | const xmlChar *token, |
| 7582 | void *transdata, |
| 7583 | void *inputdata) { |
| 7584 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7585 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
| 7586 | int ret; |
| 7587 | |
| 7588 | #ifdef DEBUG_COMPILE |
| 7589 | xmlGenericError(xmlGenericErrorContext, |
| 7590 | "Compiled callback for: '%s'\n", token); |
| 7591 | #endif |
| 7592 | if (ctxt == NULL) { |
| 7593 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7594 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7595 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7596 | return; |
| 7597 | } |
| 7598 | if (define == NULL) { |
| 7599 | if (token[0] == '#') |
| 7600 | return; |
| 7601 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7602 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7603 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7604 | return; |
| 7605 | } |
| 7606 | if ((ctxt == NULL) || (define == NULL)) { |
| 7607 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7608 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7609 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7610 | return; |
| 7611 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
| 7612 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7613 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7614 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7615 | return; |
| 7616 | } |
| 7617 | ret = xmlRelaxNGValidateDefinition(ctxt, define); |
| 7618 | } |
| 7619 | |
| 7620 | /** |
| 7621 | * xmlRelaxNGValidateCompiledContent: |
| 7622 | * @ctxt: the RelaxNG validation context |
| 7623 | * @regexp: the regular expression as compiled |
| 7624 | * @content: list of children to test against the regexp |
| 7625 | * |
| 7626 | * Validate the content model of an element or start using the regexp |
| 7627 | * |
| 7628 | * Returns 0 in case of success, -1 in case of error. |
| 7629 | */ |
| 7630 | static int |
| 7631 | xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 7632 | xmlRegexpPtr regexp, xmlNodePtr content) { |
| 7633 | xmlRegExecCtxtPtr exec; |
| 7634 | xmlNodePtr cur; |
Daniel Veillard | 6216360 | 2003-04-17 09:36:38 +0000 | [diff] [blame] | 7635 | int ret = 0; |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 7636 | |
| 7637 | if ((ctxt == NULL) || (regexp == NULL)) |
| 7638 | return(-1); |
| 7639 | exec = xmlRegNewExecCtxt(regexp, |
| 7640 | xmlRelaxNGValidateCompiledCallback, ctxt); |
| 7641 | cur = content; |
| 7642 | while (cur != NULL) { |
| 7643 | ctxt->state->seq = cur; |
| 7644 | switch (cur->type) { |
| 7645 | case XML_TEXT_NODE: |
| 7646 | case XML_CDATA_SECTION_NODE: |
| 7647 | if (xmlIsBlankNode(cur)) |
| 7648 | break; |
| 7649 | ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt); |
| 7650 | if (ret < 0) { |
| 7651 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, cur->parent->name); |
| 7652 | } |
| 7653 | break; |
| 7654 | case XML_ELEMENT_NODE: |
| 7655 | if (cur->ns != NULL) { |
| 7656 | ret = xmlRegExecPushString2(exec, cur->name, |
| 7657 | cur->ns->href, ctxt); |
| 7658 | } else { |
| 7659 | ret = xmlRegExecPushString(exec, cur->name, ctxt); |
| 7660 | } |
| 7661 | if (ret < 0) { |
| 7662 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name); |
| 7663 | } |
| 7664 | break; |
| 7665 | default: |
| 7666 | break; |
| 7667 | } |
| 7668 | if (ret < 0) break; |
| 7669 | /* |
| 7670 | * Switch to next element |
| 7671 | */ |
| 7672 | cur = cur->next; |
| 7673 | } |
| 7674 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 7675 | if (ret == 1) { |
| 7676 | ret = 0; |
| 7677 | ctxt->state->seq = NULL; |
| 7678 | } else if (ret == 0) { |
| 7679 | /* |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7680 | * 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] | 7681 | */ |
| 7682 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 7683 | ret = -1; |
| 7684 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7685 | xmlRelaxNGDumpValidError(ctxt); |
| 7686 | } else { |
| 7687 | ret = -1; |
| 7688 | } |
| 7689 | xmlRegFreeExecCtxt(exec); |
| 7690 | return(ret); |
| 7691 | } |
| 7692 | |
| 7693 | /************************************************************************ |
| 7694 | * * |
| 7695 | * Progressive validation of when possible * |
| 7696 | * * |
| 7697 | ************************************************************************/ |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7698 | static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 7699 | xmlRelaxNGDefinePtr defines); |
| 7700 | static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt); |
| 7701 | |
| 7702 | /** |
| 7703 | * xmlRelaxNGElemPush: |
| 7704 | * @ctxt: the validation context |
| 7705 | * @exec: the regexp runtime for the new content model |
| 7706 | * |
| 7707 | * Push a new regexp for the current node content model on the stack |
| 7708 | * |
| 7709 | * Returns 0 in case of success and -1 in case of error. |
| 7710 | */ |
| 7711 | static int |
| 7712 | xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) { |
| 7713 | if (ctxt->elemTab == NULL) { |
| 7714 | ctxt->elemMax = 10; |
| 7715 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax * |
| 7716 | sizeof(xmlRegExecCtxtPtr)); |
| 7717 | if (ctxt->elemTab == NULL) { |
| 7718 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7719 | return(-1); |
| 7720 | } |
| 7721 | } |
| 7722 | if (ctxt->elemNr >= ctxt->elemMax) { |
| 7723 | ctxt->elemMax *= 2; |
| 7724 | ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab, |
| 7725 | ctxt->elemMax * sizeof(xmlRegExecCtxtPtr)); |
| 7726 | if (ctxt->elemTab == NULL) { |
| 7727 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 7728 | return(-1); |
| 7729 | } |
| 7730 | } |
| 7731 | ctxt->elemTab[ctxt->elemNr++] = exec; |
| 7732 | ctxt->elem = exec; |
| 7733 | return(0); |
| 7734 | } |
| 7735 | |
| 7736 | /** |
| 7737 | * xmlRelaxNGElemPop: |
| 7738 | * @ctxt: the validation context |
| 7739 | * |
| 7740 | * Pop the regexp of the current node content model from the stack |
| 7741 | * |
| 7742 | * Returns the exec or NULL if empty |
| 7743 | */ |
| 7744 | static xmlRegExecCtxtPtr |
| 7745 | xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) { |
| 7746 | xmlRegExecCtxtPtr ret; |
| 7747 | |
| 7748 | if (ctxt->elemNr <= 0) return(NULL); |
| 7749 | ctxt->elemNr--; |
| 7750 | ret = ctxt->elemTab[ctxt->elemNr]; |
| 7751 | ctxt->elemTab[ctxt->elemNr] = NULL; |
| 7752 | if (ctxt->elemNr > 0) |
| 7753 | ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1]; |
| 7754 | else |
| 7755 | ctxt->elem = NULL; |
| 7756 | return(ret); |
| 7757 | } |
| 7758 | |
| 7759 | /** |
| 7760 | * xmlRelaxNGValidateProgressiveCallback: |
| 7761 | * @exec: the regular expression instance |
| 7762 | * @token: the token which matched |
| 7763 | * @transdata: callback data, the define for the subelement if available |
| 7764 | @ @inputdata: callback data, the Relax NG validation context |
| 7765 | * |
| 7766 | * Handle the callback and if needed validate the element children. |
| 7767 | * some of the in/out informations are passed via the context in @inputdata. |
| 7768 | */ |
| 7769 | static void |
| 7770 | xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED, |
| 7771 | const xmlChar *token, |
| 7772 | void *transdata, |
| 7773 | void *inputdata) { |
| 7774 | xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata; |
| 7775 | xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7776 | xmlRelaxNGValidStatePtr state, oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7777 | xmlNodePtr node = ctxt->pnode; |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 7778 | int ret = 0, oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7779 | |
| 7780 | #ifdef DEBUG_PROGRESSIVE |
| 7781 | xmlGenericError(xmlGenericErrorContext, |
| 7782 | "Progressive callback for: '%s'\n", token); |
| 7783 | #endif |
| 7784 | if (ctxt == NULL) { |
| 7785 | fprintf(stderr, "callback on %s missing context\n", token); |
| 7786 | return; |
| 7787 | } |
| 7788 | ctxt->pstate = 1; |
| 7789 | if (define == NULL) { |
| 7790 | if (token[0] == '#') |
| 7791 | return; |
| 7792 | fprintf(stderr, "callback on %s missing define\n", token); |
| 7793 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7794 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7795 | ctxt->pstate = -1; |
| 7796 | return; |
| 7797 | } |
| 7798 | if ((ctxt == NULL) || (define == NULL)) { |
| 7799 | fprintf(stderr, "callback on %s missing info\n", token); |
| 7800 | if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK)) |
| 7801 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7802 | ctxt->pstate = -1; |
| 7803 | return; |
| 7804 | } else if (define->type != XML_RELAXNG_ELEMENT) { |
| 7805 | fprintf(stderr, "callback on %s define is not element\n", token); |
| 7806 | if (ctxt->errNo == XML_RELAXNG_OK) |
| 7807 | ctxt->errNo = XML_RELAXNG_ERR_INTERNAL; |
| 7808 | ctxt->pstate = -1; |
| 7809 | return; |
| 7810 | } |
| 7811 | if (node->type != XML_ELEMENT_NODE) { |
| 7812 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 7813 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 7814 | xmlRelaxNGDumpValidError(ctxt); |
| 7815 | ctxt->pstate = -1; |
| 7816 | return; |
| 7817 | } |
| 7818 | if (define->contModel == NULL) { |
| 7819 | /* |
| 7820 | * this node cannot be validated in a streamable fashion |
| 7821 | */ |
| 7822 | #ifdef DEBUG_PROGRESSIVE |
| 7823 | xmlGenericError(xmlGenericErrorContext, |
| 7824 | "Element '%s' validation is not streamable\n", token); |
| 7825 | #endif |
| 7826 | ctxt->pstate = 0; |
| 7827 | ctxt->pdef = define; |
| 7828 | return; |
| 7829 | } |
| 7830 | exec = xmlRegNewExecCtxt(define->contModel, |
| 7831 | xmlRelaxNGValidateProgressiveCallback, |
| 7832 | ctxt); |
| 7833 | if (exec == NULL) { |
| 7834 | ctxt->pstate = -1; |
| 7835 | return; |
| 7836 | } |
| 7837 | xmlRelaxNGElemPush(ctxt, exec); |
| 7838 | |
| 7839 | /* |
| 7840 | * Validate the attributes part of the content. |
| 7841 | */ |
| 7842 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 7843 | if (state == NULL) { |
| 7844 | ctxt->pstate = -1; |
| 7845 | return; |
| 7846 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7847 | oldstate = ctxt->state; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7848 | ctxt->state = state; |
| 7849 | if (define->attrs != NULL) { |
| 7850 | ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 7851 | if (ret != 0) { |
| 7852 | ctxt->pstate = -1; |
| 7853 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 7854 | } |
| 7855 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7856 | if (ctxt->state != NULL) { |
| 7857 | ctxt->state->seq = NULL; |
| 7858 | ret = xmlRelaxNGValidateElementEnd(ctxt); |
| 7859 | if (ret != 0) { |
| 7860 | ctxt->pstate = -1; |
| 7861 | } |
| 7862 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 7863 | } else if (ctxt->states != NULL) { |
| 7864 | int tmp = -1, i; |
| 7865 | |
| 7866 | oldflags = ctxt->flags; |
| 7867 | ctxt->flags |= FLAGS_IGNORABLE; |
| 7868 | |
| 7869 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 7870 | state = ctxt->states->tabState[i]; |
| 7871 | ctxt->state = state; |
| 7872 | ctxt->state->seq = NULL; |
| 7873 | |
| 7874 | if (xmlRelaxNGValidateElementEnd(ctxt) == 0) |
| 7875 | tmp = 0; |
| 7876 | xmlRelaxNGFreeValidState(ctxt, state); |
| 7877 | } |
| 7878 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 7879 | ctxt->states = NULL; |
| 7880 | if ((ret == 0) && (tmp == -1)) |
| 7881 | ctxt->pstate = -1; |
| 7882 | ctxt->flags = oldflags; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7883 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 7884 | if (ctxt->pstate == -1) { |
| 7885 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 7886 | xmlRelaxNGDumpValidError(ctxt); |
| 7887 | } |
| 7888 | } |
| 7889 | ctxt->state = oldstate; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7890 | } |
| 7891 | |
| 7892 | /** |
| 7893 | * xmlRelaxNGValidatePushElement: |
| 7894 | * @ctxt: the validation context |
| 7895 | * @doc: a document instance |
| 7896 | * @elem: an element instance |
| 7897 | * |
| 7898 | * Push a new element start on the RelaxNG validation stack. |
| 7899 | * |
| 7900 | * returns 1 if no validation problem was found or 0 if validating the |
| 7901 | * element requires a full node, and -1 in case of error. |
| 7902 | */ |
| 7903 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7904 | xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 7905 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7906 | xmlNodePtr elem) |
| 7907 | { |
| 7908 | int ret = 1; |
| 7909 | |
| 7910 | if ((ctxt == NULL) || (elem == NULL)) |
| 7911 | return (-1); |
| 7912 | |
| 7913 | #ifdef DEBUG_PROGRESSIVE |
| 7914 | xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name); |
| 7915 | #endif |
| 7916 | if (ctxt->elem == 0) { |
| 7917 | xmlRelaxNGPtr schema; |
| 7918 | xmlRelaxNGGrammarPtr grammar; |
| 7919 | xmlRegExecCtxtPtr exec; |
| 7920 | xmlRelaxNGDefinePtr define; |
| 7921 | |
| 7922 | schema = ctxt->schema; |
| 7923 | if (schema == NULL) { |
| 7924 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 7925 | return (-1); |
| 7926 | } |
| 7927 | grammar = schema->topgrammar; |
| 7928 | if ((grammar == NULL) || (grammar->start == NULL)) { |
| 7929 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
| 7930 | return (-1); |
| 7931 | } |
| 7932 | define = grammar->start; |
| 7933 | if (define->contModel == NULL) { |
| 7934 | ctxt->pdef = define; |
| 7935 | return (0); |
| 7936 | } |
| 7937 | exec = xmlRegNewExecCtxt(define->contModel, |
| 7938 | xmlRelaxNGValidateProgressiveCallback, |
| 7939 | ctxt); |
| 7940 | if (exec == NULL) { |
| 7941 | return (-1); |
| 7942 | } |
| 7943 | xmlRelaxNGElemPush(ctxt, exec); |
| 7944 | } |
| 7945 | ctxt->pnode = elem; |
| 7946 | ctxt->pstate = 0; |
| 7947 | if (elem->ns != NULL) { |
| 7948 | ret = |
| 7949 | xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href, |
| 7950 | ctxt); |
| 7951 | } else { |
| 7952 | ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt); |
| 7953 | } |
| 7954 | if (ret < 0) { |
| 7955 | VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name); |
| 7956 | } else { |
| 7957 | if (ctxt->pstate == 0) |
| 7958 | ret = 0; |
| 7959 | else if (ctxt->pstate < 0) |
| 7960 | ret = -1; |
| 7961 | else |
| 7962 | ret = 1; |
| 7963 | } |
| 7964 | #ifdef DEBUG_PROGRESSIVE |
| 7965 | if (ret < 0) |
| 7966 | xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n", |
| 7967 | elem->name); |
| 7968 | #endif |
| 7969 | return (ret); |
| 7970 | } |
| 7971 | |
| 7972 | /** |
| 7973 | * xmlRelaxNGValidatePushCData: |
| 7974 | * @ctxt: the RelaxNG validation context |
| 7975 | * @data: some character data read |
| 7976 | * @len: the lenght of the data |
| 7977 | * |
| 7978 | * check the CData parsed for validation in the current stack |
| 7979 | * |
| 7980 | * returns 1 if no validation problem was found or -1 otherwise |
| 7981 | */ |
| 7982 | int |
| 7983 | xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt, |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 7984 | const xmlChar * data, |
| 7985 | int len ATTRIBUTE_UNUSED) |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 7986 | { |
| 7987 | int ret = 1; |
| 7988 | |
| 7989 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL)) |
| 7990 | return (-1); |
| 7991 | |
| 7992 | #ifdef DEBUG_PROGRESSIVE |
| 7993 | xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len); |
| 7994 | #endif |
| 7995 | |
| 7996 | while (*data != 0) { |
| 7997 | if (!IS_BLANK(*data)) |
| 7998 | break; |
| 7999 | data++; |
| 8000 | } |
| 8001 | if (*data == 0) |
| 8002 | return(1); |
| 8003 | |
| 8004 | ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt); |
| 8005 | if (ret < 0) { |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8006 | VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO "); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8007 | #ifdef DEBUG_PROGRESSIVE |
| 8008 | xmlGenericError(xmlGenericErrorContext, "CDATA failed\n"); |
| 8009 | #endif |
| 8010 | |
| 8011 | return(-1); |
| 8012 | } |
| 8013 | return(1); |
| 8014 | } |
| 8015 | |
| 8016 | /** |
| 8017 | * xmlRelaxNGValidatePopElement: |
| 8018 | * @ctxt: the RelaxNG validation context |
| 8019 | * @doc: a document instance |
| 8020 | * @elem: an element instance |
| 8021 | * |
| 8022 | * Pop the element end from the RelaxNG validation stack. |
| 8023 | * |
| 8024 | * returns 1 if no validation problem was found or 0 otherwise |
| 8025 | */ |
| 8026 | int |
| 8027 | xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8028 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
| 8029 | xmlNodePtr elem) { |
| 8030 | int ret; |
| 8031 | xmlRegExecCtxtPtr exec; |
| 8032 | |
| 8033 | if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) return(-1); |
| 8034 | #ifdef DEBUG_PROGRESSIVE |
| 8035 | xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name); |
| 8036 | #endif |
| 8037 | /* |
| 8038 | * verify that we reached a terminal state of the content model. |
| 8039 | */ |
| 8040 | exec = xmlRelaxNGElemPop(ctxt); |
| 8041 | ret = xmlRegExecPushString(exec, NULL, NULL); |
| 8042 | if (ret == 0) { |
| 8043 | /* |
| 8044 | * TODO: get some of the names needed to exit the current state of exec |
| 8045 | */ |
| 8046 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST ""); |
| 8047 | ret = -1; |
| 8048 | } else if (ret < 0) { |
| 8049 | ret = -1; |
| 8050 | } else { |
| 8051 | ret = 1; |
| 8052 | } |
| 8053 | xmlRegFreeExecCtxt(exec); |
| 8054 | #ifdef DEBUG_PROGRESSIVE |
| 8055 | if (ret < 0) |
| 8056 | xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n", |
| 8057 | elem->name); |
| 8058 | #endif |
| 8059 | return(ret); |
| 8060 | } |
| 8061 | |
| 8062 | /** |
| 8063 | * xmlRelaxNGValidateFullElement: |
| 8064 | * @ctxt: the validation context |
| 8065 | * @doc: a document instance |
| 8066 | * @elem: an element instance |
| 8067 | * |
| 8068 | * Validate a full subtree when xmlRelaxNGValidatePushElement() returned |
| 8069 | * 0 and the content of the node has been expanded. |
| 8070 | * |
| 8071 | * returns 1 if no validation problem was found or -1 in case of error. |
| 8072 | */ |
| 8073 | int |
Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 8074 | xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt, |
| 8075 | xmlDocPtr doc ATTRIBUTE_UNUSED, |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 8076 | xmlNodePtr elem) { |
| 8077 | int ret; |
| 8078 | xmlRelaxNGValidStatePtr state; |
| 8079 | |
| 8080 | if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) return(-1); |
| 8081 | #ifdef DEBUG_PROGRESSIVE |
| 8082 | xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name); |
| 8083 | #endif |
| 8084 | state = xmlRelaxNGNewValidState(ctxt, elem->parent); |
| 8085 | if (state == NULL) { |
| 8086 | return(-1); |
| 8087 | } |
| 8088 | state->seq = elem; |
| 8089 | ctxt->state = state; |
| 8090 | ctxt->errNo = XML_RELAXNG_OK; |
| 8091 | ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef); |
| 8092 | if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) ret = -1; |
| 8093 | else ret = 1; |
| 8094 | xmlRelaxNGFreeValidState(ctxt, state); |
| 8095 | ctxt->state = NULL; |
| 8096 | #ifdef DEBUG_PROGRESSIVE |
| 8097 | if (ret < 0) |
| 8098 | xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n", |
| 8099 | elem->name); |
| 8100 | #endif |
| 8101 | return(ret); |
| 8102 | } |
| 8103 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 8104 | /************************************************************************ |
| 8105 | * * |
| 8106 | * Generic interpreted validation implementation * |
| 8107 | * * |
| 8108 | ************************************************************************/ |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8109 | static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8110 | xmlRelaxNGDefinePtr define); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8111 | |
| 8112 | /** |
| 8113 | * xmlRelaxNGSkipIgnored: |
| 8114 | * @ctxt: a schema validation context |
| 8115 | * @node: the top node. |
| 8116 | * |
| 8117 | * Skip ignorable nodes in that context |
| 8118 | * |
| 8119 | * Returns the new sibling or NULL in case of error. |
| 8120 | */ |
| 8121 | static xmlNodePtr |
| 8122 | xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED, |
| 8123 | xmlNodePtr node) { |
| 8124 | /* |
| 8125 | * TODO complete and handle entities |
| 8126 | */ |
| 8127 | while ((node != NULL) && |
| 8128 | ((node->type == XML_COMMENT_NODE) || |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8129 | (node->type == XML_PI_NODE) || |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 8130 | (((node->type == XML_TEXT_NODE) || |
| 8131 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8132 | ((ctxt->flags & FLAGS_MIXED_CONTENT) || |
| 8133 | (IS_BLANK_NODE(node)))))) { |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8134 | node = node->next; |
| 8135 | } |
| 8136 | return(node); |
| 8137 | } |
| 8138 | |
| 8139 | /** |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8140 | * xmlRelaxNGNormalize: |
| 8141 | * @ctxt: a schema validation context |
| 8142 | * @str: the string to normalize |
| 8143 | * |
| 8144 | * Implements the normalizeWhiteSpace( s ) function from |
| 8145 | * section 6.2.9 of the spec |
| 8146 | * |
| 8147 | * Returns the new string or NULL in case of error. |
| 8148 | */ |
| 8149 | static xmlChar * |
| 8150 | xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) { |
| 8151 | xmlChar *ret, *p; |
| 8152 | const xmlChar *tmp; |
| 8153 | int len; |
| 8154 | |
| 8155 | if (str == NULL) |
| 8156 | return(NULL); |
| 8157 | tmp = str; |
| 8158 | while (*tmp != 0) tmp++; |
| 8159 | len = tmp - str; |
| 8160 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8161 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8162 | if (ret == NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8163 | if (ctxt != NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8164 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8165 | } else { |
| 8166 | xmlGenericError(xmlGenericErrorContext, |
| 8167 | "xmlRelaxNGNormalize: out of memory\n"); |
| 8168 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8169 | return(NULL); |
| 8170 | } |
| 8171 | p = ret; |
| 8172 | while (IS_BLANK(*str)) str++; |
| 8173 | while (*str != 0) { |
| 8174 | if (IS_BLANK(*str)) { |
| 8175 | while (IS_BLANK(*str)) str++; |
| 8176 | if (*str == 0) |
| 8177 | break; |
| 8178 | *p++ = ' '; |
| 8179 | } else |
| 8180 | *p++ = *str++; |
| 8181 | } |
| 8182 | *p = 0; |
| 8183 | return(ret); |
| 8184 | } |
| 8185 | |
| 8186 | /** |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8187 | * xmlRelaxNGValidateDatatype: |
| 8188 | * @ctxt: a Relax-NG validation context |
| 8189 | * @value: the string value |
| 8190 | * @type: the datatype definition |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8191 | * @node: the node |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8192 | * |
| 8193 | * Validate the given value against the dataype |
| 8194 | * |
| 8195 | * Returns 0 if the validation succeeded or an error code. |
| 8196 | */ |
| 8197 | static int |
| 8198 | xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value, |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8199 | xmlRelaxNGDefinePtr define, xmlNodePtr node) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8200 | int ret, tmp; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8201 | xmlRelaxNGTypeLibraryPtr lib; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8202 | void *result = NULL; |
| 8203 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8204 | |
| 8205 | if ((define == NULL) || (define->data == NULL)) { |
| 8206 | return(-1); |
| 8207 | } |
| 8208 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8209 | if (lib->check != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8210 | if ((define->attrs != NULL) && |
| 8211 | (define->attrs->type == XML_RELAXNG_PARAM)) { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8212 | ret = lib->check(lib->data, define->name, value, &result, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8213 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8214 | ret = lib->check(lib->data, define->name, value, NULL, node); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8215 | } |
| 8216 | } else |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8217 | ret = -1; |
| 8218 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8219 | VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8220 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8221 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8222 | return(-1); |
| 8223 | } else if (ret == 1) { |
| 8224 | ret = 0; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8225 | } else if (ret == 2) { |
| 8226 | VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8227 | } else { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8228 | VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8229 | ret = -1; |
| 8230 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8231 | cur = define->attrs; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8232 | while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) { |
| 8233 | if (lib->facet != NULL) { |
| 8234 | tmp = lib->facet(lib->data, define->name, cur->name, |
| 8235 | cur->value, value, result); |
| 8236 | if (tmp != 0) |
| 8237 | ret = -1; |
| 8238 | } |
| 8239 | cur = cur->next; |
| 8240 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8241 | if ((ret == 0) && (define->content != NULL)) { |
| 8242 | const xmlChar *oldvalue, *oldendvalue; |
| 8243 | |
| 8244 | oldvalue = ctxt->state->value; |
| 8245 | oldendvalue = ctxt->state->endvalue; |
| 8246 | ctxt->state->value = (xmlChar *) value; |
| 8247 | ctxt->state->endvalue = NULL; |
| 8248 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8249 | ctxt->state->value = (xmlChar *) oldvalue; |
| 8250 | ctxt->state->endvalue = (xmlChar *) oldendvalue; |
| 8251 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 8252 | if ((result != NULL) && (lib != NULL) && (lib->freef != NULL)) |
| 8253 | lib->freef(lib->data, result); |
Daniel Veillard | dd1655c | 2003-01-25 18:01:32 +0000 | [diff] [blame] | 8254 | return(ret); |
| 8255 | } |
| 8256 | |
| 8257 | /** |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8258 | * xmlRelaxNGNextValue: |
| 8259 | * @ctxt: a Relax-NG validation context |
| 8260 | * |
| 8261 | * Skip to the next value when validating within a list |
| 8262 | * |
| 8263 | * Returns 0 if the operation succeeded or an error code. |
| 8264 | */ |
| 8265 | static int |
| 8266 | xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) { |
| 8267 | xmlChar *cur; |
| 8268 | |
| 8269 | cur = ctxt->state->value; |
| 8270 | if ((cur == NULL) || (ctxt->state->endvalue == NULL)) { |
| 8271 | ctxt->state->value = NULL; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 8272 | ctxt->state->endvalue = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8273 | return(0); |
| 8274 | } |
| 8275 | while (*cur != 0) cur++; |
| 8276 | while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++; |
| 8277 | if (cur == ctxt->state->endvalue) |
| 8278 | ctxt->state->value = NULL; |
| 8279 | else |
| 8280 | ctxt->state->value = cur; |
| 8281 | return(0); |
| 8282 | } |
| 8283 | |
| 8284 | /** |
| 8285 | * xmlRelaxNGValidateValueList: |
| 8286 | * @ctxt: a Relax-NG validation context |
| 8287 | * @defines: the list of definitions to verify |
| 8288 | * |
| 8289 | * Validate the given set of definitions for the current value |
| 8290 | * |
| 8291 | * Returns 0 if the validation succeeded or an error code. |
| 8292 | */ |
| 8293 | static int |
| 8294 | xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8295 | xmlRelaxNGDefinePtr defines) { |
| 8296 | int ret = 0; |
| 8297 | |
| 8298 | while (defines != NULL) { |
| 8299 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8300 | if (ret != 0) |
| 8301 | break; |
| 8302 | defines = defines->next; |
| 8303 | } |
| 8304 | return(ret); |
| 8305 | } |
| 8306 | |
| 8307 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8308 | * xmlRelaxNGValidateValue: |
| 8309 | * @ctxt: a Relax-NG validation context |
| 8310 | * @define: the definition to verify |
| 8311 | * |
| 8312 | * Validate the given definition for the current value |
| 8313 | * |
| 8314 | * Returns 0 if the validation succeeded or an error code. |
| 8315 | */ |
| 8316 | static int |
| 8317 | xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt, |
| 8318 | xmlRelaxNGDefinePtr define) { |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8319 | int ret = 0, oldflags; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8320 | xmlChar *value; |
| 8321 | |
| 8322 | value = ctxt->state->value; |
| 8323 | switch (define->type) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8324 | case XML_RELAXNG_EMPTY: { |
| 8325 | if ((value != NULL) && (value[0] != 0)) { |
| 8326 | int idx = 0; |
| 8327 | |
| 8328 | while (IS_BLANK(value[idx])) |
| 8329 | idx++; |
| 8330 | if (value[idx] != 0) |
| 8331 | ret = -1; |
| 8332 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8333 | break; |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8334 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8335 | case XML_RELAXNG_TEXT: |
| 8336 | break; |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8337 | case XML_RELAXNG_VALUE: { |
| 8338 | if (!xmlStrEqual(value, define->value)) { |
| 8339 | if (define->name != NULL) { |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8340 | xmlRelaxNGTypeLibraryPtr lib; |
| 8341 | |
| 8342 | lib = (xmlRelaxNGTypeLibraryPtr) define->data; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 8343 | if ((lib != NULL) && (lib->comp != NULL)) { |
| 8344 | ret = lib->comp(lib->data, define->name, |
| 8345 | define->value, define->node, |
| 8346 | (void *) define->attrs, |
| 8347 | value, ctxt->state->node); |
| 8348 | } else |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8349 | ret = -1; |
| 8350 | if (ret < 0) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8351 | VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, define->name); |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8352 | return(-1); |
| 8353 | } else if (ret == 1) { |
| 8354 | ret = 0; |
| 8355 | } else { |
| 8356 | ret = -1; |
| 8357 | } |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8358 | } else { |
| 8359 | xmlChar *nval, *nvalue; |
| 8360 | |
| 8361 | /* |
| 8362 | * TODO: trivial optimizations are possible by |
| 8363 | * computing at compile-time |
| 8364 | */ |
| 8365 | nval = xmlRelaxNGNormalize(ctxt, define->value); |
| 8366 | nvalue = xmlRelaxNGNormalize(ctxt, value); |
| 8367 | |
Daniel Veillard | ea3f398 | 2003-01-26 19:45:18 +0000 | [diff] [blame] | 8368 | if ((nval == NULL) || (nvalue == NULL) || |
| 8369 | (!xmlStrEqual(nval, nvalue))) |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8370 | ret = -1; |
| 8371 | if (nval != NULL) |
| 8372 | xmlFree(nval); |
| 8373 | if (nvalue != NULL) |
| 8374 | xmlFree(nvalue); |
| 8375 | } |
| 8376 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8377 | if (ret == 0) |
| 8378 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | edc9192 | 2003-01-26 00:52:04 +0000 | [diff] [blame] | 8379 | break; |
| 8380 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8381 | case XML_RELAXNG_DATATYPE: { |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8382 | ret = xmlRelaxNGValidateDatatype(ctxt, value, define, |
| 8383 | ctxt->state->seq); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8384 | if (ret == 0) |
| 8385 | xmlRelaxNGNextValue(ctxt); |
| 8386 | |
| 8387 | break; |
| 8388 | } |
| 8389 | case XML_RELAXNG_CHOICE: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8390 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8391 | xmlChar *oldvalue; |
| 8392 | |
| 8393 | oldflags = ctxt->flags; |
| 8394 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8395 | |
| 8396 | oldvalue = ctxt->state->value; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8397 | while (list != NULL) { |
| 8398 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8399 | if (ret == 0) { |
| 8400 | break; |
| 8401 | } |
| 8402 | ctxt->state->value = oldvalue; |
| 8403 | list = list->next; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8404 | } |
| 8405 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8406 | if (ret != 0) { |
| 8407 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8408 | xmlRelaxNGDumpValidError(ctxt); |
| 8409 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8410 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8411 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8412 | if (ret == 0) |
| 8413 | xmlRelaxNGNextValue(ctxt); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8414 | break; |
| 8415 | } |
| 8416 | case XML_RELAXNG_LIST: { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8417 | xmlRelaxNGDefinePtr list = define->content; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8418 | xmlChar *oldvalue, *oldend, *val, *cur; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8419 | #ifdef DEBUG_LIST |
| 8420 | int nb_values = 0; |
| 8421 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8422 | |
| 8423 | oldvalue = ctxt->state->value; |
| 8424 | oldend = ctxt->state->endvalue; |
| 8425 | |
| 8426 | val = xmlStrdup(oldvalue); |
| 8427 | if (val == NULL) { |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8428 | val = xmlStrdup(BAD_CAST ""); |
| 8429 | } |
| 8430 | if (val == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8431 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8432 | return(-1); |
| 8433 | } |
| 8434 | cur = val; |
| 8435 | while (*cur != 0) { |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8436 | if (IS_BLANK(*cur)) { |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8437 | *cur = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8438 | cur++; |
| 8439 | #ifdef DEBUG_LIST |
| 8440 | nb_values++; |
| 8441 | #endif |
| 8442 | while (IS_BLANK(*cur)) |
| 8443 | *cur++ = 0; |
| 8444 | } else |
| 8445 | cur++; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8446 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8447 | #ifdef DEBUG_LIST |
| 8448 | xmlGenericError(xmlGenericErrorContext, |
| 8449 | "list value: '%s' found %d items\n", oldvalue, nb_values); |
| 8450 | nb_values = 0; |
| 8451 | #endif |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8452 | ctxt->state->endvalue = cur; |
| 8453 | cur = val; |
| 8454 | while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8455 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8456 | ctxt->state->value = cur; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8457 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8458 | while (list != NULL) { |
| 8459 | if (ctxt->state->value == ctxt->state->endvalue) |
| 8460 | ctxt->state->value = NULL; |
| 8461 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8462 | if (ret != 0) { |
| 8463 | #ifdef DEBUG_LIST |
| 8464 | xmlGenericError(xmlGenericErrorContext, |
| 8465 | "Failed to validate value: '%s' with %d rule\n", |
| 8466 | ctxt->state->value, nb_values); |
| 8467 | #endif |
| 8468 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 8469 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8470 | #ifdef DEBUG_LIST |
| 8471 | nb_values++; |
| 8472 | #endif |
| 8473 | list = list->next; |
| 8474 | } |
| 8475 | |
| 8476 | if ((ret == 0) && (ctxt->state->value != NULL) && |
| 8477 | (ctxt->state->value != ctxt->state->endvalue)) { |
| 8478 | VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, ctxt->state->value); |
| 8479 | ret = -1; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8480 | } |
| 8481 | xmlFree(val); |
| 8482 | ctxt->state->value = oldvalue; |
| 8483 | ctxt->state->endvalue = oldend; |
| 8484 | break; |
| 8485 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8486 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8487 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8488 | if (ret != 0) { |
| 8489 | break; |
| 8490 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8491 | /* no break on purpose */ |
| 8492 | case XML_RELAXNG_ZEROORMORE: { |
| 8493 | xmlChar *cur, *temp; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8494 | |
| 8495 | oldflags = ctxt->flags; |
| 8496 | ctxt->flags |= FLAGS_IGNORABLE; |
| 8497 | cur = ctxt->state->value; |
| 8498 | temp = NULL; |
| 8499 | while ((cur != NULL) && (cur != ctxt->state->endvalue) && |
| 8500 | (temp != cur)) { |
| 8501 | temp = cur; |
| 8502 | ret = xmlRelaxNGValidateValueList(ctxt, define->content); |
| 8503 | if (ret != 0) { |
| 8504 | ctxt->state->value = temp; |
| 8505 | ret = 0; |
| 8506 | break; |
| 8507 | } |
| 8508 | cur = ctxt->state->value; |
| 8509 | } |
| 8510 | ctxt->flags = oldflags; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8511 | if (ret != 0) { |
| 8512 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 8513 | xmlRelaxNGDumpValidError(ctxt); |
| 8514 | } else { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 8515 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 8516 | } |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 8517 | break; |
| 8518 | } |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 8519 | case XML_RELAXNG_EXCEPT: { |
| 8520 | xmlRelaxNGDefinePtr list; |
| 8521 | |
| 8522 | list = define->content; |
| 8523 | while (list != NULL) { |
| 8524 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8525 | if (ret == 0) { |
| 8526 | ret = -1; |
| 8527 | break; |
| 8528 | } else |
| 8529 | ret = 0; |
| 8530 | list = list->next; |
| 8531 | } |
| 8532 | break; |
| 8533 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8534 | case XML_RELAXNG_DEF: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8535 | case XML_RELAXNG_GROUP: { |
| 8536 | xmlRelaxNGDefinePtr list; |
| 8537 | |
| 8538 | list = define->content; |
| 8539 | while (list != NULL) { |
| 8540 | ret = xmlRelaxNGValidateValue(ctxt, list); |
| 8541 | if (ret != 0) { |
| 8542 | ret = -1; |
| 8543 | break; |
| 8544 | } else |
| 8545 | ret = 0; |
| 8546 | list = list->next; |
| 8547 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 8548 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8549 | } |
Daniel Veillard | 463a547 | 2003-02-27 21:30:32 +0000 | [diff] [blame] | 8550 | case XML_RELAXNG_REF: |
| 8551 | case XML_RELAXNG_PARENTREF: |
| 8552 | ret = xmlRelaxNGValidateValue(ctxt, define->content); |
| 8553 | break; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8554 | default: |
| 8555 | TODO |
| 8556 | ret = -1; |
| 8557 | } |
| 8558 | return(ret); |
| 8559 | } |
| 8560 | |
| 8561 | /** |
| 8562 | * xmlRelaxNGValidateValueContent: |
| 8563 | * @ctxt: a Relax-NG validation context |
| 8564 | * @defines: the list of definitions to verify |
| 8565 | * |
| 8566 | * Validate the given definitions for the current value |
| 8567 | * |
| 8568 | * Returns 0 if the validation succeeded or an error code. |
| 8569 | */ |
| 8570 | static int |
| 8571 | xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt, |
| 8572 | xmlRelaxNGDefinePtr defines) { |
| 8573 | int ret = 0; |
| 8574 | |
| 8575 | while (defines != NULL) { |
| 8576 | ret = xmlRelaxNGValidateValue(ctxt, defines); |
| 8577 | if (ret != 0) |
| 8578 | break; |
| 8579 | defines = defines->next; |
| 8580 | } |
| 8581 | return(ret); |
| 8582 | } |
| 8583 | |
| 8584 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8585 | * xmlRelaxNGAttributeMatch: |
| 8586 | * @ctxt: a Relax-NG validation context |
| 8587 | * @define: the definition to check |
| 8588 | * @prop: the attribute |
| 8589 | * |
| 8590 | * Check if the attribute matches the definition nameClass |
| 8591 | * |
| 8592 | * Returns 1 if the attribute matches, 0 if no, or -1 in case of error |
| 8593 | */ |
| 8594 | static int |
| 8595 | xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 8596 | xmlRelaxNGDefinePtr define, |
| 8597 | xmlAttrPtr prop) { |
| 8598 | int ret; |
| 8599 | |
| 8600 | if (define->name != NULL) { |
| 8601 | if (!xmlStrEqual(define->name, prop->name)) |
| 8602 | return(0); |
| 8603 | } |
| 8604 | if (define->ns != NULL) { |
| 8605 | if (define->ns[0] == 0) { |
| 8606 | if (prop->ns != NULL) |
| 8607 | return(0); |
| 8608 | } else { |
| 8609 | if ((prop->ns == NULL) || |
| 8610 | (!xmlStrEqual(define->ns, prop->ns->href))) |
| 8611 | return(0); |
| 8612 | } |
| 8613 | } |
| 8614 | if (define->nameClass == NULL) |
| 8615 | return(1); |
| 8616 | define = define->nameClass; |
| 8617 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 8618 | xmlRelaxNGDefinePtr list; |
| 8619 | |
| 8620 | list = define->content; |
| 8621 | while (list != NULL) { |
| 8622 | ret = xmlRelaxNGAttributeMatch(ctxt, list, prop); |
| 8623 | if (ret == 1) |
| 8624 | return(0); |
| 8625 | if (ret < 0) |
| 8626 | return(ret); |
| 8627 | list = list->next; |
| 8628 | } |
| 8629 | } else { |
| 8630 | TODO |
| 8631 | } |
| 8632 | return(1); |
| 8633 | } |
| 8634 | |
| 8635 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8636 | * xmlRelaxNGValidateAttribute: |
| 8637 | * @ctxt: a Relax-NG validation context |
| 8638 | * @define: the definition to verify |
| 8639 | * |
| 8640 | * Validate the given attribute definition for that node |
| 8641 | * |
| 8642 | * Returns 0 if the validation succeeded or an error code. |
| 8643 | */ |
| 8644 | static int |
| 8645 | xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt, |
| 8646 | xmlRelaxNGDefinePtr define) { |
| 8647 | int ret = 0, i; |
| 8648 | xmlChar *value, *oldvalue; |
| 8649 | xmlAttrPtr prop = NULL, tmp; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8650 | xmlNodePtr oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8651 | |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8652 | if (ctxt->state->nbAttrLeft <= 0) |
| 8653 | return(-1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8654 | if (define->name != NULL) { |
| 8655 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 8656 | tmp = ctxt->state->attrs[i]; |
| 8657 | if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) { |
| 8658 | if ((((define->ns == NULL) || (define->ns[0] == 0)) && |
| 8659 | (tmp->ns == NULL)) || |
| 8660 | ((tmp->ns != NULL) && |
| 8661 | (xmlStrEqual(define->ns, tmp->ns->href)))) { |
| 8662 | prop = tmp; |
| 8663 | break; |
| 8664 | } |
| 8665 | } |
| 8666 | } |
| 8667 | if (prop != NULL) { |
| 8668 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8669 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8670 | oldseq = ctxt->state->seq; |
| 8671 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8672 | ctxt->state->value = value; |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8673 | ctxt->state->endvalue = NULL; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8674 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8675 | if (ctxt->state->value != NULL) |
| 8676 | value = ctxt->state->value; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8677 | if (value != NULL) |
| 8678 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8679 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8680 | ctxt->state->seq = oldseq; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8681 | if (ret == 0) { |
| 8682 | /* |
| 8683 | * flag the attribute as processed |
| 8684 | */ |
| 8685 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8686 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8687 | } |
| 8688 | } else { |
| 8689 | ret = -1; |
| 8690 | } |
| 8691 | #ifdef DEBUG |
| 8692 | xmlGenericError(xmlGenericErrorContext, |
| 8693 | "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret); |
| 8694 | #endif |
| 8695 | } else { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8696 | for (i = 0;i < ctxt->state->nbAttrs;i++) { |
| 8697 | tmp = ctxt->state->attrs[i]; |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 8698 | if ((tmp != NULL) && |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8699 | (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) { |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8700 | prop = tmp; |
| 8701 | break; |
| 8702 | } |
| 8703 | } |
| 8704 | if (prop != NULL) { |
| 8705 | value = xmlNodeListGetString(prop->doc, prop->children, 1); |
| 8706 | oldvalue = ctxt->state->value; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8707 | oldseq = ctxt->state->seq; |
| 8708 | ctxt->state->seq = (xmlNodePtr) prop; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8709 | ctxt->state->value = value; |
| 8710 | ret = xmlRelaxNGValidateValueContent(ctxt, define->content); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8711 | if (ctxt->state->value != NULL) |
| 8712 | value = ctxt->state->value; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8713 | if (value != NULL) |
| 8714 | xmlFree(value); |
Daniel Veillard | 231d791 | 2003-02-09 14:22:17 +0000 | [diff] [blame] | 8715 | ctxt->state->value = oldvalue; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 8716 | ctxt->state->seq = oldseq; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8717 | if (ret == 0) { |
| 8718 | /* |
| 8719 | * flag the attribute as processed |
| 8720 | */ |
| 8721 | ctxt->state->attrs[i] = NULL; |
Daniel Veillard | 1ed7f36 | 2003-02-03 10:57:45 +0000 | [diff] [blame] | 8722 | ctxt->state->nbAttrLeft--; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8723 | } |
| 8724 | } else { |
| 8725 | ret = -1; |
| 8726 | } |
| 8727 | #ifdef DEBUG |
Daniel Veillard | 144fae1 | 2003-02-03 13:17:57 +0000 | [diff] [blame] | 8728 | if (define->ns != NULL) { |
| 8729 | xmlGenericError(xmlGenericErrorContext, |
| 8730 | "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n", |
| 8731 | define->ns, ret); |
| 8732 | } else { |
| 8733 | xmlGenericError(xmlGenericErrorContext, |
| 8734 | "xmlRelaxNGValidateAttribute(anyName): %d\n", |
| 8735 | ret); |
| 8736 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 8737 | #endif |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 8738 | } |
| 8739 | |
| 8740 | return(ret); |
| 8741 | } |
| 8742 | |
| 8743 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8744 | * xmlRelaxNGValidateAttributeList: |
| 8745 | * @ctxt: a Relax-NG validation context |
| 8746 | * @define: the list of definition to verify |
| 8747 | * |
| 8748 | * Validate the given node against the list of attribute definitions |
| 8749 | * |
| 8750 | * Returns 0 if the validation succeeded or an error code. |
| 8751 | */ |
| 8752 | static int |
| 8753 | xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt, |
| 8754 | xmlRelaxNGDefinePtr defines) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8755 | int ret = 0, res; |
| 8756 | int needmore = 0; |
| 8757 | xmlRelaxNGDefinePtr cur; |
| 8758 | |
| 8759 | cur = defines; |
| 8760 | while (cur != NULL) { |
| 8761 | if (cur->type == XML_RELAXNG_ATTRIBUTE) { |
| 8762 | if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0) |
| 8763 | ret = -1; |
| 8764 | } else |
| 8765 | needmore = 1; |
| 8766 | cur = cur->next; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8767 | } |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 8768 | if (!needmore) |
| 8769 | return(ret); |
| 8770 | cur = defines; |
| 8771 | while (cur != NULL) { |
| 8772 | if (cur->type != XML_RELAXNG_ATTRIBUTE) { |
| 8773 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 8774 | res = xmlRelaxNGValidateDefinition(ctxt, cur); |
| 8775 | if (res < 0) |
| 8776 | ret = -1; |
| 8777 | } else { |
| 8778 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 8779 | return(-1); |
| 8780 | } |
| 8781 | if (res == -1) /* continues on -2 */ |
| 8782 | break; |
| 8783 | } |
| 8784 | cur = cur->next; |
| 8785 | } |
| 8786 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8787 | return(ret); |
| 8788 | } |
| 8789 | |
| 8790 | /** |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8791 | * xmlRelaxNGNodeMatchesList: |
| 8792 | * @node: the node |
| 8793 | * @list: a NULL terminated array of definitions |
| 8794 | * |
| 8795 | * Check if a node can be matched by one of the definitions |
| 8796 | * |
| 8797 | * Returns 1 if matches 0 otherwise |
| 8798 | */ |
| 8799 | static int |
| 8800 | xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) { |
| 8801 | xmlRelaxNGDefinePtr cur; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8802 | int i = 0, tmp; |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8803 | |
| 8804 | if ((node == NULL) || (list == NULL)) |
| 8805 | return(0); |
| 8806 | |
| 8807 | cur = list[i++]; |
| 8808 | while (cur != NULL) { |
| 8809 | if ((node->type == XML_ELEMENT_NODE) && |
| 8810 | (cur->type == XML_RELAXNG_ELEMENT)) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 8811 | tmp = xmlRelaxNGElementMatch(NULL, cur, node); |
| 8812 | if (tmp == 1) |
| 8813 | return(1); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 8814 | } else if (((node->type == XML_TEXT_NODE) || |
| 8815 | (node->type == XML_CDATA_SECTION_NODE)) && |
Daniel Veillard | 76fc5ed | 2003-01-28 20:58:15 +0000 | [diff] [blame] | 8816 | (cur->type == XML_RELAXNG_TEXT)) { |
| 8817 | return(1); |
| 8818 | } |
| 8819 | cur = list[i++]; |
| 8820 | } |
| 8821 | return(0); |
| 8822 | } |
| 8823 | |
| 8824 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8825 | * xmlRelaxNGValidateInterleave: |
| 8826 | * @ctxt: a Relax-NG validation context |
| 8827 | * @define: the definition to verify |
| 8828 | * |
| 8829 | * Validate an interleave definition for a node. |
| 8830 | * |
| 8831 | * Returns 0 if the validation succeeded or an error code. |
| 8832 | */ |
| 8833 | static int |
| 8834 | xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt, |
| 8835 | xmlRelaxNGDefinePtr define) { |
| 8836 | int ret = 0, i, nbgroups, left; |
| 8837 | int errNr = ctxt->errNr; |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8838 | int oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8839 | |
| 8840 | xmlRelaxNGValidStatePtr oldstate; |
| 8841 | xmlRelaxNGPartitionPtr partitions; |
| 8842 | xmlRelaxNGInterleaveGroupPtr group = NULL; |
| 8843 | xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem; |
| 8844 | xmlNodePtr *list = NULL, *lasts = NULL; |
| 8845 | |
| 8846 | if (define->data != NULL) { |
| 8847 | partitions = (xmlRelaxNGPartitionPtr) define->data; |
| 8848 | nbgroups = partitions->nbgroups; |
| 8849 | left = nbgroups; |
| 8850 | } else { |
| 8851 | VALID_ERR(XML_RELAXNG_ERR_INTERNODATA); |
| 8852 | return(-1); |
| 8853 | } |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8854 | /* |
| 8855 | * Optimizations for MIXED |
| 8856 | */ |
| 8857 | oldflags = ctxt->flags; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 8858 | if (define->dflags & IS_MIXED) { |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 8859 | ctxt->flags |= FLAGS_MIXED_CONTENT; |
| 8860 | if (nbgroups == 2) { |
| 8861 | /* |
| 8862 | * this is a pure <mixed> case |
| 8863 | */ |
| 8864 | if (ctxt->state != NULL) |
| 8865 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8866 | ctxt->state->seq); |
| 8867 | if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT) |
| 8868 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8869 | partitions->groups[1]->rule); |
| 8870 | else |
| 8871 | ret = xmlRelaxNGValidateDefinition(ctxt, |
| 8872 | partitions->groups[0]->rule); |
| 8873 | if (ret == 0) { |
| 8874 | if (ctxt->state != NULL) |
| 8875 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, |
| 8876 | ctxt->state->seq); |
| 8877 | } |
| 8878 | ctxt->flags = oldflags; |
| 8879 | return(ret); |
| 8880 | } |
| 8881 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8882 | |
| 8883 | /* |
| 8884 | * Build arrays to store the first and last node of the chain |
| 8885 | * pertaining to each group |
| 8886 | */ |
| 8887 | list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 8888 | if (list == NULL) { |
| 8889 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8890 | return(-1); |
| 8891 | } |
| 8892 | memset(list, 0, nbgroups * sizeof(xmlNodePtr)); |
| 8893 | lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr)); |
| 8894 | if (lasts == NULL) { |
| 8895 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 8896 | return(-1); |
| 8897 | } |
| 8898 | memset(lasts, 0, nbgroups * sizeof(xmlNodePtr)); |
| 8899 | |
| 8900 | /* |
| 8901 | * Walk the sequence of children finding the right group and |
| 8902 | * sorting them in sequences. |
| 8903 | */ |
| 8904 | cur = ctxt->state->seq; |
| 8905 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 8906 | start = cur; |
| 8907 | while (cur != NULL) { |
| 8908 | ctxt->state->seq = cur; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 8909 | if ((partitions->triage != NULL) && |
| 8910 | (partitions->flags & IS_DETERMINIST)) { |
| 8911 | void *tmp = NULL; |
| 8912 | |
| 8913 | if ((cur->type == XML_TEXT_NODE) || |
| 8914 | (cur->type == XML_CDATA_SECTION_NODE)) { |
| 8915 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text", |
| 8916 | NULL); |
| 8917 | } else if (cur->type == XML_ELEMENT_NODE) { |
| 8918 | if (cur->ns != NULL) { |
| 8919 | tmp = xmlHashLookup2(partitions->triage, cur->name, |
| 8920 | cur->ns->href); |
| 8921 | if (tmp == NULL) |
| 8922 | tmp = xmlHashLookup2(partitions->triage, |
| 8923 | BAD_CAST "#any", cur->ns->href); |
| 8924 | } else |
| 8925 | tmp = xmlHashLookup2(partitions->triage, cur->name, NULL); |
| 8926 | if (tmp == NULL) |
| 8927 | tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#any", |
| 8928 | NULL); |
| 8929 | } |
| 8930 | |
| 8931 | if (tmp == NULL) { |
| 8932 | i = nbgroups; |
| 8933 | } else { |
| 8934 | i = ((long) tmp) - 1; |
| 8935 | if (partitions->flags & IS_NEEDCHECK) { |
| 8936 | group = partitions->groups[i]; |
| 8937 | if (!xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 8938 | i = nbgroups; |
| 8939 | } |
| 8940 | } |
| 8941 | } else { |
| 8942 | for (i = 0;i < nbgroups;i++) { |
| 8943 | group = partitions->groups[i]; |
| 8944 | if (group == NULL) |
| 8945 | continue; |
| 8946 | if (xmlRelaxNGNodeMatchesList(cur, group->defs)) |
| 8947 | break; |
| 8948 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8949 | } |
| 8950 | /* |
| 8951 | * We break as soon as an element not matched is found |
| 8952 | */ |
| 8953 | if (i >= nbgroups) { |
| 8954 | break; |
| 8955 | } |
| 8956 | if (lasts[i] != NULL) { |
| 8957 | lasts[i]->next = cur; |
| 8958 | lasts[i] = cur; |
| 8959 | } else { |
| 8960 | list[i] = cur; |
| 8961 | lasts[i] = cur; |
| 8962 | } |
| 8963 | if (cur->next != NULL) |
| 8964 | lastchg = cur->next; |
| 8965 | else |
| 8966 | lastchg = cur; |
| 8967 | cur = xmlRelaxNGSkipIgnored(ctxt, cur->next); |
| 8968 | } |
| 8969 | if (ret != 0) { |
| 8970 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 8971 | ret = -1; |
| 8972 | goto done; |
| 8973 | } |
| 8974 | lastelem = cur; |
| 8975 | oldstate = ctxt->state; |
| 8976 | for (i = 0;i < nbgroups;i++) { |
| 8977 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate); |
| 8978 | group = partitions->groups[i]; |
| 8979 | if (lasts[i] != NULL) { |
| 8980 | last = lasts[i]->next; |
| 8981 | lasts[i]->next = NULL; |
| 8982 | } |
| 8983 | ctxt->state->seq = list[i]; |
| 8984 | ret = xmlRelaxNGValidateDefinition(ctxt, group->rule); |
| 8985 | if (ret != 0) |
| 8986 | break; |
| 8987 | if (ctxt->state != NULL) { |
| 8988 | cur = ctxt->state->seq; |
| 8989 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 8990 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 8991 | oldstate = ctxt->state; |
| 8992 | ctxt->state = NULL; |
| 8993 | if (cur != NULL) { |
| 8994 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 8995 | ret = -1; |
| 8996 | ctxt->state = oldstate; |
| 8997 | goto done; |
| 8998 | } |
| 8999 | } else if (ctxt->states != NULL) { |
| 9000 | int j; |
| 9001 | int found = 0; |
| 9002 | |
| 9003 | for (j = 0;j < ctxt->states->nbState;j++) { |
| 9004 | cur = ctxt->states->tabState[j]->seq; |
| 9005 | cur = xmlRelaxNGSkipIgnored(ctxt, cur); |
| 9006 | if (cur == NULL) { |
| 9007 | found = 1; |
| 9008 | break; |
| 9009 | } |
| 9010 | } |
| 9011 | if (ctxt->states->nbState > 0) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9012 | xmlRelaxNGFreeValidState(ctxt,oldstate); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9013 | oldstate = ctxt->states->tabState[ctxt->states->nbState - 1]; |
| 9014 | } |
| 9015 | for (j = 0;j < ctxt->states->nbState - 1;j++) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9016 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[j]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9017 | } |
| 9018 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9019 | ctxt->states = NULL; |
| 9020 | if (found == 0) { |
| 9021 | VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name); |
| 9022 | ret = -1; |
| 9023 | ctxt->state = oldstate; |
| 9024 | goto done; |
| 9025 | } |
| 9026 | } else { |
| 9027 | ret = -1; |
| 9028 | break; |
| 9029 | } |
| 9030 | if (lasts[i] != NULL) { |
| 9031 | lasts[i]->next = last; |
| 9032 | } |
| 9033 | } |
| 9034 | if (ctxt->state != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 9035 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9036 | ctxt->state = oldstate; |
| 9037 | ctxt->state->seq = lastelem; |
| 9038 | if (ret != 0) { |
| 9039 | VALID_ERR(XML_RELAXNG_ERR_INTERSEQ); |
| 9040 | ret = -1; |
| 9041 | goto done; |
| 9042 | } |
| 9043 | |
| 9044 | done: |
Daniel Veillard | 249d7bb | 2003-03-19 21:02:29 +0000 | [diff] [blame] | 9045 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9046 | /* |
| 9047 | * builds the next links chain from the prev one |
| 9048 | */ |
| 9049 | cur = lastchg; |
| 9050 | while (cur != NULL) { |
| 9051 | if ((cur == start) || (cur->prev == NULL)) |
| 9052 | break; |
| 9053 | cur->prev->next = cur; |
| 9054 | cur = cur->prev; |
| 9055 | } |
| 9056 | if (ret == 0) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 9057 | if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9058 | } |
| 9059 | |
| 9060 | xmlFree(list); |
| 9061 | xmlFree(lasts); |
| 9062 | return(ret); |
| 9063 | } |
| 9064 | |
| 9065 | /** |
| 9066 | * xmlRelaxNGValidateDefinitionList: |
| 9067 | * @ctxt: a Relax-NG validation context |
| 9068 | * @define: the list of definition to verify |
| 9069 | * |
| 9070 | * Validate the given node content against the (list) of definitions |
| 9071 | * |
| 9072 | * Returns 0 if the validation succeeded or an error code. |
| 9073 | */ |
| 9074 | static int |
| 9075 | xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt, |
| 9076 | xmlRelaxNGDefinePtr defines) { |
| 9077 | int ret = 0, res; |
| 9078 | |
| 9079 | |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9080 | if (defines == NULL) { |
| 9081 | VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, BAD_CAST "NULL definition list"); |
| 9082 | return(-1); |
| 9083 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9084 | while (defines != NULL) { |
| 9085 | if ((ctxt->state != NULL) || (ctxt->states != NULL)) { |
| 9086 | res = xmlRelaxNGValidateDefinition(ctxt, defines); |
| 9087 | if (res < 0) |
| 9088 | ret = -1; |
| 9089 | } else { |
| 9090 | VALID_ERR(XML_RELAXNG_ERR_NOSTATE); |
| 9091 | return(-1); |
| 9092 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 9093 | if (res == -1) /* continues on -2 */ |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9094 | break; |
| 9095 | defines = defines->next; |
| 9096 | } |
| 9097 | |
| 9098 | return(ret); |
| 9099 | } |
| 9100 | |
| 9101 | /** |
| 9102 | * xmlRelaxNGElementMatch: |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9103 | * @ctxt: a Relax-NG validation context |
| 9104 | * @define: the definition to check |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9105 | * @elem: the element |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9106 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9107 | * Check if the element matches the definition nameClass |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9108 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9109 | * 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] | 9110 | */ |
| 9111 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9112 | xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt, |
| 9113 | xmlRelaxNGDefinePtr define, |
| 9114 | xmlNodePtr elem) { |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 9115 | int ret = 0, oldflags = 0; |
Daniel Veillard | 416589a | 2003-02-17 17:25:42 +0000 | [diff] [blame] | 9116 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9117 | if (define->name != NULL) { |
| 9118 | if (!xmlStrEqual(elem->name, define->name)) { |
| 9119 | VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name); |
| 9120 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9121 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 9122 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9123 | if ((define->ns != NULL) && (define->ns[0] != 0)) { |
| 9124 | if (elem->ns == NULL) { |
| 9125 | VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, |
| 9126 | elem->name); |
| 9127 | return(0); |
| 9128 | } else if (!xmlStrEqual(elem->ns->href, define->ns)) { |
| 9129 | VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS, |
| 9130 | elem->name, define->ns); |
| 9131 | return(0); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9132 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9133 | } else if ((elem->ns != NULL) && (define->ns != NULL) && |
| 9134 | (define->name == NULL)) { |
| 9135 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 9136 | elem->name); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9137 | return(0); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9138 | } else if ((elem->ns != NULL) && (define->name != NULL)) { |
| 9139 | VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, |
| 9140 | define->name); |
| 9141 | return(0); |
| 9142 | } |
| 9143 | |
| 9144 | if (define->nameClass == NULL) |
| 9145 | return(1); |
| 9146 | |
| 9147 | define = define->nameClass; |
| 9148 | if (define->type == XML_RELAXNG_EXCEPT) { |
| 9149 | xmlRelaxNGDefinePtr list; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9150 | if (ctxt != NULL) { |
| 9151 | oldflags = ctxt->flags; |
| 9152 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9153 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9154 | |
| 9155 | list = define->content; |
| 9156 | while (list != NULL) { |
| 9157 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9158 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9159 | if (ctxt != NULL) |
| 9160 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9161 | return(0); |
| 9162 | } |
| 9163 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9164 | if (ctxt != NULL) |
| 9165 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9166 | return(ret); |
| 9167 | } |
| 9168 | list = list->next; |
| 9169 | } |
| 9170 | ret = 1; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9171 | if (ctxt != NULL) { |
| 9172 | ctxt->flags = oldflags; |
| 9173 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9174 | } else if (define->type == XML_RELAXNG_CHOICE) { |
| 9175 | xmlRelaxNGDefinePtr list; |
| 9176 | |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9177 | if (ctxt != NULL) { |
| 9178 | oldflags = ctxt->flags; |
| 9179 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9180 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9181 | |
| 9182 | list = define->nameClass; |
| 9183 | while (list != NULL) { |
| 9184 | ret = xmlRelaxNGElementMatch(ctxt, list, elem); |
| 9185 | if (ret == 1) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9186 | if (ctxt != NULL) |
| 9187 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9188 | return(1); |
| 9189 | } |
| 9190 | if (ret < 0) { |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9191 | if (ctxt != NULL) |
| 9192 | ctxt->flags = oldflags; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9193 | return(ret); |
| 9194 | } |
| 9195 | list = list->next; |
| 9196 | } |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9197 | if (ctxt != NULL) { |
| 9198 | if (ret != 0) { |
| 9199 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9200 | xmlRelaxNGDumpValidError(ctxt); |
| 9201 | } else { |
| 9202 | if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0); |
| 9203 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9204 | } |
| 9205 | ret = 0; |
Daniel Veillard | 0e3d3ce | 2003-03-21 12:43:18 +0000 | [diff] [blame] | 9206 | if (ctxt != NULL) { |
| 9207 | ctxt->flags = oldflags; |
| 9208 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9209 | } else { |
| 9210 | TODO |
| 9211 | ret = -1; |
| 9212 | } |
| 9213 | return(ret); |
| 9214 | } |
| 9215 | |
| 9216 | /** |
| 9217 | * xmlRelaxNGValidateElementEnd: |
| 9218 | * @ctxt: a Relax-NG validation context |
| 9219 | * |
| 9220 | * Validate the end of the element, implements check that |
| 9221 | * there is nothing left not consumed in the element content |
| 9222 | * or in the attribute list. |
| 9223 | * |
| 9224 | * Returns 0 if the validation succeeded or an error code. |
| 9225 | */ |
| 9226 | static int |
| 9227 | xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt) { |
| 9228 | int ret = 0, i; |
| 9229 | xmlRelaxNGValidStatePtr state; |
| 9230 | |
| 9231 | state = ctxt->state; |
| 9232 | if (state->seq != NULL) { |
| 9233 | state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq); |
| 9234 | if (state->seq != NULL) { |
| 9235 | VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT, |
| 9236 | state->node->name, state->seq->name); |
| 9237 | ret = -1; |
| 9238 | } |
| 9239 | } |
| 9240 | for (i = 0;i < state->nbAttrs;i++) { |
| 9241 | if (state->attrs[i] != NULL) { |
| 9242 | VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR, |
| 9243 | state->attrs[i]->name, state->node->name); |
| 9244 | ret = -1; |
| 9245 | } |
| 9246 | } |
| 9247 | return(ret); |
| 9248 | } |
| 9249 | |
| 9250 | /** |
| 9251 | * xmlRelaxNGValidateState: |
| 9252 | * @ctxt: a Relax-NG validation context |
| 9253 | * @define: the definition to verify |
| 9254 | * |
| 9255 | * Validate the current state against the definition |
| 9256 | * |
| 9257 | * Returns 0 if the validation succeeded or an error code. |
| 9258 | */ |
| 9259 | static int |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9260 | xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt, |
| 9261 | xmlRelaxNGDefinePtr define) |
| 9262 | { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9263 | xmlNodePtr node; |
| 9264 | int ret = 0, i, tmp, oldflags, errNr; |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 9265 | xmlRelaxNGValidStatePtr oldstate = NULL, state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9266 | |
| 9267 | if (define == NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9268 | VALID_ERR(XML_RELAXNG_ERR_NODEFINE); |
| 9269 | return (-1); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9270 | } |
| 9271 | |
| 9272 | if (ctxt->state != NULL) { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9273 | node = ctxt->state->seq; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9274 | } else { |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9275 | node = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9276 | } |
| 9277 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9278 | for (i = 0; i < ctxt->depth; i++) |
| 9279 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9280 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9281 | "Start validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9282 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9283 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9284 | if ((node != NULL) && (node->name != NULL)) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9285 | xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9286 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9287 | xmlGenericError(xmlGenericErrorContext, "\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9288 | #endif |
| 9289 | ctxt->depth++; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9290 | switch (define->type) { |
| 9291 | case XML_RELAXNG_EMPTY: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9292 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9293 | ret = 0; |
| 9294 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9295 | case XML_RELAXNG_NOT_ALLOWED: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9296 | ret = -1; |
| 9297 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9298 | case XML_RELAXNG_TEXT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9299 | while ((node != NULL) && |
| 9300 | ((node->type == XML_TEXT_NODE) || |
| 9301 | (node->type == XML_COMMENT_NODE) || |
| 9302 | (node->type == XML_PI_NODE) || |
| 9303 | (node->type == XML_CDATA_SECTION_NODE))) |
| 9304 | node = node->next; |
| 9305 | ctxt->state->seq = node; |
| 9306 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9307 | case XML_RELAXNG_ELEMENT: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9308 | errNr = ctxt->errNr; |
| 9309 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 9310 | if (node == NULL) { |
| 9311 | VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name); |
| 9312 | ret = -1; |
| 9313 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9314 | xmlRelaxNGDumpValidError(ctxt); |
| 9315 | break; |
| 9316 | } |
| 9317 | if (node->type != XML_ELEMENT_NODE) { |
| 9318 | VALID_ERR(XML_RELAXNG_ERR_NOTELEM); |
| 9319 | ret = -1; |
| 9320 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9321 | xmlRelaxNGDumpValidError(ctxt); |
| 9322 | break; |
| 9323 | } |
| 9324 | /* |
| 9325 | * This node was already validated successfully against |
| 9326 | * this definition. |
| 9327 | */ |
| 9328 | if (node->_private == define) { |
| 9329 | ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9330 | if (ctxt->errNr > errNr) |
| 9331 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9332 | if (ctxt->errNr != 0) { |
| 9333 | while ((ctxt->err != NULL) && |
| 9334 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) |
| 9335 | && (xmlStrEqual(ctxt->err->arg2, node->name))) |
| 9336 | || |
| 9337 | ((ctxt->err->err == |
| 9338 | XML_RELAXNG_ERR_ELEMEXTRANS) |
| 9339 | && (xmlStrEqual(ctxt->err->arg1, node->name))) |
| 9340 | || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) |
| 9341 | || (ctxt->err->err == |
| 9342 | XML_RELAXNG_ERR_NOTELEM))) |
| 9343 | xmlRelaxNGValidErrorPop(ctxt); |
| 9344 | } |
| 9345 | break; |
| 9346 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9347 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9348 | ret = xmlRelaxNGElementMatch(ctxt, define, node); |
| 9349 | if (ret <= 0) { |
| 9350 | ret = -1; |
| 9351 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9352 | xmlRelaxNGDumpValidError(ctxt); |
| 9353 | break; |
| 9354 | } |
| 9355 | ret = 0; |
| 9356 | if (ctxt->errNr != 0) { |
| 9357 | if (ctxt->errNr > errNr) |
| 9358 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9359 | while ((ctxt->err != NULL) && |
| 9360 | (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) && |
| 9361 | (xmlStrEqual(ctxt->err->arg2, node->name))) || |
| 9362 | ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) && |
| 9363 | (xmlStrEqual(ctxt->err->arg1, node->name))) || |
| 9364 | (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) || |
| 9365 | (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM))) |
| 9366 | xmlRelaxNGValidErrorPop(ctxt); |
| 9367 | } |
| 9368 | errNr = ctxt->errNr; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9369 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9370 | oldflags = ctxt->flags; |
| 9371 | if (ctxt->flags & FLAGS_MIXED_CONTENT) { |
| 9372 | ctxt->flags -= FLAGS_MIXED_CONTENT; |
| 9373 | } |
| 9374 | state = xmlRelaxNGNewValidState(ctxt, node); |
| 9375 | if (state == NULL) { |
| 9376 | ret = -1; |
| 9377 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) |
| 9378 | xmlRelaxNGDumpValidError(ctxt); |
| 9379 | break; |
| 9380 | } |
Daniel Veillard | 7fe1f3a | 2003-03-31 22:13:33 +0000 | [diff] [blame] | 9381 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9382 | oldstate = ctxt->state; |
| 9383 | ctxt->state = state; |
| 9384 | if (define->attrs != NULL) { |
| 9385 | tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs); |
| 9386 | if (tmp != 0) { |
| 9387 | ret = -1; |
| 9388 | VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name); |
| 9389 | } |
| 9390 | } |
| 9391 | if (define->contModel != NULL) { |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9392 | xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state; |
| 9393 | xmlRelaxNGStatesPtr tmpstates = ctxt->states; |
| 9394 | xmlNodePtr nseq; |
| 9395 | |
| 9396 | nstate = xmlRelaxNGNewValidState(ctxt, node); |
| 9397 | ctxt->state = nstate; |
| 9398 | ctxt->states = NULL; |
| 9399 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9400 | tmp = xmlRelaxNGValidateCompiledContent(ctxt, |
| 9401 | define->contModel, |
| 9402 | ctxt->state->seq); |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9403 | nseq = ctxt->state->seq; |
| 9404 | ctxt->state = tmpstate; |
| 9405 | ctxt->states = tmpstates; |
| 9406 | xmlRelaxNGFreeValidState(ctxt, nstate); |
| 9407 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9408 | #ifdef DEBUG_COMPILE |
| 9409 | xmlGenericError(xmlGenericErrorContext, |
| 9410 | "Validating content of '%s' : %d\n", define->name, tmp); |
| 9411 | #endif |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9412 | if (tmp != 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9413 | ret = -1; |
Daniel Veillard | ce192eb | 2003-04-16 15:58:05 +0000 | [diff] [blame] | 9414 | |
| 9415 | if (ctxt->states != NULL) { |
| 9416 | tmp = -1; |
| 9417 | |
| 9418 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9419 | |
| 9420 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9421 | state = ctxt->states->tabState[i]; |
| 9422 | ctxt->state = state; |
| 9423 | ctxt->state->seq = nseq; |
| 9424 | |
| 9425 | if (xmlRelaxNGValidateElementEnd(ctxt) == 0) |
| 9426 | tmp = 0; |
| 9427 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9428 | } |
| 9429 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9430 | ctxt->flags = oldflags; |
| 9431 | ctxt->states = NULL; |
| 9432 | if ((ret == 0) && (tmp == -1)) |
| 9433 | ret = -1; |
| 9434 | } else { |
| 9435 | state = ctxt->state; |
| 9436 | ctxt->state->seq = nseq; |
| 9437 | if (ret == 0) |
| 9438 | ret = xmlRelaxNGValidateElementEnd(ctxt); |
| 9439 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9440 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9441 | } else { |
| 9442 | if (define->content != NULL) { |
| 9443 | tmp = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9444 | define->content); |
| 9445 | if (tmp != 0) { |
| 9446 | ret = -1; |
| 9447 | if (ctxt->state == NULL) { |
| 9448 | ctxt->state = oldstate; |
| 9449 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9450 | node->name); |
| 9451 | ctxt->state = NULL; |
| 9452 | } else { |
| 9453 | VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, |
| 9454 | node->name); |
| 9455 | } |
| 9456 | |
| 9457 | } |
| 9458 | } |
| 9459 | if (ctxt->states != NULL) { |
| 9460 | tmp = -1; |
| 9461 | |
| 9462 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9463 | |
| 9464 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9465 | state = ctxt->states->tabState[i]; |
| 9466 | ctxt->state = state; |
| 9467 | |
| 9468 | if (xmlRelaxNGValidateElementEnd(ctxt) == 0) |
| 9469 | tmp = 0; |
| 9470 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9471 | } |
| 9472 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9473 | ctxt->flags = oldflags; |
| 9474 | ctxt->states = NULL; |
| 9475 | if ((ret == 0) && (tmp == -1)) |
| 9476 | ret = -1; |
| 9477 | } else { |
| 9478 | state = ctxt->state; |
| 9479 | if (ret == 0) |
| 9480 | ret = xmlRelaxNGValidateElementEnd(ctxt); |
| 9481 | xmlRelaxNGFreeValidState(ctxt, state); |
| 9482 | } |
| 9483 | } |
| 9484 | if (ret == 0) { |
| 9485 | node->_private = define; |
| 9486 | } |
| 9487 | ctxt->flags = oldflags; |
| 9488 | ctxt->state = oldstate; |
| 9489 | if (oldstate != NULL) |
| 9490 | oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next); |
| 9491 | if (ret != 0) { |
| 9492 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9493 | xmlRelaxNGDumpValidError(ctxt); |
| 9494 | ret = 0; |
| 9495 | } else { |
| 9496 | ret = -2; |
| 9497 | } |
| 9498 | } else { |
| 9499 | if (ctxt->errNr > errNr) |
| 9500 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9501 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9502 | |
| 9503 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9504 | xmlGenericError(xmlGenericErrorContext, |
| 9505 | "xmlRelaxNGValidateDefinition(): validated %s : %d", |
| 9506 | node->name, ret); |
| 9507 | if (oldstate == NULL) |
| 9508 | xmlGenericError(xmlGenericErrorContext, ": no state\n"); |
| 9509 | else if (oldstate->seq == NULL) |
| 9510 | xmlGenericError(xmlGenericErrorContext, ": done\n"); |
| 9511 | else if (oldstate->seq->type == XML_ELEMENT_NODE) |
| 9512 | xmlGenericError(xmlGenericErrorContext, ": next elem %s\n", |
| 9513 | oldstate->seq->name); |
| 9514 | else |
| 9515 | xmlGenericError(xmlGenericErrorContext, ": next %s %d\n", |
| 9516 | oldstate->seq->name, oldstate->seq->type); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9517 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9518 | break; |
| 9519 | case XML_RELAXNG_OPTIONAL:{ |
| 9520 | errNr = ctxt->errNr; |
| 9521 | oldflags = ctxt->flags; |
| 9522 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9523 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9524 | ret = |
| 9525 | xmlRelaxNGValidateDefinitionList(ctxt, |
| 9526 | define->content); |
| 9527 | if (ret != 0) { |
| 9528 | if (ctxt->state != NULL) |
| 9529 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9530 | ctxt->state = oldstate; |
| 9531 | ctxt->flags = oldflags; |
| 9532 | ret = 0; |
| 9533 | if (ctxt->errNr > errNr) |
| 9534 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9535 | break; |
| 9536 | } |
| 9537 | if (ctxt->states != NULL) { |
| 9538 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9539 | } else { |
| 9540 | ctxt->states = xmlRelaxNGNewStates(ctxt, 1); |
| 9541 | if (ctxt->states == NULL) { |
| 9542 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9543 | ctxt->flags = oldflags; |
| 9544 | ret = -1; |
| 9545 | if (ctxt->errNr > errNr) |
| 9546 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9547 | break; |
| 9548 | } |
| 9549 | xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate); |
| 9550 | xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state); |
| 9551 | ctxt->state = NULL; |
| 9552 | } |
| 9553 | ctxt->flags = oldflags; |
| 9554 | ret = 0; |
| 9555 | if (ctxt->errNr > errNr) |
| 9556 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9557 | break; |
| 9558 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9559 | case XML_RELAXNG_ONEORMORE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9560 | errNr = ctxt->errNr; |
| 9561 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9562 | if (ret != 0) { |
| 9563 | break; |
| 9564 | } |
| 9565 | if (ctxt->errNr > errNr) |
| 9566 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9567 | /* no break on purpose */ |
| 9568 | case XML_RELAXNG_ZEROORMORE:{ |
| 9569 | int progress; |
| 9570 | xmlRelaxNGStatesPtr states = NULL, res = NULL; |
| 9571 | int base, j; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9572 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9573 | errNr = ctxt->errNr; |
| 9574 | res = xmlRelaxNGNewStates(ctxt, 1); |
| 9575 | if (res == NULL) { |
| 9576 | ret = -1; |
| 9577 | break; |
| 9578 | } |
| 9579 | /* |
| 9580 | * All the input states are also exit states |
| 9581 | */ |
| 9582 | if (ctxt->state != NULL) { |
| 9583 | xmlRelaxNGAddStates(ctxt, res, |
| 9584 | xmlRelaxNGCopyValidState(ctxt, |
| 9585 | ctxt-> |
| 9586 | state)); |
| 9587 | } else { |
| 9588 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9589 | xmlRelaxNGAddStates(ctxt, res, |
| 9590 | xmlRelaxNGCopyValidState(ctxt, |
| 9591 | ctxt-> |
| 9592 | states-> |
| 9593 | tabState |
| 9594 | [j])); |
| 9595 | } |
| 9596 | } |
| 9597 | oldflags = ctxt->flags; |
| 9598 | ctxt->flags |= FLAGS_IGNORABLE; |
| 9599 | do { |
| 9600 | progress = 0; |
| 9601 | base = res->nbState; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9602 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9603 | if (ctxt->states != NULL) { |
| 9604 | states = ctxt->states; |
| 9605 | for (i = 0; i < states->nbState; i++) { |
| 9606 | ctxt->state = states->tabState[i]; |
| 9607 | ctxt->states = NULL; |
| 9608 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9609 | define-> |
| 9610 | content); |
| 9611 | if (ret == 0) { |
| 9612 | if (ctxt->state != NULL) { |
| 9613 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9614 | ctxt->state); |
| 9615 | ctxt->state = NULL; |
| 9616 | if (tmp == 1) |
| 9617 | progress = 1; |
| 9618 | } else if (ctxt->states != NULL) { |
| 9619 | for (j = 0; j < ctxt->states->nbState; |
| 9620 | j++) { |
| 9621 | tmp = |
| 9622 | xmlRelaxNGAddStates(ctxt, res, |
| 9623 | ctxt-> |
| 9624 | states-> |
| 9625 | tabState |
| 9626 | [j]); |
| 9627 | if (tmp == 1) |
| 9628 | progress = 1; |
| 9629 | } |
| 9630 | xmlRelaxNGFreeStates(ctxt, |
| 9631 | ctxt->states); |
| 9632 | ctxt->states = NULL; |
| 9633 | } |
| 9634 | } else { |
| 9635 | if (ctxt->state != NULL) { |
| 9636 | xmlRelaxNGFreeValidState(ctxt, |
| 9637 | ctxt->state); |
| 9638 | ctxt->state = NULL; |
| 9639 | } |
| 9640 | } |
| 9641 | } |
| 9642 | } else { |
| 9643 | ret = xmlRelaxNGValidateDefinitionList(ctxt, |
| 9644 | define-> |
| 9645 | content); |
| 9646 | if (ret != 0) { |
| 9647 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9648 | ctxt->state = NULL; |
| 9649 | } else { |
| 9650 | base = res->nbState; |
| 9651 | if (ctxt->state != NULL) { |
| 9652 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9653 | ctxt->state); |
| 9654 | ctxt->state = NULL; |
| 9655 | if (tmp == 1) |
| 9656 | progress = 1; |
| 9657 | } else if (ctxt->states != NULL) { |
| 9658 | for (j = 0; j < ctxt->states->nbState; j++) { |
| 9659 | tmp = xmlRelaxNGAddStates(ctxt, res, |
| 9660 | ctxt-> |
| 9661 | states-> |
| 9662 | tabState[j]); |
| 9663 | if (tmp == 1) |
| 9664 | progress = 1; |
| 9665 | } |
| 9666 | if (states == NULL) { |
| 9667 | states = ctxt->states; |
| 9668 | } else { |
| 9669 | xmlRelaxNGFreeStates(ctxt, |
| 9670 | ctxt->states); |
| 9671 | } |
| 9672 | ctxt->states = NULL; |
| 9673 | } |
| 9674 | } |
| 9675 | } |
| 9676 | if (progress) { |
| 9677 | /* |
| 9678 | * Collect all the new nodes added at that step |
| 9679 | * and make them the new node set |
| 9680 | */ |
| 9681 | if (res->nbState - base == 1) { |
| 9682 | ctxt->state = xmlRelaxNGCopyValidState(ctxt, |
| 9683 | res-> |
| 9684 | tabState |
| 9685 | [base]); |
| 9686 | } else { |
| 9687 | if (states == NULL) { |
| 9688 | xmlRelaxNGNewStates(ctxt, |
| 9689 | res->nbState - base); |
| 9690 | } |
| 9691 | states->nbState = 0; |
| 9692 | for (i = base; i < res->nbState; i++) |
| 9693 | xmlRelaxNGAddStates(ctxt, states, |
| 9694 | xmlRelaxNGCopyValidState |
| 9695 | (ctxt, |
| 9696 | res->tabState[i])); |
| 9697 | ctxt->states = states; |
| 9698 | } |
| 9699 | } |
| 9700 | } while (progress == 1); |
| 9701 | if (states != NULL) { |
| 9702 | xmlRelaxNGFreeStates(ctxt, states); |
| 9703 | } |
| 9704 | ctxt->states = res; |
| 9705 | ctxt->flags = oldflags; |
| 9706 | if (ctxt->errNr > errNr) |
| 9707 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9708 | ret = 0; |
| 9709 | break; |
| 9710 | } |
| 9711 | case XML_RELAXNG_CHOICE:{ |
| 9712 | xmlRelaxNGDefinePtr list = NULL; |
| 9713 | xmlRelaxNGStatesPtr states = NULL; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9714 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9715 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9716 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9717 | errNr = ctxt->errNr; |
| 9718 | if ((define->dflags & IS_TRIABLE) |
| 9719 | && (define->data != NULL)) { |
| 9720 | xmlHashTablePtr triage = |
| 9721 | (xmlHashTablePtr) define->data; |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9722 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9723 | /* |
| 9724 | * Something we can optimize cleanly there is only one |
| 9725 | * possble branch out ! |
| 9726 | */ |
| 9727 | if (node == NULL) { |
| 9728 | ret = -1; |
| 9729 | break; |
| 9730 | } |
| 9731 | if ((node->type == XML_TEXT_NODE) || |
| 9732 | (node->type == XML_CDATA_SECTION_NODE)) { |
| 9733 | list = |
| 9734 | xmlHashLookup2(triage, BAD_CAST "#text", NULL); |
| 9735 | } else if (node->type == XML_ELEMENT_NODE) { |
| 9736 | if (node->ns != NULL) { |
| 9737 | list = xmlHashLookup2(triage, node->name, |
| 9738 | node->ns->href); |
| 9739 | if (list == NULL) |
| 9740 | list = |
| 9741 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9742 | node->ns->href); |
| 9743 | } else |
| 9744 | list = |
| 9745 | xmlHashLookup2(triage, node->name, NULL); |
| 9746 | if (list == NULL) |
| 9747 | list = |
| 9748 | xmlHashLookup2(triage, BAD_CAST "#any", |
| 9749 | NULL); |
| 9750 | } |
| 9751 | if (list == NULL) { |
| 9752 | ret = -1; |
| 9753 | break; |
| 9754 | } |
| 9755 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 9756 | if (ret == 0) { |
| 9757 | } |
| 9758 | break; |
| 9759 | } |
Daniel Veillard | e063f48 | 2003-03-21 16:53:17 +0000 | [diff] [blame] | 9760 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9761 | list = define->content; |
| 9762 | oldflags = ctxt->flags; |
| 9763 | ctxt->flags |= FLAGS_IGNORABLE; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9764 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9765 | while (list != NULL) { |
| 9766 | oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state); |
| 9767 | ret = xmlRelaxNGValidateDefinition(ctxt, list); |
| 9768 | if (ret == 0) { |
| 9769 | if (states == NULL) { |
| 9770 | states = xmlRelaxNGNewStates(ctxt, 1); |
| 9771 | } |
| 9772 | if (ctxt->state != NULL) { |
| 9773 | xmlRelaxNGAddStates(ctxt, states, ctxt->state); |
| 9774 | } else if (ctxt->states != NULL) { |
| 9775 | for (i = 0; i < ctxt->states->nbState; i++) { |
| 9776 | xmlRelaxNGAddStates(ctxt, states, |
| 9777 | ctxt->states-> |
| 9778 | tabState[i]); |
| 9779 | } |
| 9780 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 9781 | ctxt->states = NULL; |
| 9782 | } |
| 9783 | } else { |
| 9784 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 9785 | } |
| 9786 | ctxt->state = oldstate; |
| 9787 | list = list->next; |
| 9788 | } |
| 9789 | if (states != NULL) { |
| 9790 | xmlRelaxNGFreeValidState(ctxt, oldstate); |
| 9791 | ctxt->states = states; |
| 9792 | ctxt->state = NULL; |
| 9793 | ret = 0; |
| 9794 | } else { |
| 9795 | ctxt->states = NULL; |
| 9796 | } |
| 9797 | ctxt->flags = oldflags; |
| 9798 | if (ret != 0) { |
| 9799 | if ((ctxt->flags & FLAGS_IGNORABLE) == 0) { |
| 9800 | xmlRelaxNGDumpValidError(ctxt); |
| 9801 | } |
| 9802 | } else { |
| 9803 | if (ctxt->errNr > errNr) |
| 9804 | xmlRelaxNGPopErrors(ctxt, errNr); |
| 9805 | } |
| 9806 | break; |
| 9807 | } |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9808 | case XML_RELAXNG_DEF: |
| 9809 | case XML_RELAXNG_GROUP: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9810 | ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content); |
| 9811 | break; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9812 | case XML_RELAXNG_INTERLEAVE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9813 | ret = xmlRelaxNGValidateInterleave(ctxt, define); |
| 9814 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9815 | case XML_RELAXNG_ATTRIBUTE: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9816 | ret = xmlRelaxNGValidateAttribute(ctxt, define); |
| 9817 | break; |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 9818 | case XML_RELAXNG_START: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9819 | case XML_RELAXNG_NOOP: |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9820 | case XML_RELAXNG_REF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9821 | case XML_RELAXNG_EXTERNALREF: |
Daniel Veillard | 952379b | 2003-03-17 15:37:12 +0000 | [diff] [blame] | 9822 | case XML_RELAXNG_PARENTREF: |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9823 | ret = xmlRelaxNGValidateDefinition(ctxt, define->content); |
| 9824 | break; |
| 9825 | case XML_RELAXNG_DATATYPE:{ |
| 9826 | xmlNodePtr child; |
| 9827 | xmlChar *content = NULL; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9828 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9829 | child = node; |
| 9830 | while (child != NULL) { |
| 9831 | if (child->type == XML_ELEMENT_NODE) { |
| 9832 | VALID_ERR2(XML_RELAXNG_ERR_DATAELEM, |
| 9833 | node->parent->name); |
| 9834 | ret = -1; |
| 9835 | break; |
| 9836 | } else if ((child->type == XML_TEXT_NODE) || |
| 9837 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 9838 | content = xmlStrcat(content, child->content); |
| 9839 | } |
| 9840 | /* TODO: handle entities ... */ |
| 9841 | child = child->next; |
| 9842 | } |
| 9843 | if (ret == -1) { |
| 9844 | if (content != NULL) |
| 9845 | xmlFree(content); |
| 9846 | break; |
| 9847 | } |
| 9848 | if (content == NULL) { |
| 9849 | content = xmlStrdup(BAD_CAST ""); |
| 9850 | if (content == NULL) { |
| 9851 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 9852 | ret = -1; |
| 9853 | break; |
| 9854 | } |
| 9855 | } |
| 9856 | ret = xmlRelaxNGValidateDatatype(ctxt, content, define, |
| 9857 | ctxt->state->seq); |
| 9858 | if (ret == -1) { |
| 9859 | VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name); |
| 9860 | } else if (ret == 0) { |
| 9861 | ctxt->state->seq = NULL; |
| 9862 | } |
| 9863 | if (content != NULL) |
| 9864 | xmlFree(content); |
| 9865 | break; |
| 9866 | } |
| 9867 | case XML_RELAXNG_VALUE:{ |
| 9868 | xmlChar *content = NULL; |
| 9869 | xmlChar *oldvalue; |
| 9870 | xmlNodePtr child; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9871 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9872 | child = node; |
| 9873 | while (child != NULL) { |
| 9874 | if (child->type == XML_ELEMENT_NODE) { |
| 9875 | VALID_ERR2(XML_RELAXNG_ERR_VALELEM, |
| 9876 | node->parent->name); |
| 9877 | ret = -1; |
| 9878 | break; |
| 9879 | } else if ((child->type == XML_TEXT_NODE) || |
| 9880 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 9881 | content = xmlStrcat(content, child->content); |
| 9882 | } |
| 9883 | /* TODO: handle entities ... */ |
| 9884 | child = child->next; |
| 9885 | } |
| 9886 | if (ret == -1) { |
| 9887 | if (content != NULL) |
| 9888 | xmlFree(content); |
| 9889 | break; |
| 9890 | } |
| 9891 | if (content == NULL) { |
| 9892 | content = xmlStrdup(BAD_CAST ""); |
| 9893 | if (content == NULL) { |
| 9894 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 9895 | ret = -1; |
| 9896 | break; |
| 9897 | } |
| 9898 | } |
| 9899 | oldvalue = ctxt->state->value; |
| 9900 | ctxt->state->value = content; |
| 9901 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 9902 | ctxt->state->value = oldvalue; |
| 9903 | if (ret == -1) { |
| 9904 | VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name); |
| 9905 | } else if (ret == 0) { |
| 9906 | ctxt->state->seq = NULL; |
| 9907 | } |
| 9908 | if (content != NULL) |
| 9909 | xmlFree(content); |
| 9910 | break; |
| 9911 | } |
| 9912 | case XML_RELAXNG_LIST:{ |
| 9913 | xmlChar *content; |
| 9914 | xmlNodePtr child; |
| 9915 | xmlChar *oldvalue, *oldendvalue; |
| 9916 | int len; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9917 | |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9918 | /* |
| 9919 | * Make sure it's only text nodes |
| 9920 | */ |
| 9921 | |
| 9922 | content = NULL; |
| 9923 | child = node; |
| 9924 | while (child != NULL) { |
| 9925 | if (child->type == XML_ELEMENT_NODE) { |
| 9926 | VALID_ERR2(XML_RELAXNG_ERR_LISTELEM, |
| 9927 | node->parent->name); |
| 9928 | ret = -1; |
| 9929 | break; |
| 9930 | } else if ((child->type == XML_TEXT_NODE) || |
| 9931 | (child->type == XML_CDATA_SECTION_NODE)) { |
| 9932 | content = xmlStrcat(content, child->content); |
| 9933 | } |
| 9934 | /* TODO: handle entities ... */ |
| 9935 | child = child->next; |
| 9936 | } |
| 9937 | if (ret == -1) { |
| 9938 | if (content != NULL) |
| 9939 | xmlFree(content); |
| 9940 | break; |
| 9941 | } |
| 9942 | if (content == NULL) { |
| 9943 | content = xmlStrdup(BAD_CAST ""); |
| 9944 | if (content == NULL) { |
| 9945 | VALID_ERR(XML_RELAXNG_ERR_MEMORY); |
| 9946 | ret = -1; |
| 9947 | break; |
| 9948 | } |
| 9949 | } |
| 9950 | len = xmlStrlen(content); |
| 9951 | oldvalue = ctxt->state->value; |
| 9952 | oldendvalue = ctxt->state->endvalue; |
| 9953 | ctxt->state->value = content; |
| 9954 | ctxt->state->endvalue = content + len; |
| 9955 | ret = xmlRelaxNGValidateValue(ctxt, define); |
| 9956 | ctxt->state->value = oldvalue; |
| 9957 | ctxt->state->endvalue = oldendvalue; |
| 9958 | if (ret == -1) { |
| 9959 | VALID_ERR(XML_RELAXNG_ERR_LIST); |
| 9960 | } else if ((ret == 0) && (node != NULL)) { |
| 9961 | ctxt->state->seq = node->next; |
| 9962 | } |
| 9963 | if (content != NULL) |
| 9964 | xmlFree(content); |
| 9965 | break; |
| 9966 | } |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9967 | case XML_RELAXNG_EXCEPT: |
| 9968 | case XML_RELAXNG_PARAM: |
| 9969 | TODO ret = -1; |
| 9970 | break; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9971 | } |
| 9972 | ctxt->depth--; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9973 | #ifdef DEBUG |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9974 | for (i = 0; i < ctxt->depth; i++) |
| 9975 | xmlGenericError(xmlGenericErrorContext, " "); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9976 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9977 | "Validating %s ", xmlRelaxNGDefName(define)); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9978 | if (define->name != NULL) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9979 | xmlGenericError(xmlGenericErrorContext, "%s ", define->name); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9980 | if (ret == 0) |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9981 | xmlGenericError(xmlGenericErrorContext, "suceeded\n"); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9982 | else |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9983 | xmlGenericError(xmlGenericErrorContext, "failed\n"); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9984 | #endif |
Daniel Veillard | c58f4ef | 2003-04-14 16:11:26 +0000 | [diff] [blame] | 9985 | return (ret); |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9986 | } |
| 9987 | |
| 9988 | /** |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9989 | * xmlRelaxNGValidateDefinition: |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9990 | * @ctxt: a Relax-NG validation context |
| 9991 | * @define: the definition to verify |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9992 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9993 | * Validate the current node lists against the definition |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9994 | * |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9995 | * Returns 0 if the validation succeeded or an error code. |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 9996 | */ |
| 9997 | static int |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 9998 | xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt, |
| 9999 | xmlRelaxNGDefinePtr define) { |
| 10000 | xmlRelaxNGStatesPtr states, res; |
| 10001 | int i, j, k, ret, oldflags; |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10002 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10003 | /* |
| 10004 | * We should NOT have both ctxt->state and ctxt->states |
| 10005 | */ |
| 10006 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10007 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10008 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10009 | ctxt->state = NULL; |
| 10010 | } |
| 10011 | |
| 10012 | if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) { |
| 10013 | if (ctxt->states != NULL) { |
| 10014 | ctxt->state = ctxt->states->tabState[0]; |
| 10015 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10016 | ctxt->states = NULL; |
| 10017 | } |
| 10018 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10019 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10020 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10021 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10022 | ctxt->state = NULL; |
| 10023 | } |
| 10024 | if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) { |
| 10025 | ctxt->state = ctxt->states->tabState[0]; |
| 10026 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10027 | ctxt->states = NULL; |
| 10028 | } |
| 10029 | return(ret); |
| 10030 | } |
| 10031 | |
| 10032 | states = ctxt->states; |
| 10033 | ctxt->states = NULL; |
| 10034 | res = NULL; |
| 10035 | j = 0; |
| 10036 | oldflags = ctxt->flags; |
| 10037 | ctxt->flags |= FLAGS_IGNORABLE; |
| 10038 | for (i = 0;i < states->nbState;i++) { |
| 10039 | ctxt->state = states->tabState[i]; |
| 10040 | ctxt->states = NULL; |
| 10041 | ret = xmlRelaxNGValidateState(ctxt, define); |
| 10042 | /* |
| 10043 | * We should NOT have both ctxt->state and ctxt->states |
| 10044 | */ |
| 10045 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10046 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10047 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10048 | ctxt->state = NULL; |
| 10049 | } |
| 10050 | if (ret == 0) { |
| 10051 | if (ctxt->states == NULL) { |
| 10052 | if (res != NULL) { |
| 10053 | /* add the state to the container */ |
| 10054 | xmlRelaxNGAddStates(ctxt, res, ctxt->state); |
| 10055 | ctxt->state = NULL; |
| 10056 | } else { |
| 10057 | /* add the state directly in states */ |
| 10058 | states->tabState[j++] = ctxt->state; |
| 10059 | ctxt->state = NULL; |
| 10060 | } |
| 10061 | } else { |
| 10062 | if (res == NULL) { |
| 10063 | /* make it the new container and copy other results */ |
| 10064 | res = ctxt->states; |
| 10065 | ctxt->states = NULL; |
| 10066 | for (k = 0;k < j;k++) |
| 10067 | xmlRelaxNGAddStates(ctxt, res, states->tabState[k]); |
| 10068 | } else { |
| 10069 | /* add all the new results to res and reff the container */ |
| 10070 | for (k = 0;k < ctxt->states->nbState;k++) |
| 10071 | xmlRelaxNGAddStates(ctxt, res, |
| 10072 | ctxt->states->tabState[k]); |
| 10073 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10074 | ctxt->states = NULL; |
| 10075 | } |
| 10076 | } |
| 10077 | } else { |
| 10078 | if (ctxt->state != NULL) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10079 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10080 | ctxt->state = NULL; |
| 10081 | } else if (ctxt->states != NULL) { |
| 10082 | for (k = 0;k < ctxt->states->nbState;k++) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10083 | xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[k]); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10084 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10085 | ctxt->states = NULL; |
| 10086 | } |
| 10087 | } |
| 10088 | } |
| 10089 | ctxt->flags = oldflags; |
| 10090 | if (res != NULL) { |
| 10091 | xmlRelaxNGFreeStates(ctxt, states); |
| 10092 | ctxt->states = res; |
| 10093 | ret = 0; |
| 10094 | } else if (j > 1) { |
| 10095 | states->nbState = j; |
| 10096 | ctxt->states = states; |
| 10097 | ret =0; |
| 10098 | } else if (j == 1) { |
| 10099 | ctxt->state = states->tabState[0]; |
| 10100 | xmlRelaxNGFreeStates(ctxt, states); |
| 10101 | ret = 0; |
| 10102 | } else { |
| 10103 | ret = -1; |
| 10104 | xmlRelaxNGFreeStates(ctxt, states); |
| 10105 | if (ctxt->states != NULL) { |
| 10106 | xmlRelaxNGFreeStates(ctxt, ctxt->states); |
| 10107 | ctxt->states = NULL; |
| 10108 | } |
| 10109 | } |
| 10110 | if ((ctxt->state != NULL) && (ctxt->states != NULL)) { |
| 10111 | TODO |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10112 | xmlRelaxNGFreeValidState(ctxt,ctxt->state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10113 | ctxt->state = NULL; |
| 10114 | } |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10115 | return(ret); |
| 10116 | } |
| 10117 | |
Daniel Veillard | 1564e6e | 2003-03-15 21:30:25 +0000 | [diff] [blame] | 10118 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10119 | * xmlRelaxNGValidateDocument: |
| 10120 | * @ctxt: a Relax-NG validation context |
| 10121 | * @doc: the document |
| 10122 | * |
| 10123 | * Validate the given document |
| 10124 | * |
| 10125 | * Returns 0 if the validation succeeded or an error code. |
| 10126 | */ |
| 10127 | static int |
| 10128 | xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 10129 | int ret; |
| 10130 | xmlRelaxNGPtr schema; |
| 10131 | xmlRelaxNGGrammarPtr grammar; |
| 10132 | xmlRelaxNGValidStatePtr state; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10133 | xmlNodePtr node; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10134 | |
| 10135 | if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL)) |
| 10136 | return(-1); |
| 10137 | |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10138 | ctxt->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10139 | schema = ctxt->schema; |
| 10140 | grammar = schema->topgrammar; |
| 10141 | if (grammar == NULL) { |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10142 | VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10143 | return(-1); |
| 10144 | } |
| 10145 | state = xmlRelaxNGNewValidState(ctxt, NULL); |
| 10146 | ctxt->state = state; |
| 10147 | ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10148 | if ((ctxt->state != NULL) && (state->seq != NULL)) { |
| 10149 | state = ctxt->state; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10150 | node = state->seq; |
| 10151 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10152 | if (node != NULL) { |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10153 | if (ret != -1) { |
| 10154 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10155 | ret = -1; |
| 10156 | } |
| 10157 | } |
| 10158 | } else if (ctxt->states != NULL) { |
| 10159 | int i; |
| 10160 | int tmp = -1; |
| 10161 | |
| 10162 | for (i = 0;i < ctxt->states->nbState;i++) { |
| 10163 | state = ctxt->states->tabState[i]; |
| 10164 | node = state->seq; |
| 10165 | node = xmlRelaxNGSkipIgnored(ctxt, node); |
| 10166 | if (node == NULL) |
| 10167 | tmp = 0; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10168 | xmlRelaxNGFreeValidState(ctxt,state); |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10169 | } |
| 10170 | if (tmp == -1) { |
| 10171 | if (ret != -1) { |
| 10172 | VALID_ERR(XML_RELAXNG_ERR_EXTRADATA); |
| 10173 | ret = -1; |
| 10174 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10175 | } |
| 10176 | } |
Daniel Veillard | bbb78b5 | 2003-03-21 01:24:45 +0000 | [diff] [blame] | 10177 | if (ctxt->state != NULL) { |
| 10178 | xmlRelaxNGFreeValidState(ctxt, ctxt->state); |
| 10179 | ctxt->state = NULL; |
| 10180 | } |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10181 | if (ret != 0) |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10182 | xmlRelaxNGDumpValidError(ctxt); |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 10183 | #ifdef DEBUG |
| 10184 | else if (ctxt->errNr != 0) { |
| 10185 | ctxt->error(ctxt->userData, "%d Extra error messages left on stack !\n", |
| 10186 | ctxt->errNr); |
| 10187 | xmlRelaxNGDumpValidError(ctxt); |
| 10188 | } |
| 10189 | #endif |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10190 | if (ctxt->idref == 1) { |
| 10191 | xmlValidCtxt vctxt; |
| 10192 | |
| 10193 | memset(&vctxt, 0, sizeof(xmlValidCtxt)); |
| 10194 | vctxt.valid = 1; |
| 10195 | vctxt.error = ctxt->error; |
| 10196 | vctxt.warning = ctxt->warning; |
| 10197 | vctxt.userData = ctxt->userData; |
| 10198 | |
| 10199 | if (xmlValidateDocumentFinal(&vctxt, doc) != 1) |
| 10200 | ret = -1; |
| 10201 | } |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10202 | if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK)) |
| 10203 | ret = -1; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10204 | |
| 10205 | return(ret); |
| 10206 | } |
| 10207 | |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10208 | /************************************************************************ |
| 10209 | * * |
| 10210 | * Validation interfaces * |
| 10211 | * * |
| 10212 | ************************************************************************/ |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10213 | /** |
| 10214 | * xmlRelaxNGNewValidCtxt: |
| 10215 | * @schema: a precompiled XML RelaxNGs |
| 10216 | * |
| 10217 | * Create an XML RelaxNGs validation context based on the given schema |
| 10218 | * |
| 10219 | * Returns the validation context or NULL in case of error |
| 10220 | */ |
| 10221 | xmlRelaxNGValidCtxtPtr |
| 10222 | xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) { |
| 10223 | xmlRelaxNGValidCtxtPtr ret; |
| 10224 | |
| 10225 | ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt)); |
| 10226 | if (ret == NULL) { |
| 10227 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10228 | "Failed to allocate new schema validation context\n"); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10229 | return (NULL); |
| 10230 | } |
| 10231 | memset(ret, 0, sizeof(xmlRelaxNGValidCtxt)); |
| 10232 | ret->schema = schema; |
Daniel Veillard | 1703c5f | 2003-02-10 14:28:44 +0000 | [diff] [blame] | 10233 | ret->error = xmlGenericError; |
| 10234 | ret->userData = xmlGenericErrorContext; |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10235 | ret->errNr = 0; |
| 10236 | ret->errMax = 0; |
| 10237 | ret->err = NULL; |
| 10238 | ret->errTab = NULL; |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 10239 | ret->idref = schema->idref; |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10240 | ret->states = NULL; |
| 10241 | ret->freeState = NULL; |
| 10242 | ret->freeStates = NULL; |
Daniel Veillard | a507fbf | 2003-03-31 16:09:37 +0000 | [diff] [blame] | 10243 | ret->errNo = XML_RELAXNG_OK; |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10244 | return (ret); |
| 10245 | } |
| 10246 | |
| 10247 | /** |
| 10248 | * xmlRelaxNGFreeValidCtxt: |
| 10249 | * @ctxt: the schema validation context |
| 10250 | * |
| 10251 | * Free the resources associated to the schema validation context |
| 10252 | */ |
| 10253 | void |
| 10254 | xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) { |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10255 | int k; |
| 10256 | |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10257 | if (ctxt == NULL) |
| 10258 | return; |
Daniel Veillard | fd573f1 | 2003-03-16 17:52:32 +0000 | [diff] [blame] | 10259 | if (ctxt->states != NULL) |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10260 | xmlRelaxNGFreeStates(NULL, ctxt->states); |
| 10261 | if (ctxt->freeState != NULL) { |
| 10262 | for (k = 0;k < ctxt->freeState->nbState;k++) { |
| 10263 | xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]); |
| 10264 | } |
| 10265 | xmlRelaxNGFreeStates(NULL, ctxt->freeState); |
| 10266 | } |
Daniel Veillard | 798024a | 2003-03-19 10:36:09 +0000 | [diff] [blame] | 10267 | if (ctxt->freeStates != NULL) { |
| 10268 | for (k = 0;k < ctxt->freeStatesNr;k++) { |
| 10269 | xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]); |
| 10270 | } |
| 10271 | xmlFree(ctxt->freeStates); |
| 10272 | } |
Daniel Veillard | 42f12e9 | 2003-03-07 18:32:59 +0000 | [diff] [blame] | 10273 | if (ctxt->errTab != NULL) |
| 10274 | xmlFree(ctxt->errTab); |
Daniel Veillard | f4e5576 | 2003-04-15 23:32:22 +0000 | [diff] [blame] | 10275 | if (ctxt->elemTab != NULL) { |
| 10276 | xmlRegExecCtxtPtr exec; |
| 10277 | |
| 10278 | exec = xmlRelaxNGElemPop(ctxt); |
| 10279 | while (exec != NULL) { |
| 10280 | xmlRegFreeExecCtxt(exec); |
| 10281 | exec = xmlRelaxNGElemPop(ctxt); |
| 10282 | } |
| 10283 | xmlFree(ctxt->elemTab); |
| 10284 | } |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10285 | xmlFree(ctxt); |
| 10286 | } |
| 10287 | |
| 10288 | /** |
| 10289 | * xmlRelaxNGSetValidErrors: |
| 10290 | * @ctxt: a Relax-NG validation context |
| 10291 | * @err: the error function |
| 10292 | * @warn: the warning function |
| 10293 | * @ctx: the functions context |
| 10294 | * |
| 10295 | * Set the error and warning callback informations |
| 10296 | */ |
| 10297 | void |
| 10298 | xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
| 10299 | xmlRelaxNGValidityErrorFunc err, |
| 10300 | xmlRelaxNGValidityWarningFunc warn, void *ctx) { |
| 10301 | if (ctxt == NULL) |
| 10302 | return; |
| 10303 | ctxt->error = err; |
| 10304 | ctxt->warning = warn; |
| 10305 | ctxt->userData = ctx; |
| 10306 | } |
| 10307 | |
| 10308 | /** |
Daniel Veillard | 409a814 | 2003-07-18 15:16:57 +0000 | [diff] [blame] | 10309 | * xmlRelaxNGGetValidErrors: |
| 10310 | * @ctxt: a Relax-NG validation context |
| 10311 | * @err: the error function result |
| 10312 | * @warn: the warning function result |
| 10313 | * @ctx: the functions context result |
| 10314 | * |
| 10315 | * Get the error and warning callback informations |
| 10316 | * |
| 10317 | * Returns -1 in case of error and 0 otherwise |
| 10318 | */ |
| 10319 | int |
| 10320 | xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt, |
| 10321 | xmlRelaxNGValidityErrorFunc *err, |
| 10322 | xmlRelaxNGValidityWarningFunc *warn, void **ctx) { |
| 10323 | if (ctxt == NULL) |
| 10324 | return(-1); |
| 10325 | if (err != NULL) *err = ctxt->error; |
| 10326 | if (warn != NULL) *warn = ctxt->warning; |
| 10327 | if (ctx != NULL) *ctx = ctxt->userData; |
| 10328 | return(0); |
| 10329 | } |
| 10330 | |
| 10331 | /** |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10332 | * xmlRelaxNGValidateDoc: |
| 10333 | * @ctxt: a Relax-NG validation context |
| 10334 | * @doc: a parsed document tree |
| 10335 | * |
| 10336 | * Validate a document tree in memory. |
| 10337 | * |
| 10338 | * Returns 0 if the document is valid, a positive error code |
| 10339 | * number otherwise and -1 in case of internal or API error. |
| 10340 | */ |
| 10341 | int |
| 10342 | xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) { |
| 10343 | int ret; |
| 10344 | |
| 10345 | if ((ctxt == NULL) || (doc == NULL)) |
| 10346 | return(-1); |
| 10347 | |
| 10348 | ctxt->doc = doc; |
| 10349 | |
| 10350 | ret = xmlRelaxNGValidateDocument(ctxt, doc); |
Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 10351 | /* |
| 10352 | * TODO: build error codes |
| 10353 | */ |
| 10354 | if (ret == -1) |
| 10355 | return(1); |
Daniel Veillard | 6eadf63 | 2003-01-23 18:29:16 +0000 | [diff] [blame] | 10356 | return(ret); |
| 10357 | } |
| 10358 | |
| 10359 | #endif /* LIBXML_SCHEMAS_ENABLED */ |
| 10360 | |